diff options
Diffstat (limited to 'dhcp-controller.lua')
-rw-r--r-- | dhcp-controller.lua | 280 |
1 files changed, 18 insertions, 262 deletions
diff --git a/dhcp-controller.lua b/dhcp-controller.lua index b79c3dc..13639f0 100644 --- a/dhcp-controller.lua +++ b/dhcp-controller.lua @@ -1,286 +1,42 @@ -- the dhcpd controller - module (..., package.seeall) -default_action = "status" - -dep = function( self ) - - -- do the dependancy check - msg = self.model.dep_check() - - -- make sure nobody accidentally calls this action - if msg == nil then - redirect(self, "home") - end - - -- go ahead - return ( cfe ({ msg = msg }) ) -end +require("controllerfunctions") -help = function( self ) +default_action = "status" - return ( cfe ({ option = "" }) ) +status = function ( self ) + return self.model.getstatus() end -delnet = function( self ) - - local net = {} - - if not self.clientdata.cmd then - redirect(self) - end - if not self.clientdata.network then - redirect(self) - end - - local option = { script = self.conf.script, - prefix = self.conf.prefix, - controller = self.conf.controller, - action = self.conf.action, - extra = "" - } - - if self.clientdata.cmd == "delete" then - if self.clientdata.confirm == "yes" then - msg = self.model.subnet_delete( self.clientdata.network ) - redirect(self) - else - net = self.model.subnet_read( self.clientdata.network ); - return ( cfe({ option = option, value = net, msg = msg }) ) - end - end +startstop = function ( self ) + return controllerfunctions.handle_startstop(self, self.model.startstop_service, self.model.getstatus, self.clientdata) end settings = function( self ) - - if not self.clientdata.cmd then - redirect(self) - end - - local settings = {} - local option = { script = self.conf.script, - prefix = self.conf.prefix, - controller = self.conf.controller, - action = self.conf.action, - extra = "" - } - - if self.clientdata.cmd == "update" then - tmp = self.clientdata - settings = self.model.create_new_settings( tmp.defleasetime, tmp.maxleasetime, tmp.domainname ) - errcode, settings = self.model.update_settings( settings ) - return ( cfe ({ option = option, value = settings, errcode = errcode })) - else - settings = self.model.read_settings() - return ( cfe ({ option = option, value = settings, errcode = { msg = "", fields={} }}) ) - end -end - -editnet = function ( self ) - - if not self.clientdata.cmd then - redirect(self) - else - if self.clientdata.cmd == "back" then - redirect(self) - end - end - - local net = {} - local option = { script = self.conf.script, - prefix = self.conf.prefix, - controller = self.conf.controller, - action = self.conf.action, - extra = "" - } - - if self.clientdata.network then - if self.clientdata.network == "choose" then - redirect(self) - end - end - - if self.clientdata.cmd == "update" then - tmp = self.clientdata - dynamicx = tmp.dynamichosts - advancedx = tmp.advanced - if tmp.unknownclients == "allow" then - dynamicx = tmp.dynamicx - end - if tmp.useadvanced ~= "use" then - advancedx = tmp.advancedx - end - net = self.model.create_new_net( tmp.name, tmp.defleasetime, tmp.maxleasetime, tmp.gateway, - tmp.domainname, tmp.dnssrv1, tmp.dnssrv2, tmp.subnet, tmp.netmask, tmp.leaserangestart, - tmp.leaserangeend, tmp.wpad, tmp.statichosts, tmp.unknownclients, dynamicx, advancedx, tmp.useadvanced ) - errcode, net = self.model.subnet_write( net ) - return ( cfe({ option = option, value = net, errcode = errcode }) ) - end - - net = self.model.subnet_read( self.clientdata.network ); - return ( cfe({ option = option, value = net, errcode = { msg="", fields={} }}) ) + return controllerfunctions.handle_form(self, self.model.read_settings, self.model.update_settings, self.clientdata, "Save", "Update Global Settings", "Global Settings Updated") end -editspc = function ( self ) - if not self.clientdata.cmd then - redirect( self ) - end - - local option = { script = self.conf.script, - prefix = self.conf.prefix, - controller = self.conf.controller, - action = self.conf.action, - extra = "" - } - - if self.clientdata.cmd == "update" then - msg = "" - fields = {} - tmp = self.clientdata - errmsg = self.model.validate_dynamichosts( tmp.dynamic ) - if #errmsg > 0 then - msg = errmsg - table.insert(fields, "dynamichosts") - end - value = self.model.advglobal_update( tmp.preconfig, tmp.postconfig, tmp.dynamic ) - return ( cfe({ option = option, value = value, errcode = { msg=msg, fields=fields }}) ) - end - - value = self.model.advglobal_read() - return ( cfe({ option = option, value = value, errcode = { msg="", fields={} }}) ) +editsubnet = function ( self ) + return controllerfunctions.handle_form(self, function() return self.model.subnet_read(self.clientdata.subnet) end, self.model.subnet_update, self.clientdata, "Save", "Edit Subnet", "Subnet Settings Updated") end -createnet = function ( self ) - - if not self.clientdata.cmd then - redirect(self) - end - - local option = { script = self.conf.script, - prefix = self.conf.prefix, - controller = self.conf.controller, - action = self.conf.action, - extra = "" - } - - if self.clientdata.cmd == "new" then - net = self.model.create_new_net( "<new>", nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil ) - return ( cfe({ option = option, value = net, errcode = { msg = "", fields = nil }}) ) - elseif self.clientdata.cmd == "create" then - tmp = self.clientdata - net = self.model.create_new_net( tmp.name, tmp.defleasetime, tmp.maxleasetime, - tmp.gateway, tmp.domainname, tmp.dnssrv1, tmp.dnssrv2, tmp.subnet, - tmp.netmask, tmp.leaserangestart, tmp.leaserangeend, tmp.wpad, "", tmp.unknownclients, "", "", "" ) - errcode, net = self.model.subnet_create( net ) - if #errcode.msg == 0 then - redirect(self, "home") - else - return ( cfe({ option = option, value = net, errcode = errcode }) ) - end - end +createsubnet = function ( self ) + return controllerfunctions.handle_form(self, self.model.create_new_subnet, self.model.subnet_create, self.clientdata, "Create", "Create new subnet", "New subnet Created") end -status = function ( self ) - return cfe({ option = { script = self.conf.script, - prefix = self.conf.prefix, - controller = self.conf.controller, - action = self.conf.action, - extra = "", - }, - value = "", - genmsg = genmsg, - info = self.model.getstatus() }) +delsubnet = function(self) + return self:redirect_to_referrer(self.model.subnet_delete(self.clientdata.subnet)) end -home = function ( self ) - - -- dependancy check for neccessary libs/packages et al. - msg = self.model.dep_check() - if msg ~= nil then - redirect(self, "dep") - end - - local srvctrl = "" - if self.clientdata.srvcmd then - srvcmd = self.clientdata.srvcmd - if srvcmd == "start" or srvcmd == "stop" or srvcmd == "restart" then - srvctrl = self.model.service_control(self.clientdata.srvcmd) - end - end - - if self.clientdata.cmd == "generate" then - genmsg = self.model.config_generate() - end - - local option = { script = self.conf.script, - prefix = self.conf.prefix, - controller = self.conf.controller, - action = self.conf.action, - extra = "" - } - - local info = self.model.getstatus() - info.subnets = self.model.get_subnets() - - -- 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 (string.lower(info.status.value) == "enabled") then management.start.disabled = "yes" end - if (string.lower(info.status.value) == "disabled") then management.stop.disabled = "yes" end - if (string.lower(info.status.value) == "disabled") then management.restart.disabled = "yes" end - - return ( cfe({ option = option, value = "", genmsg = genmsg, info = info, management = management, }) ) +listsubnets = function ( self ) + return self.model.get_subnets() end viewleases = function ( self ) - - local filename = "/var/lib/dhcp/dhcpd.leases"; - - local option = { script = self.conf.script, - prefix = self.conf.prefix, - controller = self.conf.controller, - action = self.conf.action, - extra = "" - } - - local value = { filename = { value=filename }, - contents = { value=self.model.read_file(filename) } - } - - return ( cfe({ option = option, value = value }) ) + return self.model.getleases() end -viewconfig = function ( self ) - - local filename = "/etc/dhcp/dhcpd.conf" - - local option = { script = self.conf.script, - prefix = self.conf.prefix, - controller = self.conf.controller, - action = self.conf.action, - extra = "" - } - - local value = { filename = { value=filename }, - contents = { value=self.model.read_file(filename) } - } - - return ( cfe({ option = option, value = value }) ) +expert = function(self) + return controllerfunctions.handle_form(self, self.model.getconfigfile, self.model.setconfigfile, self.clientdata, "Save", "Edit Config", "Configuration Set") end - |