jdk/src/share/classes/sun/security/krb5/Realm.java
author weijun
Thu, 24 Jun 2010 14:26:35 +0800
changeset 5975 076cd013e5e4
parent 5506 202f599c92aa
child 7977 f47f211cd627
permissions -rw-r--r--
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false) Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3949
diff changeset
     2
 * Copyright (c) 2000, 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: 3949
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: 3949
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: 3949
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3949
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3949
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.krb5.internal.Krb5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.EmptyStackException;
3949
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2163
diff changeset
    41
import sun.security.krb5.internal.util.KerberosString;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Implements the ASN.1 Realm type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Realm ::= GeneralString
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * </xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class Realm implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private String realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static boolean DEBUG = Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private Realm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public Realm(String name) throws RealmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        realm = parseRealm(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        Realm new_realm = new Realm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (realm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            new_realm.realm = new String(realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return new_realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (!(obj instanceof Realm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        Realm that = (Realm)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (this.realm != null && that.realm != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            return this.realm.equals(that.realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            return (this.realm == null && that.realm == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        int result = 17 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if( realm != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            result = 37 * result + realm.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Constructs a Realm object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param encoding a Der-encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @exception RealmException if an error occurs while parsing a Realm object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public Realm(DerValue encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        throws Asn1Exception, RealmException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (encoding == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            throw new IllegalArgumentException("encoding can not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
3949
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2163
diff changeset
   108
        realm = new KerberosString(encoding).toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (realm == null || realm.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throw new RealmException(Krb5.REALM_NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (!isValidRealmString(realm))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new RealmException(Krb5.REALM_ILLCHAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public static String parseRealmAtSeparator(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        throws RealmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                ("null input name is not allowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        String temp = new String(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        String result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        while (i < temp.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            if (temp.charAt(i) == PrincipalName.NAME_REALM_SEPARATOR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                if (i == 0 || temp.charAt(i - 1) != '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    if (i + 1 < temp.length())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                        result = temp.substring(i + 1, temp.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (result.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                throw new RealmException(Krb5.REALM_NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if (!isValidRealmString(result))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                throw new RealmException(Krb5.REALM_ILLCHAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public static String parseRealmComponent(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                ("null input name is not allowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        String temp = new String(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        String result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        while (i < temp.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (temp.charAt(i) == PrincipalName.REALM_COMPONENT_SEPARATOR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                if (i == 0 || temp.charAt(i - 1) != '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    if (i + 1 < temp.length())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        result = temp.substring(i + 1, temp.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    protected static String parseRealm(String name) throws RealmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        String result = parseRealmAtSeparator(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (result == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            result = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (result == null || result.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            throw new RealmException(Krb5.REALM_NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (!isValidRealmString(result))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            throw new RealmException(Krb5.REALM_ILLCHAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    // This is protected because the definition of a realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    // string is fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    protected static boolean isValidRealmString(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (name.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        for (int i = 0; i < name.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            if (name.charAt(i) == '/' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                name.charAt(i) == ':' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                name.charAt(i) == '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Encodes a Realm object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @return the byte array of encoded KrbCredInfo object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public byte[] asn1Encode() throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        DerOutputStream out = new DerOutputStream();
3949
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2163
diff changeset
   205
        out.putDerValue(new KerberosString(this.realm).toDerValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return out.toByteArray();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Parse (unmarshal) a realm from a DER input stream.  This form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * parsing might be used when expanding a value which is part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * a constructed sequence and uses explicitly tagged type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @exception Asn1Exception on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @param data the Der input stream value, which contains one or more marshaled value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param explicitTag tag number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param optional indicate if this data field is optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @return an instance of Realm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public static Realm parse(DerInputStream data, byte explicitTag, boolean optional) throws Asn1Exception, IOException, RealmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if ((optional) && (((byte)data.peekByte() & (byte)0x1F) != explicitTag)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        DerValue der = data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (explicitTag != (der.getTag() & (byte)0x1F))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            DerValue subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            return new Realm(subDer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
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
     * First leg of realms parsing. Used by getRealmsList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    private static String[] doInitialParse(String cRealm, String sRealm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        throws KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            if (cRealm == null || sRealm == null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                throw new KrbException(Krb5.API_INVALID_ARG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                System.out.println(">>> Realm doInitialParse: cRealm=["
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                   + cRealm + "], sRealm=[" +sRealm + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            if (cRealm.equals(sRealm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                String[] retList = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                retList = new String[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                retList[0] = new String(cRealm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    System.out.println(">>> Realm doInitialParse: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                                       + retList[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                return retList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Returns an array of realms that may be traversed to obtain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * a TGT from the initiating realm cRealm to the target realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * sRealm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * There may be an arbitrary number of intermediate realms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * between cRealm and sRealm. The realms may be organized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * organized hierarchically, or the paths between them may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * specified in the [capaths] stanza of the caller's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Kerberos configuration file. The configuration file is consulted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * first. Then a hirarchical organization is assumed if no realms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * are found in the configuration file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * The returned list, if not null, contains cRealm as the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * entry. sRealm is not included unless it is mistakenly listed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * in the configuration file as an intermediary realm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param cRealm the initiating realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @param sRealm the target realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @returns array of realms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @thows KrbException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public static String[] getRealmsList(String cRealm, String sRealm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        throws KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            String[] retList = doInitialParse(cRealm, sRealm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            if (retList != null && retList.length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                return retList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
             * Try [capaths].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            retList = parseCapaths(cRealm, sRealm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            if (retList != null && retList.length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                return retList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
             * Now assume the realms are organized hierarchically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            retList = parseHierarchy(cRealm, sRealm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            return retList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Parses the [capaths] stanza of the configuration file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * for a list of realms to traverse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * to obtain credentials from the initiating realm cRealm to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * the target realm sRealm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @param cRealm the initiating realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @param sRealm the target realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @returns array of realms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @ throws KrbException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * parseCapaths works for a capaths organized such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * for a given client realm C there is a tag C that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * contains subtags Ci ... Cn that completely define intermediate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * realms from C to target T. For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * [capaths]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *    TIVOLI.COM = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *        IBM.COM = IBM_LDAPCENTRAL.COM MOONLITE.ORG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *        IBM_LDAPCENTRAL.COM = LDAPCENTRAL.NET
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *        LDAPCENTRAL.NET = .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * The tag TIVOLI.COM contains subtags IBM.COM, IBM_LDAPCENTRAL.COM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * and LDAPCENTRAL.NET that completely define the path from TIVOLI.COM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * to IBM.COM (TIVOLI.COM->LADAPCENTRAL.NET->IBM_LDAPCENTRAL.COM->IBM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * or TIVOLI.COM->MOONLITE.ORG->IBM.COM).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * A direct path is assumed for an intermediary whose entry is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * "closed" by a "." In the above example, TIVOLI.COM is assumed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * to have a direct path to MOONLITE.ORG and MOONLITE.COM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * in turn to IBM.COM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    private static String[] parseCapaths(String cRealm, String sRealm) throws KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        String[] retList = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        Config cfg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            cfg = Config.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                System.out.println ("Configuration information can not be " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                                    "obtained " + exc.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        String intermediaries = cfg.getDefault(sRealm, cRealm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (intermediaries == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                System.out.println(">>> Realm parseCapaths: no cfg entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        String tempTarget = null, tempRealm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        Stack<String> iStack = new Stack<String> ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
         * I don't expect any more than a handful of intermediaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        Vector<String> tempList = new Vector<String> (8, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
         * The initiator at first location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        tempList.add(cRealm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        int count = 0; // For debug only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            tempTarget = sRealm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
2064
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   379
        out: do {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                System.out.println(">>> Realm parseCapaths: loop " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                                   count + ": target=" + tempTarget);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            if (intermediaries != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                !intermediaries.equals(PrincipalName.REALM_COMPONENT_SEPARATOR_STR))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    System.out.println(">>> Realm parseCapaths: loop " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                       count + ": intermediaries=[" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                                       intermediaries + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                 * We have one or more space-separated intermediary realms.
2064
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   397
                 * Stack them. A null is always added between intermedies of
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   398
                 * different targets. When this null is popped, it means none
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   399
                 * of the intermedies for this target is useful (because of
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   400
                 * infinite loop), the target is then removed from the partial
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   401
                 * tempList, and the next possible intermediary is tried.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                 */
2064
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   403
                iStack.push(null);
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   404
                String[] ints = intermediaries.split("\\s+");
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   405
                for (int i = ints.length-1; i>=0; i--)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                {
2064
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   407
                    tempRealm = ints[i];
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   408
                    if (tempRealm.equals(PrincipalName.REALM_COMPONENT_SEPARATOR_STR)) {
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   409
                        break out;
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   410
                    }
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   411
                    if (!tempList.contains(tempRealm)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                        iStack.push(tempRealm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                            System.out.println(">>> Realm parseCapaths: loop " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                                               count +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                                               ": pushed realm on to stack: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                                               tempRealm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    } else if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        System.out.println(">>> Realm parseCapaths: loop " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                                           count +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                                           ": ignoring realm: [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                                           tempRealm + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                }
2064
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   426
            } else {
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   427
                if (DEBUG) {
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   428
                    System.out.println(">>> Realm parseCapaths: loop " +
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   429
                                       count +
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   430
                                       ": no intermediaries");
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   431
                }
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   432
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
             * Get next intermediary realm from the stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            try {
2064
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   440
                while ((tempTarget = iStack.pop()) == null) {
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   441
                    tempList.removeElementAt(tempList.size()-1);
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   442
                    if (DEBUG) {
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   443
                        System.out.println(">>> Realm parseCapaths: backtrack, remove tail");
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   444
                    }
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   445
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            } catch (EmptyStackException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                tempTarget = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            if (tempTarget == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                 * No more intermediaries. We're done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            tempList.add(tempTarget);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                System.out.println(">>> Realm parseCapaths: loop " + count +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                                   ": added intermediary to list: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                                   tempTarget);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            intermediaries = cfg.getDefault(tempTarget, cRealm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        } while (true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        retList = new String[tempList.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            retList = tempList.toArray(retList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        } catch (ArrayStoreException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            retList = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        if (DEBUG && retList != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            for (int i = 0; i < retList.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                System.out.println(">>> Realm parseCapaths [" + i +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                                   "]=" + retList[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        return retList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * Build a list of realm that can be traversed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * to obtain credentials from the initiating realm cRealm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * for a service in the target realm sRealm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @param cRealm the initiating realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @param sRealm the target realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @returns array of realms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @throws KrbException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    private static String[] parseHierarchy(String cRealm, String sRealm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        throws KrbException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        String[] retList = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        // Parse the components and determine common part, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        String[] cComponents = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        String[] sComponents = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        StringTokenizer strTok =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        new StringTokenizer(cRealm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                            PrincipalName.REALM_COMPONENT_SEPARATOR_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        // Parse cRealm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        int cCount = strTok.countTokens();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        cComponents = new String[cCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        for (cCount = 0; strTok.hasMoreTokens(); cCount++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            cComponents[cCount] = strTok.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            System.out.println(">>> Realm parseHierarchy: cRealm has " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                               cCount + " components:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            while (j < cCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                System.out.println(">>> Realm parseHierarchy: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                                   "cComponents["+j+"]=" + cComponents[j++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        // Parse sRealm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        strTok = new StringTokenizer(sRealm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                                     PrincipalName.REALM_COMPONENT_SEPARATOR_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        int sCount = strTok.countTokens();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        sComponents = new String[sCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        for (sCount = 0; strTok.hasMoreTokens(); sCount++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            sComponents[sCount] = strTok.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            System.out.println(">>> Realm parseHierarchy: sRealm has " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                               sCount + " components:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            while (j < sCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                System.out.println(">>> Realm parseHierarchy: sComponents["+j+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                                   "]=" + sComponents[j++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        // Determine common components, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        int commonComponents = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        //while (sCount > 0 && cCount > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        //          sComponents[--sCount].equals(cComponents[--cCount]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        for (sCount--, cCount--; sCount >=0 && cCount >= 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                 sComponents[sCount].equals(cComponents[cCount]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
             sCount--, cCount--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            commonComponents++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        int cCommonStart = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        int sCommonStart = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        int links = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        if (commonComponents > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            sCommonStart = sCount+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            cCommonStart = cCount+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            // components from common to ancestors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            links += sCommonStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            links += cCommonStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            links++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            if (commonComponents > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                System.out.println(">>> Realm parseHierarchy: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                                   commonComponents + " common component" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                                   (commonComponents > 1 ? "s" : " "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                System.out.println(">>> Realm parseHierarchy: common part "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                                   +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                                   "in cRealm (starts at index " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                                   cCommonStart + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                System.out.println(">>> Realm parseHierarchy: common part in sRealm (starts at index " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                                   sCommonStart + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                String commonPart = substring(cRealm, cCommonStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                System.out.println(">>> Realm parseHierarchy: common part in cRealm=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                                   commonPart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                commonPart = substring(sRealm, sCommonStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                System.out.println(">>> Realm parseHierarchy: common part in sRealm=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                                   commonPart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            System.out.println(">>> Realm parseHierarchy: no common part");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            System.out.println(">>> Realm parseHierarchy: total links=" + links);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        retList = new String[links];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        retList[0] = new String(cRealm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            System.out.println(">>> Realm parseHierarchy A: retList[0]=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                               retList[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        // For an initiator realm A.B.C.D.COM,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        // build a list krbtgt/B.C.D.COM@A.B.C.D.COM up to the common part,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        // ie the issuer realm is the immediate descendant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        // of the target realm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        String cTemp = null, sTemp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        for (i = 1, cCount = 0; i < links && cCount < cCommonStart; cCount++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            sTemp = substring(cRealm, cCount+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            //cTemp = substring(cRealm, cCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            retList[i++] = new String(sTemp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                System.out.println(">>> Realm parseHierarchy B: retList[" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                                   (i-1) +"]="+retList[i-1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        for (sCount = sCommonStart; i < links && sCount - 1 > 0; sCount--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            sTemp = substring(sRealm, sCount-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            //cTemp = substring(sRealm, sCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            retList[i++] = new String(sTemp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                System.out.println(">>> Realm parseHierarchy D: retList[" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                                   (i-1) +"]="+retList[i-1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        return retList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    private static String substring(String realm, int componentIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        int i = 0 , j = 0, len = realm.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        while(i < len && j != componentIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            if (realm.charAt(i++) != PrincipalName.REALM_COMPONENT_SEPARATOR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        return realm.substring(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    static int getRandIndex(int arraySize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        return (int)(Math.random() * 16384.0) % arraySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    static void printNames(String[] names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        if (names == null || names.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        int len = names.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        System.out.println("List length = " + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        while (i < names.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            System.out.println("["+ i +"]=" + names[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
}