Project

General

Profile

Download (7.87 KB) Statistics
| Branch: | Tag: | Revision:
1 737ed7d1 jim-p
<?php
2
/*
3
	diag_dns.php
4
5
	Copyright (C) 2009 Jim Pingle (jpingle@gmail.com)
6 ce77a9c4 Phil Davis
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
7 737ed7d1 jim-p
	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 13d193c2 Scott Ullrich
/*
32 3fc58484 Stephen Beaver
	pfSense_MODULE: dns
33 13d193c2 Scott Ullrich
*/
34
35 286aa3f2 sbeaver
$pgtitle = array(gettext("Diagnostics"),gettext("DNS lookup"));
36 737ed7d1 jim-p
require("guiconfig.inc");
37
38 ed2a6e89 jim-p
$host = trim($_REQUEST['host'], " \t\n\r\0\x0B[];\"'");
39 76c4ff0e Renato Botelho
$host_esc = escapeshellarg($host);
40
41 6ee37b41 Sjon Hortensius
/* If this section of config.xml has not been populated yet we need to set it up
42 d5b28fcf Stephen Beaver
*/
43
if (!is_array($config['aliases']['alias'])) {
44
	$config['aliases']['alias'] = array();
45 bc0a452f jim-p
}
46 d5b28fcf Stephen Beaver
$a_aliases = &$config['aliases']['alias'];
47
48 ed2a6e89 jim-p
$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 e55d4b3c Scott Ullrich
		$override = true;
62
	$resolved = gethostbyname($host);
63 ed2a6e89 jim-p
	$type = "hostname";
64 e55d4b3c Scott Ullrich
	if($resolved) {
65 ed2a6e89 jim-p
		$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 e55d4b3c Scott Ullrich
		$isfirst = true;
68
		foreach($resolved as $re) {
69 3fc58484 Stephen Beaver
			if($re != "") {
70 286aa3f2 sbeaver
				if(!$isfirst)
71 7a87cb97 Scott Ullrich
					$addresses .= " ";
72 ed2a6e89 jim-p
				$addresses .= rtrim($re) . "/32";
73 7a87cb97 Scott Ullrich
				$isfirst = false;
74
			}
75 e55d4b3c Scott Ullrich
		}
76
		$newalias = array();
77 06da0d4e Sjon Hortensius
		if($override)
78 e55d4b3c Scott Ullrich
			$alias_exists = false;
79
		if($alias_exists == false) {
80 705c72ea Scott Ullrich
			$newalias['name'] = $aliasname;
81 e1f6a0d9 Scott Ullrich
			$newalias['type'] = "network";
82 e55d4b3c Scott Ullrich
			$newalias['address'] = $addresses;
83
			$newalias['descr'] = "Created from Diagnostics-> DNS Lookup";
84 286aa3f2 sbeaver
			if($override)
85 e55d4b3c Scott Ullrich
				$a_aliases[$id] = $newalias;
86
			else
87
				$a_aliases[] = $newalias;
88
			write_config();
89
			$createdalias = true;
90
		}
91
	}
92
}
93
94 737ed7d1 jim-p
if ($_POST) {
95
	unset($input_errors);
96
97
	$reqdfields = explode(" ", "host");
98
	$reqdfieldsn = explode(",", "Host");
99
100 1e9b4611 Renato Botelho
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
101 286aa3f2 sbeaver
102 72cd706b bcyrill
	if (!is_hostname($host) && !is_ipaddr($host)) {
103 ee91ae30 jean.feltrin
		$input_errors[] = gettext("Host must be a valid hostname or IP address.");
104 72cd706b bcyrill
	} else {
105
		// Test resolution speed of each DNS server.
106 86ab47ff sullrich
		$dns_speeds = array();
107 ed2a6e89 jim-p
		$dns_servers = array();
108
		exec("/usr/bin/grep nameserver /etc/resolv.conf | /usr/bin/cut -f2 -d' '", $dns_servers);
109 40af551f jim-p
		foreach ($dns_servers as $dns_server) {
110 ed2a6e89 jim-p
			$query_time = exec("/usr/bin/drill {$host_esc} " . escapeshellarg("@" . trim($dns_server)) . " | /usr/bin/grep Query | /usr/bin/cut -d':' -f2");
111 77f87165 sullrich
			if($query_time == "")
112 ee91ae30 jean.feltrin
				$query_time = gettext("No response");
113 86ab47ff sullrich
			$new_qt = array();
114
			$new_qt['dns_server'] = $dns_server;
115 ed2a6e89 jim-p
			$new_qt['query_time'] = $query_time;
116 86ab47ff sullrich
			$dns_speeds[] = $new_qt;
117
			unset($new_qt);
118
		}
119 737ed7d1 jim-p
	}
120
121 da652a46 Vinicius Coque
	$type = "unknown";
122 737ed7d1 jim-p
	$resolved = "";
123 2312b0eb jim-p
	$ipaddr = "";
124
	$hostname = "";
125 737ed7d1 jim-p
	if (!$input_errors) {
126
		if (is_ipaddr($host)) {
127
			$type = "ip";
128
			$resolved = gethostbyaddr($host);
129 2312b0eb jim-p
			$ipaddr = $host;
130
			if ($host != $resolved)
131
				$hostname = $resolved;
132 737ed7d1 jim-p
		} elseif (is_hostname($host)) {
133
			$type = "hostname";
134
			$resolved = gethostbyname($host);
135 c43f732a Scott Ullrich
			if($resolved) {
136 ed2a6e89 jim-p
				$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 c43f732a Scott Ullrich
			}
139 2312b0eb jim-p
			$hostname = $host;
140
			if ($host != $resolved)
141 3e85ac2b Scott Ullrich
				$ipaddr = $resolved[0];
142 737ed7d1 jim-p
		}
143
144
		if ($host == $resolved) {
145 ee91ae30 jean.feltrin
			$resolved = gettext("No record found");
146 737ed7d1 jim-p
		}
147
	}
148
}
149
150 fe74228f N0YB
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 79f5aebe Robert Nelson
	$map_lengths = function($element) { return strlen($element[0]); };
