Integration/DBMail.iRedMail.with.MySQL.backend/RHEL

From iRedMail

Revision as of 01:34, 14 August 2011 by ZhangHuangbin (Talk | contribs)
Jump to: navigation, search

Contents


STILL WORKING IN PROGRESS, DO NOT TRY IT ON PRODUCTION SERVER.

Summary

This tutorial is written for:

  • Red Hat Enterprise Linux 5.x, 6.x
  • CentOS 5.x, 6.x
  • Scientific Linux 6.x

This tutorial describes how to integrate DBMail in iRedMail, store mail messages in MySQL.

  • What DBMail is? DBMail is an open-source project that enables storage of mail messages in a relational database. Currently MySQL, PostgreSQL and SQLite can be used as storage backends. Web site: http://www.dbmail.org/
  • Why DBMail? Quote from DBMail web site:
    • Scalability. DBMail is as scalable as the database system used for storage.
    • Manageability. DBMail can be managed by updating the relational database or directory service - without shell access.
    • Speed. Dbmail uses very efficient, database specific queries for retrieving mail information.
    • Security. Dbmail doesn't require filesystem access. It's as secure as the database and directory server used.
    • Flexibility. Changes in a Dbmail system (adding of users, changing passwords etc.) are effective immediately. Users can be stored in the database, or managed separately in an LDAP server such as OpenLDAP or Active Directory.

Note:

  • For RHEL/CentOS/Scientific Linux 5.x, we will install DBMail-2.2.17 from yum repository EPEL.
  • For RHEL/CentOS/Scientific Linux 6.x, we will install DBMail-3.0.0-rc from yum repository EPEL.
  • DBMail-2.x doesn't provide native TLS/SSL support.

Requirements and Preparations

  • OS:
    • Red Hat Enterprise Linux 5.x or 6.x
    • CentOS 5.x or 6.x
    • Scientific Linux 5.x or 6.x
  • A running iRedMail server. MySQL server is installed by default in both OpenLDAP and MySQL backends, so either backend is OK.


DBMail, IMAP/POP3 Server

Preparations

Before installing DBMail software, we need to create necessary system user and group to run DBMail daemons, and create necessary MySQL database which used to store mail accounts and messages.

  • Create necessary system user (dbmail) and group (dbmail).
Terminal:
# useradd -s /sbin/nologin -m dbmail

Above command will create group dbmail and system user dbmail. You can verify it with command id:

Terminal:
# id dbmail
uid=504(dbmail) gid=504(dbmail) groups=504(dbmail)
  • Create necessary MySQL database to store mail accounts and mails: dbmail. Replace password_of_dbmail below with your password.
Terminal:
$ mysql -uroot -p
mysql> CREATE DATABASE dbmail DEFAULT CHARACTER SET utf8;
mysql> GRANT ALL ON dbmail.* TO dbmail@localhost IDENTIFIED BY 'password_of_dbmail';
mysql> FLUSH PRIVILEGES;

We now have a MySQL database dbmail, you can access it with MySQL user dbmail and password password_of_dbmail. You can verify it with MySQL command line:

Terminal:
$ mysql -udbmail -p
Enter password:                # <- Type password of MySQL user dbmail here.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| dbmail             |      # <- Database 'dbmail' is available.
+--------------------+
2 rows in set (0.04 sec)

mysql> 
  • Stop Dovecot daemon. Since Dovecot will be replaced by DBMail, we must stop it:
Terminal:
# /etc/init.d/dovecot stop
# chkconfig --level 345 dovecot off

Install DBMail

DBMail is available in EPEL repository, refer to Fedora wiki for how to use EPEL repository.

It's now ready to install DBMail core component and library used to connect MySQL.

Terminal:
# ---- For RHEL/CentOS/Scientific Linux 5.x
# yum install dbmail dbmail-mysql

# ---- For RHEL/CentOS/Scientific Linux 6.x
# yum install dbmail

Main configure file of DBMail is /etc/dbmail.conf by default, we will configure it later.

