diff options
author | Ted Trask <ttrask01@yahoo.com> | 2008-09-25 17:27:00 +0000 |
---|---|---|
committer | Ted Trask <ttrask01@yahoo.com> | 2008-09-25 17:27:00 +0000 |
commit | f245e2e14659aa70b4d752df31745b1455cc955a (patch) | |
tree | d70371de44c0a471c2ea18d4e19d276206bc6072 | |
parent | 7211c255e90d9262fe2b4b71bc33fe6857603183 (diff) | |
download | acf-openntpd-f245e2e14659aa70b4d752df31745b1455cc955a.tar.bz2 acf-openntpd-f245e2e14659aa70b4d752df31745b1455cc955a.tar.xz |
Modified openntpd to use new libraries and lsp files.
git-svn-id: svn://svn.alpinelinux.org/acf/openntpd/trunk@1514 ab2d0c66-481e-0410-8bed-d214d4d58bed
-rw-r--r-- | openntpd-config-html.lsp | 70 | ||||
-rw-r--r-- | openntpd-controller.lua | 306 | ||||
-rw-r--r-- | openntpd-details-html.lsp | 23 | ||||
l---------[-rw-r--r--] | openntpd-expert-html.lsp | 59 | ||||
-rw-r--r-- | openntpd-logfile-html.lsp | 38 | ||||
-rw-r--r-- | openntpd-model.lua | 477 | ||||
l--------- | openntpd-startstop-html.lsp | 1 | ||||
l---------[-rw-r--r--] | openntpd-status-html.lsp | 30 | ||||
-rw-r--r-- | openntpd.menu | 2 | ||||
-rw-r--r-- | openntpd.roles | 4 |
10 files changed, 148 insertions, 862 deletions
diff --git a/openntpd-config-html.lsp b/openntpd-config-html.lsp index 97a5a97..a84e5f2 100644 --- a/openntpd-config-html.lsp +++ b/openntpd-config-html.lsp @@ -1,4 +1,4 @@ -<% local form = ... +<% local form, viewlibrary, page_info = ... require("viewfunctions") %> <% @@ -9,63 +9,17 @@ io.write("</span>") --]] %> -<H1>SYSTEM INFO</H1> -<DL> -<% -local myform = form.status -local tags = { "status", "version", "autostart", } -displayinfo(myform,tags,"viewonly") -%> -</DL> - -<form name="cmd" action="" method="POST"> -<h1>CONFIGURATION</h1> -<H2>Advanced config</H2> -<H3>General settings</H3> -<DL> -<% -local myform = form.config -local tags = { "setstimeonstartup", "cmdsavesetstimeonstartup", } -displayinfo(myform,tags) -%> -</DL> - - -<h3>'SET TIME' OPTIONS</h3> -<DL> -<% -local myform = form.config -local tags = { "hosts_list", "hosts_cmd_delete", } -displayinfo(myform,tags) -%> -</DL> - -<DL> -<% -local myform = form.config -local tags = { "hosts_add", "hosts_type", "hosts_cmd_add", } -displayinfo(myform,tags) -%> -</DL> - - -<h3>'PRESENT TIME' OPTIONS (ACT AS TIME SERVER)</h3> -<DL> -<% -local myform = form.config -local tags = { "listen_list", "listen_cmd_delete", "listen_add", "listen_cmd_add", } -displayinfo(myform,tags) -%> -</DL> +<% if viewlibrary and viewlibrary.dispatch_component then +viewlibrary.dispatch_component("status") +end %> +<H1><%= form.label %></H1> <% --- Management buttons -local myform = form.management -local tags = { "start", "stop", "restart" } -if (myform) then - io.write("<H1>MANAGEMENT</H1>\n<DL>") - displaymanagement(myform,tags) - io.write("</DL>") -end + form.action = page_info.script .. page_info.prefix .. page_info.controller .. "/" .. page_info.action + local order = { "setstimeonstartup", "listen", "server", "servers" } + displayform(form, order) %> -</form> + +<% if viewlibrary and viewlibrary.dispatch_component then + viewlibrary.dispatch_component("startstop") +end %> diff --git a/openntpd-controller.lua b/openntpd-controller.lua index a206213..d8fa596 100644 --- a/openntpd-controller.lua +++ b/openntpd-controller.lua @@ -1,55 +1,7 @@ module (..., package.seeall) -- Load libraries -require("posix") -require("validator") - --- ################################################################################ --- LOCAL FUNCTIONS - -local function displaycmdmanagement(pidofstatus) - -- Add a management buttons - local management = {} - management.start = cfe({ name="cmdmanagement", - label="Program control-panel", - value="Start", - type="submit", - }) - management.stop = cfe({ name="cmdmanagement", - label="Program control-panel", - value="Stop", - type="submit", - }) - management.restart = cfe({ name="cmdmanagement", - label="Program control-panel", - value="Restart", - type="submit", - }) - -- next CFE can be used to present the result of the previous action - management.actionresult = cfe({ name="actionresult", - label="Previous action result", - }) - - -- Disable management buttons based on if the process is running or not - if (pidofstatus) then - management.start.disabled = "yes" - else - management.stop.disabled = "yes" - management.restart.disabled = "yes" - end - - return management -end - -local function displaycmdsave(self) - -- Add a cmd button to the view - local cmdsave = cfe({ name="cmdsave", - label="Save/Apply above settings", - value="Save", - type="submit", - }) - return cmdsave -end +require("controllerfunctions") -- ################################################################################ -- PUBLIC FUNCTIONS @@ -57,259 +9,21 @@ end default_action = "status" function config(self) - local errors = {} - local modify_opts = nil - local cmdsavereply = {} - local cmdsaveresult = {} - - -- Start/Stop/Restart process - local cmdmanagement, actionresult - if ( self.clientdata.cmdmanagement) then - cmdmanagement = cfe({ - name="cmdmanagement", - label="Previous action result", - action=cfe({ - name="cmdmanagement", - value=string.lower(self.clientdata.cmdmanagement), -- This row contains start/stop/restart (one of these commands) - }), - }) - actionresult, cmdmanagement = self.model:startstop_service( cmdmanagement.action ) - end - - if (self.clientdata.cmdsavesetstimeonstartup) then - if (self.clientdata.setstimeonstartup) then - modify_opts = self.model:modify_opts("add", "/etc/conf.d/ntpd", "NTPD_OPTS", "-s") - else - modify_opts = self.model:modify_opts("remove", "/etc/conf.d/ntpd", "NTPD_OPTS", "-s") - end - end - - if ( self.clientdata.hosts_cmd_delete) then - local variables="hosts_cmd_delete" - for var in string.gmatch(variables, "%S+") do - -- Send nil instead of "" causes the parameter to be removed/deleted/empty/unset the variable in the config-file --- if (self.clientdata[var] == "") then self.clientdata[var] = nil end - cmdsaveresult[var], cmdsavereply[var] = self.model:modify_config( "delete", - "server%S?" , - string.match(self.clientdata["hosts_list"], "^%s*(%S*)") ) - end - end - - if ( self.clientdata.listen_cmd_delete) then - local variables="listen_cmd_delete" - for var in string.gmatch(variables, "%S+") do - -- Send nil instead of "" causes the parameter to be removed/deleted/empty/unset the variable in the config-file --- if (self.clientdata[var] == "") then self.clientdata[var] = nil end - cmdsaveresult[var], cmdsavereply[var] = self.model:modify_config( "delete", - "listen on" , - string.match(self.clientdata["listen_list"], "^%s*(%S*)") ) - end - end - - if ( self.clientdata.hosts_cmd_add) then - local variables="hosts_cmd_add" - for var in string.gmatch(variables, "%S+") do - local freetocommit - -- Do some checking before executing the command - if not (self.clientdata["hosts_type"]) then - cmdsavereply["hosts_type"] = cfe ({ - errtxt="You need chose type of host to add!", - }) - cmdsavereply["hosts_add"] = cfe ({ - value=self.clientdata["hosts_add"], - }) - end - if (#self.clientdata["hosts_add"] == 0) then - cmdsavereply["hosts_add"] = cfe ({ errtxt="You need to add a valid hostname/IP!", }) - end - - if not ( cmdsavereply["hosts_type"] and cmdsavereply["hosts_type"]["errtxt"]) and not - (cmdsavereply["hosts_add"] and cmdsavereply["hosts_add"]["errtxt"]) then - -- Send nil instead of "" causes the parameter to be removed/deleted/empty/unset the variable in the config-file --- if (self.clientdata[var] == "") then self.clientdata[var] = nil end - cmdsaveresult[var], cmdsavereply[var] = self.model:modify_config( "add", - self.clientdata["hosts_type"] , - self.clientdata["hosts_add"] ) - end - end - end - - if ( self.clientdata.listen_cmd_add) then - local variables="listen_cmd_add" - for var in string.gmatch(variables, "%S+") do - local freetocommit - -- Do some checking before executing the command - if (#self.clientdata["listen_add"] == 0) then - cmdsavereply["listen_add"] = cfe ({ errtxt="You need to add a valid hostname/IP!", }) - end - - if not ( cmdsavereply["listen_add"] and cmdsavereply["listen_add"]["errtxt"]) then - -- Send nil instead of "" causes the parameter to be removed/deleted/empty/unset the variable in the config-file --- if (self.clientdata[var] == "") then self.clientdata[var] = nil end - cmdsaveresult[var], cmdsavereply[var] = self.model:modify_config( "add", - "listen on", - self.clientdata["listen_add"] ) - end - end - end - - local url = self.conf.script .. self.conf.prefix .. self.conf.controller - local status = self.model:getstatus(self) - local config = self.model:getconfig() - - -- Add buttons - config.hosts_cmd_delete = cfe({ - name = "hosts_cmd_delete", - label="Delete selected host", - value="Delete", - type="submit", - }) - config.hosts_cmd_delete.descr="Mark a item in '" .. config.hosts_list.label .. "'-list before pressing [" .. config.hosts_cmd_delete.value .. "]." - if (#config.hosts_list.option == 0) then - config.hosts_cmd_delete.disabled = "yes" - config.hosts_cmd_delete.descr = "" - end - - config.listen_cmd_delete = cfe({ - name = "listen_cmd_delete", - label="Delete selected host", - value="Delete", - type="submit", - }) - config.listen_cmd_delete.descr="Mark a item in '" .. config.listen_list.label .. "'-list before pressing [" .. config.listen_cmd_delete.value .. "]." - if (#config.listen_list.option == 0) then - config.listen_cmd_delete.disabled = "yes" - config.listen_cmd_delete.descr = "" - end - - config.hosts_cmd_add = cfe({ - name = "hosts_cmd_add", - label="Add selected host", - value="Add", - type="submit", - }) - config.hosts_cmd_add.descr="Enter your values in '" .. config.hosts_add.label .. "' and chose '" .. config.hosts_type.label .. "' before pressing [" .. config.hosts_cmd_add.value .. "]." - - config.listen_cmd_add = cfe({ - name = "listen_cmd_add", - label="Add new listen address", - value="Add", - type="submit", - }) - config.listen_cmd_add.descr="Enter your values in '" .. config.listen_add.label .. "' before pressing [" .. config.listen_cmd_add.value .. "]." - - -- Management buttons (Hide/show buttons - local pidofstatus - if (string.lower(status.status.value) == "enabled" ) then pidofstatus = true end - management = displaycmdmanagement(pidofstatus) - if (actionresult) then - management.actionresult.descr=cmdmanagement.descr - management.actionresult.errtxt=cmdmanagement.errtxt - end - if (status) and (status.version) and (#status.version.value == 0) then - management.start.disabled = "yes" - management.stop.disabled = "yes" - management.restart.disabled = "yes" - end - - -- Write out erros and descriptions from previous actions - for k,v in pairs(cmdsavereply) do - if (config[k]) and (cmdsavereply[k]["errtxt"]) and (#cmdsavereply[k]["errtxt"] > 0) then - config[k]["errtxt"] = tostring(cmdsavereply[k]["errtxt"]) - end - if (config[k]) and (cmdsavereply[k]["descr"]) and (#cmdsavereply[k]["descr"] > 0) then - config[k]["descr"] = tostring(cmdsavereply[k]["descr"]) - end - if (config[k]) and (cmdsavereply[k]["value"]) and (#cmdsavereply[k]["value"] > 0) then - config[k]["value"] = tostring(cmdsavereply[k]["value"]) - end - end - - return ( {status = status, - config = config, - management = management, - url = url, - errors = errors, - cmdsavereply = cmdsavereply, - cmdsaveresult = cmdsaveresult, - modify_opts = modify_opts, - clientdata = self.clientdata, - } ) + return controllerfunctions.handle_form(self, self.model.read_config, self.model.update_config, self.clientdata, "Save", "Edit OpenNTPD Config", "OpenNTPD Configuration Saved") end -function logfile(self) - - local status = self.model:getstatus(self) - local logfile = self.model:get_logfile() - - return ({ - status = status, - logfile = logfile, - url = url, - }) +function status (self) + return self.model.getstatus() end -function status (self) - local cmd = self.clientdata.cmd - local url = self.conf.script .. self.conf.prefix .. self.conf.controller - return ( {status = self.model:getstatus(self), url = url } ) +function details (self) + return self.model.getstatusdetails() end function expert (self) - local modifications = self.clientdata.filecontent or "" - if ( self.clientdata.cmdsave ) then - modifications = self.model:update_filecontent(modifications) - end - local url = self.conf.script .. self.conf.prefix .. self.conf.controller - - -- Start/Stop/Restart process - local cmdmanagement, actionresult - if ( self.clientdata.cmdmanagement) then - cmdmanagement = cfe({ - name="cmdmanagement", - label="Previous action result", - action=cfe({ - name="cmdmanagement", - value=string.lower(self.clientdata.cmdmanagement), -- This row contains start/stop/restart (one of these commands) - }), - }) - actionresult, cmdmanagement = self.model:startstop_service( cmdmanagement.action ) - end - - local status = self.model:getstatus(self) - local file = self.model:get_filedetails() - - -- Add buttons - file.cmdsave = cfe ({ - name="cmdsave", - label="Apply settings", - value="Apply", - type="submit", - }) - if (self.clientdata.cmdsave) then - file.cmdsave.descr="* Changes has been saved!" - end - - - -- Management buttons (Hide/show buttons - local pidofstatus - if (string.lower(status.status.value) == "enabled" ) then pidofstatus = true end - management = displaycmdmanagement(pidofstatus) - if (actionresult) then - management.actionresult.descr=cmdmanagement.descr - management.actionresult.errtxt=cmdmanagement.errtxt - end - if (status) and (status.version) and (#status.version.value == 0) then - management.start.disabled = "yes" - management.stop.disabled = "yes" - management.restart.disabled = "yes" - end - - return ( { - status = status, - file = file, - modifications = modifications, - management = management, - url = url, } ) + return controllerfunctions.handle_form(self, self.model.get_filedetails, self.model.update_filedetails, self.clientdata, "Save", "Edit Config", "Configuration Set") end +function startstop(self) + return controllerfunctions.handle_startstop(self, self.model.startstop_service, self.model.getstatus, self.clientdata) +end diff --git a/openntpd-details-html.lsp b/openntpd-details-html.lsp new file mode 100644 index 0000000..0aa9292 --- /dev/null +++ b/openntpd-details-html.lsp @@ -0,0 +1,23 @@ +<% local data, viewlibrary = ... +require("viewfunctions") +%> +<% +--[[ DEBUG INFORMATION +io.write("<H1>DEBUGGING</H1><span style='color:red'><H2>DEBUG INFO: CFE</H2>") +io.write(html.cfe_unpack(data)) +io.write("</span>") +--]] +%> + +<% if viewlibrary and viewlibrary.dispatch_component then + viewlibrary.dispatch_component("status") +end %> + +<H2><%= data.label %></H2> +<DL> +<% +displayitem(data.value.date) +displayitem(data.value.timechanged) +%> +</DL> + diff --git a/openntpd-expert-html.lsp b/openntpd-expert-html.lsp index 78ec15d..207f324 100644..120000 --- a/openntpd-expert-html.lsp +++ b/openntpd-expert-html.lsp @@ -1,58 +1 @@ -<% local form = ... -require("viewfunctions") -%> -<% ---[[ DEBUG INFORMATION -io.write("<H1>DEBUGGING</H1><span style='color:red'><H2>DEBUG INFO: CFE</H2>") -io.write(html.cfe_unpack(form)) -io.write("</span>") ---]] -%> - -<H1>SYSTEM INFO</H1> -<DL> -<% -local myform = form.status -local tags = { "status", "version", "autostart", } -displayinfo(myform,tags,"viewonly") -%> -</DL> - -<form name="myform" action="" method="POST"> -<h1>CONFIGURATION</h1> -<H2>Expert config</H2> -<h3>File details</h3> -<DL> -<% -local myform = form.file -local tags = { "filename", "filesize", "mtime", "sumerrors", } -displayinfo(myform,tags,"viewonly") -%> -</DL> - -<H3>FILE CONTENT</H3> -<% -local myform = form.file -io.write(html.form[myform.filecontent.type](myform.filecontent)) -%> - -<H2>SAVE AND APPLY ABOVE SETTINGS</H2> -<DL> -<% -local tags = { "cmdsave", } -displayinfo(myform,tags) -%> -</DL> - -<% --- Management buttons -local myform = form.management -local tags = { "start", "stop", "restart" } -if (myform) then - io.write("<H1>MANAGEMENT</H1>\n<DL>") - displaymanagement(myform,tags) - io.write("</DL>") -end -%> -</form> - +../expert-html.lsp
\ No newline at end of file diff --git a/openntpd-logfile-html.lsp b/openntpd-logfile-html.lsp index 15daa91..23f732e 100644 --- a/openntpd-logfile-html.lsp +++ b/openntpd-logfile-html.lsp @@ -1,37 +1,7 @@ -<% local form = ... +<% local data, viewlibrary = ... require("viewfunctions") %> -<% ---[[ DEBUG INFORMATION -io.write("<H1>DEBUGGING</H1><span style='color:red'><H2>DEBUG INFO: CFE</H2>") -io.write(html.cfe_unpack(form)) -io.write("</span>") ---]] -%> - -<H1>SYSTEM INFO</H1> -<DL> -<% -local myform = form.status -local tags = { "status", "version", "autostart", } -displayinfo(myform,tags,"viewonly") -%> -</DL> - -<% local myform = form.logfile %> -<form name="myform" action="" method="POST"> -<h1>LOGFILE</h1> -<h2>Details</h2> -<DL> -<% -local tags = { "filename", "filesize", "mtime", "sumerrors", } -displayinfo(myform,tags,"viewonly") -%> -</DL> - -<H3>FILE CONTENT</H3> -<% -io.write(html.form[myform.filecontent.type](myform.filecontent)) -%> -</form> +<% if viewlibrary and viewlibrary.dispatch_component then + viewlibrary.dispatch_component("alpine-baselayout/logfiles/view", {name="/var/log/messages", grep="ntpd"}) +end %> diff --git a/openntpd-model.lua b/openntpd-model.lua index b5405b9..2c77e39 100644 --- a/openntpd-model.lua +++ b/openntpd-model.lua @@ -1,12 +1,9 @@ module (..., package.seeall) -- Load libraries -require("format") +require("modelfunctions") require("fs") -require("procps") require("getopts") -require("daemoncontrol") -require("processinfo") -- Set variables local configfile = "/etc/ntpd.conf" @@ -17,31 +14,6 @@ local processname = "ntpd" -- ################################################################################ -- 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 or {})) 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 last_time_change() local cmdoutput = {} local cmd, error = io.popen("cat /var/log/messages | grep ntpd | grep adjusting | tail -1" ,r) @@ -52,396 +24,133 @@ local function last_time_change() 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, variable, value ) - local file = configfile - local cmdoutput - local filecontentarray = fs.read_file_as_array(file) - if (addremove == "delete" ) then - if not (value) then - return false, cfe({ - name="openntpd.model.addremove_config(delete)", - errtxt="You need to choose a 'Timeserver host' to delete!", - }) - end - - local modifyresult - for k,v in pairs(filecontentarray) do - if (string.find(v,"^%s*" .. variable .. "%s*" .. value .. "%s*$" )) then - modifyresult = k - break - end - end - - if (modifyresult) then - table.remove(filecontentarray,modifyresult) - fs.write_file(file, table.concat(filecontentarray, "\n")) - - return true, cfe({ - name="openntpd.model.addremove_config(delete)", - descr="* Record was successfully deleted!", - }) - end - - -- If nothing happened... then report error! - return false, cfe({ - name="openntpd.model.addremove_config(delete)", - errtxt="Couldn't find the record to delete!", - }) - - elseif (addremove == "add" ) then - if not (value) then - return false, cfe({ - name="openntpd.model.addremove_config(add)", - errtxt="You need to enter a value to add!", - }) - end - if not (variable) then - return false, cfe({ - name="openntpd.model.addremove_config(add)", - errtxt="You need to enter a variable for the value to add!", - }) - end - - local alreadyexists - for k,v in pairs(filecontentarray) do --- if (string.find(v,"^.-%s+" .. value .. "%s*$" )) then - if (string.find(v,"^%s*" .. variable .. "%s*" .. value .. "%s*$" )) then - alreadyexists = k - break +local function validate_config(config) + local success = true + + -- Three of the fields are just lists of IP addresses / hostnames + local tags = {"server", "servers", "listen"} + for i,tag in ipairs(tags) do + local field = config.value[tag] + for j,entry in ipairs(field.value) do + if string.find(entry, "[^%w.-]") then + if not (tag=="listen" and entry=="*") then + field.errtxt = "Invalid IP address/hostname" + success = false + break + end end end - - if (alreadyexists) then - return false, cfe({ - name="openntpd.model.addremove_config(add)", - errtxt="The entered record already exists in the config!", - }) - end - - table.insert(filecontentarray, variable .. " " .. value ) - fs.write_file(file, table.concat(filecontentarray, "\n")) - - return true, cfe({ - name="openntpd.model.addremove_config(add)", - descr="* Record was successfully added!", - }) - else - return false, cfe({ - name="openntpd.model.addremove_config()", - errtxt="Wrong usage of this function!", - }) end - return false, cfe({ - name="openntpd.model.addremove_config()", - errtxt="Something went wrong! You entered '" .. tostring(addremove) .. "' as function.", - }) -end -local function process_status_text(procname) - local t = procps.pidof(procname) - if (t) and (#t > 0) then - return "Enabled" - else - return "Disabled" - end + return success, config end -- ################################################################################ -- PUBLIC FUNCTIONS --- action should be a CFE -function startstop_service ( self, action ) - local cmd = action.value - local cmdresult,cmdmessage,cmderror,cmdaction = daemoncontrol.daemoncontrol(processname, cmd) - action.descr=cmdmessage - action.errtxt=cmderror - -- Reporting back (true|false, the original acition) - return cmdresult,action +function startstop_service(action) + return modelfunctions.startstop_service(processname, action) end - -function modify_config(self, addremove, variable, value) - return addremove_config(addremove, variable, value) -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 +function getstatus() + return modelfunctions.getstatus(processname, packagename, "OpenNTPD Status") end -function getstatus () - local path = configfile +function getstatusdetails() local status = {} - local config = getconfig() - if not (fs.is_file(path)) then - local file_result,err = fs.write_file(path, "") - end - - local value, errtxt = processinfo.package_version(packagename) - status.version = cfe({ name = "version", - label="Program version", - value=value, - errtxt=errtxt, - }) - - status.status = cfe({ name="status", - label="Program status", - value=process_status_text(processname), - }) - - local autostart_sequense, autostart_errtxt = processinfo.process_botsequence(processname) - status.autostart = cfe({ name="autostart", - label="Autostart sequence", - value=autostart_sequense, - errtxt=autostart_errtxt, - }) - status.date = config.date + status.timechanged = cfe({ value=last_time_change(), label="Previous time adjustment" }) + status.date = cfe({ value=os.date(), label="Current time" }) - status.setstimeonstartup = config.setstimeonstartup - - status.timechanged = config.timechanged - - return status + return cfe({ type="group", value=status, label="OpenNTPD Detailed Status" }) end -function getconfig () - local path = configfile +function read_config () local config = {} - local configopts = getopts.getoptsfromfile(confdfile, "") or {} - - configopts["variables"] = config_content(path) + config.server = cfe({ type="list", value={}, label="Single servers", descr="List of server IP addresses/hostnames. OpenNTPD will attempt to synchronize to one resolved address for each hostname entry." }) + config.servers = cfe({ type="list", value={}, label="Multiple servers", descr="List of server IP addresses/hostnames. OpenNTPD will attempt to synchronize to all resolved addresses for each hostname entry." }) + config.listen = cfe({ type="list", value={}, label="Addresses to listen on", descr="List of IP addresses/hostnames to listen on. '*' means listen on all local addresses." }) + config.setstimeonstartup = cfe({ type="boolean", value=false, label="Set time on startup" }) + + local conf = format.parse_linesandwords(fs.read_file(configfile)) + for i,line in ipairs(conf) do + if line[1] == "server" then + table.insert(config.server.value, line[2]) + elseif line[1] == "servers" then + table.insert(config.servers.value, line[2]) + elseif line[1] == "listen" and line[2] == "on" then + table.insert(config.listen.value, line[3]) + end + end - config.setstimeonstartup = cfe({ - name = "setstimeonstartup", - label="Sets time directly at startup", - value = "No", - type="checkbox", - }) - if (getopts.getoptsfromfile(confdfile, "", "NTPD_OPTS", true, "-s")) then - config.setstimeonstartup.value = "Yes" - config.setstimeonstartup.checked = "Yes" + if getopts.getoptsfromfile(confdfile, "", "NTPD_OPTS", true, "-s") then + config.setstimeonstartup.value = true end - config.cmdsavesetstimeonstartup = cfe({ - name = "cmdsavesetstimeonstartup", - label="Save the above settings", - value = "Save", - type="submit", - descr="See above checkbox", - }) + return cfe({ type="group", value=config, label="OpenNTPD Config" }) +end - config.timechanged = cfe({ - name = "timechanged", - label="Previous time adjustment", - value = last_time_change(), - }) +function update_config(config) + local success, config = validate_config(config) - config.date = cfe({ - name = "date", - label="Current time", - value=os.date(), - }) + if success then + local reverseserver = {} for i,val in ipairs(config.value.server.value) do reverseserver[val] = i end + local reverseservers = {} for i,val in ipairs(config.value.servers.value) do reverseservers[val] = i end + local reverselisten = {} for i,val in ipairs(config.value.listen.value) do reverselisten[val] = i end - local options = {} - for k,v in pairs(configopts.variables) do - if (type(v) == "table") and ( (string.lower(k) == "servers") or (string.lower(k) == "server")) then - local srvtype = "" - if (string.lower(k) == "servers") then srvtype = " (pool)" end - for k1, v1 in pairs(v) do - if (v1.value) then - table.insert(options,v1.value..srvtype) + local configcontent = string.gsub(fs.read_file(configfile), "\n+$", "") + local configlines = format.parse_linesandwords(configcontent) + for i=#configlines,1,-1 do + if configlines[i][1] == "server" then + if reverseserver[configlines[i][2]] then + reverseserver[configlines[i][2]] = nil + else + configcontent = format.replace_line(configcontent, configlines[i].linenum) end - end - end - end - config.hosts_list = cfe({ - name = "hosts_list", - label="Timeserver hosts", - type="select", - option=options, - size=#options + 1, - descr="In most cases you could use <i><b>pool.ntp.org</b></i> or <i><b>[countryname].pool.ntp.org</i></b> (if listed in <i><b>http://www.pool.ntp.org/</b></i>)." - }) - if (#options < 2) then - config.hosts_list.size = 2 - end - - config.hosts_add = cfe({ - name = "hosts_add", - label="Host to add", - }) - - local options = { - cfe({value="1", label="Val1",}), - cfe({value="2", label="Val1",}), - } - config.hosts_type = cfe({ - name="hosts_type", - label="Type of server", - value="2", - type="radio", - option={ - cfe({value="servers", label="Server pool",}), - cfe({value="server", label="Single server",}), - }, - }) - - local options = {} - for k,v in pairs(configopts.variables) do - if (type(v) == "table") and (string.lower(k) == "listen") then - for k1, v1 in pairs(v) do - if (v1.value) then - table.insert(options,v1.value) + elseif configlines[i][1] == "servers" then + if reverseservers[configlines[i][2]] then + reverseservers[configlines[i][2]] = nil + else + configcontent = format.replace_line(configcontent, configlines[i].linenum) + end + elseif configlines[i][1] == "listen" and configlines[i][2] == "on" then + if reverselisten[configlines[i][3]] then + reverselisten[configlines[i][3]] = nil + else + configcontent = format.replace_line(configcontent, configlines[i].linenum) end end end - end - config.listen_list = cfe({ - name = "listen_list", - label="Listen on address", - type="select", - option=options, - size=#options + 1, - descr="Empty list = Listening (acting as server) is disabled; '*' = Listen on all local addresses.", - }) - if (#options < 2) then - config.listen_list.size = 2 - end - - config.listen_add = cfe({ - name = "listen_add", - label="New listen address", - }) - - - -- DEBUG INFORMATION - config.debug = cfe({ - name = "debug", - label="Debug information", - value=configopts, - }) - - return config -end - -function get_logfile () - local file = {} - local cmdtxt = "grep ".. processname .. " /var/log/messages" - local cmd, error = io.popen(cmdtxt ,r) - local cmdoutput = cmd:read("*a") - cmd:close() - - file["filename"] = cfe({ - name="filename", - label="File name", - value=cmdtxt, - }) - - file["filecontent"] = cfe({ - type="longtext", - name="filecontent", - label="File content", - value=cmdoutput, - }) + -- Then add the missing ones + lines = {configcontent} + for entry in pairs(reverselisten) do + lines[#lines+1] = "listen on "..entry + end + for entry in pairs(reverseserver) do + lines[#lines+1] = "server "..entry + end + for entry in pairs(reverseservers) do + lines[#lines+1] = "servers "..entry + end + fs.write_file(configfile, table.concat(lines, "\n")) - return file -end -function get_filedetails(self,num) - local path - if (num == "2") then - path = configfile2 + -- Finally, handle setstimeonstartup + local opts = {} + if config.value.setstimeonstartup.value then opts["-s"] = "" end + getopts.setoptsinfile(confdfile, "", "NTPD_OPTS", '"'..getopts.table_to_opts(opts)..'"') else - path = configfile + config.errtxt = "Failed to save config" end - local file = {} - local filedetails = {} - local config = {} - local filenameerrtxt - if (path) and (fs.is_file(path)) then - filedetails = fs.stat(path) - config = getconfig(path) - else - config = {} - config.filename = {} - config["filename"]["errtxt"]="Config file '".. path .. "' is missing!" - end - - file["filename" .. (num or "")] = cfe({ - name="filename" .. (num or ""), - label="File name", - value=path, - errtxt=filenameerrtxt - }) - file["filesize" .. (num or "")] = cfe({ - name="filesize" .. (num or ""), - label="File size", - value=filedetails.size or 0, - }) - file["mtime" .. (num or "")] = cfe({ - name="mtime" .. (num or ""), - label="File date", - value=filedetails.mtime or "---", - }) - file["filecontent" .. (num or "")] = cfe({ - type="longtext", - name="filecontent" .. (num or ""), - label="File content", - value=fs.read_file(path), - }) - -- Sum all errors into one cfe - local sumerrors = "" - for k,v in pairs(config) do - if (config[k]) and (config[k]["errtxt"]) and (config[k]["errtxt"] ~= "") then - sumerrors = sumerrors .. config[k]["errtxt"] .. "\n" - end - end - if (sumerrors ~= "") then - file["sumerrors" .. (num or "")] = cfe ({ - name="sumerrors" .. (num or ""), - label = "Configuration errors", - errtxt = string.match(sumerrors, "(.-)\n$"), - }) - end + return config +end - return file +function get_filedetails() + return modelfunctions.getfiledetails(configfile) end -function update_filecontent (self, modifications) - local path = configfile - local file_result,err = fs.write_file(path, format.dostounix(modifications)) - return file_result +function update_filedetails(filedetails) + filedetails.value.filename.value = configfile + return modelfunctions.setfiledetails(filedetails) end diff --git a/openntpd-startstop-html.lsp b/openntpd-startstop-html.lsp new file mode 120000 index 0000000..0ea2627 --- /dev/null +++ b/openntpd-startstop-html.lsp @@ -0,0 +1 @@ +../startstop-html.lsp
\ No newline at end of file diff --git a/openntpd-status-html.lsp b/openntpd-status-html.lsp index c4c709d..b2f8480 100644..120000 --- a/openntpd-status-html.lsp +++ b/openntpd-status-html.lsp @@ -1,29 +1 @@ -<% local form = ... -require("viewfunctions") -%> -<% ---[[ DEBUG INFORMATION -io.write("<H1>DEBUGGING</H1><span style='color:red'><H2>DEBUG INFO: CFE</H2>") -io.write(html.cfe_unpack(form)) -io.write("</span>") ---]] -%> - -<H1>SYSTEM INFO</H1> -<DL> -<% -local myform = form.status -local tags = { "status", "version", "autostart", } -displayinfo(myform,tags,"viewonly") -%> -</DL> - -<H2>PROGRAM SPECIFIC OPTIONS/INFORMATION</H2> -<DL> -<% -local myform = form.status -local tags = { "setstimeonstartup", "date", "timechanged", } -displayinfo(myform,tags,"viewonly") -%> -</DL> - +../status-html.lsp
\ No newline at end of file diff --git a/openntpd.menu b/openntpd.menu index 0ac48df..bf0ebd6 100644 --- a/openntpd.menu +++ b/openntpd.menu @@ -1,5 +1,5 @@ #CAT GROUP/DESC TAB ACTION -Networking 20NTP(openntpd) Status status +Networking 20NTP(openntpd) Status details Networking 20NTP(openntpd) Config config Networking 20NTP(openntpd) Expert expert Networking 20NTP(openntpd) Logfile logfile diff --git a/openntpd.roles b/openntpd.roles index 37eb9a2..b37a1e4 100644 --- a/openntpd.roles +++ b/openntpd.roles @@ -1,2 +1,2 @@ -READ=openntpd:status,openntpd:logfile -UPDATE=openntpd:expert,openntpd:config +READ=openntpd:status,openntpd:logfile,openntpd:details +UPDATE=openntpd:expert,openntpd:config,openntpd:startstop |