Project

General

Profile

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

    
60
$pgtitle = array(gettext("Diagnostics"), gettext("DNS Lookup"));
61
require("guiconfig.inc");
62

    
63
$host = trim($_REQUEST['host'], " \t\n\r\0\x0B[];\"'");
64
$host_esc = escapeshellarg($host);
65

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

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

    
84
if (isset($_POST['create_alias']) && (is_hostname($host) || is_ipaddr($host))) {
85
	if ($_POST['override']) {
86
		$override = true;
87
	}
88
	$resolved = gethostbyname($host);
89
	$type = "hostname";
90
	if ($resolved) {
91
		$resolved = array();
92
		exec("/usr/bin/drill {$host_esc} A | /usr/bin/grep {$host_esc} | /usr/bin/grep -v ';' | /usr/bin/awk '{ print $5 }'", $resolved);
93
		$isfirst = true;
94
		foreach($resolved as $re) {
95
			if($re != "") {
96
				if(!$isfirst)
97
					$addresses .= " ";
98
				$addresses .= rtrim($re) . "/32";
99
				$isfirst = false;
100
			}
101
		}
102
		$newalias = array();
103
		if ($override) {
104
			$alias_exists = false;
105
		}
106
		if ($alias_exists == false) {
107
			$newalias['name'] = $aliasname;
108
			$newalias['type'] = "network";
109
			$newalias['address'] = $addresses;
110
			$newalias['descr'] = "Created from Diagnostics-> DNS Lookup";
111
			if ($override) {
112
				$a_aliases[$id] = $newalias;
113
			} else {
114
				$a_aliases[] = $newalias;
115
			}
116
			write_config();
117
			$createdalias = true;
118
		}
119
	}
120
}
121

    
122
if ($_POST) {
123
	unset($input_errors);
124

    
125
	$reqdfields = explode(" ", "host");
126
	$reqdfieldsn = explode(",", "Host");
127

    
128
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
129

    
130
	if (!is_hostname($host) && !is_ipaddr($host)) {
131
		$input_errors[] = gettext("Host must be a valid hostname or IP address.");
132
	} else {
133
		// Test resolution speed of each DNS server.
134
		$dns_speeds = array();
135
		$dns_servers = array();
136
		exec("/usr/bin/grep nameserver /etc/resolv.conf | /usr/bin/cut -f2 -d' '", $dns_servers);
137
		foreach ($dns_servers as $dns_server) {
138
			$query_time = exec("/usr/bin/drill {$host_esc} " . escapeshellarg("@" . trim($dns_server)) . " | /usr/bin/grep Query | /usr/bin/cut -d':' -f2");
139
			if ($query_time == "") {
140
				$query_time = gettext("No response");
141
			}
142
			$new_qt = array();
143
			$new_qt['dns_server'] = $dns_server;
144
			$new_qt['query_time'] = $query_time;
145
			$dns_speeds[] = $new_qt;
146
			unset($new_qt);
147
		}
148
	}
149

    
150
	$type = "unknown";
151
	$resolved = "";
152
	$ipaddr = "";
153
	$hostname = "";
154
	if (!$input_errors) {
155
		if (is_ipaddr($host)) {
156
			$type = "ip";
157
			$resolved = gethostbyaddr($host);
158
			$ipaddr = $host;
159
			if ($host != $resolved) {
160
				$hostname = $resolved;
161
			}
162
		} elseif (is_hostname($host)) {
163
			$type = "hostname";
164
			$resolved = gethostbyname($host);
165
			if ($resolved) {
166
				$resolved = array();
167
				exec("/usr/bin/drill {$host_esc} A | /usr/bin/grep {$host_esc} | /usr/bin/grep -v ';' | /usr/bin/awk '{ print $5 }'", $resolved);
168
			}
169
			$hostname = $host;
170
			if ($host != $resolved) {
171
				$ipaddr = $resolved[0];
172
			}
173
		}
174

    
175
		if ($host == $resolved) {
176
			$resolved = gettext("No record found");
177
		}
178
	}
179
}
180

    
181
if (($_POST['host']) && ($_POST['dialog_output'])) {
182
	display_host_results ($host, $resolved, $dns_speeds);
183
	exit;
184
}
185

    
186
function display_host_results ($address, $hostname, $dns_speeds) {
187
	$map_lengths = function($element) { return strlen($element[0]); };
188

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

    
205
include("head.inc");
206

    
207
/* Display any error messages resulting from user input */
208
if ($input_errors)
209
	print_input_errors($input_errors);
210
else if (!$resolved && $type)
211
	print('<div class="alert alert-warning" role="alert">' . gettext("Host") .' "'. $host .'" '. gettext("could not be resolved") . '</div>');
212

    
213
if ($createdalias)
214
	print('<div class="alert alert-success" role="alert">'.gettext("Alias was created/updated successfully").'</div>');
215

    
216
require('classes/Form.class.php');
217

    
218
$form = new Form('Lookup');
219
$section = new Form_Section('DNS Lookup');
220

    
221
$section->addInput(new Form_Input(
222
	'host',
223
	'Hostname',
224
	'text',
225
	$host,
226
	['placeholder' => 'Hostname to look up.']
227
));
228

    
229
if (!empty($resolved)) {
230
	$form->addGlobal(new Form_Button(
231
		'create_alias',
232
		'Add alias'
233
	))->removeClass('btn-primary')->addClass('btn-success');
234
}
235

    
236
$form->add($section);
237
print $form;
238

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

    
261
<!-- Second table displays the server resolution times -->
262
<div class="panel panel-default">
263
	<div class="panel-heading"><h2 class="panel-title">Timings</h2></div>
264
	<div class="panel-body">
265
		<table class="table">
266
		<thead>
267
			<tr>
268
				<th>Name server</th>
269
				<th>Query time</th>
270
			</tr>
271
		</thead>
272

    
273
		<tbody>
274
<? foreach ((array)$dns_speeds as $qt):?>
275
		<tr>
276
			<td><?=$qt['dns_server']?></td><td><?=$qt['query_time']?></td>
277
		</tr>
278
<? endforeach?>
279
		</tbody>
280
		</table>
281
	</div>
282
</div>
283

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