Project

General

Profile

Download (9.31 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 3fc58484 Stephen Beaver
	pfSense_MODULE: dns
57 13d193c2 Scott Ullrich
*/
58
59 a57d9fa2 jim-p
##|+PRIV
60
##|*IDENT=page-diagnostics-dns
61 5230f468 jim-p
##|*NAME=Diagnostics: DNS Lookup
62 a57d9fa2 jim-p
##|*DESCR=Allow access to the 'Diagnostics: DNS Lookup' page.
63
##|*MATCH=diag_dns.php*
64
##|-PRIV
65
66 699737d9 Phil Davis
$pgtitle = array(gettext("Diagnostics"), gettext("DNS Lookup"));
67 737ed7d1 jim-p
require("guiconfig.inc");
68
69 ed2a6e89 jim-p
$host = trim($_REQUEST['host'], " \t\n\r\0\x0B[];\"'");
70 76c4ff0e Renato Botelho
$host_esc = escapeshellarg($host);
71
72 6ee37b41 Sjon Hortensius
/* If this section of config.xml has not been populated yet we need to set it up
73 d5b28fcf Stephen Beaver
*/
74
if (!is_array($config['aliases']['alias'])) {
75
	$config['aliases']['alias'] = array();
76 bc0a452f jim-p
}
77 d5b28fcf Stephen Beaver
$a_aliases = &$config['aliases']['alias'];
78
79 699737d9 Phil Davis
$aliasname = str_replace(array(".", "-"), "_", $host);
80 ed2a6e89 jim-p
$alias_exists = false;
81 6c07db48 Phil Davis
$counter = 0;
82 5f601060 Phil Davis
foreach ($a_aliases as $a) {
83
	if ($a['name'] == $aliasname) {
84 ed2a6e89 jim-p
		$alias_exists = true;
85 6c07db48 Phil Davis
		$id = $counter;
86 ed2a6e89 jim-p
	}
87
	$counter++;
88
}
89
90 5f601060 Phil Davis
if (isset($_POST['create_alias']) && (is_hostname($host) || is_ipaddr($host))) {
91
	if ($_POST['override']) {
92 e55d4b3c Scott Ullrich
		$override = true;
93 5f601060 Phil Davis
	}
94 e55d4b3c Scott Ullrich
	$resolved = gethostbyname($host);
95 ed2a6e89 jim-p
	$type = "hostname";
96 5f601060 Phil Davis
	if ($resolved) {
97 ed2a6e89 jim-p
		$resolved = array();
98
		exec("/usr/bin/drill {$host_esc} A | /usr/bin/grep {$host_esc} | /usr/bin/grep -v ';' | /usr/bin/awk '{ print $5 }'", $resolved);
99 e55d4b3c Scott Ullrich
		$isfirst = true;
100 288a2a0f Phil Davis
		foreach ($resolved as $re) {
101
			if ($re != "") {
102
				if (!$isfirst)
103 7a87cb97 Scott Ullrich
					$addresses .= " ";
104 ed2a6e89 jim-p
				$addresses .= rtrim($re) . "/32";
105 7a87cb97 Scott Ullrich
				$isfirst = false;
106
			}
107 e55d4b3c Scott Ullrich
		}
108
		$newalias = array();
109 5f601060 Phil Davis
		if ($override) {
110 e55d4b3c Scott Ullrich
			$alias_exists = false;
111 5f601060 Phil Davis
		}
112
		if ($alias_exists == false) {
113 705c72ea Scott Ullrich
			$newalias['name'] = $aliasname;
114 e1f6a0d9 Scott Ullrich
			$newalias['type'] = "network";
115 e55d4b3c Scott Ullrich
			$newalias['address'] = $addresses;
116
			$newalias['descr'] = "Created from Diagnostics-> DNS Lookup";
117 5f601060 Phil Davis
			if ($override) {
118 e55d4b3c Scott Ullrich
				$a_aliases[$id] = $newalias;
119 5f601060 Phil Davis
			} else {
120 e55d4b3c Scott Ullrich
				$a_aliases[] = $newalias;
121 5f601060 Phil Davis
			}
122 e55d4b3c Scott Ullrich
			write_config();
123
			$createdalias = true;
124
		}
125
	}
126
}
127
128 737ed7d1 jim-p
if ($_POST) {
129
	unset($input_errors);
130
131
	$reqdfields = explode(" ", "host");
132
	$reqdfieldsn = explode(",", "Host");
133
134 1e9b4611 Renato Botelho
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
135 286aa3f2 sbeaver
136 72cd706b bcyrill
	if (!is_hostname($host) && !is_ipaddr($host)) {
137 ee91ae30 jean.feltrin
		$input_errors[] = gettext("Host must be a valid hostname or IP address.");
138 72cd706b bcyrill
	} else {
139
		// Test resolution speed of each DNS server.
140 86ab47ff sullrich
		$dns_speeds = array();
141 ed2a6e89 jim-p
		$dns_servers = array();
142
		exec("/usr/bin/grep nameserver /etc/resolv.conf | /usr/bin/cut -f2 -d' '", $dns_servers);
143 40af551f jim-p
		foreach ($dns_servers as $dns_server) {
144 ed2a6e89 jim-p
			$query_time = exec("/usr/bin/drill {$host_esc} " . escapeshellarg("@" . trim($dns_server)) . " | /usr/bin/grep Query | /usr/bin/cut -d':' -f2");
145 5f601060 Phil Davis
			if ($query_time == "") {
146 ee91ae30 jean.feltrin
				$query_time = gettext("No response");
147 5f601060 Phil Davis
			}
148 86ab47ff sullrich
			$new_qt = array();
149
			$new_qt['dns_server'] = $dns_server;
150 ed2a6e89 jim-p
			$new_qt['query_time'] = $query_time;
151 86ab47ff sullrich
			$dns_speeds[] = $new_qt;
152
			unset($new_qt);
153
		}
154 737ed7d1 jim-p
	}
155
156 da652a46 Vinicius Coque
	$type = "unknown";
157 737ed7d1 jim-p
	$resolved = "";
158 2312b0eb jim-p
	$ipaddr = "";
159
	$hostname = "";
160 737ed7d1 jim-p
	if (!$input_errors) {
161
		if (is_ipaddr($host)) {
162
			$type = "ip";
163
			$resolved = gethostbyaddr($host);
164 2312b0eb jim-p
			$ipaddr = $host;
165 5f601060 Phil Davis
			if ($host != $resolved) {
166 2312b0eb jim-p
				$hostname = $resolved;
167 5f601060 Phil Davis
			}
168 737ed7d1 jim-p
		} elseif (is_hostname($host)) {
169
			$type = "hostname";
170
			$resolved = gethostbyname($host);
171 5f601060 Phil Davis
			if ($resolved) {
172 ed2a6e89 jim-p
				$resolved = array();
173
				exec("/usr/bin/drill {$host_esc} A | /usr/bin/grep {$host_esc} | /usr/bin/grep -v ';' | /usr/bin/awk '{ print $5 }'", $resolved);
174 c43f732a Scott Ullrich
			}
175 2312b0eb jim-p
			$hostname = $host;
176 5f601060 Phil Davis
			if ($host != $resolved) {
177 3e85ac2b Scott Ullrich
				$ipaddr = $resolved[0];
178 5f601060 Phil Davis
			}
179 737ed7d1 jim-p
		}
180
181
		if ($host == $resolved) {
182 ee91ae30 jean.feltrin
			$resolved = gettext("No record found");
183 737ed7d1 jim-p
		}
184
	}
185
}
186
187 5f601060 Phil Davis
if (($_POST['host']) && ($_POST['dialog_output'])) {
188 699737d9 Phil Davis
	display_host_results ($host, $resolved, $dns_speeds);
189 fe74228f N0YB
	exit;
190
}
191
192 699737d9 Phil Davis
function display_host_results ($address, $hostname, $dns_speeds) {
193 79f5aebe Robert Nelson
	$map_lengths = function($element) { return strlen($element[0]); };
194 6ff71328 Robert Nelson
195 fe74228f N0YB
	echo gettext("IP Address") . ": {$address} \n";
196
	echo gettext("Host Name") . ": {$hostname} \n";
197
	echo "\n";
198 6ff71328 Robert Nelson
	$text_table = array();
199
	$text_table[] = array(gettext("Server"), gettext("Query Time"));
200
	if (is_array($dns_speeds)) {
201
		foreach ($dns_speeds as $qt) {
202
			$text_table[] = array(trim($qt['dns_server']), trim($qt['query_time']));
203 fe74228f N0YB
		}
204 6ff71328 Robert Nelson
	}
205 79f5aebe Robert Nelson
	$col0_padlength = max(array_map($map_lengths, $text_table)) + 4;
206 6ff71328 Robert Nelson
	foreach ($text_table as $text_row) {
207 79f5aebe Robert Nelson
		echo str_pad($text_row[0], $col0_padlength) . $text_row[1] . "\n";
208 6ff71328 Robert Nelson
	}
209 fe74228f N0YB
}
210
211 286aa3f2 sbeaver
include("head.inc");
212 e55d4b3c Scott Ullrich
213 286aa3f2 sbeaver
/* Display any error messages resulting from user input */
214
if ($input_errors)
215
	print_input_errors($input_errors);
216
else if (!$resolved && $type)
217 06da0d4e Sjon Hortensius
	print('<div class="alert alert-warning" role="alert">' . gettext("Host") .' "'. $host .'" '. gettext("could not be resolved") . '</div>');
218 286aa3f2 sbeaver
219 06da0d4e Sjon Hortensius
if ($createdalias)
220
	print('<div class="alert alert-success" role="alert">'.gettext("Alias was created/updated successfully").'</div>');
221 286aa3f2 sbeaver
222 38e06c66 Sjon Hortensius
$form = new Form('Lookup');
223 06da0d4e Sjon Hortensius
$section = new Form_Section('DNS Lookup');
224 286aa3f2 sbeaver
225
$section->addInput(new Form_Input(
226
	'host',
227 06da0d4e Sjon Hortensius
	'Hostname',
228 286aa3f2 sbeaver
	'text',
229 06da0d4e Sjon Hortensius
	$host,
230
	['placeholder' => 'Hostname to look up.']
231
));
232 286aa3f2 sbeaver
233 06da0d4e Sjon Hortensius
if (!empty($resolved)) {
234 286aa3f2 sbeaver
	$form->addGlobal(new Form_Button(
235 06da0d4e Sjon Hortensius
		'create_alias',
236
		'Add alias'
237 286aa3f2 sbeaver
	))->removeClass('btn-primary')->addClass('btn-success');
238
}
239
240
$form->add($section);
241
print $form;
242
243 bcd938ef sbeaver
if (!$input_errors && $type) {
244 6ee37b41 Sjon Hortensius
	if ($resolved):
245 86ab47ff sullrich
?>
246 06da0d4e Sjon Hortensius
<div class="panel panel-default">
247 f17594c7 Sjon Hortensius
	<div class="panel-heading"><h2 class="panel-title">Results</h2></div>
248 06da0d4e Sjon Hortensius
	<div class="panel-body">
249
		<ul class="list-group">
250
<?
251
		foreach ((array)$resolved as $hostitem) {
252 86ab47ff sullrich
?>
253 06da0d4e Sjon Hortensius
			<li class="list-group-item"><?=$hostitem?></li>
254
<?
255
			if ($hostitem != "") {
256
				$found++;
257
			}
258
		}
259 286aa3f2 sbeaver
?>
260 06da0d4e Sjon Hortensius
		</ul>
261
	</div>
262
</div>
263 6ee37b41 Sjon Hortensius
<? endif?>
264 06da0d4e Sjon Hortensius
265
<!-- Second table displays the server resolution times -->
266
<div class="panel panel-default">
267 f17594c7 Sjon Hortensius
	<div class="panel-heading"><h2 class="panel-title">Timings</h2></div>
268 06da0d4e Sjon Hortensius
	<div class="panel-body">
269
		<table class="table">
270
		<thead>
271
			<tr>
272
				<th>Name server</th>
273
				<th>Query time</th>
274
			</tr>
275
		</thead>
276
277
		<tbody>
278
<? foreach ((array)$dns_speeds as $qt):?>
279 737ed7d1 jim-p
		<tr>
280 06da0d4e Sjon Hortensius
			<td><?=$qt['dns_server']?></td><td><?=$qt['query_time']?></td>
281 737ed7d1 jim-p
		</tr>
282 06da0d4e Sjon Hortensius
<? endforeach?>
283
		</tbody>
284
		</table>
285
	</div>
286
</div>
287
288
<!-- Third table displays "More information" -->
289
<div class="panel panel-default">
290 f17594c7 Sjon Hortensius
	<div class="panel-heading"><h2 class="panel-title">More information</h2></div>
291 06da0d4e Sjon Hortensius
	<div class="panel-body">
292
		<ul class="list-group">
293 7d67222e heper
			<li class="list-group-item"><a href="/diag_ping.php?host=<?=htmlspecialchars($host)?>&amp;count=3"><?=gettext("Ping")?></a></li>
294 06da0d4e Sjon Hortensius
			<li class="list-group-item"><a href="/diag_traceroute.php?host=<?=htmlspecialchars($host)?>&amp;ttl=18"><?=gettext("Traceroute")?></a></li>
295
		</ul>
296 57adffdd Jared Dillard
		<h5><?=gettext("NOTE: The following links are to external services, so their reliability cannot be guaranteed.");?></h5>
297 06da0d4e Sjon Hortensius
		<ul class="list-group">
298
			<li class="list-group-item"><a target="_blank" href="http://private.dnsstuff.com/tools/whois.ch?ip=<?php echo $ipaddr; ?>"><?=gettext("IP WHOIS @ DNS Stuff");?></a></li>
299
			<li class="list-group-item"><a target="_blank" href="http://private.dnsstuff.com/tools/ipall.ch?ip=<?php echo $ipaddr; ?>"><?=gettext("IP Info @ DNS Stuff");?></a></li>
300
		</ul>
301
	</div>
302
</div>
303
<?php
304 bcd938ef sbeaver
}
305 c10cb196 Stephen Beaver
include("foot.inc");