18.5. LDAP サーバーの構築例

ここでは、以下のモデルをベースに LDAP サーバーを構築するまでの手順を解説します。この LDAP サーバーは、Sales グループと Development グループの人員情報検索、および Development グループに所属する人員の場合は、認証サーバーとしても機能しています。

18.5.1. /etc/openldap/slapd.conf の編集

上記の設計をもとに LDAP サーバーの設定ファイル /etc/openldap/slapd.conf を編集します。デフォルトの内容は以下の通りです。

#
# See slapd.conf(5) for details on configuration options.
# This file should NOT be world readable.
#
include         /etc/openldap/schema/core.schema
include         /etc/openldap/schema/cosine.schema
include         /etc/openldap/schema/inetorgperson.schema
include         /etc/openldap/schema/nis.schema
include         /etc/openldap/schema/dnszone.schema

# Define global ACLs to disable default read access.

# Do not enable referrals until AFTER you have a working directory
# service AND an understanding of referrals.
#referral       ldap://root.openldap.org

pidfile         /var/run/slapd.pid
argsfile        /var/run/slapd.args

# Load dynamic backend modules:
modulepath      /usr/lib/openldap
moduleload      back_bdb.la
# moduleload    back_bdb.la
# moduleload    back_ldap.la
# moduleload    back_ldbm.la
# moduleload    back_passwd.la
# moduleload    back_shell.la

# Sample security restrictions
#       Require integrity protection (prevent hijacking)
#       Require 112-bit (3DES or better) encryption for updates
#       Require 63-bit encryption for simple bind
# security ssf=1 update_ssf=112 simple_bind=64

# Sample access control policy:
#       Root DSE: allow anyone to read it
#       Subschema (sub)entry DSE: allow anyone to read it
#       Other DSEs:
#               Allow self write access
#               Allow authenticated users read access
#               Allow anonymous users to authenticate
#       Directives needed to implement policy:
# access to dn.base="" by * read
# access to dn.base="cn=Subschema" by * read
# access to *
#       by self write
#       by users read
#       by anonymous auth
#
# if no access controls are present, the default policy
# allows anyone and everyone to read anything but restricts
# updates to rootdn.  (e.g., "access to * by * read")
#
# rootdn can always read and write EVERYTHING!

#######################################################################
# BDB database definitions
#######################################################################

database        bdb
suffix          dc=localhost,dc=localdomain
rootdn          cn=Manager,dc=localhost,dc=localdomain

# Cleartext passwords, especially for the rootdn, should
# be avoided.  See slappasswd(8) and slapd.conf(5) for details.
# Use of strong authentication encouraged.
rootpw          {SHA}s1qM/cSaS+X0cNcg1/AdnYiIEQg=
password-hash   {SHA}

# The database directory MUST exist prior to running slapd AND
# should only be accessible by the slapd and slap tools.
# Mode 700 recommended.
directory       /var/openldap-data

# Indices to maintain
index   objectClass     eq

スキーマファイルの設定

最初にスキーマファイルの設定を行います。ここでは認証サーバーとして LDAP を構築する場合に必要な標準的なスキーマを include しています。これは、Turbolinux 11 Server のデフォルトの設定です。その他の LDAP サーバーを構築する場合にはディレクトリツリーの設計に基づいて適切なスキーマファイルを include する必要があります。

include         /etc/openldap/schema/core.schema
include         /etc/openldap/schema/cosine.schema
include         /etc/openldap/schema/inetorgperson.schema
include         /etc/openldap/schema/nis.schema
include         /etc/openldap/schema/dnszone.schema
suffix、rootdn、rootpwの設定

LDAP サーバで管理するディレクトリツリーの基点を定義します。ここでは、dc=example,dc=com をディレクトリツリーのベース DN(suffix)として設定します。

suffix "dc=exmple,dc=com"

また、これらのベース DN を管理する LDAP サーバー管理者のユーザ名とパスワードを以下のように設定します。

