diff options
Diffstat (limited to 'ppp-model.lua')
-rw-r--r-- | ppp-model.lua | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/ppp-model.lua b/ppp-model.lua new file mode 100644 index 0000000..0260533 --- /dev/null +++ b/ppp-model.lua @@ -0,0 +1,108 @@ +module(..., package.seeall) + +-- Load libraries +require("modelfunctions") +require("processinfo") +require("fs") +require("validator") + +-- Set variables +local processname = "pppd" +local packagename = "ppp" +local papfile = "/etc/ppp/pap-secrets" +local chapfile = "/etc/ppp/chap-secrets" +local peerspath = "/etc/ppp/peers/" + +local path = "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin " + +-- ################################################################################ +-- LOCAL FUNCTIONS + +-- ################################################################################ +-- PUBLIC FUNCTIONS + +function getstatus() + local status = {} + + local value, errtxt = processinfo.package_version(packagename) + status.version = cfe({ + label="Program version", + value=value, + errtxt=errtxt, + name=packagename + }) + + status.status = modelfunctions.getenabled(processname) + + return cfe({ type="group", value=status, label="PPP Status" }) +end + +function read_papfiledetails() + return modelfunctions.getfiledetails(papfile) +end + +function update_papfiledetails(filedetails) + return modelfunctions.setfiledetails(filedetails, {papfile}) +end + +function read_chapfiledetails() + return modelfunctions.getfiledetails(chapfile) +end + +function update_chapfiledetails(filedetails) + return modelfunctions.setfiledetails(filedetails, {chapfile}) +end + +function list_peers() + return cfe({ type="list", value=fs.find_files_as_array(nil, peerspath), label="Peers" }) +end + +function get_newpeer() + local newpeer = {} + newpeer.name = cfe({ label="Name" }) + return cfe({ type="group", value=newpeer, label="New Peer" }) +end + +function create_peer(newpeer) + newpeer.errtxt = "Failed to create peer" + local path = newpeer.value.name.value + if not string.find(path, "/") then + path = peerspath .. path + end + + if validator.is_valid_filename(path, peerspath) then + if (posix.stat(path)) then + newpeer.value.name.errtxt = "Peer already exists" + else + fs.create_file(path) + newpeer.errtxt = nil + end + else + newpeer.value.name.errtxt = "Invalid name" + end + + return newpeer +end + +function delete_peer(name) + local retval = cfe({ value="Failed to delete peer", label="Delete Peer Result", errtxt="Peer not found" }) + + for file in fs.find(nil, peerspath) do + if name == file then + os.remove(file) + retval.value = "Peer Deleted" + retval.errtxt = nil + break + end + end + + return retval +end + +function read_peerfile(name) + return modelfunctions.getfiledetails(name, fs.find_files_as_array(nil, peerspath)) +end + +function update_peerfile(filedetails) + return modelfunctions.setfiledetails(filedetails, fs.find_files_as_array(nil, peerspath)) +end |