Project

General

Profile

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