0N/A/*
2609N/A * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.jndi.dns;
0N/A
0N/A
0N/Aimport java.net.MalformedURLException;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.Hashtable;
0N/Aimport java.util.List;
0N/A
0N/Aimport javax.naming.*;
0N/Aimport javax.naming.spi.*;
0N/A
0N/Aimport com.sun.jndi.toolkit.url.UrlUtil;
0N/Aimport sun.net.dns.ResolverConfiguration; // available since 1.4.1
0N/A
0N/A
0N/A/**
0N/A * A DnsContextFactory serves as the initial context factory for DNS.
0N/A *
0N/A * <p> When an initial context is being created, the environment
0N/A * property "java.naming.provider.url" should contain a DNS pseudo-URL
0N/A * (see DnsUrl) or a space-separated list of them. Multiple URLs must
0N/A * all have the same domain value.
0N/A * If the property is not set, the default "dns:" is used.
0N/A *
0N/A * @author Scott Seligman
0N/A */
0N/A
0N/A
0N/Apublic class DnsContextFactory implements InitialContextFactory {
0N/A
0N/A private static final String DEFAULT_URL = "dns:";
2893N/A private static final int DEFAULT_PORT = 53;
0N/A
0N/A
0N/A public Context getInitialContext(Hashtable<?,?> env) throws NamingException {
0N/A if (env == null) {
0N/A env = new Hashtable(5);
0N/A }
0N/A return urlToContext(getInitCtxUrl(env), env);
0N/A }
0N/A
0N/A public static DnsContext getContext(String domain,
0N/A String[] servers, Hashtable<?,?> env)
0N/A throws NamingException {
0N/A return new DnsContext(domain, servers, env);
0N/A }
0N/A
0N/A /*
0N/A * "urls" are used to determine the servers, but any domain
0N/A * components are overridden by "domain".
0N/A */
0N/A public static DnsContext getContext(String domain,
0N/A DnsUrl[] urls, Hashtable env)
0N/A throws NamingException {
0N/A
0N/A String[] servers = serversForUrls(urls);
0N/A DnsContext ctx = getContext(domain, servers, env);
0N/A if (platformServersUsed(urls)) {
0N/A ctx.setProviderUrl(constructProviderUrl(domain, servers));
0N/A }
0N/A return ctx;
0N/A }
0N/A
0N/A /*
0N/A * Public for use by product test suite.
0N/A */
0N/A public static boolean platformServersAvailable() {
2893N/A return !filterNameServers(
2893N/A ResolverConfiguration.open().nameservers(), true
2893N/A ).isEmpty();
0N/A }
0N/A
0N/A private static Context urlToContext(String url, Hashtable env)
0N/A throws NamingException {
0N/A
0N/A DnsUrl[] urls;
0N/A try {
0N/A urls = DnsUrl.fromList(url);
0N/A } catch (MalformedURLException e) {
0N/A throw new ConfigurationException(e.getMessage());
0N/A }
0N/A if (urls.length == 0) {
0N/A throw new ConfigurationException(
0N/A "Invalid DNS pseudo-URL(s): " + url);
0N/A }
0N/A String domain = urls[0].getDomain();
0N/A
0N/A // If multiple urls, all must have the same domain.
0N/A for (int i = 1; i < urls.length; i++) {
0N/A if (!domain.equalsIgnoreCase(urls[i].getDomain())) {
0N/A throw new ConfigurationException(
0N/A "Conflicting domains: " + url);
0N/A }
0N/A }
0N/A return getContext(domain, urls, env);
0N/A }
0N/A
0N/A /*
0N/A * Returns all the servers specified in a set of URLs.
0N/A * If a URL has no host (or port), the servers configured on the
0N/A * underlying platform are used if possible. If no configured
0N/A * servers can be found, then fall back to the old behavior of
0N/A * using "localhost".
0N/A * There must be at least one URL.
0N/A */
0N/A private static String[] serversForUrls(DnsUrl[] urls)
0N/A throws NamingException {
0N/A
0N/A if (urls.length == 0) {
0N/A throw new ConfigurationException("DNS pseudo-URL required");
0N/A }
0N/A
2609N/A List<String> servers = new ArrayList<>();
0N/A
0N/A for (int i = 0; i < urls.length; i++) {
0N/A String server = urls[i].getHost();
0N/A int port = urls[i].getPort();
0N/A
0N/A if (server == null && port < 0) {
0N/A // No server or port given, so look to underlying platform.
0N/A // ResolverConfiguration does some limited caching, so the
0N/A // following is reasonably efficient even if called rapid-fire.
2913N/A List<String> platformServers = filterNameServers(
2893N/A ResolverConfiguration.open().nameservers(), false);
0N/A if (!platformServers.isEmpty()) {
0N/A servers.addAll(platformServers);
0N/A continue; // on to next URL (if any, which is unlikely)
0N/A }
0N/A }
0N/A
0N/A if (server == null) {
0N/A server = "localhost";
0N/A }
0N/A servers.add((port < 0)
0N/A ? server
0N/A : server + ":" + port);
0N/A }
2609N/A return servers.toArray(new String[servers.size()]);
0N/A }
0N/A
0N/A /*
0N/A * Returns true if serversForUrls(urls) would make use of servers
0N/A * from the underlying platform.
0N/A */
0N/A private static boolean platformServersUsed(DnsUrl[] urls) {
0N/A if (!platformServersAvailable()) {
0N/A return false;
0N/A }
0N/A for (int i = 0; i < urls.length; i++) {
0N/A if (urls[i].getHost() == null &&
0N/A urls[i].getPort() < 0) {
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /*
0N/A * Returns a value for the PROVIDER_URL property (space-separated URL
0N/A * Strings) that reflects the given domain and servers.
0N/A * Each server is of the form "server[:port]".
0N/A * There must be at least one server.
0N/A * IPv6 literal host names include delimiting brackets.
0N/A */
0N/A private static String constructProviderUrl(String domain,
0N/A String[] servers) {
0N/A String path = "";
0N/A if (!domain.equals(".")) {
0N/A try {
0N/A path = "/" + UrlUtil.encode(domain, "ISO-8859-1");
0N/A } catch (java.io.UnsupportedEncodingException e) {
0N/A // assert false : "ISO-Latin-1 charset unavailable";
0N/A }
0N/A }
0N/A
0N/A StringBuffer buf = new StringBuffer();
0N/A for (int i = 0; i < servers.length; i++) {
0N/A if (i > 0) {
0N/A buf.append(' ');
0N/A }
0N/A buf.append("dns://").append(servers[i]).append(path);
0N/A }
0N/A return buf.toString();
0N/A }
0N/A
0N/A /*
0N/A * Reads environment to find URL(s) of initial context.
0N/A * Default URL is "dns:".
0N/A */
0N/A private static String getInitCtxUrl(Hashtable env) {
0N/A String url = (String) env.get(Context.PROVIDER_URL);
0N/A return ((url != null) ? url : DEFAULT_URL);
0N/A }
2893N/A
2893N/A /**
2893N/A * Removes any DNS server that's not permitted to access
2893N/A * @param input the input server[:port] list, must not be null
2893N/A * @param oneIsEnough return output once there exists one ok
2893N/A * @return the filtered list, all non-permitted input removed
2893N/A */
2893N/A private static List filterNameServers(List input, boolean oneIsEnough) {
2893N/A SecurityManager security = System.getSecurityManager();
2893N/A if (security == null || input == null || input.isEmpty()) {
2893N/A return input;
2893N/A } else {
2893N/A List output = new ArrayList();
2893N/A for (Object o: input) {
2893N/A if (o instanceof String) {
2893N/A String platformServer = (String)o;
2893N/A int colon = platformServer.indexOf(':',
2893N/A platformServer.indexOf(']') + 1);
2893N/A
2893N/A int p = (colon < 0)
2893N/A ? DEFAULT_PORT
2893N/A : Integer.parseInt(
2893N/A platformServer.substring(colon + 1));
2893N/A String s = (colon < 0)
2893N/A ? platformServer
2893N/A : platformServer.substring(0, colon);
2893N/A try {
2893N/A security.checkConnect(s, p);
2893N/A output.add(platformServer);
2893N/A if (oneIsEnough) {
2893N/A return output;
2893N/A }
2893N/A } catch (SecurityException se) {
2893N/A continue;
2893N/A }
2893N/A }
2893N/A }
2893N/A return output;
2893N/A }
2893N/A }
0N/A}