On Wed, Oct 02, 2013 at 05:17:53PM +0200, Robin Kipp wrote:
> So, now that spamassassin is flagging junk mail, I really would like a way of 
> having said junk sorted into a different folder for each user. I'm aware this 
> can also be done on the MUA side, but on some end-user devices (such as 
> smartphones), junkmail filtering is often not supported very unfortunately.
> So, I was just wondering… Is there any way I could do this on the server side?
> My main problem really is that I'm not sure where I'd best set this up - but 
> I'm guessing I'd have to do it in Postfix, as I use the 'virtual' local 
> delivery agent to drop incoming mail into the appropriate directories. Also, 
> the mailboxes are stored in maildir format if that makes any difference…
> If anyone has anything like this working or could point me in the right 
> direction I would greatly appreciate that!
> Many thanks :-)

I have what you describe going also using postfix, and maildrop for
filtering. The relevent part of my /etc/postfix/main.cf reads:

virtual_mailbox_domains = example.com
virtual_mailbox_base = /var/mail/vhosts
virtual_mailbox_maps = hash:/etc/postfix/vmailbox
virtual_minimum_uid = 100
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000
maildrop_destination_recipient_limit = 1
virtual_transport = maildrop
virtual_mailbox_limit = 0

In my /etc/postfix/master.cf I have:

# maildrop. See the Postfix MAILDROP_README file for details.
# Also specify in main.cf: maildrop_destination_recipient_limit=1
#
maildrop  unix  -       n       n       -       -       pipe
  flags=DRhu user=vhosts argv=/usr/bin/maildrop
# /var/mail/vhosts/maildrop_filters/${domain}/${mailbox}

My /etc/postfix/vmailbox reads:

i...@example.com    na

Don't forget to run:

postmap vmailbox
postfix reload

when you're ready to go live with this setup.
All of the above so far was configured with the help of
/usr/share/doc/postfix with the postfix-doc package installed, and
with the help of the postfix man pages. I might also have used
/usr/share/doc/postfix-doc/examples, but I don't remember if I
actually did or not.

Next, I created a user called vhosts and a group called vhosts. Both
the uid and gid are 5000. Both the uid and gid must match what you
have in /etc/postfix/main.cf. The home directory of vhosts is of
course /var/mail/vhosts, which must also match in
/etc/postfix/main.cf.

Ok. The below is partly from postfix documentation, partly from
maildrop documentation, and partly from the web. In /var/mail/vhosts I
have the following:

example.com, which is a directory (more on that below)
maildrop_filters which is a directory (more on that below)
spamass_domain_list, which is a text file (more on that below)

I'll explain the above one by one. In the directory called example.com
I have a typical maildir called info for the user
i...@example.com. This maildir has another maildir inside it called
.Spam/ which as you might guess is where all the spam mail goes.

Let's move on to the maildrop_filters directory. In this directory, I
have another directory called example.com. In this example.com
directory, I have a file called ... you guessed it ... info. This info
file reads:

DEFAULT="$HOME/example.com/info/"
xfilter "/usr/bin/spamc"
if (/^X-Spam-Flag: YES/:h || /^X-Spam-Status: Yes/:h)
{
  to "$DEFAULT/.Spam/"
}
 
These are the instructions used by maildrop to put ham in info/, and
spam in info/.Spam/ based on the message headers.

Finally, the text file spamass_domain_list reads:

example.com

This file contains all the domains to be checked for ham/spam by the
script I'll describe below.

In /etc/cron.daily, I have a script called spam_script. I found this
script on the web, and modified it as needed. I don't remember where I
found it at, but a web search should yield that info probably
easily. The script reads:

#!/bin/bash
#
# Script to learn spam using sa-learn. Just edit the default names of
#your
# spam folder using the variables
# below.
#

SPAM_FOLDER=.Spam

#
#
########################################

DOMAIN_LIST=`cat /var/mail/vhosts/spamass_domain_list`
SALEARN=`which sa-learn`
LEARN_SPAM="$SALEARN --dbpath /var/mail/vhosts/.spamassassin --no-sync
--spam"
LEARN_HAM="$SALEARN --dbpath /var/mail/vhosts/.spamassassin --no-sync
--ham"
VPOPMAIL_HOME=/var/mail/vhosts

for DOMAIN in $DOMAIN_LIST; do

        DOMAIN_USERS="`ls /var/mail/vhosts/$DOMAIN/
        --ignore=postmaster`"

        for USER in $DOMAIN_USERS; do

        $LEARN_SPAM $VPOPMAIL_HOME/$DOMAIN/$USER/$SPAM_FOLDER/cur

                $LEARN_HAM $VPOPMAIL_HOME/$DOMAIN/$USER/cur

                done

done

$SALEARN --dbpath /var/mail/vhosts/.spamassassin --sync
/usr/local/sbin/spam_fix
 
I see some of the lines wrapped when I pasted it. So, you'll want to
fix those.
The spam_fix script was written by me. It fixes ownership which gets
changed when salearn is run. The spam_fix script reads:

#!/bin/sh
# script to fix ownership for spamassassin files
chown vhosts ~vhosts/.spamassassin/bayes_*
 

That's it! When mail comes in for i...@example.com, postfix passes it
to maildrop. Then maildrop runs it through spamc, and places it into
either info/ or info/.Spam based on the spam header which spamassassin
adds to the message. This particular user my mother, checks her mail
through sqwebmail. She checks the .Spam folder (which shows up as just
Spam) first. She moves ham messages out of it into her inbox, and
deletes the spam mail. Then she moves any spam which ended up being
tagged as ham into the Spam folder. Any messages which were moved from
inbox to Spam, and from Spam to inbox have to stay there for 24 hours,
so they can be learned as being what they are. 

A couple of notes on this setup. This doesn't use amavisd of
course. The reason for that is that my mother wants to see every mail
that comes in, regardless of what it is. So, I decided it would be
better to simply accept all mail, and deal with filtering it once the
smtp exchange is over. I also thought it would be better and more
elegant to have two additional folders called Newham, and Newspam. As
the names suggest, any incorrectly tagged ham and spam would be placed
in each respective folder by my mother for the spam_script to learn
from during the daily cron job. My mother found this confusing though,
so it is why the way it is now. Also, one disadvantage here is too
that all virtual users share a single .spamassassin directory in
~vhosts. This is a spamassassin problem though, and I couldn't find a
way around it where each virtual user has their own .spamassassin,
rather than all virtual users sharing a single one.

Hope my way of doing spam filtering at least gets you started, and
anyone else who is interested too.

Greg


-- 
web site: http://www.gregn..net
gpg public key: http://www.gregn..net/pubkey.asc
skype: gregn1
(authorization required, add me to your contacts list first)

--
Free domains: http://www.eu.org/ or mail dns-mana...@eu.org


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: http://lists.debian.org/20131003012245.gb22...@gregn.net

Reply via email to