Project

General

Profile

Download (9.8 KB) Statistics
| Branch: | Tag: | Revision:
1 737ed7d1 jim-p
<?php
2
/*
3
	diag_dns.php
4
*/
5 0da0d43e Phil Davis
/* ====================================================================
6
 *  Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7 fd9ebcd5 Stephen Beaver
 *
8 0da0d43e Phil Davis
 *  Redistribution and use in source and binary forms, with or without modification,
9
 *  are permitted provided that the following conditions are met:
10 fd9ebcd5 Stephen Beaver
 *
11
 *  1. Redistributions of source code must retain the above copyright notice,
12
 *      this list of conditions and the following disclaimer.
13
 *
14
 *  2. Redistributions in binary form must reproduce the above copyright
15
 *      notice, this list of conditions and the following disclaimer in
16
 *      the documentation and/or other materials provided with the
17 0da0d43e Phil Davis
 *      distribution.
18 fd9ebcd5 Stephen Beaver
 *
19 0da0d43e Phil Davis
 *  3. All advertising materials mentioning features or use of this software
20 fd9ebcd5 Stephen Beaver
 *      must display the following acknowledgment:
21
 *      "This product includes software developed by the pfSense Project
22 0da0d43e Phil Davis
 *       for use in the pfSense software distribution. (http://www.pfsense.org/).
23 fd9ebcd5 Stephen Beaver
 *
24
 *  4. The names "pfSense" and "pfSense Project" must not be used to
25
 *       endorse or promote products derived from this software without
26
 *       prior written permission. For written permission, please contact
27
 *       coreteam@pfsense.org.
28
 *
29
 *  5. Products derived from this software may not be called "pfSense"
30
 *      nor may "pfSense" appear in their names without prior written
31
 *      permission of the Electric Sheep Fencing, LLC.
32
 *
33
 *  6. Redistributions of any form whatsoever must retain the following
34
 *      acknowledgment:
35
 *
36
 *  "This product includes software developed by the pfSense Project
37
 *  for use in the pfSense software distribution (http://www.pfsense.org/).
38 0da0d43e Phil Davis
 *
39 fd9ebcd5 Stephen Beaver
 *  THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
40
 *  EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
43
 *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45
 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48
 *  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
51
 *
52
 *  ====================================================================
53
 *
54
 */
