Search in sources :

Example 51 with InitialDirContext

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

the class DnsDiscoveryService method discoverNodes.

@Override
public Iterable<DiscoveryNode> discoverNodes() {
    LOGGER.log(Level.FINER, "Starting Domain Node Discovery");
    List<DiscoveryNode> nodes = new LinkedList<>();
    for (String host : settings) {
        String hostname;
        String port;
        int colon = host.indexOf(':');
        if (colon == -1) {
            hostname = host;
            port = DEFAULT_PORT;
        } else {
            hostname = host.substring(0, colon);
            port = host.substring(colon + 1);
        }
        try {
            InetAddress[] addresses = InetAddress.getAllByName(hostname);
            for (InetAddress address : addresses) {
                if (!address.isLoopbackAddress()) {
                    LOGGER.log(Level.FINE, "Adding Node {0}", address);
                    nodes.add(new SimpleDiscoveryNode(new Address(address.getHostAddress(), Integer.valueOf(port))));
                }
            }
        } catch (UnknownHostException ex) {
            LOGGER.log(Level.FINEST, ex.getMessage());
            // not a known host, do a DNS lookup
            try {
                DirContext urlContext = new InitialDirContext();
                Attributes attributes = urlContext.getAttributes("dns:/" + hostname, new String[] { "A" });
                NamingEnumeration record = attributes.get("A").getAll();
                while (record.hasMore()) {
                    String address = record.next().toString();
                    LOGGER.log(Level.FINE, "Adding Node {0}", address);
                    nodes.add(new SimpleDiscoveryNode(new Address(address, Integer.valueOf(port))));
                }
            } catch (NamingException | UnknownHostException ex1) {
                LOGGER.log(Level.WARNING, "Unable to find DNS record for {0}", hostname);
            }
        }
    }
    return nodes;
}
Also used : SimpleDiscoveryNode(com.hazelcast.spi.discovery.SimpleDiscoveryNode) DiscoveryNode(com.hazelcast.spi.discovery.DiscoveryNode) Address(com.hazelcast.cluster.Address) InetAddress(java.net.InetAddress) UnknownHostException(java.net.UnknownHostException) Attributes(javax.naming.directory.Attributes) NamingEnumeration(javax.naming.NamingEnumeration) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext) LinkedList(java.util.LinkedList) SimpleDiscoveryNode(com.hazelcast.spi.discovery.SimpleDiscoveryNode) InetAddress(java.net.InetAddress)

Example 52 with InitialDirContext

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

the class LDAPRealm method findAndBind.

/**
 * Supports mode=find-bind. See class documentation.
 *
 * @param _username
 * @param _password
 * @return
 * @throws LoginException
 */
