Projet

Général

Profil

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

univnautes / usr / local / www / diag_dns.php @ 086e76dc

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
if (is_array($config['aliases']['alias'])) {
41
	$a_aliases = &$config['aliases']['alias'];
42
} else {
43
	$a_aliases = array();
44
}
45
$aliasname = str_replace(array(".","-"), "_", $host);
46
$alias_exists = false;
47
$counter=0;
48
foreach($a_aliases as $a) {
49
	if($a['name'] == $aliasname) {
50
		$alias_exists = true;
51
		$id=$counter;
52
	}
53
	$counter++;
54
}
55

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

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

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

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

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

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

    
147
function display_host_results ($address,$hostname,$dns_speeds) {
148
	$map_lengths = function($element) { return strlen($element[0]); };
149

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

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

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