module(..., package.seeall) -- Load libraries require("modelfunctions") require("validator") require("fs") require("posix") require("getopts") -- Set variables local configfile = "/etc/ssh/sshd_config" local processname = "sshd" local packagename = "openssh" local header = "SSH" local default = { Port = 22, ListenAddress = "0.0.0.0", -- PermitRootLogin = true, PasswordAuthentication = true, UseDNS = true, } -- ################################################################################ -- LOCAL FUNCTIONS 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 local function config_value(value) if type(value) == "boolean" then if value then return "Yes" else return "No" end end 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(configfile, "r") local line, key, _, k, v if not f then return nil end -- clone default conf for k, v in pairs(default) do conf[k] = v end for line in f:lines() do line = string.gsub(line, "#.*", "") for key, _ in pairs(default) do local k,v = string.match(line, "^("..key..")%s+(.*)") if k then conf[k] = v end end end f:close() 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(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(configfile) or {} for i, j in ipairs(lines) do for k, v in pairs(conf) do if string.match(j, "^#?"..k.."%s+") then lines[i] = k .. " " .. config_value(v) conf[k] = nil end end end -- append config opts to end for k,v in pairs(conf) do table.insert(lines, k .. " " .. config_value(v)) end -- write file 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