module(..., package.seeall) -- This is the object/text used when we want to add a new record require("format") local newrecordtxt = "[New]" local list_redir = function (self) self.conf.action = "status" self.conf.type = "redir" error (self.conf) end mvc = {} mvc.on_load = function(self, parent) if (self.worker[self.conf.action] == nil ) or ( self.conf.action == "init" ) then self.worker[self.conf.action] = list_redir(self) end end local function displaycmdmanagement(disablestart,disablestop,disablerestart) -- Add a management buttons local management = {} ---[[ management.start = cfe({ name="cmdmanagement", label="Program control-panel", value="Start", type="submit", }) management.stop = cfe({ name="cmdmanagement", label="Program control-panel", value="Stop", type="submit", }) --]] management.restart = cfe({ name="cmdmanagement", label="Program control-panel", value="Restart", type="submit", }) -- Disable management buttons based on if the process is running or not if (disablestart) then management.start.disabled = "yes" end if (disablestop) then management.stop.disabled = "yes" end if (disablerestart) then management.restart.disabled = "yes" end return management end local function displaycmdsave(self) -- Add a cmd button to the view local cmdsave = cfe({ name="cmdsave", label="Save/Apply above settings", value="Save", type="submit", }) return cmdsave end -- ################################################################################ -- PUBLIC FUNCTIONS function edit_records(self,types,record) local recorddetails = {} local edit = {} local config=self.model:getconfig() -- Split the record into a table local recordtable = {} for word in string.gmatch(record, "%S+") do table.insert(recordtable, word) end if (types == "params") then table.insert(edit, cfe({ name="field1", value=recordtable[1], label="Variable name", })) if (record == newrecordtxt) then edit[1]["value"] = "VARIABLE=XXX" end end -- Display save button local cmdsave = displaycmdsave() cmdsave.disabled="yes" -- DEBUGGING cmdsave.descr="This button is not yet programmed to work" -- DEBUGGING -- Display delete button cmddelete = cfe({ name="cmddelete", label="Delete this record", value="Delete", type="submit", }) cmddelete.disabled="yes" -- DEBUGGING cmddelete.descr="This button is not yet programmed to work" -- DEBUGGING if (types == "interfaces") then -- Fetch the list of existing interfaces -- local interfaceslist = {} -- local interfaces, int_w_loaded, int_m_loaded = self:new("alpine-baselayout/interfaces") -- if (int_m_loaded) then -- interfaceslist = interfaces.worker.read(interfaces) -- end -- Create a cfe-table of the existing records local fieldnum = 1 edit[fieldnum] = cfe({ label="Zone", name="field".. fieldnum, value=recordtable[fieldnum], type="select", option={}, debug=interfaceslist, }) -- IF user creates '[New]' record... then clean up the output. if (record == newrecordtxt) then edit[fieldnum]["value"] = "-" end table.insert(edit[fieldnum]["option"], "-") for k,v in pairs(config.zones_list.option or {}) do table.insert(edit[fieldnum]["option"], string.match(v, "^%s*(%S*)")) end -- IF the value is not one of the existing options, then warn and add this option. for k,v in pairs(edit[fieldnum]["option"]) do edit[fieldnum]["errtxt"] = "'" .. edit[fieldnum]["value"] .. "' is not a valid option!" if (tostring(v) == tostring(edit[fieldnum]["value"])) then edit[fieldnum]["errtxt"] = "" break end end -- Now add this option to the list (just to show what it was) if (#edit[fieldnum]["errtxt"] > 0) then table.insert(edit[fieldnum]["option"], edit[fieldnum]["value"]) end -- Create a cfe-table of the existing records local fieldnum = 2 edit[fieldnum] = cfe({ label="Interfaces", name="field".. fieldnum, value=recordtable[fieldnum], -- descr="Available interfaces are: ", }) -- Create a cfe-table of the existing records local fieldnum = 3 edit[fieldnum] = cfe({ label="Broadcast", name="field".. fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 4 edit[fieldnum] = cfe({ label="Broadcast", name="field".. fieldnum, value=recordtable[fieldnum], }) end if (types == "zones") then -- Create a cfe-table of the existing records local fieldnum = 1 edit[fieldnum] = cfe({ label="Zone", name="field".. fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 2 edit[fieldnum] = cfe({ label="Type", name="field".. fieldnum, value=recordtable[fieldnum], type="select", option={"ipv4", "ipsec", "firewall",} }) -- IF user creates '[New]' record... then clean up the output. if (record == newrecordtxt) then edit[fieldnum]["value"] = "ipv4" end -- IF the value is not one of the existing options, then warn and add this option. for k,v in pairs(edit[fieldnum]["option"]) do edit[fieldnum]["errtxt"] = "'" .. edit[fieldnum]["value"] .. "' is not a valid option!" if (tostring(v) == tostring(edit[fieldnum]["value"])) then edit[fieldnum]["errtxt"] = "" break end end -- Now add this option to the list (just to show what it was) if (#edit[fieldnum]["errtxt"] > 0) then table.insert(edit[fieldnum]["option"], edit[fieldnum]["value"]) end -- Create a cfe-table of the existing records local fieldnum = 3 edit[fieldnum] = cfe({ label="Options", name="field".. fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 4 edit[fieldnum] = cfe({ label="IN Options", name="field".. fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 5 edit[fieldnum] = cfe({ label="OUT Options", name="field".. fieldnum, value=recordtable[fieldnum], }) end if (types == "policy") then -- Create a cfe-table of the existing records local fieldnum = 1 edit[fieldnum] = cfe({ label="Source zone", name="field".. fieldnum, value=recordtable[fieldnum], type="select", option=self.model.get_defined_zones(), }) -- IF user creates '[New]' record... then clean up the output. if (record == newrecordtxt) then edit[fieldnum]["value"] = "all" end table.insert(edit[fieldnum]["option"], "$FW") table.insert(edit[fieldnum]["option"], "all") -- IF the value is not one of the existing options, then warn and add this option. for k,v in pairs(edit[fieldnum]["option"]) do edit[fieldnum]["errtxt"] = "'" .. edit[fieldnum]["value"] .. "' is not a valid option!" if (tostring(v) == tostring(edit[fieldnum]["value"])) then edit[fieldnum]["errtxt"] = "" break end end -- Now add this option to the list (just to show what it was) if (#edit[fieldnum]["errtxt"] > 0) then table.insert(edit[fieldnum]["option"], edit[fieldnum]["value"]) end -- Create a cfe-table of the existing records local fieldnum = 2 edit[fieldnum] = cfe({ label="Destination zone", name="field".. fieldnum, value=recordtable[fieldnum], type="select", option=self.model.get_defined_zones(), }) -- IF user creates '[New]' record... then clean up the output. if (record == newrecordtxt) then edit[fieldnum]["value"] = "all" end table.insert(edit[fieldnum]["option"], "$FW") table.insert(edit[fieldnum]["option"], "all") -- IF the value is not one of the existing options, then warn and add this option. for k,v in pairs(edit[fieldnum]["option"]) do edit[fieldnum]["errtxt"] = "'" .. edit[fieldnum]["value"] .. "' is not a valid option!" if (tostring(v) == tostring(edit[fieldnum]["value"])) then edit[fieldnum]["errtxt"] = "" break end end -- Now add this option to the list (just to show what it was) if (#edit[fieldnum]["errtxt"] > 0) then table.insert(edit[fieldnum]["option"], edit[fieldnum]["value"]) end -- Create a cfe-table of the existing records local fieldnum = 3 edit[fieldnum] = cfe({ label="Policy", name="field".. fieldnum, value=recordtable[fieldnum], type="select", option={"ACCEPT","DROP","REJECT","CONTINUE","QUEUE","NONE"} }) -- IF user creates '[New]' record... then clean up the output. if (record == newrecordtxt) then edit[fieldnum]["value"] = "DROP" end -- IF the value is not one of the existing options, then warn and add this option. for k,v in pairs(edit[fieldnum]["option"]) do edit[fieldnum]["errtxt"] = "'" .. edit[fieldnum]["value"] .. "' is not a valid option!" if (tostring(v) == tostring(edit[fieldnum]["value"])) then edit[fieldnum]["errtxt"] = "" break end end -- Now add this option to the list (just to show what it was) if (#edit[fieldnum]["errtxt"] > 0) then table.insert(edit[fieldnum]["option"], edit[fieldnum]["value"]) end -- Create a cfe-table of the existing records local fieldnum = 4 edit[fieldnum] = cfe({ label="Log level", name="field".. fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 5 edit[fieldnum] = cfe({ label="Burst:Limit", name="field".. fieldnum, value=recordtable[fieldnum], }) end if (types == "rules") then -- Create a cfe-table of the existing records local fieldnum = 1 edit[fieldnum] = cfe({ label="Action", name="field".. fieldnum, value=recordtable[fieldnum], type="select", option={ "ACCEPT", "ACCEPT+", "ACCEPT!", "NONAT", "DROP", "DROP!", "REJECT", "REJECT!", "DNAT", "DNAT-", "REDIRECT", "REDIRECT-", "CONTINUE", "CONTINUE!", "LOG", "QUEUE", "QUEUE!", "COMMENT", }, }) -- IF user creates '[New]' record... then clean up the output. if (record == newrecordtxt) then edit[fieldnum]["value"] = "DROP" end -- IF the value is not one of the existing options, then warn and add this option. for k,v in pairs(edit[fieldnum]["option"]) do edit[fieldnum]["errtxt"] = "Attention! '" .. edit[fieldnum]["value"] .. "' could be a invalid option (or is a action defined in 'actions')." if (tostring(v) == tostring(edit[fieldnum]["value"])) then edit[fieldnum]["errtxt"] = "" break end end -- Now add this option to the list (just to show what it was) if (#edit[fieldnum]["errtxt"] > 0) then table.insert(edit[fieldnum]["option"], edit[fieldnum]["value"]) end -- Create a cfe-table of the existing records local fieldnum = 2 edit[fieldnum] = cfe({ label="Source", name="field".. fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 3 edit[fieldnum] = cfe({ label="Destination", name="field".. fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 4 edit[fieldnum] = cfe({ label="Proto", name="field".. fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 5 edit[fieldnum] = cfe({ label="Destination port", name="field".. fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 6 edit[fieldnum] = cfe({ label="Source port(s)", name="field".. fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 7 edit[fieldnum] = cfe({ label="Original destination", name="field".. fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 8 edit[fieldnum] = cfe({ label="Rate:Limit", name="field".. fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 9 edit[fieldnum] = cfe({ label="User/Group", name="field".. fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 9 edit[fieldnum] = cfe({ label="Mark", name="field".. fieldnum, value=recordtable[fieldnum], }) end return { edit=edit, cmdsave=cmdsave, cmddelete=cmddelete, clientdata=clientdata, } end function status(self) return { status=self.model.getstatus() } end function config(self) local config=self.model:getconfig() local status=self.model.getstatus() -- Redirect if button is pressed if (self.clientdata.params_list_cmd) then self.conf.action = "edit_records" self.conf.type = "redir" return edit_records(self,"params", self.clientdata.params_list) end -- Redirect if button is pressed if (self.clientdata.interfaces_list_cmd) then self.conf.action = "edit_records" self.conf.type = "redir" return edit_records(self,"interfaces", self.clientdata.interfaces_list) end -- Redirect if button is pressed if (self.clientdata.zones_list_cmd) then self.conf.action = "edit_records" self.conf.type = "redir" return edit_records(self,"zones", self.clientdata.zones_list) end -- Redirect if button is pressed if (self.clientdata.policies_list_cmd) then self.conf.action = "edit_records" self.conf.type = "redir" return edit_records(self,"policy", self.clientdata.policies_list) end -- Redirect if button is pressed if (self.clientdata.rules_list_cmd) then self.conf.action = "edit_records" self.conf.type = "redir" return edit_records(self,"rules", self.clientdata.rules_list) end -- Add a [New] record to the options table.insert(config.interfaces_list.option, newrecordtxt) table.insert(config.zones_list.option, newrecordtxt) table.insert(config.policies_list.option, newrecordtxt) table.insert(config.rules_list.option, newrecordtxt) table.insert(config.params_list.option, newrecordtxt) -- Add button config.params_list_cmd = cfe ({ name="params_list_cmd", label="Edit above record", value="Edit", type="submit", -- disabled="yes", }) config.params_list_cmd.descr="Mark a item in above list before pressing [" .. config.params_list_cmd.value .. "]" -- Add button config.interfaces_list_cmd = cfe ({ name="interfaces_list_cmd", label="Edit above record", value="Edit", type="submit", -- disabled="yes", }) config.interfaces_list_cmd.descr="Mark a item in above list before pressing [" .. config.interfaces_list_cmd.value .. "]" -- Add button config.zones_list_cmd = cfe ({ name="zones_list_cmd", label="Edit above record", value="Edit", type="submit", -- disabled="yes", }) config.zones_list_cmd.descr="Mark a item in above list before pressing [" .. config.zones_list_cmd.value .. "]" -- Add button config.policies_list_cmd = cfe ({ name="policies_list_cmd", label="Edit above record", value="Edit", type="submit", -- disabled="yes", }) config.policies_list_cmd.descr="Mark a item in above list before pressing [" .. config.policies_list_cmd.value .. "]" -- Add button config.rules_list_cmd = cfe ({ name="rules_list_cmd", label="Edit above record", value="Edit", type="submit", -- disabled="yes", -- errtxt="This button is not yet programmed!", }) config.rules_list_cmd.descr="Mark a item in above list before pressing [" .. config.rules_list_cmd.value .. "]" -- Management buttons -- Display management buttons local management = displaycmdmanagement(disablestart,disablestop,disablerestart) return { status=status, config=config, management = management, option={ script=ENV["SCRIPT_NAME"], prefix=self.conf.prefix, controller = self.conf.controller, action = "expert", link = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller, }, clientdata=clientdata, } end function check(self) local cmdmanagement, cmdmanagementresult if ( self.clientdata.cmdmanagement) then cmdmanagement = cfe({ name="cmdmanagement", value=string.lower(self.clientdata.cmdmanagement), }) cmdmanagementresult, cmdmanagement = self.model:startstop_service( cmdmanagement ) end local status = self.model.getstatus() local config = self.model:configcheck() -- Display management buttons local management = displaycmdmanagement(disablestart,disablestop,disablerestart) return { option={ script=ENV["SCRIPT_NAME"], prefix=self.conf.prefix, controller = self.conf.controller, action = "expert", link = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller, }, cmdmanagement = cmdmanagement, management = management, config = config, status = status, startstop = startstop, debugclientdata = self.clientdata, } end function logfile(self) local cmdmanagement, cmdmanagementresult if ( self.clientdata.cmdmanagement) then cmdmanagement = cfe({ name="cmdmanagement", value=string.lower(self.clientdata.cmdmanagement), }) cmdmanagementresult, cmdmanagement = self.model:startstop_service( cmdmanagement ) end local status = self.model.getstatus() local config = self.model:getlogfile() return { option={ script=ENV["SCRIPT_NAME"], prefix=self.conf.prefix, controller = self.conf.controller, action = "expert", link = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller, }, cmdmanagement = cmdmanagement, management = management, config = config, status = status, startstop = startstop, debugclientdata = self.clientdata, } end function expert(self) local cmdmanagement, cmdmanagementresult if ( self.clientdata.cmdmanagement) then cmdmanagement = cfe({ name="cmdmanagement", value=string.lower(self.clientdata.cmdmanagement), }) cmdmanagementresult, cmdmanagement = self.model:startstop_service( cmdmanagement ) end local status = self.model.getstatus() local config = self.model:getfilelist() return { option={ script=ENV["SCRIPT_NAME"], prefix=self.conf.prefix, controller = self.conf.controller, action = "expert", link = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller, }, cmdmanagement = cmdmanagement, management = management, config = config, status = status, startstop = startstop, debugclientdata = self.clientdata, } end function edit(self) local cmdmanagement, cmdmanagementresult, modifications if ( self.clientdata.cmdmanagement) then cmdmanagement = cfe({ name="cmdmanagement", value=string.lower(self.clientdata.cmdmanagement), }) cmdmanagementresult, cmdmanagement = self.model:startstop_service( cmdmanagement ) end -- Save changes if ( self.clientdata.cmdsave) then local filetochange = cfe ({ name=self.clientdata.filename, value=self.clientdata.filecontent, }) modifications = self.model:updatefilecontent(filetochange) self.clientdata.name = self.clientdata.filename end local status = self.model.getstatus() local config = self.model:getfiledetails(cfe({ name="editfile", value=self.clientdata.name, })) -- Display save button config.cmdsave = displaycmdsave() return { option={ script=ENV["SCRIPT_NAME"], prefix=self.conf.prefix, controller = self.conf.controller, action = "edit", link = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller, }, cmdmanagement = cmdmanagement, modifications = modifications, management = management, config = config, status = status, startstop = startstop, debugclientdata = self.clientdata, } end