#!/bin/sh -
#
#	@(#)rblookup            e07@nikhef.nl (Eric Wassenaar) 971111
#
# Author:	E.Wassenaar, Nikhef-H
# Version:	11-NOV-1997
# Revision:
#
# Lookup a dotted quad IP address in the Realtime Blackhole List
# of the Mail Abuse Prevention System. See: http://maps.vix.com/rbl
#
#	Syntax:
#		rblookup hostname
#		rblookup -i dottedquad
#
#	Returns:
#		zero if the given host was found on the black list.
#		nonzero if not, or if undetermined.
#
# The RBL is an on-line, dynamically updated database of spam hosts,
# maintained via the DNS. The search key is the reversed dotted quad
# IP address of the given host, within the zone "rbl.maps.vix.com".
# A query should be done for an A resource record. If it exists, the
# given host is blacklisted as a notorious spam host. The value of the
# retrieved A record is irrelevant and can be ignored. Additional info
# may be found via an extra query for a TXT resource record.
#
# The RBL data in the DNS is replicated by several nameservers, using
# a refresh time of 10 minutes. The TTL for local caching is 5 minutes.
# The RBL nameservers do not allow zone transfers from arbitrary hosts,
# thereby preventing the setup of a stealth server, unless you sign a
# non-proliferation agreement (and you are running BIND version 8).
#
# This script is just an example of a quick and dirty wrapper for the
# ``host'' utility. The technique can relatively easy be integrated 
# into MTA programs like sendmail.

exec=echo
exec=

# ----------------------------------------------------------------------
# Setup environment.
# ----------------------------------------------------------------------

# This is where the ``host'' executable lives.
BINDIR=/usr/local/bin

PATH=${BINDIR}:/bin:/usr/bin:/usr/ucb ; export PATH

cmd=`basename $0`

options="[-v] [-i]"
usage="Usage: $cmd $options hostname"

# ----------------------------------------------------------------------
# Exit codes from <sysexits.h>
# ----------------------------------------------------------------------

EX_OK=0
EX_USAGE=64
EX_UNAVAILABLE=69

# ----------------------------------------------------------------------
# Process options.
# ----------------------------------------------------------------------

verbose=
reverse=

skip=
for i
do
	if [ $skip ]
	then
		skip=
		continue
	fi

	case "$i" in
	-i)
		reverse=true
		;;
	-v)
		verbose="-v"
		;;
	-d)
		exec=echo
		;;
	-*)
		echo "$cmd: Unknown option $i" 1>&2 ; exit $EX_USAGE
		;;
	*)
		break
		;;
	esac
	shift
done

# ----------------------------------------------------------------------
# Process arguments.
# ----------------------------------------------------------------------

name="$1"

if [ "X$name" = "X" ]
then
	echo "$usage" 1>&2 ; exit $EX_USAGE
fi

# Remove trailing dots.
name=`echo $name | sed 's/\.*$//'`

if [ $reverse ]
then
	# Assume this is already a dotted quad.
	address="$name"
else
	# Try to resolve domain name into dotted quad.
	address=`host "$name" | awk '$2 == "A" {print $3}'`
	[ "X$address" = "X" ] && address="$name"
fi

# ----------------------------------------------------------------------
# Auxiliary routines.
# ----------------------------------------------------------------------

invalid ()
{
	echo "Invalid dotted quad $address" 1>&2 ; exit $EX_USAGE
}

numeric ()
{
	[ "X$1" = "X" ] && invalid

	# Must be numeric.
	n=`expr $1 + 0` ; [ "X$n" = "X" ] && invalid

	# Must be in range.
	[ "$n" -lt 0 -o "$n" -gt 255 ] && invalid

	return $EX_OK
}

# ----------------------------------------------------------------------
# Main procedure.
# ----------------------------------------------------------------------

labels=`echo "$address" | sed -e 's/\./ /g'`
set $labels
case "$#" in
1)
	numeric $1
	reversed="0.0.0.$1"
	;;
2)
	numeric $1 && numeric $2
	reversed="0.0.$2.$1"
	;;
3)
	numeric $1 && numeric $2 && numeric $3
	reversed="0.$3.$2.$1"
	;;
4)
	numeric $1 && numeric $2 && numeric $3 && numeric $4
	reversed="$4.$3.$2.$1"
	;;
*)
	invalid
	;;
esac

# Construct proper name in map.
name="$reversed.rbl.maps.vix.com"

echo "--- $name ---"
$exec host $verbose -t A $name
found=$?
[ $found -eq $EX_OK ] && $exec host $verbose -t TXT $name
exit $found

