left header graphic
The Network People Banner
right header graphic
   

web this site

Qmail + vpopmail + CourierIMAP + sqwebmail + MySQL for FreeBSD 4.5 Tutorial (v2.1.1)
Written by Matt Simerson and last updated (ChangeLog)03/12/2002.

The original of this document can be found on Matt's server in Michigan or my other server in Atlanta.

This HOW-TO makes some assumptions. First it assumes that you are a moderately competent system administrator. It also assumes you run FreeBSD and you track the -stable source tree. PAY ATTENTION to software versions as listed below. Installation order IS important. These programs check and configure themselves for use with each other. Build them in this order and your experience will be better.

Support is NOT provided. I value my time and if you want some, expect to pay for it. There is a mailing list available where I and a growing crowd of mail toaster owners hang out. You can even browse the archives.

Contributions: If you have something to contribute to the toaster, express the idea on the mailing list. If you've built a toaster and are anxious to express your gratitude, feel free to purchase something from my wish list.

Features:

SMTP Mail Server (+SMTP-AUTH, TLS (SSL), tarpitting)
Virtual Domain Hosting
Virtual Domain Users
AutoResponder
Mailing List
Web Based E-Mail (SSL, spell check, LDAP lookup)
Web Based Virtual Domain Administration
Spam Blocking
SMTP roaming via SMTP-AUTH, POP-AUTH, & IMAP-AUTH
POP3 (+SSL)
IMAP (+SSL)

You will install the following programs:

qmail-1.03.tar.gz : qmail!
mysql-3.23 : Database engine
ucspi-tcp-0.88.tar.gz : Inetd replacement
daemontools-0.70.tar.gz : Useful tools for managing daemons
qmailanalog-0.70.tar.gz: Tools for compiling qmail logs into useful info.
ezmlm-idx-0.40.tar.gz: Mailing list management
Apache + ssl, php, & perl: Secure HTTP server
OpenLDAP: LDAP client & server
vpopmail : Virtual Domain module
autorespond : Auto Responder
gdbm : Database routines
ispell : Spell checking for web mail
qmailadmin : Web Control Panel Interface
sqwebmail: Web based email
courier-IMAP: IMAP & POP3 servers

Install from the FreeBSD Packages/Ports Collection:

pkg_add -r ispell
pkg_add -r gdbm
pkg_add -r setquota
pkg_add -r autoconf
pkg_add -r automake
pkg_add -r expect
pkg_add -r gnupg
pkg_add -r maildrop
cd /usr/ports/mail/autorespond; make install clean
cd /usr/ports/mail/qmail; make enable-qmail clean
cd /usr/ports/mail/qmailanalog; make install clean

Make Users & Groups for Vpopmail

pw groupadd -n vchkpw -g 89
pw useradd -n vpopmail -u 89 -g 89 -c Vpopmail-Master \
-d /usr/local/vpopmail -s /sbin/nologin

Install Qmail (with a few hacks)

mkdir -p /usr/local/src/mail; cd /usr/local/src/mail
cp /usr/ports/distfiles/qmail-1.03.tar.gz .
fetch http://matt.simerson.net/computing/mail/toaster/qmail-1.03-toaster-2.0.patch
tar -xzf qmail-1.03.tar.gz
cd qmail-1.03
patch -p1 < ../qmail-1.03-toaster-2.0.patch
make setup
make cert
echo "255" > /var/qmail/control/concurrencyremote
chmod 644 /var/qmail/control/concurrencyremote
echo "domain.com" > /var/qmail/control/me
echo "50" > /var/qmail/control/tarpitcount
echo "5" > /var/qmail/control/tarpitdelay

Make Directories for Special Modules

mkdir -p /var/qmail/supervise; cd /var/qmail/supervise
mkdir -p smtp/log send/log
chmod +t smtp send
mkdir /service

Edit config files

mkdir -p ~vpopmail/etc
fetch -o ~vpopmail/etc/tcp.smtp \
http://matt.simerson.net/computing/mail/toaster/tcp_smtp.txt

echo user@domain.com > /var/qmail/alias/.qmail-root
echo user@domain.com > /var/qmail/alias/.qmail-postmaster
echo user@domain.com > /var/qmail/alias/.qmail-mailer-daemon

/var/qmail/bin/maildirmake /usr/share/skel/Maildir ( necessary if using /etc/passwd accounts)

Install Apache www server + mod_php + mod_ssl + mod_perl

Apache build instructions

UCSPI-TCP: UNIX Client-Server Program Interface for TCP
The patch adds MySQL support and patches rblsmtpd to support the RBL's new format.

cd /usr/local/src/mail
fetch http://cr.yp.to/ucspi-tcp/ucspi-tcp-0.88.tar.gz
tar -xzf ucspi-tcp-0.88.tar.gz
fetch http://matt.simerson.net/computing/mail/toaster/ucspi-tcp-0.88-mysql+rss.patch
cd ucspi-tcp-0.88
patch -p1 < ../ucspi-tcp-0.88-mysql+rss.patch
make setup check

Create the configuration file for tcpserver

