Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
email:configure-dkim-spf-dmarc [2025/03/05 16:14] – created willy | email:configure-dkim-spf-dmarc [2025/03/13 14:05] (current) – [Configure proper mail delivery] willy | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Configure proper mail delivery ====== | + | ====== |
+ | You need access to your domain DNS records, this is mandatory. | ||
- | ===== SPF ===== | + | ===== SPF (Sender Policy Framework) |
- | [[https://pypi.org/project/spf-engine/|SPF Engine]] is a nice little Python package to check SPF validity | + | SPF works both //outbound// and //inbound// |
- | First of all install | + | === SPF Outbound === |
+ | |||
+ | This is the most difficult, but critical step. You need to add to your DNS a TXT record shaped like this: | ||
+ | < | ||
+ | mydomain.com. | ||
+ | </ | ||
+ | |||
+ | This record specify **who** is allowed to send email for the // | ||
+ | |||
+ | For the above example: | ||
+ | * **v=spf1**: the type (SPF) and version (1) of the record | ||
+ | * **a**: refer to // | ||
+ | * **mx**: refer to the mx record of the domain | ||
+ | * **ptr**: refer to // | ||
+ | * **-all**: anybody. **Always always always** put //-all// as the last part of the record. | ||
+ | |||
+ | In short, the above record allow **only** our mx record and main domain to send emails for // | ||
+ | |||
+ | This will be enough to protect your // | ||
+ | |||
+ | === SPF Inbound === | ||
+ | |||
+ | You have already installed // | ||
+ | |||
+ | There is nothing to configure! If you followed [[email: | ||
+ | |||
+ | |||
+ | ===== DKIM (Domain Keys Identified Mail) ===== | ||
+ | |||
+ | OpenDKIM provides great documentation [[http:// | ||
+ | |||
+ | You need to choose a // | ||
<code bash> | <code bash> | ||
- | emerge -vp mail-filter/spf-engine | + | cd /etc/ |
+ | opendkim-genkey | ||
</ | </ | ||
- | Then link it to Postfix | + | This will create two files: **mydomain.com.private**, |
+ | |||
+ | === DNS record === | ||
+ | |||
+ | As an example consider the following | ||
< | < | ||
- | smtpd_recipient_restrictions | + | mydomain.com._domainkey |
+ | " | ||
</ | </ | ||
- | While in **master.cf**> | + | You need to create a new TXT record |
+ | |||
+ | === Postfix setup === | ||
+ | |||
+ | OpenDKIM acts as a //milter//, which means a mail filter, for Postfix. The postfix configuration described [[email: | ||
+ | |||
+ | === Socket Setup === | ||
+ | |||
+ | For security reasons you want the DKIM keys not to be readable by Postfix, but you want Postfix capable to access the OpenDKIM socket or it would not be possible to actually sign any outbound email at all. The default Gentoo users setup is not ideal for this, as you either let Postfix access the keys by adding it to the // | ||
+ | |||
+ | The solution is to add a new group, called | ||
+ | <code bash> | ||
+ | groupadd dkimsocket | ||
+ | usermod --append --groups dkimsocket postfix | ||
+ | usermod --gid dkimsocket opendkim | ||
+ | usermod --append --groups opendkim opendkim | ||
+ | </ | ||
+ | |||
+ | === Final wrapup === | ||
+ | Let's wrap it all up with the following **/ | ||
+ | <file - opendkim.conf> | ||
+ | Syslog | ||
+ | SyslogSuccess | ||
+ | Canonicalization | ||
+ | SendReports | ||
+ | PidFile / | ||
+ | Socket local:/ | ||
+ | UMask 0117 | ||
+ | UserID opendkim: | ||
+ | AutoRestart | ||
+ | AutoRestartRate | ||
+ | Mode sv | ||
+ | # Use the following lines for a single domain/ | ||
+ | Domain | ||
+ | Selector | ||
+ | KeyFile | ||
+ | # Use the following lines for multiple domain/ | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | </ | ||
+ | |||
+ | If you want to use multiple domains and selectors, you need to create the table files and put the multiple references there. Check the official OpenDKIM documentation linked above. | ||
+ | |||
+ | === Start & Autostart OpenDKIM === | ||
+ | |||
+ | <code bash> | ||
+ | rc-update add opendkim default | ||
+ | / | ||
+ | </ | ||
+ | |||
+ | === Test your DKIM setup === | ||
+ | |||
+ | After your DNS record has propagated, you can test it with: | ||
+ | <code bash> | ||
+ | opendkim-testkey -d mydomain.com -s mydomain.com -k mydomain.com.private -vvv | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== DMARC (Domain-based Message Authentication, | ||
+ | |||
+ | OpenDMARC sample configutation can be found [[https:// | ||
+ | |||
+ | This is pretty easy to setup, just edit the **/ | ||
+ | <file - opendmarc.conf> | ||
+ | AuthservID mydomain.com | ||
+ | FailureReports true | ||
+ | RejectFailures false | ||
+ | SPFSelfValidate yes | ||
+ | Socket local:/ | ||
+ | SoftwareHeader true | ||
+ | Syslog true | ||
+ | SyslogFacility mail | ||
+ | TrustedAuthservIDs mail.mydomain.com | ||
+ | HistoryFile / | ||
+ | UMask 0002 | ||
+ | UserID opendmarc | ||
+ | PidFile / | ||
+ | </ | ||
+ | |||
+ | === DNS record === | ||
+ | |||
+ | A DMARC DNS record can be pretty simple or pretty complex. [[https:// | ||
+ | |||
+ | The following is a simple example that you can start from: | ||
< | < | ||
- | policy-spf | + | _dmarc IN TXT ( "v=DMARC1; p=reject; rua=mailto: |
- | user=nobody argv=/ | + | |
</ | </ | ||
+ | |||
+ | where: | ||
+ | * p: policy, you want reject here most probably | ||
+ | * rua: email address to sent aggregate reports to (optional) | ||
+ | * ruf: email address to sent failure reports to (optional) | ||
+ | |||
+ | === Postfix setup === | ||
+ | |||
+ | OpenDMARC acts as a //milter//, which means a mail filter, for Postfix. The postfix configuration described [[email: | ||
+ | |||
+ | === Start & Autostart OpenDMARC === | ||
+ | |||
+ | <code bash> | ||
+ | rc-update add opendmarc default | ||
+ | / | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ |