diff options
-rw-r--r-- | dhcp-controller.lua | 13 | ||||
-rw-r--r-- | dhcp-createnet-html.lsp | 6 | ||||
-rw-r--r-- | dhcp-editnet-html.lsp | 34 | ||||
-rw-r--r-- | dhcp-editspc-html.lsp | 9 | ||||
-rw-r--r-- | dhcp-model.lua | 218 |
5 files changed, 152 insertions, 128 deletions
diff --git a/dhcp-controller.lua b/dhcp-controller.lua index bb85950..5c7e6e4 100644 --- a/dhcp-controller.lua +++ b/dhcp-controller.lua @@ -132,7 +132,7 @@ editnet = function ( self ) tmp = self.clientdata dynamicx = tmp.dynamichosts advancedx = tmp.advanced - if tmp.unknownclients == "permit" then + if tmp.unknownclients == "allow" then dynamicx = tmp.dynamicx end if tmp.useadvanced ~= "use" then @@ -162,9 +162,16 @@ editspc = function ( self ) } if self.clientdata.cmd == "update" then + msg = "" + fields = {} tmp = self.clientdata - value = self.model.advglobal_update( tmp.preconfig, tmp.postconfig ) - return ( cfe({ option = option, value = value, errcode = { msg="", fields={} }}) ) + errmsg = self.model.validate_dynamichosts( tmp.dynamic ) + if #errmsg > 0 then + msg = errmsg + table.insert(fields, "dynamichosts") + end + value = self.model.advglobal_update( tmp.preconfig, tmp.postconfig, tmp.dynamic ) + return ( cfe({ option = option, value = value, errcode = { msg=msg, fields=fields }}) ) end value = self.model.advglobal_read() diff --git a/dhcp-createnet-html.lsp b/dhcp-createnet-html.lsp index dc5d88f..fd80622 100644 --- a/dhcp-createnet-html.lsp +++ b/dhcp-createnet-html.lsp @@ -29,11 +29,11 @@ <tr><td><nobr>Lease Range End:</nobr></td><td><input type="text" name="leaserangeend" value="<? io.write(net.leaserangeend.value) ?>"></td></tr> <tr><td><nobr>Unknown Clients</nobr></td><td><select name="unknownclients" size="1"> <? - if net.unknownclients.value == "permit" then - io.write(" <option selected>permit</option>\n"); + if net.unknownclients.value == "allow" then + io.write(" <option selected>allow</option>\n"); io.write(" <option>deny</option>\n"); else - io.write(" <option>permit</option>\n"); + io.write(" <option>allow</option>\n"); io.write(" <option selected>deny</option>\n"); end ?> diff --git a/dhcp-editnet-html.lsp b/dhcp-editnet-html.lsp index d2b4c75..2287bcb 100644 --- a/dhcp-editnet-html.lsp +++ b/dhcp-editnet-html.lsp @@ -6,20 +6,6 @@ ?> <script language="JavaScript"> - function handleDynChg() { - if (document.myform.unknownclients.value == "permit") { - document.getElementById('dynamic').innerHTML = ''; - } else { - document.getElementById('dynamic').innerHTML = '<textarea name="dynamichosts" style="width:600px">' + document.myform.dynamicx.value + '</textarea>'; - } - } - - function initDynChg() { - if (document.myform.unknownclients.value == "deny") { - document.getElementById('dynamic').innerHTML = '<textarea name="dynamichosts" style="width:600px">' + document.myform.dynamicx.value + '</textarea>'; - } - } - function handleAdvChg() { if (document.myform.useadvanced.checked == true) { document.getElementById('advanced').innerHTML = '<textarea name="advanced" style="width:600px">' + document.myform.advancedx.value + '</textarea>'; @@ -56,11 +42,11 @@ <tr><td><nobr>Lease Range End:</nobr></td><td><input type="text" name="leaserangeend" value="<? io.write(net.leaserangeend.value) ?>"></td></tr> <tr><td><nobr>Unknown Clients</nobr></td><td><select name="unknownclients" size="1" onChange="javascript:handleDynChg();"> <? - if net.unknownclients.value == "permit" then - io.write(" <option selected>permit</option>\n"); + if net.unknownclients.value == "allow" then + io.write(" <option selected>allow</option>\n"); io.write(" <option>deny</option>\n"); else - io.write(" <option>permit</option>\n"); + io.write(" <option>allow</option>\n"); io.write(" <option selected>deny</option>\n"); end ?> @@ -79,18 +65,8 @@ Enter one host per line in the format: <i>hostname;ip;mac;comment</i><br> <table> <tr><td><textarea name="statichosts" style="width:600px;"><? io.write( net.statichosts.value ) ?></textarea></td></tr> </table><br> -<h2>Subnet: Dynamic Hosts</h2> -Enter one host per line in the format: <i>hostname;mac;comment</i><br> -<table> -<tr><td>Hostname</td><td>[a-z], [0-9], -</td></tr> -<tr><td>MAC</td><td>XX:XX:XX:XX:XX:XX</td></tr> -</table> -<table> -<tr><td><div id="dynamic"></div></td></tr> -</table><br> -<input type="hidden" name="advancedx" value="<? io.write(net.advanced.value) ?>"> -<input type="hidden" name="dynamicx" value="<? io.write(net.dynamichosts.value) ?>"> +<input type="hidden" name="advancedx" value="<? io.write(net.advanced.value) ?>"> <h2>Subnet: Advanced Configuration</h2> <input type="checkbox" name="useadvanced" value="use" onChange="javascript:handleAdvChg();"> <i>Activate Advanced Configuration - use with caution!</i><br><br> <table> @@ -104,6 +80,4 @@ Enter one host per line in the format: <i>hostname;mac;comment</i><br> <input type=submit name="cmd" value="back" style="width:100px"></form></td></tr> </table> </form> -<body onload="initDynChg();"> -</body> diff --git a/dhcp-editspc-html.lsp b/dhcp-editspc-html.lsp index 8a06c9c..922fe24 100644 --- a/dhcp-editspc-html.lsp +++ b/dhcp-editspc-html.lsp @@ -4,13 +4,18 @@ local errcode = form.errcode ?> <h1>DHCPd - Advanced Global Configuration</h1> -These fields are copied into the final dhcpd.conf on configuration generation without any validation check. So only -use them if you extactly know what you are doing here. <form name="myform" action="<? io.write(option.script .. option.prefix .. option.controller .. "/" .. option.action .. option.extra) ?>" method="POST"> +<pre style="color: #ff2020;"><? io.write( form.errcode.msg ) ?></pre> + +<h2>DHCPd - Dynamic Hosts</h2> +<table><tr><td><textarea name="dynamic" style="width:600px"><? io.write( form.value.dynamic ) ?></textarea></td></tr></table> + <h2>DHCPd - Pre Main Configuration</h2> +These fields below are copied into the final dhcpd.conf on configuration generation without any validation check. Do not use +them unless you know what you are doing. <table><tr><td><textarea name="preconfig" style="width:600px"><? io.write(form.value.preconfig) ?></textarea></td></tr></table> <h2>DHCPd - Post Main Configuration</h2> 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 |