1

In Linux Shell I use the following command to get all Distinguished Names (DN) in LDAP:

ldapsearch -x -b "" -H URL -D uid=administrator,cn=admins,cn=city  -w PASS |grep dn:

My problem: How to get all DN's in Java as I did using the above command?

rlandster
  • 7,294
  • 14
  • 58
  • 96
srr7
  • 151
  • 1
  • 11
  • You read about the Java **ProcessBuilder** to run that command directly within a JVM. If that is what you are asking for. – GhostCat Sep 18 '16 at 13:45
  • Thanks but i want another way!!! – srr7 Sep 19 '16 at 04:25
  • I and I would want that you newbies initially understand that we are not **you**. We cant know what you want, because you are not giving any details about that idea of yours. – GhostCat Sep 19 '16 at 04:31
  • I want copy **anything** in my ldap server to another ldap server so i know user dn but i dont know another dn.for example i see dn in source ldap that dn is cn=config so i want any dn in my ldap in java code and respected that dn i added to destination ldap server – srr7 Sep 19 '16 at 05:01

1 Answers1

3

You can use the Java Naming and Directory Interface (JNDI).

Here is an example inspired from the linked tutorial :

import javax.naming.*;
import javax.naming.directory.*;
import java.util.Hashtable;

/**
 * Retrieves the DN from the search results
 */
class FullName {
  public static void printSearchEnumeration(NamingEnumeration retEnum) {
    try {
      while (retEnum.hasMore()) {
        SearchResult sr = (SearchResult) retEnum.next();
        System.out.println(">>" + sr.getNameInNamespace());
      }
    } 
    catch (NamingException e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

    env.put(Context.PROVIDER_URL, "ldap://localhost:389");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "uid=administrator,cn=admins,cn=city");
    env.put(Context.SECURITY_CREDENTIALS, "PASS");

    // Perform search in the entire subtree.
    SearchControls ctl = new SearchControls();
    ctl.setSearchScope(SearchControls.SUBTREE_SCOPE);

    try {
      // Create initial context
      DirContext ctx = new InitialDirContext(env);

      NamingEnumeration answer = ctx.search("", null, ctl);

      // Print the answer
      printSearchEnumeration(answer);

      // Close the context when we're done
      ctx.close();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}
EricLavault
  • 12,130
  • 3
  • 23
  • 45
  • I already use this code and java give me this error:javax.naming.NameNotFoundException: [LDAP: error code 32 - No Such Object]; remaining name 'ou=People' – srr7 Sep 19 '16 at 04:21
  • That is another problem, I suggest you post a question related to this issue with code and stack trace, I will take a look. Thanks – EricLavault Sep 19 '16 at 07:48
  • OK I will do it – srr7 Sep 19 '16 at 07:56
  • 1
    @SaeidRasouli, actually I updated my answer to specify the scope and also cleared the search context since you didn't mention `ou=People` but depending on your environment properties you may have to set a basedn (domain components at least) to prevent the NameNotFoundException. – EricLavault Sep 19 '16 at 09:02
  • Thank you very much:-)) i use "(&(objectClass=*))" for second parameter. – srr7 Sep 19 '16 at 09:10
  • That's right in some LDAP directories you will need this filter (I don't update the answer since for the other directories it would just slow down the search). By the way `(objectClass=*)` should be sufficient. You can mark the answer as "accepted" if you think it's acceptable, and please don't forget to delete the duplicate. – EricLavault Sep 19 '16 at 09:45