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;
}
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;
}
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;
}
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);
}
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;
}
Aggregations