private DirContext createDirContext(String securityPrincipal, char[] securityCredential, ReferralMode mode, SocketFactory socketFactory) throws NamingException { final ClassLoader oldClassLoader = setClassLoaderTo(targetClassLoader); try{ Hashtable<String, Object> env = new Hashtable<>(); env.put(InitialDirContext.INITIAL_CONTEXT_FACTORY, initialContextFactory); env.put(InitialDirContext.PROVIDER_URL, providerUrl); env.put(InitialDirContext.SECURITY_AUTHENTICATION, securityAuthentication); if (securityPrincipal != null) env.put(InitialDirContext.SECURITY_PRINCIPAL, securityPrincipal); initialContext = new InitialLdapContext(env, null); } catch (NamingException ne) { log.debugf(ne, "Could not create [%s]. Failed to connect to LDAP server.", InitialLdapContext.class);
public void close() throws KettleException { if ( ctx != null ) { try { ctx.close(); if ( log.isBasic() ) { log.logBasic( BaseMessages.getString( PKG, "LDAPInput.log.Disconnection.Done" ) ); } } catch ( Exception e ) { log.logError( BaseMessages.getString( PKG, "LDAPInput.Exception.ErrorDisconecting", e.toString() ) ); log.logError( Const.getStackTracker( e ) ); } finally { ctx = null; } } } }
Attributes attrs = getInitialContext().getAttributes( "", new String[] { "namingContexts" } ); Attribute attr = attrs.get( "namingContexts" ); setSearchBase( attr.get().toString() ); if ( log.isDetailed() ) { log.logDetailed( BaseMessages.getString( PKG, "LDAPInput.SearchBaseFound", getSearchBase() ) ); this.controls = new SearchControls(); if ( limitRows > 0 ) { this.controls.setCountLimit( limitRows ); this.controls.setTimeLimit( getTimeLimit() * 1000 ); ctls[index++] = ctlp; getInitialContext().setRequestControls( ctls ); this.results = getInitialContext().search( getSearchBase(), getFilter(), getSearchControls() );
@SuppressWarnings( "rawtypes" ) private void getPaths( String rootName, Map<String, Attributes> childs, List<String> paths ) throws Exception { NamingEnumeration ne = getInitialContext().list( rootName ); while ( ne.hasMore() ) { NameClassPair nameCP = (NameClassPair) ne.next(); childs.put( nameCP.getName() + "," + rootName, getInitialContext().getAttributes( nameCP.getName() + "," + rootName ) ); getPaths( nameCP.getName() + "," + rootName, childs, paths ); paths.add( nameCP.getName() + "," + rootName ); } }
getInitialContext().getAttributes( dn ); found = true; } catch ( NameNotFoundException n ) { Attributes attrs = buildAttributes( dn, attributes, values, multValuedSeparator ); getInitialContext().createSubcontext( dn, attrs ); return STATUS_INSERTED; getInitialContext().modifyAttributes( dn, DirContext.REPLACE_ATTRIBUTE, attrs ); return STATUS_UPDATED;
public Attributes getAttributes() throws KettleException { while ( !getSearchResult().hasMoreElements() ) { if ( isPagingUsed() ) { Control[] rc = getInitialContext().getResponseControls(); if ( rc != null ) { for ( int i = 0; i < rc.length; i++ ) { getInitialContext().setRequestControls( new Control[] { new SortControl( getSortingAttributesKeys(), Control.NONCRITICAL ), new PagedResultsControl( GetPagingSize(), cookie, Control.CRITICAL ) } ); } else { getInitialContext().setRequestControls( new Control[] { new PagedResultsControl( GetPagingSize(), cookie, Control.CRITICAL ) } ); this.results = getInitialContext().search( getSearchBase(), getFilter(), getSearchControls() ); } else { return null; while ( !getSearchResult().hasMoreElements() ) { return null; SearchResult searchResult = getSearchResult().next(); Attributes results = searchResult.getAttributes(); results.put( "dn", searchResult.getNameInNamespace() ); return results; } catch ( Exception e ) {
private InitialDirContext createInitialContext() throws NamingException { Hashtable<String, String> env = new Hashtable<>(5); env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(javax.naming.Context.PROVIDER_URL, ldapuri.toString()); env.put(javax.naming.Context.SECURITY_AUTHENTICATION, SECURITY_AUTHENTICATION); env.put(javax.naming.Context.SECURITY_PRINCIPAL, bindDN); env.put(javax.naming.Context.SECURITY_CREDENTIALS, bindPassword); return new InitialLdapContext(env, null); } //CHECKSTYLE:ON
Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://my.domain.com:389"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, "cn=administrator,cn=users,dc=my,dc=domain,dc=com"); env.put(Context.SECURITY_CREDENTIALS, "********"); LdapContext context = new InitialLdapContext(env, null); String searchBase = "cn=Partitions,cn=Configuration,dc=my,dc=domain,dc=com"; String searchFilter = "(&(objectcategory=Crossref)(netbiosname=*))"; SearchControls controls = new SearchControls(); controls.setSearchScope(SearchControls.ONELEVEL_SCOPE); NamingEnumeration answers = context.search(searchBase, searchFilter, controls); while (answers.hasMore()) { SearchResult rs = (SearchResult) answers.next(); String netBiosName = rs.getAttributes().get("NetBIOSName").get(0).toString(); System.out.println(netBiosName);
Hashtable env = new Hashtable(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://myldapserver:389/"); LdapContext ctx = new InitialLdapContext(env, null); ctx.setRequestControls(null); NamingEnumeration<?> namingEnum = ctx.search("CN=mycert,ou=CAs,dc=whatever,dc=com", "(objectclass=*)", getSimpleSearchControls()); while (namingEnum.hasMore ()) { SearchResult result = (SearchResult) namingEnum.next (); attrs = result.getAttributes (); System.out.println(attrs.get("cn")); cert = attrs.get("caCertificate;binary"); System.out.println(cert); System.out.println(attrs.get("objectclass")); namingEnum.close(); } catch (Exception e) { e.printStackTrace(); if (((Attribute)cert).size() == 0) { System.out.println("The cert attribute was cert.size == 0!"); System.exit(0); System.out.println("The cert attribute cert.size was: [" + ((Attribute)cert).size() + "]"); System.out.println("Here's cert again: [" + cert + "]"); for (NamingEnumeration e = attr.getAll();e.hasMore();) {
String baseUrl = (String) props.get(Context.PROVIDER_URL); baseUrl = baseUrl.substring(0, baseUrl.lastIndexOf('/')); props.put(Context.PROVIDER_URL, baseUrl); DirContext schemaCtx = new InitialLdapContext(props, null); SearchControls sc = new SearchControls(); sc.setSearchScope(SearchControls.OBJECT_SCOPE); sc.setReturningAttributes(new String[]{"subschemaSubentry"}); String subschemaSubentryDN = null; if (schemaDnSR.hasMore()) { sr = schemaDnSR.next(); subschemaSubentry = sr.getAttributes().get("subschemaSubentry"); if (subschemaSubentry != null && subschemaSubentry.size() > 0) { subschemaSubentryDN = (String) subschemaSubentry.get(); Attribute schemaAttr = schemaAttrs.get(attr); if (schemaAttr != null) { attrsResult.put(schemaAttr.getID(), (List<String>) Collections.list(schemaAttr.getAll()));
protected String bindDNAuthentication(InitialLdapContext ctx, String user, Object credential, String baseDN, String filter) throws NamingException SearchControls constraints = new SearchControls(); constraints.setSearchScope(searchScope); constraints.setTimeLimit(searchTimeLimit); String attrList[] = {distinguishedNameAttribute}; constraints.setReturningAttributes(attrList); results = ctx.search(baseDN, filter, filterArgs, constraints); if (!results.hasMore()) results.close(); throw PicketBoxMessages.MESSAGES.failedToFindBaseContextDN(baseDN); Attribute dn = attrs.get(distinguishedNameAttribute); if (dn != null) userDN = (String) dn.get(); if (sr.isRelative()) userDN = name + ("".equals(baseDN) ? "" : "," + baseDN); else userCtx.close();
protected InitialLdapContext openContext(String userDn, String password) { Hashtable<String, String> env = new Hashtable<>(); env.put(Context.INITIAL_CONTEXT_FACTORY, ldapConfiguration.getInitialContextFactory()); env.put(Context.SECURITY_AUTHENTICATION, ldapConfiguration.getSecurityAuthentication()); env.put(Context.PROVIDER_URL, ldapConfiguration.getServerUrl()); env.put(Context.SECURITY_PRINCIPAL, userDn); env.put(Context.SECURITY_CREDENTIALS, password); // for anonymous login if(ldapConfiguration.isAllowAnonymousLogin() && password.isEmpty()) { env.put(Context.SECURITY_AUTHENTICATION, "none"); } if(ldapConfiguration.isUseSsl()) { env.put(Context.SECURITY_PROTOCOL, "ssl"); } // add additional properties Map<String, String> contextProperties = ldapConfiguration.getContextProperties(); if(contextProperties != null) { env.putAll(contextProperties); } try { return new InitialLdapContext(env, null); } catch(AuthenticationException e) { throw new LdapAuthenticationException("Could not authenticate with LDAP server", e); } catch(NamingException e) { throw new IdentityProviderException("Could not connect to LDAP server", e); } }
@Override public void doInit(final Properties messageAttributes) throws PerfCakeException { final Hashtable<String, Object> env = new Hashtable<>(); env.put(Context.SECURITY_AUTHENTICATION, "simple"); if (ldapUsername != null) { env.put(Context.SECURITY_PRINCIPAL, ldapUsername); } if (ldapPassword != null) { env.put(Context.SECURITY_CREDENTIALS, ldapPassword); } env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, safeGetTarget(messageAttributes)); if (logger.isTraceEnabled()) { logger.trace("Connecting to " + safeGetTarget(messageAttributes)); } try { ctx = new InitialLdapContext(env, null); } catch (NamingException e) { throw new PerfCakeException("Cannot create LDAP context: ", e); } searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); }
Hashtable<String,String> environment = new Hashtable<String,String>(); environment.put("java.naming.provider.url", url); environment.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory"); InitialLdapContext context = new InitialLdapContext(environment, null); ... context.addToEnvironment("java.naming.security.principal", principal); context.addToEnvironment("java.naming.security.credentials", credentials); ... // execute some kind of search, based on your needs
private LdapContext createLdapContext() throws NamingException { return new InitialLdapContext(new Hashtable<Object, Object>(this.connectionProperties), null); }
SearchControls constraints = new SearchControls(); NamingEnumeration<SearchResult> result = ctx.search(roleContext, roleFilter, constraints); if (result.hasMore()) { SearchResult sr = result.next(); Attribute ldapUserId = sr.getAttributes().get(roleAttrId); if (ldapUserId.contains(groupId)) { exists = true; result.close(); if (ctx != null) { try { ctx.close(); } catch (NamingException e) { e.printStackTrace();
private LdapContext getContextForEnvironmentVariables(Hashtable<?, ?> environment) throws NamingException { Hashtable<Object, Object> tempEnv = new Hashtable<>(); tempEnv.putAll(environment); LdapContext context = new InitialLdapContext(tempEnv, null); return context; } }
private LdapContext createInitialDirContext(final String principal, final String password, final String providerUrl, final boolean isSystemContext, Long domainId) throws NamingException, IOException { Hashtable<String, String> environment = getEnvironment(principal, password, providerUrl, isSystemContext, domainId); s_logger.debug("initializing ldap with provider url: " + environment.get(Context.PROVIDER_URL)); return new InitialLdapContext(environment, null); }
ctx.destroySubcontext(roleDN); SearchControls controls = new SearchControls(); controls.setSearchScope(searchScope); controls.setReturningAttributes(roleAttr); controls.setTimeLimit(searchTimeLimit); NamingEnumeration answer = ctx.search(getUserContextDN(), roleFilter.toString(), filterArgs, controls); while (answer.hasMore()) SearchResult sr = (SearchResult) answer.next(); Attributes attrs = sr.getAttributes(); Attribute user = attrs.get( getUserRoleAttribute() ); user.remove(roleDN); ctx.modifyAttributes(sr.getNameInNamespace(), new ModificationItem[] { new ModificationItem(DirContext.REPLACE_ATTRIBUTE, user)}); answer.close(); ctx.close();
env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system"); env.put(Context.SECURITY_CREDENTIALS, "secret"); final LdapContext ctx = new InitialLdapContext(env, null); final SearchControls searchControls = new SearchControls(); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); NamingEnumeration<?> namingEnum = ctx.search("dc=jboss,dc=org", "(uid=*)", searchControls); while (namingEnum.hasMore()) { SearchResult sr = (SearchResult) namingEnum.next(); Attributes attrs = sr.getAttributes(); System.out.println(attrs.get("cn")); namingEnum.close(); ctx.close();