Virtual Mailhosting System GuideKen NowackEzra GormanThis document details how to create a virtual mailhosting system based upon postfix, mysql, courier-imap, and cyrus-sasl. 1.0.17 Aug 2003Introduction

For most gentoo users, a simple mail client and fetchmail will do. However, if you're hosting a domain with your system, you'll need a full blown MTA (Mail Transfer Agent). And if you're hosting multiple domains, then you'll definitely need something more robust to handle all of the email for your users. This system was designed to be an elegant solution to that problem.

A virtual mail system needs to be able to handle email for numerous domains with multiple users over a variety of interfaces. This presents some issues that must be dealt with. For instance, what if you have two users on different domains that want the same user name? If you are providing imap access and smtp-auth, how do combine the various authentication daemons into a single system? How do you provide security for the numerous components that comprise the system? How do you manage it all?

This howto will show you how to set up with a mail system capable of handling mail for as many domains as your hardware can handle, supports virtual mail users that don't require shell accounts, has domain specific user names, can authenticate web, imap, smtp, and pop3 clients against a single database, utilizes ssl for transport layer security, has a web interface, can handle mailing lists for any domain on the machine, and is controlled by a nice, central and easy mysql database.

There are quite a variety of ways to go about setting up a virtual mailhosting system. With so may options, another may be the best choice for your specific needs. Consider investigating http://www.qmail.org/ and http://www.exim.org/ to explore your options.

The following packages are used in this setup:
apache, courier-imap, pam_mysql, postfix, mod_php, mod_ssl, phpmyadmin, squirrelmail, cyrus-sasl, mysql, php, and mailman.

Make sure to turn on the following USE variables in /etc/make.conf before compiling the packages: USE="mysql imap libwww maildir sasl ssl". Otherwise you will most likely have to recompile things to get the support you need for all the protocols. Further, it's a good idea to turn off any other mail and network variables, like ipv6.

This howto was written for postfix-2.0.x. If you are using postfix < 2 some of the variables in this document will be different. It is recommended that you upgrade. Some other packages included in this howto are version sensitive as well. You are advised to read the documentation included with packages if you run into issues with this.This document uses apache-1.3.x. Apache-2 has been marked stable in portage. However there are still a number of issues with php integration. Until php support in apache-2.0.x is marked stable, this guide will continue to use the 1.3.x version.You need a domain name to run a public mail server, or at least an MX record for a domain. Ideally you would have control of at least two domains to take advantage of your new virtual domain functionality.Make sure /etc/hostname is set to the right hostname for your mail server. Verify your hostname is set correctly with hostname. Also verify that there are no conflicting entries in /etc/hosts.It is recommended that you read this entire document and familiarize yourself with all the steps before attempting the install. If you run into problems with any of the steps, check the troubleshooting guide at the end of this document. Also, not all the referenced packages are necessary, this set up is very flexible. For instance, if you do not desire a web interface, feel free to skip the squirrelmail section.Postfix Basics

# emerge postfix

Verify that you have not installed any other MTA, such as ssmtp, exim, or qmail, or you will surely have BIG problems.

After postfix is installed, it's time to configure it. Change the following options in /etc/postfix/main.cf:

Next, edit /etc/mail/aliases to add your local aliases. There should at least be an alias for root like: root: your@email.address.

# /usr/bin/newaliasesThis will install the new aliases. You only need to do this when you update or install aliases.
# /etc/init.d/postfix start

Now that postfix is running, fire up your favorite console mail client and send yourself an email. I use mutt for all my console mail. Verify that postfix is delivering mail to local users, once that's done, we're on to the next step.

I strongly recommend that you verify this basic postfix setup is functioning before you progress to the next step of the howto. Courier-imap

# emerge courier-imap

# cd /etc/courier-imapIf you want to use the ssl capabilities of courier-imap or pop3, you'll need to create certs for this purpose.This step is recommended. If you do not want to use ssl, skip this step.
# nano -w pop3d.cnf
# nano -w imapd.cnfChange the C, ST, L, CN, and email parameters to match your server.
# mkpop3dcert
# mkimapdcert

Start up your favorite mail client and verify that all connections you've started work for receiving and sending mail. Now that the basics work, we're going to do a whole bunch of stuff at once to get the rest of the system running. Again, please verify that what we've installed already works before progressing.

Cyrus-sasl

Next we're going to install cyrus-sasl. Sasl is going to play the role of actually passing your auth variables to pam, which will in turn pass that information to mysql for authentication of smtp users. For this howto, we'll not even try to verify that sasl is working until mysql is set up and contains a test user. Which is fine since we'll be authenticating against mysql in the end anyway.

