diff --git a/src/www/diag_testport.php b/src/www/diag_testport.php index 6b4a5d1f7..1f95eef57 100644 --- a/src/www/diag_testport.php +++ b/src/www/diag_testport.php @@ -1,291 +1,238 @@ - Copyright (C) 2003-2005 Bob Zoller (bob@kludgebox.com) and Manuel Kasper . - All rights reserved. + Copyright (C) 2016 Deciso B.V. + Copyright (C) 2013 Jim Pingle + Copyright (C) 2003-2005 Bob Zoller (bob@kludgebox.com) and Manuel Kasper . + All rights reserved. - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. - THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, - INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY - AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, - OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - POSSIBILITY OF SUCH DAMAGE. + THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. */ require_once("guiconfig.inc"); require_once("system.inc"); require_once("interfaces.inc"); -define('NC_TIMEOUT', 10); -$do_testport = false; +$cmd_output = false; +if ($_SERVER['REQUEST_METHOD'] === 'GET') { + // set form defaults + $pconfig = array(); + $pconfig['ipprotocol'] = 'ipv4'; + $pconfig['host'] = null; + $pconfig['port'] = null; + $pconfig['showtext'] = null; + $pconfig['sourceip'] = null; +} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') { + $pconfig = $_POST; + $input_errors = array(); -if ($_POST || $_REQUEST['host']) { - unset($input_errors); + /* input validation */ + $reqdfields = explode(" ", "host port"); + $reqdfieldsn = array(gettext("Host"),gettext("Port")); + do_input_validation($pconfig, $reqdfields, $reqdfieldsn, $input_errors); - /* input validation */ - $reqdfields = explode(" ", "host port"); - $reqdfieldsn = array(gettext("Host"),gettext("Port")); - do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors); + if (!is_ipaddr($pconfig['host']) && !is_hostname($pconfig['host'])) { + $input_errors[] = gettext("Please enter a valid IP or hostname."); + } - if (!is_ipaddr($_REQUEST['host']) && !is_hostname($_REQUEST['host'])) { - $input_errors[] = gettext("Please enter a valid IP or hostname."); - } + if (!is_port($pconfig['port'])) { + $input_errors[] = gettext("Please enter a valid port number."); + } - if (!is_port($_REQUEST['port'])) { - $input_errors[] = gettext("Please enter a valid port number."); - } + if (($pconfig['srcport'] != "") && (!is_numeric($pconfig['srcport']) || !is_port($pconfig['srcport']))) { + $input_errors[] = gettext("Please enter a valid source port number, or leave the field blank."); + } - if (($_REQUEST['srcport'] != "") && (!is_numeric($_REQUEST['srcport']) || !is_port($_REQUEST['srcport']))) { - $input_errors[] = gettext("Please enter a valid source port number, or leave the field blank."); - } + if (is_ipaddrv4($pconfig['host']) && ($pconfig['ipprotocol'] == "ipv6")) { + $input_errors[] = gettext("You cannot connect to an IPv4 address using IPv6."); + } + if (is_ipaddrv6($pconfig['host']) && ($pconfig['ipprotocol'] == "ipv4")) { + $input_errors[] = gettext("You cannot connect to an IPv6 address using IPv4."); + } - if (is_ipaddrv4($_REQUEST['host']) && ($_REQUEST['ipprotocol'] == "ipv6")) { - $input_errors[] = gettext("You cannot connect to an IPv4 address using IPv6."); - } - if (is_ipaddrv6($_REQUEST['host']) && ($_REQUEST['ipprotocol'] == "ipv4")) { - $input_errors[] = gettext("You cannot connect to an IPv6 address using IPv4."); - } + if (count($input_errors) == 0) { + $nc_args = "-w 10" ; + if (empty($pconfig['showtext'])) { + $nc_args .= " -z "; + } + if (!empty($pconfig['srcport'])) { + $nc_args .= " -p " . escapeshellarg($pconfig['srcport']) . " "; + } + switch ($pconfig['ipprotocol']) { + case "ipv4": + $ifaddr = ($pconfig['sourceip'] == "any") ? "" : get_interface_ip($pconfig['sourceip']); + $nc_args .= " -4"; + break; + case "ipv6": + $ifaddr = (is_linklocal($pconfig['sourceip']) ? $pconfig['sourceip'] : get_interface_ipv6($pconfig['sourceip'])); + $nc_args .= " -6"; + break; + } + if (!empty($ifaddr)) { + $nc_args .= " -s " . escapeshellarg($ifaddr) . " "; + $scope = get_ll_scope($ifaddr); + if (!empty($scope) && !strstr($host, "%")) { + $host .= "%{$scope}"; + } + } - if (!$input_errors) { - $do_testport = true; - $timeout = NC_TIMEOUT; - } - - /* Save these request vars even if there were input errors. Then the fields are refilled for the user to correct. */ - $host = $_REQUEST['host']; - $sourceip = $_REQUEST['sourceip']; - $port = $_REQUEST['port']; - $srcport = $_REQUEST['srcport']; - $showtext = isset($_REQUEST['showtext']); - $ipprotocol = $_REQUEST['ipprotocol']; + $cmd_action = "/usr/bin/nc {$nc_args} " . escapeshellarg($pconfig['host']) . " " . escapeshellarg($pconfig['port']) . " 2>&1"; + $process = proc_open($cmd_action, array(array("pipe", "r"), array("pipe", "w"), array("pipe", "w")), $pipes); + if (is_resource($process)) { + $cmd_output = stream_get_contents($pipes[1]); + $cmd_output .= stream_get_contents($pipes[2]); + } + } } +legacy_html_escape_form_data($pconfig); include("head.inc"); ?> - - -
-
-
- -
- - -

- -


- - 0) print_input_errors($input_errors); ?> - -
- -
-

