jdk/src/share/classes/sun/security/krb5/SCDynamicStoreConfig.java
author alanb
Fri, 02 Nov 2012 15:50:11 +0000
changeset 14342 8435a30053c1
parent 14327 c0d86f6f8be8
child 22951 5fd21112b2b6
permissions -rw-r--r--
7197491: update copyright year to match last edit in jdk8 jdk repository Reviewed-by: chegar, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 14327
diff changeset
     2
 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     4
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    10
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    15
 * accompanied this code).
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    16
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    20
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    23
 * questions.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    24
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    25
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    26
package sun.security.krb5;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    27
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    28
import java.io.IOException;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    29
import java.util.Collection;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    30
import java.util.Hashtable;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    31
import java.util.Vector;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    32
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    33
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    34
public class SCDynamicStoreConfig {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    35
    private static native void installNotificationCallback();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    36
    private static native Hashtable<String, Object> getKerberosConfig();
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
    37
    private static boolean DEBUG = sun.security.krb5.internal.Krb5.DEBUG;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    38
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    39
    static {
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
    40
        boolean isMac = java.security.AccessController.doPrivileged(
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
    41
            new java.security.PrivilegedAction<Boolean>() {
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
    42
                public Boolean run() {
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
    43
                    String osname = System.getProperty("os.name");
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
    44
                    if (osname.contains("OS X")) {
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
    45
                        System.loadLibrary("osx");
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
    46
                        return true;
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
    47
                    }
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
    48
                    return false;
12559
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12047
diff changeset
    49
                }
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12047
diff changeset
    50
            });
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
    51
        if (isMac) installNotificationCallback();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    52
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    53
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
    54
    private static Vector<String> unwrapHost(
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
    55
            Collection<Hashtable<String, String>> c) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    56
        Vector<String> vector = new Vector<String>();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    57
        for (Hashtable<String, String> m : c) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    58
            vector.add(m.get("host"));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    59
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    60
        return vector;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    61
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    62
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    63
    /**
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    64
     * convertRealmConfigs: Maps the Object graph that we get from JNI to the
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    65
     * object graph that Config expects. Also the items inside the kdc array
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    66
     * are wrapped inside Hashtables
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    67
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    68
    @SuppressWarnings("unchecked")
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
    69
    private static Hashtable<String, Object>
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
    70
            convertRealmConfigs(Hashtable<String, ?> configs) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    71
        Hashtable<String, Object> realmsTable = new Hashtable<String, Object>();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    72
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    73
        for (String realm : configs.keySet()) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    74
            // get the kdc
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
    75
            Hashtable<String, Collection<?>> map =
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
    76
                    (Hashtable<String, Collection<?>>) configs.get(realm);
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
    77
            Hashtable<String, Vector<String>> realmMap =
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
    78
                    new Hashtable<String, Vector<String>>();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    79
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    80
            // put the kdc into the realmMap
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
    81
            Collection<Hashtable<String, String>> kdc =
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
    82
                    (Collection<Hashtable<String, String>>) map.get("kdc");
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    83
            if (kdc != null) realmMap.put("kdc", unwrapHost(kdc));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    84
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    85
            // put the admin server into the realmMap
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
    86
            Collection<Hashtable<String, String>> kadmin =
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
    87
                    (Collection<Hashtable<String, String>>) map.get("kadmin");
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    88
            if (kadmin != null) realmMap.put("admin_server", unwrapHost(kadmin));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    89
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    90
            // add the full entry to the realmTable
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    91
            realmsTable.put(realm, realmMap);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    92
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    93
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    94
        return realmsTable;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    95
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    96
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    97
    /**
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    98
     * Calls down to JNI to get the raw Kerberos Config and maps the object
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    99
     * graph to the one that Kerberos Config in Java expects
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   100
     *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   101
     * @return
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   102
     * @throws IOException
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   103
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   104
    public static Hashtable<String, Object> getConfig() throws IOException {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   105
        Hashtable<String, Object> stanzaTable = getKerberosConfig();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   106
        if (stanzaTable == null) {
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   107
            throw new IOException(
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   108
                    "Could not load configuration from SCDynamicStore");
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   109
        }
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   110
        if (DEBUG) System.out.println("Raw map from JNI: " + stanzaTable);
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   111
        return convertNativeConfig(stanzaTable);
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   112
    }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   113
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   114
    @SuppressWarnings("unchecked")
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   115
    private static Hashtable<String, Object> convertNativeConfig(
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   116
            Hashtable<String, Object> stanzaTable) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   117
        // convert SCDynamicStore realm structure to Java realm structure
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   118
        Hashtable<String, ?> realms =
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   119
                (Hashtable<String, ?>) stanzaTable.get("realms");
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   120
        if (realms != null) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   121
            stanzaTable.remove("realms");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   122
            Hashtable<String, Object> realmsTable = convertRealmConfigs(realms);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   123
            stanzaTable.put("realms", realmsTable);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   124
        }
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   125
        WrapAllStringInVector(stanzaTable);
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   126
        if (DEBUG) System.out.println("stanzaTable : " + stanzaTable);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   127
        return stanzaTable;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   128
    }
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   129
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   130
    @SuppressWarnings("unchecked")
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   131
    private static void WrapAllStringInVector(
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   132
            Hashtable<String, Object> stanzaTable) {
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   133
        for (String s: stanzaTable.keySet()) {
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   134
            Object v = stanzaTable.get(s);
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   135
            if (v instanceof Hashtable) {
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   136
                WrapAllStringInVector((Hashtable<String,Object>)v);
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   137
            } else if (v instanceof String) {
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   138
                Vector<String> vec = new Vector<>();
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   139
                vec.add((String)v);
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   140
                stanzaTable.put(s, vec);
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   141
            }
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   142
        }
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 12559
diff changeset
   143
    }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   144
}