Project

General

Profile

Download (8.78 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 (is_array($config['aliases']['alias'])) {
42
	$a_aliases = &$config['aliases']['alias'];
43
} else {
44
	$a_aliases = array();
45
}
46
$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
		$override = true;
60
	$resolved = gethostbyname($host);
61
	$type = "hostname";
62
	if($resolved) {
63
		$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
		$isfirst = true;
66
		foreach($resolved as $re) {
67
			if($re <> "") {
68
				if(!$isfirst) 
69
					$addresses .= " ";
70
				$addresses .= rtrim($re) . "/32";
71
				$isfirst = false;
72
			}
73
		}
74
		$newalias = array();
75
		if($override) 
76
			$alias_exists = false;
77
		if($alias_exists == false) {
78
			$newalias['name'] = $aliasname;
79
			$newalias['type'] = "network";
80
			$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
if ($_POST) {
93
	unset($input_errors);
94

    
95
	$reqdfields = explode(" ", "host");
96
	$reqdfieldsn = explode(",", "Host");
97

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

    
119
	$type = "unknown";
120
	$resolved = "";
121
	$ipaddr = "";
122
	$hostname = "";
123
	if (!$input_errors) {
124
		if (is_ipaddr($host)) {
125
			$type = "ip";
126
			$resolved = gethostbyaddr($host);
127
			$ipaddr = $host;
128
			if ($host != $resolved)
129
				$hostname = $resolved;
130
		} elseif (is_hostname($host)) {
131
			$type = "hostname";
132
			$resolved = gethostbyname($host);
133
			if($resolved) {
134
				$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
			}
137
			$hostname = $host;
138
			if ($host != $resolved)
139
				$ipaddr = $resolved[0];
140
		}
141

    
142
		if ($host == $resolved) {
143
			$resolved = gettext("No record found");
144
		}
145
	}
146
}
147

    
148
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
	$map_lengths = function($element) { return strlen($element[0]); };
155

    
156
	echo gettext("IP Address") . ": {$address} \n";
157
	echo gettext("Host Name") . ": {$hostname} \n";
158
	echo "\n";
159
	$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
		}
165
	}
166
	$col0_padlength = max(array_map($map_lengths, $text_table)) + 4;
167
	foreach ($text_table as $text_row) {
168
		echo str_pad($text_row[0], $col0_padlength) . $text_row[1] . "\n";
169
	}
170
}
171

    
172
include("head.inc"); ?>
173
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
174
<?php include("fbegin.inc"); ?>
175
<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="diag dns">
176
        <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
	  <table width="100%" border="0" cellpadding="6" cellspacing="0" summary="tabcont">
181
		<tr>
182
			<td colspan="2" valign="top" class="listtopic"> <?=gettext("Resolve DNS hostname or IP");?></td>
183
		</tr>
184
        <tr>
185
		  <td width="22%" valign="top" class="vncellreq"><?=gettext("Hostname or IP");?></td>
186
		  <td width="78%" class="vtable">
187
            <?=$mandfldhtml;?>
188
			<table summary="results">
189
				<tr><td valign="top">
190
			<input name="host" type="text" class="formfld unknown" id="host" size="20" value="<?=htmlspecialchars($host);?>" />
191
			</td>
192
			<?php if ($resolved && $type) { ?>
193
			<td valign="middle">&nbsp;=&nbsp;</td><td>
194
			<font size="+1">
195
<?php
196
				$found = 0;
197
				if(is_array($resolved)) { 
198
					foreach($resolved as $hostitem) {
199
						if($hostitem <> "") {
200
							echo $hostitem . "<br />";
201
							$found++;
202
						}
203
					}
204
				} else {
205
					echo $resolved; 
206
				} 
207
				if($found > 0) { ?>
208
					<br/></font><font size='-2'>
209
				<?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
					}
220
				}
221
?>
222

    
223
			<?php } ?>
224
			</font></td></tr></table>
225
		  </td>
226
		</tr>
227
<?php		if($_POST): ?>
228
		<tr>
229
		  <td width="22%" valign="top" class="vncell"><?=gettext("Resolution time per server");?></td>
230
		  <td width="78%" class="vtable">
231
				<table width="170" border="0" cellpadding="6" cellspacing="0" summary="resolution time">
232
					<tr>
233
						<td class="listhdrr">
234
							<?=gettext("Server");?>
235
						</td>
236
						<td class="listhdrr">
237
							<?=gettext("Query time");?>
238
						</td>
239
					</tr>
240
<?php
241
					if(is_array($dns_speeds)) 
242
						foreach($dns_speeds as $qt):
243
?>
244
					<tr>
245
						<td class="listlr">
246
							<?=$qt['dns_server']?>
247
						</td>
248
						<td class="listr">
249
							<?=$qt['query_time']?>
250
						</td>
251
					</tr>
252
<?php
253
					endforeach;
254
?>
255
				</table>
256
		  </td>
257
		</tr>
258
		<?php endif; ?>
259
		<?php if (!$input_errors && $ipaddr) { ?>
260
		<tr>
261
			<td width="22%" valign="top"  class="vncell"><?=gettext("More Information:");?></td>
262
			<td width="78%" class="vtable">
263
				<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
				<?=gettext("NOTE: The following links are to external services, so their reliability cannot be guaranteed.");?><br /><br />
267
				<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
			</td>
271
		</tr>
272
		<?php } ?>
273
		<tr>
274
		  <td width="22%" valign="top">&nbsp;</td>
275
		  <td width="78%">
276
			<br />&nbsp;
277
            <input name="Submit" type="submit" class="formbtn" value="<?=gettext("DNS Lookup");?>" />
278
		</td>
279
		</tr>
280
	</table>
281
</form>
282
</td></tr></table>
283
<?php include("fend.inc"); ?>
284
</body>
285
</html>
(10-10/256)