Console Login

Postfix on CentOS 6: Mail Delivery with PTR, SPF and DKIM

The problem

A freshly installed Postfix server accepts and sends mail, but the messages do not arrive where they should. Typical symptoms in /var/log/maillog and in the recipient's headers:

  • Remote servers answer with 5xx rejections that mention the client's reverse DNS, the HELO name or a blocklist.
  • Messages are accepted but filed as spam at large webmail providers, with spf=none or dkim=none in the authentication results.
  • Users on home or office networks cannot send because outbound port 25 is filtered, and there is no secure authenticated alternative.
  • With a careless "minimal" configuration, the server relays mail for anyone, and its IP address ends up on public blocklists.

A working daemon is not enough. Receivers judge a sending server on its DNS identity, on whether the message can be tied to the domain, and on how well the server protects itself against abuse.

Why it happens

Several independent causes usually combine:

  • Identity mismatch. The IP address has no PTR record, or the PTR name differs from myhostname, or the name has no matching A record. Many receivers treat a missing or mismatched reverse name as a strong spam signal.
  • No domain-level authentication. Without an SPF record and a DKIM signature, a receiver cannot tell the mail from forged mail using the same domain.
  • Weak access control. If smtpd_recipient_restrictions lacks reject_unauth_destination, the server is an open relay. Without HELO and sender sanity checks, it accepts traffic that well-run servers refuse.
  • No encrypted submission. Authenticated users need a separate port (587) with mandatory TLS. Otherwise they send in clear text or not at all.
  • Disk latency. The mail queue is written to disk constantly, so slow or contended storage lets the queue back up under load.

Source of truth

The Postfix documentation is the reference for the mail-server side of the fix:

  • Postfix SMTP relay and access control confirms that restrictions are lists evaluated per stage of the SMTP conversation. Placing a PERMIT before a REJECT creates exceptions, which is why permit_mynetworks and permit_sasl_authenticated come first and reject_unauth_destination follows.
  • Postfix TLS Support confirms that TLS in the SMTP server is switched on with smtpd_tls_security_level. The value encrypt makes the server announce STARTTLS and accept no mail without encryption. This is the current replacement for the old smtpd_enforce_tls parameter.
  • Red Hat Enterprise Linux 6 Security Guide, Postfix and SASL confirms that the RHEL 6 Postfix (and so CentOS 6) can use Dovecot or Cyrus SASL for SMTP AUTH, enabled with smtpd_sasl_auth_enable = yes. It points to the Postfix SASL_README for server details.

SPF and DKIM are DNS-published mechanisms defined in their own specifications (RFC 4408 and RFC 6376). The exact record syntax comes from those and from the OpenDKIM tools, which generate the DKIM record text.

Fix options

Mail server and submission

  • Minimal Postfix plus an external relay. Relay all outbound mail through a provider's smarthost. This avoids IP reputation work but hands control and deliverability to a third party.
  • Self-hosted Postfix with restrictions, TLS submission and SASL. The server controls its own identity and policy, at the cost of DNS and maintenance work.

SASL backend

  • Cyrus SASL with saslauthd. Checks system accounts through PAM. It is quick to set up on a single host.
  • Dovecot SASL. Best when Dovecot already serves IMAP/POP and virtual users.

Domain authentication

  • SPF only. Easy, but it breaks on forwarding and says nothing about message integrity.
  • SPF plus DKIM through a milter (OpenDKIM). The signature survives forwarding, and the milter plugs into Postfix without patching it.

Why this fix

The self-hosted combination is chosen because it covers every cause above with standard components in CentOS 6 and EPEL: Postfix 2.6, Cyrus SASL and OpenDKIM. Cyrus SASL with saslauthd keeps the example short and needs no extra daemon besides saslauthd. SPF and DKIM are used together because receivers evaluate both, and either one can fail legitimately in some paths.

The configuration below also corrects several points that commonly appear in short tutorials:

  • smtpd_enforce_tls is obsolete. Use smtpd_tls_security_level = encrypt for the submission service, and provide a certificate and key.
  • Enabling smtpd_sasl_auth_enable does nothing useful without a working SASL backend.
  • DKIM does not work until the public key is published in DNS. Generating the key is only half of the job.
  • HELO-related checks belong in smtpd_helo_restrictions. The recipient list should keep only the relay and sender/recipient checks.
  • SPF -all is a hard-fail policy: receivers may reject or discard mail from other IPs, though how they act is their own decision. ~all (soft fail) is the safer choice while testing.
  • The blocklist check runs at RCPT time. This is before the message body is transferred, but it is a rejection of the recipient command, not a dropped connection.

Step by step

1. DNS identity first

Create an A record for mail.yourdomain.no and set the PTR of the server IP to the same name. Use the same name as myhostname. Check both directions (replace 192.0.2.10 with the real address):

dig +short A mail.yourdomain.no
dig +short -x 192.0.2.10

Each query should return the other half of the pair.

2. Install packages and make Postfix the MTA

