diff options
author | Natanael Copa <natanael.copa@gmail.com> | 2008-06-14 20:55:20 +0000 |
---|---|---|
committer | Natanael Copa <natanael.copa@gmail.com> | 2008-06-14 20:55:20 +0000 |
commit | 9c1669a4343097cbb2453a51ac09b9417ad95f70 (patch) | |
tree | dcf2576cf3463c1d6c0d88b76f73835d3bdf5e44 /openssh-model.lua | |
download | acf-openssh-9c1669a4343097cbb2453a51ac09b9417ad95f70.tar.bz2 acf-openssh-9c1669a4343097cbb2453a51ac09b9417ad95f70.tar.xz |
added initial acf for opensshv0.1
git-svn-id: svn://svn.alpinelinux.org/acf/openssh/trunk@1218 ab2d0c66-481e-0410-8bed-d214d4d58bed
Diffstat (limited to 'openssh-model.lua')
-rw-r--r-- | openssh-model.lua | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/openssh-model.lua b/openssh-model.lua new file mode 100644 index 0000000..2debc11 --- /dev/null +++ b/openssh-model.lua @@ -0,0 +1,107 @@ +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 + |