http://www.sxpress.com/~henry/qmail-pop-imap-web-howto.htm
http://www.arda.homeunix.net/mailsetup_old.html

Install Courier IMAP

You have downloaded the tarball earlier. You are now yourself (NOT root). From your home directory:

tar -xzf courier-imap-1.4.3.tar.gz
cd courier-imap-1.4.3
./configure --without-authdaemon --without-authldap --mandir=/usr/local/man
make
make check
su
umask 022
make install
make install-configure

You have just made and installed the Couried IMAP binaries from the source. Now you need to configure it so that it will start up right.

Configure

If you happen to read Courier IMAP's web page, you will see some discussion on virtual mailboxes, authdaemon, and Courier's POP. We have a dedicated e-mail server here so we did not bother with virtual mailboxes. As for authdaemon, this requires another process to be running in the background and we did not particularly feel like running another process. We also like Qmail's POP better.

You are now root. You need to edit the IMAP configuration file: vi /usr/lib/courier-imap/etc/imapd

There are only a few things that need to be changed. I am listing the lines that we changed:

ADDRESS=IP Address of your server

AUTHMODULES="authpam"

IMAP_CHECK_ALL_FOLDERS=1

IMAP_MOVE_EXPUNGE_TO_TRASH=1

Leave the other lines as default. Only the ADDRESS and AUTHMODULES are essential. We like the other two functions so because we want to mimic the Exchange 2000 server that we test drove. Also, PAM is installed by default with Red Hat Linux 7.1. Now make some links to make life easier:

ln -s /usr/lib/courier-imap/libexec/imapd.rc /etc/init.d/imapd
ln -s ../init.d/imapd /etc/rc0.d/K31imapd
ln -s ../init.d/imapd /etc/rc1.d/K31imapd
ln -s ../init.d/imapd /etc/rc2.d/S81imapd
ln -s ../init.d/imapd /etc/rc3.d/S81imapd
ln -s ../init.d/imapd /etc/rc4.d/S81imapd
ln -s ../init.d/imapd /etc/rc5.d/S81imapd
ln -s ../init.d/imapd /etc/rc6.d/K31imapd

We made the counter in the rc directories 1 greater than Qmail because we want to start Qmail first when we reboot. To start (and stop) Courier IMAP, do this:

/etc/init.d/imapd start
/etc/init.d/imapd stop

Testing

Startup Courier IMAP and test it. From any IMAP client (I use Outlook Express), once you set it up, it will look for IMAP folders from your server. In /var/log/messages, you should also see a bunch of PAM authentication messages. In the Inbox, you should see the messages from TEST.receive. You should also be able to create new IMAP folders, subscribe/unsubscribe to these folders. Furthermore, you should also be able to move messages to/from any folder to any other folder.

After you have created some IMAP folders, you can check to see what your Maildir directory looks like. You should see several more directories (with a "dot" in the beginning of the directory name, i.e. you have to do ls -al to see them).

Setting up /etc/skel

This Maildir directory structure, especially with all the IMAP folders, gets complicated. So, as the root user, I recommend the following:

mkdir -p /etc/skel/Maildir/cur
mkdir -p /etc/skel/Maildir/new
mkdir -p /etc/skel/Maildir/tmp
mkdir -p /etc/skel/Maildir/.Trash/cur
mkdir -p /etc/skel/Maildir/.Trash/new
mkdir -p /etc/skel/Maildir/.Trash/tmp
mkdir -p /etc/skel/Maildir/.Drafts/cur
mkdir -p /etc/skel/Maildir/.Drafts/new
mkdir -p /etc/skel/Maildir/.Drafts/tmp
mkdir -p /etc/skel/Maildir/.Sent\ Items/cur
mkdir -p /etc/skel/Maildir/.Sent\ Items/new
mkdir -p /etc/skel/Maildir/.Sent\ Items/tmp
chmod -R 700 /etc/skel/Maildir

Now, whenever we create a new user with the useradd command, the new user will get a set of IMAP folders that mimics Exchange 2000. The Trash folder will also catch all the e-mails that the user expunges. The .Sent\ Items directory mimics Exchange 2000's Sent Items folder and it will catch the sent mails from IMAP clients (this is not guaranteed to work, for me, only IMP uses the Sent Items folder; I also got Outlook Express 5 to use the Sent Items folder once but Outlook 2000 and Outlook Express 6 definitely puts the sent e-mails in the local Sent Items folder, and not on the IMAP server).

You can also setup IMAP folders (in the Maildir format) with the maildirmake command. However, beware that you have two different maildirmake commands, one from the Qmail distribution located at /usr/bin/maildirmake (which is linked to /var/qmail/bin/maildirmake) and another from the Courier IMAP distribution located at /usr/lib/courier-imap/bin/maildirmake. The command from the Courier IMAP distribution does a lot more things than that from the Qmail distribution, i.e. creating shared folders. I have included a section on shared/public folders near the end of this document. Setting up quota

