Server

18. メールサーバーの暗号化通信対応

 この章では、Oracle Linux 8で、メールサーバーの暗号化通信対応の設定について記述しています。

 最初にやる事は、電子証明書の取得です。前の章でのApacheの暗号化通信対応時と異なり、無料の自己署名証明書でも特に問題なく使う事ができました。

 しかし、値段が安いですし、いざという時のサポートも受けられますので、前の章と同じく、PositiveSSLで私は購入しました。

 購入時の注意点は、前の章で述べたのとほぼ同じですが、FQDN(完全な長さのドメイン名)が、mail.example.netになることだけ注意です。

 リネームするファイル名も前の章と同じにします。以下にもう一度表を掲載しておきます。


種類 ファイル名
秘密鍵 privkey.pem
サーバー証明書 cert.pem
中間証明書 chain.pem
サーバー証明書+中間証明書 fullchain.pem

 これらのファイルを、mail.example.netフォルダに保存しておいてください。

 このmail.example.netフォルダを、WinSCPやRLoginを使ってoluserのホームディレクトリに転送します。以下、ホームディレクトリでの作業になります。

 まず、PostfixとDovecotの電子証明書の置き場所を作ります。できれば一つのディレクトリで管理したかったのですが、私の知識不足でPostfix/Dovecotの両方から問題なく見えるセキュリティコンテキストの設定が分からなかったため、このようになっています。すいません。

sudo mkdir /etc/postfix/keys
sudo mkdir /etc/dovecot/keys

 パーミッション(許可属性)と、セキュリティコンテキストを調整します。

sudo chmod 700 /etc/postfix/keys
sudo chmod 700 /etc/dovecot/keys
sudo chcon --reference=/etc/postfix/ /etc/postfix/keys/
sudo chcon --reference=/etc/dovecot/ /etc/dovecot/keys/

 電子証明書のディレクトリをコピーします。

sudo cp -r mail.example.net /etc/postfix/keys/
sudo cp -r mail.example.net /etc/dovecot/keys/

 パーミッション(許可属性)と、セキュリティコンテキストを調整します。

