Project

General

Profile

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

    
5
	Copyright (C) 2009 Jim Pingle (jpingle@gmail.com)
6
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
7
	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
/*
32
	pfSense_MODULE:	dns
33
*/
34

    
35
$pgtitle = array(gettext("Diagnostics"),gettext("DNS Lookup"));
36
require("guiconfig.inc");
37

    
38
$host = trim($_REQUEST['host'], " \t\n\r\0\x0B[];\"'");
39
$host_esc = escapeshellarg($host);
40

    
41
/* If this section of config.xml has not been populated yet we need to set it up
42
*/
43
if (!is_array($config['aliases']['alias'])) {
44
	$config['aliases']['alias'] = array();
45
}
46
$a_aliases = &$config['aliases']['alias'];
47

    
48
$aliasname = str_replace(array(".","-"), "_", $host);
49
$alias_exists = false;
50
$counter=0;
51
foreach ($a_aliases as $a) {
52
	if ($a['name'] == $aliasname) {
53
		$alias_exists = true;
54
		$id=$counter;
55
	}
56
	$counter++;
57
}
58

    
59
if (isset($_POST['create_alias']) && (is_hostname($host) || is_ipaddr($host))) {
60
	if ($_POST['override']) {
61
		$override = true;
62
	}
63
	$resolved = gethostbyname($host);
64
	$type = "hostname";
65
	if ($resolved) {
66
		$resolved = array();
67
		exec("/usr/bin/drill {$host_esc} A | /usr/bin/grep {$host_esc} | /usr/bin/grep -v ';' | /usr/bin/awk '{ print $5 }'", $resolved);
68
		$isfirst = true;
69
		foreach ($resolved as $re) {
70
			if ($re <> "") {
71
				if (!$isfirst) {
72
					$addresses .= " ";
73
				}
74
				$addresses .= rtrim($re) . "/32";
75
				$isfirst = false;
76
			}
77
		}
78
		$newalias = array();
79
		if ($override) {
80
			$alias_exists = false;
81
		}
82
		if ($alias_exists == false) {
83
			$newalias['name'] = $aliasname;
84
			$newalias['type'] = "network";
85
			$newalias['address'] = $addresses;
86
			$newalias['descr'] = "Created from Diagnostics-> DNS Lookup";
87
			if ($override) {
88
				$a_aliases[$id] = $newalias;
89
			} else {
90
				$a_aliases[] = $newalias;
91
			}
92
			write_config();
93
			$createdalias = true;
94
		}
95
	}
96
}
97

    
98
if ($_POST) {
99
	unset($input_errors);
100

    
101
	$reqdfields = explode(" ", "host");
102
	$reqdfieldsn = explode(",", "Host");
103

    
104
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
105

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

    
126
	$type = "unknown";
127
	$resolved = "";
128
	$ipaddr = "";
129
	$hostname = "";
130
	if (!$input_errors) {
131
		if (is_ipaddr($host)) {
132
			$type = "ip";
133
			$resolved = gethostbyaddr($host);
134
			$ipaddr = $host;
135
			if ($host != $resolved) {
136
				$hostname = $resolved;
137
			}
138
		} elseif (is_hostname($host)) {
139
			$type = "hostname";
140
			$resolved = gethostbyname($host);
141
			if ($resolved) {
142
				$resolved = array();
143
				exec("/usr/bin/drill {$host_esc} A | /usr/bin/grep {$host_esc} | /usr/bin/grep -v ';' | /usr/bin/awk '{ print $5 }'", $resolved);
144
			}
145
			$hostname = $host;
146
			if ($host != $resolved) {
147
				$ipaddr = $resolved[0];
148
			}
149
		}
150

    
151
		if ($host == $resolved) {
152
			$resolved = gettext("No record found");
153
		}
154
	}
155
}
156

    
157
if (($_POST['host']) && ($_POST['dialog_output'])) {
158
	display_host_results ($host,$resolved,$dns_speeds);
159
	exit;
160
}
161

    
162
function display_host_results ($address,$hostname,$dns_speeds) {
163
	$map_lengths = function($element) { return strlen($element[0]); };
164

    
165
	echo gettext("IP Address") . ": {$address} \n";
166
	echo gettext("Host Name") . ": {$hostname} \n";
167
	echo "\n";
168
	$text_table = array();
169
	$text_table[] = array(gettext("Server"), gettext("Query Time"));
170
	if (is_array($dns_speeds)) {
171
		foreach ($dns_speeds as $qt) {
172
			$text_table[] = array(trim($qt['dns_server']), trim($qt['query_time']));
173
		}
174
	}
175
	$col0_padlength = max(array_map($map_lengths, $text_table)) + 4;
176
	foreach ($text_table as $text_row) {
177
		echo str_pad($text_row[0], $col0_padlength) . $text_row[1] . "\n";
178
	}
179
}
180

    
181
include("head.inc"); ?>
182
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
183
<?php include("fbegin.inc"); ?>
184
<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="diag dns">
185
		<tr>