Now for some reason, sasl will not play nicely with pam against the shadow file. I banged my head against this problem for, well, a long time. If anyone knows why sasl will not auth against the shadow file in its current gentoo incarnation, please email me as I'd love to hear a solution to this.

# USE='-ldap -mysql' emerge cyrus-saslWe don't have ldap and we're not using sasl's mysql capabilities so we need to turn them off for this build.

Verify that the above AUTH and STARTTLS lines now appear in your postfix install. As I said before, as it stands now AUTH will not work. that's because sasl will try to auth against it's sasldb, instead of the shadow file for some unknown reason, which we have not set up. So we're going to just plow through and set up mysql to hold all of our auth and virtual domain information.

MySQL

Next we're going to install and configure MySQL. You'll need the genericmailsql.sql dumpfile for this step.

Next we'll set up apache and add an interface to interact with the database more easily.

# emerge apache mod_php mod_ssl phpmyadmin

There are plenty of guides out there about how to set up apache with php. Like this one: http://www.linuxguruz.org/z.php?id=31. There are also numerous posts on http://forums.gentoo.org detailing how to solve problems with the installation (search for 'apache php'). So, that said, I'm not going to cover it here. Set up the apache and php installs, then continue with this howto. Now, a word for the wise: .htaccess the directory that you put phpmyadmin in. If you do not do this, search engine spiders will come along and index the page which in turn will mean that anyone will be able to find your phpmyadmin page via google and in turn be able to come change your database however they want which is BAD! There are many howtos on this including: http://docs.csoft.net/micro/black-htaccess.html.

Now we're going to install the Apache certificates we made previously. The Apache-SSL directives that you need to use the resulting cert are:

Now enter the phpmyadmin page and browse the tables. You'll want to add in your local aliases, edit your user table to add a test user, and change your transport table to add information about your domains. The default values supplied with the dumpfile should be a sufficient guide to what values need to go where. Make sure that if you put information in the database that it is accurate. For instance, make sure the local users home dir exists and that the correct uid/gid values are in place. The maildirs should be created automatically by postfix when the user receives their first email. So, in general, it's a good idea to send a "Welcome" mail to a new user after you setup their account to make sure the .maildir gets created.

The vmail user

At this point you may be wondering what user and directory to use for virtual mail users, and rightly so. Let's set that up.

So now when you're setting up vmail accounts, use the vmail uid, gid, and homedir. When you're setting up local accounts, use that users uid, gid, and homedir. We've been meaning to create a php admin page for this setup but haven't gotten around to it yet, as phpmyadmin generally works fine for us.

Configuring MySQL Authentication and vhosts

Next we'll reconfigure our authentication to use the mailsql database in courier-imap and postfix. In all of the following examples, replace $password with the password you set for the mailsql mysql user.

As of Postfix 2.0.x, there were a number of significant changes over the 1.1.x release. Notably the transport, virtual-gid, and virtual-uid tables are no longer necessary. The tables are still included if you wish to use them.

It is recommended tha you read VIRTUAL_README included with the postfix doc's for more information.

# postfix reload

