How to deploy a personal email server

Although emails are a very common thing on internet, deploying an email server is not trivial. Therefore, you may find a lot of tutorials on the web, more or less trustworthy, more or less complete and more or less out-of-date. Each of those tutorial uses it's favorite software and has its own way of doing things, so here is mine.

Prerequisites

This guide supposes you:

  • have admin rights on a machine running an UNIX-like operation system;
  • own a domain name;
  • know how to modify the DNS entries associated to your domain name;
  • know what are X.509 certificates (improperly called SSL certificates);
  • know how to use your operating system (in particular, know how to manage daemons, packages, ports, …);
  • understand that depending on your distribution some files may have a different location;
  • are able to perform some research all by yourself;
  • do not copy/paste stuff without understanding it.

First things first: the DNS

In order to access the email server, it is important to add an MX field to our domain name. In this case, we have it referring to a sub-domain which has both a A and a AAAA entries.

example.org.      IN  MX 10 mx1.example.org.
mx1.example.org.  IN  A     203.0.113.42
mx1.example.org.  IN  AAAA  2001:db8::42

Before going any further, you must check whether or not your domain name has a valid FCrDNS. In short, reverse DNS lookup to the IP addresses must return the associated domain name.

$ dig +short -x "203.0.113.42"
mx1.example.org.
$ dig +short -x "2001:db8::42"
mx1.example.org.

If it's not the case, fix your DNS configuration.

SPF and DMARC

In order to fight against identity theft, there is several solutions. Two of them are SPF and DMARC. To configure them, we have to add TXT entries defining restrictions to apply to emails coming from our domain name. Example:

example.org.             IN  TXT  "v=spf1 a mx ip6:2001:db8::/32 -all"
_dmarc.example.org.      IN  TXT  "v=DMARC1;adkim=s;aspf=s;p=reject;sp=reject;ruf=mailto:postmaster@example.org;fo=1"

The MTA

The MTA (Mail Transfer Agent) is the software which allows us to send and receive emails. There is many of those (qmail, exim, postfix, sendmail, …), here we will focus on an MTA that is stable, full-featured and extremely easy to configure: OpenSMTPD. Beware, this tutorial requires OpenSMTPD version 6.4. This specific version is not wildly available on GNU/Linux since it is linked to LibreSSL, unlike the preceding versions that allowed OpenSSL 1.0.

Once installed, the configuration is done in /etc/mail/smtpd.conf and man 5 smtpd.conf comes in handy. To have a server able to receive emails for the example.org domain name, all we need to have is this:

listen on 127.0.0.1
listen on ::1

listen on 203.0.113.42
listen on 2001:db8::42

table aliases "/etc/smtpd/aliases"

action local_deliver maildir alias <aliases>

match action local_deliver
match from any for domain "example.org" action local_deliver

We will now make this configuration more robust and functional by using TLS and authorizing users to send emails.

pki example.org cert "/etc/ssl/private/example.org.pem"
pki example.org key  "/etc/ssl/private/example.org.key"

# SMTP port 25 from localhost
listen on 127.0.0.1    tls pki example.org hostname mx1.example.org
listen on ::1          tls pki example.org hostname mx1.example.org

# SMTP port 25 from external sources
listen on 203.0.113.42 tls pki example.org hostname mx1.example.org
listen on 2001:db8::42 tls pki example.org hostname mx1.example.org

# SMTPS port 465 from authenticated users
listen on 203.0.113.42 smtps pki example.org auth hostname mx1.example.org mask-src
listen on 2001:db8::42 smtps pki example.org auth hostname mx1.example.org mask-src

table aliases "/etc/smtpd/aliases"

action local_deliver maildir alias <aliases>
action relay_out relay helo example.org

match action local_deliver
match from any for domain "example.org" action local_deliver
match from any auth for any action relay_out

The private key must not be accessible (read, write, execute), excepted for the owner. It is therefore recommended to use chmod 600 or chmod 400 on it.

Ports

We shall accept incoming TCP connections on ports 25 (SMTP) and 465 (SMTPS).

Aliases

In our case, only local users may receive emails. Whichever your MTA is, you'll wish to configure aliases. At least, we should redirect emails addressed to root to a different user and also configure some common aliases. In order to do so, let's edit /etc/smtpd/aliases:

#
# Please run `smtpctl update table <table_name>` after any change to this file.
#

# Person who should get root's mail. Don't receive mail as root!
root:           your_username

