summaryrefslogtreecommitdiffstats
path: root/shorewall-model.lua
blob: d438b66a2d287f19434bad2ee1dff586e667f210 (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
module(..., package.seeall)

-- Load libraries
require("modelfunctions")
fs = require("acf.fs")
format = require("acf.format")

-- Set variables
local configfile = "/etc/shorewall/shorewall.conf"
local processname = "shorewall"
local packagename = "shorewall-shell"
local baseurl = "/etc/shorewall/"
--[[
local config = {}

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

local function read_config(file)
	local path = baseurl .. file
	if not (fs.is_file(path)) then
		return {}
	end
	local filecontent = fs.read_file_as_array(path) or {}
	local output = {}
	for k,v in pairs(filecontent) do
		if not string.find ( v, "^[;#].*" ) and not (string.find (v, "^%s*$")) then
			local details = {}
			for v in string.gmatch(v, "%S+") do
				table.insert(details, v)
			end
			table.insert(output, details)
		end
	end
	return output
end

local function addremove_config( addremove, file, value, orgvalue )
	filepath = baseurl .. file
	local cmdoutput

	-- Check if we are about to change a valid filename
	local isvalidfile
	for k,v in pairs(getfilelist()) do
		isvalidfile = true
		if (v.value == filepath) then
			break
		end
		isvalidfile = false
	end
	if not (fs.is_file(filepath)) or not (isvalidfile) then
		return false, cfe({ 
			name="model:addremove_config()", 
			errtxt="'" .. filepath .. "' is not a valid file!",
			})
	end

	if not (type(value) == "table") then
		return false, cfe({ 
			name="model:addremove_config()", 
			errtxt="Value should come as an array!",
			})
	end		

	local filecontentarray = fs.read_file_as_array(filepath) or {}

	if (addremove == "delete" ) then
		local modifyrow
		local orgrecordtable = {}
		for word in string.gmatch(orgvalue, "%S+") do
			table.insert(orgrecordtable, word)
		end
		for i=1, #filecontentarray do
			local recordtable = {}
			for word in string.gmatch(filecontentarray[i], "%S+") do
				table.insert(recordtable, word)
			end

			if (table.concat(recordtable) == table.concat(orgrecordtable)) then
				modifyrow = i
			end
		end

		if (tonumber(modifyrow)) then
			table.remove(filecontentarray, modifyrow)
			fs.write_file(filepath, table.concat(filecontentarray, "\n"))
			return true, cfe({ 
				name="model:addremove_config()", 
				descr="* Record was successfully deleted!",
				})
		else
			return false, cfe({ 
				name="model:addremove_config()", 
				errtxt="Record was not deleted!",
				})
		end

	elseif (addremove == "add" ) then
		--Check if such record already exists
		for k,v in pairs(filecontentarray) do
			if not string.find ( v, "^[;#].*" ) then
				local recordtable = {}
				for word in string.gmatch(v, "%S+") do
					table.insert(recordtable, word)
				end
				if (table.concat(recordtable) == table.concat(value)) then
					return false, cfe({ 
						name="model:addremove_config()", 
						errtxt="The config already holds this kind of config!",
						})
				end
			end
		end

		table.insert(filecontentarray, (#filecontentarray), table.concat(value, "\t"))
		fs.write_file(filepath, table.concat(filecontentarray, "\n"))
		return true, cfe({ 
			name="model:addremove_config()", 
			descr="* Record was successfully added!",
			})

	elseif (addremove == "modify" ) then
		local modifyrow
		local orgrecordtable = {}
		for word in string.gmatch(orgvalue, "%S+") do
			table.insert(orgrecordtable, word)
		end
		for i=1, #filecontentarray do
			local recordtable = {}
			for word in string.gmatch(filecontentarray[i], "%S+") do
				table.insert(recordtable, word)
			end

			if (table.concat(recordtable) == table.concat(orgrecordtable)) then
				modifyrow = i
			end
		end

		if (tonumber(modifyrow)) then
			table.remove(filecontentarray, modifyrow)
			table.insert(filecontentarray, modifyrow, table.concat(value, "\t"))
			fs.write_file(filepath, table.concat(filecontentarray, "\n"))
			return true, cfe({ 
				name="model:addremove_config()", 
				descr="* Record was successfully modified!",
				})
		else
			return false, cfe({ 
				name="model:addremove_config()", 
				errtxt="Record was not modified!",
				})
		end

	else
		return false, cfe({ 
				name="model:addremove_config()", 
				errtxt="Wrong usage of this function! Available options are [add|delete|modify]. You chose '" .. addremove .. "'",
				})
	end

	return false, cfe({ 
			name="model:addremove_config()", 
			errtxt="Something went wrong!",
			debug=value,
			})
end
--]]


-- ################################################################################
-- PUBLIC FUNCTIONS

function getstatus()
	return modelfunctions.getstatus(processname, packagename, "Shorewall Status")
end

function getstatusdetails()
	local f = io.popen("/sbin/shorewall status 2>%1")
	local programstate = f:read("*a") or ""
	f:close()
	return cfe({ value=programstate, label="Shorewall status report" })
end

function get_startstop(self, clientdata)       
        return modelfunctions.get_startstop(processname)
end

function startstop_service(self, startstop, action)        
        return modelfunctions.startstop_service(startstop, action)
end

function getlogfile ()
	local logfilepath = format.parse_ini_file(fs.read_file(configfile) or "","","LOGFILE") or ""
	return cfe({ value=logfilepath, label="Shorewall logfile" })
end

function getfilelist ()
	local listed_files = {}

	if fs.is_dir(baseurl) then
		for name in posix.files(baseurl) do
			if not string.match(name, "^%.") and not string.match(name, "^Makefile") then
				local filedetails = fs.stat(baseurl .. name)
				table.insert ( listed_files , {filename=baseurl..name, mtime=filedetails.mtime, filesize=filedetails.size} )
			end
		end
	end

	table.sort(listed_files, function (a,b) return (a.filename < b.filename) end )

	return cfe({ type="list", value=listed_files, label="Shorewall File List" })
end

local function is_valid_filename(filename)
	local available_files = getfilelist()
	for i,file in ipairs(available_files.value) do
		if file.filename == filename then
			return true
		end
	end
	return false
end

function getfiledetails(filename)
	return modelfunctions.getfiledetails(filename, is_valid_filename)
end

function updatefiledetails(self, filedetails)
	return modelfunctions.setfiledetails(self, filedetails, is_valid_filename)
end

--[[
function modify_config(self, addremove, file, value, orgvalue )
	return addremove_config(addremove, file, value, orgvalue )
end

function getconfig()
	local config = {}

	config.params = cfe({ 
		name = "params",
		label="List of parameters",
		type="select",
		option={},
		 })
	for k,v in pairs(read_config("params")) do
		table.insert(config.params.option, v[1])
	end
	config.params.size=#config.params.option + 1

	config.interfaces = cfe({ 
		name = "interfaces",
		label="List of interfaces",
		type="select",
		option=read_config("interfaces"),
		 })

	config.zones = cfe({ 
		name = "zones",
		label="List of zones",
		type="select",
		option=read_config("zones"),
		 })

	config.policy = cfe({ 
		name = "policy",
		label="List of policy",
		type="select",
		option=read_config("policy"),
		 })

	config.rules = cfe({ 
		name = "rules",
		label="List of rules",
		type="select",
		option=read_config("rules"),
		 })

	config.masq = cfe({ 
		name = "masq",
		label="List of rules",
		type="select",
		option=read_config("masq"),
		 })

	return config

end

function get_defined_zones ()
	local output = {}
	for k,v in pairs(read_config("zones")) do
		table.insert(output, string.match(v, "^%s*(%S*)"))
	end
	return output
end
--]]