#!BOURNESHELL
#
#	program:	check1101
#	purpose:	To examine a zone's resource records and make sure
#			that the optional things from RFC1101 are done right.
#	input:		domain name on command line.
#	output:		A verbose description of what was found and what
#			looks wrong to stdout.  Usage errors go to stderr.
#	exit value:	0 if nothing went wrong, 1 if something did.

usage() {
	echo "usage: $0 dom.ain." >&2
	echo "       $0 -r [-s] hostzeroaddress [parentnetmask]" >&2
	exit 1
}
if test $# -lt 1 -o $# -gt 4; then
	usage
fi

VER="1.3"		# version of check1101

SFLAG=0 RFLAG=0 retval=0 arg1='' arg2=''
for i do
	case "$i" in
		-s)	SFLAG=1 ;;
		-r)	RFLAG=1 ;;
		*)	if test x"$arg1" = x""; then
				arg1="$i"
			else
				if test $RFLAG -eq 0; then
					usage
				else
					arg2="$i"
				fi
			fi
	esac
done
if test $RFLAG -eq 0; then
	zone="$arg1"
else
	hostzero="$arg1"
	parentnetmask="$arg2"
# if parentnetmask is empty string, then it wasn't specified on command line.
fi

if test $SFLAG -eq 0; then
	echo "; Check1101 v$VER"
fi

# Decide which path to follow; domain, or hostzero address

if test $RFLAG -eq 0; then
	echo ";; Checking zone $zone"

# Make sure what they gave us is really a zone served by name servers!

	nameservers=`DOMBIN/ns $zone`
	if test $? -ne 0 -o x"$nameservers" = x""; then
		echo "ERROR: $zone doesn't appear to have any nameservers."
		retval=1
	else
		echo ";; $zone nameservers $nameservers" | tr '\012' ' '
		echo ""
	fi

# Verify zone has one or more host zero addresses,
# and call us recursively to verify them all.

	numnets=0
	hostzeroes=`DOMBIN/ptr $zone`
	if test $? -ne 0; then
		echo "ERROR: $zone has no PTR records pointing to host-zero records."
		retval=1
	else
		for hostzero in $hostzeroes; do
			$0 -s -r $hostzero
			if test $retval -eq 0; then
				retval=$?
			fi
			numnets=`expr $numnets + 1`
		done
	fi

	S=""; if test $numnets -ne 1; then S="s"; fi
	echo ";; $numnets network$S total."

else

#
# -r flag -- verify $hostzero recursively.
#

	echo ";; Checking $hostzero host zero records."
	NONAME=0
	name=`DOMBIN/ptr $hostzero`
	if test $? -ne 0; then
		retval=$?
		echo "ERROR: no $hostzero PTR record pointing to network name."
		NONAME=1
	fi
	net=`DOMBIN/i2faddr $hostzero`
	if test $? -ne 0; then
		echo "ERROR: i2faddr $hostzero failed, maybe you specified the wrong zone."
		exit 1
	fi
	if test $NONAME -eq 0; then
		echo ";; network $net name record $name was found."
	fi

	newhostzero=`DOMBIN/ptr $name`
	if test $? -ne 0; then
		echo "ERROR: $name PTR record not found, it should point to $hostzero"
	fi
	if test x"$newhostzero" = x"$hostzero"; then
		echo ";; pointer from net name to host-zero record exists."
	else
		echo "ERROR: $name PTR record points to $newhostzero instead of $hostzero"
	fi

# Look for a netmask from this host-zero address, and loop through all subnets
# if the netmask implies we're subnetted.

	if test x"$parentnetmask" = x""; then
		parentnetmask=`DOMBIN/addr2mask $net`
	fi
	netmask=`DOMBIN/address $hostzero`
	if test $? -eq 0; then
		echo ";; network $net netmask $netmask record found."
	else
		netmask=$parentnetmask
		echo ";; note: no netmask found for $net, assuming $netmask"
	fi
	if test x"$parentnetmask" = x"$netmask"; then
		echo ";; network is not subnetted."
	else
		echo ";; network is subnetted."

# add loop through all subnets here...
		
# add recursive call to self with -s -r and parentnetmask here...

	fi
fi

exit $retval
