1 /*
   2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.krb5;
  27 
  28 import java.io.IOException;
  29 import java.util.Collection;
  30 import java.util.Hashtable;
  31 import java.util.Vector;
  32 
  33 
  34 public class SCDynamicStoreConfig {
  35     private static native void installNotificationCallback();
  36     private static native Hashtable<String, Object> getKerberosConfig();
  37 
  38     static {
  39         java.security.AccessController.doPrivileged(
  40             new java.security.PrivilegedAction<Void>() {
  41                 public Void run() {
  42                     System.loadLibrary("osx");
  43                     return null;
  44                 }
  45             });
  46         installNotificationCallback();
  47     }
  48 
  49     private static Vector<String> unwrapHost(Collection<Hashtable<String, String>> c) {
  50         Vector<String> vector = new Vector<String>();
  51         for (Hashtable<String, String> m : c) {
  52             vector.add(m.get("host"));
  53         }
  54         return vector;
  55     }
  56 
  57     /**
  58      * convertRealmConfigs: Maps the Object graph that we get from JNI to the
  59      * object graph that Config expects. Also the items inside the kdc array
  60      * are wrapped inside Hashtables
  61      */
  62     @SuppressWarnings("unchecked")
  63     private static Hashtable<String, Object> convertRealmConfigs(Hashtable<String, ?> configs) {
  64         Hashtable<String, Object> realmsTable = new Hashtable<String, Object>();
  65 
  66         for (String realm : configs.keySet()) {
  67             // get the kdc
  68             Hashtable<String, Collection<?>> map = (Hashtable<String, Collection<?>>) configs.get(realm);
  69             Collection<Hashtable<String, String>> kdc = (Collection<Hashtable<String, String>>) map.get("kdc");
  70 
  71             // put the kdc into the realmMap
  72             Hashtable<String, Vector<String>> realmMap = new Hashtable<String, Vector<String>>();
  73             if (kdc != null) realmMap.put("kdc", unwrapHost(kdc));
  74 
  75             // put the admin server into the realmMap
  76             Collection<Hashtable<String, String>> kadmin = (Collection<Hashtable<String, String>>) map.get("kadmin");
  77             if (kadmin != null) realmMap.put("admin_server", unwrapHost(kadmin));
  78 
  79             // add the full entry to the realmTable
  80             realmsTable.put(realm, realmMap);
  81         }
  82 
  83         return realmsTable;
  84     }
  85 
  86     /**
  87      * Calls down to JNI to get the raw Kerberos Config and maps the object
  88      * graph to the one that Kerberos Config in Java expects
  89      *
  90      * @return
  91      * @throws IOException
  92      */
  93     @SuppressWarnings("unchecked")
  94     public static Hashtable<String, Object> getConfig() throws IOException {
  95         Hashtable<String, Object> stanzaTable = getKerberosConfig();
  96         if (stanzaTable == null) {
  97             throw new IOException("Could not load configuration from SCDynamicStore");
  98         }
  99         //System.out.println("Raw map from JNI: " + stanzaTable);
 100 
 101         // convert SCDynamicStore realm structure to Java realm structure
 102         Hashtable<String, ?> realms = (Hashtable<String, ?>) stanzaTable.get("realms");
 103         if (realms != null) {
 104             stanzaTable.remove("realms");
 105             Hashtable<String, Object> realmsTable = convertRealmConfigs(realms);
 106             stanzaTable.put("realms", realmsTable);
 107         }
 108 
 109        // System.out.println("stanzaTable : " + stanzaTable);
 110         return stanzaTable;
 111     }
 112 }