Browsing an LDAP directory

J

John Smith

I want to be able to browse an LDAP directory (Active Directory) and bring
back the names of users and servers. I can connect to the active directory
server and get authenticated, but how do I construct a query to browse for
information I have tried:

LdapContext context = new InitialLdapContext(env,null);

NamingEnumeration e = context.search("cn=,ou=,o=",null);

while (e.hasMoreElements()) {

Object o = e.nextElement();

System.out.println(o.getClass().getName()+" "+o.toString());

}



But just get:

Exception in thread "main" javax.naming.InvalidNameException: cn=,ou=,o=:
[LDAP: error code 34 - 0000208F: NameErr: DSID-031001BA, problem 2006
(BAD_NAME), data 8350, best match of:

'cn=,ou=,o='



Thanks



Jon
 
J

jan V

John Smith said:
I want to be able to browse an LDAP directory (Active Directory) and bring
back the names of users and servers. I can connect to the active directory
server and get authenticated, but how do I construct a query to browse for
information I have tried:

LdapContext context = new InitialLdapContext(env,null);

NamingEnumeration e = context.search("cn=,ou=,o=",null);

From the exception, it looks like the String you pass to search() isn't
properly formatted. Are you sure the formatting is OK?
 
S

shakah

John said:
I want to be able to browse an LDAP directory (Active Directory) and bring
back the names of users and servers. I can connect to the active directory
server and get authenticated, but how do I construct a query to browse for
information I have tried:

LdapContext context = new InitialLdapContext(env,null);

NamingEnumeration e = context.search("cn=,ou=,o=",null);

while (e.hasMoreElements()) {

Object o = e.nextElement();

System.out.println(o.getClass().getName()+" "+o.toString());

}



But just get:

Exception in thread "main" javax.naming.InvalidNameException: cn=,ou=,o=:
[LDAP: error code 34 - 0000208F: NameErr: DSID-031001BA, problem 2006
(BAD_NAME), data 8350, best match of:

'cn=,ou=,o='

Working out LDAP search strings is a bit easier if you start with the
ldapsearch command-line tool, but in any case see if something like the
following works for you:

SearchControls scTemp = new SearchControls() ;
scTemp.setSearchScope(SearchControls.SUBTREE_SCOPE) ;
scTemp.setReturningAttributes(new String [] { "cn" }) ;
for(NamingEnumeration ne=ctx.search("", "(objectClass=*)", scTemp);
ne.hasMore();
) {
SearchResult srTemp = (SearchResult) ne.next() ;
Attributes attrsTemp = srTemp.getAttributes() ;

System.out.println("srTemp.getName(): " + srTemp.getName()) ;
System.out.println(" cn: '" + attrsTemp.get("cn").get() + "'") ;
}
 
R

Roedy Green

I want to be able to browse an LDAP directory (Active Directory)

this is some code for getting MX records. It might give you a hint to
solving your problem:

/**
* Gets all matching dns records as an array of strings.
*
* @param domain domain, e.g. oberon.ark.com or oberon.com which
you want
* the DNS records.
*
* @param types e.g. new String {"MX","A"}
* to describe which types of record you want.
* @return ArrayList of Strings
*/
static ArrayList getDNSRecs (String domain, String[] types) throws
NamingException
{
ArrayList results = new ArrayList(15);
DirContext ictx = new InitialDirContext();
Attributes attrs = ictx.getAttributes("dns://" + dnsServer + "/"
+ domain,
types);
for ( Enumeration e = attrs.getAll(); e.hasMoreElements(); )
{
Attribute a = (Attribute) e.nextElement();
int size = a.size();
for ( int i=0; i<size; i++ )
{
// MX string has priority (lower better) followed by
associated mailserver
// A string is just IP
results.add(a.get(i));
} // end inner for
} // end outer for

return results;
}
 
J

John Smith

Thanks for this, asa newbie I did not quite express what I wanted, want I
need to know is how to get the root node when connection to an ldap server I
only know the ip/user/password. I can browse it using
DC=ourdomain,DC=co,DC=UK, but there must be a way to do this.

Thanks

Jon
shakah said:
John said:
I want to be able to browse an LDAP directory (Active Directory) and
bring
back the names of users and servers. I can connect to the active
directory
server and get authenticated, but how do I construct a query to browse
for
information I have tried:

LdapContext context = new InitialLdapContext(env,null);

NamingEnumeration e = context.search("cn=,ou=,o=",null);

while (e.hasMoreElements()) {

Object o = e.nextElement();

System.out.println(o.getClass().getName()+" "+o.toString());

}



But just get:

Exception in thread "main" javax.naming.InvalidNameException: cn=,ou=,o=:
[LDAP: error code 34 - 0000208F: NameErr: DSID-031001BA, problem 2006
(BAD_NAME), data 8350, best match of:

'cn=,ou=,o='

Working out LDAP search strings is a bit easier if you start with the
ldapsearch command-line tool, but in any case see if something like the
following works for you:

SearchControls scTemp = new SearchControls() ;
scTemp.setSearchScope(SearchControls.SUBTREE_SCOPE) ;
scTemp.setReturningAttributes(new String [] { "cn" }) ;
for(NamingEnumeration ne=ctx.search("", "(objectClass=*)", scTemp);
ne.hasMore();
) {
SearchResult srTemp = (SearchResult) ne.next() ;
Attributes attrsTemp = srTemp.getAttributes() ;

System.out.println("srTemp.getName(): " + srTemp.getName()) ;
System.out.println(" cn: '" + attrsTemp.get("cn").get() + "'") ;
}
 
S

shakah

Not sure what you mean by "root node". If you mean "how do I know from
where to base my search (e.g. DC=ourdomain,DC=co,DC=uk)?", that's just
a piece of information that you must provide, similar to ip/user/pwd.

John said:
Thanks for this, asa newbie I did not quite express what I wanted, want I
need to know is how to get the root node when connection to an ldap server I
only know the ip/user/password. I can browse it using
DC=ourdomain,DC=co,DC=UK, but there must be a way to do this.

Thanks

Jon
shakah said:
John said:
I want to be able to browse an LDAP directory (Active Directory) and
bring
back the names of users and servers. I can connect to the active
directory
server and get authenticated, but how do I construct a query to browse
for
information I have tried:

LdapContext context = new InitialLdapContext(env,null);

NamingEnumeration e = context.search("cn=,ou=,o=",null);

while (e.hasMoreElements()) {

Object o = e.nextElement();

System.out.println(o.getClass().getName()+" "+o.toString());

}



But just get:

Exception in thread "main" javax.naming.InvalidNameException: cn=,ou=,o=:
[LDAP: error code 34 - 0000208F: NameErr: DSID-031001BA, problem 2006
(BAD_NAME), data 8350, best match of:

'cn=,ou=,o='

Working out LDAP search strings is a bit easier if you start with the
ldapsearch command-line tool, but in any case see if something like the
following works for you:

SearchControls scTemp = new SearchControls() ;
scTemp.setSearchScope(SearchControls.SUBTREE_SCOPE) ;
scTemp.setReturningAttributes(new String [] { "cn" }) ;
for(NamingEnumeration ne=ctx.search("", "(objectClass=*)", scTemp);
ne.hasMore();
) {
SearchResult srTemp = (SearchResult) ne.next() ;
Attributes attrsTemp = srTemp.getAttributes() ;

System.out.println("srTemp.getName(): " + srTemp.getName()) ;
System.out.println(" cn: '" + attrsTemp.get("cn").get() + "'") ;
}
 
J

John Smith

For those that follow, this is how I solved it and found out the domain:

LdapContext context = new InitialLdapContext(env, null);

Attributes attributes = context.getAttributes(context.getNameInNamespace());

Attribute attribute = attributes.get("defaultNamingContext");



Thanks for all the help



Jon

shakah said:
Not sure what you mean by "root node". If you mean "how do I know from
where to base my search (e.g. DC=ourdomain,DC=co,DC=uk)?", that's just
a piece of information that you must provide, similar to ip/user/pwd.

John said:
Thanks for this, asa newbie I did not quite express what I wanted, want I
need to know is how to get the root node when connection to an ldap
server I
only know the ip/user/password. I can browse it using
DC=ourdomain,DC=co,DC=UK, but there must be a way to do this.

Thanks

Jon
shakah said:
John Smith wrote:
I want to be able to browse an LDAP directory (Active Directory) and
bring
back the names of users and servers. I can connect to the active
directory
server and get authenticated, but how do I construct a query to browse
for
information I have tried:

LdapContext context = new InitialLdapContext(env,null);

NamingEnumeration e = context.search("cn=,ou=,o=",null);

while (e.hasMoreElements()) {

Object o = e.nextElement();

System.out.println(o.getClass().getName()+" "+o.toString());

}



But just get:

Exception in thread "main" javax.naming.InvalidNameException:
cn=,ou=,o=:
[LDAP: error code 34 - 0000208F: NameErr: DSID-031001BA, problem 2006
(BAD_NAME), data 8350, best match of:

'cn=,ou=,o='

Working out LDAP search strings is a bit easier if you start with the
ldapsearch command-line tool, but in any case see if something like the
following works for you:

SearchControls scTemp = new SearchControls() ;
scTemp.setSearchScope(SearchControls.SUBTREE_SCOPE) ;
scTemp.setReturningAttributes(new String [] { "cn" }) ;
for(NamingEnumeration ne=ctx.search("", "(objectClass=*)", scTemp);
ne.hasMore();
) {
SearchResult srTemp = (SearchResult) ne.next() ;
Attributes attrsTemp = srTemp.getAttributes() ;

System.out.println("srTemp.getName(): " + srTemp.getName()) ;
System.out.println(" cn: '" + attrsTemp.get("cn").get() + "'") ;
}
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top