提交 8504c8ad 编写于 作者: W weijun

7184246: Simplify Config.get() of krb5

Reviewed-by: xuelei
上级 2e656f02
......@@ -74,14 +74,18 @@ public class Checksum {
private static boolean DEBUG = Krb5.DEBUG;
static {
initStatic();
}
public static void initStatic() {
String temp = null;
Config cfg = null;
try {
cfg = Config.getInstance();
temp = cfg.getDefault("default_checksum", "libdefaults");
temp = cfg.get("libdefaults", "default_checksum");
if (temp != null)
{
CKSUMTYPE_DEFAULT = cfg.getType(temp);
CKSUMTYPE_DEFAULT = Config.getType(temp);
} else {
/*
* If the default checksum is not
......@@ -103,10 +107,10 @@ public class Checksum {
try {
temp = cfg.getDefault("safe_checksum_type", "libdefaults");
temp = cfg.get("libdefaults", "safe_checksum_type");
if (temp != null)
{
SAFECKSUMTYPE_DEFAULT = cfg.getType(temp);
SAFECKSUMTYPE_DEFAULT = Config.getType(temp);
} else {
SAFECKSUMTYPE_DEFAULT = CKSUMTYPE_RSA_MD5_DES;
}
......
......@@ -142,11 +142,11 @@ public final class KdcComm {
try {
Config cfg = Config.getInstance();
String temp = cfg.getDefault("kdc_timeout", "libdefaults");
String temp = cfg.get("libdefaults", "kdc_timeout");
timeout = parsePositiveIntString(temp);
temp = cfg.getDefault("max_retries", "libdefaults");
temp = cfg.get("libdefaults", "max_retries");
max_retries = parsePositiveIntString(temp);
temp = cfg.getDefault("udp_preference_limit", "libdefaults");
temp = cfg.get("libdefaults", "udp_preference_limit");
udf_pref_limit = parsePositiveIntString(temp);
} catch (Exception exc) {
// ignore any exceptions; use default values
......@@ -421,7 +421,7 @@ public final class KdcComm {
int temp = -1;
try {
String value =
Config.getInstance().getDefault(key, realm);
Config.getInstance().get("realms", realm, key);
temp = parsePositiveIntString(value);
} catch (Exception exc) {
// Ignored, defValue will be picked up
......
......@@ -655,19 +655,19 @@ public class PrincipalName implements Cloneable {
try {
String subname = null;
Config c = Config.getInstance();
if ((result = c.getDefault(name, "domain_realm")) != null)
if ((result = c.get("domain_realm", name)) != null)
return result;
else {
for (int i = 1; i < name.length(); i++) {
if ((name.charAt(i) == '.') && (i != name.length() - 1)) { //mapping could be .ibm.com = AUSTIN.IBM.COM
subname = name.substring(i);
result = c.getDefault(subname, "domain_realm");
result = c.get("domain_realm", subname);
if (result != null) {
break;
}
else {
subname = name.substring(i + 1); //or mapping could be ibm.com = AUSTIN.IBM.COM
result = c.getDefault(subname, "domain_realm");
result = c.get("domain_realm", subname);
if (result != null) {
break;
}
......
......@@ -350,7 +350,7 @@ public class Realm implements Cloneable {
return null;
}
String intermediaries = cfg.getDefault(sRealm, cRealm);
String intermediaries = cfg.getAll("capaths", cRealm, sRealm);
if (intermediaries == null) {
if (DEBUG) {
......@@ -459,7 +459,7 @@ public class Realm implements Cloneable {
tempTarget);
}
intermediaries = cfg.getDefault(tempTarget, cRealm);
intermediaries = cfg.getAll("capaths", cRealm, tempTarget);
} while (true);
......
......@@ -34,19 +34,25 @@ import java.util.Vector;
public class SCDynamicStoreConfig {
private static native void installNotificationCallback();
private static native Hashtable<String, Object> getKerberosConfig();
private static boolean DEBUG = sun.security.krb5.internal.Krb5.DEBUG;
static {
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<Void>() {
public Void run() {
System.loadLibrary("osx");
return null;
boolean isMac = java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<Boolean>() {
public Boolean run() {
String osname = System.getProperty("os.name");
if (osname.contains("OS X")) {
System.loadLibrary("osx");
return true;
}
return false;
}
});
installNotificationCallback();
if (isMac) installNotificationCallback();
}
private static Vector<String> unwrapHost(Collection<Hashtable<String, String>> c) {
private static Vector<String> unwrapHost(
Collection<Hashtable<String, String>> c) {
Vector<String> vector = new Vector<String>();
for (Hashtable<String, String> m : c) {
vector.add(m.get("host"));
......@@ -60,20 +66,25 @@ public class SCDynamicStoreConfig {
* are wrapped inside Hashtables
*/
@SuppressWarnings("unchecked")
private static Hashtable<String, Object> convertRealmConfigs(Hashtable<String, ?> configs) {
private static Hashtable<String, Object>
convertRealmConfigs(Hashtable<String, ?> configs) {
Hashtable<String, Object> realmsTable = new Hashtable<String, Object>();
for (String realm : configs.keySet()) {
// get the kdc
Hashtable<String, Collection<?>> map = (Hashtable<String, Collection<?>>) configs.get(realm);
Collection<Hashtable<String, String>> kdc = (Collection<Hashtable<String, String>>) map.get("kdc");
Hashtable<String, Collection<?>> map =
(Hashtable<String, Collection<?>>) configs.get(realm);
Hashtable<String, Vector<String>> realmMap =
new Hashtable<String, Vector<String>>();
// put the kdc into the realmMap
Hashtable<String, Vector<String>> realmMap = new Hashtable<String, Vector<String>>();
Collection<Hashtable<String, String>> kdc =
(Collection<Hashtable<String, String>>) map.get("kdc");
if (kdc != null) realmMap.put("kdc", unwrapHost(kdc));
// put the admin server into the realmMap
Collection<Hashtable<String, String>> kadmin = (Collection<Hashtable<String, String>>) map.get("kadmin");
Collection<Hashtable<String, String>> kadmin =
(Collection<Hashtable<String, String>>) map.get("kadmin");
if (kadmin != null) realmMap.put("admin_server", unwrapHost(kadmin));
// add the full entry to the realmTable
......@@ -90,23 +101,44 @@ public class SCDynamicStoreConfig {
* @return
* @throws IOException
*/
@SuppressWarnings("unchecked")
public static Hashtable<String, Object> getConfig() throws IOException {
Hashtable<String, Object> stanzaTable = getKerberosConfig();
if (stanzaTable == null) {
throw new IOException("Could not load configuration from SCDynamicStore");
throw new IOException(
"Could not load configuration from SCDynamicStore");
}
//System.out.println("Raw map from JNI: " + stanzaTable);
if (DEBUG) System.out.println("Raw map from JNI: " + stanzaTable);
return convertNativeConfig(stanzaTable);
}
@SuppressWarnings("unchecked")
private static Hashtable<String, Object> convertNativeConfig(
Hashtable<String, Object> stanzaTable) {
// convert SCDynamicStore realm structure to Java realm structure
Hashtable<String, ?> realms = (Hashtable<String, ?>) stanzaTable.get("realms");
Hashtable<String, ?> realms =
(Hashtable<String, ?>) stanzaTable.get("realms");
if (realms != null) {
stanzaTable.remove("realms");
Hashtable<String, Object> realmsTable = convertRealmConfigs(realms);
stanzaTable.put("realms", realmsTable);
}
// System.out.println("stanzaTable : " + stanzaTable);
WrapAllStringInVector(stanzaTable);
if (DEBUG) System.out.println("stanzaTable : " + stanzaTable);
return stanzaTable;
}
@SuppressWarnings("unchecked")
private static void WrapAllStringInVector(
Hashtable<String, Object> stanzaTable) {
for (String s: stanzaTable.keySet()) {
Object v = stanzaTable.get(s);
if (v instanceof Hashtable) {
WrapAllStringInVector((Hashtable<String,Object>)v);
} else if (v instanceof String) {
Vector<String> vec = new Vector<>();
vec.add((String)v);
stanzaTable.put(s, vec);
}
}
}
}
......@@ -244,25 +244,23 @@ public class KDCOptions extends KerberosFlags {
Config config = Config.getInstance();
/*
* First see if the IBM hex format is being used.
* If not, try the Sun's string (boolean) format.
*/
// If key not present, returns Integer.MIN_VALUE, which is
// almost all zero.
int options =config.getDefaultIntValue("kdc_default_options",
"libdefaults");
int options = config.getIntValue("libdefaults",
"kdc_default_options");
if ((options & RENEWABLE_OK) == RENEWABLE_OK) {
set(RENEWABLE_OK, true);
} else {
if (config.getDefaultBooleanValue("renewable", "libdefaults")) {
if (config.getBooleanValue("libdefaults", "renewable")) {
set(RENEWABLE_OK, true);
}
}
if ((options & PROXIABLE) == PROXIABLE) {
set(PROXIABLE, true);
} else {
if (config.getDefaultBooleanValue("proxiable", "libdefaults")) {
if (config.getBooleanValue("libdefaults", "proxiable")) {
set(PROXIABLE, true);
}
}
......@@ -270,7 +268,7 @@ public class KDCOptions extends KerberosFlags {
if ((options & FORWARDABLE) == FORWARDABLE) {
set(FORWARDABLE, true);
} else {
if (config.getDefaultBooleanValue("forwardable", "libdefaults")) {
if (config.getBooleanValue("libdefaults", "forwardable")) {
set(FORWARDABLE, true);
}
}
......
......@@ -350,9 +350,9 @@ public class KerberosTime implements Cloneable {
public static int getDefaultSkew() {
int tdiff = Krb5.DEFAULT_ALLOWABLE_CLOCKSKEW;
try {
Config c = Config.getInstance();
if ((tdiff = c.getDefaultIntValue("clockskew",
"libdefaults")) == Integer.MIN_VALUE) { //value is not defined
if ((tdiff = Config.getInstance().getIntValue(
"libdefaults", "clockskew"))
== Integer.MIN_VALUE) { //value is not defined
tdiff = Krb5.DEFAULT_ALLOWABLE_CLOCKSKEW;
}
} catch (KrbException e) {
......
......@@ -126,10 +126,10 @@ public abstract class CksumType {
int cksumType = Checksum.CKSUMTYPE_RSA_MD5; // default
try {
Config c = Config.getInstance();
if ((cksumType = (c.getType(c.getDefault("ap_req_checksum_type",
"libdefaults")))) == - 1) {
if ((cksumType = c.getType(c.getDefault("checksum_type",
"libdefaults"))) == -1) {
if ((cksumType = (Config.getType(c.get("libdefaults",
"ap_req_checksum_type")))) == - 1) {
if ((cksumType = Config.getType(c.get("libdefaults",
"checksum_type"))) == -1) {
cksumType = Checksum.CKSUMTYPE_RSA_MD5; // default
}
}
......
......@@ -48,13 +48,17 @@ import java.util.ArrayList;
public abstract class EType {
private static final boolean DEBUG = Krb5.DEBUG;
private static final boolean ALLOW_WEAK_CRYPTO;
private static boolean allowWeakCrypto;
static {
initStatic();
}
public static void initStatic() {
boolean allowed = true;
try {
Config cfg = Config.getInstance();
String temp = cfg.getDefault("allow_weak_crypto", "libdefaults");
String temp = cfg.get("libdefaults", "allow_weak_crypto");
if (temp != null && temp.equals("false")) allowed = false;
} catch (Exception exc) {
if (DEBUG) {
......@@ -63,7 +67,7 @@ public abstract class EType {
exc.getMessage());
}
}
ALLOW_WEAK_CRYPTO = allowed;
allowWeakCrypto = allowed;
}
public static EType getInstance (int eTypeConst)
......@@ -216,7 +220,7 @@ public abstract class EType {
} else {
result = BUILTIN_ETYPES;
}
if (!ALLOW_WEAK_CRYPTO) {
if (!allowWeakCrypto) {
// The last 2 etypes are now weak ones
return Arrays.copyOfRange(result, 0, result.length - 2);
}
......
......@@ -186,8 +186,8 @@ public class KeyTab implements KeyTabConstants {
} else {
String kname = null;
try {
String keytab_names = Config.getInstance().getDefault
("default_keytab_name", "libdefaults");
String keytab_names = Config.getInstance().get
("libdefaults", "default_keytab_name");
if (keytab_names != null) {
StringTokenizer st = new StringTokenizer(keytab_names, " ");
while (st.hasMoreTokens()) {
......
......@@ -25,6 +25,7 @@
* @bug 6857795
* @bug 6858589
* @bug 6972005
* @compile -XDignore.symbol.file ConfPlusProp.java
* @run main/othervm ConfPlusProp
* @summary krb5.conf ignored if system properties on realm and kdc are provided
*/
......@@ -75,7 +76,7 @@ public class ConfPlusProp {
check("R1", "k1");
check("R2", "old");
check("R3", null);
if (!config.getDefault("forwardable", "libdefaults").equals("well")) {
if (!config.get("libdefaults", "forwardable").equals("well")) {
throw new Exception("Extra config error");
}
......@@ -103,7 +104,7 @@ public class ConfPlusProp {
check("R1", null);
check("R2", null);
check("R3", null);
if (config.getDefault("forwardable", "libdefaults") != null) {
if (config.get("libdefaults", "forwardable") != null) {
throw new Exception("Extra config error");
}
}
......@@ -121,7 +122,7 @@ public class ConfPlusProp {
check("R1", "k1");
check("R2", "k2");
check("R3", "k2");
if (!config.getDefault("forwardable", "libdefaults").equals("well")) {
if (!config.get("libdefaults", "forwardable").equals("well")) {
throw new Exception("Extra config error");
}
......@@ -143,7 +144,7 @@ public class ConfPlusProp {
check("R1", "k2");
check("R2", "k2");
check("R3", "k2");
if (config.getDefault("forwardable", "libdefaults") != null) {
if (config.get("libdefaults", "forwardable") != null) {
throw new Exception("Extra config error");
}
}
......
......@@ -28,12 +28,20 @@
* @summary fix dns_fallback parse error, and use dns by default
*/
import sun.security.krb5.*;
import java.io.*;
import java.lang.reflect.Method;
import sun.security.krb5.Config;
public class DnsFallback {
static Method useDNS_Realm;
public static void main(String[] args) throws Exception {
useDNS_Realm = Config.class.getDeclaredMethod("useDNS_Realm");
useDNS_Realm.setAccessible(true);
// for 6673164
check("true", "true", true);
check("false", "true", false);
......@@ -48,22 +56,25 @@ public class DnsFallback {
check(null, null, true);
}
static void check(String realm, String fallback, boolean output) throws Exception {
FileOutputStream fo = new FileOutputStream("dnsfallback.conf");
StringBuffer sb = new StringBuffer();
sb.append("[libdefaults]\n");
if (realm != null) {
sb.append("dns_lookup_realm=" + realm + "\n");
}
if (fallback != null) {
sb.append("dns_fallback=" + fallback + "\n");
static void check(String realm, String fallback, boolean output)
throws Exception {
try (PrintStream ps =
new PrintStream(new FileOutputStream("dnsfallback.conf"))) {
ps.println("[libdefaults]\n");
if (realm != null) {
ps.println("dns_lookup_realm=" + realm);
}
if (fallback != null) {
ps.println("dns_fallback=" + fallback);
}
}
fo.write(sb.toString().getBytes());
fo.close();
System.setProperty("java.security.krb5.conf", "dnsfallback.conf");
Config.refresh();
System.out.println("Testing " + realm + ", " + fallback + ", " + output);
if (Config.getInstance().useDNS_Realm() != output) {
if (!useDNS_Realm.invoke(Config.getInstance()).equals(output)) {
throw new Exception("Fail");
}
}
......
......@@ -23,6 +23,7 @@
/*
* @test
* @bug 6319046
* @compile -XDignore.symbol.file ParseConfig.java
* @run main/othervm ParseConfig
* @summary Problem with parsing krb5.conf
*/
......@@ -37,7 +38,7 @@ public class ParseConfig {
String sample = "kdc.example.com kdc2.example.com";
for ( int i = 0; i < 4; i++ ) {
String expected = config.getDefault("kdc", "EXAMPLE_" + i + ".COM");
String expected = config.getAll("realms", "EXAMPLE_" + i + ".COM", "kdc");
if (!sample.equals(expected)) {
throw new Exception("krb5.conf: unexpected kdc value \"" +
expected + "\"");
......
......@@ -25,6 +25,7 @@
* @test
* @bug 6706974
* @summary Add krb5 test infrastructure
* @compile -XDignore.symbol.file BasicKrb5Test.java
* @run main/othervm BasicKrb5Test
* @run main/othervm BasicKrb5Test des-cbc-crc
* @run main/othervm BasicKrb5Test des-cbc-md5
......@@ -86,7 +87,7 @@ public class BasicKrb5Test {
new OneKDC(etype).writeJAASConf();
System.out.println("Testing etype " + etype);
if (etype != null && !EType.isSupported(Config.getInstance().getType(etype))) {
if (etype != null && !EType.isSupported(Config.getType(etype))) {
// aes256 is not enabled on all systems
System.out.println("Not supported.");
return;
......
......@@ -108,7 +108,7 @@ public class MaxRetries {
if (line.startsWith(">>> KDCCommunication")) {
System.out.println(line);
if (line.indexOf(timeoutTag) < 0) {
throw new Exception("Wrong timeout value");
throw new Exception("Wrong timeout value" + timeoutTag);
}
count--;
}
......
/*
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 7184246
* @compile -XDignore.symbol.file Duplicates.java
* @run main/othervm Duplicates
* @summary Simplify Config.get() of krb5
*/
import sun.security.krb5.Config;
public class Duplicates {
public static void main(String[] args) throws Exception {
System.setProperty("java.security.krb5.conf",
System.getProperty("test.src", ".") +"/k1.conf");
Config config = Config.getInstance();
config.listTable();
String s;
// Latter overwrites former for root section
s = config.get("libdefaults", "default_realm");
if (s != null) {
throw new Exception();
}
// Latter overwrites former for strings
s = config.get("libdefaults", "default_tkt_enctypes");
if (!s.equals("aes256-cts")) {
throw new Exception();
}
// Latter overwrites former for sub-section
s = config.get("realms", "R1", "kdc");
if (!s.equals("k2")) {
throw new Exception(s);
}
// Duplicate keys in [realms] are merged
s = config.getAll("realms", "R2", "kdc");
if (!s.equals("k1 k2 k3 k4")) {
throw new Exception(s);
}
// Duplicate keys in [capaths] are merged
s = config.getAll("capaths", "R1", "R2");
if (!s.equals("R3 R4 R5 R6")) {
throw new Exception(s);
}
// We can be very deep now
s = config.get("new", "x", "y", "z", "a", "b", "c");
if (!s.equals("d")) {
throw new Exception(s);
}
}
}
/*
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 7184246
* @summary Simplify Config.get() of krb5
*/
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Hashtable;
import java.util.Vector;
import sun.security.krb5.Config;
import sun.security.krb5.SCDynamicStoreConfig;
public class SCDynamicConfigTest {
static Vector<Hashtable<String,String>>hosts() {
Vector <Hashtable<String,String>> result = new Vector<>();
Hashtable<String,String> pair = new Hashtable<>();
pair.put("host", "127.0.0.1");
result.add(pair);
pair = new Hashtable<>();
pair.put("host", "127.0.0.2");
result.add(pair);
return result;
}
public static void main(String[] args) throws Exception {
// Reconstruct a typical SCDynamicConfig.getKerberosConfig() output
Hashtable<String, Object> conf = new Hashtable<>();
Hashtable<String, Object> libdefaults = new Hashtable<>();
libdefaults.put("default_realm", "REALM.COM");
conf.put("libdefaults", libdefaults);
Hashtable<String, Object> realms = new Hashtable<>();
Hashtable<String, Object> thisRealm = new Hashtable<>();
realms.put("REALM.COM", thisRealm);
thisRealm.put("kpasswd", hosts());
thisRealm.put("kadmin", hosts());
thisRealm.put("kdc", hosts());
conf.put("realms", realms);
Hashtable<String, Object> domain_realm = new Hashtable<>();
domain_realm.put(".realm.com", "REALM.COM");
domain_realm.put("realm.com", "REALM.COM");
conf.put("domain_realm", domain_realm);
System.out.println("SCDynamicConfig:\n");
System.out.println(conf);
// Simulate SCDynamicConfig.getConfig() output
Method m = SCDynamicStoreConfig.class.getDeclaredMethod(
"convertNativeConfig", Hashtable.class);
m.setAccessible(true);
conf = (Hashtable)m.invoke(null, conf);
System.out.println("\nkrb5.conf:\n");
System.out.println(conf);
// Feed it into a Config object
System.setProperty("java.security.krb5.conf", "not-a-file");
Config cf = Config.getInstance();
Field f = Config.class.getDeclaredField("stanzaTable");
f.setAccessible(true);
f.set(cf, conf);
System.out.println("\nConfig:\n");
System.out.println(cf);
if (!cf.getDefaultRealm().equals("REALM.COM")) {
throw new Exception();
}
if (!cf.getKDCList("REALM.COM").equals("127.0.0.1 127.0.0.2")) {
throw new Exception();
}
if (!cf.get("domain_realm", ".realm.com").equals("REALM.COM")) {
throw new Exception();
}
}
}
[libdefaults]
default_realm = R1
[libdefaults]
default_tkt_enctypes = aes128-cts
default_tkt_enctypes = aes256-cts
[realms]
R1 = {
kdc = k1
}
R1 = {
kdc = k2
}
R2 = {
kdc = k1
kdc = k2 k3
admin_server = a1
kdc = k4
}
[capaths]
R1 = {
R2 = R3
R2 = R4 R5
R2 = R6
}
[new]
x = {
y = {
z = {
a = {
b = {
c = d
}
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册