diff options
Diffstat (limited to 'freeradius3-model.lua')
-rw-r--r-- | freeradius3-model.lua | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/freeradius3-model.lua b/freeradius3-model.lua new file mode 100644 index 0000000..473219e --- /dev/null +++ b/freeradius3-model.lua @@ -0,0 +1,118 @@ +local mymodule = {} + +-- Load libraries +modelfunctions = require("modelfunctions") +posix = require("posix") +fs = require("acf.fs") +format = require("acf.format") +validator = require("acf.validator") + +-- Set variables +local processname = "radiusd" +local packagename = "freeradius3" +local baseurl = "/etc/raddb" +local owner = "radius" +local group = "root" + +-- ################################################################################ +-- LOCAL FUNCTIONS + +local is_valid_filename = function(filename) + local dirname = posix.dirname(filename) + return validator.is_valid_filename(filename) and string.match(dirname, baseurl) and not string.match(dirname, "%.%.") +end + +-- ################################################################################ +-- PUBLIC FUNCTIONS + +function mymodule.get_status() + return modelfunctions.getstatus(processname, packagename, "Freeradius Status") +end + +function mymodule.get_startstop(self, clientdata) + return modelfunctions.get_startstop(processname) +end + +function mymodule.startstop_service(self, startstop, action) + return modelfunctions.startstop_service(startstop, action) +end + +function mymodule.get_file(self, clientdata) + local filename = clientdata.filename + return modelfunctions.getfiledetails(filename, is_valid_filename) +end + +function mymodule.update_file(self, filedetails) + local ret = modelfunctions.setfiledetails(self, filedetails, is_valid_filename) + if not ret.errtxt then + posix.chmod(filedetails.value.filename.value, "rw-r-----") + posix.chown(filedetails.value.filename.value, posix.getpasswd(owner, "uid") or 0, posix.getpasswd(group, "gid") or 0) + end + return ret +end + +function mymodule.list_files() + local retval = {} + for file in fs.find(null, baseurl) do + local details = fs.stat(file) + if details.type == "regular" then + details.filename = file + table.insert(retval, details) + end + end + table.sort(retval, function(a,b) return a.filename < b.filename end) + return cfe({ type="structure", value=retval, label="List of Freeradius files" }) +end + +function mymodule.getnewfile() + local filename = cfe({ label="File Name", descr="Must be in "..baseurl }) + return cfe({ type="group", value={filename=filename}, label="Freeradius File" }) +end + +function mymodule.createfile(self, filedetails) + local success = true + local path = string.match(filedetails.value.filename.value, "^%s*(.*%S)%s*$") or "" + if not string.find(path, "/") then + path = baseurl.."/"..path + end + + if not is_valid_filename(path) then + success = false + filedetails.value.filename.errtxt = "Invalid filename" + else + if not fs.is_dir(baseurl) then fs.create_directory(baseurl) end + if posix.stat(path) then + success = false + filedetails.value.filename.errtxt = "Filename already exists" + end + end + + if success then + fs.create_file(path) + else + filedetails.errtxt = "Failed to Create File" + end + + return filedetails +end + +function mymodule.getdeletefile(self, clientdata) + local retval = {} + retval.filename = cfe({ label="File Name", value=clientdata.filename or "" }) + return cfe({ type="group", value=retval, label="Delete Freeradius File" }) +end + +function mymodule.deletefile(self, delfile) + delfile.errtxt = "Failed to delete Freeradius File - invalid filename" + for i,file in ipairs(mymodule.list_files().value) do + if delfile.value.filename.value == file.filename then + delfile.errtxt = nil + os.remove(delfile.value.filename.value) + break + end + end + + return delfile +end + +return mymodule |