Project

General

Profile

Download (8.78 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 29aef6c4 Jim Thompson
        Copyright (C) 2013-2014 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
	pfSense_MODULE:	dns
33
*/
34
35 09128488 Vinicius Coque
$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 bc0a452f jim-p
if (is_array($config['aliases']['alias'])) {
42
	$a_aliases = &$config['aliases']['alias'];
43
} else {
44
	$a_aliases = array();
45
}
46 ed2a6e89 jim-p
$aliasname = str_replace(array(".","-"), "_", $host);
47
$alias_exists = false;
48
$counter=0;
49
foreach($a_aliases as $a) {
50
	if($a['name'] == $aliasname) {
51
		$alias_exists = true;
52
		$id=$counter;
53
	}
54
	$counter++;
55
}
56
57
if(isset($_POST['create_alias']) && (is_hostname($host) || is_ipaddr($host))) {
58
	if($_POST['override'])
59 e55d4b3c Scott Ullrich
		$override = true;
60
	$resolved = gethostbyname($host);
61 ed2a6e89 jim-p
	$type = "hostname";
62 e55d4b3c Scott Ullrich
	if($resolved) {
63 ed2a6e89 jim-p
		$resolved = array();
64
		exec("/usr/bin/drill {$host_esc} A | /usr/bin/grep {$host_esc} | /usr/bin/grep -v ';' | /usr/bin/awk '{ print $5 }'", $resolved);
65 e55d4b3c Scott Ullrich
		$isfirst = true;
66
		foreach($resolved as $re) {
67 7a87cb97 Scott Ullrich
			if($re <> "") {
68
				if(!$isfirst) 
69
					$addresses .= " ";
70 ed2a6e89 jim-p
				$addresses .= rtrim($re) . "/32";
71 7a87cb97 Scott Ullrich
				$isfirst = false;
72
			}
73 e55d4b3c Scott Ullrich
		}
74
		$newalias = array();
75
		if($override) 
76
			$alias_exists = false;
77
		if($alias_exists == false) {
78 705c72ea Scott Ullrich
			$newalias['name'] = $aliasname;
79 e1f6a0d9 Scott Ullrich
			$newalias['type'] = "network";
80 e55d4b3c Scott Ullrich
			$newalias['address'] = $addresses;
81
			$newalias['descr'] = "Created from Diagnostics-> DNS Lookup";
82
			if($override) 
83
				$a_aliases[$id] = $newalias;
84
			else
85
				$a_aliases[] = $newalias;
86
			write_config();
87
			$createdalias = true;
88
		}
89
	}
90
}
91
92 737ed7d1 jim-p
if ($_POST) {
93
	unset($input_errors);
94
95
	$reqdfields = explode(" ", "host");
96
	$reqdfieldsn = explode(",", "Host");
97
98 1e9b4611 Renato Botelho
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
99 b97c5ed6 sullrich
	
100 72cd706b bcyrill
	if (!is_hostname($host) && !is_ipaddr($host)) {
101 ee91ae30 jean.feltrin
		$input_errors[] = gettext("Host must be a valid hostname or IP address.");
102 72cd706b bcyrill
	} else {
103
		// Test resolution speed of each DNS server.
104 86ab47ff sullrich
		$dns_speeds = array();
105 ed2a6e89 jim-p
		$dns_servers = array();
106
		exec("/usr/bin/grep nameserver /etc/resolv.conf | /usr/bin/cut -f2 -d' '", $dns_servers);
107 40af551f jim-p
		foreach ($dns_servers as $dns_server) {
108 ed2a6e89 jim-p
			$query_time = exec("/usr/bin/drill {$host_esc} " . escapeshellarg("@" . trim($dns_server)) . " | /usr/bin/grep Query | /usr/bin/cut -d':' -f2");
109 77f87165 sullrich
			if($query_time == "")
110 ee91ae30 jean.feltrin
				$query_time = gettext("No response");
111 86ab47ff sullrich
			$new_qt = array();
112
			$new_qt['dns_server'] = $dns_server;
113 ed2a6e89 jim-p
			$new_qt['query_time'] = $query_time;
114 86ab47ff sullrich
			$dns_speeds[] = $new_qt;
115
			unset($new_qt);
116
		}
117 737ed7d1 jim-p
	}
118
119 da652a46 Vinicius Coque
	$type = "unknown";
120 737ed7d1 jim-p
	$resolved = "";
121 2312b0eb jim-p
	$ipaddr = "";
122
	$hostname = "";
123 737ed7d1 jim-p
	if (!$input_errors) {
124
		if (is_ipaddr($host)) {
125
			$type = "ip";
126
			$resolved = gethostbyaddr($host);
127 2312b0eb jim-p
			$ipaddr = $host;
128
			if ($host != $resolved)
129
				$hostname = $resolved;
130 737ed7d1 jim-p
		} elseif (is_hostname($host)) {
131
			$type = "hostname";
132
			$resolved = gethostbyname($host);
133 c43f732a Scott Ullrich
			if($resolved) {
134 ed2a6e89 jim-p
				$resolved = array();
135
				exec("/usr/bin/drill {$host_esc} A | /usr/bin/grep {$host_esc} | /usr/bin/grep -v ';' | /usr/bin/awk '{ print $5 }'", $resolved);
136 c43f732a Scott Ullrich
			}
137 2312b0eb jim-p
			$hostname = $host;
138
			if ($host != $resolved)
139 3e85ac2b Scott Ullrich
				$ipaddr = $resolved[0];
140 737ed7d1 jim-p
		}
141
142
		if ($host == $resolved) {
143 ee91ae30 jean.feltrin
			$resolved = gettext("No record found");
144 737ed7d1 jim-p
		}
145
	}
146
}
147
148 fe74228f N0YB
if( ($_POST['host']) && ($_POST['dialog_output']) ) {
149
	display_host_results ($host,$resolved,$dns_speeds);
150
	exit;
151
}
152
153
function display_host_results ($address,$hostname,$dns_speeds) {
154 79f5aebe Robert Nelson
	$map_lengths = function($element) { return strlen($element[0]); };
155 6ff71328 Robert Nelson
156 fe74228f N0YB
	echo gettext("IP Address") . ": {$address} \n";
157
	echo gettext("Host Name") . ": {$hostname} \n";
158
	echo "\n";
159 6ff71328 Robert Nelson
	$text_table = array();
160
	$text_table[] = array(gettext("Server"), gettext("Query Time"));
161
	if (is_array($dns_speeds)) {
162
		foreach ($dns_speeds as $qt) {
163
			$text_table[] = array(trim($qt['dns_server']), trim($qt['query_time']));
164 fe74228f N0YB
		}
165 6ff71328 Robert Nelson
	}
166 79f5aebe Robert Nelson
	$col0_padlength = max(array_map($map_lengths, $text_table)) + 4;
167 6ff71328 Robert Nelson
	foreach ($text_table as $text_row) {
168 79f5aebe Robert Nelson
		echo str_pad($text_row[0], $col0_padlength) . $text_row[1] . "\n";
169 6ff71328 Robert Nelson
	}
170 fe74228f N0YB
}
171
172 737ed7d1 jim-p
include("head.inc"); ?>
173
<body link="#000000" vlink="#000000" alink="#000000">
174 a3381369 Colin Fleming
<?php include("fbegin.inc"); ?>
175 afc09384 Colin Fleming
<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="diag dns">
176 737ed7d1 jim-p
        <tr>
177
                <td>
178
<?php if ($input_errors) print_input_errors($input_errors); ?>
179
	<form action="diag_dns.php" method="post" name="iform" id="iform">
180 afc09384 Colin Fleming
	  <table width="100%" border="0" cellpadding="6" cellspacing="0" summary="tabcont">
181 4c7e2f4e sullrich
		<tr>
182 ee91ae30 jean.feltrin
			<td colspan="2" valign="top" class="listtopic"> <?=gettext("Resolve DNS hostname or IP");?></td>
183 4c7e2f4e sullrich
		</tr>
184 737ed7d1 jim-p
        <tr>
185 da652a46 Vinicius Coque
		  <td width="22%" valign="top" class="vncellreq"><?=gettext("Hostname or IP");?></td>
186 737ed7d1 jim-p
		  <td width="78%" class="vtable">
187 a812abb3 Scott Ullrich
            <?=$mandfldhtml;?>
188 afc09384 Colin Fleming
			<table summary="results">
189 a812abb3 Scott Ullrich
				<tr><td valign="top">
190 8de8ff0f Colin Fleming
			<input name="host" type="text" class="formfld" id="host" size="20" value="<?=htmlspecialchars($host);?>" />
191 a812abb3 Scott Ullrich
			</td>
192 a3381369 Colin Fleming
			<?php if ($resolved && $type) { ?>
193 8de8ff0f Colin Fleming
			<td valign="middle">&nbsp;=&nbsp;</td><td>
194 e6f98d5b Colin Fleming
			<font size="+1">
195 a812abb3 Scott Ullrich
<?php
196 3a6dc294 Scott Ullrich
				$found = 0;
197 a812abb3 Scott Ullrich
				if(is_array($resolved)) { 
198
					foreach($resolved as $hostitem) {
199 3a6dc294 Scott Ullrich
						if($hostitem <> "") {
200 8cd558b6 ayvis
							echo $hostitem . "<br />";
201 3a6dc294 Scott Ullrich
							$found++;
202
						}
203 a812abb3 Scott Ullrich
					}
204
				} else {
205
					echo $resolved; 
206
				} 
207 ed2a6e89 jim-p
				if($found > 0) { ?>
208 8de8ff0f Colin Fleming
					<br/></font><font size='-2'>
209 ed2a6e89 jim-p
				<?PHP	if($alias_exists) { ?>
210
							An alias already exists for the hostname <?= htmlspecialchars($host) ?>. <br />
211
							<input type="hidden" name="override" value="true"/>
212
							<input type="submit" name="create_alias" value="Overwrite Alias"/>
213
				<?PHP	} else {
214
						if(!$createdalias) { ?>
215
							<input type="submit" name="create_alias" value="Create Alias from These Entries"/>
216
					<?PHP	} else { ?>
217
							Alias created with name <?= htmlspecialchars($newalias['name']) ?>
218
					<?PHP	}
219 e55d4b3c Scott Ullrich
					}
220
				}
221 a812abb3 Scott Ullrich
?>
222 e55d4b3c Scott Ullrich
223 30cbc007 Renato Botelho
			<?php } ?>
224 8de8ff0f Colin Fleming
			</font></td></tr></table>
225 737ed7d1 jim-p
		  </td>
226
		</tr>
227 37d98ce7 sullrich
<?php		if($_POST): ?>
228 86ab47ff sullrich
		<tr>
229 ee91ae30 jean.feltrin
		  <td width="22%" valign="top" class="vncell"><?=gettext("Resolution time per server");?></td>
230 86ab47ff sullrich
		  <td width="78%" class="vtable">
231 e6f98d5b Colin Fleming
				<table width="170" border="0" cellpadding="6" cellspacing="0" summary="resolution time">
232 86ab47ff sullrich
					<tr>
233 e6f98d5b Colin Fleming
						<td class="listhdrr">
234
							<?=gettext("Server");?>
235 86ab47ff sullrich
						</td>
236 e6f98d5b Colin Fleming
						<td class="listhdrr">
237
							<?=gettext("Query time");?>
238 86ab47ff sullrich
						</td>
239
					</tr>
240
<?php
241
					if(is_array($dns_speeds)) 
242
						foreach($dns_speeds as $qt):
243
?>
244
					<tr>
245 e6f98d5b Colin Fleming
						<td class="listlr">
246 86ab47ff sullrich
							<?=$qt['dns_server']?>
247
						</td>
248 e6f98d5b Colin Fleming
						<td class="listr">
249 86ab47ff sullrich
							<?=$qt['query_time']?>
250
						</td>
251
					</tr>
252
<?php
253
					endforeach;
254
?>
255
				</table>
256
		  </td>
257
		</tr>
258 37d98ce7 sullrich
		<?php endif; ?>
259 2312b0eb jim-p
		<?php if (!$input_errors && $ipaddr) { ?>
260
		<tr>
261 ee91ae30 jean.feltrin
			<td width="22%" valign="top"  class="vncell"><?=gettext("More Information:");?></td>
262 2312b0eb jim-p
			<td width="78%" class="vtable">
263 afc09384 Colin Fleming
				<a href ="/diag_ping.php?host=<?=htmlspecialchars($host)?>&amp;interface=wan&amp;count=3"><?=gettext("Ping");?></a> <br />
264
				<a href ="/diag_traceroute.php?host=<?=htmlspecialchars($host)?>&amp;ttl=18"><?=gettext("Traceroute");?></a>
265
				<p>
266 8cd558b6 ayvis
				<?=gettext("NOTE: The following links are to external services, so their reliability cannot be guaranteed.");?><br /><br />
267 afc09384 Colin Fleming
				<a target="_blank" href="http://private.dnsstuff.com/tools/whois.ch?ip=<?php echo $ipaddr; ?>"><?=gettext("IP WHOIS @ DNS Stuff");?></a><br />
268
				<a target="_blank" href="http://private.dnsstuff.com/tools/ipall.ch?ip=<?php echo $ipaddr; ?>"><?=gettext("IP Info @ DNS Stuff");?></a>
269
				</p>
270 2312b0eb jim-p
			</td>
271
		</tr>
272
		<?php } ?>
273 737ed7d1 jim-p
		<tr>
274
		  <td width="22%" valign="top">&nbsp;</td>
275
		  <td width="78%">
276 8cd558b6 ayvis
			<br />&nbsp;
277 afc09384 Colin Fleming
            <input name="Submit" type="submit" class="formbtn" value="<?=gettext("DNS Lookup");?>" />
278 737ed7d1 jim-p
		</td>
279
		</tr>
280
	</table>
281
</form>
282
</td></tr></table>
283
<?php include("fend.inc"); ?>
284 afc09384 Colin Fleming
</body>
285
</html>