public String[] findAndBind(String _username, char[] _password) throws LoginException {
    // do search for user, substituting %s for username
    _username = RFC2254Encode(_username);
    StringBuilder sb = new StringBuilder(getProperty(PARAM_SEARCH_FILTER));
    substitute(sb, SUBST_SUBJECT_NAME, _username);
    String userid = sb.toString();
    // attempt to bind as the user
    DirContext ctx = null;
    String srcFilter = null;
    String[] grpList = null;
    String dynFilter = null;
    String dynMember = getProperty(PARAM_DYNAMIC_GRP_TARGET);
    try {
        ctx = new InitialDirContext(getLdapBindProps());
        String realUserDN = userSearch(ctx, getProperty(PARAM_USERDN), userid);
        if (realUserDN == null) {
            String msg = sm.getString("ldaprealm.usernotfound", _username);
            throw new LoginException(msg);
        }
        boolean bindSuccessful = bindAsUser(realUserDN, _password);
        if (!bindSuccessful) {
            String msg = sm.getString("ldaprealm.bindfailed", realUserDN);
            throw new LoginException(msg);
        }
        // search groups using above connection, substituting %d (and %s)
        sb = new StringBuilder(getProperty(PARAM_GRP_SEARCH_FILTER));
        StringBuilder dynSb = new StringBuilder(getProperty(PARAM_DYNAMIC_GRP_FILTER));
        substitute(sb, SUBST_SUBJECT_NAME, _username);
        substitute(sb, SUBST_SUBJECT_DN, realUserDN);
        substitute(dynSb, SUBST_SUBJECT_NAME, _username);
        substitute(dynSb, SUBST_SUBJECT_DN, realUserDN);
        srcFilter = sb.toString();
        dynFilter = dynSb.toString();
        List<String> groupsList = new ArrayList<>();
        groupsList.addAll(groupSearch(ctx, getProperty(PARAM_GRPDN), srcFilter, getProperty(PARAM_GRP_TARGET)));
        // search filter is constructed internally as
        // as a groupofURLS
        groupsList.addAll(dynamicGroupSearch(ctx, getProperty(PARAM_GRPDN), dynMember, dynFilter, getProperty(PARAM_GRP_TARGET)));
        grpList = new String[groupsList.size()];
        groupsList.toArray(grpList);
    } catch (Exception e) {
        LoginException le = new LoginException(e.toString());
        le.initCause(e);
        _logger.log(Level.SEVERE, "ldaprealm.exception", le);
        throw le;
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException e) {
            }
        }
    }
    if (_logger.isLoggable(FINE)) {
        _logger.log(FINE, "LDAP:Group search filter: {0}", srcFilter);
        StringBuilder gb = new StringBuilder();
        gb.append("Group memberships found: ");
        if (grpList.length > 0) {
            for (String grpList1 : grpList) {
                gb.append(" ").append(grpList1);
            }
        } else {
            gb.append("(null)");
        }
        if (_logger.isLoggable(FINE)) {
            _logger.log(FINE, "LDAP: {0}", gb.toString());
        }
    }
    grpList = addAssignGroups(grpList);
    grpList = this.addMappedGroupNames(grpList);
    setGroupNames(_username, grpList);
    if (_logger.isLoggable(FINE)) {
        _logger.log(FINE, "LDAP: login succeeded for: {0}", _username);
    }
    return grpList;
}
Also used : ArrayList(java.util.ArrayList) LoginException(javax.security.auth.login.LoginException) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) 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) NoSuchUserException(com.sun.enterprise.security.auth.realm.NoSuchUserException)

Example 53 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project sonarqube by SonarSource.

the class LdapContextFactory method createInitialDirContextUsingGssapi.

private InitialDirContext createInitialDirContextUsingGssapi(String principal, String credentials) throws NamingException {
    Configuration.setConfiguration(new Krb5LoginConfiguration());
    InitialDirContext initialDirContext;
    try {
        LoginContext lc = new LoginContext(getClass().getName(), new CallbackHandlerImpl(principal, credentials));
        lc.login();
        initialDirContext = Subject.doAs(lc.getSubject(), new PrivilegedExceptionAction<InitialDirContext>() {

            @Override
            public InitialDirContext run() throws NamingException {
                Properties env = new Properties();
                env.put(Context.INITIAL_CONTEXT_FACTORY, factory);
                env.put(Context.PROVIDER_URL, providerUrl);
                env.put(Context.REFERRAL, referral);
                return new InitialLdapContext(env, null);
            }
        });
    } catch (LoginException | PrivilegedActionException e) {
        NamingException namingException = new NamingException(e.getMessage());
        namingException.initCause(e);
        throw namingException;
    }
    return initialDirContext;
}
Also used : LoginContext(javax.security.auth.login.LoginContext) PrivilegedActionException(java.security.PrivilegedActionException) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LoginException(javax.security.auth.login.LoginException) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) Properties(java.util.Properties)

Example 54 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project athenz by yahoo.

the class LDAPAuthority method getDirContext.

DirContext getDirContext(String finalDN, String password) throws NamingException {
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, providerURL);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, finalDN);
    env.put(Context.SECURITY_CREDENTIALS, password);
    return new InitialDirContext(env);
}
Also used : Hashtable(java.util.Hashtable) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 55 with InitialDirContext

use of javax.naming.directory.InitialDirContext 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", encodeForLdap(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()) {
                    LOGGER.warn("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

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