DBMail provides a MySQL template file /usr/share/doc/dbmail-x.y.z/sql/mysql/create_tables.mysql (replace 'x.y.z' by real version number on your server), we should import it to create necessary MySQL tables to store mail accounts and messages. Here we use 'dbmail-3.0.0' for example.

Terminal:
$ mysql -udbmail -p
mysql> USE dbmail;
mysql> SOURCE /usr/share/doc/dbmail-3.0.0/sql/mysql/create_tables.mysql;
mysql> SHOW TABLES;               # < See what tables were created.

Configure DBMail

Now open configure file of DBMail, /etc/dbmail.conf, update below settings:

File: /etc/dbmail.conf
[DBMAIL]
# Store mail messages in which kind of database. We use MySQL here.
driver                = mysql

# Authenticate mail users from SQL database or LDAP server. We use SQL (MySQL) here.
authdriver            = sql

# Host for database.
host                 = 127.0.0.1
sqlport              = 3306
sqlsocket            = /var/lib/mysql/mysql.sock

# Database username.
user                 = dbmail

# Database password.
pass                 = password_of_dbmail

# Database name.
db                   = dbmail

# Table prefix. Defaults to "dbmail_" if not specified.
table_prefix         = dbmail_

# ---- For DBMail-2.2.x ----
# Debug level. 0 for production use, 511 for debug.
#TRACE_SYSLOG          = 0
#TRACE_STDERR          = 0
# ---- For DBMail-3.0.0 ----
file_logging_levels       = 0
syslog_logging_levels     = 0

# ---- For DBMail-2.2.x ----
# Run DBMail services as which system user and group.
#EFFECTIVE_USER        = dbmail
#EFFECTIVE_GROUP       = dbmail
# ---- For DBMail-3.0.0 ----
effective_user        = dbmail
effective_group       = dbmail

# Where we have DBMail libraries installed.
# It's /usr/lib/dbmail on i386 system, /usr/lib64/dbmail on x86_64 system.
library_directory     = /usr/lib64/dbmail

# directory for storing PID files.
pid_directory         = /var/run/dbmail

###########################################
# Below settings are DBMail-3.0.0 special #
###########################################
#
# SSL/TLS certificates
#
# A file containing a list of CAs in PEM format
tls_cafile            = /etc/pki/tls/certs/iRedMail_CA.pem

# A file containing a PEM format certificate
tls_cert              = /etc/pki/tls/certs/iRedMail_CA.pem

# A file containing a PEM format RSA or DSA key
tls_key               = /etc/pki/tls/private/iRedMail.key

[POP]
tls_port              = 995

[IMAP]
tls_port              = 993

[SIEVE]
# Note: Please set it to 2000 on RHEL/CentOS 5.x, 4190 on RHEL/CentOS 6.x.
port                  = 4190

Create directory to store PID files:

Terminal:
# mkdir /var/run/dbmail
# chown dbmail:dbmail /var/run/dbmail

It's ready to start DBMail daemons:

Terminal:
# for i in dbmail-imapd dbmail-lmtpd dbmail-pop3d dbmail-timsieved; do /etc/init.d/$i restart; done

Make sure DBMail daemons will start when system startup:

Terminal:
# for i in dbmail-imapd dbmail-lmtpd dbmail-pop3d dbmail-timsieved; do chkconfig --level 345 $i on; done

Check status of DBMail daemons:

Terminal:
tcp        0      0 0.0.0.0:110                 0.0.0.0:*                   LISTEN      1747/dbmail-pop3d   
tcp        0      0 0.0.0.0:143                 0.0.0.0:*                   LISTEN      1710/dbmail-imapd   
tcp        0      0 0.0.0.0:24                  0.0.0.0:*                   LISTEN      1734/dbmail-lmtpd   
tcp        0      0 0.0.0.0:4190                0.0.0.0:*                   LISTEN      1760/dbmail-timsiev 
tcp        0      0 0.0.0.0:993                 0.0.0.0:*                   LISTEN      1710/dbmail-imapd   
tcp        0      0 0.0.0.0:995                 0.0.0.0:*                   LISTEN      1747/dbmail-pop3d