rootdn "cn=Manager,dc=example,dc=com"
rootpw {SSHA}TmTx+14ulaXYXdMYONvlYCbwwsOFskIO

このとき、パスワードの設定には /usr/sbin/slappasswd コマンドを使用します。

# /usr/sbin/slappasswd
New password:
Re-enter new password:
{SSHA}TmTx+14ulaXYXdMYONvlYCbwwsOFskIO
モジュールパスの指定

モジュールのパスとロードに関する指定を行います。

modulepath      /usr/lib/openldap
moduleload      back_bdb.la

ティップ

Turbolinux 11 Server 64 ビット版の場合には以下のようになっています。

modulepath  /usr/lib64/openldap
moduleload      back_bdb.la
アクセスコントロールの設定

Development グループ用の認証サーバーの設定を行います。LDAP サーバーのアクセス制御には、TCP Wrappers ライブラリによるホストベースのアクセス制御とデータ属性(cn や uid 等)ベースのアクセス制御とに大別することができます。データ属性のアクセス制御は /etc/openldap/slapd.conf に access 指定子を記述することで行います。今回のケースでは、以下のようなルールが必要になります。

  • 全ての人は userPassword 属性を除いた属性が読める

  • ベース DN の管理者(cn=Manager,dc=example,dc=com)は全ての属性を読み書きできる

  • 自身の userPassword 属性は読み書きできる

このようなルールの場合、/etc/openldap/slapd.conf に以下のような access 指定子を追記します。

access to *
  by self write
  by dn="cn=Manager,dc=example,dc=com" write
  by * read

access to attr=userPassword
  by self write
  by dn="cn=Manager,dc=example,dc=com" write
  by anonymous auth
  by * none
LDAP サーバーの起動と /etc/hosts.allow の設定

/etc/openldap/slapd.conf の編集完了後、slapd デーモンを起動します。

# /etc/init.d/ldap start

何も問題がなければ [OK] と表示されます。設定に問題があれば /var/log/messages にログが書き込まれますので確認してください。また、slapd は、TCP Wrappers ライブラリを利用したアクセス制御が働いていますので、アクセスを許可するクライアントを /etc/hosts.allow に指定する必要があります。

slapd : 192.168.0.

これで、LDAP サーバーの設定は完了です。以下のコマンドを実行して LDAP サーバーを起動します。

# /etc/init.d/ldap start

18.5.2. LDIF の作成と LDAP サーバーへの追加

起動した LDAP サーバーにエントリを登録します。登録する手順は以下の通りです。

  1. 各エントリの LDIF ファイルを作成

  2. ldapadd コマンドにて LDIF ファイルを LDAP サーバーに登録

  3. ldapsearch コマンドで確認

エントリは、ldapadd コマンドで直接コンソールから登録することも可能ですが、ここでは、LDIF(LDAP Data Interchange Format)と呼ばれるテキストファイルにエントリを記述し、その後 ldapadd コマンドで登録します。LDIF ファイルの書式は以下の通りです。

# comment
dn: distinguished name
attrdesc: attrvalue
attrdesc: attrvalue

# comment
dn: distinguished name
attrdesc: attrvalue
attrdesc: attrvalue

 ...

# の行はコメントとして扱われます。エントリの先頭には、このエントリの識別名となる DN(distinguished name)を指定します。attrdescattrvalue には、オブジェクトクラスの指定と、そのエントリに登録する属性、および属性値を指定します。また、1 つのファイルに複数のエントリを記述することも可能です。空白行はエントリが終了し、次のエントリが開始することを意味します。このフォーマットに従い、最初にディレクトリツリーの基点となるベース DN と管理者のエントリを登録します。以下の内容を記述し、suffix.ldif ファイルとして保存します。

dn: dc=example,dc=com
objectClass: dcObject
objectClass: organization
dc: example
o: Example Corporation

dn: cn=Manager,dc=example,dc=com
objectClass: organizationalRole
cn: Manager

作成した suffix.ldif ファイルを LDAP サーバーに登録するには、以下のように ldapadd コマンドを実行します。

