module (..., package.seeall) require("fs") require("posix") -- require("procps") -- require("daemoncontrol") -- require("processinfo") -- Set variables local config_file = "/etc/ssh/sshd_config" local packagename = "openssh" local processname = "sshd" -- valid keywords and default config local default = { Port = 22, ListenAddress = "0.0.0.0", PermitRootLogin = true, PasswordAuthentication = true, UseDNS = true } -- 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" end 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 function read_config() local conf = {} local f = io.open(config_file, "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 write_config(config) local k, v, lines, i,j local conf = {} -- filter out unsupported keys for k,v in pairs(config) do if default[k] ~= nil then conf[k] = v end end lines = fs.read_file_as_array(config_file) 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(config_file)) local f = io.open(config_file, "w") for _,i in ipairs(lines) do f:write(i.."\n") end f:close() end