Query LDAP server as normal user for user id of a different user through Java 6 JNDI

Java 6, JNDI:
I am logged in to a corporate LDAP server as a normal user and would like to query other users. My primary objective is to query LDAP for a Common Name based on the provided user id from a List. I can query my general info successfully, although cannot return my uid. Is this due in part that my account limits access for a general user? Setting the SearchControl to anything but SearchControls.OBJECT_SCOPE returns nothing. Below is a code snippet of the query setup.

Code snippet:
-----------------
 try {
            ctx = connect(managerName, managerPassword);

            String filter = "(objectclass=Person)";

            // ceate default search controls
            SearchControls controls = new SearchControls();

            String[] attrId =  {"sn", "givenname", "cn", "ou", "uid", "memberOf"};
            controls.setReturningAttributes(attrId);

            // anything other than object scope fails
           controls.setSearchScope(SearchControls.OBJECT_SCOPE); // Search object only

            NamingEnumeration<SearchResult> answer = ctx.search(bindDn, filter, controls);
            isValid = true;
            while (answer.hasMore()) {
                SearchResult sr = (SearchResult) answer.next();
                Attributes attr = sr.getAttributes();
                System.out.println("Attributes:" + attr);
            }
-------------------------
Returned Attributes are populated with CN, DC, OU, SN, and so on but no UID:
rayskeltonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

anilallewarCommented:
It should be a problem with your credentials as you are able to connect but you have limited privilages. You might need to login with an admin account to access this functionality.
0
rayskeltonAuthor Commented:
The query attribute of   "sAMAccountName" and not uid was the query problem. With this name, I can query my user id so now the challenge is to query others id. I had a special account setup for this tool that should have the permissions to query others so how is this done?

Code snippet:
-----------------
 try {
            ctx = connect(managerName, managerPassword);

            String filter = "(objectclass=*)";

            // ceate default search controls
            SearchControls controls = new SearchControls();

            String[] attrId =  {"sn", "givenname", "cn", "ou", " sAMAccountName ", "memberOf"};
            controls.setReturningAttributes(attrId);
           controls.setSearchScope(SearchControls.SUBTREE_SCOPE);

            NamingEnumeration<SearchResult> answer = ctx.search(bindDn, filter, controls);
            isValid = true;
            while (answer.hasMore()) {
                SearchResult sr = (SearchResult) answer.next();
                Attributes attr = sr.getAttributes();
                System.out.println("Attributes:" + attr);
            }


0
rayskeltonAuthor Commented:
After connected to server through a JNDI connect, I resolved the problem myself with the following code

BasicAttributes basAttr = new BasicAttributes();
            basAttr.put("sAMAccountName", "otheruserid");

           // This was the key to this question
           NamingEnumeration<SearchResult> reply = ctx.search("OU=Users, OU=xxxx, DC=xxx, DC=company, DC=net",  basAttr);

             while (reply.hasMore()) {
SearchResult sr = (SearchResult) reply.next();
                System.out.println("Search Results:" + sr.toString());
                Attributes attr = sr.getAttributes();
                Attribute uid = attr.get("sAMAccountName");
                Attribute name = attr.get("distinguishedName");
                System.out.println("uid:" + uid + "Name:" + name);                
            }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.