<p>In this first tutorial, I’ll explain how I’ve configured my mail server using the following :</p>
<ul>
<li>A server running Linux Debian (jessie) ;</li>
<li>Postfix ;</li>
<li>Postfix-policyd-spf-python ;</li>
<li>Dovecot ;</li>
<li>Spamassassin ;</li>
<li>OpenDKIM ;</li>
<li>OpenDMARC ;</li>
<li>Monit ;</li>
<li>Rainloop.</li>
</ul>
<p>I’m assuming you have some basic knowledge of Linux and DNS configuration.</p>
<p>You can host this server at home, but you might have issues with your ISP not allowing outbound traffic on TCP port 25, and your emails might be considered to be spam by other providers if your IP is dynamic and/or you can’t configure a reverse DNS record on it.</p>
<p>The cheapest VMs from <ahref="https://www.digitalocean.com/?refcode=1cd69e4c3389">DigitalOcean</a> or <ahref="http://www.vultr.com/?ref=6804947">Vultr</a> are powerful enough to have this configuration running smoothly.</p>
<p>We’ll also need a SSL certificate for this configuration. You can create an auto-signed one or get a free valid one from <ahref="http://www.startssl.com/">StartSSL</a>. For the purpose of this tutorial, I’ll consider you’ve chosen the latter.</p>
<p>You’ll also need a domain name. I’ve chosen <ahref="http://www.namecheap.com/?aff=85990">Namecheap</a> as a registrar. I won’t go into details on how to configure it, but you’ll need at the very least a A record on your server’s IP as well as a MX record pointing to it.</p>
<p>I use the captainark.net domain as an example throughout this tutorial. You’ll have to use your actual domain for your configuration to work !</p>
<p><em>Note: links in this section are sponsored.</em></p>
<p>During its installation, Postfix will prompt you with configuration questions. Choose “Internet Site”, and when asked about your System mail name, provide it with your server’s FQDN (it should be the output of the <code>hostname -f</code> command on your server).</p>
<p>You’ll also have to set-up a password for the MySQL root user.</p>
<p>The PTR records on your server’s IPv4 and/or IPv6 should match your server’s FQDN (a <code>dig -x</code> on your server’s IP should match a <code>hostname -f</code> on your server).</p>
<p>You’ll have to open the following TCP ports on your server for this configuration to work : 25, 465, 587 and 993.</p>
<p>If you don’t want to have to remember the root user MySQL password, you can create a .my.cnf file in your current user home directory containing the following lines :</p>
<pre><codeclass="language-bash">[client]
host = localhost
user = root
password = myverysecurepassword
socket = /var/run/mysqld/mysqld.sock
</code></pre>
<p>Once it has been created, change the permissions on the file to make sure no other user can read it :</p>
<p>I also like to change the default MySQL shell to see what database I’m using at any given time. Since I use bash, I achieve this the following way :</p>
<p>You’ll have to logout from the current shell for the modification to be taken into account (if you’re using SSH, log out and back into your server).</p>
<p>You should now be able to log into MySQL without specifying a password, and it should look like this :</p>
<pre><codeclass="language-bash">:~$ mysql mysql
[...]
[root@localhost] (mysql)>
</code></pre>
<h2id="configuring-the-mysql-database">Configuring the MySQL database</h2>
<p>We now need to configure the MySQL database Postfix and Dovecot will be using. In this tutorial, we’ll be calling it “mail”, but you can name it whatever you want.</p>
<p>First, in a mysql shell, let’s create the MySQL database :</p>
<p>Now, we are going to create the user that Postfix and Dovecot will be using to access the database. We will only be granting this user select permission :</p>
<pre><codeclass="language-sql">GRANT SELECT ON mail.* TO 'mail'@'localhost' IDENTIFIED BY 'mailpassword';
FLUSH PRIVILEGES;
</code></pre>
<p>We are now going to create the necessary tables for our needs. Let’s first use the mail database :</p>
<pre><codeclass="language-sql">USE mail;
</code></pre>
<p>The first table we are going to create will contain the domains we will be using with our mail server :</p>
<p>Now, all messages sent to alias@captainark.net will be forwarded to example@captainark.net.</p>
<p>Use the same syntax to create additional domains, users and aliases. If you have more than one domains configured, be sure to associate your users and aliases with the correct domain_id.</p>
<p>Purists will probably want to store their certificates in /etc/ssl/private. If you choose to do so, you’ll have to adapt the path of those files for the remainder of this tutorial.</p>
<p>If you’ve decided to create a certificate with StartSSL, you’ll end up with two files, a .crt and a .key. I’ll name those files server.crt and server-with-passphrase.key. Put both these files in the folder we’ve just created.</p>
<p>Now, let’s remove the passphrase from the key :</p>
<p>The variable “myhostname” has to be defined to you server’s FQDN. The file /etc/mailname should contain your server’s FQDN as well.</p>
<p>Next, we need to edit the /etc/postfix/master.cf file. You need to uncomment the following lines :</p>
<h3id="dovecot-global-configuration">Dovecot global configuration</h3>
<p>By default, on Debian, <ahref="http://www.dovecot.org/">Dovecot</a> uses multiple configuration files in /etc/dovecot/conf.d. I found it annoying to maintain, and I ended up only using the /etc/doveconf.conf file.</p>
<p>As always, let’s start by backing up the original configuration file :</p>
<p>Dovecot will create the virtual users’ folders automatically.</p>
<h3id="dovecot-access-to-the-mysql-database">Dovecot access to the MySQL database</h3>
<p>We now need to allow Dovecot to connect to the mail database we have populated earlier. To do so, we are going to create a /etc/dovecot/sql.conf file with the following content :</p>
password_query = SELECT email as user, password FROM virtual_users WHERE email='%u';
</code></pre>
<p>You’ll have to change the password to the one you have defined earlier. Since this file contains a password, let’s make sure it’s not world-readable :</p>
<p>The last thing we need to configure here is sieve. The idea is to have all messages flagged as spam automatically moved to the mailbox Junk folder.</p>
<p>To do so, let’s first create the required folders :</p>
<p>If you want to have sieve rules for a specific user, simply create $user@$domain.sieve file in the users folder (example@captainark.net in my case).</p>
<p>All .sieve files in the before folder will be used for all your virtual users, before their individual configuration ; the .sieve files in the after folder will be used, well, you guessed it, after.</p>
<p>Let’s create a filter.sieve file in the /var/mail/sieve/before folder with the following content :</p>
<p>Next thing we have to do is to configure the actual anti-spam. I tried a few, but I ended up sticking with <ahref="http://spamassassin.apache.org/">SpamAssassin</a>. Here’s why :</p>
<ul>
<li><ahref="http://dspam.nuclearelephant.com/">DSPAM</a> is <ahref="http://sourceforge.net/p/dspam/mailman/message/32585111/">no longer maintained</a> and <ahref="https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=754810">has been removed from Debian Jessie</a> ;</li>
<li><ahref="https://rspamd.com/">Rspamd</a> is interesting, has been <ahref="https://packages.debian.org/source/jessie/rspamd">integrated in Debian Jessie</a>, but is poorly documented at this time ;</li>
<li><ahref="http://bogofilter.sourceforge.net/">Bogofilter</a> does not seem to have the greatest server integration.</li>
</ul>
<h3id="the-actual-configuration">The actual configuration</h3>
<p>SpamAssassin’s configuration is pretty straightforward. First, let’s edit the /etc/default/spamassassin file :</p>
<pre><code>ENABLED=1
[...]
CRON=1
</code></pre>
<p>Before the cron runs for the first time, we have to manually update SpamAssassin’s ruleset :</p>
<pre><codeclass="language-bash">sa-learn
</code></pre>
<p>Next, as usual, let’s back up the original configuration file :</p>
<p>Let’s create a new /etc/spamassassin/local.cf file with the following content :</p>
<pre><code>rewrite_header Subject [SPAM]
report_safe 0
required_score 5.0
use_bayes 1
bayes_auto_learn 1
whitelist_from *@captainark.net
</code></pre>
<p>Next, to have Postfix send incoming emails through SpamAssassin, we have to edit the /etc/postfix/master.cf file. At the very beginning, we have to add a line under the smtp definition :</p>
<pre><code>smtp inet n - - - - smtpd
-o content_filter=spamassassin
</code></pre>
<p>At the very end of the same file, we have to add the following lines :</p>
<p>That’s all for SpamAssassin ! To check if it is working, send yourself an email from another provider. You should see the following headers in it :</p>
<pre><code>X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
myserver.captainark.net
X-Spam-Level:
</code></pre>
<h2id="configuring-spf">Configuring SPF</h2>
<h3id="allowing-your-server-to-send-emails-for-your-domain">Allowing your server to send emails for your domain</h3>
<p><ahref="http://www.openspf.org/">SPF</a> (Sender Policy Framework) is a mechanism that confirms that your server’s IP is allowed to send emails for your domain. Technically, it is a TXT DNS record which looks something like this :</p>
<pre><code>captainark.net IN TXT "v=spf1 mx ~all"
</code></pre>
<p>This DNS record lets other mail servers know that hosts that have a MX record for my domain are also allowed to send emails for it.</p>
<p>For more information on SPF syntax, you can consult the <ahref="http://www.openspf.org/SPF_Record_Syntax">official documentation</a>.</p>
<p>Without a properly configured SPF record, other mail servers might flag your emails as spam or outright drop them.</p>
<h3id="checking-spf-record-for-inbound-mail">Checking SPF record for inbound mail</h3>
<p>Now that we have set up our own SPF record, let’s configure Postfix to check that other mail servers communicating with us have done the same.</p>
<p>First, let’s add the two following lines at the end of /etc/postfix-policyd-spf-python/policyd-spf.conf :</p>
<pre><code>Header_Type = AR
Authserv_Id = "<server's FQDN>"
</code></pre>
<p>Then, let’s edit the /etc/postfix/master.cf file and add the following lines at the end :</p>
<pre><code>policy-spf unix - n n - - spawn
user=nobody argv=/usr/bin/policyd-spf
</code></pre>
<p>Let’s now edit the /etc/postfix/main.cf. In the “smtpd_recipient_restrictions” section, add the “check_policy_service” line as seen below :</p>
<p><ahref="http://www.dkim.org/">DKIM</a> (DomainKeys Identified Mail) is a mechanism that validates a domain name identity for an email through cryptographic authentication.</p>
<p>While not mandatory, setting up DKIM improves the odds of emails sent from your server not being flagged as spam by other providers.</p>
<p>With this configuration, OpenDKIM will also check the key for inbound emails.</p>
<h3id="software-side">Software side</h3>
<p>First, let’s backup the original configuration file and create a folder for the configuration files :</p>
<p>For DKIM to work, you have to configure a DNS TXT record in your zone. This record was automatically generated by OpenDKIM in the mail.txt file mentioned earlier :</p>
<pre><code>mail._domainkey IN TXT "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCkJq0CW3tl2XHZ1CN5XdbqRDU7KfXOJ70nlwI09bHmDU63/Yz3J5rl863S0t2ncVHfIudZANj0OaiJe5HRR7WCsjuNIhQFfPFGIWLNClpxqdQVQURI38sAGeyn7Ed/Cor1AiWABzFWzel0kvXILw8K/NTzxaAPeSa9ttwQEgSmowIDAQAB" ; ----- DKIM key mail for captainark.net
</code></pre>
<p>All you have to do is to copy and paste this record in your DNS zone file.</p>
<p>To make sure that OpenDKIM is working, you can send an empty email to <ahref="mailto:check-auth@verifier.port25.com">check-auth@verifier.port25.com</a>. You should receive a response with the following content :</p>
<p><ahref="http://dmarc.org/">DMARC</a> (Domain-based Message Authentication, Reporting & Conformance) standardizes SPF and DKIM authentication mechanisms.</p>
<p>It lets the owner of a domain name indicate that his email is protected by SPF and/or DKIM and what other providers should do with emails that do not pass those checks.</p>
<h3id="software-side-1">Software side</h3>
<p>Once again, let’s backup the original configuration file :</p>
<p>DMARC, like SPF and DKIM, is based on DNS TXT records.</p>
<p>Here is how I configured it for the captainark.net domain :</p>
<pre><code>_dmarc IN TXT "v=DMARC1; p=none; rua=mailto:postmaster@captainark.net; ruf=mailto:postmaster@captainark.net"
</code></pre>
<p>This tells other providers to not reject or quarantine emails should a SPF or DKIM check fail, but to send a daily report of those checks to postmaster@captainark.net.</p>
<p>For more information on the DMARC syntax, here is an <ahref="https://support.google.com/a/answer/2466563?hl=en">article from Google</a>.</p>
<h2id="configuring-monit">Configuring Monit</h2>
<p><ahref="http://mmonit.com/monit/">Monit</a> is a daemon that makes sure that other daemons are running. If they crash, it restarts them automatically. Is is not directly related to a mail server per say, but it’s pretty easy to set up.</p>
<p>First, as always, let’s backup the original configuration file :</p>
<p><ahref="http://www.rainloop.net/">Rainloop</a> is a web-based email client. I won’t go into details on how to configure it in this tutorial ; here’s a link to the <ahref="http://www.rainloop.net/docs/installation/">official documentation</a>.</p>
<p>You’ll need a web server with PHP 5.3+ to run Rainloop. You do not have to run Rainloop on the same host as your mail server. No database is required.</p>
<h2id="conclusion">Conclusion</h2>
<p>We now have a mail server that should be running pretty smoothly. It could still be improved by setting up things such as greylisting or virus detection.</p>
<p>If you have found this tutorial useful, if you’ve found an error in it or if you have any question, please feel free to leave a comment below or to contact me on <ahref="https://twitter.com/captainark">Twitter</a>.</p>
<h2id="references">References</h2>
<p>Here are the tutorials I used to set up my own mail server :</p>
<ul>
<li><ahref="http://sealedabstract.com/code/nsa-proof-your-e-mail-in-2-hours/">A complete tutorial on setting up a mail server</a></li>
<li><ahref="https://www.digitalocean.com/community/tutorials/how-to-configure-a-mail-server-using-postfix-dovecot-mysql-and-spamassasin">A third tutorial from DigitalOcean</a></li>
<li><ahref="https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-dkim-with-postfix-on-debian-wheezy">A tutorial on setting up OpenDKIM</a></li>
<li><ahref="https://guillaume.vaillant.me/?p=481">A tutorial on setting up OpenDMARC</a> (in french)</li>
<sectionclass="poweredby">Proudly generated by <aclass="icon-hugo"href="http://gohugo.io">HUGO</a>, with <aclass="icon-theme"href="https://github.com/vjeantet/hugo-theme-casper">Casper</a> theme</section>