Commit a7e43fcc authored by Ad Schellevis's avatar Ad Schellevis

(legacy) refactor diag_dns.php

parent 55743152
<?php <?php
/* /*
Copyright (C) 2014 Deciso B.V. Copyright (C) 2016 Deciso B.V.
Copyright (C) 2009 Jim Pingle (jpingle@gmail.com) Copyright (C) 2009 Jim Pingle (jpingle@gmail.com)
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, 1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer. this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright 2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution. documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE. POSSIBILITY OF SUCH DAMAGE.
*/ */
require_once("guiconfig.inc"); require_once("guiconfig.inc");
$host = trim($_REQUEST['host'], " \t\n\r\0\x0B[];\"'"); $resolved = array();
$host_esc = escapeshellarg($host); $dns_speeds = array();
if (!empty($_REQUEST['host'])) {
if (isset($config['aliases']['alias']) && is_array($config['aliases']['alias'])) { $host = trim($_REQUEST['host'], " \t\n\r\0\x0B[];\"'");
$a_aliases = &$config['aliases']['alias']; $host_esc = escapeshellarg($host);
} else {
$a_aliases = array(); if (!is_hostname($host) && !is_ipaddr($host)) {
} $input_errors[] = gettext("Host must be a valid hostname or IP address.");
$aliasname = str_replace(array(".","-"), "_", $host); } else {
$alias_exists = false; // Test resolution speed of each DNS server.
$counter=0; $dns_servers = array();
foreach($a_aliases as $a) { exec("/usr/bin/grep nameserver /etc/resolv.conf | /usr/bin/cut -f2 -d' '", $dns_servers);
if($a['name'] == $aliasname) { foreach ($dns_servers as $dns_server) {
$alias_exists = true; $query_time = exec("/usr/bin/drill {$host_esc} " . escapeshellarg("@" . trim($dns_server)) . " | /usr/bin/grep Query | /usr/bin/cut -d':' -f2");
$id=$counter; if ($query_time == "") {
} $query_time = gettext("No response");
$counter++; }
} $dns_speeds[] = array('dns_server' => $dns_server, 'query_time' => $query_time);
}
if( ($_GET['host']) && ($_GET['dialog_output']) ) { }
display_host_results($host,$resolved,$dns_speeds);
exit; $ipaddr = "";
} if (count($input_errors) == 0) {
if (is_ipaddr($host)) {
if(isset($_POST['create_alias']) && (is_hostname($host) || is_ipaddr($host))) { $resolved[] = " " . gethostbyaddr($host); // add a space to provide an empty type field
if($_POST['override']) $ipaddr = $host;
$override = true; } elseif (is_hostname($host)) {
$resolved = gethostbyname($host); exec("/usr/bin/drill {$host_esc} A | /usr/bin/grep 'IN' | /usr/bin/grep -v ';' | /usr/bin/awk '{ print $4 \" \" $5 }'", $resolved);
$type = "hostname"; $ipaddr = explode(" ", $resolved[count($resolved)-1])[1];
if($resolved) { }
$resolved = array(); }
exec("/usr/bin/drill {$host_esc} A | /usr/bin/grep {$host_esc} | /usr/bin/grep -v ';' | /usr/bin/awk '{ print $5 }'", $resolved);
$isfirst = true;
foreach($resolved as $re) {
if($re <> "") {
if(!$isfirst)
$addresses .= " ";
$addresses .= rtrim($re) . "/32";
$isfirst = false;
}
}
$newalias = array();
if($override)
$alias_exists = false;
if($alias_exists == false) {
$newalias['name'] = $aliasname;
$newalias['type'] = "network";
$newalias['address'] = $addresses;
$newalias['descr'] = gettext('Created from Diagnostics-> DNS Lookup');
if($override)
$a_aliases[$id] = $newalias;
else
$a_aliases[] = $newalias;
write_config();
$createdalias = true;
}
}
}
if ($_POST) {
unset($input_errors);
$reqdfields = explode(" ", "host");
$reqdfieldsn = explode(",", "Host");
do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
if (!is_hostname($host) && !is_ipaddr($host)) {
$input_errors[] = gettext("Host must be a valid hostname or IP address.");
} else {
// Test resolution speed of each DNS server.
$dns_speeds = array();
$dns_servers = array();
exec("/usr/bin/grep nameserver /etc/resolv.conf | /usr/bin/cut -f2 -d' '", $dns_servers);
foreach ($dns_servers as $dns_server) {
$query_time = exec("/usr/bin/drill {$host_esc} " . escapeshellarg("@" . trim($dns_server)) . " | /usr/bin/grep Query | /usr/bin/cut -d':' -f2");
if($query_time == "")
$query_time = gettext("No response");
$new_qt = array();
$new_qt['dns_server'] = $dns_server;
$new_qt['query_time'] = $query_time;
$dns_speeds[] = $new_qt;
unset($new_qt);
}
}
$type = "unknown";
$resolved = "";
$ipaddr = "";
$hostname = "";
if (!$input_errors) {
if (is_ipaddr($host)) {
$type = "ip";
$resolved = gethostbyaddr($host);
$ipaddr = $host;
if ($host != $resolved)
$hostname = $resolved;
} elseif (is_hostname($host)) {
$type = "hostname";
$resolved = gethostbyname($host);
if($resolved) {
$resolved = array();
exec("/usr/bin/drill {$host_esc} A | /usr/bin/grep {$host_esc} | /usr/bin/grep -v ';' | /usr/bin/awk '{ print $5 }'", $resolved);
}
$hostname = $host;
if ($host != $resolved)
$ipaddr = $resolved[0];
}
if ($host == $resolved) {
$resolved = gettext("No record found");
}
}
}
function display_host_results ($address,$hostname,$dns_speeds) {
$map_lengths = function($element) { return strlen($element[0]); };
echo gettext("IP Address") . ": {$address} \n";
echo gettext("Host Name") . ": {$hostname} \n";
echo "\n";
$text_table = array();
$text_table[] = array(gettext("Server"), gettext("Query Time"));
if (is_array($dns_speeds)) {
foreach ($dns_speeds as $qt) {
$text_table[] = array(trim($qt['dns_server']), trim($qt['query_time']));
}
}
$col0_padlength = max(array_map($map_lengths, $text_table)) + 4;
foreach ($text_table as $text_row) {
echo str_pad($text_row[0], $col0_padlength) . $text_row[1] . "\n";
}
} }
include("head.inc"); ?> include("head.inc"); ?>
<body> <body>
<?php include("fbegin.inc"); ?> <?php include("fbegin.inc"); ?>
<section class="page-content-main"> <section class="page-content-main">
<div class="container-fluid"> <div class="container-fluid">
<div class="row"> <div class="row">
<form method="post" name="iform" id="iform">
<form action="<?=$_SERVER['REQUEST_URI'];?>" method="post" name="iform" id="iform"> <section class="col-xs-12">
<section class="col-xs-12"> <div class="content-box">
<div class="content-box"> <?php if (isset($input_errors) && count($input_errors) > 0) print_input_errors($input_errors); ?>
<header class="content-box-head container-fluid">
<?php if (isset($input_errors) && count($input_errors) > 0) print_input_errors($input_errors); ?> <h3><?=gettext("Resolve DNS hostname or IP");?></h3>
</header>
<header class="content-box-head container-fluid"> <div class="table-responsive">
<h3><?=gettext("Resolve DNS hostname or IP");?></h3> <table class="table table-striped">
</header> <tbody>
<tr>
<div class="content-box-main"> <td><?=gettext("Hostname or IP");?></td>
<div class="table-responsive"> <td>
<table class="table table-striped"> <input name="host" type="text" value="<?=htmlspecialchars($host);?>" />
<tbody> </td>
<tr> </tr>
<td><?=gettext("Hostname or IP");?></td> <?php
<td><input name="host" type="text" class="form-control" id="host" size="20" value="<?=htmlspecialchars($host);?>" /></td> if (count($resolved) > 0):?>
<?php if ($resolved && $type): ?> <tr>
<td> <td><?=gettext("Response");?></td>
<?php <td>
$found = 0; <table class="table table-striped table-condensed">
if(is_array($resolved)) { <tr>
foreach($resolved as $hostitem) { <th><?=gettext("Type");?></th>
if($hostitem <> "") { <th><?=gettext("Address");?></th>
echo $hostitem . "<br />"; </tr>
$found++; <?php
} foreach($resolved as $hostitem):?>
} <tr>
} else { <td><?=explode(' ',$hostitem)[0];?></td>
echo $resolved; <td><?=explode(' ',$hostitem)[1];?></td>
} </tr>
?> <?php
</td> endforeach;?>
<td> </table>
<?php if($found > 0): ?> </td>
<?php if($alias_exists): ?> </tr>
<?= sprintf(gettext('An alias already exists for the hostname %s.'), htmlspecialchars($host)) ?>. <br /> <tr>
<input type="hidden" name="override" value="true"/> <td><?=gettext("Resolution time per server");?></td>
<input type="submit" class="btn btn-default" name="create_alias" value="Overwrite Alias"/> <td colspan="2">
<?php else: ?> <table class="table table-striped table-condensed">
<?php if(!$createdalias): ?> <tr>
<input type="submit" class="btn btn-default" name="create_alias" value="Create Alias"/> <th><?=gettext("Server");?></th>
<?php else: ?> <th><?=gettext("Query time");?></th>
<?= sprintf(gettext('Alias created with name %s.'), htmlspecialchars($newalias['name'])) ?> </tr>
<?php endif; endif; endif; ?>
</td>
<?php else: ?>
<td></td><td></td>
<?php endif; ?>
</tr>
<?php if (!empty($_POST)): ?>
<tr>
<td><?=gettext("Resolution time per server");?></td>
<td colspan="3">
<table class="table table-striped"> <?php
<tr> foreach($dns_speeds as $qt): ?>
<th><?=gettext("Server");?></th> <tr>
<th><?=gettext("Query time");?></th> <td><?=$qt['dns_server']?></td>
</tr> <td><?=$qt['query_time']?></td>
<?php if(is_array($dns_speeds)): foreach($dns_speeds as $qt): ?> </tr>
<tr> <?php
<td> endforeach; ?>
<?=$qt['dns_server']?> </table>
</td> </td>
<td> </tr>
<?=$qt['query_time']?> <?php
</td> endif;
</tr> if (!empty($ipaddr)):?>
<?php endforeach; endif; ?> <tr>
</table> <td><?=gettext("More Information:");?></td>
</td> <td>
</tr> <a href ="/diag_ping.php?host=<?=htmlspecialchars($host)?>&amp;interface=wan&amp;count=3"><?=gettext("Ping");?></a> <br />
<?php endif; ?> <a href ="/diag_traceroute.php?host=<?=htmlspecialchars($host)?>&amp;ttl=18"><?=gettext("Traceroute");?></a>
<?php if (!$input_errors && $ipaddr): ?> <p>
<tr> <?=gettext("NOTE: The following links are to external services, so their reliability cannot be guaranteed.");?><br /><br />
<td><?=gettext("More Information:");?></td> <a target="_blank" href="http://private.dnsstuff.com/tools/whois.ch?ip=<?=$ipaddr; ?>"><?=gettext("IP WHOIS @ DNS Stuff");?></a><br />
<td colspan="3"><a href ="/diag_ping.php?host=<?=htmlspecialchars($host)?>&amp;interface=wan&amp;count=3"><?=gettext("Ping");?></a> <br /> <a target="_blank" href="http://private.dnsstuff.com/tools/ipall.ch?ip=<?=$ipaddr; ?>"><?=gettext("IP Info @ DNS Stuff");?></a>
<a href ="/diag_traceroute.php?host=<?=htmlspecialchars($host)?>&amp;ttl=18"><?=gettext("Traceroute");?></a> </p>
<p> </td>
<?=gettext("NOTE: The following links are to external services, so their reliability cannot be guaranteed.");?><br /><br /> </tr>
<a target="_blank" href="http://private.dnsstuff.com/tools/whois.ch?ip=<?php echo $ipaddr; ?>"><?=gettext("IP WHOIS @ DNS Stuff");?></a><br /> <?php
<a target="_blank" href="http://private.dnsstuff.com/tools/ipall.ch?ip=<?php echo $ipaddr; ?>"><?=gettext("IP Info @ DNS Stuff");?></a> endif; ?>
</p> </tbody>
</td> <tfoot>
</tr> <tr>
<?php endif; ?> <td></td>
</tbody> <td>
</table> <input type="submit" class="btn btn-primary btn-fixed" value="<?=gettext("DNS Lookup");?>" />
</div> </td>
<div class="container-fluid"> </tr>
<input type="submit" class="btn btn-primary btn-fixed pull-right" value="<?=gettext("DNS Lookup");?>" /> </tfoot>
</div> </table>
</div> </div>
</div> </div>
</section> </section>
</form> </form>
</div> </div>
</div> </div>
</section> </section>
<?php include("foot.inc"); ?> <?php include("foot.inc"); ?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment