# Ruby-ldap and paged results on windows

**URL:** <https://www.ruby-forum.com/t/ruby-ldap-and-paged-results-on-windows/112689>\
**Category:** Ruby\
**Created:** [August 22, 2007, 5:15am UTC](https://www.ruby-forum.com/t/ruby-ldap-and-paged-results-on-windows/112689 "2007-08-22T05:15:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![davis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e19adc/32.png) [@davis](https://www.ruby-forum.com/u/davis)\
**Post date:** [August 22, 2007, 5:15am UTC](https://www.ruby-forum.com/t/ruby-ldap-and-paged-results-on-windows/112689/1 "2007-08-22T05:15:28Z")

</div>

Hi-  
I’m trying to get paged results to work with ruby-ldap against  
Active Directory in Windows. I compiled ruby-ldap 0.9.7 following the  
directions on  
[http://www.turnofthecrank.com/2006/09/11/ruby-ldap-and-win32/](http://www.turnofthecrank.com/2006/09/11/ruby-ldap-and-win32/)

Then I tried to run this code I found. The error I get is that ruby  
can’t find the constant LDAP\_CONTROL\_PAGEDRESULTS and if I put  
LDAP.constants it’s not listed. Any ideas?

require ‘ldap’

# Find a PagedResults control in an Array of LDAP controls.

# 

def paged\_results\_ctl( ctls )  
ctls.each do |ctl|  
return ctl if ctl.oid == LDAP::LDAP\_CONTROL\_PAGEDRESULTS  
end

nil  
end

unless ARGV[0]  
$stderr.puts “Please give a page size.”  
exit  
end  
page\_size = ARGV[0].to\_i  
cookie = ‘’  
critical = true

conn = LDAP::Conn.new( ‘[foo.corp.google.com](http://foo.corp.google.com)’, 389 )  
conn.set\_option( LDAP::LDAP\_OPT\_PROTOCOL\_VERSION, 3 )  
conn.bind( ‘user’, ‘password’ )

search = 0  
total = 0

loop do  
ber\_string = LDAP::Control.encode( page\_size, cookie )  
control = LDAP::Control.new( LDAP::LDAP\_CONTROL\_PAGEDRESULTS,  
ber\_string,  
critical )  
conn.set\_option( LDAP::LDAP\_OPT\_SERVER\_CONTROLS, [control] )

count = 0

# In this patched version of ruby-ldap, Conn#search no longer returns

self.

# Instead, it returns an Array of LDAP referrals and controls, each of

# which is also an Array.

conn.search( ‘ou=users,ou=nyc,dc=corp,dc=google,dc=com’,  
LDAP::LDAP\_SCOPE\_SUBTREE, ‘cn=\*’, [‘cn’]) do |x|  
count += 1  
end

printf( “Page %d has %d entries.\n”, search += 1, count )  
printf( “That’s %d entries in total.\n”, total += count )

# Get the PagedResults control from the list of controls set by the

# last search operation.

control = paged\_results\_ctl( conn.controls )

# How many entries does the server estimate are left?

# Also, get the cookie returned by the last search.

returned\_size, cookie = control.decode  
puts “Returned size = #{returned\_size}” if $DEBUG

# According to RFC 2696, the server may not be able to estimate the

number

# of results outstanding. In that case, it will return zero, so we

should

# only use the new size if it is non-zero.

page\_size = returned\_size if returned\_size \> 0

puts “Returned cookie = #{cookie.inspect}” if $DEBUG

break if cookie.empty?

puts  
end

---

<div class="post-metadata">

**Author:** ![davis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e19adc/32.png) [@davis](https://www.ruby-forum.com/u/davis)\
**Post date:** [August 22, 2007, 5:57am UTC](https://www.ruby-forum.com/t/ruby-ldap-and-paged-results-on-windows/112689/2 "2007-08-22T05:57:30Z")

</div>

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

> Hi-  
> I’m trying to get paged results to work with ruby-ldap against  
> Active Directory in Windows. I compiled ruby-ldap 0.9.7 following the  
> directions on  
> [http://www.turnofthecrank.com/2006/09/11/ruby-ldap-and-win32/](http://www.turnofthecrank.com/2006/09/11/ruby-ldap-and-win32/)
> 
> Then I tried to run this code I found. The error I get is that ruby  
> can’t find the constant LDAP\_CONTROL\_PAGEDRESULTS and if I put  
> LDAP.constants it’s not listed. Any ideas?

The literal value of the control is “1.2.840.113556.1.4.319”  
You might try defining that in your code and see if it works. (If you  
have  
sharp eyes you’ll notice that the enterprise number in the OID belongs  
to  
Microsoft. Paged controls started life as a hack to make Active  
Directory  
performance slightly less horrible.)

If you’re still having trouble, try the Net::LDAP library. It supports  
paged  
results transparently.
