diff options
-rw-r--r-- | openssh-controller.lua | 225 | ||||
-rw-r--r-- | openssh-model.lua | 100 | ||||
-rw-r--r-- | openssh.menu | 2 | ||||
-rw-r--r-- | openssh.roles | 2 |
4 files changed, 155 insertions, 174 deletions
diff --git a/openssh-controller.lua b/openssh-controller.lua index 767c5fd..c5b199a 100644 --- a/openssh-controller.lua +++ b/openssh-controller.lua @@ -1,166 +1,93 @@ -module (..., package.seeall) +module(..., package.seeall) -- Load libraries -require("posix") -require("validator") - --- ################################################################################ --- LOCAL FUNCTIONS - -local function displaycmdmanagement(pidofstatus) - -- Add a management buttons - local management = {} - management.start = cfe({ name="cmdmanagement", - label="Program control-panel", - value="Start", - type="submit", - }) - management.stop = cfe({ name="cmdmanagement", - label="Program control-panel", - value="Stop", - type="submit", - }) - management.restart = cfe({ name="cmdmanagement", - label="Program control-panel", - value="Restart", - type="submit", - }) - -- next CFE can be used to present the result of the previous action - management.actionresult = cfe({ name="actionresult", - label="Previous action result", - }) - - -- Disable management buttons based on if the process is running or not - if (pidofstatus) then - management.start.disabled = "yes" - else - management.stop.disabled = "yes" - management.restart.disabled = "yes" - end - - return management -end - -local function displaycmdsave(self) - -- Add a cmd button to the view - local cmdsave = cfe({ name="cmdsave", - label="Save/Apply above settings", - value="Save", - type="submit", - }) - return cmdsave -end - --- ################################################################################ --- PUBLIC FUNCTIONS - -default_action = "config" - -function config(self) - local errors = {} - local modify_opts = nil - local cmdsavereply = {} - local cmdsaveresult = {} - - -- Start/Stop/Restart process - local cmdmanagement, actionresult - if ( self.clientdata.cmdmanagement) then - cmdmanagement = cfe({ - name="cmdmanagement", - label="Previous action result", - action=cfe({ - name="cmdmanagement", - value=string.lower(self.clientdata.cmdmanagement), -- This row contains start/stop/restart (one of these commands) - }), - }) - actionresult, cmdmanagement = self.model:startstop_service( cmdmanagement.action ) - end - - return ( {status = status, - config = self.model.read_config(), - management = management, - url = url, - errors = errors, - cmdsavereply = cmdsavereply, - cmdsaveresult = cmdsaveresult, - modify_opts = modify_opts, - clientdata = self.clientdata, - } ) -end - -function logfile(self) +require("controllerfunctions") - local status = self.model:getstatus(self) - local logfile = self.model:get_logfile() +local checkboxes = { "PermitRootLogin", "PasswordAuthentication", "UseDNS" } - return ({ - status = status, - logfile = logfile, - url = url, - }) -end +default_action = "status" -function status (self) - local cmd = self.clientdata.cmd - local url = self.conf.script .. self.conf.prefix .. self.conf.controller - return ( {status = "whoops", url = url } ) +function status(self) + return self.model.getstatus() end -function expert (self) - local modifications = self.clientdata.filecontent or "" - if ( self.clientdata.cmdsave ) then - modifications = self.model:update_filecontent(modifications) - end - local url = self.conf.script .. self.conf.prefix .. self.conf.controller - - -- Start/Stop/Restart process - local cmdmanagement, actionresult - if ( self.clientdata.cmdmanagement) then - cmdmanagement = cfe({ - name="cmdmanagement", - label="Previous action result", - action=cfe({ - name="cmdmanagement", - value=string.lower(self.clientdata.cmdmanagement), -- This row contains start/stop/restart (one of these commands) - }), - }) - actionresult, cmdmanagement = self.model:startstop_service( cmdmanagement.action ) +function config(self) + local output = {} + local errtxt = {} + local successfuledit + if (self.clientdata.cmdsave) then + local fields = { "Port", "ListenAddress", "PermitRootLogin", "PasswordAuthentication", "UseDNS", } + local newconfig = {} + for _ , v in pairs(fields) do + newconfig[v] = self.clientdata[v] or "" + end + successfuledit, errtxt = self.model.write_config(newconfig) end - local status = self.model:getstatus(self) - local file = self.model:get_filedetails() - - -- Add buttons - file.cmdsave = cfe ({ + local config = self.model.read_config() or {} + output.cmdsave = cfe({ name="cmdsave", - label="Apply settings", + label="Save above changes", value="Apply", type="submit", - }) - if (self.clientdata.cmdsave) then - file.cmdsave.descr="* Changes has been saved!" - end - - - -- Management buttons (Hide/show buttons - local pidofstatus - if (string.lower(status.status.value) == "enabled" ) then pidofstatus = true end - management = displaycmdmanagement(pidofstatus) - if (actionresult) then - management.actionresult.descr=cmdmanagement.descr - management.actionresult.errtxt=cmdmanagement.errtxt + }) + + output.Port = cfe({ + name="Port", + label="Port", + value=config.Port, + }) + output.ListenAddress = cfe({ + name="ListenAddress", + label="Listen address", + value=config.ListenAddress, + }) + output.PermitRootLogin = cfe({ + name="PermitRootLogin", + label="Permit Root Login", + }) + + output.PasswordAuthentication = cfe({ + name="PasswordAuthentication", + label="Password Authentication", + }) + + output.UseDNS = cfe({ + name="UseDNS", + label="Use DNS", + }) + + -- Display checkboxes checked/unchecked + for k,v in pairs(checkboxes) do + output[v]['value']="yes" + output[v]['type']="checkbox" + if (config[v]) and (string.lower(tostring(config[v] or "")) == "yes") then + output[v]['checked']="yes" + end end - if (status) and (status.version) and (#status.version.value == 0) then - management.start.disabled = "yes" - management.stop.disabled = "yes" - management.restart.disabled = "yes" + + -- Display results of previous save action + if (self.clientdata.cmdsave) and not (successfuledit) then + for k,v in pairs(self.clientdata) do + if (output) and (output[k]) and (k) then + output[k]['value'] = self.clientdata[k] + output[k]['errtxt'] = errtxt[k] + end + end + + output.cmdsave.errtxt = "Save action was canceled because there was invalid input." + elseif (self.clientdata.cmdsave) and (successfuledit) then + output.cmdsave.descr = "* Configuration was succesfully saved" end - return ( { - status = status, - file = file, - modifications = modifications, - management = management, - url = url, } ) + return output +end + +function startstop(self) + return controllerfunctions.handle_startstop(self, self.model.startstop_service, self.model.getstatus, self.clientdata) +end + +function expert(self) + return controllerfunctions.handle_form(self, self.model.getconfigfile, self.model.setconfigfile, self.clientdata, "Save", "Edit Config", "Configuration Saved") end diff --git a/openssh-model.lua b/openssh-model.lua index 2debc11..83fba22 100644 --- a/openssh-model.lua +++ b/openssh-model.lua @@ -1,34 +1,43 @@ -module (..., package.seeall) +module(..., package.seeall) +-- Load libraries +require("modelfunctions") +require("validator") require("fs") require("posix") --- require("procps") --- require("daemoncontrol") --- require("processinfo") +require("getopts") -- Set variables -local config_file = "/etc/ssh/sshd_config" -local packagename = "openssh" +local configfile = "/etc/ssh/sshd_config" local processname = "sshd" +local packagename = "openssh" +local header = "SSH" --- valid keywords and default config local default = { Port = 22, ListenAddress = "0.0.0.0", - PermitRootLogin = true, +-- PermitRootLogin = true, PasswordAuthentication = true, - UseDNS = true + UseDNS = true, } +-- ################################################################################ +-- LOCAL FUNCTIONS --- This function is used to get config_content. -local function process_status_text(procname) - local t = procps.pidof(procname) - if (t) and (#t > 0) then - return "Enabled" - else - return "Disabled" +local function parseconfigfile(file) + file = file or "" + local retval = {} + for line in string.gmatch(file, "([^\n]+)\n?") do + line = string.gsub(line, "#.*$", "") + if line and line ~= "" then + table.insert(retval, {}) + for word in string.gmatch(line, "%S+") do + table.insert(retval[#retval], word) + end + end end + + return retval end -- return "Yes" or "No" on true/false or value as string @@ -43,9 +52,26 @@ local function config_value(value) return tostring(value) end +local function validateconfig(config) + + if config.ListenAddress and not validator.is_ipv4(config.ListenAddress) then + return false, { ['ListenAddress'] = "You entered invalid IP", } + end + + if config.Port and not validator.is_port(config.Port) then + return false, { ['Port'] = "You entered invalid Port", } + end + + return true +end +-- ################################################################################ +-- PUBLIC FUNCTIONS + +-- valid keywords and default config + function read_config() local conf = {} - local f = io.open(config_file, "r") + local f = io.open(configfile, "r") local line, key, _, k, v if not f then @@ -70,18 +96,44 @@ function read_config() return conf end + +function startstop_service(action) + return modelfunctions.startstop_service(processname, action) +end + +function getstatus() + return modelfunctions.getstatus(processname, packagename, header .. " status") +end + +function getconfigfile() + return modelfunctions.getfiledetails(configfile) +end + +function setconfigfile(filedetails) + filedetails.value.filename.value = configfile + return modelfunctions.setfiledetails(filedetails) +end + function write_config(config) local k, v, lines, i,j + local errtxt = {} local conf = {} + local validated, errtxt = validateconfig(config) + if not validated then + return false, errtxt + end + -- filter out unsupported keys - for k,v in pairs(config) do - if default[k] ~= nil then - conf[k] = v + for k,v in pairs(default) do + if (config[k] == nil) or (config[k] == "") then + conf[k] = "no" + else + conf[k] = config[k] end end - lines = fs.read_file_as_array(config_file) + lines = fs.read_file_as_array(configfile) or {} for i, j in ipairs(lines) do for k, v in pairs(conf) do if string.match(j, "^#?"..k.."%s+") then @@ -97,11 +149,13 @@ function write_config(config) end -- write file - posix.mkdir(posix.dirname(config_file)) - local f = io.open(config_file, "w") + posix.mkdir(posix.dirname(configfile)) + local f = io.open(configfile, "w") for _,i in ipairs(lines) do f:write(i.."\n") end f:close() + + return true end diff --git a/openssh.menu b/openssh.menu index f9a010e..324f80a 100644 --- a/openssh.menu +++ b/openssh.menu @@ -2,4 +2,4 @@ Networking 20SSH Status status Networking 20SSH Config config Networking 20SSH Expert expert -Networking 20SSH Logfile logfile +#Networking 20SSH Logfile logfile diff --git a/openssh.roles b/openssh.roles index 38a3c65..534a776 100644 --- a/openssh.roles +++ b/openssh.roles @@ -1,2 +1,2 @@ READ=openssh:status,openssh:logfile -UPDATE=openssh:expert,openssh:config +UPDATE=openssh:expert,openssh:config,openssh:startstop, |