diff options
Diffstat (limited to 'dansguardian-model.lua')
-rw-r--r-- | dansguardian-model.lua | 353 |
1 files changed, 105 insertions, 248 deletions
diff --git a/dansguardian-model.lua b/dansguardian-model.lua index 7fb76a1..f24748e 100644 --- a/dansguardian-model.lua +++ b/dansguardian-model.lua @@ -3,12 +3,9 @@ module (..., package.seeall) -- Load libraries require("modelfunctions") -require "posix" -require "format" -require("processinfo") -require("procps") +require("getopts") +--require "posix" require("fs") -require("daemoncontrol") require("validator") -- Set variables @@ -16,7 +13,9 @@ dansguardiancfg = "/etc/dansguardian/dansguardian.conf" dansguardiancfg2 = "/etc/dansguardian/dansguardianf1.conf" local processname = "dansguardian" local packagename = "dansguardian" -local baseurl = "/etc/dansguardian" -- Without trailing / +local baseurl = "/etc/dansguardian" + +--[[ local categoryfiles = { ['weighted'] = tostring(baseurl .. "/weightedphraselist"), ['banned'] = tostring(baseurl .. "/bannedphraselist"), @@ -42,7 +41,39 @@ local categoryfilecontent = { ['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 @@ -55,274 +86,100 @@ startstop_service = function( action ) return modelfunctions.startstop_service(processname, action) end -get_general_config = function() +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 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!" + local config = getopts.getoptsfromfile(dansguardiancfg, "") + 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 - 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!" + config = getopts.getoptsfromfile(dansguardiancfg2, "") + if config then + if config.naughtynesslimit then retval.naughtynesslimit.value = config.naughtynesslimit end end - return retval, error -end - -getconfigfile = function() - return modelfunctions.getfiledetails(dansguardiancfg) + return cfe({ type="group", value=retval, label="Dansguardian General Config" }) 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!" +update_general_config = function( config ) + local success, config = validate_general_config(config) + + if success then + local a,b,c + local text = fs.read_file(dansguardiancfg) + a,b,c,text = getopts.setoptsinfile(text, "", "filterip", config.value.filterip.value) + a,b,c,text = getopts.setoptsinfile(text, "", "filterport", config.value.filterport.value) + a,b,c,text = getopts.setoptsinfile(text, "", "proxyip", config.value.proxyip.value) + a,b,c,text = getopts.setoptsinfile(text, "", "proxyport", config.value.proxyport.value) + a,b,c,text = getopts.setoptsinfile(text, "", "accessdeniedaddress", "'"..config.value.accessdeniedaddress.value.."'") + fs.write_file(dansguardiancfg, string.gsub(text, "\n+$", "")) + getopts.setoptsinfile(dansguardiancfg2, "", "naughtynesslimit", config.value.naughtynesslimit.value) + else + config.errtxt = "Failed to set config" end - return retval, error + return config 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 = "" +get_file = function(filename) + local retval + if is_valid_filename(filename) then + retval = modelfunctions.getfiledetails(filename) else - error = "Failed to open /etc/dansguardian/" .. name .. " file!" + retval = modelfunctions.getfiledetails("") + retval.value.filename.value = filename 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 ) +update_file = function(filedetails) + local retval + if is_valid_filename(filedetails.value.filename.value) then + retval = modelfunctions.setfiledetails(filedetails) + else + retval.value.filename.errtxt = "Invalid filename" + retval.errtxt = "Failed to save file" end - - return retval -end - -get_advanced_config = function() - local retval = { files = {} } - local errmsg = "" - - get_file_tree( retval.files, "/etc/dansguardian", "" ) - - return retval, errmsg + return retval 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 .. "/" ) +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 - - return + return cfe({ type="structure", value=retval, label="List of Dansguardian files" }) 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 +list_config_files = function() + local list = {} + local details = fs.stat(dansguardiancfg) + details.filename = dansguardiancfg + list[1] = details + details = fs.stat(dansguardiancfg2) + details.filename = dansguardiancfg2 + list[2] = details + return cfe({ type="list", value=list, label="List of Dansguardian config files" }) end +--[[ get_categories = function() local retval = {} @@ -412,4 +269,4 @@ get_category = function(category, object) return retval end - +--]] |