#!/bin/sh
#	program:	siteinfo
#	release:	1.5.0
#	input:		domain name of zone to analyze,
#			optional nameserver to direct queries to.
#	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).
#
# 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.
#

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

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

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

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] dom.ain." >&2
        echo 'ERROR'
        exit 1
fi

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

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

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

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

echo "; Siteinfo version 1.5.0, Copyright (C) 1993 Paul A. Balyoz"
echo "; Siteinfo comes with ABSOLUTELY NO WARRANTY."
echo "; This is free software, and you are welcome to redistribute it"
echo "; under certain conditions; see file COPYING for details."
echo ";"
echo "; Site information about zone $zone"
echo ";"

/usr/local/domtools/bin/any $atnameserver $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 | /usr/bin/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 | /usr/bin/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 | /usr/bin/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=`/usr/local/domtools/bin/f2iaddr $a`
		# Note: only one host should be returned from "ptr",
		# but some sites I've seen return more!
		hosts=`/usr/local/domtools/bin/ptr $atnameserver $inaddr`
		for h in $hosts; do
			echo "	  $h ($a)"
		done
	done
fi

cname=`grep '^CNAME ' $TMP | /usr/bin/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 | /usr/bin/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=`/usr/local/domtools/bin/i2faddr $p`
		netname=`/usr/local/domtools/bin/ptr $atnameserver $p`
		netmask=`/usr/local/domtools/bin/netmask $atnameserver $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
	/usr/local/domtools/bin/any $atnameserver "$svc.$zone" > $TMP 2> /dev/null
	grep '^A ' $TMP > /dev/null
	founda=$?
	grep '^CNAME ' $TMP > /dev/null
	foundcname=$?
	grep '^MX ' $TMP > /dev/null
	foundmx=$?
	if test $founda -eq 0 -o $foundcname -eq 0; then
		case $svc in
			www)		echo "standard world wide web server available. ($svc.$zone)" ;;
			web)		echo "semi-standard world wide web server available. ($svc.$zone)" ;;
			dns|ns|ns?)	echo "standard domain name server available. ($svc.$zone)" ;;
			irc)		echo "standard Internet relay chat 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
	elif test $foundmx -eq 0; then
		case $svc in
			www)		echo "standard world wide web server available, for mail only. ($svc.$zone)" ;;
			web)		echo "semi-standard world wide web server available, for mail only. ($svc.$zone)" ;;
			dns|ns|ns?)	echo "standard domain name server available, for mail only. ($svc.$zone)" ;;
			irc)		echo "standard Internet relay chat server available, for mail only. ($svc.$zone)" ;;
			news|usenet)	echo "standard usenet news server available, for mail only. ($svc.$zone)" ;;
			info)		echo "some kind of information server available, for mail only. ($svc.$zone)" ;;
			*)		echo "standard $svc server available, for mail only. ($svc.$zone)" ;;
		esac
	fi
done

rm -f $TMP
exit 0
