1
|
<?php
|
2
|
/*
|
3
|
diag_dns.php
|
4
|
|
5
|
Copyright (C) 2009 Jim Pingle (jpingle@gmail.com)
|
6
|
Copyright (C) 2013-2015 Electric Sheep Fencing, LP
|
7
|
All rights reserved.
|
8
|
|
9
|
Redistribution and use in source and binary forms, with or without
|
10
|
modification, are permitted provided that the following conditions are met:
|
11
|
|
12
|
1. Redistributions of source code must retain the above copyright notice,
|
13
|
this list of conditions and the following disclaimer.
|
14
|
|
15
|
2. Redistributions in binary form must reproduce the above copyright
|
16
|
notice, this list of conditions and the following disclaimer in the
|
17
|
documentation and/or other materials provided with the distribution.
|
18
|
|
19
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
20
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
21
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
22
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
23
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
24
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
25
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
26
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
27
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
28
|
POSSIBILITY OF SUCH DAMAGE.
|
29
|
*/
|
30
|
|
31
|
/*
|
32
|
pfSense_MODULE: dns
|
33
|
*/
|
34
|
|
35
|
$pgtitle = array(gettext("Diagnostics"),gettext("DNS lookup"));
|
36
|
require("guiconfig.inc");
|
37
|
|
38
|
$host = trim($_REQUEST['host'], " \t\n\r\0\x0B[];\"'");
|
39
|
$host_esc = escapeshellarg($host);
|
40
|
|
41
|
/* If this section of config.xml has not been populated yet we need to set it up
|
42
|
*/
|
43
|
if (!is_array($config['aliases']['alias'])) {
|
44
|
$config['aliases']['alias'] = array();
|
45
|
}
|
46
|
$a_aliases = &$config['aliases']['alias'];
|
47
|
|
48
|
$aliasname = str_replace(array(".","-"), "_", $host);
|
49
|
$alias_exists = false;
|
50
|
$counter=0;
|
51
|
foreach($a_aliases as $a) {
|
52
|
if($a['name'] == $aliasname) {
|
53
|
$alias_exists = true;
|
54
|
$id=$counter;
|
55
|
}
|
56
|
$counter++;
|
57
|
}
|
58
|
|
59
|
if(isset($_POST['create_alias']) && (is_hostname($host) || is_ipaddr($host))) {
|
60
|
if($_POST['override'])
|
61
|
$override = true;
|
62
|
$resolved = gethostbyname($host);
|
63
|
$type = "hostname";
|
64
|
if($resolved) {
|
65
|
$resolved = array();
|
66
|
exec("/usr/bin/drill {$host_esc} A | /usr/bin/grep {$host_esc} | /usr/bin/grep -v ';' | /usr/bin/awk '{ print $5 }'", $resolved);
|
67
|
$isfirst = true;
|
68
|
foreach($resolved as $re) {
|
69
|
if($re != "") {
|
70
|
if(!$isfirst)
|
71
|
$addresses .= " ";
|
72
|
$addresses .= rtrim($re) . "/32";
|
73
|
$isfirst = false;
|
74
|
}
|
75
|
}
|
76
|
$newalias = array();
|
77
|
if($override)
|
78
|
$alias_exists = false;
|
79
|
if($alias_exists == false) {
|
80
|
$newalias['name'] = $aliasname;
|
81
|
$newalias['type'] = "network";
|
82
|
$newalias['address'] = $addresses;
|
83
|
$newalias['descr'] = "Created from Diagnostics-> DNS Lookup";
|
84
|
if($override)
|
85
|
$a_aliases[$id] = $newalias;
|
86
|
else
|
87
|
$a_aliases[] = $newalias;
|
88
|
write_config();
|
89
|
$createdalias = true;
|
90
|
}
|
91
|
}
|
92
|
}
|
93
|
|
94
|
if ($_POST) {
|
95
|
unset($input_errors);
|
96
|
|
97
|
$reqdfields = explode(" ", "host");
|
98
|
$reqdfieldsn = explode(",", "Host");
|
99
|
|
100
|
do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
|
101
|
|
102
|
if (!is_hostname($host) && !is_ipaddr($host)) {
|
103
|
$input_errors[] = gettext("Host must be a valid hostname or IP address.");
|
104
|
} else {
|
105
|
// Test resolution speed of each DNS server.
|
106
|
$dns_speeds = array();
|
107
|
$dns_servers = array();
|
108
|
exec("/usr/bin/grep nameserver /etc/resolv.conf | /usr/bin/cut -f2 -d' '", $dns_servers);
|
109
|
foreach ($dns_servers as $dns_server) {
|
110
|
$query_time = exec("/usr/bin/drill {$host_esc} " . escapeshellarg("@" . trim($dns_server)) . " | /usr/bin/grep Query | /usr/bin/cut -d':' -f2");
|
111
|
if($query_time == "")
|
112
|
$query_time = gettext("No response");
|
113
|
$new_qt = array();
|
114
|
$new_qt['dns_server'] = $dns_server;
|
115
|
$new_qt['query_time'] = $query_time;
|
116
|
$dns_speeds[] = $new_qt;
|
117
|
unset($new_qt);
|
118
|
}
|
119
|
}
|
120
|
|
121
|
$type = "unknown";
|
122
|
$resolved = "";
|
123
|
$ipaddr = "";
|
124
|
$hostname = "";
|
125
|
if (!$input_errors) {
|
126
|
if (is_ipaddr($host)) {
|
127
|
$type = "ip";
|
128
|
$resolved = gethostbyaddr($host);
|
129
|
$ipaddr = $host;
|
130
|
if ($host != $resolved)
|
131
|
$hostname = $resolved;
|
132
|
} elseif (is_hostname($host)) {
|
133
|
$type = "hostname";
|
134
|
$resolved = gethostbyname($host);
|
135
|
if($resolved) {
|
136
|
$resolved = array();
|
137
|
exec("/usr/bin/drill {$host_esc} A | /usr/bin/grep {$host_esc} | /usr/bin/grep -v ';' | /usr/bin/awk '{ print $5 }'", $resolved);
|
138
|
}
|
139
|
$hostname = $host;
|
140
|
if ($host != $resolved)
|
141
|
$ipaddr = $resolved[0];
|
142
|
}
|
143
|
|
144
|
if ($host == $resolved) {
|
145
|
$resolved = gettext("No record found");
|
146
|
}
|
147
|
}
|
148
|
}
|
149
|
|
150
|
if( ($_POST['host']) && ($_POST['dialog_output']) ) {
|
151
|
display_host_results ($host,$resolved,$dns_speeds);
|
152
|
exit;
|
153
|
}
|
154
|
|
155
|
function display_host_results ($address,$hostname,$dns_speeds) {
|
156
|
$map_lengths = function($element) { return strlen($element[0]); };
|
157
|
|
158
|
echo gettext("IP Address") . ": {$address} \n";
|
159
|
echo gettext("Host Name") . ": {$hostname} \n";
|
160
|
echo "\n";
|
161
|
$text_table = array();
|
162
|
$text_table[] = array(gettext("Server"), gettext("Query Time"));
|
163
|
if (is_array($dns_speeds)) {
|
164
|
foreach ($dns_speeds as $qt) {
|
165
|
$text_table[] = array(trim($qt['dns_server']), trim($qt['query_time']));
|
166
|
}
|
167
|
}
|
168
|
$col0_padlength = max(array_map($map_lengths, $text_table)) + 4;
|
169
|
foreach ($text_table as $text_row) {
|
170
|
echo str_pad($text_row[0], $col0_padlength) . $text_row[1] . "\n";
|
171
|
}
|
172
|
}
|
173
|
|
174
|
include("head.inc");
|
175
|
|
176
|
/* Display any error messages resulting from user input */
|
177
|
if ($input_errors)
|
178
|
print_input_errors($input_errors);
|
179
|
else if (!$resolved && $type)
|
180
|
print('<div class="alert alert-warning" role="alert">' . gettext("Host") .' "'. $host .'" '. gettext("could not be resolved") . '</div>');
|
181
|
|
182
|
if ($createdalias)
|
183
|
print('<div class="alert alert-success" role="alert">'.gettext("Alias was created/updated successfully").'</div>');
|
184
|
|
185
|
require('classes/Form.class.php');
|
186
|
|
187
|
$form = new Form(new Form_Button(
|
188
|
'lookup',
|
189
|
'Lookup'
|
190
|
));
|
191
|
$section = new Form_Section('DNS Lookup');
|
192
|
|
193
|
$section->addInput(new Form_Input(
|
194
|
'host',
|
195
|
'Hostname',
|
196
|
'text',
|
197
|
$host,
|
198
|
['placeholder' => 'Hostname to look up.']
|
199
|
));
|
200
|
|
201
|
if (!empty($resolved)) {
|
202
|
$form->addGlobal(new Form_Button(
|
203
|
'create_alias',
|
204
|
'Add alias'
|
205
|
))->removeClass('btn-primary')->addClass('btn-success');
|
206
|
}
|
207
|
|
208
|
$form->add($section);
|
209
|
print $form;
|
210
|
|
211
|
if (!$input_errors && $type) {
|
212
|
if ($resolved):
|
213
|
?>
|
214
|
<div class="panel panel-default">
|
215
|
<div class="panel-heading">Results</div>
|
216
|
<div class="panel-body">
|
217
|
<ul class="list-group">
|
218
|
<?
|
219
|
foreach ((array)$resolved as $hostitem) {
|
220
|
?>
|
221
|
<li class="list-group-item"><?=$hostitem?></li>
|
222
|
<?
|
223
|
if ($hostitem != "") {
|
224
|
$found++;
|
225
|
}
|
226
|
}
|
227
|
?>
|
228
|
</ul>
|
229
|
</div>
|
230
|
</div>
|
231
|
<? endif?>
|
232
|
|
233
|
<!-- Second table displays the server resolution times -->
|
234
|
<div class="panel panel-default">
|
235
|
<div class="panel-heading">Timings</div>
|
236
|
<div class="panel-body">
|
237
|
<table class="table">
|
238
|
<thead>
|
239
|
<tr>
|
240
|
<th>Name server</th>
|
241
|
<th>Query time</th>
|
242
|
</tr>
|
243
|
</thead>
|
244
|
|
245
|
<tbody>
|
246
|
<? foreach ((array)$dns_speeds as $qt):?>
|
247
|
<tr>
|
248
|
<td><?=$qt['dns_server']?></td><td><?=$qt['query_time']?></td>
|
249
|
</tr>
|
250
|
<? endforeach?>
|
251
|
</tbody>
|
252
|
</table>
|
253
|
</div>
|
254
|
</div>
|
255
|
|
256
|
<!-- Third table displays "More information" -->
|
257
|
<div class="panel panel-default">
|
258
|
<div class="panel-heading">More information</div>
|
259
|
<div class="panel-body">
|
260
|
<ul class="list-group">
|
261
|
<li class="list-group-item"><a href="/diag_ping.php?host=<?=htmlspecialchars($host)?>&interface=wan&count=3"><?=gettext("Ping")?></a></li>
|
262
|
<li class="list-group-item"><a href="/diag_traceroute.php?host=<?=htmlspecialchars($host)?>&ttl=18"><?=gettext("Traceroute")?></a></li>
|
263
|
</ul>
|
264
|
<p><?=gettext("NOTE: The following links are to external services, so their reliability cannot be guaranteed.");?></p>
|
265
|
<ul class="list-group">
|
266
|
<li class="list-group-item"><a target="_blank" href="http://private.dnsstuff.com/tools/whois.ch?ip=<?php echo $ipaddr; ?>"><?=gettext("IP WHOIS @ DNS Stuff");?></a></li>
|
267
|
<li class="list-group-item"><a target="_blank" href="http://private.dnsstuff.com/tools/ipall.ch?ip=<?php echo $ipaddr; ?>"><?=gettext("IP Info @ DNS Stuff");?></a></li>
|
268
|
</ul>
|
269
|
</div>
|
270
|
</div>
|
271
|
<?php
|
272
|
}
|
273
|
include("foot.inc");
|