4.4BSD/usr/src/contrib/bind-4.9/contrib/umich/zt

#!/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.
# -------------------------------------------------------------
RECURSE=0 
DEBUG=0 
XFER=/etc/named-xfer 
QUERY=/usr/local/bin/query

# -------------------------------------------------------------
#  Do args
#	-R	Recursive.  Grab all zones below give zone too.
#	-d	Debug.  Print handy debugging info.
# -------------------------------------------------------------
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
		#----------------------------------------------------
		$QUERY -t NS -h $ZONE > /tmp/zt.$$ 2> /dev/null
		if [ $? -ne 0 ] ; then
			echo Zone $zone unknown or nameserver unavailable
			exit -1
		fi
		NSLIST=`cat /tmp/zt.$$ | awk '/address/ { 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
				}' >> /tmp/zt.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 /tmp/zt.$$
		#----------------------------------------------------
		#  If we're doing a recursive query, capture the NS
		#  records to build a new ZONELIST of subdomains
		#----------------------------------------------------
		SUBZONES=""
		if [ $RECURSE = 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.$$ > /tmp/zl.$$
			if [ -s /tmp/zl.$$ ] ; then
				SUBZONES=`echo $SUBZONES ; sort -u /tmp/zl.$$`
				DONE=0
			fi
			rm -f /tmp/zl.$$
		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 = 1 ] ; then
		if [ -s /tmp/zt.out.$$ ] ; then
			cat /tmp/zt.out.$$
		else
			echo "Could not perform a zone transfer of $1"
		fi
		rm -f /tmp/zt.out.$$
		exit 0
	else
		ZONELIST=$SUBZONES
	fi
done
exit 0