Search in sources :

Example 36 with SearchResult

use of javax.naming.directory.SearchResult in project qpid-broker-j by apache.

the class SimpleLDAPAuthenticationManagerImpl method findGroups.

private Set<Principal> findGroups(DirContext context, String userDN) throws NamingException {
    Set<Principal> groupPrincipals = new HashSet<>();
    if (getGroupAttributeName() != null && !"".equals(getGroupAttributeName())) {
        Attributes attributes = context.getAttributes(userDN, new String[] { getGroupAttributeName() });
        NamingEnumeration<? extends Attribute> namingEnum = attributes.getAll();
        while (namingEnum.hasMore()) {
            Attribute attribute = namingEnum.next();
            if (attribute != null) {
                NamingEnumeration<?> attributeValues = attribute.getAll();
                while (attributeValues.hasMore()) {
                    Object attributeValue = attributeValues.next();
                    if (attributeValue != null) {
                        String groupDN = String.valueOf(attributeValue);
                        groupPrincipals.add(new GroupPrincipal(groupDN, this));
                    }
                }
            }
        }
    }
    if (getGroupSearchContext() != null && !"".equals(getGroupSearchContext()) && getGroupSearchFilter() != null && !"".equals(getGroupSearchFilter())) {
        SearchControls searchControls = new SearchControls();
        searchControls.setReturningAttributes(new String[] {});
        searchControls.setSearchScope(isGroupSubtreeSearchScope() ? SearchControls.SUBTREE_SCOPE : SearchControls.ONELEVEL_SCOPE);
        NamingEnumeration<?> groupEnumeration = context.search(getGroupSearchContext(), getGroupSearchFilter(), new String[] { encode(userDN) }, searchControls);
        while (groupEnumeration.hasMore()) {
            SearchResult result = (SearchResult) groupEnumeration.next();
            String groupDN = result.getNameInNamespace();
            groupPrincipals.add(new GroupPrincipal(groupDN, this));
        }
    }
    return groupPrincipals;
}
Also used : Attribute(javax.naming.directory.Attribute) GroupPrincipal(org.apache.qpid.server.security.group.GroupPrincipal) Attributes(javax.naming.directory.Attributes) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) UsernamePrincipal(org.apache.qpid.server.security.auth.UsernamePrincipal) GroupPrincipal(org.apache.qpid.server.security.group.GroupPrincipal) Principal(java.security.Principal) HashSet(java.util.HashSet)

Example 37 with SearchResult

use of javax.naming.directory.SearchResult in project pentaho-kettle by pentaho.

the class LDAPConnection method getAttributes.

public Attributes getAttributes() throws KettleException {
    byte[] cookie = null;
    while (!getSearchResult().hasMoreElements()) {
        if (isPagingUsed()) {
            // and pass back cookie to next page
            try {
                // examine response controls
                Control[] rc = getInitialContext().getResponseControls();
                if (rc != null) {
                    for (int i = 0; i < rc.length; i++) {
                        if (rc[i] instanceof PagedResultsResponseControl) {
                            PagedResultsResponseControl prc = (PagedResultsResponseControl) rc[i];
                            cookie = prc.getCookie();
                        }
                    }
                }
                // pass the cookie back for the next page
                if (isSortingAttributes()) {
                    getInitialContext().setRequestControls(new Control[] { new SortControl(getSortingAttributesKeys(), Control.NONCRITICAL), new PagedResultsControl(GetPagingSize(), cookie, Control.CRITICAL) });
                } else {
                    getInitialContext().setRequestControls(new Control[] { new PagedResultsControl(GetPagingSize(), cookie, Control.CRITICAL) });
                }
                if ((cookie != null) && (cookie.length != 0)) {
                    // get search result for the page
                    this.results = getInitialContext().search(getSearchBase(), getFilter(), getSearchControls());
                } else {
                    return null;
                }
            } catch (Exception e) {
                throw new KettleException(BaseMessages.getString(PKG, "LDAPInput.Exception.ErrorPaging"), e);
            }
            while (!getSearchResult().hasMoreElements()) {
                return null;
            }
        } else {
            // we have already returned all the result
            return null;
        }
    }
    try {
        SearchResult searchResult = getSearchResult().next();
        Attributes results = searchResult.getAttributes();
        results.put("dn", searchResult.getNameInNamespace());
        return results;
    } catch (Exception e) {
        throw new KettleException(BaseMessages.getString(PKG, "LDAPConnection.Exception.GettingAttributes"), e);
    }
}
Also used : SortControl(javax.naming.ldap.SortControl) KettleException(org.pentaho.di.core.exception.KettleException) Control(javax.naming.ldap.Control) SortControl(javax.naming.ldap.SortControl) PagedResultsControl(javax.naming.ldap.PagedResultsControl) PagedResultsResponseControl(javax.naming.ldap.PagedResultsResponseControl) PagedResultsResponseControl(javax.naming.ldap.PagedResultsResponseControl) Attributes(javax.naming.directory.Attributes) SearchResult(javax.naming.directory.SearchResult) KettleException(org.pentaho.di.core.exception.KettleException) NameNotFoundException(javax.naming.NameNotFoundException) PagedResultsControl(javax.naming.ldap.PagedResultsControl)

