author | weijun |
Tue, 14 Aug 2018 22:39:34 +0800 | |
changeset 51398 | 3c389a284345 |
parent 47216 | 71c04702a3d5 |
child 58611 | 53ddf218eddd |
permissions | -rw-r--r-- |
2 | 1 |
/* |
51398
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 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 |
||
51398
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
34 |
import sun.security.action.GetBooleanAction; |
2 | 35 |
import sun.security.krb5.internal.Krb5; |
36 |
import sun.security.util.*; |
|
37 |
import java.io.IOException; |
|
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
38 |
import java.util.*; |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
39 |
|
3949
00603a93b589
6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents:
2163
diff
changeset
|
40 |
import sun.security.krb5.internal.util.KerberosString; |
2 | 41 |
|
42 |
/** |
|
43 |
* Implements the ASN.1 Realm type. |
|
44 |
* |
|
32003 | 45 |
* {@code Realm ::= GeneralString} |
46 |
* |
|
13247 | 47 |
* This class is immutable. |
2 | 48 |
*/ |
49 |
public class Realm implements Cloneable { |
|
33282 | 50 |
|
51398
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
51 |
public static final boolean AUTODEDUCEREALM = GetBooleanAction |
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
52 |
.privilegedGetProperty("sun.security.krb5.autodeducerealm"); |
33282 | 53 |
|
13247 | 54 |
private final String realm; // not null nor empty |
2 | 55 |
|
56 |
public Realm(String name) throws RealmException { |
|
57 |
realm = parseRealm(name); |
|
58 |
} |
|
59 |
||
13247 | 60 |
public static Realm getDefault() throws RealmException { |
61 |
try { |
|
62 |
return new Realm(Config.getInstance().getDefaultRealm()); |
|
63 |
} catch (RealmException re) { |
|
64 |
throw re; |
|
65 |
} catch (KrbException ke) { |
|
66 |
throw new RealmException(ke); |
|
67 |
} |
|
68 |
} |
|
69 |
||
70 |
// Immutable class, no need to clone |
|
2 | 71 |
public Object clone() { |
13247 | 72 |
return this; |
2 | 73 |
} |
74 |
||
75 |
public boolean equals(Object obj) { |
|
76 |
if (this == obj) { |
|
77 |
return true; |
|
78 |
} |
|
79 |
||
80 |
if (!(obj instanceof Realm)) { |
|
81 |
return false; |
|
82 |
} |
|
83 |
||
84 |
Realm that = (Realm)obj; |
|
13247 | 85 |
return this.realm.equals(that.realm); |
2 | 86 |
} |
87 |
||
88 |
public int hashCode() { |
|
13247 | 89 |
return realm.hashCode(); |
2 | 90 |
} |
91 |
||
92 |
/** |
|
93 |
* Constructs a Realm object. |
|
94 |
* @param encoding a Der-encoded data. |
|
95 |
* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data. |
|
96 |
* @exception IOException if an I/O error occurs while reading encoded data. |
|
97 |
* @exception RealmException if an error occurs while parsing a Realm object. |
|
98 |
*/ |
|
99 |
public Realm(DerValue encoding) |
|
100 |
throws Asn1Exception, RealmException, IOException { |
|
101 |
if (encoding == null) { |
|
102 |
throw new IllegalArgumentException("encoding can not be null"); |
|
103 |
} |
|
3949
00603a93b589
6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents:
2163
diff
changeset
|
104 |
realm = new KerberosString(encoding).toString(); |
2 | 105 |
if (realm == null || realm.length() == 0) |
106 |
throw new RealmException(Krb5.REALM_NULL); |
|
107 |
if (!isValidRealmString(realm)) |
|
108 |
throw new RealmException(Krb5.REALM_ILLCHAR); |
|
109 |
} |
|
110 |
||
111 |
public String toString() { |
|
112 |
return realm; |
|
113 |
} |
|
114 |
||
13247 | 115 |
// Extract realm from a string like dummy@REALM |
2 | 116 |
public static String parseRealmAtSeparator(String name) |
117 |
throws RealmException { |
|
118 |
if (name == null) { |
|
119 |
throw new IllegalArgumentException |
|
120 |
("null input name is not allowed"); |
|
121 |
} |
|
122 |
String temp = new String(name); |
|
123 |
String result = null; |
|
124 |
int i = 0; |
|
125 |
while (i < temp.length()) { |
|
126 |
if (temp.charAt(i) == PrincipalName.NAME_REALM_SEPARATOR) { |
|
127 |
if (i == 0 || temp.charAt(i - 1) != '\\') { |
|
13247 | 128 |
if (i + 1 < temp.length()) { |
2 | 129 |
result = temp.substring(i + 1, temp.length()); |
13247 | 130 |
} else { |
131 |
throw new IllegalArgumentException |
|
132 |
("empty realm part not allowed"); |
|
133 |
} |
|
2 | 134 |
break; |
135 |
} |
|
136 |
} |
|
137 |
i++; |
|
138 |
} |
|
139 |
if (result != null) { |
|
140 |
if (result.length() == 0) |
|
141 |
throw new RealmException(Krb5.REALM_NULL); |
|
142 |
if (!isValidRealmString(result)) |
|
143 |
throw new RealmException(Krb5.REALM_ILLCHAR); |
|
144 |
} |
|
145 |
return result; |
|
146 |
} |
|
147 |
||
148 |
public static String parseRealmComponent(String name) { |
|
149 |
if (name == null) { |
|
150 |
throw new IllegalArgumentException |
|
151 |
("null input name is not allowed"); |
|
152 |
} |
|
153 |
String temp = new String(name); |
|
154 |
String result = null; |
|
155 |
int i = 0; |
|
156 |
while (i < temp.length()) { |
|
157 |
if (temp.charAt(i) == PrincipalName.REALM_COMPONENT_SEPARATOR) { |
|
158 |
if (i == 0 || temp.charAt(i - 1) != '\\') { |
|
159 |
if (i + 1 < temp.length()) |
|
160 |
result = temp.substring(i + 1, temp.length()); |
|
161 |
break; |
|
162 |
} |
|
163 |
} |
|
164 |
i++; |
|
165 |
} |
|
166 |
return result; |
|
167 |
} |
|
168 |
||
169 |
protected static String parseRealm(String name) throws RealmException { |
|
170 |
String result = parseRealmAtSeparator(name); |
|
171 |
if (result == null) |
|
172 |
result = name; |
|
173 |
if (result == null || result.length() == 0) |
|
174 |
throw new RealmException(Krb5.REALM_NULL); |
|
175 |
if (!isValidRealmString(result)) |
|
176 |
throw new RealmException(Krb5.REALM_ILLCHAR); |
|
177 |
return result; |
|
178 |
} |
|
179 |
||
180 |
// This is protected because the definition of a realm |
|
181 |
// string is fixed |
|
182 |
protected static boolean isValidRealmString(String name) { |
|
183 |
if (name == null) |
|
184 |
return false; |
|
185 |
if (name.length() == 0) |
|
186 |
return false; |
|
187 |
for (int i = 0; i < name.length(); i++) { |
|
188 |
if (name.charAt(i) == '/' || |
|
189 |
name.charAt(i) == ':' || |
|
190 |
name.charAt(i) == '\0') { |
|
191 |
return false; |
|
192 |
} |
|
193 |
} |
|
194 |
return true; |
|
195 |
} |
|
196 |
||
197 |
/** |
|
198 |
* Encodes a Realm object. |
|
199 |
* @return the byte array of encoded KrbCredInfo object. |
|
200 |
* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data. |
|
201 |
* @exception IOException if an I/O error occurs while reading encoded data. |
|
202 |
* |
|
203 |
*/ |
|
204 |
public byte[] asn1Encode() throws Asn1Exception, IOException { |
|
205 |
DerOutputStream out = new DerOutputStream(); |
|
3949
00603a93b589
6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents:
2163
diff
changeset
|
206 |
out.putDerValue(new KerberosString(this.realm).toDerValue()); |
2 | 207 |
return out.toByteArray(); |
208 |
} |
|
209 |
||
210 |
||
211 |
/** |
|
212 |
* Parse (unmarshal) a realm from a DER input stream. This form |
|
213 |
* parsing might be used when expanding a value which is part of |
|
214 |
* a constructed sequence and uses explicitly tagged type. |
|
215 |
* |
|
216 |
* @exception Asn1Exception on error. |
|
217 |
* @param data the Der input stream value, which contains one or more marshaled value. |
|
218 |
* @param explicitTag tag number. |
|
219 |
* @param optional indicate if this data field is optional |
|
220 |
* @return an instance of Realm. |
|
221 |
* |
|
222 |
*/ |
|
13247 | 223 |
public static Realm parse(DerInputStream data, byte explicitTag, boolean optional) |
224 |
throws Asn1Exception, IOException, RealmException { |
|
2 | 225 |
if ((optional) && (((byte)data.peekByte() & (byte)0x1F) != explicitTag)) { |
226 |
return null; |
|
227 |
} |
|
228 |
DerValue der = data.getDerValue(); |
|
229 |
if (explicitTag != (der.getTag() & (byte)0x1F)) { |
|
230 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
231 |
} else { |
|
232 |
DerValue subDer = der.getData().getDerValue(); |
|
233 |
return new Realm(subDer); |
|
234 |
} |
|
235 |
} |
|
236 |
||
237 |
/** |
|
238 |
* Returns an array of realms that may be traversed to obtain |
|
239 |
* a TGT from the initiating realm cRealm to the target realm |
|
240 |
* sRealm. |
|
241 |
* <br> |
|
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
242 |
* This method would read [capaths] to create a path, or generate a |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
243 |
* hierarchical path if [capaths] does not contain a sub-stanza for cRealm |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
244 |
* or the sub-stanza does not contain a tag for sRealm. |
2 | 245 |
* <br> |
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
246 |
* The returned list would never be null, and it always contains |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
247 |
* cRealm as the head entry. sRealm is not included as the tail. |
2 | 248 |
* |
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
249 |
* @param cRealm the initiating realm, not null |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
250 |
* @param sRealm the target realm, not null, not equals to cRealm |
32003 | 251 |
* @return array of realms including at least cRealm as the first |
252 |
* element |
|
2 | 253 |
*/ |
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
254 |
public static String[] getRealmsList(String cRealm, String sRealm) { |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
255 |
try { |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
256 |
// Try [capaths] |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
257 |
return parseCapaths(cRealm, sRealm); |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
258 |
} catch (KrbException ke) { |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
259 |
// Now assume the realms are organized hierarchically. |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
260 |
return parseHierarchy(cRealm, sRealm); |
2 | 261 |
} |
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
262 |
} |
2 | 263 |
|
264 |
/** |
|
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
265 |
* Parses the [capaths] stanza of the configuration file for a |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
266 |
* list of realms to traverse to obtain credentials from the |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
267 |
* initiating realm cRealm to the target realm sRealm. |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
268 |
* |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
269 |
* For a given client realm C there is a tag C in [capaths] whose |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
270 |
* subtag S has a value which is a (possibly partial) path from C |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
271 |
* to S. When the path is partial, it contains only the tail of the |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
272 |
* full path. Values of other subtags will be used to build the full |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
273 |
* path. The value "." means a direct path from C to S. If realm S |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
274 |
* does not appear as a subtag, there is no path defined here. |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
275 |
* |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
276 |
* The implementation ignores all values which equals to C or S, or |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
277 |
* a "." in multiple values, or any duplicated realm names. |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
278 |
* |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
279 |
* When a path value has more than two realms, they can be specified |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
280 |
* with multiple key-value pairs each having a single value, but the |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
281 |
* order must not change. |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
282 |
* |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
283 |
* For example: |
2 | 284 |
* |
285 |
* [capaths] |
|
286 |
* TIVOLI.COM = { |
|
287 |
* IBM.COM = IBM_LDAPCENTRAL.COM MOONLITE.ORG |
|
288 |
* IBM_LDAPCENTRAL.COM = LDAPCENTRAL.NET |
|
289 |
* LDAPCENTRAL.NET = . |
|
290 |
* } |
|
291 |
* |
|
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
292 |
* TIVOLI.COM has a direct path to LDAPCENTRAL.NET, which has a direct |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
293 |
* path to IBM_LDAPCENTRAL.COM. It also has a partial path to IBM.COM |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
294 |
* being "IBM_LDAPCENTRAL.COM MOONLITE.ORG". Merging these info together, |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
295 |
* a full path from TIVOLI.COM to IBM.COM will be |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
296 |
* |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
297 |
* TIVOLI.COM -> LDAPCENTRAL.NET -> IBM_LDAPCENTRAL.COM |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
298 |
* -> IBM_LDAPCENTRAL.COM -> MOONLITE.ORG |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
299 |
* |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
300 |
* Please note the sRealm IBM.COM does not appear in the path. |
2 | 301 |
* |
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
302 |
* @param cRealm the initiating realm |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
303 |
* @param sRealm the target realm, not the same as cRealm |
32275
17eeb583a331
8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents:
32003
diff
changeset
|
304 |
* @return array of realms including at least cRealm as the first |
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
305 |
* element |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
306 |
* @throws KrbException if the config does not contain a sub-stanza |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
307 |
* for cRealm in [capaths] or the sub-stanza does not contain |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
308 |
* sRealm as a tag |
2 | 309 |
*/ |
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
310 |
private static String[] parseCapaths(String cRealm, String sRealm) |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
311 |
throws KrbException { |
2 | 312 |
|
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
313 |
// This line could throw a KrbException |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
314 |
Config cfg = Config.getInstance(); |
2 | 315 |
|
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
316 |
if (!cfg.exists("capaths", cRealm, sRealm)) { |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
317 |
throw new KrbException("No conf"); |
2 | 318 |
} |
319 |
||
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
320 |
LinkedList<String> path = new LinkedList<>(); |
2 | 321 |
|
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
322 |
String head = sRealm; |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
323 |
while (true) { |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
324 |
String value = cfg.getAll("capaths", cRealm, head); |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
325 |
if (value == null) { |
2064 | 326 |
break; |
2 | 327 |
} |
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
328 |
String[] more = value.split("\\s+"); |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
329 |
boolean changed = false; |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
330 |
for (int i=more.length-1; i>=0; i--) { |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
331 |
if (path.contains(more[i]) |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
332 |
|| more[i].equals(".") |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
333 |
|| more[i].equals(cRealm) |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
334 |
|| more[i].equals(sRealm) |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
335 |
|| more[i].equals(head)) { |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
336 |
// Ignore invalid values |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
337 |
continue; |
2064 | 338 |
} |
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
339 |
changed = true; |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
340 |
path.addFirst(more[i]); |
2 | 341 |
} |
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
342 |
if (!changed) break; |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
343 |
head = path.getFirst(); |
9007
05f25362fb76
7019384: Realm.getRealmsList returns realms list in wrong (reverse) order
weijun
parents:
7977
diff
changeset
|
344 |
} |
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
345 |
path.addFirst(cRealm); |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
346 |
return path.toArray(new String[path.size()]); |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
347 |
} |
2 | 348 |
|
349 |
/** |
|
350 |
* Build a list of realm that can be traversed |
|
351 |
* to obtain credentials from the initiating realm cRealm |
|
352 |
* for a service in the target realm sRealm. |
|
353 |
* @param cRealm the initiating realm |
|
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
354 |
* @param sRealm the target realm, not the same as cRealm |
32275
17eeb583a331
8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents:
32003
diff
changeset
|
355 |
* @return array of realms including cRealm as the first element |
2 | 356 |
*/ |
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
357 |
private static String[] parseHierarchy(String cRealm, String sRealm) { |
2 | 358 |
|
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
359 |
String[] cComponents = cRealm.split("\\."); |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
360 |
String[] sComponents = sRealm.split("\\."); |
2 | 361 |
|
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
362 |
int cPos = cComponents.length; |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
363 |
int sPos = sComponents.length; |
2 | 364 |
|
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
365 |
boolean hasCommon = false; |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
366 |
for (sPos--, cPos--; sPos >=0 && cPos >= 0 && |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
367 |
sComponents[sPos].equals(cComponents[cPos]); |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
368 |
sPos--, cPos--) { |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
369 |
hasCommon = true; |
2 | 370 |
} |
371 |
||
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
372 |
// For those with common components: |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
373 |
// length pos |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
374 |
// SITES1.SALES.EXAMPLE.COM 4 1 |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
375 |
// EVERYWHERE.EXAMPLE.COM 3 0 |
2 | 376 |
|
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
377 |
// For those without common components: |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
378 |
// length pos |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
379 |
// DEVEL.EXAMPLE.COM 3 2 |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
380 |
// PROD.EXAMPLE.ORG 3 2 |
2 | 381 |
|
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
382 |
LinkedList<String> path = new LinkedList<>(); |
2 | 383 |
|
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
384 |
// Un-common ones for client side |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
385 |
for (int i=0; i<=cPos; i++) { |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
386 |
path.addLast(subStringFrom(cComponents, i)); |
2 | 387 |
} |
388 |
||
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
389 |
// Common one |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
390 |
if (hasCommon) { |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
391 |
path.addLast(subStringFrom(cComponents, cPos+1)); |
2 | 392 |
} |
393 |
||
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
394 |
// Un-common ones for server side |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
395 |
for (int i=sPos; i>=0; i--) { |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
396 |
path.addLast(subStringFrom(sComponents, i)); |
2 | 397 |
} |
398 |
||
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
399 |
// Remove sRealm from path. Note that it might be added at last loop |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
400 |
// or as a common component, if sRealm is a parent of cRealm |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
401 |
path.removeLast(); |
2 | 402 |
|
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
403 |
return path.toArray(new String[path.size()]); |
2 | 404 |
} |
405 |
||
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
406 |
/** |
21278 | 407 |
* Creates a realm name using components from the given position. |
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
408 |
* For example, subStringFrom({"A", "B", "C"}, 1) is "B.C". |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
409 |
*/ |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
410 |
private static String subStringFrom(String[] components, int from) { |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
411 |
StringBuilder sb = new StringBuilder(); |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
412 |
for (int i=from; i<components.length; i++) { |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
413 |
if (sb.length() != 0) sb.append('.'); |
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
414 |
sb.append(components[i]); |
2 | 415 |
} |
20175
a65ad0a49e3c
8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents:
14342
diff
changeset
|
416 |
return sb.toString(); |
2 | 417 |
} |
418 |
} |