Postfix の SMTP 認証の設定(saslauthd認証)

カテゴリ:メールサーバ

CentOS/Ubuntu 両対応

SMTP認証は、認証に成功したユーザーのみに他のドメインへのリレー(オープンリレー)を許可する仕組みです。 これによりスパムメールの踏み台にされることを防止します。

Noteこの記事ではsaslauthdによるSMTP認証の方法を解説しています。なお、Postfix 2.3 以降ではDovecotによるSMTP認証も可能です。

PostfixでSMTP認証を行うためには、以下の設定を実施します。

saslauthdのインストール

saslauthdをインストールします。

CentOS 7 の場合:

# yum -y install cyrus-sasl

CentOS 8 以降の場合:

# dnf -y install cyrus-sasl

Ubuntu の場合:

# apt -y install sasl2-bin

saslauthdの設定

SASLによる認証方法には、主にUNIXユーザー認証(PAM認証)SASL側のデータベース(sasldb)による認証が用いられます。

PAM認証を使用する場合

saslauthdの設定ファイル(/etc/sasl2/smtpd.conf)は、UNIXユーザーで認証(初期設定はPAM(Pluggable Authentication Modules)認証)する場合は、pwcheck_method:は初期設定のsaslauthdのままで問題ありません。

注意仮想ドメイン(マルチドメイン)でSMTP認証を行う場合は、理由は後述しますがPAM認証にする必要があります。

CentOS の場合(/etc/sasl2/smtpd.conf):

pwcheck_method: saslauthd
mech_list: plain login

Note Ubuntuの場合は初期設定ではsmtpd.confファイルは存在しません。
/etc/postfix/sasl/smtpd.confなどに新たに作成して設定します。

UNIXユーザー認証の方式の確認

UNIXユーザー認証の方式がPAM認証であるかは、saslauthdの設定ファイルで確認できます。
以下のようにMECH=pam(Ubuntuの場合はMECHANISMS="pam")であればPAM認証です。(PAM認証がデフォルトです)

CentOS の場合(/etc/sysconfig/saslauthd):

# 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

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

Ubuntu の場合(/etc/default/saslauthd):

#
# Settings for saslauthd daemon
# Please read /usr/share/doc/sasl2-bin/README.Debian for details.
#

# Should saslauthd run automatically on startup? (default: no)
START=no

# Description of this saslauthd instance. Recommended.
# (suggestion: SASL Authentication Daemon)
DESC="SASL Authentication Daemon"

# Short name of this saslauthd instance. Strongly recommended.
# (suggestion: saslauthd)
NAME="saslauthd"

# Which authentication mechanisms should saslauthd use? (default: pam)
#
# Available options in this Debian package:
# getpwent  -- use the getpwent() library function
# kerberos5 -- use Kerberos 5
# pam       -- use PAM
# rimap     -- use a remote IMAP server
# shadow    -- use the local shadow password file
# sasldb    -- use the local sasldb database file
# ldap      -- use LDAP (configuration is in /etc/saslauthd.conf)
#
# Only one option may be used at a time. See the saslauthd man page
# for more information.
#
# Example: MECHANISMS="pam"
MECHANISMS="pam"

# Additional options for this mechanism. (default: none)
# See the saslauthd man page for information about mech-specific options.
MECH_OPTIONS=""

# How many saslauthd processes should we run? (default: 5)
# A value of 0 will fork a new process for each connection.
THREADS=5

