Project

General

Profile

Download (9.27 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-2019 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
init_config_arr(array('aliases', 'alias'));
35
$a_aliases = &$config['aliases']['alias'];
36

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

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

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

    
83
if (isAllowedPage('firewall_aliases_edit.php') && isset($_POST['create_alias']) && (is_hostname($host) || is_ipaddr($host))) {
84
	$resolved = gethostbyname($host);
85
	$type = "hostname";
86
	if ($resolved) {
87
		$resolved = resolve_host_addresses($host);
88
		$isfirst = true;
89
		$addresses = "";
90
		foreach ($resolved as $re) {
91
			if ($re['data'] != "") {
92
				if (!$isfirst) {
93
					$addresses .= " ";
94
				}
95
				$re = rtrim($re['data']);
96
				if (is_ipaddr($re)) {
97
					$sn = is_ipaddrv6($re) ? '/128' : '/32';
98
				} else {
99
					// The name was a CNAME and resolved to another name, rather than an address.
100
					// In this case the alias entry will have a FQDN, so do not put a CIDR after it.
101
					$sn = "";
102
				}
103
				$addresses .= $re . $sn;
104
				$isfirst = false;
105
			}
106
		}
107
		if ($addresses == "") {
108
			$couldnotcreatealias = true;
109
		} else {
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
			$createdalias = true;
122
		}
123
	} else {
124
		$couldnotcreatealias = true;
125
	}
126
}
127

    
128
if ($_POST) {
129
	unset($input_errors);
130

    
131
	$reqdfields = explode(" ", "host");
132
	$reqdfieldsn = explode(",", "Host");
133

    
134
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
135

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

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

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

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

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

    
203
include("head.inc");
204

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

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

    
219
	$alias_exists = true;
220
}
221

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

    
230
$form = new Form(false);
231
$section = new Form_Section('DNS Lookup');
232

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

    
241
$form->add($section);
242

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

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

    
264
print $form;
265

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

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

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

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

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

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