/** * 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() ); }
@Override public boolean verifyCertificate(X509Certificate certificate, Attributes attributes) throws NamingException { Attribute attribute = attributes.get(ldapAttribute); if (attribute == null) return false; final int size = attribute.size(); for (int i = 0; i < size; i++) { Object attrSerialNumber = attribute.get(i); if (attrSerialNumber != null){ BigInteger value = new BigInteger((String) attrSerialNumber); if (certificate.getSerialNumber().equals(value)) { return true; } } } 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; }
@Override public boolean verifyCertificate(X509Certificate certificate, Attributes attributes) throws NamingException { Attribute attribute = attributes.get(ldapAttribute); if (attribute == null) return false; final int size = attribute.size(); for (int i = 0; i < size; i++) { Object attrSubject = attribute.get(i); if (attrSubject != null){ X500Principal certSubjectX500Principal = certificate.getSubjectX500Principal(); X500Principal attSubjectX500Principal = new X500Principal((String) attrSubject); if ( certSubjectX500Principal.equals(attSubjectX500Principal) ) { return true; } } } return false; } }
/** * Resolve an attribute for a hostname. * * @param hostname * @param attrName * @return * @throws NamingException * @throws UnknownHostException */ @SuppressWarnings("rawtypes") private List<InetAddress> resolve(String hostname, String attrName) throws NamingException, UnknownHostException { Attributes attrs = context.getAttributes(hostname, new String[] { attrName }); List<InetAddress> inetAddresses = new ArrayList<>(); Attribute attr = attrs.get(attrName); if (attr != null && attr.size() > 0) { NamingEnumeration e = attr.getAll(); while (e.hasMore()) { InetAddress inetAddress = InetAddress.getByName("" + e.next()); inetAddresses.add(InetAddress.getByAddress(hostname, inetAddress.getAddress())); } } return inetAddresses; }
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; }
@Override public boolean verifyCertificate(X509Certificate certificate, Attributes attributes) throws NamingException, RealmUnavailableException { Attribute attribute = LdapUtil.getBinaryAttribute(attributes, ldapAttribute); if (attribute == null) return false; final int size = attribute.size(); try { for (int i = 0; i < size; i++) { Object attrCertificate = attribute.get(i); if (attrCertificate != null){ if (Arrays.equals(certificate.getEncoded(), (byte[]) attrCertificate)) { return true; } } } } catch (CertificateEncodingException e) { throw new RealmUnavailableException(e); } return false; } }
Attribute attr = attrs.get("CNAME"); if (attr != null && attr.size() > 0) { NamingEnumeration e = attr.getAll();
@Override public boolean verifyCertificate(X509Certificate certificate, Attributes attributes) throws NamingException, RealmUnavailableException { Attribute attribute = attributes.get(ldapAttribute); if (attribute == null) return false; final int size = attribute.size(); try { MessageDigest md = MessageDigest.getInstance(algorithm); String digest = ByteIterator.ofBytes(md.digest(certificate.getEncoded())).hexEncode(true).drainToString(); for (int i = 0; i < size; i++) { Object attrDigest = attribute.get(i); if (attrDigest != null){ if (digest.equalsIgnoreCase((String) attrDigest)) { return true; } } } } catch (NoSuchAlgorithmException | CertificateEncodingException e) { throw new RealmUnavailableException(e); } return false; } }
return Collections.emptyList(); WeightedHostAddress[] hosts = new WeightedHostAddress[srvRecords.size()]; final boolean directTLS = lookup.startsWith( "_xmpps-" ); // XEP-0368 for (int i = 0; i < srvRecords.size(); i++) { hosts[i] = new WeightedHostAddress(((String)srvRecords.get(i)).split(" "), directTLS);
Attribute memberOf = result.getAttributes().get("memberOf"); if (memberOf != null) {// null if this user belongs to no group at all for (int i = 0; i < memberOf.size(); i++) { Attributes atts = context.getAttributes(memberOf.get(i).toString(), new String[] { "CN" }); Attribute att = atts.get("CN");
@Override public <C extends Credential> C getCredential(final Class<C> credentialType, final String credentialAlgorithm, final AlgorithmParameterSpec parameterSpec, Supplier<Provider[]> providers) { if (credentialType != PasswordCredential.class) { return null; } try { Attribute attribute = LdapUtil.getBinaryAttribute(attributes, userPasswordAttributeName); if (attribute != null) { final int size = attribute.size(); for (int i = 0; i < size; i++) { byte[] value = (byte[]) attribute.get(i); Password password = parseUserPassword(value); if (credentialType.isAssignableFrom(PasswordCredential.class) && (credentialAlgorithm == null || credentialAlgorithm.equals(password.getAlgorithm()))) { return credentialType.cast(new PasswordCredential(password)); } } } } catch (NamingException | InvalidKeySpecException e) { if (log.isTraceEnabled()) { log.trace("Getting user-password credential " + credentialType.getName() + " failed. dn=" + distinguishedName, e); } } return null; }
public DirContextOperations localCompareAuthenticate(DirContextOperations user, String password) { boolean match = false; try { Attributes attributes = user.getAttributes(); Attribute attr = attributes.get(getPasswordAttributeName()); if (attr.size()==0) { throw new AuthenticationCredentialsNotFoundException("Missing "+getPasswordAttributeName()+" attribute."); } for (int i = 0; (attr != null) && (!match) && (i < attr.size()); i++) { Object valObject = attr.get(i); if (valObject != null && valObject instanceof byte[]) { if (passwordEncoder instanceof DynamicPasswordComparator) { byte[] received = password.getBytes(); byte[] stored = (byte[]) valObject; match = ((DynamicPasswordComparator) passwordEncoder).comparePasswords(received, stored); } else { String encodedPassword = passwordEncoder.encodePassword(password, null); byte[] passwordBytes = Utf8.encode(encodedPassword); match = Arrays.equals(passwordBytes, (byte[]) valObject); } } } } catch (NamingException e) { throw new BadCredentialsException("Bad credentials", e); } if (!match) throw new BadCredentialsException("Bad credentials"); return user; }
Hashtable env = new Hashtable(); 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 ) || ( attr.size() == 0 )) { attrs = ictx.getAttributes( hostName, new String[] { "A" }); attr = attrs.get( "A" ); if( attr == null ) throw new NamingException ( "No match for name '" + hostName + "'" ); }
private static byte[][] byteArrays(Attribute attr) throws NamingException { if (attr == null) return new byte[0][]; byte[][] bb = new byte[attr.size()][]; for (int i = 0; i < bb.length; i++) bb[i] = (byte[]) attr.get(i); return bb; }
public static String[] stringArray(Attribute attr, String... defVals) throws NamingException { if (attr == null) return defVals; String[] ss = new String[attr.size()]; for (int i = 0; i < ss.length; i++) ss[i] = (String) attr.get(i); return ss; }
public static String[] stringArray(Attribute attr, String... defVals) throws NamingException { if (attr == null) return defVals; String[] ss = new String[attr.size()]; for (int i = 0; i < ss.length; i++) ss[i] = (String) attr.get(i); return ss; }
public static int[] intArray(Attribute attr) throws NamingException { if (attr == null) return ByteUtils.EMPTY_INTS; int[] a = new int[attr.size()]; for (int i = 0; i < a.length; i++) a[i] = Integer.parseInt((String) attr.get(i)); return a; }
public static <T extends Enum<T>> T[] enumArray(Class<T> enumType, Attribute attr) throws NamingException { T[] a = (T[]) Array.newInstance(enumType, attr != null ? attr.size() : 0); for (int i = 0; i < a.length; i++) a[i] = Enum.valueOf(enumType, (String) attr.get(i)); return a; }
public static Code[] codeArray(Attribute attr) throws NamingException { if (attr == null) return EMPTY_CODES; Code[] codes = new Code[attr.size()]; for (int i = 0; i < codes.length; i++) codes[i] = new Code((String) attr.get(i)); return codes; }