Project

General

Profile

Download (8.92 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();
121
		$createdalias = true;
122
	}
123
}
124

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

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

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

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

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

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

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

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

    
200
include("head.inc");
201

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

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

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

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

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

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

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

    
251
print $form;
252

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

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

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

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

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