-- the dhcpd controller module (..., package.seeall) -- Cause an http redirect to our "read" action -- We use the self.conf table because it already has prefix,controller,etc -- The redir code is defined in the application error handler (acf-controller) local list_redir = function (self) self.conf.action = "home" self.conf.type = "redir" error (self.conf) end local pvt = {} mvc= {} mvc.on_load = function( self, parent ) -- If they try to run a bogus action, send them to read if ( rawget(self.worker, self.conf.action) == nil ) then list_redir(self) end pvt.parent_on_exec = parent.worker.mvc.post_exec logit ("dhcpd controller on_load has finished") end mvc.pre_exec = function( self ) logit ("dhcpd-controller pre_exec activated") -- pvt.parent_on_exec () end mvc.post_exec = function( self ) logit ("dhcpd-controller post_exec activated") return pvt.parent_on_exec() end dep = function( self ) -- do the dependancy check msg = self.model.dep_check() -- make sure nobody accidentally calls this action if msg == nil then self.conf.type = "redir" self.conf.action = "home" error (self.conf) end -- go ahead return ( cfe ({ msg = msg }) ) end delnet = function( self ) local net = {} if not self.clientdata.cmd then list_redir(self) end if not self.clientdata.network then list_redir(self) end local option = { script = ENV["SCRIPT_NAME"], 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 ) list_redir(self) else net = self.model.subnet_read( self.clientdata.network ); return ( cfe({ option = option, value = net, msg = msg }) ) end end end settings = function( self ) if not self.clientdata.cmd then list_redir(self) end local settings = {} local option = { script = ENV["SCRIPT_NAME"], 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 list_redir(self) end local net = {} local option = { script = ENV["SCRIPT_NAME"], prefix = self.conf.prefix, controller = self.conf.controller, action = self.conf.action, extra = "" } if self.clientdata.network then if self.clientdata.network == "choose" then list_redir(self) end end if self.clientdata.cmd == "update" 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.spechosts ) 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={} }}) ) end createnet = function ( self ) if not self.clientdata.cmd then list_redir(self) end local option = { script = ENV["SCRIPT_NAME"], 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( "", 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, "" ) errcode, net = self.model.subnet_create( net ) if #errcode.msg == 0 then self.conf.type = "redir" self.conf.action = "home" error (self.conf) else return ( cfe({ option = option, value = net, errcode = errcode }) ) end end end home = function ( self ) -- dependancy check for neccessary libs/packages et al. msg = self.model.dep_check() if msg ~= nil then self.conf.type = "redir" self.conf.action = "dep" error(self.conf) 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 = ENV["SCRIPT_NAME"], prefix = self.conf.prefix, controller = self.conf.controller, action = self.conf.action, extra = "" } local info = { status = { value = "stopped" }, version = { value = self.model.get_dhcpd_version() }, srvctrl = { value = srvctrl} }; if self.model.is_running( "dhcpd" ) then info.status.value = "running" end info.subnets = self.model.get_subnets() return ( cfe({ option = option, value = "", genmsg = genmsg, info = info }) ) end view = function ( self ) local filename = "" if self.clientdata.conf then filename = "/etc/dhcp/dhcpd.conf" elseif self.clientdata.leases then filename = "/var/lib/dhcpd/dhcpd.leases"; else list_redir(self) end local option = { script = ENV["SCRIPT_NAME"], 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 }) ) end