#!/bin/sh

DBDIR=/etc/mail
DBSRC=/etc/mail
DBTYPE=hash

PATH=/bin:/sbin:/usr/bin:/usr/sbin
export PATH

# convert_subnets()
# convert mask/bits format into "subnet"-IPs
# unused bits in mask must be 0
# e.g. 128.1.1.1/1 does not work. Use 128.0.0.0/1
convert_subnets () {
awk '
/^[0-9].*\/[0-9]+/ {
	split($1, subs, "/")
	ipbase = ""
	i = 1
	split(subs[1], byte, ".")
	for(bits = subs[2]; bits > 8; bits -= 8)
		ipbase = ipbase byte[i++] "."
	
	if(bits == 8)
		print ipbase byte[i]
	else
		for(ii = 0; ii < 2**(8 - bits); ii++)
		{
			$1 = ipbase (ii + byte[i])
			print
		}
	next
}
{ print }'
};

# add_default_rhs
# adds RHS "REJECT" if there is none
add_default_rhs () {
awk '
{
	if($1 != "" && $2 == "")
		print $1, "REJECT"
	else
		print
}'
};


# add_special_rhs
add_special_rhs () {
awk '
{
	if($1 != "" && $2 == "")
		print $1, "\"552 spam rejected. In case of error contact the postmaster.\""
	else
		print
}'
};

# make access.db
cat 	$DBSRC/access | \
	convert_subnets | \
	add_default_rhs | \
	makemap -r $DBTYPE $DBDIR/access

