diff --git a/data.php b/data.php index 23c48b02..2b822018 100644 --- a/data.php +++ b/data.php @@ -3,10 +3,22 @@ $setupVars = parse_ini_file("/etc/pihole/setupVars.conf"); $hosts = file_exists("/etc/hosts") ? file("/etc/hosts") : array(); - $log = new \SplFileObject('/var/log/pihole.log'); - $gravity = new \SplFileObject('/etc/pihole/list.preEventHorizon'); - $whitelist = new \SplFileObject('/etc/pihole/whitelist.txt'); - $blacklist = new \SplFileObject('/etc/pihole/blacklist.txt'); + + // Check if pihole.log exists and is readable + $logListName = checkfile("/var/log/pihole.log"); + $log = new \SplFileObject($logListName); + + // Check if preEventHorizon exists and is readable + $gravityListName = checkfile("/etc/pihole/list.preEventHorizon"); + $gravity = new \SplFileObject($gravityListName); + + // whitelist.txt is optional and might not be there + $whiteListFile = checkfile("/etc/pihole/whitelist.txt"); + $whitelist = new \SplFileObject($whiteListFile); + + // blacklist.txt is optional and might not be there + $blackListFile = checkfile("/etc/pihole/blacklist.txt"); + $blacklist = new \SplFileObject($blackListFile); /******* Public Members ********/ function getSummaryData() { @@ -238,8 +250,9 @@ /******** Private Members ********/ function gravityCount() { - $preEventHorizon = exec("grep -c ^ /etc/pihole/list.preEventHorizon"); - $blacklist = exec("grep -c ^ /etc/pihole/blacklist.txt"); + global $gravityListName,$blackListFile; + $preEventHorizon = exec("grep -c ^ $gravityListName"); + $blacklist = exec("grep -c ^ $blackListFile"); return ($preEventHorizon + $blacklist); } @@ -255,7 +268,8 @@ } function countDnsQueries() { - return exec("grep -c \": query\\[\" /var/log/pihole.log"); + global $logListName; + return exec("grep -c \": query\\[\" $logListName"); } function getDnsQueriesAll(\SplFileObject $log) { @@ -350,7 +364,8 @@ } function countBlockedQueries() { - return exec("grep \"gravity.list\" /var/log/pihole.log | grep -c \" is \""); + global $logListName; + return exec("grep \"gravity.list\" $logListName | grep -c \" is \""); } function getForwards(\SplFileObject $log) { diff --git a/php/auth.php b/php/auth.php index 4d87d975..7972480b 100644 --- a/php/auth.php +++ b/php/auth.php @@ -15,9 +15,12 @@ function log_and_die($message) { } function check_cors() { + $setupVars = parse_ini_file("/etc/pihole/setupVars.conf"); + $ipv4 = isset($setupVars["IPV4_ADDRESS"]) ? explode("/", $setupVars["IPV4_ADDRESS"])[0] : $_SERVER['SERVER_ADDR']; + // Check CORS $AUTHORIZED_HOSTNAMES = array( - 'http://' . $_SERVER['SERVER_ADDR'], + 'http://' . $ipv4, 'http://' . $_SERVER['SERVER_NAME'], 'http://pi.hole', 'http://localhost' @@ -30,8 +33,16 @@ function check_cors() { // Since the Host header is easily manipulated, we can only check if it's wrong and can't use it // to validate that the client is authorized, only unauthorized. - if(isset($_SERVER['HTTP_HOST']) && !in_array("http://".$_SERVER['HTTP_HOST'], $AUTHORIZED_HOSTNAMES)) { - log_and_die("Failed Host Check: " . $_SERVER['HTTP_HOST'] .' vs '. join(', ', $AUTHORIZED_HOSTNAMES)); + $server_host = $_SERVER['HTTP_HOST']; + + // If HTTP_HOST contains a non-standard port (!= 80) we have to strip the port + if(strpos($server_host,":")) + { + $server_host = parse_url($_SERVER['HTTP_HOST'], PHP_URL_HOST); + } + + if(isset($_SERVER['HTTP_HOST']) && !in_array("http://".$server_host, $AUTHORIZED_HOSTNAMES)) { + log_and_die("Failed Host Check: " . $server_host .' vs '. join(', ', $AUTHORIZED_HOSTNAMES)); } if(isset($_SERVER['HTTP_ORIGIN'])) { diff --git a/php/func.php b/php/func.php index 060b4f43..37ef790f 100644 --- a/php/func.php +++ b/php/func.php @@ -7,4 +7,16 @@ function is_valid_domain_name($domain_name) preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $domain_name)); //length of each label } -?> \ No newline at end of file +function checkfile($filename) { + if(is_readable($filename)) + { + return $filename; + } + else + { + // substitute dummy file + return "/dev/null"; + } +} + +?> diff --git a/php/get.php b/php/get.php index eeae056c..fada75b9 100644 --- a/php/get.php +++ b/php/get.php @@ -7,7 +7,9 @@ $type = $_GET['list']; if($type !== "white" && $type !== "black") die("Invalid list parameter"); -$rawList = file_get_contents("/etc/pihole/${type}list.txt"); +require "func.php"; + +$rawList = file_get_contents(checkfile("/etc/pihole/${type}list.txt")); $list = explode("\n", $rawList); // Get rid of empty lines diff --git a/settings.php b/settings.php index 87bfc815..2ae032ea 100644 --- a/settings.php +++ b/settings.php @@ -106,10 +106,17 @@ { $DHCP = false; // Try to guess initial settings - $DHCPdomain = explode(".",$piHoleIPv4); - $DHCPstart = $DHCPdomain[0].".".$DHCPdomain[1].".".$DHCPdomain[2].".201"; - $DHCPend = $DHCPdomain[0].".".$DHCPdomain[1].".".$DHCPdomain[2].".251"; - $DHCProuter = $DHCPdomain[0].".".$DHCPdomain[1].".".$DHCPdomain[2].".1"; + if($piHoleIPv4 !== "unknown") { + $DHCPdomain = explode(".",$piHoleIPv4); + $DHCPstart = $DHCPdomain[0].".".$DHCPdomain[1].".".$DHCPdomain[2].".201"; + $DHCPend = $DHCPdomain[0].".".$DHCPdomain[1].".".$DHCPdomain[2].".251"; + $DHCProuter = $DHCPdomain[0].".".$DHCPdomain[1].".".$DHCPdomain[2].".1"; + } + else { + $DHCPstart = ""; + $DHCPend = ""; + $DHCProuter = ""; + } } if(isset($setupVars["PIHOLE_DOMAIN"])){ $piHoleDomain = $setupVars["PIHOLE_DOMAIN"]; @@ -361,7 +368,7 @@ { $excludedDomains = explode(",", $setupVars["API_EXCLUDE_DOMAINS"]); } else { - $excludedDomains = ""; + $excludedDomains = []; } // Exluded clients in API Query Log call @@ -369,7 +376,7 @@ { $excludedClients = explode(",", $setupVars["API_EXCLUDE_CLIENTS"]); } else { - $excludedClients = ""; + $excludedClients = []; } // Exluded clients