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