Question

I would like to use JNDI to look up Kerberos SRV records in a local network. I try to guess the local domain in hopefully clever ways. If that fails I would like to look up the plain entry, e.g. _kerberos._tcp without any suffix and rely on the DNS domain search list to find the right entry. This works on Windows with nslookup -type=srv _kerberos._tcp and Linux with host -t srv _kerberos._tcp. The domain example.test is appended and the entry is found.

Here is an example program to do DNS lookups via JNDI:

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

public class JndiDnsTest {

    public static void main(String[] args) {
        if (args.length < 2) {
            System.out.println("Usage: " + JndiDnsTest.class.getName() +
                    " name record-types...");
            return;
        }
        String name = args[0];
        String[] recordTypes = new String[args.length - 1];
        System.arraycopy(args, 1, recordTypes, 0, args.length - 1);
        Hashtable<String, String> env = new Hashtable<String,String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
        try {
            DirContext ctx = new InitialDirContext(env);
            Attributes dnsQueryResult = ctx.getAttributes(name, recordTypes);
            if (dnsQueryResult == null) {
                System.out.println("Not found: '" + name + "'");
            }
            for (String rrType: recordTypes) {
                Attribute rr = dnsQueryResult.get(rrType);
                if (rr != null) {
                    for (NamingEnumeration<?> vals = rr.getAll(); vals.hasMoreElements();) {
                        System.out.print(rrType + "\t");
                        System.out.println(vals.nextElement());
                    }
                }
            }
        } catch (NamingException e) {
            e.printStackTrace(System.err);
        }
        System.out.println("\nThe DNS search list:");
        for (Object entry: sun.net.dns.ResolverConfiguration.open().searchlist()) {
            System.out.println(entry);
        }
        System.out.println("\nsun.net.spi.nameservice.domain = " +
                System.getProperty("sun.net.spi.nameservice.domain"));
    }
}

It appears to me that JNDI only does one lookup for the direct name. No entry is found where above commands succeed. It seems it does not use the DNS search list. Its contents are printed correctly at the bottom, though.

On the other hand the Networking properties documentation says that

If the sun.net.spi.nameservice.domain property is not defined then the provider will use any domain or domain search list configured in the platform DNS configuration.

(The property is not set.) The Java version is Sun Java 1.6.0_20.

Does JNDI use the DNS search list or not?

Was it helpful?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top