# Basic system aliases
MAILER-DAEMON:  postmaster
postmaster:     root

# General redirections for pseudo accounts
bin:            root
daemon:         root
nobody:         root
decode:         root

# Well-known aliases
manager:        root
dumper:         root
operator:       root
abuse:          root
admin:          root
webmaster:      root
hostmaster:     root
spam:           root

# Personal aliases

After each modification of this file, we have to run smtpctl update table aliases.

About email distribution

There is many ways to store someone's emails. The most common are:

  • maildir: emails are stored within the user's personal directory, usually in the ~/Maildir folder
  • mbox: emails are stored in the /var/mail directory

In this case, we used maildir and will continue to do so. The folder will be created if it doesn't already exists.

Shitlist

Some unscrupulous administrators do not hesitate to bulk send emails without asking the recipients first and do not handle their with to be withdrawn from such mailing lists. It's not all about scams or malicious activities, quite often it's more about corporate advertisement or sales prospection sent by legitimate companies. Because the origin domains does not vary much, it's easy to create a list (opensmtpd-shitlist) and reject any email coming from one of those domains. Once the file is downloaded, integrating it to OpenSMTPD is trivial:

table shitlist "/etc/smtpd/shitlist"
match from any mail-from <shitlist> for any reject

Fetching our emails

Right now, emails are stored in our ~/Maildir folder. In order to fetch them from our MUA (Mail User Agent, like ThunderBird or mutt) we now have to deploy a POP or IMAP server. Let's use Dovecot. To configure it, you should first run doveconf -nP to get the list of parameters with non-default values. In my configuration, I:

  • activated IMAP but not POP;
  • increased the security by configuring SSL/TLS in a decent way;
  • add support for sieve.
# 2.3.6 (7eab80676): /etc/dovecot/dovecot.conf
# Pigeonhole version 0.5.6 (92dc263a)
# OS: Linux 4.19.53-1-lts x86_64
# Hostname: localhost.localdomain
auth_verbose = yes
listen = 203.0.113.42, 2001:db8::42
mail_debug = yes
mail_location = maildir:~/Maildir
managesieve_notify_capability = mailto
managesieve_sieve_capability = fileinto reject envelope encoded-character vacation subaddress comparator-i;ascii-numeric relational regex imap4flags copy include variables body enotify environment mailbox date index ihave duplicate mime foreverypart extracttext editheader imapflags notify
namespace inbox {
  inbox = yes
  location =
  mailbox Drafts {
    special_use = \Drafts
  }
  mailbox Junk {
    special_use = \Junk
  }
  mailbox Sent {
    special_use = \Sent
  }
  mailbox "Sent Messages" {
    special_use = \Sent
  }
  mailbox Trash {
    special_use = \Trash
  }
  prefix =
  separator = .
}
passdb {
  driver = pam
}
plugin {
  sieve = ~/.dovecot.sieve
  sieve_dir = ~/sieve
  sieve_extensions = +notify +imap4flags +imapflags +editheader
}
postmaster_address = postmaster@example.org
protocols = imap sieve lmtp
ssl = required
ssl_cert = </etc/ssl/private/example.org.pem
ssl_cipher_list = HIGH:!aNULL:!eNULL:!LOW:!MEDIUM:!EXP:!RC4:!3DES:!MD5:!SHA1:!PSK:!kRSA:!SRP:-AES128:-DH:+ECDH
ssl_dh = </etc/dovecot/dh.pem
ssl_key = </etc/ssl/private/example.org.key
ssl_min_protocol = TLSv1.2
ssl_prefer_server_ciphers = yes
userdb {
  driver = passwd
}
protocol lmtp {
  mail_plugins = " sieve"
}
protocol lda {
  mail_plugins = " sieve"
}

Be careful with the certificate, you really should use the default self-generated one. Please note the minimal TLS version is 1.2 since previous version are not recommended. In fact, TLS 1.2 is also not recommended, ideally we''l use only TLS 1.3. Unfortunately, Dovecot currently does not allows to set TLS 1.3 as the minimal version.

You should also notice the TLS configuration which forbids traditional Diffie-Hellman key exchanges but allows the variant based on elliptical curves. Therefore, there is no need to worry about the default weak DH parameters. However, if you enable back the traditional DH, you really should generate new strong DH parameters with openssl dhparam -outform PEM -out "/etc/dovecot/dh.pem" 4096.

Ports

