summaryrefslogtreecommitdiffstats
path: root/protocols/luci-proto-openfortivpn/root/usr/share/rpcd/ucode/luci.openfortivpn
blob: 8588f7453288ff47159b6d7859a8286b73cf3e7f (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_cert_string = "/etc/openfortivpn/user-cert-%s.pem";
const user_key_string = "/etc/openfortivpn/user-key-%s.pem";
const ca_file_string = "/etc/openfortivpn/ca-%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_cert: "user_cert",
					user_key: "user_key",
					ca_file: "ca_file"
				}
			};
		}
	},

	getCertificates: {
		args: {
			interface: "interface",
		},
		call: function(req) {

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

			const user_cert_pem = _readfile(sprintf(user_cert_string, _interface));
			const user_key_pem = _readfile(sprintf(user_key_string, _interface));
			const ca_file_pem = _readfile(sprintf(ca_file_string, _interface));

			if(user_cert_pem && user_key_pem && ca_file_pem){
				return {
					user_cert: user_cert_pem,
					user_key: user_key_pem,
					ca_file: ca_file_pem,
				};
			}

		}
	},

	setCertificates: {
		args: {
			interface: "interface",
			user_cert: "user_cert",
			user_key: "user_key",
			ca_file: "ca_file",
		},
		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_cert) {
				result = _writefile(sprintf(user_cert_string, interface), req.args?.user_cert);
			}
			if (req.args?.user_key) {
				result = _writefile(sprintf(user_key_string, interface), req.args?.user_key);
			}
			if (req.args?.ca_file) {
				result = _writefile(sprintf(ca_file_string, interface), req.args?.ca_file);
			}

			return {
				result: result,
			};

		}
	}

};

return { 'luci.openfortivpn': methods };