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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
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 cmd = "/sbin/apk_version -v -s " .. processname .. " | cut -d ' ' -f 1"
local cmd_output = io.popen( cmd )
local cmd_output_result = cmd_output:read("*a") or ""
cmd_output:close()
return cmd_output_result
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, 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
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
-- ################################################################################
-- PUBLIC FUNCTIONS
-- action should be a CFE
function startstop_service ( self, action )
local cmd = action.value
local cmdresult,cmdmessage,cmderror,cmdaction = daemoncontrol.daemoncontrol("ntpd", cmd)
action.descr=cmdmessage
action.errtxt=cmderror
-- Reporting back (true|false, the original acition)
return cmdresult,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
end
function getstatus ()
local path = configfile
local status = {}
local config = getconfig()
if not (fs.is_file(path)) then
local file_result,err = fs.write_file(path, "")
end
status.version = cfe({
name = "version",
label="Program version",
value=get_version(),
})
status.status = cfe({
name="status",
label="Program status",
value=procps.pidof(processname),
})
status.date = config.date
status.setstimeonstartup = config.setstimeonstartup
status.timechanged = config.timechanged
return status
end
function getconfig ()
local path = configfile
local config = {}
local configopts = getopts.getoptsfromfile(confdfile)
configopts["variables"] = config_content(path)
config.setstimeonstartup = cfe({
name = "setstimeonstartup",
label="Sets time directly at startup",
value = "No",
type="checkbox",
})
if (getopts.getoptsfromfile(confdfile,"NTPD_OPTS", "-s")) then
config.setstimeonstartup.value = "Yes"
config.setstimeonstartup.checked = "Yes"
end
config.cmdsavesetstimeonstartup = cfe({
name = "cmdsavesetstimeonstartup",
label="Save the above settings",
value = "Save",
type="submit",
descr="See above checkbox",
})
config.timechanged = cfe({
name = "timechanged",
label="Previous time adjustment",
value = last_time_change(),
})
config.date = cfe({
name = "date",
label="Current time",
value=os.date(),
})
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)
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)
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 = "cat /var/log/messages | grep ntpd"
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,
})
return file
end
function get_filedetails()
local path = configfile
local filedetails = fs.stat(path)
local file = {}
file["filename"] = cfe({
name="filename",
label="File name",
value=path,
})
file["filesize"] = cfe({
name="filesize",
label="File size",
value=filedetails.size or 0,
})
file["mtime"] = cfe({
name="mtime",
label="File date",
value=filedetails.mtime or "---",
})
file["filecontent"] = cfe({
type="longtext",
name="filecontent",
label="File content",
value=fs.read_file(path),
})
return file
end
function update_filecontent (self, modifications)
local path = configfile
local file_result,err = fs.write_file(path, format.dostounix(modifications))
return file_result
end
|