Project

General

Profile

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