$ ldapadd -h localhost -x -W -D "cn=Manager,dc=example,dc=com" -f suffix.ldif

-h オプションは、LDAP サーバーのホスト名の指定です。ローカルホストで実行するのであれば指定する必要はありません。-x オプションは SASL の代わりに簡易認証を使うことを指定しています。OpenLDAP は、SASL を使用したより強固な認証システムを利用できますが、この例では使用していません。その場合、-W オプションを指定して、エントリーの登録時に認証プロンプトが表示されるようにします。-D オプションは、この操作をどのユーザー権限で実行するのかを指定するためのものです。/etc/openldap/slapd.conf の rootdn で指定した cn=Manager,dc=example,dc=com を指定します。最後に -f オプションで LDIF ファイルを指定しています。

次に、各グループ情報のエントリを登録するために、以下の内容を記述した busho.ldif ファイルを作成します。

dn: ou=Sales,dc=example,dc=com
objectClass: organizationalUnit
ou: Sales

dn: ou=Development,dc=example,dc=com
objectClass: organizationalUnit
ou: Development

同様に ldapadd コマンドで LDAP サーバーに登録します。

$ ldapadd -h localhost -x -D "cn=Manager,dc=example,dc=com" -W -f busho.ldif

次に Sales グループに所属する人員を登録します。各個人の LDIF ファイルの内容は、以下のようになります。この例では、エントリの識別名 DN に uid 属性を使用していますが、mail など他の属性を使用して DN を定義することも可能です。ただし、DN は一意でなければならないことに注意してください。例えば、cn(氏名)、sn(姓)、givenName(名)などの属性を使用すると、同姓同名の人員が存在する場合に一意な DN の指定が困難になります。

dn: uid=hayashi,ou=Sales,dc=example,dc=com
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: inetOrgPerson
uid: hayashi
cn: 林 一朗
sn: 林
givenName: 一朗
mail: hayashi@example.com

上記のように日本語が含まれている LDIF ファイルを登録する場合には、LDIF ファイルが UTF-8 エンコーディングされている必要があります。Turbolinux 11 Server のデフォルトは UTF-8 です。

$ ldapadd -h localhost -x -D "cn=Manager,dc=example,dc=com" -W -f sales-people-hayashi-utf8.ldif

次に Development グループに所属する人員を登録します。Development グループの人員は認証情報も必要となりますので、LDIF ファイルは以下のようになります。オブジェクトクラスに posixAccount と shadowAccount を指定することで、これらの属性を使用できるようになります。

dn: uid=taro,ou=Development,dc=example,dc=com
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
uid: taro
cn: 鈴木 太郎
sn: 鈴木
givenName: 太郎
mail: taro@example.com
shadowLastChange: 11414
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 2000
gidNumber: 2000
homeDirectory: /home/taro
userPassword: {SSHA}WAAvvjkaGJFNhneiOT3WXwKwN+aCFtRI

userPassword の設定は、rootpw で使用した /usr/sbin/slappasswd コマンドが利用できます。編集後、エントリを登録します。

$ ldapadd -h localhost -x -D "cn=Manager,dc=example,dc=com" -W -f development-people-suzuki-utf8.ldif

18.5.3. 基本的な LDAP コマンドの使用方法

エントリの検索

LDAP サーバーに登録したエントリを確認します。登録したエントリの情報を検索するには ldapsearch コマンドを使用します。書式は以下の通りです。

ldapsearch [-h localhost] -x -b searchbase filter

-b searchbase オプションでは、どのツリー以下を検索対象とするのかを指定します。searchbase には DN を指定します。filter には、検索条件を指定します。例えば、先ほど登録したエントリを全て確認するには以下のようにコマンドを実行します。

$ ldapsearch -x -b "dc=example,dc=com" "(objectclass=*)"

ldapsearch を使用して検索を行う場合には "どのツリー" から "何を" 検索するかを指定します。objectclass=* が検索条件です。全てのエントリは必ず何らかの objectclass を持っているため全データが検索されることになります.

