1
|
<?php
|
2
|
/*
|
3
|
* diag_dns.php
|
4
|
*
|
5
|
* part of pfSense (https://www.pfsense.org)
|
6
|
* Copyright (c) 2004-2016 Electric Sheep Fencing, LLC
|
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
|
/* If this section of config.xml has not been populated yet we need to set it up
|
35
|
*/
|
36
|
if (!is_array($config['aliases']['alias'])) {
|
37
|
$config['aliases']['alias'] = array();
|
38
|
}
|
39
|
$a_aliases = &$config['aliases']['alias'];
|
40
|
|
41
|
$aliasname = substr(str_replace(array(".", "-"), "_", $host), 0, 31);
|
42
|
$alias_exists = false;
|
43
|
$counter = 0;
|
44
|
foreach ($a_aliases as $a) {
|
45
|
if ($a['name'] == $aliasname) {
|
46
|
$alias_exists = true;
|
47
|
$id = $counter;
|
48
|
}
|
49
|
$counter++;
|
50
|
}
|
51
|
|
52
|
function resolve_host_addresses($host) {
|
53
|
$recordtypes = array(DNS_A, DNS_AAAA, DNS_CNAME);
|
54
|
$dnsresult = array();
|
55
|
$resolved = array();
|
56
|
$errreporting = error_reporting();
|
57
|
error_reporting($errreporting & ~E_WARNING);// dns_get_record throws a warning if nothing is resolved..
|
58
|
foreach($recordtypes as $recordtype) {
|
59
|
$tmp = dns_get_record($host, $recordtype);
|
60
|
if (is_array($tmp)) {
|
61
|
$dnsresult = array_merge($dnsresult, $tmp);
|
62
|
}
|
63
|
}
|
64
|
error_reporting($errreporting);// restore original php warning/error settings.
|
65
|
|
66
|
foreach($dnsresult as $item) {
|
67
|
$newitem = array();
|
68
|
$newitem['type'] = $item['type'];
|
69
|
switch ($item['type']) {
|
70
|
case 'CNAME':
|
71
|
$newitem['data'] = $item['target'];
|
72
|
$resolved[] = $newitem;
|
73
|
break;
|
74
|
case 'A':
|
75
|
$newitem['data'] = $item['ip'];
|
76
|
$resolved[] = $newitem;
|
77
|
break;
|
78
|
case 'AAAA':
|
79
|
$newitem['data'] = $item['ipv6'];
|
80
|
$resolved[] = $newitem;
|
81
|
break;
|
82
|
|
83
|
}
|
84
|
}
|
85
|
return $resolved;
|
86
|
}
|
87
|
|
88
|
if (isset($_POST['create_alias']) && (is_hostname($host) || is_ipaddr($host))) {
|
89
|
$resolved = gethostbyname($host);
|
90
|
$type = "hostname";
|
91
|
if ($resolved) {
|
92
|
$resolved = resolve_host_addresses($host);
|
93
|
$isfirst = true;
|
94
|
foreach ($resolved as $re) {
|
95
|
if ($re['data'] != "") {
|
96
|
if (!$isfirst) {
|
97
|
$addresses .= " ";
|
98
|
}
|
99
|
$re = rtrim($re['data']);
|
100
|
if (is_ipaddr($re)) {
|
101
|
$sn = is_ipaddrv6($re) ? '/128' : '/32';
|
102
|
} else {
|
103
|
// The name was a CNAME and resolved to another name, rather than an address.
|
104
|
// In this case the alias entry will have a FQDN, so do not put a CIDR after it.
|
105
|
$sn = "";
|
106
|
}
|
107
|
$addresses .= $re . $sn;
|
108
|
$isfirst = false;
|
109
|
}
|
110
|
}
|
111
|
$newalias = array();
|
112
|
$newalias['name'] = $aliasname;
|
113
|
$newalias['type'] = "network";
|
114
|
$newalias['address'] = $addresses;
|
115
|
$newalias['descr'] = gettext("Created from Diagnostics-> DNS Lookup");
|
116
|
if ($alias_exists) {
|
117
|
$a_aliases[$id] = $newalias;
|
118
|
} else {
|
119
|
$a_aliases[] = $newalias;
|
120
|
}
|
121
|
write_config();
|
122
|
$createdalias = true;
|
123
|
}
|
124
|
}
|
125
|
|
126
|
if ($_POST) {
|
127
|
unset($input_errors);
|
128
|
|
129
|
$reqdfields = explode(" ", "host");
|
130
|
$reqdfieldsn = explode(",", "Host");
|
131
|
|
132
|
do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
|
133
|
|
134
|
if (!is_hostname($host) && !is_ipaddr($host)) {
|
135
|
$input_errors[] = gettext("Host must be a valid hostname or IP address.");
|
136
|
} else {
|
137
|
// Test resolution speed of each DNS server.
|
138
|
$dns_speeds = array();
|
139
|
$dns_servers = array();
|
140
|
exec("/usr/bin/grep nameserver /etc/resolv.conf | /usr/bin/cut -f2 -d' '", $dns_servers);
|
141
|
foreach ($dns_servers as $dns_server) {
|
142
|
$query_time = exec("/usr/bin/drill {$host_esc} " . escapeshellarg("@" . trim($dns_server)) . " | /usr/bin/grep Query | /usr/bin/cut -d':' -f2");
|
143
|
if ($query_time == "") {
|
144
|
$query_time = gettext("No response");
|
145
|
}
|
146
|
$new_qt = array();
|
147
|
$new_qt['dns_server'] = $dns_server;
|
148
|
$new_qt['query_time'] = $query_time;
|
149
|
$dns_speeds[] = $new_qt;
|
150
|
unset($new_qt);
|
151
|
}
|
152
|
}
|
153
|
|
154
|
$type = "unknown";
|
155
|
$resolved = "";
|
156
|
$ipaddr = "";
|
157
|
$hostname = "";
|
158
|
if (!$input_errors) {
|
159
|
if (is_ipaddr($host)) {
|
160
|
$type = "ip";
|
161
|
$resolvedptr = gethostbyaddr($host);
|
162
|
$ipaddr = $host;
|
163
|
if ($host != $resolvedptr) {
|
164
|
$tmpresolved = array();
|
165
|
$tmpresolved['type'] = "PTR";
|
166
|
$tmpresolved['data'] = $resolvedptr;
|
167
|
$resolved[] = $tmpresolved;
|
168
|
}
|
169
|
} elseif (is_hostname($host)) {
|
170
|
$type = "hostname";
|
171
|
$resolved = gethostbyname($host);
|
172
|
if ($resolved) {
|
173
|
$resolved = resolve_host_addresses($host);
|
174
|
}
|
175
|
$hostname = $host;
|
176
|
if ($host != $resolved) {
|
177
|
$ipaddr = $resolved[0];
|
178
|
}
|
179
|
}
|
180
|
|
181
|
if ($host == $resolved) {
|
182
|
$resolved = gettext("No record found");
|
183
|
}
|
184
|
}
|
185
|
}
|
186
|
|
187
|
if (($_POST['host']) && ($_POST['dialog_output'])) {
|
188
|
display_host_results ($host, $resolved, $dns_speeds);
|
189
|
exit;
|
190
|
}
|
191
|
|
192
|
function display_host_results ($address, $hostname, $dns_speeds) {
|
193
|
$map_lengths = function($element) { return strlen($element[0]); };
|
194
|
|
195
|
echo gettext("IP Address") . ": {$address} \n";
|
196
|
echo gettext("Host Name") . ": {$hostname} \n";
|
197
|
echo "\n";
|
198
|
$text_table = array();
|
199
|
$text_table[] = array(gettext("Server"), gettext("Query Time"));
|
200
|
if (is_array($dns_speeds)) {
|
201
|
foreach ($dns_speeds as $qt) {
|
202
|
$text_table[] = array(trim($qt['dns_server']), trim($qt['query_time']));
|
203
|
}
|
204
|
}
|
205
|
$col0_padlength = max(array_map($map_lengths, $text_table)) + 4;
|
206
|
foreach ($text_table as $text_row) {
|
207
|
echo str_pad($text_row[0], $col0_padlength) . $text_row[1] . "\n";
|
208
|
}
|
209
|
}
|
210
|
|
211
|
include("head.inc");
|
212
|
|
213
|
/* Display any error messages resulting from user input */
|
214
|
if ($input_errors) {
|
215
|
print_input_errors($input_errors);
|
216
|
} else if (!$resolved && $type) {
|
217
|
print_info_box(sprintf(gettext('Host "%s" could not be resolved.'), $host), 'warning', false);
|
218
|
}
|
219
|
|
220
|
if ($createdalias) {
|
221
|
if ($alias_exists) {
|
222
|
print_info_box(gettext("Alias was updated successfully."), 'success');
|
223
|
} else {
|
224
|
print_info_box(gettext("Alias was created successfully."), 'success');
|
225
|
}
|
226
|
}
|
227
|
|
228
|
$form = new Form(false);
|
229
|
$section = new Form_Section('DNS Lookup');
|
230
|
|
231
|
$section->addInput(new Form_Input(
|
232
|
'host',
|
233
|
'Hostname',
|
234
|
'text',
|
235
|
$host,
|
236
|
['placeholder' => 'Hostname to look up.']
|
237
|
));
|
238
|
|
239
|
$form->add($section);
|
240
|
|
241
|
$form->addGlobal(new Form_Button(
|
242
|
'Submit',
|
243
|
'Lookup',
|
244
|
null,
|
245
|
'fa-search'
|
246
|
))->addClass('btn-primary');
|
247
|
|
248
|
if (!empty($resolved)) {
|
249
|
if ($alias_exists) {
|
250
|
$button_text = gettext("Update alias");
|
251
|
} else {
|
252
|
$button_text = gettext("Add alias");
|
253
|
}
|
254
|
$form->addGlobal(new Form_Button(
|
255
|
'create_alias',
|
256
|
$button_text,
|
257
|
null,
|
258
|
'fa-plus'
|
259
|
))->removeClass('btn-primary')->addClass('btn-success');
|
260
|
}
|
261
|
|
262
|
print $form;
|
263
|
|
264
|
if (!$input_errors && $type) {
|
265
|
if ($resolved):
|
266
|
?>
|
267
|
<div class="panel panel-default">
|
268
|
<div class="panel-heading"><h2 class="panel-title"><?=gettext('Results')?></h2></div>
|
269
|
<div class="panel-body">
|
270
|
|
271
|
<table class="table">
|
272
|
<thead>
|
273
|
<tr>
|
274
|
<th><?=gettext('Result')?></th>
|
275
|
<th><?=gettext('Record type')?></th>
|
276
|
</tr>
|
277
|
</thead>
|
278
|
<tbody>
|
279
|
<?php foreach ((array)$resolved as $hostitem):?>
|
280
|
<tr>
|
281
|
<td><?=$hostitem['data']?></td><td><?=$hostitem['type']?></td>
|
282
|
</tr>
|
283
|
<?php endforeach; ?>
|
284
|
</tbody>
|
285
|
</table>
|
286
|
</div>
|
287
|
</div>
|
288
|
<?php endif; ?>
|
289
|
|
290
|
<!-- Second table displays the server resolution times -->
|
291
|
<div class="panel panel-default">
|
292
|
<div class="panel-heading"><h2 class="panel-title"><?=gettext('Timings')?></h2></div>
|
293
|
<div class="panel-body">
|
294
|
<table class="table">
|
295
|
<thead>
|
296
|
<tr>
|
297
|
<th><?=gettext('Name server')?></th>
|
298
|
<th><?=gettext('Query time')?></th>
|
299
|
</tr>
|
300
|
</thead>
|
301
|
|
302
|
<tbody>
|
303
|
<?php foreach ((array)$dns_speeds as $qt):?>
|
304
|
<tr>
|
305
|
<td><?=$qt['dns_server']?></td><td><?=$qt['query_time']?></td>
|
306
|
</tr>
|
307
|
<?php endforeach; ?>
|
308
|
</tbody>
|
309
|
</table>
|
310
|
</div>
|
311
|
</div>
|
312
|
|
313
|
<!-- Third table displays "More information" -->
|
314
|
<div class="panel panel-default">
|
315
|
<div class="panel-heading"><h2 class="panel-title"><?=gettext('More Information')?></h2></div>
|
316
|
<div class="panel-body">
|
317
|
<ul class="list-group">
|
318
|
<li class="list-group-item"><a href="/diag_ping.php?host=<?=htmlspecialchars($host)?>&count=3"><?=gettext("Ping")?></a></li>
|
319
|
<li class="list-group-item"><a href="/diag_traceroute.php?host=<?=htmlspecialchars($host)?>&ttl=18"><?=gettext("Traceroute")?></a></li>
|
320
|
</ul>
|
321
|
<h5><?=gettext("NOTE: The following links are to external services, so their reliability cannot be guaranteed.");?></h5>
|
322
|
<ul class="list-group">
|
323
|
<li class="list-group-item"><a target="_blank" href="http://private.dnsstuff.com/tools/whois.ch?ip=<?=$ipaddr;?>"><?=gettext("IP WHOIS @ DNS Stuff");?></a></li>
|
324
|
<li class="list-group-item"><a target="_blank" href="http://private.dnsstuff.com/tools/ipall.ch?ip=<?=$ipaddr;?>"><?=gettext("IP Info @ DNS Stuff");?></a></li>
|
325
|
</ul>
|
326
|
</div>
|
327
|
</div>
|
328
|
<?php
|
329
|
}
|
330
|
include("foot.inc");
|