dlostboy (at) lostinfo (dot) com 

Home | Journal | Multimedia | Files | Other | Links | About

      FreeBSD 4.4 HOW-TO for Courier-IMAP w/ Procmail

Sometime or another, you may decide that using IMAP is the way to go. FreeBSD does not ship with a IMAP daemon but has several in the ports tree. By far the easiest to use is uw-imapd. Simply by compiling this port and editing your inetd.conf to point to it, you've gained access to imap folders. The problem with uw however is that it continues to put mail in /var/mail/username and thus must also use that file for it's own data and that it's really not a fast daemon in my experience (being traditionally called from inetd.conf doesn't help). Having that extra data in the mail file causes a strange email to show up every time you use POP3 on the box. Additionally, it's not nearly as robust - not scaling well for large email collections. Many people who switch to IMAP find that leaving mail on the server is rather handy, so it's easy to wind up with an email box that's 20, 50, even 100MB (quota willing). The option is to use MailDir's which is the idea of making a directory for email and putting each message into it's own file instead of one big one.

Now if we change to using MailDir's we'll also have to change our LDA (Local Delivery Agent) because mail.local will not deliver mail to MailDirs. We'll continue to use sendmail for our MTA (Message Transfer Agent) since we like it. In looking for a good solution, I decided that a widely used IMAP daemon to use would be Courier-IMAP in conjunction with the LDA ProcMail. Both are in ports (although the Courier port leaves a little to be desired). Lastly, if you were using qpopper for POP3, you'll have to walk away from that too due to the MailDirs. Thankfully Courier ships with a POP3 daemon too so we're ok there.

Courier stays running (as opposed to being called from inetd) and doesn't use alot of ram. That's always good. Additionally, using ProcMail will allow us to do lots of "cool" things like vacation messaging, autoresponders, forwarding, etc..

