Project

General

Profile

Download (8.43 KB) Statistics
| Branch: | Tag: | Revision:
1 737ed7d1 jim-p
<?php
2
/*
3 c5d81585 Renato Botelho
 * diag_dns.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 38809d47 Renato Botelho do Couto
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8 8f2f85c3 Luiz Otavio O Souza
 * Copyright (c) 2014-2022 Rubicon Communications, LLC (Netgate)
9 c5d81585 Renato Botelho
 * All rights reserved.
10
 *
11 b12ea3fb Renato Botelho
 * 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 c5d81585 Renato Botelho
 *
15 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
16 c5d81585 Renato Botelho
 *
17 b12ea3fb Renato Botelho
 * 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 fd9ebcd5 Stephen Beaver
 */
23 13d193c2 Scott Ullrich
24 a57d9fa2 jim-p
##|+PRIV
25
##|*IDENT=page-diagnostics-dns
26 5230f468 jim-p
##|*NAME=Diagnostics: DNS Lookup
27 a57d9fa2 jim-p
##|*DESCR=Allow access to the 'Diagnostics: DNS Lookup' page.
28
##|*MATCH=diag_dns.php*
29
##|-PRIV
30
31 699737d9 Phil Davis
$pgtitle = array(gettext("Diagnostics"), gettext("DNS Lookup"));
32 c81ef6e2 Phil Davis
require_once("guiconfig.inc");
33 6e658d8d Viktor G
require_once("pfsense-utils.inc");
34 737ed7d1 jim-p
35 c8a39f1b Viktor G
$host = idn_to_ascii(trim($_REQUEST['host'], " \t\n\r\0\x0B[];\"'"));
36 76c4ff0e Renato Botelho
37 c6c398c6 jim-p
init_config_arr(array('aliases', 'alias'));
38 d5b28fcf Stephen Beaver
$a_aliases = &$config['aliases']['alias'];
39
40 d79ff71a Chris Buechler
$aliasname = substr(str_replace(array(".", "-"), "_", $host), 0, 31);
41 ed2a6e89 jim-p
$alias_exists = false;
42 6c07db48 Phil Davis
$counter = 0;
43 5f601060 Phil Davis
foreach ($a_aliases as $a) {
44
	if ($a['name'] == $aliasname) {
45 ed2a6e89 jim-p
		$alias_exists = true;
46 6c07db48 Phil Davis
		$id = $counter;
47 ed2a6e89 jim-p
	}
48
	$counter++;
49
}
50
51 7fcccc8f Phil Davis
if (isAllowedPage('firewall_aliases_edit.php') && isset($_POST['create_alias']) && (is_hostname($host) || is_ipaddr($host))) {
52 e55d4b3c Scott Ullrich
	$resolved = gethostbyname($host);
53 ed2a6e89 jim-p
	$type = "hostname";
54 5f601060 Phil Davis
	if ($resolved) {
55 ba40ee75 PiBa-NL
		$resolved = resolve_host_addresses($host);
56 e55d4b3c Scott Ullrich
		$isfirst = true;
57 f4453851 Phil Davis
		$addresses = "";
58 288a2a0f Phil Davis
		foreach ($resolved as $re) {
59 ba40ee75 PiBa-NL
			if ($re['data'] != "") {
60 947141fd Phil Davis
				if (!$isfirst) {
61 7a87cb97 Scott Ullrich
					$addresses .= " ";
62 947141fd Phil Davis
				}
63 ba40ee75 PiBa-NL
				$re = rtrim($re['data']);
64 607b785f Phil Davis
				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 97f42a05 Renato Botelho
				$addresses .= $re . $sn;
72 7a87cb97 Scott Ullrich
				$isfirst = false;
73
			}
74 e55d4b3c Scott Ullrich
		}
75 f4453851 Phil Davis
		if ($addresses == "") {
76
			$couldnotcreatealias = true;
77 a2d92b48 Phil Davis
		} else {
78 f4453851 Phil Davis
			$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 e55d4b3c Scott Ullrich
		}
91 f4453851 Phil Davis
	} else {
92
		$couldnotcreatealias = true;
93 e55d4b3c Scott Ullrich
	}
94
}
95
96 737ed7d1 jim-p
if ($_POST) {
97
	unset($input_errors);
98
99
	$reqdfields = explode(" ", "host");
100
	$reqdfieldsn = explode(",", "Host");
101
102 1e9b4611 Renato Botelho
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
103 286aa3f2 sbeaver
104 912562c4 jim-p
	if (!is_hostname(rtrim($host, '.')) && !is_ipaddr($host)) {
105 ee91ae30 jean.feltrin
		$input_errors[] = gettext("Host must be a valid hostname or IP address.");
106 72cd706b bcyrill
	} else {
107
		// Test resolution speed of each DNS server.
108 86ab47ff sullrich
		$dns_speeds = array();
109 f0c51530 jim-p
		$dns_servers = get_dns_nameservers(false, true);
110 40af551f jim-p
		foreach ($dns_servers as $dns_server) {
111 1efc9177 jim-p
			$query_time = exec("/usr/bin/drill " . escapeshellarg($host) . " " . escapeshellarg("@" . trim($dns_server)) . " | /usr/bin/grep Query | /usr/bin/cut -d':' -f2");
112 5f601060 Phil Davis
			if ($query_time == "") {
113 ee91ae30 jean.feltrin
				$query_time = gettext("No response");
114 5f601060 Phil Davis
			}
115 86ab47ff sullrich
			$new_qt = array();
116
			$new_qt['dns_server'] = $dns_server;
117 ed2a6e89 jim-p
			$new_qt['query_time'] = $query_time;
118 86ab47ff sullrich
			$dns_speeds[] = $new_qt;
119
			unset($new_qt);
120
		}
121 737ed7d1 jim-p
	}
122
123 da652a46 Vinicius Coque
	$type = "unknown";
124 df7f65a3 NewEraCracker
	$resolved = array();
125 2312b0eb jim-p
	$ipaddr = "";
126 737ed7d1 jim-p
	if (!$input_errors) {
127
		if (is_ipaddr($host)) {
128
			$type = "ip";
129 a9b6c19a Chris Buechler
			$resolvedptr = gethostbyaddr($host);
130 2312b0eb jim-p
			$ipaddr = $host;
131 a9b6c19a Chris Buechler
			if ($host != $resolvedptr) {
132
				$tmpresolved = array();
133
				$tmpresolved['type'] = "PTR";
134
				$tmpresolved['data'] = $resolvedptr;
135
				$resolved[] = $tmpresolved;
136 5f601060 Phil Davis
			}
137 e56c473d jim-p
		} elseif (is_hostname(rtrim($host, '.'))) {
138 737ed7d1 jim-p
			$type = "hostname";
139 df7f65a3 NewEraCracker
			$ipaddr = gethostbyname($host);
140
			$resolved = resolve_host_addresses($host);
141 737ed7d1 jim-p
		}
142
	}
143
}
144
145 df7f65a3 NewEraCracker
if ($_POST['host'] && $_POST['dialog_output']) {
146
	$host = (isset($resolvedptr) ? $resolvedptr : $host);
147
	display_host_results ($ipaddr, $host, $dns_speeds);
148 fe74228f N0YB
	exit;
149
}
150
151 699737d9 Phil Davis
function display_host_results ($address, $hostname, $dns_speeds) {
152 79f5aebe Robert Nelson
	$map_lengths = function($element) { return strlen($element[0]); };
153 6ff71328 Robert Nelson
154 a92de66e jim-p
	echo gettext("IP Address") . ": " . htmlspecialchars($address) . " \n";
155
	echo gettext("Host Name") . ": " . htmlspecialchars($hostname) .  " \n";
156 fe74228f N0YB
	echo "\n";
157 6ff71328 Robert Nelson
	$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 fe74228f N0YB
		}
