From f0f92cf5cad70c463ae0d7720f45d39c276c3fc6 Mon Sep 17 00:00:00 2001 From: Mika Havela Date: Fri, 22 Aug 2008 15:51:04 +0000 Subject: Removed code from controller/model and used some already existing lib's. Programmed/activated the statuspage. Programmed/activated the expertpage (linking to already existing code). Configuration page works with some validation (might need some more validation). git-svn-id: svn://svn.alpinelinux.org/acf/openssh/trunk@1400 ab2d0c66-481e-0410-8bed-d214d4d58bed --- openssh-model.lua | 100 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 77 insertions(+), 23 deletions(-) (limited to 'openssh-model.lua') 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 -- cgit v1.2.3