■PostfixのLDAP対応
案件的にはあまり多くないのですが、大規模な組織となればユーザ認証は全て中央のLDAPサーバに任すというとこ ろもあります。ホスティングサーバや中小の規模だとメールのユーザはメールサーバのローカルアカウントを使用しているところが多いです。
ここでは認証にOpenLDAPを使った設定のメモ書きを残します。OpenLDAPは既にインストールされアカウントも登録されているという前提です。Postfixがインストールされていなければyumからインストールを行います。
# yum install postfix # rpm -qa | grep postfix postfix-2.6.6-2.2.el6_1.i686 |
/etc/postfix/main.cfが設定ファイルとなります。以下、基本設定の部分です。
# vi /etc/postfix/main.cf
myhostname = mail.unix-power.net
mydomain = unix-power.net
myorigin = $myhostname
#inet_interfaces = localhost
inet_interfaces = all
inet_protocols = ipv4
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
mynetworks = 192.168.0.0/16, 127.0.0.1
home_mailbox = Maildir/
smtpd_recipient_restrictions = permit_mynetworks
reject_unauth_destination
|
続いてLDAP連携に関する設定をmain.cfの最後尾に追記します。
# vi /etc/postfix/main.cf virtual_transport = virtual # 下記設定はmydestinationとの重複はNG。複数ある場合はカンマで列挙する。 virtual_mailbox_domains = unix-power.net virtual_mailbox_base = /var/spool/virtual virtual_alias_maps = ldap:/etc/postfix/ldap-alias.cf virtual_mailbox_maps = ldap:/etc/postfix/ldap-mailbox.cf virtual_uid_maps = static:10000 virtual_gid_maps = static:10000 local_recipient_maps = proxy:unix:passwd.byname $alias_maps $virtual_mailbox_maps |
上記で指定した/etc/postfix/ldap-alias.cfファイルとldap-mailbox.cf ファイルを新規作成します。まず大前提としてLDAP連携する場合、それはPostfix上ではバーチャルドメインの仮想ユーザとして扱われます。ldap-alias.cfファイルは仮想ユーザから実ユーザへのマッピング定義で あるのに対し、ldap-mailbox.cfファイルは仮想ユーザと実際のメールの配送先(ディレクトリ)を定義しています。
# vi /etc/postfix/ldap-alias.cf server_host = 127.0.0.1 search_base = ou=Users,dc=unix-power,dc=net bind = yes bind_dn = cn=Manager,ou=Users,o=unix-power,dc=net bind_pw = password scope = one query_filter = (mail=%s) result_attribute = mail |
# vi /etc/postfix/ldap-mailbox.cf server_host = 127.0.0.1 search_base = ou=Users,dc=unix-power,dc=net bind = yes bind_dn = cn=Manager,ou=Users,o=unix-power,dc=net bind_pw = password scope = one query_filter = (mail=%s) result_attribute = homeDirectory result_format = %s/Maildir/ |
上記で幾つかの設定を実施しておりますが、それぞれの意味は下記のようになります。
| 項 目 |
説明 |
| server_host |
LDAPサーバのIPアドレスを指定します。 |
| search_base |
その名の通り、検索の起点を指定します。 |
| bind / bind_dn / bind_pw |
デフォルトだと匿名でLDAPサーバに接続するので 明示的に接続ユーザとパスワードを指定します。 |
| scope |
ldapsearch の-sオプションであり検索範囲を指定します。 oneに指定することでBaseDN自身と直下のエントリに限定します。 |
| query_filter |
検 索の条件式を指定します。基本的にはldapsearchと使い方は同じです。 特殊文字も利用可能であり、%uは@より前の部分、 %dは@より後の部分、%sはメールアドレスを指します。 |
| result_attribute |
LDAPサーバから取得する属性です。 |
| result_format |
実際の戻り値を整形します。%sはresult_attributeで得た値となります。 |
一旦、postfixを再起動します。
# /etc/rc.d/init.d/postfix start |
ここで実際にpostfixにパラメータを渡してLDAPから属性を取得できるか試してみます。
# postalias -q suzuki@unix-power.net ldap:/etc/postfix/ldap-mailbox.cf /home/suzuki/Maildir/ # postalias -q suzuki@unix-power.net ldap:/etc/postfix/ldap-alias.cf suzuki@unix-power.net |
上記のように正常な値を返してきていたらLDAP連携ともうまくいっていると思って良いと思います。あとはpostaliasコマンドでデーターベース化しておきます。
# postalias /etc/postfix/ldap-mailbox.cf # postalias /etc/postfix/ldap-alias.cf |
■PostfixのSMTP認証対応
各組織のローカルネットワークからのみメールが送信できるように設定するのが一般的ですが外部からも送信できたほうが便利です。但し、無条件に外部から送信できてしまうとこれを悪用されることがありSPAMメールの踏み台とされてしまいます。これを回避するため外部からのメール発信には必ず一度認証を通す必要があるよう設定します。これをSMTP認証と呼びます。
必要なソフトがインストールされていなければyumからインストールします。
# yum install cyrus-sasl cyrus-sasl-plain # rpm -qa | grep cyrus cyrus-sasl-2.1.23-13.el6_3.1.i686 cyrus-sasl-plain-2.1.23-13.el6_3.1.i686 cyrus-sasl-lib-2.1.23-13.el6_3.1.i686 |
LDAPに関する設定を行います。/etc/saslauthd.confに設定を行いますが、これはデフォルトで存在しないので新規作成 します。
# vi /etc/saslauthd.conf ldap_servers: ldap://localhost ldap_search_base: ou=Users,dc=unix-power,dc=net ldap_filter: (uid=%u) |
続いて/etc/sysconfig/saslauthdを編集します。
# vi /etc/sysconfig/saslauthd #MECH=pam MECH=ldap |
そしてpostfix側の設定を変更します。以下、編集部分のみ記載します。
# vi /etc/postfix/main.cf
# smtpd_recipient_restrictions = permit_mynetworks
# reject_unauth_destination
smtpd_recipient_restrictions = permit_mynetworks
permit_sasl_authenticated ※SMTP認証を行ったクライアントは許可
reject_unauth_destination
# SASL認証を有効化する
smtpd_sasl_auth_enable = yes
# mynetworks以外で匿名での接続を拒否する
smtpd_sasl_security_options = noanonymous
# 規格外の動作に対応させる
broken_sasl_auth_clients = yes
|
サブミッションポート( TCP/587 )をオープンにしておき、これを経由する際はSMTP Authを必須とします。
# 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 |
ここまでで設定は完了なのでCyrusを起動しPostfixもリロードします。
# /etc/rc.d/init.d/postfix reload postfix を再読み込み中: [ OK ] # /etc/rc.d/init.d/saslauthd start saslauthd を起動中: [ OK ] |
コマンドラインから動作確認を行います。Cyrusにtestsaslauthdコマンドというのが用意されていますのでこれを利用しま す。
# testsaslauthd -u tanaka -p password 0: OK "Success." |
上記のように「OK “Success.”」と表示されれば認証は成功しています。さらにPostfixでSMTP認証が正常に機能しているのか下記のコマンドから確認可能です。
# telnet localhost 25 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. 220 mail.unix-power.net ESMTP Postfix EHLO unix-power.net 250-mail.unix-power.net 250-PIPELINING 250-SIZE 10240000 250-VRFY 250-ETRN 250-AUTH PLAIN LOGIN 250-ENHANCEDSTATUSCODES 250-8BITMIME 250 DSN AUTH LOGIN 334 VXNlcm5hbWU6 |
上記のAUTH LOGINを発行した際にエラーが出なければOKです。
■DovecotのLDAP対応
LDAPに対応するためのDovecotの設定を行います。基本設定を含めてそれぞれのファイルを編集 します。
# vi /etc/dovecont/conf.d/10-mail.conf mail_location = maildir:~/Maildir |
# vi /etc/dovecot/dovecot.conf #protocols = imap pop3 lmtp protocols = imap pop3 #listen = *, :: listen = * |
# vi /etc/dovecot/conf.d/10-ssl.conf ssl = no |
# vi /etc/dovecot/conf.d/10-auth.conf disable_plaintext_auth = no !include auth-ldap.conf.ext |
auth-ldap.conf.extの中身は次のようになっています。編集箇所を太文字にしています。
# cat /etc/dovecot/conf.d/10-auth.conf
# more auth-ldap.conf.ext
# Authentication for LDAP users. Included from auth.conf.
#
# <doc/wiki/AuthDatabase.LDAP.txt>
passdb {
driver = ldap
# Path for LDAP configuration file, see example-config/dovecot-ldap.conf.ext
args = /etc/dovecot/dovecot-ldap.conf.ext
}
# "prefetch" user database means that the passdb already provided the
# needed information and there's no need to do a separate userdb lookup.
# <doc/wiki/UserDatabase.Prefetch.txt>
#userdb {
# driver = prefetch
#}
userdb {
driver = passwd
args = /etc/dovecot/dovecot-ldap.conf.ext
}
# If you don't have any user-specific settings, you can avoid the userdb LDAP
# lookup by using userdb static instead of userdb ldap, for example:
# <doc/wiki/UserDatabase.Static.txt>
userdb {
driver = static
args = uid=10000 gid=10000 home=/var/spool/virtual/home/%u
}
|
上記のファイルの中で指定されている/etc/dovecot/dovecot-ldap.conf.extは存在しないので新規に作成す る必要があります。これはLDAP連携の設定であり、サンプルの設定ファイルがありますのでそれを流用します。以下は編集部分のみ記載致します。
# cp /usr/share/doc/dovecot-2.0.9/example-config/dovecot-ldap.conf.ext /etc/dovecot/ # vi /etc/dovecot/dovecot-ldap.conf.ext hosts = localhost auth_bind = yes base = ou=Users,dc=unix-power,dc=net scope = onelevel user_attrs = mail,homeDirectory user_filter = (&(objectClass=posixAccount)(uid=%u)) pass_attrs = mail,userPassowrd pass_filter = (&(objectClass=posixAccount)(uid=%u)) default_pass_scheme = LDAP-SHA |
各項目の意味は次のようになります。
| 項 目 |
説 明 |
| hosts |
LDAP サーバの指定を行います。 |
| auth_bind |
メールクライアントによって与えられたユーザ名とパスワードを使って LDAPサーバにログインします。 |
| base |
アカウントを検索するディレクトリツリーの定義です。 |
| scope |
ldapsearch の-sオプションにあたるもの。 base, onelevel, subtreeのいずれかから選択します。 |
| user_attrs |
LDAPサーバから取得する属性でmailおよびhomeDirectoryの値を取得します。 |
| user_filter |
ObjectClass にposixAccountが設定されているもので、 かつクライアントのアカウント名とuidが一致するもの |
| pass_attrs |
LDAP サーバから取得する属性でmailおよびuserPasswordの値を取得します。 |
| pass_filter |
user_filter と同等。 |
| default_pass_scheme |
パスワードで使用しているハッシュ形式を指定します。 |
■動作確認
実際の動作確認をコマンドラインから行います。まだ一度もログインを行ったことのないユーザtanakaで試してみます。まず必要なディレクトリを予め作成し属性を変更しておきます。
# mkdir /var/spool/virtual # chown 10000.10000 /var/spool/virtual/ |
rootからユーザtanakaにメールを送信してみます。
# mail -v tanaka@unix-power.net Subject: test test . EOT Mail Delivery Status Report will be mailed to <root>. |
ディレクトリを確認し、自動作成されたことを確認します。
# ls -al /var/spool/virtual/home/tanaka/Maildir/new/ 合計 12 drwx------ 2 10000 10000 4096 1月 23 18:00 2013 . drwx------ 5 10000 10000 4096 1月 23 18:00 2013 .. -rw------- 1 10000 10000 540 1月 23 18:00 2013 1358931623.V802I603afM140971.xxxxx.yy.jp |
Dovecotで受信確認を行なってみます。
# telnet localhost 110
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
+OK Dovecot ready.
user tanaka
+OK
pass password
list
+OK Logged in.
+OK 1 messages:
1 556
.
retr 1
+OK 556 octets
Return-Path: <root@mail.unix-power.net>
X-Original-To: tanaka@unix-power.net
Delivered-To: tanaka@unix-power.net
Received: by mail.unix-power.net (Postfix, from userid 0)
id 11BD240C67; Wed, 23 Jan 2013 18:00:23 +0900 (JST)
Date: Wed, 23 Jan 2013 18:00:23 +0900
To: tanaka@unix-power.net
Subject: test
User-Agent: Heirloom mailx 12.4 7/29/08
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-Id: <20130123090023.11BD240C67@mail.unix-power.net>
From: root@mail.unix-power.net (root)
test
.
|
これで動作確認がとれました。