メールサーバーもSSL/TLS化

Linux (CentOS、RockyLinux)

メールの送受信も無事に動作し、Let’s Encrypt で証明書も取れたので、メールサーバーもSSL/TLSの設定にしようと思います

いつものように、こちらを参考にさせていただきました

https://www.rem-system.com/mail-postfix03/

メールサーバ用の証明書発行

ホスト名でIPアドレスを解決できるかの確認

https化の時にも書きましたが、ホスト名でのアクセスが必要です
「host」コマンドで、ホスト名でIPアドレスが表示されるか確認してみます
IPアドレスが表示されればOK!

$ host mail.hogehoge.com
 mail.hogehoge.com has address 12.123.45.6

ファイヤウォールの確認

「firewall-cmd –list-all」コマンドで現状の設定を確認します
http と https が許可されていない場合には、許可が必要です

$ sudo firewall-cmd --list-all
 public (active)
   target: default
   icmp-block-inversion: no
   interfaces: wlp8s0
   sources:
   services: ssh dhcpv6-client http https smtp smtp-submission pop3 imap
   ports:
   protocols:
   masquerade: no
   forward-ports:
   source-ports:
   icmp-blocks:
   rich rules:


    //http と https が許可されていない場合には追加する
 $ sudo firewall-cmd --add-service={http,https} --permanent
 $ sudo firewall-cmd --reload

メールサーバー証明書を作成

前回までにすでに、certbot を導入し、WebサーバーについてはSSL化は終了しています。

この時には、「webroot」プラグインを使用しましたが、今回は参考サイトによると「standalone」を使用しているので、同様にやってみようと思います

# certbot certonly --standalone -d mail.kazuban.com

ところが、
「Problem binding to port 80: Could not bind to IPv4 or IPv6.」
のエラーが出て、うまく進んでくれません
これは、standalone と webroot プラグインの動作環境が影響してしているようです。
「standalone」では、ポート80番(http)を使用するので、このサービスを止める必要があるようです。
「systemctl stop http」でhttpサービスを停止して、再度実行してみます

# systemctl stop httpd
# certbot certonly --standalone -d mail.kazuban.com

今度は無事に、「Congratulations! 」が出てくれました

証明書の自動更新設定

Let’s Encrypt の証明書の有効期限は三ヶ月なので、「cron」を使い定期的自動的に発行してもらうように設定したほうが便利です。
参考サイトを参考に、設定しようと思います。
「renew」は一度取得した証明書を自動更新してくれるコマンドです。
「–deploy-hook」は証明書の発行が終了した後に、実行するシェルコマンドを指定するオプションです。
「crontab -e」コマンドで、「cron」の自動実行設定ファイルを編集できます
「crontab -l」で確認ができます
今回は、毎日3:00に実行するように設定しようと思います
ただし、root ユーザーで実行する必要があるので、「 crontab -e 」の前にrootになる必要がります

# su
パスワード:

# crontab -e
 00 3 * * * certbot renew -q --deploy-hook "systemctl restart httpd;systemctl restart postfix dovecot"


# crontab -l
 00 3 * * * certbot renew -q --deploy-hook "systemctl restart httpd;systemctl restart postfix dovecot"

自動更新でエラー発生

自動更新は、有効期限の30日前になると、実際の発行手続きを行うようです。
ところが、この時にエラー発生のメールが届きました。
良く考えると、webの場合には「webroot」を使い、メールの場合には「standalone」を使って証明書を発行した事が原因のようです。
「renew」コマンドは、一度発行した時のオプションを引き継ぐようなので、違うコマンドの場合には矛盾が出てきます
具体的には、webroot」は、webサーバー(80番ポート)が動作している必要がありますが、 standalone 」は停止していなくてはいけません。
したがって、どちらかの環境に合わせて、再度手動実行する事で更新することができました。
さらに調べてみると、「/etc/letsencrypt/renewal」ディレクトリの中に、「renew」用の設定ファイルが作成されていました。
このファイルを「webroot」もしくは「standalone」に統一することで自動更新時のエラーが回避できるかもしれません

 # renew_before_expiry = 30 days
 version = 0.36.0
 archive_dir = /etc/letsencrypt/archive/www.hogehoge.com
 cert = /etc/letsencrypt/live/www.hogehoge.com/cert.pem
 privkey = /etc/letsencrypt/live/www.hogehoge.com/privkey.pem
 chain = /etc/letsencrypt/live/www.hogehoge.com/chain.pem
 fullchain = /etc/letsencrypt/live/www.hogehoge.com/fullchain.pem
 Options used in the renewal process
 [renewalparams]
 authenticator = webroot   <----------- プラグイン
 account = aaa_______________cg
 webroot_path = /home/hogehoge/html,
 server = https://acme-v02.api.letsencrypt.org/directory
 renew_hook = systemctl restart httpd;systemctl restart postfix dovecot
 [[webroot_map]]
 hogehoge.com = /home/hogehoge/html
 www.hogehoge.com = /home/hogehoge/html

