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

-- Load libraries
require("modelfunctions")
--require "posix"
require("fs")
require("format")
require("validator")

-- Set variables
dansguardiancfg = "/etc/dansguardian/dansguardian.conf"
dansguardiancfg2 = "/etc/dansguardian/dansguardianf1.conf"
local processname = "dansguardian"
local packagename = "dansguardian"
local baseurl = "/etc/dansguardian"

--[[
local categoryfiles = {
	['weighted'] = tostring(baseurl .. "/weightedphraselist"),
	['banned'] = tostring(baseurl .. "/bannedphraselist"),
	['exception'] = tostring(baseurl .. "/exceptionphraselist"),
	}

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

local function get_includes_from_file(file)
	local retval = {}
	for k,v in pairs(fs.read_file_as_array(file) or {}) do
		if (string.match(v, '^%s*.Include')) then
			local val = string.match(v,'^%s*.Include<(.*)>%s*')
			retval[val] = true
		end
	end
	return retval
end

local categoryfilecontent = {
	['weighted'] = get_includes_from_file(categoryfiles['weighted']),
	['banned'] = get_includes_from_file(categoryfiles['banned']),
	['exception'] = get_includes_from_file(categoryfiles['exception']),
	}
--]]

local validate_general_config = function( config )
	local success = true
	if config.value.filterip.value ~= "" and not validator.is_ipv4(config.value.filterip.value) then
		config.value.filterip.errtxt = "Invalid IP address"
		success = false
	end
	if not validator.is_port(config.value.filterport.value) then
		config.value.filterport.errtxt = "Invalid port"
		success = false
	end
	if not validator.is_ipv4(config.value.proxyip.value) then
		config.value.proxyip.errtxt = "Invalid IP address"
		success = false
	end
	if not validator.is_port(config.value.proxyport.value) then
		config.value.proxyport.errtxt = "Invalid port"
		success = false
	end
	-- FIXME don't know how to validate accessdeniedaddress
	if not validator.is_integer(config.value.naughtynesslimit.value) then
		config.value.naughtynesslimit.errtxt = "Invalid number"
		success = false
	end

	return success, config
end

local is_valid_filename = function(filename)
	local dirname = dirname(filename)
	return validator.is_valid_filename(filename) and string.match(dirname, baseurl) and not string.match(dirname, "%.%.")
end

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

get_status = function()
	return modelfunctions.getstatus(processname, packagename, "Dans Guardian Status")
end

startstop_service = function( action )
	return modelfunctions.startstop_service(processname, action)
end

read_general_config = function()
	local retval = { filterip = cfe({ label="Filter IP", descr="Leave blank to listen on all IPs" }),
		filterport = cfe({ label="Filter Port" }),
		proxyip = cfe({ label="Proxy IP" }),
		proxyport = cfe({ label="Proxy Port" }),
		accessdeniedaddress = cfe({ label="AccessDeniedAddress" }),
		naughtynesslimit = cfe({ label="NaughtynessLimit" })
	}

	local config = format.parse_ini_file(fs.read_file(dansguardiancfg) or "", "")
	if config then
		if config.filterip then	retval.filterip.value = config.filterip end
		if config.filterport then retval.filterport.value = config.filterport end
		if config.proxyip then retval.proxyip.value = config.proxyip end
		if config.proxyport then retval.proxyport.value = config.proxyport end
		if config.accessdeniedaddress then retval.accessdeniedaddress.value = string.sub(config.accessdeniedaddress, 2, -2) end
	end
	
	config = format.parse_ini_file(fs.read_file(dansguardiancfg2) or "", "")
	if config then
		if config.naughtynesslimit then retval.naughtynesslimit.value = config.naughtynesslimit end
	end
	           
	return cfe({ type="group", value=retval, label="Dansguardian General Config" })
end

