Search in sources :

Example 41 with DirContext

use of javax.naming.directory.DirContext in project jdk8u_jdk by JetBrains.

the class DirContextStringPair method getTargetContext.

protected DirContextStringPair getTargetContext(String name) throws NamingException {
    if (cpe.getResolvedObj() == null)
        throw (NamingException) cpe.fillInStackTrace();
    Context ctx = NamingManager.getContext(cpe.getResolvedObj(), cpe.getAltName(), cpe.getAltNameCtx(), env);
    if (ctx instanceof DirContext)
        return new DirContextStringPair((DirContext) ctx, name);
    if (ctx instanceof Resolver) {
        Resolver res = (Resolver) ctx;
        ResolveResult rr = res.resolveToClass(name, DirContext.class);
        // Reached a DirContext; return result.
        DirContext dctx = (DirContext) rr.getResolvedObj();
        Name tmp = rr.getRemainingName();
        String remains = (tmp != null) ? tmp.toString() : "";
        return (new DirContextStringPair(dctx, remains));
    }
    // Resolve all the way using lookup().  This may allow the operation
    // to succeed if it doesn't require the penultimate context.
    Object ultimate = ctx.lookup(name);
    if (ultimate instanceof DirContext) {
        return (new DirContextStringPair((DirContext) ultimate, ""));
    }
    throw (NamingException) cpe.fillInStackTrace();
}
Also used : DirContext(javax.naming.directory.DirContext) Context(javax.naming.Context) NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext) CompositeName(javax.naming.CompositeName) Name(javax.naming.Name)

Example 42 with DirContext

use of javax.naming.directory.DirContext in project jdk8u_jdk by JetBrains.

the class ldapURLContextFactory method getUsingURLIgnoreRootDN.

static ResolveResult getUsingURLIgnoreRootDN(String url, Hashtable<?, ?> env) throws NamingException {
    LdapURL ldapUrl = new LdapURL(url);
    DirContext ctx = new LdapCtx("", ldapUrl.getHost(), ldapUrl.getPort(), env, ldapUrl.useSsl());
    String dn = (ldapUrl.getDN() != null ? ldapUrl.getDN() : "");
    // Represent DN as empty or single-component composite name.
    CompositeName remaining = new CompositeName();
    if (!"".equals(dn)) {
        // if nonempty, add component
        remaining.add(dn);
    }
    return new ResolveResult(ctx, remaining);
}
Also used : LdapURL(com.sun.jndi.ldap.LdapURL) LdapCtx(com.sun.jndi.ldap.LdapCtx) DirContext(javax.naming.directory.DirContext)

Example 43 with DirContext

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

the class LDAPGroupStore method getConnection.

protected DirContext getConnection() {
    //JNDI boilerplate to connect to an initial context
    DirContext context = (DirContext) contexts.get("context");
    if (context == null) {
        Hashtable jndienv = new Hashtable();
        jndienv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        jndienv.put(Context.SECURITY_AUTHENTICATION, "simple");
        if (url.startsWith("ldaps")) {
            // Handle SSL connections
            String newurl = url.substring(0, 4) + url.substring(5);
            jndienv.put(Context.SECURITY_PROTOCOL, "ssl");
            jndienv.put(Context.PROVIDER_URL, newurl);
        } else {
            jndienv.put(Context.PROVIDER_URL, url);
        }
        if (logonid != null)
            jndienv.put(Context.SECURITY_PRINCIPAL, logonid);
        if (logonpassword != null)
            jndienv.put(Context.SECURITY_CREDENTIALS, logonpassword);
        try {
            context = new InitialDirContext(jndienv);
        } catch (NamingException nex) {
            log.error("LDAPGroupStore: unable to get context", nex);
        }
        contexts.put("context", context);
    }
    return context;
}
Also used : Hashtable(java.util.Hashtable) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 44 with DirContext

use of javax.naming.directory.DirContext in project jetcd by coreos.

the class DnsSrvNameResolver method getServers.

@Override
protected List<ResolvedServerInfo> getServers() {
    try {
        DirContext ctx = new InitialDirContext(ENV);
        NamingEnumeration<?> resolved = ctx.getAttributes(name, ATTRIBUTE_IDS).get("srv").getAll();
        List<ResolvedServerInfo> servers = new LinkedList<>();
        while (resolved.hasMore()) {
            servers.add(srvRecordToServerInfo((String) resolved.next()));
        }
        return servers;
    } catch (Exception e) {
        LOGGER.warn("", e);
    }
    return Collections.emptyList();
}
Also used : DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext) ResolvedServerInfo(io.grpc.ResolvedServerInfo) LinkedList(java.util.LinkedList)

Example 45 with DirContext

use of javax.naming.directory.DirContext in project tomcat by apache.

the class JNDIRealm method authenticate.

// ---------------------------------------------------------- Realm Methods
/**
     * Return the Principal associated with the specified username and
     * credentials, if there is one; otherwise return <code>null</code>.
     *
     * If there are any errors with the JDBC connection, executing
     * the query or anything we return null (don't authenticate). This
     * event is also logged, and the connection will be closed so that
     * a subsequent request will automatically re-open it.
     *
     * @param username Username of the Principal to look up
     * @param credentials Password or other credentials to use in
     *  authenticating this username
     * @return the associated principal, or <code>null</code> if there is none.
     */
@Override
public Principal authenticate(String username, String credentials) {
    DirContext context = null;
    Principal principal = null;
    try {
        // Ensure that we have a directory context available
        context = open();
        // time before giving up.
        try {
            // Authenticate the specified username if possible
            principal = authenticate(context, username, credentials);
        } catch (NullPointerException | CommunicationException | ServiceUnavailableException e) {
            /* BZ 42449 - Catch NPE - Kludge Sun's LDAP provider
                   with broken SSL
                */
            // log the exception so we know it's there.
            containerLog.info(sm.getString("jndiRealm.exception.retry"), e);
            // close the connection so we know it will be reopened.
            if (context != null)
                close(context);
            // open a new directory context.
            context = open();
            // Try the authentication again.
            principal = authenticate(context, username, credentials);
        }
        // Release this context
        release(context);
        // Return the authenticated Principal (if any)
        return principal;
    } catch (NamingException e) {
        // Log the problem for posterity
        containerLog.error(sm.getString("jndiRealm.exception"), e);
        // Close the connection so that it gets reopened next time
        if (context != null)
            close(context);
        // Return "not authenticated" for this request
        if (containerLog.isDebugEnabled())
            containerLog.debug("Returning null principal.");
        return null;
    }
}
Also used : CommunicationException(javax.naming.CommunicationException) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) ServiceUnavailableException(javax.naming.ServiceUnavailableException) Principal(java.security.Principal)

Aggregations

DirContext (javax.naming.directory.DirContext)76 NamingException (javax.naming.NamingException)32 InitialDirContext (javax.naming.directory.InitialDirContext)32 SearchResult (javax.naming.directory.SearchResult)26 SearchControls (javax.naming.directory.SearchControls)22 Attributes (javax.naming.directory.Attributes)18 Attribute (javax.naming.directory.Attribute)16 NamingEnumeration (javax.naming.NamingEnumeration)14 Test (org.junit.Test)14 Hashtable (java.util.Hashtable)12 DistinguishedName (org.springframework.ldap.core.DistinguishedName)11 Name (javax.naming.Name)7 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 BasicAttribute (javax.naming.directory.BasicAttribute)6 BasicAttributes (javax.naming.directory.BasicAttributes)6 Authentication (org.springframework.security.core.Authentication)5 Principal (java.security.Principal)3 LdapContext (javax.naming.ldap.LdapContext)3