Search in sources :

Example 91 with SearchResult

use of javax.naming.directory.SearchResult in project uPortal by Jasig.

the class LDAPGroupStore method processLdapResults.

protected void processLdapResults(NamingEnumeration results, ArrayList keys) {
    // long loop1=System.currentTimeMillis();
    try {
        while (results.hasMore()) {
            // long loop2 = System.currentTimeMillis();
            // long cast1=System.currentTimeMillis();
            // looping=looping+loop2-loop1;
            SearchResult result = (SearchResult) results.next();
            // long cast2 = System.currentTimeMillis();
            // long get1 = System.currentTimeMillis();
            Attributes ldapattribs = result.getAttributes();
            // long get2 = System.currentTimeMillis();
            // long set1 = System.currentTimeMillis();
            Attribute attrib = ldapattribs.get(keyfield);
            if (attrib != null) {
                keys.add(String.valueOf(attrib.get()).toLowerCase());
            }
        // long set2 = System.currentTimeMillis();
        // loop1=System.currentTimeMillis();
        // casting=casting+cast2-cast1;
        // setting=setting+set2-set1;
        // getting=getting+get2-get1;
        }
    } catch (NamingException nex) {
        log.error("LDAPGroupStore: error processing results", nex);
    } finally {
        try {
            results.close();
        } catch (Exception e) {
        }
    }
// long time5 = System.currentTimeMillis();
// System.out.println("Result processing took "+(time5-time1)+": "+getting+" for getting, "
// +setting+" for setting, "+casting+" for casting, "+looping+" for looping,"
// +(time5-loop1)+" for closing");
}
Also used : Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) ResourceMissingException(org.apereo.portal.ResourceMissingException) NamingException(javax.naming.NamingException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) GroupsException(org.apereo.portal.groups.GroupsException) SAXException(org.xml.sax.SAXException)

Example 92 with SearchResult

use of javax.naming.directory.SearchResult in project cxf by apache.

the class LDAPSearchTest method testSearch.

@Test
@Ignore
public void testSearch() throws URISyntaxException, NamingException {
    LdapSearch ldapSearch = new LdapSearch("ldap://localhost:2389", "cn=Directory Manager,dc=example,dc=com", "test", 2);
    NamingEnumeration<SearchResult> answer = ldapSearch.searchSubTree("dc=example, dc=com", "(cn=Testuser)");
    while (answer.hasMore()) {
        SearchResult sr = answer.next();
        Attributes attrs = sr.getAttributes();
        Attribute cn = attrs.get("sn");
        System.out.println(cn.get());
    }
}
Also used : Attribute(javax.naming.directory.Attribute) Attributes(javax.naming.directory.Attributes) SearchResult(javax.naming.directory.SearchResult) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 93 with SearchResult

use of javax.naming.directory.SearchResult in project traccar by tananaev.

the class LdapProvider method lookupUser.

private SearchResult lookupUser(String accountName) throws NamingException {
    InitialDirContext context = initContext();
    String searchString = searchFilter.replace(":login", accountName);
    SearchControls searchControls = new SearchControls();
    String[] attributeFilter = { idAttribute, nameAttribute, mailAttribute };
    searchControls.setReturningAttributes(attributeFilter);
    searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    NamingEnumeration<SearchResult> results = context.search(searchBase, searchString, searchControls);
    SearchResult searchResult = null;
    if (results.hasMoreElements()) {
        searchResult = results.nextElement();
        if (results.hasMoreElements()) {
            Log.warning("Matched multiple users for the accountName: " + accountName);
            return null;
        }
    }
    return searchResult;
}
Also used : SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 94 with SearchResult

use of javax.naming.directory.SearchResult in project Payara by payara.

the class LDAPRealm method dynamicGroupSearch.

/**
 * Search for group membership using the given connection.
 */
