LDAPに関して
LDAPとは、Lightweight Directory Access Protocolの略。簡単に言うと、システムを使うユーザ情報をディレクトリサービスとして一元管理できるデータベースである。ディレクトリと呼ばれる情報の蓄積場所からキーに値する情報を取り出す仕組みである。
例えば、LDAPを用いることでLinux/Windowsアカウントの統合管理だったり、アプリユーザの一元管理だったりを実現できる。サーバ数やユーザ数が少なければそこまで必要はないかもしれないが、数が増えてくるに従って、統合管理のメリットが生まれてくる。
LDAPディレクトリ構成
直感的なイメージ的には図が分かりやすい。必ず出てくる概念としては以下。
※各項目が複数あっても別に良い。
- dc: Domain Component。LDAPディレクトリ階層上の一番上に当たる部分。例えば、example.comであればcomが上位階層でその下にexampleがくる。
- ou: Organization Unit。組織単位(OU)
- cn: ユーザ名・グループ名等
LDAPによりディレクトリサービスにアクセスする際に対象を指定する手段としてはLDAP識別名(DN)を利用する。LDAP識別名(DN)は、上記のような階層情報をsitakaraカンマ区切りで並べて表現する。例えば上記図のuid=user01を指定する場合は、cn=user01,cn=org01,ou=Group,dc=abc,dc=def,dc=com
と指定する。
また、ベースDNという概念もあり、これはLDAPユーザを追加削除等していく場合にどの配下にユーザがいるかを指定するもの。LDAPではこれをdc部分で表現する。上記図の場合、ベースDNはdc=abc,dc=def,dc=com
。このベースDNに対して各種の更新を行うための管理者ユーザ(管理者DN)を配置する際は、cd=Manager,dc=abc,dc=def,dc=com
とする。管理者DNは実際にLDAPが管理対象とするユーザではなく、あくまでLDAPの特定ドメイン内での管理アカウントという位置付け。
参考
https://www.tanchallenge-glory40.com/ldap_basedn/
LDAP構築(初期インストール〜cn登録まで)
初期インストール〜cn登録まで簡単に構築してみる。その後の他ミドルなどとの連携はまた別の機会にでも。
1.インストール&起動
yum -y install openldap openldap-clients openldap-servers
systemctl enable slapd --now
2.LDAPサーバのadminパスワード設定
### 任意のパスワードを入れてハッシュ化した文字列を生成。後続で利用
slappasswd
### LDAP定義ファイル作成。生成したadminパスワードをolcRootPWに指定
vi chrootpw.ldif
dn: olcDatabase={0}config,cn=config
changetype: modify
add: olcRootPW
olcRootPW: {SSHA}xxxxxxxxxxxxxxxxxxxxxxxx
# 反映
ldapadd -Y EXTERNAL -H ldapi:/// -f chrootpw.ldif
3.基本的スキーマの読み込み
/etc/openldap/schemaにデフォルトで用意されているスキーマファイルがあるが、最初はcoreしか読み込まれていない。基本的なスキーマを読み込んでおく。
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/cosine.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/nis.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/inetorgperson.ldif
4.Suffixの更新
デフォルトのサフィックスはdc=my-domain,dc=comのため、自身のものに変更
### 任意のパスワードを入れてハッシュ化した文字列を生成。後続で利用
slappasswd
### LDAP定義ファイル作成。ベースDNと管理者DN作成用。ベースDNは、dc=example,dc=comとここではする
vi chdomain.ldif
dn: olcDatabase={1}monitor,cn=config
changetype: modify
replace: olcAccess
olcAccess: {0}to * by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth"
read by dn.base="cn=Manager,dc=example,dc=com" read by * none
dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcSuffix
olcSuffix: dc=example,dc=com
dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcRootDN
olcRootDN: cn=Manager,dc=example,dc=com
dn: olcDatabase={2}hdb,cn=config
changetype: modify
add: olcRootPW
olcRootPW: {SSHA}xxxxxxxxxxxxxxxxxxxxxxxx
### 反映
ldapmodify -Y EXTERNAL -H ldapi:/// -f chdomain.ldif
5.ベースDN(dc)/グループ(ou)/管理者DNを設定
### LDAP定義ファイル作成。ベースDNと管理者DN作成用。ベースDNは、dc=example,dc=comとここではする
vi add_basedn.ldif
dn: dc=example,dc=com
objectClass: top
objectClass: dcObject
objectclass: organization
o: Example Inc.
dc: example
dn: cn=Manager,dc=example,dc=com
objectClass: organizationalRole
cn: Manager
dn: ou=users,dc=example,dc=com
objectClass: organizationalUnit
ou: users
### 反映
ldapadd -x -D cn=Manager,dc=example,dc=com -W -f add_basedn.ldif
Enter LDAP Password: ---> Managerのパスワードを入れて認証
確認
ldapsearch -x -LLL -D "cn=Manager,dc=example,dc=com" -W -b "ou=users,dc=example,dc=com"
Enter LDAP Password:
dn: ou=users,dc=example,dc=com
objectClass: organizationalUnit
ou: users