summaryrefslogtreecommitdiffstats
path: root/protocols/luci-proto-openconnect/root/usr/share/rpcd/ucode/luci.openconnect
blob: 1bc13a17068aaba4f30e58e3c76d9e7ca955c931 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'use strict';

import { readfile, writefile, stat } from 'fs';

const interfaceregex = /^[a-zA-Z0-9_]+$/;
const paths = {
	user_certificate: "/etc/openconnect/user-cert-vpn-%s.pem",
	user_privatekey:  "/etc/openconnect/user-key-vpn-%s.pem",
	ca_certificate:   "/etc/openconnect/ca-vpn-%s.pem"
};

function _readfile(path) {
	let s = stat(path);
	return (s?.type == 'file') ? trim(readfile(path) ?? '') || 'File empty' : null;
}

function _writefile(path, data) {
	return data ? writefile(path, data) == length(data) : false;
}

function is_valid_iface(ifname) {
	return ifname && match(ifname, interfaceregex);
}

const methods = {
	list: {
		call: function() {
			return {
				getCertificates: { interface: "interface" },
				setCertificates: {
					interface: "interface",
					user_certificate: "user_certificate",
					user_privatekey: "user_privatekey",
					ca_certificate: "ca_certificate"
				}
			};
		}
	},

	getCertificates: {
		args: { interface: "interface" },
		call: function(req) {
			let iface = req.args?.interface;
			if (!is_valid_iface(iface)) return;

			let result = {};
			for (let k in paths)
				result[k] = _readfile(sprintf(paths[k], iface));

			return result;
		}
	},

	setCertificates: {
		args: {
			interface: "interface",
			user_certificate: "user_certificate",
			user_privatekey: "user_privatekey",
			ca_certificate: "ca_certificate",
		},
		call: function(req) {
			let iface = req.args?.interface;
			if (!is_valid_iface(iface)) return;

			let result = false;
			for (let k in paths) {
				if (req.args?.[k])
					result = _writefile(sprintf(paths[k], iface), req.args[k]);
			}
			return { result: result };
		}
	}
};

return { 'luci.openconnect': methods };