#!/bin/sh
# -------------------------------------------------------------
#  Copyright (c) 1991 Regents of the University of Michigan.
#  All rights reserved.
#
#  Redistribution and use is permitted provided that this notice 
#  is preserved and that due credit is given to the University of 
#  Michigan. The name of the University may not be used to endorse 
#  or promote products derived from this software without specific 
#  prior written permission. This software is provided "as is" 
#  without express or implied warranty.
# -------------------------------------------------------------

# -------------------------------------------------------------
#  tailor to your system
# -------------------------------------------------------------

##
##  Where is the zone transfer binary?
##
XFER=/usr/etc/in.named-xfer 

##
##  Where are scratch files kept?
##
TMP=/usr/tmp

# -------------------------------------------------------------
#  you should not need to touch anything below this point
# -------------------------------------------------------------
OUT=$TMP/.zt.out.$$
TEMP=$TMP/.zt.$$
ZLIST=$TMP/.zlist.$$

# -------------------------------------------------------------
#  Do args
#	-R	Recursive.  Grab all zones below give zone too.
#	-d	Debug.  Print handy debugging info.
# -------------------------------------------------------------
RECURSE=0 
DEBUG=0 

if [ $# = 0 ] ; then
	echo "Usage: [-R] [-d] zone"
	exit -1
fi
while [ $# != 1 ] ; do
	case "$1" in
		-R)
		RECURSE=1
		;;

		-d)
		DEBUG=1
		;;

		-?)
		echo Illegal arg $1
		exit -1
		;;

	esac
	shift
done
ZONELIST=$1

while [ 1 ] ; do
	#----------------------------------------------------
	# Loop through the list of zones
	#----------------------------------------------------
	DONE=1
	for ZONE in $ZONELIST ; do
		#----------------------------------------------------
		#  Get the addr of the zone's nameservers
		#----------------------------------------------------
		dig $ZONE NS > $TEMP 2> /dev/null
		if [ $? -ne 0 ] ; then
			echo Zone $ZONE unknown or nameserver unavailable
			exit -1
		fi
		NSLIST=`cat $TEMP | awk '$3 == "A" { print $4 }'`
		#----------------------------------------------------
		#  Use named-xfer to bring over a copy of the zone
		#  Use 0 as the serial # since will likely be less
		#  than the current serial number in the SOA
		#----------------------------------------------------
		for I in $NSLIST ; do
			if [ $DEBUG = 1 ] ; then
				echo Trying nameserver at address $I
			fi
			$XFER -z $ZONE -f $TMP/$ZONE.$$ -s 0 $I
			if [ $? = 1 ] ; then
				grep -v ";" $TMP/$ZONE.$$ |
				awk '{
					if ($1 == "$ORIGIN") {
						origin=$2
						if (origin != old) {
							old = origin
							print $0
						}
					}
					else
						print $0
				}' >> $OUT
				if [ $DEBUG = 1 ] ; then
					echo Got zone $ZONE
				fi
				break
			elif [ $DEBUG = 1 ] ; then
				echo Nameserver $I for $ZONE not working
			fi
		done
		rm -f $TEMP
		#----------------------------------------------------
		#  If we're doing a recursive query, capture the NS
		#  records to build a new ZONELIST of subdomains
		#----------------------------------------------------
		SUBZONES=""
 		if [ $RECURSE -eq 1 -a -s $TMP/$ZONE.$$ ] ; then
 			awk '{
 				if ($1 == "$ORIGIN")
 					origin = substr($2, 1, length($2) - 1)
 				else if ($3 == "NS") {
 					sub = $1 "." origin
 					if (sub != zone)
 						print sub
 				}
 			}' zone=$ZONE $TMP/$ZONE.$$ > $ZLIST
 			if [ -s $ZLIST ] ; then
 				SUBZONES=`echo $SUBZONES ; sort -u $ZLIST`
 				DONE=0
 			fi
 			rm -f $ZLIST
 		fi
		rm -f $TMP/$ZONE.$$
	done
	#----------------------------------------------------
	#  If we weren't recursive, or we were recursive and
	#  have run out of subdomains, we're done
	#----------------------------------------------------
	if [ $DONE -eq 1 ] ; then
		if [ -s $OUT ] ; then
			cat $OUT
		else
			echo "Could not perform a zone transfer of $1"
		fi
		rm -f $OUT
		exit 0
	else
		ZONELIST=$SUBZONES
	fi
done
exit 0
