Project

General

Profile

Download (6.76 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	diag_traceroute.php
4
*/
5
/* ====================================================================
6
 *  Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *  Copyright (c)  2005 Paul Taylor (paultaylor@winndixie.com) and Manuel Kasper <mk@neon1.net>
8
 *
9
 *  Some or all of this file is based on the m0n0wall project which is
10
 *  Copyright (c)  2004 Manuel Kasper (BSD 2 clause)
11
 *
12
 *  Redistribution and use in source and binary forms, with or without modification,
13
 *  are permitted provided that the following conditions are met:
14
 *
15
 *  1. Redistributions of source code must retain the above copyright notice,
16
 *      this list of conditions and the following disclaimer.
17
 *
18
 *  2. Redistributions in binary form must reproduce the above copyright
19
 *      notice, this list of conditions and the following disclaimer in
20
 *      the documentation and/or other materials provided with the
21
 *      distribution.
22
 *
23
 *  3. All advertising materials mentioning features or use of this software
24
 *      must display the following acknowledgment:
25
 *      "This product includes software developed by the pfSense Project
26
 *       for use in the pfSense software distribution. (http://www.pfsense.org/).
27
 *
28
 *  4. The names "pfSense" and "pfSense Project" must not be used to
29
 *       endorse or promote products derived from this software without
30
 *       prior written permission. For written permission, please contact
31
 *       coreteam@pfsense.org.
32
 *
33
 *  5. Products derived from this software may not be called "pfSense"
34
 *      nor may "pfSense" appear in their names without prior written
35
 *      permission of the Electric Sheep Fencing, LLC.
36
 *
37
 *  6. Redistributions of any form whatsoever must retain the following
38
 *      acknowledgment:
39
 *
40
 *  "This product includes software developed by the pfSense Project
41
 *  for use in the pfSense software distribution (http://www.pfsense.org/).
42
 *
43
 *  THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
44
 *  EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
46
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
47
 *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
49
 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
50
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51
 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
52
 *  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
54
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
55
 *
56
 *  ====================================================================
57
 *
58
 */
59

    
60
##|+PRIV
61
##|*IDENT=page-diagnostics-traceroute
62
##|*NAME=Diagnostics: Traceroute
63
##|*DESCR=Allow access to the 'Diagnostics: Traceroute' page.
64
##|*MATCH=diag_traceroute.php*
65
##|-PRIV
66

    
67
require("guiconfig.inc");
68

    
69
$allowautocomplete = true;
70
$pgtitle = array(gettext("Diagnostics"), gettext("Traceroute"));
71
include("head.inc");
72

    
73
define('MAX_TTL', 64);
74
define('DEFAULT_TTL', 18);
75

    
76
// Set defaults in case they are not supplied.
77
$do_traceroute = false;
78
$host = '';
79
$ttl = DEFAULT_TTL;
80
$ipproto = 'ipv4';
81
$sourceip = 'any';
82

    
83
if ($_POST || $_REQUEST['host']) {
84
	unset($input_errors);
85

    
86
	/* input validation */
87
	$reqdfields = explode(" ", "host ttl");
88
	$reqdfieldsn = array(gettext("Host"), gettext("ttl"));
89
	do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
90

    
91
	if (($_REQUEST['ttl'] < 1) || ($_REQUEST['ttl'] > MAX_TTL)) {
92
		$input_errors[] = sprintf(gettext("Maximum number of hops must be between 1 and %s"), MAX_TTL);
93
	}
94
	$host = trim($_REQUEST['host']);
95
	$ipproto = $_REQUEST['ipproto'];
96
	if (($ipproto == "ipv4") && is_ipaddrv6($host)) {
97
		$input_errors[] = gettext("When using IPv4, the target host must be an IPv4 address or hostname.");
98
	}
99
	if (($ipproto == "ipv6") && is_ipaddrv4($host)) {
100
		$input_errors[] = gettext("When using IPv6, the target host must be an IPv6 address or hostname.");
101
	}
102

    
103
	$sourceip = $_REQUEST['sourceip'];
104
	$ttl = $_REQUEST['ttl'];
105
	$resolve = $_REQUEST['resolve'];
106
	$useicmp = $_REQUEST['useicmp'];
107

    
108
	if ($_POST && !$input_errors) {
109
		$do_traceroute = true;
110
	}
111

    
112
} else {
113
	$resolve = false;
114
	$useicmp = false;
115
}
116

    
117
if ($input_errors) {
118
	print_input_errors($input_errors);
119
}
120

    
121
/* Do the traceroute and show any error */
122
if ($do_traceroute) {
123
	$useicmpparam = isset($useicmp) ? "-I" : "";
124
	$n = isset($resolve) ? "" : "-n";
125

    
126
	$command = "/usr/sbin/traceroute";
127
	if ($ipproto == "ipv6") {
128
		$command .= "6";
129
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ipv6($sourceip);
130
	} else {
131
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ip($sourceip);
132
	}
133

    
134
	if ($ifaddr && (is_ipaddr($host) || is_hostname($host))) {
135
		$srcip = "-s " . escapeshellarg($ifaddr);
136
	}
137

    
138
	$cmd = "{$command} {$n} {$srcip} -w 2 {$useicmpparam} -m " . escapeshellarg($ttl) . " " . escapeshellarg($host);
139
	$result = shell_exec($cmd);
140

    
141
	if (!$result) {
142
		print_info_box(sprintf(gettext('Error: %s could not be traced/resolved'), $host));
143
	}
144
}
145

    
146
$form = new Form(false);
147

    
148
$section = new Form_Section('Traceroute');
149

    
150
$section->addInput(new Form_Input(
151
	'host',
152
	'Hostname',
153
	'text',
154
	$host,
155
	['placeholder' => 'Hostname to trace.']
156
));
157

    
158
$section->addInput(new Form_Select(
159
	'ipproto',
160
	'IP Protocol',
161
	$ipproto,
162
	array('ipv4' => 'IPv4', 'ipv6' => 'IPv6')
163
))->setHelp('Select the protocol to use.');
164

    
165
$section->addInput(new Form_Select(
166
	'sourceip',
167
	'Source Address',
168
	$sourceip,
169
	array('any' => gettext('Any')) + get_possible_traffic_source_addresses(true)
170
))->setHelp('Select source address for the trace.');
171

    
172
$section->addInput(new Form_Select(
173
	'ttl',
174
	'Maximum nuber of hops',
175
	$ttl,
176
	array_combine(range(1, MAX_TTL), range(1, MAX_TTL))
177
))->setHelp('Select the maximum number of network hops to trace.');
178

    
179
$section->addInput(new Form_Checkbox(
180
	'resolve',
181
	'Reverse Address Lookup',
182
	'',
183
	$resolve
184
))->setHelp('When checked, traceroute will attempt to perform a PTR lookup to locate hostnames for hops along the path. This will slow down the process as it has to wait for DNS replies.');
185

    
186
$section->addInput(new Form_Checkbox(
187
	'useicmp',
188
	gettext("Use ICMP"),
189
	'',
190
	$useicmp
191
))->setHelp('By default, traceroute uses UDP but that may be blocked by some routers. Check this box to use ICMP instead, which may succeed. ');
192

    
193
$form->add($section);
194

    
195
$form->addGlobal(new Form_Button(
196
	'Submit',
197
	'Traceroute',
198
	null,
199
	'fa-rss'
200
))->addClass('btn-primary');
201

    
202
print $form;
203

    
204
/* Show the traceroute results */
205
if ($do_traceroute && $result) {
206
?>
207
	<div class="panel panel-default">
208
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Results')?></h2></div>
209
		<div class="panel-body">
210
<?php
211
	print('<pre>' . $result . '</pre>');
212
?>
213
		</div>
214
	</div>
215
<?php
216
}
217

    
218
include("foot.inc");
219
?>
(33-33/225)