Saturday, January 23, 2010

Handling Multiple Domains with Postfix Admin in RHEL/CentOS 5

SkyHi @ Saturday, January 23, 2010

This article will show you how to configure Postfix and Dovecot to handle multiple domains stored in MySQL. Postfix Admin is used to manage the domains, mailboxes and aliases in MySQL. See How to Install Postfix Admin to setup the MySQL database that will be used by Postfix and Dovecot.

Creating the Virtual Mail User Account

Since the user names will be stored in MySQL, we will have to create a user that will be the owner for all the files belonging to the MySQL user names.

Create user vmail
1. Create a new user, we will call it vmail. Change the Login Shell to /sbin/nologin, this user account should not be used for logging in. Learn how to use the User Manager application here.
User Manager
2. Take note of the User ID and Home Directory of vmail.
User Manager
3. Click the Groups tab and now note down the Group ID of vmail. We’ll be needing all of them later.

Configuring Postfix

Edit mysql-domains.cf
2. Create the file /etc/postfix/mysql-domains.cf containing the lines below.
host = localhost
user = postfix
password = your_password
dbname = postfix
table = domain
select_field = domain
where_field = domain
additional_conditions = and backupmx = '0' and active = '1'
postmap mysql-domains.cf
3. Test /etc/postfix/mysql-domains.cf using the command below.
postmap -q acme.com mysql:/etc/postfix/mysql-domains.cf

Replace acme.com with your own domain name. It should echo your domain.

Edit config.inc.php
4. Create the file /etc/postfix/mysql-users.cf containing the lines below.
host = localhost
user = postfix
password = your_password
dbname = postfix
table = mailbox
select_field = maildir
where_field = username
additional_conditions = and active = '1'
result_format = %sMaildir/
postmap mysql-users.cf
5. Test /etc/postfix/mysql-users.cf using the command below.
postmap -q johndoe@acme.com mysql:/etc/postfix/mysql-users.cf

Replace johndoe@acme.com with your own email address. You should see the mailbox path.

Edit mysql-aliases.cf
6. Create the file /etc/postfix/mysql-aliases.cf containing the lines below.
host = localhost
user = postfix
password = your_password
dbname = postfix
table = alias
select_field = goto
where_field = address
additional_conditions = and active = '1'
postmap mysql-aliases.cf
7. Test /etc/postfix/mysql-aliases.cf using the command below.
postmap -q john@acme.com mysql:/etc/postfix/mysql-aliases.cf

Replace john@acme.com with your own alias address. You should see the destination email.

Edit main.cf
8. Edit the postfix configuration file /etc/postfix/main.cf and edit the line below.
mydestination = $myhostname, localhost.$mydomain, localhost

and add the lines below

virtual_mailbox_domains = mysql:/etc/postfix/mysql-domains.cf
virtual_mailbox_maps = mysql:/etc/postfix/mysql-users.cf
virtual_alias_maps = mysql:/etc/postfix/mysql-aliases.cf
virtual_mailbox_base = /home/vmail
virtual_uid_maps = static:501
virtual_gid_maps = static:501

virtual_mailbox_base, virtual_uid_maps and virtual_gid_maps should contain the home directory, user id and group id of vmail respectively.

NoteMake sure $mydomain in mydestination has been removed, otherwise the lookup will not work and you will get a “User unknown in local recipient table” error.
Service Configuration
9. Restart the Postfix or MailScanner service if you have installed it. Learn how to start and stop services here.
Terminal
10. You should now be able to send email to addresses found in MySQL. See Test Postfix using Telnet and try using MySQL email addresses instead of the system user names.

Configuring Dovecot

Edit dovecot-mysql.conf
1. Create the file /etc/dovecot-mysql.conf containing the lines below.
driver = mysql
connect = host=localhost dbname=postfix user=postfix password=password
default_pass_scheme = PLAIN
password_query = SELECT password FROM mailbox WHERE username = '%u'
Edit dovecot.conf
2. Edit the file /etc/dovecot.conf and change the value of the following keys below.
auth_username_format = %Lu

passdb sql {
args = /etc/dovecot-mysql.conf
}

userdb static {
args = uid=501 gid=501 home=/home/vmail/%d/%n
}

uid, gid and home should contain the user id, group id and home directory respectively of the vmail user account.

NoteComment out all the other passdb and userdb sections except for those specified above to ensure that nothing will conflict with our MySQL virtual accounts.

Service Configuration
3. Restart the dovecot service. Learn how to start and stop services here.
Terminal
4. You should now be able to login using the user names found in MySQL. See Test Dovecot using Telnet and try using MySQL user names instead of the system user names.
NoteIf you encounter any problems, check the log file at /var/log/maillog.



REFERENCE

http://www.linuxmail.info/multiple-domains-postfix-admin-centos-5/