Project

General

Profile

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