# Ldap search not succeding in a single attempt

**URL:** <https://www.ruby-forum.com/t/ldap-search-not-succeding-in-a-single-attempt/112314>\
**Category:** Ruby\
**Created:** [August 17, 2007, 11:41am UTC](https://www.ruby-forum.com/t/ldap-search-not-succeding-in-a-single-attempt/112314 "2007-08-17T11:41:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 17, 2007, 11:41am UTC](https://www.ruby-forum.com/t/ldap-search-not-succeding-in-a-single-attempt/112314/1 "2007-08-17T11:41:26Z")

</div>

Hi,  
I am writing a code for user authentication from ldapserver in ruby.  
For that first i am doing an anonymous search on bluepages  
ldap\_con = Net::LDAP.new  
ldap\_con.host = ‘[X.Y.com](http://X.Y.com)’  
ldap\_con.port = 389  
treebase = “ou=X,[o=Y.com](http://o=Y.com)”  
user\_filter = Net::LDAP::Filter.eq(“notesShortName”,username)  
dn = String.new  
@res = ldap\_con.search( :base =\> treebase, :filter =\>  
user\_filter) do |entry|  
dn = entry.dn

then getting “dn” and after that doing authentication with passwd and  
dn.

```
unless dn.empty?
ldap_con.auth dn,password
if ldap_con.bind
  login_succeeded = true

```

But I am getting strange results. Some times the ldap search was  
successful some times not. Then i tried with putting the ldap search  
in a for loop and tried also to get the return code so I found like

#  
#  
#

I dont know why it is happening for same search( same parameters)

Can you help me in this( why the search is not successful in first  
iteration) ?  
I also wants to tell you few Things  
1- First of all i was testing(doing anonymous ldap search) with  
[A.Y.com](http://A.Y.com) and there was no problem with  
this server at all or i can say 100% results i got if intranetid/  
passwd were correct.  
2- I tried on command line ( “cmd” on windows ) with this  
ldapsearch -h [A.Y.com](http://A.Y.com) -b [o=Y.com](http://o=Y.com) emailaddress=removed\_email\_address@domain.invalid  
ldapsearch -h [X.Y.com](http://X.Y.com) -b [o=Y.com](http://o=Y.com) emailaddress= removed\_email\_address@domain.invalid  
both were giving me 100 % results.  
3- Then with the help of a ldap person in pune i tried with a ldap  
server here and there also i did not got any problem.

Problem is there only with bluepages and how it can possible that for  
anonymous search it gives  
#  
#  
#

Previously i was suspecting timeout but from the above lines it is  
clear that it is access permission related issue.  
Please help me in resolving this issue.

One thing i forgot that the problem is only with ldapsearch, if we get  
correct “dn” there is no issues i faced in  
authentication means 100 % successful.

Is it possible if i can see the logs on bluepages, that what is  
happening with my anonymous search on bluepages.

Or do you have any idea about it?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 17, 2007, 3:23pm UTC](https://www.ruby-forum.com/t/ldap-search-not-succeding-in-a-single-attempt/112314/2 "2007-08-17T15:23:48Z")

</div>

On 8/17/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> @res = ldap_con.search( :base => treebase, :filter =>
> 
> ```
> 
> But I am getting strange results. Some times the ldap search was  
> iteration) ?  
> server here and there also i did not got any problem.

For one thing, you can try using Net::LDAP#bind\_as, which will do the dn  
lookup and the password binding in one step.

For another, you can try using an actual user account when you do the dn  
lookup.

But as fas as the anonymous access working sometimes and not other  
times,  
that’s hard to understand. I assume that bluepages uses either TDS or  
some  
version of Lotus Notes. I know that Net::LDAP works well on both. What  
happens if you use the IP address of the server rather than the domain  
name,  
[X.Y.com](http://X.Y.com)? Perhaps your DNS has multiple entries, or you are in front of a  
hardware load balancer that is distributing the traffic to more than one  
actual LDAP server.
