How To Configure ISP Mail Server With Virtual Users/Domains On Centos 5.0 Using Postfix, Dovecot, MySQL, phpMyAdmin, TLS/SSL - Page 2

Configuration:

SMTP-AUTH/TLS

First we configure SMTP-AUTH and TLS. For this edit /usr/lib/sasl2/smtpd.conf with your favorite editor.

vi /usr/lib/sasl2/smtpd.conf

and make changes as given below.

pwcheck_method: saslauthd
mech_list: plain login

Create directories, then private key and lastly the certificate.

mkdir -p /etc/postfix/ssl/mailserver
cd /etc/postfix/ssl/mailserver
openssl genrsa -des3 -rand /etc/hosts -out smtpd.key 1024
chmod 600 smtpd.key
openssl req -new -key smtpd.key -out smtpd.csr
openssl x509 -req -days 3650 -in smtpd.csr -signkey smtpd.key -out smtpd.crt
openssl rsa -in smtpd.key -out smtpd.key.unencrypted
mv -f smtpd.key.unencrypted smtpd.key
openssl req -new -x509 -extensions v3_ca -keyout cakey.pem -out cacert.pem -days 3650

Private keys and certificates have been created. Later on we will tell postfix to use them.

 

MySQL:

We will now create a database named mail, for this we will issue commands given below;

mysql -u root -p

Enter the password and you will be at the MySQL prompt (mysql>).

CREATE DATABASE mail;

Give all privileges on mail to user mail.

GRANT ALL PRIVILEGES ON mail.* TO 'mail'@'localhost' IDENTIFIED BY 'mail';
FLUSH PRIVILEGES;
quit

Set password for mail user. This will be done by the following statement.

mysqladmin -u mail password newpassword

Then we will create the necessary tables for our new database (mail) that contains domains, users, aliases and mailboxes information.

mysql -u mail -p

After giving the password you will be at the MySQL prompt.

show databases;

It will show all databases, including our "mail" database. We will use "mail".

USE mail;

1. Create the domain table.

CREATE TABLE domain ( domain varchar(255) NOT NULL default '', description varchar(255) NOT NULL default '', aliases int(10) NOT NULL default '0', mailboxes int(10) NOT NULL default '0', maxquota int(10) NOT NULL default '0', transport varchar(255) default NULL, backupmx tinyint(1) NOT NULL default '0', created datetime NOT NULL default '0000-00-00 00:00:00', modified datetime NOT NULL default '0000-00-00 00:00:00', active tinyint(1) NOT NULL default '1', PRIMARY KEY (domain), KEY domain (domain) ) TYPE=MyISAM COMMENT=' Virtual Domains';

2. Second most important table is mailbox, so create mailbox.

CREATE TABLE mailbox ( username varchar(255) NOT NULL default '', password varchar(255) NOT NULL default '', name varchar(255) NOT NULL default '', maildir varchar(255) NOT NULL default '', quota int(10) NOT NULL default '0', domain varchar(255) NOT NULL default '', created datetime NOT NULL default '0000-00-00 00:00:00', modified datetime NOT NULL default '0000-00-00 00:00:00', active tinyint(1) NOT NULL default '1', PRIMARY KEY (username), KEY username (username) ) TYPE=MyISAM COMMENT='Virtual Mailboxes';

3. Create the alias table.

CREATE TABLE alias ( address varchar(255) NOT NULL default '', goto text NOT NULL, domain varchar(255) NOT NULL default '', created datetime NOT NULL default '0000-00-00 00:00:00', modified datetime NOT NULL default '0000-00-00 00:00:00', active tinyint(1) NOT NULL default '1', PRIMARY KEY (address), KEY address (address) ) TYPE=MyISAM COMMENT='Virtual Aliases';

We have created the necessary tables, so quit MySQL.

quit

 

Postfix MySQL:

Postfix needs to know where and how it can look up all mailbox related information. For this purpose we will create the following files under /etc/postfix. Recent versions of Postfix may use that instead of the other statements, and in that case, just comment all lines out, and un-comment the last one.

