LoginSignup
31
28

More than 5 years have passed since last update.

[AWS] RHEL7でOpenLDAP構築

Posted at

はじめに

こんにちは。
今回は、AWS上のRHEL7を使ってOpenLDAPの構築をやってみようと思います。
OpenLDAPインストールからユーザーの追加までやります!
内容は、基本的な部分が多いです。

環境

・Red Hat Enterprise Linux 7.2
・openldap-2.4.40-8.el7.x86_64

目次

  1. OpenLDAPインストール
  2. OpenLDAP管理者パスワードの設定
  3. スキーマの読み込み
  4. サフィックスの設定
  5. ベースDNの登録
  6. ACL設定
  7. ユーザーの登録

1. OpenLDAPインストール

・OpenLDAPのインストール
yum -y install openldap-servers openldap-clients

・データベースの作成

cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
chown ldap. /var/lib/ldap/DB_CONFIG 

・OpenLDAPの起動及び自動起動設定
 LDAPのプロセスは、slapdで、使用ポートは389となります。

systemctl start slapd 
systemctl enable slapd 

・プロセスの起動確認

# ps cax|grep slapd
 9393 ?        Ssl    0:00 slapd

これでインストールは完了です。
OpenLDAPの設定ファイルは、/etc/openldap/slapd.d/cn=config に格納されています。

OpenLDAPの設定を変更するときは、ldifファイルを作成し、LDAPコマンドで読み込ませ設定の変更を行います。
以前のOpenLDAPでは、slapd.confという設定ファイルがあり、それを直接編集していたようですが、現バージョンでは設定ファイルを直接編集することが推奨されていません。

設定ファイルの中にもその旨が記載されています。

# cat olcDatabase\=\{2\}hdb.ldif 
# AUTO-GENERATED FILE - DO NOT EDIT!! Use ldapmodify.

2.OpenLDAP管理者パスワードの設定

LDAPの編集をするために、まずはOpenLDAPの管理者を設定します。

・管理者パスワードを生成
パスワードを2回入力するとSSHAでパスワードが生成されます。

# slappasswd 
New password: 
Re-enter new password: 
{SSHA}xxxxxxxxxxxxxxxxxxxxxxxx

・管理者パスワード変更用のldifファイルを作成
olcRootPWには、slappasswdで生成したSHA値を入力します。

# vi changepw_ldif 
dn: olcDatabase={0}config,cn=config
changetype: modify
add: olcRootPW
olcRootPW: {SSHA}xxxxxxxxxxxxxxxxxxxxxxxx

・管理者パスワードを変更

# ldapadd -Y EXTERNAL -H ldapi:// -f changepw_ldif 
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
modifying entry "olcDatabase={0}config,cn=config"

・変更できているか確認

# cat olcDatabase\=\{0\}config.ldif 

# AUTO-GENERATED FILE - DO NOT EDIT!! Use ldapmodify.
# CRC32 b6be7a4c
dn: olcDatabase={0}config
objectClass: olcDatabaseConfig
olcDatabase: {0}config
olcAccess: {0}to * by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=extern
 al,cn=auth" manage by * none
structuralObjectClass: olcDatabaseConfig
entryUUID: 27ce9190-5a0d-1035-90b8-71715f2b2773
creatorsName: cn=config
createTimestamp: 20160128131658Z
olcRootPW:: xxxxxxxxxxxxxxxxxxxxxxxx   ←変更した値
entryCSN: 20160128224612.693622Z#000000#000#000000
modifiersName: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
modifyTimestamp: 20160128224612Z

3.スキーマの読み込み

OpenLDAPでは、/etc/openldap/schemaにデフォルトで用意されているスキーマファイルがあります。デフォルトでは、coreしか読み込まれていないので、必要に応じてスキーマファイルを読み込みます。読み込むのは、ldifファイルです。
スキーマの解説はこちらの記事を参考にしてください。