Create testing account

DBMail daemons are running, let's create a testing account to test POP3/POP3S/IMAP/IMAPS/Managesieve services.

  • Create a testing account test@domain.ltd, with password mypass:
Terminal:
# dbmail-users -a test@domain.ltd -w mypass -s test@domain.ltd
Adding INBOX for new user... ok.
[test@domain.ltd]
Done
test@domain.ltd:x:5:0:0.00:0.00:test@domain.ltd

Refer to DBMail wiki site for more information about managing users: http://dbmail.org/dokuwiki/doku.php/manage_users

Test POP3/IMAP/Managesieve services with telnet

It's OK to test POP3/POP3S/IMAP/IMAPS services with telnet, mutt or Roundcube webmail, here we use telnet and mutt instead. After testing, you can login to Roundcube Webmail directly.

Testing POP3 service

Terminal:
$ telnet localhost 110
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
+OK DBMAIL pop3 server ready to rock <e2ef9332465296ebd4ec7ae5828d9cc3@c60.iredmail.org>   # <-- DBMail response

USER test@domain.ltd                # <- Type this command to login
+OK Password required for test@domain.ltd

PASS bypass                      # <-- Type your password
+OK test@domain.ltd has 0 messages (0 octets)

LIST                   # <-- Check mailbox.
+OK 0 messages (0 octets)
.

QUIT                 # <-- DIsconnect
+OK see ya later
Connection closed by foreign host.

Seems DBMail POP3S service doesn't work well with telnet, you can test it with mutt instead.

Terminal:
$ mutt -f pops://"test@domain.ltd":mypass@localhost

If POP3S works well, mutt will show you an empty mailbox. Then type 'q' to exit mutt.

Testing IMAP service

Terminal:
$ telnet localhost 143
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
* OK [CAPABILITY IMAP4rev1 AUTH=LOGIN AUTH=CRAM-MD5 STARTTLS ID] dbmail 3.0.0-rc1 ready.       # <-- DBMail response.

. LOGIN test@domain.ltd mypass                # <-- Login. Don't forget the leading dot in command.
* CAPABILITY IMAP4rev1 STARTTLS ID ACL RIGHTS=texk NAMESPACE CHILDREN SORT QUOTA THREAD=ORDEREDSUBJECT UNSELECT IDLE
. OK LOGIN completed

. LIST '' '*'                  # <-- Check mailbox.
* LIST (\hasnochildren) "/" "INBOX"
. OK LIST completed

. LOGOUT               # <-- Disconnect.
* BYE
. OK LOGOUT completed
Connection closed by foreign host.

Seems DBMail IMAPS service doesn't work well with telnet, you can test it with mutt instead.

Terminal:
$ mutt -f imaps://"test@domain.ltd":mypass@localhost

If IMAPS works well, mutt will show you an empty mailbox. Then type 'q' to exit mutt.

Testing Managesieve service

Before testing managesieve service, we have to encode username and password first.

Terminal:
$ perl sieve-auth-command.pl test@domain.ltd mypass
AUTHENTICATE "PLAIN" "AHRlc3RAZG9tYWluLmx0ZABteXBhc3M="

The command output is what we need.

Now start to test managesieve service:

Terminal:
$ telnet localhost 2000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
"IMPLEMENTATION" "DBMail timsieved 3.0.0-rc1"
"SASL" "PLAIN"
"SIEVE" "regex imap4flags relational subaddress fileinto reject envelope vacation notify "
OK


AUTHENTICATE "PLAIN" "AHRlc3RAZG9tYWluLmx0ZABteXBhc3M="               # <-- Type this command.
OK

Integrate DBMail in Postfix

  • Since DBMail uses different SQL structure from iRedMail, we have to disable some iRedMail special features in Postfix first.
Terminal:
# postconf -e recipient_bcc_maps=''
# postconf -e relay_domains='$mydestination'
# postconf -e sender_bcc_maps=''
# postconf -e smtpd_sender_login_maps=''
# postconf -e transport_maps=''
# postconf -e virtual_alias_maps=''
# postconf -e virtual_mailbox_domains=''
# postconf -e virtual_mailbox_maps=''
  • Add below line in /etc/postfix/master.cf, it's new transport provided by DBMail.
