opendkim備忘録


参考

https://qiita.com/bezeklik/items/a3619b5abc01ab38bec4

https://minkymoon.jp/2021/02/14/centos8%E3%81%ABopendkim%E3%82%92%E5%B0%8E%E5%85%A5%E3%81%97%E3%81%A6%E3%81%BF%E3%81%9F/

環境

  • サーバー
    • RockyLinux(RHEL8)
  • DNS サーバー
    • Route53

準備

opendkim インストール

dnf install epel-release
dnf install opendkim
dnf install opendkim-tools

鍵作成

opendkim-genkey -D [出力先ディレクトリ] -b [鍵の長さ] -d [ドメイン名] -s [セレクタ名]

// 鍵を保存するディレクトリを作成
mkdir /etc/opendkim/keys/example.com

// 鍵作成
opendkim-genkey -D /etc/opendkim/keys/example.com/ -b 2048 -d example.com -s 20220823

// 以下2つのファイルが作成されます
// 20220823.private
// 20220823.txt
// xxx.privateが秘密鍵、xxx.txtが公開鍵(DNSのDKIMレコード)となる


chown -R opendkim:opendkim /etc/opendkim/keys/*

DNS レコード登録(Route53)

先ほど作成した 20220823.txt の中身を貼る

DKIMレコードの登録
Name: 20170309._domainkey.example.com
Type: TXT - Text
Alias: No
TTL: 任意
Value: "v=DKIM1; k=rsa; p=xxxxpubkeyxxxxxx..."
Routing Policy: Simple
Save Record Setをクリック

ADSPレコードの登録(電子署名失敗した場合も送信するため)
Name: _adsp._domainkey.example.com
Type: TXT - Text
Alias: No
TTL: 任意
Value: "dkim=unknown"
Routing Policy: Simple
Save Record Setをクリック

自端末で DNS レコードを確認。Windows の場合はコマンドプロンプトを開いて下記実行

nslookup -type=TXT 20220823._domainkey.example.com
nslookup -type=TXT _adsp._domainkey.example.com

各種設定ファイル

123
opendkim.conf ファイル/etc/opendkim.confopendkim の設定ファイル
SigningTable ファイル/etc/opendkim/SigningTableどの DKIM レコードに対応する秘密鍵で署名するか決定するテーブル※1
KeyTable ファイル/etc/opendkim/KeyTableDKIM レコードと秘密鍵の関連付け※1
TrustedHosts ファイル/etc/opendkim/TrustedHostsopendkim にキーを使用するユーザーを指定します

※1.同一サーバー上に複数の仮想ドメインを提供するメールサーバーサービスを動作させている場合に使用する 参考:https://wiki.archlinux.jp/index.php/OpenDKIM 参考2:https://qiita.com/bezeklik/items/a3619b5abc01ab38bec4#keyfile-%E3%83%87%E3%82%A3%E3%83%AC%E3%82%AF%E3%83%86%E3%82%A3%E3%83%96

opendkim ファイル編集

mkdir /etc/opendkim.conf /etc/opendkim.conf_bk
vi /etc/opendkim.conf
  • このファイルのみ編集でもいいが、※1のように複数のドメインを運用させる場合は、その他のファイルを編集する必要がある
  • 今回は単一ドメインだが、各種ファイルに設定を書く
# diff -u /etc/opendkim.conf /etc/opendkim.conf_default
--- /etc/opendkim.conf  2022-08-23 09:10:48.066492557 +0000
+++ /etc/opendkim.conf_default  2022-02-24 05:14:07.000000000 +0000
@@ -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   sv
+Mode   v

 ##  Log activity to the system log.
 Syslog yes
@@ -75,7 +75,7 @@

 ##  Add a DKIM-Filter header field to messages passing through this filter
 ##  to identify messages it has processed.
-SoftwareHeader no
+SoftwareHeader yes

 ## SIGNING OPTIONS

@@ -95,24 +95,24 @@

 ##  Gives the location of a private key to be used for signing ALL messages. This
 ##  directive is ignored if KeyTable is enabled.
-#KeyFile       /etc/opendkim/keys/default.private
+KeyFile        /etc/opendkim/keys/default.private

 ##  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.
-ExternalIgnoreList     refile:/etc/opendkim/TrustedHosts
+# ExternalIgnoreList   refile:/etc/opendkim/TrustedHosts

 ##  Identifies a set "internal" hosts whose mail should be signed rather than verified.
-InternalHosts  refile:/etc/opendkim/TrustedHosts
+# InternalHosts        refile:/etc/opendkim/TrustedHosts

 ##  Contains a list of IP addresses, CIDR blocks, hostnames or domain names
 ##  whose mail should be neither signed nor verified by this filter.  See man
  • /etc/opendkim 配下の各種ファイルにアクセスできるように設定している

KeyTable、SigningTable の更新

参考:https://qiita.com/taku1201/items/3b2dffd950340b9726d0#3-4-opendkimconf%E3%81%AE%E8%A8%AD%E5%AE%9A

KeyTable

 vi /etc/opendkim/KeyTable

以下の書式に従い最終行に追加する

  • DKIM レコード名 ドメイン名:セレクタ名:秘密鍵ファイル

▼ 記述例

20170309._domainkey.example.com example.com:20170309:/etc/opendkim/keys/20130709.private

SigningTable

vi /etc/opendkim/SigningTable

以下の書式に従い最終行に追加する

  • 対象のメールアカウント DKIM レコード名

▼ 記述例

# アカウントを指定する書き方
hoge@examle.com 20170309._domainkey.example.com

# ワイルドカードを利用する書き方
*@example.com 20170309._domainkey.example.com

# ワイルドカードを利用しない書き方
@example.com 20170309._domainkey.example.com

Postfix 設定

vi /etc/postfix/main.cf

下記を最終行に追加

milter_default_action = accept
non_smtpd_milters = $smtpd_milters
smtpd_milters = inet:127.0.0.1:8891
123
milter_default_actionaccept:メールフィルタが存在しないかのように進めます
reject: このセッションのそれ以降のすべてのコマンドを永久状態コードで拒否します。
tempfail:このセッションのそれ以降のすべてのコマンドを一時状態コードで拒否します。
Milter (メールフィルタ) アプリケーションが使えなかったり設定が間違えているときのデフォルトの動作
non_smtpd_miltersローカル IP:ポートPostfix smtpd(8) サーバーを通さずに届いた新しいメールに対する Milter (メールフィルタ) アプリケーションのリスト
smtpd_miltersローカル IP:ポートPostfix smtpd(8) サーバーを通して届いた新しいメールに対する Milter (メールフィルタ) アプリケーションのリスト