From 423591aa32c702334c6d0d8874b7dc7384f9716b Mon Sep 17 00:00:00 2001 From: trimalcione Date: Sun, 24 Nov 2019 22:54:07 +0100 Subject: [PATCH] add import/export of DHCP static leases Signed-off-by: trimalcione --- scripts/pi-hole/php/savesettings.php | 156 ++++++++++++--------------- scripts/pi-hole/php/teleporter.php | 3 - settings.php | 5 - 3 files changed, 68 insertions(+), 96 deletions(-) diff --git a/scripts/pi-hole/php/savesettings.php b/scripts/pi-hole/php/savesettings.php index ef09491c..ea6ae878 100644 --- a/scripts/pi-hole/php/savesettings.php +++ b/scripts/pi-hole/php/savesettings.php @@ -6,7 +6,7 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ -if(!in_array(basename($_SERVER['SCRIPT_FILENAME']), array("settings.php", "teleporter.php"), true)) +if(basename($_SERVER['SCRIPT_FILENAME']) !== "settings.php") { die("Direct access to this script is forbidden!"); } @@ -66,15 +66,18 @@ function validEmail($email) && escapeshellcmd($email) === $email; } -function processStaticLeasesFile($file_path) { - $dhcp_static_leases_local = array(); +$dhcp_static_leases = array(); +function readStaticLeasesFile() +{ + global $dhcp_static_leases; + $dhcp_static_leases = array(); try { - $dhcpstatic = @fopen($file_path, 'r'); + $dhcpstatic = @fopen('/etc/dnsmasq.d/04-pihole-static-dhcp.conf', 'r'); } catch(Exception $e) { - echo "Warning: Failed to read $file_path, this is not an error"; + echo "Warning: Failed to read /etc/dnsmasq.d/04-pihole-static-dhcp.conf, this is not an error"; return false; } @@ -90,34 +93,21 @@ function processStaticLeasesFile($file_path) { { if(validIP($one) && strlen($two) == 0) // dhcp-host=mac,IP - no HOST - array_push($dhcp_static_leases_local,["hwaddr"=>$mac, "IP"=>$one, "host"=>""]); + array_push($dhcp_static_leases,["hwaddr"=>$mac, "IP"=>$one, "host"=>""]); elseif(strlen($two) == 0) // dhcp-host=mac,hostname - no IP - array_push($dhcp_static_leases_local,["hwaddr"=>$mac, "IP"=>"", "host"=>$one]); + array_push($dhcp_static_leases,["hwaddr"=>$mac, "IP"=>"", "host"=>$one]); else // dhcp-host=mac,IP,hostname - array_push($dhcp_static_leases_local,["hwaddr"=>$mac, "IP"=>$one, "host"=>$two]); + array_push($dhcp_static_leases,["hwaddr"=>$mac, "IP"=>$one, "host"=>$two]); } else if(validIP($one) && validDomain($mac)) { // dhcp-host=hostname,IP - no MAC - array_push($dhcp_static_leases_local,["hwaddr"=>"", "IP"=>$one, "host"=>$mac]); + array_push($dhcp_static_leases,["hwaddr"=>"", "IP"=>$one, "host"=>$mac]); } } - return $dhcp_static_leases_local; -} - -$dhcp_static_leases = array(); -function readStaticLeasesFile() -{ - global $dhcp_static_leases; - $dhcp_static_leases = array(); - $result = processStaticLeasesFile('/etc/dnsmasq.d/04-pihole-static-dhcp.conf'); - if($result !== false) { - $dhcp_static_leases = $result; - return true; - } - return $result; + return true; } function isequal(&$argument, &$compareto) { @@ -195,66 +185,6 @@ function readAdlists() return $list; } -function addDHCPLease($mac, $ip, $hostname) { - $result = array('message' => "", 'value' => false); - - if(!validMAC($mac)) - { - $result['message'] .= "MAC address (".htmlspecialchars($mac).") is invalid!
"; - } - $mac = strtoupper($mac); - - if(!validIP($ip) && strlen($ip) > 0) - { - $result['message'] .= "IP address (".htmlspecialchars($ip).") is invalid!
"; - } - - if(!validDomain($hostname) && strlen($hostname) > 0) - { - $result['message'] .= "Host name (".htmlspecialchars($hostname).") is invalid!
"; - } - - if(strlen($hostname) == 0 && strlen($ip) == 0) - { - $result['message'] .= "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 lease is already included - readStaticLeasesFile(); - global $dhcp_static_leases; - foreach($dhcp_static_leases as $lease) { - if($lease["hwaddr"] === $mac) - { - $result['message'] .= "Static release for MAC address (".htmlspecialchars($mac).") already defined!
"; - break; - } - if($ip !== "noip" && $lease["IP"] === $ip) - { - $result['message'] .= "Static lease for IP address (".htmlspecialchars($ip).") already defined!
"; - break; - } - if($lease["host"] === $hostname) - { - $result['message'] .= "Static lease for hostname (".htmlspecialchars($hostname).") already defined!
"; - break; - } - } - - if(!strlen($result['message'])) - { - exec("sudo pihole -a addstaticdhcp ".$mac." ".$ip." ".$hostname); - $result['message'] = "A new static address has been added"; - $result['value'] = true; - } - return $result; -} - // Read available adlists $adlist = readAdlists(); // Read available DNS server list @@ -604,11 +534,61 @@ function addDHCPLease($mac, $ip, $hostname) { if(isset($_POST["addstatic"])) { - $result = addDHCPLease($_POST["AddMAC"], $_POST["AddIP"], $_POST["AddHostname"]); - if($result['value']) { - $success .= $result['message']; - } else { - $error .= $result['message']; + $mac = $_POST["AddMAC"]; + $ip = $_POST["AddIP"]; + $hostname = $_POST["AddHostname"]; + + if(!validMAC($mac)) + { + $error .= "MAC address (".htmlspecialchars($mac).") is invalid!
"; + } + $mac = strtoupper($mac); + + if(!validIP($ip) && strlen($ip) > 0) + { + $error .= "IP address (".htmlspecialchars($ip).") is invalid!
"; + } + + if(!validDomain($hostname) && strlen($hostname) > 0) + { + $error .= "Host name (".htmlspecialchars($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 lease is already included + readStaticLeasesFile(); + foreach($dhcp_static_leases as $lease) { + if($lease["hwaddr"] === $mac) + { + $error .= "Static release for MAC address (".htmlspecialchars($mac).") already defined!
"; + break; + } + if($ip !== "noip" && $lease["IP"] === $ip) + { + $error .= "Static lease for IP address (".htmlspecialchars($ip).") already defined!
"; + break; + } + if($lease["host"] === $hostname) + { + $error .= "Static lease for hostname (".htmlspecialchars($hostname).") already defined!
"; + break; + } + } + + if(!strlen($error)) + { + exec("sudo pihole -a addstaticdhcp ".$mac." ".$ip." ".$hostname); + $success .= "A new static address has been added"; } break; } diff --git a/scripts/pi-hole/php/teleporter.php b/scripts/pi-hole/php/teleporter.php index 8fb35702..87e7752b 100644 --- a/scripts/pi-hole/php/teleporter.php +++ b/scripts/pi-hole/php/teleporter.php @@ -9,7 +9,6 @@ require "password.php"; require "auth.php"; // Also imports func.php require "database.php"; -require_once "savesettings.php"; if (php_sapi_name() !== "cli") { if(!$auth) die("Not authorized"); @@ -53,7 +52,6 @@ function archive_add_table($name, $table) $archive[$name] = json_encode($content); } - /** * Restore the contents of a table from an uploaded archive * @@ -153,7 +151,6 @@ function archive_restore_table($file, $table, $flush=false) return $num; } -======= /** * Create table rows from an uploaded archive file * diff --git a/settings.php b/settings.php index 29094927..e4d8ad3f 100644 --- a/settings.php +++ b/settings.php @@ -1172,11 +1172,6 @@ if (isset($_GET['tab']) && in_array($_GET['tab'], array("sysadmin", "blocklists" checked> Audit log -
- -