Project

General

Profile

Download (9.38 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	diag_dns.php
4
*/
5
/* ====================================================================
6
 *  Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *
8
 *  Redistribution and use in source and binary forms, with or without modification,
9
 *  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
16
 *      the documentation and/or other materials provided with the
17
 *      distribution.
18
 *
19
 *  3. All advertising materials mentioning features or use of this software
20
 *      must display the following acknowledgment:
21
 *      "This product includes software developed by the pfSense Project
22
 *       for use in the pfSense software distribution. (http://www.pfsense.org/).
23
 *
24
 *  4. The names "pfSense" and "pfSense Project" must not be used to
25
 *       endorse or promote products derived from this software without
26
 *       prior written permission. For written permission, please contact
27
 *       coreteam@pfsense.org.
28
 *
29
 *  5. Products derived from this software may not be called "pfSense"
30
 *      nor may "pfSense" appear in their names without prior written
31
 *      permission of the Electric Sheep Fencing, LLC.
32
 *
33
 *  6. Redistributions of any form whatsoever must retain the following
34
 *      acknowledgment:
35
 *
36
 *  "This product includes software developed by the pfSense Project
37
 *  for use in the pfSense software distribution (http://www.pfsense.org/).
38
 *
39
 *  THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
40
 *  EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
43
 *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45
 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48
 *  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
51
 *
52
 *  ====================================================================
53
 *
54
 */
55

    
56
##|+PRIV
57
##|*IDENT=page-diagnostics-dns
58
##|*NAME=Diagnostics: DNS Lookup
59
##|*DESCR=Allow access to the 'Diagnostics: DNS Lookup' page.
60
##|*MATCH=diag_dns.php*
61
##|-PRIV
62

    
63
$pgtitle = array(gettext("Diagnostics"), gettext("DNS Lookup"));
64
require("guiconfig.inc");
65

    
66
$host = trim($_REQUEST['host'], " \t\n\r\0\x0B[];\"'");
67
$host_esc = escapeshellarg($host);
68

    
69
/* If this section of config.xml has not been populated yet we need to set it up
70
*/
71
if (!is_array($config['aliases']['alias'])) {
72
	$config['aliases']['alias'] = array();
73
}
74
$a_aliases = &$config['aliases']['alias'];
75

    
76
$aliasname = str_replace(array(".", "-"), "_", $host);
77
$alias_exists = false;
78
$counter = 0;
79
foreach ($a_aliases as $a) {
80
	if ($a['name'] == $aliasname) {
81
		$alias_exists = true;
82
		$id = $counter;
83
	}
84
	$counter++;
85
}
86

    
87
if (isset($_POST['create_alias']) && (is_hostname($host) || is_ipaddr($host))) {
88
	if ($_POST['override']) {
89
		$override = true;
90
	}
91
	$resolved = gethostbyname($host);
92
	$type = "hostname";
93
	if ($resolved) {
94
		$resolved = array();
95
		exec("/usr/bin/drill {$host_esc} A | /usr/bin/grep {$host_esc} | /usr/bin/grep -v ';' | /usr/bin/awk '{ print $5 }'", $resolved);
96
		$isfirst = true;
97
		foreach ($resolved as $re) {
98
			if ($re != "") {
99
				if (!$isfirst) {
100
					$addresses .= " ";
101
				}
102
				$addresses .= rtrim($re) . "/32";
103
				$isfirst = false;
104
			}
105
		}
106
		$newalias = array();
107
		if ($override) {
108
			$alias_exists = false;
109
		}
110
		if ($alias_exists == false) {
111
			$newalias['name'] = $aliasname;
112
			$newalias['type'] = "network";
113
			$newalias['address'] = $addresses;
114
			$newalias['descr'] = gettext("Created from Diagnostics-> DNS Lookup");
115
			if ($override) {
116
				$a_aliases[$id] = $newalias;
117
			} else {
118
				$a_aliases[] = $newalias;
119
			}
120
			write_config();
121
			$createdalias = true;
122
		}
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
			$resolved = gethostbyaddr($host);
162
			$ipaddr = $host;
163
			if ($host != $resolved) {
164
				$hostname = $resolved;
165
			}
166
		} elseif (is_hostname($host)) {
167
			$type = "hostname";
168
			$resolved = gethostbyname($host);
169
			if ($resolved) {
170
				$resolved = array();
171
				exec("/usr/bin/drill {$host_esc} A | /usr/bin/grep {$host_esc} | /usr/bin/grep -v ';' | /usr/bin/awk '{ print $5 }'", $resolved);
172
			}
173
			$hostname = $host;
174
			if ($host != $resolved) {
175
				$ipaddr = $resolved[0];
176
			}
177
		}
178

    
179
		if ($host == $resolved) {
180
			$resolved = gettext("No record found");
181
		}
182
	}
183
}
184

    
185
if (($_POST['host']) && ($_POST['dialog_output'])) {
186
	display_host_results ($host, $resolved, $dns_speeds);
187
	exit;
188
}
189

    
190
function display_host_results ($address, $hostname, $dns_speeds) {
191
	$map_lengths = function($element) { return strlen($element[0]); };
192

    
193
	echo gettext("IP Address") . ": {$address} \n";
194
	echo gettext("Host Name") . ": {$hostname} \n";
195
	echo "\n";
196
	$text_table = array();
197
	$text_table[] = array(gettext("Server"), gettext("Query Time"));
198
	if (is_array($dns_speeds)) {
199
		foreach ($dns_speeds as $qt) {
200
			$text_table[] = array(trim($qt['dns_server']), trim($qt['query_time']));
201
		}
202
	}
203
	$col0_padlength = max(array_map($map_lengths, $text_table)) + 4;
204
	foreach ($text_table as $text_row) {
205
		echo str_pad($text_row[0], $col0_padlength) . $text_row[1] . "\n";
206
	}
207
}
208

    
209
include("head.inc");
210

    
211
/* Display any error messages resulting from user input */
212
if ($input_errors) {
213
	print_input_errors($input_errors);
214
} else if (!$resolved && $type) {
215
	print('<div class="alert alert-warning" role="alert">' . sprintf(gettext('Host "%s" could not be resolved'), $host) . '</div>');
216
}
217

    
218
if ($createdalias) {
219
	print('<div class="alert alert-success" role="alert">' . gettext("Alias was created/updated successfully") . '</div>');
220
}
221

    
222
$form = new Form('Lookup');
223
$section = new Form_Section('DNS Lookup');
224

    
225
$section->addInput(new Form_Input(
226
	'host',
227
	'Hostname',
228
	'text',
229
	$host,
230
	['placeholder' => 'Hostname to look up.']
231
));
232

    
233
if (!empty($resolved)) {
234
	$form->addGlobal(new Form_Button(
235
		'create_alias',
236
		'Add alias'
237
	))->removeClass('btn-primary')->addClass('btn-success');
238
}
239

    
240
$form->add($section);
241
print $form;
242

    
243
if (!$input_errors && $type) {
244
	if ($resolved):
245
?>
246
<div class="panel panel-default">
247
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Results')?></h2></div>
248
	<div class="panel-body">
249
		<ul class="list-group">
250
<?php
251
		foreach ((array)$resolved as $hostitem) {
252
?>
253
			<li class="list-group-item"><?=$hostitem?></li>
254
<?php
255
			if ($hostitem != "") {
256
				$found++;
257
			}
258
		}
259
?>
260
		</ul>
261
	</div>
262
</div>
263
<?php endif; ?>
264

    
265
<!-- Second table displays the server resolution times -->
266
<div class="panel panel-default">
267
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Timings')?></h2></div>
268
	<div class="panel-body">
269
		<table class="table">
270
		<thead>
271
			<tr>
272
				<th><?=gettext('Name server')?></th>
273
				<th><?=gettext('Query time')?></th>
274
			</tr>
275
		</thead>
276

    
277
		<tbody>
278
<?php foreach ((array)$dns_speeds as $qt):?>
279
		<tr>
280
			<td><?=$qt['dns_server']?></td><td><?=$qt['query_time']?></td>
281
		</tr>
282
<?php endforeach; ?>
283
		</tbody>
284
		</table>
285
	</div>
286
</div>
287

    
288
<!-- Third table displays "More information" -->
289
<div class="panel panel-default">
290
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('More information')?></h2></div>
291
	<div class="panel-body">
292
		<ul class="list-group">
293
			<li class="list-group-item"><a href="/diag_ping.php?host=<?=htmlspecialchars($host)?>&amp;count=3"><?=gettext("Ping")?></a></li>
294
			<li class="list-group-item"><a href="/diag_traceroute.php?host=<?=htmlspecialchars($host)?>&amp;ttl=18"><?=gettext("Traceroute")?></a></li>
295
		</ul>
296
		<h5><?=gettext("NOTE: The following links are to external services, so their reliability cannot be guaranteed.");?></h5>
297
		<ul class="list-group">
298
			<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>
299
			<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>
300
		</ul>
301
	</div>
302
</div>
303
<?php
304
}
305
include("foot.inc");
(10-10/229)