Project

General

Profile

Download (9.36 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-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2019 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14
 *
15
 * http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22
 */
23

    
24
##|+PRIV
25
##|*IDENT=page-diagnostics-dns
26
##|*NAME=Diagnostics: DNS Lookup
27
##|*DESCR=Allow access to the 'Diagnostics: DNS Lookup' page.
28
##|*MATCH=diag_dns.php*
29
##|-PRIV
30

    
31
$pgtitle = array(gettext("Diagnostics"), gettext("DNS Lookup"));
32
require_once("guiconfig.inc");
33

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

    
36
init_config_arr(array('aliases', 'alias'));
37
$a_aliases = &$config['aliases']['alias'];
38

    
39
$aliasname = substr(str_replace(array(".", "-"), "_", $host), 0, 31);
40
$alias_exists = false;
41
$counter = 0;
42
foreach ($a_aliases as $a) {
43
	if ($a['name'] == $aliasname) {
44
		$alias_exists = true;
45
		$id = $counter;
46
	}
47
	$counter++;
48
}
49

    
50
function resolve_host_addresses($host) {
51
	$recordtypes = array(DNS_A, DNS_AAAA, DNS_CNAME);
52
	$dnsresult = array();
53
	$resolved = array();
54
	$errreporting = error_reporting();
55
	error_reporting($errreporting & ~E_WARNING);// dns_get_record throws a warning if nothing is resolved..
56
	foreach ($recordtypes as $recordtype) {
57
		$tmp = dns_get_record($host, $recordtype);
58
		if (is_array($tmp)) {
59
			$dnsresult = array_merge($dnsresult, $tmp);
60
		}
61
	}
62
	error_reporting($errreporting);// restore original php warning/error settings.
63

    
64
	foreach ($dnsresult as $item) {
65
		$newitem = array();
66
		$newitem['type'] = $item['type'];
67
		switch ($item['type']) {
68
			case 'CNAME':
69
				$newitem['data'] = $item['target'];
70
				$resolved[] = $newitem;
71
				break;
72
			case 'A':
73
				$newitem['data'] = $item['ip'];
74
				$resolved[] = $newitem;
75
				break;
76
			case 'AAAA':
77
				$newitem['data'] = $item['ipv6'];
78
				$resolved[] = $newitem;
79
				break;
80
		}
81
	}
82
	return $resolved;
83
}
84

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

    
130
if ($_POST) {
131
	unset($input_errors);
132

    
133
	$reqdfields = explode(" ", "host");
134
	$reqdfieldsn = explode(",", "Host");
135

    
136
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
137

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

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

    
180
if ($_POST['host'] && $_POST['dialog_output']) {
181
	$host = (isset($resolvedptr) ? $resolvedptr : $host);
182
	display_host_results ($ipaddr, $host, $dns_speeds);
183
	exit;
184
}
185

    
186
function display_host_results ($address, $hostname, $dns_speeds) {
187
	$map_lengths = function($element) { return strlen($element[0]); };
188

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

    
205
include("head.inc");
206

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

    
214
if ($createdalias) {
215
	if ($alias_exists) {
216
		print_info_box(gettext("Alias was updated successfully."), 'success');
217
	} else {
218
		print_info_box(gettext("Alias was created successfully."), 'success');
219
	}
220

    
221
	$alias_exists = true;
222
}
223

    
224
if ($couldnotcreatealias) {
225
	if ($alias_exists) {
226
		print_info_box(sprintf(gettext("Could not update alias for %s"), $host), 'warning', false);
227
	} else {
228
		print_info_box(sprintf(gettext("Could not create alias for %s"), $host), 'warning', false);
229
	}
230
}
231

    
232
$form = new Form(false);
233
$section = new Form_Section('DNS Lookup');
234

    
235
$section->addInput(new Form_Input(
236
	'host',
237
	'*Hostname',
238
	'text',
239
	$host,
240
	['placeholder' => 'Hostname to look up.']
241
));
242

    
243
$form->add($section);
244

    
245
$form->addGlobal(new Form_Button(
246
        'Submit',
247
        'Lookup',
248
        null,
249
        'fa-search'
250
))->addClass('btn-primary');
251

    
252
if (!empty($resolved) && isAllowedPage('firewall_aliases_edit.php')) {
253
	if ($alias_exists) {
254
		$button_text = gettext("Update alias");
255
	} else {
256
		$button_text = gettext("Add alias");
257
	}
258
	$form->addGlobal(new Form_Button(
259
		'create_alias',
260
		$button_text,
261
		null,
262
		'fa-plus'
263
	))->removeClass('btn-primary')->addClass('btn-success');
264
}
265

    
266
print $form;
267

    
268
if (!$input_errors && $type) {
269
	if ($resolved):
270
?>
271
<div class="panel panel-default">
272
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Results')?></h2></div>
273
	<div class="panel-body">
274

    
275
		<table class="table">
276
		<thead>
277
			<tr>
278
				<th><?=gettext('Result')?></th>
279
				<th><?=gettext('Record type')?></th>
280
			</tr>
281
		</thead>
282
		<tbody>
283
<?php foreach ((array)$resolved as $hostitem):?>
284
		<tr>
285
			<td><?=htmlspecialchars($hostitem['data'])?></td><td><?=htmlspecialchars($hostitem['type'])?></td>
286
		</tr>
287
<?php endforeach; ?>
288
		</tbody>
289
		</table>
290
	</div>
291
</div>
292
<?php endif; ?>
293

    
294
<!-- Second table displays the server resolution times -->
295
<div class="panel panel-default">
296
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Timings')?></h2></div>
297
	<div class="panel-body">
298
		<table class="table">
299
		<thead>
300
			<tr>
301
				<th><?=gettext('Name server')?></th>
302
				<th><?=gettext('Query time')?></th>
303
			</tr>
304
		</thead>
305

    
306
		<tbody>
307
<?php foreach ((array)$dns_speeds as $qt):?>
308
		<tr>
309
			<td><?=htmlspecialchars($qt['dns_server'])?></td><td><?=htmlspecialchars($qt['query_time'])?></td>
310
		</tr>
311
<?php endforeach; ?>
312
		</tbody>
313
		</table>
314
	</div>
315
</div>
316

    
317
<!-- Third table displays "More information" -->
318
<div class="panel panel-default">
319
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('More Information')?></h2></div>
320
	<div class="panel-body">
321
		<ul class="list-group">
322
			<li class="list-group-item"><a href="/diag_ping.php?host=<?=htmlspecialchars($host)?>&amp;count=3"><?=gettext("Ping")?></a></li>
323
			<li class="list-group-item"><a href="/diag_traceroute.php?host=<?=htmlspecialchars($host)?>&amp;ttl=18"><?=gettext("Traceroute")?></a></li>
324
		</ul>
325
	</div>
326
</div>
327
<?php
328
}
329
if (!$input_errors):
330
?>
331
<script type="text/javascript">
332
//<![CDATA[
333
events.push(function() {
334
	var original_host = <?=json_encode($host);?>;
335

    
336
	$('input[name="host"]').on('input', function() {
337
		if ($('#host').val() == original_host) {
338
			disableInput('create_alias', false);
339
		} else {
340
			disableInput('create_alias', true);
341
		}
342
	});
343
});
344
//]]>
345
</script>
346
<?php
347
endif;
348
include("foot.inc");
(14-14/227)