summaryrefslogtreecommitdiffhomepage
path: root/protocols/luci-proto-openfortivpn/root/usr/share/rpcd
diff options
context:
space:
mode:
authorPaul Donald <newtwen+github@gmail.com>2024-11-22 20:10:50 +0100
committerPaul Donald <newtwen+github@gmail.com>2024-11-22 20:26:23 +0100
commit0060e431742bca744bed6960bac302ab995d6660 (patch)
tree6d230a7da29fdd250e3c9d6711bb9f7693abec7c /protocols/luci-proto-openfortivpn/root/usr/share/rpcd
parent891e2d803ea92f74b52ca4fba028a8faade97346 (diff)
luci-proto-openfortivpn: convert helper to ucode
set also dep to luci-base Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Diffstat (limited to 'protocols/luci-proto-openfortivpn/root/usr/share/rpcd')
-rw-r--r--protocols/luci-proto-openfortivpn/root/usr/share/rpcd/ucode/luci.openfortivpn115
1 files changed, 115 insertions, 0 deletions
diff --git a/protocols/luci-proto-openfortivpn/root/usr/share/rpcd/ucode/luci.openfortivpn b/protocols/luci-proto-openfortivpn/root/usr/share/rpcd/ucode/luci.openfortivpn
new file mode 100644
index 0000000000..8588f74532
--- /dev/null
+++ b/protocols/luci-proto-openfortivpn/root/usr/share/rpcd/ucode/luci.openfortivpn
@@ -0,0 +1,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 };
+