jdk/src/share/classes/sun/security/krb5/KrbKdcReq.java
author weijun
Tue, 09 Nov 2010 08:34:11 +0800
changeset 7175 46ec4c2dbc16
parent 5506 202f599c92aa
permissions -rw-r--r--
6952519: kdc_timeout is not being honoured when using TCP Reviewed-by: valeriep
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
package sun.security.krb5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    34
import java.security.AccessController;
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    35
import java.security.PrivilegedAction;
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    36
import java.security.Security;
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    37
import java.util.Locale;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.krb5.internal.Krb5;
7175
46ec4c2dbc16 6952519: kdc_timeout is not being honoured when using TCP
weijun
parents: 5506
diff changeset
    39
import sun.security.krb5.internal.NetClient;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.net.SocketTimeoutException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.PrivilegedActionException;
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    46
import java.util.ArrayList;
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    47
import java.util.List;
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    48
import java.util.Set;
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    49
import java.util.HashSet;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public abstract class KrbKdcReq {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
    53
    // The following settings can be configured in [libdefaults]
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
    54
    // section of krb5.conf, which are global for all realms. Each of
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
    55
    // them can also be defined in a realm, which overrides value here.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
    58
     * max retry time for a single KDC, default Krb5.KDC_RETRY_LIMIT (3)
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
    59
     */
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
    60
    private static int defaultKdcRetryLimit;
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
    61
    /**
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
    62
     * timeout requesting a ticket from KDC, in millisec, default 30 sec
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
    64
    private static int defaultKdcTimeout;
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
    65
    /**
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
    66
     * max UDP packet size, default unlimited (-1)
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
    67
     */
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
    68
    private static int defaultUdpPrefLimit;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static final boolean DEBUG = Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    72
    private static final String BAD_POLICY_KEY = "krb5.kdc.bad.policy";
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    73
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    74
    /**
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    75
     * What to do when a KDC is unavailable, specified in the
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    76
     * java.security file with key krb5.kdc.bad.policy.
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
    77
     * Possible values can be TRY_LAST or TRY_LESS. Reloaded when refreshed.
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    78
     */
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    79
    private enum BpType {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    80
        NONE, TRY_LAST, TRY_LESS
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    81
    }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    82
    private static int tryLessMaxRetries = 1;
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    83
    private static int tryLessTimeout = 5000;
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    84
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
    85
    private static BpType badPolicy;
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    86
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static {
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
    88
        initStatic();
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
    89
    }
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
    90
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
    91
    /**
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
    92
     * Read global settings
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
    93
     */
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
    94
    public static void initStatic() {
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    95
        String value = AccessController.doPrivileged(
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    96
        new PrivilegedAction<String>() {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    97
            public String run() {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    98
                return Security.getProperty(BAD_POLICY_KEY);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
    99
            }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   100
        });
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   101
        if (value != null) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   102
            value = value.toLowerCase(Locale.ENGLISH);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   103
            String[] ss = value.split(":");
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   104
            if ("tryless".equals(ss[0])) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   105
                if (ss.length > 1) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   106
                    String[] params = ss[1].split(",");
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   107
                    try {
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   108
                        int tmp0 = Integer.parseInt(params[0]);
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   109
                        if (params.length > 1) {
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   110
                            tryLessTimeout = Integer.parseInt(params[1]);
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   111
                        }
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   112
                        // Assign here in case of exception at params[1]
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   113
                        tryLessMaxRetries = tmp0;
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   114
                    } catch (NumberFormatException nfe) {
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   115
                        // Ignored. Please note that tryLess is recognized and
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   116
                        // used, parameters using default values
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   117
                        if (DEBUG) {
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   118
                            System.out.println("Invalid " + BAD_POLICY_KEY +
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   119
                                    " parameter for tryLess: " +
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   120
                                    value + ", use default");
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   121
                        }
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   122
                    }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   123
                }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   124
                badPolicy = BpType.TRY_LESS;
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   125
            } else if ("trylast".equals(ss[0])) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   126
                badPolicy = BpType.TRY_LAST;
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   127
            } else {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   128
                badPolicy = BpType.NONE;
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   129
            }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   130
        } else {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   131
            badPolicy = BpType.NONE;
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   132
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        int timeout = -1;
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   136
        int max_retries = -1;
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   137
        int udf_pref_limit = -1;
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   138
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            Config cfg = Config.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            String temp = cfg.getDefault("kdc_timeout", "libdefaults");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            timeout = parsePositiveIntString(temp);
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   143
            temp = cfg.getDefault("max_retries", "libdefaults");
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   144
            max_retries = parsePositiveIntString(temp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            temp = cfg.getDefault("udp_preference_limit", "libdefaults");
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   146
            udf_pref_limit = parsePositiveIntString(temp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        } catch (Exception exc) {
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   148
           // ignore any exceptions; use default values
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
           if (DEBUG) {
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   150
                System.out.println ("Exception in getting KDC communication " +
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   151
                                    "settings, using default value " +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                    exc.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   155
        defaultKdcTimeout = timeout > 0 ? timeout : 30*1000; // 30 seconds
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   156
        defaultKdcRetryLimit =
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   157
                max_retries > 0 ? max_retries : Krb5.KDC_RETRY_LIMIT;
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   158
        defaultUdpPrefLimit = udf_pref_limit;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   160
        KdcAccessibility.reset();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    protected byte[] obuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    protected byte[] ibuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Sends the provided data to the KDC of the specified realm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Returns the response from the KDC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Default realm/KDC is used if realm is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param realm the realm of the KDC where data is to be sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @returns the kdc to which the AS request was sent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @exception InterruptedIOException if timeout expires
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @exception KrbException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public String send(String realm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        throws IOException, KrbException {
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   178
        int udpPrefLimit = getRealmSpecificValue(
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   179
                realm, "udp_preference_limit", defaultUdpPrefLimit);
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   180
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        boolean useTCP = (udpPrefLimit > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
             (obuf != null && obuf.length > udpPrefLimit));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return (send(realm, useTCP));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public String send(String realm, boolean useTCP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        throws IOException, KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (obuf == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        Exception savedException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        Config cfg = Config.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (realm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            realm = cfg.getDefaultRealm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (realm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                throw new KrbException(Krb5.KRB_ERR_GENERIC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                       "Cannot find default realm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        String kdcList = cfg.getKDCList(realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (kdcList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            throw new KrbException("Cannot get kdc for realm " + realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        String tempKdc = null; // may include the port number also
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   208
        for (String tmp: KdcAccessibility.list(kdcList)) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   209
            tempKdc = tmp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                send(realm,tempKdc,useTCP);
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   212
                KdcAccessibility.removeBad(tempKdc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            } catch (Exception e) {
3864
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   215
                if (DEBUG) {
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   216
                    System.out.println(">>> KrbKdcReq send: error trying " +
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   217
                            tempKdc);
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   218
                    e.printStackTrace(System.out);
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   219
                }
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   220
                KdcAccessibility.addBad(tempKdc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                savedException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (ibuf == null && savedException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            if (savedException instanceof IOException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                throw (IOException) savedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                throw (KrbException) savedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return tempKdc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    // send the AS Request to the specified KDC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public void send(String realm, String tempKdc, boolean useTCP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        throws IOException, KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (obuf == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            return;
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   241
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        int port = Krb5.KDC_INET_DEFAULT_PORT;
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   243
        int retries = getRealmSpecificValue(
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   244
                realm, "max_retries", defaultKdcRetryLimit);
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   245
        int timeout = getRealmSpecificValue(
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   246
                realm, "kdc_timeout", defaultKdcTimeout);
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   247
        if (badPolicy == BpType.TRY_LESS &&
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   248
                KdcAccessibility.isBad(tempKdc)) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   249
            if (retries > tryLessMaxRetries) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   250
                retries = tryLessMaxRetries; // less retries
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   251
            }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   252
            if (timeout > tryLessTimeout) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   253
                timeout = tryLessTimeout; // less time
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   254
            }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   255
        }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   256
3864
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   257
        String kdc = null;
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   258
        String portStr = null;
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   259
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   260
        if (tempKdc.charAt(0) == '[') {     // Explicit IPv6 in []
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   261
            int pos = tempKdc.indexOf(']', 1);
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   262
            if (pos == -1) {
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   263
                throw new IOException("Illegal KDC: " + tempKdc);
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   264
            }
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   265
            kdc = tempKdc.substring(1, pos);
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   266
            if (pos != tempKdc.length() - 1) {  // with port number
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   267
                if (tempKdc.charAt(pos+1) != ':') {
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   268
                    throw new IOException("Illegal KDC: " + tempKdc);
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   269
                }
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   270
                portStr = tempKdc.substring(pos+2);
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   271
            }
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   272
        } else {
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   273
            int colon = tempKdc.indexOf(':');
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   274
            if (colon == -1) {      // Hostname or IPv4 host only
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   275
                kdc = tempKdc;
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   276
            } else {
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   277
                int nextColon = tempKdc.indexOf(':', colon+1);
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   278
                if (nextColon > 0) {    // >=2 ":", IPv6 with no port
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   279
                    kdc = tempKdc;
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   280
                } else {                // 1 ":", hostname or IPv4 with port
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   281
                    kdc = tempKdc.substring(0, colon);
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   282
                    portStr = tempKdc.substring(colon+1);
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   283
                }
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   284
            }
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   285
        }
21eb0b487d1f 6877357: IPv6 address does not work
weijun
parents: 2597
diff changeset
   286
        if (portStr != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            int tempPort = parsePositiveIntString(portStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            if (tempPort > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                port = tempPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            System.out.println(">>> KrbKdcReq send: kdc=" + kdc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                               + (useTCP ? " TCP:":" UDP:")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                               +  port +  ", timeout="
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                               + timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                               + ", number of retries ="
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   298
                               + retries
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                               + ", #bytes=" + obuf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        KdcCommunication kdcCommunication =
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   303
            new KdcCommunication(kdc, port, useTCP, timeout, retries, obuf);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            ibuf = AccessController.doPrivileged(kdcCommunication);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                System.out.println(">>> KrbKdcReq send: #bytes read="
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                        + (ibuf != null ? ibuf.length : 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            Exception wrappedException = e.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            if (wrappedException instanceof IOException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                throw (IOException) wrappedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                throw (KrbException) wrappedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            System.out.println(">>> KrbKdcReq send: #bytes read="
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                               + (ibuf != null ? ibuf.length : 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    private static class KdcCommunication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        implements PrivilegedExceptionAction<byte[]> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        private String kdc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        private int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        private boolean useTCP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        private int timeout;
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   331
        private int retries;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        private byte[] obuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        public KdcCommunication(String kdc, int port, boolean useTCP,
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   335
                                int timeout, int retries, byte[] obuf) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            this.kdc = kdc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            this.useTCP = useTCP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            this.timeout = timeout;
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   340
            this.retries = retries;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            this.obuf = obuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        // The caller only casts IOException and KrbException so don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        // add any new ones!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        public byte[] run() throws IOException, KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            byte[] ibuf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
7175
46ec4c2dbc16 6952519: kdc_timeout is not being honoured when using TCP
weijun
parents: 5506
diff changeset
   351
            for (int i=1; i <= retries; i++) {
46ec4c2dbc16 6952519: kdc_timeout is not being honoured when using TCP
weijun
parents: 5506
diff changeset
   352
                String proto = useTCP?"TCP":"UDP";
46ec4c2dbc16 6952519: kdc_timeout is not being honoured when using TCP
weijun
parents: 5506
diff changeset
   353
                NetClient kdcClient = NetClient.getInstance(
46ec4c2dbc16 6952519: kdc_timeout is not being honoured when using TCP
weijun
parents: 5506
diff changeset
   354
                        proto, kdc, port, timeout);
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   355
                if (DEBUG) {
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   356
                    System.out.println(">>> KDCCommunication: kdc=" + kdc
7175
46ec4c2dbc16 6952519: kdc_timeout is not being honoured when using TCP
weijun
parents: 5506
diff changeset
   357
                           + " " + proto + ":"
46ec4c2dbc16 6952519: kdc_timeout is not being honoured when using TCP
weijun
parents: 5506
diff changeset
   358
                           +  port +  ", timeout="
46ec4c2dbc16 6952519: kdc_timeout is not being honoured when using TCP
weijun
parents: 5506
diff changeset
   359
                           + timeout
46ec4c2dbc16 6952519: kdc_timeout is not being honoured when using TCP
weijun
parents: 5506
diff changeset
   360
                           + ",Attempt =" + i
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   361
                           + ", #bytes=" + obuf.length);
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   362
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                     * Send the data to the kdc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    kdcClient.send(obuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                     * And get a response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    ibuf = kdcClient.receive();
7175
46ec4c2dbc16 6952519: kdc_timeout is not being honoured when using TCP
weijun
parents: 5506
diff changeset
   372
                    break;
46ec4c2dbc16 6952519: kdc_timeout is not being honoured when using TCP
weijun
parents: 5506
diff changeset
   373
                } catch (SocketTimeoutException se) {
46ec4c2dbc16 6952519: kdc_timeout is not being honoured when using TCP
weijun
parents: 5506
diff changeset
   374
                    if (DEBUG) {
46ec4c2dbc16 6952519: kdc_timeout is not being honoured when using TCP
weijun
parents: 5506
diff changeset
   375
                        System.out.println ("SocketTimeOutException with " +
46ec4c2dbc16 6952519: kdc_timeout is not being honoured when using TCP
weijun
parents: 5506
diff changeset
   376
                                            "attempt: " + i);
46ec4c2dbc16 6952519: kdc_timeout is not being honoured when using TCP
weijun
parents: 5506
diff changeset
   377
                    }
46ec4c2dbc16 6952519: kdc_timeout is not being honoured when using TCP
weijun
parents: 5506
diff changeset
   378
                    if (i == retries) {
46ec4c2dbc16 6952519: kdc_timeout is not being honoured when using TCP
weijun
parents: 5506
diff changeset
   379
                        ibuf = null;
46ec4c2dbc16 6952519: kdc_timeout is not being honoured when using TCP
weijun
parents: 5506
diff changeset
   380
                        throw se;
46ec4c2dbc16 6952519: kdc_timeout is not being honoured when using TCP
weijun
parents: 5506
diff changeset
   381
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    kdcClient.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            return ibuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    /**
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   391
     * Returns krb5.conf setting of {@code key} for a specfic realm,
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   392
     * which can be:
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   393
     * 1. defined in the sub-stanza for the given realm inside [realms], or
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   394
     * 2. defined in [libdefaults], or
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   395
     * 3. defValue
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   396
     * @param realm the given realm in which the setting is requested. Returns
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   397
     * the global setting if null
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   398
     * @param key the key for the setting
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   399
     * @param defValue default value
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   400
     * @return a value for the key
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   402
    private int getRealmSpecificValue(String realm, String key, int defValue) {
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   403
        int v = defValue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   405
        if (realm == null) return v;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   407
        int temp = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        try {
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   409
            String value =
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   410
               Config.getInstance().getDefault(key, realm);
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   411
            temp = parsePositiveIntString(value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        } catch (Exception exc) {
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   413
            // Ignored, defValue will be picked up
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   416
        if (temp > 0) v = temp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   418
        return v;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   421
    private static int parsePositiveIntString(String intString) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if (intString == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        int ret = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            ret = Integer.parseInt(intString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (ret >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   438
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   439
    /**
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   440
     * Maintains a KDC accessible list. Unavailable KDCs are put into a
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   441
     * blacklist, when a KDC in the blacklist is available, it's removed
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   442
     * from there. No insertion order in the blacklist.
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   443
     *
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   444
     * There are two methods to deal with KDCs in the blacklist. 1. Only try
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   445
     * them when there's no KDC not on the blacklist. 2. Still try them, but
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   446
     * with lesser number of retries and smaller timeout value.
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   447
     */
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   448
    static class KdcAccessibility {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   449
        // Known bad KDCs
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   450
        private static Set<String> bads = new HashSet<String>();
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   451
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   452
        private static synchronized void addBad(String kdc) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   453
            if (DEBUG) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   454
                System.out.println(">>> KdcAccessibility: add " + kdc);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   455
            }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   456
            bads.add(kdc);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   457
        }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   458
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   459
        private static synchronized void removeBad(String kdc) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   460
            if (DEBUG) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   461
                System.out.println(">>> KdcAccessibility: remove " + kdc);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   462
            }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   463
            bads.remove(kdc);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   464
        }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   465
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   466
        private static synchronized boolean isBad(String kdc) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   467
            return bads.contains(kdc);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   468
        }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   469
5458
62f857d96000 6844193: support max_retries in krb5.conf
weijun
parents: 4531
diff changeset
   470
        private static synchronized void reset() {
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   471
            if (DEBUG) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   472
                System.out.println(">>> KdcAccessibility: reset");
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   473
            }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   474
            bads.clear();
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   475
        }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   476
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   477
        // Returns a preferred KDC list by putting the bad ones at the end
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   478
        private static synchronized String[] list(String kdcList) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   479
            StringTokenizer st = new StringTokenizer(kdcList);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   480
            List<String> list = new ArrayList<String>();
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   481
            if (badPolicy == BpType.TRY_LAST) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   482
                List<String> badkdcs = new ArrayList<String>();
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   483
                while (st.hasMoreTokens()) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   484
                    String t = st.nextToken();
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   485
                    if (bads.contains(t)) badkdcs.add(t);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   486
                    else list.add(t);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   487
                }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   488
                // Bad KDCs are put at last
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   489
                list.addAll(badkdcs);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   490
            } else {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   491
                // All KDCs are returned in their original order,
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   492
                // This include TRY_LESS and NONE
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   493
                while (st.hasMoreTokens()) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   494
                    list.add(st.nextToken());
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   495
                }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   496
            }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   497
            return list.toArray(new String[list.size()]);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   498
        }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   499
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
}
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 3864
diff changeset
   501