Projet

Général

Profil

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

univnautes / usr / local / www / diag_dns.php @ 8108b423

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
$host = trim($_REQUEST['host'], " \t\n\r\0\x0B[];\"'");
38
$host_esc = escapeshellarg($host);
39

    
40
$a_aliases = &$config['aliases']['alias'];
41
$aliasname = str_replace(array(".","-"), "_", $host);
42
$alias_exists = false;
43
$counter=0;
44
foreach($a_aliases as $a) {
45
	if($a['name'] == $aliasname) {
46
		$alias_exists = true;
47
		$id=$counter;
48
	}
49
	$counter++;
50
}
51

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

    
87
if ($_POST) {
88
	unset($input_errors);
89

    
90
	$reqdfields = explode(" ", "host");
91
	$reqdfieldsn = explode(",", "Host");
92

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

    
114
	$type = "unknown";
115
	$resolved = "";
116
	$ipaddr = "";
117
	$hostname = "";
118
	if (!$input_errors) {
119
		if (is_ipaddr($host)) {
120
			$type = "ip";
121
			$resolved = gethostbyaddr($host);
122
			$ipaddr = $host;
123
			if ($host != $resolved)
124
				$hostname = $resolved;
125
		} elseif (is_hostname($host)) {
126
			$type = "hostname";
127
			$resolved = gethostbyname($host);
128
			if($resolved) {
129
				$resolved = array();
130
				exec("/usr/bin/dig {$host_esc} A | /usr/bin/grep {$host_esc} | /usr/bin/grep -v ';' | /usr/bin/awk '{ print $5 }'", $resolved);
131
			}
132
			$hostname = $host;
133
			if ($host != $resolved)
134
				$ipaddr = $resolved[0];
135
		}
136

    
137
		if ($host == $resolved) {
138
			$resolved = gettext("No record found");
139
		}
140
	}
141
}
142

    
143
function display_host_results ($address,$hostname,$dns_speeds) {
144
	$map_lengths = function($element) { return strlen($element[0]); };
145

    
146
	echo gettext("IP Address") . ": {$address} \n";
147
	echo gettext("Host Name") . ": {$hostname} \n";
148
	echo "\n";
149
	$text_table = array();
150
	$text_table[] = array(gettext("Server"), gettext("Query Time"));
151
	if (is_array($dns_speeds)) {
152
		foreach ($dns_speeds as $qt) {
153
			$text_table[] = array(trim($qt['dns_server']), trim($qt['query_time']));
154
		}
155
	}
156
	$col0_padlength = max(array_map($map_lengths, $text_table)) + 4;
157
	foreach ($text_table as $text_row) {
158
		echo str_pad($text_row[0], $col0_padlength) . $text_row[1] . "\n";
159
	}
160
}
161

    
162
include("head.inc"); ?>
163
<body link="#000000" vlink="#000000" alink="#000000">
164
<?php include("fbegin.inc"); ?>
165
<table width="100%" border="0" cellpadding="0" cellspacing="0">
166
        <tr>
167
                <td>
168
<?php if ($input_errors) print_input_errors($input_errors); ?>
169
	<form action="diag_dns.php" method="post" name="iform" id="iform">
170
	  <table width="100%" border="0" cellpadding="6" cellspacing="0">
171
		<tr>
172
			<td colspan="2" valign="top" class="listtopic"> <?=gettext("Resolve DNS hostname or IP");?></td>
173
		</tr>
174
        <tr>
175
		  <td width="22%" valign="top" class="vncellreq"><?=gettext("Hostname or IP");?></td>
176
		  <td width="78%" class="vtable">
177
            <?=$mandfldhtml;?>
178
			<table>
179
				<tr><td valign="top">
180
			<input name="host" type="text" class="formfld" id="host" size="20" value="<?=htmlspecialchars($host);?>">
181
			</td>
182
			<td>
183
			<?php if ($resolved && $type) { ?>
184
			=  <font size="+1">
185
<?php
186
				$found = 0;
187
				if(is_array($resolved)) { 
188
					foreach($resolved as $hostitem) {
189
						if($hostitem <> "") {
190
							echo $hostitem . "<br/>";
191
							$found++;
192
						}
193
					}
194
				} else {
195
					echo $resolved; 
196
				} 
197
				if($found > 0) { ?>
198
					<br/><font size='-2'>
199
				<?PHP	if($alias_exists) { ?>
200
							An alias already exists for the hostname <?= htmlspecialchars($host) ?>. <br />
201
							<input type="hidden" name="override" value="true"/>
202
							<input type="submit" name="create_alias" value="Overwrite Alias"/>
203
				<?PHP	} else {
204
						if(!$createdalias) { ?>
205
							<input type="submit" name="create_alias" value="Create Alias from These Entries"/>
206
					<?PHP	} else { ?>
207
							Alias created with name <?= htmlspecialchars($newalias['name']) ?>
208
					<?PHP	}
209
					}
210
				}
211
?>
212
				<font size="-1">
213

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