Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/SCDynamicStoreConfig.java
41159 views
/*1* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.krb5;2627import java.io.IOException;28import java.util.Hashtable;29import java.util.Iterator;30import java.util.List;31import java.util.Vector;323334public class SCDynamicStoreConfig {35private static native void installNotificationCallback();3637/**38* Returns the dynamic store setting for kerberos in a string array.39* (realm kdc* null) null (mapping-domain mapping-realm)*40*/41private static native List<String> getKerberosConfig();42private static boolean DEBUG = sun.security.krb5.internal.Krb5.DEBUG;4344static {45@SuppressWarnings("removal")46boolean isMac = java.security.AccessController.doPrivileged(47new java.security.PrivilegedAction<Boolean>() {48public Boolean run() {49String osname = System.getProperty("os.name");50if (osname.contains("OS X")) {51System.loadLibrary("osxkrb5");52return true;53}54return false;55}56});57if (isMac) installNotificationCallback();58}5960/**61* Calls down to JNI to get the raw Kerberos Config and maps the object62* graph to the one that Kerberos Config in Java expects63*64* @return65* @throws IOException66*/67public static Hashtable<String, Object> getConfig() throws IOException {68List<String> list = getKerberosConfig();69if (list == null) {70throw new IOException(71"Could not load configuration from SCDynamicStore");72}73if (DEBUG) System.out.println("Raw map from JNI: " + list);7475Hashtable<String,Object> v = new Hashtable<>();76Hashtable<String,Object> realms = new Hashtable<>();77Iterator<String> iterator = list.iterator();78String defaultRealm = null;7980while (true) {81String nextRealm = iterator.next();82if (nextRealm == null) {83break;84}85if (defaultRealm == null) {86defaultRealm = nextRealm;87Hashtable<String,Object> dr = new Hashtable<>();88dr.put("default_realm", v1(defaultRealm));89v.put("libdefaults", dr);90}91Vector<String> kdcs = new Vector<>();92while (true) {93String nextKdc = iterator.next();94if (nextKdc == null) {95break;96}97kdcs.add(nextKdc);98}99if (!kdcs.isEmpty()) {100Hashtable<String,Object> ri = new Hashtable<>();101ri.put("kdc", kdcs);102realms.put(nextRealm, ri);103}104}105if (!realms.isEmpty()) {106v.put("realms", realms);107}108Hashtable<String,Object> mapping = new Hashtable<>();109while (true) {110if (!iterator.hasNext()) {111break;112}113mapping.put(iterator.next(), v1(iterator.next()));114}115if (!mapping.isEmpty()) {116v.put("domain_realm", mapping);117}118return v;119}120121// Make a single value Vector. Config's stanzaTable always122// use Vector as end values.123private static Vector<String> v1(String s) {124Vector<String> out = new Vector<>();125out.add(s);126return out;127}128}129130131