/** * Load up the DNS JNDI context provider. */ public static DirContext getDirContext() { Hashtable<String, String> env = new Hashtable<String, String>(); env.put(JAVA_NAMING_FACTORY_INITIAL, DNS_NAMING_FACTORY); env.put(JAVA_NAMING_PROVIDER_URL, DNS_PROVIDER_URL); try { return new InitialDirContext(env); } catch (Throwable e) { throw new RuntimeException("Cannot get dir context for some reason", e); } }
InitialDirContext dirContext = createDnsDirContext(); try { Attributes attributes = dirContext.getAttributes(host, new String[]{"TXT"}); Attribute attribute = attributes.get("TXT"); if (attribute != null) { NamingEnumeration<?> txtRecordEnumeration = attribute.getAll(); if (txtRecordEnumeration.hasMore()) { additionalQueryParameters = ((String) txtRecordEnumeration.next()).replaceAll("\\s", ""); if (txtRecordEnumeration.hasMore()) { throw new MongoConfigurationException(format("Multiple TXT records found for host '%s'. Only one is permitted", host)); } finally { try { dirContext.close(); } catch (NamingException e) {
private boolean isAdmin(String accountName) { if (this.adminFilter != null) { try { InitialDirContext context = initContext(); String searchString = adminFilter.replace(":login", 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; }
private String[] getSMTPServerByJNDI(String to) throws Exception { String host=getDomainFromAddress(to); Properties jndiEnvironmentProperties = new Properties(); jndiEnvironmentProperties.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); InitialDirContext initialDirContext = new InitialDirContext(jndiEnvironmentProperties); Attributes attributes = initialDirContext.getAttributes(host, new String[] {"MX"}); Attribute attribute = attributes.get("MX"); String[] servers = new String[attribute.size()]; for (int i = 0; i < attribute.size(); i++) { servers[i] = attribute.get(i).toString(); servers[i]=servers[i].substring(servers[i].indexOf(" ") + 1, servers[i].length() -1); } return servers; }
ctx.addToEnvironment(Context.PROVIDER_URL, getProviderURL(dn)); answer = ctx.search( "", filter, if (answer == null || !answer.hasMoreElements())
@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()); } Assert.assertTrue(set.contains("uid=admin")); Assert.assertTrue(set.contains("ou=users")); Assert.assertTrue(set.contains("ou=groups")); Assert.assertTrue(set.contains("ou=configuration")); Assert.assertTrue(set.contains("prefNodeName=sysPrefRoot")); }
/** * Downloads a CRL from given LDAP url, e.g. * ldap://ldap.infonotary.com/dc=identity-ca,dc=infonotary,dc=com */ private static X509CRL downloadCRLFromLDAP(String ldapURL) throws CertificateException, NamingException, CRLException, CertificateVerificationException { Hashtable<String, String> env = new Hashtable<>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, ldapURL); // https://docs.oracle.com/javase/jndi/tutorial/ldap/connect/create.html // don't wait forever behind corporate proxy env.put("com.sun.jndi.ldap.connect.timeout", "1000"); DirContext ctx = new InitialDirContext(env); Attributes avals = ctx.getAttributes(""); Attribute aval = avals.get("certificateRevocationList;binary"); byte[] val = (byte[]) aval.get(); if (val == null || val.length == 0) { throw new CertificateVerificationException("Can not download CRL from: " + ldapURL); } else { InputStream inStream = new ByteArrayInputStream(val); CertificateFactory cf = CertificateFactory.getInstance("X.509"); return (X509CRL) cf.generateCRL(inStream); } }
return true; Hashtable<String, String> env = new Hashtable<String, String>(); env.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory"); env.put("java.naming.provider.url", ldapUrl); env.put("java.naming.security.authentication", "simple"); env.put("java.naming.security.principal", username + ConstUtils.EMAIL_SUFFIX); DirContext ctx = null; try { ctx = new InitialDirContext(env); if (ctx != null) { return true; if (ctx != null) { try { ctx.close(); } catch (Exception e) { logger.error(e.getMessage(), e);
/** * verify if there is a mail server registered to the domain name. and return the email servers count */ public static int mailServersCount( String hostName ) throws NamingException { Hashtable<String, String> env = new Hashtable<String, String>(); env.put( "java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory" ); DirContext ictx = new InitialDirContext( env ); Attributes attrs = ictx.getAttributes( hostName, new String[] { "MX" } ); Attribute attr = attrs.get( "MX" ); if ( attr == null ) { return ( 0 ); } return ( attr.size() ); }
Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory"); DirContext ctx = new InitialDirContext(env); Attributes attrs = ctx.getAttributes(lookup, new String[] {"PTR"}); for (NamingEnumeration ae = attrs.getAll(); ae.hasMoreElements();) { Attribute attr = (Attribute) ae.next(); for (Enumeration vals = attr.getAll(); vals.hasMoreElements();) { Object elem = vals.nextElement(); if ("PTR".equals(attr.getID()) && elem != null) { return elem.toString(); ctx.close(); } catch (Exception e) {
Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory"); env.put("java.naming.provider.url", "dns://"); DirContext ctx = new InitialDirContext(env); String dnsUrls = (String) ctx.getEnvironment().get("java.naming.provider.url");
private static ArrayList<String> getMX( String hostName ) throws NamingException { // Perform a DNS lookup for MX records in the domain Hashtable<String, String> env = new Hashtable<String, String>(); env.put( "java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory" ); DirContext ictx = new InitialDirContext( env ); Attributes attrs = ictx.getAttributes( hostName, new String[] { "MX" } ); Attribute attr = attrs.get( "MX" ); // if we don't have an MX record, try the machine itself if ( ( attr == null ) || ( attr.size() == 0 ) ) { attrs = ictx.getAttributes( hostName, new String[] { "A" } ); attr = attrs.get( "A" ); if ( attr == null ) { throw new NamingException( BaseMessages.getString( PKG, "MailValidator.NoMatchName", hostName ) ); } } // Huzzah! we have machines to try. Return them as an array list // NOTE: We SHOULD take the preference into account to be absolutely // correct. This is left as an exercise for anyone who cares. ArrayList<String> res = new ArrayList<String>(); NamingEnumeration<?> en = attr.getAll(); while ( en.hasMore() ) { String x = (String) en.next(); String[] f = x.split( " " ); if ( f[1].endsWith( "." ) ) { f[1] = f[1].substring( 0, ( f[1].length() - 1 ) ); } res.add( f[1] ); } return res; }
public void start() throws Exception { LOG.info("connecting..."); Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); this.ldapURI = getUri(); LOG.debug(" URI [{}]", this.ldapURI); env.put(Context.PROVIDER_URL, this.ldapURI.toString()); if (anonymousAuthentication) { LOG.debug(" login credentials [anonymous]"); while (!isConnected) { try { context = new InitialDirContext(env); isConnected = true; } catch (CommunicationException err) { LOG.debug(" filter [{}]", searchFilter); LOG.debug(" scope [{}]", searchControls.getSearchScope()); NamingEnumeration<SearchResult> results = context.search(base, searchFilter, searchControls); while (results.hasMore()) { addConnector(results.next()); EventDirContext eventContext = (EventDirContext) context.lookup(""); eventContext.addNamingListener(base, searchFilter, searchControls, this); } else { // otherwise close context (i.e. connection as it is no longer needed) context.close();
Hashtable<String, String> env = new Hashtable<String, String>(); env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); env.put("java.naming.provider.url", "dns:"); DirContext ctx = new InitialDirContext(env); Attributes attributes = ctx.getAttributes(srvName, new String[]{"SRV"}); NamingEnumeration<? extends Attribute> records = attributes.getAll(); while (records.hasMore()) { Attribute next = records.next(); NamingEnumeration<String> values = (NamingEnumeration<String>) next.getAll(); while (values.hasMore()) { String dns = values.next(); String[] split = dns.split(" ");
Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://1.2.3.4:389"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, domain + "\\" + username); ctx = new InitialDirContext(env); SearchResult sr = (SearchResult) ns.next(); sr = (SearchResult) result.next(); break;
String url = "ldap://directory.cornell.edu/o=Cornell%20University,c=US"; Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, url); DirContext context = new InitialDirContext(env); SearchControls ctrl = new SearchControls(); ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE); NamingEnumeration enumeration = context.search("", query, ctrl); while (enumeration.hasMore()) { SearchResult result = (SearchResult) enumeration.next(); Attributes attribs = result.getAttributes(); NamingEnumeration values = ((BasicAttribute) attribs.get(attribute)).getAll(); while (values.hasMore()) { if (output.length() > 0) { output.append("|"); } output.append(values.next().toString()); } }
Hashtable<String, Object> env = new Hashtable<String, Object>(); env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); env.put("java.naming.provider.url", "dns://<your DNS server>"); DirContext ictx = new InitialDirContext(env); Attributes attrs = ictx.getAttributes("www.heise.de", new String[] {"A", "AAAA"}); NamingEnumeration<? extends Attribute> e = attrs.getAll(); while(e.hasMoreElements()) { Attribute a = e.next(); System.out.println(a.getID() + " = " + a.get()); }
public static DirContext createDirContext(Map<String, String> environment) throws NamingException { return new InitialDirContext(new Hashtable<>(environment)); } }
props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); props.put(Context.PROVIDER_URL, "ldap://LDAPSERVER:PORT"); props.put(Context.SECURITY_PRINCIPAL, "uid=adminuser,ou=special users,o=xx.com");//adminuser - User with special priviledge, dn user props.put(Context.SECURITY_CREDENTIALS, "adminpassword");//dn user password InitialDirContext context = new InitialDirContext(props); ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE); NamingEnumeration<javax.naming.directory.SearchResult> answers = context.search("o=xx.com", "(uid=" + username + ")", ctrls); javax.naming.directory.SearchResult result = answers.nextElement(); props.put(Context.SECURITY_CREDENTIALS, password); context = new InitialDirContext(props); } catch (Exception e) { return false;
@Override public Set<String> discoverMembers() { LOGGER.debug("CELLAR DNS: query services with name [{}]", dnsService); Set<String> members = new HashSet<String>(); try { DirContext ctx = new InitialDirContext(this.dnsEnv); Attributes attrs = ctx.getAttributes(this.dnsService, new String[]{"SRV"}); NamingEnumeration<?> servers = attrs.get("srv").getAll(); while (servers.hasMore()) { String dns = (String)servers.next(); String[] split = dns.split(" "); members.add(split[3] + ":" + split[2]); } } catch (Exception e) { LOGGER.error("CELLAR DNS: can't get service", e); } return members; }