LoginSignup
9
8

More than 5 years have passed since last update.

LDAPサーバーOpenDJを使おう(2)

Last updated at Posted at 2014-05-13

LDAPサーバーOpenDJを使おう(1) の続きです。

REST API が気になって書きはじめましたが今回はまだ REST は出てきません。
control-panel がリモートから使えないので今回は OpenDJ での基本操作などを。control-panel を使うとコマンドラインで実行する方法も表示してくれるので大変便利です。dsconfig コマンドも対話型で選択していった後にその操作をするためのコマンドラインオプションを表示することができてこっちも便利です。

前回の続きなので、LDAP リポジトリには example.com ドメインに People OU があり、user.0, user.1, user.2, user.3, user.4 だけが存在します。
setup コマンドがサンプルとして登録したデータです。

ユーザーの追加

LDIF ファイルを作成し、ldapmodify コマンドを使って登録します。
後で使うので admin というユーザーを追加してみます。

admin.ldif
dn: uid=admin,ou=People,dc=example,dc=com
objectClass: person
objectClass: inetorgperson
objectClass: organizationalperson
objectClass: top
userPassword: password
mail: admin@example.com
givenName: Taro
uid: admin
cn: Administrator
sn: Admin
$ /opt/opendj/bin/ldapmodify \
    --hostname localhost \
    --port 1389 \
    --bindDN cn=Directory\ Manager \
    --bindPassword password \
    --defaultAdd \
    --filename /vagrant/admin.ldif

--defaultAdd を指定していることで dn が存在しない場合に追加されます。

ldapmodify の代わりに import-ldif を使って追加、更新を行うこともできますが replication 環境で行うと再同期処理が必要になります。

$ /opt/opendj/bin/import-ldif \
    --bindPassword password \
    --trustAll \
    --backendID userRoot \
    --append \
    --replaceExisting \
    --ldifFile /vagrant/admin.ldif

ユーザー情報の編集

編集も ldapmodify を使います。

LDIF ファイルには

  • 対象の dn
  • オペレーションタイプ
    • add
    • delete
    • modify
    • moddn
    • modrdn
  • 更新アトリビュート

だけを書きます。

admin-modify.ldif
dn: uid=admin,ou=People,dc=example,dc=com
changetype: modify
replace: cn
cn: Super Administrator

ユーザーの削除

$ echo 'uid=user.1,ou=People,dc=example,dc=com' | \
  /opt/opendj/bin/ldapdelete \
   --hostName localhost \
   --port 1636 \
   --bindDN cn=Directory\ Manager \
   --bindPassword password \
   --trustAll \
   --useSSL \
   --noPropertiesFile 

--noPropertiesFile を指定すると標準入力から読み込んで処理します。
複数まとめて流しこむことも可能です。この方法は作成とか、編集などでも使えます。

uid=user.1,ou=People,dc=example,dc=com
uid=user.2,ou=People,dc=example,dc=com
uid=user.3,ou=People,dc=example,dc=com
uid=user.4,ou=People,dc=example,dc=com

グループ操作

グループを登録

Groups OU の作成と、そこへの sales グループの追加を行います。これも LDIF ファイル作って ldapmodify です。

create-groups-ou.ldif
dn: ou=Groups,dc=example,dc=com
objectClass: organizationalUnit
objectClass: top
ou: Groups

dn: cn=sales,ou=Groups,dc=example,dc=com
objectClass: groupOfUniqueNames
objectClass: top
description: Sales Group
cn: sales
$ /opt/opendj/bin/ldapmodify \
    --hostname localhost \
    --port 1389 \
    --bindDN cn=Directory\ Manager \
    --bindPassword password \
    --defaultAdd \
    --filename /vagrant/create-groups-ou.ldif

グループにメンバーを追加する

add-member.ldif
dn: cn=sales,ou=Groups,dc=example,dc=com
changetype: modify
replace: uniquemember
uniquemember: uid=user.0,ou=People,dc=example,dc=com
uniquemember: uid=user.1,ou=People,dc=example,dc=com
uniquemember: uid=user.2,ou=People,dc=example,dc=com
uniquemember: uid=user.3,ou=People,dc=example,dc=com
$ /opt/opendj/bin/ldapmodify \
    --hostname localhost \
    --port 1389 \
    --bindDN cn=Directory\ Manager \
    --bindPassword password \
    --defaultAdd \
    --filename /vagrant/add-member.ldif

グループからメンバーを外す

delete-member.ldif
dn: cn=sales,ou=Groups,dc=example,dc=com
changetype: modify
delete: uniquemember
uniquemember: uid=user.3,ou=People,dc=example,dc=com
$ /opt/opendj/bin/ldapmodify \
    --hostname localhost \
    --port 1389 \
    --bindDN cn=Directory\ Manager \
    --bindPassword password \
    --defaultAdd \
    --filename /vagrant/delete-member.ldif

