<?php

# ============================================================== 
# tellme: get all the lowdown details on that address ------ 1.2 
#
# 8. Legal ----------------------------------------------------- 
#
#   Copyright (c) 1999 Stephen Cope
#
#   This program is free software; you can redistribute it
#   and/or modify it under the terms of the GNU GPL.
#
#   This program is distributed in the hope that it will be
#   useful, but without any warranty; without even the implied
#   warranty of merchantability or fitness for a particular
#   purpose. See the GNU General Public License for more
#   details. 
#
#   You should not have received a copy of the license referred
#   to here as you probably already have one. If you have not
#   got a copy, or have questions about tellme licensing,
#   please contact project-tellme@kimihia.org.nz .
#
#   p.s. keep the files 'tellme.txt' and 'tellme.php3'
#   together or they'll get lonely.
#

#   see the file 'tellme.txt' in this directory for more
#   detailed information

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
 <title>tellme: get all the lowdown details on that address</title>
</head>
<body>

<h2><tt>tellme 1.2</tt>: get all the lowdown details on that address</h2>

<?php

# ============================================================== 
#
#   From the Manual, some handy function -----------------------
#
#   gethostbyaddr - Get the Internet host name corresponding to
#     a given IP address.
#   gethostbyname - Get the IP address corresponding to a given
#     Internet host name.
#   gethostbynamel - Get a list of IP addresses corresponding to
#     a given Internet host name.
#   checkdnsrr - Check DNS records corresponding to a given
#     Internet host name or IP address.
#   getmxrr(string hostname, array mxhosts) - Get MX records
#     corresponding to a given Internet host name

# ============================================================== 
#
#   Logic? What logic? -----------------------------------------
#
#   I guess there should be *some* documentation regarding the
#   precedence and meaning of q_IP and q_Host.
#
#   I tossed this together late at night and it is all a bit
#   mixed up. She'll be right. Here's what I understand from
#   reading my code.
#
#   Either an IP address (q_IP) or host name (q_Host) is passed
#   to the script.
#   If neither q_IP or q_Host is specified, we have a moron in
#   the house, and q_Host becomes 'www.php.net'.
#
#   In the case of both q_IP and q_Host being specified, we biff
#   q_IP.
#
#   Use q_This when extending this, unless you need a specific
#   q_Host or q_IP, in which case you should test their
#   existence before using them.
#
#   our_IP and our_Host are just one-off incidents.


# ============================================================== 
#
#   Define some functions for the output

