Project

General

Profile

Download (8.88 KB) Statistics
| Branch: | Tag: | Revision:
1
<?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
if ($_POST || $_REQUEST['host']) {
60
	unset($input_errors);
61

    
62
	/* input validation */
63
	$reqdfields = explode(" ", "host port");
64
	$reqdfieldsn = array(gettext("Host"),gettext("Port"));
65
	do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
66

    
67
	if (!is_ipaddr($_REQUEST['host']) && !is_hostname($_REQUEST['host'])) {
68
		$input_errors[] = gettext("Please enter a valid IP or hostname.");
69
	}
70

    
71
	if (!is_port($_REQUEST['port'])) {
72
		$input_errors[] = gettext("Please enter a valid port number.");
73
	}
74

    
75
	if (($_REQUEST['srcport'] != "") && (!is_numeric($_REQUEST['srcport']) || !is_port($_REQUEST['srcport']))) {
76
		$input_errors[] = gettext("Please enter a valid source port number, or leave the field blank.");
77
	}
78

    
79
	if (is_ipaddrv4($_REQUEST['host']) && ($_REQUEST['ipprotocol'] == "ipv6")) {
80
		$input_errors[] = gettext("You cannot connect to an IPv4 address using IPv6.");
81
	}
82
	if (is_ipaddrv6($_REQUEST['host']) && ($_REQUEST['ipprotocol'] == "ipv4")) {
83
		$input_errors[] = gettext("You cannot connect to an IPv6 address using IPv4.");
84
	}
85

    
86
	if (!$input_errors) {
87
		$do_testport = true;
88
		$timeout = NC_TIMEOUT;
89
	}
90

    
91
	/* Save these request vars even if there were input errors. Then the fields are refilled for the user to correct. */
92
	$host = $_REQUEST['host'];
93
	$sourceip = $_REQUEST['sourceip'];
94
	$port = $_REQUEST['port'];
95
	$srcport = $_REQUEST['srcport'];
96
	$showtext = isset($_REQUEST['showtext']);
97
	$ipprotocol = $_REQUEST['ipprotocol'];
98

    
99
	if ( $do_testport ) {
100
?>
101
		<script type="text/javascript">
102
			//<![CDATA[
103
			window.onload=function(){
104
				document.getElementById("testportCaptured").wrap='off';
105
			}
106
			//]]>
107
		</script>
108
<?php
109
		$result = "";
110
		$ncoutput = "";
111
		$nc_base_cmd = '/usr/bin/nc';
112
		$nc_args = "-w " . escapeshellarg($timeout);
113
		if (!$showtext)
114
			$nc_args .= ' -z ';
115
		if (!empty($srcport))
116
			$nc_args .= ' -p ' . escapeshellarg($srcport) . ' ';
117

    
118
		/* Attempt to determine the interface address, if possible. Else try both. */
119
		if (is_ipaddrv4($host)) {
120
			$ifaddr = ($sourceip == "any") ? "" : get_interface_ip($sourceip);
121
			$nc_args .= ' -4';
122
		} elseif (is_ipaddrv6($host)) {
123
			if ($sourceip == "any")
124
				$ifaddr = '';
125
			else if (is_linklocal($sourceip))
126
				$ifaddr = $sourceip;
127
			else
128
				$ifaddr = get_interface_ipv6($sourceip);
129
			$nc_args .= ' -6';
130
		} else {
131
			switch ($ipprotocol) {
132
				case "ipv4":
133
					$ifaddr = get_interface_ip($sourceip);
134
					$nc_ipproto = ' -4';
135
					break;
136
				case "ipv6":
137
					$ifaddr = (is_linklocal($sourceip) ? $sourceip : get_interface_ipv6($sourceip));
138
					$nc_ipproto = ' -6';
139
					break;
140
				case "any":
141
					$ifaddr = get_interface_ip($sourceip);
142
					$nc_ipproto = (!empty($ifaddr)) ? ' -4' : '';
143
					if (empty($ifaddr)) {
144
						$ifaddr = (is_linklocal($sourceip) ? $sourceip : get_interface_ipv6($sourceip));
145
						$nc_ipproto = (!empty($ifaddr)) ? ' -6' : '';
146
					}
147
					break;
148
			}
149
			/* Netcat doesn't like it if we try to connect using a certain type of IP without specifying the family. */
150
			if (!empty($ifaddr)) {
151
				$nc_args .= $nc_ipproto;
152
			} elseif ($sourceip == "any") {
153
				switch ($ipprotocol) {
154
					case "ipv4":
155
						$nc_ipproto = ' -4';
156
						break;
157
					case "ipv6":
158
						$nc_ipproto = ' -6';
159
						break;
160
				}
161
				$nc_args .= $nc_ipproto;
162
			}
163
		}
164
		/* Only add on the interface IP if we managed to find one. */
165
		if (!empty($ifaddr)) {
166
			$nc_args .= ' -s ' . escapeshellarg($ifaddr) . ' ';
167
			$scope = get_ll_scope($ifaddr);
168
			if (!empty($scope) && !strstr($host, "%"))
169
				$host .= "%{$scope}";
170
		}
171

    
172
		$nc_cmd = "{$nc_base_cmd} {$nc_args} " . escapeshellarg($host) . ' ' . escapeshellarg($port) . ' 2>&1';
173
		exec($nc_cmd, $result, $retval);
174
	//	echo "NC CMD: {$nc_cmd}\n\n";
175

    
176
		if (!empty($result)) {
177
			if (is_array($result)) {
178
				foreach ($result as $resline) {
179
					$ncoutput .= htmlspecialchars($resline) . "\n";
180
				}
181
			} else {
182
				$ncoutput .= htmlspecialchars($result);
183
			}
184
		}
185
	}
186
}
187

    
188
include("head.inc");
189

    
190
// Handle the display of all messages here wher the user can readily see them
191
if ($input_errors)
192
	print_input_errors($input_errors);
