Postfix+Dovecotのインストール(複数ドメイン/バーチャルユーザ/SSL対応)
PostfixとDovecotでメールサーバを構築します。
複数ドメイン対応で、実アカウントではなくバーチャルユーザを作成します。
また、SSL対応にします。
なお、SSLサーバ証明書は既にインストールされているものとします。まだインストールしていない場合には、先にインストールを完了して下さい。
※postfixの仕様で、設定できるSSLサーバ証明書は1つのみです。複数ドメインで利用したい場合には、1つの証明書で複数のドメインに対応するマルチドメイン証明書等を設定して下さい。
1. Postfixのインストール
postfixをインストールします。
(1) インストール
(2) main.cfの編集
「/etc/postfix/main.cf」ファイルを編集します。
# cd /etc/postfix
# vi main.cf
■inet_interfaces
■inet_protocols
■mydestination
※バーチャルメールボックスで管理するドメインは mydestinationに指定してはいけません。(空白を指定)
■home_mailbox
それから行末に以下を追加します。
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = mail.example.jp, mail.example.com
smtpd_recipient_restrictions =
permit_mynetworks,
permit_sasl_authenticated,
reject_unauth_destination
smtpd_sasl_security_options = noanonymous
message_size_limit = 10240000
mailbox_size_limit = 102400000
virtual_transport = virtual
virtual_mailbox_domains = example.jp, example.com
virtual_mailbox_maps = hash:/etc/postfix/virtual-mailbox
virtual_mailbox_base = /var/spool/virtual
virtual_alias_maps = hash:/etc/postfix/valias_map
virtual_uid_maps = static:10000
virtual_gid_maps = static:10000
########## TLS/SSL関連 ##########
# TLSを有効化
smtpd_use_tls = yes
# 宛先のメールサーバがTLSに対応していれば、通信を暗号化する
smtp_tls_security_level = may
# サーバ証明書と秘密鍵を指定
# ※ここではLet's EncryptのSSLサーバ証明書の設定例です。実際の環境に合わせて設定して下さい。
smtpd_tls_cert_file = /etc/letsencrypt/live/(ドメイン名)/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/(ドメイン名)/privkey.pem
# TLSログレベルの設定
# 0:出力しない 1:TLSハンドシェイクと証明書情報 2:TLSネゴシエーションの全て
smtpd_tls_loglevel = 1
# 暗号に関する情報を "Received:" メッセージヘッダに含める
smtpd_tls_received_header = yes
# 接続キャッシュファイルの指定
smtpd_tls_session_cache_database = btree:/var/lib/postfix/smtpd_scache
# キャッシュの保持時間の指定
smtpd_tls_session_cache_timeout = 3600s
※「smtpd_sasl_local_domain」、「virtual_mailbox_domains」の項目は運用するドメイン名に変更して下さい。
※「smtpd_tls_cert_file」、「smtpd_tls_key_file」の項目には、SSLサーバ証明書のファイル名を指定して下さい。
標準のpostfixに存在していないファイルを作成します。
# touch /etc/postfix/virtual-mailbox
# touch /etc/postfix/valias_map
(3) master.cfの編集
「/etc/postfix/master.cf」ファイルを編集します。
# cd /etc/postfix
# vi master.cf
サブミッションポートの設定を行います。
submission inet n - n - - smtpd
# -o syslog_name=postfix/submission
# -o smtpd_tls_security_level=encrypt
-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
SMTPSの設定も行います。
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
設定ファイルのチェックを行います。
何も表示されなければOKです。
# /usr/sbin/postfix check
(4) postfixのサービスの起動
postfixのサービスを起動します。
# systemctl start postfix.service
postfixの自動起動の設定を行います。
# systemctl enable postfix.service
(5) 管理用ユーザの作成
管理用ユーザを作成します。
# groupadd -g 10000 vmail
# useradd -u 10000 -g vmail -s /usr/sbin/nologin vmail
(6) 配信用ディレクトリの作成
仮想アカウントへメールが配信されるディレクトリを作成します。
# mkdir /var/spool/virtual
# chown -R vmail:vmail /var/spool/virtual
# chmod -R 700 /var/spool/virtual
(7) 配送ファイルの作成
ドメイン毎、ユーザ毎のディレクトリはPostfixが自動的に「/var/spool/virtual」の下に作成しますので、事前に作成しておく必要はありません。
このディレクトリに各ユーザ用ディレクトリに配送するためのファイルを作成します。
ここでは「main.cf」の「virtual_mailbox_map」項目に設定した「/etc/postfix/virtual-mailbox」というファイルを新規作成します。
# vi /etc/postfix/virtual-mailbox
info@example.jp example.jp/info/Maildir/
test@example.com example.com/test/Maildir/
※末尾の「Maildir/」の「/」を忘れないこと
postmapコマンドで「virtual-mailbox.db」を作成・更新します。
# postmap /etc/postfix/virtual-mailbox
(8) エイリアス(転送)設定
実メールアカウントの場合には「/etc/aliases」ファイルを使用しますが、バーチャルユーザの場合には「main.cf」に追加した「virtual_alias_maps」項目に指定したファイルを作成します。
# vi /etc/postfix/valias_map
test@example.jp info@xxxxx.jp, info@example.com
※外部メールアドレスおよびバーチャルドメインメールアドレス共指定できます。
また、自身のメールアドレスにも残したい場合は、転送元と同じメールアドレスを右側に記入します。
test@example.jp test@example.jp, info@xxxxx.jp, info@example.com
※一見無限ループになるように思えますが、正常に転送されます。
「/etc/postfix/valias_map.db」を作成・更新します。
# postmap /etc/postfix/valias_map
(9) SMTP認証クライアントのインストール
SMTP認証クライアントとしてCyrusをインストールします。
# yum install cyrus-sasl cyrus-sasl-plain cyrus-sasl-md5
なお、STMP認証ユーザおよびパスワードは、Linuxのアカウントではなく、SASL独自のパスワードデータベースで管理します。
そこで「/etc/sasl2/smtpd.conf」を編集します。
# vi /etc/sasl2/smtpd.conf
#pwcheck_method: saslauthd
pwcheck_method: auxprop
#mech_list: plain login
mech_list: cram-md5 plain login
sasldb2のグループをpostfixに変更します。
# chgrp postfix /etc/sasldb2
※ユーザの登録が1件もない場合にはこのファイルが存在していない可能性があります。その場合にはユーザを登録後にこの設定を行って下さい。
saslauthdサービスを停止します。
# systemctl stop saslauthd.service
# systemctl disable saslauthd.service
(10) saslのコマンド
■ユーザー登録
# /usr/sbin/saslpasswd2 -u mail.example.com -c (ユーザー名)
【例】
# /usr/sbin/saslpasswd2 -u mail.example.com -c info@example.com
※-uのドメインは、postfixのmain.cfの「smtpd_sasl_local_domain」で指定したものと同じ値にします。この値が一致しないと認証に失敗します。
■ユーザー削除
# /usr/sbin/saslpasswd2 -u mail.example.com -d (ユーザー名)
【例】
# /usr/sbin/saslpasswd2 -u mail.example.com -d info@example.com)
■登録している認証ユーザーの確認
# /usr/sbin/sasldblistusers2
2. Dovecotのインストール
dovecotをインストールします。
(1) インストール
(2) dovecot.confの設定
「/etc/dovecot/dovecot.conf」を編集します。
※「imaps」、「pop3s」を利用する場合でも、この項目は上記の2つだけでOKです。
(3) /etc/dovecot/conf.dの各ファイルの設定
■10-auth.conf
disable_plaintext_auth = no
auth_mechanisms = cram-md5 plain login
#!include auth-deny.conf.ext
#!include auth-master.conf.ext
!include auth-system.conf.ext
#!include auth-sql.conf.ext
#!include auth-ldap.conf.ext
!include auth-passwdfile.conf.ext
#!include auth-checkpassword.conf.ext
#!include auth-vpopmail.conf.ext
!include auth-static.conf.ext
■ 10-mail.conf
mail_location = maildir:/var/spool/virtual/%d/%n/Maildir
※%d:ドメイン、%n:ユーザ
■ 10-master.conf
service imap-login {
inet_listener imap {
port = 143
}
inet_listener imaps {
port = 993
ssl = yes
service pop3-login {
inet_listener pop3 {
port = 110
}
inet_listener pop3s {
port = 995
ssl = yes
}
}
service auth {
・・・ (略) ・・・
unix_listener /var/spool/postfix/private/auth {
mode = 0666
user = postfix
group = postfix
}
# Auth process is run as this user.
#user = $default_internal_user
}
■ 10-ssl.conf
ssl = yes
ssl_cert = </etc/letsencrypt/live/(ドメイン名)/fullchain.pem
ssl_key = </etc/letsencrypt/live/(ドメイン名)/privkey.pem
■ auth-passwdfile.conf.ext
passdb {
driver = passwd-file
args = /etc/dovecot/users
}
userdb {
driver = passwd-file
args = /etc/dovecot/users
# Default fields that can be overridden by passwd-file
#default_fields = quota_rule=*:storage=1G
# Override fields from passwd-file
#override_fields = home=/home/virtual/%u
}
■ auth-static.conf.ext
userdb {
driver = static
args = uid=vmail gid=vmail home=/var/spool/virtual/%d/%u/Maildir
}
(4) CRAM-MD5ユーザファイルの作成
CRAM-MD5のパスワードを生成します。
# doveadm pw -s CRAM-MD5
Enter new password: (パスワード入力)
Retype new password: (同じパスワードをもう一度)
※ここでのパスワードはsaslpasswd2で作成したパスワードと同じにします。
以下のようなパスワード文字列が生成されます。(※この文字列はサンプルです。)
{CRAM-MD5}072c56cabbc67d2399eeaec47e5962395de0b711ab6f76366f6b80d5671cde47
前述の「auth-passwdfile.conf.ext」で記載したパス名でパスワードファイルを作成し、ユーザー情報および生成したパスワードをファイルに書き込みます。
書式は、以下の通りです。
ユーザ名):{CRAM-MD5}(生成したパスワード文字列)
【例】
info@example.jp:{CRAM-MD5}072c56cabbc67d2399eeaec47e5962395de0b711ab6f76366f6b80d5671cde47
作成したパスワードファイルのオーナーをdovecotにします。
# chown dovecot:dovecot users
(5) dovecotのサービスの起動
dovecotのサービスを起動します。
# systemctl start dovecot.service
dovecotの自動起動の設定を行います。
# systemctl enable dovecot.service
3. メール用アカウントの作成
(1) 配送用ファイルの編集
「/etc/postfix/virtual-mailbox」にユーザアカウント情報を追加します。
# vi /etc/postfix/virtual-mailbox
test@example.com example.com/test/Maildir/
info@example.jp example.jp/info/Maildir/
# postmap /etc/postfix/virtual-mailbox
(2) SASL認証用のアカウントとパスワードを作成
# saslpasswd2 -u mail.example.jp -c info@example.jp
Password:(パスワード)
Again (for verification):(パスワード)
(3) CRAM-MD5用のアカウントとパスワードを作成
{CRAM-MD5}072c56cabbc67d2399eeaec47e5962395de0b711ab6f76366f6b80d5671cde47
※上記文字列はサンプルです。実際の暗号化した文字列とは異なります。
info@example.jp:{CRAM-MD5}072c56cabbc67d2399eeaec47e5962395de0b711ab6f76366f6b80d5671cde47
最終更新:2020年05月12日 07:44