#!/usr/bin/perl

use strict;
use Net::DNS;
use Socket;

my @ips=();
my @names=();
my $rc=0;

my $res = Net::DNS::Resolver->new;
my $query;
my $revQuery;
my $chkdomain=0;
my $chkmxes=0;
my $chknses=0;
my @thingstocheck=();
my $printsyntax=0;

for my $curopt (@ARGV) {
  if    ($curopt =~ /^(-(h|\?)|--help)$/i)   { $printsyntax++; }
  elsif ($curopt =~ /^-mx$/i)                { $chkmxes++; $chkdomain++; }
  elsif ($curopt =~ /^-ns$/i)                { $chknses++; $chkdomain++; }
  elsif ($curopt =~ /^-/i)
  { print "Unkown parameter ".$curopt."\n"; 
    $printsyntax++;
    $rc++;
  }
  else
  { push @thingstocheck, $curopt; }
}

if ( ( scalar(@thingstocheck) eq 0) and (!$printsyntax) )
{ print "No parameters given.\n";
  $printsyntax++;
  $rc++;
}
if ($printsyntax)
{ print 'Syntax:
'.'
  revchk ( -mx | -ns | -mx -ns ) domainname ...
  revchk ( hostname | ipaddress ) ...
  revchk ( -h | -? | --help )
'.' 
  Checks the reverse resolvability of the given hosts, IP addresses,
  or the MX respectively NS records of the given domains.
'.'
  -mx             Check the MX records of the given domains.
  -ns             Check the NS records of the given domains.
  -h, -?, --help  Print this help.
'.'
';
  exit $rc;
}

my %namestocheck=();
my %ipstocheck=();

if ($chkmxes)
{ for my $curtocheck (@thingstocheck)
  { next if ($curtocheck =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/);
    print "Detecting MXes for domain $curtocheck ...\n";
    my @mxes=mx($res,$curtocheck);
    if (@mxes)
    { foreach my $curmx (@mxes)
      { print $curmx->exchange." (priority ".$curmx->preference.")\n";
        if ($curmx->exchange  =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/)
        { $ipstocheck{$curmx->exchange}=$curmx->exchange; }
        else
        { $namestocheck{$curmx->exchange}=$curmx->exchange; }
      }
    }
    else
    { print "COULD NOT DETECT MX-Records of domain $curtocheck\n";
      $rc++;
    }
  }
}

if ($chknses)
{ for my $curtocheck (@thingstocheck)
  { next if ($curtocheck =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/);
    print "Detecting NSes for domain $curtocheck ...\n";
    $query = $res->query($curtocheck, "NS");
    if ($query)
    { foreach my $rr ($query->answer)
      { next if ($rr->type ne "NS");
        print $rr->nsdname."\n";
        if ($rr->nsdname  =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/)
        { $ipstocheck{$rr->nsdname}=$rr->nsdname; }
        else
        { $namestocheck{$rr->nsdname}=$rr->nsdname; }
      }
    }
    else
    { print "COULD NOT DETECT NS-Records of domain $curtocheck\n"; 
      $rc++;
    }
  }
}

if (!$chkdomain)
{ for my $curtocheck (@thingstocheck)
  { if ($curtocheck =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/)
    { $ipstocheck{$curtocheck}=$curtocheck; }
    else
    { $namestocheck{$curtocheck}=$curtocheck; }
  }
}

if (scalar(%namestocheck) gt 0)
{ print "Forward resolving detected respectively given hostnames ...\n";
  foreach my $curname (sort(keys(%namestocheck)))
  { print "Name ".$curname." resolves to ... \n";
    $query=$res->query($curname,"A");
    if ($query)
    { foreach my $rr ($query->answer)
      { next unless ($rr->type eq "A");
        $ipstocheck{$rr->address}=$rr->address;
        print $rr->address."\n";
      }
    }
    else
    { print $curname." COULD NOT BE RESOLVED.\n";
      $rc++;
    }
  }
}

#my %revereresults=();
if (scalar(%ipstocheck) gt 0)
{ print "Reverse resolving detected respectively given IP addresses ...\n";
  foreach my $curip (sort(keys(%ipstocheck)))
  { if ($curip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/)
    { my $curRevName=$4.".".$3.".".$2.".".$1.".in-addr.arpa";
      print "IP ".$curip." resp. ".$curRevName." reverse resolves to ... \n";
      $revQuery=$res->query($curip,"PTR");
      my $rdstring;
      if ($revQuery) {
        foreach my $rr ($revQuery->answer)
        { #print "DEGUB: rr='".$rr."'\n";
          next unless ($rr->type eq "PTR");
          $rdstring = $rr->rdstring;
          # not sure wether to strip a trailing '.'
        }
      }
      if ($rdstring)
      { print $rdstring; 
        $query=$res->query($rdstring,"A");
        if ($query)
        { my $fwok=0; 
          foreach my $rr ($query->answer)
          { next unless ($rr->type eq "A");
            next if $fwok;
            if ($rr->address eq $curip)
            { print " which also forward resolves correctly\n"; 
              $fwok++;
            }
          }
          if (!$fwok)
          { print " which DOES NOT FORWARD RESOLVE to $curip\n";
            $rc++;
          }
        }
        else
        { print " which DOES NOT FORWARD RESOLVE.\n";
          $rc++;
        }
      }
      else
      { print $curip." COULD NOT BE REVERSE RESOLVED.\n";
        $rc++;
      }
    }
    else
    { print $curip." DOES NOT LOOK LIKE AN IP ADDRESS.\n";
      $rc++;
    }
  }
}
else
{ print "NO IPS TO RESOLVE.\n";
  $rc++;
}


exit $rc;
