#!/bin/sh # sdbgrey version 2008-04-18 by Ulric Eriksson LD_LIBRARY_PATH="/usr/local/lib/mysql:/usr/local/lib:$LD_LIBRARY_PATH" PATH="/usr/local/bin:$PATH" export LD_LIBRARY_PATH PATH database_url="mysql:uid=postfix:pwd=postfix:db=greylist:host=localhost" greylist_delay=60 auto_whitelist_threshold=10 # No syslogging, just log to a file. log_file=/var/log/greylist.log sdb() { test "$verbose" && echo "sdb $1" 1>&2 sdb_client -c "$1" "$database_url" } smtpd_access_policy() { if test $auto_whitelist_threshold -gt 0; then count=`sdb "select coalesce(greyvalue,0) from greylist where greykey = '$client_address'"` if test "$count" -gt $auto_whitelist_threshold; then echo dunno return fi fi key="$client_address/$sender/$recipient" time_stamp=`sdb "select greyvalue from greylist where greykey='$key'"` now=`sdb "select unix_timestamp()"` if test -z "$time_stamp"; then time_stamp="$now" sdb "replace into greylist (greykey, greyvalue) values ('$key', $time_stamp)" fi age=`expr $now - $time_stamp` test "$verbose" && echo "request age $age" 1>&2 if test $age -gt $greylist_delay; then if test $auto_whitelist_threshold -gt 0; then sdb "replace into greylist (greykey, greyvalue) values ('$client_address', $count+1)" fi echo dunno else echo "defer_if_permit Service is unavailable" #echo "dunno Service is unavailable" fi } if test "$1" = '-v'; then verbose=1 fi exec 2> $log_file while read REPLY; do test "$verbose" && echo "Attribute: $REPLY" 1>&2 case "$REPLY" in *=* ) eval $REPLY ;; '' ) action=`smtpd_access_policy` test "$verbose" && echo "Action: $action" 1>&2 echo "action=$action" echo client_address= sender= recipient= ;; * ) echo "warning: ignoring garbage $REPLY" 1>&2 client_address= sender= recipient= ;; esac done