I finally have referrals working using the ldap_set_rebind_proc function. Don't connect to the referral server in your callback function. This is done for you. You only have to bind. The callback must return 0 if the bind succeeds or 1 if it fails.
Consider a master - slave LDAP setup where the slave is read-only and refers writes to the master. For the PHP on the slave, you need something like this:
<?php
function rebind($ldap, $referral) {
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap, LDAP_OPT_REFERRALS, True);
ldap_set_rebind_proc($ldap, 'rebind');
$refparts = explode('/', $referral);
if (count($refparts) > 2) {
$dn = $refparts[3];
if (!ldap_bind($ldap, $dn, $pass)) {
echo 'Could not bind to referral server';
return 1;
}
} else {
if (!ldap_bind($ldap)) {
echo 'Could not bind to referral server anonymously';
return 1;
}
}
return 0;
}
$ldap_host = 'localhost'
$ds = ldap_connect($ldap_host);
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3)
ldap_set_option($ds, LDAP_OPT_REFERRALS, True)
ldap_set_rebind_proc($ds, 'rebind'))
ldap_bind($ds, $dn, $pass)
ldap_modify($ds, $dn, $attr);
?>
Accessing passwords and other data from your callback is easier if you use a class method as the callback function. The callback would be initialized like this:
<?php
ldap_set_rebind_proc($ldap, 'MyClass::rebind');
?>