sudo chmod 700 /etc/postfix/keys/mail.example.net
sudo chmod 700 /etc/dovecot/keys/mail.example.net
sudo chmod 400 /etc/postfix/keys/mail.example.net/*
sudo chmod 400 /etc/dovecot/keys/mail.example.net/*
sudo chcon -R --reference=/etc/postfix/ /etc/postfix/keys/mail.example.net/
sudo chcon -R --reference=/etc/dovecot/ /etc/dovecot/keys/mail.example.net/

 続けて、Postfixの設定に移ります。/etc/postfix/main.cfを編集します。

sudo vim /etc/postfix/main.cf

 715行目あたりを以下のように変更します。

 変更前

# The full pathname of a file with the Postfix SMTP server RSA certificate
# in PEM format. Intermediate certificates should be included in general,
# the server certificate first, then the issuing CA(s) (bottom-up order).
#
smtpd_tls_cert_file = /etc/pki/tls/certs/postfix.pem

 変更後

# The full pathname of a file with the Postfix SMTP server RSA certificate
# in PEM format. Intermediate certificates should be included in general,
# the server certificate first, then the issuing CA(s) (bottom-up order).
#
#smtpd_tls_cert_file = /etc/pki/tls/certs/postfix.pem
smtpd_tls_cert_file = /etc/postfix/keys/mail.example.net/fullchain.pem

 720行目あたりを以下のように変更します。

 変更前

# The full pathname of a file with the Postfix SMTP server RSA private key
# in PEM format. The private key must be accessible without a pass-phrase,
# i.e. it must not be encrypted.
#
smtpd_tls_key_file = /etc/pki/tls/private/postfix.key

 変更後

# The full pathname of a file with the Postfix SMTP server RSA private key
# in PEM format. The private key must be accessible without a pass-phrase,
# i.e. it must not be encrypted.
#
#smtpd_tls_key_file = /etc/pki/tls/private/postfix.key
smtpd_tls_key_file = /etc/postfix/keys/mail.example.net/privkey.pem

 725行目あたりが以下のように、may(強制しない)になっているか確認。

# Announce STARTTLS support to remote SMTP clients, but do not require that
# clients use TLS encryption (opportunistic TLS inbound).
#
smtpd_tls_security_level = may

 730行目あたりの、サーバー証明書の設定を変更します。

 変更前

# Directory with PEM format Certification Authority certificates that the
# Postfix SMTP client uses to verify a remote SMTP server certificate.
#
smtp_tls_CApath = /etc/pki/tls/certs

# The full pathname of a file containing CA certificates of root CAs
# trusted to sign either remote SMTP server certificates or intermediate CA
# certificates.
#
smtp_tls_CAfile = /etc/pki/tls/certs/ca-bundle.crt

 変更後

# Directory with PEM format Certification Authority certificates that the
# Postfix SMTP client uses to verify a remote SMTP server certificate.
#
#smtp_tls_CApath = /etc/pki/tls/certs
smtp_tls_CApath = /etc/postfix/keys/mail.example.net

# The full pathname of a file containing CA certificates of root CAs
# trusted to sign either remote SMTP server certificates or intermediate CA
# certificates.
#
#smtp_tls_CAfile = /etc/pki/tls/certs/ca-bundle.crt
smtp_tls_CAfile = /etc/postfix/keys/mail.example.net/cert.pem

 最終行に以下を追加します。

### SSL/TLS Settings
smtpd_tls_session_cache_database = btree:/var/lib/postfix/smtpd_scache
smtpd_tls_session_cache_timeout = 3600s
smtpd_tls_received_header = yes
smtpd_tls_loglevel = 1

 上からそれぞれ、SSL/TLSキャッシュデータベースの指定、SSL/TLS通信のキャッシュ保持期間、暗号化情報をヘッダと言われる部分に追加、SSL/TLS通信時のログ出力レベルになります。

 保存してvimを終了してください。


 次に、/etc/postfix/master.cfの編集に移ります。

sudo vim /etc/postfix/master.cf

 30行目あたりを以下のように変更。

 変更前

#smtps     inet  n       -       n       -       -       smtpd
#  -o syslog_name=postfix/smtps
#  -o smtpd_tls_wrappermode=yes
#  -o smtpd_sasl_auth_enable=yes
#  -o smtpd_reject_unlisted_recipient=no
#  -o smtpd_client_restrictions=$mua_client_restrictions
#  -o smtpd_helo_restrictions=$mua_helo_restrictions
#  -o smtpd_sender_restrictions=$mua_sender_restrictions
#  -o smtpd_recipient_restrictions=
#  -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
#  -o milter_macro_daemon_name=ORIGINATING

 変更後

smtps     inet  n       -       n       -       -       smtpd
#  -o syslog_name=postfix/smtps
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes
#  -o smtpd_reject_unlisted_recipient=no
#  -o smtpd_client_restrictions=$mua_client_restrictions
#  -o smtpd_helo_restrictions=$mua_helo_restrictions
#  -o smtpd_sender_restrictions=$mua_sender_restrictions
#  -o smtpd_recipient_restrictions=
#  -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
#  -o milter_macro_daemon_name=ORIGINATING

 上の3つの変更は、コメントを外すだけです。master.cfでの変更は、それぞれのデーモンと呼ばれるプログラムにおいて、main.cfの設定を上書きするものです。


 続いてDovecotの設定に移ります。/etc/dovecot/conf.d/10-ssl.conを編集します。

sudo vim /etc/dovecot/conf.d/10-ssl.conf

 5行目辺りを以下のように変更します。

 変更前

# SSL/TLS support: yes, no, required. <doc/wiki/SSL.txt>
# disable plain pop3 and imap, allowed are only pop3+TLS, pop3s, imap+TLS and imaps
# plain imap and pop3 are still allowed for local connections
#ssl = required
ssl = no

 変更後

# SSL/TLS support: yes, no, required. <doc/wiki/SSL.txt>
# disable plain pop3 and imap, allowed are only pop3+TLS, pop3s, imap+TLS and imaps
# plain imap and pop3 are still allowed for local connections
ssl = required
#ssl = no

 15行目辺りを以下のように変更します。

 変更前

# PEM encoded X.509 SSL/TLS certificate and private key. They're opened before
# dropping root privileges, so keep the key file unreadable by anyone but
# root. Included doc/mkcert.sh can be used to easily generate self-signed
# certificate, just make sure to update the domains in dovecot-openssl.cnf
ssl_cert = </etc/pki/dovecot/certs/dovecot.pem
ssl_key = </etc/pki/dovecot/private/dovecot.pem

 変更後

# PEM encoded X.509 SSL/TLS certificate and private key. They're opened before
# dropping root privileges, so keep the key file unreadable by anyone but
# root. Included doc/mkcert.sh can be used to easily generate self-signed
# certificate, just make sure to update the domains in dovecot-openssl.cnf
#ssl_cert = </etc/pki/dovecot/certs/dovecot.pem
#ssl_key = </etc/pki/dovecot/private/dovecot.pem
ssl_cert = </etc/dovecot/keys/mail.example.net/fullchain.pem
ssl_key = </etc/dovecot/keys/mail.example.net/privkey.pem

 なお、パラメーターで書いている<は、削除しないようにしてください。


 続いて/etc/dovecot/conf.d/10-master.confを編集します。

sudo vim /etc/dovecot/conf.d/10-master.conf

 40行目辺りを以下のように変更します。

 変更前

service pop3-login {
  inet_listener pop3 {
    #port = 110
  }
  inet_listener pop3s {
    #port = 995
    #ssl = yes
  }
}

 変更後

service pop3-login {
  inet_listener pop3 {
    #port = 110
    port = 0
  }
  inet_listener pop3s {
    port = 995
    ssl = yes
  }
}

 上のport=0で、平文でのPOP3通信を拒否します。


 続いて/etc/dovecot/conf.d/10-auth.confを編集します。

sudo vim /etc/dovecot/conf.d/10-auth.conf

 10行目辺りを以下のように変更します。

 変更前

#disable_plaintext_auth = yes
disable_plaintext_auth = no

 変更後

disable_plaintext_auth = yes
#disable_plaintext_auth = no

 100行目辺りを以下のように変更します。

 変更前

# Space separated list of wanted authentication mechanisms:
#   plain login digest-md5 cram-md5 ntlm rpa apop anonymous gssapi otp skey
#   gss-spnego
# NOTE: See also disable_plaintext_auth setting.
auth_mechanisms = plain

 変更後

# Space separated list of wanted authentication mechanisms:
#   plain login digest-md5 cram-md5 ntlm rpa apop anonymous gssapi otp skey
#   gss-spnego
# NOTE: See also disable_plaintext_auth setting.
#auth_mechanisms = plain

 ファイアーウォールにSMTPS,POP3Sを追加し、POP3を削除します。

sudo firewall-cmd --add-service=pop3s --permanent
sudo firewall-cmd --add-service=smtps --permanent
sudo firewall-cmd --remove-service=pop3 --permanent
sudo firewall-cmd --reload

 Postfix/Dovecotを再起動します。

sudo systemctl restart dovecot
sudo systemctl restart postfix

 エラーが出てないかステータスを確認します。

sudo systemctl status dovecot
sudo systemctl status postfix

 ルーターの設定を変更し、SMTPS(TCP:465番)と、POP3S(TCP:995番)を、このサーバー(192.168.0.2)へ転送するように設定します。この時の画面等は、前の章を参照してください。


 ここでいったん、動作確認をします。Mozilla Thunderbirdのアカウントを、以下のように変更します。この時の画面は、前の章等を見てください。


受信サーバー 送信サーバー
プロトコル POP3 プロトコル SMTP
サーバー mail.example.net サーバー mail.example.net
ポート番号 995 ポート番号 465
SSL SSL/TLS SSL SSL/TLS
認証方式 通常のパスワード認証 認証方式 通常のパスワード認証
ユーザー名 oluser ユーザー名 oluser

 ここでの注意点は前と同じです。新規アカウントとして登録する場合は、ユーザー名がデフォルトでoluser@example.netとなっていますので、@以下の部分を削除するのを忘れないようにしてください。


 動作確認ができたら、ここからは、Dovecotのポート番号の変更の設定を行います。

 どこのメールサーバーから受信するかわからないPostfixの方は、ポート番号を変更することができませんが、自宅サーバーのメールを取り出す人は少ないでしょうから、Dovecotのポート番号の変更は可能です。攻撃に合う回数を少しでも減らすため、変更できるポート番号は変更してしまいましょう。

 ここでは、POP3S通信の待ち受けポートを、10037番とすると仮定して設定します。この部分は、各自で調整してください。


 まず、現在のSELinuxの設定を確認します。なお、semanageコマンドを使うには、別途パッケージのインストールが必要です。前の章を確認してください。

sudo semanage port --list | grep pop

 以下のように表示されると思います。

pop_port_t                     tcp      106, 109, 110, 143, 220, 993, 995, 1109, 10993

 SELinuxのPOPポートに、10037番を追加します。

sudo semanage port --add --type pop_port_t --proto tcp 10037

 追加されているかどうか確認します。

sudo semanage port --list | grep pop

 以下のように表示されれば、成功です。

pop_port_t                     tcp      10037, 106, 109, 110, 143, 220, 993, 995, 1109, 10993

 続けて/etc/dovecot/conf.d/10-master.confファイルを編集します。

sudo vim /etc/dovecot/conf.d/10-master.conf

 40行目辺りを以下のように変更します。

 変更前

service pop3-login {
  inet_listener pop3 {
    #port = 110
    port = 0
  }
  inet_listener pop3s {
    port = 995
    ssl = yes
  }
}

 変更後

service pop3-login {
  inet_listener pop3 {
    #port = 110
    port = 0
  }
  inet_listener pop3s {
    #port = 995
    port = 10037
    ssl = yes
  }
}

 ファイアーウォールの設定を変更します。設定ファイルをコピーします。

sudo cp /usr/lib/firewalld/services/pop3s.xml /etc/firewalld/services/pop3s-10037.xml

 コピーした設定ファイルを編集します。

sudo vim /etc/firewalld/services/pop3s-10037.xml

 5行目辺りを以下のように変更します。

 変更前

  <port protocol="tcp" port="995"/>

 変更後

  <port protocol="tcp" port="10037"/>

 ファイアーウォールの設定を変更します。

sudo firewall-cmd --add-service=pop3s-10037 --permanent
sudo firewall-cmd --remove-service=pop3s --permanent
sudo firewall-cmd --reload

 Dovecotを再起動します。

sudo systemctl restart dovecot

 ルーターの設定で、TCP:995番を削除し、TCP:10037番を追加します。また、動作確認時、Mozilla ThunderbirdのPOP3サーバーの設定のポート番号を10037番にすることを忘れないようにしましょう。