# ll /etc/openldap/schema/
合計 308
-r--r--r--. 1 root root  2036  9月 23 11:25 collective.ldif
-r--r--r--. 1 root root  6190  9月 23 11:25 collective.schema
-r--r--r--. 1 root root  1845  9月 23 11:25 corba.ldif
-r--r--r--. 1 root root  8063  9月 23 11:25 corba.schema
-r--r--r--. 1 root root 20612  9月 23 11:25 core.ldif
-r--r--r--. 1 root root 20499  9月 23 11:25 core.schema
-r--r--r--. 1 root root 12006  9月 23 11:25 cosine.ldif
-r--r--r--. 1 root root 73994  9月 23 11:25 cosine.schema
-r--r--r--. 1 root root  4842  9月 23 11:25 duaconf.ldif
-r--r--r--. 1 root root 10388  9月 23 11:25 duaconf.schema
-r--r--r--. 1 root root  3330  9月 23 11:25 dyngroup.ldif
-r--r--r--. 1 root root  3289  9月 23 11:25 dyngroup.schema
-r--r--r--. 1 root root  3481  9月 23 11:25 inetorgperson.ldif
-r--r--r--. 1 root root  6267  9月 23 11:25 inetorgperson.schema
-r--r--r--. 1 root root  2979  9月 23 11:25 java.ldif
-r--r--r--. 1 root root 13901  9月 23 11:25 java.schema
-r--r--r--. 1 root root  2082  9月 23 11:25 misc.ldif
-r--r--r--. 1 root root  2387  9月 23 11:25 misc.schema
-r--r--r--. 1 root root  6809  9月 23 11:25 nis.ldif
-r--r--r--. 1 root root  7640  9月 23 11:25 nis.schema
-r--r--r--. 1 root root  3308  9月 23 11:25 openldap.ldif
-r--r--r--. 1 root root  1514  9月 23 11:25 openldap.schema
-r--r--r--. 1 root root  6904  9月 23 11:25 pmi.ldif
-r--r--r--. 1 root root 20467  9月 23 11:25 pmi.schema
-r--r--r--. 1 root root  4356  9月 23 11:25 ppolicy.ldif
-r--r--r--. 1 root root 19963  9月 23 11:25 ppolicy.schema

・スキーマの読み込み
ldapadd -Y EXTERNAL -H ldapi:/// -f <読み込むスキーマ>.ldif

4.サフィックスの設定

デフォルトのサフィックスはdc=my-domain,dc=comとなっているので、ご自身で設定したい値に変更します。
ここでは、dc=test,dc=localに変更します。

・サフィックス変更用ldifの作成
データベースのパスワードも先ほど出力したパスワードに変更します。

vi changedomain.ldif

dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcSuffix
olcSuffix: dc=test,dc=local

dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcRootDN
olcRootDN: cn=Manager,dc=test,dc=local

dn: olcDatabase={2}hdb,cn=config
changetype: modify
add: olcRootPW
olcRootPW: {SSHA}xxxxxxxxxxxxxxxxxxxxxxxx

・サフィックスの変更

# ldapmodify -Y EXTERNAL -H ldapi:/// -f changedomain.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
modifying entry "olcDatabase={2}hdb,cn=config"

modifying entry "olcDatabase={2}hdb,cn=config"

modifying entry "olcDatabase={2}hdb,cn=config"

・変更ができているか確認

# cat olcDatabase={2}hdb.ldif
# AUTO-GENERATED FILE - DO NOT EDIT!! Use ldapmodify.
# CRC32 5e7441c2
dn: olcDatabase={2}hdb
objectClass: olcDatabaseConfig
objectClass: olcHdbConfig
olcDatabase: {2}hdb
olcDbDirectory: /var/lib/ldap
olcDbIndex: objectClass eq,pres
olcDbIndex: ou,cn,mail,surname,givenname eq,pres,sub
structuralObjectClass: olcHdbConfig
entryUUID: 27cee1ae-5a0d-1035-90ba-71715f2b2773
creatorsName: cn=config
createTimestamp: 20160128131658Z
olcSuffix: dc=test,dc=local               ←変更した値
olcRootDN: cn=Manager,dc=test,dc=local  ←変更した値
olcRootPW:: xxxxxxxxxxxxxxxxxxxxxxxx   ←変更した値
entryCSN: 20160206133820.930227Z#000000#000#000000
modifiersName: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
modifyTimestamp: 20160206133820Z

