#!BOURNESHELL
#	program:	siteinfo
#	input:		domain name of zone to analyze.
#	output:		English text describing the site, obtained from queries
#			to nameservers in and around the site zone,
#			or the word "ERROR".
#	exit value:	0 if succeeds, 1 if fails (and "ERROR" output).

# List of possible service host names inside site zone.
SERVICES="www ftp mail gopher wais prospero archie news usenet mbone dns ns whois info ns1 ns2 ns3 ns4"

if test $# -ne 1; then
	echo "usage: $0 dom.ain." >&2
	echo 'ERROR'
	exit 1
fi

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

domain="$1"

# Verify that the argument passed in is at least a domain name.

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

# Verify that the domain name passed in is really a zone.

zone=`zone $domain 2>/dev/null`
if isequal $domain $zone >/dev/null 2>&1; then
	:
else
	case "$zone" in
		""|.) echo "$0: $domain is not truly a zone." >&2 ;;
		*)    echo "$0: $domain is not truly a zone, perhaps you should use $zone" >&2 ;;
	esac
	echo 'ERROR'
	exit 1
fi

echo "Information About Site Zone $zone"

DOMBIN/any $zone > $TMP 2> /dev/null

grep '^TXT ' $TMP | sed -e 's/^TXT //'

soa=`grep '^SOA ' $TMP`
if test $? -ne 0; then
	echo "No SOA found for site zone $zone (this could be bad!)" >&2
else
	set $soa
	echo "Primary Nameserver:  $2"
	adminmail=`echo $3 | sed -e 's/^\([^@.]*\)\./\1@/'`
	echo "Domain Administrator's E-Mail Address:  $adminmail"
fi

mxers=`grep '^MX ' $TMP | sort -n +1 | awk '{print $NF}' | tr '\012' ' '`
if test x"$mxers" = x""; then
	echo "No mail exchangers found for $zone"
else
	echo "E-Mail to $zone handled by:  $mxers"
fi

nses=`grep '^NS ' $TMP | awk '{print $NF}' | tr '\012' ' '`
if test x"$nses" = x""; then
	echo "No name servers found for site zone $zone (this could be bad!)"
else
	echo "Nameservers:  $nses"
fi

addr=`grep '^A ' $TMP | awk '{print $NF}' | tr '\012' ' '`
if test x"$addr" != x""; then
	echo "Connections directly to $zone really connect to:"
	for a in $addr; do
		inaddr=`DOMBIN/f2iaddr $a`
		# Note: only one host should be returned from "ptr",
		# but some sites I've seen return more!
		hosts=`DOMBIN/ptr $inaddr`
		for h in $hosts; do
			echo "    $h ($a)"
		done
	done
fi

cname=`grep '^CNAME ' $TMP | awk '{print $NF}' | tr '\012' ' '`
if test x"$cname" != x""; then
	echo "Site zone $zone is aliased to:  $cname"
fi

if test x"$addr$cname" = x""; then
	echo "Connections directly to $zone will fail."
fi

# Check for optional RFC1101 compliance.
ptr=`grep '^PTR ' $TMP | awk '{print $NF}' | tr '\012' ' '`
if test x"$ptr" = x""; then
	echo "No RFC1101 compliance (it's optional)."
else
	echo "RFC1101 compliant.  Local networks are:"
	for p in $ptr; do
		network=`DOMBIN/i2faddr $p`
		netname=`DOMBIN/ptr $p`
		netmask=`DOMBIN/netmask $network`
		if test $? -eq 0; then
			echo "    $netname ($network) netmask $netmask"
		fi
	done
fi

# Check for site services domain records.

for svc in $SERVICES; do
	DOMBIN/any "$svc.$zone" > $TMP 2> /dev/null
	grep '^A ' $TMP > /dev/null
	founda=$?
	grep '^CNAME ' $TMP > /dev/null
	foundcname=$?
	if test $founda -eq 0 -o $foundcname -eq 0; then
		case $svc in
			www)		echo "standard world wide web server available. ($svc.$zone)" ;;
			dns|ns|ns?)	echo "standard domain name server available. ($svc.$zone)" ;;
			news|usenet)	echo "standard usenet news server available. ($svc.$zone)" ;;
			info)		echo "some kind of information server available. ($svc.$zone)" ;;
			*) 		echo "standard $svc server available. ($svc.$zone)" ;;
		esac
	fi
done

rm -f $TMP
exit 0
