Search in sources :

Example 71 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project Activiti by Activiti.

the class LDAPTemplate method execute.

public <T> T execute(LDAPCallBack<T> ldapCallBack) {
    InitialDirContext initialDirContext = null;
    try {
        initialDirContext = LDAPConnectionUtil.creatDirectoryContext(ldapConfigurator);
    } catch (Exception e) {
        LOGGER.info("Could not create LDAP connection : " + e.getMessage(), e);
    }
    T result = ldapCallBack.executeInContext(initialDirContext);
    LDAPConnectionUtil.closeDirectoryContext(initialDirContext);
    return result;
}
Also used : InitialDirContext(javax.naming.directory.InitialDirContext)

Example 72 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project geode by apache.

the class SocketCreator method reverseDNS.

/**
   * This method uses JNDI to look up an address in DNS and return its name
   * 
   * @param addr
   *
   * @return the host name associated with the address or null if lookup isn't possible or there is
   *         no host name for this address
   */
public static String reverseDNS(InetAddress addr) {
    byte[] addrBytes = addr.getAddress();
    // reverse the address suitable for reverse lookup
    String lookup = "";
    for (int index = addrBytes.length - 1; index >= 0; index--) {
        lookup = lookup + (addrBytes[index] & 0xff) + '.';
    }
    lookup += "in-addr.arpa";
    try {
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
        DirContext ctx = new InitialDirContext(env);
        Attributes attrs = ctx.getAttributes(lookup, new String[] { "PTR" });
        for (NamingEnumeration ae = attrs.getAll(); ae.hasMoreElements(); ) {
            Attribute attr = (Attribute) ae.next();
            for (Enumeration vals = attr.getAll(); vals.hasMoreElements(); ) {
                Object elem = vals.nextElement();
                if ("PTR".equals(attr.getID()) && elem != null) {
                    return elem.toString();
                }
            }
        }
        ctx.close();
    } catch (Exception e) {
    // ignored
    }
    return null;
}
Also used : Enumeration(java.util.Enumeration) NamingEnumeration(javax.naming.NamingEnumeration) Attribute(javax.naming.directory.Attribute) Hashtable(java.util.Hashtable) Attributes(javax.naming.directory.Attributes) NamingEnumeration(javax.naming.NamingEnumeration) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) KeyStoreException(java.security.KeyStoreException) GeneralSecurityException(java.security.GeneralSecurityException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) SSLException(javax.net.ssl.SSLException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BindException(java.net.BindException) SocketException(java.net.SocketException) SystemConnectException(org.apache.geode.SystemConnectException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) GemFireConfigException(org.apache.geode.GemFireConfigException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) UnknownHostException(java.net.UnknownHostException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException)

Example 73 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project gerrit by GerritCodeReview.

the class Helper method authenticate.

DirContext authenticate(String dn, String password) throws AccountException {
    final Properties env = createContextProperties();
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, dn);
    env.put(Context.SECURITY_CREDENTIALS, password);
    env.put(Context.REFERRAL, referral);
    try {
        return new InitialDirContext(env);
    } catch (NamingException e) {
        throw new AuthenticationFailedException("Incorrect username or password", e);
    }
}
Also used : AuthenticationFailedException(com.google.gerrit.server.account.AuthenticationFailedException) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) Properties(java.util.Properties)

Example 74 with InitialDirContext

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

the class LdapPublicCertUtilImpl method ldapSearch.

/**
	 * Searches for certificates in public LDAP servers using the subject name.
	 * @param subjectName The subject's email address or domain name.
	 * @return Collection of certificates matching the LDAP query for the subject name.
	 */