-
- -
-
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-

-
/> -

-
-
- -

it will result in an error. For example if you force IPv4 and use a hostname that only returns an AAAA IPv6 IP address, it will not work."); ?>

-
 " />
-
-
-
- -
-
- - - - - -
- - -
- -
-

-
- -
-
-
+  
+
+
+ +
+
+ 0) print_input_errors($input_errors); ?> +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + +   +
+ + +
+ +
+ +
+ + +
+ /> + +
+ +
 " />
+
+
+
+
+
+ +
+
+
+

+
+
+ +
+ +
+ +
+ - $result = ""; - $nc_base_cmd = "/usr/bin/nc"; - $nc_args = "-w " . escapeshellarg($timeout); - if (!$showtext) - $nc_args .= " -z "; - if (!empty($srcport)) - $nc_args .= " -p " . escapeshellarg($srcport) . " "; - - /* Attempt to determine the interface address, if possible. Else try both. */ - if (is_ipaddrv4($host)) { - $ifaddr = ($sourceip == "any") ? "" : get_interface_ip($sourceip); - $nc_args .= " -4"; - } elseif (is_ipaddrv6($host)) { - if ($sourceip == "any") - $ifaddr = ""; - else if (is_linklocal($sourceip)) - $ifaddr = $sourceip; - else - $ifaddr = get_interface_ipv6($sourceip); - $nc_args .= " -6"; - } else { - switch ($ipprotocol) { - case "ipv4": - $ifaddr = get_interface_ip($sourceip); - $nc_ipproto = " -4"; - break; - case "ipv6": - $ifaddr = (is_linklocal($sourceip) ? $sourceip : get_interface_ipv6($sourceip)); - $nc_ipproto = " -6"; - break; - case "any": - $ifaddr = get_interface_ip($sourceip); - $nc_ipproto = (!empty($ifaddr)) ? " -4" : ""; - if (empty($ifaddr)) { - $ifaddr = (is_linklocal($sourceip) ? $sourceip : get_interface_ipv6($sourceip)); - $nc_ipproto = (!empty($ifaddr)) ? " -6" : ""; - } - break; - } - /* Netcat doesn't like it if we try to connect using a certain type of IP without specifying the family. */ - if (!empty($ifaddr)) { - $nc_args .= $nc_ipproto; - } elseif ($sourceip == "any") { - switch ($ipprotocol) { - case "ipv4": - $nc_ipproto = " -4"; - break; - case "ipv6": - $nc_ipproto = " -6"; - break; - } - $nc_args .= $nc_ipproto; - } - } - /* Only add on the interface IP if we managed to find one. */ - if (!empty($ifaddr)) { - $nc_args .= " -s " . escapeshellarg($ifaddr) . " "; - $scope = get_ll_scope($ifaddr); - if (!empty($scope) && !strstr($host, "%")) - $host .= "%{$scope}"; - } - - $nc_cmd = "{$nc_base_cmd} {$nc_args} " . escapeshellarg($host) . " " . escapeshellarg($port) . " 2>&1"; - exec($nc_cmd, $result, $retval); - //echo "NC CMD: {$nc_cmd}\n\n"; - if (empty($result)) { - if ($showtext) - echo gettext("No output received, or connection failed. Try with \"Show Remote Text\" unchecked first."); - else - echo gettext("Connection failed (Refused/Timeout)"); - } else { - if (is_array($result)) { - foreach ($result as $resline) { - echo htmlspecialchars($resline) . "\n"; - } - } else { - echo htmlspecialchars($result); - } - } - -?> -
-
-
-
- -
-
+ + +
+ + + -