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
|
module(..., package.seeall)
-- Load libraries
require("modelfunctions")
require("format")
require("fs")
require("validator")
-- Set variables
local configfile = "/etc/chrony/chrony.conf"
local processname = "chronyd"
local packagename = "chrony"
local keyfile = "/etc/chrony/chrony.keys"
local path = "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin "
-- ################################################################################
-- LOCAL FUNCTIONS
function validate_config(config)
local success = true
for i,val in ipairs(config.value.server.value) do
if string.find(val, "[^%w%s.-]") then
config.value.server.errtxt = "Invalid entry on line "..i
success = false
break
end
end
for i,val in ipairs(config.value.allow.value) do
if string.find(val, "[^%w%s.-/]") then
config.value.allow.errtxt = "Invalid entry on line "..i
success = false
break
end
end
if not validator.is_valid_filename(config.value.driftfile.value) then
config.value.driftfile.errtxt = "Invalid file name"
success = false
end
if not validator.is_valid_filename(config.value.keyfile.value) then
config.value.keyfile.errtxt = "Invalid file name"
success = false
end
if config.value.commandkey.value ~= "" and not validator.is_integer(config.value.commandkey.value) then
config.value.commandkey.errtxt = "Must be an integer"
success = false
end
return success, config
end
local function get_keyfilestatus(filedetails)
filedetails.value.filecontent.descr = "List of password numbers and passwords (ie. '10 cronpass')"
-- check to see if the file is being used
filedetails.value.status = cfe({ value="Key file in use", label="Key file status" })
local config = get_config()
if config.value.keyfile.value ~= keyfile then
filedetails.value.status.value = ""
filedetails.value.status.errtxt = "Key file is not in use"
end
return filedetails
end
-- ################################################################################
-- PUBLIC FUNCTIONS
function startstop_service(action)
local result = modelfunctions.startstop_service(processname, action)
table.insert(result.value.actions.value, "online")
table.insert(result.value.actions.value, "offline")
if action then
local lower = action:lower()
if lower == "online" or lower == "offline" then
result.value.result.value = ""
result.value.result.errtxt = nil
-- try to find the password
local config = get_config()
if config.value.keyfile.value == "" then
result.value.result.errtxt = "No key file defined"
elseif config.value.commandkey.value == "" then
result.value.result.errtxt = "No command key defined"
else
local content = fs.read_file(config.value.keyfile.value) or ""
local password = string.match("\n"..content, "\n"..config.value.commandkey.value.."%s+(%S+)")
if not password then
result.value.result.errtxt = "Could not find password in key file"
else
local cmd = path.."chronyc <<EOF\npassword "..format.escapespecialcharacters(password).."\n"..lower.."\nEOF"
local f = io.popen(cmd)
result.value.result.value = f:read("*a") or ""
f:close()
if (result.value.result.value == "") then result.value.result.errtxt = "Command failed" end
end
end
end
end
return result
end
function getstatus()
return modelfunctions.getstatus(processname, packagename, "Chrony Status")
end
function getdetails()
local details = {}
details.time = cfe({ value=os.date(), label="Current Time" })
details.sources = cfe({ type="longtext", value="Unavailable", label="Sources" })
details.sourcestats = cfe({ type="longtext", value="Unavailable", label="Source Stats" })
details.tracking = cfe({ type="longtext", value="Unavailable", label="Tracking" })
local pid = processinfo.pidof(processname)
if pid and #pid > 0 then
local cmd = path.."chronyc sources"
local f = io.popen(cmd)
details.sources.value = f:read("*a") or ""
f:close()
cmd = path.."chronyc sourcestats"
f = io.popen(cmd)
details.sourcestats.value = f:read("*a") or ""
f:close()
cmd = path.."chronyc tracking"
f = io.popen(cmd)
details.tracking.value = f:read("*a") or ""
f:close()
end
return cfe({ type="group", value=details, label="Chrony Status Details" })
end
function get_config()
local output = {}
output.server = cfe({ type="list", value={}, label="Servers", descr="List of NTP servers by name or IP (ie. 0.pool.ntp.org). If infrequent Internet connection, follow name/IP with 'offline'." })
output.allow = cfe({ type="list", value={}, label="Allow", descr="List of allowed clients by name/subnet/IP or 'all'."})
output.driftfile = cfe({ label="Drift File", descr="Name of drift file (ie. /var/log/chrony/chrony.drift)" })
output.keyfile = cfe({ label="Key File", descr="Name of key file (ie. /etc/chrony/chrony.keys)" })
output.commandkey = cfe({ label="Command Key", descr="Number of key in Key File for commands." })
local config = format.parse_linesandwords(fs.read_file(configfile) or "", "[!;#%%]")
if config then
for i,entry in ipairs(config) do
if output[entry[1]] then
if type(output[entry[1]].value) == "table" then
table.insert(output[entry[1]].value, table.concat(entry, " ", 2))
else
output[entry[1]].value = table.concat(entry, " ", 2)
end
end
end
end
return cfe({ type="group", value=output, label="Chrony Config" })
end
function update_config(config)
local success, config = validate_config(config)
if success then
for name,val in pairs(config.value) do
if type(val.value) == "table" then
if #val.value > 0 then
val.line = name.." "..table.concat(val.value, "\n"..name.." ")
end
else
if val.value ~= "" then
val.line = name .. " " .. val.value
end
end
end
local lines = fs.read_file_as_array(configfile) or {}
local conf = format.parse_linesandwords(lines, "[!;#%%]")
for i,entry in ipairs(conf) do
if config.value[entry[1]] then
if config.value[entry[1]].line then
lines[entry.linenum] = config.value[entry[1]].line
else
lines[entry.linenum] = nil
end
config.value[entry[1]].line = nil
end
end
-- remove the holes in the lines array (sparse array due to removing entries)
local newlines = {}
for i=1,table.maxn(lines) do
table.insert(newlines, lines[i])
end
-- add in missing entries to end
for name,val in pairs(config.value) do
if val.line then
newlines[#newlines+1] = val.line
val.line = nil
end
end
fs.write_file(configfile, table.concat(newlines, "\n"))
else
config.errtxt = "Failed to save config"
end
return config
end
function get_keyfiledetails()
return get_keyfilestatus(modelfunctions.getfiledetails(keyfile))
end
function update_keyfiledetails(filedetails)
return get_keyfilestatus(modelfunctions.setfiledetails(filedetails, {keyfile}))
end
function enable_keyfile()
local result = cfe({ value="Enabled key file", label="Enable Key file result" })
local config = get_config()
config.value.keyfile.value = keyfile
config = update_config(config)
if config.errtxt then
result.value = ""
result.errtxt = {config.errtxt}
for name,val in pairs(config.value) do
if val.errtxt then
table.insert(result.errtxt, name.." - "..val.errtxt)
end
end
result.errtxt = table.concat(result.errtxt, "\n")
end
return result
end
function get_filedetails()
-- FIXME validate
return modelfunctions.getfiledetails(configfile)
end
function update_filedetails(filedetails)
-- FIXME validate
return modelfunctions.setfiledetails(filedetails, {configfile})
end
|