use of javax.naming.directory.InitialDirContext in project activemq-artemis by apache.
the class LDAPModuleRoleExpansionTest method testRunning.
@Test
public void testRunning() throws Exception {
Hashtable<String, String> env = new Hashtable<>();
env.put(Context.PROVIDER_URL, "ldap://localhost:1024");
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, PRINCIPAL);
env.put(Context.SECURITY_CREDENTIALS, CREDENTIALS);
DirContext ctx = new InitialDirContext(env);
HashSet<String> set = new HashSet<>();
NamingEnumeration<NameClassPair> list = ctx.list("ou=system");
while (list.hasMore()) {
NameClassPair ncp = list.next();
set.add(ncp.getName());
}
assertTrue(set.contains("uid=admin"));
assertTrue(set.contains("ou=users"));
assertTrue(set.contains("ou=groups"));
assertTrue(set.contains("ou=configuration"));
assertTrue(set.contains("prefNodeName=sysPrefRoot"));
}
use of javax.naming.directory.InitialDirContext in project sonar-java by SonarSource.
the class B method lookupUser.
public User lookupUser(String username, String base, String[] requestedAttrs) {
DirContext dctx = new InitialDirContext(env);
String[] requestedAttrsLocal = new String[12];
SearchControls sc = new SearchControls();
// Noncompliant [[sc=31;ec=45]] {{Make sure that "requestedAttrs" is sanitized before use in this LDAP request.}}
sc.setReturningAttributes(requestedAttrs);
sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
String filter = "(&(objectClass=user)(sAMAccountName=" + username + "))";
NamingEnumeration results = // Noncompliant [[sc=45;ec=49]] {{Make sure that "base" is sanitized before use in this LDAP request.}}
dctx.search(// Noncompliant [[sc=45;ec=49]] {{Make sure that "base" is sanitized before use in this LDAP request.}}
base, // Noncompliant {{Make sure that "username" is sanitized before use in this LDAP request.}}
filter, sc);
results = // Noncompliant {{Make sure that "base" is sanitized before use in this LDAP request.}}
dctx.search(// Noncompliant {{Make sure that "base" is sanitized before use in this LDAP request.}}
base + "", // Noncompliant {{Make sure that "username" is sanitized before use in this LDAP request.}}
filter, sc);
// Noncompliant {{Make sure that "requestedAttrsField" is sanitized before use in this LDAP request.}}
sc.setReturningAttributes(requestedAttrsField);
// Noncompliant {{Make sure that "username" is sanitized before use in this LDAP request.}}
sc.setReturningAttributes(new String[] { " ", username });
// compliant
sc.setReturningAttributes(new String[] { " ", " Foo" });
// Noncompliant {{Make sure that "requestedAttrsLocal" is sanitized before use in this LDAP request.}}
sc.setReturningAttributes(requestedAttrsLocal);
javax.naming.directory.InitialDirContext idc = org.owasp.benchmark.helpers.Utils.getInitialDirContext();
// Noncompliant {{Make sure that "username" is sanitized before use in this LDAP request.}}
idc.search("name", filter, new javax.naming.directory.SearchControls());
// Compliant
idc.search("name", getAttributes(), null);
}
use of javax.naming.directory.InitialDirContext in project syncope by apache.
the class ApacheDSRootDseServlet method doGet.
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException {
try {
resp.setContentType("text/plain");
PrintWriter out = resp.getWriter();
out.println("*** ApacheDS RootDSE ***\n");
DirContext ctx = new InitialDirContext(this.createEnv());
SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(new String[] { "*", "+" });
ctls.setSearchScope(SearchControls.OBJECT_SCOPE);
NamingEnumeration<SearchResult> result = ctx.search("", "(objectClass=*)", ctls);
if (result.hasMore()) {
SearchResult entry = result.next();
Attributes as = entry.getAttributes();
NamingEnumeration<String> ids = as.getIDs();
while (ids.hasMore()) {
String id = ids.next();
Attribute attr = as.get(id);
for (int i = 0; i < attr.size(); ++i) {
out.println(id + ": " + attr.get(i));
}
}
}
ctx.close();
out.flush();
} catch (Exception e) {
throw new ServletException(e);
}
}
use of javax.naming.directory.InitialDirContext in project syncope by apache.
the class AbstractITCase method updateLdapRemoteObject.
protected void updateLdapRemoteObject(final String bindDn, final String bindPwd, final String objectDn, final Pair<String, String> attribute) {
InitialDirContext ctx = null;
try {
ctx = getLdapResourceDirContext(bindDn, bindPwd);
Attribute ldapAttribute = new BasicAttribute(attribute.getKey(), attribute.getValue());
ModificationItem[] item = new ModificationItem[1];
item[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, ldapAttribute);
ctx.modifyAttributes(objectDn, item);
} catch (Exception e) {
// ignore
} finally {
if (ctx != null) {
try {
ctx.close();
} catch (NamingException e) {
// ignore
}
}
}
}
use of javax.naming.directory.InitialDirContext in project syncope by apache.
the class AbstractITCase method getLdapResourceDirContext.
@SuppressWarnings({ "unchecked", "rawtypes", "UseOfObsoleteCollectionType" })
protected InitialDirContext getLdapResourceDirContext(final String bindDn, final String bindPwd) throws NamingException {
ResourceTO ldapRes = resourceService.read(RESOURCE_NAME_LDAP);
ConnInstanceTO ldapConn = connectorService.read(ldapRes.getConnector(), Locale.ENGLISH.getLanguage());
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://" + ldapConn.getConf("host").get().getValues().get(0) + ":" + ldapConn.getConf("port").get().getValues().get(0) + "/");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, bindDn == null ? ldapConn.getConf("principal").get().getValues().get(0) : bindDn);
env.put(Context.SECURITY_CREDENTIALS, bindPwd == null ? ldapConn.getConf("credentials").get().getValues().get(0) : bindPwd);
return new InitialDirContext(env);
}
Aggregations