# Other options (default: -c -m /var/run/saslauthd)
# Note: You MUST specify the -m option or saslauthd won't run!
#
# WARNING: DO NOT SPECIFY THE -d OPTION.
# The -d option will cause saslauthd to run in the foreground instead of as
# a daemon. This will PREVENT YOUR SYSTEM FROM BOOTING PROPERLY. If you wish
# to run saslauthd in debug mode, please run it by hand to be safe.
#
# See /usr/share/doc/sasl2-bin/README.Debian for Debian-specific information.
# See the saslauthd man page and the output of 'saslauthd -h' for general
# information about these options.
#
# Example for chroot Postfix users: "-c -m /var/spool/postfix/var/run/saslauthd"
# Example for non-chroot Postfix users: "-c -m /var/run/saslauthd"
#
# To know if your Postfix is running chroot, check /etc/postfix/master.cf.
# If it has the line "smtp inet n - y - - smtpd" or "smtp inet n - - - - smtpd"
# then your Postfix is running in a chroot.
# If it has the line "smtp inet n - n - - smtpd" then your Postfix is NOT
# running in a chroot.
OPTIONS="-c -m /var/run/saslauthd"

sasldbによる認証を使用する場合

sasldbで認証する場合は、smtpd.confファイルのpwcheck_method:にauxpropを指定します。

pwcheck_method: auxprop
mech_list: plain login

SMTP認証のためのmain.cfの設定

/etc/postfix/main.cf ファイルにSMTP認証関連のパラメータを設定します。

smtpd_sasl_auth_enable

SMTP認証を使用するかどうかの設定です。使用する場合はyesを設定します。
自分(当該VPSのユーザー)が不特定のIPアドレス(自宅PCやスマートフォン等)からVPS経由でメールを送信したい場合は、必ずSMTP認証を有効にしましょう。

smtpd_sasl_auth_enable = yes

smtpd_sasl_local_domain

PAM認証の場合

仮想ドメイン環境など、PAM認証を使用する場合は値を無し(何も指定しない)にします。

smtpd_sasl_local_domain=

sasldbによる認証の場合

SASLの認証を有効にするドメインを指定します。
但しドメインは1つしか指定できません。
また、後述するSASLユーザーの登録時に指定するドメインと、ここで指定するドメイン(この例では myhostname に指定されているドメイン)が同一でなければ認証に失敗します。
そのため、マルチドメイン環境では、sasldbによる認証は使用できないのです。

smtpd_sasl_local_domain=$myhostname

smtpd_recipient_restrictions

受信者制限です。permit_sasl_authenticated と permit_inet_interfaces を追加します。
このパラメータは左から順番に判定して行き、条件に一致した時点で終了(許可か拒否かが決定される)します。

smtpd_recipient_restrictions = permit_mynetworks permit_sasl_authenticated permit_inet_interfaces reject_unauth_destination

broken_sasl_auth_clients

古いバージョンのAUTHコマンドを有効にする場合はyesにします。

broken_sasl_auth_clients = yes

smtpd_sasl_security_options

anonymousでのSMTP認証を拒否します。

smtpd_sasl_security_options=noanonymous

postconfコマンドを使用して設定する場合

なお、main.cfを直接編集せずに、postconfコマンドを使用して同様の設定を行う場合は、以下を実行します。

# postconf -e smtpd_sasl_auth_enable=yes
# postconf -e smtpd_sasl_local_domain=$myhostname
# postconf -e 'smtpd_recipient_restrictions=permit_mynetworks permit_sasl_authenticated permit_inet_interfaces reject_unauth_destination'
# postconf -e smtpd_sasl_security_options=noanonymous

sasldb関連の設定(sasldbによる認証の場合のみ)

SASL2のユーザー、パスワードを作成

以下のコマンドでSMTP認証を行うドメインとユーザー、パスワードを作成します。

saslpasswd2 -u ドメイン名 -c ユーザー名

以下の例では、example.com ドメインの hoge ユーザーを作成しています。
# saslpasswd2 -u example.com -c hoge
Password:
Again (for verification):

※パスワードは2回入力します。
※ドメイン名は main.cf の smtpd_sasl_local_domain パラメータに指定したもの(この例ではmyhostname)と同一のドメインを指定する必要があります。つまり、この例は $myhostname に example.com が指定されている場合を想定したものです。

作成したユーザーを確認

以下のコマンドで作成したSMTP認証のユーザーとドメインを確認できます。

