Projet

Général

Profil

Télécharger (8,57 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / usr / local / www / diag_dns.php @ ee4ba9fb

1
<?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
/*
31
	pfSense_MODULE:	dns
32
*/
33

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

    
37
/* Cheap hack to support both $_GET and $_POST */
38
if ($_GET['host'])
39
	$_POST = $_GET;
40

    
41
$host = trim($_POST['host'], " \t\n\r\0\x0B[];\"'");
42
$host_esc = escapeshellarg($host);
43

    
44
if($_GET['createalias'] == "true" && (is_hostname($host) || is_ipaddr($host))) {
45
	if($_GET['override'])
46
		$override = true;
47
	$a_aliases = &$config['aliases']['alias'];
48
	$type = "hostname";
49
	$resolved = gethostbyname($host);
50
	if($resolved) {
51
		$dig=`dig "{$host_esc}" A | grep "{$host_esc}" | grep -v ";" | awk '{ print $5 }'`;
52
		$resolved = explode("\n", $dig);
53
		$isfirst = true;
54
		foreach($resolved as $re) {
55
			if($re <> "") {
56
				if(!$isfirst) 
57
					$addresses .= " ";
58
				$addresses .= $re . "/32";
59
				$isfirst = false;
60
			}
61
		}
62
		$newalias = array();
63
		$aliasname = str_replace(array(".","-"), "_", $host);
64
		$alias_exists = false;
65
		$counter=0;
66
		foreach($a_aliases as $a) {
67
			if($a['name'] == $aliasname) {
68
				$alias_exists = true;
69
				$id=$counter;
70
			}
71
			$counter++;
72
		}
73
		if($override) 
74
			$alias_exists = false;
75
		if($alias_exists == false) {
76
			$newalias['name'] = $aliasname;
77
			$newalias['type'] = "network";
78
			$newalias['address'] = $addresses;
79
			$newalias['descr'] = "Created from Diagnostics-> DNS Lookup";
80
			if($override) 
81
				$a_aliases[$id] = $newalias;
82
			else
83
				$a_aliases[] = $newalias;
84
			write_config();
85
			$createdalias = true;
86
		}
87
	}
88
}
89

    
90
if ($_POST) {
91
	unset($input_errors);
92

    
93
	$reqdfields = explode(" ", "host");
94
	$reqdfieldsn = explode(",", "Host");
95

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

    
117
	$type = "unknown";
118
	$resolved = "";
119
	$ipaddr = "";
120
	$hostname = "";
121
	if (!$input_errors) {
122
		if (is_ipaddr($host)) {
123
			$type = "ip";
124
			$resolved = gethostbyaddr($host);
125
			$ipaddr = $host;
126
			if ($host != $resolved)
127
				$hostname = $resolved;
128
		} elseif (is_hostname($host)) {
129
			$type = "hostname";
130
			$resolved = gethostbyname($host);
131
			if($resolved) {
132
				$dig=`dig {$host_esc} A | grep {$host_esc} | grep -v ";" | awk '{ print $5 }'`;
133
				$resolved = explode("\n", $dig);
134
			}
135
			$hostname = $host;
136
			if ($host != $resolved)
137
				$ipaddr = $resolved[0];
138
		}
139

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

    
146
if( ($_POST['host']) && ($_POST['dialog_output']) ) {
147
	display_host_results ($host,$resolved,$dns_speeds);
148
	exit;
149
}
150

    
151
function display_host_results ($address,$hostname,$dns_speeds) {
152
	$map_lengths = function($element) { return strlen($element[0]); };
153

    
154
	echo gettext("IP Address") . ": {$address} \n";
155
	echo gettext("Host Name") . ": {$hostname} \n";
156
	echo "\n";
157
	$text_table = array();
158
	$text_table[] = array(gettext("Server"), gettext("Query Time"));
159
	if (is_array($dns_speeds)) {
160
		foreach ($dns_speeds as $qt) {
161
			$text_table[] = array(trim($qt['dns_server']), trim($qt['query_time']));
162
		}
163
	}
164
	$col0_padlength = max(array_map($map_lengths, $text_table)) + 4;
165
	foreach ($text_table as $text_row) {
166
		echo str_pad($text_row[0], $col0_padlength) . $text_row[1] . "\n";
167
	}
168
}
169

    
170
include("head.inc"); ?>
171
<body link="#000000" vlink="#000000" alink="#000000">
172
<?php include("fbegin.inc"); ?>
173
<table width="100%" border="0" cellpadding="0" cellspacing="0">
174
        <tr>
175
                <td>
176
<?php if ($input_errors) print_input_errors($input_errors); ?>
177
	<form action="diag_dns.php" method="post" name="iform" id="iform">
178
	  <table width="100%" border="0" cellpadding="6" cellspacing="0">
179
		<tr>
180
			<td colspan="2" valign="top" class="listtopic"> <?=gettext("Resolve DNS hostname or IP");?></td>
181
		</tr>
182
        <tr>
183
		  <td width="22%" valign="top" class="vncellreq"><?=gettext("Hostname or IP");?></td>
184
		  <td width="78%" class="vtable">
185
            <?=$mandfldhtml;?>
186
			<table>
187
				<tr><td valign="top">
188
			<input name="host" type="text" class="formfld" id="host" size="20" value="<?=htmlspecialchars($host);?>">
189
			</td>
190
			<td>
191
			<?php if ($resolved && $type) { ?>
192
			=  <font size="+1">
193
<?php
194
				$found = 0;
195
				if(is_array($resolved)) { 
196
					foreach($resolved as $hostitem) {
197
						if($hostitem <> "") {
198
							echo $hostitem . "<br/>";
199
							$found++;
200
						}
201
					}
202
				} else {
203
					echo $resolved; 
204
				} 
205
				if($found > 0) {
206
					if($alias_exists) {
207
						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>.";
208
					} else { 
209
						if(!$createdalias) {
210
							echo "<br/><font size='-2'><a href='diag_dns.php?host=" . trim(urlencode(htmlspecialchars($host))) . "&createalias=true'>Create alias</a> out of these entries.";
211
						} else {
212
							echo "<br/><font size='-2'>Alias created with name " . htmlspecialchars($newalias['name']);
213
						}
214
					}
215
				}
216
?>
217
				<font size="-1">
218

    
219
			<?	} ?>
220
			</td></tr></table>
221
		  </td>
222
		</tr>
223
<?php		if($_POST): ?>
224
		<tr>
225
		  <td width="22%" valign="top" class="vncell"><?=gettext("Resolution time per server");?></td>
226
		  <td width="78%" class="vtable">
227
				<table width="170" border="1" cellpadding="2" style="border-width: 1px 1px 1px 1px; border-collapse: collapse;">
228
					<tr>
229
						<td>
230
							<b><?=gettext("Server");?></b>
231
						</td>
232
						<td>
233
							<b><?=gettext("Query time");?></b>
234
						</td>
235
					</tr>
236
<?php
237
					if(is_array($dns_speeds)) 
238
						foreach($dns_speeds as $qt):
239
?>
240
					<tr>
241
						<td>
242
							<?=$qt['dns_server']?>
243
						</td>
244
						<td>
245
							<?=$qt['query_time']?>
246
						</td>
247
					</tr>
248
<?php
249
					endforeach;
250
?>
251
				</table>
252
		  </td>
253
		</tr>
254
		<?php endif; ?>
255
		<?php if (!$input_errors && $ipaddr) { ?>
256
		<tr>
257
			<td width="22%" valign="top"  class="vncell"><?=gettext("More Information:");?></td>
258
			<td width="78%" class="vtable">
259
				<a target="_new" href ="/diag_ping.php?host=<?=htmlspecialchars($host)?>&interface=wan&count=3"><?=gettext("Ping");?></a> <br/>
260
				<a target="_new" href ="/diag_traceroute.php?host=<?=htmlspecialchars($host)?>&ttl=18"><?=gettext("Traceroute");?></a>
261
				<p/>
262
				<?=gettext("NOTE: The following links are to external services, so their reliability cannot be guaranteed.");?><br/><br/>
263
				<a target="_new" href="http://private.dnsstuff.com/tools/whois.ch?ip=<?php echo $ipaddr; ?>"><?=gettext("IP WHOIS @ DNS Stuff");?></a><br />
264
				<a target="_new" href="http://private.dnsstuff.com/tools/ipall.ch?ip=<?php echo $ipaddr; ?>"><?=gettext("IP Info @ DNS Stuff");?></a>
265
			</td>
266
		</tr>
267
		<?php } ?>
268
		<tr>
269
		  <td width="22%" valign="top">&nbsp;</td>
270
		  <td width="78%">
271
			<br/>&nbsp;
272
            <input name="Submit" type="submit" class="formbtn" value="<?=gettext("DNS Lookup");?>">
273
		</td>
274
		</tr>
275
	</table>
276
</form>
277
</td></tr></table>
278
<?php include("fend.inc"); ?>
(10-10/246)