Sales グループに所属する人員を全て検索するには以下のコマンドを実行します。

$ ldapsearch -x -b "ou=Sales,dc=example,dc=com" "(uid=*)"

全エントリから属性 mail が taro ではじまる人員を検索するには以下のコマンドを実行します。

$ ldapsearch -x -b "dc=example,dc=com" "(mail=taro*)"
エントリの修正

エントリのデータを変更するには、LDIF ファイルを修正し、ldapadd コマンドのときと同じように ldapmodify コマンドを実行するだけです。

$ ldapmodify -x -D "cn=Manager,dc=example,dc=com" -W -f sample.ldif
エントリの削除

エントリを削除するには、以下のように削除したい DN を指定して ldapdelete コマンドを実行します。

$ ldapdelete -x -D "cn=Manager,dc=example,dc=com" "uid=2000,dc=example,dc=com" -W

ただし、削除できるのはこのエントリがディレクトリツリーの最後に位置している場合に限られます。指定した DN より下のツリーを再帰的にすべて削除するには -r オプションを指定します。例えば、以下のように指定すれば、すべてのエントリは削除されます。

$ ldapdelete -x -D "cn=Manager,dc=example,dc=com" "dc=example,dc=com" -W -r

18.5.4. LDAP クライアントの設定

LDAP サーバーを認証サーバーとして構築した場合の LDAP クライアント側の設定方法について解説します。クライアント側の設定は、authconfig-tui というツールを使用することで、簡単に設定することができます。root ユーザで以下のコマンドを実行して authconfig を起動します。

# miniuni
# authconfig-tui

ティップ

コンソールから authconfig-tui を起動した場合、標準の表示言語は英語です。日本語で表示するには、miniuni コマンドを実行したあとに authconfig を実行します。

以下の画面が表示されます。

"LDAP を使用" 、"LDAP 認証を使用" にチェックを入れ[次へ]ボタンを押します。

"サーバー" と "ベース DN" を設定します。ここでは、LDAP サーバーの IP アドレスを 192.168.0.10、ベース DN を dc=example,dc=com としています。[OK]を押して authconfig-tui を終了します。

ティップ

次のように実行するとコマンドベースで同様の設定をします。

# authconfig --enableldap --enableldapauth --ldapserver=192.168.0.10 \
  --ldapbasedn="dc=example,dc=com" --update

--ldapserver= には、LDAP サーバーの IP アドレスを、--ldapbasedn には、ベース DN を "dc=example,dc=com" のように指定します。

これにより、LDAP クライアントの設定ファイル /etc/openldap/ldap.conf と pam_ldap の設定ファイル /etc/ldap.conf が以下のように編集されます。

uri ldap://192.168.0.10/
base dc=example,dc=com

/etc/nsswitch.conf には、LDAP サーバーを参照するように ldap の文字が追記されます。

passwd:     files nisplus ldap
shadow:     files nisplus ldap
group:      files nisplus ldap

また、LDAP の認証モジュールである pam_ldap を使用するように、PAM(Pluggable Authentication Module)の設定ファイル /etc/pam.d/system-auth が以下のように編集されます。

#%PAM-1.0
# This file is auto-generated.
# User changes will be destroyed the next time authconfig is run.
auth        required      pam_env.so
auth        sufficient    pam_unix.so nullok try_first_pass
auth        requisite     pam_succeed_if.so uid >= 500 quiet
auth        sufficient    pam_ldap.so use_first_pass
auth        required      pam_deny.so

account     required      pam_unix.so broken_shadow
account     sufficient    pam_localuser.so
account     sufficient    pam_succeed_if.so uid < 500 quiet
account     [default=bad success=ok user_unknown=ignore] pam_ldap.so
account     required      pam_permit.so

password    requisite     pam_cracklib.so retry=3
password    sufficient    pam_unix.so md5 shadow nullok try_first_pass use_authtok
password    sufficient    pam_ldap.so use_authtok
password    required      pam_deny.so

