Postfix + OpenDKIM (DKIM) configuration example
Introduction
One of the standard and widely used email message sender authentication mechanism is DomainKeys Identified Mail (DKIM). DKIM relies in private/public key pair cryptography (asymmetric cryptography).
The message sender holds a private key that is used to sign messages he sends. The resulting message digest is included in the mail message itself as a MIME header. The public key is published as a DNS entry, so when a receiver receives a signed message he fetches the public key and then checks if the message content, together with the included digest, are legitimate and were not tampered by any means.
This is straight-forward asymmetric cryptography.
The tutorial considers the following software and environment:
- Ubuntu 12.10
- Postfix 2.9.6
- OpenDKIM 2.6.8
Installing OpenDKIM
We will integrate our Postfix instance with OpenDKIM. OpenDKIM will then be used to sign all messages sent by Postfix. In order to install OpenDKIM issue the following command:
Generating the keys
Now we will generate the needed private and public keys. We will assume that the keys will be generated in:
/etc/opendkim
Place yourself in /etc/opendkim and issue the following commands:
openssl rsa -in private.key -out public.key -pubout -outform PEM
chmod 600 /etc/opendkim/private.key
Your private key will be stored in private.key and your public key will be stored in public.key
Creating the hosts file
We will create a hosts file. This file contains the hosts which are considered internal so any message sent to this hosts does not need to be signed by our Postfix instance. Create the following file:
/etc/opendkim/opendkimhosts
Insert the following contents in the file:
localhost
# Your IP addresses (one per line)
215.222.32.54
#Your hostnames (one per line)
example.com
Configuring OpenDKIM
Now we will configure OpenDKIM. Edit the following file:
/etc/opendkim.conf
Comment the following line:
Insert the following lines:
KeyFile /etc/opendkim/private.key
Selector mail
InternalHosts /etc/opendkim/opendkimhosts
ExternalIgnoreList /etc/opendkim/opendkimhosts
AutoRestart yes
Background yes
Canonicalization simple
DNSTimeout 5
Mode sv
SignatureAlgorithm rsa-sha256
SubDomains no
#UseASPDiscard no
#Version rfc4871
X-Header no
Now edit the following file:
/etc/default/opendkim
Insert the following line:
Configuring Postfix
Now we need to configure Postfix. Edit Postfix main.cf file and add the following lines:
milter_protocol = 2
smtpd_milters = inet:localhost:8891
non_smtpd_milters = inet:localhost:8891
Setup the DNS entry
The last step is to make our public key to be publicly available. We set it up as a DNS entry. Go into your DNS configuration and add the following TXT record:
mail._domainkey.example.com
entry value:
v=DKIM1; k=rsa; p=[THE_CONTENT_FROM_YOUR_PUBLIC_KEY_FILE]
Note2: The content of your public key file is placed in the record value without the square brackets.
Testing
When you send a message and inspect the message content you will see a header similar to the following:
Configuring a mail server can be problematic especially if you are doing it for the first time. Postfix provides a robust logging mechanism and by looking at the logs you can almost certainly check what went wrong. The log file is located at: