そういうのがいいブログ

SIerで働く30代サラリーマンSEがインフラエンジニアに憧れてLinux・クラウド・AWSの勉強をするブログ

AlmaLinux9.6+Postfix3.5+Dovecot2.3とサーバー証明書でSSLを利用した安全なメールサーバーを構築

※[PR]当ブログの記事の中にはプロモーションが含まれています。

・AlmaLinux9.6+Postfix3.5+Dovecot2.3とサーバー証明書SSLを利用した安全なメールサーバーを構築する方法を知りたい。
・具体的な手順を分かりやすく教えてほしい。

こういった疑問に答えます。

本記事の内容

  1. AlmaLinux9.6+Postfix3.5+Dovecot2.3とサーバー証明書SSLを利用した安全なメールサーバーを構築する手順

サトナカ (@souiunogaii)

この記事を書いている私は、某SIerに勤務しながら、
WEB系エンジニア・インフラエンジニアに憧れて、プログラミングおよびインフラ技術の勉強をしています。

こういった私が、解説していきます。

私が実機で試したコマンドや画像を載せて書いています。
記事の信頼性担保に繋がると思います。

サーバー証明書SSLを利用した安全なメールサーバーを構築

前回の記事

souiunogaii.hatenablog.com

サーバー証明書秘密鍵ファイルを配置

/etc/pki/tls/certs/blue-planet.crt
/etc/pki/tls/certs/blue-planet.key

Postfixのmain.cfの編集

/etc/postfix/main.cf
    708 # The full pathname of a file with the Postfix SMTP server RSA certificate
    709 # in PEM format. Intermediate certificates should be included in general,
    710 # the server certificate first, then the issuing CA(s) (bottom-up order).
    711 #
    712 #smtpd_tls_cert_file = /etc/pki/tls/certs/postfix.pem  ←★★★コメントで無効化
    713
    714 # The full pathname of a file with the Postfix SMTP server RSA private key
    715 # in PEM format. The private key must be accessible without a pass-phrase,
    716 # i.e. it must not be encrypted.
    717 #
    718 #smtpd_tls_key_file = /etc/pki/tls/private/postfix.key  ←★★★コメントで無効化
    720 # Announce STARTTLS support to remote SMTP clients, but do not require that
    721 # clients use TLS encryption (opportunistic TLS inbound).
    722 #
    723 #smtpd_tls_security_level = may  ←★★★コメントで無効化

  (中略)

    752 ### SSL/TLS Settings  ←★★★追加
    753 smtp_tls_security_level = may  ←★★★追加
    754 smtpd_tls_cert_file = /etc/pki/tls/certs/blue-planet.crt  ←★★★追加
    755 smtpd_tls_key_file = /etc/pki/tls/certs/blue-planet.key  ←★★★追加
    756 smtpd_tls_session_cache_database = btree:/var/lib/postfix/smtpd_scache  ←★★★追加
    757 smtpd_tls_session_cache_timeout = 3600s  ←★★★追加
    758 smtpd_tls_received_header = yes  ←★★★追加
    759 smtpd_tls_loglevel = 1  ←★★★追加
    760

Postfixのmaster.cfの編集

/etc/postfix/master.cf
      8 # ==========================================================================
      9 # service type  private unpriv  chroot  wakeup  maxproc command + args
     10 #               (yes)   (yes)   (no)    (never) (100)
     11 # ==========================================================================
     12 smtp      inet  n       -       n       -       -       smtpd
     13 #smtp      inet  n       -       n       -       1       postscreen
     14 #smtpd     pass  -       -       n       -       -       smtpd
     15 #dnsblog   unix  -       -       n       -       0       dnsblog
     16 #tlsproxy  unix  -       -       n       -       0       tlsproxy
     17 submission inet n       -       n       -       -       smtpd
     18 #  -o syslog_name=postfix/submission
     19 #  -o smtpd_tls_security_level=encrypt
     20 #  -o smtpd_sasl_auth_enable=yes
     21 #  -o smtpd_tls_auth_only=yes
     22 #  -o smtpd_reject_unlisted_recipient=no
     23 #  -o smtpd_client_restrictions=$mua_client_restrictions
     24 #  -o smtpd_helo_restrictions=$mua_helo_restrictions
     25 #  -o smtpd_sender_restrictions=$mua_sender_restrictions
     26 #  -o smtpd_recipient_restrictions=
     27 #  -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
     28 #  -o milter_macro_daemon_name=ORIGINATING
     29 smtps     inet  n       -       n       -       -       smtpd  ←★★★コメント解除
     30 #  -o syslog_name=postfix/smtps
     31   -o smtpd_tls_wrappermode=yes  ←★★★コメント解除
     32   -o smtpd_sasl_auth_enable=yes  ←★★★コメント解除
     33 #  -o smtpd_reject_unlisted_recipient=no
     34 #  -o smtpd_client_restrictions=$mua_client_restrictions
     35 #  -o smtpd_helo_restrictions=$mua_helo_restrictions
     36 #  -o smtpd_sender_restrictions=$mua_sender_restrictions
     37 #  -o smtpd_recipient_restrictions=
     38   -o smtpd_relay_restrictions=permit_sasl_authenticated,reject  ←★★★コメント解除
     39 #  -o milter_macro_daemon_name=ORIGINATING

