#!/usr/bin/perl
#
# lookup.pl
# Copyright (c) 1996-1999 SurfUtah.Com
# written by Rus Berrett
#
# $Id: lookup.pl,v 4.7 2000/07/18 22:04:54 phil Exp $
#
# check domain name availability (any top level domain)
# 

##############################################################################
# initialization and configuration

use Env qw(PATH); 
require "util.pl";

$PATH .= ":.";

# check_referer("your-domain.name", "another-domain.name);

# where does everything live
$whois = "/usr/bin/whois";
$iwhois = "iwhois.pl";
$nslookup = "/usr/sbin/nslookup";
$domaincodes = "domain-codes";

# check for existence of necessary files
unless (-e "$whois") {
  $errmsg = "Request failed due to improper script configuration.\n";
  $errmsg .= "Cannot find \"$whois\".  Please check your configuration\n";
  $errmsg .= "settings at the top of the $ENV{'SCRIPT_NAME'} file.\n<p>\n";
  return_error("Domain Name Lookup Error: whois not found", $errmsg);
}
unless (-e "$iwhois") {
  $errmsg = "Request failed due to improper script configuration.\n";
  $errmsg .= "Cannot find \"$iwhois\".  Please check your configuration\n";
  $errmsg .= "settings at the top of the $ENV{'SCRIPT_NAME'} file.\n<p>\n";
  return_error("Domain Name Lookup Error: iwhois not found", $errmsg);
}
unless (-e "$nslookup") {
  $errmsg = "Request failed due to improper script configuration.\n";
  $errmsg .= "Cannot find \"$nslookup\".  Please check your configuration\n";
  $errmsg .= "settings at the top of the $ENV{'SCRIPT_NAME'} file.\n<p>\n";
  return_error("Domain Name Lookup Error: nslookup not found", $errmsg);
}
unless (-e "$domaincodes") {
  $errmsg = "Request failed due to improper script configuration.  Cannot\n";
  $errmsg .= "find \"$domaincodes\".  Please check your configuration\n";
  $errmsg .= "settings at the top of the $ENV{'SCRIPT_NAME'} file.\n<p>\n";
  return_error("Domain Name Lookup Error: domain codes not found", $errmsg);
}

##############################################################################
# parse for input

# get the form data
parse_form_data(*array);

##############################################################################
# error checking
#
# required variables:  name
#

if ($array{'name'} eq "") {
  $errmsg = "You failed to provide a value for the domain name.  Please \n";
  $errmsg .= "provide a value for the domain name and resubmit.\n<p>\n";
  return_error("Domain Name Lookup Error: Name Not Provided", $errmsg);
}

##############################################################################
# process the data

# check for valid characters in name
if ($array{'name'} =~ /[^a-zA-Z0-9-\.]/) {
  $array{'name'} =~ s/[^a-zA-Z0-9-\.]//g;
}

# check for valid tld
@subd = split(/\./, $array{'name'});
$tld = $subd[$#subd];
$tld =~ tr/A-Z/a-z/;
$ftld = "";
open(DCF, "$domaincodes");
while (<DCF>) {
  tr/A-Z/a-z/;
  $index = index($_, "\ ");
  $ftld = substr($_, 0, $index);
  if ($ftld eq $tld) {
    last;
  }
  $ftld = "";
}
close(DCF);
if ($ftld eq "") {
  $errmsg = "You failed to provide a valid value for the domain name. \n";
  $errmsg .= "The top level domain you entered, $tld, is not valid.\n";
  $errmsg .= "Provide a valid value for the domain name and resubmit.\n<p>\n";
  return_error("Domain Name Error: Unknown Top Level Domain", $errmsg);
}

# get rid of any useless prefix, braindead check for braindead users
if (($array{'name'} =~ /^www\./) || ($array{'name'} =~ /^ftp\./) ||
    ($array{'name'} =~ /^smtp\./) || ($array{'name'} =~ /^pop\./) ||
    ($array{'name'} =~ /^imap\./) || ($array{'name'} =~ /^ns\./)) {
  $index = index($array{'name'}, "\.");
  $array{'name'} = substr($array{'name'}, $index);
}

# figure out what whois server we need to use
$host = "";
if (($array{'name'} =~ /\.com$/) ||
    ($array{'name'} =~ /\.net$/) ||
    ($array{'name'} =~ /\.org$/)) {
  $command = "$whois -h whois.internic.net \"$array{'name'}\" 2>&1";
  open(SHELL, "$command |");
  while(<SHELL>) {
    s/^\s+//g;
    s/\s+$//g;
    s/\s+/ /g;
    if (/^Whois Server: (.*)/) {
      $host = $1;
      last;
    }
  }
  close(SHELL);
  if ($host eq "") {
    $host = "whois.networksolutions.com";
  }
}

##############################################################################
# send a message back to the client

print_header_info("Domain Name Lookup: $array{'name'}");

# do the whois first
print "<ul><pre>";
if ($host eq "") {
  $command = "$iwhois \"$array{'name'}\" 2>&1";
}
else {
  $command = "$whois -h $host \"$array{'name'}\" 2>&1";
}
open(SHELL, "$command |");
print $_ while(<SHELL>);
close(SHELL);
print "</pre></ul>\n\n";

print "<p><ul><hr></ul><p>\n";

# do the nslookup second
$command = "$nslookup -timeout=2 -q=ns $array{'name'} 2>&1";
open(SHELL, "$command |");
while(<SHELL>) {
  # looking for authoritative nameserver
  if (/^Authoritative answers/) {
    $_ = <SHELL>;
    ($authns) = (split(/=/))[1];
    last;
  }
}
close(SHELL);
print "<ul><pre>";
$command = "$nslookup -timeout=2 -q=any $array{'name'} $authns 2>&1";
open(SHELL, "$command |");
print $_ while(<SHELL>);
close(SHELL);
print "</pre></ul>\n\n";

print "<p>\n";

print_footer_info();

##############################################################################
# eof lookup.pl

