From c6119c771d8fdc4f4794e03afd9a4f84b5cdbebe Mon Sep 17 00:00:00 2001 From: Guillaume Mazoyer Date: Thu, 23 Jul 2015 17:59:59 +0200 Subject: Rework hostname parameter check. Rename FQDN to hostname which seems more appropriate. Refactor match_fqdn / match_hostname function to use a more accurate way of checking a potential hostname. --- includes/config.defaults.php | 10 ++++----- includes/utils.php | 50 +++++++++++++++++++++++--------------------- index.php | 4 ++-- routers/bird.php | 24 ++++++++++----------- routers/cisco.php | 20 +++++++++--------- routers/juniper.php | 8 +++---- routers/quagga.php | 24 ++++++++++----------- 7 files changed, 71 insertions(+), 69 deletions(-) diff --git a/includes/config.defaults.php b/includes/config.defaults.php index bd1687f..0fbca1a 100644 --- a/includes/config.defaults.php +++ b/includes/config.defaults.php @@ -132,15 +132,15 @@ $config = array( ), // Documentation for the 'ping' query 'ping' => array( - 'command' => 'ping IP_ADDRESS|FQDN', - 'description' => 'Send 10 pings to the given destination.', - 'parameter' => 'The parameter must be an IPv4/IPv6 address (without mask) or a fully qualified domain name.
RFC1918 addresses, IPv6 starting with FD or FC, and IPv4 reserved ranges (0.0.0.0/8, 169.254.0.0/16, 192.0.2.0/24 and 224.0.0.0/4) may be refused.

Example of valid arguments:
' + 'command' => 'ping IP_ADDRESS|HOSTNAME', + 'description' => 'Send pings to the given destination.', + 'parameter' => 'The parameter must be an IPv4/IPv6 address (without mask) or a hostname.
RFC1918 addresses, IPv6 starting with FD or FC, and IPv4 reserved ranges (0.0.0.0/8, 169.254.0.0/16, 192.0.2.0/24 and 224.0.0.0/4) may be refused.

Example of valid arguments:
' ), // Documentation for the 'traceroute' query 'traceroute' => array( - 'command' =>'traceroute IP_ADDRESS|FQDN', + 'command' =>'traceroute IP_ADDRESS|HOSTNAME', 'description' => 'Display the path to a given destination.', - 'parameter' => 'The parameter must be an IPv4/IPv6 address (without mask) or a fully qualified domain name.
RFC1918 addresses, IPv6 starting with FD or FC, and IPv4 reserved ranges (0.0.0.0/8, 169.254.0.0/16, 192.0.2.0/24 and 224.0.0.0/4) may be refused.

Example of valid arguments:
' + 'parameter' => 'The parameter must be an IPv4/IPv6 address (without mask) or a hostname.
RFC1918 addresses, IPv6 starting with FD or FC, and IPv4 reserved ranges (0.0.0.0/8, 169.254.0.0/16, 192.0.2.0/24 and 224.0.0.0/4) may be refused.

