in reply to ldap query only returning 1k lines

Here is some code that worked for me, running on Windows, in an AD environment:
# lists all users in an Everyone OU #This logs in to LDAP and generates a CSV file with each members displ +ay name and then a list of their groups. use strict; use Net::LDAP; use Net::LDAP::Control::Paged; use Net::LDAP::Constant ( "LDAP_CONTROL_PAGED" ); my $ldap = Net::LDAP->new('your.domaincontroller.com') or die "Could n +ot connect to Domain controller $@"; # params for Net::LDAP::new # bind to a directory with dn and password my $mesg = $ldap->bind( 'ldapreadonlyUsername', password => 'LDAPRead0nlyPassword' ); die $mesg->error if $mesg->code; # How many LDAP query results to grab for each paged round # Set to under 1000 for Active Directory my $page = Net::LDAP::Control::Paged->new( size => 100 ); my $fields = ['displayName','memberOf','useraccountcontrol']; my $strFilter = "(&(objectclass=user)(objectcategory=person)" # A + User # . "(!useraccountcontrol:1.2.840.113556.1.4.803:=2)" # + NOT Disabled . "(useraccountcontrol:dn:1.2.840.113556.1.4.803:=2)" +#The 1.2.840.113556.1.4.803 is a logical AND. . ")"; my $result = $ldap->search ( base => "dc=CompanyDomainName,dc=com", filter => $strFilter, attrs => $fields, control => [ $page ], ); die $result->error if $result->code; for my $item ( $result->entries) { next unless defined $item->get_value("displayName"); my $user; $user->{groups} = [ ref ( $item->get_value('memberOf') ) ? @{$item->get_value('memberOf')} : ($item->get_value('memberOf')) ]; $user->{groups} = [ map { /CN=(.+?),/ ; $1 } @{$user->{groups}} ]; print '"',$item->get_value("displayName"),'",'; print join(",", map { '"' . $_ . '"' } sort @{$user->{groups}} +),"\n"; } # Get cookie from paged control my($resp) = $mesg->control( LDAP_CONTROL_PAGED ) or print "-- No mo +re data (1) --\n"; ## last; my $cookie = $resp->cookie or print "-- No more data (2) --\n"; # Sh +ould do LAST here.. # Set cookie in paged control $page->cookie($cookie);

     ..to maintain is to slowly feel your soul, sanity and sentience ebb away as you become one with the Evil.

Replies are listed 'Best First'.
Re^2: ldap query only returning 1k lines
by Anonymous Monk on May 13, 2009 at 21:43 UTC
    I think this is getting closer. When I see you're line:
    # How many LDAP query results to grab for each paged round # Set to under 1000 for Active Directory my $page = Net::LDAP::Control::Paged->new( size => 100 );
    I see you are set to 100. If I set mine to 100, I only get 100 entries. If I set mine to 1000, I get 1000 lines. If I set it any higher than 1000, I still only get 1000 entries. Does that ring any bells for anyone or jive?
      From http://support.microsoft.com/kb/315071, You can use NTDSUTIL to define policy/ parameters:

      MaxPageSize - This value controls the maximum number of objects that are returned in a single search result, independent of how large each returned object is. To perform a search where the result might exceed this number of objects, the client must specify the paged search control. This is to group the returned results in groups that are no larger than the MaxPageSize value. To summarize, MaxPageSize controls the number of objects that are returned in a single search result.

      Default value: 1,000

           ..to maintain is to slowly feel your soul, sanity and sentience ebb away as you become one with the Evil.

        OK, I love the explanation, and I hope that it works, but why would the query still return more that 1k results when run from windows? Thanks again!
        I love the explanation, and I hope it works, but why would the query from a windows machine return all of the records but not the UNIX query?