Project

General

Profile

Download (8.5 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-2024 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
config_init_path('aliases/alias');
38
$a_aliases = config_get_path('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
			config_set_path('aliases/alias', $a_aliases);
89
			write_config(gettext("Created an alias from Diagnostics - DNS Lookup page."));
90
			$createdalias = true;
91
		}
92
	} else {
93
		$couldnotcreatealias = true;
94
	}
95
}
96

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

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

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

    
105
	if (!is_hostname(rtrim($host, '.')) && !is_ipaddr($host)) {
106
		$input_errors[] = gettext("Host must be a valid hostname or IP address.");
107
	} else {
108
		// Test resolution speed of each DNS server.
109
		$dns_speeds = array();
110
		$dns_servers = get_dns_nameservers(false, true);
111
		foreach ($dns_servers as $dns_server) {
112
			$query_time = exec("/usr/bin/drill " . escapeshellarg($host) . " " . 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.'), idn_to_utf8($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
	idn_to_utf8($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-solid fa-search'
216
))->addClass('btn-primary');
217

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

    
227
print $form;
228

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

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

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

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

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

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