We shall accept incoming TCP connections on port 993 (IMAPS).

The MDA

While the MTA is able to send and receive emails, the MDA (Mail Delivery Agent) is the software which delivers the emails to the recipients. Most SMTP servers can act as both an MTA and an MDA, which is the case for OpenSMTPD. That's why we have been able to configure it to deliver emails into the maildir folder.

An other MDA

Using the MDA integrated into our SMTP server works, however it is quite limited in term of functionalities. Instead, we'll use the one that comes with dovecot and interface it using LMTP. This MDA has the advantage of using sieve.

OpenSMTPD

In OpenSMTPD's configuration, all we need to do is to edit our local_deliver action and give it the LMTP's socket path:

action local_deliver lmtp "/run/dovecot/lmtp" alias <aliases>

The socket's address may vary depending on your operating system.

Filtering emails

Now we can use sieve to filter emails. Each user may set his own sieve rules in the ~/.dovecot.sieve file. Here is an example:

require ["fileinto", "imapflags"];

if anyof(header :contains "x-spam-level" "****", header :contains "x-fose-spam" "This message appears to be spam.") {
  setflag "\\Seen";
  fileinto "Junk";
}
elsif address :is ["From", "To", "Cc", "Reply-to"] "nantes@lists.afpy.org" {
  fileinto "INBOX.Python.Nantes";
}
elsif address :is ["From", "To", "Cc", "Reply-to"] "python@example.org" {
  fileinto "INBOX.Python";
}

This example put emails marked as spam in the spam folder. Then, it sorts emails into custom folders.

DKIM

DomainKeys Identified Mail (DKIM) is an other way to fight against identity theft. The principle is to sign (at the server level) all sent emails and publish the public key in a DNS entry. Thank to this, when receiving an email from whatever@example.org, a DNS query is made to a sub-domain of example.org in order to fetch the public key allowing to verify the email's signature. If everything is ok, it proves the email has been sent by a server which has the associated private key.

OpenDKIM certainly is the most popular DKIM implementation. Unfortunately, it uses a milter to be integrated in the MTA, which is not supported by OpenSMTPD. We'll have a to a lesser known solution: dkimproxy.

Signing outgoing emails

As its name shows, dkimproxy acts as an SMTP proxy. One sends it an email via SMTP on a specific port and it returns the same email, signed, on a different port. To configure it, we'll suppose the dkimproxy configuration is located in the /etc/dkimproxy directory instead of the default one.

# mkdir -p "/etc/dkimproxy/private"
# chown dkimproxy:root "/etc/dkimproxy/private"
# chmod 700 "/etc/dkimproxy/private"
# cd "/etc/dkimproxy/private"
# openssl genrsa -out "private.key" 2048
# openssl rsa -in "private.key" -pubout -out "public.key"
# chown dkimproxy:root "private.key" "public.key"
# chmod 600 "private.key"

And for /etc/dkimproxy/dkimproxy_out.conf :

# specify what address/port DKIMproxy should listen on
listen    127.0.0.1:10027

# specify what address/port DKIMproxy forwards mail to
relay     127.0.0.1:10028

# specify what domains DKIMproxy can sign for (comma-separated, no spaces)
domain    example.org

# specify what signatures to add
signature dkim(c=relaxed)
signature domainkeys(c=nofws)

# specify location of the private key
keyfile   /etc/dkimproxy/private/private.key

# specify the selector (i.e. the name of the key record put in DNS)
selector  pubkey

Then add a TXT entry for pubkey._domainkey.example.org with the value v=DKIM1; k=rsa; t=s; p=contenu_de_la_clef_publique (the available options are defined in RFC 6376).

Beware, with RSA keys of 2048 bits or higher the public key's length will exceed the 255 bytes limit of a TXT entry. As shown in RFC 4408, the solution is to split it into several strings that will be concatenated.

pubkey._domainkey.example.org.  IN  TXT  "v=DKIM1; k=rsa; t=s; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEjPmxQHTJeBoHSJPuPMl0ry0q31TqoC2OuqUiVHSk3hM6x6oDat4pIischVYi/3ODsjBC6wg1BPgtfdVfzboAF/bQK+Kh0rBaQlv2vloP96mu6dmUGMJSo5BrtoAMdx6DbMe7s7TP58uAUknQNEL6s9w7iNF9gD/+yBiEB+yDYwIDAQAB"

At last, let's configure OpenSMTPD to have him use dkimproxy.