Example 38 with SearchResult

use of javax.naming.directory.SearchResult in project fess by codelibs.

the class LdapManager method processSearchRoles.

protected void processSearchRoles(final List<SearchResult> result, final BiConsumer<String, String> consumer) throws NamingException {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    for (final SearchResult srcrslt : result) {
        final Attributes attrs = srcrslt.getAttributes();
        //get group attr
        final Attribute attr = attrs.get(fessConfig.getLdapMemberofAttribute());
        if (attr == null) {
            continue;
        }
        for (int i = 0; i < attr.size(); i++) {
            final Object attrValue = attr.get(i);
            if (attrValue != null) {
                final String entryDn = attrValue.toString();
                int start = 0;
                int end = 0;
                start = entryDn.indexOf("CN=");
                if (start < 0) {
                    start = entryDn.indexOf("cn=");
                }
                if (start == -1) {
                    continue;
                }
                start += 3;
                end = entryDn.indexOf(',');
                String name;
                if (end == -1) {
                    name = entryDn.substring(start);
                } else {
                    name = entryDn.substring(start, end);
                }
                consumer.accept(entryDn, name);
            }
        }
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) SearchResult(javax.naming.directory.SearchResult) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig)

Example 39 with SearchResult

use of javax.naming.directory.SearchResult in project Lucee by lucee.

the class LDAPClient method query.

/**
 * @param dn
 * @param strAttributes
 * @param scope
 * @param startrow
 * @param maxrows
 * @param timeout
 * @param sort
 * @param sortType
 * @param sortDirection
 * @param start
 * @param separator
 * @param filter
 * @return
 * @throws NamingException
 * @throws PageException
 * @throws IOException
 */