Postfixを再起動して変更を反映

[root@a-email01 ~]# postconf -n
[root@a-email01 ~]# postfix check
[root@a-email01 ~]# systemctl restart postfix

SMTPS(465番)ポートがLISTENであることを確認

[root@a-email01 ~]# netstat -nat
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:587             0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:199           0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:110             0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:143             0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:465             0.0.0.0:*               LISTEN
tcp        0      0 192.168.100.121:143     192.168.100.101:64289   ESTABLISHED
tcp        0      0 192.168.100.121:22      192.168.100.101:64270   ESTABLISHED
tcp        0      0 192.168.100.121:143     192.168.100.101:64290   ESTABLISHED
tcp        0    268 192.168.100.121:22      192.168.100.101:64320   ESTABLISHED
tcp6       0      0 :::22                   :::*                    LISTEN
tcp6       0      0 :::110                  :::*                    LISTEN
tcp6       0      0 :::143                  :::*                    LISTEN

Dovecotの10-ssl.confの編集

/etc/dovecot/conf.d/10-ssl.conf
      2 ## SSL settings
      3 ##
      4
      5 # SSL/TLS support: yes, no, required. <doc/wiki/SSL.txt>
      6 # disable plain pop3 and imap, allowed are only pop3+TLS, pop3s, imap+TLS and imaps
      7 # plain imap and pop3 are still allowed for local connections
      8 ssl = required  ←★★★変更

     10 # PEM encoded X.509 SSL/TLS certificate and private key. They're opened before
     11 # dropping root privileges, so keep the key file unreadable by anyone but
     12 # root. Included doc/mkcert.sh can be used to easily generate self-signed
     13 # certificate, just make sure to update the domains in dovecot-openssl.cnf
     14 ssl_cert = </etc/pki/tls/certs/blue-planet.crt  ←★★★変更
     15 ssl_key = </etc/pki/tls/certs/blue-planet.key  ←★★★変更

Dovecotの10-master.confの編集

/etc/dovecot/conf.d/10-master.conf
     17 service imap-login {
     18   inet_listener imap {
     19     port = 0  ←★★★変更
     20   }
     21   inet_listener imaps {
     22     port = 993 ←★★★コメント解除
     23     ssl = yes ←★★★コメント解除
     24   }

  (中略)

     38 service pop3-login {
     39   inet_listener pop3 {
     40     port = 0  ←★★★変更
     41   }
     42   inet_listener pop3s {
     43     port = 995 ←★★★コメント解除
     44     ssl = yes ←★★★コメント解除
     45   }
     46 }

Dovecotを再起動して変更を反映

[root@a-email01 ~]# doveconf -n
[root@a-email01 ~]# systemctl restart dovecot

POP3S(995番)、IMAPS(993番)ポートがLISTENであることを確認

[root@a-email01 ~]# netstat -nat
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:587             0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:995             0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:993             0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:199           0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:465             0.0.0.0:*               LISTEN
tcp        0      0 192.168.100.121:143     192.168.100.101:64289   FIN_WAIT2
tcp        0      0 192.168.100.121:22      192.168.100.101:64270   ESTABLISHED
tcp        0      0 192.168.100.121:143     192.168.100.101:64290   TIME_WAIT
tcp        0      0 192.168.100.121:22      192.168.100.101:64320   ESTABLISHED
tcp6       0      0 :::995                  :::*                    LISTEN
tcp6       0      0 :::993                  :::*                    LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN

firewalldに許可サービスを追加

smtps,imaps,pop3s
[root@a-email01 ~]# firewall-cmd --zone=home --add-service={smtps,imaps,pop3s} --permanent
success
[root@a-email01 ~]# firewall-cmd --reload
success

メールソフトで接続確認

アカウントの設定がみつからないエラーになりますが,
「詳細設定」ボタンを押します。

「OK」を押します。

「セキュリティ例外の追加」画面が表示されたら「セキュリティ例外を承認」を押します。

ヘルプ>トラブルシューティング情報

プロファイルフォルダーを開く

cert_override.txt を開く

995 で追加された行をコピーして,993465 の行を作成。

# PSM Certificate Override Settings file      
# This is a generated file!  Do not edit.      
a-email01.blue-planet.internal:995: OID.2.16.840.1.101.3.4.2.1  61:7A:E8:83:1(省略)48:11:95
a-email01.blue-planet.internal:993: OID.2.16.840.1.101.3.4.2.1  61:7A:E8:83:1(省略)48:11:95
a-email01.blue-planet.internal:465: OID.2.16.840.1.101.3.4.2.1  61:7A:E8:83:1(省略)48:11:95

一度メールソフトを再起動すると,アカウントが設定できるようになる。