listen on lo port 10028 tag DkimOut

action to_dkimproxy relay host smtp://127.0.0.1:10027
action relay_out relay helo example.org

match from any tagged DkimOut for any action relay_out
match for any action to_dkimproxy
match from any auth for any action to_dkimproxy

Anti-spam

For now, I uses Amavis to detect spam. By default, it listens on port 10024 and returns the email on port 10025 with additional headers indicating the spam status.

listen on lo port 10025 tag Clean

action to_amavisd relay host smtp://127.0.0.1:10024

match from any tag Clean for domain <domains> action local_deliver
match from any for domain <domains> action to_amavisd

The MUA

The configuration of your email client should be done as follows:

Incoming server

  • Name/address: your domain name
  • Protocol: IMAP
  • Security: SSL/TLS
  • Port: 993
  • Authentication method: normal password
  • Username: your username (no alias or full email)
  • Password: your account's password

Outgoing server

  • Name/address: your domain name
  • Security: SSL/TLS
  • Port: 465
  • Authentication method: normal password
  • Username: your username (no alias or full email)
  • Password: your account's password

Summary

Receiving an email

Réception d'un email

  1. OpenSMTPD accepts the email comming from Bob;
  2. OpenSMTPD gives it to Amavis in order to detect whether or not it's a spam;
  3. Amavis gives it back to OpenSMTPD;
  4. OpenSMTPD sends it to Dovecot via LMTP;
  5. Dovecot puts the email in John's ~/Maildir folder (not illustrated: it uses sieve);
  6. John look his emails;
  7. Dovecot fetches the new email from Bob;
  8. Dovecot transfers the email to John's email client.

Sending an email

Émission d'un email

  1. John sends his email to OpenSMTPD;
  2. OpenSMTPD gives the email to DkimProxy;
  3. DkimProxy gives it back to OpenSMTPD after signing it;
  4. OpenSMTPD sends the now signed email to the recipients.

OpenSMTPD's configuration

# This is the smtpd server system-wide configuration file.
# See smtpd.conf(5) for more information.


#
# Encryption
#

pki example.org cert "/etc/ssl/private/example.org.pem"
pki example.org key  "/etc/ssl/private/example.org.key"


#
# Listening
#

# SMTP port 25 from localhost
listen on 127.0.0.1 tls pki example.org hostname mx1.example.org
listen on ::1       tls pki example.org hostname mx1.example.org

# SMTP port 25 from external sources
listen on 203.0.113.42 tls pki example.org hostname mx1.example.org
listen on 2001:db8::42 tls pki example.org hostname mx1.example.org

# SMTPS port 465 from authenticated users
listen on 203.0.113.42 smtps pki example.org auth hostname mx1.example.org mask-src
listen on 2001:db8::42 smtps pki example.org auth hostname mx1.example.org mask-src

# signed by dkimproxy
listen on lo port 10028 tag DkimOut

# filtered by amavisd
listen on lo port 10025 tag Clean


#
# Tables
# If you edit a file, you have to run "smtpctl update table <table_name>"
#

table aliases "/etc/smtpd/aliases"
table shitlist "/etc/smtpd/shitlist"


#
# Actions
#

action local_deliver lmtp "/run/dovecot/lmtp" alias <aliases>
action to_amavisd    relay host smtp://127.0.0.1:10024
action to_dkimproxy  relay host smtp://127.0.0.1:10027
action relay_out     relay helo example.org


#
# Matches
#

# Reject emails from banned domains
match from any mail-from <shitlist> for any reject

# Deliver local emails
match action local_deliver
match for domain "example.org" action local_deliver
match from any auth for domain "example.org" action local_deliver

# Deliver filtered emails
match from any tag Clean for domain "example.org" action local_deliver

# Send unfiltered emails to amavisd
match from any for domain "example.org" action to_amavisd

# Relay outgoing emails when signed
match from any tag DkimOut for any action relay_out

# Send outgoing unsigned emails to dkimproxy
match for any action to_dkimproxy
match from any auth for any action to_dkimproxy

Tests

Some tools can help you to check whether or not your configuration is working as expected:

Other emails with an auto-reply to test DKIM and SPF

In order to be sure the emails you send have been signed and SPF is correctly configured, you can send an email to check-auth@verifier.port25.com (IPv6 + IPv4).

IPv6 support

To check if your server handles IPv6, you can send an email to test@doesnotwork.eu and wait for the auto-reply.

Links