はじめに
DMARCでメールの検証を行うには、SPFとDKIM両方を設定しないといけない。 この記事では、DKIMの設定を取り上げる。DKIMの概要
SPF が配送もとの IP アドレス (つまりヘッダーの `Received: from helo-host.example.org (resolved-host.example.org [192.0.2.27])` の部分) を検証するのに対して、 DKIM では、メールを配送する際に、主要なヘッダー情報 (`Date`, `From`, `Reply-To`, `To`, `Subject` など) に対して署名を付加する。 公開鍵を DNS の TXT レコードに設定することになっており、受信側のメールサーバーで署名を検証する。SPF と同様に DNS レコードを使ってメールを検証するので、 DNS が改ざんされると第三者がなりすましメールを送ることができてしまう。また、メール本文の改ざんは DKIM では検知できない。
設定手順
このセクションでは、 EL9 (AlmaLinux 9 や Rocky Linux 9) 上の Postfix で DKIM 署名を付加するための設定を説明する。 EL8 でもほぼ同様の方法で設定できる。OpenDKIM のインストール
前もって CRB と EPEL をインストールしておき、以下のコマンドで OpenDKIM をインストールする。sudo dnf --enablerepo=epel,crb install opendkim opendkim-tools
キーの作成
cd $(mktemp -d) opendkim-genkey --directory ./ --domain example.com --selector 20240921 --bit 2048 --append-domain sudo cp 20240921.private /etc/opendkim/keys/ -i sudo chown opendkim:opendkim /etc/opendkim/keys/20240921.private
DNSレコードの設定
公開鍵が記載されたファイル20240921.txt
が生成されるので、これを参照してDNSレコードを設定する。
上記の例の場合、 20240921._domainkey.example.com.
の TXT レコードを設定する。
TXT以降はカッコでかこまれてダブルクオートがついているが、例えば Cloudflare の DNS 場合は、カッコ・ダブルクオート・改行を取って Content に登録する。
設定ファイルの編集
sudo cp /etc/opendkim.conf /etc/opendkim.conf-20240921 sudoedit /etc/opendkim.conf以下のように編集して保存する。
@@ -36,7 +36,7 @@ ## Selects operating modes. Valid modes are s (sign) and v (verify). Default is v. ## Must be changed to s (sign only) or sv (sign and verify) in order to sign outgoing ## messages. -Mode v +Mode sv ## Log activity to the system log. Syslog yes @@ -101,12 +101,12 @@ ## Gives the location of a file mapping key names to signing keys. In simple terms, ## this tells OpenDKIM where to find your keys. If present, overrides any KeyFile ## directive in the configuration file. Requires SigningTable be enabled. -# KeyTable /etc/opendkim/KeyTable +KeyTable /etc/opendkim/KeyTable ## Defines a table used to select one or more signatures to apply to a message based ## on the address found in the From: header field. In simple terms, this tells ## OpenDKIM how to use your keys. Requires KeyTable be enabled. -# SigningTable refile:/etc/opendkim/SigningTable +SigningTable refile:/etc/opendkim/SigningTable ## Identifies a set of "external" hosts that may send mail through the server as one ## of the signing domains without credentials as such.
sudoedit /etc/opendkim/KeyTable以下の1行を加える。
20240921._domainkey.example.com example.com:20240921:/etc/opendkim/keys/20240921.private
sudoedit /etc/opendkim/SigningTable以下の1行を加える。
*@example.com 20240921._domainkey.example.com
OpenDKIMを起動する
sudo systemctl enable --now opendkim.service sudo systemctl status opendkim.service
Postfixの設定
cd /etc/postfix/ sudo cp main.cf main.cf-20240921 sudoedit main.cf以下の3行を追加する。
smtpd_milters = local:/run/opendkim/opendkim.sock non_smtpd_milters = local:/run/opendkim/opendkim.sock milter_default_action = acceptもしローカルからしか配送しないなら、
smtpd_milters
は設定しなくてよさそう。
Postfixを再起動する
グループも設定したので、再起動する。sudo systemctl restart postfix sudo systemctl status postfix
動作確認
TXTレコードが設定されているかどうか確認する。dig txt 20240921._domainkey.example.com他のメールサーバーへ送信してみる。 Gmailなどへ送付すれば、「メッセージのソースを表示」からDKIMの検証状況を確認できる。
date -R | mutt -s 'DKIM key should be added' user@example.orgもし問題があれば、
/etc/postfix/main.cf
をもとに戻して確認しよう。
小言
SSLのように外部の機関に認証してもらう必要がなく、コストがかからないのがありがたい。参考
- CentOS Stream 8、CentOS Stream 9 で opendkim をインストールする手順(仮) - ほぼこの記事の通りの操作で設定できました。
- 技術解説 / DKIM (Domainkeys Identified Mail), 一般財団法人インターネット協会 - DKIMの概要が図示されていて分かりやすい。