#! /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=/usr/local/etc/zmailer/zmailer.conf
fi

. $ZCONFIG

DBDIR="$MAILVAR/db/"
DBFILE="smtp-policy"
USAGE="Usage: $0 [-n] [-d dbdir] [-f dbfile]"

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

	-f)
	    shift
	    DBFILE=$1
	    if [ ! -f "${DBDIR}/${DBFILE}.src" ]
	    then
	        echo $USAGE
	        exit 1
	    fi
	    ;;
	?)
	    echo $USAGE
	    exit 0
	    ;;
	*)
	    echo $USAGE
	    exit 2
	    ;;
    esac
    shift
done


umask 022

cd $DBDIR

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

#if [ f$FLAG != fn ]; then
#    if [ -x $MAILBIN/${DBFILE}-retrieve.pl ] ; then
#	$MAILBIN/${DBFILE}-retrieve.pl
#    else
#	if [ -x $MAILBIN/spamlist.py -a -r spamlist_sources ] ; then
#	    $MAILBIN/spamlist.py spamlist_sources > ${DBFILE}.spam.new && \
#		mv  ${DBFILE}.spam.new ${DBFILE}.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"  ${DBFILE}.spam  file.
#	    #
#	    > ${DBFILE}.spam.new
#	   lynx -source http://www.webeasy.com:8080/spam/spam_download_table \
#		| sed -e '1,$s/^@/./' \
#		>> ${DBFILE}.spam.new
#	   lynx -source http://www.sprocket.com/Security/SpamDomains | \
#		awk 'NF > 0 {printf ".%s\n",$1}' >> ${DBFILE}.spam.new
#	    cat ${DBFILE}.spam.new | sed 's/^@//g' | tr "[A-Z]" "[a-z]" | \
#		    sort | uniq > ${DBFILE}.spam.new2
#	    if [ `grep -c cyberpromo ${DBFILE}.spam.new` -gt "0" ]; then
#		mv ${DBFILE}.spam ${DBFILE}.spam.old
#		mv ${DBFILE}.spam.new2 ${DBFILE}.spam
#		rm -f ${DBFILE}.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 ${DBFILE}.src

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

  # ${DBFILE}.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 ${DBFILE}.relay.manual ] ; then
    echo "# -------------------------"
    echo "# ${DBFILE}.relay.manual:"
    cat ${DBFILE}.relay.manual | \
    sed -e 's/#.*$//' | \
    awk '/^#/{next;}
	NF >= 1 {printf "%s = _full_rights\n",$0;next;}'
  fi
  if [ -f ${DBFILE}.relay ] ; then
    echo "# ------------------"
    echo "# ${DBFILE}.relay:"
    cat ${DBFILE}.relay | \
    sed -e 's/#.*$//' | \
    awk '/^#/{next;}
	NF >= 1 {printf "%s = _full_rights\n",$0;next;}'
  fi

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

  # ${DBFILE}.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 ${DBFILE}.spam -o -f ${DBFILE}.spam.manual ] ; then
    echo "# ---------------------------"
    echo "# ${DBFILE}.spam{,.manual}:"
    ( if [ -f ${DBFILE}.spam ] ; then
	cat ${DBFILE}.spam
      fi
      if [ -f ${DBFILE}.spam.manual ] ; then
	cat ${DBFILE}.spam.manual
      fi ) | tr "[A-Z]" "[a-z]" | \
    sed -e 's/#.*$//' | \
    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
) > ${DBFILE}.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 ${DBFILE}-new ${DBFILE}.dat
then
  :
else
  rc=$?
  echo "${DBFILE} control database makedb failure rc= $rc"
  exit $rc
fi

case $DBTYPE in
dbm)
	mv ${DBFILE}-new.dir  ${DBFILE}.dir
	mv ${DBFILE}-new.pag  ${DBFILE}.pag
	;;
ndbm)
	mv ${DBFILE}-new.dir  ${DBFILE}.dir
	mv ${DBFILE}-new.pag  ${DBFILE}.pag
	;;
gdbm)
	mv ${DBFILE}-new.gdbm ${DBFILE}.gdbm
	;;
btree)
	mv ${DBFILE}-new.db   ${DBFILE}.db
	;;
esac

exit 0
