Project

General

Profile

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