function render_Open($name) { print '
<table border=1 width="100%" cellspacing=0 cellpadding=4 bordercolor=black>
 <tr>
  <td width="15%" style="background-color: gray; vertical-align: top; font-weight: bold">'.$name.'</td>
  <td width="85%"><pre>'; }
function render_Close() { print '</pre>
  </td>
 </tr>
</table><br>'; }

# ============================================================== 
#
#   De-moronify ------------------------------------------------
if ( !$q_IP && !$q_Host )
 $q_Host = "www.php.net";

if ( $q_IP && $q_Host )
 unset($q_IP);

#   now either $q_IP or q_Host contains a value

?><p><tt>tellme</tt> is examining the <?php

if ( $q_IP ) {
 $q_This = $q_IP;
 ?> IP address <?php
}
else {
 $q_This = $q_Host;
 ?> Host named <?php
}
 print '<a href="http://'.$q_This.'/">'.$q_This.'</a>';

# ============================================================== 
#
#   Maintain options into links --------------------------------

$opt = "";
if ( $o_DNS    ) $opt .= "&o_DNS=on";
if ( $o_WhoIs  ) $opt .= "&o_WhoIs=on";
if ( $o_TraceRT) $opt .= "&o_TraceRT=on";
if ( $o_Server ) $opt .= "&o_Server=on";
if ( $o_Head   ) $opt .= "&o_Head=on";

# ============================================================== 
#
#   Module::a - Forward DNS: Retrieve list of corresponding IP
#   addresses

if ( $q_Host ) {
 render_Open("Forward DNS");
 $our_IP = gethostbynamel($q_Host);

 for ( ; list($nul, $data) = each($our_IP) ; )
  print '<a href="tellme.php3?q_IP='.$data.$opt.'">'.$data."</a>\n";
 render_Close();
}

# ============================================================== 
#
#   Module::b - Reverse DNS: Retrieve a host name

if ( $q_IP ) {
 render_Open("Reverse DNS");
 $our_Host = gethostbyaddr($q_IP);

 print '<a href="tellme.php3?q_Host='.$our_Host.$opt.'">'.$our_Host."</a>";
 render_Close();
}

# ============================================================== 
#
#   Module::c - Check DNS: See what records exist

if ( $o_DNS ) {
 render_Open("Check DNS");
 if ( checkdnsrr($q_This, "A")     ) print "A found\n";
 if ( checkdnsrr($q_This, "MX")    ) {
   print "MX found\n";
   if ( getmxrr( $q_This, &$our_Hosts, &$our_Weights) )
     for ( ; next($our_Hosts) ; next($our_Weights) )
      print ' &#0149; <a href="tellme.php3?q_Host=' . current($our_Hosts) . $opt . '">'
        . current($our_Hosts) . ' - ' . $current($our_Weights) . "\n";
 }
 if ( checkdnsrr($q_This, "NS")    ) print "NS found\n";
 if ( checkdnsrr($q_This, "SOA")   ) print "SOA found\n";
 if ( checkdnsrr($q_This, "PTR")   ) print "PTR found\n";
 if ( checkdnsrr($q_This, "CNAME") ) print "CNAME found";
 render_Close();
}

# ============================================================== 
#
#   Module::d - WHOIS: Passthru to whois()
#
#   If your confuser lacks the whois() system call, then coming
#   soon is the capability to extract the result from:
#   http://www.networksolutions.com/cgi-bin/whois/whois/
#   http://www.networksolutions.com/cgi-bin/whois/whois/whois?host+209.50.247.140

if ( $o_WhoIs ) {
 render_Open("WHOIS");
 if ( $q_Host )
  passthru(EscapeShellCmd("whois " . $q_This));
 else
  print 'we need a host name - use "block owner" for ip addresses';
 render_Close();
}

# ============================================================== 
#
#   Module::e - Trace route: Passthru to tracert()
#
#   This can be potentially lengthy, and your script may time
#   out. I would advise that you pass an argument limiting the
#   time / hops that the tracert continues to. Put these in the
#   empty air quotes.

if ( $o_TraceRT ) {
 render_Open("Trace route");
 passthru(EscapeShellCmd("tracert " . $q_This . " "));
 render_Close();
}

# ============================================================== 
#
#   Module::f - Block owner: Retrieve from
#   http://ipindex.dragonstar.net/

# class a: 0.x.x.x to 127.x.x.x       ---> /a/indexa.html
# class b: 128.0.x.x to 191.255.x.x   ---> /b/154.html
# class c: 192.0.0.x to 223.255.255.x ---> /c/209/209.html

if ( $o_Block ) {
 render_Open("Block owner");

 if ( $q_IP ) {
  $our_Block = 'c';
  $our_Pieces = explode($q_IP);

  print '<a href="http://ipindex.dragonstar.net/' .
   $our_Block .
   $our_Left . '/' .
   $our_Left . '_' . $our_Next .
   '.html">ipindex.dragonstar.net</a>';
 } else {
  print 'needs an IP address';
 }
 render_Close();
}

# ============================================================== 
#
#   Module::g - Server: Do a HEAD and read the 'Server:' line

if ( $o_Server ) {
 render_Open("Server");
 $fp = fsockopen($q_This, 80, &$errno, &$errstr, 20);

 if(!$fp) {
  print "&#0149; <strong>Died while attempting connection</strong>\n";
  print "&#0149; host: '$q_This'\n&#0149; port: '80'\n&#0149; roar: '$errstr' ($errno)\n";
 } else {
  fputs($fp, "HEAD / HTTP/1.1\r\nHost: $q_This\r\nUser-Agent: TellMe/1.2 PHP/" . phpversion() . "\r\n\r\n");
  $tx = "";
  while(!feof($fp))
   $tx .= fgets($fp, 128);

  $tx_p = explode("\n", $tx);
  for ( ; list($nul, $data) = each($tx_p) ; )
   if ( ereg("Server:", $data) )
    echo substr( $data, 8, strlen($data)-8 );

  fclose($fp);
 }
 print "\n[ <a href=\"http://www.netcraft.com/whats/?host=$q_This\">More from Netcraft</a> ]";
 render_Close();
}

# ============================================================== 
#
#   Module::h - HEAD: Code from sockhead

if ( $o_Head ) {
 render_Open("HEAD");
 $fp = fsockopen($q_This, 80, &$errno, &$errstr, 20);

 if(!$fp) {
  print "&#0149; <strong>Died while attempting connection</strong>\n";
  print "&#0149; host: '$q_This'\n&#0149; port: '80'\n&#0149; roar: '$errstr' ($errno)\n";
 } else {
  fputs($fp, "HEAD / HTTP/1.1\r\nHost: $q_This\r\nUser-Agent: TellMe/1.2 PHP/". phpversion() ."\r\n\r\n");
  while(!feof($fp))
   echo fgets($fp, 128);
  fclose($fp);
 }
 render_Close();
}

# ============================================================== 

?>

<hr>
<form action="tellme.php3" method=get>
<pre>
----------------------------- Address
IP address : <input name="q_IP"<?php if ( isset($q_IP) ) print ' value="'.$q_IP.'"'; ?>>
Host name  : <input name="q_Host"<?php if ( isset($q_Host) ) print ' value="'.$q_Host.'"'; ?>>
----------------------------- Modules
Forward DNS: Yes
Reverse DNS: Yes
Check DNS  : <input type="checkbox" name="o_DNS"<?php     if ($o_DNS) print " checked" ?>>
WHOIS      : <input type="checkbox" name="o_WhoIs"<?php   if ($o_WhoIs) print " checked" ?>>
Trace route: <input type="checkbox" name="o_TraceRT"<?php if ($o_TraceRT) print " checked" ?>>
Block owner: <input type="checkbox" name="o_Block"<?php   if ($o_Block) print " checked" ?>>
Server     : <input type="checkbox" name="o_Server"<?php  if ($o_Server) print " checked" ?>>
HEAD       : <input type="checkbox" name="o_Head"<?php    if ($o_Head) print " checked" ?>>
----------------------------- Submit
<input type="submit" value="Submit to the state!">
----------------------------- </pre>
</form>

</body>
</html>