55 13d193c2 Scott Ullrich
56 a57d9fa2 jim-p
##|+PRIV
57
##|*IDENT=page-diagnostics-dns
58 5230f468 jim-p
##|*NAME=Diagnostics: DNS Lookup
59 a57d9fa2 jim-p
##|*DESCR=Allow access to the 'Diagnostics: DNS Lookup' page.
60
##|*MATCH=diag_dns.php*
61
##|-PRIV
62
63 699737d9 Phil Davis
$pgtitle = array(gettext("Diagnostics"), gettext("DNS Lookup"));
64 737ed7d1 jim-p
require("guiconfig.inc");
65
66 ed2a6e89 jim-p
$host = trim($_REQUEST['host'], " \t\n\r\0\x0B[];\"'");
67 76c4ff0e Renato Botelho
$host_esc = escapeshellarg($host);
68
69 6ee37b41 Sjon Hortensius
/* If this section of config.xml has not been populated yet we need to set it up
70 d5b28fcf Stephen Beaver
*/
71
if (!is_array($config['aliases']['alias'])) {
72
	$config['aliases']['alias'] = array();
73 bc0a452f jim-p
}
74 d5b28fcf Stephen Beaver
$a_aliases = &$config['aliases']['alias'];
75
76 7bc19545 Chris Buechler
$aliasname = substr(str_replace(array(".", "-"), "_", $host), 0, 31);
77 ed2a6e89 jim-p
$alias_exists = false;
78 6c07db48 Phil Davis
$counter = 0;
79 5f601060 Phil Davis
foreach ($a_aliases as $a) {
80
	if ($a['name'] == $aliasname) {
81 ed2a6e89 jim-p
		$alias_exists = true;
82 6c07db48 Phil Davis
		$id = $counter;
83 ed2a6e89 jim-p
	}
84
	$counter++;
85
}
86
87 5f601060 Phil Davis
if (isset($_POST['create_alias']) && (is_hostname($host) || is_ipaddr($host))) {
88 e55d4b3c Scott Ullrich
	$resolved = gethostbyname($host);
89 ed2a6e89 jim-p
	$type = "hostname";
90 5f601060 Phil Davis
	if ($resolved) {
91 ed2a6e89 jim-p
		$resolved = array();
92
		exec("/usr/bin/drill {$host_esc} A | /usr/bin/grep {$host_esc} | /usr/bin/grep -v ';' | /usr/bin/awk '{ print $5 }'", $resolved);
93 e55d4b3c Scott Ullrich
		$isfirst = true;
94 288a2a0f Phil Davis
		foreach ($resolved as $re) {
95
			if ($re != "") {
96 947141fd Phil Davis
				if (!$isfirst) {
97 7a87cb97 Scott Ullrich
					$addresses .= " ";
98 947141fd Phil Davis
				}
99 97f42a05 Renato Botelho
				$re = rtrim($re);
100 11614bc9 Phil Davis
				if (is_ipaddr($re)) {
101
					$sn = is_ipaddrv6($re) ? '/128' : '/32';
102
				} else {
103
					// The name was a CNAME and resolved to another name, rather than an address.
104
					// In this case the alias entry will have a FQDN, so do not put a CIDR after it.
105
					$sn = "";
106
				}
107 97f42a05 Renato Botelho
				$addresses .= $re . $sn;
108 7a87cb97 Scott Ullrich
				$isfirst = false;
109
			}
110 e55d4b3c Scott Ullrich
		}
111
		$newalias = array();
112 4d9a5d99 Stephen Beaver
		$newalias['name'] = $aliasname;
113
		$newalias['type'] = "network";
114
		$newalias['address'] = $addresses;
115
		$newalias['descr'] = gettext("Created from Diagnostics-> DNS Lookup");
116
		if ($alias_exists) {
117
			$a_aliases[$id] = $newalias;
118
		} else {
119
			$a_aliases[] = $newalias;
120 e55d4b3c Scott Ullrich
		}
121 4d9a5d99 Stephen Beaver
		write_config();
122
		$createdalias = true;
123 e55d4b3c Scott Ullrich
	}
124
}
125
126 737ed7d1 jim-p
if ($_POST) {
127
	unset($input_errors);
128
129
	$reqdfields = explode(" ", "host");
130
	$reqdfieldsn = explode(",", "Host");
131
132 1e9b4611 Renato Botelho
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
133 286aa3f2 sbeaver
134 72cd706b bcyrill
	if (!is_hostname($host) && !is_ipaddr($host)) {
135 ee91ae30 jean.feltrin
		$input_errors[] = gettext("Host must be a valid hostname or IP address.");
136 72cd706b bcyrill
	} else {
137
		// Test resolution speed of each DNS server.
138 86ab47ff sullrich
		$dns_speeds = array();
139 ed2a6e89 jim-p
		$dns_servers = array();
140
		exec("/usr/bin/grep nameserver /etc/resolv.conf | /usr/bin/cut -f2 -d' '", $dns_servers);
141 40af551f jim-p
		foreach ($dns_servers as $dns_server) {
142 ed2a6e89 jim-p
			$query_time = exec("/usr/bin/drill {$host_esc} " . escapeshellarg("@" . trim($dns_server)) . " | /usr/bin/grep Query | /usr/bin/cut -d':' -f2");
143 5f601060 Phil Davis
			if ($query_time == "") {
144 ee91ae30 jean.feltrin
				$query_time = gettext("No response");
145 5f601060 Phil Davis
			}
146 86ab47ff sullrich
			$new_qt = array();
147
			$new_qt['dns_server'] = $dns_server;
148 ed2a6e89 jim-p
			$new_qt['query_time'] = $query_time;
149 86ab47ff sullrich
			$dns_speeds[] = $new_qt;
150
			unset($new_qt);
151
		}
152 737ed7d1 jim-p
	}
153
154 da652a46 Vinicius Coque
	$type = "unknown";
155 737ed7d1 jim-p
	$resolved = "";
156 2312b0eb jim-p
	$ipaddr = "";
157
	$hostname = "";
158 737ed7d1 jim-p
	if (!$input_errors) {
159
		if (is_ipaddr($host)) {
160
			$type = "ip";
161
			$resolved = gethostbyaddr($host);
162 2312b0eb jim-p
			$ipaddr = $host;
163 5f601060 Phil Davis
			if ($host != $resolved) {
164 2312b0eb jim-p
				$hostname = $resolved;
165 5f601060 Phil Davis
			}
166 737ed7d1 jim-p
		} elseif (is_hostname($host)) {
167
			$type = "hostname";
168
			$resolved = gethostbyname($host);
169 5f601060 Phil Davis
			if ($resolved) {
170 ed2a6e89 jim-p
				$resolved = array();
171
				exec("/usr/bin/drill {$host_esc} A | /usr/bin/grep {$host_esc} | /usr/bin/grep -v ';' | /usr/bin/awk '{ print $5 }'", $resolved);
172 c43f732a Scott Ullrich
			}
173 2312b0eb jim-p
			$hostname = $host;
174 5f601060 Phil Davis
			if ($host != $resolved) {
175 3e85ac2b Scott Ullrich
				$ipaddr = $resolved[0];
176 5f601060 Phil Davis
			}
177 737ed7d1 jim-p
		}
178
179
		if ($host == $resolved) {
180 ee91ae30 jean.feltrin
			$resolved = gettext("No record found");
181 737ed7d1 jim-p
		}
182
	}
183
}
184
185 5f601060 Phil Davis
if (($_POST['host']) && ($_POST['dialog_output'])) {
186 699737d9 Phil Davis
	display_host_results ($host, $resolved, $dns_speeds);
187 fe74228f N0YB
	exit;
188
}
189
190 699737d9 Phil Davis
function display_host_results ($address, $hostname, $dns_speeds) {
191 79f5aebe Robert Nelson
	$map_lengths = function($element) { return strlen($element[0]); };
192 6ff71328 Robert Nelson
193 fe74228f N0YB
	echo gettext("IP Address") . ": {$address} \n";
194
	echo gettext("Host Name") . ": {$hostname} \n";
195
	echo "\n";
196 6ff71328 Robert Nelson
	$text_table = array();
197
	$text_table[] = array(gettext("Server"), gettext("Query Time"));
198
	if (is_array($dns_speeds)) {
199
		foreach ($dns_speeds as $qt) {
200
			$text_table[] = array(trim($qt['dns_server']), trim($qt['query_time']));
201 fe74228f N0YB
		}
202 6ff71328 Robert Nelson
	}
203 79f5aebe Robert Nelson
	$col0_padlength = max(array_map($map_lengths, $text_table)) + 4;
204 6ff71328 Robert Nelson
	foreach ($text_table as $text_row) {
205 79f5aebe Robert Nelson
		echo str_pad($text_row[0], $col0_padlength) . $text_row[1] . "\n";
206 6ff71328 Robert Nelson
	}
207 fe74228f N0YB
}
208
209 286aa3f2 sbeaver
include("head.inc");
210 e55d4b3c Scott Ullrich
211 286aa3f2 sbeaver
/* Display any error messages resulting from user input */
212 947141fd Phil Davis
if ($input_errors) {
213 286aa3f2 sbeaver
	print_input_errors($input_errors);
214 947141fd Phil Davis
} else if (!$resolved && $type) {
215 02342d8c k-paulius
	print_info_box(sprintf(gettext('Host "%s" could not be resolved.'), $host), 'warning', false);
216 947141fd Phil Davis
}
217 286aa3f2 sbeaver
218 947141fd Phil Davis
if ($createdalias) {
219 4d9a5d99 Stephen Beaver
	if ($alias_exists) {
220
		print_info_box(gettext("Alias was updated successfully."), 'success');
221
	} else {
222
		print_info_box(gettext("Alias was created successfully."), 'success');
223
	}
224 947141fd Phil Davis
}
225 286aa3f2 sbeaver
226 37676f4e jim-p
$form = new Form(false);
227 06da0d4e Sjon Hortensius
$section = new Form_Section('DNS Lookup');
228 286aa3f2 sbeaver
229
$section->addInput(new Form_Input(
230
	'host',
231 06da0d4e Sjon Hortensius
	'Hostname',
232 286aa3f2 sbeaver
	'text',
233 06da0d4e Sjon Hortensius
	$host,
234
	['placeholder' => 'Hostname to look up.']
235
));
236 286aa3f2 sbeaver
237 06da0d4e Sjon Hortensius
if (!empty($resolved)) {
238 4d9a5d99 Stephen Beaver
	if ($alias_exists) {
239
		$button_text = gettext("Update alias");
240
	} else {
241
		$button_text = gettext("Add alias");
242
	}
243 286aa3f2 sbeaver
	$form->addGlobal(new Form_Button(
244 06da0d4e Sjon Hortensius
		'create_alias',
245 4d9a5d99 Stephen Beaver
		$button_text,
246 37676f4e jim-p
		null,
247
		'fa-plus'
248 286aa3f2 sbeaver
	))->removeClass('btn-primary')->addClass('btn-success');
249
}
250
251
$form->add($section);
252 37676f4e jim-p
253
$form->addGlobal(new Form_Button(
254
	'Submit',
255 faab522f Renato Botelho
	'Lookup',
256 37676f4e jim-p
	null,
257
	'fa-search'
258
))->addClass('btn-primary');
259
260 286aa3f2 sbeaver
print $form;
261
262 bcd938ef sbeaver
if (!$input_errors && $type) {
263 6ee37b41 Sjon Hortensius
	if ($resolved):
264 86ab47ff sullrich
?>
265 06da0d4e Sjon Hortensius
<div class="panel panel-default">
266 babf5d85 Phil Davis
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Results')?></h2></div>
267 06da0d4e Sjon Hortensius
	<div class="panel-body">
268
		<ul class="list-group">
269 e9258698 NewEraCracker
<?php
270 06da0d4e Sjon Hortensius
		foreach ((array)$resolved as $hostitem) {
271 86ab47ff sullrich
?>
272 06da0d4e Sjon Hortensius
			<li class="list-group-item"><?=$hostitem?></li>
273 e9258698 NewEraCracker
<?php
274 06da0d4e Sjon Hortensius
			if ($hostitem != "") {
275
				$found++;
276
			}
277
		}
278 286aa3f2 sbeaver
?>
279 06da0d4e Sjon Hortensius
		</ul>
280
	</div>
281
</div>
282 fa172bc5 NewEraCracker
<?php endif; ?>
283 06da0d4e Sjon Hortensius
284
<!-- Second table displays the server resolution times -->
285
<div class="panel panel-default">
286 babf5d85 Phil Davis
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Timings')?></h2></div>
287 06da0d4e Sjon Hortensius
	<div class="panel-body">
288
		<table class="table">
289
		<thead>
290
			<tr>
291 babf5d85 Phil Davis
				<th><?=gettext('Name server')?></th>
292
				<th><?=gettext('Query time')?></th>
293 06da0d4e Sjon Hortensius
			</tr>
294
		</thead>
295
296
		<tbody>
297 fa172bc5 NewEraCracker
<?php foreach ((array)$dns_speeds as $qt):?>
298 737ed7d1 jim-p
		<tr>
299 06da0d4e Sjon Hortensius
			<td><?=$qt['dns_server']?></td><td><?=$qt['query_time']?></td>
300 737ed7d1 jim-p
		</tr>
301 fa172bc5 NewEraCracker
<?php endforeach; ?>
302 06da0d4e Sjon Hortensius
		</tbody>
303
		</table>
304
	</div>
305
</div>
306
307
<!-- Third table displays "More information" -->
308
<div class="panel panel-default">
309 3d7a8696 k-paulius
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('More Information')?></h2></div>
310 06da0d4e Sjon Hortensius
	<div class="panel-body">
311
		<ul class="list-group">
312 7d67222e heper
			<li class="list-group-item"><a href="/diag_ping.php?host=<?=htmlspecialchars($host)?>&amp;count=3"><?=gettext("Ping")?></a></li>
313 06da0d4e Sjon Hortensius
			<li class="list-group-item"><a href="/diag_traceroute.php?host=<?=htmlspecialchars($host)?>&amp;ttl=18"><?=gettext("Traceroute")?></a></li>
314
		</ul>
315 57adffdd Jared Dillard
		<h5><?=gettext("NOTE: The following links are to external services, so their reliability cannot be guaranteed.");?></h5>
316 06da0d4e Sjon Hortensius
		<ul class="list-group">
317 5c0ab3cd NewEraCracker
			<li class="list-group-item"><a target="_blank" href="http://private.dnsstuff.com/tools/whois.ch?ip=<?=$ipaddr;?>"><?=gettext("IP WHOIS @ DNS Stuff");?></a></li>
318
			<li class="list-group-item"><a target="_blank" href="http://private.dnsstuff.com/tools/ipall.ch?ip=<?=$ipaddr;?>"><?=gettext("IP Info @ DNS Stuff");?></a></li>
319 06da0d4e Sjon Hortensius
		</ul>
320
	</div>
321
</div>
322
<?php
323 bcd938ef sbeaver
}
324 c10cb196 Stephen Beaver
include("foot.inc");