diff options
author | Paul Donald <newtwen+github@gmail.com> | 2024-11-22 19:50:43 +0100 |
---|---|---|
committer | Paul Donald <newtwen+github@gmail.com> | 2024-11-22 20:26:23 +0100 |
commit | aa955d6465b4d0f00cc713904e2de7bfb0cbd062 (patch) | |
tree | a56d88aae9923a49e900ae20c8c64ef353a558b4 /protocols/luci-proto-openconnect/root/usr/share/rpcd/ucode | |
parent | 0060e431742bca744bed6960bac302ab995d6660 (diff) |
luci-proto-openconnect: 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-openconnect/root/usr/share/rpcd/ucode')
-rw-r--r-- | protocols/luci-proto-openconnect/root/usr/share/rpcd/ucode/luci.openconnect | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/protocols/luci-proto-openconnect/root/usr/share/rpcd/ucode/luci.openconnect b/protocols/luci-proto-openconnect/root/usr/share/rpcd/ucode/luci.openconnect new file mode 100644 index 0000000000..8e728e6bd0 --- /dev/null +++ b/protocols/luci-proto-openconnect/root/usr/share/rpcd/ucode/luci.openconnect @@ -0,0 +1,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 }; + |