#!/bin/sh
#	subdom2
#	Domtools release 1.5.0
#
#	This is a part of subdom, and should not be used by anyone else.
#
#	Print a list of sub-domains in this domain.  This is not recursive
#	unless the "-r" command-line flag is given.  This is the whole program
#	except that the output from here needs to be uniq'd, but we can't
#	easily do that from here because we may call ourselves recursively,
#	so the outer shell script "subdom" does that job.  Therefore this
#	program is NOT INTENDED FOR DIRECT USER USE.  Users should run the
#	"subdom" program instead.
#
# Copyright (C) 1993-2000 Paul A. Balyoz <pab@domtools.com>
#
#    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., 675 Mass Ave, Cambridge, MA 02139, USA.
#

TMP=/tmp/subdom1.$$
TMP2=/tmp/subdom2.$$

SUBDOMAWK=/usr/local/domtools/lib/subdom.awk

usage() {
	echo "usage: subdom [-r] [@nameserver] dom.ain." >&2
	echo 'ERROR'
	exit 1
}

if test $# -lt 1 -o $# -gt 3; then
	usage
fi

dom=
nameservers=
atnameserver=
RECURSIVE=false
for arg do
	case "$arg" in
		-r)	RECURSIVE=true ;;
		@*)	atnameserver="$1" nameservers=`echo $1 | sed -e 's/@//'` ;;
		*)	if test x"$dom" = x""; then
				dom="$arg"
			else
				usage
			fi ;;
	esac
done
if test x"$dom" = x""; then
	usage
fi

# Figure out the zone for this domain.

zone=`/usr/local/domtools/bin/zone $atnameserver $dom`
if test $? -ne 0; then
	echo "subdom: cannot determine what zone $dom is in." >&2
	echo 'ERROR'
	exit 1
fi

# Get the zone dump for this zone.

/usr/local/domtools/bin/axfr $atnameserver $zone > $TMP
if test $? -ne 0; then
	echo "subdom: cannot get zone dump of $zone" >&2
	echo 'ERROR'
	rm -f $TMP
	exit 1
fi

# Build a list of domains, one per line.  Add "subdomain" after it if this
# domain is definitely in another zone (was found on an SOA or NS record).
# This also sorts the records by domain, upper-level domains first.
# The extra "uniq" should stay in here to reduce the number of
# records that domsort must look at, which speeds things up a lot.
# This list may include the current domain, not just subdomains.
# Any given domain could be listed twice; with and without "subdomain"
# as its second field.

/usr/bin/awk -f $SUBDOMAWK $TMP | uniq | /usr/local/domtools/bin/domsort -u | \
	/usr/local/bin/perl5.00502 -ne "@a=split(' ', \$_, 9999); print \$_ if \$a[0] =~ /\\b$dom\$/i" > $TMP2

# If the query to a nameserver succeeded but awk parsing returns no records,
# then we exit normally.

lines=`wc -l < $TMP2`
if test $lines -eq 0; then
	rm -f $TMP $TMP2
	exit 0
fi

# Loop through all subdomain info records we built above.

i=1
while test $i -le $lines; do

# Get each record from the file.  $1 is a domain name,
# $2 is "subdomain" if $1 is in another zone for sure;
# $2 nonexistent if we aren't sure.

	set `sed -e $i!d < $TMP2`

# If recursive, print all domains including the current one.
# If not recursive, print all but the current one.

	isme=`/usr/local/domtools/bin/isequal "$1" "$dom"`
	if test "$isme" = "false" -o $RECURSIVE = true; then
		echo $1
	fi

# If recursive mode specified and the current domain is in another zone
# and it's not the zone we're already in, then call ourselves recursively.

	if $RECURSIVE; then
		if test $# -gt 1; then
			if test "$2" = "subdomain"; then
				if test "$isme" = "false"; then
					/usr/local/domtools/bin/subdom2 -r $atnameserver $1
if test $? -ne 0; then
	echo "subdom: recursive call to subdom2 failed; skipping subdomain $1" >&2
fi
				fi
			fi
		fi
	fi

# Bottom of domain names loop.

	i=`expr $i + 1`
done

# Cleanup and exit.

rm -f $TMP $TMP2

exit 0
