diff options
Diffstat (limited to 'dhcp-model.lua')
-rw-r--r-- | dhcp-model.lua | 186 |
1 files changed, 158 insertions, 28 deletions
diff --git a/dhcp-model.lua b/dhcp-model.lua index ca02a08..9a0839c 100644 --- a/dhcp-model.lua +++ b/dhcp-model.lua @@ -2,10 +2,9 @@ -- Copyright(c) 2007 A. Brodmann - Licensed under terms of GPL2 module (..., package.seeall) -require("lfs") require("validator") -local subnet = { } +local subnet = {} local cfgdir = "/etc/dhcp/" dep_check = function () @@ -39,6 +38,108 @@ dep_check = function () return retval end +config_generate = function() + + msg = "" + tmpfilename = os.tmpname() + + -- create tmp config file + local tmpfile = io.open( tmpfilename, "w+" ) + + -- get, validate and write global settings to tmp config file + settings = read_settings() + s_msg, s_fields = validate_settings( settings ) + if #s_msg > 0 then + tmpfile:close() + os.remove( tmpfilename ) + msg = "Configuration Generation Failed!\n\n" .. + "Reason: Error in Global Settings\n" + return msg + end + + tmpfile:write( "authoritative;\n" ) + tmpfile:write( "ddns-update-style none;\n\n" ) + tmpfile:write( "option local-wpad-server code 252 = text;\n\n" ) + if #settings.domainname.value > 0 then + tmpfile:write( "option domain-name \"" .. settings.domainname.value .. "\";\n" ) + end + tmpfile:write( "default-lease-time " .. settings.defleasetime.value .. ";\n" ) + tmpfile:write( "max-lease-time " .. settings.maxleasetime.value .. ";\n\n" ) + + -- get, validate and write subnet configurations to tmp config file + subnets = get_subnets() + local numnetworks = 0 + for k,v in ipairs(subnets) do + numnetworks = numnetworks + 1 + net = subnet_read( v ) + sn_msg, sn_fields = validate_network( net ) + if #sn_msg > 0 then + tmpfile:close() + os.remove( tmpfilename ) + msg = "Configuration Generation Failed!\n\n" .. + "Reason: Error in Subnet '" .. v .. "'\n" + return msg + end + + tmpfile:write( "# " .. net.name.value .. "\n" ) + tmpfile:write( "subnet " .. net.subnet.value .. " netmask " .. net.netmask.value .. " {\n" ) + if #net.defleasetime.value > 0 then + tmpfile:write( " default-lease-time " .. net.defleasetime.value .. ";\n" ) + end + if #net.maxleasetime.value > 0 then + tmpfile:write( " max-lease-time " .. net.maxleasetime.value .. ";\n" ) + end + tmpfile:write( " option routers " .. net.gateway.value .. ";\n" ) + dnssrvrs = "" + if #net.dnssrv1.value > 0 then + dnssrvrs = net.dnssrv1.value + end + if #net.dnssrv2.value > 0 then + if #dnssrvrs > 0 then + dnssrvrs = dnssrvrs .. ", " .. net.dnssrv2.value + else + dnssrvrs = net.dnssrv2.value + end + end + if #dnssrvrs > 0 then + tmpfile:write( " option domain-name-servers " .. dnssrvrs .. ";\n" ) + end + if #net.domainname.value > 0 then + tmpfile:write( " option domain-name \"" .. net.domainname.value .. "\";\n" ) + end + if #net.leaserangestart.value > 0 then + tmpfile:write( " range " .. net.leaserangestart.value .. " " .. net.leaserangeend.value .. ";\n" ) + end + if #net.wpad.value > 0 then + tmpfile:write( " option local-wpad-server \"" .. net.wpad.value .. "\\n\";\n" ) + end + tmpfile:write( "}\n\n" ) + end + + if numnetworks <= 0 then + tmpfile:close() + os.remove( tmpfilename ) + msg = "Configuration Generation Failed!\n\n" .. + "Reason: No Subnets defined!\n" + return msg + end + + io.close( tmpfile ) + os.rename( tmpfilename, "/etc/dhcp/dhcpd.conf" ) + + return "Configuration Generation Successful!\n" +end + +subnet_delete = function( name ) + + local msg = "" + + local filename = cfgdir .. name .. ".subnet" + os.remove( filename ) + + return msg +end + subnet_read = function( name ) local filename = cfgdir .. name .. ".subnet" local net = { name = cfe({ type="message", value=name, label="Name" }), @@ -52,7 +153,7 @@ subnet_read = function( name ) netmask = cfe({ label="Netmask" }), leaserangestart = cfe({ label="Lease Range Start" }), leaserangeend = cfe({ label="Lease Range End" }), - wpad = cfe({ label="Web Proxy Auto Discovery", type="select", option = { "yes", "no" } }) + wpad = cfe({ label="Web Proxy Auto Discovery", type="text", value="" }) } for line in io.lines(filename) do @@ -158,29 +259,39 @@ end validate_network = function( net ) fields = {} msg = "" - if #net.name.value < 4 then + if #net.name.value < 3 then table.insert(fields, "name") - msg = msg .. "Minimum network name length is 4 characters!\n" + msg = msg .. "Minimum network name length is 3 characters!\n" + end + if not is_valid_netname( net.name.value ) then + table.insert( fields, "name" ) + msg = msg .. "Invalid network name: allowed characters are: 'a..z', '0..9', '-'\n" end if net.name.value == "<new>" then table.insert(fields, "name") msg = msg .. "<new> is not a valid network name!\n" end - if not validator.is_integer_in_range(_tonumber(net.defleasetime.value), 1800, 86400) then - table.insert(fields, "defleasetime") - msg = msg .. "Default-Lease-Time must be: 1800 < x < 86400\n" + if #net.defleasetime.value > 0 then + if not validator.is_integer_in_range(_tonumber(net.defleasetime.value), 1800, 86400) then + table.insert(fields, "defleasetime") + msg = msg .. "Default-Lease-Time must be: 1800 < x < 86400\n" + end end - if not validator.is_integer_in_range(_tonumber(net.maxleasetime.value), 1800, 86400) then - table.insert(fields, "maxleasetime") - msg = msg .. "Maximum-Lease-Time must be: 1800 < x < 86400\n" + if #net.maxleasetime.value > 0 then + if not validator.is_integer_in_range(_tonumber(net.maxleasetime.value), 1800, 86400) then + table.insert(fields, "maxleasetime") + msg = msg .. "Maximum-Lease-Time must be: 1800 < x < 86400\n" + end end if not validator.is_ipv4(net.gateway.value) then table.insert(fields, "gateway") msg = msg .. "Gateway: invalid IPv4 address!\n" end - if not validator.is_ipv4(net.dnssrv1.value) then - table.insert(fields, "dnssrv1") - msg = msg .. "DNS Server 1: invalid IPv4 address!\n" + if #net.dnssrv1.value > 0 then + if not validator.is_ipv4(net.dnssrv1.value) then + table.insert(fields, "dnssrv1") + msg = msg .. "DNS Server 1: invalid IPv4 address!\n" + end end if not validator.is_ipv4(net.dnssrv2.value) then if #net.dnssrv2.value > 0 then @@ -196,14 +307,14 @@ validate_network = function( net ) table.insert(fields, "netmask") msg = msg .. "Netmask: invalid IPv4 address!\n" end - if not validator.is_ipv4(net.leaserangestart.value) then - if #net.leaserangestart.value > 0 then + if #net.leaserangestart.value > 0 then + if not validator.is_ipv4(net.leaserangestart.value) then table.insert(fields, "leaserangestart") msg = msg .. "Lease-Range-Start: invalid IPv4 address!\n" end end - if not validator.is_ipv4(net.leaserangeend.value) then - if #net.leaserangeend.value > 0 then + if #net.leaserangeend.value > 0 then + if not validator.is_ipv4(net.leaserangeend.value) then table.insert(fields, "leaserangeend") msg = msg .. "Lease-Range-End: invalid IPv4 address!\n" end @@ -256,7 +367,7 @@ end get_dhcpd_version = function() local retval = "dhcpd" - local file = io.popen("/usr/sbin/dhcpd --version") + local file = io.popen("/usr/sbin/dhcpd --version 2>&1") if file ~= nil then local line = file:read( "*a" ) if #line > 0 then @@ -296,11 +407,13 @@ end get_subnets = function () - local retval = retval or {} + local retval = {} - for sn in lfs.dir( cfgdir ) do - if string.sub(sn, -7) == ".subnet" then - table.insert(retval, string.sub(sn, 1, -8)) + lpos = require "posix" + files = lpos.dir( "/etc/dhcp" ) + for k,v in ipairs(files) do + if string.sub(v, -7) == ".subnet" then + table.insert(retval, string.sub(v, 1, -8)) end end @@ -319,12 +432,8 @@ create_new_net = function( name, defleasetime, maxleasetime, gateway, domainname netmask = { label="Netmask", value=nonil(netmask), type="text" }, leaserangestart = { label="Lease Range Start", value=nonil(leaserangestart), type="text" }, leaserangeend = { label="Lease Range End", value=nonil(leaserangeend), type="text" }, - wpad = { label="Web Proxy Auto Discovery", value=nonil(wpad), - type="select", value=nonil(wpad), option = { "yes", "no"} } + wpad = { label="Web Proxy Auto Discovery", value=nonil(wpad), value=nonil(wpad) } } - if net.wpad.value == "" then - net.wpad.value = "no" - end return net end @@ -380,4 +489,25 @@ is_valid_hostname = function ( hostname ) return retval end + +is_valid_netname = function ( netname ) + + local retval = true + + name = string.lower( netname ) + lap = 1 + while lap <= #name do + chr = string.sub( name, lap, lap ) + if (chr >= "a" and chr <= "z") or + (chr >= "0" and chr <= "9") or + (chr == "-") then + + else + retval = false + end + lap = lap + 1 + end + + return retval +end |