Search code examples
javaldapldap-query

LDAP Query for fetching dn and all cn data


enter image description here

I'm using netscape ldapsdk-4.1.jar,I am able to connect & authenticate with my ldap server :

try{
            ldi.connect(hostname,LDAPv3.DEFAULT_PORT);
            LDAPConnectionInfo.ldapSearchResults = ldi.search(LDAPConnectionInfo.MY_SEARCHBASE,LDAPConnectionInfo.MY_SCOPE,LDAPConnectionInfo.MY_FILTER,null,false);
            System.out.println("Ldap Search Result : " +LDAPConnectionInfo.ldapSearchResults);
            
            ldi.authenticate(3, "host", "****");
}
.....
        

Now I want fetch the data from :

  • Under the domain component(dn, here=example.com) how many organizational units(ou) are there.
  • For each organizational unit(ou) to get all common name(cn) and it's leaf objects under the common name(cn).

I think I have to use some ldap query to fetch these data....any input will be highly appreciated.


Solution

  • Create a search request for each organizational unit where:

    • the base object is the organizational unit
    • the scope is subtree
    • the filter is either objectClass=commonObjectClass where commonObjectCLass is an objectClass common to entries to be retrieved, or cn=*
    • the list of attributes to be retrieved for each entry

    transmit each search request in turn to the LDAP directory server and interpret the responses.

    Do not use the netscape code, it's ancient, buggy, and slow. Use the UnboundID LDAP SDK instead