#! /bin/sh
#
# Sample smtp-policy-db builder script.
#
# This merges following files from $MAILVAR/db/ directory:
#	smtp-policy.src
#	localnames	         ('= _localnames')
#	smtp-policy.relay.manual ('= _full_rights')
#	smtp-policy.relay        ('= _full_rights')
#	smtp-policy.mx.manual    ('= _relaytarget')
#	smtp-policy.mx	         ('= _relaytarget')
#	smtp-policy.spam         ('= _bulk_mail')
#	smtp-policy.spam.manual  ('= _bulk_mail')
#
# These all together are used to produce files:  smtp-policy.$DBEXT
# The produced database retains the first instance of any given key.
#

#FLAG=
#while getopts n c; do
#  case $c in
#    n)       FLAG=$c;;
#    ?)       exit 2;;
#  esac
#done
#shift `expr $OPTIND - 1`

if [ -z "$ZCONFIG" ] ; then
    ZCONFIG=/opt/mail/zmailer.conf
fi

. $ZCONFIG

DBDIR="$MAILVAR/db/"
USAGE="Usage: $0 [-n] [-d dbdir]"

while [ "$1" != "" ]; do
    case "$1" in
	-n)
	    FLAG=n
	    ;;
	-d)
	    shift
	    DBDIR=$1
	    if [ ! -d $DBDIR ]; then
		echo $USAGE
		exit 1
	    fi
	    ;;
	?)
	    echo $USAGE
	    exit 0
	    ;;
	*)
	    echo $USAGE
	    exit 2
	    ;;
    esac
    shift
done


umask 022

cd $DBDIR

if [ ! -f smtp-policy.src ] ; then
	echo "No $DBDIR/smtp-policy.src input file"
	exit 64 # EX_USAGE
fi

#if [ f$FLAG != fn ]; then
#    if [ -x $MAILBIN/smtp-policy-retrieve.pl ] ; then
#	$MAILBIN/smtp-policy-retrieve.pl
#    else
#	if [ -x $MAILBIN/spamlist.py -a -r spamlist_sources ] ; then
#	    $MAILBIN/spamlist.py spamlist_sources > smtp-policy.spam.new && \
#		mv  smtp-policy.spam.new smtp-policy.spam
#	else
#	    #
#	    # Following IS NOT SAFE, if either produces errors, those
#	    # go (usually) to the result file, and in the end the result
#	    # OVERWRITES the "running"  smtp-policy.spam  file.
#	    #
#	    > smtp-policy.spam.new
#	   lynx -source http://www.webeasy.com:8080/spam/spam_download_table \
#		| sed -e '1,$s/^@/./' \
#		>> smtp-policy.spam.new
#	   lynx -source http://www.sprocket.com/Security/SpamDomains | \
#		awk 'NF > 0 {printf ".%s\n",$1}' >> smtp-policy.spam.new
#	    cat smtp-policy.spam.new | sed 's/^@//g' | tr "[A-Z]" "[a-z]" | \
#		    sort | uniq > smtp-policy.spam.new2
#	    if [ `grep -c cyberpromo smtp-policy.spam.new` -gt "0" ]; then
#		mv smtp-policy.spam smtp-policy.spam.old
#		mv smtp-policy.spam.new2 smtp-policy.spam
#		rm -f smtp-policy.spam.new
#	    else
#		echo "Hmm....something went wrong while updating the spam policy."
#		echo "Please try again."
# 		exit 1
#	    fi
#	    :
#	fi
#    fi
#fi

# Fork off a subshell to do it all...
(
  # The basic boilerplate
  cat smtp-policy.src

  # Localnames
  echo "# ----------"
  echo "# localnames:"
  cat localnames | \
  awk '/^#/{next;} NF >= 1 {printf "%s = _localnames\n",$1;}'

  # smtp-policy.relay
  # (Lists NETWORKS (NO DOMAINS!) that are allowed to use us as relay)
  # (well, actually it could also list e.g.: ".our.domain" if it would
  #  be fine to allow relaying from anybody whose IP address reverses to
  #  domain suffix ".our.domain")
  if [ -f smtp-policy.relay.manual ] ; then
    echo "# -------------------------"
    echo "# smtp-policy.relay.manual:"
    cat smtp-policy.relay.manual | \
    awk '/^#/{next;}
	NF >= 1 {printf "%s = _full_rights\n",$0;next;}'
  fi
  if [ -f smtp-policy.relay ] ; then
    echo "# ------------------"
    echo "# smtp-policy.relay:"
    cat smtp-policy.relay | \
    awk '/^#/{next;}
	NF >= 1 {printf "%s = _full_rights\n",$0;next;}'
  fi

  # smtp-policy.mx.manual
  # (Lists domains that are allowed to use us as inbound MX relay for them)
  if [ -f smtp-policy.mx.manual ] ; then
    echo "# ----------------------"
    echo "# smtp-policy.mx.manual:"
    cat smtp-policy.mx.manual | \
    awk '/^#/{next;} NF >= 1 {printf "%s = _relaytarget\n",$0;}'
  fi
  # smtp-policy.mx
  # (Lists domains that are allowed to use us as inbound MX relay for them)
  if [ -f smtp-policy.mx ] ; then
    echo "# ---------------"
    echo "# smtp-policy.mx:"
    cat smtp-policy.mx | \
    awk '/^#/{next;} NF >= 1 {printf "%s = _relaytarget\n",$0;}'
  fi

  # smtp-policy.spam
  # (Lists users, and domains that are known spam sources)
  # (We use file from "http://www.webeasy.com:8080/spam/spam_download_table"
  #  which is intended for QMAIL, and thus needs to be edited..)
  if [ -f smtp-policy.spam -o -f smtp-policy.spam.manual ] ; then
    echo "# ---------------------------"
    echo "# smtp-policy.spam{,.manual}:"
    ( if [ -f smtp-policy.spam ] ; then
	cat smtp-policy.spam
      fi
      if [ -f smtp-policy.spam.manual ] ; then
	cat smtp-policy.spam.manual
      fi ) | tr "[A-Z]" "[a-z]" | sed 's/^@//g' | sort | uniq | \
    awk '/^\[/{ # an address block to reject
	    printf "%s  rejectnet +\n",$0;
	    next;
	}
	NF > 0 { # All other cases are usernames with their domains
	    printf "%s  = _bulk_mail\n",$0;
	}'
  fi

# --------- end of subshell
) > smtp-policy.dat

umask 022 # Make sure the resulting db file(s) are readable by all

# Build the actual binary policy database (-p), and if the input
# has same key repeating, append latter data instances to the first
# one (-A):

if $MAILBIN/makedb -A -p $DBTYPE smtp-policy-new smtp-policy.dat
then
  :
else
  rc=$?
  echo "smtp-policy control database makedb failure rc= $rc"
  exit $rc
fi

case $DBTYPE in
dbm)
	mv smtp-policy-new.dir  smtp-policy.dir
	mv smtp-policy-new.pag  smtp-policy.pag
	;;
ndbm)
	mv smtp-policy-new.dir  smtp-policy.dir
	mv smtp-policy-new.pag  smtp-policy.pag
	;;
gdbm)
	mv smtp-policy-new.gdbm smtp-policy.gdbm
	;;
btree)
	mv smtp-policy-new.db   smtp-policy.db
	;;
esac

exit 0


syntax highlighted by Code2HTML, v. 0.9.1