summaryrefslogtreecommitdiffstats
path: root/openntpd-model.lua
diff options
context:
space:
mode:
authorMika Havela <mika.havela@gmail.com>2007-12-06 16:35:45 +0000
committerMika Havela <mika.havela@gmail.com>2007-12-06 16:35:45 +0000
commit95f699fa380368841157fdd7043ff7730bcae50e (patch)
treee74aa62e9370a88171fcf1dac2f0e8e27dc3aa6d /openntpd-model.lua
parent81c1c354f6c24e0307b31309551d78f5822450d2 (diff)
downloadacf-openntpd-95f699fa380368841157fdd7043ff7730bcae50e.tar.bz2
acf-openntpd-95f699fa380368841157fdd7043ff7730bcae50e.tar.xz
'Set time options' works. You can add/remove a server/servers. You are informed if you miss some parameters.
git-svn-id: svn://svn.alpinelinux.org/acf/openntpd/trunk@416 ab2d0c66-481e-0410-8bed-d214d4d58bed
Diffstat (limited to 'openntpd-model.lua')
-rw-r--r--openntpd-model.lua90
1 files changed, 90 insertions, 0 deletions
diff --git a/openntpd-model.lua b/openntpd-model.lua
index e1bc5d3..454db5d 100644
--- a/openntpd-model.lua
+++ b/openntpd-model.lua
@@ -6,6 +6,7 @@ require("fs")
require("posix")
local ntpdconfig = "/etc/ntpd.conf"
+local ntpdconfd = "/etc/conf.d/ntpd"
local progname = "openntpd"
local function config_content( f )
@@ -85,6 +86,74 @@ local last_time_change = function()
return cmdoutput1 .. cmdoutput2
end
+local get_confdopts = function(search)
+ local opts = {}
+ local searchresult = nil
+ local conf_file = fs.read_file_as_array ( ntpdconfd )
+ for i=1,table.maxn(conf_file) do
+ local l = conf_file[i]
+ -- Filter out commented lines
+ if not string.find ( l, "^[;#].*" ) then
+ local a = string.match ( l, "^%s*(%S+)%=" )
+ if (a) then
+ if not (opts[string.lower(a)]) then
+ opts[string.lower(a)] = {}
+ end
+ local b = string.gsub(string.match ( l, '^%s*%S+%=(.*)' ), '"', '')
+ for i=1,table.maxn(format.string_to_table(" ", b)) do
+ local option = rawget(format.string_to_table(" ", b),i)
+ table.insert (opts[string.lower(a)], i, option)
+ if (option == search) then
+ searchresult = "Yes"
+ end
+ end
+ end
+ end
+ end
+ return opts,searchresult
+end
+
+-- This function needs:
+-- addremove = [add|remove]
+-- file = Path to the file
+-- variable = e.g. "NTPD_OPTS"
+-- option = What value to look for to add or remove from the variable.
+local addremove_opts = function ( addremove, file, variable, option )
+ if (string.lower(addremove) == "remove" ) then
+ cmdtxt = "/bin/sed -i 's/\\(" .. variable .. ".*\\)" .. option .. "/\\1/' " .. file
+ local cmd, error = io.popen ( cmdtxt )
+ local cmdoutput = cmd:read("*a")
+ cmd:close()
+ -- Cleanup the variable by removing unneccesary blanks
+ cmdtxt = "/bin/sed -i 's/\\\"\\ /\\\"/g' " .. file
+ cmdtxt = cmdtxt .. ";/bin/sed -i 's/\\ \\\"/\\\"/g' " .. file
+ local cmd, error = io.popen ( cmdtxt )
+ cmd:close()
+ elseif (string.lower(addremove) == "add" ) then
+ cmdtxt = "/bin/sed -i 's/\\(" .. variable .. ".*\\)\\\"/\\1" .. option .. " \\\"/' " .. file
+ local cmd, error = io.popen ( cmdtxt )
+ local cmdoutput = cmd:read("*a")
+ cmd:close()
+ end
+ return cmdtxt
+end
+local addremove_config = function ( addremove, file, variable )
+ -- Notes on known/unknown bugs: Remove 'server www.test.org' wont work if config has multiple space/tab e.g. 'server www.test.org'
+ -- FIXME: Make num-space unsensetive.
+ -- FIXME: Can hold multiple rows with same values (when deleting... every equal row is deleted)
+ if (string.lower(addremove) == "delete" ) then
+ cmdtxt = "/bin/sed -i '/" .. variable .. "/d' " .. file
+ local cmd, error = io.popen ( cmdtxt )
+ local cmdoutput = cmd:read("*a")
+ cmd:close()
+ elseif (string.lower(addremove) == "add" ) then
+ cmdtxt = "/bin/echo '" .. variable .. "' >> " .. file
+ local cmd, error = io.popen ( cmdtxt )
+ local cmdoutput = cmd:read("*a")
+ cmd:close()
+ end
+ return cmdtxt
+end
-- ################################################################################
-- PUBLIC FUNCTIONS
@@ -102,6 +171,26 @@ function startstop_service ( self, state )
end
+function modify_config (self, addremove, file, variable, opts)
+ if (file == nil) then file = ntpdconfig end
+ -- See to that only *my* configs are modyfied.
+ if (file == ntpdconfig) or (file == ntpdconfd) then
+ return addremove_config(addremove, file, variable)
+ end
+
+end
+
+function modify_opts (self, addremove, file, variable, opts)
+ -- See to that only *my* configs are modyfied.
+ if (file == ntpdconfig) or (file == ntpdconfd) then
+ -- See to that the variable/option only exists once
+ if (addremove == "add") then
+ local remove = addremove_opts( "remove", file, variable, opts )
+ end
+ local remove = addremove_opts( addremove, file, variable, opts )
+ end
+end
+
function get ()
local path = ntpdconfig
local config = {}
@@ -118,6 +207,7 @@ function get ()
config["version"] = string.match(get_version(), "^(%S*)" )
config["date"] = os.date()
config["status"] = startstop
+ config["confd"],config["setstimeonstartup"] = get_confdopts("-s")
config["listenstate"] = server
config["timechanged"] = last_time_change()
config["timezone"] = date.what_tz()