private List dynamicGroupSearch(DirContext ctx, String baseDN, String memberOfAttr, String filter, String target) throws NamingException {
    List groupList = new ArrayList();
    String[] targets = new String[] { memberOfAttr };
    try {
        SearchControls ctls = new SearchControls();
        ctls.setReturningAttributes(targets);
        ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        // Set this to false to avoid objects and hence exposing ldap object
        // injection.
        ctls.setReturningObjFlag(false);
        NamingEnumeration e = ctx.search(baseDN, filter, ctls);
        while (e.hasMore()) {
            SearchResult res = (SearchResult) e.next();
            Attribute isMemberOf = res.getAttributes().get(memberOfAttr);
            if (isMemberOf != null) {
                for (Enumeration values = isMemberOf.getAll(); values.hasMoreElements(); ) {
                    String groupDN = (String) values.nextElement();
                    LdapName dn = new LdapName(groupDN);
                    for (Rdn rdn : dn.getRdns()) {
                        if (rdn.getType().equalsIgnoreCase(target)) {
                            groupList.add(rdn.getValue());
                            break;
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        _logger.log(Level.WARNING, "ldaprealm.searcherror", filter);
        _logger.log(Level.WARNING, "security.exception", e);
    }
    return groupList;
}
Also used : NamingEnumeration(javax.naming.NamingEnumeration) Attribute(javax.naming.directory.Attribute) SearchControls(javax.naming.directory.SearchControls) NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) Rdn(javax.naming.ldap.Rdn) LoginException(javax.security.auth.login.LoginException) BadRealmException(com.sun.enterprise.security.auth.realm.BadRealmException) NamingException(javax.naming.NamingException) NoSuchRealmException(com.sun.enterprise.security.auth.realm.NoSuchRealmException) InvalidOperationException(com.sun.enterprise.security.auth.realm.InvalidOperationException) IOException(java.io.IOException) NoSuchUserException(com.sun.enterprise.security.auth.realm.NoSuchUserException) LdapName(javax.naming.ldap.LdapName)

Example 95 with SearchResult

use of javax.naming.directory.SearchResult in project scheduling by ow2-proactive.

the class LDAPLoginModule method getLDAPUserDN.

/**
 * Connects anonymously to the LDAP server <code>url</code> and retrieve
 * DN of the user <code>username</code>
 *
 * <p>
 * @exception NamingException
 *                if a naming exception is encountered.
 * <p>
 *
 * @return the String containing the UID of the user or null if the user is
 *         not found.
 */
private String getLDAPUserDN(String username) throws NamingException {
    String userDN = null;
    DirContext ctx = null;
    try {
        // Create the initial directory context
        ctx = this.connectAndGetContext();
        SearchControls sControl = new SearchControls();
        sControl.setSearchScope(SearchControls.SUBTREE_SCOPE);
        String filter = String.format(ldapProperties.getProperty(LDAPProperties.LDAP_USER_FILTER), username);
        // looking for the user dn (distinguish name)
        NamingEnumeration<SearchResult> answer = ctx.search(USERS_DN, filter, sControl);
        if (answer.hasMoreElements()) {
            SearchResult result = (SearchResult) answer.next();
            userDN = result.getNameInNamespace();
            if (logger.isDebugEnabled()) {
                logger.debug("User " + username + " has LDAP entry " + userDN);
            }
            subject.getPrincipals().add(new UserNamePrincipal(username));
            // looking for the user groups
            String groupFilter = String.format(ldapProperties.getProperty(LDAPProperties.LDAP_GROUP_FILTER), userDN);
            NamingEnumeration<SearchResult> groupResults = ctx.search(GROUPS_DN, groupFilter, sControl);
            while (groupResults.hasMoreElements()) {
                SearchResult res = (SearchResult) groupResults.next();
                Attribute attr = res.getAttributes().get(ldapProperties.getProperty(LDAPProperties.LDAP_GROUPNAME_ATTR));
                if (attr != null) {
                    String groupName = attr.get().toString();
                    subject.getPrincipals().add(new GroupNamePrincipal(groupName));
                    if (logger.isDebugEnabled()) {
                        logger.debug("User " + username + " is a member of group " + groupName);
                    }
                }
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("User DN not found");
            }
        }
    } catch (NamingException e) {
        logger.error("Problem with the search in mode: " + AUTHENTICATION_METHOD + e);
        throw e;
    } finally {
        try {
            if (ctx != null) {
                ctx.close();
            }
        } catch (NamingException e) {
            logger.error("", e);
            logger.error("Problem closing LDAP connection: " + e.getMessage());
        }
    }
    return userDN;
}
Also used : UserNamePrincipal(org.ow2.proactive.authentication.principals.UserNamePrincipal) Attribute(javax.naming.directory.Attribute) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) GroupNamePrincipal(org.ow2.proactive.authentication.principals.GroupNamePrincipal)

Aggregations

SearchResult (javax.naming.directory.SearchResult)95 SearchControls (javax.naming.directory.SearchControls)63 NamingException (javax.naming.NamingException)43 Attributes (javax.naming.directory.Attributes)35 Attribute (javax.naming.directory.Attribute)32 ArrayList (java.util.ArrayList)29 DirContext (javax.naming.directory.DirContext)27 NamingEnumeration (javax.naming.NamingEnumeration)24 InitialDirContext (javax.naming.directory.InitialDirContext)16 Test (org.junit.Test)15 LdapContext (javax.naming.ldap.LdapContext)13 IOException (java.io.IOException)9 BasicAttributes (javax.naming.directory.BasicAttributes)9 DistinguishedName (org.springframework.ldap.core.DistinguishedName)9 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)7 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 Name (javax.naming.Name)6 BasicAttribute (javax.naming.directory.BasicAttribute)6 PartialResultException (javax.naming.PartialResultException)5