LoginSignup
12
12

More than 3 years have passed since last update.

CentOS7 Postfix + SMTP-Auth/Submission Port

Last updated at Posted at 2020-04-08

大昔に書いたPostfixの設定のUpdate。CentOS7でメール送りたいが、さすがに認証はさせておこうという事でpostfix + SMTP-AUTHを使う。認証に使うアカパスはCentOSのOSユーザを利用する。

必要パッケージのインストール

いつものやつ

yum install -y postfix cyrus-sasl cyrus-sasl-plain

postfix設定

/etc/postfix/main.cf

# myhostnameにホスト名を指定
myhostname = mail.hogehoge.com  

# mydomainにドメイン名を指定
mydomain = hogehoge.com

# myoriginのコメント削除
myorigin = $mydomain

# inet_protocolをipv4に限定
inet_protocols = ipv4

# mydestination設定変更
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain

# mynetworksを自環境に合わせ変更
mynetworks = 127.0.0.0/8,192.168.0.0/24,10.0.0.0/8

# relay_domainsのコメント削除
relay_domains = $mydestination

# header_checksのコメント削除
header_checks = regexp:/etc/postfix/header_checks

# smtpd_banne設定
smtpd_banner = $myhostname ESMTP

# 最下行に以下追記
# メッセージサイズ
message_size_limit = 5242880

# VRFYコマンド無効化
disable_vrfy_command = yes

# HELOコマンドを要求する(スパム対策)
smtpd_helo_required = yes

# MAIL FROMやRCPT TOコマンドに不正な文法を認めない
strict_rfc821_envelopes = yes

# user%domain" の形から "user@domain" への書換えを有効化
allow_percent_hack = yes

# site!user" から "user@site" への書換えを有効化
swap_bangpath = yes

# 信頼していないクライアントから $relay_domains にマッチする配送先へ、 送信者が指定したルーティングを持つメールであっても転送しない
allow_untrusted_routing = no

# 信頼しているネットワークからの接続のみ許可
smtpd_client_restrictions =
   permit_mynetworks,
   reject_unknown_client,
   permit

# SMTP DATA コマンドの場面で適用するオプションの制限
smtpd_data_restrictions =
   reject_unauth_pipelining,
   reject_multi_recipient_bounce

# SMTP HELO コマンドの場面で適用するオプションの制限
smtpd_helo_restrictions =
   permit_mynetworks,
   reject_invalid_hostname,
   reject_non_fqdn_hostname,
   reject_unknown_hostname,
   permit

# MAIL FROM コマンドの場面で適用するオプションの制限
smtpd_sender_restrictions =
   permit_mynetworks,
   reject_unknown_sender_domain,
   reject_non_fqdn_sender,
   permit

# ETRN 要求の場面で適用するオプションの制限
smtpd_etrn_restrictions =
   permit_mynetworks,
   reject_invalid_hostname,
   permit

# SMTP-Auth
# SASL認証有効化
smtpd_sasl_auth_enable = yes
# mynetworks以外で匿名での接続を拒否
smtpd_sasl_security_options = noanonymous
# 規格外の動作に対応
broken_sasl_auth_clients = yes
# RCPT TO コマンドの場面で適用するアクセス制限。SMTP_Authを利用したクラアントを許可
smtpd_recipient_restrictions = permit_mynetworks,
    permit_sasl_authenticated,
    reject_unauth_destination

サブミッションポート(TCP/587)の有効化

サブミッションポートを有効化し、利用の際にはSMTP-Auth必須とする

/etc/postfix/master.cf

修正前

# ==========================================================================
# service type  private unpriv  chroot  wakeup  maxproc command + args
#               (yes)   (yes)   (yes)   (never) (100)
# ==========================================================================
smtp      inet  n       -       n       -       -       smtpd
#smtp      inet  n       -       n       -       1       postscreen
#smtpd     pass  -       -       n       -       -       smtpd
#dnsblog   unix  -       -       n       -       0       dnsblog
#tlsproxy  unix  -       -       n       -       0       tlsproxy
# 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

修正後

# ==========================================================================
# service type  private unpriv  chroot  wakeup  maxproc command + args
#               (yes)   (yes)   (yes)   (never) (100)
# ==========================================================================
smtp      inet  n       -       n       -       -       smtpd
#smtp      inet  n       -       n       -       1       postscreen
#smtpd     pass  -       -       n       -       -       smtpd
#dnsblog   unix  -       -       n       -       0       dnsblog
#tlsproxy  unix  -       -       n       -       0       tlsproxy
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

*SASL認証使わずにサブミッションポートだけ有効にしたい場合は-o smtpd_recipient_restrictions=permit_sasl_authenticated,rejectの部分はコメントアウトのままにする

postfix 起動と自動起動設定

systemctl enable postfix
systemctl start postfix

Port587が起動していることを確認

ss -natup |grep master
tcp    LISTEN     0      100       *:25                    *:*                   users:(("master",pid=12822,fd=13))
tcp    LISTEN     0      100       *:587                   *:*                   users:(("master",pid=12822,fd=17))

SASL設定変更

OSユーザのアカウント情報を利用するように設定

/etc/sysconfig/saslauthd
MECH=pamをコメントアウトして、MECH=shadowを追記

# Directory in which to place saslauthd's listening socket, pid file, and so
# on.  This directory must already exist.
SOCKETDIR=/run/saslauthd

# Mechanism to use when checking passwords.  Run "saslauthd -v" to get a list
# of which mechanism your installation was compiled with the ablity to use.
#MECH=pam
MECH=shadow

# Additional flags to pass to saslauthd on the command line.  See saslauthd(8)
# for the list of accepted flags.
FLAGS=

SASL 起動と自動起動設定

systemctl enable saslauthd
systemctl start saslauthd

ユーザを追加と動作確認

OSログインはさせないようにユーザ追加

useradd -s /sbin/nologin hogehoge
passwd hogehoge

作ったユーザでテスト実施。OKがでればOK

testsaslauthd -u hogehoge -p yourpassword
0: OK "Success."

postfixでSMTP-Authが正常起動しているか確認
EHLOを入力した後、250 DSNまで一気に流れるので、AUTH LOGINを入力。エラーが出なければOK

telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 mail.hogehoge.com ESMTP
EHLO hogehoge.com
250-mail.hogehoge.com
250-PIPELINING
250-SIZE 10240000
250-ETRN
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
AUTH LOGIN
334 VXNlcm5hbWU6

参考サイト

CentOS7 PostfixのSMTP認証
Postfix 設定パラメータ
Postfixのセキュリティ設定について

12
12
1

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
12
12