Project

General

Profile

Download (11.5 KB) Statistics
| Branch: | Tag: | Revision:
1 737ed7d1 jim-p
<?php
2
/*
3 aaec5634 Renato Botelho
 * diag_dns.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 2a2396a6 Renato Botelho
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7 aaec5634 Renato Botelho
 * All rights reserved.
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, 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 fd9ebcd5 Stephen Beaver
 */
53 13d193c2 Scott Ullrich
54 a57d9fa2 jim-p
##|+PRIV
55
##|*IDENT=page-diagnostics-dns
56 5230f468 jim-p
##|*NAME=Diagnostics: DNS Lookup
57 a57d9fa2 jim-p
##|*DESCR=Allow access to the 'Diagnostics: DNS Lookup' page.
58
##|*MATCH=diag_dns.php*
59
##|-PRIV
60
61 699737d9 Phil Davis
$pgtitle = array(gettext("Diagnostics"), gettext("DNS Lookup"));
62 aceaf18c Phil Davis
require_once("guiconfig.inc");
63 737ed7d1 jim-p
64 ed2a6e89 jim-p
$host = trim($_REQUEST['host'], " \t\n\r\0\x0B[];\"'");
65 76c4ff0e Renato Botelho
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 7bc19545 Chris Buechler
$aliasname = substr(str_replace(array(".", "-"), "_", $host), 0, 31);
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 0b316bd1 PiBa-NL
function resolve_host_addresses($host) {
85
	$recordtypes = array(DNS_A, DNS_AAAA, DNS_CNAME);
86
	$dnsresult = array();
87
	$resolved = array();
88
	$errreporting = error_reporting();
89
	error_reporting($errreporting & ~E_WARNING);// dns_get_record throws a warning if nothing is resolved..
90 9488f42b Phil Davis
	foreach ($recordtypes as $recordtype) {
91 0b316bd1 PiBa-NL
		$tmp = dns_get_record($host, $recordtype);
92
		if (is_array($tmp)) {
93
			$dnsresult = array_merge($dnsresult, $tmp);
94
		}
95
	}
96
	error_reporting($errreporting);// restore original php warning/error settings.
97 6002af93 NewEraCracker
98 9488f42b Phil Davis
	foreach ($dnsresult as $item) {
99 0b316bd1 PiBa-NL
		$newitem = array();
100
		$newitem['type'] = $item['type'];
101
		switch ($item['type']) {
102
			case 'CNAME':
103
				$newitem['data'] = $item['target'];
104
				$resolved[] = $newitem;
105
				break;
106
			case 'A':
107
				$newitem['data'] = $item['ip'];
108
				$resolved[] = $newitem;
109
				break;
110
			case 'AAAA':
111
				$newitem['data'] = $item['ipv6'];
112
				$resolved[] = $newitem;
113
				break;
114
		}
115
	}
116
	return $resolved;
117
}
118
119 d9f7babc Phil Davis
if (isAllowedPage('firewall_aliases_edit.php') && isset($_POST['create_alias']) && (is_hostname($host) || is_ipaddr($host))) {
120 e55d4b3c Scott Ullrich
	$resolved = gethostbyname($host);
121 ed2a6e89 jim-p
	$type = "hostname";
122 5f601060 Phil Davis
	if ($resolved) {
123 0b316bd1 PiBa-NL
		$resolved = resolve_host_addresses($host);
124 e55d4b3c Scott Ullrich
		$isfirst = true;
125 7de0b827 Phil Davis
		$addresses = "";
126 288a2a0f Phil Davis
		foreach ($resolved as $re) {
127 0b316bd1 PiBa-NL
			if ($re['data'] != "") {
128 947141fd Phil Davis
				if (!$isfirst) {
129 7a87cb97 Scott Ullrich
					$addresses .= " ";
130 947141fd Phil Davis
				}
131 0b316bd1 PiBa-NL
				$re = rtrim($re['data']);
132 11614bc9 Phil Davis
				if (is_ipaddr($re)) {
133
					$sn = is_ipaddrv6($re) ? '/128' : '/32';
134
				} else {
135
					// The name was a CNAME and resolved to another name, rather than an address.
136
					// In this case the alias entry will have a FQDN, so do not put a CIDR after it.
137
					$sn = "";
138
				}
139 97f42a05 Renato Botelho
				$addresses .= $re . $sn;
140 7a87cb97 Scott Ullrich
				$isfirst = false;
141
			}
142 e55d4b3c Scott Ullrich
		}
143 7de0b827 Phil Davis
		if ($addresses == "") {
144
			$couldnotcreatealias = true;
145 4d9a5d99 Stephen Beaver
		} else {
146 7de0b827 Phil Davis
			$newalias = array();
147
			$newalias['name'] = $aliasname;
148
			$newalias['type'] = "network";
149
			$newalias['address'] = $addresses;
150
			$newalias['descr'] = gettext("Created from Diagnostics-> DNS Lookup");
151
			if ($alias_exists) {
152
				$a_aliases[$id] = $newalias;
153
			} else {
154
				$a_aliases[] = $newalias;
155
			}
156
			write_config(gettext("Created an alias from Diagnostics - DNS Lookup page."));
157
			$createdalias = true;
158 e55d4b3c Scott Ullrich
		}
159 7de0b827 Phil Davis
	} else {
160
		$couldnotcreatealias = true;
161 e55d4b3c Scott Ullrich
	}
162
}
163
164 737ed7d1 jim-p
if ($_POST) {
165
	unset($input_errors);
166
167
	$reqdfields = explode(" ", "host");
168
	$reqdfieldsn = explode(",", "Host");
169
170 1e9b4611 Renato Botelho
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
171 286aa3f2 sbeaver
172 72cd706b bcyrill
	if (!is_hostname($host) && !is_ipaddr($host)) {
173 ee91ae30 jean.feltrin
		$input_errors[] = gettext("Host must be a valid hostname or IP address.");
174 72cd706b bcyrill
	} else {
175
		// Test resolution speed of each DNS server.
176 86ab47ff sullrich
		$dns_speeds = array();
177 ed2a6e89 jim-p
		$dns_servers = array();
178
		exec("/usr/bin/grep nameserver /etc/resolv.conf | /usr/bin/cut -f2 -d' '", $dns_servers);
179 40af551f jim-p
		foreach ($dns_servers as $dns_server) {
180 ed2a6e89 jim-p
			$query_time = exec("/usr/bin/drill {$host_esc} " . escapeshellarg("@" . trim($dns_server)) . " | /usr/bin/grep Query | /usr/bin/cut -d':' -f2");
181 5f601060 Phil Davis
			if ($query_time == "") {
182 ee91ae30 jean.feltrin
				$query_time = gettext("No response");
183 5f601060 Phil Davis
			}
184 86ab47ff sullrich
			$new_qt = array();
185
			$new_qt['dns_server'] = $dns_server;
186 ed2a6e89 jim-p
			$new_qt['query_time'] = $query_time;
187 86ab47ff sullrich
			$dns_speeds[] = $new_qt;
188
			unset($new_qt);
189
		}
190 737ed7d1 jim-p
	}
191
192 da652a46 Vinicius Coque
	$type = "unknown";
193 6002af93 NewEraCracker
	$resolved = array();
194 2312b0eb jim-p
	$ipaddr = "";
195 737ed7d1 jim-p
	if (!$input_errors) {
196
		if (is_ipaddr($host)) {
197
			$type = "ip";
198 c4045b79 Chris Buechler
			$resolvedptr = gethostbyaddr($host);
199 2312b0eb jim-p
			$ipaddr = $host;
200 c4045b79 Chris Buechler
			if ($host != $resolvedptr) {
201
				$tmpresolved = array();
202
				$tmpresolved['type'] = "PTR";
203
				$tmpresolved['data'] = $resolvedptr;
204
				$resolved[] = $tmpresolved;
205 5f601060 Phil Davis
			}
206 737ed7d1 jim-p
		} elseif (is_hostname($host)) {
207
			$type = "hostname";
208 6002af93 NewEraCracker
			$ipaddr = gethostbyname($host);
209
			$resolved = resolve_host_addresses($host);
210 737ed7d1 jim-p
		}
211
	}
212
}
213
214 6002af93 NewEraCracker
if ($_POST['host'] && $_POST['dialog_output']) {
215
	$host = (isset($resolvedptr) ? $resolvedptr : $host);
216
	display_host_results ($ipaddr, $host, $dns_speeds);
217 fe74228f N0YB
	exit;
218
}
219
220 699737d9 Phil Davis
function display_host_results ($address, $hostname, $dns_speeds) {
221 79f5aebe Robert Nelson
	$map_lengths = function($element) { return strlen($element[0]); };
222 6ff71328 Robert Nelson
223 d2466ce6 jim-p
	echo gettext("IP Address") . ": " . htmlspecialchars($address) . " \n";
224
	echo gettext("Host Name") . ": " . htmlspecialchars($hostname) .  " \n";
225 fe74228f N0YB
	echo "\n";
226 6ff71328 Robert Nelson
	$text_table = array();
227
	$text_table[] = array(gettext("Server"), gettext("Query Time"));
228
	if (is_array($dns_speeds)) {
229
		foreach ($dns_speeds as $qt) {
230
			$text_table[] = array(trim($qt['dns_server']), trim($qt['query_time']));
231 fe74228f N0YB
		}
232 6ff71328 Robert Nelson
	}
233 79f5aebe Robert Nelson
	$col0_padlength = max(array_map($map_lengths, $text_table)) + 4;
234 6ff71328 Robert Nelson
	foreach ($text_table as $text_row) {
235 79f5aebe Robert Nelson
		echo str_pad($text_row[0], $col0_padlength) . $text_row[1] . "\n";
236 6ff71328 Robert Nelson
	}
237 fe74228f N0YB
}
238
239 286aa3f2 sbeaver
include("head.inc");
240 e55d4b3c Scott Ullrich
241 286aa3f2 sbeaver
/* Display any error messages resulting from user input */
242 947141fd Phil Davis
if ($input_errors) {
243 286aa3f2 sbeaver
	print_input_errors($input_errors);
244 947141fd Phil Davis
} else if (!$resolved && $type) {
245 02342d8c k-paulius
	print_info_box(sprintf(gettext('Host "%s" could not be resolved.'), $host), 'warning', false);
246 947141fd Phil Davis
}
247 286aa3f2 sbeaver
248 947141fd Phil Davis
if ($createdalias) {
249 4d9a5d99 Stephen Beaver
	if ($alias_exists) {
250
		print_info_box(gettext("Alias was updated successfully."), 'success');
251
	} else {
252
		print_info_box(gettext("Alias was created successfully."), 'success');
253
	}
254 137196e7 Phil Davis
255
	$alias_exists = true;
256 947141fd Phil Davis
}
257 286aa3f2 sbeaver
258 7de0b827 Phil Davis
if ($couldnotcreatealias) {
259
	if ($alias_exists) {
260
		print_info_box(sprintf(gettext("Could not update alias for %s"), $host), 'warning', false);
261
	} else {
262
		print_info_box(sprintf(gettext("Could not create alias for %s"), $host), 'warning', false);
263
	}
264
}
265
266 37676f4e jim-p
$form = new Form(false);
267 06da0d4e Sjon Hortensius
$section = new Form_Section('DNS Lookup');
268 286aa3f2 sbeaver
269
$section->addInput(new Form_Input(
270
	'host',
271 edfefd7e Phil Davis
	'*Hostname',
272 286aa3f2 sbeaver
	'text',
273 06da0d4e Sjon Hortensius
	$host,
274
	['placeholder' => 'Hostname to look up.']
275
));
276 286aa3f2 sbeaver
277 7208f387 Chris Buechler
$form->add($section);
278
279
$form->addGlobal(new Form_Button(
280
        'Submit',
281
        'Lookup',
282
        null,
283
        'fa-search'
284
))->addClass('btn-primary');
285
286 d9f7babc Phil Davis
if (!empty($resolved) && isAllowedPage('firewall_aliases_edit.php')) {
287 4d9a5d99 Stephen Beaver
	if ($alias_exists) {
288
		$button_text = gettext("Update alias");
289
	} else {
290
		$button_text = gettext("Add alias");
291
	}
292 286aa3f2 sbeaver
	$form->addGlobal(new Form_Button(
293 06da0d4e Sjon Hortensius
		'create_alias',
294 4d9a5d99 Stephen Beaver
		$button_text,
295 37676f4e jim-p
		null,
296
		'fa-plus'
297 286aa3f2 sbeaver
	))->removeClass('btn-primary')->addClass('btn-success');
298
}
299
300
print $form;
301
302 bcd938ef sbeaver
if (!$input_errors && $type) {
303 6ee37b41 Sjon Hortensius
	if ($resolved):
304 86ab47ff sullrich
?>
305 06da0d4e Sjon Hortensius
<div class="panel panel-default">
306 babf5d85 Phil Davis
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Results')?></h2></div>
307 06da0d4e Sjon Hortensius
	<div class="panel-body">
308 6002af93 NewEraCracker
309 0b316bd1 PiBa-NL
		<table class="table">
310
		<thead>
311
			<tr>
312
				<th><?=gettext('Result')?></th>
313
				<th><?=gettext('Record type')?></th>
314
			</tr>
315
		</thead>
316
		<tbody>
317
<?php foreach ((array)$resolved as $hostitem):?>
318
		<tr>
319 d2466ce6 jim-p
			<td><?=htmlspecialchars($hostitem['data'])?></td><td><?=htmlspecialchars($hostitem['type'])?></td>
320 0b316bd1 PiBa-NL
		</tr>
321
<?php endforeach; ?>
322
		</tbody>
323
		</table>
324 06da0d4e Sjon Hortensius
	</div>
325
</div>
326 fa172bc5 NewEraCracker
<?php endif; ?>
327 06da0d4e Sjon Hortensius
328
<!-- Second table displays the server resolution times -->
329
<div class="panel panel-default">
330 babf5d85 Phil Davis
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Timings')?></h2></div>
331 06da0d4e Sjon Hortensius
	<div class="panel-body">
332
		<table class="table">
333
		<thead>
334
			<tr>
335 babf5d85 Phil Davis
				<th><?=gettext('Name server')?></th>
336
				<th><?=gettext('Query time')?></th>
337 06da0d4e Sjon Hortensius
			</tr>
338
		</thead>
339
340
		<tbody>
341 fa172bc5 NewEraCracker
<?php foreach ((array)$dns_speeds as $qt):?>
342 737ed7d1 jim-p
		<tr>
343 d2466ce6 jim-p
			<td><?=htmlspecialchars($qt['dns_server'])?></td><td><?=htmlspecialchars($qt['query_time'])?></td>
344 737ed7d1 jim-p
		</tr>
345 fa172bc5 NewEraCracker
<?php endforeach; ?>
346 06da0d4e Sjon Hortensius
		</tbody>
347
		</table>
348
	</div>
349
</div>
350
351
<!-- Third table displays "More information" -->
352
<div class="panel panel-default">
353 3d7a8696 k-paulius
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('More Information')?></h2></div>
354 06da0d4e Sjon Hortensius
	<div class="panel-body">
355
		<ul class="list-group">
356 7d67222e heper
			<li class="list-group-item"><a href="/diag_ping.php?host=<?=htmlspecialchars($host)?>&amp;count=3"><?=gettext("Ping")?></a></li>
357 06da0d4e Sjon Hortensius
			<li class="list-group-item"><a href="/diag_traceroute.php?host=<?=htmlspecialchars($host)?>&amp;ttl=18"><?=gettext("Traceroute")?></a></li>
358
		</ul>
359 57adffdd Jared Dillard
		<h5><?=gettext("NOTE: The following links are to external services, so their reliability cannot be guaranteed.");?></h5>
360 06da0d4e Sjon Hortensius
		<ul class="list-group">
361 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>
362
			<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>
363 06da0d4e Sjon Hortensius
		</ul>
364
	</div>
365
</div>
366
<?php
367 bcd938ef sbeaver
}
368 254fb5b4 Phil Davis
?>
369
<script type="text/javascript">
370
//<![CDATA[
371
events.push(function() {
372
	var original_host = "<?=$host;?>";
373
374
	$('input[name="host"]').on('input', function() {
375
		if ($('#host').val() == original_host) {
376
			disableInput('create_alias', false);
377
		} else {
378
			disableInput('create_alias', true);
379
		}
380
	});
381
});
382
//]]>
383
</script>
384
<?php
385 c10cb196 Stephen Beaver
include("foot.inc");