diff options
author | Ted Trask <ttrask01@yahoo.com> | 2008-08-12 12:07:14 +0000 |
---|---|---|
committer | Ted Trask <ttrask01@yahoo.com> | 2008-08-12 12:07:14 +0000 |
commit | af2b190d33683aafc06c73b0bab1ae743e69468a (patch) | |
tree | bffc185e18806da99144c01ce824313e21cb8571 | |
parent | e45a01ccebf7c9a756e3d27e3fe6abbeb8797a36 (diff) | |
download | acf-dhcp-af2b190d33683aafc06c73b0bab1ae743e69468a.tar.bz2 acf-dhcp-af2b190d33683aafc06c73b0bab1ae743e69468a.tar.xz |
Modified dhcp to add host support.
git-svn-id: svn://svn.alpinelinux.org/acf/dhcp/trunk@1376 ab2d0c66-481e-0410-8bed-d214d4d58bed
-rw-r--r-- | dhcp-controller.lua | 16 | ||||
-rw-r--r-- | dhcp-home-html.lsp | 4 | ||||
-rw-r--r-- | dhcp-model.lua | 189 |
3 files changed, 201 insertions, 8 deletions
diff --git a/dhcp-controller.lua b/dhcp-controller.lua index 13639f0..5f36d7f 100644 --- a/dhcp-controller.lua +++ b/dhcp-controller.lua @@ -33,6 +33,22 @@ listsubnets = function ( self ) return self.model.get_subnets() end +edithost = function ( self ) + return controllerfunctions.handle_form(self, function() return self.model.host_read(self.clientdata.host) end, self.model.host_update, self.clientdata, "Save", "Edit Host", "Host Settings Updated") +end + +createhost = function ( self ) + return controllerfunctions.handle_form(self, self.model.create_new_host, self.model.host_create, self.clientdata, "Create", "Create new host", "New host Created") +end + +delhost = function(self) + return self:redirect_to_referrer(self.model.host_delete(self.clientdata.host)) +end + +listhosts = function ( self ) + return self.model.get_hosts() +end + viewleases = function ( self ) return self.model.getleases() end diff --git a/dhcp-home-html.lsp b/dhcp-home-html.lsp index 1591f76..4962d4f 100644 --- a/dhcp-home-html.lsp +++ b/dhcp-home-html.lsp @@ -18,9 +18,9 @@ end %> viewlibrary.dispatch_component("listsubnets") end %> -<% --[[if viewlibrary and viewlibrary.dispatch_component then +<% if viewlibrary and viewlibrary.dispatch_component then viewlibrary.dispatch_component("listhosts") -end --]] %> +end %> <% if viewlibrary and viewlibrary.dispatch_component then viewlibrary.dispatch_component("startstop") diff --git a/dhcp-model.lua b/dhcp-model.lua index 11c0228..94aeadb 100644 --- a/dhcp-model.lua +++ b/dhcp-model.lua @@ -24,7 +24,7 @@ local replaceentry = function(file, configentry, newstring) if newstring then return string.gsub(file, string.gsub(replacemagiccharacters(table.concat(configentry, "\n")), "\n", "%%s+"), replacemagiccharacters(newstring), 1) else - return string.gsub(file, string.gsub(replacemagiccharacters(table.concat(configentry, "\n")), "\n", "%%s+").."%s*;%s*\n?", "", 1) + return string.gsub(file, "[^\n%S]*"..string.gsub(replacemagiccharacters(table.concat(configentry, "\n")), "\n", "%%s+").."%s*;%s*\n?", "", 1) end end @@ -71,11 +71,24 @@ local parseconfigfile = function(file) end local validate_host = function( host ) - -- FIXME --- hostname --- ip --- mac - return true, host + local success = true + if host.value.host.value == "" or string.find(host.value.host.value, "[^%w.-]") then + host.value.host.errtxt = "Invalid host name" + success = false + end + if not validator.is_mac(host.value.mac.value) then + host.value.mac.errtxt = "Invalid mac address" + success = false + end + if host.value.addresses.value ~= "" then + for address in string.gmatch(host.value.addresses.value, "([^,%s]+),?%s*") do + if string.find(address, "[^%w.-]") then + host.value.addresses.errtxt = "Invalid domain name / IPv4 address" + success = false + end + end + end + return success, host end local validate_subnet = function( net ) @@ -178,6 +191,63 @@ local find_section_end = function(file, section_start) return i-1 end +local host_write = function(host) + local file = fs.read_file(configfile) + config = config or parseconfigfile(file) + + -- First, add the host line if necessary + local hostline = "host "..host.value.host.value + local found = false + for i,value in ipairs(config or {}) do + if value[1] == "host" and value[2] == host.value.host.value then + found = true + end + end + if not found then file = file.."\n"..hostline.." {\n}" end + + -- Now, find the host section + local host_start = select(2, string.find(file, replacemagiccharacters(hostline).."%s*{")) + local host_end = find_section_end(file, host_start) - 1 + host_start = string.find(file, "\n", host_start) + 1 + local hostcontent = string.sub(file, host_start, host_end) + + -- Update the host data + host.value.mac.replace = "hardware ethernet "..host.value.mac.value + if host.value.addresses.value ~= "" then + host.value.addresses.replace = "fixed-address "..host.value.addresses.value + end + + local hostconfig = parseconfigfile(hostcontent) + for i,value in ipairs(hostconfig or {}) do + if value[1] == "hardware" and value[2] == "ethernet" then + hostcontent = replaceentry(hostcontent, value, host.value.mac.replace) + host.value.mac.replace = nil + elseif value[1] == "fixed-address" then + hostcontent = replaceentry(hostcontent, value, host.value.addresses.replace) + host.value.addresses.replace = nil + end + end + + -- add in new lines at the top if they didn't exist + local newlines = {} + newlines[#newlines+1] = host.value.mac.replace + host.value.mac.replace = nil + newlines[#newlines+1] = host.value.addresses.replace + host.value.addresses.replace = nil + if #newlines > 0 then + for i,line in ipairs(newlines) do newlines[i] = " "..line end + newlines[#newlines+1] = hostcontent + hostcontent = table.concat(newlines, ";\n") + end + + -- The host is updated, put it into the file + file = string.sub(file, 1, host_start-1) .. hostcontent .. string.sub(file, host_end+1, -1) + + -- Finally, write out the new file + fs.write_file(configfile, string.gsub(file, "\n*$", "")) + config = nil +end + local subnet_write = function(net) local file = fs.read_file(configfile) config = config or parseconfigfile(file) @@ -333,6 +403,113 @@ function getstatus () return modelfunctions.getstatus(processname, packagename, "DHCP Status") end +create_new_host = function() + host = { + host = cfe({ label="Host Name" }), + mac = cfe({ label="MAC Address" }), + addresses = cfe({ label="Fixed Addresses", descr="Comma-separated addresses" }), + } + + return cfe({ type="group", value=host, label="Host" }) +end + +host_read = function( name ) + config = config or parseconfigfile(fs.read_file(configfile)) + local host = create_new_host() + host.value.host.value = name + + for j,k in ipairs(config) do + if k[1] == "host" and k[2] == name then + for i,value in ipairs(k.sub or {}) do + if value[1] == "hardware" and value[2] == "ethernet" then + host.value.mac.value = value[3] or "" + elseif value[1] == "fixed-address" then + host.value.addresses.value = table.concat(value, " ", 2) + end + end + break + end + end + + return host +end + +host_update = function( host ) + local success, host = validate_host( host ) + if not host.value.host.errtxt then + local previous_success = success + success = false + host.value.host.errtxt = "This host does not exist" + local hosts = get_hosts() + for i,ht in ipairs(hosts.value) do + if ht == host.value.host.value then + success = previous_success + host.value.host.errtxt = nil + break + end + end + end + if success then + host_write(host) + else + host.errtxt = "Failed to update host" + end + + return host +end + +host_create = function( host ) + local success, host = validate_host(host) + if not host.value.host.errtxt then + local hosts = get_hosts() + for i,ht in ipairs(hosts.value) do + if ht == host.value.host.value then + success = false + host.value.host.errtxt = "This host already exists" + break + end + end + end + if success then + host_write(host) + else + host.errtxt = "Failed to create host" + end + + return host +end + +host_delete = function(name) + local file = fs.read_file(configfile) + config = config or parseconfigfile(file) + local cmdresult = cfe({ value="Failed to delete host - not found", label="Delete host result" }) + local hosts = get_hosts() + for i,host in ipairs(hosts.value) do + if host == name then + local start, endd = string.find(file, "host%s*"..replacemagiccharacters(name).."[^{]*{") + endd = find_section_end(file, endd) + endd = string.find(file, "\n", endd) + file = string.sub(file, 1, start-1) .. string.sub(file, endd+1, -1) + fs.write_file(configfile, string.gsub(file, "\n*$", "")) + config = nil + end + end + + return cmdresult +end + +get_hosts = function () + config = config or parseconfigfile(fs.read_file(configfile)) + local retval = {} + for i,entry in ipairs(config) do + if string.lower(entry[1] or "") == "host" then + table.insert(retval, entry[2]) + end + end + + return cfe({ type="list", value=retval, label="Host list" }) +end + create_new_subnet = function() net = { subnet = cfe({ label="Subnet" }), |