1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
module (..., package.seeall)
-- Load libraries
require("modelfunctions")
require("fs")
require("format")
-- Set variables
local configfile = "/etc/ntpd.conf"
local confdfile = "/etc/conf.d/ntpd"
local packagename = "openntpd"
local processname = "ntpd"
-- ################################################################################
-- LOCAL FUNCTIONS
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
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
end
return success, config
end
-- ################################################################################
-- PUBLIC FUNCTIONS
function startstop_service(action)
return modelfunctions.startstop_service(processname, action)
end
function getstatus()
return modelfunctions.getstatus(processname, packagename, "OpenNTPD Status")
end
function getstatusdetails()
local status = {}
status.timechanged = cfe({ value=last_time_change(), label="Previous time adjustment" })
status.date = cfe({ value=os.date(), label="Current time" })
return cfe({ type="group", value=status, label="OpenNTPD Detailed Status" })
end
function read_config ()
local config = {}
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
local opts = string.sub(format.parse_configfile2(fs.read_file(confdfile), "", "NTPD_OPTS"), 2, -2)
if format.opts_to_table(opts, "-s") then
config.setstimeonstartup.value = true
end
return cfe({ type="group", value=config, label="OpenNTPD Config" })
end
function update_config(config)
local success, config = validate_config(config)
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 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
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
-- 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"))
-- Finally, handle setstimeonstartup
local opts = {}
if config.value.setstimeonstartup.value then opts["-s"] = "" end
fs.write_file(confdfile, format.update_configfile2(fs.read_file(confdfile) or "", "", "NTPD_OPTS", '"'..format.table_to_opts(opts)..'"'))
else
config.errtxt = "Failed to save config"
end
return config
end
function get_filedetails()
return modelfunctions.getfiledetails(configfile)
end
function update_filedetails(filedetails)
filedetails.value.filename.value = configfile
return modelfunctions.setfiledetails(filedetails)
end
|