Project

General

Profile

Download (9.8 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 7fcccc8f Phil Davis
if (isAllowedPage('firewall_aliases_edit.php') && 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 f4453851 Phil Davis
		$addresses = "";
94 288a2a0f Phil Davis
		foreach ($resolved as $re) {
95 ba40ee75 PiBa-NL
			if ($re['data'] != "") {
96 947141fd Phil Davis
				if (!$isfirst) {
97 7a87cb97 Scott Ullrich
					$addresses .= " ";
98 947141fd Phil Davis
				}
99 ba40ee75 PiBa-NL
				$re = rtrim($re['data']);
100 607b785f Phil Davis
				if (is_ipaddr($re)) {
101
					$sn = is_ipaddrv6($re) ? '/128' : '/32';
102
				} else {
103
					// The name was a CNAME and resolved to another name, rather than an address.
104
					// In this case the alias entry will have a FQDN, so do not put a CIDR after it.
105
					$sn = "";
106
				}
107 97f42a05 Renato Botelho
				$addresses .= $re . $sn;
108 7a87cb97 Scott Ullrich
				$isfirst = false;
109
			}
110 e55d4b3c Scott Ullrich
		}
111 f4453851 Phil Davis
		if ($addresses == "") {
112
			$couldnotcreatealias = true;
113 a2d92b48 Phil Davis
		} else {
114 f4453851 Phil Davis
			$newalias = array();
115
			$newalias['name'] = $aliasname;
116
			$newalias['type'] = "network";
117
			$newalias['address'] = $addresses;
118
			$newalias['descr'] = gettext("Created from Diagnostics-> DNS Lookup");
119
			if ($alias_exists) {
120
				$a_aliases[$id] = $newalias;
121
			} else {
122
				$a_aliases[] = $newalias;
123
			}
124
			write_config(gettext("Created an alias from Diagnostics - DNS Lookup page."));
125
			$createdalias = true;
126 e55d4b3c Scott Ullrich
		}
127 f4453851 Phil Davis
	} else {
128
		$couldnotcreatealias = true;
129 e55d4b3c Scott Ullrich
	}
130
}
131
132 737ed7d1 jim-p
if ($_POST) {
133
	unset($input_errors);
134
135
	$reqdfields = explode(" ", "host");
136
	$reqdfieldsn = explode(",", "Host");
137
138 1e9b4611 Renato Botelho
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
139 286aa3f2 sbeaver
140 72cd706b bcyrill
	if (!is_hostname($host) && !is_ipaddr($host)) {
141 ee91ae30 jean.feltrin
		$input_errors[] = gettext("Host must be a valid hostname or IP address.");
142 72cd706b bcyrill
	} else {
143
		// Test resolution speed of each DNS server.
144 86ab47ff sullrich
		$dns_speeds = array();
145 ed2a6e89 jim-p
		$dns_servers = array();
146
		exec("/usr/bin/grep nameserver /etc/resolv.conf | /usr/bin/cut -f2 -d' '", $dns_servers);
147 40af551f jim-p
		foreach ($dns_servers as $dns_server) {
148 ed2a6e89 jim-p
			$query_time = exec("/usr/bin/drill {$host_esc} " . escapeshellarg("@" . trim($dns_server)) . " | /usr/bin/grep Query | /usr/bin/cut -d':' -f2");
149 5f601060 Phil Davis
			if ($query_time == "") {
150 ee91ae30 jean.feltrin
				$query_time = gettext("No response");
151 5f601060 Phil Davis
			}
152 86ab47ff sullrich
			$new_qt = array();
153
			$new_qt['dns_server'] = $dns_server;
154 ed2a6e89 jim-p
			$new_qt['query_time'] = $query_time;
155 86ab47ff sullrich
			$dns_speeds[] = $new_qt;
156
			unset($new_qt);
157
		}
158 737ed7d1 jim-p
	}
159
160 da652a46 Vinicius Coque
	$type = "unknown";
161 df7f65a3 NewEraCracker
	$resolved = array();
162 2312b0eb jim-p
	$ipaddr = "";
163 737ed7d1 jim-p
	if (!$input_errors) {
164
		if (is_ipaddr($host)) {
165
			$type = "ip";
166 a9b6c19a Chris Buechler
			$resolvedptr = gethostbyaddr($host);
167 2312b0eb jim-p
			$ipaddr = $host;
168 a9b6c19a Chris Buechler
			if ($host != $resolvedptr) {
169
				$tmpresolved = array();
170
				$tmpresolved['type'] = "PTR";
171
				$tmpresolved['data'] = $resolvedptr;
172
				$resolved[] = $tmpresolved;
173 5f601060 Phil Davis
			}
174 737ed7d1 jim-p
		} elseif (is_hostname($host)) {
175
			$type = "hostname";
176 df7f65a3 NewEraCracker
			$ipaddr = gethostbyname($host);
177
			$resolved = resolve_host_addresses($host);
178 737ed7d1 jim-p
		}
179
	}
180
}
181
182 df7f65a3 NewEraCracker
if ($_POST['host'] && $_POST['dialog_output']) {
183
	$host = (isset($resolvedptr) ? $resolvedptr : $host);
184
	display_host_results ($ipaddr, $host, $dns_speeds);
185 fe74228f N0YB
	exit;
186
}
187
188 699737d9 Phil Davis
function display_host_results ($address, $hostname, $dns_speeds) {
189 79f5aebe Robert Nelson
	$map_lengths = function($element) { return strlen($element[0]); };
190 6ff71328 Robert Nelson
191 a92de66e jim-p
	echo gettext("IP Address") . ": " . htmlspecialchars($address) . " \n";
192
	echo gettext("Host Name") . ": " . htmlspecialchars($hostname) .  " \n";
193 fe74228f N0YB
	echo "\n";
194 6ff71328 Robert Nelson
	$text_table = array();
195
	$text_table[] = array(gettext("Server"), gettext("Query Time"));
196
	if (is_array($dns_speeds)) {
197
		foreach ($dns_speeds as $qt) {
198
			$text_table[] = array(trim($qt['dns_server']), trim($qt['query_time']));
199 fe74228f N0YB
		}
200 6ff71328 Robert Nelson
	}
201 79f5aebe Robert Nelson
	$col0_padlength = max(array_map($map_lengths, $text_table)) + 4;
202 6ff71328 Robert Nelson
	foreach ($text_table as $text_row) {
203 79f5aebe Robert Nelson
		echo str_pad($text_row[0], $col0_padlength) . $text_row[1] . "\n";
204 6ff71328 Robert Nelson
	}
205 fe74228f N0YB
}
206
207 286aa3f2 sbeaver
include("head.inc");
208 e55d4b3c Scott Ullrich
209 286aa3f2 sbeaver
/* Display any error messages resulting from user input */
210 947141fd Phil Davis
if ($input_errors) {
211 286aa3f2 sbeaver
	print_input_errors($input_errors);
212 947141fd Phil Davis
} else if (!$resolved && $type) {
213 02342d8c k-paulius
	print_info_box(sprintf(gettext('Host "%s" could not be resolved.'), $host), 'warning', false);
214 947141fd Phil Davis
}
215 286aa3f2 sbeaver
216 947141fd Phil Davis
if ($createdalias) {
217 a2d92b48 Phil Davis
	if ($alias_exists) {
218
		print_info_box(gettext("Alias was updated successfully."), 'success');
219
	} else {
220
		print_info_box(gettext("Alias was created successfully."), 'success');
221
	}
222 474b2756 Phil Davis
223
	$alias_exists = true;
224 947141fd Phil Davis
}
225 286aa3f2 sbeaver
226 f4453851 Phil Davis
if ($couldnotcreatealias) {
227
	if ($alias_exists) {
228
		print_info_box(sprintf(gettext("Could not update alias for %s"), $host), 'warning', false);
229
	} else {
230
		print_info_box(sprintf(gettext("Could not create alias for %s"), $host), 'warning', false);
231
	}
232
}
233
234 37676f4e jim-p
$form = new Form(false);
235 06da0d4e Sjon Hortensius
$section = new Form_Section('DNS Lookup');
236 286aa3f2 sbeaver
237
$section->addInput(new Form_Input(
238
	'host',
239 3e2028f4 Phil Davis
	'*Hostname',
240 286aa3f2 sbeaver
	'text',
241 06da0d4e Sjon Hortensius
	$host,
242
	['placeholder' => 'Hostname to look up.']
243
));
244 286aa3f2 sbeaver
245 65292972 Chris Buechler
$form->add($section);
246
247
$form->addGlobal(new Form_Button(
248
        'Submit',
249
        'Lookup',
250
        null,
251
        'fa-search'
252
))->addClass('btn-primary');
253
254 7fcccc8f Phil Davis
if (!empty($resolved) && isAllowedPage('firewall_aliases_edit.php')) {
255 a2d92b48 Phil Davis
	if ($alias_exists) {
256
		$button_text = gettext("Update alias");
257
	} else {
258
		$button_text = gettext("Add alias");
259
	}
260 286aa3f2 sbeaver
	$form->addGlobal(new Form_Button(
261 06da0d4e Sjon Hortensius
		'create_alias',
262 a2d92b48 Phil Davis
		$button_text,
263 37676f4e jim-p
		null,
264
		'fa-plus'
265 286aa3f2 sbeaver
	))->removeClass('btn-primary')->addClass('btn-success');
266
}
267
268
print $form;
269
270 bcd938ef sbeaver
if (!$input_errors && $type) {
271 6ee37b41 Sjon Hortensius
	if ($resolved):
272 86ab47ff sullrich
?>
273 06da0d4e Sjon Hortensius
<div class="panel panel-default">
274 babf5d85 Phil Davis
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Results')?></h2></div>
275 06da0d4e Sjon Hortensius
	<div class="panel-body">
276 df7f65a3 NewEraCracker
277 ba40ee75 PiBa-NL
		<table class="table">
278
		<thead>
279
			<tr>
280
				<th><?=gettext('Result')?></th>
281
				<th><?=gettext('Record type')?></th>
282
			</tr>
283
		</thead>
284
		<tbody>
285
<?php foreach ((array)$resolved as $hostitem):?>
286
		<tr>
287 a92de66e jim-p
			<td><?=htmlspecialchars($hostitem['data'])?></td><td><?=htmlspecialchars($hostitem['type'])?></td>
288 ba40ee75 PiBa-NL
		</tr>
289
<?php endforeach; ?>
290
		</tbody>
291
		</table>
292 06da0d4e Sjon Hortensius
	</div>
293
</div>
294 fa172bc5 NewEraCracker
<?php endif; ?>
295 06da0d4e Sjon Hortensius
296
<!-- Second table displays the server resolution times -->
297
<div class="panel panel-default">
298 babf5d85 Phil Davis
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Timings')?></h2></div>
299 06da0d4e Sjon Hortensius
	<div class="panel-body">
300
		<table class="table">
301
		<thead>
302
			<tr>
303 babf5d85 Phil Davis
				<th><?=gettext('Name server')?></th>
304
				<th><?=gettext('Query time')?></th>
305 06da0d4e Sjon Hortensius
			</tr>
306
		</thead>
307
308
		<tbody>
309 fa172bc5 NewEraCracker
<?php foreach ((array)$dns_speeds as $qt):?>
310 737ed7d1 jim-p
		<tr>
311 a92de66e jim-p
			<td><?=htmlspecialchars($qt['dns_server'])?></td><td><?=htmlspecialchars($qt['query_time'])?></td>
312 737ed7d1 jim-p
		</tr>
313 fa172bc5 NewEraCracker
<?php endforeach; ?>
314 06da0d4e Sjon Hortensius
		</tbody>
315
		</table>
316
	</div>
317
</div>
318
319
<!-- Third table displays "More information" -->
320
<div class="panel panel-default">
321 3d7a8696 k-paulius
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('More Information')?></h2></div>
322 06da0d4e Sjon Hortensius
	<div class="panel-body">
323
		<ul class="list-group">
324 7d67222e heper
			<li class="list-group-item"><a href="/diag_ping.php?host=<?=htmlspecialchars($host)?>&amp;count=3"><?=gettext("Ping")?></a></li>
325 06da0d4e Sjon Hortensius
			<li class="list-group-item"><a href="/diag_traceroute.php?host=<?=htmlspecialchars($host)?>&amp;ttl=18"><?=gettext("Traceroute")?></a></li>
326
		</ul>
327 57adffdd Jared Dillard
		<h5><?=gettext("NOTE: The following links are to external services, so their reliability cannot be guaranteed.");?></h5>
328 06da0d4e Sjon Hortensius
		<ul class="list-group">
329 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>
330
			<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>
331 06da0d4e Sjon Hortensius
		</ul>
332
	</div>
333
</div>
334
<?php
335 bcd938ef sbeaver
}
336 45eafdbd Phil Davis
?>
337
<script type="text/javascript">
338
//<![CDATA[
339
events.push(function() {
340
	var original_host = "<?=$host;?>";
341
342
	$('input[name="host"]').on('input', function() {
343
		if ($('#host').val() == original_host) {
344
			disableInput('create_alias', false);
345
		} else {
346
			disableInput('create_alias', true);
347
		}
348
	});
349
});
350
//]]>
351
</script>
352
<?php
353 c10cb196 Stephen Beaver
include("foot.inc");