The best way to control disk space quota is via the operating system. Hence, it is also useful to setup quotas for the /home directory. To do this, you have to edit the /etc/fstab file. I replace this line:

LABEL=/home  /home  ext2  defaults  1 2 

With this line:

LABEL=/home  /home  ext2  defaults,usrquota  1 2 

Now, you need to reboot the server for this to take hold. It is a good time to reboot the server anyway, to test whether all the boot up scripts for Qmail and Courier IMAP will work properly.

After the server reboots, you should see that the Qmail stuff is running with supervise, a bunch of multilog processes, and some Courier IMAP related processes. To complete the quota setup, do this:

su
touch /home/aquota.user
chmod 600 /home/aquota.user
/usr/sbin/quotacheck /home 

quotacheck will report some errors because the aquota.user file that you just created is garbage and quotacheck just made it right. I also created this script to make life simpler vi /root/newuseradd

#!/usr/bin/perl
#
# Wrapper for useradd and setquota
# Usage: newuseradd [username] [password] "[comment]"

use POSIX;

if ($ARGV[0] eq '' || $ARGV[1] eq '' || $ARGV[2] eq '') {
    print "Usage: sxuseradd [username] [password] \"[comment]\"\n";
    exit; }

$username = $ARGV[0];
$comment = $ARGV[2];

# Encrypt password
srand($$^time&$ENV{RANDOM});
$salt = seedchar().seedchar();
$password = crypt($ARGV[1],$salt);

system "/usr/sbin/useradd -g client -c \"$comment\" -p $password $username";
system "/usr/sbin/setquota /home 0 10000 0 0 $username";

exit;

sub seedchar {
    ('a'..'z','A'..'Z','0'..'9','.','/')[rand(64)];
}

This assumes that you want to give new users a 10MB disk quota for e-mail. I also used crypt instead of the PAM thing so this does not give you as good an encrypted password as Red Hat's default useradd. Then, I added the following command aliases to root's .bashrc file:

echo "alias useradd=/root/newuseradd" >> /root/.bashrc

And don't forget to add the client group and make the new script executable:

/usr/sbin/groupadd client
chmod 755 /root/newuseradd 

So, now, every time you do an useradd, you will also take care of the disk quota. Note that Courier IMAP has other ways of implementing quota that are more "elegant". However, from the web site discussion, it is not as robust as implementing quota control on the systems level.

Testing

Setup a few new users to make sure that they all have the correct Maildir directory structure. Check with the IMAP client to make sure that (1) you can login and (2) the correct IMAP folders show up. If you don't care for the web interface for IMAP, you can stop now.

Shared Folder (Public Folder) in (Courier) IMAP

(This only works with Courier IMAP 1.3.9)

Another useful thing to add to your e-mail server is the concept of a public folder. Using Courier IMAP, you can create a real public folder (accessible by all users), or a folder that is only accessible to users in a particular group. This is a condensed version of this page and the "man pages" (man maildirmake).

First we need to create a directory that can hold a collection of folders that can be shared:

/usr/lib/courier-imap/bin/maildirmake -S maildir_path

Note that I am using the full path of maildirmake because there are two copies of this on the server, one from Qmail distribution and the other from the Courier IMAP distribution. We want to use the one from the Courier IMAP distribution. Also note your location when you run this command. If you are not careful, you may create a directory somewhere that is not secure. Also, don't make the maildir_path the default Maildir directory.

Now we will create a mail folder that gives read/write permission to all users in a group. Note that this, by itself, actually does not control group access, it merely sets up the proper file permission flags. You will also need to first define users and groups (and which users belong to which groups) to make this work:

/usr/lib/courier-imap/bin/maildirmake -s write,group -f folder_name maildir_path
chgrp -R groupname maildir_path/.folder_name

Now, all users in the groupname will be able to access this folder. There are other options for the -s switch. Check the "man pages" for more options. Specifically, if you want to create a public folder accessible by every user, just use -s write

If the group permissions are already setup, the above 2 commands can be run by the user, to create a shareable IMAP folder in the user's home directory. Make sure that you have quotas setup because a user can run an IMAP folder where every user on the system can deposit files into this user's home directory structure.

Lastly, you want to link this shared folder to a user's IMAP folder. Become this user and run this command:

/usr/lib/courier-imap/bin/maildirmake --add nickname=maildir_path $HOME/Maildir

nickname is the name definition that will appear in the IMAP folder list. Note that you can have multiple collections of shared folders as defined by many maildir_path and also multiple mail folders in each maildir_path each with varying permission settings. The user will only be able to see mail folders in the shared folders that are "added" to the the user's $HOME/Maildir and only those mail folders with the appropriate file permission setting.

The directory structure created by this command is rather complex. I recommend that you use this command to add and delete links to shared folders. To delete a folder, do this:

/usr/lib/courier-imap/bin/maildirmake --del nickname $HOME/Maildir

You can also define global shared folders, instead of running the -add command for every user. Do this: vi /usr/lib/courier-imap/etc/maildirshared and each line in this file takes the following form:

nicknamemaildir_path