Project

General

Profile

Download (8.57 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	diag_dns.php
4

    
5
	Copyright© 2015 Rubicon Communications, LLC (Netgate)
6
	This file is a part of pfSense®
7

    
8
	Copyright (C) 2009 Jim Pingle (jpingle@gmail.com)
9
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
10
	All rights reserved.
11

    
12
	Redistribution and use in source and binary forms, with or without
13
	modification, are permitted provided that the following conditions are met:
14

    
15
	1. Redistributions of source code must retain the above copyright notice,
16
	this list of conditions and the following disclaimer.
17

    
18
	2. Redistributions in binary form must reproduce the above copyright
19
	notice, this list of conditions and the following disclaimer in the
20
	documentation and/or other materials provided with the distribution.
21
s
22
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
	POSSIBILITY OF SUCH DAMAGE.
32
*/
33

    
34
/*
35
	pfSense_MODULE: dns
36
*/
37

    
38
/* Bootstrap conversion: sbeaver@netgate.com */
39
require("guiconfig.inc");
40
define('NO_BUTTON', false);
41
$pgtitle = array(gettext("Diagnostics"),gettext("DNS lookup"));
42
$host = trim($_REQUEST['host'], " \t\n\r\0\x0B[];\"'");
43
$host_esc = escapeshellarg($host);
44

    
45
if(isset($_POST['create_alias']))
46
	$host = $_POST['host'] = $_POST['alias'];
47

    
48
$host_esc = escapeshellarg($host);
49

    
50
/* If this section of config.xml has not been populated yet we need to set it up
51
*/
52
if (!is_array($config['aliases']['alias'])) {
53
	$config['aliases']['alias'] = array();
54
}
55
$a_aliases = &$config['aliases']['alias'];
56

    
57
$aliasname = str_replace(array(".","-"), "_", $host);
58
$alias_exists = false;
59
$counter=0;
60
foreach($a_aliases as $a) {
61
	if($a['name'] == $aliasname) {
62
		$alias_exists = true;
63
		$id=$counter;
64
	}
65
	$counter++;
66
}
67

    
68
if(isset($_POST['create_alias']) && (is_hostname($host) || is_ipaddr($host))) {
69
	if($_POST['override'])
70
		$override = true;
71

    
72
	$resolved = gethostbyname($host);
73

    
74
	$type = "hostname";
75
	if($resolved) {
76
		$resolved = array();
77
		exec("/usr/bin/drill {$host_esc} A | /usr/bin/grep {$host_esc} | /usr/bin/grep -v ';' | /usr/bin/awk '{ print $5 }'", $resolved);
78
		$isfirst = true;
79
		foreach($resolved as $re) {
80
			if($re != "") {
81
				if(!$isfirst)
82
					$addresses .= " ";
83
				$addresses .= rtrim($re) . "/32";
84
				$isfirst = false;
85
			}
86
		}
87
		$newalias = array();
88

    
89
	/*	if($override)  */
90
			$alias_exists = false;
91

    
92
		if($alias_exists == false) {
93
			$newalias['name'] = $aliasname;
94
			$newalias['type'] = "network";
95
			$newalias['address'] = $addresses;
96
			$newalias['descr'] = "Created from Diagnostics-> DNS Lookup";
97
			if($override)
98
				$a_aliases[$id] = $newalias;
99
			else
100
				$a_aliases[] = $newalias;
101
			write_config();
102

    
103
			$createdalias = true;
104
		}
105
	}
106
}
107

    
108
if ($_POST) {
109
	unset($input_errors);
110

    
111
	$reqdfields = explode(" ", "host");
112
	$reqdfieldsn = explode(",", "Host");
113

    
114
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
115

    
116
	if (!is_hostname($host) && !is_ipaddr($host)) {
117
		$input_errors[] = gettext("Host must be a valid hostname or IP address.");
118
	} else {
119
		// Test resolution speed of each DNS server.
120
		$dns_speeds = array();
121
		$dns_servers = array();
122
		exec("/usr/bin/grep nameserver /etc/resolv.conf | /usr/bin/cut -f2 -d' '", $dns_servers);
123
		foreach ($dns_servers as $dns_server) {
124
			$query_time = exec("/usr/bin/drill {$host_esc} " . escapeshellarg("@" . trim($dns_server)) . " | /usr/bin/grep Query | /usr/bin/cut -d':' -f2");
125
			if($query_time == "")
126
				$query_time = gettext("No response");
127
			$new_qt = array();
128
			$new_qt['dns_server'] = $dns_server;
129
			$new_qt['query_time'] = $query_time;
130
			$dns_speeds[] = $new_qt;
131
			unset($new_qt);
132
		}
133
	}
134

    
135
	$type = "unknown";
136
	$resolved = "";
137
	$ipaddr = "";
138
	$hostname = "";
139
	if (!$input_errors) {
140
		if (is_ipaddr($host)) {
141
			$type = "ip";
142
			$resolved = gethostbyaddr($host);
143
			$ipaddr = $host;
144
			if ($host != $resolved)
145
				$hostname = $resolved;
146
		} elseif (is_hostname($host)) {
147
			$type = "hostname";
148
			$resolved = gethostbyname($host);
149
			if($resolved) {
150
				$resolved = array();
151
				exec("/usr/bin/drill {$host_esc} A | /usr/bin/grep {$host_esc} | /usr/bin/grep -v ';' | /usr/bin/awk '{ print $5 }'", $resolved);
152
			}
153
			$hostname = $host;
154
			if ($host != $resolved)
155
				$ipaddr = $resolved[0];
156
		}
157

    
158
		if ($host == $resolved) {
159
			$resolved = gettext("No record found");
160
		}
161
	}
162
}
163

    
164
if( ($_POST['host']) && ($_POST['dialog_output']) ) {
165
	display_host_results ($host,$resolved,$dns_speeds);
166
	exit;
167
}
168

    
169
function display_host_results ($address,$hostname,$dns_speeds) {
170
	$map_lengths = function($element) { return strlen($element[0]); };
171

    
172
	$text_table = array();
173
	$text_table[] = array(gettext("Server"), gettext("Query Time"));
174
	if (is_array($dns_speeds)) {
175
		foreach ($dns_speeds as $qt) {
176
			$text_table[] = array(trim($qt['dns_server']), trim($qt['query_time']));
177
		}
178
	}
179
	$col0_padlength = max(array_map($map_lengths, $text_table)) + 4;
180
	foreach ($text_table as $text_row) {
181
		echo str_pad($text_row[0], $col0_padlength) . $text_row[1] . "\n";
182
	}
183
}
184

    
185
include("head.inc");
186

    
187
/* Display any error messages resulting from user input */
188
if ($input_errors)
189
	print_input_errors($input_errors);
190
else if (!$resolved && $type)
191
	print('<div class="alert alert-warning" role="alert">' . gettext("Host \"") . $host . "\"" . gettext(" could not be resolved") . '</div>');
192

    
193
if($createdalias)
194
   print('<div class="alert alert-success" role="alert">Alias was created/updated successfully</div>');
195
?>
196

    
197
<?php if (!$input_errors && $ipaddr) { ?>
198
<form action="diag_dns.php" method="post" name="iform" id="iform">
199

    
200
<?php
201
	unset($dns_results);
202
	if ($resolved && $type) {
203
		$dns_results = array();
204
		$found = 0;
205
		$dns_results[$found] = array("Address(es)");
206
		if(is_array($resolved)) {
207
			foreach($resolved as $hostitem) {
208
				if($hostitem != "") {
209
					$found++;
210
					$dns_results[$found] = array($hostitem);
211
				}
212
			}
213
		} else {
214
			$dns_results[1] = array($resolved);
215
	}
216
 }
217
?>
218

    
219
<!-- Second table displays the server resolution times -->
220
<?php
221
	$timing_results = array();
222
	$timing_results[0] = array("Name server", "Query time");
223

    
224
	if(is_array($dns_speeds)) {
225
		$i = 1;
226

    
227
		foreach($dns_speeds as $qt):
228
		  $timing_results[$i++] = array($qt['dns_server'], $qt['query_time']);
229
		endforeach;
230
	}
231

    
232
?>
233

    
234
<!-- Third table displays "More information" -->
235
<?php
236
	$more_information = array();
237
	$more_information[0] = array("");
238
	$more_information[1] = array("<a href =\"/diag_ping.php?host=" . htmlspecialchars($host) . "&amp;interface=wan&amp;count=3\">" . gettext("Ping") . "</a>");
239
	$more_information[2] = array("<a href =\"/diag_traceroute.php?host=" . htmlspecialchars($host) . "&amp;ttl=18\">" . gettext("Traceroute") . "</a>");
240
	$more_information[3] = array("<font size=\"-1\">\r\n" . "NOTE: The following links are to external services, so their reliability cannot be guaranteed. They use only the first IP address returned above.\r\n");
241
	$more_information[4] = array("<a target=\"_blank\" href=\"http://private.dnsstuff.com/tools/whois.ch?ip=" . $ipaddr . "\">" . gettext("IP WHOIS @ DNS Stuff") . "</a>");
242
	$more_information[5] = array("<a target=\"_blank\" href=\"http://private.dnsstuff.com/tools/ipall.ch?ip=" . $ipaddr . "\">" . gettext("IP Info @ DNS Stuff") . "</a>");
243
?>
244

    
245
<?php
246
}
247

    
248
require('classes/Form.class.php');
249

    
250
$form = new Form(NO_BUTTON);
251
$section = new Form_Section('DNS Lookup');
252

    
253
if( isset($dns_results))
254
	$section->addInput(new Form_Table("Results", $dns_results));
255

    
256
if(isset($timing_results))
257
	$section->addInput(new Form_Table("Timing", $timing_results));
258

    
259
$form->addGlobal(new Form_Button(
260
	'lookup',
261
	'Lookup'
262
));
263

    
264
$section->addInput(new Form_Input(
265
	'alias',
266
	'',
267
	'hidden',
268
	$host,
269
	['placeholder' => 'Description']
270
));
271

    
272
$section->addInput(new Form_Input(
273
	'host',
274
	'Host name',
275
	'text',
276
	$pconfig['descr'],
277
	['placeholder' => 'Description']
278
))->setWidth(3)->setHelp(gettext('Host name to look up.'));
279

    
280
if($found > 0)
281
	{
282
	$form->addGlobal(new Form_Button(
283
	   'create_alias',
284
	   'Add alias'
285
	))->removeClass('btn-primary')->addClass('btn-success');
286
}
287

    
288
$form->add($section);
289
print $form;
290

    
291
if($found > 0) {
292
	print("<br />");
293
	$form2 = new Form(NO_BUTTON);
294
	$section2 = new Form_Section('More information');
295
	$section2->addInput(new Form_Table("", $more_information));
296
	$form2->add($section2);
297
	print $form2;
298
}
299
include("foot.inc");
300
?>
(11-11/253)