Let’s EncryptでメールサーバーもSSL化する。
Let’s Encryptでは既存のWEBサーバー用サブドメイン(www.xxxxx.com)の証明書にメールサーバー用のサブドメイン(mail.xxxxx.com)の証明書を追加(SAN : Subject Alternative Name)して、メールサーバーをSSL化することができます。メールサーバー用のサブドメインは取得済みであることを前提に、メールサーバーのSSL化について説明します。
参考サイト:Let’s Encrypt 総合ポータル (非公式解説サイト)
1.Apacheのvhostにメールサーバーのエイリアスを追加します。(httpsの証明書と共用するため)
/etc/apache2/sites-available/www.sample.com.conf
<VirtualHost *:80>
# HTTP connection to redirect to HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request’s Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.sample.com
ServerAdmin webmaster@localhost
ServerName sample.com
ServerAlias www.sample.com
# mail Alias added ↓ここに追加
ServerAlias mail.sample.com
DocumentRoot /home/www/sample
2.Apacheを停止します。
service apache2 stop
3.既存のサーバー証明書にメールサーバー用サブドメインを追加します。
以下のコマンドでメールサーバー用サブドメインのサーバー証明書を追加します。(先頭に既存のサブドメイン、その次に今回追加するサブドメイン)
certbot –authenticator standalone –installer apache –expand -d www.sample.com -d mail.sample.com
The requested apache plugin does not appear to be installed
→python3のPlug-inが足りないと言われたら、
apt install python3-certbot-apache
再度実行
What would you like to do?
——————————————————————————-
1: Attempt to reinstall this existing certificate
2: Renew & replace the cert (limit ~5 per 7 days)
——————————————————————————-
Select the appropriate number [1-2] then [enter] (press ‘c’ to cancel): 1
Keeping the existing certificate
Created an SSL vhost at /etc/apache2/sites-available/sample.com-le-ssl.conf
Deploying Certificate to VirtualHost /etc/apache2/sites-available/sample.com-le-ssl.conf
Enabling available site: /etc/apache2/sites-available/sample.com-le-ssl.conf
Deploying Certificate to VirtualHost /etc/apache2/sites-available/sample.com-le-ssl.conf
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
——————————————————————————-
1: No redirect – Make no further changes to the webserver configuration.
2: Redirect – Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you’re confident your site works on HTTPS. You can undo this
change by editing your web server’s configuration.
——————————————————————————-
Select the appropriate number [1-2] then [enter] (press ‘c’ to cancel): 1 ←vhost内でリダイレクトしているので1を選択します。
——————————————————————————-
Congratulations! You have successfully enabled https://www.sample.com and
https://mail.sample.com ←成功!のメッセージ
You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=www.sample.com
https://www.ssllabs.com/ssltest/analyze.html?d=mail.sample.com ←追加されています。
——————————————————————————-
IMPORTANT NOTES:
– Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/www.sample.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/www.sample.com/privkey.pem
Your cert will expire on 9999-99-99. To obtain a new or tweaked
version of this certificate in the future, simply run certbot again
with the “certonly” option. To non-interactively renew *all* of
your certificates, run “certbot renew”
– Some rewrite rules copied from
/etc/apache2/sites-enabled/sample.com.conf were disabled in
the vhost for your HTTPS site located at
/etc/apache2/sites-available/sample.com-le-ssl.conf because
they have the potential to create redirection loops.
– If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let’s Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le
5.Apacheを起動します。
service apache2 start
6.iptablesにメール用ポートの設定をします。(設定用シェルがある場合は、再実行する。)
# WEB OK(HTTPS)
$IPTABLES -A INPUT -p tcp –dport 80 -j ACCEPT
$IPTABLES -A INPUT -p tcp –dport 443 -j ACCEPT
# SMTP OK(SMTPS)
$IPTABLES -A INPUT -p tcp –dport 25 -j ACCEPT
$IPTABLES -A INPUT -p tcp –dport 465 -j ACCEPT
$IPTABLES -A INPUT -p tcp –dport 587 -j ACCEPT
# SSH Port xxxx OK(FTP NG) xxxxは任意
$IPTABLES -A INPUT -p tcp –dport xxxx -j ACCEP
# POP OK (POP3S)
$IPTABLES -A INPUT -p tcp –dport 995 -j ACCEPT
※ iptables-persistentが起動時に読み込めるよう、rules.ipv4も修正します。
参考サイト:Ubuntuでiptablesの設定をiptables-persistentで永続化する
/etc/iptables/rules.ipv4
-A INPUT -p icmp -j ACCEPT
-A INPUT -p tcp -m tcp –dport 80 -j ACCEPT
-A INPUT -p tcp -m tcp –dport 443 -j ACCEPT
-A INPUT -p tcp -m tcp –dport 25 -j ACCEPT
-A INPUT -p tcp -m tcp –dport 465 -j ACCEPT
-A INPUT -p tcp -m tcp –dport 587 -j ACCEPT
-A INPUT -p tcp -m tcp –dport xxxx -j ACCEPT
-A INPUT -p tcp -m tcp –dport 995 -j ACCEPT
7.Postfix & Dovecotの設定ファイルを修正します。
参考サイト:Server World(SSL/TLS の設定)
※ httpsの証明書と共用するための証明書の指定は、以下の通りです。
※ メールサーバーのサブドメインではなく、WEBサーバー用の証明書を指定します。
/etc/postfix/main.cf
# SMTP-Auth 設定
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_security_options = noanonymous
smtpd_recipient_restrictions = permit_mynetworks, permit_auth_destination, permit_sasl_authenticated, reject
# smtps 設定
smtpd_use_tls = yes
smtp_tls_mandatory_protocols = !SSLv2, !SSLv3
smtpd_tls_mandatory_protocols = !SSLv2, !SSLv3
smtpd_tls_cert_file = /etc/letsencrypt/live/www.sample.com/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/www.sample.com/privkey.pem
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
dovecot
/etc/dovecot/conf.d/10-ssl.conf
ssl = yes
ssl_cert = </etc/letsencrypt/live/www.sample.com/fullchain.pem
ssl_key = </etc/letsencrypt/live/www.sample.com/privkey.pem
8.Postfix & Dovecotを再起動します。
service postfix dovecot restart
9.PCのメールクライアントを変更します。
受信ポート:995 送信ポート:587+SSLポート:465 (詳細は、7のURLを参照)
10.外部(WAN)からメールの送受信ができるよう、ルーターのポートを開放(NAT or NAPT)することをお忘れなく
995(WAN) ←→ 995(LAN)
465(WAN) ←→ 465(LAN)
587(WAN) ←→ 587(LAN)
11.メールの送受信(LAN、WAN)が上手く行けばOK
12.追記(Postfixの設定ファイル、master.cfについて)
maister.cfの設定で、コメントを外す際に上部のsmtpをコメントアウトしないこと。理由は、サーバー間のメール転送は従来通りPort 25を利用するため。
※詳細設定を記述します。
/etc/postfix/master.cf
smtp inet n – y – – smtpd ←コメントアウトしないこと。
#smtp inet n – y – 1 postscreen
#smtpd pass – – y – – smtpd
#dnsblog unix – – y – 0 dnsblog
#tlsproxy unix – – y – 0 tlsproxy
# smtps updated 2019/02/16
submission inet n – y – – smtpd ←以下、コメントを外します。(SSL,STARTTLSどちらにも対応)
# -o syslog_name=postfix/submission
-o smtpd_tls_security_level=encrypt
-o smtpd_sasl_auth_enable=yes
# -o smtpd_tls_auth_only=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 – y – – 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
#628 inet n – y – – qmqpd
◎メールサーバーのSSL化は設定ヶ所が多くとにかく手が掛かります。何度も躓くと思いますが、mail.logを頼りに最後まで頑張ることが大切です。