CentOS 5.2 に Postfix を SMTP-AUTH 含めて外部レンタルサーバ仕様でセットアップする

October 7, 2008

VPSサーバに独自ドメインを用いてSMTPサーバをセットアップしたのでメモしておく。
スパムメール送信防止で Outbound Port 25 Blocking を行っているプロバイダーが多く、自分のところも該当している。そのため、Submission Port 587 でSMTP認証(SMTP AUTH)をサポートする必要があったのでその設定も行う。なお、SMTP 認証は PLAIN でセットアップした。

yum から必要なパッケージをインストール

1
2
# yum install postfix.i386
# yum install cyrus-sasl-plain.i386

Postfix /etc/postfix/main.cf の設定

書き換え追加して設定した部分は下記のとおり。mailbox は Maildir にする。SASL の設定を追記した。
※ホストのFQDNは mail.domain.tld で受け取るメールアドレスは user@domain.tld とする。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
myhostname = mail.domain.tld
mydomain = domain.tld
myorigin = $mydomain
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
mynetworks_style = host
home_mailbox = Maildir/
\# SMTP-Auth
#
smtpd\_sasl\_auth_enable = yes
smtpd\_tls\_auth_only = no
smtpd\_sasl\_local_domain = $myhostname
smtp\_sasl\_security_options = noanonymous
smtpd\_recipient\_restrictions = permit\_mynetworks, permit\_sasl\_authenticated, reject\_unau
th_destination
disable\_vrfy\_command = yes

※レンタルサーバは 1 ホストだけの構成として、mynetworks_style = host とホスト単体に設定した。

新規ユーザ用の Maildir を用意

※既存のユーザには、 ~/Maildir の同様に作成する。

1
2
3
4
\# mkdir -p /etc/skel/Maildir/cur/
\# mkdir /etc/skel/Maildir/new/
\# mkdir /etc/skel/Maildir/tmp/
\# chmod -R 700 /etc/skel/Maildir

Postfix /etc/postfix/master.cf の設定

smtp と submission をコメント部分を下記のとおり修正して設定する。通常の外部からのメールを受ける場合は、SASL は使わないので SMTP ではオフにする。

1
2
3
4
5
smtp      inet  n       -       n       -       -       smtpd
-o smtpd\_sasl\_auth_enable=no
submission inet n - n - - smtpd
-o smtpd\_sasl\_auth_enable=yes
-o smtpd\_recipient\_restrictions=permit\_sasl\_authenticated,reject

SASL の設定

認証はsaslauthd ではなく、データベース sasldb2 で行う。

/usr/lib/sasl2/smtpd.conf を設定
1
2
3
pwcheck_method: auxprop
auxprop_plugin: sasldb
mech_list: plain login
認証ユーザ・パスワードを作成
1
# saslpasswd2 -u mail.domain.tld user

※上記のユーザとパスワードをメーラーでの送信サーバに接続で使用する。

sasldb2のパーミッションを変更

ユーザ postfix で読めるようにする。

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

ファイアウォール /etc/sysconfig/iptables の設定

IN/OUT の 25番, IN の 587 番ポートを開ける。

1
2
3
-A INPUT -p tcp -m tcp --dport 25 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 587 -j ACCEPT
-A OUTPUT -p tcp -m tcp --sport 25 -j ACCEPT

postfix, iptables のサービス設定・再起動

最後は postfix の自動起動を on にする。

1
2
3
# service postfix start
# service iptables restart
# chkconfig postfix on

sendmail 等を使用していた場合、postfix に MTA を切り替える。

1
# alternatives --config mta

UBE(迷惑メール)中継対策

http://www.ipa.go.jp/security/ciadr/antirelay.html
RBL.JPで対策テストができる。
main.cf の disable_vrfy_command = yes はユーザーが存在するか確認できてしまうため VRFY コマンドを無効にしている。

tilfin freelance software engineer