CentOS 6.5 に メールサーバ構築

使用するソフト

  1. Postfix 2.6.6
  2. Dovecot 2.0.9

設定目標

example.com は読み替えで。

HOSTNAMEの変更

  • /etc/sysconfig/network の HOSTNAME が localhost.localdomain のままだとスパムDBに登録されてしまうため。
vi /etc/sysconfig/network
< HOSTNAME=localhost.localdomain
---
> HOSTNAME=examle.com

ソフトのインストール

yum install postfix dovecot cyrus-sasl-md5

サーバ実行ユーザの作成

groupadd -g 10000 vmail
useradd -u 10000 -g 10000 -d /var/mail/virtual -r -s /sbin/nologin vmail
mkdir /var/mail/virtual
chown vmail:vmail /var/mail/virtual
chmod 700 /var/mail/virtual

オレオレ証明書の作成

cd /etc/pki/tls/certs
openssl req -new -x509 -nodes -days 365 -out mail.crt -keyout mail.key
Generating a 2048 bit RSA private key
...+++
....+++
writing new private key to 'mail.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:*.example.com
Email Address []:
chmod 600 mail.key
  • 証明書の作成
openssl x509 -in mail.crt -outform der -out mail.der

Postfixの設定

main.cf
cp /etc/postfix/main.cf /etc/postfix/main.cf.org
vi /etc/postfix/main.cf
< #inet_interfaces = all
---
> inet_interfaces = all

< inet_interfaces = localhost
---
> #inet_interfaces = localhost

< inet_protocols = all
---
> #inet_protocols = all
> inet_protocols = ipv4

> relayhost = [smtp.your.isp.com]:587

> smtpd_banner = $myhostname ESMTP

> 
> smtpd_client_restrictions =
>   reject_rbl_client all.rbl.jp,
>   reject_rbl_client bl.spamcop.net
> 
> # Smtp client setting of relay isp smtpd.
> smtp_sasl_auth_enable = yes
> smtp_sasl_password_maps = hash:/etc/postfix/isp_smtp_auth
> smtp_sasl_security_options = noanonymous
> smtp_sasl_mechanism_filter = login, plain
> 
> # SASL
> smtpd_sasl_type = dovecot
> smtpd_sasl_path = private/dovecot-auth
> smtpd_sasl_security_options = noanonymous
> 
> # TLS
> smtpd_tls_security_level = may
> smtpd_tls_key_file = /etc/pki/tls/certs/mail.key
> smtpd_tls_cert_file = /etc/pki/tls/certs/mail.crt
> 
> # Virtual
> virtual_mailbox_domains = example.com
> virtual_mailbox_base = /var/mail/virtual
> virtual_mailbox_maps = hash:/etc/postfix/vmailbox
> virtual_uid_maps = static:10000
> virtual_gid_maps = static:10000
> virtual_alias_maps = hash:/etc/postfix/virtual
  • inet_interfaces = localhostコメントアウトし、inet_interfaces = all
  • inet_protocols = all をコメントアウトし、inet_protocols = ipv4
  • relayhost = [your.isp.smtp.com]:587 を追加し、ISPSMTPサーバへリレーする
  • smtpd_banner = $myhostname ESMTP とし、匿名性を高める
  • smtpd_client_restrictions = スパムメールDBを利用
  • smtp_sasl_auth_enable = yes 以下ISPSMTPサーバへのログインの設定をする(SMTPクライアントは smtp_ サーバは smtpd_ で設定することに注意)
  • smtpd_sasl_type 以下でSMTP-AUTHdovecot の利用を設定
  • smtpd_tls_security_level 以下で暗号化の有効と鍵と証明書の場所を指定
  • virtual_mailbox_domains 以下でバーチャルの設定、ドメインとか、メールボックスの場所とかユーザとか
master.cf
cp /etc/postfix/master.cf /etc/postfix/master.cf.org
vi /etc/postfix/master.cf
< #submission inet n       -       n       -       -       smtpd
< #  -o smtpd_tls_security_level=encrypt
< #  -o smtpd_sasl_auth_enable=yes
< #  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
< #  -o milter_macro_daemon_name=ORIGINATING
---
> submission inet n       -       n       -       -       smtpd
>   -o smtpd_tls_security_level=encrypt
>   -o smtpd_sasl_auth_enable=yes
>   -o smtpd_client_restrictions=permit_sasl_authenticated,reject
>   -o milter_macro_daemon_name=ORIGINATING

SMTPISPSMTPサーバに転送する

  • ISPからもらったログイン情報を /etc/postfix/isp_smtp_auth に書き込む
vi /etc/postfix/isp_smtp_auth
[smtp.your.isp.com]:587 username:passwrod
postmap /etc/postfix/isp_smtp_auth

/etc/postfix/isp_smtp_auth.db が生成されていればOK

メールユーザの作成

  • パスワードハッシュの生成
doveadm pw
Enter new password:
Retype new password:
{CRAM-MD5}00747cf2ffaf11c5ea4a64979c3901fc1d20dee13f480bb598f7d8575b23e61b
  • ハッシュを元にユーザを定義 /etc/dovecot/passwd を作成し定義する
vi /etc/dovecot/passwd 
> test@example.com:{CRAM-MD5}00747cf2ffaf11c5ea4a64979c3901fc1d20dee13f480bb598f7d8575b23e61b
  • 作成したユーザ用のMaildirを定義 /etc/postfix/vmailbox を作成し定義する
vi /etc/postfix/vmailbox
> test@example.com    example.com/test/Maildir/
  • /etc/postfix/vmailbox の内容をdbに書き込む
