#!/bin/sh
#
# Build zone containing blackholed addresses.  Uses Christian Rignoni's
# DNS zone templates.
#
# Boyd Roberts
# November 1997
#

myname="`basename \"$0\"`"

RBL=rbl.maps.france3.fr		# blackhole domain name
ADDR=127.0.0.2			# address attributed to a blackholed 'host'
LIST=rbl.list			# list of blackholed addresses

# Configation files
BOOT=/etc/named.boot
RCONF=/etc/resolv.conf

# File extensions
SOA=soa
HOSTS=hosts
HOSTNAMES=hostnames

# Templates
MODELE=modele

# Determine current domain

DOM="`awk \"\\$1 == \\"domain\\" {
	print \\$2
	exit
	}\" \"$RCONF\""

case "$DOM" in
'')
	echo "$myname: Could not find 'domain' in '$RCONF'." 1>&2
	exit 1
	;;
esac

# Determine configuration directory

DIR="`awk \"\\$1 == \\"directory\\" {
	print \\$2
	exit
	}\" \"$BOOT\""

case "$DIR" in
'')
	echo "$myname: Could not find 'directory' in '$BOOT'." 1>&2
	exit 1
	;;
esac

# Determine zone file for domain

FILE="`awk \"\\$1 == \\"primary\\" && \\$2 == \\"$RBL\\" {
	print \\$3
	exit
	}\" \"$BOOT\""

case "$FILE" in
'')
	echo "$myname: Could not find primary for $RBL in '$BOOT'." 1>&2
	exit 1
	;;
esac

# Check the list exists

if [ ! -r "$DIR/$LIST" ]
then
	echo "$myname: Realtime Blackhole List '$DIR/$LIST' missing." 1>&2
	exit 1
fi

# Determine hostname

UNAME="`(uname -n || hostname) | sed 's/\..*$//'`"

case "$UNAME" in
'')
	echo "$myname: Could not determine hostname." 1>&2
	exit 1
	;;
esac

# Build $FILE

f="$DIR/$FILE"

if [ ! -s "$f" ]
then
	m="$DIR/$MODELE.$HOSTS"
	sed -e "s/ZONE/$RBL/g" "$m" > "$f" || exit 1
fi

ZONE_A="$RBL"

# Construct Start Of Authority

f="$DIR/$RBL.$SOA"

if [ ! -s "$f" ]
then
	m="$DIR/$MODELE.$SOA"
	sed -e "s/UNAME.ZONE_A/$UNAME.$DOM/g" -e "s/ZONE_A/$ZONE_A/g" "$m" > "$f" || exit 1
fi


# Build host list
#
# Comments after the address are appended as comments in the zone file.

awk '
$0 ~ /^;/				{ next }

$1 !~ /[0-9]+.[0-9]+.[0-9]+.[0-9]+/	{ next }

{
	split($1, a, ".")
	print a[4] "." a[3] "." a[2] "." a[1] "\tIN\tA\t" addr "\t; " $0
}

' addr="$ADDR" "$DIR/$LIST" > "$DIR/$RBL.$HOSTNAMES" || exit 1

# Update serial number

serial="`awk '$0 ~ /;[ 	]*serial/ { print $1 }' \"$DIR/$RBL.$SOA\"`"

case "$serial" in
00000001)
	n=0
	;;

*)
	n="`expr \"$serial\" : '......\\(..\\)'`"
	;;
esac

day="`date '+%y%m%d'`"
n="`awk 'END { printf("%02d\n", n + 1)}' n=\"$n\" /dev/null`"

ed - "$DIR/$RBL.$SOA" <<! && exit 0
/;[ 	]*serial/s/$serial/$day$n/
w
q
!

echo "$myname: Could not modify serial number of '$DIR/$RBL.$SOA'." 1>&2
exit 1


© 1998, Boyd Roberts: boyd@fr.inter.net