From 915bbb3e5dc1c54edc05e2f10d87e1d98c12477a Mon Sep 17 00:00:00 2001 From: DL6ER Date: Wed, 25 Jan 2017 11:44:17 +0100 Subject: [PATCH] Add and remove static DHCPv4 releases using Settings page, prevent adding more than one entry with the same MAC address --- scripts/pi-hole/php/savesettings.php | 78 +++++++++++++++++++++++++++- settings.php | 14 +++-- 2 files changed, 87 insertions(+), 5 deletions(-) diff --git a/scripts/pi-hole/php/savesettings.php b/scripts/pi-hole/php/savesettings.php index 5480dd0e..660fac72 100644 --- a/scripts/pi-hole/php/savesettings.php +++ b/scripts/pi-hole/php/savesettings.php @@ -38,6 +38,34 @@ function validMAC($mac_addr) return (preg_match('/([a-fA-F0-9]{2}[:]?){6}/', $mac_addr) == 1); } +$dhcp_static_leases = array(); +function readStaticLeasesFile() +{ + global $dhcp_static_leases; + $dhcp_static_leases = array(); + $dhcpstatic = @fopen('/etc/dnsmasq.d/04-pihole-static-dhcp.conf', 'r'); + + if(!is_resource($dhcpstatic)) + return false; + + while(!feof($dhcpstatic)) + { + // Remove any possibly existing variable with this name + $mac = ""; + sscanf(trim(fgets($dhcpstatic)),"dhcp-host=%[^,],%[^,],%[^,]",$mac,$one,$two); + if(strlen($mac) > 0) + { + if(validIP($one) && strlen($two) == 0) + array_push($dhcp_static_leases,["hwaddr"=>$mac, "IP"=>$one, "host"=>""]); + elseif(strlen($two) == 0) + array_push($dhcp_static_leases,["hwaddr"=>$mac, "IP"=>"", "host"=>$one]); + else + array_push($dhcp_static_leases,["hwaddr"=>$mac, "IP"=>$one, "host"=>$two]); + } + } + return true; +} + $DNSserverslist = [ "8.8.8.8" => "Google (Primary)", "208.67.222.222" => "OpenDNS (Primary)", @@ -328,19 +356,67 @@ function validMAC($mac_addr) $mac = $_POST["AddMAC"]; $ip = $_POST["AddIP"]; $hostname = $_POST["AddHostname"]; + if(!validMAC($mac)) { $error .= "MAC address (".htmlentities($mac).") is invalid!
"; } $mac = strtoupper($mac); - if(!validIP($ip)) + + if(!validIP($ip) && strlen($ip) > 0) { $error .= "IP address (".htmlentities($ip).") is invalid!
"; } + if(!validDomain($hostname) && strlen($hostname) > 0) { $error .= "Host name (".htmlentities($hostname).") is invalid!
"; } + + if(strlen($hostname) == 0 && strlen($ip) == 0) + { + $error .= "You can not omit both the IP address and the host name!
"; + } + + if(strlen($hostname) == 0) + $hostname = "nohost"; + + if(strlen($ip) == 0) + $ip = "noip"; + + // Test if this MAC address is already included + readStaticLeasesFile(); + foreach($dhcp_static_leases as $lease) { + if($lease["hwaddr"] === $mac) + { + $error .= "Static release for MAC address (".htmlentities($mac).") already defined!
"; + break; + } + } + + if(!strlen($error)) + { + exec("sudo pihole -a addstaticdhcp ".$mac." ".$ip." ".$hostname); + $success .= "A new static address has been added"; + } + break; + } + + if(isset($_POST["removestatic"])) + { + $debug = true; + $mac = $_POST["removestatic"]; + if(!validMAC($mac)) + { + $error .= "MAC address (".htmlentities($mac).") is invalid!
"; + } + $mac = strtoupper($mac); + + if(!strlen($error)) + { + exec("sudo pihole -a removestaticdhcp ".$mac); + $success .= "The static address with MAC address ".htmlentities($mac)." has been added"; + } break; } diff --git a/settings.php b/settings.php index c8bc726f..0494667f 100644 --- a/settings.php +++ b/settings.php @@ -257,8 +257,10 @@ // Read leases file $leasesfile = true; - $dhcpleases = fopen('/etc/pihole/dhcp.leases', 'r') or $leasesfile = false; - $dhcp_leases = []; + $dhcpleases = @fopen('/etc/pihole/dhcp.leases', 'r'); + if(!is_resource($dhcpleases )) + $leasesfile = false; + $dhcp_leases = array(); function convertseconds($argument) { $seconds = round($argument); @@ -325,9 +327,12 @@ array_push($dhcp_leases,["TIME"=>$time, "hwaddr"=>$line[1], "IP"=>$line[2], "host"=>$host, "clid"=>$clid, "type"=>$type]); } } + + readStaticLeasesFile(); + ?>
-
+
collapsed-box">

DHCP leases

@@ -359,10 +364,11 @@ - + +

Specifying the MAC address is mandatory. If the IP address is omitted, then it will be generated dynamically. Hoever, the specified host name is used instead of the host name the machine claims to be using. If the host name is omitted, only a static release will be added.