DanNorth.net

September 9, 2007

Virtual mailboxes with courier-imap and postfix

This is the first of an occasional series of posts about Linux systems administration. I’ve been an on-off Linux sysadmin for about, well, my first Linux was Slackware on a stack of 3.5” floppies. Every now and then I do something “fiddly” and I want to capture these episodes in case I ever need to do it again, or in case someone else wants to and they find this useful.

I run Debian Testing on kenny, the server that hosts http://dannorth.net, http://behaviour-driven.org and a bunch of other websites, blogs and wikis. (Random plug: it’s hosted by the fine folks at SolarVPS. I have no affiliation with them but they rock.) It seems a good balance between Debian Stable – which is rock solid but always about a year out of date – and Unstable, which requires updating far too often and hosed my old server (cartman – can you see a theme here?).

Mostly it Just Works. In particular WordPress and MoinMoin are a joy to configure and use. I get change out of about 10 minutes to add a new blog.

I also use it as a mail server, using postfix for sending and receiving mail and courier-imap for reading it. I tried a number of mail servers and settled on postfix after dismissing sendmail and qmail as just too complicated and exim after staring at the impenetrable documentation one time too many.

I found it pretty easy to set up secure SMTP over TLS and secure IMAP over SSL, but I stumbled at setting up virtual mail addresses. This article is about how to do that using postfix and courier-imap. It’s pretty straightforward once you know where all the moving parts are, but they were less than obvious to me.

Virtual mail addresses

Mostly you send email to bob@address.com and it turns up in bob’s mailbox in bob’s login account. Sometimes you don’t want this. For instance, I host about 20 domain names on kenny, and addresses like info@blah.com or sales@blah.com need to go to specific people. That’s the first type of virtual addressing, known as virtual alias domains.

The second case is “more” virtual – the user doesn’t even need to exist on the server with a regular login account. Postfix puts the mail into a special directory, and courier-imap presents that directory as the mailbox when the user “logs in” over IMAP. This is where all the moving parts come in. This is known as virtual mailbox domains.

Setting up postfix for virtual alias domains

This is the easier of the two, since the mail ends up in a real email address, so there is no corresponding configuration on the courier-imap side. This information came from the Postfix Virtual Domain Hosting Howto.

In /etc/postfix/main.cf add the following lines:


    virtual_alias_domains = dannorth.net
    virtual_alias_maps = hash:/etc/postfix/virtual

 

This says that postfix will treat the name dannorth.net as a virtual alias domain and will use the file /etc/postfix/virtual to do the mappings. Your /etc/postfix/virtual might look like this:


    # deliver to local account
    dan@dannorth.net    dan

    # forward to another mail address
    example@dannorth.net  dan@example.com

 

Once you have this file configured, run the command:


    postmap /etc/postfix/virtual

 

to create the hash database that postfix will use, then run:


    postfix reload

 

to update the configuration.

Setting up virtual mailbox domains

Ok, there are several moving parts here. We need: * a directory to deliver the mail to * to tell postfix to deliver it there * to enable virtual users in courier * to tell courier where the virtual users’ mail lives

In this example, I’ll set up two virtual users, fred and barney, at example.com.

System accounts and directories

The virtual users’ files need to be owned by someone, so we’ll create a “fake” user and group. I’m using vmail for both the user and group names, with uid and gid both set to 5000.

From a root prompt:


    # groupadd -g 5000 vmail
    # useradd -g vmail -u 5000 vmail
    # mkdir -p /home/vhosts/example.com
    # chown vmail:vmail /home/vhosts/example.com

 

Configuring postfix for virtual mailbox domains

In /etc/postfix/main.cf


    virtual_mailbox_domains = example.com
    virtual_mailbox_base = /home/vhosts
    virtual_mailbox_maps = hash:/etc/postfix/vmailbox
    virtual_minimum_uid = 100
    virtual_uid_maps = static:5000
    virtual_gid_maps = static:5000

 

This is pretty self-explanatory. It says example.com is a magic virtual mailbox domain, all users and groups map to a fixed number (you can get cleverer than this but I’m not worried about it for now), and that all the interesting stuff is in /etc/postfix/vmailbox. The minimum uid is postfix’s safety measure in case I do something stupid. It means I can’t accidentally let someone have access to system files.

Now let’s look at /etc/postfix/vmailbox:


    fred@example.com            example.com/fred/
    barney@example.com        example.com/barney/

 

The magic here is the / at the end of each line. This says to use maildir format (the format courier-imap is expecting) rather than clunky old mbox format. Postfix will create the appropriate directory structure for fred and barney,

Again we create a hash of this for postfix:


    # postmap /etc/postfix/vmailbox
    # postfix reload

 

Phew! That’s the postfix side done. Now stop for a cup of tea.

Configuring courier-imap for virtual mailboxes

