#!/usr/bin/perl -w 
#
# Copyright (C) 2007, Joshua D. Abraham (jabra@spl0it.org)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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 have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
# use strict;
#
# zone_transfer - perform a zone transfer 
#
# ex: $ ./zone_transfer.pl -d google.com -n ns1.google.com 
#
use strict;
use Net::DNS;
use Getopt::Long;

use vars qw( $PROG );
( $PROG = $0 ) =~ s/^.*[\/\\]//;    # Truncate calling path from the prog name


my $AUTH    = 'Joshua D. Abraham';  # author
my $EMAIL    = 'jabra@spl0it.org';  # email
my $VERSION = '1.0';                # version

my %options;
my $name_server;
#
# help: ->
# display help information
# side effect:  exits program
#
sub help {
    print "Usage: $PROG [Options]
   -d  --domain     [domain]    Domain for Zone Transfer
   -n  --nameserver [ns]        Nameserver for Zone Transfer
   
   -v  --version                Display version
   -h  --help                   Display this information

Send Comments to $AUTH ( $EMAIL )\n";
    exit;
}
#
# print_version ->
# displays version
# side effect: exits program
#
sub print_version {
    print "$PROG version $VERSION by $AUTH ( $EMAIL )\n";
    exit;
}

GetOptions(
    \%options,
    'domain|d=s', 'nameserver|n=s',

    'help|h'    => sub { help(); },
    'version|v' => sub { print_version(); },
    )
    or exit 1;

if ( $options{nameserver} ) {
    $name_server = $options{nameserver};
}
else {
    print "Input Error\n";
    help();
}

if ( $options{domain} ) {
    my $res  = Net::DNS::Resolver->new;
    
    $res->nameservers($name_server);
  
    my @zone = $res->axfr( $options{domain} );
    foreach my $rr (@zone) {
        $rr->print;
    }
}
