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