SSL/TLS設定

PostfixのSSL/TLS設定

postfixの設定は、「/etc/postfix/main.cf」「 /etc/postfix/ master.cf」で行います。
まずは、両ファイルともバックアップファイルを作成し、編集します
「main.cf」には次の項目を追加します

# cd /etc/postfix/
# cp -p ./main.cf ./main.cf.org3
# vi ./main.cf
  ・
  ・
  ・
### SSL/TLS Settings ###
smtp_tls_security_level = may
smtpd_tls_cert_file = /etc/letsencrypt/live/mail.hogehoge.com/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/mail.hogehoge.com/privkey.pem
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

「master.cf」は、 コメント(#)になっている、SMTPに関する行のコメントを外して有効にします

# cp -p ./master.cf ./master.cf.org2
# vi ./master.cf
  ・
  ・
  ・
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=permit_sasl_authenticated,reject
# -o milter_macro_daemon_name=ORIGINATING

「postfix check」コマンドでエラーが出なければ、編集は完了
「systemctl restart postfix」で再起動して設定を反映
「systemctl status postfix」で「active」を確認できます
さらに、「netstat -nat」で。465 LISTEN がある事を確認します

dovecotの設定

「/etc/dovecot/conf.d/10-ssl.conf」「 /etc/dovecot/conf.d/10-master.conf」ファイルを編集します
「10-ssl.conf」では、「ssl = no」を「ssl = required」に変更し、証明書を指定します

# cd  /etc/dovecot/conf.d/ 
# cp -p 10-ssl.conf 10-ssl.conf.org2
# vi ./10-ssl.conf
  ・
  ・
 ssl = no ----変更------->  ssl = required
 # ssl_cert = </etc/pki/dovecot/certs/dovecot.pem
 # ssl_key = </etc/pki/dovecot/private/dovecot.pem
  ・
  ・取得した証明書を指定
  ・
 ssl_cert = </etc/letsencrypt/live/mail.hogehoge.com/fullchain.pem
 ssl_key = </etc/letsencrypt/live/mail.hogehoge.com/privkey.pem

「 10-master.conf」では、iMAP/POP3  を無効化して、IMAPS/POP3S を有効にします

# cd  /etc/dovecot/conf.d/  
# cp -p 10-master.conf 10-master.conf.org2
# vi ./10-master.conf
  ・
  ・
 service imap-login {
   inet_listener imap {
      #port = 143
      port = 0
   }
   inet_listener imaps {
     port = 993
     ssl = yes
   }
   ・
   ・
 service pop3-login {
     #port = 110
   inet_listener pop3 {
     port = 0
   }
   inet_listener pop3s {
     port = 995
     ssl = yes
   }
 }

「dovecot」を再起動し確認します

$ sudo systemctl restart dovecot
$ sudo systemctl status dovecot
$ netstat -nat

ファイアウォールの設定

「firewall-cmd」コマンドで、サービスの追加、再起動、確認を行います

$ sudo firewall-cmd --add-service={smtps,imaps,pop3s} --permanent
success

$ sudo firewall-cmd --reload
success

$ sudo firewall-cmd --list-all
 public (active)
   target: default
   icmp-block-inversion: no
   interfaces: wlp8s0
   sources:
   services: ssh dhcpv6-client http https smtp smtp-submission pop3 imap  smtps imaps pop3s
   ports:
   protocols:
   masquerade: no
   forward-ports:
   source-ports:
   icmp-blocks:
   rich rules:

自宅ルータの設定

これで設定完了ですが、外部のメーラから送受信のテストをするとうまくいきませんでした。
よくわかりませんが、自宅ルータのポートフォワード設定で、143と25をサーバーに通してあげたら、受信できるようになりました。

自宅でWordPressを動かそう!


1 2 3 4 5 6 7 8 9 10 11

コメント

  1. かずばん より:

    覚書き
    openssl x509 -in /etc/letsencrypt/live/mail.hogehoge.com/cert.pem -noout -dates
    certbot certonly –dry-run –webroot -w /home/www/html/ -d mail.hogehoge.com

タイトルとURLをコピーしました