#!/bin/sh
#	program:	soalist
#	release:	1.5.0
#	input:		zone domain name on command line,
#			optional nameserver to direct queries to.
#	output:		a list of all authoritative nameservers for the zone with
#			their respective SOA records,
#			or the word "ERROR" (distinguishable from a real
#			record because it has no period on the end).
#	exit value:	0 if succeeds, 1 if fails (and "ERROR" output).
#
#	note:		Looking up parent nameservers is done with the "@ns" argument,
#			however, querying those parent nameservers is done directly
#			to them -- bypasses the @ns argument.  This should be OK
#			within an intranet environment (behind a firewall).
#
# 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.
#

type=SOA

TMP="/tmp/`basename $0`.$$"

if test $# -lt 1 -o $# -gt 2; then
	echo "usage: $0 [@nameserver] zone.dom.ain." >&2
	echo 'ERROR'
	exit 1
fi

dom=
nameservers=
atnameserver=
while test $# -gt 0; do
        case "$1" in
                @*)  atnameserver="$1" nameservers=`echo $1 | sed -e 's/@//'` ;;
                *)  dom="$1" ;;
        esac
        shift
done
if test x"$dom" = x""; then
        echo "$0: No domain name specified; aborting." >&2
        echo "usage: $0 [@nameserver] zone.dom.ain." >&2
        echo 'ERROR'
        exit 1
fi

if test "`/usr/local/domtools/bin/type $dom`" = "forward"; then
	echo "$0: argument $dom is not a domain name." >&2
	echo 'ERROR'
	exit 1
fi

# If they slipped us the root domain, there's no parent zone above it,
# so we just query all the root domain nameservers instead.

if test x"$dom" = x"."; then
	zone="."
	parentdomain="."
	parentzone="."
	parentnameservers=`/usr/local/domtools/bin/root`
	if test $? -ne 0 -o x"$parentnameservers" = x""; then
		echo "$0: cannot find the names of nameservers for the root domain." >&2
		echo 'ERROR'
		exit 1
	fi
else

# Find what zone the given domain is in (it should be it's own zone!)

	zone=`/usr/local/domtools/bin/zone $atnameserver $dom`
	if test $? -ne 0; then
		echo "$0: cannot find the zone of the domain $dom" >&2
		echo 'ERROR'
		exit 1
	fi
	if /usr/local/domtools/bin/isequal "$zone" "$dom" > /dev/null; then
		:
	else
		echo "$0: $dom is not truly a zone, perhaps you should use $zone" >&2
		echo 'ERROR'
		exit 1
	fi

# Its a regular zone; find the parent zone

	parentdomain=`/usr/local/domtools/bin/basedomain $zone`
	parentzone=`/usr/local/domtools/bin/zone $atnameserver $parentdomain`
	if test $? -ne 0; then
		echo "$0: cannot find the zone of the parent domain $parentdomain" >&2
		echo 'ERROR'
		exit 1
	fi

	parentnameservers=`/usr/local/domtools/bin/ns $atnameserver $parentzone`
	if test $? -ne 0 -o x"$parentnameservers" = x""; then
		echo "$0: cannot find the names of the nameservers for parent domain $parentdomain" >&2
		echo 'ERROR'
		exit 1
	fi
fi

# Find a parent zone nameserver that will tell us which nameservers are supposed to be
# authoritative for the zone we were given.

for pns in $parentnameservers; do
	dig @$pns $zone NS +ret=2 +pfset=0x2024 | /usr/local/domtools/bin/digparse | /usr/local/bin/perl5.00502 -pe 'END{$? = !defined $l} s/^\S+\s+//; s/[\t ]+/ /g; $l++' > $TMP
	status=$?
	if test $status -eq 0 -o $status -eq 3; then
		break
	fi
done
if test $status -ne 0; then
	echo "$0: could not determine nameservers for $dom from any $parentdomain name server." >&2
	echo 'ERROR'
	rm -f $TMP
	exit 1
fi
nameservers=`sed -n -e 's/^NS //p' < $TMP`
rm -f $TMP

# Now query every name server for our zone, displaying their SOA records.
# I added the "head -1" because some zones return 2 SOA records for some reason (strange).
for ns in $nameservers; do
	rr=`dig @$ns $zone $type +ret=2 +pfset=0x2024 | /usr/local/domtools/bin/digparse | /usr/local/bin/perl5.00502 -pe 'END{$? = !defined $l} s/^\S+\s+//; s/[\t ]+/ /g; $l++' | head -1`
	echo "$ns $rr" | perl -ne '($ns,$rr)=split (/[\t ]+/,$_,2); printf "%-20s -> %s",$ns,$rr;'
done

rm -f $TMP
exit 0
