ruby-ldap and paged results on windows

D

Dave King

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/

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', 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top