diff options
author | Mika Havela <mika.havela@gmail.com> | 2008-03-06 16:36:19 +0000 |
---|---|---|
committer | Mika Havela <mika.havela@gmail.com> | 2008-03-06 16:36:19 +0000 |
commit | 41d13da850347a5c2691af7e4d4389fec227188a (patch) | |
tree | d97d6dad8c0b41166ddeb7b0877c195cb9fd3afd /shorewall-model.lua | |
parent | 04d24d79929167c44875b3291849293b359ee480 (diff) | |
download | acf-shorewall-41d13da850347a5c2691af7e4d4389fec227188a.tar.bz2 acf-shorewall-41d13da850347a5c2691af7e4d4389fec227188a.tar.xz |
Now the config-tab works just find (as I can see).
git-svn-id: svn://svn.alpinelinux.org/acf/shorewall/trunk@823 ab2d0c66-481e-0410-8bed-d214d4d58bed
Diffstat (limited to 'shorewall-model.lua')
-rw-r--r-- | shorewall-model.lua | 264 |
1 files changed, 167 insertions, 97 deletions
diff --git a/shorewall-model.lua b/shorewall-model.lua index 0375b06..e9ca529 100644 --- a/shorewall-model.lua +++ b/shorewall-model.lua @@ -48,9 +48,160 @@ local function read_config(file) 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) + + 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!" .. +"<BR>orgvalue:" .. tostring(orgvalue) .. +"<BR>modifyrow:" .. tostring(modifyrow) .. +"<BR>orgrecordtable:" .. table.concat(orgrecordtable, ";") .. +"<BR>file:" .. tostring(file) .. +"<BR>filecontentarray:" .. table.concat(filecontentarray, ";") .. +"" + , + }) + 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!".. +"<BR>orgvalue:" .. tostring(orgvalue) .. +"<BR>modifyrow:" .. tostring(modifyrow) .. +"<BR>orgrecordtable:" .. table.concat(orgrecordtable, ";") .. +"<BR>file:" .. tostring(file) .. +"<BR>filecontentarray:" .. table.concat(filecontentarray, ";") .. +"" +, + }) + 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 modify_config(self, addremove, file, value, orgvalue ) + return addremove_config(addremove, file, value, orgvalue ) +end + -- action should be a CFE function startstop_service ( self, action ) local cmd = action.value @@ -65,45 +216,45 @@ end function getconfig() local config = {} - config.params_list = cfe({ - name = "params_list", + config.params = cfe({ + name = "params", label="List of parameters", type="select", option=read_config("params"), }) - config.params_list.size=#config.params_list.option + 1 + config.params.size=#config.params.option + 1 - config.interfaces_list = cfe({ - name = "interfaces_list", + config.interfaces = cfe({ + name = "interfaces", label="List of interfaces", type="select", option=read_config("interfaces"), }) - config.interfaces_list.size=#config.interfaces_list.option + 1 + config.interfaces.size=#config.interfaces.option + 1 - config.zones_list = cfe({ - name = "zones_list", + config.zones = cfe({ + name = "zones", label="List of zones", type="select", option=read_config("zones"), }) - config.zones_list.size=#config.zones_list.option + 1 + config.zones.size=#config.zones.option + 1 - config.policies_list = cfe({ - name = "policies_list", - label="List of policies", + config.policy = cfe({ + name = "policy", + label="List of policy", type="select", option=read_config("policy"), }) - config.policies_list.size=#config.policies_list.option + 1 + config.policy.size=#config.policy.option + 1 - config.rules_list = cfe({ - name = "rules_list", + config.rules = cfe({ + name = "rules", label="List of rules", type="select", option=read_config("rules"), }) - config.rules_list.size=#config.rules_list.option + 1 + config.rules.size=#config.rules.option + 1 return config @@ -228,87 +379,6 @@ function getfiledetails(self,search) return file end - --- IMPORTANT! This function is a exception! It's not fed with CFE's --- Parameter should be one of the ones defined in the variable 'variabletranslator'. --- value should be whatever the new value should be. -function setconfigs(self,parameter,value) - -- Set variables - local variable = "SYSLOGD_OPTS" - local variabletranslator = ({ - logfile = "-O", - loglevel = "-l", - smallerlogs = "-S", - maxsize = "-s", - numrotate = "-b", - localandnetworklog = "-L", - remotelogging = "-R", - }) - cmdparameter = variabletranslator[parameter] - - -- Report a error if someone tryes to use a invalid parameter - if not (cmdparameter) then - local availablevariables = "" - for k,v in pairs(variabletranslator) do - availablevariables = k .. ", " .. availablevariables - end - parameter = parameter or "" - return false, cfe({ - name="syslog.model.setconfigs()", - errtxt="'" .. parameter .. "' is not a valid parameter!\nValid options are: " .. availablevariables, - }) - end - - --TODO: Validate so that user cant add values with '-' (could cause major breakage next time you do getopts) - - -- This config-file only accepts one type of parameters (report error if someone uses wrong parameter) - if not (string.find(cmdparameter, "-%a$")) then - return false, cfe({ - name="syslog.model.setconfigs()", - errtxt="Parameter must be formated '-a' (where a is one upper/lowercase letter [a-z])", - }) - end - - -- Validate userinput (if valid path/filename) - if (value) and (cmdparameter == "-O") then - local cmdresult, cmdmessage = validator.is_valid_filename(value, "/var/log" ) - if not (cmdresult) then - return false, cfe({ - name="syslog.model.setconfigs()", - errtxt=cmdmessage, - }) - end - end - - -- Validate userinput (Has the user entered a valid hostname and/or port) - if (value) and (cmdparameter == "-R") then - local hostport = format.string_to_table(value, ":") - local host = hostport[1] - local port = hostport[2] - if (port) and not (validator.is_port(port)) then - return false, cfe({ - name="syslog.model.setconfigs.getopts.setoptsinfile()", - errtxt="You entered '" .. tostring(port) .. "' as port - This is not valid!", - }) - end - end - - -- Set/Unset checkbox variables - if (value) and ((cmdparameter == "-S") or (cmdparameter == "-L")) then value = "" end - - local cmdresult, cmdmessage, cmderror = getopts.setoptsinfile(configfile,variable,cmdparameter,value) - if (cmderror) then - return false, cfe({ - name="syslog.model.setconfigs.getopts.setoptsinfile()", - errtxt=cmderror, - }) - end - return true, cfe({ - name="syslog.model.setconfigs()", - value=cmdmessage, - }) -end - -- modifications should be a CFE function updatefilecontent (self, filetochange) local path = nil |