Integration/DBMail.iRedMail.with.MySQL.backend/RHEL
From iRedMail
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 = 16 #TRACE_STDERR = 16 # ---- For DBMail-3.0.0 ---- file_logging_levels = 16 syslog_logging_levels = 16 # ---- 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 # directory for storing PID files. pid_directory = /var/run/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 ########################################### # 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 -p md5 -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.
- Download a small perl script here: http://www.rename-it.nl/dovecot/utilities/sieve-auth-command.pl
- Run this script to encode your username and password:
| 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' # postconf -e virtual_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 smtpd_sender_login_maps='proxy:mysql:/etc/postfix/dbmail_recipients.cf' # postconf -e virtual_mailbox_domains='proxy:mysql:/etc/postfix/dbmail_domains.cf' # postconf -e virtual_mailbox_maps='proxy:mysql:/etc/postfix/dbmail_mailboxes.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' |
Content of file /etc/postfix/dbmail_domains.cf:
| File: /etc/postfix/dbmail_recipients.cf |
hosts = 127.0.0.1 dbname = dbmail user = dbmail password = password_of_dbmail query = SELECT DISTINCT 1 FROM dbmail_aliases WHERE SUBSTRING_INDEX(alias, '@', -1) = '%s' |
Content of file /etc/postfix/dbmail_mailboxes.cf:
| File: /etc/postfix/dbmail_mailboxes.cf |
hosts = 127.0.0.1 dbname = dbmail user = dbmail password = password_of_dbmail query = SELECT 1 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 # postmap -q 'domain.ltd' mysql:/etc/postfix/dbmail_domains.cf 1 # postmap -q 'test@domain.ltd' mysql:/etc/postfix/dbmail_mailboxes.cf 1 |
- It's now OK to send a test email with command mail:
| Terminal: |
# mail -s "test" test@domain.ltd < /etc/hosts |
Log in Postfix log file /var/log/maillog:
Aug 14 06:40:20 c60 postfix/pickup[6017]: B89A141FAD: uid=0 from=<root> Aug 14 06:40:20 c60 postfix/cleanup[6022]: B89A141FAD: message-id=<20110814104020.B89A141FAD@c60.iredmail.org> Aug 14 06:40:20 c60 postfix/qmgr[6016]: B89A141FAD: from=<root@c60.iredmail.org>, size=566, nrcpt=1 (queue active) Aug 14 06:40:20 c60 postfix/lmtp[6025]: B89A141FAD: to=<test@domain.ltd>, relay=127.0.0.1[127.0.0.1]:24, delay=0.15, delays=0.05/0.02/0/0.09, dsn=2.0.0, status=sent (215 Recipient <test@domain.ltd> OK) Aug 14 06:40:20 c60 postfix/qmgr[6016]: B89A141FAD: removed
You can view sent email after logging into Roundcube webmail.
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: dbmail sql_passwd: password_of_dbmail 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 -r rimap -s imap -f /var/spool/postfix/var/run/saslauthd/mux -u 'test@domain.ltd' -p 'mypass' 0: NO "authentication failed" |
Although it shows "authentication failed", but the authentication is success. You can verify it in /var/log/messages:
Aug 14 06:11:24 c60 saslauthd[4742]: auth_rimap: unexpected response to auth request: * CAPABILITY IMAP4rev1 STARTTLS ID ACL RIGHTS=texk NAMESPACE CHILDREN SORT QUOTA THREAD=ORDEREDSUBJECT UNSELECT IDLE Aug 14 06:11:24 c60 saslauthd[4742]: do_auth : auth failure: [user=test2@domain.ltd] [service=imap] [realm=rimap] [mech=rimap] [reason=[ALERT] Unexpected response from remote authentication server]
The first line means DBMail accepted your login, and returned IMAP command response, but testsaslauthd cannot not understand its response.
Enable Cyrus-SASL in Postfix
| Terminal: |
# postconf -e smtpd_sasl_path='smtpd' # postconf -e smtpd_sasl_type='cyrus' |
Restarting Postfix service to make it work.
ChangeLog
- 2011-08-14: Initial release.
