Project

General

Profile

Download (9.84 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * diag_dns.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * 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
 *
13
 * http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * 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
 */
21

    
22
##|+PRIV
23
##|*IDENT=page-diagnostics-dns
24
##|*NAME=Diagnostics: DNS Lookup
25
##|*DESCR=Allow access to the 'Diagnostics: DNS Lookup' page.
26
##|*MATCH=diag_dns.php*
27
##|-PRIV
28

    
29
$pgtitle = array(gettext("Diagnostics"), gettext("DNS Lookup"));
30
require_once("guiconfig.inc");
31

    
32
$host = trim($_REQUEST['host'], " \t\n\r\0\x0B[];\"'");
33

    
34
/* If this section of config.xml has not been populated yet we need to set it up
35
*/
36
if (!is_array($config['aliases']['alias'])) {
37
	$config['aliases']['alias'] = array();
38
}
39
$a_aliases = &$config['aliases']['alias'];
40

    
41
$aliasname = substr(str_replace(array(".", "-"), "_", $host), 0, 31);
42
$alias_exists = false;
43
$counter = 0;
44
foreach ($a_aliases as $a) {
45
	if ($a['name'] == $aliasname) {
46
		$alias_exists = true;
47
		$id = $counter;
48
	}
49
	$counter++;
50
}
51

    
52
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
	foreach ($recordtypes as $recordtype) {
59
		$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

    
66
	foreach ($dnsresult as $item) {
67
		$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
if (isAllowedPage('firewall_aliases_edit.php') && isset($_POST['create_alias']) && (is_hostname($host) || is_ipaddr($host))) {
88
	$resolved = gethostbyname($host);
89
	$type = "hostname";
90
	if ($resolved) {
91
		$resolved = resolve_host_addresses($host);
92
		$isfirst = true;
93
		$addresses = "";
94
		foreach ($resolved as $re) {
95
			if ($re['data'] != "") {
96
				if (!$isfirst) {
97
					$addresses .= " ";
98
				}
99
				$re = rtrim($re['data']);
100
				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
				$addresses .= $re . $sn;
108
				$isfirst = false;
109
			}
110
		}
111
		if ($addresses == "") {
112
			$couldnotcreatealias = true;
113
		} else {
114
			$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
		}
127
	} else {
128
		$couldnotcreatealias = true;
129
	}
130
}
131

    
132
if ($_POST) {
133
	unset($input_errors);
134

    
135
	$reqdfields = explode(" ", "host");
136
	$reqdfieldsn = explode(",", "Host");
137

    
138
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
139

    
140
	if (!is_hostname($host) && !is_ipaddr($host)) {
141
		$input_errors[] = gettext("Host must be a valid hostname or IP address.");
142
	} else {
143
		// Test resolution speed of each DNS server.
144
		$dns_speeds = array();
145
		$dns_servers = array();
146
		exec("/usr/bin/grep nameserver /etc/resolv.conf | /usr/bin/cut -f2 -d' '", $dns_servers);
147
		foreach ($dns_servers as $dns_server) {
148
			$query_time = exec("/usr/bin/drill {$host_esc} " . escapeshellarg("@" . trim($dns_server)) . " | /usr/bin/grep Query | /usr/bin/cut -d':' -f2");
149
			if ($query_time == "") {
150
				$query_time = gettext("No response");
151
			}
152
			$new_qt = array();
153
			$new_qt['dns_server'] = $dns_server;
154
			$new_qt['query_time'] = $query_time;
155
			$dns_speeds[] = $new_qt;
156
			unset($new_qt);
157
		}
158
	}
159

    
160
	$type = "unknown";
161
	$resolved = array();
162
	$ipaddr = "";
163
	if (!$input_errors) {
164
		if (is_ipaddr($host)) {
165
			$type = "ip";
166
			$resolvedptr = gethostbyaddr($host);
167
			$ipaddr = $host;
168
			if ($host != $resolvedptr) {
169
				$tmpresolved = array();
170
				$tmpresolved['type'] = "PTR";
171
				$tmpresolved['data'] = $resolvedptr;
172
				$resolved[] = $tmpresolved;
173
			}
174
		} elseif (is_hostname($host)) {
175
			$type = "hostname";
176
			$ipaddr = gethostbyname($host);
177
			$resolved = resolve_host_addresses($host);
178
		}
179
	}
180
}
181

    
182
if ($_POST['host'] && $_POST['dialog_output']) {
183
	$host = (isset($resolvedptr) ? $resolvedptr : $host);
184
	display_host_results ($ipaddr, $host, $dns_speeds);
185
	exit;
186
}
187

    
188
function display_host_results ($address, $hostname, $dns_speeds) {
189
	$map_lengths = function($element) { return strlen($element[0]); };
190

    
191
	echo gettext("IP Address") . ": " . htmlspecialchars($address) . " \n";
192
	echo gettext("Host Name") . ": " . htmlspecialchars($hostname) .  " \n";
193
	echo "\n";
194
	$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
		}
200
	}
201
	$col0_padlength = max(array_map($map_lengths, $text_table)) + 4;
202
	foreach ($text_table as $text_row) {
203
		echo str_pad($text_row[0], $col0_padlength) . $text_row[1] . "\n";
204
	}
205
}
206

    
207
include("head.inc");
208

    
209
/* Display any error messages resulting from user input */
210
if ($input_errors) {
211
	print_input_errors($input_errors);
212
} else if (!$resolved && $type) {
213
	print_info_box(sprintf(gettext('Host "%s" could not be resolved.'), $host), 'warning', false);
214
}
215

    
216
if ($createdalias) {
217
	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

    
223
	$alias_exists = true;
224
}
225

    
226
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
$form = new Form(false);
235
$section = new Form_Section('DNS Lookup');
236

    
237
$section->addInput(new Form_Input(
238
	'host',
239
	'*Hostname',
240
	'text',
241
	$host,
242
	['placeholder' => 'Hostname to look up.']
243
));
244

    
245
$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
if (!empty($resolved) && isAllowedPage('firewall_aliases_edit.php')) {
255
	if ($alias_exists) {
256
		$button_text = gettext("Update alias");
257
	} else {
258
		$button_text = gettext("Add alias");
259
	}
260
	$form->addGlobal(new Form_Button(
261
		'create_alias',
262
		$button_text,
263
		null,
264
		'fa-plus'
265
	))->removeClass('btn-primary')->addClass('btn-success');
266
}
267

    
268
print $form;
269

    
270
if (!$input_errors && $type) {
271
	if ($resolved):
272
?>
273
<div class="panel panel-default">
274
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Results')?></h2></div>
275
	<div class="panel-body">
276

    
277
		<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
			<td><?=htmlspecialchars($hostitem['data'])?></td><td><?=htmlspecialchars($hostitem['type'])?></td>
288
		</tr>
289
<?php endforeach; ?>
290
		</tbody>
291
		</table>
292
	</div>
293
</div>
294
<?php endif; ?>
295

    
296
<!-- Second table displays the server resolution times -->
297
<div class="panel panel-default">
298
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Timings')?></h2></div>
299
	<div class="panel-body">
300
		<table class="table">
301
		<thead>
302
			<tr>
303
				<th><?=gettext('Name server')?></th>
304
				<th><?=gettext('Query time')?></th>
305
			</tr>
306
		</thead>
307

    
308
		<tbody>
309
<?php foreach ((array)$dns_speeds as $qt):?>
310
		<tr>
311
			<td><?=htmlspecialchars($qt['dns_server'])?></td><td><?=htmlspecialchars($qt['query_time'])?></td>
312
		</tr>
313
<?php endforeach; ?>
314
		</tbody>
315
		</table>
316
	</div>
317
</div>
318

    
319
<!-- Third table displays "More information" -->
320
<div class="panel panel-default">
321
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('More Information')?></h2></div>
322
	<div class="panel-body">
323
		<ul class="list-group">
324
			<li class="list-group-item"><a href="/diag_ping.php?host=<?=htmlspecialchars($host)?>&amp;count=3"><?=gettext("Ping")?></a></li>
325
			<li class="list-group-item"><a href="/diag_traceroute.php?host=<?=htmlspecialchars($host)?>&amp;ttl=18"><?=gettext("Traceroute")?></a></li>
326
		</ul>
327
		<h5><?=gettext("NOTE: The following links are to external services, so their reliability cannot be guaranteed.");?></h5>
328
		<ul class="list-group">
329
			<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
		</ul>
332
	</div>
333
</div>
334
<?php
335
}
336
if (!$input_errors):
337
?>
338
<script type="text/javascript">
339
//<![CDATA[
340
events.push(function() {
341
	var original_host = <?=json_encode($host);?>;
342

    
343
	$('input[name="host"]').on('input', function() {
344
		if ($('#host').val() == original_host) {
345
			disableInput('create_alias', false);
346
		} else {
347
			disableInput('create_alias', true);
348
		}
349
	});
350
});
351
//]]>
352
</script>
353
<?php
354
endif;
355
include("foot.inc");
(14-14/230)