193
else {
194
	// New page
195
	if(empty($result) && $retval != 0 && !$showtext) {	    
196
	    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.' .
197
	          ' 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>');
198
	}
199

    
200
	// Good host & port
201
	if($retval == 0 && $do_testport == 1)	{
202
		if(!$showtext)
203
			print('<div class="alert alert-success" role="alert">'.gettext("Port test to host: " . $host . " Port: " . $port . " successful").'</div>');
204
		else
205
			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>');
206
	}
207

    
208
	// netcat exit value != 0
209
	if($retval != 0 && !empty($result))
210
		if($showtext)
211
			print('<div class="alert alert-danger" role="alert">'.gettext('No output received, or connection failed. Try with "Show Remote Text" unchecked first.').'</div>');
212
		else
213
			print('<div class="alert alert-danger" role="alert">'.gettext('Connection failed.').'</div>');
214
}
215

    
216
require('classes/Form.class.php');
217

    
218
$form = new Form(new Form_Button(
219
	'Submit',
220
	'Test'
221
));
222

    
223
$section = new Form_Section('Test Port');
224

    
225
$section->addInput(new Form_Input(
226
	'host',
227
	'Hostname',
228
	'text',
229
	$host,
230
	['placeholder' => 'Hostname to look up.']
231
));
232

    
233
$section->addInput(new Form_Input(
234
	'port',
235
	'Port',
236
	'text',
237
	$port,
238
	['placeholder' => 'Port to test.']
239
));
240

    
241
$section->addInput(new Form_Input(
242
	'srcport',
243
	'Source Port',
244
	'text',
245
	$srcport,
246
	['placeholder' => 'Typically left blank.']
247
));
248

    
249
$section->addInput(new Form_Checkbox(
250
	'showtext',
251
	'Remote text',
252
	'Show remote text',
253
	$showtext
254
))->setHelp("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.");
255

    
256
$section->addInput(new Form_Select(
257
	'sourceip',
258
	'Source Address',
259
	$sourceip,
260
	array_merge(array('' => 'Any'), get_possible_traffic_source_addresses(true))
261
))->setHelp('Select source address for the trace');
262

    
263
$section->addInput(new Form_Select(
264
	'ipproto',
265
	'IP Protocol',
266
	$ipprotocol,
267
	array('ipv4' => 'IPv4', 'ipv6' => 'IPv6')
268
))->setHelp("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." .
269
					" For example if you force IPv4 and use a hostname that only returns an AAAA IPv6 IP address, it will not work.");
270

    
271
$form->add($section);
272
print $form;
273

    
274
if($ncoutput && !empty($result) && $showtext && $retval == 0): ?>
275
	<div class="panel panel-default">
276
		<div class="panel-heading">
277
			<h2 class="panel-title">Received Remote Text</h2>
278
		</div>
279
		<div class="panel-body">
280
			<pre><?= $ncoutput ?></pre>
281
		</div>
282
	</div>
283
<?php endif;
284

    
285
include("foot.inc");
(52-52/252)