Postfix & devecot その2

CentOS6に対してインストールした際のメモとして記載する。


前回CentOS5に対して、PostfixとDovecotをインストールしたが、久しぶりにサービスを起動して使ってみると、SMTP認証に失敗し、メールを送信できなかった。
そこで、もう一度設定をやり直してみた。postfix,dovecotは一度yumで削除し、定義ファイルも消してやり直し。

sendmailの停止
実際には使用していなかったので停止の必要は無し。

標準MTAの切り替え
こちらもPostfixになっていたので作業不要。

# alternatives --config mta
There is 1 program that provides 'mta'.
Selection Command
-----------------------------------------------
*+ 1 /usr/sbin/sendmail.postfix
Postfixとdovecotのインストール yum -y install postfix
yum -y install dovecot
/etc/postfix/main.cfの編集

ホスト名(FDQN)の指定

myhostnae = mail.example.jp

ドメインの指定

mydomain= example.jp

送信元アドレスは、myoriginで指定する。例としては、$myhostnameや$mydomainを指定する。
私は以下にした。
myorigin = $myhostname

メールを受け付けるネットワークインタフェースの指定

inet_interfaces=all
にして外部ネットからも受け付けられるようにする。

メールアドレスのドメイン部になるmydestinationは,

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

にした。最後の$mydomainがないと、他のSMTPサーバからメールが転送されてこなかった。

mynetworks_style の設定は触らず。

メールの置き方は、Maildir形式にする。

home_mailbox = Maildir/

SMTPで使用しているメーラがわからないようにする。

smtpd_banner = $myhostname ESMTP unknown

最後にSMTP-Authの設定を追加する。

disable_vrfy_command = yes
smtpd_sasl_auth_enable = yes
broken_sasl_auth_clients = yes
smtpd_sasl_security_options = noanonymous
smtpd_sasl_local_domain = $myhostname
smtpd_sender_restrictions = reject_unknown_sender_domain
smtpd_client_restrictions = permit_mynetworks,
reject_rbl_client relays.ordb.org,
reject_rbl_client spamcop.net,
reject_rbl_client dynablock.wirehub.net,
reject_rbl_client sbl.spamhaus.org,
check_client_access hash:/etc/postfix/reject_client,reject_unknown_client,permit
smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
message_size_limit = 10485760

smtpd_tls_cert_file = /etc/pki/tls/certs/mail.crt
smtpd_tls_key_file = /etc/pki/tls/certs/mail.key
smtpd_use_tls = yes

mail.crtとmail.keyについては後ほど。

IPアドレスでのリジェクト

上記の/etc/postfix/reject_clientは、IPアドレスで制限するが、まずは空で作成。

# vi /etc/postfix/reject_client

このファイルと上記の定義をすると、reject_client.dbというDBファイルが必要になるので作成する。

# /usr/sbin/postmap reject_client

 

# vi /etc/sasl2/smtpd.conf

pwcheck_method: saslauthd
mech_list: plain login
allow_plaintext: true

 

saslauthdの起動

# service saslauthd start

chkconfig --list saslauthd

で確認

Dovecotの設定

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

mail_location = maildir:~/Maildir

valid_chroot_dirs = /home

補足:

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

#disable_plaintext_auth = yes

のところはそのまま。plaintext認証はNGにしておく。

 

メールボックスの作成 自分のホームディレクトリ配下にMaildirディレクトリを作成
ファイアウォール
# 外部からのTCP25番ポート(SMTP)へのアクセスを許可 
iptables -A INPUT -p tcp --dport 25 -j ACCEPT
# 外部からのTCP465番ポート(SMTPS)へのアクセスを許可
iptables -A INPUT -p tcp --dport 465 -j ACCEPT
# 外部からのTCP110番ポート(POP3)へのアクセスを許可
# ※POP3サーバーを公開する場合のみ
iptables -A INPUT -p tcp --dport 110 -j ACCEPT
# 外部からのTCP143番ポート(IMAP)へのアクセスを許可 
iptables -A INPUT -p tcp --dport 143 -j ACCEPT
# 外部からのTCP993番ポート(IMAPS)へのアクセスを許可 
# ※IMAPSサーバーを公開する場合のみ 
iptables -A INPUT -p tcp --dport 993 -j ACCEPT
最後に # iptables-save
# service iptables restart 補足: #-A INPUT -j REJECT --reject-with icmp-host-prohibited が有効になっていると、iMacのMailで設定できなかったので、試しにコメントアウト。とりあえず、コメントアウトせずに試してからの方がよいでしょう。
メールの中継

vi /etc/postfix/main.cf

他のメールサーバから受信することはできるが、他のメールサーバに送信することができなかった場合、以下を確認。

smtpd_recipient_restrictions = permit_mynetworks,
permit_sasl_authenticated,  
reject_unauth_destination
「permit_sasl_authenticated:SMTP_AUTHに認証されたクライアントを許可 reject_unauth_destination:以下のアドレスにマッチするものは許可 $inet_interfaces $mydestination $virtual_alias_domains $virtual_mailbox_domains 以下のドメイン・サブドメインは許可 $relay_domains 上記以外は全て拒否」とのこと
smtpd_sender_restrictions = reject_unknown_sender_domain
も設定。reject_unknown_sender_domain:送信者のメールアドレスが DNS の Aレコードや MXレコードを持たない場合は拒否とのこと。
    

 

テスト

・自分のメールサーバへのメール送信:OK

・他のメールサーバからのメール受信:OK

・他のメールサーバへのメール送信:OK

・iPhoneでのメール送受信:OK

・iPhoneから他のメールサーバへのメール送信:OK

・存在しないアカウントへのSMTP接続がエラーになること:OK

MacのThundirbird,Mailとも問題なし。

なんちゃって証明書作成

http://tipszone.jp/20120521_mail_server/
の方の方法を参考にさせていただいた。設定はほとんど既定値のまま。Common Nameだけ、正しいホスト名を書いた。また、有効期限は1年とした。

# cd /etc/pki/tls/certs
# openssl req -new -x509 -nodes -days 365 -out mail.crt -keyout mail.key

Common Name (eg, your name or your server's hostname) []:*.example.com

# chmod 600 mail.key

セキュリティ証明書を作成する。

# openssl x509 -in mail.crt -outform der -out mail.der

注意

なんちゃって証明書だと、ThunderBirdは例外承認を求めてくるし、iMacのMailが「本当にいいの?」聞いてくる。別途、正式な証明書を追加する方法も書く予定。