Search in sources :

Example 86 with InitialDirContext

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"));
}
Also used : Hashtable(java.util.Hashtable) NameClassPair(javax.naming.NameClassPair) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 87 with InitialDirContext

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);
}
Also used : SearchControls(javax.naming.directory.SearchControls) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext) SearchControls(javax.naming.directory.SearchControls) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 88 with InitialDirContext

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);
    }
}
Also used : Attribute(javax.naming.directory.Attribute) Attributes(javax.naming.directory.Attributes) SearchResult(javax.naming.directory.SearchResult) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext) ServletException(javax.servlet.ServletException) ServletException(javax.servlet.ServletException) SearchControls(javax.naming.directory.SearchControls) PrintWriter(java.io.PrintWriter)

Example 89 with InitialDirContext

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
            }
        }
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) ModificationItem(javax.naming.directory.ModificationItem) Attribute(javax.naming.directory.Attribute) BasicAttribute(javax.naming.directory.BasicAttribute) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) NamingException(javax.naming.NamingException)

Example 90 with InitialDirContext

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);
}
Also used : ResourceTO(org.apache.syncope.common.lib.to.ResourceTO) ConnInstanceTO(org.apache.syncope.common.lib.to.ConnInstanceTO) InitialDirContext(javax.naming.directory.InitialDirContext) Properties(java.util.Properties)

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