-- Copyright(c) 2007 A. Brodmann - Licensed under terms of GPL2 module (..., package.seeall) -- Load libraries require("modelfunctions") require "posix" require "format" require("processinfo") require("procps") require("fs") require("daemoncontrol") require("validator") -- Set variables dansguardiancfg = "/etc/dansguardian/dansguardian.conf" dansguardiancfg2 = "/etc/dansguardian/dansguardianf1.conf" local processname = "dansguardian" local packagename = "dansguardian" local baseurl = "/etc/dansguardian" -- Without trailing / 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)) 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']), } -- ################################################################################ -- 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 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 getconfigfile = function() return modelfunctions.getfiledetails(dansguardiancfg) 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 updateconfigfile = function( filedetails ) filedetails.value.filename.value = dansguardiancfg return modelfunctions.setfiledetails(dansguardiancfg) 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 local path = dir .. "/" .. v local filedetails = fs.stat(path) filedetails.path = prefix .. v table.insert( treetable, filedetails ) 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.path == name then retval = true end end return retval 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), type="longtext", }) return retval end