# sasldblistusers2
hoge@example.com: userPassword

dbのパーミッションを設定

作成したデータベースファイルのパーミッションを設定します。
この設定は重要です。

# chgrp postfix /etc/sasldb2
# chmod 640 /etc/sasldb2

saslauthdの開始と自動起動

以下のコマンドでsaslauthdの自動起動を有効にした上で、開始します。

# systemctl enable saslauthd
# systemctl start saslauthd

Postfixの設定のリロード

Postfixをリロードして設定を反映します。

# systemctl reload postfix

動作の確認方法

/etc/postfix/master.cf ファイルの smtp の行の stmpd の後ろに「-v」を付けて詳細ログを出力するように設定し、"postfix reload" で設定を反映させます。
※サブミッションポートで待ち受けている場合は submission の行に追加します。

smtp      inet  n       -       n       -       -       smtpd -v

メーラーでSMTP認証を有効にして外部ドメインへのリレーが許可される事を確認します。
SMTP認証に成功した場合、maillog に「name=permit_sasl_authenticated status=1」が記録されます。

Sep 30 05:47:29 centos64 postfix/smtpd[3300]: >>> START Recipient address RESTRICTIONS <<<
Sep 30 05:47:29 centos64 postfix/smtpd[3300]: generic_checks: name=permit_sasl_authenticated
Sep 30 05:47:29 centos64 postfix/smtpd[3300]: generic_checks: name=permit_sasl_authenticated status=1

次にSMTP認証を無効にした状態で外部ドメイン宛に送信し、リレーが拒否される事を確認します。
SMTP認証に失敗した場合、maillog に「name=permit_sasl_authenticated status=0」が記録されます。
また、「name=reject_unauth_destination status=2」は smtpd_recipient_restrictions の reject_unauth_destination に一致して拒否した事を示します。

Sep 30 05:46:58 centos64 postfix/smtpd[3300]: >>> START Recipient address RESTRICTIONS <<<
Sep 30 05:46:58 centos64 postfix/smtpd[3300]: generic_checks: name=permit_sasl_authenticated
Sep 30 05:46:58 centos64 postfix/smtpd[3300]: generic_checks: name=permit_sasl_authenticated status=0
...
Sep 30 05:46:58 centos64 postfix/smtpd[3300]: NOQUEUE: reject: RCPT from unknown[192.168.126.1]: 554 5.7.1 : Relay access denied; from= to= proto=ESMTP helo=<[192.168.126.1]>
Sep 30 05:46:58 centos64 postfix/smtpd[3300]: generic_checks: name=reject_unauth_destination status=2

このログでの検証は運用前に確実に実施しておきましょう。
SMTP認証に成功した場合(当該VPSのユーザー)は他のドメインにリレー出来ること、SMTP認証が実施されていない場合(当該VPSのユーザー以外=スパマー)は他のドメインにリレー出来ないこと(踏み台にされないこと)を確認しておきます。

特に後者を許した場合はスパマーと見なされる恐れがあるため特に重要です。
というのも自分はスパムメールを一切送信していないにも拘らず、スパムメールを受け取った側から見れば大量のスパムを送りつけてくる悪質なスパムメール送信元以外の何者でも無いからです。
これがオープンリレー状態の怖いところです。
更に、スパム配送元として見なされメールレピュテーションサービス(MTAのブラックリスト)に自分のVPSのIPアドレスが登録される恐れもあります。
もし一旦上記ブラックリストに登録されると解除してもらうのは容易な事ではありません。最悪、IPアドレスを変更するためにVPSを再契約・再構築せざるを得ない可能性もあります。

公開日時:2014年07月20日 23:48:08
最終更新日時:2024年03月25日 00:30:52

なお、VPS選びで迷ったらこちらの記事で主要VPSのメモリ容量ごとの月額、年額料金を比較していますので、是非参考にしてみてください。

メールサーバに戻る

このページのトップに戻る