note 102653 modified in function.ldap-set-rebind-proc by danbrown

[email protected] Sun, 27 Feb 2011 08:22:59 -0800
Newsgroups php.notes
Message-ID <[email protected]>
I have now spent enough time looking at this issue to say that ldap referrals, at least when trying to add, modify or delete a record on a slave server that correctly gives a referral to a master server, does not work in php. My suggestion is turn turn off ldap referrals and write your own add, modify and delete functions with built in referral handling. Something like this:

<?php
function ldap_referral_add($connection,$add_dn,$Add_entry,$bind_dn,$bind_pw)
	{
	$rconnection = $connection;
	$loop = 10; # max number of referral hops.
	# Turn off normal referrals
	ldap_get_option($connection,LDAP_OPT_REFERRALS,$old_referral_setting)
	do
		{
		$response = ldap_add($rconnection,$dn,$entry);
		# We get a success message:
		if ( $response ) 
			{
			ldap_unbind($rconnection);
			$loop = 0;	# Probably not needed
			ldap_set_option($connection,LDAP_OPT_REFERRALS,$old_referral_setting);
			return true;
			}
		# We get a referral message:
		elseif ( !$response && ldap_errno($rconnection) == 0x0a )
			{
			$new_server_url = $server= preg_replace('!^(ldap://[^/]+)/.*$!', '\\1', $ldap_error($rconnection)); # May need some sanity checking here
			$rconnection = ldap_connect($new_server_url);
			ldap_set_option($rconnection,LDAP_OPT_REFERRALS,0)
			ldap_set_option($rconnection,LDAP_OPT_PROTOCOL_VERSION, 3)
			ldap_bind($rconnection,$bind_dn,$bind_pw);
			$loop = $loop - 1;
			}
		else 
			{
			ldap_unbind($rconnection);
			$loop = 0;	# Probably not needed
			ldap_set_option($connection,LDAP_OPT_REFERRALS,$old_referral_setting);
			return false;
			}
		} while ( $loop > 0);
	}
?>

--was--
I have now spent enough time looking at this issue to say that ldap referrals, at least when trying to add, modify or delete a record on a slave server that correctly gives a referral to a master server, does not work in php. My suggestion is turn turn off ldap referrals and write your own add, modify and delete functions with built in referral handling. Something like this:

function ldap_referral_add($connection,$add_dn,$Add_entry,$bind_dn,$bind_pw)
	{
	$rconnection = $connection;
	$loop = 10; # max number of referral hops.
	# Turn off normal referrals
	ldap_get_option($connection,LDAP_OPT_REFERRALS,$old_referral_setting)
	do
		{
		$response = ldap_add($rconnection,$dn,$entry);
		# We get a success message:
		if ( $response ) 
			{
			ldap_unbind($rconnection);
			$loop = 0;	# Probably not needed
			ldap_set_option($connection,LDAP_OPT_REFERRALS,$old_referral_setting);
			return true;
			}
		# We get a referral message:
		elseif ( !$response && ldap_errno($rconnection) == 0x0a )
			{
			$new_server_url = $server= preg_replace('!^(ldap://[^/]+)/.*$!', '\\1', $ldap_error($rconnection)); # May need some sanity checking here
			$rconnection = ldap_connect($new_server_url);
			ldap_set_option($rconnection,LDAP_OPT_REFERRALS,0)
			ldap_set_option($rconnection,LDAP_OPT_PROTOCOL_VERSION, 3)
			ldap_bind($rconnection,$bind_dn,$bind_pw);
			$loop = $loop - 1;
			}
		else 
			{
			ldap_unbind($rconnection);
			$loop = 0;	# Probably not needed
			ldap_set_option($connection,LDAP_OPT_REFERRALS,$old_referral_setting);
			return false;
			}
		} while ( $loop > 0);
	}

http://php.net/manual/en/function.ldap-set-rebind-proc.php