jdk/src/share/classes/sun/security/krb5/internal/CredentialsUtil.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 4336 4c792c19266e
child 13247 74902cfeb9c6
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
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: 4336
diff changeset
     2
 * Copyright (c) 2001, 2009, 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: 4336
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: 4336
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: 4336
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4336
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4336
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.internal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.krb5.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.krb5.internal.ccache.CredentialsCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.krb5.internal.ktab.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.BufferedReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.InputStreamReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.io.UnsupportedEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * This class is a utility that contains much of the TGS-Exchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * protocol. It is used by ../Credentials.java for service ticket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * acquisition in both the normal and the x-realm case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class CredentialsUtil {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static boolean DEBUG = sun.security.krb5.internal.Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    * Acquires credentials for a specified service using initial credential. Wh
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
en the service has a different realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    * from the initial credential, we do cross-realm authentication - first, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 use the current credential to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    * a cross-realm credential from the local KDC, then use that cross-realm cr
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
edential to request service credential
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    * from the foreigh KDC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    * @param service the name of service principal using format components@real
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
m
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    * @param ccreds client's initial credential.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    * @exception Exception general exception will be thrown when any error occu
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
rs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    * @return a <code>Credentials</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static Credentials acquireServiceCreds(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                String service, Credentials ccreds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        ServiceName sname = new ServiceName(service);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        String serviceRealm = sname.getRealmString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        String localRealm = ccreds.getClient().getRealmString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        String defaultRealm = Config.getInstance().getDefaultRealm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (localRealm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            PrincipalName temp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if ((temp = ccreds.getServer()) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                localRealm = temp.getRealmString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (localRealm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            localRealm = defaultRealm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (serviceRealm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            serviceRealm = localRealm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            sname.setRealm(serviceRealm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
          if (!localRealm.equalsIgnoreCase(serviceRealm)) { //do cross-realm auth entication
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
          if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
          System.out.println(">>>DEBUG: Credentails request cross realm ticket for " + "krbtgt/" + serviceRealm + "@" + localRealm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
          Credentials crossCreds = serviceCreds(new ServiceName("krbtgt/" + serviceRealm + "@" + localRealm), ccreds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
          if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
          printDebug(crossCreds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
          Credentials result = serviceCreds(sname, crossCreds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
          if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
          printDebug(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
          return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
          else return serviceCreds(sname, ccreds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (localRealm.equals(serviceRealm))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (DEBUG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                System.out.println(">>> Credentials acquireServiceCreds: same realm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return serviceCreds(sname, ccreds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        // Get a list of realms to traverse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        String[] realms = Realm.getRealmsList(localRealm, serviceRealm);
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 2
diff changeset
   120
        boolean okAsDelegate = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (realms == null || realms.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (DEBUG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                System.out.println(">>> Credentials acquireServiceCreds: no realms list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        int i = 0, k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        Credentials cTgt = null, newTgt = null, theTgt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        ServiceName tempService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        String realm = null, newTgtRealm = null, theTgtRealm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        for (cTgt = ccreds, i = 0; i < realms.length;)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            tempService = new ServiceName(PrincipalName.TGS_DEFAULT_SRV_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                          serviceRealm, realms[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (DEBUG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                System.out.println(">>> Credentials acquireServiceCreds: main loop: [" + i +"] tempService=" + tempService);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                newTgt = serviceCreds(tempService, cTgt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                newTgt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (newTgt == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                if (DEBUG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    System.out.println(">>> Credentials acquireServiceCreds: no tgt; searching backwards");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                 * No tgt found. Try to get one for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                 * realm as close to the target as possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                 * That means traversing the realms list backwards.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                for (newTgt = null, k = realms.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                     newTgt == null && k > i; k--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    tempService = new ServiceName(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                       PrincipalName.TGS_DEFAULT_SRV_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                                       realms[k], realms[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    if (DEBUG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                        System.out.println(">>> Credentials acquireServiceCreds: inner loop: [" + k +"] tempService=" + tempService);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        newTgt = serviceCreds(tempService, cTgt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        newTgt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            } // Ends 'if (newTgt == null)'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (newTgt == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                if (DEBUG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    System.out.println(">>> Credentials acquireServiceCreds: no tgt; cannot get creds");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
             * We have a tgt. It may or may not be for the target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
             * If it's for the target realm, we're done looking for a tgt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            newTgtRealm = newTgt.getServer().getInstanceComponent();
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 2
diff changeset
   198
            if (okAsDelegate && !newTgt.checkDelegate()) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 2
diff changeset
   199
                if (DEBUG)
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 2
diff changeset
   200
                {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 2
diff changeset
   201
                    System.out.println(">>> Credentials acquireServiceCreds: " +
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 2
diff changeset
   202
                            "global OK-AS-DELEGATE turned off at " +
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 2
diff changeset
   203
                            newTgt.getServer());
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 2
diff changeset
   204
                }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 2
diff changeset
   205
                okAsDelegate = false;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 2
diff changeset
   206
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            if (DEBUG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                System.out.println(">>> Credentials acquireServiceCreds: got tgt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                //printDebug(newTgt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            if (newTgtRealm.equals(serviceRealm))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                /* We got the right tgt */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                theTgt = newTgt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                theTgtRealm = newTgtRealm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
             * The new tgt is not for the target realm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
             * See if the realm of the new tgt is in the list of realms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
             * and continue looking from there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            for (k = i+1; k < realms.length; k++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                if (newTgtRealm.equals(realms[k]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            if (k < realms.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                 * (re)set the counter so we start looking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                 * from the realm we just obtained a tgt for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                i = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                cTgt = newTgt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                if (DEBUG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    System.out.println(">>> Credentials acquireServiceCreds: continuing with main loop counter reset to " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                 * The new tgt's realm is not in the heirarchy of realms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                 * It's probably not safe to get a tgt from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                 * a tgs that is outside the known list of realms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                 * Give up now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        } // Ends outermost/main 'for' loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        Credentials theCreds = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (theTgt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            /* We have the right tgt. Let's get the service creds */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            if (DEBUG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                System.out.println(">>> Credentials acquireServiceCreds: got right tgt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                //printDebug(theTgt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                System.out.println(">>> Credentials acquireServiceCreds: obtaining service creds for " + sname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                theCreds = serviceCreds(sname, theTgt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
              if (DEBUG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                System.out.println(exc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
              theCreds = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (theCreds != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if (DEBUG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                System.out.println(">>> Credentials acquireServiceCreds: returning creds:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                Credentials.printDebug(theCreds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 2
diff changeset
   296
            if (!okAsDelegate) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 2
diff changeset
   297
                theCreds.resetDelegate();
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 2
diff changeset
   298
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            return theCreds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        throw new KrbApErrException(Krb5.KRB_AP_ERR_GEN_CRED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                    "No service creds");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
   /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    * This method does the real job to request the service credential.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    private static Credentials serviceCreds(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            ServiceName service, Credentials ccreds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        return new KrbTgsReq(ccreds, service).sendAndGetCreds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
}