Project

General

Profile

Download (8.92 KB) Statistics
| Branch: | Tag: | Revision:
1 737ed7d1 jim-p
<?php
2
/*
3 c5d81585 Renato Botelho
 * diag_dns.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 81299b5c Renato Botelho
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7 c5d81585 Renato Botelho
 * All rights reserved.
8
 *
9 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12 c5d81585 Renato Botelho
 *
13 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
14 c5d81585 Renato Botelho
 *
15 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20 fd9ebcd5 Stephen Beaver
 */
21 13d193c2 Scott Ullrich
22 a57d9fa2 jim-p
##|+PRIV
23
##|*IDENT=page-diagnostics-dns
24 5230f468 jim-p
##|*NAME=Diagnostics: DNS Lookup
25 a57d9fa2 jim-p
##|*DESCR=Allow access to the 'Diagnostics: DNS Lookup' page.
26
##|*MATCH=diag_dns.php*
27
##|-PRIV
28
29 699737d9 Phil Davis
$pgtitle = array(gettext("Diagnostics"), gettext("DNS Lookup"));
30 c81ef6e2 Phil Davis
require_once("guiconfig.inc");
31 737ed7d1 jim-p
32 ed2a6e89 jim-p
$host = trim($_REQUEST['host'], " \t\n\r\0\x0B[];\"'");
33 76c4ff0e Renato Botelho
34 6ee37b41 Sjon Hortensius
/* If this section of config.xml has not been populated yet we need to set it up
35 d5b28fcf Stephen Beaver
*/
36
if (!is_array($config['aliases']['alias'])) {
37
	$config['aliases']['alias'] = array();
38 bc0a452f jim-p
}
39 d5b28fcf Stephen Beaver
$a_aliases = &$config['aliases']['alias'];
40
41 d79ff71a Chris Buechler
$aliasname = substr(str_replace(array(".", "-"), "_", $host), 0, 31);
42 ed2a6e89 jim-p
$alias_exists = false;
43 6c07db48 Phil Davis
$counter = 0;
44 5f601060 Phil Davis
foreach ($a_aliases as $a) {
45
	if ($a['name'] == $aliasname) {
46 ed2a6e89 jim-p
		$alias_exists = true;
47 6c07db48 Phil Davis
		$id = $counter;
48 ed2a6e89 jim-p
	}
49
	$counter++;
50
}
51
52 ba40ee75 PiBa-NL
function resolve_host_addresses($host) {
53
	$recordtypes = array(DNS_A, DNS_AAAA, DNS_CNAME);
54
	$dnsresult = array();
55
	$resolved = array();
56
	$errreporting = error_reporting();
57
	error_reporting($errreporting & ~E_WARNING);// dns_get_record throws a warning if nothing is resolved..
58 9d3e8723 Phil Davis
	foreach ($recordtypes as $recordtype) {
59 ba40ee75 PiBa-NL
		$tmp = dns_get_record($host, $recordtype);
60
		if (is_array($tmp)) {
61
			$dnsresult = array_merge($dnsresult, $tmp);
62
		}
63
	}
64
	error_reporting($errreporting);// restore original php warning/error settings.
65 df7f65a3 NewEraCracker
66 9d3e8723 Phil Davis
	foreach ($dnsresult as $item) {
67 ba40ee75 PiBa-NL
		$newitem = array();
68
		$newitem['type'] = $item['type'];
69
		switch ($item['type']) {
70
			case 'CNAME':
71
				$newitem['data'] = $item['target'];
72
				$resolved[] = $newitem;
73
				break;
74
			case 'A':
75
				$newitem['data'] = $item['ip'];
76
				$resolved[] = $newitem;
77
				break;
78
			case 'AAAA':
79
				$newitem['data'] = $item['ipv6'];
80
				$resolved[] = $newitem;
81
				break;
82
		}
83
	}
84
	return $resolved;
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 ba40ee75 PiBa-NL
		$resolved = resolve_host_addresses($host);
92 e55d4b3c Scott Ullrich
		$isfirst = true;
93 288a2a0f Phil Davis
		foreach ($resolved as $re) {
94 ba40ee75 PiBa-NL
			if ($re['data'] != "") {
95 947141fd Phil Davis
				if (!$isfirst) {
96 7a87cb97 Scott Ullrich
					$addresses .= " ";
97 947141fd Phil Davis
				}
98 ba40ee75 PiBa-NL
				$re = rtrim($re['data']);
99 607b785f Phil Davis
				if (is_ipaddr($re)) {
100
					$sn = is_ipaddrv6($re) ? '/128' : '/32';
101
				} else {
102
					// The name was a CNAME and resolved to another name, rather than an address.
103
					// In this case the alias entry will have a FQDN, so do not put a CIDR after it.
104
					$sn = "";
105
				}
106 97f42a05 Renato Botelho
				$addresses .= $re . $sn;
107 7a87cb97 Scott Ullrich
				$isfirst = false;
108
			}
109 e55d4b3c Scott Ullrich
		}
110
		$newalias = array();
111 a2d92b48 Phil Davis
		$newalias['name'] = $aliasname;
112
		$newalias['type'] = "network";
113
		$newalias['address'] = $addresses;
114
		$newalias['descr'] = gettext("Created from Diagnostics-> DNS Lookup");
115
		if ($alias_exists) {
116
			$a_aliases[$id] = $newalias;
117
		} else {
118
			$a_aliases[] = $newalias;
119 e55d4b3c Scott Ullrich
		}
120 a2d92b48 Phil Davis
		write_config();
121
		$createdalias = true;
122 e55d4b3c Scott Ullrich
	}