yum install postfix cyrus-sasl cyrus-sasl-plain mailx
alternatives --set mta /usr/sbin/sendmail.postfix
chkconfig postfix on
chkconfig saslauthd on

If Sendmail is installed, remove it with yum remove sendmail before the chkconfig steps. Disabling a service that is already removed fails.

3. Core main.cf

myhostname = mail.yourdomain.no
mydomain = yourdomain.no
myorigin = $mydomain
inet_interfaces = all
inet_protocols = ipv4
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
mynetworks = 127.0.0.0/8
home_mailbox = Maildir/
smtpd_banner = $myhostname ESMTP
disable_vrfy_command = yes

smtpd_helo_required = yes
smtpd_helo_restrictions =
    permit_mynetworks,
    reject_invalid_helo_hostname,
    reject_non_fqdn_helo_hostname

smtpd_recipient_restrictions =
    permit_mynetworks,
    permit_sasl_authenticated,
    reject_unauth_destination,
    reject_non_fqdn_sender,
    reject_non_fqdn_recipient,
    reject_unknown_sender_domain,
    reject_rbl_client zen.spamhaus.org

Blocklist lookups depend on a working DNS resolver. A local caching resolver is preferable to a shared one, because operators of public blocklists may limit or refuse queries from heavily used resolvers. Check the operator's usage terms.

4. SASL backend

Create /etc/sasl2/smtpd.conf:

pwcheck_method: saslauthd
mech_list: PLAIN LOGIN

Add to main.cf:

smtpd_sasl_type = cyrus
smtpd_sasl_security_options = noanonymous
smtpd_tls_cert_file = /etc/pki/tls/certs/mail.crt
smtpd_tls_key_file = /etc/pki/tls/private/mail.key

Create a certificate (self-signed is enough for testing; use a CA-signed one for production) and start the authentication daemon:

openssl req -new -x509 -nodes -days 365 -newkey rsa:2048 -subj "/CN=mail.yourdomain.no" -keyout /etc/pki/tls/private/mail.key -out /etc/pki/tls/certs/mail.crt
chmod 600 /etc/pki/tls/private/mail.key
service saslauthd start

5. Submission service on port 587

In /etc/postfix/master.cf, enable or add:

submission inet n       -       n       -       -       smtpd
  -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject

6. SPF record

Publish a TXT record for yourdomain.no. Start with a soft fail and tighten it once the mail flow is verified:

v=spf1 mx a ip4:192.0.2.10 ~all

7. DKIM with OpenDKIM

Install the EPEL release package from the EPEL project (it is not in the base repository), then OpenDKIM, and generate the key pair:

yum install opendkim
mkdir -p /etc/opendkim/keys
opendkim-genkey -s default -d yourdomain.no -D /etc/opendkim/keys
chown opendkim:opendkim /etc/opendkim/keys/default.private
chmod 600 /etc/opendkim/keys/default.private
cat /etc/opendkim/keys/default.txt

The last command prints the TXT record. Publish it in DNS under the name default._domainkey.yourdomain.no. Then set /etc/opendkim.conf:

Mode    sv
Socket  inet:8891@localhost
Domain  yourdomain.no
KeyFile /etc/opendkim/keys/default.private
Selector default

Connect Postfix to the milter in main.cf:

smtpd_milters = inet:127.0.0.1:8891
non_smtpd_milters = $smtpd_milters
milter_default_action = accept

milter_default_action = accept keeps mail flowing if the milter is down, at the price of sending unsigned mail. Use tempfail if signing is mandatory.

8. Start and verify

postfix check
service opendkim start
chkconfig opendkim on
service postfix restart
tail -f /var/log/maillog

From a second terminal, check the key, the published record and a test message:

opendkim-testkey -d yourdomain.no -s default -k /etc/opendkim/keys/default.private -vvv
dig +short TXT default._domainkey.yourdomain.no
dig +short TXT yourdomain.no
echo "test body" | mail -s "delivery test" [email protected]

In the received message's original headers, look for SPF and DKIM results of pass. Also check that postconf -n shows reject_unauth_destination in the recipient restrictions. An external open-relay test should then be refused.

Review

The setup addresses the causes in order: matching forward and reverse DNS, a restricted SMTP policy that is not an open relay, encrypted authenticated submission, and SPF plus DKIM for the sending domain. Each step has a check, so a failure can be traced to its layer.

  • Limits. A clean configuration does not give a clean reputation. If the IP address was previously used for spam, delisting and warming up are separate tasks. Large receivers may still file low-volume mail from a new address as spam.
  • One domain, one key. The single-key Domain/KeyFile configuration suits one domain. For several domains, OpenDKIM's key and signing tables are the better choice.
  • No content filtering. There is no spam or virus scanning of inbound mail here. Adding it needs further components such as SpamAssassin or ClamAV.
  • Storage. Mail queues are write-heavy. Place /var/spool/postfix on storage with predictable latency, and watch the I/O wait during bursts.
  • Alternatives. For a small volume of transactional mail, a smarthost relay avoids reputation management. If Dovecot already serves mailboxes, use Dovecot SASL instead of saslauthd.