Project

General

Profile

Download (9 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 (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
		foreach ($resolved as $re) {
94
			if ($re['data'] != "") {
95
				if (!$isfirst) {
96
					$addresses .= " ";
97
				}
98
				$re = rtrim($re['data']);
99
				if (is_ipaddr($re)) {
100
					$sn = is_ipaddrv6($re) ? '/128' : '/32';
101
				} else {
102
					// The name was a CNAME and resolved to another name, rather than an address.
103
					// In this case the alias entry will have a FQDN, so do not put a CIDR after it.
104
					$sn = "";
105
				}
106
				$addresses .= $re . $sn;
107
				$isfirst = false;
108
			}
109
		}
110
		$newalias = array();
111
		$newalias['name'] = $aliasname;
112
		$newalias['type'] = "network";
113
		$newalias['address'] = $addresses;
114
		$newalias['descr'] = gettext("Created from Diagnostics-> DNS Lookup");
115
		if ($alias_exists) {
116
			$a_aliases[$id] = $newalias;
117
		} else {
118
			$a_aliases[] = $newalias;
119
		}
120
		write_config(gettext("Created an alias from Diagnostics - DNS Lookup page."));
121
		write_config();
122
		$createdalias = true;
123
	}
124
}
125

    
126
if ($_POST) {
127
	unset($input_errors);
128

    
129
	$reqdfields = explode(" ", "host");
130
	$reqdfieldsn = explode(",", "Host");
131

    
132
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
133

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

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

    
176
if ($_POST['host'] && $_POST['dialog_output']) {
177
	$host = (isset($resolvedptr) ? $resolvedptr : $host);
178
	display_host_results ($ipaddr, $host, $dns_speeds);
179
	exit;
180
}
181

    
182
function display_host_results ($address, $hostname, $dns_speeds) {
183
	$map_lengths = function($element) { return strlen($element[0]); };
184

    
185
	echo gettext("IP Address") . ": " . htmlspecialchars($address) . " \n";
186
	echo gettext("Host Name") . ": " . htmlspecialchars($hostname) .  " \n";
187
	echo "\n";
188
	$text_table = array();
189
	$text_table[] = array(gettext("Server"), gettext("Query Time"));
190
	if (is_array($dns_speeds)) {
191
		foreach ($dns_speeds as $qt) {
192
			$text_table[] = array(trim($qt['dns_server']), trim($qt['query_time']));
193
		}
194
	}
195
	$col0_padlength = max(array_map($map_lengths, $text_table)) + 4;
196
	foreach ($text_table as $text_row) {
197
		echo str_pad($text_row[0], $col0_padlength) . $text_row[1] . "\n";
198
	}
199
}
200

    
201
include("head.inc");
202

    
203
/* Display any error messages resulting from user input */
204
if ($input_errors) {
205
	print_input_errors($input_errors);
206
} else if (!$resolved && $type) {
207
	print_info_box(sprintf(gettext('Host "%s" could not be resolved.'), $host), 'warning', false);
208
}
209

    
210
if ($createdalias) {
211
	if ($alias_exists) {
212
		print_info_box(gettext("Alias was updated successfully."), 'success');
213
	} else {
214
		print_info_box(gettext("Alias was created successfully."), 'success');
215
	}
216
}
217

    
218
$form = new Form(false);
219
$section = new Form_Section('DNS Lookup');
220

    
221
$section->addInput(new Form_Input(
222
	'host',
223
	'*Hostname',
224
	'text',
225
	$host,
226
	['placeholder' => 'Hostname to look up.']
227
));
228

    
229
$form->add($section);
230

    
231
$form->addGlobal(new Form_Button(
232
        'Submit',
233
        'Lookup',
234
        null,
235
        'fa-search'
236
))->addClass('btn-primary');
237

    
238
if (!empty($resolved)) {
239
	if ($alias_exists) {
240
		$button_text = gettext("Update alias");
241
	} else {
242
		$button_text = gettext("Add alias");
243
	}
244
	$form->addGlobal(new Form_Button(
245
		'create_alias',
246
		$button_text,
247
		null,
248
		'fa-plus'
249
	))->removeClass('btn-primary')->addClass('btn-success');
250
}
251

    
252
print $form;
253

    
254
if (!$input_errors && $type) {
255
	if ($resolved):
256
?>
257
<div class="panel panel-default">
258
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Results')?></h2></div>
259
	<div class="panel-body">
260

    
261
		<table class="table">
262
		<thead>
263
			<tr>
264
				<th><?=gettext('Result')?></th>
265
				<th><?=gettext('Record type')?></th>
266
			</tr>
267
		</thead>
268
		<tbody>
269
<?php foreach ((array)$resolved as $hostitem):?>
270
		<tr>
271
			<td><?=htmlspecialchars($hostitem['data'])?></td><td><?=htmlspecialchars($hostitem['type'])?></td>
272
		</tr>
273
<?php endforeach; ?>
274
		</tbody>
275
		</table>
276
	</div>
277
</div>
278
<?php endif; ?>
279

    
280
<!-- Second table displays the server resolution times -->
281
<div class="panel panel-default">
282
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Timings')?></h2></div>
283
	<div class="panel-body">
284
		<table class="table">
285
		<thead>
286
			<tr>
287
				<th><?=gettext('Name server')?></th>
288
				<th><?=gettext('Query time')?></th>
289
			</tr>
290
		</thead>
291

    
292
		<tbody>
293
<?php foreach ((array)$dns_speeds as $qt):?>
294
		<tr>
295
			<td><?=htmlspecialchars($qt['dns_server'])?></td><td><?=htmlspecialchars($qt['query_time'])?></td>
296
		</tr>
297
<?php endforeach; ?>
298
		</tbody>
299
		</table>
300
	</div>
301
</div>
302

    
303
<!-- Third table displays "More information" -->
304
<div class="panel panel-default">
305
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('More Information')?></h2></div>
306
	<div class="panel-body">
307
		<ul class="list-group">
308
			<li class="list-group-item"><a href="/diag_ping.php?host=<?=htmlspecialchars($host)?>&amp;count=3"><?=gettext("Ping")?></a></li>
309
			<li class="list-group-item"><a href="/diag_traceroute.php?host=<?=htmlspecialchars($host)?>&amp;ttl=18"><?=gettext("Traceroute")?></a></li>
310
		</ul>
311
		<h5><?=gettext("NOTE: The following links are to external services, so their reliability cannot be guaranteed.");?></h5>
312
		<ul class="list-group">
313
			<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>
314
			<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>
315
		</ul>
316
	</div>
317
</div>
318
<?php
319
}
320
include("foot.inc");
(10-10/223)