PHPを使い、LDAPヘSSLでBIND認証が繋がらないとき(ldap_bind)

この中身で丸一日デバッグしまくり気力が限界に来てましたが。。何とかなった。

環境はPHPで、LDAPサーバへSSL(ポート636)形式でbind認証したい。
ldap rdn」と「pass」はもらっているので、、

<?php
	$ldaprdn  = 'cn=****,o=++++,c=--'; // ldap rdn あるいは dn
	$ldappass = 'password';  // パスワード

	$link_id = ldap_connect("ldaps://ldap.hogehoge.jp",636)
	    or die("Could not connect to LDAP server");

	    $ldapbind = ldap_bind($link_id,$ldaprdn,$ldappass);

	    // バインド結果を検証する
	    if ($ldapbind) {
	        echo "LDAP bind successful...";
	    } else {
	        echo "LDAP bind failed...";
	    }

?>

で接続できるはずが・・・できない。
正確に言えばldap connectは出来るが、bind認証でエラー。

ん?そもそもrdnってユーザ名?あれ、階層みたいに書けばいいんじゃ。。
と混乱し、anonymous(匿名)にしたり色々したり。。で丸一日。。

ググりまくりたどりついたのが以下。
http://www.mail-archive.com/php-windows@lists.php.net/msg22439.html

下流

The client is configured in the following manner:
1) Windows 2003 Server Running IIS
2) PHP 5.0.4 installed
3) LDAP support enabled
   - Uncommented the php_ldap.dll extension
   - Copied the php_ldap.dll file into the appropriate directory
   - Restarted IIS

Using the LDP tool, I was able to connect and bind via ports 389, 636,
and 3269.

Here is the code I am using to attempt the bind:


<?php

$host = "ldaps://server.addomain.domain.com";
$un = "jdoe";
$pw = "password";

$lc = ldap_connect($host);

ldap_set_option($lc, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($lc, LDAP_OPT_REFERRALS, 0);

$lb = ldap_bind($lc, $un, $pw);

ldap_close($lc);
?>

結局、PHPのiniファイルでphp_ldap.dllをコメントアウトしてる?ってところで
ひえー!そういえば。と確認したら案の定。。。。

まさか、、と思い実行したらbind認証できた!
ちなみに文中にある set optionは必須です。つまりプログラムとして正解は

<?php
	$ldaprdn  = 'cn=****,o=++++,c=--'; // ldap rdn あるいは dn
	$ldappass = 'password';  // パスワード

	$link_id = ldap_connect("ldaps://ldap.hogehoge.jp",636)
	    or die("Could not connect to LDAP server");

	ldap_set_option($link_id, LDAP_OPT_PROTOCOL_VERSION, 3) or die("couldn't set protocol version...");
	ldap_set_option($link_id, LDAP_OPT_REFERRALS, 0) or die("couldn't set referrals...");
	
	$ldapbind = ldap_bind($link_id,$ldaprdn,$ldappass);

	    // バインド結果を検証する
	    if ($ldapbind) {
	        echo "LDAP bind successful...";
	    } else {
	        echo "LDAP bind failed...";
	    }

?>

という事に。おおおおお!英語サイト、やはり毛嫌いせずみねば。
OSSのヒントは日本語だけでは限界ありますね。。