163 6ff71328 Robert Nelson
	}
164 79f5aebe Robert Nelson
	$col0_padlength = max(array_map($map_lengths, $text_table)) + 4;
165 6ff71328 Robert Nelson
	foreach ($text_table as $text_row) {
166 79f5aebe Robert Nelson
		echo str_pad($text_row[0], $col0_padlength) . $text_row[1] . "\n";
167 6ff71328 Robert Nelson
	}
168 fe74228f N0YB
}
169
170 286aa3f2 sbeaver
include("head.inc");
171 e55d4b3c Scott Ullrich
172 286aa3f2 sbeaver
/* Display any error messages resulting from user input */
173 947141fd Phil Davis
if ($input_errors) {
174 286aa3f2 sbeaver
	print_input_errors($input_errors);
175 947141fd Phil Davis
} else if (!$resolved && $type) {
176 c8a39f1b Viktor G
	print_info_box(sprintf(gettext('Host "%s" could not be resolved.'), idn_to_utf8($host)), 'warning', false);
177 947141fd Phil Davis
}
178 286aa3f2 sbeaver
179 947141fd Phil Davis
if ($createdalias) {
180 a2d92b48 Phil Davis
	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 474b2756 Phil Davis
186
	$alias_exists = true;
187 947141fd Phil Davis
}
188 286aa3f2 sbeaver
189 f4453851 Phil Davis
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 37676f4e jim-p
$form = new Form(false);
198 06da0d4e Sjon Hortensius
$section = new Form_Section('DNS Lookup');
199 286aa3f2 sbeaver
200
$section->addInput(new Form_Input(
201
	'host',
202 3e2028f4 Phil Davis
	'*Hostname',
203 286aa3f2 sbeaver
	'text',
204 c8a39f1b Viktor G
	idn_to_utf8($host),
205 06da0d4e Sjon Hortensius
	['placeholder' => 'Hostname to look up.']
206
));
207 286aa3f2 sbeaver
208 65292972 Chris Buechler
$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 7fcccc8f Phil Davis
if (!empty($resolved) && isAllowedPage('firewall_aliases_edit.php')) {
218 286aa3f2 sbeaver
	$form->addGlobal(new Form_Button(
219 06da0d4e Sjon Hortensius
		'create_alias',
220 561cc3e5 jim-p
		($alias_exists) ? gettext("Update Alias") : gettext("Add Alias"),
221 37676f4e jim-p
		null,
222 561cc3e5 jim-p
		($alias_exists) ? 'fa-refresh' : 'fa-plus'
223 286aa3f2 sbeaver
	))->removeClass('btn-primary')->addClass('btn-success');
224
}
225
226
print $form;
227
228 bcd938ef sbeaver
if (!$input_errors && $type) {
229 6ee37b41 Sjon Hortensius
	if ($resolved):
230 86ab47ff sullrich
?>
231 06da0d4e Sjon Hortensius
<div class="panel panel-default">
232 babf5d85 Phil Davis
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Results')?></h2></div>
233 06da0d4e Sjon Hortensius
	<div class="panel-body">
234 df7f65a3 NewEraCracker
235 ba40ee75 PiBa-NL
		<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 a92de66e jim-p
			<td><?=htmlspecialchars($hostitem['data'])?></td><td><?=htmlspecialchars($hostitem['type'])?></td>
246 ba40ee75 PiBa-NL
		</tr>
247
<?php endforeach; ?>
248
		</tbody>
249
		</table>
250 06da0d4e Sjon Hortensius
	</div>
251
</div>
252 fa172bc5 NewEraCracker
<?php endif; ?>
253 06da0d4e Sjon Hortensius
254
<!-- Second table displays the server resolution times -->
255
<div class="panel panel-default">
256 babf5d85 Phil Davis
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Timings')?></h2></div>
257 06da0d4e Sjon Hortensius
	<div class="panel-body">
258
		<table class="table">
259
		<thead>
260
			<tr>
261 babf5d85 Phil Davis
				<th><?=gettext('Name server')?></th>
262
				<th><?=gettext('Query time')?></th>
263 06da0d4e Sjon Hortensius
			</tr>
264
		</thead>
265
266
		<tbody>
267 fa172bc5 NewEraCracker
<?php foreach ((array)$dns_speeds as $qt):?>
268 737ed7d1 jim-p
		<tr>
269 a92de66e jim-p
			<td><?=htmlspecialchars($qt['dns_server'])?></td><td><?=htmlspecialchars($qt['query_time'])?></td>
270 737ed7d1 jim-p
		</tr>
271 fa172bc5 NewEraCracker
<?php endforeach; ?>
272 06da0d4e Sjon Hortensius
		</tbody>
273
		</table>
274
	</div>
275
</div>
276
277
<!-- Third table displays "More information" -->
278
<div class="panel panel-default">
279 3d7a8696 k-paulius
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('More Information')?></h2></div>
280 06da0d4e Sjon Hortensius
	<div class="panel-body">
281
		<ul class="list-group">
282 7d67222e heper
			<li class="list-group-item"><a href="/diag_ping.php?host=<?=htmlspecialchars($host)?>&amp;count=3"><?=gettext("Ping")?></a></li>
283 06da0d4e Sjon Hortensius
			<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 bcd938ef sbeaver
}
289 f32e9531 jim-p
if (!$input_errors):
290 45eafdbd Phil Davis
?>
291
<script type="text/javascript">
292
//<![CDATA[
293
events.push(function() {
294 f32e9531 jim-p
	var original_host = <?=json_encode($host);?>;
295 45eafdbd Phil Davis
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 f32e9531 jim-p
endif;
308 c10cb196 Stephen Beaver
include("foot.inc");