module (..., package.seeall) --require("date") --require("posix") require("format") require("fs") require("procps") require("getopts") require("daemoncontrol") local configfile = "/etc/ntpd.conf" local confdfile = "/etc/conf.d/ntpd" local processname = "openntpd" -- ################################################################################ -- LOCAL FUNCTIONS -- This function is used to get config_content. local function config_content( f ) local config = {} config.name = f local conf_file = fs.read_file_as_array ( config.name ) 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,b,c = string.match ( l, "^%s*(%S+)%s*(%S*)%s*(%S*).*$" ) if (a) then if not (config[string.lower(a)]) then config[string.lower(a)] = {} end if (string.lower(a) == "listen") then table.insert (config[string.lower(a)], {value=c} ) else table.insert (config[string.lower(a)], {value=b} ) end end end end return config end local function get_version () local f,error = io.popen("/sbin/apk_version -v -s openntpd") local programversion = f:read("*a") f:close() return programversion end local function last_time_change() local cmdoutput = {} local cmd, error = io.popen("cat /var/log/messages | grep ntpd | grep adjusting | tail -1" ,r) local cmdoutput1,cmdoutput2 = string.match(cmd:read("*a"), "^%s*(%S+%s+%S+%s+%S+%s+).*: (.*)$") cmd:close() cmdoutput1 = cmdoutput1 or "(Have no data on updates)" cmdoutput2 = cmdoutput2 or "" return cmdoutput1 .. cmdoutput2 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 function addremove_opts( 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 function addremove_config( 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) local cmdoutput = nil if (addremove == "delete" ) then cmdtxt = "/bin/sed -i '/" .. variable .. "/d' " .. file local cmd, error = io.popen ( cmdtxt ) cmdoutput = cmd:read("*a") cmd:close() elseif (addremove == "add" ) then cmdtxt = "/bin/echo '" .. variable .. "' >> " .. file local cmd, error = io.popen ( cmdtxt ) cmdoutput = cmd:read("*a") cmd:close() end if (cmdoutput) then cmdresult = "Config is modified! (" .. cmdtxt .. ")" end return cmdresult end -- ################################################################################ -- PUBLIC FUNCTIONS function startstop_service ( self, state ) return daemoncontrol.daemoncontrol("ntpd", state) end function modify_config (self, addremove, file, variable) if (file == nil) then file = configfile end -- See to that only *my* configs are modyfied. if (file == configfile) or (file == confdfile) 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 == configfile) or (file == confdfile) 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 getstatus () local path = configfile local status = {} if not (fs.is_file(path)) then local file_result,err = fs.write_file(path, "") end status["version"] = string.match(get_version(), "^(%S*)" ) status["date"] = os.date() status["enabled"] = procps.pidof("ntpd") status["setstimeonstartup"] = getopts.getoptsfromfile(confdfile,"NTPD_OPTS", "-s") status["listenstate"] = server status["timechanged"] = last_time_change() return status end function getconfig () local path = configfile local config = {} config = getopts.getoptsfromfile(confdfile) config["variables"] = config_content(path) return config end function get_logfile () local me = {} local cmdtxt = "cat /var/log/messages | grep ntpd" local cmd, error = io.popen(cmdtxt ,r) local cmdoutput = cmd:read("*a") cmd:close() me.value = cmdoutput me.cmd = cmdtxt return me end function get_filedetails() local filedetails = {} local path = configfile filedetails.details = fs.stat(path) filedetails.content = fs.read_file(path) return filedetails end function update_filecontent (self, modifications) local path = configfile local file_result,err = fs.write_file(path, format.dostounix(modifications)) return file_result end