Project

General

Profile

Download (10.8 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	diag_dns.php
4
*/
5
/* ====================================================================
6
 *  Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *
8
 *  Redistribution and use in source and binary forms, with or without modification,
9
 *  are permitted provided that the following conditions are met:
10
 *
11
 *  1. Redistributions of source code must retain the above copyright notice,
12
 *      this list of conditions and the following disclaimer.
13
 *
14
 *  2. Redistributions in binary form must reproduce the above copyright
15
 *      notice, this list of conditions and the following disclaimer in
16
 *      the documentation and/or other materials provided with the
17
 *      distribution.
18
 *
19
 *  3. All advertising materials mentioning features or use of this software
20
 *      must display the following acknowledgment:
21
 *      "This product includes software developed by the pfSense Project
22
 *       for use in the pfSense software distribution. (http://www.pfsense.org/).
23
 *
24
 *  4. The names "pfSense" and "pfSense Project" must not be used to
25
 *       endorse or promote products derived from this software without
26
 *       prior written permission. For written permission, please contact
27
 *       coreteam@pfsense.org.
28
 *
29
 *  5. Products derived from this software may not be called "pfSense"
30
 *      nor may "pfSense" appear in their names without prior written
31
 *      permission of the Electric Sheep Fencing, LLC.
32
 *
33
 *  6. Redistributions of any form whatsoever must retain the following
34
 *      acknowledgment:
35
 *
36
 *  "This product includes software developed by the pfSense Project
37
 *  for use in the pfSense software distribution (http://www.pfsense.org/).
38
 *
39
 *  THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
40
 *  EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
43
 *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45
 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48
 *  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
51
 *
52
 *  ====================================================================
53
 *
54
 */
55

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

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

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

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

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

    
86
function resolve_host_addresses($host) {
87
	$recordtypes = array(DNS_A, DNS_AAAA, DNS_CNAME);
88
	$dnsresult = array();
89
	$resolved = array();
90
	$errreporting = error_reporting();
91
	error_reporting($errreporting & ~E_WARNING);// dns_get_record throws a warning if nothing is resolved..
92
	foreach($recordtypes as $recordtype) {
93
		$tmp = dns_get_record($host, $recordtype);
94
		if (is_array($tmp)) {
95
			$dnsresult = array_merge($dnsresult, $tmp);
96
		}
97
	}
98
	error_reporting($errreporting);// restore original php warning/error settings.
99
	
100
	foreach($dnsresult as $item) {
101
		$newitem = array();
102
		$newitem['type'] = $item['type'];
103
		switch ($item['type']) {
104
			case 'CNAME':
105
				$newitem['data'] = $item['target'];
106
				$resolved[] = $newitem;
107
				break;
108
			case 'A':
109
				$newitem['data'] = $item['ip'];
110
				$resolved[] = $newitem;
111
				break;
112
			case 'AAAA':
113
				$newitem['data'] = $item['ipv6'];
114
				$resolved[] = $newitem;
115
				break;
116
				
117
		}
118
	}
119
	return $resolved;
120
}
121

    
122
if (isset($_POST['create_alias']) && (is_hostname($host) || is_ipaddr($host))) {
123
	$resolved = gethostbyname($host);
124
	$type = "hostname";
125
	if ($resolved) {
126
		$resolved = resolve_host_addresses($host);
127
		$isfirst = true;
128
		foreach ($resolved as $re) {
129
			if ($re['data'] != "") {
130
				if (!$isfirst) {
131
					$addresses .= " ";
132
				}
133
				$re = rtrim($re['data']);
134
				if (is_ipaddr($re)) {
135
					$sn = is_ipaddrv6($re) ? '/128' : '/32';
136
				} else {
137
					// The name was a CNAME and resolved to another name, rather than an address.
138
					// In this case the alias entry will have a FQDN, so do not put a CIDR after it.
139
					$sn = "";
140
				}
141
				$addresses .= $re . $sn;
142
				$isfirst = false;
143
			}
144
		}
145
		$newalias = array();
146
		$newalias['name'] = $aliasname;
147
		$newalias['type'] = "network";
148
		$newalias['address'] = $addresses;
149
		$newalias['descr'] = gettext("Created from Diagnostics-> DNS Lookup");
150
		if ($alias_exists) {
151
			$a_aliases[$id] = $newalias;
152
		} else {
153
			$a_aliases[] = $newalias;
154
		}
155
		write_config();
156
		$createdalias = true;
157
	}
158
}
159

    
160
if ($_POST) {
161
	unset($input_errors);
162

    
163
	$reqdfields = explode(" ", "host");
164
	$reqdfieldsn = explode(",", "Host");
165

    
166
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
167

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

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

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

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

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

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

    
245
include("head.inc");
246

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

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

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

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

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

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

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

    
296
print $form;
297

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

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

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

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