PostfixでTLSを有効にする

Googleの2024年2月1日からの新しいメールセキュリティポリシーに対応するため、PostfixTLSを有効にする。証明書はLet's Encryptで取得済み。

main.cf

/etc/postfix/main.cf に以下を記載。デフォルトで設定入っているようなので注意。

asmtpd_tls_security_level = may
smtpd_tls_cert_file = /etc/letsencrypt/live/mail.example.jp/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/mail.example.jp/privkey.pem
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtpd_tls_session_cache_timeout = 3600s
smtpd_tls_received_header = yes
smtpd_tls_loglevel = 1

master.cf

/etc/postfix/master.cf を編集。smtpsを有効化(コメント解除)する。

Postfix再起動

postfix checkで文法チェック実施

Postfixを再起動

# postfix check
# systemctl restart postfix
# systemctl status postfix
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2024-01-02 14:42:15 JST; 5s ago
  Process: 3671390 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS)
  Process: 3671421 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
  Process: 3671419 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
  Process: 3671411 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
  Process: 3671407 ExecStartPre=/usr/sbin/restorecon -R /var/spool/postfix/pid/master.pid (code=exited, status=255)
 Main PID: 3671489 (master)
    Tasks: 3 (limit: 203056)
   Memory: 8.6M
   CGroup: /system.slice/postfix.service
           tq3671489 /usr/libexec/postfix/master -w
           tq3671490 pickup -l -t unix -u
           mq3671491 qmgr -l -t unix -u

 Jan 02 14:42:14 mail.example.jp systemd[1]: Starting Postfix Mail Transport Agent...
 Jan 02 14:42:14 mail.example.jp restorecon[3671407]: /usr/sbin/restorecon: lstat(/var/spool/postfix/pid/master.pid) failed: No such file o>
 Jan 02 14:42:15 mail.example.jp postfix/master[3671489]: daemon started -- version x.y.z, configuration /etc/postfix
 Jan 02 14:42:15 mail.example.jp systemd[1]: Started Postfix Mail Transport Agent.
#

送受信テストを実施して、メール受信とログを確認しておく。

証明書更新時の処理

Let's Encryptで取得した証明書の有効期限は90日なので、定期的に証明書更新処理がお行われる。証明書が更新されたら、Postfix側で再読み込みする必要がある。

Let's Encryptの証明書更新処理は、certobtの更新処理(renew)がsystemdのタイマー機能に登録されている

# systemctl list-timers | grep cert
Tue 2024-01-02 16:50:00 JST  1h 47min left Tue 2024-01-02 03:07:02 JST  11h ago      snap.certbot.renew.timer     snap.certbot.renew.service
#

更新処理は毎日実施されているが、certbotのrenewは証明書の有効期限が30日を切るまでは更新処理を行わない。

certbotが証明書を更新した後にPostfixを再起動するため、

  • /etc/letsencrypt/renewal-hooks/deploy 以下にPostfixを再起動するスクリプトを用意する
  • 用意したスクリプトPostfixが再起動することを確認する
    • systemctl status postfix 、/var/log/maillogから確認しておく
  • /etc/letsencrypt/renewal-hooks/pre 以下にhttpdを停止するスクリプトを置いておく
    • mail.example.jp と www.example.jpの2つの証明書を更新するため、httpdが動いているとmail.example.jpの証明書更新に失敗する
  • certbot renew --force-renew で強制的に証明書を更新し、以下を確認する
    • 証明書が更新されること
    • Postfixが再起動すること
    • httpdが再起動されること

という手順を踏む。

deploy以下のスクリプトは更新する証明書ごと(今回だと2回)に実行されるので、環境変数RENEWED_DOMAINSなどを参照して、対象ドメインの場合のみ再起動するような処理を入れたほうがよい。

まずはdry run

# certbot renew --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/mail.example.jp.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Simulating renewal of an existing certificate for mail.example.jp

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/www.example.jp.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Simulating renewal of an existing certificate for www.example.jp

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded:
  /etc/letsencrypt/live/mail.example.jp/fullchain.pem (success)
  /etc/letsencrypt/live/www.example.jp/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[root@kasumi letsencrypt]#

続いて、certbot renew --force-renew を実行して確認する。

  • /etc/letsencrypt/live/ 以下の証明書が更新されている
  • Postfix, httpdが再起動されている

事を確認しておく。