Project

General

Profile

Download (8.93 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	diag_testport.php
4
	Copyright (C) 2013 Jim P (jimp@pfsense.org)
5
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
6

    
7
	All rights reserved.
8

    
9
	Portions based on diag_ping.php
10
	part of m0n0wall (http://m0n0.ch/wall)
11
	Copyright (C) 2003-2005 Bob Zoller (bob@kludgebox.com) and Manuel Kasper <mk@neon1.net>.
12
	All rights reserved.
13

    
14
	Redistribution and use in source and binary forms, with or without
15
	modification, are permitted provided that the following conditions are met:
16

    
17
	1. Redistributions of source code must retain the above copyright notice,
18
	this list of conditions and the following disclaimer.
19

    
20
	2. Redistributions in binary form must reproduce the above copyright
21
	notice, this list of conditions and the following disclaimer in the
22
	documentation and/or other materials provided with the distribution.
23

    
24
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
26
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
	POSSIBILITY OF SUCH DAMAGE.
34
*/
35

    
36
/*
37
	pfSense_BUILDER_BINARIES:	/usr/bin/nc
38
	pfSense_MODULE: routing
39
*/
40

    
41
##|+PRIV
42
##|*IDENT=page-diagnostics-testport
43
##|*NAME=Diagnostics: Test Port
44
##|*DESCR=Allow access to the 'Diagnostics: Test Port' page.
45
##|*MATCH=diag_testport.php*
46
##|-PRIV
47

    
48
// Calling netcat and parsing hte results has been moved to the if ($_POST) section so that the results are known
49
// before we draw the form and any resulting error messages will allear in the correct place
50

    
51
$allowautocomplete = true;
52

    
53
$pgtitle = array(gettext("Diagnostics"), gettext("Test Port"));
54
require("guiconfig.inc");
55

    
56
define('NC_TIMEOUT', 10);
57
$do_testport = false;
58
$retval = 1;
59

    
60
if ($_POST || $_REQUEST['host']) {
61
	unset($input_errors);
62

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

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

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

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

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

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

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

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

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

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

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

    
189
include("head.inc");
190

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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