Project

General

Profile

Download (8.47 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-2020 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 = 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 = array();
110
		exec("/usr/bin/grep nameserver /etc/resolv.conf | /usr/bin/cut -f2 -d' '", $dns_servers);
111
		foreach ($dns_servers as $dns_server) {
112
			$query_time = exec("/usr/bin/drill {$host_esc} " . escapeshellarg("@" . trim($dns_server)) . " | /usr/bin/grep Query | /usr/bin/cut -d':' -f2");
113
			if ($query_time == "") {
114
				$query_time = gettext("No response");
115
			}
116
			$new_qt = array();
117
			$new_qt['dns_server'] = $dns_server;
118
			$new_qt['query_time'] = $query_time;
119
			$dns_speeds[] = $new_qt;
120
			unset($new_qt);
121
		}
122
	}
123

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

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

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

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

    
171
include("head.inc");
172

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

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

    
187
	$alias_exists = true;
188
}
189

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

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

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

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

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

    
218
if (!empty($resolved) && isAllowedPage('firewall_aliases_edit.php')) {
219
	if ($alias_exists) {
220
		$button_text = gettext("Update alias");
221
	} else {
222
		$button_text = gettext("Add alias");
223
	}
224
	$form->addGlobal(new Form_Button(
225
		'create_alias',
226
		$button_text,
227
		null,
228
		'fa-plus'
229
	))->removeClass('btn-primary')->addClass('btn-success');
230
}
231

    
232
print $form;
233

    
234
if (!$input_errors && $type) {
235
	if ($resolved):
236
?>
237
<div class="panel panel-default">
238
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Results')?></h2></div>
239
	<div class="panel-body">
240

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

    
260
<!-- Second table displays the server resolution times -->
261
<div class="panel panel-default">
262
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Timings')?></h2></div>
263
	<div class="panel-body">
264
		<table class="table">
265
		<thead>
266
			<tr>
267
				<th><?=gettext('Name server')?></th>
268
				<th><?=gettext('Query time')?></th>
269
			</tr>
270
		</thead>
271

    
272
		<tbody>
273
<?php foreach ((array)$dns_speeds as $qt):?>
274
		<tr>
275
			<td><?=htmlspecialchars($qt['dns_server'])?></td><td><?=htmlspecialchars($qt['query_time'])?></td>
276
		</tr>
277
<?php endforeach; ?>
278
		</tbody>
279
		</table>
280
	</div>
281
</div>
282

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

    
302
	$('input[name="host"]').on('input', function() {
303
		if ($('#host').val() == original_host) {
304
			disableInput('create_alias', false);
305
		} else {
306
			disableInput('create_alias', true);
307
		}
308
	});
309
});
310
//]]>
311
</script>
312
<?php
313
endif;
314
include("foot.inc");
(14-14/227)