Project

General

Profile

Download (9.91 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-2018 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'])) {
37
	$config['aliases'] = array();
38
}
39

    
40
if (!is_array($config['aliases']['alias'])) {
41
	$config['aliases']['alias'] = array();
42
}
43

    
44
$a_aliases = &$config['aliases']['alias'];
45

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

    
57
function resolve_host_addresses($host) {
58
	$recordtypes = array(DNS_A, DNS_AAAA, DNS_CNAME);
59
	$dnsresult = array();
60
	$resolved = array();
61
	$errreporting = error_reporting();
62
	error_reporting($errreporting & ~E_WARNING);// dns_get_record throws a warning if nothing is resolved..
63
	foreach ($recordtypes as $recordtype) {
64
		$tmp = dns_get_record($host, $recordtype);
65
		if (is_array($tmp)) {
66
			$dnsresult = array_merge($dnsresult, $tmp);
67
		}
68
	}
69
	error_reporting($errreporting);// restore original php warning/error settings.
70

    
71
	foreach ($dnsresult as $item) {
72
		$newitem = array();
73
		$newitem['type'] = $item['type'];
74
		switch ($item['type']) {
75
			case 'CNAME':
76
				$newitem['data'] = $item['target'];
77
				$resolved[] = $newitem;
78
				break;
79
			case 'A':
80
				$newitem['data'] = $item['ip'];
81
				$resolved[] = $newitem;
82
				break;
83
			case 'AAAA':
84
				$newitem['data'] = $item['ipv6'];
85
				$resolved[] = $newitem;
86
				break;
87
		}
88
	}
89
	return $resolved;
90
}
91

    
92
if (isAllowedPage('firewall_aliases_edit.php') && isset($_POST['create_alias']) && (is_hostname($host) || is_ipaddr($host))) {
93
	$resolved = gethostbyname($host);
94
	$type = "hostname";
95
	if ($resolved) {
96
		$resolved = resolve_host_addresses($host);
97
		$isfirst = true;
98
		$addresses = "";
99
		foreach ($resolved as $re) {
100
			if ($re['data'] != "") {
101
				if (!$isfirst) {
102
					$addresses .= " ";
103
				}
104
				$re = rtrim($re['data']);
105
				if (is_ipaddr($re)) {
106
					$sn = is_ipaddrv6($re) ? '/128' : '/32';
107
				} else {
108
					// The name was a CNAME and resolved to another name, rather than an address.
109
					// In this case the alias entry will have a FQDN, so do not put a CIDR after it.
110
					$sn = "";
111
				}
112
				$addresses .= $re . $sn;
113
				$isfirst = false;
114
			}
115
		}
116
		if ($addresses == "") {
117
			$couldnotcreatealias = true;
118
		} else {
119
			$newalias = array();
120
			$newalias['name'] = $aliasname;
121
			$newalias['type'] = "network";
122
			$newalias['address'] = $addresses;
123
			$newalias['descr'] = gettext("Created from Diagnostics-> DNS Lookup");
124
			if ($alias_exists) {
125
				$a_aliases[$id] = $newalias;
126
			} else {
127
				$a_aliases[] = $newalias;
128
			}
129
			write_config(gettext("Created an alias from Diagnostics - DNS Lookup page."));
130
			$createdalias = true;
131
		}
132
	} else {
133
		$couldnotcreatealias = true;
134
	}
135
}
136

    
137
if ($_POST) {
138
	unset($input_errors);
139

    
140
	$reqdfields = explode(" ", "host");
141
	$reqdfieldsn = explode(",", "Host");
142

    
143
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
144

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

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

    
187
if ($_POST['host'] && $_POST['dialog_output']) {
188
	$host = (isset($resolvedptr) ? $resolvedptr : $host);
189
	display_host_results ($ipaddr, $host, $dns_speeds);
190
	exit;
191
}
192

    
193
function display_host_results ($address, $hostname, $dns_speeds) {
194
	$map_lengths = function($element) { return strlen($element[0]); };
195

    
196
	echo gettext("IP Address") . ": " . htmlspecialchars($address) . " \n";
197
	echo gettext("Host Name") . ": " . htmlspecialchars($hostname) .  " \n";
198
	echo "\n";
199
	$text_table = array();
200
	$text_table[] = array(gettext("Server"), gettext("Query Time"));
201
	if (is_array($dns_speeds)) {
202
		foreach ($dns_speeds as $qt) {
203
			$text_table[] = array(trim($qt['dns_server']), trim($qt['query_time']));
204
		}
205
	}
206
	$col0_padlength = max(array_map($map_lengths, $text_table)) + 4;
207
	foreach ($text_table as $text_row) {
208
		echo str_pad($text_row[0], $col0_padlength) . $text_row[1] . "\n";
209
	}
210
}
211

    
212
include("head.inc");
213

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

    
221
if ($createdalias) {
222
	if ($alias_exists) {
223
		print_info_box(gettext("Alias was updated successfully."), 'success');
224
	} else {
225
		print_info_box(gettext("Alias was created successfully."), 'success');
226
	}
227

    
228
	$alias_exists = true;
229
}
230

    
231
if ($couldnotcreatealias) {
232
	if ($alias_exists) {
233
		print_info_box(sprintf(gettext("Could not update alias for %s"), $host), 'warning', false);
234
	} else {
235
		print_info_box(sprintf(gettext("Could not create alias for %s"), $host), 'warning', false);
236
	}
237
}
238

    
239
$form = new Form(false);
240
$section = new Form_Section('DNS Lookup');
241

    
242
$section->addInput(new Form_Input(
243
	'host',
244
	'*Hostname',
245
	'text',
246
	$host,
247
	['placeholder' => 'Hostname to look up.']
248
));
249

    
250
$form->add($section);
251

    
252
$form->addGlobal(new Form_Button(
253
        'Submit',
254
        'Lookup',
255
        null,
256
        'fa-search'
257
))->addClass('btn-primary');
258

    
259
if (!empty($resolved) && isAllowedPage('firewall_aliases_edit.php')) {
260
	if ($alias_exists) {
261
		$button_text = gettext("Update alias");
262
	} else {
263
		$button_text = gettext("Add alias");
264
	}
265
	$form->addGlobal(new Form_Button(
266
		'create_alias',
267
		$button_text,
268
		null,
269
		'fa-plus'
270
	))->removeClass('btn-primary')->addClass('btn-success');
271
}
272

    
273
print $form;
274

    
275
if (!$input_errors && $type) {
276
	if ($resolved):
277
?>
278
<div class="panel panel-default">
279
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Results')?></h2></div>
280
	<div class="panel-body">
281

    
282
		<table class="table">
283
		<thead>
284
			<tr>
285
				<th><?=gettext('Result')?></th>
286
				<th><?=gettext('Record type')?></th>
287
			</tr>
288
		</thead>
289
		<tbody>
290
<?php foreach ((array)$resolved as $hostitem):?>
291
		<tr>
292
			<td><?=htmlspecialchars($hostitem['data'])?></td><td><?=htmlspecialchars($hostitem['type'])?></td>
293
		</tr>
294
<?php endforeach; ?>
295
		</tbody>
296
		</table>
297
	</div>
298
</div>
299
<?php endif; ?>
300

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

    
313
		<tbody>
314
<?php foreach ((array)$dns_speeds as $qt):?>
315
		<tr>
316
			<td><?=htmlspecialchars($qt['dns_server'])?></td><td><?=htmlspecialchars($qt['query_time'])?></td>
317
		</tr>
318
<?php endforeach; ?>
319
		</tbody>
320
		</table>
321
	</div>
322
</div>
323

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

    
348
	$('input[name="host"]').on('input', function() {
349
		if ($('#host').val() == original_host) {
350
			disableInput('create_alias', false);
351
		} else {
352
			disableInput('create_alias', true);
353
		}
354
	});
355
});
356
//]]>
357
</script>
358
<?php
359
endif;
360
include("foot.inc");
(13-13/234)