Virtual Users And Domains With Postfix, Courier, MySQL And SquirrelMail (Ubuntu 8.10)

Want to support HowtoForge? Become a subscriber!
 
Submitted by falko (Contact Author) (Forums) on Thu, 2008-11-13 18:59. :: Anti-Spam/Virus | Ubuntu | Postfix

Virtual Users And Domains With Postfix, Courier, MySQL And SquirrelMail (Ubuntu 8.10)

Version 1.0
Author: Falko Timme <ft [at] falkotimme [dot] com>
Last edited 11/04/2008

This tutorial is Copyright (c) 2008 by Falko Timme. It is derived from a tutorial from Christoph Haas which you can find at http://workaround.org. You are free to use this tutorial under the Creative Commons license 2.5 or any later version.

This document describes how to install a mail server based on Postfix that is based on virtual users and domains, i.e. users and domains that are in a MySQL database. I'll also demonstrate the installation and configuration of Courier (Courier-POP3, Courier-IMAP), so that Courier can authenticate against the same MySQL database Postfix uses.

The resulting Postfix server is capable of SMTP-AUTH and TLS and quota (quota is not built into Postfix by default, I'll show how to patch your Postfix appropriately). Passwords are stored in encrypted form in the database (most documents I found were dealing with plain text passwords which is a security risk). In addition to that, this tutorial covers the installation of Amavisd, SpamAssassin and ClamAV so that emails will be scanned for spam and viruses. I will also show how to install SquirrelMail as a webmail interface so that users can read and send emails and change their passwords.

The advantage of such a "virtual" setup (virtual users and domains in a MySQL database) is that it is far more performant than a setup that is based on "real" system users. With this virtual setup your mail server can handle thousands of domains and users. Besides, it is easier to administrate because you only have to deal with the MySQL database when you add new users/domains or edit existing ones. No more postmap commands to create db files, no more reloading of Postfix, etc. For the administration of the MySQL database you can use web based tools like phpMyAdmin which will also be installed in this howto. The third advantage is that users have an email address as user name (instead of a user name + an email address) which is easier to understand and keep in mind.

This howto is meant as a practical guide; it does not cover the theoretical backgrounds. They are treated in a lot of other documents in the web.

This document comes without warranty of any kind! I want to say that this is not the only way of setting up such a system. There are many ways of achieving this goal but this is the way I take. I do not issue any guarantee that this will work for you!

 

1 Preliminary Note

This tutorial is based on Ubuntu 8.10 Server (Intrepid Ibex), so you should set up a basic Ubuntu 8.10 server installation before you continue with this tutorial (e.g. as shown on the pages 1 - 3 in this tutorial: The Perfect Server - Ubuntu Intrepid Ibex (Ubuntu 8.10)). The system should have a static IP address. I use 192.168.0.100 as my IP address in this tutorial and server1.example.com as the hostname.

Make sure that you are logged in as root (type in

sudo su

to become root), because we must run all the steps from this tutorial as root user.

It is very important that you make /bin/sh a symlink to /bin/bash...

ln -sf /bin/bash /bin/sh

... and that you disable AppArmor:

/etc/init.d/apparmor stop
update-rc.d -f apparmor remove
apt-get remove apparmor apparmor-utils

 

2 Install Postfix, Courier, Saslauthd, MySQL, phpMyAdmin

To install Postfix, Courier, Saslauthd, MySQL, and phpMyAdmin, we simply run

apt-get install postfix postfix-mysql postfix-doc mysql-client mysql-server courier-authdaemon courier-authlib-mysql courier-pop courier-pop-ssl courier-imap courier-imap-ssl postfix-tls libsasl2-2 libsasl2-modules libsasl2-modules-sql sasl2-bin libpam-mysql openssl phpmyadmin apache2 libapache2-mod-php5 php5 php5-mysql libpam-smbpass

You will be asked a few questions:

New password for the MySQL "root" user: <-- yourrootsqlpassword
Repeat password for the MySQL "root" user: <-- yourrootsqlpassword
Create directories for web-based administration? <-- No
General type of mail configuration: <-- Internet Site
System mail name: <-- server1.example.com
SSL certificate required <-- Ok
Web server to reconfigure automatically: <-- apache2

 

3 Apply The Quota Patch To Postfix

We have to get the Postfix sources, patch it with the quota patch, build new Postfix .deb packages and install those .deb packages:

apt-get install build-essential dpkg-dev fakeroot debhelper libgdbm-dev libldap2-dev libpcre3-dev libssl-dev libsasl2-dev postgresql-server-dev-8.3 po-debconf dpatch libmysqlclient15-dev lsb-release libcdb-dev libdb-dev hardening-wrapper

cd /usr/src
apt-get source postfix

(Make sure you use the correct Postfix version in the following commands. I have Postfix 2.5.5 installed. You can find out your Postfix version by running

postconf -d | grep mail_version

The output should look like this:

root@server1:/usr/src# postconf -d | grep mail_version
mail_version = 2.5.5
milter_macro_v = $mail_name $mail_version
root@server1:/usr/src#

)

wget http://vda.sourceforge.net/VDA/postfix-2.5.5-vda-ng.patch.gz
gunzip postfix-2.5.5-vda-ng.patch.gz
cd postfix-2.5.5
patch -p1 < ../postfix-2.5.5-vda-ng.patch
dpkg-buildpackage

You might see a warning like this at the end of the dpkg-buildpackage command:

dpkg-buildpackage: warning: Failed to sign .dsc and .changes file

You can ignore this message.

Now we go one directory up, that's where the new .deb packages have been created:

cd ..

The command

ls -l

shows you the available packages:

root@server1:/usr/src# ls -l
total 5872
drwxr-xr-x 19 root root    4096 2008-11-04 16:19 postfix-2.5.5
-rw-r--r--  1 root src   230154 2008-11-04 16:16 postfix_2.5.5-1.diff.gz
-rw-r--r--  1 root src     1170 2008-11-04 16:16 postfix_2.5.5-1.dsc
-rw-r--r--  1 root src     3737 2008-11-04 16:21 postfix_2.5.5-1_i386.changes
-rw-r--r--  1 root src  1230234 2008-11-04 16:21 postfix_2.5.5-1_i386.deb
-rw-r--r--  1 root src  3157877 2008-09-03 17:04 postfix_2.5.5.orig.tar.gz
-rw-r--r--  1 root src    58389 2008-09-06 05:02 postfix-2.5.5-vda-ng.patch
-rw-r--r--  1 root src    41404 2008-11-04 16:21 postfix-cdb_2.5.5-1_i386.deb
-rw-r--r--  1 root src   140970 2008-11-04 16:21 postfix-dev_2.5.5-1_all.deb
-rw-r--r--  1 root src   916504 2008-11-04 16:21 postfix-doc_2.5.5-1_all.deb
-rw-r--r--  1 root src    48652 2008-11-04 16:21 postfix-ldap_2.5.5-1_i386.deb
-rw-r--r--  1 root src    43332 2008-11-04 16:21 postfix-mysql_2.5.5-1_i386.deb
-rw-r--r--  1 root src    43256 2008-11-04 16:21 postfix-pcre_2.5.5-1_i386.deb
-rw-r--r--  1 root src    43412 2008-11-04 16:21 postfix-pgsql_2.5.5-1_i386.deb
root@server1:/usr/src#

Pick the postfix and postfix-mysql packages and install them like this:

dpkg -i postfix_2.5.5-1_i386.deb postfix-mysql_2.5.5-1_i386.deb

 

4 Create The MySQL Database For Postfix/Courier

Now we create a database called mail:

mysqladmin -u root -p create mail

Next, we go to the MySQL shell:

mysql -u root -p

On the MySQL shell, we create the user mail_admin with the passwort mail_admin_password (replace it with your own password) who has SELECT,INSERT,UPDATE,DELETE privileges on the mail database. This user will be used by Postfix and Courier to connect to the mail database:

GRANT SELECT, INSERT, UPDATE, DELETE ON mail.* TO 'mail_admin'@'localhost' IDENTIFIED BY 'mail_admin_password';
GRANT SELECT, INSERT, UPDATE, DELETE ON mail.* TO 'mail_admin'@'localhost.localdomain' IDENTIFIED BY 'mail_admin_password';
FLUSH PRIVILEGES;

Still on the MySQL shell, we create the tables needed by Postfix and Courier:

USE mail;

CREATE TABLE domains (
domain varchar(50) NOT NULL,
PRIMARY KEY (domain) )
TYPE=MyISAM;

CREATE TABLE forwardings (
source varchar(80) NOT NULL,
destination TEXT NOT NULL,
PRIMARY KEY (source) )
TYPE=MyISAM;

CREATE TABLE users (
email varchar(80) NOT NULL,
password varchar(20) NOT NULL,
quota INT(10) DEFAULT '10485760',
PRIMARY KEY (email)
) TYPE=MyISAM;

CREATE TABLE transport (
domain varchar(128) NOT NULL default '',
transport varchar(128) NOT NULL default '',
UNIQUE KEY domain (domain)
) TYPE=MyISAM;

quit;

As you may have noticed, with the quit; command we have left the MySQL shell and are back on the Linux shell.

The domains table will store each virtual domain that Postfix should receive emails for (e.g. example.com).

domain
example.com

The forwardings table is for aliasing one email address to another, e.g. forward emails for info@example.com to sales@example.com.

source destination
info@example.com sales@example.com

The users table stores all virtual users (i.e. email addresses, because the email address and user name is the same) and passwords (in encrypted form!) and a quota value for each mail box (in this example the default value is 10485760 bytes which means 10MB).

email password quota
sales@example.com No9.E4skNvGa. ("secret" in encrypted form) 10485760

The transport table is optional, it is for advanced users. It allows to forward mails for single users, whole domains or all mails to another server. For example,

domain transport
example.com smtp:[1.2.3.4]

would forward all emails for example.com via the smtp protocol to the server with the IP address 1.2.3.4 (the square brackets [] mean "do not make a lookup of the MX DNS record" (which makes sense for IP addresses...). If you use a fully qualified domain name (FQDN) instead you would not use the square brackets.).

BTW, (I'm assuming that the IP address of your mail server system is 192.168.0.100) you can access phpMyAdmin over http://192.168.0.100/phpmyadmin/ in a browser and log in as mail_admin. Then you can have a look at the database. Later on you can use phpMyAdmin to administrate your mail server.


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Dale Hutchinson (not registered) on Tue, 2009-04-21 01:09.

Falko,

 Thank you for these guides. Not only do these get a great server up and running with little effort your are teaching me Linux! 

 

Thanks!

 

Submitted by Johnny (not registered) on Wed, 2009-03-25 20:34.
Excellent guide, thanks. You got any advice on integrating domain keys into the setup?
Submitted by hansaudun (registered user) on Tue, 2009-01-20 14:04.

What a great guide!

I found a small typo in the configuration of postfix.

The line:

postconf -e 'virtual_mailbox_extended = yes'

should be:

postconf -e 'virtual_maildir_extended = yes'

If not, quota does not work

Best regards,

Hans Audun

Submitted by click (registered user) on Wed, 2008-11-26 21:02.
Thanks for this one.
Submitted by Michel (not registered) on Sat, 2009-03-07 01:49.

hi my problem is not recive email send is ok

log:

Mar  7 01:38:39 mail postfix/smtpd[23978]: connect from localhost[127.0.0.1]
Mar  7 01:38:39 mail postfix/smtpd[23978]: 2A94618127: client=localhost[127.0.0.1]
Mar  7 01:38:39 mail postfix/cleanup[23974]: 2A94618127: message-id=<01d701c99ebd$055b0af0$101120d0$@b@netcore.ch>
Mar  7 01:38:39 mail postfix/qmgr[23768]: 2A94618127: from=<michel.b@netcore.ch>, size=3640, nrcpt=1 (queue active)
Mar  7 01:38:39 mail postfix/smtpd[23978]: disconnect from localhost[127.0.0.1]
Mar  7 01:38:39 mail amavis[23161]: (23161-07) Passed CLEAN, [212.90.203.27] [62.48.100.149] <michel.b@netcore.ch> -> <info@dmbox.ch>, Message-ID: <01d701c99ebd$055b0af0$101120d0$@b@netcore.ch>, mail_id: $
Mar  7 01:38:39 mail postfix/smtp[23975]: 00A8918126: to=<info@dmbox.ch>, relay=127.0.0.1[127.0.0.1]:10024, delay=1.2, delays=0.22/0.01/0.01/1, dsn=2.0.0, status=sent (250 2.0.0 Ok, id=23161-07, from MTA($
Mar  7 01:38:39 mail postfix/qmgr[23768]: 00A8918126: removed
Mar  7 01:38:39 mail pop3d-ssl: Connection, ip=[::ffff:62.48.100.149]
Mar  7 01:38:39 mail postfix/smtpd[23971]: warning: 62.48.100.157: hostname 62-48-100-157.adsl.ticino.com verification failed: Name or service not known
Mar  7 01:38:39 mail postfix/smtpd[23971]: connect from unknown[62.48.100.157]
Mar  7 01:38:39 mail postfix/smtp[23979]: warning: host mail.dmbox.ch[62.48.100.157]:25 greeted me with my own hostname mail.dmbox.ch
Mar  7 01:38:39 mail postfix/smtp[23979]: warning: host mail.dmbox.ch[62.48.100.157]:25 replied to HELO/EHLO with my own hostname mail.dmbox.ch
Mar  7 01:38:39 mail postfix/smtp[23979]: 2A94618127: to=<info@dmbox.ch>, relay=mail.dmbox.ch[62.48.100.157]:25, delay=0.25, delays=0.01/0.02/0.22/0, dsn=5.4.6, status=bounced (mail for mail.dmbox.ch loop$
Mar  7 01:38:39 mail postfix/smtpd[23971]: disconnect from unknown[62.48.100.157]
Mar  7 01:38:39 mail postfix/cleanup[23974]: 6995018128: message-id=<20090307003839.6995018128@mail.dmbox.ch>
Mar  7 01:38:39 mail postfix/qmgr[23768]: 6995018128: from=<>, size=5436, nrcpt=1 (queue active)
Mar  7 01:38:39 mail postfix/bounce[23982]: 2A94618127: sender non-delivery notification: 6995018128
Mar  7 01:38:39 mail postfix/qmgr[23768]: 2A94618127: removed
Mar  7 01:38:39 mail pop3d-ssl: LOGIN, user=info@dmbox.ch, ip=[::ffff:62.48.100.149], port=[62236]
Mar  7 01:38:39 mail pop3d-ssl: LOGOUT, user=info@dmbox.ch, ip=[::ffff:62.48.100.149], port=[62236], top=0, retr=0, rcvd=12, sent=39, time=0
Mar  7 01:38:40 mail postfix/smtp[23979]: 6995018128: to=<michel.b@netcore.ch>, relay=mail.netcore.ch[213.200.248.134]:25, delay=1.4, delays=0.01/0/0.77/0.65, dsn=2.0.0, status=sent (250 Requested mail ac$
Mar  7 01:38:40 mail postfix/qmgr[23768]: 6995018128: removed
Mar  7 01:38:41 mail pop3d-ssl: Connection, ip=[::ffff:62.48.100.149]
Mar  7 01:38:41 mail pop3d-ssl: LOGIN, user=info@dmbox.ch, ip=[::ffff:62.48.100.149], port=[62248]
Mar  7 01:38:41 mail pop3d-ssl: LOGOUT, user=info@dmbox.ch, ip=[::ffff:62.48.100.149], port=[62248], top=0, retr=0, rcvd=12, sent=39, time=0

my main.cf

myhostname = mail.dmbox.ch
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = mail.dmbox.ch, localhost, localhost.localdomain
relayhost =
mynetworks = 127.0.0.0/8
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
html_directory = /usr/share/doc/postfix/html
message_size_limit = 30720000
virtual_alias_domains =
virtual_alias_maps = proxy:mysql:/etc/postfix/mysql-virtual_forwardings.cf, mysql:/etc/postfix/mysql-virtual_email2email.cf
virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql-virtual_domains.cf
virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql-virtual_mailboxes.cf
virtual_mailbox_base = /home/vmail
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000
smtpd_sasl_auth_enable = yes
broken_sasl_auth_clients = yes
smtpd_sasl_authenticated_header = yes
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
transport_maps = proxy:mysql:/etc/postfix/mysql-virtual_transports.cf
virtual_create_maildirsize = yes
virtual_maildir_extended = yes
#virtual_mailbox_extended = yes
virtual_mailbox_limit_maps = proxy:mysql:/etc/postfix/mysql-virtual_mailbox_limit_maps.cf
virtual_mailbox_limit_override = yes
virtual_maildir_limit_message = "The user you are trying to reach is over quota."
virtual_overquota_bounce = yes
proxy_read_maps = $local_recipient_maps $mydestination $virtual_alias_maps $virtual_alias_domains $virtual_mailbox_maps $virtual_mailbox_domains $relay_recipient_maps $relay_domains $canonical_maps $sende$
content_filter = amavis:[127.0.0.1]:10024
receive_override_options = no_address_mappings

help my please