Another look at shared spam folder under Courier IMAP

This article is meant to be a continuation from my previous attempt to integrate SpamAssassin with Courier IMAP. Quite a few people emailed me asking about receiving new emails on a shared Courier IMAP folder. In my original implementation, all the flagged spams will be delivered to a shared maildir by procmail, however Courier does not seem to pick them up if you are not the recipient of these new emails, i.e. you don't actually own the files in the maildir. I didn't really care until recently. But now I want to try some Bayesian spam classifiers that is built into later versions of SpamAssassin, and would like to get shared IMAP folders to work so I can set up spam and ham buckets. Thus I started my quest to look again at receiving new emails in shared folders in Courier IMAP.

At the point of writing, my mail server is still running an older version of Courier IMAP, on my old Mandrake 9.0. Version 1.6.2 to be exact, where as 2.1.0 is already available on their website. I might upgrade when I can overcome procrastination.

Documentation

First of all, there are some of the official documentations on the shared folder implementation on Courier IMAP.

Step Through

My aim is to ask procmail to deliver everyone's spam emails into "shared.System.Spam" IMAP folder. All my mail-receiving users belong to a unix group called "users".

  1. I need to create the shared maildir first. I put all my shared folders under "/var/spool/Maildir", so first I will create a "System" sharable maildir, and then create a "Spam" folder inside it. Run these commands as root.

      # maildirmake -S /var/spool/Maildir/System
      # maildirmake -s write -f Spam /var/spool/Maildir/System
      # ls -lF /var/spool/Maildir/System/.Spam
      total 2
      drwxrwxrwt    2 root     root           48 Aug 20 07:28 cur/
      -rw-------    1 root     root            0 Aug 20 07:28 maildirfolder
      drwxrwxrwt    2 root     root           48 Aug 20 07:28 new/
      drwxrwxrwt    2 root     root           48 Aug 20 07:28 tmp/
  2. The problem with the default "maildirmake -s write" is that it creates directories with mode 1777. Because of this sticky bit (mode 't' displayed in directory listing), you cannot remove files that does not belong to you. However, it is necessary for Courier IMAP to move all email files from 'new/' to 'cur/' when a user tries to select that folder. Thus the files that cannot be moved, i.e. files belong to other users, will not be picked up by Courier IMAP. That is the reason you cannot see other users' newly delivered emails, even though these files do exist in the shared folder.

    The quick 'n' dirty solution would be turning off the sticky bit. But we also want to restrict the access to our user group only. So..

      # chgrp -R users /var/spool/Maildir/System/.Spam
      # chmod 1770 /var/spool/Maildir/System/.Spam/[cnt]??
  3. Finally, you want to link the shared folder to your own maildir. Run this as a regular user.

      $ maildirmake --add System=/var/spool/Maildir/System $HOME/Maildir

    You might need to run the above command for every user which needs to access the sharable maildir. Alternatively, you can set up a file called "/etc/maildirshared" (check the maildirmake man page for the exact location) to have the following configuration:

      System    /var/spool/Maildir/System

    Note that it is a <tab> character between "System" and the directory.

  4. Now you should be able to subscribe to the shared IMAP folder through your favourite mail user agent (Mozilla Mail in my case)! You should also be able to see all the new emails delivered to this folder, even if they are addressed to another user.

Now I also use the same shared folder to feed the Bayesian spam classifier, in case any spam email slipped through SpamAssassin's heavy guard. All I need to do is to drag that uncaught spam from my INBOX and drop it into the "shared.System.Spam" folder, and Bayesian learner will step in once a day to analyse the spams.