File: /etc/postfix/master.cf
dbmail-lmtp     unix    -       -       n       -       -       lmtp
  • Update postfix setting to use this transport:
Terminal:
# postconf -e mailbox_transport='dbmail-lmtp:127.0.0.1:24'
  • Update postfix setting in /etc/postfix/main.cf, remove reject_unknown_sender_domain in smtpd_recipient_restrictions setting.
  • Update postfix setting to lookup mail users and aliases from DBMail MySQL database:
Terminal:
# postconf -e local_recipient_maps='proxy:mysql:/etc/postfix/dbmail_recipients.cf'
# postconf -e smtpd_sender_login_maps='proxy:mysql:/etc/postfix/dbmail_recipients.cf'

Content of file /etc/postfix/dbmail_recipients.cf:

File: /etc/postfix/dbmail_recipients.cf
hosts = 127.0.0.1
dbname = dbmail
user = dbmail
password = password_of_dbmail
query = SELECT alias FROM dbmail_aliases WHERE alias='%s'
  • Restart Postfix service, and test mail accounts:
Terminal:
# postmap -q 'user@domain.ltd' mysql:/etc/postfix/dbmail_recipients.cf
user@domain.ltd

Replace Dovecot with Cyrus-SASL as SMTP SASL auth daemon

Postfix uses 'dovecot' as SASL type in iRedMail by default, since Dovecot will be replaced by DBMail, we cannot use Dovecot anymore. So we're going to install Cyrus-SASL libraries, and use daemon 'saslauthd' for SMTP SASL auth.

In this section, we will:

  • Install Cyrus-SASL libraries.
  • Configure Cyrus-SASL.
  • Running and testing Cyrus-SASL auth daemon 'saslauthd' immediately.

Install Cyrus-SASL libraries

Cyrus-SASL libraries are available in default yum repositories.

  • For RHEL, they're available in RHN or CD/DVD images.
  • For CentOS and Scientific Linux, they're available in default yum repositories.
Terminal:
# yum install cyrus-sasl cyrus-sasl-lib cyrus-sasl-sql cyrus-sasl-plain cyrus-sasl-md5

Configure Cyrus-SASL

Configure Cyrus-SASL daemon config file:

  • On RHEL/CentOS/Scientific Linux 5.x:
    • On i386, it's /usr/lib/sasl2/smtpd.conf
    • On x86_64, it's /usr/lib64/sasl2/smtpd.conf
  • On RHEL/CentOS/Scientific Linux 6.x:
    • On i386, it's /etc/sasl2/smtpd.conf
File: smtpd.conf
pwcheck_method: saslauthd
mech_list: plain login
auxprop_plugin: sql
sql_engine: mysql
sql_hostnames: 127.0.0.1
sql_user: root
sql_passwd: passwd
sql_database: dbmail
sql_verbose: no
sql_select: SELECT passwd FROM dbmail_users WHERE userid = '%u'

Update /etc/sysconfig/saslauthd (Refer to manual page saslauthd(8) for more information):

File: /etc/sysconfig/saslauthd
SOCKETDIR=/var/spool/postfix/var/run/saslauthd
MECH=rimap
FLAGS='-O 127.0.0.1 -r'

Note:

  • We set SOCKETDIR to /var/spool/postfix/var/run/saslauthd/ so that Postfix works fine under chroot.
  • Don’t forget ‘-r’ in FLAGS. It will force saslauthd daemon uses full email address ad user name.

Create directory used to store saslauthd daemon socket file:

Terminal:
# mkdir -p /var/spool/postfix/var/run/saslauthd/

Start service saslauthd now, and make it auto start when system boot:

Terminal:
# /etc/init.d/saslauthd restart
# chkconfig --level 345 saslauthd on

Test saslauthd and troubleshooting

Terminal:
# testsaslauthd -u 'user@domain.ltd' -p 'plain_passwd'
Personal tools