The way we will set things up will be using the default authentication built into Courier (which is fine, and allows us to use less user accounts on the machine).

  • cd /usr/ports/mail/courier-imap; make install (do *not* clean)
  • cd /usr/ports/mail/procmail; make install clean

    We are going to use this structure for our accounts:
          /home/username
          /home/username/email/domainname.ext
          /home/username/email/domainname.ext/email_user (this is where mail will be)
          /home/username/email/domainname.ext/other_email_user

    Finally, we'll store all the authentication information in /usr/local/etc/. For these examples, I'll use dlostboy@lostinfo.com as my email address, assuming that I have a user account called dlostboy, and that I also want to get email at info@lostinfo.com which will have an alias of sales@lostinfo.com (an alias is an email address that is simply mapped to another as opposed to having separate mail space/password).For both accounts, I'd just like to use the password "mypass".

  • mkdir -p /usr/local/etc/userdb/
  • chmod 700 /usr/local/etc/userdb/
  • mkdir -p /home/dlostboy/email/lostinfo.com/
  • maildirmake /home/dlostboy/email/lostinfo.com/dlostboy
  • chown -R dlostboy.dlostboy /home/dlostboy/email/lostinfo.com/dlostboy
  • maildirmake /home/dlostboy/email/lostinfo.com/info
  • chown -R dlostboy.dlostboy /home/dlostboy/email/lostinfo.com/info

    We will now need to get the uid/gid for "dlostboy", so if you

  • grep dlostboy /etc/passwd
    You should get a response like:
        dlostboy:*:1004:1004:dlostboy:/home/dlostboy:/bin/tcsh
    The first number is your UID and the second is your GID.
  • userdb "lostinfo.com/dlostboy@lostinfo.com" set home=/home/dlostboy/email/lostinfo.com/dlostboy
            mail=./ uid=1004 gid=1004

    That will write out a file in /usr/local/etc/userdb called "lostinfo.com" with a single line in it that will start with "dlostboy@lostinfo.com". Now we'll add our second virtual domain email user:

  • userdb "lostinfo.com/info@lostinfo.com" set home=/home/dlostboy/email/lostinfo.com/info
            mail=./ uid=1004 gid=1004

    Now we'll set our passwords, echo'ing like this is horrible in practice since it'll show up in "ps" but for this example we'll live with it...makes it easier for me to type this up.

  • echo mypass | userdbpw | userdb "lostinfo.com/dlostboy@lostinfo.com" set systempw
  • echo mypass | userdbpw | userdb "lostinfo.com/info@lostinfo.com" set systempw
  • makeuserdb (this compiles your text-based changes into the binary file courier authenticates off of)

    Now until the port is fixed (wasn't in 8/2001) we have to

  • cp /usr/local/libexec/imapd.rc /usr/local/etc/rc.d
  • cp /usr/local/libexec/pop3d.rc /usr/local/etc/rc.d
  • chmod 755 /usr/local/etc/rc.d/imapd.rc
  • chmod 755 /usr/local/etc/rc.d/pop3d.rc

    in order for the daemons to start when we reboot. For now we'll start them by:

  • /usr/local/etc/rc.d/pop3d.rc start
  • /usr/local/etc/rc.d/imapd.rc start Ok, now everything should be working *whew*. Courier should be logging messages to /var/log/maillog at this point so look there for errors if you run into any. Now lets run a test to make sure our password works (as well as imapd).
  • cd /usr/ports/mail/courier-imap/work/courier-imap-version_number/authlib
  • ./authtest dlostboy@lostinfo.com mypass
    Which should give you something like this:

          Authenticated: module authuserdb
          Home directory: /home/dlostboy/email/lostinfo.com/dlostboy
          UID/GID: 1004/1004
          Maildir: ./
          AUTHADDR=dlostboy@lostinfo.com
          AUTHFULLNAME=

    So, assuming that's working, lets test pop3d:

  • telnet localhost 110
          Trying 127.0.0.1...
          Connected to localhost.
          Escape character is '^]'.
          +OK Hello there.
          user dlostboy@lostinfo.com
          +OK Password required.
          pass mypass
          +OK logged in.
          stat
          +OK 0 0
          quit
          +OK Bye-bye.
          Connection closed by foreign host.

    That's definately a good sign. At this point, we've verified that our IMAP daemon and POP3 daemon are both working soundly. Lets move on to setting up that alias we wanted to, and using ProcMail.

    For the alias, we simply have to add it to our virtual users table. Since we're gonna assume that you want to handle many virtual domains, we'll say that you're storing VUT's for each domain separately, but all in one directory. If that's the case, and that happens to be /usr/local/etc/virtualusers/domainname.ext then we need to create a file called /usr/local/etc/virtualusers/lostinfo.com and then put this line in it:
          sales@lostinfo.com info@lostinfo.com
    Once you have that line in there, this simple script will create the master virtual users table for you. Run it after any changes are made.

          rm /etc/mail/virtusertable
          for i in `ls /usr/local/etc/virtualusers/`
          do
            `cat /usr/local/etc/virtualusers/$i >> /etc/mail/virtusertable`
          done
          /usr/sbin/makemap hash /etc/mail/virtusertable < /etc/mail/virtusertable

    Now with that done, of course we have to add our domain to local-host-names, so

  • echo lostinfo.com >> /etc/mail/local-host-names

    Now we have to change our LDA to be procmail. Open up /etc/mail/sendmail.cf in your favorite text editor and find the line that reads:       Mlocal, P=/usr/libexec/mail.lcoal, F=lsDFMAw5:/|@qPSXfmnz9P, S=EnvFromL/HdrFromL, R=EnvToL/HdrToL,
                T=DNS/RFC822/SMTP,
                A=mail.local -l

    Change that to read:
          Mlocal, P=/usr/local/bin/procmail, F=SAw5:|/@glDFMPhsfn, S=10/30,
                R=20/40,
                A=procmail -Y -a $h -d $u

    With all of that done, now we have to tell ProcMail how to deliver the mail for your domains. This is done through the /usr/local/etc/procmailrc file. This file should read:
          VERBOSE=no
          LOGABSTRACT=all
          UMASK=077
          MAILDIR=
          PMDIR=$HOME/Procmail
          LOGFILE=$PMDIR/log
          INCLUDERC=$PMDIR/procmail.rc

    GRR...what now? I don't know the MAILDIR....

  •  
     

      ©2000, ©2001 LostInformation