Project

General

Profile

Download (7.81 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
/* Cheap hack to support both $_GET and $_POST */
38
if ($_GET['host'])
39
	$_POST = $_GET;
40
41 e55d4b3c Scott Ullrich
if($_GET['createalias'] == "true") {
42
	$host = trim($_POST['host']);
43
	if($_GET['override'])
44
		$override = true;
45
	$a_aliases = &$config['aliases']['alias'];
46
	$type = "hostname";
47
	$resolved = gethostbyname($host);
48
	if($resolved) {
49
		$host = trim($_POST['host']);
50
		$dig=`dig "$host" A | grep "$host" | grep -v ";" | awk '{ print $5 }'`;
51 cfbfd941 smos
		$resolved = explode("\n", $dig);
52 e55d4b3c Scott Ullrich
		$isfirst = true;
53
		foreach($resolved as $re) {
54 7a87cb97 Scott Ullrich
			if($re <> "") {
55
				if(!$isfirst) 
56
					$addresses .= " ";
57
				$addresses .= $re . "/32";
58
				$isfirst = false;
59
			}
60 e55d4b3c Scott Ullrich
		}
61
		$newalias = array();
62
		$aliasname = str_replace(array(".","-"), "_", $host);
63
		$alias_exists = false;
64
		$counter=0;
65
		foreach($a_aliases as $a) {
66
			if($a['name'] == $aliasname) {
67
				$alias_exists = true;
68
				$id=$counter;
69
			}
70
			$counter++;
71
		}
72
		if($override) 
73
			$alias_exists = false;
74
		if($alias_exists == false) {
75 705c72ea Scott Ullrich
			$newalias['name'] = $aliasname;
76 e1f6a0d9 Scott Ullrich
			$newalias['type'] = "network";
77 e55d4b3c Scott Ullrich
			$newalias['address'] = $addresses;
78
			$newalias['descr'] = "Created from Diagnostics-> DNS Lookup";
79
			if($override) 
80
				$a_aliases[$id] = $newalias;
81
			else
82
				$a_aliases[] = $newalias;
83
			write_config();
84
			$createdalias = true;
85
		}
86
	}
87
}
88
89 737ed7d1 jim-p
if ($_POST) {
90
	unset($input_errors);
91
92
	$reqdfields = explode(" ", "host");
93
	$reqdfieldsn = explode(",", "Host");
94
95
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
96 72cd706b bcyrill
	$host = trim($_POST['host'], " \t\n\r\0\x0B[]");
97
	$host_esc = escapeshellarg($host);
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 40af551f jim-p
		$resolvconf_servers = `grep nameserver /etc/resolv.conf | cut -f2 -d' '`;
105 360ed9fa jim-p
		$dns_servers = explode("\n", trim($resolvconf_servers));
106 40af551f jim-p
		foreach ($dns_servers as $dns_server) {
107 b97c5ed6 sullrich
			$query_time = `dig {$host_esc} @{$dns_server} | grep Query | 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 77f87165 sullrich
			$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 5aaae0e3 Scott Ullrich
				$dig=`dig $host_esc A | grep $host_esc | grep -v ";" | awk '{ print $5 }'`;
134 cfbfd941 smos
				$resolved = explode("\n", $dig);
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
include("head.inc"); ?>
148
<body link="#000000" vlink="#000000" alink="#000000">
149 a3381369 Colin Fleming
<?php include("fbegin.inc"); ?>
150 737ed7d1 jim-p
<table width="100%" border="0" cellpadding="0" cellspacing="0">
151
        <tr>
152
                <td>
153
<?php if ($input_errors) print_input_errors($input_errors); ?>
154
	<form action="diag_dns.php" method="post" name="iform" id="iform">
155
	  <table width="100%" border="0" cellpadding="6" cellspacing="0">
156 4c7e2f4e sullrich
		<tr>
157 ee91ae30 jean.feltrin
			<td colspan="2" valign="top" class="listtopic"> <?=gettext("Resolve DNS hostname or IP");?></td>
158 4c7e2f4e sullrich
		</tr>
159 737ed7d1 jim-p
        <tr>
160 da652a46 Vinicius Coque
		  <td width="22%" valign="top" class="vncellreq"><?=gettext("Hostname or IP");?></td>
161 737ed7d1 jim-p
		  <td width="78%" class="vtable">
162 a812abb3 Scott Ullrich
            <?=$mandfldhtml;?>
163
			<table>
164
				<tr><td valign="top">
165
			<input name="host" type="text" class="formfld" id="host" size="20" value="<?=htmlspecialchars($host);?>">
166
			</td>
167
			<td>
168 a3381369 Colin Fleming
			<?php if ($resolved && $type) { ?>
169 a812abb3 Scott Ullrich
			=  <font size="+1">
170
<?php
171 3a6dc294 Scott Ullrich
				$found = 0;
172 a812abb3 Scott Ullrich
				if(is_array($resolved)) { 
173
					foreach($resolved as $hostitem) {
174 3a6dc294 Scott Ullrich
						if($hostitem <> "") {
175
							echo $hostitem . "<br/>";
176
							$found++;
177
						}
178 a812abb3 Scott Ullrich
					}
179
				} else {
180
					echo $resolved; 
181
				} 
182 3a6dc294 Scott Ullrich
				if($found > 0) {
183
					if($alias_exists) {
184 0f08affe Scott Ullrich
						echo "<br/><font size='-2'>An alias already exists for the hostname " . htmlspecialchars($host) . ".  To overwrite, click <a href='diag_dns.php?host=" . trim(urlencode(htmlspecialchars($host))) . "&createalias=true&override=true'>here</a>.";
185 3a6dc294 Scott Ullrich
					} else { 
186
						if(!$createdalias) {
187 0f08affe Scott Ullrich
							echo "<br/><font size='-2'><a href='diag_dns.php?host=" . trim(urlencode(htmlspecialchars($host))) . "&createalias=true'>Create alias</a> out of these entries.";
188 3a6dc294 Scott Ullrich
						} else {
189 0f08affe Scott Ullrich
							echo "<br/><font size='-2'>Alias created with name " . htmlspecialchars($newalias['name']);
190 3a6dc294 Scott Ullrich
						}
191 e55d4b3c Scott Ullrich
					}
192
				}
193 a812abb3 Scott Ullrich
?>
194
				<font size="-1>">
195 e55d4b3c Scott Ullrich
196 737ed7d1 jim-p
			<?	} ?>
197 a812abb3 Scott Ullrich
			</td></tr></table>
198 737ed7d1 jim-p
		  </td>
199
		</tr>
200 37d98ce7 sullrich
<?php		if($_POST): ?>
201 86ab47ff sullrich
		<tr>
202 ee91ae30 jean.feltrin
		  <td width="22%" valign="top" class="vncell"><?=gettext("Resolution time per server");?></td>
203 86ab47ff sullrich
		  <td width="78%" class="vtable">
204 87fa30ba sullrich
				<table width="170" border="1" cellpadding="2" style="border-width: 1px 1px 1px 1px; border-collapse: collapse;">
205 86ab47ff sullrich
					<tr>
206
						<td>
207 ee91ae30 jean.feltrin
							<b><?=gettext("Server");?></b>
208 86ab47ff sullrich
						</td>
209
						<td>
210 da652a46 Vinicius Coque
							<b><?=gettext("Query time");?></b>
211 86ab47ff sullrich
						</td>
212
					</tr>
213
<?php
214
					if(is_array($dns_speeds)) 
215
						foreach($dns_speeds as $qt):
216
?>
217
					<tr>
218
						<td>
219
							<?=$qt['dns_server']?>
220
						</td>
221
						<td>
222
							<?=$qt['query_time']?>
223
						</td>
224
					</tr>
225
<?php
226
					endforeach;
227
?>
228
				</table>
229
		  </td>
230
		</tr>
231 37d98ce7 sullrich
		<?php endif; ?>
232 2312b0eb jim-p
		<?php if (!$input_errors && $ipaddr) { ?>
233
		<tr>
234 ee91ae30 jean.feltrin
			<td width="22%" valign="top"  class="vncell"><?=gettext("More Information:");?></td>
235 2312b0eb jim-p
			<td width="78%" class="vtable">
236 0f08affe Scott Ullrich
				<a target="_new" href ="/diag_ping.php?host=<?=htmlspecialchars($host)?>&interface=wan&count=3"><?=gettext("Ping");?></a> <br/>
237
				<a target="_new" href ="/diag_traceroute.php?host=<?=htmlspecialchars($host)?>&ttl=18"><?=gettext("Traceroute");?></a>
238 b8cc74ed sullrich
				<p/>
239 ee91ae30 jean.feltrin
				<?=gettext("NOTE: The following links are to external services, so their reliability cannot be guaranteed.");?><br/><br/>
240
				<a target="_new" href="http://private.dnsstuff.com/tools/whois.ch?ip=<?php echo $ipaddr; ?>"><?=gettext("IP WHOIS @ DNS Stuff");?></a><br />
241 09128488 Vinicius Coque
				<a target="_new" href="http://private.dnsstuff.com/tools/ipall.ch?ip=<?php echo $ipaddr; ?>"><?=gettext("IP Info @ DNS Stuff");?></a>
242 2312b0eb jim-p
			</td>
243
		</tr>
244
		<?php } ?>
245 737ed7d1 jim-p
		<tr>
246
		  <td width="22%" valign="top">&nbsp;</td>
247
		  <td width="78%">
248 b97c5ed6 sullrich
			<br/>&nbsp;
249 ee91ae30 jean.feltrin
            <input name="Submit" type="submit" class="formbtn" value="<?=gettext("DNS Lookup");?>">
250 737ed7d1 jim-p
		</td>
251
		</tr>
252
	</table>
253
</form>
254
</td></tr></table>
255
<?php include("fend.inc"); ?>