Ubuntu10.10にOpenLDAPをインストールしてPHPのLDAP関数を使うまで
調べないと分からなかったことも多いのでまとめておきます。
PHPとApacheがインストールされていることが前提になっています。
OpenLDAPをインストール。
OpenLDAP2.3からはConfiguration Backendという方式になったようなので、設定はすべてLDIFファイルから追加することになります。
$ sudo apt-get install slapd ldap-utils
スキーマを追加。
$ sudo ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/ldap/schema/cosine.ldif $ sudo ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/ldap/schema/nis.ldif $ sudo ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/ldap/schema/inetorgperson.ldif
backendをつくる。
$ sudo gedit /etc/ldap/backend.example.com.ldif
backend.example.com.ldif
dn: cn=module,cn=config objectClass: olcModuleList cn: module olcModulepath: /usr/lib/ldap olcModuleload: back_hdb dn: olcDatabase=hdb,cn=config objectClass: olcDatabaseConfig objectClass: olcHdbConfig olcDatabase: {1}hdb olcSuffix: dc=example,dc=com olcDbDirectory: /var/lib/ldap olcRootDN: cn=Manager,dc=example,dc=com olcRootPW: secret olcDbConfig: set_cachesize 0 2097152 0 olcDbConfig: set_lk_max_objects 1500 olcDbConfig: set_lk_max_locks 1500 olcDbConfig: set_lk_max_lockers 1500 olcDbIndex: objectClass eq olcLastMod: TRUE olcDbCheckpoint: 512 30 olcAccess: to attrs=userPassword by dn="cn=Manager,dc=example,dc=com" write by anonymous auth by self write by * none olcAccess: to attrs=shadowLastChange by self write by * read olcAccess: to dn.base="" by * read olcAccess: to * by dn="cn=Manager,dc=example,dc=com" write by * read
backendを追加する。
$ sudo ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/ldap/backend.example.com.ldif
frontendをつくる。
$ sudo gedit /etc/ldap/frontend.example.com.ldif
frontend.example.com.ldif
dn: dc=example,dc=com objectClass: dcObject objectClass: organization dc: example o: example dn: cn=Manager,dc=example,dc=com objectClass: simpleSecurityObject objectClass: organizationalRole cn: Manager userPassword: secret
frontendを追加する。
$ sudo ldapadd -x -D cn=Manager,dc=example,dc=com -W -f /etc/ldap/frontend.example.com.ldif
backendとfrontendにでてくる "dc=example,dc=com" や "cn=Manager,dc=example,dc=com" は任意のドメイン名を指定。
secret は任意のパスワードを指定。
OpenLDAPに関してはこれでとりあえず完了。
PHPのLDAP関数を使うにはApacheにモジュールが必要なのでインストールしてApache再起動。
$ sudo apt-get install php5-ldap $ sudo service apache2 restart
なお、LDAP関数を使用した際にプロトコルエラーと言われることがあります。
それを回避するには ldap_bind( ) の前にLDAPのバージョンを指定してあげる必要があるとか。
//$linkid は ldap_connect( ) が返すリンクID ldap_set_option($linkid, LDAP_OPT_PROTOCOL_VERSION, 3);
以下のWebサイトを参考にしました。というかほぼコピペして繋げただけです。
OpenLDAP Server
PHP: ldap_set_option - Manual