summaryrefslogtreecommitdiffstats
path: root/shorewall-controller.lua
blob: 0300bd1ea182fec1c2706f184ffe3c5482f7f53f (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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
module(..., package.seeall)

require("controllerfunctions")
--[[
local newrecordtxt = "[New]"

-- ################################################################################
-- LOCAL FUNCTIONS

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
--]]
-- ################################################################################
-- PUBLIC FUNCTIONS

default_action = "status"

function status(self)
	return self.model.getstatus()
end

function details(self)
	return self.model.getstatusdetails()
end

function startstop(self)
	return controllerfunctions.handle_startstop(self, self.model.startstop_service, self.model.getstatus, self.clientdata)
end

function listfiles(self)
	return self.model.getfilelist()
end

function edit(self)
	return controllerfunctions.handle_form(self, function() return self.model.getfiledetails(self.clientdata.filename) end, self.model.updatefiledetails, self.clientdata, "Save", "Edit File", "File Saved", "listfiles")
end

function check(self)
	return self:redirect_to_referrer(self.model.configcheck())
end

function logfile(self)
	return self.model.getlogfile()
end
--[[
function editrecords(self,types,record,errormessage)
	local recorddetails = {}
	local edit = {}
	local config=self.model:getconfig()


	-- Split the record into a table
	local recordtable = {}
	for word in string.gmatch(record, "%S+") do
		table.insert(recordtable, word)
	end

	-- Save info on how the original record looked like
	edit.orgrecord = cfe({ 
		name="orgrecord",
		label="Original record",
		value=record,
		type="hidden",
		})

	-- Specify a actiontype (this is then used when we make changes)
	edit.actiontype = cfe({ 
		name="actiontype",
		label="Type of action [add|delete|modify]",
		type="hidden",
		})
	if (record == newrecordtxt) then
		edit.actiontype.value = "add"
	else
		edit.actiontype.value = "modify"
	end

	if (types == "params") then
		table.insert(edit, cfe({ 
			name=1,
			value=recordtable[1],
			label="Variable name",
			}))
		if (record == newrecordtxt) then
			edit[1]["value"] = "VARIABLE=XXX"
		end
	end

	-- Display save button
	local cmdsave = displaycmdsave()
	cmdsave.errtxt = errormessage

	-- Display delete button
	cmddelete = cfe({ name="cmddelete",
		label="Delete this record",
		value="Delete",
		type="submit",
		})

	if (types == "interfaces") then

		-- Fetch the list of existing interfaces
--		local interfaceslist = {}
--		local interfaces, int_w_loaded, int_m_loaded  = self:new("alpine-baselayout/interfaces")
--		if (int_m_loaded) then
--			interfaceslist = interfaces.worker.read(interfaces)
--		end

		-- Create a cfe-table of the existing records
		local fieldnum = 1
		edit[fieldnum] = cfe({ 
			label="Zone",
			name=fieldnum,
			value=recordtable[fieldnum],
			type="select",
			option={},
			debug=interfaceslist,
			})
		-- IF user creates '[New]' record... then clean up the output.
		if (record == newrecordtxt) then
			edit[fieldnum]["value"] = "-"
		end
		table.insert(edit[fieldnum]["option"], "-")
		for k,v in pairs(config.zones.option or {}) do
			table.insert(edit[fieldnum]["option"], string.match(v, "^%s*(%S*)"))
		end
		-- IF the value is not one of the existing options, then warn and add this option.
		for k,v in pairs(edit[fieldnum]["option"]) do
			edit[fieldnum]["errtxt"] = "'" .. edit[fieldnum]["value"] .. "' is not a valid option!"
			if (tostring(v) == tostring(edit[fieldnum]["value"])) then
				edit[fieldnum]["errtxt"] = nil
				break
			end
		end
		-- Now add this option to the list (just to show what it was)
		if (edit[fieldnum]["errtxt"]) then
			table.insert(edit[fieldnum]["option"], edit[fieldnum]["value"])
		end

		-- Create a cfe-table of the existing records
		local fieldnum = 2
		edit[fieldnum] = cfe({ 
			label="Interfaces",
			name=fieldnum,
			value=recordtable[fieldnum],
--			descr="Available interfaces are: ",
			})

		-- Create a cfe-table of the existing records
		local fieldnum = 3
		edit[fieldnum] = cfe({ 
			label="Broadcast",
			name=fieldnum,
			value=recordtable[fieldnum],
			})

		-- Create a cfe-table of the existing records
		local fieldnum = 4
		edit[fieldnum] = cfe({ 
			label="Broadcast",
			name=fieldnum,
			value=recordtable[fieldnum],
			})

	end

	if (types == "zones") then

		-- Create a cfe-table of the existing records
		local fieldnum = 1
		edit[fieldnum] = cfe({ 
			label="Zone",
			name=fieldnum,
			value=recordtable[fieldnum],
			})

		-- Create a cfe-table of the existing records
		local fieldnum = 2
		edit[fieldnum] = cfe({ 
			label="Type",
			name=fieldnum,
			value=recordtable[fieldnum],
			type="select",
			option={"ipv4", "ipsec", "firewall",}
			})
		-- IF user creates '[New]' record... then clean up the output.
		if (record == newrecordtxt) then
			edit[fieldnum]["value"] = "ipv4"
		end
		-- IF the value is not one of the existing options, then warn and add this option.
		for k,v in pairs(edit[fieldnum]["option"]) do
			edit[fieldnum]["errtxt"] = "'" .. edit[fieldnum]["value"] .. "' is not a valid option!"
			if (tostring(v) == tostring(edit[fieldnum]["value"])) then
				edit[fieldnum]["errtxt"] = nil
				break
			end
		end
		-- Now add this option to the list (just to show what it was)
		if (edit[fieldnum]["errtxt"]) then
			table.insert(edit[fieldnum]["option"], edit[fieldnum]["value"])
		end

		-- Create a cfe-table of the existing records
		local fieldnum = 3
		edit[fieldnum] = cfe({ 
			label="Options",
			name=fieldnum,
			value=recordtable[fieldnum],
			})

		-- Create a cfe-table of the existing records
		local fieldnum = 4
		edit[fieldnum] = cfe({ 
			label="IN Options",
			name=fieldnum,
			value=recordtable[fieldnum],
			})

		-- Create a cfe-table of the existing records
		local fieldnum = 5
		edit[fieldnum] = cfe({ 
			label="OUT Options",
			name=fieldnum,
			value=recordtable[fieldnum],
			})
	end

	if (types == "policy") then

		-- Create a cfe-table of the existing records
		local fieldnum = 1
		edit[fieldnum] = cfe({ 
			label="Source zone",
			name=fieldnum,
			value=recordtable[fieldnum],
			type="select",
			option=self.model.get_defined_zones(),
			})
		-- IF user creates '[New]' record... then clean up the output.
		if (record == newrecordtxt) then
			edit[fieldnum]["value"] = "all"
		end
		table.insert(edit[fieldnum]["option"], "$FW")
		table.insert(edit[fieldnum]["option"], "all")
		-- IF the value is not one of the existing options, then warn and add this option.
		for k,v in pairs(edit[fieldnum]["option"]) do
			edit[fieldnum]["errtxt"] = "'" .. edit[fieldnum]["value"] .. "' is not a valid option!"
			if (tostring(v) == tostring(edit[fieldnum]["value"])) then
				edit[fieldnum]["errtxt"] = nil
				break
			end
		end
		-- Now add this option to the list (just to show what it was)
		if (edit[fieldnum]["errtxt"]) then
			table.insert(edit[fieldnum]["option"], edit[fieldnum]["value"])
		end


		-- Create a cfe-table of the existing records
		local fieldnum = 2
		edit[fieldnum] = cfe({ 
			label="Destination zone",
			name=fieldnum,
			value=recordtable[fieldnum],
			type="select",
			option=self.model.get_defined_zones(),
			})
		-- IF user creates '[New]' record... then clean up the output.
		if (record == newrecordtxt) then
			edit[fieldnum]["value"] = "all"
		end
		table.insert(edit[fieldnum]["option"], "$FW")
		table.insert(edit[fieldnum]["option"], "all")
		-- IF the value is not one of the existing options, then warn and add this option.
		for k,v in pairs(edit[fieldnum]["option"]) do
			edit[fieldnum]["errtxt"] = "'" .. edit[fieldnum]["value"] .. "' is not a valid option!"
			if (tostring(v) == tostring(edit[fieldnum]["value"])) then
				edit[fieldnum]["errtxt"] = nil
				break
			end
		end
		-- Now add this option to the list (just to show what it was)
		if (edit[fieldnum]["errtxt"]) then
			table.insert(edit[fieldnum]["option"], edit[fieldnum]["value"])
		end


		-- Create a cfe-table of the existing records
		local fieldnum = 3
		edit[fieldnum] = cfe({ 
			label="Policy",
			name=fieldnum,
			value=recordtable[fieldnum],
			type="select",
			option={"ACCEPT","DROP","REJECT","CONTINUE","QUEUE","NONE"}
			})
		-- IF user creates '[New]' record... then clean up the output.
		if (record == newrecordtxt) then
			edit[fieldnum]["value"] = "DROP"
		end
		-- IF the value is not one of the existing options, then warn and add this option.
		for k,v in pairs(edit[fieldnum]["option"]) do
			edit[fieldnum]["errtxt"] = "'" .. edit[fieldnum]["value"] .. "' is not a valid option!"
			if (tostring(v) == tostring(edit[fieldnum]["value"])) then
				edit[fieldnum]["errtxt"] = nil
				break
			end
		end
		-- Now add this option to the list (just to show what it was)
		if (edit[fieldnum]["errtxt"]) then
			table.insert(edit[fieldnum]["option"], edit[fieldnum]["value"])
		end

		-- Create a cfe-table of the existing records
		local fieldnum = 4
		edit[fieldnum] = cfe({ 
			label="Log level",
			name=fieldnum,
			value=recordtable[fieldnum],
			})

		-- Create a cfe-table of the existing records
		local fieldnum = 5
		edit[fieldnum] = cfe({ 
			label="Burst:Limit",
			name=fieldnum,
			value=recordtable[fieldnum],
			})
	end

	if (types == "rules") then

		-- Create a cfe-table of the existing records
		local fieldnum = 1
		edit[fieldnum] = cfe({ 
			label="Action",
			name=fieldnum,
			value=recordtable[fieldnum],
			type="select",
			option={
				"ACCEPT", "ACCEPT+", "ACCEPT!",
				"NONAT",
				"DROP", "DROP!",
				"REJECT", "REJECT!",
				"DNAT", "DNAT-",
				"REDIRECT", "REDIRECT-", 
				"CONTINUE", "CONTINUE!",
				"LOG", 
				"QUEUE", "QUEUE!",
				"COMMENT",
				},
			})
		-- IF user creates '[New]' record... then clean up the output.
		if (record == newrecordtxt) then
			edit[fieldnum]["value"] = "DROP"
		end
		-- IF the value is not one of the existing options, then warn and add this option.
		for k,v in pairs(edit[fieldnum]["option"]) do
			edit[fieldnum]["errtxt"] = "Attention! '" .. edit[fieldnum]["value"] .. "' could be a invalid option (or is a action defined in 'actions')."
			if (tostring(v) == tostring(edit[fieldnum]["value"])) then
				edit[fieldnum]["errtxt"] = nil
				break
			end
		end
		-- Now add this option to the list (just to show what it was)
		if (edit[fieldnum]["errtxt"]) then
			table.insert(edit[fieldnum]["option"], edit[fieldnum]["value"])
		end


		-- Create a cfe-table of the existing records
		local fieldnum = 2
		edit[fieldnum] = cfe({ 
			label="Source",
			name=fieldnum,
			value=recordtable[fieldnum],
			})

		-- Create a cfe-table of the existing records
		local fieldnum = 3
		edit[fieldnum] = cfe({ 
			label="Destination",
			name=fieldnum,
			value=recordtable[fieldnum],
			})

		-- Create a cfe-table of the existing records
		local fieldnum = 4
		edit[fieldnum] = cfe({ 
			label="Proto",
			name=fieldnum,
			value=recordtable[fieldnum],
			})

		-- Create a cfe-table of the existing records
		local fieldnum = 5
		edit[fieldnum] = cfe({ 
			label="Destination port",
			name=fieldnum,
			value=recordtable[fieldnum],
			})

		-- Create a cfe-table of the existing records
		local fieldnum = 6
		edit[fieldnum] = cfe({ 
			label="Source port(s)",
			name=fieldnum,
			value=recordtable[fieldnum],
			})

		-- Create a cfe-table of the existing records
		local fieldnum = 7
		edit[fieldnum] = cfe({ 
			label="Original destination",
			name=fieldnum,
			value=recordtable[fieldnum],
			})

		-- Create a cfe-table of the existing records
		local fieldnum = 8
		edit[fieldnum] = cfe({ 
			label="Rate:Limit",
			name=fieldnum,
			value=recordtable[fieldnum],
			})

		-- Create a cfe-table of the existing records
		local fieldnum = 9
		edit[fieldnum] = cfe({ 
			label="User/Group",
			name=fieldnum,
			value=recordtable[fieldnum],
			})

		-- Create a cfe-table of the existing records
		local fieldnum = 9
		edit[fieldnum] = cfe({ 
			label="Mark",
			name=fieldnum,
			value=recordtable[fieldnum],
			})

	end

	-- Add a hidden input where we define which config-file should be modified
	edit.filename = cfe({ 
		label="File to edit",
		name="filename",
		value=types,
		type="hidden",
		})


	return {
		edit=edit,
		option={ script=self.conf.script,
			prefix=self.conf.prefix,
			controller = self.conf.controller,
			action = "config", 
			link = self.conf.script .. self.conf.prefix .. self.conf.controller, },
		cmdsave=cmdsave,
		cmddelete=cmddelete,
		clientdata=clientdata,
		}
end

function config(self)

	-- If we made some changes to a recods... then proceed with the modification
	local savesuccess, configmessage
	if (self.clientdata.cmdsave) or (self.clientdata.cmddelete) then

		-- Check to see that user has entered accepted values
		local inputfields = {}
		local invalidinputfields
		if (self.clientdata) then
			for i=1,#self.clientdata do
				table.insert(inputfields, self.clientdata[i])
				if (self.clientdata[i+1]) and (#self.clientdata[i+1] > 0 ) and (#self.clientdata[i] == 0) then
					configmessage = cfe({errtxt="Skipped fileds should contain '-'"})
				end
			end
		end

		-- What are we going to do with the record...
		local modify_actiontype = self.clientdata.actiontype or "unknown"
		if (self.clientdata.cmddelete) then
			modify_actiontype = "delete"
		end

		

		if not ((configmessage) and (configmessage.errtxt)) then
			--Actually change the values
			savesuccess, configmessage = self.model:modify_config(modify_actiontype, 
									self.clientdata.filename, 
									inputfields, 
									self.clientdata.orgrecord)
		end

	end
	-- If we previously made some changes, report this to user
	if ((self.clientdata.cmdsave) or (self.clientdata.cmddelete)) and not (savesuccess) then
		self.conf.action = "editrecords"
		self.conf.type = "redir"
		return editrecords(self,self.clientdata.filename, self.clientdata.orgrecord,tostring((configmessage.errtxt or "")))
	end

	local config=self.model:getconfig()

	-- Add a [New] record to the options
	table.insert(config.params.option, newrecordtxt)

	-- Add button
	config.params_cmd = cfe ({
		name="params_cmd",
		label="Edit above record",
		value="Edit",
		type="submit",
		})
	config.params_cmd.descr="Mark a item in above list before pressing [" .. config.params_cmd.value .. "]"

	-- Add button
	config.interfaces_cmd = cfe ({
		name="interfaces_cmd",
		label="Edit above record",
		value="Edit",
		type="submit",
		})
	config.interfaces_cmd.descr="Mark a item in above list before pressing [" .. config.interfaces_cmd.value .. "]"

	-- Add button
	config.zones_cmd = cfe ({
		name="zones_cmd",
		label="Edit above record",
		value="Edit",
		type="submit",
		})
	config.zones_cmd.descr="Mark a item in above list before pressing [" .. config.zones_cmd.value .. "]"

	-- Add button
	config.policy_cmd = cfe ({
		name="policy_cmd",
		label="Edit above record",
		value="Edit",
		type="submit",
		})
	config.policy_cmd.descr="Mark a item in above list before pressing [" .. config.policy_cmd.value .. "]"

	-- Add button
	config.rules_cmd = cfe ({
		name="rules_cmd",
		label="Edit above record",
		value="Edit",
		type="submit",
		})
	config.rules_cmd.descr="Mark a item in above list before pressing [" .. config.rules_cmd.value .. "]"

	-- Add button
	config.check_cmd = cfe ({
		name="check_cmd",
		label="Check if config works",
		descr="After your check, you will be able to start/stop/restart the process.",
		errtxt="Attention! This check could take a long time depending on your configuration.",
		value="Check",
		type="submit",
		})

	-- Redirect if button is pressed
	if (self.clientdata.params_cmd) and (self.clientdata.params) then
		self.conf.action = "editrecords"
		self.conf.type = "redir"
		return editrecords(self,"params", self.clientdata.params)
	elseif (self.clientdata.params_cmd) and not (self.clientdata.params) then
		config.params_cmd.errtxt = "You need to specify a record to edit!"
	end

	-- Redirect if button is pressed
	if (self.clientdata.interfaces_cmd) and (self.clientdata.interfaces) then
		self.conf.action = "editrecords"
		self.conf.type = "redir"
		return editrecords(self,"interfaces", self.clientdata.interfaces)
	elseif (self.clientdata.interfaces_cmd) and not (self.clientdata.interfaces) then
		config.interfaces_cmd.errtxt = "You need to specify a record to edit!"
	end

	-- Redirect if button is pressed
	if (self.clientdata.zones_cmd) and (self.clientdata.zones) then
		self.conf.action = "editrecords"
		self.conf.type = "redir"
		return editrecords(self,"zones", self.clientdata.zones)
	elseif (self.clientdata.zones_cmd) and not (self.clientdata.zones) then
		config.zones_cmd.errtxt = "You need to specify a record to edit!"
	end

	-- Redirect if button is pressed
	if (self.clientdata.policy_cmd) and (self.clientdata.policy) then
		self.conf.action = "editrecords"
		self.conf.type = "redir"
		return editrecords(self,"policy", self.clientdata.policy)
	elseif (self.clientdata.policy_cmd) and not (self.clientdata.policy) then
		config.policy_cmd.errtxt = "You need to specify a record to edit!"
	end

	-- Redirect if button is pressed
	if (self.clientdata.rules_cmd) and (self.clientdata.rules) then
		self.conf.action = "editrecords"
		self.conf.type = "redir"
		return editrecords(self,"rules", self.clientdata.rules)
	elseif (self.clientdata.rules_cmd) and not (self.clientdata.rules) then
		config.rules_cmd.errtxt = "You need to specify a record to edit!"
	end

	-- If we previously made some changes, report this to user
	if ((self.clientdata.cmdsave) or (self.clientdata.cmddelete)) and (savesuccess) and (configmessage) and (configmessage.descr) then
		local reporttobutton = self.clientdata.filename .. "_cmd"
		if (config[reporttobutton]) then
			config[reporttobutton]["descr"] = tostring(configmessage.descr)
		end
	end

	return {
		config=config,
		option={ script=self.conf.script,
			prefix=self.conf.prefix,
			controller = self.conf.controller,
			action = "check", 
			link = self.conf.script .. self.conf.prefix .. self.conf.controller, },
		clientdata=clientdata,
		savesuccess=savesuccess, 
		configmessage=configmessage,
		}
end
--]]