1 |
dfd6f179
|
sbeaver
|
<?php
|
2 |
|
|
/*
|
3 |
|
|
diag_testport.php
|
4 |
|
|
Copyright (C) 2013 Jim P (jimp@pfsense.org)
|
5 |
|
|
|
6 |
|
|
All rights reserved.
|
7 |
|
|
|
8 |
|
|
Portions based on diag_ping.php
|
9 |
|
|
part of m0n0wall (http://m0n0.ch/wall)
|
10 |
|
|
Copyright (C) 2003-2005 Bob Zoller (bob@kludgebox.com) and Manuel Kasper <mk@neon1.net>.
|
11 |
|
|
All rights reserved.
|
12 |
|
|
|
13 |
|
|
Redistribution and use in source and binary forms, with or without
|
14 |
|
|
modification, are permitted provided that the following conditions are met:
|
15 |
|
|
|
16 |
|
|
1. Redistributions of source code must retain the above copyright notice,
|
17 |
|
|
this list of conditions and the following disclaimer.
|
18 |
|
|
|
19 |
|
|
2. Redistributions in binary form must reproduce the above copyright
|
20 |
|
|
notice, this list of conditions and the following disclaimer in the
|
21 |
|
|
documentation and/or other materials provided with the distribution.
|
22 |
|
|
|
23 |
|
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
24 |
|
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
25 |
|
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
26 |
|
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
27 |
|
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
28 |
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
29 |
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
30 |
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31 |
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
32 |
|
|
POSSIBILITY OF SUCH DAMAGE.
|
33 |
|
|
*/
|
34 |
|
|
|
35 |
|
|
/*
|
36 |
|
|
pfSense_BUILDER_BINARIES: /usr/bin/nc
|
37 |
|
|
pfSense_MODULE: routing
|
38 |
|
|
*/
|
39 |
|
|
|
40 |
|
|
##|+PRIV
|
41 |
|
|
##|*IDENT=page-diagnostics-testport
|
42 |
|
|
##|*NAME=Diagnostics: Test Port
|
43 |
|
|
##|*DESCR=Allow access to the 'Diagnostics: Test Port' page.
|
44 |
|
|
##|*MATCH=diag_testport.php*
|
45 |
|
|
##|-PRIV
|
46 |
|
|
|
47 |
|
|
// Calling netcat and parsing hte results has been moved to the if ($_POST) section so that the results are known
|
48 |
|
|
// before we draw the form and any resulting error messages will allear in the correct place
|
49 |
|
|
|
50 |
|
|
$allowautocomplete = true;
|
51 |
|
|
|
52 |
|
|
$pgtitle = array(gettext("Diagnostics"), gettext("Test Port"));
|
53 |
|
|
require("guiconfig.inc");
|
54 |
|
|
|
55 |
|
|
define('NC_TIMEOUT', 10);
|
56 |
|
|
$do_testport = false;
|
57 |
|
|
$retval = 1;
|
58 |
|
|
|
59 |
|
|
function create_sourceaddresslist() {
|
60 |
|
|
$list = array('any' => 'Any');
|
61 |
|
|
|
62 |
|
|
$sourceips = get_possible_traffic_source_addresses(true);
|
63 |
|
|
|
64 |
|
|
foreach ($sourceips as $sipvalue => $sipname)
|
65 |
|
|
$list[$sipname[value]] = $sipname[name];
|
66 |
|
|
|
67 |
|
|
return($list);
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
if ($_POST || $_REQUEST['host']) {
|
71 |
|
|
unset($input_errors);
|
72 |
|
|
|
73 |
|
|
/* input validation */
|
74 |
|
|
$reqdfields = explode(" ", "host port");
|
75 |
|
|
$reqdfieldsn = array(gettext("Host"),gettext("Port"));
|
76 |
|
|
do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
|
77 |
|
|
|
78 |
|
|
if (!is_ipaddr($_REQUEST['host']) && !is_hostname($_REQUEST['host'])) {
|
79 |
|
|
$input_errors[] = gettext("Please enter a valid IP or hostname.");
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
if (!is_port($_REQUEST['port'])) {
|
83 |
|
|
$input_errors[] = gettext("Please enter a valid port number.");
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
if (($_REQUEST['srcport'] != "") && (!is_numeric($_REQUEST['srcport']) || !is_port($_REQUEST['srcport']))) {
|
87 |
|
|
$input_errors[] = gettext("Please enter a valid source port number, or leave the field blank.");
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
if (is_ipaddrv4($_REQUEST['host']) && ($_REQUEST['ipprotocol'] == "ipv6")) {
|
91 |
|
|
$input_errors[] = gettext("You cannot connect to an IPv4 address using IPv6.");
|
92 |
|
|
}
|
93 |
|
|
if (is_ipaddrv6($_REQUEST['host']) && ($_REQUEST['ipprotocol'] == "ipv4")) {
|
94 |
|
|
$input_errors[] = gettext("You cannot connect to an IPv6 address using IPv4.");
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
if (!$input_errors) {
|
98 |
|
|
$do_testport = true;
|
99 |
|
|
$timeout = NC_TIMEOUT;
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
/* Save these request vars even if there were input errors. Then the fields are refilled for the user to correct. */
|
103 |
|
|
$host = $_REQUEST['host'];
|
104 |
|
|
$sourceip = $_REQUEST['sourceip'];
|
105 |
|
|
$port = $_REQUEST['port'];
|
106 |
|
|
$srcport = $_REQUEST['srcport'];
|
107 |
|
|
$showtext = isset($_REQUEST['showtext']);
|
108 |
|
|
$ipprotocol = $_REQUEST['ipprotocol'];
|
109 |
|
|
|
110 |
|
|
if ( $do_testport ) {
|
111 |
|
|
?>
|
112 |
|
|
<script type="text/javascript">
|
113 |
|
|
//<![CDATA[
|
114 |
|
|
window.onload=function(){
|
115 |
|
|
document.getElementById("testportCaptured").wrap='off';
|
116 |
|
|
}
|
117 |
|
|
//]]>
|
118 |
|
|
</script>
|
119 |
|
|
<?php
|
120 |
|
|
$result = "";
|
121 |
|
|
$ncoutput = "";
|
122 |
|
|
$nc_base_cmd = '/usr/bin/nc';
|
123 |
|
|
$nc_args = "-w " . escapeshellarg($timeout);
|
124 |
|
|
if (!$showtext)
|
125 |
|
|
$nc_args .= ' -z ';
|
126 |
|
|
if (!empty($srcport))
|
127 |
|
|
$nc_args .= ' -p ' . escapeshellarg($srcport) . ' ';
|
128 |
|
|
|
129 |
|
|
/* Attempt to determine the interface address, if possible. Else try both. */
|
130 |
|
|
if (is_ipaddrv4($host)) {
|
131 |
|
|
$ifaddr = ($sourceip == "any") ? "" : get_interface_ip($sourceip);
|
132 |
|
|
$nc_args .= ' -4';
|
133 |
|
|
} elseif (is_ipaddrv6($host)) {
|
134 |
|
|
if ($sourceip == "any")
|
135 |
|
|
$ifaddr = '';
|
136 |
|
|
else if (is_linklocal($sourceip))
|
137 |
|
|
$ifaddr = $sourceip;
|
138 |
|
|
else
|
139 |
|
|
$ifaddr = get_interface_ipv6($sourceip);
|
140 |
|
|
$nc_args .= ' -6';
|
141 |
|
|
} else {
|
142 |
|
|
switch ($ipprotocol) {
|
143 |
|
|
case "ipv4":
|
144 |
|
|
$ifaddr = get_interface_ip($sourceip);
|
145 |
|
|
$nc_ipproto = ' -4';
|
146 |
|
|
break;
|
147 |
|
|
case "ipv6":
|
148 |
|
|
$ifaddr = (is_linklocal($sourceip) ? $sourceip : get_interface_ipv6($sourceip));
|
149 |
|
|
$nc_ipproto = ' -6';
|
150 |
|
|
break;
|
151 |
|
|
case "any":
|
152 |
|
|
$ifaddr = get_interface_ip($sourceip);
|
153 |
|
|
$nc_ipproto = (!empty($ifaddr)) ? ' -4' : '';
|
154 |
|
|
if (empty($ifaddr)) {
|
155 |
|
|
$ifaddr = (is_linklocal($sourceip) ? $sourceip : get_interface_ipv6($sourceip));
|
156 |
|
|
$nc_ipproto = (!empty($ifaddr)) ? ' -6' : '';
|
157 |
|
|
}
|
158 |
|
|
break;
|
159 |
|
|
}
|
160 |
|
|
/* Netcat doesn't like it if we try to connect using a certain type of IP without specifying the family. */
|
161 |
|
|
if (!empty($ifaddr)) {
|
162 |
|
|
$nc_args .= $nc_ipproto;
|
163 |
|
|
} elseif ($sourceip == "any") {
|
164 |
|
|
switch ($ipprotocol) {
|
165 |
|
|
case "ipv4":
|
166 |
|
|
$nc_ipproto = ' -4';
|
167 |
|
|
break;
|
168 |
|
|
case "ipv6":
|
169 |
|
|
$nc_ipproto = ' -6';
|
170 |
|
|
break;
|
171 |
|
|
}
|
172 |
|
|
$nc_args .= $nc_ipproto;
|
173 |
|
|
}
|
174 |
|
|
}
|
175 |
|
|
/* Only add on the interface IP if we managed to find one. */
|
176 |
|
|
if (!empty($ifaddr)) {
|
177 |
|
|
$nc_args .= ' -s ' . escapeshellarg($ifaddr) . ' ';
|
178 |
|
|
$scope = get_ll_scope($ifaddr);
|
179 |
|
|
if (!empty($scope) && !strstr($host, "%"))
|
180 |
|
|
$host .= "%{$scope}";
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
$nc_cmd = "{$nc_base_cmd} {$nc_args} " . escapeshellarg($host) . ' ' . escapeshellarg($port) . ' 2>&1';
|
184 |
|
|
exec($nc_cmd, $result, $retval);
|
185 |
|
|
// echo "NC CMD: {$nc_cmd}\n\n";
|
186 |
|
|
|
187 |
|
|
if (!empty($result)) {
|
188 |
|
|
if (is_array($result)) {
|
189 |
|
|
foreach ($result as $resline) {
|
190 |
|
|
$ncoutput .= htmlspecialchars($resline) . "\n";
|
191 |
|
|
}
|
192 |
|
|
} else {
|
193 |
|
|
$ncoutput .= htmlspecialchars($result);
|
194 |
|
|
}
|
195 |
|
|
}
|
196 |
|
|
}
|
197 |
|
|
}
|
198 |
|
|
|
199 |
|
|
include("head.inc");
|
200 |
|
|
|
201 |
|
|
// Handle the display of all messages here wher the user can readily see them
|
202 |
|
|
if ($input_errors)
|
203 |
|
|
print_input_errors($input_errors);
|
204 |
|
|
else {
|
205 |
|
|
// New page
|
206 |
|
|
if(empty($result) && $retval != 0 && !$showtext) {
|
207 |
|
|
print('<div class="alert alert-warning" role="alert">' . 'This page allows you to perform a simple TCP connection test to determine if a host is up and accepting connections on a given port.' .
|
208 |
|
|
' This test does not function for UDP since there is no way to reliably determine if a UDP port accepts connections in this manner.' . '</div>');
|
209 |
|
|
}
|
210 |
|
|
|
211 |
|
|
// Good host & port
|
212 |
|
|
if($retval == 0 && $do_testport == 1) {
|
213 |
|
|
if(!$showtext)
|
214 |
|
|
print('<div class="alert alert-success" role="alert">'.gettext("Port test to host: " . $host . " Port: " . $port . " successful").'</div>');
|
215 |
|
|
else
|
216 |
|
|
print('<div class="alert alert-success" role="alert">'.gettext("Port test to host: " . $host . " Port: " . $port . " successful") . '. Any text received from teh host will be shown below the form.' . '</div>');
|
217 |
|
|
}
|
218 |
|
|
|
219 |
|
|
// netcat exit value != 0
|
220 |
|
|
if($retval != 0 && !empty($result))
|
221 |
|
|
if($showtext)
|
222 |
|
|
print('<div class="alert alert-danger" role="alert">'.gettext('No output received, or connection failed. Try with "Show Remote Text" unchecked first.').'</div>');
|
223 |
|
|
else
|
224 |
|
|
print('<div class="alert alert-danger" role="alert">'.gettext('Connection failed.').'</div>');
|
225 |
|
|
}
|
226 |
|
|
|
227 |
|
|
require('classes/Form.class.php');
|
228 |
|
|
|
229 |
|
|
$form = new Form(new Form_Button(
|
230 |
|
|
'Submit',
|
231 |
|
|
gettext('Test')
|
232 |
|
|
));
|
233 |
|
|
|
234 |
|
|
$section = new Form_Section('Test Port');
|
235 |
|
|
|
236 |
|
|
$section->addInput(new Form_Input(
|
237 |
|
|
'host',
|
238 |
|
|
'Hostname',
|
239 |
|
|
'text',
|
240 |
|
|
htmlspecialchars($host),
|
241 |
|
|
['placeholder' => 'Hostname to look up.']
|
242 |
|
|
));
|
243 |
|
|
|
244 |
|
|
$section->addInput(new Form_Input(
|
245 |
|
|
'port',
|
246 |
|
|
'Port',
|
247 |
|
|
'text',
|
248 |
|
|
htmlspecialchars($port),
|
249 |
|
|
['placeholder' => 'Port to test.']
|
250 |
|
|
));
|
251 |
|
|
|
252 |
|
|
$section->addInput(new Form_Input(
|
253 |
|
|
'srcport',
|
254 |
|
|
'Source Port',
|
255 |
|
|
'text',
|
256 |
|
|
htmlspecialchars($srcport),
|
257 |
|
|
['placeholder' => 'Typically left blank.']
|
258 |
|
|
));
|
259 |
|
|
|
260 |
|
|
$section->addInput(new Form_Checkbox(
|
261 |
|
|
'showtext',
|
262 |
|
|
'Show Remote Text',
|
263 |
|
|
'',
|
264 |
|
|
$showtext
|
265 |
|
|
))->setHelp(gettext("Shows the text given by the server when connecting to the port. If checked it will take 10+ seconds to display in a panel below this form."));
|
266 |
|
|
|
267 |
|
|
$section->addInput(new Form_Select(
|
268 |
|
|
'sourceip',
|
269 |
|
|
'Source Address',
|
270 |
|
|
$pconfig['source'],
|
271 |
|
|
create_sourceaddresslist()
|
272 |
|
|
))->setHelp('Select source address for the trace');
|
273 |
|
|
|
274 |
|
|
$section->addInput(new Form_Select(
|
275 |
|
|
'ipproto',
|
276 |
|
|
'IP Protocol',
|
277 |
|
|
$pconfig['protocol'],
|
278 |
|
|
array('ipv4' => 'IPv4', 'ipv6' => 'IPv6')
|
279 |
|
|
))->setHelp(gettext("If you force IPv4 or IPv6 and use a hostname that does not contain a result using that protocol, it will result in an error." .
|
280 |
|
|
" For example if you force IPv4 and use a hostname that only returns an AAAA IPv6 IP address, it will not work."));
|
281 |
|
|
|
282 |
|
|
$form->add($section);
|
283 |
|
|
print $form;
|
284 |
|
|
|
285 |
|
|
if($ncoutput && !empty($result) && $showtext && $retval == 0)
|
286 |
|
|
{
|
287 |
|
|
print("<div class=\"panel panel-default\">" . "<div class=\"panel-heading\">Received Remote Text</div>" . "<div class=\"panel-body\">");
|
288 |
|
|
print(nl2br($ncoutput));
|
289 |
|
|
print("</dev></div>");
|
290 |
|
|
}
|
291 |
|
|
|
292 |
|
|
include("foot.inc"); ?>
|