session     optional      pam_keyinit.so revoke
session     required      pam_limits.so
session     [success=1 default=ignore] pam_succeed_if.so service in crond quiet
use_uid
session     required      pam_unix.so
session     optional      pam_ldap.s

以上で設定は完了です。 id taro と実行して taro ユーザーが認識されていることを確認してください。

$ id taro
uid=2000(taro) gid=2000 groups=2000

また、必要に応じて以下の設定を LDAP クライアントで行ってください。

ユーザーのホームディレクトリを自動作成する

LDAP サーバーで管理しているユーザーが LDAP クライアントにログインをすると、ホームディレクトリが存在しないためエラーが表示されます。初回ログイン時に、ユーザーのホームディレクトリを自動的に作成したい場合は、以下の行を /etc/pam.d/system-auth ファイルの最後に追記してください。

session    optional    pam_mkhomedir.so skel=/etc/skel umask=022
LDAP クライアントに ssh ログインする

OpenSSH サーバーは PAM 認証をデフォルトで no に設定していますので、LDAP クライアント上で動作している SSH サーバーに他のリモートホストからログインする場合は、/etc/ssh/sshd_config の以下の行を yes に変更し、SSH サーバーを再起動する必要があります。

UsePAM yes

18.5.5. アドレス帳からの検索

ここでは、LDAP サーバーに登録した情報を一般的な電子メールクライアントである Netscape と Microsoft Outlook Express のアドレス帳で検索するまでの手順を示します。

注意

アドレス帳が使用する属性は、メールクライアントに依存して異なります。mail や cn といった一般的な属性は多くのクライアントで使用されていますが、検索結果をアドレス帳の項目に表示するには、各メールクライアントがどのような属性を使用しているかを調査しなければなりません。そして、その属性を含むスキーマファイルを定義し、エントリに属性を指定する必要があります。

18.5.5.1. Thunderbird を使用する場合

  1. "ウィンドウ" -> "アドレス帳" を選択します。

  2. 「アドレス帳」ウィンドウで "ファイル" -> "新規作成" -> "LDAP ディレクトリ" を選択し、次の「ディレクトリサーバのプロパティ」の "一般" タブを表示します。

  3. 以下の項目を指定し[OK]ボタンをクリックします。

    名前

    アドレス帳に設定する任意の識別名です。

    ホスト名

    LDAP サーバーのホスト名または IP アドレスを指定します。

    ベースDN

    検索する LDAP サーバーのベース DN を指定します。

  4. Thunderbird のアドレス帳に "名前" で指定したアドレス帳が表示されます。

  5. "次の文字列を含む名前または電子メールアドレス" で LDAP サーバーで管理しているエントリを検索することができます。

18.5.5.2. Microsoft Outlook Express を使用する場合

  1. "ツール" -> "アカウント" を選択します。

  2. 「インターネットアカウント」画面の "ディレクトリサービス" タブを表示します。

  3. 右側の "追加" -> "ディレクトリサービス" を選択すると「インターネット接続ウィザード」が表示されます。

  4. "インターネットディレクトリ(LDAP)サーバ" に LDAP サーバーのホスト名を指定し、[次へ]ボタンをクリックします。

  5. 「電子メールアドレスのチェック」画面で[はい]または[いいえ]を選択し[次へ]ボタンを クリックします。

  6. 「設定完了」画面で[完了]ボタンをクリックして「インターネットアカウント」画面に戻ります。

  7. 作成したディレクトリをダブルクリックし、「プロパティ」を表示します。

  8. "サーバー名" に LDAP サーバーのホスト名を入力します。

  9. "詳細設定" タブをクリックします。

    "検索ベース" に LDAP サーバーのベース DN を入力し、[OK] ボタンをクリックします。

  10. LDAP サーバーから情報を検索するには、アドレス帳を表示し[人の検索]ボタンをクリックします。

  11. [探す場所]から LDAP サーバーのホスト名を選択し、検索条件を指定します。

  12. [検索開始]ボタンをクリックします。LDAP サーバーに問い合わせを行い、マッチしたユーザー情報が表示されます。