Christopher Jackson's

hacks, thoughts, and utter ramblings.

Setting Up a Mail Server on Ubuntu 12.04 Precise

The following is a guide to setup a mail server using: postfix, dovecot, postgres, and roundcube on Ubuntu 12.04 Precise. I assume that you already have apache2 and postgres installed.

Postfix

First let us install Postfix and Sasl by issuing the following command:

1
sudo apt-get install postfix sasl2-bin

You will be asked some questions just leave everything as default, we will configure these packages in the next step. Run the following command to configure postfix:

1
sudo dpkg-reconfigure postfix

Again, you will be asked some questions:
General type of mail configuration? Internet Site
System mail name? example.com
Root and postmaster mail recipient? Leave blank
Other destinations to accept mail for? example.com, localhost.example.com, localhost
Force synchronous updates on mail queue? No
Local networks? Leave default (127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128)
Mailbox size limit (bytes)? 0 (0 stands for unlimited)
Local address extension character? Leave default (+)
Internet protocols to use? ipv4 (most likely)
Next, let’s take care of certificates for TLS. You will be asked several questions during this process. Fill them in as you see fit.

1
2
3
4
5
6
7
8
9
mkdir /etc/postfix/ssl
cd /etc/postfix/ssl/
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

Now we need to finish configuring Postfix for TLS and SASL. Run the following commands:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
postconf -e 'smtpd_sasl_local_domain ='
postconf -e 'smtpd_sasl_auth_enable = yes'
postconf -e 'smtpd_sasl_security_options = noanonymous'
postconf -e 'broken_sasl_auth_clients = yes'
postconf -e 'smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination'
postconf -e 'inet_interfaces = all'
echo 'pwcheck_method: saslauthd' >> /etc/postfix/sasl/smtpd.conf
echo 'mech_list: plain login' >> /etc/postfix/sasl/smtpd.conf
postconf -e 'smtpd_tls_auth_only = no'
postconf -e 'smtp_use_tls = yes'
postconf -e 'smtpd_use_tls = yes'
postconf -e 'smtp_tls_note_starttls_offer = yes'
postconf -e 'smtpd_tls_key_file = /etc/postfix/ssl/smtpd.key'
postconf -e 'smtpd_tls_cert_file = /etc/postfix/ssl/smtpd.crt'
postconf -e 'smtpd_tls_CAfile = /etc/postfix/ssl/cacert.pem'
postconf -e 'smtpd_tls_loglevel = 1'
postconf -e 'smtpd_tls_received_header = yes'
postconf -e 'smtpd_tls_session_cache_timeout = 3600s'
postconf -e 'tls_random_source = dev:/dev/urandom'
postconf -e 'myhostname = server1.example.com'
postconf -e 'home_mailbox = Maildir/'
postconf -e 'mailbox_command ='

Finally, we restart Postfix by issuing the following command:

1
sudo service postfix restart

Postfix Virtual Hosts

If you need to have multiple mail domains on this server follow these instructions. Otherwise skip to the SASL section.


First we need to tell postfix which domains we will use, to do that we create a file to list them in. To do this issue the following commands:

1
2
sudo mkdir /etc/postfix/virtual
sudo touch /etc/postfix/virtual/domains

In file /etc/postfix/virtual/domains you will need to list all the domains that you wish to handle mail for, it might look something like this:

1
2
3
example.com
foo.com
mysite.com

Now we need to setup the mappings between email addresses and local accounts by creating another file. To do this we issue the following command:

1
sudo touch /etc/postfix/virtual/addresses

In file /etc/postfix/virtual/addresses you will need to list the mappings for each email address, it might look something like this:

1
2
3
4
5
6
7
8
9
example.com              DOMAIN
bob@example.com          bob
steve@example.com        steve

foo.com                    DOMAIN
@foo.com                 steve

mysite.com               DOMAIN
@mysite.com              chris

The above examples setup four mappings:
Mail sent to bob@example.com goes to the local user bob.
Mail sent to steve@example.com goes to the local user steve.
Mail sent to @foo.com goes to the local user steve.
Mail sent to
@mysite.com goes to the local user chris.


Now we need to tell postfix to use these settings by updating the /etc/postfix/main.cf file with the following:

1
2
mydestination = $myhostname, /etc/postfix/virtual/domains
virtual_maps  = hash:/etc/postfix/virtual/addresses

We also need to create a hash of the /etc/postfix/virtual/addresses file. To do this we issue the following command:

1
postmap /etc/postfix/virtual/addresses

Now to apply these changes we need to reload postfix, issue the following command:

1
sudo service postfix reload

SASL

Authentication will be done by saslauthd which will need to be configured to support a chrooted Postfix setup.


Fir we need to edit /etc/default/saslauthd and add or change the following settings so that they match:

1
2
START=yes
OPTIONS="-c -m /var/spool/postfix/var/run/saslauthd"

Finish up SASL by creating the chroot directory, adding the postfix user to the sasl group, and then starting saslauthd. Run the following commands to do that:

1
2
3
4
mkdir -p /var/spool/postfix/var/run/saslauthd
dpkg-statoverride --add root sasl 710 /var/spool/postfix/var/run/saslauthd
adduser postfix sasl
/etc/init.d/saslauthd start

Testing Postfix and SASL


At this point, core email services should be up and running. Let’s make sure that you’re in good shape before moving on. First, establish a connection with the mail server by running the following:

1
telnet localhost 25

After establsihing a connection with the Postfix service, run:

1
ehlo localhost

You should see a few lines of output. Make sure that the two most important lines are there:

1
2
3
4
. . .
250-STARTTLS
250-AUTH PLAIN LOGIN
. . .

To exit telnet type

1
quit

Dovecot.

We now need to install and configure Dovecot, set the mailddir parameter, and restart the service to accept the change. To do that run the following commands:

1
2
3
aptitude install dovecot-imapd dovecot-pop3d
perl -pi -e 's/#mail_location =/mail_location = maildir:\/home\/\%u\/Maildir/' /etc/dovecot/conf.d/10-mail.conf
/etc/init.d/dovecot restart

If everything went smoothly you should now be in email nirvana. Each user has their own email account and you can move on to virtual accounts if you desire.


Virtual Accounts

TO DO


Roundcube

In this guide we will be installing Roundcube into directory /var/www/webmail.
First we need to find out what the latest version of Roundcube is. Go to the Roundcube dowload site, here, and notate the version number of the form x.x.x, as we will need this information later. Right click the download button and copy the link. At the time of writing this the version number was 0.8.5 and the download link was http://sourceforge.net/projects/roundcubemail/files/roundcubemail/0.8.5/roundcubemail-0.8.5.tar.gz/download.
Now that we have the link and version number we issue the following commands replacing xxx with the version number and http_link with the link we copied.

1
2
3
cd /tmp && wget -O roundcubemail-xxx.tar.gz http_link
sudo tar -xzvf roundcubemail-xxx.tar.gz -C /var/www
sudo mv /var/www/roundcubemail-xxx/ /var/www/webmail

Now we need to fix file ownership for some of the newly created directories by issuing the following commands:

1
2
sudo chown -R www-data.www-data /var/www/webmail/temp
sudo chown -R www-data.www-data /var/www/webmail/logs

Now we need to create the database and database user for Roundcube in postgres. Issue the following commands to do that:

1

Now we’re all set to start the configuration of Roundcube to do that we will use the web based installer. Navigate to the following address in your web browswer:

1
http://localhost/webmail/installer/

From here just following onscreen prompts to configure your Roundcube instance.

References

RoundCube Install
Postfix Virtual Domains