1. Create file mysql_virtual_alias_maps.cf for forwarding emails from one email address to another.

vi /etc/postfix/mysql_virtual_alias_maps.cf
user = mail
password = mail
hosts = localhost
dbname = mail
table = alias
select_field = goto
where_field = address
additional_conditions = and active = '1'
#query = SELECT goto FROM alias WHERE address='%s' AND active = '1'

2. Create file mysql_virtual_domains_maps.cf, for the virtual domains mapping. Virtual domains are queried using information provided in this file.

vi /etc/postfix/mysql_virtual_domains_maps.cf
user = mail
password = mail
hosts = localhost
dbname = mail
table = domain
select_field = domain
where_field = domain
additional_conditions = and backupmx = '0' and active = '1'
#query = SELECT domain FROM domain WHERE domain='%s' AND backupmx = '0' AND active = '1'

3. Create the file mysql_virtual_mailbox_maps.cf. Which is usually the mapping of email addresses to the location of the user's mailbox on your hard disk. If you saved incoming email to the hard disk using Postfix' built-in virtual delivery agent then it would be queried to find out the mailbox path.

vi /etc/postfix/mysql_virtual_mailbox_maps.cf
user = mail
password = mail
hosts = localhost
dbname = mail
table = mailbox
select_field = CONCAT(domain,'/',maildir)
where_field = username
additional_conditions = and active = '1'
#query = SELECT CONCAT(domain,'/',maildir) FROM mailbox WHERE username='%s' AND active = '1'

4. Lastly create file mysql_virtual_mailbox_limit_maps.cf which will be used for mapping users mailboxes quota limit.

vi /etc/postfix/mysql_virtual_mailbox_limit_maps.cf
user = mail
password = mail
hosts = localhost
dbname = mail
table = mailbox
select_field = quota
where_field = username
additional_conditions = and active = '1'
#query = SELECT quota FROM mailbox WHERE username='%s' AND active = '1'

5. Give the mysql_virtual files appropriate ownership and permission.

chown root:postfix *.cf
chmod 644 *.cf

 

Postfix:

In the Postfix configuration section we will edit the main.cf file located in configuration directory of postfix (/etc/postfix), to enter some basic information necessary for Postfix.

mv /etc/postfix/main.cf /etc/postfix/main.cf.orig
vi /etc/postfix/main.cf
############## Postfix###############
#Date Modified 17th June 2008
#-------------------------------------------------------
smtpd_banner = $myhostname
biff = no
append_dot_mydomain = no
relayhost =
mynetworks = 192.168.49.0/24
inet_interfaces = 192.168.49.81
mailbox_size_limit = 0
recipient_delimiter = +
alias_database = hash:/etc/postfix/aliases
alias_maps = $alias_database
myhostname = example.co.tz
mydomain = rnd
myorigin = $myhostname
mydestination = $myhostname, localhost.$mydomain, $transport_maps
mail_spool_directory = /var/spool/mail
debug_peer_level = 2
debugger_command =
  PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
  xxgdb $daemon_directory/$process_name $process_id & sleep 5
disable_vrfy_command = no
readme_directory = /usr/share/doc/postfix-2.2.10/README_FILES
sample_directory = /usr/share/doc/postfix-2.2.10/samples
sendmail_path = /usr/sbin/sendmail
html_directory = no
setgid_group = postdrop
command_directory = /usr/sbin
manpage_directory = /usr/share/man
daemon_directory = /usr/libexec/postfix
newaliases_path = /usr/bin/newaliases
mailq_path = /usr/bin/mailq
queue_directory = /var/spool/postfix
mail_owner = postfix
unknown_local_recipient_reject_code = 450
####################postfix section ends here###############

 

Postfix Virtual user information:

Again we will edit main.cf file to add support for virtual users. The "virtual_minimum_uid" and "virtual_uid_maps" point to user id 150 in my case, which is a user I created specifically for handling virtual mail. It uses the standard "mail" group with the default gid 12. So first create the user by issuing the useradd command or adduser.

useradd -r -u 150 -g mail -d /var/vmail -s /sbin/nologin -c "Virtual mailbox" vmail
chmod 770 /var/vmail/ (create the directory if doesn't exit)
chown vmail:mail /var/vmail
vi /etc/postfix/main.cf
#######################Virtual Domains Users and mailboxes###############
virtual_mailbox_domains = mysql:$config_directory/mysql_virtual_domains_maps.cf
virtual_mailbox_base = /var/vmail
virtual_mailbox_maps = mysql:$config_directory/mysql_virtual_mailbox_maps.cf
virtual_alias_maps = mysql:$config_directory/mysql_virtual_alias_maps.cf
virtual_minimum_uid = 150
virtual_uid_maps = static:150
virtual_gid_maps = static:12
##############################Virtual section of main.cf ends##############

 

Postfix SASL/TLS authentication:

Finally we will again edit main.cf file to enable SASL/TLS authentication. Previously we created some certificates, we will use them here to secure mail server.

vi /etc/postfix.main.cf
#################### SASL/TLS Authentication###########################
######SASL PART#########
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
broken_sasl_auth_clients = yes
smtpd_sasl_local_domain =
smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination
######TLS PART###########
smptpd_tls_cert_file = /etc/postfix/ssl/mailserver/smtpd.crt
smtpd_tls_key_file = /etc/postfix/ssl/mailserver/smtpd.key
smtpd_tls_CAfile = /etc/postfix/ssl/mailserver/cacert.pem
smtp_tls_auth_only = no
smtp_use_tls = yes
smtpd_use_tls = yes
smtpd_tls_received_header = no
smtp_tls_note_starttls_offer = yes
smtpd_tls_loglevel = 1
tls_random_source = dev:/dev/urandom
smtpd_tls_session_cache_timeout = 3600s
smtpd_tls_recieved_header = yes
###########################SASL/TLS Authentication ends here#############

For my convenience I have divided main.cf in three sections { Postfix, virtual-Domains-Users-and-mailboxes, SASL/TLS-Authentication}. SASL/TLS-Authentication is further divided into ( SASL-PART and TLS-PART). To show you each section and their parameters I have edited the same file thrice. Now copy the /etc/aliases and /etc/aliases.db to /etc/postfix/ and run newaliases.

cp /etc/aliases* /etc/postfix/
newaliases

 

Dovecot v1.x IMAP and POP:

Let us configure Dovecot which provides both a POP3 and an IMAP service. The configuration file for Dovecot is /etc/dovecot.conf. We'll back up the original file to dovecot.conf.orig, and amend the running file to our needs. To handle virtual users with dovecot we will create the file /etc/dovecot-mysql.conf.

vi /etc/dovecot-mysql.conf
######dovecot-mysql.conf should look like this##########
# NOTE: '\' line splitting is used only for readability, currently Dovecot doesn't support it
# The mysqld.sock socket may be in different locations in different systems
driver = mysql
default_pass_scheme = plain
#connect = host=/var/run/mysqld/mysqld.sock dbname=mail user=root password=default
# Alternatively you can connect to localhost as well:
connect = host=localhost dbname=mail user=mail password=mail
password_query = SELECT password FROM mailbox WHERE username = '%u'
user_query = SELECT '/var/vmail/%d/%n' as home, 'maildir:/var/vmail/%d/%n' as mail, 150 AS uid, 12 AS gid, concat('dirsize:storage=',quota) AS quota FROM mailbox WHERE username ='%u' AND active ='1'
####################ends here####################

Now moving to configure dovecot.conf, to authenticate virtual user using mysql_auth method. Keep this thing in mind that normal Linux users can't login to mail-server. Because we haven't enabled Pam-authentication method in our dovecot configuration. Also first_valid_uid and last_valid_uid are set to 150, which means only the user with uid 150 will be able to log in. Pop3 and pop3s protocols are made available.

cp -p /etc/dovecot.conf /etc/dovecot.conf.orig
vi /etc/dovecot.conf

(Only edit the following lines)

##############dovecot configured to work with virtual users############
base_dir = /var/run/dovecot/
protocols = imap pop3 imaps pop3s
listen = [::]
login_dir = /var/run/dovecot-login
mail_location = mbox:/var/vmail/%d/%n
mbox_read_locks = fcntl
log_timestamp = "%Y-%m-%d %H:%M:%S "
log_path = /var/log/maillog
mail_extra_groups = mail
first_valid_uid = 150
last_valid_uid = 150
maildir_copy_with_hardlinks = yes
userdb sql {
args = /etc/dovecot-mysql.conf
}
passdb sql {
args = /etc/dovecot-mysql.conf
}
####################################ends here######################

After this we will set the ownership and access rights on /etc/dovecot-mysql.conf.

chmod 600 /etc/dovecot/*.conf
chown vmail /etc/dovecot/*.conf

 

Roundcube Installation & Configuration:

Based on INSTALLATION NOTES (Roundcube)

1. Decompress and put this folder somewhere inside your document root (/var/www/html/mail)
2. Make sure that the following directories (and the files within) are writable by the webserver

- /temp
- /logs
3. Create a new database and a database user for RoundCube (see DATABASE SETUP)
4. Point your browser to http://url-to-roundcube/installer/
5. Follow the instructions of the install script (or see MANUAL CONFINGURATION)
6. After creating and testing the configuration, remove the installer directory
7. Done!

DATABASE SETUP

* MySQL 4.1.x/5.x

For MySQL version 4.1 and up, it's recommended to create the database for RoundCube with utf-8 charset. Here's an example of the init procedure:

mysql -u root -p
CREATE DATABASE roundcubemail DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
GRANT ALL PRIVILEGES ON roundcubemail.* TO roundcube@localhost IDENTIFIED BY 'password';
quit
mysql -u mail -p roundcubemail < SQL/mysql5.initial.sql

Note: 'password' is the master password for the roundcube user. It is strongly recommended you replace this with a more secure password. Please keep in mind: You need to specify this password later in 'config/db.inc.php'.

 

HTTP Section:

To start using the mailserver web front end we will edit file /etc/httpd/conf/httpd.conf.

vi /etc/httpd/conf/httpd.conf

And append the statements given below to it.

#Front end mail access using roundcube
<VirtualHost *:80>
  DocumentRoot /var/www/html/mail
  ServerName mail.example.co.tz
</VirtualHost>

Save the configuration and exit.

 

Creating Virtual users and domains:

1. Now we will create virtual domains and virtual users in our mail database.

mysql -u mail -p

2. Enter the password and you will be at the mysql> prompt.

USE mail;

3. First create a virtual domain in the domain table (example.co.tz) using the command given below.

INSERT INTO domain (domain,description,aliases,mailboxes,maxquota,transport,backupmx,active) VALUES ('example.co.tz','Virtual domain','10','10', '0','virtual', '0','1');

4. Now create two virtual users in the mailbox table. I have created ([email protected] & [email protected]) as usernames for kiiza and hoboka.

INSERT INTO mailbox (username,password,name,maildir,quota,domain,active) VALUES ('[email protected]','mwamaLis', 'Hoboka Mwamakunge ','hoboka/', '0','example.co.tz','1');
INSERT INTO mailbox (username,password,name,maildir,quota,domain,active) VALUES ('[email protected]','gekman', 'Kiiza Mutungi','kiiza/', '0','example.co.tz','1');
quit

Now that we have created virtual users and virtual domain, we want to test our mail server by logging in and sending mail from one user account to another. So let's start Dovecot, Postfix, MySQL and webserver daemons. Also we want that they should start themselves on next reboot. For this we issue following commands.

chkconfig -level 235 mysqld on
chkconfig -level 235 saslauthd on
chkconfig -level 235 postfix on
chkconfig -level 235 dovecot on
chkconfig -level 235 httpd on
/etc/init.d/saslauthd start
/etc/init.d/mysqld start
/etc/init.d/postfix start
/etc/init.d/dovecot start
/etc/init.d/httpd start
Share this page:

5 Comment(s)