postmap /etc/postfix/vmailbox

/etc/postfix/vmailbox.db が生成されていればOK

Dovecotの設定

dovecot.conf
cp /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.org
vi /etc/dovecot/dovecot.conf
> listen = *
 
> # Postfix SMTP Auth
> service auth {
>   unix_listener /var/spool/postfix/private/dovecot-auth {
>     group = postfix
>     mode = 0660
>     user = postfix
>   }
> }
  • listen IPv6形式のlisten を削除する
  • service auth以下 PostfixSMTP-AUTH の時 dovecotのユーザ定義を使えるように設定する
conf.d/10-mail.conf
cp /etc/dovecot/conf.d/10-mail.conf /etc/dovecot/conf.d/10-mail.conf.org
vi /etc/dovecot/conf.d/10-mail.conf
> mail_location = maildir:~/Maildir
  • Maildirの場所をメールユーザホームからの相対パスで指定
conf.d/10-ssl.conf

cp /etc/dovecot/conf.d/10-ssl.conf /etc/dovecot/conf.d/10-ssl.conf.org
vi /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/pki/dovecot/certs/dovecot.pem
> #ssl_key = </etc/pki/dovecot/private/dovecot.pem
> ssl_cert = </etc/pki/tls/certs/mail.crt
> ssl_key = </etc/pki/tls/certs/mail.key
conf.d/10-auth.conf
cp /etc/dovecot/conf.d/10-auth.conf /etc/dovecot/conf.d/10-auth.conf.org
vi /etc/dovecot/conf.d/10-auth.conf
< auth_mechanisms = plain
---
> #auth_mechanisms = plain
> auth_mechanisms = cram-md5 plain login

< #!include auth-passwdfile.conf.ext
---
> !include auth-passwdfile.conf.ext

< #!include auth-static.conf.ext
---
> !include auth-static.conf.ext
  • auth_mechanisms cram-md5 login でのログインを可能化
  • 拡張設定ファイルの読み込みのコメントアウトを解除
conf.d/auth-passwdfile.conf.ext
cp /etc/dovecot/conf.d/auth-passwdfile.conf.ext /etc/dovecot/conf.d/auth-passwdfile.conf.ext.org
vi /etc/dovecot/conf.d/auth-passwdfile.conf.ext
<   args = scheme=CRYPT username_format=%u /etc/dovecot/users
---
>   #args = scheme=CRYPT username_format=%u /etc/dovecot/users
>   args = scheme=CRAM-MD5 username_format=%u /etc/dovecot/passwd

< userdb {
<   driver = passwd-file
<   args = username_format=%u /etc/dovecot/users
< }
---
> #userdb {
> #  driver = passwd-file
> #  args = username_format=%u /etc/dovecot/users
> #}
  • CRAM-MD5を使うように設定し、ユーザ定義の位置を /etc/dovecot/passwd と設定する
  • userdb 以下はstaticで定義するのでコメントアウト
conf.d/auth-static.conf.ext
cp /etc/dovecot/conf.d/auth-static.conf.ext /etc/dovecot/conf.d/auth-static.conf.ext.org
vi /etc/dovecot/conf.d/auth-static.conf.ext
< #userdb {
< #  driver = static
---
> userdb {
>   driver = static

< #}
---
>   args = uid=vmail gid=vmail home=/var/mail/virtual/%d/%n 
> }

設定の反映と自動実行の設定

  • 再起動して反映
service postfix restart
service dovecot restart
  • chkconfig で自動実行化
cd /etc/rc.d/init.d/
chkconfig postfix on
chkconfig dovecot on

Postgrey 以外はここまでで設定完了
メールのエラー等は /var/log/maillog に吐出されるので、
デバッグはそこを見てみるとOK

Postgrey

EPELレポジトリの追加

http://dl.fedoraproject.org/pub/epel/
ここからバージョンにあったrpmを探す
今回はCentOS6.5、64bitなので
6→x86_64と進み
epel-release-X-Y.noarch.rpm を インストールする(X Y は最新)

rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
  • 通常はEPELを無効にし、必要なときだけ使用する
vi /etc/yum.repos.d/epel.repo

#enabled=1
enabled=0

< enabled=1
---
> #enabled=1
> enabled=0
Postgreyのインストール
  • --enablerepo=epel をつけてyumを実行した時のみEPELが参照される
yum --enablerepo=epel install postgrey
vi /etc/postfix/main.cf
> smtpd_recipient_restrictions =
>   permit_sasl_authenticated,
>   reject_unauth_destination,
>   check_policy_service unix:postgrey/socket
service postgrey start
cd /etc/rc.d/init.d/
chkconfig postrey on

以上でPostgreyが動作し、greylisting が行われる

postgrey の設定項目は

/usr/sbin/postgrey --help

で確認することができ、
起動スクリプト /etc/rc.d/init.d/postgrey
の options= 項目を変更することで、
デーモンにパラメータを渡すことができる

動作確認

Yahooメール等からメールを送信して /var/log/maillog を見る

450 4.2.0 <test@example.com>: Recipient address rejected: Greylisted, see http://postgrey.schweikert.ch/help/example.com.html; from=<xxxxxx@yahoo.co.jp> to=<test@example.com> proto=SMTP helo=<xxx.yyy.zzz.yahoo.co.jp>

という感じの greylisting されたよというログが残り、
postgrey の --delay= 設置値(デフォルト300秒)経過後あたりに

postgrey[xxxx]: action=pass, reason=triplet found,

と送信元SMTPサーバがpassされメールが届けばOK