123
}
124
125 737ed7d1 jim-p
if ($_POST) {
126
	unset($input_errors);
127
128
	$reqdfields = explode(" ", "host");
129
	$reqdfieldsn = explode(",", "Host");
130
131 1e9b4611 Renato Botelho
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
132 286aa3f2 sbeaver
133 72cd706b bcyrill
	if (!is_hostname($host) && !is_ipaddr($host)) {
134 ee91ae30 jean.feltrin
		$input_errors[] = gettext("Host must be a valid hostname or IP address.");
135 72cd706b bcyrill
	} else {
136
		// Test resolution speed of each DNS server.
137 86ab47ff sullrich
		$dns_speeds = array();
138 ed2a6e89 jim-p
		$dns_servers = array();
139
		exec("/usr/bin/grep nameserver /etc/resolv.conf | /usr/bin/cut -f2 -d' '", $dns_servers);
140 40af551f jim-p
		foreach ($dns_servers as $dns_server) {
141 ed2a6e89 jim-p
			$query_time = exec("/usr/bin/drill {$host_esc} " . escapeshellarg("@" . trim($dns_server)) . " | /usr/bin/grep Query | /usr/bin/cut -d':' -f2");
142 5f601060 Phil Davis
			if ($query_time == "") {
143 ee91ae30 jean.feltrin
				$query_time = gettext("No response");
144 5f601060 Phil Davis
			}
145 86ab47ff sullrich
			$new_qt = array();
146
			$new_qt['dns_server'] = $dns_server;
147 ed2a6e89 jim-p
			$new_qt['query_time'] = $query_time;
148 86ab47ff sullrich
			$dns_speeds[] = $new_qt;
149
			unset($new_qt);
150
		}
151 737ed7d1 jim-p
	}
152
153 da652a46 Vinicius Coque
	$type = "unknown";
154 df7f65a3 NewEraCracker
	$resolved = array();
155 2312b0eb jim-p
	$ipaddr = "";
156 737ed7d1 jim-p
	if (!$input_errors) {
157
		if (is_ipaddr($host)) {
158
			$type = "ip";
159 a9b6c19a Chris Buechler
			$resolvedptr = gethostbyaddr($host);
160 2312b0eb jim-p
			$ipaddr = $host;
161 a9b6c19a Chris Buechler
			if ($host != $resolvedptr) {
162
				$tmpresolved = array();
163
				$tmpresolved['type'] = "PTR";
164
				$tmpresolved['data'] = $resolvedptr;
165
				$resolved[] = $tmpresolved;
166 5f601060 Phil Davis
			}
167 737ed7d1 jim-p
		} elseif (is_hostname($host)) {
168
			$type = "hostname";
169 df7f65a3 NewEraCracker
			$ipaddr = gethostbyname($host);
170
			$resolved = resolve_host_addresses($host);
171 737ed7d1 jim-p
		}
172
	}
173
}
174
175 df7f65a3 NewEraCracker
if ($_POST['host'] && $_POST['dialog_output']) {
176
	$host = (isset($resolvedptr) ? $resolvedptr : $host);
177
	display_host_results ($ipaddr, $host, $dns_speeds);
178 fe74228f N0YB
	exit;
179
}
180
181 699737d9 Phil Davis
function display_host_results ($address, $hostname, $dns_speeds) {
182 79f5aebe Robert Nelson
	$map_lengths = function($element) { return strlen($element[0]); };
183 6ff71328 Robert Nelson
184 a92de66e jim-p
	echo gettext("IP Address") . ": " . htmlspecialchars($address) . " \n";
185
	echo gettext("Host Name") . ": " . htmlspecialchars($hostname) .  " \n";
186 fe74228f N0YB
	echo "\n";
187 6ff71328 Robert Nelson
	$text_table = array();
188
	$text_table[] = array(gettext("Server"), gettext("Query Time"));
189
	if (is_array($dns_speeds)) {
190
		foreach ($dns_speeds as $qt) {
191
			$text_table[] = array(trim($qt['dns_server']), trim($qt['query_time']));
192 fe74228f N0YB
		}
193 6ff71328 Robert Nelson
	}
194 79f5aebe Robert Nelson
	$col0_padlength = max(array_map($map_lengths, $text_table)) + 4;
195 6ff71328 Robert Nelson
	foreach ($text_table as $text_row) {
196 79f5aebe Robert Nelson
		echo str_pad($text_row[0], $col0_padlength) . $text_row[1] . "\n";
197 6ff71328 Robert Nelson
	}
198 fe74228f N0YB
}
199
200 286aa3f2 sbeaver
include("head.inc");
201 e55d4b3c Scott Ullrich
202 286aa3f2 sbeaver
/* Display any error messages resulting from user input */
203 947141fd Phil Davis
if ($input_errors) {
204 286aa3f2 sbeaver
	print_input_errors($input_errors);
205 947141fd Phil Davis
} else if (!$resolved && $type) {
206 02342d8c k-paulius
	print_info_box(sprintf(gettext('Host "%s" could not be resolved.'), $host), 'warning', false);
207 947141fd Phil Davis
}
208 286aa3f2 sbeaver
209 947141fd Phil Davis
if ($createdalias) {
210 a2d92b48 Phil Davis
	if ($alias_exists) {
211
		print_info_box(gettext("Alias was updated successfully."), 'success');
212
	} else {
213
		print_info_box(gettext("Alias was created successfully."), 'success');
214
	}
215 947141fd Phil Davis
}
216 286aa3f2 sbeaver
217 37676f4e jim-p
$form = new Form(false);
218 06da0d4e Sjon Hortensius
$section = new Form_Section('DNS Lookup');
219 286aa3f2 sbeaver
220
$section->addInput(new Form_Input(
221
	'host',
222 3e2028f4 Phil Davis
	'*Hostname',
223 286aa3f2 sbeaver
	'text',
224 06da0d4e Sjon Hortensius
	$host,
225
	['placeholder' => 'Hostname to look up.']
226
));
227 286aa3f2 sbeaver
228 65292972 Chris Buechler
$form->add($section);
229
230
$form->addGlobal(new Form_Button(
231
        'Submit',
232
        'Lookup',
233
        null,
234
        'fa-search'
235
))->addClass('btn-primary');
236
237 06da0d4e Sjon Hortensius
if (!empty($resolved)) {
238 a2d92b48 Phil Davis
	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 a2d92b48 Phil Davis
		$button_text,
246 37676f4e jim-p
		null,
247
		'fa-plus'
248 286aa3f2 sbeaver
	))->removeClass('btn-primary')->addClass('btn-success');
