Project

General

Profile

Download (10.8 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
 * 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
 */
53

    
54
##|+PRIV
55
##|*IDENT=page-diagnostics-dns
56
##|*NAME=Diagnostics: DNS Lookup
57
##|*DESCR=Allow access to the 'Diagnostics: DNS Lookup' page.
58
##|*MATCH=diag_dns.php*
59
##|-PRIV
60

    
61
$pgtitle = array(gettext("Diagnostics"), gettext("DNS Lookup"));
62
require_once("guiconfig.inc");
63

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

    
66
/* If this section of config.xml has not been populated yet we need to set it up
67
*/
68
if (!is_array($config['aliases']['alias'])) {
69
	$config['aliases']['alias'] = array();
70
}
71
$a_aliases = &$config['aliases']['alias'];
72

    
73
$aliasname = substr(str_replace(array(".", "-"), "_", $host), 0, 31);
74
$alias_exists = false;
75
$counter = 0;
76
foreach ($a_aliases as $a) {
77
	if ($a['name'] == $aliasname) {
78
		$alias_exists = true;
79
		$id = $counter;
80
	}
81
	$counter++;
82
}
83

    
84
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
	foreach ($recordtypes as $recordtype) {
91
		$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
	
98
	foreach ($dnsresult as $item) {
99
		$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
	}
117
	return $resolved;
118
}
119

    
120
if (isset($_POST['create_alias']) && (is_hostname($host) || is_ipaddr($host))) {
121
	$resolved = gethostbyname($host);
122
	$type = "hostname";
123
	if ($resolved) {
124
		$resolved = resolve_host_addresses($host);
125
		$isfirst = true;
126
		foreach ($resolved as $re) {
127
			if ($re['data'] != "") {
128
				if (!$isfirst) {
129
					$addresses .= " ";
130
				}
131
				$re = rtrim($re['data']);
132
				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
				$addresses .= $re . $sn;
140
				$isfirst = false;
141
			}
142
		}
143
		$newalias = array();
144
		$newalias['name'] = $aliasname;
145
		$newalias['type'] = "network";
146
		$newalias['address'] = $addresses;
147
		$newalias['descr'] = gettext("Created from Diagnostics-> DNS Lookup");
148
		if ($alias_exists) {
149
			$a_aliases[$id] = $newalias;
150
		} else {
151
			$a_aliases[] = $newalias;
152
		}
153
		write_config();
154
		$createdalias = true;
155
	}
156
}
157

    
158
if ($_POST) {
159
	unset($input_errors);
160

    
161
	$reqdfields = explode(" ", "host");
162
	$reqdfieldsn = explode(",", "Host");
163

    
164
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
165

    
166
	if (!is_hostname($host) && !is_ipaddr($host)) {
167
		$input_errors[] = gettext("Host must be a valid hostname or IP address.");
168
	} else {
169
		// Test resolution speed of each DNS server.
170
		$dns_speeds = array();
171
		$dns_servers = array();
172
		exec("/usr/bin/grep nameserver /etc/resolv.conf | /usr/bin/cut -f2 -d' '", $dns_servers);
173
		foreach ($dns_servers as $dns_server) {
174
			$query_time = exec("/usr/bin/drill {$host_esc} " . escapeshellarg("@" . trim($dns_server)) . " | /usr/bin/grep Query | /usr/bin/cut -d':' -f2");
175
			if ($query_time == "") {
176
				$query_time = gettext("No response");
177
			}
178
			$new_qt = array();
179
			$new_qt['dns_server'] = $dns_server;
180
			$new_qt['query_time'] = $query_time;
181
			$dns_speeds[] = $new_qt;
182
			unset($new_qt);
183
		}
184
	}
185

    
186
	$type = "unknown";
187
	$resolved = "";
188
	$ipaddr = "";
189
	if (!$input_errors) {
190
		if (is_ipaddr($host)) {
191
			$type = "ip";
192
			$resolvedptr = gethostbyaddr($host);
193
			$ipaddr = $host;
194
			if ($host != $resolvedptr) {
195
				$tmpresolved = array();
196
				$tmpresolved['type'] = "PTR";
197
				$tmpresolved['data'] = $resolvedptr;
198
				$resolved[] = $tmpresolved;
199
			}
200
		} elseif (is_hostname($host)) {
201
			$type = "hostname";
202
			$resolved = gethostbyname($host);
203
			if ($host != $resolved) {
204
				$resolved = resolve_host_addresses($host);
205
				foreach ($resolved as $item) {
206
					if ($item['type'] == 'A') {
207
						$ipaddr = $item['data'];
208
						break;
209
					}
210
				}
211
			}
212
		}
213

    
214
		if ($host == $resolved) {
215
			$resolved = gettext("No record found");
216
		}
217
	}
218
}
219

    
220
if (($_POST['host']) && ($_POST['dialog_output'])) {
221
	display_host_results ($host, $resolved, $dns_speeds);
222
	exit;
223
}
224

    
225
function display_host_results ($address, $hostname, $dns_speeds) {
226
	$map_lengths = function($element) { return strlen($element[0]); };
227

    
228
	echo gettext("IP Address") . ": " . htmlspecialchars($address) . " \n";
229
	echo gettext("Host Name") . ": " . htmlspecialchars($hostname) .  " \n";
230
	echo "\n";
231
	$text_table = array();
232
	$text_table[] = array(gettext("Server"), gettext("Query Time"));
233
	if (is_array($dns_speeds)) {
234
		foreach ($dns_speeds as $qt) {
235
			$text_table[] = array(trim($qt['dns_server']), trim($qt['query_time']));
236
		}
237
	}
238
	$col0_padlength = max(array_map($map_lengths, $text_table)) + 4;
239
	foreach ($text_table as $text_row) {
240
		echo str_pad($text_row[0], $col0_padlength) . $text_row[1] . "\n";
241
	}
242
}
243

    
244
include("head.inc");
245

    
246
/* Display any error messages resulting from user input */
247
if ($input_errors) {
248
	print_input_errors($input_errors);
249
} else if (!$resolved && $type) {
250
	print_info_box(sprintf(gettext('Host "%s" could not be resolved.'), $host), 'warning', false);
251
}
252

    
253
if ($createdalias) {
254
	if ($alias_exists) {
255
		print_info_box(gettext("Alias was updated successfully."), 'success');
256
	} else {
257
		print_info_box(gettext("Alias was created successfully."), 'success');
258
	}
259
}
260

    
261
$form = new Form(false);
262
$section = new Form_Section('DNS Lookup');
263

    
264
$section->addInput(new Form_Input(
265
	'host',
266
	'Hostname',
267
	'text',
268
	$host,
269
	['placeholder' => 'Hostname to look up.']
270
));
271

    
272
$form->add($section);
273

    
274
$form->addGlobal(new Form_Button(
275
        'Submit',
276
        'Lookup',
277
        null,
278
        'fa-search'
279
))->addClass('btn-primary');
280

    
281
if (!empty($resolved)) {
282
	if ($alias_exists) {
283
		$button_text = gettext("Update alias");
284
	} else {
285
		$button_text = gettext("Add alias");
286
	}
287
	$form->addGlobal(new Form_Button(
288
		'create_alias',
289
		$button_text,
290
		null,
291
		'fa-plus'
292
	))->removeClass('btn-primary')->addClass('btn-success');
293
}
294

    
295
print $form;
296

    
297
if (!$input_errors && $type) {
298
	if ($resolved):
299
?>
300
<div class="panel panel-default">
301
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Results')?></h2></div>
302
	<div class="panel-body">
303
		
304
		<table class="table">
305
		<thead>
306
			<tr>
307
				<th><?=gettext('Result')?></th>
308
				<th><?=gettext('Record type')?></th>
309
			</tr>
310
		</thead>
311
		<tbody>
312
<?php foreach ((array)$resolved as $hostitem):?>
313
		<tr>
314
			<td><?=htmlspecialchars($hostitem['data'])?></td><td><?=htmlspecialchars($hostitem['type'])?></td>
315
		</tr>
316
<?php endforeach; ?>
317
		</tbody>
318
		</table>
319
	</div>
320
</div>
321
<?php endif; ?>
322

    
323
<!-- Second table displays the server resolution times -->
324
<div class="panel panel-default">
325
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Timings')?></h2></div>
326
	<div class="panel-body">
327
		<table class="table">
328
		<thead>
329
			<tr>
330
				<th><?=gettext('Name server')?></th>
331
				<th><?=gettext('Query time')?></th>
332
			</tr>
333
		</thead>
334

    
335
		<tbody>
336
<?php foreach ((array)$dns_speeds as $qt):?>
337
		<tr>
338
			<td><?=htmlspecialchars($qt['dns_server'])?></td><td><?=htmlspecialchars($qt['query_time'])?></td>
339
		</tr>
340
<?php endforeach; ?>
341
		</tbody>
342
		</table>
343
	</div>
344
</div>
345

    
346
<!-- Third table displays "More information" -->
347
<div class="panel panel-default">
348
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('More Information')?></h2></div>
349
	<div class="panel-body">
350
		<ul class="list-group">
351
			<li class="list-group-item"><a href="/diag_ping.php?host=<?=htmlspecialchars($host)?>&amp;count=3"><?=gettext("Ping")?></a></li>
352
			<li class="list-group-item"><a href="/diag_traceroute.php?host=<?=htmlspecialchars($host)?>&amp;ttl=18"><?=gettext("Traceroute")?></a></li>
353
		</ul>
354
		<h5><?=gettext("NOTE: The following links are to external services, so their reliability cannot be guaranteed.");?></h5>
355
		<ul class="list-group">
356
			<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>
357
			<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>
358
		</ul>
359
	</div>
360
</div>
361
<?php
362
}
363
include("foot.inc");
(10-10/227)