157 6ff71328 Robert Nelson
158 fe74228f N0YB
	echo gettext("IP Address") . ": {$address} \n";
159
	echo gettext("Host Name") . ": {$hostname} \n";
160
	echo "\n";
161 6ff71328 Robert Nelson
	$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 fe74228f N0YB
		}
167 6ff71328 Robert Nelson
	}
168 79f5aebe Robert Nelson
	$col0_padlength = max(array_map($map_lengths, $text_table)) + 4;
169 6ff71328 Robert Nelson
	foreach ($text_table as $text_row) {
170 79f5aebe Robert Nelson
		echo str_pad($text_row[0], $col0_padlength) . $text_row[1] . "\n";
171 6ff71328 Robert Nelson
	}
172 fe74228f N0YB
}
173
174 286aa3f2 sbeaver
include("head.inc");
175 e55d4b3c Scott Ullrich
176 286aa3f2 sbeaver
/* Display any error messages resulting from user input */
177
if ($input_errors)
178
	print_input_errors($input_errors);
179
else if (!$resolved && $type)
180 06da0d4e Sjon Hortensius
	print('<div class="alert alert-warning" role="alert">' . gettext("Host") .' "'. $host .'" '. gettext("could not be resolved") . '</div>');
181 286aa3f2 sbeaver
182 06da0d4e Sjon Hortensius
if ($createdalias)
183
	print('<div class="alert alert-success" role="alert">'.gettext("Alias was created/updated successfully").'</div>');
184 286aa3f2 sbeaver
185
require('classes/Form.class.php');
186 3fc58484 Stephen Beaver
187 06da0d4e Sjon Hortensius
$form = new Form(new Form_Button(
188 286aa3f2 sbeaver
	'lookup',
189
	'Lookup'
190
));
191 06da0d4e Sjon Hortensius
$section = new Form_Section('DNS Lookup');
192 286aa3f2 sbeaver
193
$section->addInput(new Form_Input(
194
	'host',
195 06da0d4e Sjon Hortensius
	'Hostname',
196 286aa3f2 sbeaver
	'text',
197 06da0d4e Sjon Hortensius
	$host,
198
	['placeholder' => 'Hostname to look up.']
199
));
200 286aa3f2 sbeaver
201 06da0d4e Sjon Hortensius
if (!empty($resolved)) {
202 286aa3f2 sbeaver
	$form->addGlobal(new Form_Button(
203 06da0d4e Sjon Hortensius
		'create_alias',
204
		'Add alias'
205 286aa3f2 sbeaver
	))->removeClass('btn-primary')->addClass('btn-success');
206
}
207
208
$form->add($section);
209
print $form;
210
211 bcd938ef sbeaver
if (!$input_errors && $type) {
212 6ee37b41 Sjon Hortensius
	if ($resolved):
213 86ab47ff sullrich
?>
214 06da0d4e Sjon Hortensius
<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 86ab47ff sullrich
?>
221 06da0d4e Sjon Hortensius
			<li class="list-group-item"><?=$hostitem?></li>
222
<?
223
			if ($hostitem != "") {
224
				$found++;
225
			}
226
		}
227 286aa3f2 sbeaver
?>
228 06da0d4e Sjon Hortensius
		</ul>
229
	</div>
230
</div>
231 6ee37b41 Sjon Hortensius
<? endif?>
232 06da0d4e Sjon Hortensius
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 737ed7d1 jim-p
		<tr>
248 06da0d4e Sjon Hortensius
			<td><?=$qt['dns_server']?></td><td><?=$qt['query_time']?></td>
249 737ed7d1 jim-p
		</tr>
250 06da0d4e Sjon Hortensius
<? 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)?>&amp;interface=wan&amp;count=3"><?=gettext("Ping")?></a></li>
262
			<li class="list-group-item"><a href="/diag_traceroute.php?host=<?=htmlspecialchars($host)?>&amp;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 bcd938ef sbeaver
}
273 06da0d4e Sjon Hortensius
include("foot.inc");