diff options
Diffstat (limited to 'usr')
-rwxr-xr-x | usr/local/www/diag_dhcp_leases.php | 43 | ||||
-rwxr-xr-x | usr/local/www/services_dhcp.php | 47 |
2 files changed, 88 insertions, 2 deletions
diff --git a/usr/local/www/diag_dhcp_leases.php b/usr/local/www/diag_dhcp_leases.php index 003cb13..0a0292d 100755 --- a/usr/local/www/diag_dhcp_leases.php +++ b/usr/local/www/diag_dhcp_leases.php @@ -34,6 +34,41 @@ require("guiconfig.inc"); $pgtitle = "Diagnostics: DHCP leases"; + +$leasesfile = "{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases"; + +if (($_GET['deleteip']) && (is_ipaddr($_GET['deleteip']))) { + /* Stop DHCPD */ + killbyname("dhcpd"); + + /* Read existing leases */ + $leases_contents = explode("\n", file_get_contents($leasesfile)); + $newleases_contents = array(); + $i=0; + while ($i < count($leases_contents)) { + /* Find the lease(s) we want to delete */ + if ($leases_contents[$i] == "lease {$_GET['deleteip']} {") { + /* Skip to the end of the lease declaration */ + do { + $i++; + } while ($leases_contents[$i] != "}"); + } else { + /* It's a line we want to keep, copy it over. */ + $newleases_contents[] = $leases_contents[$i]; + } + $i++; + } + + /* Write out the new leases file */ + $fd = fopen($leasesfile, 'w'); + fwrite($fd, implode("\n", $newleases_contents)); + fclose($fd); + + /* Restart DHCP Service */ + services_dhcpd_configure(); + header("Location: diag_dhcp_leases.php"); +} + include("head.inc"); ?> @@ -63,7 +98,6 @@ function remove_duplicate($array, $field) return $new; } -$leasesfile = "{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases"; $awk = "/usr/bin/awk"; /* this pattern sticks comments into a single array item */ $cleanpattern = "'{ gsub(\"#.*\", \"\");} { gsub(\";\", \"\"); print;}'"; @@ -321,9 +355,14 @@ foreach ($leases as $data) { echo "<td class=\"list\" valign=\"middle\">"; echo "<img src=\"/themes/{$g['theme']}/images/icons/icon_plus_mo.gif\" width=\"17\" height=\"17\" border=\"0\"></td>\n"; } - echo "<td valign=\"middle\"><a href=\"services_wol_edit.php?if={$data['if']}&mac={$data['mac']}&descr={$data['hostname']}\">"; echo "<img src=\"/themes/{$g['theme']}/images/icons/icon_wol_all.gif\" width=\"17\" height=\"17\" border=\"0\" title=\"add a Wake on Lan mapping for this MAC address\"></a></td>\n"; + + /* Only show the button for offline dynamic leases */ + if (($data['type'] == "dynamic") && ($data['online'] != "online")) { + echo "<td class=\"list\" valign=\"middle\"><a href=\"diag_dhcp_leases.php?deleteip={$data['ip']}\">"; + echo "<img src=\"/themes/{$g['theme']}/images/icons/icon_x.gif\" width=\"17\" height=\"17\" border=\"0\" title=\"delete this dhcp lease\"></a></td>\n"; + } echo "</tr>\n"; } } diff --git a/usr/local/www/services_dhcp.php b/usr/local/www/services_dhcp.php index d5849ba..b5b75bd 100755 --- a/usr/local/www/services_dhcp.php +++ b/usr/local/www/services_dhcp.php @@ -31,6 +31,50 @@ require("guiconfig.inc"); +/* This function will remove entries from dhcpd.leases that would otherwise + * overlap with static DHCP reservations. If we don't clean these out, + * then DHCP will print a warning in the logs about a duplicate lease + */ +function dhcp_clean_leases() { + global $g, $config; + $leasesfile = "{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases"; + /* Build list of static MACs */ + $staticmacs = array(); + foreach($config['interfaces'] as $ifname => $ifarr) + if (is_array($config['dhcpd'][$ifname]['staticmap'])) + foreach($config['dhcpd'][$ifname]['staticmap'] as $static) + $staticmacs[] = $static['mac']; + /* Read existing leases */ + $leases_contents = explode("\n", file_get_contents($leasesfile)); + $newleases_contents = array(); + $i=0; + while ($i < count($leases_contents)) { + /* Find a lease definition */ + if (substr($leases_contents[$i], 0, 6) == "lease ") { + $templease = array(); + $thismac = ""; + /* Read to the end of the lease declaration */ + do { + if (substr($leases_contents[$i], 0, 20) == " hardware ethernet ") + $thismac = substr($leases_contents[$i], 20, 17); + $templease[] = $leases_contents[$i]; + $i++; + } while ($leases_contents[$i-1] != "}"); + /* Check for a matching MAC address and if not present, keep it. */ + if (! in_array($thismac, $staticmacs)) + $newleases_contents = array_merge($newleases_contents, $templease); + } else { + /* It's a line we want to keep, copy it over. */ + $newleases_contents[] = $leases_contents[$i]; + $i++; + } + } + /* Write out the new leases file */ + $fd = fopen($leasesfile, 'w'); + fwrite($fd, implode("\n", $newleases_contents)); + fclose($fd); +} + $if = $_GET['if']; if ($_POST['if']) $if = $_POST['if']; @@ -234,6 +278,9 @@ if ($_POST) { $retvaldhcp = 0; $retvaldns = 0; config_lock(); + /* Stop DHCP so we can cleanup leases */ + killbyname("dhcpd"); + dhcp_clean_leases(); /* dnsmasq_configure calls dhcpd_configure */ /* no need to restart dhcpd twice */ if (isset($config['dnsmasq']['regdhcpstatic'])) { |