249
}
250
251
print $form;
252
253 bcd938ef sbeaver
if (!$input_errors && $type) {
254 6ee37b41 Sjon Hortensius
	if ($resolved):
255 86ab47ff sullrich
?>
256 06da0d4e Sjon Hortensius
<div class="panel panel-default">
257 babf5d85 Phil Davis
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Results')?></h2></div>
258 06da0d4e Sjon Hortensius
	<div class="panel-body">
259 df7f65a3 NewEraCracker
260 ba40ee75 PiBa-NL
		<table class="table">
261
		<thead>
262
			<tr>
263
				<th><?=gettext('Result')?></th>
264
				<th><?=gettext('Record type')?></th>
265
			</tr>
266
		</thead>
267
		<tbody>
268
<?php foreach ((array)$resolved as $hostitem):?>
269
		<tr>
270 a92de66e jim-p
			<td><?=htmlspecialchars($hostitem['data'])?></td><td><?=htmlspecialchars($hostitem['type'])?></td>
271 ba40ee75 PiBa-NL
		</tr>
272
<?php endforeach; ?>
273
		</tbody>
274
		</table>
275 06da0d4e Sjon Hortensius
	</div>
276
</div>
277 fa172bc5 NewEraCracker
<?php endif; ?>
278 06da0d4e Sjon Hortensius
279
<!-- Second table displays the server resolution times -->
280
<div class="panel panel-default">
281 babf5d85 Phil Davis
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Timings')?></h2></div>
282 06da0d4e Sjon Hortensius
	<div class="panel-body">
283
		<table class="table">
284
		<thead>
285
			<tr>
286 babf5d85 Phil Davis
				<th><?=gettext('Name server')?></th>
287
				<th><?=gettext('Query time')?></th>
288 06da0d4e Sjon Hortensius
			</tr>
289
		</thead>
290
291
		<tbody>
292 fa172bc5 NewEraCracker
<?php foreach ((array)$dns_speeds as $qt):?>
293 737ed7d1 jim-p
		<tr>
294 a92de66e jim-p
			<td><?=htmlspecialchars($qt['dns_server'])?></td><td><?=htmlspecialchars($qt['query_time'])?></td>
295 737ed7d1 jim-p
		</tr>
296 fa172bc5 NewEraCracker
<?php endforeach; ?>
297 06da0d4e Sjon Hortensius
		</tbody>
298
		</table>
299
	</div>
300
</div>
301
302
<!-- Third table displays "More information" -->
303
<div class="panel panel-default">
304 3d7a8696 k-paulius
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('More Information')?></h2></div>
305 06da0d4e Sjon Hortensius
	<div class="panel-body">
306
		<ul class="list-group">
307 7d67222e heper
			<li class="list-group-item"><a href="/diag_ping.php?host=<?=htmlspecialchars($host)?>&amp;count=3"><?=gettext("Ping")?></a></li>
308 06da0d4e Sjon Hortensius
			<li class="list-group-item"><a href="/diag_traceroute.php?host=<?=htmlspecialchars($host)?>&amp;ttl=18"><?=gettext("Traceroute")?></a></li>
309
		</ul>
310 57adffdd Jared Dillard
		<h5><?=gettext("NOTE: The following links are to external services, so their reliability cannot be guaranteed.");?></h5>
311 06da0d4e Sjon Hortensius
		<ul class="list-group">
312 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>
313
			<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>
314 06da0d4e Sjon Hortensius
		</ul>
315
	</div>
316
</div>
317
<?php
318 bcd938ef sbeaver
}
319 c10cb196 Stephen Beaver
include("foot.inc");