Project

General

Profile

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

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

    
66
require_once("guiconfig.inc");
67

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
145
$form = new Form(false);
146

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

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

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

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

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

    
178
$section->addInput(new Form_Checkbox(
179
	'resolve',
180
	'Reverse Address Lookup',
181
	'',
182
	$resolve
183
))->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.');
184

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

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

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

    
201
print $form;
202

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

    
217
include("foot.inc");
218
?>
(32-32/225)