Example of valid arguments:
' ) ), diff --git a/includes/utils.php b/includes/utils.php index 828832f..79888dd 100644 --- a/includes/utils.php +++ b/includes/utils.php @@ -142,24 +142,25 @@ function match_ipv6($ip, $ip_only = true) { } /** - * Test if a given parameter is a valid FQDN or not. + * Test if a given parameter is a valid hostname or not. * - * @param string $fqdn the parameter to test. - * @return boolean true if the parameter matches a valid FQDN, false otherwise. + * See: http://stackoverflow.com/a/4694816 + * http://stackoverflow.com/a/2183140 + * for references. + * + * @param string $hostname the parameter to test. + * @return boolean true if the parameter matches a valid hostname, false + * otherwise. */ -function match_fqdn($fqdn) { - $regex = '/(?=^.{4,255}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?show route IP_ADDRESS'); print(''); print(''); - print(''); - print(''); + print(''); + print(''); print(''); print(''); } diff --git a/routers/bird.php b/routers/bird.php index fe9b197..b1a75b8 100644 --- a/routers/bird.php +++ b/routers/bird.php @@ -26,21 +26,21 @@ final class Bird extends Router { protected function build_ping($destination) { $ping = null; - if (match_fqdn($destination)) { - $fqdn = $destination; - $destination = fqdn_to_ip_address($fqdn); + if (match_hostname($destination)) { + $hostname = $destination; + $destination = hostname_to_ip_address($hostname); if (!$destination) { - throw new Exception('No A or AAAA record found for '.$fqdn); + throw new Exception('No A or AAAA record found for '.$hostname); } } if (match_ipv4($destination)) { $ping = 'ping '.$this->global_config['tools']['ping_options'].' '. - (isset($fqdn) ? $fqdn : $destination); + (isset($hostname) ? $hostname : $destination); } else if (match_ipv6($destination)) { $ping = 'ping6 '.$this->global_config['tools']['ping_options'].' '. - (isset($fqdn) ? $fqdn : $destination); + (isset($hostname) ? $hostname : $destination); } else { throw new Exception('The parameter does not resolve to an IPv4/IPv6 address.'); } @@ -63,23 +63,23 @@ final class Bird extends Router { protected function build_traceroute($destination) { $traceroute = null; - if (match_fqdn($destination)) { - $fqdn = $destination; - $destination = fqdn_to_ip_address($fqdn); + if (match_hostname($destination)) { + $hostname = $destination; + $destination = hostname_to_ip_address($hostname); if (!$destination) { - throw new Exception('No A or AAAA record found for '.$fqdn); + throw new Exception('No A or AAAA record found for '.$hostname); } } if (match_ipv4($destination)) { $traceroute = $this->global_config['tools']['traceroute4'].' '. $this->global_config['tools']['traceroute_options'].' '. - (isset($fqdn) ? $fqdn : $destination); + (isset($hostname) ? $hostname : $destination); } else if (match_ipv6($destination)) { $traceroute = $this->global_config['tools']['traceroute6'].' '. $this->global_config['tools']['traceroute_options'].' '. - (isset($fqdn) ? $fqdn : $destination); + (isset($hostname) ? $hostname : $destination); } else { throw new Exception('The parameter does not resolve to an IPv4/IPv6 address.'); } diff --git a/routers/cisco.php b/routers/cisco.php index 0763bd7..e36a6bf 100644 --- a/routers/cisco.php +++ b/routers/cisco.php @@ -27,10 +27,10 @@ final class Cisco extends Router { $ping = null; if (match_ipv4($destination) || match_ipv6($destination) || - match_fqdn($destination)) { + match_hostname($destination)) { $ping = 'ping '.$destination.' repeat 10'; } else { - throw new Exception('The parameter is not an IPv4/IPv6 address or a FQDN.'); + throw new Exception('The parameter is not an IPv4/IPv6 address or a hostname.'); } if (($ping != null) && $this->has_source_interface_id()) { @@ -44,25 +44,25 @@ final class Cisco extends Router { $traceroute = null; if (match_ipv4($destination) || match_ipv6($destination) || - (match_fqdn($destination) && !$this->has_source_interface_id())) { + (match_hostname($destination) && !$this->has_source_interface_id())) { $traceroute = 'traceroute '.$destination; - } else if (match_fqdn($destination)) { - $fqdn = $destination; - $destination = fqdn_to_ip_address($fqdn); + } else if (match_hostname($destination)) { + $hostname = $destination; + $destination = hostname_to_ip_address($hostname); if (!$destination) { - throw new Exception('No A or AAAA record found for '.$fqdn); + throw new Exception('No A or AAAA record found for '.$hostname); } if (match_ipv4($destination)) { - $traceroute = 'traceroute ip '.(isset($fqdn) ? $fqdn : $destination); + $traceroute = 'traceroute ip '.(isset($hostname) ? $hostname : $destination); } else if (match_ipv6($destination)) { - $traceroute = 'traceroute ipv6 '.(isset($fqdn) ? $fqdn : $destination); + $traceroute = 'traceroute ipv6 '.(isset($hostname) ? $hostname : $destination); } else { throw new Exception('The parameter does not resolve to an IPv4/IPv6 address.'); } } else { - throw new Exception('The parameter is not an IPv4/IPv6 address or a FQDN.'); + throw new Exception('The parameter is not an IPv4/IPv6 address or a hostname.'); } if (($traceroute != null) && $this->has_source_interface_id() && diff --git a/routers/juniper.php b/routers/juniper.php index b5c3449..3eb34e5 100644 --- a/routers/juniper.php +++ b/routers/juniper.php @@ -27,10 +27,10 @@ final class Juniper extends Router { $ping = null; if (match_ipv4($destination) || match_ipv6($destination) || - match_fqdn($destination)) { + match_hostname($destination)) { $ping = 'ping count 10 rapid '.$destination; } else { - throw new Exception('The parameter is not an IPv4/IPv6 address or a FQDN.'); + throw new Exception('The parameter is not an IPv4/IPv6 address or a hostname.'); } if (($ping != null) && $this->has_source_interface_id()) { @@ -45,10 +45,10 @@ final class Juniper extends Router { if (match_ipv4($destination)) { $traceroute = 'traceroute as-number-lookup '.$destination; - } else if (match_ipv6($destination) || match_fqdn($destination)) { + } else if (match_ipv6($destination) || match_hostname($destination)) { $traceroute = 'traceroute '.$destination; } else { - throw new Exception('The parameter is not an IPv4/IPv6 address or a FQDN.'); + throw new Exception('The parameter is not an IPv4/IPv6 address or a hostname.'); } if (($traceroute != null) && $this->has_source_interface_id()) { diff --git a/routers/quagga.php b/routers/quagga.php index 1944273..9ce6606 100644 --- a/routers/quagga.php +++ b/routers/quagga.php @@ -26,21 +26,21 @@ final class Quagga extends Router { protected function build_ping($destination) { $ping = null; - if (match_fqdn($destination)) { - $fqdn = $destination; - $destination = fqdn_to_ip_address($fqdn); + if (match_hostname($destination)) { + $hostname = $destination; + $destination = hostname_to_ip_address($hostname); if (!$destination) { - throw new Exception('No A or AAAA record found for '.$fqdn); + throw new Exception('No A or AAAA record found for '.$hostname); } } if (match_ipv4($destination)) { $ping = 'ping '.$this->global_config['tools']['ping_options'].' '. - (isset($fqdn) ? $fqdn : $destination); + (isset($hostname) ? $hostname : $destination); } else if (match_ipv6($destination)) { $ping = 'ping6 '.$this->global_config['tools']['ping_options'].' '. - (isset($fqdn) ? $fqdn : $destination); + (isset($hostname) ? $hostname : $destination); } else { throw new Exception('The parameter does not resolve to an IPv4/IPv6 address.'); } @@ -63,23 +63,23 @@ final class Quagga extends Router { protected function build_traceroute($destination) { $traceroute = null; - if (match_fqdn($destination)) { - $fqdn = $destination; - $destination = fqdn_to_ip_address($fqdn); + if (match_hostname($destination)) { + $hostname = $destination; + $destination = hostname_to_ip_address($hostname); if (!$destination) { - throw new Exception('No A or AAAA record found for '.$fqdn); + throw new Exception('No A or AAAA record found for '.$hostname); } } if (match_ipv4($destination)) { $traceroute = $this->global_config['tools']['traceroute4'].' '. $this->global_config['tools']['traceroute_options'].' '. - (isset($fqdn) ? $fqdn : $destination); + (isset($hostname) ? $hostname : $destination); } else if (match_ipv6($destination)) { $traceroute = $this->global_config['tools']['traceroute6'].' '. $this->global_config['tools']['traceroute_options'].' '. - (isset($fqdn) ? $fqdn : $destination); + (isset($hostname) ? $hostname : $destination); } else { throw new Exception('The parameter does not resolve to an IPv4/IPv6 address.'); } -- cgit v1.2.3