summaryrefslogtreecommitdiffstats
path: root/dansguardian-model.lua
blob: 1abec68729af98e62ccbefff6103ed2b6764a0be (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
-- acf model for squid
-- Copyright(c) 2007 A. Brodmann - Licensed under terms of GPL2
module (..., package.seeall)

require "posix"
require "format"

dansguardiancfg = "/etc/dansguardian/dansguardian.conf"
dansguardiancfg2 = "/etc/dansguardian/dansguardianf1.conf"

get_status = function()

	local retval = "stopped"
	
	local ptr = io.popen( "/bin/pidof dansguardian" )
	local pid = ptr:read( "*a" )
	ptr:close()
	if pid ~= nil then
		if #pid > 1 then
			retval = "running"
		end
	end
	
	return retval
end

service_control = function( control )

	local retval = ""
	
	local ptr = io.popen( "/etc/init.d/dansguardian " .. control, "r" )
	if ptr ~= nil then
		local retmsg = ptr:read( "*a" )
		ptr:close()
		if retmsg ~= nil then
			retval = retmsg
		else
			retval = "service_control(): Failed to read output from initscript!\n"
		end
	else
		retval = "service_control(): Failed to start/stop/restart service!\n"
	end

	return retval
end

get_dansguardian_version = function()

	local retval = ""
	
	local ptr = io.popen( "/usr/sbin/dansguardian -v" )
	if ptr ~= nil then
		retval = ptr:read( "*l" )
		ptr:close()
	else
		retval = "Error - Failed to program version"
	end
	
	return retval
end

get_general_config = function()

	local retval = {}
	local error = ""
	
	retval = { filterip = { label="Filter IP", type="text", value="" },
	           filterport = { label="Filter Port", type="text", value="" },
	           proxyip = { label="Proxy IP", type="text", value="" },
	           proxyport = { label="Proxy Port", type="text", value="" },
	           accessdeniedaddress = { label="AccessDeniedAddress", type="text", value="" },
	           naughtynesslimit = { label="NaughtynessLimit", type="text", value="" }
	         }
	         
	local fptr = io.open( dansguardiancfg, "r" )
	if fptr ~= nil then
		local line = fptr:read( "*l" )
		while line ~= nil do
			if string.sub( line, 1, 1 ) ~= "#" then
				if string.sub( line, 1, 8 ) == "filterip" then
					retval.filterip.value = get_cfg_value( line )
				elseif string.sub( line, 1, 10 ) == "filterport" then
					retval.filterport.value = get_cfg_value( line )
				elseif string.sub( line, 1, 7 ) == "proxyip" then
					retval.proxyip.value = get_cfg_value( line )
				elseif string.sub( line, 1, 9 ) == "proxyport" then
					retval.proxyport.value = get_cfg_value( line )
				elseif string.sub( line, 1, 19 ) == "accessdeniedaddress" then
					retval.accessdeniedaddress.value = get_cfg_value( line )
				end
			end
			line = fptr:read( "*l" )   -- read one config file
		end
		fptr:close()
	else
		error = "Failed to open " .. dansguardiancfg .. " file!"
	end
	
	local fptr2 = io.open( dansguardiancfg2, "r" )
	if fptr2 ~= nil then
		local line = fptr2:read( "*l" )
		while line ~= nil do
			if string.sub( line, 1, 1 ) ~= "#" then
				if string.sub( line, 1, 16 ) == "naughtynesslimit" then
					retval.naughtynesslimit.value = get_cfg_value( line )
				end
			end
			line = fptr2:read( "*l" ) -- read one config file line
		end
		fptr2:close()
	else
		error = "Failed to open " .. dansguardiancfg2 .. " file!"
	end
	           
	return retval, error
end

get_plain_config = function()

	local retval = ""
	local error = ""
	
	local fptr = io.open( dansguardiancfg, "r" )
	if fptr ~= nil then
		retval = fptr:read( "*a" )
		fptr:close()
		if retval == nil then
			retval = ""
			error = "Failed to read " .. dansguardiancfg .. " file!"
		end
	else
		error = "Failed to open " .. dansguardiancfg .. " file!"
	end
	
	return retval, error
end

get_edit_config = function( name )

	local retval = ""
	local error = ""
	
	if not is_valid_configfile( name ) then
		return "", "Hacker"
	end
	
	local fptr = io.open( "/etc/dansguardian/" .. name )
	if fptr ~= nil then
		retval = fptr:read( "*a" )
		fptr:close()
		if retval == nil then
			retval = ""
			error = "Failed to read /etc/dansguardian/" .. name .. " file!"
		end
	else 
		error = "Failed to open /etc/dansguardian/" .. name .. " file!"
	end
	
	return retval, error
end

update_edit_config = function( name, config )

	local retval = ""
	
	if not is_valid_configfile( name ) then
		return "", "Hacker"
	end
	
	local fptr = io.open( "/etc/dansguardian/" .. name, "wb+" )
	if fptr ~= nil then
		fptr:write( format.dostounix( config ) )
		fptr:close()
		retval = ""
	else
		error = "Failed to open /etc/dansguardian/" .. name .. " file!"
	end
	
	return retval
end

update_general_config = function( config )

	local retval = ""
	local tmpfilename = os.tmpname()
	local tmpfile = -1
	local cfgptr = -1
	local line = ""

	tmpfile = io.open( tmpfilename, "wb+" )
	if tmpfile == nil then
		return "Failed to create temporary config file!"
	end	
	
	cfgptr = io.open( dansguardiancfg, "r" )
	if cfgptr == nil then
		tmpfile:close()
		os.remove( tmpfilename )
		return "Failed to open " .. dansguardiancfg .. "!"
	end
	
	line = cfgptr:read( "*l" )
	while line ~= nil do
		if string.sub( line, 1, 8 ) == "filterip" then
			tmpfile:write( "filterip = " .. config.filterip .. "\n" )
		elseif string.sub( line, 1, 10 ) == "filterport" then
			tmpfile:write( "filterport = " .. config.filterport .. "\n" )
		elseif string.sub( line, 1, 7 ) == "proxyip" then
			tmpfile:write( "proxyip = " .. config.proxyip .. "\n" )
		elseif string.sub( line, 1, 9 ) == "proxyport" then
			tmpfile:write( "proxyport = " .. config.proxyport .. "\n" )
		elseif string.sub( line, 1, 19 ) == "accessdeniedaddress" then
			tmpfile:write( "accessdeniedaddress = " .. config.accessdeniedaddress .. "\n" )
		else
			tmpfile:write( line .. "\n" )
		end
		line = cfgptr:read( "*l" )
	end
	
	tmpfile:close()
	cfgptr:close()
	os.rename( tmpfilename, dansguardiancfg )
	
	--- step 2 - dansguardiancfg2
	
	tmpfile = io.open( tmpfilename, "wb+" )
	if tmpfile == nil then
		return "Failed to create temporary config file!"
	end
	
	cfgptr = io.open( dansguardiancfg2, "r" )
	if cfgptr == nil then
		tmpfile:close()
		os.remove( tmpfilename )
		return "Failed to open " .. dansguardiancfg2 .. "!"
	end
	
	line = cfgptr:read( "*l" )
	while line ~= nil do
		if string.sub( line, 1, 16 ) == "naughtynesslimit" then
			tmpfile:write( "naughtynesslimit = " .. config.naughtynesslimit .. "\n" )
		else
			tmpfile:write( line .. "\n" )
		end
		line = cfgptr:read( "*l" )
	end
	
	tmpfile:close()
	cfgptr:close()
	os.rename( tmpfilename, dansguardiancfg2 )
	
	return retval
end

update_plain_config = function( config )

	local retval = ""
	local cfgptr = -1
	local error = ""
	
	cfgptr = io.open( dansguardiancfg, "wb+" )
	if cfgptr ~= nil then
		cfgptr:write( config )
		cfgptr:close()
	else
		retval = "Failed to open " .. dansguardiancfg .. " file!"
	end
	
	return retval
end

get_cfg_value = function( str )

	local retval = ""
	local pos = 1
	local found = false
	local found2 = false
	
	while not found and pos < #str -1 do
		if string.sub( str, pos, pos ) == "=" then
		  found = true
		end
		pos = pos + 1
	end
	
	if found then
		pos = pos - 1
		while not found2 and pos < #str -1 do
			if string.sub( str, pos+1, pos+1 ) ~= " " then
				found2 = true
			end
			pos = pos + 1
		end
	end
	
	if found2 then
		retval = string.sub( str, pos )
	end
	
	return retval
end

get_advanced_config = function()

	local retval = { files = {} }
	local errmsg = ""
	
	get_file_tree( retval.files, "/etc/dansguardian", "" )
	
	return retval, errmsg
end

get_file_tree = function( treetable, dir, prefix ) 

	local entries = posix.dir( dir )
	local k = ""
	local v = ""
	for k,v in ipairs( entries ) do
		local attrs = posix.stat( dir .. "/" .. v )
		if attrs.type == "regular" and string.sub( v, -4) ~= ".gif" then
			table.insert( treetable, prefix .. v )
		end
	end
	
	entries = posix.dir( dir )
	for k,v in ipairs( entries ) do
		local attrs = posix.stat( dir .. "/" .. v )
		if attrs.type == "directory" and v~= "." and v~= ".." then
			get_file_tree( treetable, dir .. "/" .. v, prefix .. v .. "/" )
		end
	end

	return
end

is_valid_configfile = function( name )

	local retval = false
	local ftable = {}
	local k
	local v
	
	
	get_file_tree( ftable, "/etc/dansguardian", "" )
	
	for k,v in ipairs( ftable ) do
		if v == name then
			retval = true
		end
	end
	
	return retval
end