public Query query(String strAttributes, int scope, int startrow, int maxrows, int timeout, String[] sort, int sortType, int sortDirection, String start, String separator, String filter) throws NamingException, PageException, IOException {
    // strAttributes=strAttributes.trim();
    boolean attEQAsterix = strAttributes.trim().equals("*");
    String[] attributes = attEQAsterix ? new String[] { "name", "value" } : toStringAttributes(strAttributes, ",");
    // Control
    SearchControls controls = new SearchControls();
    controls.setReturningObjFlag(true);
    controls.setSearchScope(scope);
    if (!attEQAsterix)
        controls.setReturningAttributes(toStringAttributes(strAttributes, ","));
    if (maxrows > 0)
        controls.setCountLimit(startrow + maxrows + 1);
    if (timeout > 0)
        controls.setTimeLimit(timeout);
    InitialLdapContext context = new InitialLdapContext(env, null);
    // Search
    Query qry = new QueryImpl(attributes, 0, "query");
    try {
        NamingEnumeration results = context.search(start, filter, controls);
        // Fill result
        int row = 1;
        if (!attEQAsterix) {
            while (results.hasMoreElements()) {
                SearchResult resultRow = (SearchResult) results.next();
                if (row++ < startrow)
                    continue;
                int len = qry.addRow();
                NamingEnumeration rowEnum = resultRow.getAttributes().getAll();
                String dn = resultRow.getNameInNamespace();
                qry.setAtEL("dn", len, dn);
                while (rowEnum.hasMore()) {
                    Attribute attr = (Attribute) rowEnum.next();
                    Collection.Key key = KeyImpl.init(attr.getID());
                    Enumeration values = attr.getAll();
                    Object value;
                    String existing, strValue;
                    while (values.hasMoreElements()) {
                        value = values.nextElement();
                        strValue = Caster.toString(value, null);
                        existing = Caster.toString(qry.getAt(key, len, null), null);
                        if (!StringUtil.isEmpty(existing) && !StringUtil.isEmpty(strValue)) {
                            value = existing + separator + strValue;
                        } else if (!StringUtil.isEmpty(existing))
                            value = existing;
                        qry.setAtEL(key, len, value);
                    }
                }
                if (maxrows > 0 && len >= maxrows)
                    break;
            }
        } else {
            outer: while (results.hasMoreElements()) {
                SearchResult resultRow = (SearchResult) results.next();
                if (row++ < startrow)
                    continue;
                Attributes attributesRow = resultRow.getAttributes();
                NamingEnumeration rowEnum = attributesRow.getIDs();
                while (rowEnum.hasMoreElements()) {
                    int len = qry.addRow();
                    String name = Caster.toString(rowEnum.next());
                    Object value = null;
                    try {
                        value = attributesRow.get(name).get();
                    } catch (Exception e) {
                    }
                    qry.setAtEL("name", len, name);
                    qry.setAtEL("value", len, value);
                    if (maxrows > 0 && len >= maxrows)
                        break outer;
                }
                qry.setAtEL("name", qry.size(), "dn");
            }
        }
    } finally {
        context.close();
    }
    // Sort
    if (sort != null && sort.length > 0) {
        int order = sortDirection == SORT_DIRECTION_ASC ? Query.ORDER_ASC : Query.ORDER_DESC;
        for (int i = sort.length - 1; i >= 0; i--) {
            String item = sort[i];
            if (item.indexOf(' ') != -1)
                item = ListUtil.first(item, " ", true);
            qry.sort(KeyImpl.getInstance(item), order);
        // keys[i] = new SortKey(item);
        }
    }
    return qry;
}
Also used : Enumeration(java.util.Enumeration) NamingEnumeration(javax.naming.NamingEnumeration) Query(lucee.runtime.type.Query) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) ClassException(lucee.commons.lang.ClassException) NamingException(javax.naming.NamingException) PageException(lucee.runtime.exp.PageException) IOException(java.io.IOException) QueryImpl(lucee.runtime.type.QueryImpl) InitialLdapContext(javax.naming.ldap.InitialLdapContext) Collection(lucee.runtime.type.Collection) SearchControls(javax.naming.directory.SearchControls)

Example 40 with SearchResult

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

the class LdapProvider method isAdmin.

private boolean isAdmin(String accountName) {
    if (this.adminFilter != null) {
        try {
            InitialDirContext context = initContext();
            String searchString = adminFilter.replace(":login", accountName);
            SearchControls searchControls = new SearchControls();
            searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            NamingEnumeration<SearchResult> results = context.search(searchBase, searchString, searchControls);
            if (results.hasMoreElements()) {
                results.nextElement();
                if (results.hasMoreElements()) {
                    Log.warning("Matched multiple users for the accountName: " + accountName);
                    return false;
                }
                return true;
            }
        } catch (NamingException e) {
            return false;
        }
    }
    return false;
}
Also used : SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext)

Aggregations

SearchResult (javax.naming.directory.SearchResult)248 SearchControls (javax.naming.directory.SearchControls)146 NamingException (javax.naming.NamingException)113 Attributes (javax.naming.directory.Attributes)96 Attribute (javax.naming.directory.Attribute)86 ArrayList (java.util.ArrayList)75 LdapContext (javax.naming.ldap.LdapContext)39 NamingEnumeration (javax.naming.NamingEnumeration)36 DirContext (javax.naming.directory.DirContext)35 Test (org.junit.Test)32 BasicAttributes (javax.naming.directory.BasicAttributes)30 HashSet (java.util.HashSet)28 InitialDirContext (javax.naming.directory.InitialDirContext)27 InitialLdapContext (javax.naming.ldap.InitialLdapContext)23 PagedResultsControl (javax.naming.ldap.PagedResultsControl)22 HashMap (java.util.HashMap)20 IOException (java.io.IOException)19 BasicAttribute (javax.naming.directory.BasicAttribute)19 Control (javax.naming.ldap.Control)16 PagedResultsResponseControl (javax.naming.ldap.PagedResultsResponseControl)15