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