Search in sources :

Example 61 with SearchControls

use of javax.naming.directory.SearchControls in project cloudstack by apache.

the class OpenLdapUserManagerImpl method searchUsers.

@Override
public List<LdapUser> searchUsers(final String username, final LdapContext context) throws NamingException, IOException {
    final SearchControls searchControls = new SearchControls();
    searchControls.setSearchScope(_ldapConfiguration.getScope());
    searchControls.setReturningAttributes(_ldapConfiguration.getReturnAttributes());
    String basedn = _ldapConfiguration.getBaseDn();
    if (StringUtils.isBlank(basedn)) {
        throw new IllegalArgumentException("ldap basedn is not configured");
    }
    byte[] cookie = null;
    int pageSize = _ldapConfiguration.getLdapPageSize();
    context.setRequestControls(new Control[] { new PagedResultsControl(pageSize, Control.NONCRITICAL) });
    final List<LdapUser> users = new ArrayList<LdapUser>();
    NamingEnumeration<SearchResult> results;
    do {
        results = context.search(basedn, generateSearchFilter(username), searchControls);
        while (results.hasMoreElements()) {
            final SearchResult result = results.nextElement();
            if (!isUserDisabled(result)) {
                users.add(createUser(result));
            }
        }
        Control[] contextControls = context.getResponseControls();
        if (contextControls != null) {
            for (Control control : contextControls) {
                if (control instanceof PagedResultsResponseControl) {
                    PagedResultsResponseControl prrc = (PagedResultsResponseControl) control;
                    cookie = prrc.getCookie();
                }
            }
        } else {
            s_logger.info("No controls were sent from the ldap server");
        }
        context.setRequestControls(new Control[] { new PagedResultsControl(pageSize, cookie, Control.CRITICAL) });
    } while (cookie != null);
    return users;
}
Also used : PagedResultsResponseControl(javax.naming.ldap.PagedResultsResponseControl) ArrayList(java.util.ArrayList) SearchResult(javax.naming.directory.SearchResult) Control(javax.naming.ldap.Control) PagedResultsControl(javax.naming.ldap.PagedResultsControl) PagedResultsResponseControl(javax.naming.ldap.PagedResultsResponseControl) SearchControls(javax.naming.directory.SearchControls) PagedResultsControl(javax.naming.ldap.PagedResultsControl)

Example 62 with SearchControls

use of javax.naming.directory.SearchControls in project karaf by apache.

the class LDAPCache method open.

public synchronized DirContext open() throws NamingException {
    if (isContextAlive()) {
        return context;
    }
    clearCache();
    context = new InitialDirContext(options.getEnv());
    EventDirContext eventContext = ((EventDirContext) context.lookup(""));
    final SearchControls constraints = new SearchControls();
    constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
    if (!options.getDisableCache()) {
        String filter = options.getUserFilter();
        filter = filter.replaceAll(Pattern.quote("%u"), Matcher.quoteReplacement("*"));
        filter = filter.replace("\\", "\\\\");
        eventContext.addNamingListener(options.getUserBaseDn(), filter, constraints, this);
        filter = options.getRoleFilter();
        if (filter != null) {
            filter = filter.replaceAll(Pattern.quote("%u"), Matcher.quoteReplacement("*"));
            filter = filter.replaceAll(Pattern.quote("%dn"), Matcher.quoteReplacement("*"));
            filter = filter.replaceAll(Pattern.quote("%fqdn"), Matcher.quoteReplacement("*"));
            filter = filter.replace("\\", "\\\\");
            eventContext.addNamingListener(options.getRoleBaseDn(), filter, constraints, this);
        }
    }
    return context;
}
Also used : EventDirContext(javax.naming.event.EventDirContext) SearchControls(javax.naming.directory.SearchControls) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 63 with SearchControls

use of javax.naming.directory.SearchControls in project nhin-d by DirectProject.

the class LdapCertUtilImpl method getDefaultSearchControls.

// /CLOVER:OFF
protected SearchControls getDefaultSearchControls() {
    SearchControls ctls = new SearchControls();
    ctls.setReturningObjFlag(true);
    ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    ctls.setReturningAttributes(new String[] { ldapEnvironment.getReturningCertAttribute() });
    return ctls;
}
Also used : SearchControls(javax.naming.directory.SearchControls)

Example 64 with SearchControls

use of javax.naming.directory.SearchControls in project nhin-d by DirectProject.

the class LdapCertUtilImpl method ldapSearch.