public Collection<X509Certificate> ldapSearch(String subjectName) {
    final Collection<X509Certificate> retVal = new ArrayList<X509Certificate>();
    String domainName;
    // find by host
    int index;
    if ((index = subjectName.indexOf("@")) > -1)
        domainName = subjectName.substring(index + 1);
    else
        domainName = subjectName;
    final String lookupName = LDAP_SRV_PREFIX + domainName;
    InitialDirContext ctx = null;
    try {
        ctx = getDirContext(lookupName);
        if (ctx != null) {
            // discover the naming contexts
            List<String> dNs = getBaseNamingContexts(ctx);
            if (!dNs.isEmpty()) {
                for (String dn : dNs) {
                    NamingEnumeration<SearchResult> searchResult = ctx.search(dn, EMAIL_ATTRIBUTE + "=" + subjectName, getDefaultSearchControls());
                    while (searchResult != null && searchResult.hasMore()) {
                        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)
                                Attribute certAttribute = certAttributes.get(CERT_ATTRIBUTE_BINARY);
                                // binary modifier
                                if (certAttribute == null)
                                    certAttribute = certAttributes.get(CERT_ATTRIBUTE);
                                if (certAttribute != null) {
                                    NamingEnumeration<? extends Object> allValues = certAttribute.getAll();
                                    // LDAP may contain a collection of certificates.
                                    while (allValues.hasMoreElements()) {
                                        byte[] rawCert = null;
                                        Object obj = allValues.nextElement();
                                        rawCert = (byte[]) obj;
                                        final CertificateFactory cf = CertificateFactory.getInstance("X.509");
                                        final ByteArrayInputStream inputStream = new ByteArrayInputStream(rawCert);
                                        try {
                                            X509Certificate addCert = (X509Certificate) cf.generateCertificate(inputStream);
                                            retVal.add(addCert);
                                        } finally {
                                            IOUtils.closeQuietly(inputStream);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new NHINDException("", e);
    } finally {
        this.closeDirContext(ctx);
    }
    return retVal;
}
Also used : Attribute(javax.naming.directory.Attribute) ArrayList(java.util.ArrayList) Attributes(javax.naming.directory.Attributes) SearchResult(javax.naming.directory.SearchResult) InitialDirContext(javax.naming.directory.InitialDirContext) CertificateFactory(java.security.cert.CertificateFactory) NHINDException(org.nhindirect.stagent.NHINDException) X509Certificate(java.security.cert.X509Certificate) NamingException(javax.naming.NamingException) UnknownHostException(java.net.UnknownHostException) NHINDException(org.nhindirect.stagent.NHINDException) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 75 with InitialDirContext

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

the class SimpleLDAPAuthenticationManagerImpl method getNameFromId.

private String getNameFromId(String id) throws NamingException {
    if (!isBindWithoutSearch()) {
        InitialDirContext ctx = createSearchInitialDirContext();
        try {
            SearchControls searchControls = new SearchControls();
            searchControls.setReturningAttributes(new String[] {});
            searchControls.setCountLimit(1l);
            searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            NamingEnumeration<?> namingEnum = null;
            LOGGER.debug("Searching for '{}'", id);
            namingEnum = ctx.search(_searchContext, _searchFilter, new String[] { id }, searchControls);
            if (namingEnum.hasMore()) {
                SearchResult result = (SearchResult) namingEnum.next();
                String name = result.getNameInNamespace();
                LOGGER.debug("Found '{}' DN '{}'", id, name);
                return name;
            } else {
                LOGGER.debug("Not found '{}'", id);
                return null;
            }
        } finally {
            closeSafely(ctx);
        }
    } else {
        return id;
    }
}
Also used : SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) InitialDirContext(javax.naming.directory.InitialDirContext)

Aggregations

InitialDirContext (javax.naming.directory.InitialDirContext)131 DirContext (javax.naming.directory.DirContext)71 NamingException (javax.naming.NamingException)67 Hashtable (java.util.Hashtable)51 Attributes (javax.naming.directory.Attributes)34 Attribute (javax.naming.directory.Attribute)24 SearchResult (javax.naming.directory.SearchResult)22 SearchControls (javax.naming.directory.SearchControls)19 IOException (java.io.IOException)11 CommunicationException (javax.naming.CommunicationException)11 Test (org.junit.Test)11 Properties (java.util.Properties)10 BasicAttributes (javax.naming.directory.BasicAttributes)9 ArrayList (java.util.ArrayList)8 LoginException (javax.security.auth.login.LoginException)8 NameClassPair (javax.naming.NameClassPair)7 HashSet (java.util.HashSet)6 NamingEnumeration (javax.naming.NamingEnumeration)6 BasicAttribute (javax.naming.directory.BasicAttribute)6 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)5