diff options
Diffstat (limited to 'dhcp-model.lua')
-rw-r--r-- | dhcp-model.lua | 218 |
1 files changed, 128 insertions, 90 deletions
diff --git a/dhcp-model.lua b/dhcp-model.lua index 1b3c71d..f0c1ada 100644 --- a/dhcp-model.lua +++ b/dhcp-model.lua @@ -114,6 +114,7 @@ config_generate = function() tmpfile:write( "max-lease-time " .. settings.maxleasetime.value .. ";\n\n" ) -- get, validate and write subnet configurations to tmp config file + tmpfile:write( "###### SUBNET CONFIG BEGIN ######\n\n" ) subnets = get_subnets() local numnetworks = 0 for k,v in ipairs(subnets) do @@ -164,13 +165,6 @@ config_generate = function() msg = "Configuration Generation Failed!\n\n" .. spec2_msg return msg end - spec2_msg = generate_hosts( tmpfile, tmpfilename, net ) - if #spec2_msg > 0 then - tmpfile:close() - os.remove( tmpfilename ) - msg = "Configuration Generation Failed!\n\n" .. spec2_msg - return msg - end --- generate advanced part / drop in advancedfile = io.open( cfgdir .. net.name.value .. ".advanced", "r" ) if advancedfile ~= nil then @@ -185,6 +179,7 @@ config_generate = function() tmpfile:write( "}\n\n" ) end + tmpfile:write( "###### SUBNET CONFIG END ######\n\n" ) if numnetworks <= 0 then tmpfile:close() @@ -194,6 +189,13 @@ config_generate = function() return msg end + msg = generate_hosts( tmpfile ) + if #msg > 0 then + tmpfile:close() + os.remove( tmpfilename ) + return msg + end + config_generate_extconfig( tmpfile, "postconfig" ) tmpfile:close() @@ -216,7 +218,7 @@ end generate_pool = function( tmpfile, tmpfilename, net ) if not validator.is_ipv4( net.leaserangestart.value ) or not validator.is_ipv4( net.leaserangeend.value ) then - if net.unknownclients.value == "permit" then + if net.unknownclients.value == "allow" then msg = "Reason: permitted unknown clients but failed to define lease range!\n" return msg end @@ -226,12 +228,12 @@ generate_pool = function( tmpfile, tmpfilename, net ) --- pool header tmpfile:write( " pool {\n" ) - if net.unknownclients.value == "permit" then - tmpfile:write( " permit known clients;\n" ) - tmpfile:write( " permit unknown clients;\n" ) + if net.unknownclients.value == "allow" then + tmpfile:write( " allow known-clients;\n" ) + tmpfile:write( " allow unknown-clients;\n" ) else - tmpfile:write( " permit known clients;\n" ) - tmpfile:write( " deny unknown clients;\n" ) + tmpfile:write( " allow known-clients;\n" ) + tmpfile:write( " deny unknown-clients;\n" ) end tmpfile:write( " range " .. net.leaserangestart.value .. " " .. net.leaserangeend.value .. ";\n" ) tmpfile:write( " }\n" ) @@ -239,53 +241,103 @@ generate_pool = function( tmpfile, tmpfilename, net ) return "" end -generate_hosts = function( tmpfile, tmpfilename, net ) - --- generate static hosts - statichostsfile = io.open( cfgdir .. net.name.value .. ".static", "r" ) - if statichostsfile ~= nil then - statichosts = statichostsfile:read( "*a" ) - if statichosts == nil then - statichostsfile:close() - msg = "Configuration Generation Failed!\n\n" .. - "Reason: failed to read static hosts file for '" .. net.name.value .. "'" - return msg - end - msg = validate_statichosts( statichosts ) +generate_hosts = function( outfile ) + + retval = "" + + outfile:write( "\n####### STATIC HOSTS BEGIN ######\n\n" ) + + snets = get_subnets() + for k,v in ipairs(snets) do + msg = generate_hosts_persubnet( outfile, v ) if #msg > 0 then - statichostsfile:close() - msg = "Configuration Generation Failed!\n\n" .. - "Reason: " .. msg return msg end + end - --- loop through all hosts - done = false - hosttoken = tokenizer.new( statichosts, "\n" ) - while not done do - hosttoken, nexthost = tokenizer.next( hosttoken ) - if nexthost ~= nil then - if string.sub( nexthost, 1, 1) ~= "#" then - spectoken = tokenizer.new( nexthost, ";" ) - spectoken, hostname = tokenizer.next( spectoken ) - spectoken, ip = tokenizer.next( spectoken ) - spectoken, mac = tokenizer.next( spectoken ) - spectoken, comment = tokenizer.next( spectoken ) - tmpfile:write(" host " .. hostname .. " {\n") - tmpfile:write(" hardware ethernet " .. mac .. ";\n") - tmpfile:write(" fixed-address " .. ip .. ";\n") - tmpfile:write(" }\n") + retval = generate_hosts_dynamic( outfile ) + + return retval +end + +generate_hosts_persubnet = function( outfile, netname ) + + retval = "" + + hostsfile = io.open( cfgdir .. netname .. ".static", "r" ) + if hostsfile ~= nil then + hostsdata = hostsfile:read( "*a" ) + if hostsdata ~= nil then + outfile:write( "# " .. netname .. "\n" ) + outfile:write( "group {\n" ) + local done = false + hosttoken = tokenizer.new( hostsdata, "\n" ) + while not done do + hosttoken, nexthost = tokenizer.next( hosttoken ) + if nexthost ~= nil then + if string.sub( nexthost, 1, 1) ~= "#" then + spectoken = tokenizer.new( nexthost, ";" ) + spectoken, hostname = tokenizer.next( spectoken ) + spectoken, ip = tokenizer.next( spectoken ) + spectoken, mac = tokenizer.next( spectoken ) + spectoken, comment = tokenizer.next( spectoken ) + outfile:write(" host " .. hostname .. " {\n") + outfile:write(" hardware ethernet " .. mac .. ";\n") + outfile:write(" fixed-address " .. ip .. ";\n") + outfile:write(" }\n") + end + else + done = true end - else - done = true end + outfile:write( "}\n\n" ) + outfile:write( "###### STATIC HOSTS END ######\n\n" ) + else + retval = "Configuration Generation Failed: Failed to read data from subnet static hosts file for " .. netname end - statichostsfile:close() + hostsfile:close() end - --- - if net.unknownclients.value == "permit" then - return "" - end + return retval +end + +generate_hosts_dynamic = function( outfile ) + + retval = "" + + hostsfile = io.open( cfgdir .. "dhcpd.dynamic", "r" ) + if hostsfile ~= nil then + hostsdata = hostsfile:read( "*a" ) + if hostsdata ~= nil then + outfile:write( "group {\n" ) + local done = false + hosttoken = tokenizer.new( hostsdata, "\n" ) + while not done do + hosttoken, nexthost = tokenizer.next( hosttoken ) + if nexthost ~= nil then + if string.sub( nexthost, 1, 1) ~= "#" then + spectoken = tokenizer.new( nexthost, ";" ) + spectoken, hostname = tokenizer.next( spectoken ) + spectoken, mac = tokenizer.next( spectoken ) + spectoken, comment = tokenizer.next( spectoken ) + outfile:write(" host " .. hostname .. " {\n") + outfile:write(" hardware ethernet " .. mac .. ";\n") + outfile:write(" }\n") + end + else + done = true + end + end + outfile:write( "}\n" ) + else + retval = "Configuration Generation Failed: Failed to read data from dynamic hosts file!" + end + end + + return retval +end + +generate_hosts_old = function( tmpfile, tmpfilename, net ) --- generate dynamic hosts dynamichostsfile = io.open( cfgdir .. net.name.value .. ".dynamic", "r" ) if dynamichostsfile ~= nil then @@ -340,6 +392,7 @@ advglobal_read = function() preconfig = "" postconfig = "" + dynamic = "" file = io.open( cfgdir .. "dhcpd.preconfig", "r" ) if file ~= nil then @@ -359,10 +412,19 @@ advglobal_read = function() file:close() end - return cfe({ preconfig = preconfig, postconfig = postconfig }) + file = io.open( cfgdir .. "dhcpd.dynamic", "r" ) + if file ~= nil then + dynamic = file:read( "*a" ) + if dynamic == nil then + dynamic = "" + end + file:close() + end + + return cfe({ preconfig = preconfig, postconfig = postconfig, dynamic = dynamic }) end -advglobal_update = function( preconfig, postconfig ) +advglobal_update = function( preconfig, postconfig, dynamic ) file = io.open( cfgdir .. "dhcpd.preconfig", "wb+" ) if file ~= nil then @@ -376,7 +438,13 @@ advglobal_update = function( preconfig, postconfig ) file:close() end - return cfe({ preconfig = preconfig, postconfig = postconfig }) + file = io.open( cfgdir .. "dhcpd.dynamic", "wb+" ) + if file ~= nil then + file:write( dynamic ) + file:close() + end + + return cfe({ preconfig = preconfig, postconfig = postconfig, dynamic = dynamic }) end subnet_read = function( name ) @@ -410,12 +478,11 @@ subnet_read = function( name ) net.unknownclients.value = string.sub(line, 18) end end - if net.unknownclients.value ~= "permit" then + if net.unknownclients.value ~= "allow" then net.unknownclients.value = "deny" end net.statichosts.value = subnet_get_spechosts( name, "static" ) - net.dynamichosts.value = subnet_get_spechosts( name, "dynamic" ) net.advanced.value = subnet_get_spechosts( name, "advanced" ) return net @@ -453,21 +520,6 @@ subnet_update_statichosts = function( name, statichosts ) return msg end -subnet_update_dynamichosts = function( name, dynamichosts ) - local msg = ""; - local filename = cfgdir .. name .. ".dynamic" - - file, errmsg = io.open( filename, "wb+" ) - if file == nil then - msg = "Error: Failed to open " .. filename .. "(" .. errmsg .. ")!" - else - file:write( dynamichosts ) - file:close() - end - - return msg -end - subnet_update_advanced = function( name, advanced ) local msg = ""; local filename = cfgdir .. name .. ".advanced" @@ -534,20 +586,6 @@ subnet_write = function( net ) table.insert( fields, "statichosts" ) end - if net.unknownclients.value == "deny" then - spec_msg = validate_dynamichosts( net.dynamichosts.value ) - if #spec_msg == 0 then - spec_msg = subnet_update_dynamichosts( net.name.value, net.dynamichosts.value ) - if #spec_msg > 0 then - msg = spec_msg - table.insert( fields, "dynamichosts" ) - end - else - msg = spec_msg - table.insert( fields, "dynamichosts" ) - end - end - spec_msg = subnet_update_advanced( net.name.value, net.advanced.value ) if #spec_msg > 0 then msg = spec_msg @@ -616,17 +654,17 @@ validate_dynamichosts = function( dynamichosts ) fieldtoken, mac = tokenizer.next( fieldtoken ) fieldtoken, comment = tokenizer.next( fieldtoken ) if hostname == nil then - msg = msg .. "Static Hosts: hostname missing on line " .. line .. "!\n" + msg = msg .. "Dynamic Hosts: hostname missing on line " .. line .. "!\n" else if not is_valid_hostname( hostname ) then - msg = msg .. "Static Hosts: Invalid hostname on line " .. line .. "!\n" + msg = msg .. "Dynamic Hosts: Invalid hostname on line " .. line .. "!\n" end end if mac == nil then - msg = msg .. "Static Hosts: mac missing on line " .. line .. "!\n" + msg = msg .. "Dynamic Hosts: mac missing on line " .. line .. "!\n" else if not validator.is_mac( mac ) then - msg = msg .. "Static Hosts: Invalid mac on line " .. line .. "!\n" + msg = msg .. "Dynamic Hosts: Invalid mac on line " .. line .. "!\n" end end end @@ -853,7 +891,7 @@ create_new_net = function( name, defleasetime, maxleasetime, gateway, domainname advanced = { label="Advanced", value=nonil(advanced), type="text" }, useadvanced = { label="Use Advanced", value=nonil(useadvanced), type="text" } } - if net.unknownclients.value ~= "permit" then + if net.unknownclients.value ~= "allow" then net.unknownclients.value = "deny" end |