LDAP

LDAPとは
ディレクトリデータベースへアクセスするためのプロトコルで、ネットワークを利用するユーザ名やマシン名などの様々な情報を管理するためのサービスのことで、ユーザ名などのキーとなる値から様々な情報を検索することが可能です。

今回はCentOSのアカウント情報を管理してみます。

■ インストール

[root@example ~]# yum install openldap*
※ 「*」は必ずつけてください。

■ 設定
LDAPのサーバ設定ファイルを編集します。

設定ファイル:/etc/openldap/slapd.conf

# スキーマ定義ファイルのインクルード
include /etc/openldap/schema/core.schema
include /etc/openldap/schema/cosine.schema
include /etc/openldap/schema/inetorgperson.schema
include /etc/openldap/schema/nis.schema

# データベース形式
database bdb

# 検索を開始するベースDN
# 自サーバーのhostnameを指定
# 例)example.comの場合
suffix "dc=example,dc=com"

# 管理者のDN(LDAPサーバ管理用)
rootdn "cn=Manager,dc=example,dc=com"

# 管理者用パスワード
rootpw sample

# LDAPデータベースの格納ディレクトリ
directory /var/lib/ldap

# データベースのインデックス設定
index objectClass eq,pres
index ou,cn,mail,surname,givenname eq,pres,sub
index uidNumber,gidNumber,loginShell eq,pres
index uid,memberUid eq,pres,sub
index nisMapName,nisMapEntry eq,pres,sub

# パスワードの閲覧制限
access to attrs=userPassword
by dn="cn=Manager,dc=example,dc=com" write
by self write
by anonymous auth
access to *
by dn="cn=Manager,dc=example,dc=com" write
by * read

LDAPクライアント用設定ファイルを編集します。

LDAPコマンド用設定ファイルです。
設定ファイル:/etc/openldap/ldap.conf


# BASEを探して、自サーバーのhostnameを指定します。
BASE dc=example,dc=com

NSS(Name Service Switch)用設定ファイルです。
設定ファイル:/etc/ldap.conf


# baseを探して、自サーバーのhostnameを指定します。
base dc=example,dc=com

起動します。

[root@example ~]# /etc/rc.d/init.d/ldap start

OS起動時に自動起動するように設定します。

[root@example ~]# chkconfig ldap on

■ パスワードの暗号化
パスワードが平文のままだと、読み取られる可能性があります。
今回はSSHAで暗号化します。

slappasswd
New password: # パスワード入力
Re-enter new password: # 確認用パスワード入力
{SSHA}1sdBZ1IwtVn6S39ZRB9KasXPpQsFIm99 # 暗号化されました。

LDAPのサーバ設定ファイルのrootpwに暗号化したパスワードを貼り付けましょう。

設定ファイル:/etc/openldap/slapd.conf


rootpw {SSHA}1sdBZ1IwtVn6S39ZRB9KasXPpQsFIm99

ldapを再起動します。

[root@example ~]# service ldap restart

■ 初期ユーザの追加
LDIFファイルを作成します。
ファイル:base.ldif(任意の場所に作成)
dn: dc=example,dc=com
objectClass: dcObject
objectClass: organization
o: example Organization
dc: example


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


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


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

LDIFに記述したユーザを追加します。

[root@example ~]# ldapadd -x -D "cn=Manager,dc=example,dc=com" -W -f base.ldif
Enter LDAP Password: # slappasswdで入力したパスワードを入力(slapd.confのrootpwのハッシュ化されたものではない)

マイグレーションツールの設定
マイグレーションを使用すれば、既存のユーザのLDIFファイルを簡単に作成することが出来ます。

設定ファイル:/usr/share/openldap/migration/migrate_common.ph


# メールのドメインを設定
$DEFAULT_MAIL_DOMAIN = "example.com";

# ベースDNを設定(自サーバーのhostname)
$DEFAULT_BASE = "dc=example,dc=jp";

■ 既存ユーザーの追加
マイグレーションを使用して既存のユーザを追加します。

ユーザ情報のLDIFファイルを作成します。
[root@example ~]# /usr/share/openldap/migration/migrate_passwd.pl /etc/passwd > users.ldif

グループ情報のLDIFファイルを作成します。
[root@example ~]# /usr/share/openldap/migration/migrate_group.pl /etc/group > groups.ldif

LDAPにユーザ情報を追加します。
[root@example ~]# ldapadd -x -D "cn=Manager,dc=example,dc=com" -W -f users.ldif

LDAPにグループ情報を追加します。
[root@example ~]# ldapadd -x -D "cn=Manager,dc=example,dc=com" -W -f groups.ldif