summaryrefslogtreecommitdiffhomepage
path: root/protocols/luci-proto-openconnect/root/usr/share/rpcd/ucode/luci.openconnect
blob: 8e728e6bd00ed262d2dbb50f97fb211118232c19 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env ucode

'use strict';

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

const interfaceregex = /^[a-zA-Z0-9_]+$/;
const user_certificate_string = "/etc/openconnect/user-cert-vpn-%s.pem";
const user_privatekey_string = "/etc/openconnect/user-key-vpn-%s.pem";
const ca_certificate_string = "/etc/openconnect/ca-vpn-%s.pem";


// Utility to read a file
function _readfile(path) {
	let _stat = stat(path);
	if (_stat && _stat.type == "file") {
		let content = readfile(path);
		return content ? trim(content) : 'File empty';
	}
	return 'File not found';
}

// Utility to write a file
function _writefile(path, data) {
	if (!data) {
		return false;
	}
	return writefile(path, data) == length(data);
}

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) {

			const _interface = req.args?.interface;
			if (!_interface || !match(_interface, interfaceregex)) {
				// printf("Invalid interface name");
				return;
			}

			const user_certificate_pem = _readfile(sprintf(user_certificate_string, _interface));
			const user_privatekey_pem = _readfile(sprintf(user_privatekey_string, _interface));
			const ca_certificate_pem = _readfile(sprintf(ca_certificate_string, _interface));

			if(user_certificate_pem && user_privatekey_pem && ca_certificate_pem){
				return {
					user_certificate: user_certificate_pem,
					user_privatekey: user_privatekey_pem,
					ca_certificate: ca_certificate_pem,
				};
			}

		}
	},

	setCertificates: {
		args: {
			interface: "interface",
			user_certificate: "user_certificate",
			user_privatekey: "user_privatekey",
			ca_certificate: "ca_certificate",
		},
		call: function(req) {

			let result = false;
			let _interface = req.args?.interface;

			if (!_interface || !match(_interface, interfaceregex)) {
				// printf("Invalid interface name");
				return;
			}

			/* the interface is set up to call 1 write per certificate,
			with only one of the following arguments not null */
			if (req.args?.user_certificate) {
				result = _writefile(sprintf(user_certificate_string, _interface), req.args?.user_certificate);
			}
			if (req.args?.user_privatekey) {
				result = _writefile(sprintf(user_privatekey_string, _interface), req.args?.user_privatekey);
			}
			if (req.args?.ca_certificate) {
				result = _writefile(sprintf(ca_certificate_string, _interface), req.args?.ca_certificate);
			}

			return {
				result: result,
			};

		}
	}

};

return { 'luci.openconnect': methods };