From 98c216184b9c26cecea09ef318bfec69f705fea1 Mon Sep 17 00:00:00 2001 From: weijun Date: Tue, 3 Feb 2009 09:38:31 +0800 Subject: [PATCH] 6785456: Read Kerberos setting from Windows environment variables Reviewed-by: valeriep --- .../classes/sun/security/krb5/Config.java | 66 +++++++++++++++++-- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/src/share/classes/sun/security/krb5/Config.java b/src/share/classes/sun/security/krb5/Config.java index 401f50206..07c78e295 100644 --- a/src/share/classes/sun/security/krb5/Config.java +++ b/src/share/classes/sun/security/krb5/Config.java @@ -1079,12 +1079,39 @@ public class Config { /** * 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() { + @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 @@ public class Config { /** * 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() { + @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; } -- GitLab