#! /bin/sh -e

# Produce a list of dictionary attack attack names that can be used
# as spam traps.  The list is generated in two forms, one to be
# added to /etc/mail/aliases to let sendmail accept Rcpt_To commands
# naming the traps and the second to be added to a sendmail access_db file.

# If you do not use the list of aliases, then you must use another
# mechanism to tell sendmail to accept for unknown users.  Sendmail
# normally rejects mail for unknown users without giving it to a milter
# filter such as dccm.

# For example, `filter-dict-attack -s filter-dict-attack` will generate
# two files that can be added to sendmail aliases and access_db files.
# The aliases point to postmaster because that mailbox should always
# be valid.  Spam addressed to the traps should be discarded before it 
# reaches the postmaster mailbox.

# Beware that `sendmail -bv` is slow on some systems, and so this script
# is often extremely slow.  It should be once, to generate files that
# are then maintained manually.

# Copyright (c) 2003 by Rhyolite Software
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE
# BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
# OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
# SOFTWARE.
#	Rhyolite Software DCC 1.2.16-1.6 $Revision$


exec </dev/null

USAGE="`basename $0`: [-x] [-s src] [-t tgt]"
SRC=dict-attack-aliases
TGT=dict-traps

while getopts "xd:s:t:" c; do
    case $c in
    x) set -x;;
    s) SRC="$OPTARG";;
    t) TGT="$OPTARG";;
    \?) echo $USAGE; exit 1;;
    esac
done
shift `expr $OPTIND - 1`
if test "$#" != 0; then
    echo $USAGE
    exit 1
fi

TGT_ALIAS=$TGT.aliases
TGT_DB=$TGT.access

if test ! -f $SRC; then
    echo 1>&2 "$SRC does not exist"
    exit 1
fi
if test -f $TGT_DB; then
    echo 1>&2 "$TGT_DB already exists"
    exit 1
fi
if test -f $TGT_ALIAS; then
    echo 1>&2 "$TGT_ALIAS already exists"
    exit 1
fi

date >$TGT_ALIAS

while read NM PMASTER; do
    if test "$PMASTER" != postmaster; then
	echo "$NM $PMASTER" >>$TGT_ALIAS
	continue
    fi

    NM=`expr "$NM" : '\(.*\):'`
    if sendmail -bv "$NM" >/dev/null; then
	NM="#$NM"
    else
	echo "To:$NM@"'	DCC: "DISCARD dictionary spam"'		\
	    | sed -e 's/^[^@]\{1,6\}@/&	/'			\
		-e 's/^[^@]\{1,14\}@/&	/'			\
		-e 's/^[^@]\{1,22\}@/&	/'			\
	    >>$TGT_DB
    fi
    echo "$NM:	postmaster"					\
	    | sed -e 's/^[^:]\{1,6\}:/&	/'			\
		-e 's/^[^:]\{1,14\}:/&	/'			\
	    >>$TGT_ALIAS
done <$SRC
