Projet

Général

Profil

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

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

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
if( ($_POST['host']) && ($_POST['dialog_output']) ) {
144
	display_host_results ($host,$resolved,$dns_speeds);
145
	exit;
146
}
147

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

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

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