update_general_config = function( config )
	local success, config = validate_general_config(config)

	if success then
		local text = fs.read_file(dansguardiancfg) or ""
		
		text = format.update_ini_file(text, "", "filterip", config.value.filterip.value)
		text = format.update_ini_file(text, "", "filterport", config.value.filterport.value)
		text = format.update_ini_file(text, "", "proxyip", config.value.proxyip.value)
		text = format.update_ini_file(text, "", "proxyport", config.value.proxyport.value)
		text = format.update_ini_file(text, "", "accessdeniedaddress", "'"..config.value.accessdeniedaddress.value.."'")
		fs.write_file(dansguardiancfg, text)

		text = fs.read_file(dansguardiancfg2) or ""
		text = format.update_ini_file(text, "", "naughtynesslimit", config.value.naughtynesslimit.value)
		fs.write_file(dansguardiancfg2, text)
	else
		config.errtxt = "Failed to set config"
	end
	
	return config
end

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

update_file = function(filedetails)
	return modelfunctions.setfiledetails(filedetails, is_valid_filename)
end

list_files = function()
	local retval = {}
	for file in fs.find(null, baseurl) do
		local details = fs.stat(file)
		if details.type == "regular" and not string.match(file, "logrotation$") and not string.match(file, "%.conf$") and not string.match(file, "%.gif$") then
			details.filename = file
			table.insert(retval, details)
		end
	end
	table.sort(retval, function(a,b) return (a.filename < b.filename) end)
	return cfe({ type="structure", value=retval, label="List of Dansguardian files" })
end

list_config_files = function()
	local list = {}
	local details = fs.stat(dansguardiancfg) or {}
	list[1] = { filename=dansguardiancfg, size = details.size or 0, mtime = details.mtime or "---" }
	details = fs.stat(dansguardiancfg2) or {}
	list[2] = { filename=dansguardiancfg2, size = details.size or 0, mtime = details.mtime or "---" }
	return cfe({ type="list", value=list, label="List of Dansguardian config files" })
end

--[[
get_categories = function()

	local retval = {}
	local entries = posix.dir( baseurl .. "/phraselists" )
	
	for k,v in ipairs( entries ) do
		local attrs = posix.stat( baseurl .. "/phraselists/" .. v )

		if attrs.type == "directory" and v ~= "." and v ~= ".." then
			local entrycontent = {}
			local someactive = false
			for k1,v1 in pairs(posix.dir( baseurl .. "/phraselists/" .. v )) do
				if not string.match(v1, "^%.") then
					local active
					if (categoryfilecontent[string.match(v1,'%w*')][baseurl .. "/phraselists/" .. v .."/" .. v1]) then 
						active = true
						someactive = true
					end
					table.insert(entrycontent, {name=v1, active=active, mhdebug=string.match(v1,'%w*'),mhdebug2=baseurl .. "/phraselists/" .. v .."/" .. v1})
				end
			end
			table.insert( retval, {name=v, option=entrycontent, active=someactive} )
		end
	end
	
	return retval
end

get_category = function(category, object)
	local filename = baseurl .. "/phraselists/" .. category .. "/" .. object

	if not (categoryfilecontent[string.match(object,'%w*')]) or 
	not (fs.is_file(filename)) or
	not (validator.is_valid_filename(filename, baseurl)) then
		return nil, "Something went wrong."
	end

	local retval = {}

	retval.activate = cfe({
		label="Activate this object",
		type="checkbox",
		checked="yes",
		})
	if not (categoryfilecontent[string.match(object,'%w*')][filename]) then
		retval.activestatus = cfe({
			label="Category object is currently",
			errtxt = "Deactivated",
			})
		retval.activate.checked=nil
	end

	retval.configfile = cfe({
		label="Activation is done in",
		value=categoryfiles[string.match(object,'%w*')],
		})

	local filedetails = fs.stat(filename)
	retval.mtime = cfe({
		label="Modify date",
		value=filedetails.mtime,
		})

	retval.category = cfe({
		name="category",
		label="Category name",
		value=category,
		})

	retval.name = cfe({
		name="name",
		label="Object name",
		value=object,
		})

	retval.filename = cfe({
		name="filename",
		label="Filename",
		type="hidden",
		value=filename,
		})
	retval.filecontent = cfe({
		label="Filename",
		value=fs.read_file(filename) or "",
		type="longtext",
		})

	return retval
end
--]]