Postfix/DKIM
For more postfix setup information, see the Complete Virtual Mail Server series, in which this article is included.
DKIM
OpenDKIM
For more information on DKIM, see its Wikipedia page.
First, install mail-filter/opendkim:
root #
emerge --ask opendkim
Configure the package, to generate the domain keys:
root #
emerge --ask --config opendkim
The default opendkim configuration file has some defaults setup, but needs some modification as shown below. Note that the Socket
line in the /etc/opendkim/opendkim.conf file is overridden by the settings in the /etc/conf.d/opendkim file. By default opendkim configures itself using an ipv4 socket, but if opendkim is run on the same server as postfix, a unix socket may be preferable. In the following example configuration file, the mail server is under the domain mail.example.com
and had as a hostname also mail
which the configuration of the package picked up.
If this host handles more (virtual) domains and the same single key is to be used for all the virtual hosts, Domain can point to a file, for example /etc/opendkim/domains with one domain per line. More complex configurations, like separate key per domain, are possible, but are beyond the scope of this example.
Example config:
/etc/opendkim/opendkim.conf
Domain example.com
Selector mail
KeyFile /etc/opendkim/mail.private
# The following line (if uncommented) is overriden by /etc/conf.d/opendkim
#Socket local:/var/run/opendkim/opendkim.sock
UMask 002
ReportAddress postmaster@example.com
PidFile /var/run/opendkim/opendkim.pid
UserID milter
Also prepare the socket/pidfile location:
root #
mkdir /var/run/opendkim
root #
chown milter:milter /var/run/opendkim
With this, opendkim can be started and should be functional:
root #
/etc/init.d/opendkim start
Start "on boot":
root #
rc-update add opendkim default
DNS
Mail and spam filters will verify the signed e-mails by using the key in the DNS system and thus, access to the DNS records is required. A TXT entry needs to be added for domain that is being used to send signed e-mail.
An example of how to add this information to bind is shown here. The public key for the domain is printed in the package configuration step, but can also be found in the example dns record in /etc/opendkim/mail.txt for the mail
host:
/var/bind/domain.tld.hosts
mail._domainkey.example.com. IN TXT "v=DKIM1; k=rsa; p=MIGaslkjD08u98adfaSDSDaasda898932asd...afDaDSD898sDSLSKDJLSDJSLDKJLDSKJ;"
The private key is written to /etc/opendkim/mail.private and should not be shared with anyone.
A restart or reload may be required to synchronize this new record to the secondary servers and propagated through the DNS system. Once the record is visible in the DNS system, the key can be used. Keep this in mind if testing fails, check the domains TXT record.
Postfix
Finally, postfix needs to be informed of the change, depending on whether the inet or unix socket is being used, this has to be added to the postfix configuration file. In the following example both approaches are shown, where the socket variant is preferred. Double check that the socket or port used here match the one from the opendkim configuration file:
/etc/postfix/main.cf
# OpenDKIM mail verification
smtpd_milters = unix:/var/run/opendkim/opendkim.sock
non_smtpd_milters = unix:/var/run/opendkim/opendkim.sock
#smtpd_milters = inet:localhost:8891
#non_smtpd_milters = inet:localhost:8891
Opendkim runs under the milter
user. This is good, but postfix can't access this. Adding postfix to the milter
group solves this easily however:
root #
usermod -a -G milter postfix
Informing postfix of the change makes immediate use of DKIM, a restart is required due to the change of permissions:
root #
/etc/init.d/postfix restart
SPF
To activate SPF (Sender Policy Framework), just add the appropriate DNS entries. SPF works by looking up the given domain (example.com), and searching for DNS TXT entries that hold SPF information. This comes as a list of IP addresses that are allowed to send mail on behalf of the domain. If the IP of the sending server is not found in the SPF record, this counts as a violation of the SPF policy. Thus, it is important that to add ALL servers that are allowed to send mail on behalf of the domain:
/var/bind/domain.tld.hosts
@ IN TXT "v=spf1 mx ~all"
This is an example that should work if the MX record points to every mail server that is authorized to send mail for the setup. If necessary, add further parameters before the "~all" part. "~all" MUST always be the last part of the SPF record.
Option name | Description | Example |
---|---|---|
a | This allows every IP listed in an a record of the domain to send mail. | "v=spf1 a ~all" |
mx | This allows every IP listed in an mx record of the domain to send mail. | "v=spf1 mx ~all" |
/24 | This syntax expands the chosen record to the given CIDR subnet (e.g. 192.168.0.1/24). This works with a, mx, ip4 and ip6 records. | "v=spf1 a/24 ~all" |
ip4 | Allows one to manually specify an IPv4 address to be included as a valid origin. | "v=spf1 ip4:192.168.0.1 ~all" |
ip6 | Allows one to manually specify an IPv6 address to be included as a valid origin. | "v=spf1 ip6:0:0:0:0:0:0:0:1 ~all" |
include:example.org | This syntax allows to include other domains. This performs a lookup of the SPF record of included domains. If the included domain does not have a SPF record, the lookup will fail. | "v=spf1 include:example.org ~all"
example.org's SPF record: "v=spf1 a ~all" |
redirect:example.org | This allows to redirect to other domains. The SPF record of the specified domain will be used. If the target domain has no valid SPF record, an error will be returned. There MUST NOT be any "all"-statement at the end of this record. | "v=spf1 redirect:example.org" |
-all | No mail will be allowed at all. This is obviously not wanted on a mail server, so you should not use this. | "v=spf1 -all" |
~all | If the preceding checks return true, allow. Otherwise, deny. This is the default and should be used in 99% of all cases. Do not use any other "all" statements unless you know what you are doing! | |
+all | This allows all ip addresses to send mail. THIS IS CONSIDERED DANGEROUS! DO NOT ENABLE UNLESS YOU KNOW WHAT YOU ARE DOING! | "v=spf1 +all" |
Testing
To test the DKIM setup, a blank email can be sent to check-auth@verifier.port25.com
. An e-mail will be sent within 30 seconds with a test report.