6785456: Read Kerberos setting from Windows environment variables
authorweijun
Tue, 03 Feb 2009 09:38:31 +0800
changeset 1947 316d79a73e74
parent 1946 2e6e15ca4d56
child 1948 5c23ac167e12
6785456: Read Kerberos setting from Windows environment variables Reviewed-by: valeriep
jdk/src/share/classes/sun/security/krb5/Config.java
--- a/jdk/src/share/classes/sun/security/krb5/Config.java	Tue Feb 03 09:38:13 2009 +0800
+++ b/jdk/src/share/classes/sun/security/krb5/Config.java	Tue Feb 03 09:38:31 2009 +0800
@@ -1079,12 +1079,39 @@
 
     /**
      * Gets default realm.
+     * @throws KrbException where no realm can be located
+     * @return the default realm, always non null
      */
     public String getDefaultRealm() throws KrbException {
+        Exception cause = null;
         String realm = getDefault("default_realm", "libdefaults");
         if ((realm == null) && useDNS_Realm()) {
             // use DNS to locate Kerberos realm
-            realm = getRealmFromDNS();
+            try {
+                realm = getRealmFromDNS();
+            } catch (KrbException ke) {
+                cause = ke;
+            }
+        }
+        if (realm == null) {
+            realm = java.security.AccessController.doPrivileged(
+                    new java.security.PrivilegedAction<String>() {
+                @Override
+                public String run() {
+                    String osname = System.getProperty("os.name");
+                    if (osname.startsWith("Windows")) {
+                        return System.getenv("USERDNSDOMAIN");
+                    }
+                    return null;
+                }
+            });
+        }
+        if (realm == null) {
+            KrbException ke = new KrbException("Cannot locate default realm");
+            if (cause != null) {
+                ke.initCause(cause);
+            }
+            throw ke;
         }
         return realm;
     }
@@ -1092,17 +1119,48 @@
     /**
      * Returns a list of KDC's with each KDC separated by a space
      *
-     * @param realm the realm for which the master KDC is desired
-     * @return the list of KDCs
+     * @param realm the realm for which the KDC list is desired
+     * @throws KrbException if there's no way to find KDC for the realm
+     * @return the list of KDCs separated by a space, always non null
      */
     public String getKDCList(String realm) throws KrbException {
         if (realm == null) {
             realm = getDefaultRealm();
         }
+        Exception cause = null;
         String kdcs = getDefault("kdc", realm);
         if ((kdcs == null) && useDNS_KDC()) {
             // use DNS to locate KDC
-            kdcs = getKDCFromDNS(realm);
+            try {
+                kdcs = getKDCFromDNS(realm);
+            } catch (KrbException ke) {
+                cause = ke;
+            }
+        }
+        if (kdcs == null) {
+            kdcs = java.security.AccessController.doPrivileged(
+                    new java.security.PrivilegedAction<String>() {
+                @Override
+                public String run() {
+                    String osname = System.getProperty("os.name");
+                    if (osname.startsWith("Windows")) {
+                        String logonServer = System.getenv("LOGONSERVER");
+                        if (logonServer != null
+                                && logonServer.startsWith("\\\\")) {
+                            logonServer = logonServer.substring(2);
+                        }
+                        return logonServer;
+                    }
+                    return null;
+                }
+            });
+        }
+        if (kdcs == null) {
+            KrbException ke = new KrbException("Cannot locate KDC");
+            if (cause != null) {
+                ke.initCause(cause);
+            }
+            throw ke;
         }
         return kdcs;
     }