LoginSignup
0
0

More than 3 years have passed since last update.

AWS EC2でサーバを構築 6 - PostfixとDovecot (SSL対応とSPAM対策)

Last updated at Posted at 2020-04-14

記事一覧

概要

PostfixとDovecotでメールサーバを構築します。SSL化とSMTP認証、SPAM対策を取り入れます。

Postfix

メインドメインがexample1.comで、こちらでSSL証明書を取得します。

メールクライアントの送受信サーバ設定では、example2.comのメールアカウントであってもmail.example1.comを指定します。

SMTP認証にはDovecotを使います。SPAM対策のためRBLを参照するよう設定します。

/etc/postfix/main.cf

myhostname = mail.example1.com
mydomain = example1.com
myorigin = $mydomain

inet_interfaces = all
inet_protocols = ipv4

mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain

home_mailbox = Maildir/

smtpd_banner = $myhostname ESMTP unknown

#
#
allow_mail_to_commands = alias,forward,include

# virtual_alias
#
virtual_alias_maps = hash:/etc/postfix/virtual
virtual_alias_domains = example2.com

# spam block - 2020.04.07
#
disable_vrfy_command = yes
smtpd_delay_reject = yes
smtpd_helo_required = yes

smtpd_helo_restrictions =
    permit_mynetworks,
    reject_non_fqdn_hostname,
    reject_invalid_hostname,
    permit

smtpd_error_sleep_time = 1s
smtpd_soft_error_limit = 5
smtpd_hard_error_limit = 10

smtpd_client_message_rate_limit = 6
smtpd_client_recipient_rate_limit = 6

# smtp-auth
#
smtpd_sasl_auth_enable = yes 
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_recipient_restrictions =
    permit_sasl_authenticated
    reject_invalid_hostname,
    reject_non_fqdn_hostname,
    reject_non_fqdn_sender,
    reject_non_fqdn_recipient,
    reject_unknown_sender_domain,
    reject_unknown_recipient_domain,
    permit_mynetworks,
    reject_rbl_client all.rbl.jp,
    reject_rbl_client bl.spamcop.net,
    reject_rbl_client zen.spamhaus.org,
    permit
broken_sasl_auth_clients = yes

# tls/ssl
#
smtpd_use_tls = yes
smtpd_tls_cert_file = /etc/letsencrypt/live/mail.example1.com/fullchain.pem
smtpd_tls_key_file  = /etc/letsencrypt/live/mail.example1.com/privkey.pem
smtp_tls_security_level = may
smtpd_tls_loglevel = 1
smtpd_tls_session_cache_database = btree:/var/lib/postfix/smtpd_scache
smtpd_tls_session_cache_timeout = 3600s

TLS有効化

SMTPSを使うための設定を行います。必要な部分をコメントインします。

/etc/postfix/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

SSL証明書取得

メールサーバmail.example1.com用にLet's Ecnryptの証明書を取得します。Nginxと同様の手順です。

80/tcpで待ち受ける必要があるため、Nginxの設定で443/tcpにリダイレクトする設定があればコメントアウトしておきます。

certbot certonly --webroot -w /var/www/html/www.example1.com/public_html/ -d mail.example1.com -m root@example1.com

エイリアス設定

root宛のメールを別ユーザーへ転送します。

/etc/alises

root:           example1

上記設定を反映させるためには以下作業が必要です。

newaliases

バーチャルドメイン設定

メインドメインであるexample1.comはバーチャルドメインにできません。

/etc/postfix/virtual (作成)

example2.com            anything
info@example2.com       example2

上記ファイルを更新したらpostmapが必要です。

postmap virtual

Dovecot

使用プロトコルとポートを指定します。実際使うのはIMAPSとPOP3Sですが、ここではimap pop3と書きます。

/etc/dovecot/dovecot.conf

#protocols = imap pop3 lmtp
protocols = imap pop3

#listen = *, ::
listen = *

認証関連

標準的なメーラーの初期設定、自動認識に対応させます。

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

#disable_plaintext_auth = yes
disable_plaintext_auth = no

# auth_mechanisms = plain
auth_mechanisms = plain login

メールボックス

mbox形式からMaildir形式に変更します。

/etc/dovecot/conf.d/10-mail.conf

#mail_location = 
mail_location = maildir:~/Maildir

プロトコルとポート

IMAPとPOP3は使わず、SSL経由のIMAPSとPOP3Sを使うよう設定します。

PostfixもSMTPSを使うので、これでメール関連はすべてSSL通信に対応できます。

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

service imap-login {
  inet_listener imap {
    #port = 143
    port = 0
  }
  inet_listener imaps {
    #port = 993
    port = 993
    #ssl = yes
    ssl = yes
  }

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

  # Postfix smtp-auth
  #unix_listener /var/spool/postfix/private/auth {
  #  mode = 0666
  #}
  unix_listener /var/spool/postfix/private/auth {
    mode = 0666
    user = postfix
    group = postfix
  }

SSL設定

Let's Ecnryptで取得したメールサーバ用の証明書を指定します。

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

#ssl_cert = </etc/pki/dovecot/certs/dovecot.pem
#ssl_key = </etc/pki/dovecot/private/dovecot.pem
ssl_cert = </etc/letsencrypt/live/mail.example1.com/fullchain.pem
ssl_key = </etc/letsencrypt/live/mail.example1.com/privkey.pem

PostfixとDovecotを起動します。

systemctl start postfix dovecot
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0