On Debian, courier imap runs as three executables, each with separate init.d scripts. courier-imap and courier-imap-ssl are the imap servers themselves (I run courier-imap bound to localhost for webmail). courier-authdaemon is the chap that does all the authentication. That’s the one we’re interested in.

Firstly, we need to enable virtual users. In the file /etc/courier/authdaemonrc you need to make sure your authmodulelist setting contains authuserdb as one of its authentication mechanisms. Mine looks like this:


    authmodulelist="authuserdb authpam"

 

Don’t forget to tell the auth daemon you’ve made a change:


    invoke-rc.d courier-authdaemon reload

 

Courier uses a file called /etc/courier/userdb to store virtual user mappings, but you don’t usually edit this file yourself. It has an arcane format (using tabs and pipes as delimiters) and should be left well alone. Instead, courier provides you with some command line tools to manipulate it.

To create an entry for fred, we do this:


    # userdb fred set uid=vmail gid=vmail home=/home/vhosts/example.com/fred mail=/home/vhosts/example.com/fred

 

(That should all be on one line – your browser might wrap it.) Then set a password for fred:


    # userdbpw -md5 | userdb fred set systempw

 

Do the same for barney. Finally we build the hashed user database that courier will actually use:


    # makeuserdb

 

Note: don’t forget to run makeuserdb after making any changes to the virtual user data otherwise courier won’t know.

Testing the configuration

Firstly, try sending an email to the virtual user. Postfix should create the maildir structure under example.com/fred. Then try connecting to courier to read the mail. If you find you are getting authentication problems from the courier side, you can try setting DEBUG_LOGIN=1 in /etc/courier/authdaemonrc and restarting the auth daemon. Don’t forget to switch it off again once it’s working.

Filed under: linux — Dan North @ 10:23 pm

RSS feed | Trackback URI

11 Comments »

Trackback by AgileJoe
2007-09-09 23:08:14

Concerns about NBehave…

This is an attempt to answer Scott Bellware’s concerns and questions about NBehave . Scott, first off…

 
2007-09-10 03:47:56

[...] Virtual mailboxes with courier-imap and postfix This information came from the Postfix Virtual Domain Hosting Howto. In /etc/postfix/main.cf add the following lines:. virtual_alias_domains = dannorth.net virtual_alias_maps = hash:/etc/postfix/virtual … [...]

 
Comment by Steve Purcell
2007-09-10 08:50:49

Dude, you should try dovecot in place of Courier IMAP. Not only is it lighter and generally spiffier than Courier, but postfix understands how to talk to dovecot’s auth process; that means you can set up a user DB for dovecot, then easily enable authenticated SMTP for your users by telling postfix to talk to dovecot.

Missing ya; hope you’re well.

Comment by Dan North
2007-09-10 13:55:25

Missing you too :)

I’ll take a look at dovecot when I get a chance. It’s going to be a while though..!

 
 
Comment by Gwiz
2007-12-04 19:13:03

Thank you very much for this, i have been looking for a nice How-to like this.
One problem though: in the /home/vhosts/example.com/fred , i had to make the maildir by myself, including the subdir “cur” “new” “tmp”
For the first user it automagically created by itself, but for the other users i had to do it myself.

Regards,

G

 
Comment by Matthew Subscribed to comments via email
2008-01-19 16:06:32

Hey, Great tutorial thanks. Just one question, does it work for courier-pop too? Any additional settings required? I am under the assumption that courier-pop and courier-imap use the same internals?

Comment by Dan North
2008-01-19 19:17:22

Hi Matthew.

Courier POP3 (and POP3/S) will operate in exactly the same way. I prefer IMAP because then the server always holds your email and your local mail client is just a copy. So you can use a webmail client (I use squirrelmail) if you can’t get to your usual computer, and you’ll still have access to all your mail.

In any event, I took Steve Purcell’s advice (a few comments above) and switched to dovecot. He’s a very smart man and dovecot is lovely. Its authentication module integrates really nicely with postfix which makes managing virtual user accounts a dream.

 
 
Comment by Aidan Subscribed to comments via email
2008-02-11 10:17:38

Made me smile to find your blog when searching for the answer to a postfix problem. It was #4 when I searched for ‘postfix virtual user database’. Anyway, hope things are going well. Funnily enough, this post answered my question ;-)

 
Comment by Matthew Subscribed to comments via email
2008-02-11 14:09:42

Hey Dan,

Just a quick note – took your/Steve Purcell’s advice and switched to Dovecot – got it all working great in the end with postfix running off the same authentication system.

Wrote a couple of bash scripts and its really easy to manage too :)

Thanks,
Matthew

 
Pingback by Les liens du jour
2008-02-22 09:15:29

[...] DanNorth.net » Virtual mailboxes with courier-imap and postfix [...]

 
Comment by Dan Farrell
2008-03-26 02:10:50

Thanks much for the very helpful write-up.

 
Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use in your comment.

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Powered by WordPress