Email Server/Courier Maildrop
Views:
Part of the Email Server documentation
Contents |
Introduction
This page describes installing and configuring Courier Maildrop for use as Postfix's delivery agent and as part of a Linux virtual domain Email Server.
Courier Maildrop will let us script up the final delivery of emails, including allowing user defined rules such as forwarding, auto-replies and sorting of mails in to certain folders in the user's maildir.
Dependancies
Courier Maildrop requires the Perl Compatible Regular Expressions library (PCRE) and it's a good idea to have GDBM too (GDBM is like the Berkeley Database from what I can tell).
These are both available as packages on CentOS
# yum install gdbm # yum install gdbm-devel # yum install pcre-devel
We're also going to need Courier Authlib to be installed so that Maildrop will support it.
Installation
Courier Maildrop does not come with CentOS 5, here we're building it from source, other distro's might have it available
Find the url for the latest version of Courier Maildrop from http://www.courier-mta.org/download.php, we're using 2.0.4 in this example.
Note: It is very important that you configure and make Courier Maildrop as a normal user and not root, this is the developers' instruction.
$ cd /usr/local/src $ wget http://prdownloads.sourceforge.net/courier/maildrop-2.0.4.tar.bz2 $ tar jxvf maildrop-2.0.4.tar.bz2 $ cd maildrop-2.0.4 $ ./configure --enable-maildirquota --enable-syslog=1 $ ./make $ su # make install
Configuration/Scripts
When Postfix is configured to use Courier Maildrop in "delivery" mode, the first thing Maildrop will do is change directory to the user's (for the email being delivered) home directory. This is a PITA when you are running a virtual domain server and don't want to have to manually run several shell commands each time you add a new user to the database.
To work around this, we can wrap Courier Maildrop with a simple script that will check for the presence of the user's home directory and create it if necessary. This means that the first time a user receives an email, their home directory will be created (think "Welcome Message").
/usr/local/bin/maildrop_primer
#!/bin/bash
# /usr/local/bin/maildrop_primer -a -w 90 -d ${recipient} ${user} ${nexthop} ${sender} ${recipient}
user=$6
domain=$7
test -n "$user" && test -n "$domain"
if [ $? -eq 0 ]
then
home="/var/mail/virtualdomains/$domain/$user"
test -d "$home"
if [ $? -ne 0 ]
then
mkdir -p "$home"
chown courier:courier "$home"
chmod -R 0700 "$home"
fi
fi
/usr/local/bin/maildrop $@
The maildroprc file checks for and creates several default maildirs, including the main one. It also looks for any user specific scripts we may decide to create, any user generated filter's (generated with Horde web mail, and a default action for any messages flagged as spam. /etc/maildroprc
###########################
# Variable Initialisation #
###########################
# See notes for "Home Directory", below.
`test -z "$HOME"`
if ($RETURNCODE == 0)
{
USER=$1
NEXTHOP=$2
HOME="/var/mail/virtualdomains/$NEXTHOP/$USER"
}
SENDER=$3
RECIPIENT=$4
DEFAULT="$HOME/Maildir"
SENDMAIL="/usr/sbin/sendmail -oi"
##################
# Home Directory #
##################
# this doesn't help if we're running maildrop in deliver mode (-d switch)
# maildrop wants to immediately chdir to the user's home directory.
# We work around this by wrapping maildrop in a simple shell script
# (/usr/local/bin/maildrop_primer).
`test -e $HOME`
if ($RETURNCODE != 0)
{
`mkdir -p "$HOME"`
`chown courier:courier "$HOME"`
`chmod -R 0700 "$HOME"`
}
###########
# Maildir #
###########
`test -e "$HOME/Maildir"`
if ($RETURNCODE != 0)
{
`/usr/local/bin/maildirmake "$HOME/Maildir"`
`chown -R courier:courier "$HOME/Maildir"`
`chmod -R 0700 "$HOME/Maildir"`
}
#############
# Mailboxes #
#############
# Drafts
`test -e "$HOME/Maildir/.Drafts"`
if ($RETURNCODE != 0)
{
`/usr/local/bin/maildirmake -f Drafts "$HOME/Maildir"`
`if ! grep -q INBOX.Drafts "$HOME/Maildir/courierimapsubscribed"; then echo "INBOX.Drafts" >> "$HOME/Maildir/courierimapsubscribed"; fi`
`chown -R courier:courier "$HOME/Maildir/.Drafts"`
`chmod -R 0700 "$HOME/Maildir/.Drafts"`
}
# Sent
`test -e "$HOME/Maildir/.Sent"`
if ($RETURNCODE != 0)
{
`/usr/local/bin/maildirmake -f Sent "$HOME/Maildir"`
`if ! grep -q INBOX.Sent "$HOME/Maildir/courierimapsubscribed"; then echo "INBOX.Sent" >> "$HOME/Maildir/courierimapsubscribed"; fi`
`chown -R courier:courier "$HOME/Maildir/.Sent"`
`chmod -R 0700 "$HOME/Maildir/.Sent"`
}
# Spam
`test -e "$HOME/Maildir/.Spam"`
if ($RETURNCODE != 0)
{
`/usr/local/bin/maildirmake -f Spam "$HOME/Maildir"`
`if ! grep -q INBOX.Spam "$HOME/Maildir/courierimapsubscribed"; then echo "INBOX.Spam" >> "$HOME/Maildir/courierimapsubscribed"; fi`
`chown -R courier:courier "$HOME/Maildir/.Spam"`
`chmod -R 0700 "$HOME/Maildir/.Spam"`
}
# Trash
`test -e "$HOME/Maildir/.Trash"`
if ($RETURNCODE != 0)
{
`/usr/local/bin/maildirmake -f Trash "$HOME/Maildir"`
`if ! grep -q INBOX.Trash "$HOME/Maildir/courierimapsubscribed"; then echo "INBOX.Trash" >> "$HOME/Maildir/courierimapsubscribed"; fi`
`chown -R courier:courier "$HOME/Maildir/.Trash"`
`chmod -R 0700 "$HOME/Maildir/.Trash"`
}
###########################
# User rules and delivery #
###########################
USERMAILDIRFILTER="$HOME/.mailfilter"
USERHORDEFILTER="/var/lib/horde/maildropfilters/$RECIPIENT"
`[ ! -e "$USERHORDEFILTER" ]`
if ($RETURNCODE == 0)
{
`touch "$USERHORDEFILTER"`
`chown courier:hordemaildrop "$USERHORDEFILTER"`
`chmod 0660 "$USERHORDEFILTER"`
}
`[ -f $USERMAILDIRFILTER ]`
if ( $RETURNCODE == 0 )
{
exception {
include $USERMAILDIRFILTER
}
}
# User filter's created in the Horde Web Mail Application. Comment out or delete this section
# if you are not going to use user generated filters, created in Horde.
`[ -f $USERHORDEFILTER ] && ! grep -q \\\` "$USERHORDEFILTER"`
if ( $RETURNCODE == 0 )
{
exception {
include $USERHORDEFILTER
}
}
##################
# Spam Flag Rule #
##################
if (/^X-Spam-Flag: YES/)
{
exception {
to "$HOME/Maildir/.Spam"
}
}
copy the maildroprc file provided with the configuration examples to /etc/maildroprc
Filesystem Permissions
Courier in general, especially Maildrop, is very security conscious, you must make sure you get the right permission in the maildroprc file. The user owner must be the same as the one you created when installing Courier Imap
# chown courier /etc/maildroprc # chmod 0400 /etc/maildroprc
The scripts, above, take pains to set all of the filesystem permissions any time they create a new folder. You'll want to make sure they are setting the user owner to the correct user.
