summaryrefslogtreecommitdiffstats
path: root/openntpd-controller.lua
blob: 22fb89fd0c71cb02e6ab2f277d281eebee69f6d2 (plain)
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
-- Controller module
module (..., package.seeall)

-- Cause an http redirect to our "read" action
-- We use the self.conf table because it already has prefix,controller,etc
-- The redir code is defined in the application error handler (acf-controller)
require("posix")
require("validator")

local list_redir = function (self)
	self.conf.action = "read"
	self.conf.type = "redir"
	error (self.conf)
end

mvc={}
mvc.on_load = function(self, parent)
	if (self.worker[self.conf.action] == nil ) or ( self.conf.action == "init" ) then
		self.worker[self.conf.action] = list_redir(self)
	end
end

local function getstatus(self)
	local status = self.model.getstatus()
	if (#status.status.value > 0) then
		status.status.value = "Enabled"
	else
		status.status.value = "Disabled"
	end
	return status
end

local function displaycmdmanagement(disablestart,disablestop,disablerestart)
	-- 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",
		})

	-- Disable management buttons based on if the process is running or not
	if (disablestart) then management.start.disabled = "yes" end
	if (disablestop) then management.stop.disabled = "yes" end
	if (disablerestart) then 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

config = function (self)
	local errors = {}
	local modify_opts = nil
	local cmdsavereply = {}
	local cmdsaveresult = {}

	-- Start/Stop/Restart process
	local cmdmanagement
	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)
			}),
		})
		local 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


--[[
	-- SECTION WHERE YOU SAVE NEW SETTINGS
	if ( self.clientdata.hosts_cmd_delete ) then
		if not (self.clientdata.hosts_list) then
			errors["hosts_list"]="You need to choose something in the list to delete"
		end

		if (self.clientdata.hosts_list) then
			modify_opts = self.model:modify_config(string.lower(tostring(self.clientdata.hosts_cmd)), nil, self.clientdata.hosts_list)
		end



	if (string.lower(tostring(self.clientdata.hosts_cmd)) == "add") then
		if (self.clientdata.hosts_add == "") then
			errors["hosts_add"]="You need to enter a server/IP"
		end
		if (self.clientdata.hosts_type == nil) then
			errors["hosts_type"]="You need to choose type of server"
			errors["hosts_add_orgvalue"] = self.clientdata.hosts_add
		end
		if (self.clientdata.hosts_add ~= "") and (self.clientdata.hosts_type ~= nil) then
			modify_opts = self.model:modify_config(string.lower(tostring(self.clientdata.hosts_cmd)), nil, self.clientdata.hosts_type .. " " .. self.clientdata.hosts_add)
		end
	elseif (string.lower(tostring(self.clientdata.listen_cmd)) == "add") then
		if (self.clientdata.listen_add == "") then
			errors["listen_add"]="You need to enter what you want to listen at"
		end
		if (self.clientdata.listen_add ~= "") then
			modify_opts = self.model:modify_config(string.lower(tostring(self.clientdata.listen_cmd)), nil, "listen on " .. self.clientdata.listen_add)
		end
	elseif (string.lower(tostring(self.clientdata.listen_cmd)) == "delete") then
		if not (self.clientdata.listen_list) then
			errors["listen_list"]="You need to choose something in the list to delete"
		end
		if (self.clientdata.listen_list) then
			modify_opts = self.model:modify_config(string.lower(tostring(self.clientdata.listen_cmd)), nil, self.clientdata.listen_list)
		end
--]]

	local url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller
	local status = 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
	local disablestart,disablestop,disablerestart
	-- Disable management buttons based on if the process is running or not
	if (string.lower(status.status.value) == "enabled" ) then
		disablestart = "yes"
	else
		disablestop = "yes"
	end
	-- Disable management buttons if there exist errors in the config
	for k,v in pairs(config) do
		if (config[k]["errtxt"] ~= "") then
			disablestart = "yes"
			disablestop = "yes"
			disablerestart = "yes"
			break
		end
	end
	-- Display management buttons
	local management = displaycmdmanagement(disablestart,disablestop,disablerestart)

	-- 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

	DEBUGMODEL = modify_opts		-- <<< DEBUG INFO >>>
	return ( {status = status, 
		config = config,
		management = management,
		url = url, 
		errors = errors,
		cmdmanagement = cmdmanagement,

		cmdsavereply = cmdsavereply,
		cmdsaveresult = cmdsaveresult,
		modify_opts = modify_opts,
		clientdata = self.clientdata,
		} )
end

logfile = function (self)
	-- Start/Stop/Restart process
	local cmdmanagement
	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)
			}),
		})
		local actionresult, cmdmanagement = self.model:startstop_service( cmdmanagement.action )
	end

	local status = getstatus(self)
	local logfile = self.model:get_logfile()

	-- Management buttons
	local disablestart,disablestop,disablerestart
	-- Disable management buttons based on if the process is running or not
	if (string.lower(status.status.value) == "enabled" ) then
		disablestart = "yes"
	else
		disablestop = "yes"
	end
	-- Display management buttons
	management = displaycmdmanagement(disablestart,disablestop,disablerestart)

	return ({
		management = management,
		cmdmanagement = cmdmanagement,
		status = status, 
		logfile = logfile, 
		url = url,
		})
end

status = function (self)
	local cmd = self.clientdata.cmd
	local url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller
	return ( {status = getstatus(self), url = url } )
end

expert = function (self)
	local modifications = self.clientdata.filecontent or ""
	if ( self.clientdata.cmdsave ) then
		modifications = self.model:update_filecontent(modifications)
	end
	local url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller

	-- Start/Stop/Restart process
	local cmdmanagement
	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)
			}),
		})
		local actionresult, cmdmanagement = self.model:startstop_service( cmdmanagement.action )
	end

	local status = 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
	local disablestart,disablestop,disablerestart
	-- Disable management buttons based on if the process is running or not
	if (string.lower(status.status.value) == "enabled" ) then
		disablestart = "yes"
	else
		disablestop = "yes"
	end
	-- Display management buttons
	management = displaycmdmanagement(disablestart,disablestop,disablerestart)

	return ( {
		status = status,
		file = file, 
		modifications = modifications,
		management = management,
		cmdmanagement = cmdmanagement,
		url = url, } )
end