# vi /var/qmail/control/sql
server db.domain.com
port 3306
database vpopmail
table relay
user vpopmail
pass secret
time 1800

Daemon Tools (Useful tools)

cd /usr/ports/sysutils/daemontools
make install clean

Install Ezmlm & Ezmlm-idx (Mailing List)

cd /usr/ports/mail/ezmlm-idx
make -DWITH_MYSQL install
cp work/ezmlm-0.53/ezmlmrc /usr/local/bin
make clean
cd /usr/local/etc/ezmlm
cp ezmlmglrc.sample ezmlmglrc
cp ezmlmrc.sample ezmlmrc
cp ezmlmsubrc.sample ezmlmsubrc

OpenLDAP

cd /usr/ports/net/openldap
make install clean

Vpopmail (Virtual Domain & POP)

cd /usr/local/src/mail
fetch http://www.inter7.com/vpopmail/vpopmail-5.2.tar.gz
tar xzf vpopmail-5.2.tar.gz; cd vpopmail-5.2
./configure --enable-mysql=y --enable-default-domain=simerson.net \
--enable-roaming-users=y --enable-defaultquota=100000000S,10000C \
--enable-learn-passwords=y --enable-mysql-replication=y \
--enable-logging=p --enable-mysql-logging=y
vi vmysql.h

make install

# crontab -e
9-59,10 * * * * /usr/local/vpopmail/bin/clearopensmtp 2>&1 > /dev/null

QmailAdmin (Web Control Interface)

cd /usr/local/src/mail
fetch http://www.inter7.com/qmailadmin/qmailadmin-1.0.2.tar.gz
tar -xzf qmailadmin-1.0.2.tar.gz; cd qmailadmin-1.0.2
./configure --enable-cgibindir=/usr/local/www/cgi-bin.mail
make install-strip

If you set up domains under /etc/passwd users you MUST do this:
chown root /usr/local/www/cgi-bin.mail/qmailadmin

Sqwebmail (Web Based Email)

cd /usr/ports/mail/sqwebmail
make -DWITH_VCHKPW -DWITH_ISPELL install
make clean

# crontab -e
40 * * * * /usr/local/share/sqwebmail/cleancache.pl 2>&1 > /dev/null

chmod -R 755 /usr/local/www/data/webmail
cd /usr/local/share/sqwebmail
cp authdaemonrc.dist authdaemonrc
cp ldapaddressbook.dist ldapaddressbook
fetch -o /usr/local/etc/rc.d/sqwebmail.sh http://matt.simerson.net/computing/qmail/toaster/sqwebmail.txt
chmod 755 /usr/local/etc/rc.d/sqwebmail.sh
/usr/local/etc/rc.d/sqwebmail.sh start

Courier-IMAP

cd /usr/local/src/mail
fetch http://download.sourceforge.net/courier/courier-imap-1.4.2.tar.gz
fetch http://matt.simerson.net/computing/mail/toaster/courier-imap-1.4.2-opensmtp.txt
tar zxvf courier-imap-1.4.2.tar.gz; cd courier-imap-1.4.2/authlib
patch -p1 < ../../courier-imap-1.4.2-opensmtp.txt; cd ..
./configure --prefix=/usr/local --exec-prefix=/usr/local --without-authldap \
--without-authshadow --with-authvchkpw --without-authcram \
--sysconfdir=/usr/local/etc/courier-imap \
--datadir=/usr/local/share/courier-imap \
--libexecdir=/usr/local/libexec/courier-imap \
--enable-workarounds-for-imap-client-bugs \
--disable-root-check --without-authdaemon
make install

cd /usr/local/etc/courier-imap
cp pop3d.cnf.dist pop3d.cnf
cp imapd.cnf.dist imapd.cnf
vi pop3d.cnf imapd.cnf
cp pop3d.dist pop3d
cp imapd.dist imapd
cp pop3d-ssl.dist pop3d-ssl
cp imapd-ssl.dist imapd-ssl
vi imapd pop3d (edit authmodules & add QUOTA to the IMAP_CAPABILITIES in imapd)
cp quotawarnmsg.example quotawarnmsg; vi quotawarnmsg
cd /usr/local/share/courier-imap; ./mkimapdcert; ./mkpop3dcert

At this point in time, we'll start to add, remove and edit configuration files. We will also accomplish a number of other fun things.

Edit DNS zone file - You will need to get the dns properly configured for each domain you want to collect mail for. If your dns records are incorrect you will likely have problems with mail routing. For some helpful DNS links have a look at my dns links.

Adding Mail Domains

mysql -u root -p
CREATE DATABASE vpopmail;
GRANT ALL PRIVILEGES ON vpopmail.* TO vpopmail@"hostname" IDENTIFIED BY 'password';
quit;
mysql -u vpopmail -h db.domain.com -p (get this working before you continue)

# ~vpopmail/bin/vadddomain test.com [password]

vadddomain will modify the following qmail files (default locations used):

/var/qmail/control/locals, rcpthosts, virtualdomains
/var/qmail/control/morercpthosts (if rcpthosts > than 50 lines)
/var/qmail/control/virtualdomains
/var/qmail/users/assign, cdb

