Postfix + Dovecot + SSL configuration example
Introduction
Postfix mail server delivers a high level of flexibility in what matters to configuration and customization. In this tutorial we will integrate Postfix with Dovecot in order to delegate user authentication and POP3 mail server access to Dovecot itself. We will also configure our mail system to be accessible over SSL.
This tutorial considers the following software and environment:
- Ubuntu 12.10
- Postfix 2.9.6
- Dovecot 2.1.7
You may check which Dovecot version you are running by issuing the following command:
dovecot --version
Installing Dovecot
In order to get and install Dovecot we issue the following command:
Generating SSL certificates
We will also need a certificate to enable SSL communication. We will use openssl to generate a self-signed certificate.
First issue the following command to generate a private key:
The key will be generated and stored in domain.key file. Now issue the following command to generate a certificate request:
You will be asked to provide some organizational information. When you are asked for Common Name make sure to provide your full domain name, ex: domain1.com. After conclusion your certificate request will be stored in domain.csr.
Finally we generate the certificate itself based on the private key and the certificate request:
The certificate will be stored in domain.crt file. You may place the private key file and the certificate file in a directory that is suitable for your needs. We will refer to these files later in the tutorial.
main.cf
Now edit main.cf Postfix configuration file and add the following lines:
smtpd_tls_session_cache_database = btree:/etc/postfix/tlscache/smtpd_scache
smtp_tls_session_cache_database = btree:/etc/postfix/tlscache/smtp_scache
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_application_name = smtpd
broken_sasl_auth_clients = yes
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination
smtpd_tls_received_header = yes
smtpd.conf
Now create /etc/postfix/sasl/smtpd.conf file and add the following contents:
pwcheck_method: saslauthd
master.cf
Now edit master.cf Postfix configuration file.
After the following line:
Insert the following lines:
-o smtpd_tls_key_file=/home/root/certs/domain.key
Uncomment the following line:
And insert the following lines after:
-o smtpd_tls_cert_file=/home/root/certs/domain.crt
-o smtpd_tls_key_file=/home/root/certs/domain.key
TLS cache
Now let's create the TLS cache files. Issue the following commands:
chmod 755 /etc/postfix/tlscache
User and Passwords file
Create the users file in:
/etc/dovecot/users
Give the necessary permissions to the file:
This file should contain the users you want for each domain you are hosting, one line per user.
Each user should be in the following format:
Now create the passwords file in:
/etc/dovecot/passwd
Give the necessary permissions to the file:
This file should contain the passwords for each user present in the users file, again one user/password pair per line.
Each line should be in the following format:
[email protected]:{CRAM-MD5}e02d374fde0dc75a17a557039a3a5338c7743304777dccd376f332bee68d2cf6
In order to generate the password hash you may run the following command:
Refreshing the aliases DB
Now we refresh our aliases DB file by issuing the following command:
10-auth.conf
Now edit:
/etc/dovecot/conf.d/10-auth.conf
Add or change the following entry:
10-mail.conf
Now edit:
/etc/dovecot/conf.d/10-mail.conf
Add or change the following entries:
mail_uid = 5000
mail_gid = 5000
10-master.conf
Now edit:
/etc/dovecot/conf.d/10-master.conf
Add or change the following entries:
inet_listener pop3 {
port = 110
}
inet_listener pop3s {
port = 995
ssl = yes
}
}
unix_listener /var/spool/postfix/private/auth {
mode = 0666
}
10-ssl.conf
Now edit:
/etc/dovecot/conf.d/10-ssl.conf
Add or change the following entries:
ssl_key = </home/root/certs/domain.key
auth-system.conf.ext
Now edit:
/etc/dovecot/conf.d/auth-system.conf.ext
Add or change the following entries:
driver = passwd-file
args = scheme=cram-md5 username_format=%u /etc/dovecot/passwd
#driver = pam
# [session=yes] [setcred=yes] [failure_show_msg=yes] [max_requests=<n>]
# [cache_key=<key>] [<service name>]
#args = dovecot
}
userdb {
driver = passwd-file
args = username_format=%u /etc/dovecot/passwd
# <doc/wiki/AuthDatabase.Passwd.txt>
#driver = passwd
# [blocking=no]
#args =
# Override fields from passwd
#override_fields = home=/home/virtual/%u
}
Testing
We are now ready for testing. Chose your favourite mail client software, such as Thunderbird or Outlook, and connect to the mail server using SSL ports both for sending and receiving. You may get a warning because the certificate we are using is self-signed. Don't forget to select POP3 as the receiving protocol.
In order to confirm you are sending message to your server through a secure connection you may send a test message and inspect the message source in the receiver. You should be able to trace the information generated by your email server and extract a portion of the message meta data that is similar to:
(using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))
by domain1.com (Postfix) with ESMTPA id E12AB5D9EBA
for <[email protected]>;
Configuring a mail server can be problematic especially if you are doing it for the first time. Both Postfix and Dovecot provide a robust logging mechanism and by looking at the logs you can almost certainly check what went wrong. The log file is located at: