« PreviousNext »

LDAP & Postfix 仮想メールユーザー作成

14 11 月 2007

 
 
LDAP & Postfixで仮想メールユーザーが使えるようにLDAPサーバーを構築します。

まずOpenLDAP関係の設定を変更する↓
これからいろいろとファイルを設定します。
赤字 はおのおの環境に合わせてください。
黄色 は注意するところです。
 は変更箇所 および 追加箇所です。
 はコマンドです。
なお、ou=Peopleou=Group は前回で作成済みとします。
 
設定ファイル①確認↓
# vi /etc/openldap/slapd.conf
 
access to attrs=userPassword
    by self write
    by dn="cn=Manager,dc=lunq,dc=net" write
    by anonymous auth
    by * none
 
↓ 以下を確認
access to attrs=userPassword
    by anonymous auth
    by dn="cn=Admin,dc=lunq,dc=net" read
    by * none
 
access to *
    by dn="cn=Manager,dc=lunq,dc=net" write
    by self write
    by * read
 
↓ 以下を確認
access to *
    by self =rwcsx
    by dn="cn=Admin,dc=lunq,dc=net" =rwcsx
    by * read
 
既存情報確認↓
# ldapsearch -x -b 'uid=server,ou=People,dc=lunq,dc=net'
# extended LDIF
#
# LDAPv3
# base <uid=server,ou=People,dc=lunq,dc=net> with scope subtree
# filter: (objectclass=*)
# requesting: ALL
#
 
# server, People, lunq.net
dn: uid=server,ou=People,dc=lunq,dc=net
uid: server
cn: server
objectClass: person
objectClass: organizationalPerson
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
postalAddress: cn=server,ou=Group,dc=lunq,dc=net
shadowLastChange: 13782
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 500
gidNumber: 500
homeDirectory: /home/server
givenName: server
sn: server
gecos: Lunq User
mail: server@lunq.net
 
# search result
search: 2
result: 0 Success
 
# numResponses: 2
# numEntries: 1
 
 
初期状態で登録をしたら ↓ のようになっている
 
--------------------ここから--------------------
 
dn: uid=hogehoge,ou=People,dc=lunq,dc=net
uid: hogehoge
cn: hogehoge
objectClass: account削除
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
userPassword: {crypt}xxxxxxxxxxxxxxxxxxxx
shadowLastChange: 13782
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 500
gidNumber: 500
homeDirectory: /home/hogehoge
 
--------------------ここまでを--------------------
 
↑上記の内容のままでは使用できないので、
↓下記のように設定を変更する。
 
--------------------ここから--------------------
 
dn: uid=hogehoge,ou=People,dc=lunq,dc=net
uid: hogehoge
cn: hogehoge
objectClass: person   ← 追加
objectClass: organizationalPerson   ← 追加
objectClass: inetOrgPerson   ← 追加
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
userPassword: {crypt}xxxxxxxxxxxxxxxxxxxx
postalAddress: cn=hogehoge,ou=Group,dc=lunq,dc=net   ← 追加
shadowLastChange: 13782
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 500
gidNumber: 500
homeDirectory: /home/hogehoge
givenName: hogehoge   ← 追加
sn: hogehoge   ← 追加
gecos: Lunq User   ← 追加
mail: hogehoge@lunq.net   ← 追加
 
--------------------ここまで--------------------
 
↑上記をメールを使うであろうユーザーすべてに変更を行う

 
 
Postfix 仮想メールユーザー用の設定↓

LDAPサーバー認証を使用するための確認をする↓
# rpm -q cyrus-sasl
cyrus-sasl-2.1.22-4
 
# rpm -q db4
db4-4.3.29-9.fc6
 
↑もし、入ってなかったら↓
 
# yum -y install cyrus-sasl
# yum -y install db4
 
↓反映させておく
# echo "/usr/lib/sasl2" >> /etc/ld.so.conf
# ldconfig
 
↓初期状態であればたぶん有効になっている、けど確認
# vi /etc/openldap/slapd.conf
include         /etc/openldap/schema/cosine.schema
include         /etc/openldap/schema/inetorgperson.schema
include         /etc/openldap/schema/misc.schema
 
# vi /etc/postfix/ldap-alias.cf既存ユーザー認証用ファイル新規作成
server_host = 127.0.0.1     ← LDAPサーバーが自分の場合 違う場合はサーバーのアドレスを指定
server_port = 389
timeout = 5
bind = yes
bind_dn = cn=Admin,dc=lunq,dc=net
bind_pw = xxxxxxxxxxxxxxxxxxxxAdmin用のパスワード、平文で記述
search_base = ou=People,dc=lunq,dc=net
query_filter = (mail=%s)
result_attribute = uid
result_format = %u
scope = sub
 
# vi /etc/postfix/ldap-account.cf仮想ユーザー認証用ファイル新規作成
server_host = 127.0.0.1     ← LDAPサーバーが自分の場合 違う場合はサーバーのアドレスを指定
server_port = 389
timeout = 5
bind = no
bind_dn = cn=Admin,dc=lunq,dc=net
bind_pw = xxxxxxxxxxxxxxxxxxxxAdmin用のパスワード、平文で記述
search_base = ou=People,dc=lunq,dc=net
 
query_filter = (mail=%s)
result_attribute = uid
result_format = %s/Maildir/
scope = sub
 
# grep vmail /etc/passwd仮想メール用ユーザーUID、GIDを調べる
vmail:x:525:525::/home/vmail:/bin/false
 
# vi /etc/postfix/main.cfPostfix設定ファイル編集
#mydomain = lunq.net先頭に#を入れる(コメントアウト)
myorigin = $myhostname$myhostnameに変更
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain$mydomain を削除
alias_maps = hash:/etc/aliases, ldap:/etc/postfix/ldap-alias.cfさきほど作ったファイルを記述
alias_database = hash:/etc/aliases
#home_mailbox = Maildir/先頭に#を入れる(コメントアウト)
smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
virtual_uid_maps = static:525仮想メール配送用UID
virtual_gid_maps = static:525仮想メール配送用GID (UID と GID は上で調べたものを指定)
virtual_mailbox_base = /home/vmail仮想メール配送用ディレクトリ
virtual_mailbox_maps = ldap:/etc/postfix/ldap-account.cf先ほど作ったファイルを記述
virtual_mailbox_domains = lunq.net仮想ユーザー用ドメイン
 
# vi /usr/lib/sasl2/smtpd.confsmtpauth用設定ファイル編集
pwcheck_method: saslauthd
mech_list: plain login
 
# vi /etc/sysconfig/saslauthdsmtpauth用設定ファイル編集
MECH=ldap
FLAGS="-O /etc/saslauthd.conf"
 
# vi /etc/saslauthd.confsmtpauth用LDAP参照用ファイル編集
ldap_servers: ldap://127.0.0.1/     ← LDAPサーバーが自分の場合 違う場合はサーバーのアドレスを指定
ldap_bind_dn: cn=Manager,dc=lunq,dc=net
ldap_password: xxxxxxxxxxxxxxxxxxxxManager用パスワードを、平文で記述
ldap_search_base: ou=People,dc=lunq,dc=net
 
# vi mailadd.sh既存ユーザー用メールフォルダ作成スクリプトを作成
 
------------------------------ここから------------------------------
 
#!/bin/bash
 
COUNT=499;
NUM=599;
 
while [ $COUNT -lt $NUM ]
do
COUNT=`expr $COUNT + 1`
if [ `grep $COUNT /etc/passwd` ]; then
grep $COUNT /etc/passwd > ldpass
 
LNAME=`cat ldpass |cut -d ":" -f 1`
 
mkdir /home/vmail/$LNAME
mkdir -p /home/vmail/$LNAME/Maildir/new
mkdir -p /home/vmail/$LNAME/Maildir/cur
mkdir -p /home/vmail/$LNAME/Maildir/tmp
chown -R vmail:vmail /home/vmail/$LNAME/
chmod -R 700 /home/vmail/$LNAME
 
rm -f ldpass
 
fi
done
 
ll /home/vmail/
 
------------------------------ここまで------------------------------
 
メールの配送や取得はユーザー vmail がするので vmail 用にユーザー、グループ、パーミッションを変更する
 
# ./mailadd.sh既存ユーザー用メールフォルダ作成スクリプトを実行
 
↓ 設定反映のためリスタート
# /etc/rc.d/init.d/saslauthd restart
# /etc/rc.d/init.d/postfix restart
 
↓ smtpauth認証テスト
# testsaslauthd -u hunter -p passwordpasswordは任意のパスワード
0: OK "Success."

 
 
Dovecot 仮想メールユーザー用の設定↓

# vi /etc/dovecot-ldap.confLDAP & Postfix連携ファイル作成
hosts = 127.0.0.1     ← LDAPサーバーが自分の場合 違う場合はサーバーのアドレスを指定
dn = cn=Admin,dc=lunq,dc=net
dnpass = xxxxxxxxxxxxxxxxxxxxAdmin用パスワードを、平文で記述
auth_bind = yes
base = ou=People,dc=lunq,dc=net
scope = subtree
pass_attrs = uid=user, userPassword=password
pass_filter = (uid=%u)
 
# vi /etc/dovecot.confDovecot 設定ファイルを編集
#mail_location = maildir:~/Maildir先頭に#を入れる(コメントアウト)210行目前後
auth default {
  mechanisms = plain
  #passdb pam {    ← 先頭に#を入れる(コメントアウト)772行目前後
	・
	・
   #args = dovecot
  #}    ← 先頭に#を入れる(コメントアウト)
  #passdb passwd {    ← 先頭に#を入れる(コメントアウト)806行目前後
  #}    ← 先頭に#を入れる(コメントアウト)
  passdb ldap {   ← コメントアウトを解除 845行目前後
    args = /etc/dovecot-ldap.confコメントアウトを解除して追加
  }   ← コメントアウトを解除
  #userdb passwd {    ← 先頭に#を入れる(コメントアウト)869行目前後
  #}    ← 先頭に#を入れる(コメントアウト)
  userdb static {   ← コメントアウトを解除 881行目前後
    args = uid=525 gid=525 home=/home/vmail/%uコメントアウトを解除して追加(UID と GID は上で調べたものを指定)
  }   ← コメントアウトを解除
  userdb ldap {   ← コメントアウトを解除 899行目前後
    args = /etc/dovecot-ldap.confコメントアウトを解除して追加
  }   ← コメントアウトを解除
  user = root  923行目前後
 
↓Dovecot 再起動
# /etc/rc.d/init.d/dovecot restart

 
 
仮想メールユーザー用スクリプト作成↓

ここで登録できるのはメールユーザーのみです。
システムユーザーも一緒に登録したければ、
LDAPサーバー構築 のldp-adduserスクリプトを参照してください。
 
# vi ldp-mailadd  ← 仮想メールユーザー登録用ファイル作成
 
--------------------ここから--------------------
 
#!/bin/bash
 
LPASS=**********;  # LDAP管理用パスワードを平文で入力
 
if [ "$1" = "" ] ; then
    echo " "
    echo "  Usage: ./ldp-adduser [username] [password]"
    echo " "
    echo "  ユーザー名を指定してください。"
    echo " "
    exit 1
fi
 
if [ "$2" = "" ] ; then
    echo " "
    echo "  Usage: ./ldp-adduser [username] [password]"
    echo " "
    echo "  パスワードを指定してください。"
    echo " "
    exit 1
fi
 
echo "dn: uid="$1",ou=People,dc=lunq,dc=net" >> mail.ldif
echo "objectClass: person" >> mail.ldif
echo "objectClass: organizationalPerson" >> mail.ldif
echo "objectClass: inetOrgPerson" >> mail.ldif
echo "uid: "$1 >> mail.ldif
echo "sn: "$1 >> mail.ldif
echo "cn: "$1 >> mail.ldif
 
PASSWORD=`slappasswd -s $2 -h {MD5}`
 
echo "userPassword: "$PASSWORD >> mail.ldif
echo "givenName: "$1 >> mail.ldif
echo "mail: "$1"@lunq.net" >> mail.ldif
 
ldapadd -h localhost -x -D "cn=Manager,dc=lunq,dc=net" -w $LPASS -f mail.ldif
 
mkdir /home/vmail/$1
mkdir -p /home/vmail/$1/Maildir/new
mkdir -p /home/vmail/$1/Maildir/cur
mkdir -p /home/vmail/$1/Maildir/tmp
chown -R vmail:vmail /home/vmail/$1/
chmod -R 700 /home/vmail/$1
 
rm -f mail.ldif
 
ldapsearch -x -b 'uid='$1',ou=People,dc=lunq,dc=net'
 
exit 0
 
--------------------ここまで--------------------
 
 
ここで削除できるのはメールユーザーのみです。
システムユーザーも一緒に削除したければ、
LDAPサーバー構築 のldp-deluserスクリプトを参照してください。
 
# vi ldp-maildel  ← 仮想メールユーザー削除用ファイル作成
 
--------------------ここから--------------------
 
#!/bin/bash
 
# LDAPパスワード
LPASS=**********;  # LDAP管理用パスワードを平文で入力
 
# Sanity check
if [ "$1" = "" ] ; then
    echo "  Usage: ./ldp-deluser [username]"
    exit 1
fi
 
# ユーザーメールボックス削除
rm -rf /home/vmail/$1
 
# LDAPサーバー削除
ldapdelete -h localhost -x -D 'cn=Manager,dc=lunq,dc=net' -w $LPASS "uid="$1",ou=People,dc=lunq,dc=net"
 
# 登録情報確認
ldapsearch -x -b 'uid='$1',ou=People,dc=lunq,dc=net'
 
--------------------ここまでを--------------------
 
# chmod +x ldp-mailadd  ← 仮想メールユーザー登録用ファイルに実行権付与
# chmod +x ldp-maildel  ← 仮想メールユーザー削除用ファイルに実行権付与

 
 
新規仮想メールユーザーの登録手順↓

# ./ldp-mailadd hunter password  ← 仮想メールユーザー登録、パスワードは任意
adding new entry "uid=hunter,ou=People,dc=lunq,dc=net"
 
# extended LDIF
#
# LDAPv3
# base <uid=hunter,ou=People,dc=lunq,dc=net> with scope subtree
# filter: (objectclass=*)
# requesting: ALL
#
 
# hunter, People, lunq.net
dn: uid=hunter,ou=People,dc=lunq,dc=net
objectClass: person
objectClass: organizationalPerson
objectClass: inetOrgPerson
uid: hunter
sn: hunter
cn: hunter
givenName: hunter
mail: hunter@lunq.net
 
# search result
search: 2
result: 0 Success
 
# numResponses: 2
# numEntries: 1
 
# ll /home/mail/  ← 仮想メールユーザー用フォルダ確認
合計 80
drwx------ 5 vmail vmail 4096 11月  5 16:41 honyarara
drwx------ 3 vmail vmail 4096 12月 30 03:18 hunter
drwx------ 3 vmail vmail 4096 12月 18 16:41 hogehoge
 
# ./ldp-maildel hunter  ← 仮想メールユーザー削除
 
# extended LDIF
#
# LDAPv3
# base <uid=hunter,ou=People,dc=lunq,dc=net> with scope subtree
# filter: (objectclass=*)
# requesting: ALL
#
 
# search result
search: 2
result: 32 No such object
matchedDN: ou=People,dc=lunq,dc=net
 
# numResponses: 1
 
# ll /home/mail/  ← 仮想メールユーザー用フォルダ確認
合計 76
drwx------ 5 vmail vmail 4096 11月  5 16:41 honyarara
drwx------ 3 vmail vmail 4096 12月 18 16:41 hogehoge

 
 
実際にメールユーザーを登録して、outlookか何かで送受信してみよう。
無理だったら、設定ファイルを見直して、もう一度チャレンジです。
あっているようであれば、ログファイルを見てみよう。
ここまで構築できた人であれば大丈夫だと思いますが、一応確認のため
 
送信サーバー(Postfix)はどこまで?
受信サーバー(Dovecot)はどこまで?
その辺を確認しながら調整してみよう。

Posted in 仮想メールサーバー構築(LDAP) | Trackback | del.icio.us | Top Of Page

No comments yet

Leave a Reply