summaryrefslogtreecommitdiffstats
path: root/dhcp-model.lua
diff options
context:
space:
mode:
Diffstat (limited to 'dhcp-model.lua')
-rw-r--r--dhcp-model.lua189
1 files changed, 183 insertions, 6 deletions
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" }),