It will also create a domains directory

~vpopmail/domains/domain.com
~vpopmail/domains/domain.com/postmaster/Maildir

Now it's time to get qmail up and running. We're going to create a bunch of startup files that the daemontools package will monitor for us.

Supervise/Svscan Startup

cd /var/qmail/supervise
fetch -o smtp/run \
http://matt.simerson.net/computing/mail/toaster/smtp_run.txt
chmod 751 smtp/run
touch smtp/log/run smtp/log/down
chmod 751 smtp/log/*
fetch -o send/run \
http://matt.simerson.net/computing/mail/toaster/send_run.txt
chmod 751 send/run
touch send/log/run send/log/down
chmod 751 send/log/run
fetch -o /var/qmail/rc \
http://matt.simerson.net/computing/mail/toaster/qmail_rc.txt
chmod 751 /var/qmail/rc
vi smtp/run send/run /var/qmail/rc
rm /usr/local/etc/rc.d/qmail.sh
fetch -o /usr/local/sbin/qmail \
http://matt.simerson.net/computing/mail/toaster/qmail.txt
chmod 751 /usr/local/sbin/qmail
fetch -o /usr/local/etc/rc.d/services.sh \
http://matt.simerson.net/computing/mail/toaster/services.txt
chmod 751 /usr/local/etc/rc.d/services.sh
ln -s /usr/local/etc/rc.d/services.sh /usr/local/sbin/services
rehash; qmail cdb

Fire up qmail

ln -s /var/qmail/supervise/smtp /service/
ln -s /var/qmail/supervise/send /service/
services start

What you should see in the process list:
# ps au

USER PID TT STAT STARTED TIME COMMAND
root 33322 p1 S 11:14PM 0:00.02 svscan
root 33323 p1 I 11:14PM 0:00.01 supervise send
root 33324 p1 I 11:14PM 0:00.01 supervise log
root 33325 p1 I 11:14PM 0:00.01 supervise smtp
root 33326 p1 I 11:14PM 0:00.01 supervise log

OK, now you're ready to have some real fun. Point a web browser at http://mail.domain.com/cgi-bin/qmailadmin. Log in with postmaster, test.com, and the test password. Add a user named "user". Time to send some email:

# mail user@test.com
Subject: test
test
.
# ls ~vpopmail/domains/test.com/user/Maildir/*

You should see a file in the new directory. If not, tail /var/log/maillog and figure out why.

Now we want to get our POP3-SSL and IMAP servers working. Install the startup scripts in /usr/local/etc/rc.d:

cp /usr/local/libexec/courier-imap/imapd.rc /usr/local/etc/rc.d/imapd.sh
cp /usr/local/libexec/courier-imap/pop3d.rc /usr/local/etc/rc.d/pop3d.sh
cp /usr/local/libexec/courier-imap/imapd-ssl.rc /usr/local/etc/rc.d/imapd-ssl.sh
cp /usr/local/libexec/courier-imap/pop3d-ssl.rc /usr/local/etc/rc.d/pop3d-ssl.sh

Once installed we'll make symbolic links to the scripts for easy administration:

ln -s /usr/local/etc/rc.d/imapd.sh /usr/local/sbin/imap
ln -s /usr/local/etc/rc.d/pop3d.sh /usr/local/sbin/pop3
ln -s /usr/local/etc/rc.d/imapd-ssl.sh /usr/local/sbin/imapssl
ln -s /usr/local/etc/rc.d/pop3d-ssl.sh /usr/local/sbin/pop3ssl

Now we can either reboot or start the servers manually:

rehash
imap start
pop3 start
imapssl start
pop3ssl start

Users can now get their email at the following addresses:

pop3://mail.domain.com
imap://mail.domain.com
imaps://mail.domain.com
http://mail.domain.com/cgi-bin/sqwebmail (redirects to https)
https://mail.domain.com/cgi-bin/sqwebmail

Domain administrators can add/edit/delete users, mailing lists, autoresponders, mail forwarding, and aliases by logging into the qmailadmin interface:

http://mail.domain.com/cgi-bin/qmailadmin
https://mail.domain.com/cgi-bin/qmailadmin

SYSTEM ADMIN stuff.

maildomain perl script - Front end to nearly everything you can do with the vpopmail programs.

Convert Multilog date stamps to human readable date stamps

# tai64nlocal < logfile > qmaillog.tmp

Statistics about your mail queue:

Feed your qmail-send output through matchup and pipe it through a stats processor:
# /usr/local/qmailanalog/bin/matchup < /var/log/mail/send/current | /usr/local/qmailanalog/bin/z*
where z* is one of the following
# zddist, zdeferrals, zfailures, zoverall, zrecipients, zrhosts, zrxdelay, zsenders, zsuccesses, zsuids

Modify the HTML interface:

Sqwebmail: html temples live in /usr/local/share/sqwebmail/html/en-us/
qmailadmin: html templates live in /usr/local/share/qmailadmin/html/

Man pages:

vpopmail: http://mail.domain.com/vpopmail/
maildrop: http://mail.domain.com/maildrop

perl -MCPAN -e 'install Date::Parse'