Project

General

Profile

Download (8.43 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-2021 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
require_once("pfsense-utils.inc");
34

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

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

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

    
51
if (isAllowedPage('firewall_aliases_edit.php') && isset($_POST['create_alias']) && (is_hostname($host) || is_ipaddr($host))) {
52
	$resolved = gethostbyname($host);
53
	$type = "hostname";
54
	if ($resolved) {
55
		$resolved = resolve_host_addresses($host);
56
		$isfirst = true;
57
		$addresses = "";
58
		foreach ($resolved as $re) {
59
			if ($re['data'] != "") {
60
				if (!$isfirst) {
61
					$addresses .= " ";
62
				}
63
				$re = rtrim($re['data']);
64
				if (is_ipaddr($re)) {
65
					$sn = is_ipaddrv6($re) ? '/128' : '/32';
66
				} else {
67
					// The name was a CNAME and resolved to another name, rather than an address.
68
					// In this case the alias entry will have a FQDN, so do not put a CIDR after it.
69
					$sn = "";
70
				}
71
				$addresses .= $re . $sn;
72
				$isfirst = false;
73
			}
74
		}
75
		if ($addresses == "") {
76
			$couldnotcreatealias = true;
77
		} else {
78
			$newalias = array();
79
			$newalias['name'] = $aliasname;
80
			$newalias['type'] = "network";
81
			$newalias['address'] = $addresses;
82
			$newalias['descr'] = gettext("Created from Diagnostics-> DNS Lookup");
83
			if ($alias_exists) {
84
				$a_aliases[$id] = $newalias;
85
			} else {
86
				$a_aliases[] = $newalias;
87
			}
88
			write_config(gettext("Created an alias from Diagnostics - DNS Lookup page."));
89
			$createdalias = true;
90
		}
91
	} else {
92
		$couldnotcreatealias = true;
93
	}
94
}
95

    
96
if ($_POST) {
97
	unset($input_errors);
98

    
99
	$reqdfields = explode(" ", "host");
100
	$reqdfieldsn = explode(",", "Host");
101

    
102
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
103

    
104
	if (!is_hostname(rtrim($host, '.')) && !is_ipaddr($host)) {
105
		$input_errors[] = gettext("Host must be a valid hostname or IP address.");
106
	} else {
107
		// Test resolution speed of each DNS server.
108
		$dns_speeds = array();
109
		$dns_servers = get_dns_nameservers(false, true);
110
		foreach ($dns_servers as $dns_server) {
111
			$query_time = exec("/usr/bin/drill " . escapeshellarg($host) . " " . escapeshellarg("@" . trim($dns_server)) . " | /usr/bin/grep Query | /usr/bin/cut -d':' -f2");
112
			if ($query_time == "") {
113
				$query_time = gettext("No response");
114
			}
115
			$new_qt = array();
116
			$new_qt['dns_server'] = $dns_server;
117
			$new_qt['query_time'] = $query_time;
118
			$dns_speeds[] = $new_qt;
119
			unset($new_qt);
120
		}
121
	}
122

    
123
	$type = "unknown";
124
	$resolved = array();
125
	$ipaddr = "";
126
	if (!$input_errors) {
127
		if (is_ipaddr($host)) {
128
			$type = "ip";
129
			$resolvedptr = gethostbyaddr($host);
130
			$ipaddr = $host;
131
			if ($host != $resolvedptr) {
132
				$tmpresolved = array();
133
				$tmpresolved['type'] = "PTR";
134
				$tmpresolved['data'] = $resolvedptr;
135
				$resolved[] = $tmpresolved;
136
			}
137
		} elseif (is_hostname(rtrim($host, '.'))) {
138
			$type = "hostname";
139
			$ipaddr = gethostbyname($host);
140
			$resolved = resolve_host_addresses($host);
141
		}
142
	}
143
}
144

    
145
if ($_POST['host'] && $_POST['dialog_output']) {
146
	$host = (isset($resolvedptr) ? $resolvedptr : $host);
147
	display_host_results ($ipaddr, $host, $dns_speeds);
148
	exit;
149
}
150

    
151
function display_host_results ($address, $hostname, $dns_speeds) {
152
	$map_lengths = function($element) { return strlen($element[0]); };
153

    
154
	echo gettext("IP Address") . ": " . htmlspecialchars($address) . " \n";
155
	echo gettext("Host Name") . ": " . htmlspecialchars($hostname) .  " \n";
156
	echo "\n";
157
	$text_table = array();
158
	$text_table[] = array(gettext("Server"), gettext("Query Time"));
159
	if (is_array($dns_speeds)) {
160
		foreach ($dns_speeds as $qt) {
161
			$text_table[] = array(trim($qt['dns_server']), trim($qt['query_time']));
162
		}
163
	}
164
	$col0_padlength = max(array_map($map_lengths, $text_table)) + 4;
165
	foreach ($text_table as $text_row) {
166
		echo str_pad($text_row[0], $col0_padlength) . $text_row[1] . "\n";
167
	}
168
}
169

    
170
include("head.inc");
171

    
172
/* Display any error messages resulting from user input */
173
if ($input_errors) {
174
	print_input_errors($input_errors);
175
} else if (!$resolved && $type) {
176
	print_info_box(sprintf(gettext('Host "%s" could not be resolved.'), idn_to_utf8($host)), 'warning', false);
177
}
178

    
179
if ($createdalias) {
180
	if ($alias_exists) {
181
		print_info_box(gettext("Alias was updated successfully."), 'success');
182
	} else {
183
		print_info_box(gettext("Alias was created successfully."), 'success');
184
	}
185

    
186
	$alias_exists = true;
187
}
188

    
189
if ($couldnotcreatealias) {
190
	if ($alias_exists) {
191
		print_info_box(sprintf(gettext("Could not update alias for %s"), $host), 'warning', false);
192
	} else {
193
		print_info_box(sprintf(gettext("Could not create alias for %s"), $host), 'warning', false);
194
	}
195
}
196

    
197
$form = new Form(false);
198
$section = new Form_Section('DNS Lookup');
199

    
200
$section->addInput(new Form_Input(
201
	'host',
202
	'*Hostname',
203
	'text',
204
	idn_to_utf8($host),
205
	['placeholder' => 'Hostname to look up.']
206
));
207

    
208
$form->add($section);
209

    
210
$form->addGlobal(new Form_Button(
211
        'Submit',
212
        'Lookup',
213
        null,
214
        'fa-search'
215
))->addClass('btn-primary');
216

    
217
if (!empty($resolved) && isAllowedPage('firewall_aliases_edit.php')) {
218
	$form->addGlobal(new Form_Button(
219
		'create_alias',
220
		($alias_exists) ? gettext("Update Alias") : gettext("Add Alias"),
221
		null,
222
		($alias_exists) ? 'fa-refresh' : 'fa-plus'
223
	))->removeClass('btn-primary')->addClass('btn-success');
224
}
225

    
226
print $form;
227

    
228
if (!$input_errors && $type) {
229
	if ($resolved):
230
?>
231
<div class="panel panel-default">
232
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Results')?></h2></div>
233
	<div class="panel-body">
234

    
235
		<table class="table">
236
		<thead>
237
			<tr>
238
				<th><?=gettext('Result')?></th>
239
				<th><?=gettext('Record type')?></th>
240
			</tr>
241
		</thead>
242
		<tbody>
243
<?php foreach ((array)$resolved as $hostitem):?>
244
		<tr>
245
			<td><?=htmlspecialchars($hostitem['data'])?></td><td><?=htmlspecialchars($hostitem['type'])?></td>
246
		</tr>
247
<?php endforeach; ?>
248
		</tbody>
249
		</table>
250
	</div>
251
</div>
252
<?php endif; ?>
253

    
254
<!-- Second table displays the server resolution times -->
255
<div class="panel panel-default">
256
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Timings')?></h2></div>
257
	<div class="panel-body">
258
		<table class="table">
259
		<thead>
260
			<tr>
261
				<th><?=gettext('Name server')?></th>
262
				<th><?=gettext('Query time')?></th>
263
			</tr>
264
		</thead>
265

    
266
		<tbody>
267
<?php foreach ((array)$dns_speeds as $qt):?>
268
		<tr>
269
			<td><?=htmlspecialchars($qt['dns_server'])?></td><td><?=htmlspecialchars($qt['query_time'])?></td>
270
		</tr>
271
<?php endforeach; ?>
272
		</tbody>
273
		</table>
274
	</div>
275
</div>
276

    
277
<!-- Third table displays "More information" -->
278
<div class="panel panel-default">
279
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('More Information')?></h2></div>
280
	<div class="panel-body">
281
		<ul class="list-group">
282
			<li class="list-group-item"><a href="/diag_ping.php?host=<?=htmlspecialchars($host)?>&amp;count=3"><?=gettext("Ping")?></a></li>
283
			<li class="list-group-item"><a href="/diag_traceroute.php?host=<?=htmlspecialchars($host)?>&amp;ttl=18"><?=gettext("Traceroute")?></a></li>
284
		</ul>
285
	</div>
286
</div>
287
<?php
288
}
289
if (!$input_errors):
290
?>
291
<script type="text/javascript">
292
//<![CDATA[
293
events.push(function() {
294
	var original_host = <?=json_encode($host);?>;
295

    
296
	$('input[name="host"]').on('input', function() {
297
		if ($('#host').val() == original_host) {
298
			disableInput('create_alias', false);
299
		} else {
300
			disableInput('create_alias', true);
301
		}
302
	});
303
});
304
//]]>
305
</script>
306
<?php
307
endif;
308
include("foot.inc");
(13-13/229)