module(..., package.seeall) -- This is the object/text used when we want to add a new record require("format") local newrecordtxt = "[New]" -- ################################################################################ -- LOCAL FUNCTIONS local function displaycmdmanagement(pidofstatus) -- 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", }) -- next CFE can be used to present the result of the previous action management.actionresult = cfe({ name="actionresult", label="Previous action result", }) 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 default_action = "status" function editrecords(self,types,record,errormessage) 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 -- Save info on how the original record looked like edit.orgrecord = cfe({ name="orgrecord", label="Original record", value=record, type="hidden", }) -- Specify a actiontype (this is then used when we make changes) edit.actiontype = cfe({ name="actiontype", label="Type of action [add|delete|modify]", type="hidden", }) if (record == newrecordtxt) then edit.actiontype.value = "add" else edit.actiontype.value = "modify" end if (types == "params") then table.insert(edit, cfe({ name=1, value=recordtable[1], label="Variable name", })) if (record == newrecordtxt) then edit[1]["value"] = "VARIABLE=XXX" end end -- Display save button local cmdsave = displaycmdsave() cmdsave.errtxt = errormessage -- Display delete button cmddelete = cfe({ name="cmddelete", label="Delete this record", value="Delete", type="submit", }) 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=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.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"] = nil break end end -- Now add this option to the list (just to show what it was) if (edit[fieldnum]["errtxt"]) 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=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=fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 4 edit[fieldnum] = cfe({ label="Broadcast", name=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=fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 2 edit[fieldnum] = cfe({ label="Type", name=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"] = nil break end end -- Now add this option to the list (just to show what it was) if (edit[fieldnum]["errtxt"]) 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=fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 4 edit[fieldnum] = cfe({ label="IN Options", name=fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 5 edit[fieldnum] = cfe({ label="OUT Options", name=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=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"] = nil break end end -- Now add this option to the list (just to show what it was) if (edit[fieldnum]["errtxt"]) 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=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"] = nil break end end -- Now add this option to the list (just to show what it was) if (edit[fieldnum]["errtxt"]) 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=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"] = nil break end end -- Now add this option to the list (just to show what it was) if (edit[fieldnum]["errtxt"]) 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=fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 5 edit[fieldnum] = cfe({ label="Burst:Limit", name=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=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"] = nil break end end -- Now add this option to the list (just to show what it was) if (edit[fieldnum]["errtxt"]) 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=fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 3 edit[fieldnum] = cfe({ label="Destination", name=fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 4 edit[fieldnum] = cfe({ label="Proto", name=fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 5 edit[fieldnum] = cfe({ label="Destination port", name=fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 6 edit[fieldnum] = cfe({ label="Source port(s)", name=fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 7 edit[fieldnum] = cfe({ label="Original destination", name=fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 8 edit[fieldnum] = cfe({ label="Rate:Limit", name=fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 9 edit[fieldnum] = cfe({ label="User/Group", name=fieldnum, value=recordtable[fieldnum], }) -- Create a cfe-table of the existing records local fieldnum = 9 edit[fieldnum] = cfe({ label="Mark", name=fieldnum, value=recordtable[fieldnum], }) end -- Add a hidden input where we define which config-file should be modified edit.filename = cfe({ label="File to edit", name="filename", value=types, type="hidden", }) return { edit=edit, option={ script=ENV["SCRIPT_NAME"], prefix=self.conf.prefix, controller = self.conf.controller, action = "config", link = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller, }, cmdsave=cmdsave, cmddelete=cmddelete, clientdata=clientdata, } end function status(self) return { status=self.model.getstatus() } end function config(self) -- If we made some changes to a recods... then proceed with the modification local savesuccess, configmessage if (self.clientdata.cmdsave) or (self.clientdata.cmddelete) then -- Check to see that user has entered accepted values local inputfields = {} local invalidinputfields if (self.clientdata) then for i=1,#self.clientdata do table.insert(inputfields, self.clientdata[i]) if (self.clientdata[i+1]) and (#self.clientdata[i+1] > 0 ) and (#self.clientdata[i] == 0) then configmessage = cfe({errtxt="Skipped fileds should contain '-'"}) end end end -- What are we going to do with the record... local modify_actiontype = self.clientdata.actiontype or "unknown" if (self.clientdata.cmddelete) then modify_actiontype = "delete" end if not ((configmessage) and (configmessage.errtxt)) then --Actually change the values savesuccess, configmessage = self.model:modify_config(modify_actiontype, self.clientdata.filename, inputfields, self.clientdata.orgrecord) end end -- If we previously made some changes, report this to user if ((self.clientdata.cmdsave) or (self.clientdata.cmddelete)) and not (savesuccess) then self.conf.action = "editrecords" self.conf.type = "redir" return editrecords(self,self.clientdata.filename, self.clientdata.orgrecord,tostring((configmessage.errtxt or ""))) end local config=self.model:getconfig() local status=self.model.getstatus() -- Add a [New] record to the options table.insert(config.interfaces.option, newrecordtxt) table.insert(config.zones.option, newrecordtxt) table.insert(config.policy.option, newrecordtxt) table.insert(config.rules.option, newrecordtxt) table.insert(config.params.option, newrecordtxt) -- Add button config.params_cmd = cfe ({ name="params_cmd", label="Edit above record", value="Edit", type="submit", }) config.params_cmd.descr="Mark a item in above list before pressing [" .. config.params_cmd.value .. "]" -- Add button config.interfaces_cmd = cfe ({ name="interfaces_cmd", label="Edit above record", value="Edit", type="submit", }) config.interfaces_cmd.descr="Mark a item in above list before pressing [" .. config.interfaces_cmd.value .. "]" -- Add button config.zones_cmd = cfe ({ name="zones_cmd", label="Edit above record", value="Edit", type="submit", }) config.zones_cmd.descr="Mark a item in above list before pressing [" .. config.zones_cmd.value .. "]" -- Add button config.policy_cmd = cfe ({ name="policy_cmd", label="Edit above record", value="Edit", type="submit", }) config.policy_cmd.descr="Mark a item in above list before pressing [" .. config.policy_cmd.value .. "]" -- Add button config.rules_cmd = cfe ({ name="rules_cmd", label="Edit above record", value="Edit", type="submit", }) config.rules_cmd.descr="Mark a item in above list before pressing [" .. config.rules_cmd.value .. "]" -- Add button config.check_cmd = cfe ({ name="check_cmd", label="Check if config works", descr="After your check, you will be able to start/stop/restart the process.", errtxt="Attention! This check could take a long time depending on your configuration.", value="Check", type="submit", }) -- Redirect if button is pressed if (self.clientdata.params_cmd) and (self.clientdata.params) then self.conf.action = "editrecords" self.conf.type = "redir" return editrecords(self,"params", self.clientdata.params) elseif (self.clientdata.params_cmd) and not (self.clientdata.params) then config.params_cmd.errtxt = "You need to specify a record to edit!" end -- Redirect if button is pressed if (self.clientdata.interfaces_cmd) and (self.clientdata.interfaces) then self.conf.action = "editrecords" self.conf.type = "redir" return editrecords(self,"interfaces", self.clientdata.interfaces) elseif (self.clientdata.interfaces_cmd) and not (self.clientdata.interfaces) then config.interfaces_cmd.errtxt = "You need to specify a record to edit!" end -- Redirect if button is pressed if (self.clientdata.zones_cmd) and (self.clientdata.zones) then self.conf.action = "editrecords" self.conf.type = "redir" return editrecords(self,"zones", self.clientdata.zones) elseif (self.clientdata.zones_cmd) and not (self.clientdata.zones) then config.zones_cmd.errtxt = "You need to specify a record to edit!" end -- Redirect if button is pressed if (self.clientdata.policy_cmd) and (self.clientdata.policy) then self.conf.action = "editrecords" self.conf.type = "redir" return editrecords(self,"policy", self.clientdata.policy) elseif (self.clientdata.policy_cmd) and not (self.clientdata.policy) then config.policy_cmd.errtxt = "You need to specify a record to edit!" end -- Redirect if button is pressed if (self.clientdata.rules_cmd) and (self.clientdata.rules) then self.conf.action = "editrecords" self.conf.type = "redir" return editrecords(self,"rules", self.clientdata.rules) elseif (self.clientdata.rules_cmd) and not (self.clientdata.rules) then config.rules_cmd.errtxt = "You need to specify a record to edit!" end -- If we previously made some changes, report this to user if ((self.clientdata.cmdsave) or (self.clientdata.cmddelete)) and (savesuccess) and (configmessage) and (configmessage.descr) then local reporttobutton = self.clientdata.filename .. "_cmd" if (config[reporttobutton]) then config[reporttobutton]["descr"] = tostring(configmessage.descr) end end return { status=status, config=config, option={ script=ENV["SCRIPT_NAME"], prefix=self.conf.prefix, controller = self.conf.controller, action = "check", link = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller, }, clientdata=clientdata, savesuccess=savesuccess, configmessage=configmessage, } end function check(self) -- Start/Stop/Restart process local cmdmanagement, actionresult if ( self.clientdata.cmdmanagement) then cmdmanagement = cfe({ name="cmdmanagement", label="Previous action result", action=cfe({ name="cmdmanagement", value=string.lower(self.clientdata.cmdmanagement), -- This row contains start/stop/restart (one of these commands) }), }) actionresult, cmdmanagement = self.model:startstop_service( cmdmanagement.action ) end local status = self.model.getstatus() local config = self.model:configcheck() -- Management buttons (Hide/show buttons local pidofstatus if (string.lower(status.status.value) == "enabled" ) then pidofstatus = true end management = displaycmdmanagement(pidofstatus) if (actionresult) then management.actionresult.descr=cmdmanagement.descr management.actionresult.errtxt=cmdmanagement.errtxt end 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, }, management = management, config = config, status = status, startstop = startstop, debugclientdata = self.clientdata, } end function logfile(self) 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, }, config = config, status = status, startstop = startstop, debugclientdata = self.clientdata, } end function expert(self) 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, }, config = config, status = status, startstop = startstop, debugclientdata = self.clientdata, } end function edit(self) -- 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() if (self.clientdata.cmdsave) then config.cmdsave.descr="* Changes has been saved!" end 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, }, modifications = modifications, config = config, status = status, startstop = startstop, debugclientdata = self.clientdata, } end