186
				<td>
187
<?php if ($input_errors) print_input_errors($input_errors); ?>
188
	<form action="diag_dns.php" method="post" name="iform" id="iform">
189
	  <table width="100%" border="0" cellpadding="6" cellspacing="0" summary="tabcont">
190
		<tr>
191
			<td colspan="2" valign="top" class="listtopic"> <?=gettext("Resolve DNS hostname or IP");?></td>
192
		</tr>
193
		<tr>
194
		  <td width="22%" valign="top" class="vncellreq"><?=gettext("Hostname or IP");?></td>
195
		  <td width="78%" class="vtable">
196
			<?=$mandfldhtml;?>
197
			<table summary="results">
198
				<tr><td valign="top">
199
			<input name="host" type="text" class="formfld unknown" id="host" size="20" value="<?=htmlspecialchars($host);?>" />
200
			</td>
201
			<?php if ($resolved && $type) { ?>
202
			<td valign="middle">&nbsp;=&nbsp;</td><td>
203
			<font size="+1">
204
<?php
205
				$found = 0;
206
				if (is_array($resolved)) {
207
					foreach ($resolved as $hostitem) {
208
						if ($hostitem <> "") {
209
							echo $hostitem . "<br />";
210
							$found++;
211
						}
212
					}
213
				} else {
214
					echo $resolved;
215
				}
216
				if ($found > 0) { ?>
217
					<br/></font><font size='-2'>
218
				<?php	if ($alias_exists) { ?>
219
							An alias already exists for the hostname <?= htmlspecialchars($host) ?>. <br />
220
							<input type="hidden" name="override" value="true"/>
221
							<input type="submit" name="create_alias" value="Overwrite Alias"/>
222
				<?php	} else {
223
						if (!$createdalias) { ?>
224
							<input type="submit" name="create_alias" value="Create Alias from These Entries"/>
225
					<?php	} else { ?>
226
							Alias created with name <?= htmlspecialchars($newalias['name']) ?>
227
					<?php	}
228
					}
229
				}
230
?>
231

    
232
			<?php } ?>
233
			</font></td></tr></table>
234
		  </td>
235
		</tr>
236
<?php		if ($_POST): ?>
237
		<tr>
238
		  <td width="22%" valign="top" class="vncell"><?=gettext("Resolution time per server");?></td>
239
		  <td width="78%" class="vtable">
240
				<table width="170" border="0" cellpadding="6" cellspacing="0" summary="resolution time">
241
					<tr>
242
						<td class="listhdrr">
243
							<?=gettext("Server");?>
244
						</td>
245
						<td class="listhdrr">
246
							<?=gettext("Query time");?>
247
						</td>
248
					</tr>
249
<?php
250
					if (is_array($dns_speeds)) {
251
						foreach ($dns_speeds as $qt):
252
?>
253
					<tr>
254
						<td class="listlr">
255
							<?=$qt['dns_server']?>
256
						</td>
257
						<td class="listr">
258
							<?=$qt['query_time']?>
259
						</td>
260
					</tr>
261
<?php
262
						endforeach;
263
					}
264
?>
265
				</table>
266
		  </td>
267
		</tr>
268
		<?php endif; ?>
269
		<?php if (!$input_errors && $ipaddr) { ?>
270
		<tr>
271
			<td width="22%" valign="top"  class="vncell"><?=gettext("More Information:");?></td>
272
			<td width="78%" class="vtable">
273
				<a href ="/diag_ping.php?host=<?=htmlspecialchars($host)?>&amp;interface=wan&amp;count=3"><?=gettext("Ping");?></a> <br />
274
				<a href ="/diag_traceroute.php?host=<?=htmlspecialchars($host)?>&amp;ttl=18"><?=gettext("Traceroute");?></a>
275
				<p>
276
				<?=gettext("NOTE: The following links are to external services, so their reliability cannot be guaranteed.");?><br /><br />
277
				<a target="_blank" href="http://private.dnsstuff.com/tools/whois.ch?ip=<?php echo $ipaddr; ?>"><?=gettext("IP WHOIS @ DNS Stuff");?></a><br />
278
				<a target="_blank" href="http://private.dnsstuff.com/tools/ipall.ch?ip=<?php echo $ipaddr; ?>"><?=gettext("IP Info @ DNS Stuff");?></a>
279
				</p>
280
			</td>
281
		</tr>
282
		<?php } ?>
283
		<tr>
284
		  <td width="22%" valign="top">&nbsp;</td>
285
		  <td width="78%">
286
			<br />&nbsp;
287
			<input name="Submit" type="submit" class="formbtn" value="<?=gettext("DNS Lookup");?>" />
288
		</td>
289
		</tr>
290
	</table>
291
</form>
292
</td></tr></table>
293
<?php include("fend.inc"); ?>
294
</body>
295
</html>
(10-10/256)