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