From: Paul Donald Date: Fri, 22 Nov 2024 19:10:50 +0000 (+0100) Subject: luci-proto-openfortivpn: convert helper to ucode X-Git-Url: http://git.lede-project.org./?a=commitdiff_plain;h=0060e431742bca744bed6960bac302ab995d6660;p=project%2Fluci.git luci-proto-openfortivpn: convert helper to ucode set also dep to luci-base Signed-off-by: Paul Donald --- diff --git a/protocols/luci-proto-openfortivpn/Makefile b/protocols/luci-proto-openfortivpn/Makefile index b4c7795129..45fdf9ddf9 100644 --- a/protocols/luci-proto-openfortivpn/Makefile +++ b/protocols/luci-proto-openfortivpn/Makefile @@ -7,7 +7,7 @@ include $(TOPDIR)/rules.mk LUCI_TITLE:=Support for OpenFortivpn -LUCI_DEPENDS:=+openfortivpn +luci-lua-runtime +LUCI_DEPENDS:=+openfortivpn +luci-base PKG_LICENSE:=Apache-2.0 PKG_MAINTAINER:=Aaron Goodman diff --git a/protocols/luci-proto-openfortivpn/root/usr/libexec/rpcd/luci.openfortivpn b/protocols/luci-proto-openfortivpn/root/usr/libexec/rpcd/luci.openfortivpn deleted file mode 100755 index caca8fcaa5..0000000000 --- a/protocols/luci-proto-openfortivpn/root/usr/libexec/rpcd/luci.openfortivpn +++ /dev/null @@ -1,86 +0,0 @@ -#!/usr/bin/env lua - -local json = require "luci.jsonc" -local fs = require "nixio.fs" - -local function readfile(path) - if fs.stat(path, "type") == "reg" then - local s = fs.readfile(path) - return s and (s:gsub("^%s+", ""):gsub("%s+$", "")) - else - return null - end -end - -local function writefile(path, data) - local n = fs.writefile(path, data) - return (n == #data) -end - -local function parseInput() - local parse = json.new() - local done, err - - while true do - local chunk = io.read(4096) - if not chunk then - break - elseif not done and not err then - done, err = parse:parse(chunk) - end - end - - if not done then - print(json.stringify({ error = err or "Incomplete input" })) - os.exit(1) - end - - return parse:get() -end - -if arg[1] == "list" then - print(json.stringify({ - getCertificates = { - interface = "interface" - }, - setCertificates = { - interface = "interface", - user_cert = "user_cert", - user_key = "user_key", - ca_file = "ca_file" - } - })) -elseif arg[1] == "call" then - local args = parseInput() - - if not args.interface or - type(args.interface) ~= "string" or - not args.interface:match("^[a-zA-Z0-9_]+$") - then - print(json.stringify({ error = "Invalid interface name" })) - os.exit(1) - end - - local user_cert_pem = string.format("/etc/openfortivpn/user-cert-%s.pem", args.interface) - local user_key_pem = string.format("/etc/openfortivpn/user-key-%s.pem", args.interface) - local ca_file_pem = string.format("/etc/openfortivpn/ca-%s.pem", args.interface) - - if arg[2] == "getCertificates" then - print(json.stringify({ - user_cert = readfile(user_cert_pem), - user_key = readfile(user_key_pem), - ca_file = readfile(ca_file_pem) - })) - elseif arg[2] == "setCertificates" then - if args.user_cert then - writefile(user_cert_pem, args.user_cert) - end - if args.user_key then - writefile(user_key_pem, args.user_key) - end - if args.ca_file then - writefile(ca_file_pem, args.ca_file) - end - print(json.stringify({ result = true })) - end -end 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 }; +