1
|
<?php
|
2
|
/*
|
3
|
diag_dns.php
|
4
|
|
5
|
Copyright (C) 2009 Jim Pingle (jpingle@gmail.com)
|
6
|
All rights reserved.
|
7
|
|
8
|
Redistribution and use in source and binary forms, with or without
|
9
|
modification, are permitted provided that the following conditions are met:
|
10
|
|
11
|
1. Redistributions of source code must retain the above copyright notice,
|
12
|
this list of conditions and the following disclaimer.
|
13
|
|
14
|
2. Redistributions in binary form must reproduce the above copyright
|
15
|
notice, this list of conditions and the following disclaimer in the
|
16
|
documentation and/or other materials provided with the distribution.
|
17
|
|
18
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
19
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
20
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
21
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
22
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
23
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
24
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
25
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
26
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27
|
POSSIBILITY OF SUCH DAMAGE.
|
28
|
*/
|
29
|
|
30
|
/*
|
31
|
pfSense_MODULE: dns
|
32
|
*/
|
33
|
|
34
|
$pgtitle = array(gettext("Diagnostics"),gettext("DNS Lookup"));
|
35
|
require("guiconfig.inc");
|
36
|
|
37
|
/* Cheap hack to support both $_GET and $_POST */
|
38
|
if ($_GET['host'])
|
39
|
$_POST = $_GET;
|
40
|
|
41
|
if($_GET['createalias'] == "true") {
|
42
|
$host = trim($_POST['host']);
|
43
|
if($_GET['override'])
|
44
|
$override = true;
|
45
|
$a_aliases = &$config['aliases']['alias'];
|
46
|
$type = "hostname";
|
47
|
$resolved = gethostbyname($host);
|
48
|
if($resolved) {
|
49
|
$host = trim($_POST['host']);
|
50
|
$drill=`/usr/bin/drill "$host" A | grep "$host" | grep -v ";" | awk '{ print $5 }'`;
|
51
|
$resolved = explode("\n", $drill);
|
52
|
$isfirst = true;
|
53
|
foreach($resolved as $re) {
|
54
|
if($re <> "") {
|
55
|
if(!$isfirst)
|
56
|
$addresses .= " ";
|
57
|
$addresses .= $re . "/32";
|
58
|
$isfirst = false;
|
59
|
}
|
60
|
}
|
61
|
$newalias = array();
|
62
|
$aliasname = str_replace(array(".","-"), "_", $host);
|
63
|
$alias_exists = false;
|
64
|
$counter=0;
|
65
|
foreach($a_aliases as $a) {
|
66
|
if($a['name'] == $aliasname) {
|
67
|
$alias_exists = true;
|
68
|
$id=$counter;
|
69
|
}
|
70
|
$counter++;
|
71
|
}
|
72
|
if($override)
|
73
|
$alias_exists = false;
|
74
|
if($alias_exists == false) {
|
75
|
$newalias['name'] = $aliasname;
|
76
|
$newalias['type'] = "network";
|
77
|
$newalias['address'] = $addresses;
|
78
|
$newalias['descr'] = "Created from Diagnostics-> DNS Lookup";
|
79
|
if($override)
|
80
|
$a_aliases[$id] = $newalias;
|
81
|
else
|
82
|
$a_aliases[] = $newalias;
|
83
|
write_config();
|
84
|
$createdalias = true;
|
85
|
}
|
86
|
}
|
87
|
}
|
88
|
|
89
|
if ($_POST) {
|
90
|
unset($input_errors);
|
91
|
|
92
|
$reqdfields = explode(" ", "host");
|
93
|
$reqdfieldsn = explode(",", "Host");
|
94
|
|
95
|
do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
|
96
|
$host = trim($_POST['host'], " \t\n\r\0\x0B[]");
|
97
|
$host_esc = escapeshellarg($host);
|
98
|
|
99
|
if (!is_hostname($host) && !is_ipaddr($host)) {
|
100
|
$input_errors[] = gettext("Host must be a valid hostname or IP address.");
|
101
|
} else {
|
102
|
// Test resolution speed of each DNS server.
|
103
|
$dns_speeds = array();
|
104
|
$resolvconf_servers = `grep nameserver /etc/resolv.conf | cut -f2 -d' '`;
|
105
|
$dns_servers = explode("\n", trim($resolvconf_servers));
|
106
|
foreach ($dns_servers as $dns_server) {
|
107
|
$query_time = `/usr/bin/drill {$host_esc} @{$dns_server} | grep Query | cut -d':' -f2`;
|
108
|
if($query_time == "")
|
109
|
$query_time = gettext("No response");
|
110
|
$new_qt = array();
|
111
|
$new_qt['dns_server'] = $dns_server;
|
112
|
$new_qt['query_time'] = $query_time;
|
113
|
$dns_speeds[] = $new_qt;
|
114
|
unset($new_qt);
|
115
|
}
|
116
|
}
|
117
|
|
118
|
$type = "unknown";
|
119
|
$resolved = "";
|
120
|
$ipaddr = "";
|
121
|
$hostname = "";
|
122
|
if (!$input_errors) {
|
123
|
if (is_ipaddr($host)) {
|
124
|
$type = "ip";
|
125
|
$resolved = gethostbyaddr($host);
|
126
|
$ipaddr = $host;
|
127
|
if ($host != $resolved)
|
128
|
$hostname = $resolved;
|
129
|
} elseif (is_hostname($host)) {
|
130
|
$type = "hostname";
|
131
|
$resolved = gethostbyname($host);
|
132
|
if($resolved) {
|
133
|
$drill=`/usr/bin/drill $host_esc A | grep $host_esc | grep -v ";" | awk '{ print $5 }'`;
|
134
|
$resolved = explode("\n", $drill);
|
135
|
}
|
136
|
$hostname = $host;
|
137
|
if ($host != $resolved)
|
138
|
$ipaddr = $resolved[0];
|
139
|
}
|
140
|
|
141
|
if ($host == $resolved) {
|
142
|
$resolved = gettext("No record found");
|
143
|
}
|
144
|
}
|
145
|
}
|
146
|
|
147
|
if( ($_POST['host']) && ($_POST['dialog_output']) ) {
|
148
|
display_host_results ($host,$resolved,$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") . ": {$address} \n";
|
156
|
echo gettext("Host Name") . ": {$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
|
<body link="#000000" vlink="#000000" alink="#000000">
|
173
|
<?php include("fbegin.inc"); ?>
|
174
|
<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="diag dns">
|
175
|
<tr>
|
176
|
<td>
|
177
|
<?php if ($input_errors) print_input_errors($input_errors); ?>
|
178
|
<form action="diag_dns.php" method="post" name="iform" id="iform">
|
179
|
<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="tabcont">
|
180
|
<tr>
|
181
|
<td colspan="2" valign="top" class="listtopic"> <?=gettext("Resolve DNS hostname or IP");?></td>
|
182
|
</tr>
|
183
|
<tr>
|
184
|
<td width="22%" valign="top" class="vncellreq"><?=gettext("Hostname or IP");?></td>
|
185
|
<td width="78%" class="vtable">
|
186
|
<?=$mandfldhtml;?>
|
187
|
<table summary="results">
|
188
|
<tr><td valign="top">
|
189
|
<input name="host" type="text" class="formfld" id="host" size="20" value="<?=htmlspecialchars($host);?>" /> =
|
190
|
</td>
|
191
|
<td>
|
192
|
<?php if ($resolved && $type) { ?>
|
193
|
<font size="+1">
|
194
|
<?php
|
195
|
$found = 0;
|
196
|
if(is_array($resolved)) {
|
197
|
foreach($resolved as $hostitem) {
|
198
|
if($hostitem <> "") {
|
199
|
echo $hostitem . "<br />";
|
200
|
$found++;
|
201
|
}
|
202
|
}
|
203
|
} else {
|
204
|
echo $resolved;
|
205
|
}
|
206
|
if($found > 0) {
|
207
|
if($alias_exists) {
|
208
|
echo "</font><br /><font size=\"-2\">An alias already exists for the hostname " . htmlspecialchars($host) . ". To overwrite, click <a href=\"diag_dns.php?host=" . trim(urlencode(htmlspecialchars($host))) . "&createalias=true&override=true\">here</a>." . "</font>";
|
209
|
} else {
|
210
|
if(!$createdalias) {
|
211
|
echo "</font><br /><font size=\"-2\"><a href=\"diag_dns.php?host=" . trim(urlencode(htmlspecialchars($host))) . "&createalias=true\">Create alias</a> out of these entries." . "</font>";
|
212
|
} else {
|
213
|
echo "</font><br /><font size=\"-2\">Alias created with name " . htmlspecialchars($newalias['name']) . "</font>";
|
214
|
}
|
215
|
}
|
216
|
}
|
217
|
?>
|
218
|
|
219
|
<?php } ?>
|
220
|
</td></tr></table>
|
221
|
</td>
|
222
|
</tr>
|
223
|
<?php if($_POST): ?>
|
224
|
<tr>
|
225
|
<td width="22%" valign="top" class="vncell"><?=gettext("Resolution time per server");?></td>
|
226
|
<td width="78%" class="vtable">
|
227
|
<table width="170" border="0" cellpadding="6" cellspacing="0" summary="resolution time">
|
228
|
<tr>
|
229
|
<td class="listhdrr">
|
230
|
<?=gettext("Server");?>
|
231
|
</td>
|
232
|
<td class="listhdrr">
|
233
|
<?=gettext("Query time");?>
|
234
|
</td>
|
235
|
</tr>
|
236
|
<?php
|
237
|
if(is_array($dns_speeds))
|
238
|
foreach($dns_speeds as $qt):
|
239
|
?>
|
240
|
<tr>
|
241
|
<td class="listlr">
|
242
|
<?=$qt['dns_server']?>
|
243
|
</td>
|
244
|
<td class="listr">
|
245
|
<?=$qt['query_time']?>
|
246
|
</td>
|
247
|
</tr>
|
248
|
<?php
|
249
|
endforeach;
|
250
|
?>
|
251
|
</table>
|
252
|
</td>
|
253
|
</tr>
|
254
|
<?php endif; ?>
|
255
|
<?php if (!$input_errors && $ipaddr) { ?>
|
256
|
<tr>
|
257
|
<td width="22%" valign="top" class="vncell"><?=gettext("More Information:");?></td>
|
258
|
<td width="78%" class="vtable">
|
259
|
<a href ="/diag_ping.php?host=<?=htmlspecialchars($host)?>&interface=wan&count=3"><?=gettext("Ping");?></a> <br />
|
260
|
<a href ="/diag_traceroute.php?host=<?=htmlspecialchars($host)?>&ttl=18"><?=gettext("Traceroute");?></a>
|
261
|
<p>
|
262
|
<?=gettext("NOTE: The following links are to external services, so their reliability cannot be guaranteed.");?><br /><br />
|
263
|
<a target="_blank" href="http://private.dnsstuff.com/tools/whois.ch?ip=<?php echo $ipaddr; ?>"><?=gettext("IP WHOIS @ DNS Stuff");?></a><br />
|
264
|
<a target="_blank" href="http://private.dnsstuff.com/tools/ipall.ch?ip=<?php echo $ipaddr; ?>"><?=gettext("IP Info @ DNS Stuff");?></a>
|
265
|
</p>
|
266
|
</td>
|
267
|
</tr>
|
268
|
<?php } ?>
|
269
|
<tr>
|
270
|
<td width="22%" valign="top"> </td>
|
271
|
<td width="78%">
|
272
|
<br />
|
273
|
<input name="Submit" type="submit" class="formbtn" value="<?=gettext("DNS Lookup");?>" />
|
274
|
</td>
|
275
|
</tr>
|
276
|
</table>
|
277
|
</form>
|
278
|
</td></tr></table>
|
279
|
<?php include("fend.inc"); ?>
|
280
|
</body>
|
281
|
</html>
|