削除後のメンバーが削除するメンバーの数よりも少ない場合は、メンバーとして残るリストをつけて replace: uniquemember とすると効率が良いです。

replace-member.ldif
dn: cn=sales,ou=Groups,dc=example,dc=com
changetype: modify
replace: uniquemember
uniquemember: uid=user.o,ou=People,dc=example,dc=com

バックアップ

オンラインバックアップ (backendID 指定)

$ /opt/opendj/bin/backup \
 --port 4444 \
 --bindDN "cn=Directory Manager" \
 --bindPassword password \
 --backendID userRoot \
 --backupDirectory /var/tmp/opendj-backup-1 \
 --start 0

--start では YYYYMMDDHH24MISS でバックアップジョブの開始日時を指定できます。0 を指定すると即時実行となります。

同じ backupDirectory を指定してバックアップすると backup.info に歴代の情報がまとめられます。20140512153931Z 部分が Baskup ID でリストア時にはこの ID を指定します。restore コマンドの --listBackups オプションで指定ディレクトリにあるバックアップファイルの情報が確認できます。backup.info ファイルにテキストで書いてある内容ですけど。

$ ls /var/tmp/opendj-backup-1
backup-userRoot-20140512153931Z  backup.info
backup-userRoot-20140513133333Z  backup.info.save
backup-userRoot-20140513133627Z
$ /opt/opendj/bin/restore \
  --listBackups \
  --backupDirectory /var/tmp/opendj-backup-1
Backup ID:          20140512153931Z
Backup Date:        12/May/2014:15:39:31 +0000
Is Incremental:     false
Is Compressed:      false
Is Encrypted:       false
Has Unsigned Hash:  false
Has Signed Hash:    false
Dependent Upon:     none

Backup ID:          20140513133333Z
Backup Date:        13/May/2014:13:33:33 +0000
Is Incremental:     false
Is Compressed:      false
Is Encrypted:       false
Has Unsigned Hash:  false
Has Signed Hash:    false
Dependent Upon:     none

Backup ID:          20140513133627Z
Backup Date:        13/May/2014:13:36:27 +0000
Is Incremental:     false
Is Compressed:      false
Is Encrypted:       false
Has Unsigned Hash:  false
Has Signed Hash:    false
Dependent Upon:     none

オンラインバックアップ (backUpAll)

$ /opt/opendj/bin/backup \
 --port 4444 \
 --bindDN "cn=Directory Manager" \
 --bindPassword password \
 --backUpAll \
 --backupDirectory /var/tmp/opendj-backup-2 \
 --start 0

--backUpAll を指定すると取れるものが全部とれるようです。

userRoot, tasks, schema というサブディレクトリが作成され、そこにバックアップファイルが作成されます。リストア時にはそれぞれのサブディレクトリまで指定する必要があります。

$ find /var/tmp/opendj-backup-2
/var/tmp/opendj-backup-2
/var/tmp/opendj-backup-2/userRoot
/var/tmp/opendj-backup-2/userRoot/backup.info
/var/tmp/opendj-backup-2/userRoot/backup-userRoot-20140512154338Z
/var/tmp/opendj-backup-2/tasks
/var/tmp/opendj-backup-2/tasks/tasks-backup-20140512154338Z
/var/tmp/opendj-backup-2/tasks/backup.info
/var/tmp/opendj-backup-2/schema
/var/tmp/opendj-backup-2/schema/backup.info
/var/tmp/opendj-backup-2/schema/schema-backup-20140512154338Z

オフラインバックアップ (backendID 指定)

整合性のとれたバックアップが取れるでしょう。

$ /opt/opendj/bin/stop-ds
$ /opt/opendj/bin/backup \
  --backendID userRoot \
  --backupDirectory /var/tmp/opendj-backup-3
$ /opt/opendj/bin/start-ds

オフラインバックアップ (backUpAll)

$ /opt/opendj/bin/stop-ds
$ /opt/opendj/bin/backup \
  --backUpAll \
  --backupDirectory /var/tmp/opendj-backup-4
$ /opt/opendj/bin/start-ds

オンラインでの3つのサブディレクトリに加え、config ディレクトリが作成されバックアップが取得されます。

リストア

$ /opt/opendj/bin/restore \
  --hostName localhost \
  --port 4444 \
  --trustAll \
  --bindDN cn=Directory\ Manager \
  --bindPassword password \
  --backupDirectory /var/tmp/opendj-backup-1 \
  --backupID 20140512153931Z

Replication

もう疲れたから OpenDJ – LDAP Server (1) とか、公式ドキュメントをどうぞ :)

あ、この blog に書いた後わかったことですがマルチドメインの場合、ドメイン毎に replication 設定が必要でした。

次回予告

いよいよ REST API を試してみたいと思います

9
8
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
9
8