jdk/src/share/classes/sun/security/krb5/Config.java
author weijun
Wed, 12 Mar 2008 09:32:38 +0800
changeset 93 6266821dc652
parent 2 90ce3da70b43
child 715 f16baef3a20e
permissions -rw-r--r--
6673164: dns_fallback parse error Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Portions Copyright 2000-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *  (C) Copyright IBM Corp. 1999 All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  Copyright 1997 The Open Group Research Institute.  All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
package sun.security.krb5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.BufferedReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.InputStreamReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.net.UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.security.krb5.internal.crypto.EType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import sun.security.krb5.internal.ktab.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.security.krb5.internal.Krb5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * This class maintains key-value pairs of Kerberos configurable constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * from configuration file or from user specified system properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class Config {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Only allow a single instance of Config.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static Config singleton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Hashtable used to store configuration infomation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private Hashtable<String,Object> stanzaTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static boolean DEBUG = sun.security.krb5.internal.Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // these are used for hexdecimal calculation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static final int BASE16_0 = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static final int BASE16_1 = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static final int BASE16_2 = 16 * 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static final int BASE16_3 = 16 * 16 * 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private String defaultRealm;   // default kdc realm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // used for native interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static native String getWindowsDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Gets an instance of Config class. One and only one instance (the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * singleton) is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @exception KrbException if error occurs when constructing a Config
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * instance. Possible causes would be configuration file not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * found, either of java.security.krb5.realm or java.security.krb5.kdc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * not specified, error reading configuration file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public static synchronized Config getInstance() throws KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (singleton == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            singleton = new Config();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return singleton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Refresh and reload the Configuration. This could involve,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * for example reading the Configuration file again or getting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * the java.security.krb5.* system properties again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @exception KrbException if error occurs when constructing a Config
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * instance. Possible causes would be configuration file not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * found, either of java.security.krb5.realm or java.security.krb5.kdc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * not specified, error reading configuration file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public static synchronized void refresh() throws KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        singleton = new Config();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        KeyTab.refresh();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Private constructor - can not be instantiated externally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private Config() throws KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
         * If these two system properties are being specified by the user,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
         * we ignore configuration file. If either one system property is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
         * specified, we throw exception. If neither of them are specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
         * we load the information from configuration file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        String kdchost =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                new sun.security.action.GetPropertyAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    ("java.security.krb5.kdc"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
         defaultRealm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                new sun.security.action.GetPropertyAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    ("java.security.krb5.realm"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if ((kdchost == null && defaultRealm != null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            (defaultRealm == null && kdchost != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            throw new KrbException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                ("System property java.security.krb5.kdc and " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                 "java.security.krb5.realm both must be set or " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                 "neither must be set.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (kdchost != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
             * If configuration information is only specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
             * properties java.security.krb5.kdc and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
             * java.security.krb5.realm, we put both in the hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
             * under [libdefaults].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            Hashtable<String,String> kdcs = new Hashtable<String,String> ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            kdcs.put("default_realm", defaultRealm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            // The user can specify a list of kdc hosts separated by ":"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            kdchost = kdchost.replace(':', ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            kdcs.put("kdc", kdchost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            stanzaTable = new Hashtable<String,Object> ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            stanzaTable.put("libdefaults", kdcs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            // Read the Kerberos configuration file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                Vector<String> configFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                configFile = loadConfigFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                stanzaTable = parseStanzaTable(configFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                KrbException ke = new KrbException("Could not load " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                                   "configuration file " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                                   ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                ke.initCause(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                throw(ke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Gets the default int value for the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param name the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @return the default Integer, null is returned if no such name and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * value are found in configuration file, or error occurs when parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * string to integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public int getDefaultIntValue(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        String result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        int value = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        result = getDefault(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                value = parseIntValue(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    System.out.println("Exception in getting value of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                       name + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                       e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    System.out.println("Setting " + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                       " to minimum value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                value = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Gets the default int value for the specified name in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * section. <br>This method is quicker by using section name as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * search key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param name the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param sectio the name string of the section.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @return the default Integer, null is returned if no such name and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * value are found in configuration file, or error occurs when parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * string to integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public int getDefaultIntValue(String name, String section) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        String result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        int value = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        result = getDefault(name, section);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                value = parseIntValue(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    System.out.println("Exception in getting value of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                       name +" in section " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                       section + " "  + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    System.out.println("Setting " + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                       " to minimum value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                value = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Gets the default string value for the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @param name the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @return the default value, null is returned if it cannot be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public String getDefault(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (stanzaTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return getDefault(name, stanzaTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * This method does the real job to recursively search through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * stanzaTable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @param k the key string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param t stanzaTable or sub hashtable within it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @return the value found in config file, returns null if no value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * matched with the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    private String getDefault(String k, Hashtable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        String result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        String key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (stanzaTable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            for (Enumeration e = t.keys(); e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                key = (String)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                Object ob = t.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                if (ob instanceof Hashtable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    result = getDefault(k, (Hashtable)ob);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                } else if (key.equalsIgnoreCase(k)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    if (ob instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        return (String)(t.get(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    } else if (ob instanceof Vector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                        result = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        int length = ((Vector)ob).size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                        for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                            if (i == length -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                result +=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                    (String)(((Vector)ob).elementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                                result +=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                                    (String)(((Vector)ob).elementAt(i)) + " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Gets the default string value for the specified name in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * specified section.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * <br>This method is quicker by using the section name as the search key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @param name the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @param section the name of the section.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @return the default value, null is returned if it cannot be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public String getDefault(String name, String section) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        String stanzaName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        String result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        Hashtable subTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
         * In the situation when kdc is specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
         * java.security.krb5.kdc, we get the kdc from [libdefaults] in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
         * hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (name.equalsIgnoreCase("kdc") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            (!section.equalsIgnoreCase("libdefaults")) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            (java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                new sun.security.action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                GetPropertyAction("java.security.krb5.kdc")) != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            result = getDefault("kdc", "libdefaults");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (stanzaTable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            for (Enumeration e = stanzaTable.keys(); e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                stanzaName = (String)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                subTable = (Hashtable)stanzaTable.get(stanzaName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                if (stanzaName.equalsIgnoreCase(section)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    if (subTable.containsKey(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                        return (String)(subTable.get(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                } else if (subTable.containsKey(section)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    Object ob = subTable.get(section);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    if (ob instanceof Hashtable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                        Hashtable temp = (Hashtable)ob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                        if (temp.containsKey(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                            Object object = temp.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                            if (object instanceof Vector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                result = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                int length = ((Vector)object).size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                    if (i == length - 1)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                        result +=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                        (String)(((Vector)object).elementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                        result +=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                        (String)(((Vector)object).elementAt(i))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                                                + " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                                result = (String)object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Gets the default boolean value for the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @param name the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @return the default boolean value, false is returned if it cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public boolean getDefaultBooleanValue(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        String val = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if (stanzaTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            val = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            val = getDefault(name, stanzaTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (val != null && val.equalsIgnoreCase("true")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Gets the default boolean value for the specified name in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * specified section.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * <br>This method is quicker by using the section name as the search key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @param name the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @param section the name of the section.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @return the default boolean value, false is returned if it cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public boolean getDefaultBooleanValue(String name, String section) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        String val = getDefault(name, section);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (val != null && val.equalsIgnoreCase("true")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Parses a string to an integer. The convertible strings include the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * string representations of positive integers, negative integers, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * hex decimal integers.  Valid inputs are, e.g., -1234, +1234,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * 0x40000.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @param input the String to be converted to an Integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @return an numeric value represented by the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @exception NumberFormationException if the String does not contain a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * parsable integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    private int parseIntValue(String input) throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        int value = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (input.startsWith("+")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            String temp = input.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            return Integer.parseInt(temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        } else if (input.startsWith("0x")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            String temp = input.substring(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            char[] chars = temp.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            if (chars.length > 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                throw new NumberFormatException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                for (int i = 0; i < chars.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    int index = chars.length - i - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    switch (chars[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    case '0':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                        value += 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    case '1':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                        value += 1 * getBase(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    case '2':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                        value += 2 * getBase(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    case '3':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        value += 3 * getBase(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    case '4':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                        value += 4 * getBase(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    case '5':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                        value += 5 * getBase(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    case '6':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                        value += 6 * getBase(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    case '7':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                        value += 7 * getBase(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    case '8':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                        value += 8 * getBase(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    case '9':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                        value += 9 * getBase(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    case 'a':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    case 'A':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                        value += 10 * getBase(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    case 'b':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    case 'B':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                        value += 11 * getBase(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    case 'c':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    case 'C':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                        value += 12 * getBase(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    case 'd':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    case 'D':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                        value += 13 * getBase(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    case 'e':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    case 'E':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                        value += 14 * getBase(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    case 'f':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    case 'F':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                        value += 15 * getBase(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                        throw new NumberFormatException("Invalid numerical format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            if (value < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                throw new NumberFormatException("Data overflow.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            value = Integer.parseInt(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    private int getBase(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        int result = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            result = BASE16_0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            result = BASE16_1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            result = BASE16_2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            result = BASE16_3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            for (int j = 1; j < i; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                result *= 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * Finds the matching value in the hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    private String find(String key1, String key2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        String result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        if ((stanzaTable != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            ((result = (String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                (((Hashtable)(stanzaTable.get(key1))).get(key2))) != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * Reads name/value pairs to the memory from the configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * file. The default location of the configuration file is in java home
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * Configuration file contains information about the default realm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * ticket parameters, location of the KDC and the admin server for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * known realms, etc. The file is divided into sections. Each section
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * contains one or more name/value pairs with one pair per line. A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * typical file would be:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * [libdefaults]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *          default_realm = EXAMPLE.COM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *          default_tgs_enctypes = des-cbc-md5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *          default_tkt_enctypes = des-cbc-md5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * [realms]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *          EXAMPLE.COM = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *                  kdc = kerberos.example.com
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *                  kdc = kerberos-1.example.com
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *                  admin_server = kerberos.example.com
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     *                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *          SAMPLE_COM = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *                  kdc = orange.sample.com
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *                  admin_server = orange.sample.com
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * [domain_realm]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *          blue.sample.com = TEST.SAMPLE.COM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *          .backup.com     = EXAMPLE.COM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    private Vector<String> loadConfigFile() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            final String fileName = getFileName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            if (!fileName.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                BufferedReader br = new BufferedReader(new InputStreamReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                new java.security.PrivilegedExceptionAction<FileInputStream> () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                public FileInputStream run() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    return new FileInputStream(fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                })));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                String Line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                Vector<String> v = new Vector<String> ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                String previous = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                while ((Line = br.readLine()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    // ignore comments and blank line in the configuration file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                    // Comments start with #.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    if (!(Line.startsWith("#") || Line.trim().isEmpty())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                        String current = Line.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                        // In practice, a subsection might look like:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                        //      EXAMPLE.COM =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                        //      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                        //              kdc = kerberos.example.com
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                        //              ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                        //      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                        // Before parsed into stanza table, it needs to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                        // converted into formal style:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                        //      EXAMPLE.COM = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                        //              kdc = kerberos.example.com
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                        //              ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                        //      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                        // So, if a line is "{", adhere to the previous line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                        if (current.equals("{")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                            if (previous == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                                throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                                    "Config file should not start with \"{\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                            previous += " " + current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                            if (previous != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                                v.addElement(previous);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                            previous = current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                if (previous != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    v.addElement(previous);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                br.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        } catch (java.security.PrivilegedActionException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            throw (IOException)pe.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * Parses stanza names and values from configuration file to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * stanzaTable (Hashtable). Hashtable key would be stanza names,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * (libdefaults, realms, domain_realms, etc), and the hashtable value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * would be another hashtable which contains the key-value pairs under
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * a stanza name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    private Hashtable<String,Object> parseStanzaTable(Vector<String> v) throws KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        if (v == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            throw new KrbException("I/O error while reading" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                        " configuration file.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        Hashtable<String,Object> table = new Hashtable<String,Object> ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        for (int i = 0; i < v.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            String line = v.elementAt(i).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            if (line.equalsIgnoreCase("[realms]")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                for (int count = i + 1; count < v.size() + 1; count++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                    // find the next stanza name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                    if ((count == v.size()) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                        (v.elementAt(count).startsWith("["))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                        Hashtable<String,Hashtable<String,Vector<String>>> temp =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                            new Hashtable<String,Hashtable<String,Vector<String>>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                        temp = parseRealmField(v, i + 1, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                        table.put("realms", temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                        i = count - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            } else if (line.equalsIgnoreCase("[capaths]")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                for (int count = i + 1; count < v.size() + 1; count++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                    // find the next stanza name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                    if ((count == v.size()) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                        (v.elementAt(count).startsWith("["))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                        Hashtable<String,Hashtable<String,Vector<String>>> temp =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                            new Hashtable<String,Hashtable<String,Vector<String>>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                        temp = parseRealmField(v, i + 1, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                        table.put("capaths", temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                        i = count - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            } else if (line.startsWith("[") && line.endsWith("]")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                String key = line.substring(1, line.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                for (int count = i + 1; count < v.size() + 1; count++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    // find the next stanza name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    if ((count == v.size()) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                        (v.elementAt(count).startsWith("["))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                        Hashtable<String,String> temp =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                            parseField(v, i + 1, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                        table.put(key, temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                        i = count - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        return table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * Gets the default configuration file name. The file will be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * in a list of possible loations in the following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * 1. the location and file name defined by system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * "java.security.krb5.conf",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * 2. at Java home lib\security directory with "krb5.conf" name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * 3. "krb5.ini" at Java home,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * 4. at windows directory with the name of "krb5.ini" for Windows,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * /etc/krb5/krb5.conf for Solaris, /etc/krb5.conf for Linux.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    private String getFileName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        String name =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                                new sun.security.action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                                GetPropertyAction("java.security.krb5.conf"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            boolean temp =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                                new FileExistsAction(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            if (temp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            name = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                        new sun.security.action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                        GetPropertyAction("java.home")) + File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                                "lib" + File.separator + "security" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                                File.separator + "krb5.conf";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            boolean temp =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                                new FileExistsAction(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            if (temp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                String osname =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                        java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                        new sun.security.action.GetPropertyAction("os.name"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                if (osname.startsWith("Windows")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                        Credentials.ensureLoaded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                        // ignore exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                    if (Credentials.alreadyLoaded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                        if ((name = getWindowsDirectory()) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                            name = "c:\\winnt\\krb5.ini";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                        } else if (name.endsWith("\\")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                            name += "krb5.ini";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                            name += "\\krb5.ini";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                        name = "c:\\winnt\\krb5.ini";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                } else if (osname.startsWith("SunOS")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                    name =  "/etc/krb5/krb5.conf";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                } else if (osname.startsWith("Linux")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                    name =  "/etc/krb5.conf";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            System.out.println("Config name: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * Parses key-value pairs under a stanza name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    private Hashtable<String,String>  parseField(Vector<String> v, int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        Hashtable<String,String> table = new Hashtable<String,String> ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        String line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        for (int i = start; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            line = v.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            for (int j = 0; j < line.length(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                if (line.charAt(j) == '=') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                    String key = (line.substring(0, j)).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                    String value = (line.substring(j + 1)).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                    table.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        return table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * Parses key-value pairs under [realms].  The key would be the realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * name, the value would be another hashtable which contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * information for the realm given within a pair of braces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    private Hashtable<String,Hashtable<String,Vector<String>>> parseRealmField(Vector<String> v, int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        Hashtable<String,Hashtable<String,Vector<String>>> table = new Hashtable<String,Hashtable<String,Vector<String>>> ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        String line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        for (int i = start; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            line = v.elementAt(i).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            if (line.endsWith("{")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                String key = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                for (int j = 0; j < line.length(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                    if (line.charAt(j) == '=') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                        key = line.substring(0, j).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                        // get the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                for (int k = i + 1; k < end; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                    boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                    line = v.elementAt(k).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                    for (int l = 0; l < line.length(); l++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                        if (line.charAt(l) == '}') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                            found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                    if (found == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                        Hashtable<String,Vector<String>> temp = parseRealmFieldEx(v, i + 1, k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                        table.put(key, temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                        i = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                        found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        return table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * Parses key-value pairs within each braces under [realms].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    private Hashtable<String,Vector<String>> parseRealmFieldEx(Vector<String> v, int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        Hashtable<String,Vector<String>> table =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                new Hashtable<String,Vector<String>> ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        Vector<String> keyVector = new Vector<String> ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        Vector<String> nameVector = new Vector<String> ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        String line = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        String key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        for (int i = start; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            line = v.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            for (int j = 0; j < line.length(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                if (line.charAt(j) == '=') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                    int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                    key = line.substring(0, j - 1).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                    if (! exists(key, keyVector)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                        keyVector.addElement(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                        nameVector = new Vector<String> ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                        nameVector = table.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                    nameVector.addElement((line.substring(j + 1)).trim());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                    table.put(key, nameVector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        return table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * Compares the key with the known keys to see if it exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    private boolean exists(String key, Vector v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        boolean exists = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        for (int i = 0; i < v.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            if (((String)(v.elementAt(i))).equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                exists = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        return exists;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * For testing purpose. This method lists all information being parsed from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * the configuration file to the hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    public void listTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        listTable(stanzaTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    private void listTable(Hashtable table) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        Vector v = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        String key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        if (stanzaTable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            for (Enumeration e = table.keys(); e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                key = (String)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                Object object = table.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                if (table == stanzaTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                    System.out.println("[" + key + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                if (object instanceof Hashtable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                    if (table != stanzaTable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                        System.out.println("\t" + key + " = {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                    listTable((Hashtable)object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                    if (table != stanzaTable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                        System.out.println("\t}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                } else if (object instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                    System.out.println("\t" + key + " = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                                (String)table.get(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                } else if (object instanceof Vector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                    v = (Vector)object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                    for (int i = 0; i < v.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                        System.out.println("\t" + key + " = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                                (String)v.elementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            System.out.println("Configuration file not found.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * Returns the default encryption types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    public int[] defaultEtype(String enctypes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        String default_enctypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        default_enctypes = getDefault(enctypes, "libdefaults");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        String delim = " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        StringTokenizer st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        int[] etype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        if (default_enctypes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                System.out.println("Using builtin default etypes for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                    enctypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            etype = EType.getBuiltInDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            for (int j = 0; j < default_enctypes.length(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                if (default_enctypes.substring(j, j + 1).equals(",")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                    // only two delimiters are allowed to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                    // according to Kerberos DCE doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                    delim = ",";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            st = new StringTokenizer(default_enctypes, delim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            int len = st.countTokens();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            ArrayList<Integer> ls = new ArrayList<Integer> (len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            int type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                type = getType(st.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                if ((type != -1) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                    (EType.isSupported(type))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                    ls.add(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            if (ls.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                        "no supported default etypes for " + enctypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                etype = new int[ls.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                for (int i = 0; i < etype.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                    etype[i] = ls.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            System.out.print("default etypes for " + enctypes + ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            for (int i = 0; i < etype.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                System.out.print(" " + etype[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            System.out.println(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        return etype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * Get the etype and checksum value for the specified encryption and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * checksum type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * This method converts the string representation of encryption type and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * checksum type to int value that can be later used by EType and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * Checksum classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    public int getType(String input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        int result = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        if (input == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        if (input.startsWith("d") || (input.startsWith("D"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            if (input.equalsIgnoreCase("des-cbc-crc")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                result = EncryptedData.ETYPE_DES_CBC_CRC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            } else if (input.equalsIgnoreCase("des-cbc-md5")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                result = EncryptedData.ETYPE_DES_CBC_MD5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            } else if (input.equalsIgnoreCase("des-mac")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                result = Checksum.CKSUMTYPE_DES_MAC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            } else if (input.equalsIgnoreCase("des-mac-k")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                result = Checksum.CKSUMTYPE_DES_MAC_K;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            } else if (input.equalsIgnoreCase("des-cbc-md4")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                result = EncryptedData.ETYPE_DES_CBC_MD4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            } else if (input.equalsIgnoreCase("des3-cbc-sha1") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                input.equalsIgnoreCase("des3-hmac-sha1") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                input.equalsIgnoreCase("des3-cbc-sha1-kd") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                input.equalsIgnoreCase("des3-cbc-hmac-sha1-kd")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                result = EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        } else if (input.startsWith("a") || (input.startsWith("A"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            // AES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            if (input.equalsIgnoreCase("aes128-cts") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                input.equalsIgnoreCase("aes128-cts-hmac-sha1-96")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                result = EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            } else if (input.equalsIgnoreCase("aes256-cts") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                input.equalsIgnoreCase("aes256-cts-hmac-sha1-96")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                result = EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            // ARCFOUR-HMAC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            } else if (input.equalsIgnoreCase("arcfour-hmac") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                   input.equalsIgnoreCase("arcfour-hmac-md5")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                result = EncryptedData.ETYPE_ARCFOUR_HMAC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        // RC4-HMAC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        } else if (input.equalsIgnoreCase("rc4-hmac")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            result = EncryptedData.ETYPE_ARCFOUR_HMAC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        } else if (input.equalsIgnoreCase("CRC32")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            result = Checksum.CKSUMTYPE_CRC32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        } else if (input.startsWith("r") || (input.startsWith("R"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            if (input.equalsIgnoreCase("rsa-md5")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                result = Checksum.CKSUMTYPE_RSA_MD5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            } else if (input.equalsIgnoreCase("rsa-md5-des")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                result = Checksum.CKSUMTYPE_RSA_MD5_DES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        } else if (input.equalsIgnoreCase("hmac-sha1-des3-kd")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            result = Checksum.CKSUMTYPE_HMAC_SHA1_DES3_KD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        } else if (input.equalsIgnoreCase("hmac-sha1-96-aes128")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            result = Checksum.CKSUMTYPE_HMAC_SHA1_96_AES128;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        } else if (input.equalsIgnoreCase("hmac-sha1-96-aes256")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            result = Checksum.CKSUMTYPE_HMAC_SHA1_96_AES256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        } else if (input.equalsIgnoreCase("hmac-md5-rc4") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                input.equalsIgnoreCase("hmac-md5-arcfour") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                input.equalsIgnoreCase("hmac-md5-enc")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            result = Checksum.CKSUMTYPE_HMAC_MD5_ARCFOUR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        } else if (input.equalsIgnoreCase("NULL")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            result = EncryptedData.ETYPE_NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * Resets the default kdc realm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * We do not need to synchronize these methods since assignments are atomic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    public void resetDefaultRealm(String realm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        defaultRealm = realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            System.out.println(">>> Config reset default kdc " + defaultRealm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * Check to use addresses in tickets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * use addresses if "no_addresses" or "noaddresses" is set to false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    public boolean useAddresses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        boolean useAddr = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        // use addresses if "no_addresses" is set to false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        String value = getDefault("no_addresses", "libdefaults");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        useAddr = (value != null && value.equalsIgnoreCase("false"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        if (useAddr == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            // use addresses if "noaddresses" is set to false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            value = getDefault("noaddresses", "libdefaults");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            useAddr = (value != null && value.equalsIgnoreCase("false"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        return useAddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * Check if need to use DNS to locate Kerberos services
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    public boolean useDNS(String name) {
93
6266821dc652 6673164: dns_fallback parse error
weijun
parents: 2
diff changeset
  1043
        String value = getDefault(name, "libdefaults");
6266821dc652 6673164: dns_fallback parse error
weijun
parents: 2
diff changeset
  1044
        if (value == null) {
6266821dc652 6673164: dns_fallback parse error
weijun
parents: 2
diff changeset
  1045
            return getDefaultBooleanValue("dns_fallback", "libdefaults");
6266821dc652 6673164: dns_fallback parse error
weijun
parents: 2
diff changeset
  1046
        } else {
6266821dc652 6673164: dns_fallback parse error
weijun
parents: 2
diff changeset
  1047
            return value.equalsIgnoreCase("true");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * Check if need to use DNS to locate the KDC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    public boolean useDNS_KDC() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        return useDNS("dns_lookup_kdc");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * Check if need to use DNS to locate the Realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    public boolean useDNS_Realm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        return useDNS("dns_lookup_realm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * Gets default realm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    public String getDefaultRealm() throws KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        String realm = getDefault("default_realm", "libdefaults");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        if ((realm == null) && useDNS_Realm()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            // use DNS to locate Kerberos realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            realm = getRealmFromDNS();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        return realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * Returns a list of KDC's with each KDC separated by a space
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * @param realm the realm for which the master KDC is desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * @return the list of KDCs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    public String getKDCList(String realm) throws KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        if (realm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            realm = getDefaultRealm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        String kdcs = getDefault("kdc", realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        if ((kdcs == null) && useDNS_KDC()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            // use DNS to locate KDC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            kdcs = getKDCFromDNS(realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        return kdcs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * Locate Kerberos realm using DNS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * @return the Kerberos realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    private String getRealmFromDNS() throws KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        // use DNS to locate Kerberos realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        String realm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        String hostName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            hostName = InetAddress.getLocalHost().getHostName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        } catch (UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            KrbException ke = new KrbException(Krb5.KRB_ERR_GENERIC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                "Unable to locate Kerberos realm: " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            ke.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            throw (ke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        // get the domain realm mapping from the configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        String mapRealm = PrincipalName.mapHostToRealm(hostName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        String[] records = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        String newRealm = mapRealm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        while ((records == null) && (newRealm != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            // locate DNS TXT record
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            records = KrbServiceLocator.getKerberosService(newRealm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            newRealm = Realm.parseRealmComponent(newRealm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            // if no DNS TXT records found, try again using sub-realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        if (records == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            // no DNS TXT records
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            throw new KrbException(Krb5.KRB_ERR_GENERIC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                                "Unable to locate Kerberos realm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        for (int i = 0; i < records.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            if (records[i].equals(mapRealm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                realm = records[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        if (found == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            throw new KrbException(Krb5.KRB_ERR_GENERIC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                                "Unable to locate Kerberos realm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        return realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * Locate KDC using DNS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * @param realm the realm for which the master KDC is desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * @return the KDC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    private String getKDCFromDNS(String realm) throws KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        // use DNS to locate KDC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        String kdcs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        String[] srvs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        // locate DNS SRV record using UDP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        srvs = KrbServiceLocator.getKerberosService(realm, "_udp.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        if (srvs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            // locate DNS SRV record using TCP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            srvs = KrbServiceLocator.getKerberosService(realm, "_tcp.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        if (srvs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            // no DNS SRV records
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            throw new KrbException(Krb5.KRB_ERR_GENERIC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                "Unable to locate KDC for realm " + realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        for (int i = 0; i < srvs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            String value = srvs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            for (int j = 0; j < srvs[i].length(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                // filter the KDC name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                if (value.charAt(j) == ':') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                    kdcs = (value.substring(0, j)).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        return kdcs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    static class FileExistsAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        implements java.security.PrivilegedAction<Boolean> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        private String fileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        public FileExistsAction(String fileName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            this.fileName = fileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        public Boolean run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            return new File(fileName).exists();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
}