Maildrop filter
From VHCP Support
Courier-maildrop can do some nice mail-filtering and this is how to configure your vhcp-driven server to use maildrop:
- Add/change /etc/postfix/master.cf (one single line!):
maildrop unix - n n - - pipe
flags=R user=vmail argv=/usr/bin/maildrop /etc/courier/maildroprc ${recipient} ${user} ${nexthop}
- Add/change /etc/postfix/main.cf:
virtual_transport = maildrop maildrop_destination_concurrency_limit = 1 maildrop_destination_recipient_limit = 1
- Create /etc/courier/maildroprc:
#!/bin/sh
LOGDIR="/tmp"
logfile "$LOGDIR/maildroprc.log"
log "Filter start"
SHELL="/bin/bash"
DEFAULT="/var/spool/mail/virtual/$3/$2/"
MAILDIR=$DEFAULT
SPAM=".Spam"
VSCAN=1
SSCAN=1
# Only scan mails smaller than SCANSPAMSIZE for spam
SCANSPAMSIZE="2000000"
# Only scan mails smaller than VSCANSIZE for a virus
VSCANSIZE="2000000"
# Mailfilter directory
MAILFILTERDIR="/var/mail/mailfilter"
LOGNAME="$2@$3"
if( $VSCAN )
{
if( $SIZE < $VSCANSIZE )
{
exception {
xfilter "/usr/bin/clamscan.sh"
}
}
# check if mail is marked as virus
if(/^X-Virus-Status:.*INFECTED/)
{
exception {
`test -d "$MAILDIR/.Virus/"`
if ($RETURNCODE != 0)
{
`/usr/bin/maildirmake -f Virus $MAILDIR/`
`echo INBOX.Virus >> $MAILDIR/courierimapsubscribed`
}
MAILDIR = $MAILDIR.Virus/
SSCAN=0
}
}
}
if( $SSCAN )
{
if( $SIZE < $SCANSPAMSIZE )
{
exception {
#xfilter "/usr/bin/spamassassin -x"
xfilter "/usr/bin/spamc -f -x -u $LOGNAME"
}
}
if(/^X-Spam-Flag: *YES/)
{
`test -d $DEFAULT/$SPAM`
if($RETURNCODE != 0)
{
`/usr/bin/maildirmake -f Spam $MAILDIR/`
`echo INBOX.Spam >> $MAILDIR/courierimapsubscribed`
}
MAILDIR = $MAILDIR.Spam/
VSCAN=0
}
}
exception {
include $MAILFILTERDIR/$LOGNAME
}
exception {
to "$MAILDIR"
}
Check the line:
DEFAULT="/var/spool/mail/virtual/$3/$2/"
Is /var/spool/mail/virtual/ the right path your virtual maildirs? Please, notice that a trailing "/" means deliverance to the Maildir format. If you remove it, it means deliverance to the mbox format.
Now:
- chown vmail:mail /etc/courier/maildroprc
- chmod 600 /etc/courier/maildroprc
- Add individual filter in $MAILFILTERDIR/<emailadress> e.g.:
MAILDIR=$MAILDIR # Default mail directory
if (/^From:.*@web\.de/:h)
{
to "$MAILDIR/.WEBDE"
}
(Notice, that an individual mailfilter file, test@example.org will handle mails to that address only. If you want one included file to collect mails from a number of addresses in the same maildir, you must use another solution.)
Check the three log-lines in maildroprc. You may comment them, when everything works fine. Perhaps, you prefer to put the log elsewhere, but maildrop's user is vmail, that is, you cannot save the log in directories where vmail has no write and exec permission. Notice that you call the individual filter file from an exception statement:
exception {
include $MAILFILTERDIR/$LOGNAME
}
This entails that if there are any error in your individual filter file, it will just be ignored without being logged, and the mail will be filtered by the next exception statement. If you want to debug the individual files, remove the exception statement around the inclusion statement. Then, you will see why testmails are rejected in the /var/log/mail.log (or whereever your maillog is).
Optionally use squirrelmail serverside filter plugin to create individual filters.
