jvdias 4b4f5f
#!/usr/bin/perl
jvdias 4b4f5f
#  
jvdias 4b4f5f
#  This script uses the named D-BUS support, which must be enabled in
jvdias 4b4f5f
#  the running named with the named '-D' option, to get and print the
jvdias 4b4f5f
#  list of forwarding zones in the running server.
jvdias 4b4f5f
#
jvdias 4b4f5f
#  It accepts an optional <zone> first argument which is the DNS name
jvdias 4b4f5f
#  of the zone whose forwarders (if any) will be retrieved.
jvdias 4b4f5f
#
jvdias 4b4f5f
#  If no zone argument is specified, all forwarding zones will be listed.
jvdias 4b4f5f
#
jvdias 4b4f5f
#  Usage: GetForwarders [-n -r] [ <zone> ]
jvdias 4b4f5f
#    -n : output forward zone statements for named.conf
jvdias 4b4f5f
#    -r : output in resolv.conf format
jvdias 4b4f5f
#       : no -r or -n: just list the forwarders
jvdias 4b4f5f
#
jvdias 4b4f5f
#  Copyright(C) Jason Vas Dias<jvdias@redhat.com> Red Hat Inc. 2005
jvdias 4b4f5f
#
jvdias 4b4f5f
#  This program is free software; you can redistribute it and/or modify
jvdias 4b4f5f
#  it under the terms of the GNU General Public License as published by
jvdias 4b4f5f
#  the Free Software Foundation at
jvdias 4b4f5f
#           http://www.fsf.org/licensing/licenses/gpl.txt
jvdias 4b4f5f
#  and included in this software distribution as the "LICENSE" file.
jvdias 4b4f5f
#
jvdias 4b4f5f
#  This program is distributed in the hope that it will be useful,
jvdias 4b4f5f
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
jvdias 4b4f5f
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jvdias 4b4f5f
#  GNU General Public License for more details.
jvdias 4b4f5f
#
jvdias 4b4f5f
use Getopt::Std;
jvdias 4b4f5f
jvdias 4b4f5f
%opts=();
jvdias 4b4f5f
jvdias 4b4f5f
getopts("rn",\%opts);
jvdias 4b4f5f
jvdias 4b4f5f
$zone = '';
jvdias 4b4f5f
if ( $#ARGV >= 0 )
jvdias 4b4f5f
{
jvdias 4b4f5f
    $zone = "string:'". join("' string:'",@ARGV)."'";
jvdias 4b4f5f
};
jvdias 4b4f5f
jvdias 4b4f5f
@dn=(); 
jvdias 4b4f5f
jvdias 4b4f5f
open(DNS,
jvdias 4b4f5f
     '/usr/bin/dbus-send --system --type=method_call --print-reply --reply-timeout=20000 '
jvdias 4b4f5f
    .'--dest=com.redhat.named /com/redhat/named com.redhat.named.text.GetForwarders '
jvdias 4b4f5f
    .$zone .'|'
jvdias 4b4f5f
    ) || die("dbus-send failed: $?: $!");
jvdias 4b4f5f
jvdias 4b4f5f
while(<DNS>)
jvdias 4b4f5f
{ 
jvdias 4b4f5f
    $_=~s/[\s\r\n]+$//;
jvdias 4b4f5f
    if ( /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/ )
jvdias 4b4f5f
    { # nameserver address
jvdias 4b4f5f
	push @{${$dn[-1]}{'s'}}, { 'a' => "$1.$2.$3.$4" }; 
jvdias 4b4f5f
    }elsif
jvdias 4b4f5f
       ( /\"(\d+)\"$/ )      
jvdias 4b4f5f
    { # port
jvdias 4b4f5f
	if ( $1 != 53 )
jvdias 4b4f5f
	{
jvdias 4b4f5f
	    ${@{${$dn[-1]}{'s'}}[-1]}{'p'} = $1;
jvdias 4b4f5f
	};
jvdias 4b4f5f
    }elsif
jvdias 4b4f5f
       ( /string\s+\"([^\"]+)\"$/ )
jvdias 4b4f5f
    { 
jvdias 4b4f5f
	if ( ($1 eq 'first') || ($1 eq 'only') )
jvdias 4b4f5f
	{ # policy
jvdias 4b4f5f
	    if( $1 eq 'only' )
jvdias 4b4f5f
	    { # not default
jvdias 4b4f5f
		${$dn[-1]}{'o'} = 1;
jvdias 4b4f5f
	    }
jvdias 4b4f5f
	}else
jvdias 4b4f5f
	{ # new DN - "zone"
jvdias 4b4f5f
	    push @dn, {'n'=>$1,'s'=>[]}; 
jvdias 4b4f5f
	};
jvdias 4b4f5f
    }; 
jvdias 4b4f5f
};
jvdias 4b4f5f
close(DNS);
jvdias 4b4f5f
jvdias 4b4f5f
if( exists($opts{'r'}) )
jvdias 4b4f5f
{ # resolv.conf style:
jvdias 4b4f5f
    my %svrs=();
jvdias 4b4f5f
    print 'search ', 
jvdias 4b4f5f
          join( ' ', 
jvdias 4b4f5f
		grep { !( $_ =~ /\.in-addr\.arpa$/) }
jvdias 4b4f5f
		map  { ${$_}{'n'} }
jvdias 4b4f5f
		@dn
jvdias 4b4f5f
	      ),"\n",
jvdias 4b4f5f
          'nameserver ',
jvdias 4b4f5f
	  join( "\nnameserver ",
jvdias 4b4f5f
		grep { exists ( $svrs{ $_ } ) ? undef : { $svrs{$_}=$_ } } 
jvdias 4b4f5f
		map  { ${$_}{'a'} }
jvdias 4b4f5f
		map  { @{${$_}{'s'}} } @dn			     
jvdias 4b4f5f
	      ),"\n";
jvdias 4b4f5f
}elsif( exists($opts{'n'}) )
jvdias 4b4f5f
{ # named.conf style:
jvdias 4b4f5f
    foreach $d (@dn)
jvdias 4b4f5f
    {
jvdias 4b4f5f
	print 'zone "',${$d}{'n'},'." IN { type forward; forwarders { ',
jvdias 4b4f5f
	      join("; ",
jvdias 4b4f5f
		   map { exists( ${$_}{'p'} ) 
jvdias 4b4f5f
			 ? ${$_}{'a'} . ' port ' . ${$_}{'p'}  
jvdias 4b4f5f
			 : ${$_}{'a'}
jvdias 4b4f5f
		       } @{${$d}{'s'}}
jvdias 4b4f5f
		  ),
jvdias 4b4f5f
	      '; }; ',
jvdias 4b4f5f
	      exists(${$d}{'o'}) ? ' forward only; ' : '',
jvdias 4b4f5f
              "};\n";
jvdias 4b4f5f
    };
jvdias 4b4f5f
}else
jvdias 4b4f5f
{ # just list:
jvdias 4b4f5f
    foreach $d (@dn)
jvdias 4b4f5f
    {
jvdias 4b4f5f
	print  ${$d}{'n'}, "\n\t", 
jvdias 4b4f5f
	       (exists(${$d}{'o'}) ? "forward only\n\t" : ''),
jvdias 4b4f5f
	       join( "\n\t",	
jvdias 4b4f5f
		     map { exists( ${$_}{'p'} )
jvdias 4b4f5f
			   ? ${$_}{'a'} . ':' . ${$_}{'p'} 
jvdias 4b4f5f
			   : ${$_}{'a'}
jvdias 4b4f5f
		         } @{${$d}{'s'}}
jvdias 4b4f5f
		   ),"\n";
jvdias 4b4f5f
    };
jvdias 4b4f5f
};