public Collection<X509Certificate> ldapSearch(String subjectName) {
    DirContext ctx = null;
    try {
        ctx = getInitialDirContext(ldapEnvironment.getEnv());
        final SearchControls ctls = getDefaultSearchControls();
        NamingEnumeration<SearchResult> searchResult = ctx.search(ldapEnvironment.getLdapSearchBase(), ldapEnvironment.getLdapSearchAttribute() + "=" + subjectName, ctls);
        ArrayList<X509Certificate> certificates = new ArrayList<X509Certificate>();
        while (searchResult != null && searchResult.hasMoreElements()) {
            final SearchResult certEntry = searchResult.nextElement();
            if (certEntry != null) {
                final Attributes certAttributes = certEntry.getAttributes();
                if (certAttributes != null) {
                    // get only the returning cert attribute (for now, ignore all other attributes)
                    final Attribute certAttribute = certAttributes.get(ldapEnvironment.getReturningCertAttribute());
                    if (certAttribute != null) {
                        NamingEnumeration<? extends Object> allValues = certAttribute.getAll();
                        // LDAP may contain a collection of certificates.
                        while (allValues.hasMoreElements()) {
                            String ksBytes = (String) allValues.nextElement();
                            Base64 base64 = new Base64();
                            byte[] decode = base64.decode(ksBytes.getBytes());
                            ByteArrayInputStream inputStream = new ByteArrayInputStream(decode);
                            if (certificateFormat.equalsIgnoreCase("pkcs12")) {
                                try {
                                    processPKCS12FileFormatAndAddToCertificates(inputStream, certificates);
                                } catch (Exception e) {
                                    closeDirContext(ctx);
                                    throw new NHINDException("", e);
                                }
                            } else {
                                if (certificateFormat.equalsIgnoreCase("X.509") || certificateFormat.equalsIgnoreCase("X509")) {
                                    CertificateFactory cf = CertificateFactory.getInstance("X.509");
                                    X509Certificate addCert = (X509Certificate) cf.generateCertificate(inputStream);
                                    certificates.add(addCert);
                                } else {
                                    closeDirContext(ctx);
                                    throw new NHINDException("Invalid certificate format requested");
                                }
                            }
                        }
                    }
                }
            }
        }
        return certificates;
    } catch (NamingException e) {
        closeDirContext(ctx);
        throw new NHINDException("", e);
    } catch (CertificateException e) {
        closeDirContext(ctx);
        throw new NHINDException("", e);
    }
}
Also used : Base64(org.apache.commons.codec.binary.Base64) Attribute(javax.naming.directory.Attribute) ArrayList(java.util.ArrayList) Attributes(javax.naming.directory.Attributes) SearchResult(javax.naming.directory.SearchResult) CertificateException(java.security.cert.CertificateException) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) NHINDException(org.nhindirect.stagent.NHINDException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) KeyStoreException(java.security.KeyStoreException) NamingException(javax.naming.NamingException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NHINDException(org.nhindirect.stagent.NHINDException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ByteArrayInputStream(java.io.ByteArrayInputStream) SearchControls(javax.naming.directory.SearchControls) NamingException(javax.naming.NamingException)

Example 65 with SearchControls

use of javax.naming.directory.SearchControls in project nhin-d by DirectProject.

the class LdapPublicCertUtilImpl method getDefaultSearchControls.

/**
	 * Gets the search controls for searching the LDAP server.  The default controls use SUBTREE_SCOPE and 
	 * return the userSMIMECertificate attribute.
	 * @return A search control object.
	 */
protected SearchControls getDefaultSearchControls() {
    SearchControls ctls = new SearchControls();
    ctls.setReturningObjFlag(true);
    ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    ctls.setReturningAttributes(new String[] { CERT_ATTRIBUTE, CERT_ATTRIBUTE_BINARY });
    return ctls;
}
Also used : SearchControls(javax.naming.directory.SearchControls)

Aggregations

SearchControls (javax.naming.directory.SearchControls)70 SearchResult (javax.naming.directory.SearchResult)55 NamingException (javax.naming.NamingException)35 ArrayList (java.util.ArrayList)24 NamingEnumeration (javax.naming.NamingEnumeration)21 Attributes (javax.naming.directory.Attributes)21 Attribute (javax.naming.directory.Attribute)19 DirContext (javax.naming.directory.DirContext)15 InitialDirContext (javax.naming.directory.InitialDirContext)14 IOException (java.io.IOException)8 LdapContext (javax.naming.ldap.LdapContext)8 HashMap (java.util.HashMap)5 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)5 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)5 HashSet (java.util.HashSet)4 LinkedHashSet (java.util.LinkedHashSet)4 Map (java.util.Map)4 PartialResultException (javax.naming.PartialResultException)4 Control (javax.naming.ldap.Control)4 LoginException (javax.security.auth.login.LoginException)4