Now, if all went well, you should have a functioning mailhost. Users should be able to authenticate against the sql database, using their full email address, for pop3, imap, and smtp. I would highly suggest that you verify that everything is working at this point. If you run into problems (with as many things as this setup has going on, it's likely that you will) check the troubleshooting section of this howto.

Squirrelmail

# emerge squirrelmailI like to add a link to the htdocs space for a shorter url.
# ln -s /home/httpd/htdocs/squirrelmail/ /home/httpd/htdocs/mail
# cd /home/httpd/htdocs/mail/conf
# ./conf.plChange your Organization, Server, and Folder settings for squirrelmail.Now you should be able to login to squirrelmail, again - with your full email address,and use your new webmail setup.

Mailman

Last step: mailman. The new version of mailman has very nice virtual domain support, which is why I use it, not to mention it's really a great package. To get this package installed and working correctly for virtual domains is going to require a bit of hacking. I really recommend reading all of the mailman documentation, including README.POSTFIX.gz, to understand what's being done here.

One further note, current versions of mailman install to /usr/local/mailman. If you're like me and wish to change the default install location, it can be overridden in the ebuild filoe by changing the INSTALLDIR variable.

# nano -w /usr/portage/net-mail/mailman/mailman-$ver.ebuild
MAILGID="280"
Set MAILGID to the mailman group instead of nobodyThis is needed for postfix integration

# emerge mailmanThis package is currently masked as well, so you'll need to unmask it or give emerge an explicit path to the ebuild. Once it's installed, follow the directionsin the README.gentoo.gz *except* do not add your aliases to /etc/mail/aliases.We will instead be linking the entire alias db into postfix.
# zless /usr/share/doc/mailman-$ver/README.gentoo.gz

# nano -w /etc/postfix/main.cf
owner_request_special = no
recipient_delimiter = +
Read README.POSTFIX.gz for details on this
alias_maps =
hash:/var/mailman/data/aliases,
mysql:/etc/postfix/mysql-aliases.cf
virtual_alias_maps =
hash:/var/mailman/data/virtual-mailman,
mysql:/etc/postfix/mysql-virtual.cf
This adds mailman alias file support to postfixYou may of course use the mysql tables for this,but I hate doing that by hand. Also, if you are notusing virtual domains, adding the virtual alias mapsto postfix may cause problems, be warned.

You should now be able to setup mailing lists for any domain on your box. Last note on this, make sure you run all mailman commands as the user mailman (su mailman) or else the permissions will be wrong and you'll have to fix them. Read the mailman doc's for more information on setting up and managing mailman lists.

Content Filtering and Anti-Virus

Coming soon...it would be done already but I need some perl help and testing to make it so. If you'd like to volunteer for that, please email me.

Wrap Up

Ok, you're all set, edit /etc/postfix/master.cf and turn off verbose mode for production use. You'll probably also want to add the services to your startup routine to make sure everything comes back up on a reboot. Make sure to add all the services you're using - apache, mysql, saslauthd, postfix, courier-imapd, courier-imapd-ssl, courier-pop3d, and courier-pop3d-ssl are all up to your decision on what access you want to provide. I generally have all the services enabled.

# postfix reload
# rc-update add $service default

Have fun!

TroubleshootingIntroduction

Troubleshooting: This is a short troubleshooting guide for the set up we've detailed how to install here. It is not exhaustive, but meant as a place to get you started in figuring out problems. With a complicated setup such as this, it's imperative that you narrow down the problem to the particular component that is malfunctioning. In general I do that by following a few steps. Start from the base of the system and work your way up, ruling out components that work along the way until you discover which component is having the problem.

Step 1: Check your config files.

Typos are killers, especially when dealing with authentication systems. Scan your config's and mailsql database for typo's. You can debug all you want, but if you're not passing the right information back and forth to your mail system, it's not going to work. If you make a change to a config file for a service, make sure you restart that service so that the config change gets picked up.

# /etc/init.d/service restart

Step 2: Are all the necessary services actually running?

If it's not running, start it up. It's awful hard to debug a service that isn't running. Sometimes a service will act like it's started but still not function. Sometimes, when a bad config is used, or a bad transmission comes into a mail component, the service will hang and keep the port from being used by another process. Sometimes you can detect this with netstat. Or, if you've been at it awhile, just take a break and reboot your box in the meantime. That will clear out any hung services. Then you can come back fresh and try it again.

# /etc/init.d/$service status
# netstat -a | grep $service (or $port)

Step 3: Are all the service using the current config's?

If you've recently made a change to a config file, restart that service to make sure it's using the current version. Some of the components will dump their current config's to you, like postfix.

Repeat after me, logs are my friend. My next troubleshooting stop is always the logs. Sometimes it's helpful to try a failed operation again then check the logs so that the error message is right at the bottom (or top depending on your logger) instead of buried in there somewhere. See if there is any information in your log that can help you diagnose the problem, or at the very least, figure out which component is having the problem.

SMTP, IMAP, and POP3 all respond to telnet sessions. As we've seen earlier when we verified postfix's config. Sometimes it's helpful to open a telnet session to the service itself and see what's happening.

# telnet localhost $portSMTP is 25, IMAP is 143, POP3 is 110. You should receive at least an OK string,letting you know that the service is running and ready to respond to requests.
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
* OK Courier-IMAP ready. Copyright 1998-2002 Double Precision, Inc. See COPYING for distribution information.

Step 6: Sometimes only the big guns will give you the information you need: strace.

You should have this installed anyway. This is an invaluable tool for debugging software. You can start commands from the command line with strace and watch all the system calls as they happen. It often dumps a huge amount of information, so you'll either need to watch it realtime as you retry a failed transaction with the mail system, or dump the output to a file for review.

Once you have the information, if you can diagnose and fix the problem, great! If not, you'll probably need to go digging on the net for information that will help you fix it. Here's a list of sites you can check to see if your error has already been resolved. There's also a really good howto on setting up smtp-auth which contains some great debugging ideas.

http://forums.gentoo.org/ - Great forums for gentoo users

http://bugs.gentoo.org/ - Bugs database for gentoo - great place to look for specific errors