5.ベースDNの登録

ベースDNを登録していきます。ベースDNは、LDAPの情報を格納する起点となる部分です。このベースDN配下に、ユーザー格納用のOUも作成します。

・ベースDN登録用ldifの作成

vi add_object.ldif

dn: dc=test,dc=local
objectClass: top
objectClass: dcObject
objectclass: organization
o: test.inc
dc: test

dn: cn=Manager,dc=test,dc=local
objectClass: organizationalRole
cn: Manager

dn: ou=users,dc=test,dc=local
objectClass: organizationalUnit
ou: users

・ベースDNの追加

ldapadd -x -D cn=Manager,dc=test,dc=local -W -f add_object.ldif

6.ACL設定

ACLの設定をします。すべてのユーザーにパスワード等への読み書き権限があると危険なので、閲覧制限や書き込み制限などをACLを使って行います。

olcAccess{0}:管理者はすべてのパスワード属性への書き込みを許可。自分自信のパスワード属性への書き込みを許可。匿名ユーザーは認証のみ許可。その他のユーザーはすべて拒否。
olcAccess{1}:管理者はすべての領域の書き込み権限。その他のユーザーは、読み込みを許可。

・ACL登録用ldifの作成

#vi add_ACL.ldif

dn: olcDatabase={2}hdb,cn=config
changetype: modify
add: olcAccess
olcAccess: {0}to attrs=userPassword by
  dn="cn=Manager,dc=test,dc=local" write by anonymous auth by self write by * none
olcAccess: {1}to * by dn="cn=Manager,dc=test,dc=local" write by * read

・ACLの登録
ldapadd -x -D cn=Manager,dc=test,dc=local -W -f add_ACL.ldif

7.ユーザーの登録

ユーザーと登録します。先ほど作ったユーザー格納用のouに追加します。

・ユーザー追加用ldifファイルを作成

dn: uid=user001,ou=users,dc=test,dc=local
objectClass: account
objectClass: posixAccount
objectClass: top
uid: user001
cn: user001
uidNumber: 1001
gidNumber: 1001
homeDirectory: /home/user001
loginShell: /bin/bash

・ユーザーを追加

# ldapadd -x -D cn=Manager,dc=test,dc=local -W -f add_user.ldif 
Enter LDAP Password: 
adding new entry "uid=user001,ou=users,dc=test,dc=local"

・ユーザーが追加できていることを確認

#ldapsearch -x -D cn=Manager,dc=test,dc=local -W -b ou=users,dc=test,dc=local

~~省略~~
# users, test.local
dn: ou=users,dc=test,dc=local
objectClass: organizationalUnit
ou: users

# user001, users, test.local        ←追加したユーザー
dn: uid=user001,ou=users,dc=test,dc=local
objectClass: account
objectClass: posixAccount
objectClass: top
uid: user001
cn: user001
uidNumber: 1001
gidNumber: 1001
homeDirectory: /home/user001
loginShell: /bin/bash

# search result
search: 2
result: 0 Success

# numResponses: 3
# numEntries: 2

無事ユーザーが追加できていることを確認できました。

さいごに

・マスタスレーブ、マルチマスタの設定や独自属性の登録など、まだまだOpenLDAPでできることは多いです。本番環境で構築するならもっとACLの部分を細かく設定しなければならないと思いました。簡単な構築をやっただけですが、ディレクトリサービスのついての理解が少し深まったと思います。手を動かすのが一番ですね!

以上となります。
ご指摘事項などありましたら、ご連絡よろしくお願いします。

参考

http://www.server-world.info/query?os=CentOS_7&p=openldap&f=1
http://unixlife.jp/linux/centos-6/openldap-schema-2.html
http://www.server-world.info/query?os=CentOS_7&p=openldap&f=2

参考にさせていただきました。ありがとうございます。

31
28
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31
28