author | mbalao |
Wed, 05 Jun 2019 01:42:11 -0300 | |
changeset 55258 | d65d3c37232c |
parent 51398 | 3c389a284345 |
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 |
||
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
34 |
import java.security.PrivilegedAction; |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
35 |
import java.security.Security; |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
36 |
import java.util.Locale; |
2 | 37 |
import sun.security.krb5.internal.Krb5; |
7175
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
38 |
import sun.security.krb5.internal.NetClient; |
2 | 39 |
import java.io.IOException; |
40 |
import java.net.SocketTimeoutException; |
|
41 |
import java.util.StringTokenizer; |
|
42 |
import java.security.AccessController; |
|
43 |
import java.security.PrivilegedExceptionAction; |
|
44 |
import java.security.PrivilegedActionException; |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
45 |
import java.util.ArrayList; |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
46 |
import java.util.List; |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
47 |
import java.util.Set; |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
48 |
import java.util.HashSet; |
18549 | 49 |
import java.util.Iterator; |
7183 | 50 |
import sun.security.krb5.internal.KRBError; |
2 | 51 |
|
7183 | 52 |
/** |
53 |
* KDC-REQ/KDC-REP communication. No more base class for KrbAsReq and |
|
54 |
* KrbTgsReq. This class is now communication only. |
|
55 |
*/ |
|
56 |
public final class KdcComm { |
|
2 | 57 |
|
5458 | 58 |
// The following settings can be configured in [libdefaults] |
59 |
// section of krb5.conf, which are global for all realms. Each of |
|
60 |
// them can also be defined in a realm, which overrides value here. |
|
2 | 61 |
|
62 |
/** |
|
5458 | 63 |
* max retry time for a single KDC, default Krb5.KDC_RETRY_LIMIT (3) |
64 |
*/ |
|
65 |
private static int defaultKdcRetryLimit; |
|
66 |
/** |
|
67 |
* timeout requesting a ticket from KDC, in millisec, default 30 sec |
|
2 | 68 |
*/ |
5458 | 69 |
private static int defaultKdcTimeout; |
70 |
/** |
|
71 |
* max UDP packet size, default unlimited (-1) |
|
72 |
*/ |
|
73 |
private static int defaultUdpPrefLimit; |
|
2 | 74 |
|
75 |
private static final boolean DEBUG = Krb5.DEBUG; |
|
76 |
||
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
77 |
/** |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
78 |
* What to do when a KDC is unavailable, specified in the |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
79 |
* java.security file with key krb5.kdc.bad.policy. |
5458 | 80 |
* Possible values can be TRY_LAST or TRY_LESS. Reloaded when refreshed. |
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
81 |
*/ |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
82 |
private enum BpType { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
83 |
NONE, TRY_LAST, TRY_LESS |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
84 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
85 |
private static int tryLessMaxRetries = 1; |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
86 |
private static int tryLessTimeout = 5000; |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
87 |
|
5458 | 88 |
private static BpType badPolicy; |
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
89 |
|
2 | 90 |
static { |
5458 | 91 |
initStatic(); |
92 |
} |
|
93 |
||
94 |
/** |
|
95 |
* Read global settings |
|
96 |
*/ |
|
97 |
public static void initStatic() { |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
98 |
String value = AccessController.doPrivileged( |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
99 |
new PrivilegedAction<String>() { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
100 |
public String run() { |
51398
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
101 |
return Security.getProperty("krb5.kdc.bad.policy"); |
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
102 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
103 |
}); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
104 |
if (value != null) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
105 |
value = value.toLowerCase(Locale.ENGLISH); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
106 |
String[] ss = value.split(":"); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
107 |
if ("tryless".equals(ss[0])) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
108 |
if (ss.length > 1) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
109 |
String[] params = ss[1].split(","); |
5458 | 110 |
try { |
111 |
int tmp0 = Integer.parseInt(params[0]); |
|
112 |
if (params.length > 1) { |
|
113 |
tryLessTimeout = Integer.parseInt(params[1]); |
|
114 |
} |
|
115 |
// Assign here in case of exception at params[1] |
|
116 |
tryLessMaxRetries = tmp0; |
|
117 |
} catch (NumberFormatException nfe) { |
|
118 |
// Ignored. Please note that tryLess is recognized and |
|
119 |
// used, parameters using default values |
|
120 |
if (DEBUG) { |
|
51398
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
121 |
System.out.println("Invalid krb5.kdc.bad.policy" + |
5458 | 122 |
" parameter for tryLess: " + |
123 |
value + ", use default"); |
|
124 |
} |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
125 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
126 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
127 |
badPolicy = BpType.TRY_LESS; |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
128 |
} else if ("trylast".equals(ss[0])) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
129 |
badPolicy = BpType.TRY_LAST; |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
130 |
} else { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
131 |
badPolicy = BpType.NONE; |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
132 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
133 |
} else { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
134 |
badPolicy = BpType.NONE; |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
135 |
} |
2 | 136 |
|
137 |
||
138 |
int timeout = -1; |
|
5458 | 139 |
int max_retries = -1; |
16504
1e8ff2df7152
8009875: Provide a default udp_preference_limit for krb5.conf
weijun
parents:
14342
diff
changeset
|
140 |
int udp_pref_limit = -1; |
5458 | 141 |
|
2 | 142 |
try { |
143 |
Config cfg = Config.getInstance(); |
|
14327 | 144 |
String temp = cfg.get("libdefaults", "kdc_timeout"); |
24631
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
145 |
timeout = parseTimeString(temp); |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
146 |
|
14327 | 147 |
temp = cfg.get("libdefaults", "max_retries"); |
5458 | 148 |
max_retries = parsePositiveIntString(temp); |
14327 | 149 |
temp = cfg.get("libdefaults", "udp_preference_limit"); |
16504
1e8ff2df7152
8009875: Provide a default udp_preference_limit for krb5.conf
weijun
parents:
14342
diff
changeset
|
150 |
udp_pref_limit = parsePositiveIntString(temp); |
2 | 151 |
} catch (Exception exc) { |
5458 | 152 |
// ignore any exceptions; use default values |
2 | 153 |
if (DEBUG) { |
5458 | 154 |
System.out.println ("Exception in getting KDC communication " + |
155 |
"settings, using default value " + |
|
2 | 156 |
exc.getMessage()); |
157 |
} |
|
158 |
} |
|
5458 | 159 |
defaultKdcTimeout = timeout > 0 ? timeout : 30*1000; // 30 seconds |
160 |
defaultKdcRetryLimit = |
|
161 |
max_retries > 0 ? max_retries : Krb5.KDC_RETRY_LIMIT; |
|
16504
1e8ff2df7152
8009875: Provide a default udp_preference_limit for krb5.conf
weijun
parents:
14342
diff
changeset
|
162 |
|
1e8ff2df7152
8009875: Provide a default udp_preference_limit for krb5.conf
weijun
parents:
14342
diff
changeset
|
163 |
if (udp_pref_limit < 0) { |
1e8ff2df7152
8009875: Provide a default udp_preference_limit for krb5.conf
weijun
parents:
14342
diff
changeset
|
164 |
defaultUdpPrefLimit = Krb5.KDC_DEFAULT_UDP_PREF_LIMIT; |
1e8ff2df7152
8009875: Provide a default udp_preference_limit for krb5.conf
weijun
parents:
14342
diff
changeset
|
165 |
} else if (udp_pref_limit > Krb5.KDC_HARD_UDP_LIMIT) { |
1e8ff2df7152
8009875: Provide a default udp_preference_limit for krb5.conf
weijun
parents:
14342
diff
changeset
|
166 |
defaultUdpPrefLimit = Krb5.KDC_HARD_UDP_LIMIT; |
1e8ff2df7152
8009875: Provide a default udp_preference_limit for krb5.conf
weijun
parents:
14342
diff
changeset
|
167 |
} else { |
1e8ff2df7152
8009875: Provide a default udp_preference_limit for krb5.conf
weijun
parents:
14342
diff
changeset
|
168 |
defaultUdpPrefLimit = udp_pref_limit; |
1e8ff2df7152
8009875: Provide a default udp_preference_limit for krb5.conf
weijun
parents:
14342
diff
changeset
|
169 |
} |
2 | 170 |
|
5458 | 171 |
KdcAccessibility.reset(); |
2 | 172 |
} |
173 |
||
7183 | 174 |
/** |
175 |
* The instance fields |
|
176 |
*/ |
|
177 |
private String realm; |
|
2 | 178 |
|
7183 | 179 |
public KdcComm(String realm) throws KrbException { |
180 |
if (realm == null) { |
|
181 |
realm = Config.getInstance().getDefaultRealm(); |
|
182 |
if (realm == null) { |
|
183 |
throw new KrbException(Krb5.KRB_ERR_GENERIC, |
|
184 |
"Cannot find default realm"); |
|
185 |
} |
|
186 |
} |
|
187 |
this.realm = realm; |
|
188 |
} |
|
2 | 189 |
|
7183 | 190 |
public byte[] send(byte[] obuf) |
2 | 191 |
throws IOException, KrbException { |
5458 | 192 |
int udpPrefLimit = getRealmSpecificValue( |
193 |
realm, "udp_preference_limit", defaultUdpPrefLimit); |
|
194 |
||
2 | 195 |
boolean useTCP = (udpPrefLimit > 0 && |
196 |
(obuf != null && obuf.length > udpPrefLimit)); |
|
197 |
||
7183 | 198 |
return send(obuf, useTCP); |
2 | 199 |
} |
200 |
||
7183 | 201 |
private byte[] send(byte[] obuf, boolean useTCP) |
2 | 202 |
throws IOException, KrbException { |
203 |
||
204 |
if (obuf == null) |
|
205 |
return null; |
|
206 |
Config cfg = Config.getInstance(); |
|
207 |
||
208 |
if (realm == null) { |
|
209 |
realm = cfg.getDefaultRealm(); |
|
210 |
if (realm == null) { |
|
211 |
throw new KrbException(Krb5.KRB_ERR_GENERIC, |
|
212 |
"Cannot find default realm"); |
|
213 |
} |
|
214 |
} |
|
215 |
||
216 |
String kdcList = cfg.getKDCList(realm); |
|
217 |
if (kdcList == null) { |
|
218 |
throw new KrbException("Cannot get kdc for realm " + realm); |
|
219 |
} |
|
18549 | 220 |
// tempKdc may include the port number also |
221 |
Iterator<String> tempKdc = KdcAccessibility.list(kdcList).iterator(); |
|
222 |
if (!tempKdc.hasNext()) { |
|
223 |
throw new KrbException("Cannot get kdc for realm " + realm); |
|
224 |
} |
|
7183 | 225 |
byte[] ibuf = null; |
18549 | 226 |
try { |
20818 | 227 |
ibuf = sendIfPossible(obuf, tempKdc.next(), useTCP); |
18549 | 228 |
} catch(Exception first) { |
20827 | 229 |
boolean ok = false; |
18549 | 230 |
while(tempKdc.hasNext()) { |
7183 | 231 |
try { |
20818 | 232 |
ibuf = sendIfPossible(obuf, tempKdc.next(), useTCP); |
20827 | 233 |
ok = true; |
234 |
break; |
|
18549 | 235 |
} catch(Exception ignore) {} |
236 |
} |
|
20827 | 237 |
if (!ok) throw first; |
18549 | 238 |
} |
20804 | 239 |
if (ibuf == null) { |
20818 | 240 |
throw new IOException("Cannot get a KDC reply"); |
2 | 241 |
} |
7183 | 242 |
return ibuf; |
18549 | 243 |
} |
244 |
||
245 |
// send the AS Request to the specified KDC |
|
246 |
// failover to using TCP if useTCP is not set and response is too big |
|
247 |
private byte[] sendIfPossible(byte[] obuf, String tempKdc, boolean useTCP) |
|
248 |
throws IOException, KrbException { |
|
249 |
||
250 |
try { |
|
251 |
byte[] ibuf = send(obuf, tempKdc, useTCP); |
|
252 |
KRBError ke = null; |
|
253 |
try { |
|
254 |
ke = new KRBError(ibuf); |
|
2 | 255 |
} catch (Exception e) { |
18549 | 256 |
// OK |
257 |
} |
|
258 |
if (ke != null && ke.getErrorCode() == |
|
259 |
Krb5.KRB_ERR_RESPONSE_TOO_BIG) { |
|
260 |
ibuf = send(obuf, tempKdc, true); |
|
2 | 261 |
} |
18549 | 262 |
KdcAccessibility.removeBad(tempKdc); |
263 |
return ibuf; |
|
264 |
} catch(Exception e) { |
|
265 |
if (DEBUG) { |
|
266 |
System.out.println(">>> KrbKdcReq send: error trying " + |
|
267 |
tempKdc); |
|
268 |
e.printStackTrace(System.out); |
|
269 |
} |
|
270 |
KdcAccessibility.addBad(tempKdc); |
|
271 |
throw e; |
|
2 | 272 |
} |
273 |
} |
|
274 |
||
275 |
// send the AS Request to the specified KDC |
|
276 |
||
7183 | 277 |
private byte[] send(byte[] obuf, String tempKdc, boolean useTCP) |
2 | 278 |
throws IOException, KrbException { |
279 |
||
280 |
if (obuf == null) |
|
7183 | 281 |
return null; |
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
282 |
|
2 | 283 |
int port = Krb5.KDC_INET_DEFAULT_PORT; |
5458 | 284 |
int retries = getRealmSpecificValue( |
285 |
realm, "max_retries", defaultKdcRetryLimit); |
|
286 |
int timeout = getRealmSpecificValue( |
|
287 |
realm, "kdc_timeout", defaultKdcTimeout); |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
288 |
if (badPolicy == BpType.TRY_LESS && |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
289 |
KdcAccessibility.isBad(tempKdc)) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
290 |
if (retries > tryLessMaxRetries) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
291 |
retries = tryLessMaxRetries; // less retries |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
292 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
293 |
if (timeout > tryLessTimeout) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
294 |
timeout = tryLessTimeout; // less time |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
295 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
296 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
297 |
|
3864 | 298 |
String kdc = null; |
299 |
String portStr = null; |
|
300 |
||
301 |
if (tempKdc.charAt(0) == '[') { // Explicit IPv6 in [] |
|
302 |
int pos = tempKdc.indexOf(']', 1); |
|
303 |
if (pos == -1) { |
|
304 |
throw new IOException("Illegal KDC: " + tempKdc); |
|
305 |
} |
|
306 |
kdc = tempKdc.substring(1, pos); |
|
307 |
if (pos != tempKdc.length() - 1) { // with port number |
|
308 |
if (tempKdc.charAt(pos+1) != ':') { |
|
309 |
throw new IOException("Illegal KDC: " + tempKdc); |
|
310 |
} |
|
311 |
portStr = tempKdc.substring(pos+2); |
|
312 |
} |
|
313 |
} else { |
|
314 |
int colon = tempKdc.indexOf(':'); |
|
315 |
if (colon == -1) { // Hostname or IPv4 host only |
|
316 |
kdc = tempKdc; |
|
317 |
} else { |
|
318 |
int nextColon = tempKdc.indexOf(':', colon+1); |
|
319 |
if (nextColon > 0) { // >=2 ":", IPv6 with no port |
|
320 |
kdc = tempKdc; |
|
321 |
} else { // 1 ":", hostname or IPv4 with port |
|
322 |
kdc = tempKdc.substring(0, colon); |
|
323 |
portStr = tempKdc.substring(colon+1); |
|
324 |
} |
|
325 |
} |
|
326 |
} |
|
327 |
if (portStr != null) { |
|
2 | 328 |
int tempPort = parsePositiveIntString(portStr); |
329 |
if (tempPort > 0) |
|
330 |
port = tempPort; |
|
331 |
} |
|
332 |
||
333 |
if (DEBUG) { |
|
334 |
System.out.println(">>> KrbKdcReq send: kdc=" + kdc |
|
335 |
+ (useTCP ? " TCP:":" UDP:") |
|
336 |
+ port + ", timeout=" |
|
337 |
+ timeout |
|
338 |
+ ", number of retries =" |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
339 |
+ retries |
2 | 340 |
+ ", #bytes=" + obuf.length); |
341 |
} |
|
342 |
||
343 |
KdcCommunication kdcCommunication = |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
344 |
new KdcCommunication(kdc, port, useTCP, timeout, retries, obuf); |
2 | 345 |
try { |
7183 | 346 |
byte[] ibuf = AccessController.doPrivileged(kdcCommunication); |
2 | 347 |
if (DEBUG) { |
348 |
System.out.println(">>> KrbKdcReq send: #bytes read=" |
|
349 |
+ (ibuf != null ? ibuf.length : 0)); |
|
350 |
} |
|
7183 | 351 |
return ibuf; |
2 | 352 |
} catch (PrivilegedActionException e) { |
353 |
Exception wrappedException = e.getException(); |
|
354 |
if (wrappedException instanceof IOException) { |
|
355 |
throw (IOException) wrappedException; |
|
356 |
} else { |
|
357 |
throw (KrbException) wrappedException; |
|
358 |
} |
|
359 |
} |
|
360 |
} |
|
361 |
||
362 |
private static class KdcCommunication |
|
363 |
implements PrivilegedExceptionAction<byte[]> { |
|
364 |
||
365 |
private String kdc; |
|
366 |
private int port; |
|
367 |
private boolean useTCP; |
|
368 |
private int timeout; |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
369 |
private int retries; |
2 | 370 |
private byte[] obuf; |
371 |
||
372 |
public KdcCommunication(String kdc, int port, boolean useTCP, |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
373 |
int timeout, int retries, byte[] obuf) { |
2 | 374 |
this.kdc = kdc; |
375 |
this.port = port; |
|
376 |
this.useTCP = useTCP; |
|
377 |
this.timeout = timeout; |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
378 |
this.retries = retries; |
2 | 379 |
this.obuf = obuf; |
380 |
} |
|
381 |
||
382 |
// The caller only casts IOException and KrbException so don't |
|
383 |
// add any new ones! |
|
384 |
||
385 |
public byte[] run() throws IOException, KrbException { |
|
386 |
||
387 |
byte[] ibuf = null; |
|
388 |
||
7175
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
389 |
for (int i=1; i <= retries; i++) { |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
390 |
String proto = useTCP?"TCP":"UDP"; |
40540
49fd4217ac48
8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
25859
diff
changeset
|
391 |
if (DEBUG) { |
49fd4217ac48
8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
25859
diff
changeset
|
392 |
System.out.println(">>> KDCCommunication: kdc=" + kdc |
12842 | 393 |
+ " " + proto + ":" |
394 |
+ port + ", timeout=" |
|
395 |
+ timeout |
|
396 |
+ ",Attempt =" + i |
|
397 |
+ ", #bytes=" + obuf.length); |
|
40540
49fd4217ac48
8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
25859
diff
changeset
|
398 |
} |
49fd4217ac48
8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
25859
diff
changeset
|
399 |
try (NetClient kdcClient = NetClient.getInstance( |
49fd4217ac48
8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
25859
diff
changeset
|
400 |
proto, kdc, port, timeout)) { |
49fd4217ac48
8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
25859
diff
changeset
|
401 |
kdcClient.send(obuf); |
49fd4217ac48
8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
25859
diff
changeset
|
402 |
ibuf = kdcClient.receive(); |
49fd4217ac48
8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
25859
diff
changeset
|
403 |
break; |
49fd4217ac48
8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
25859
diff
changeset
|
404 |
} catch (SocketTimeoutException se) { |
49fd4217ac48
8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
25859
diff
changeset
|
405 |
if (DEBUG) { |
49fd4217ac48
8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
25859
diff
changeset
|
406 |
System.out.println ("SocketTimeOutException with " + |
49fd4217ac48
8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
25859
diff
changeset
|
407 |
"attempt: " + i); |
7175
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
408 |
} |
40540
49fd4217ac48
8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
25859
diff
changeset
|
409 |
if (i == retries) { |
49fd4217ac48
8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
25859
diff
changeset
|
410 |
ibuf = null; |
49fd4217ac48
8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
25859
diff
changeset
|
411 |
throw se; |
7175
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
412 |
} |
2 | 413 |
} |
414 |
} |
|
415 |
return ibuf; |
|
416 |
} |
|
417 |
} |
|
418 |
||
419 |
/** |
|
24631
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
420 |
* Parses a time value string. If it ends with "s", parses as seconds. |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
421 |
* Otherwise, parses as milliseconds. |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
422 |
* @param s the time string |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
423 |
* @return the integer value in milliseconds, or -1 if input is null or |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
424 |
* has an invalid format |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
425 |
*/ |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
426 |
private static int parseTimeString(String s) { |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
427 |
if (s == null) { |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
428 |
return -1; |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
429 |
} |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
430 |
if (s.endsWith("s")) { |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
431 |
int seconds = parsePositiveIntString(s.substring(0, s.length()-1)); |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
432 |
return (seconds < 0) ? -1 : (seconds*1000); |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
433 |
} else { |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
434 |
return parsePositiveIntString(s); |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
435 |
} |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
436 |
} |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
437 |
|
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
438 |
/** |
21278 | 439 |
* Returns krb5.conf setting of {@code key} for a specific realm, |
5458 | 440 |
* which can be: |
441 |
* 1. defined in the sub-stanza for the given realm inside [realms], or |
|
442 |
* 2. defined in [libdefaults], or |
|
443 |
* 3. defValue |
|
444 |
* @param realm the given realm in which the setting is requested. Returns |
|
445 |
* the global setting if null |
|
446 |
* @param key the key for the setting |
|
447 |
* @param defValue default value |
|
448 |
* @return a value for the key |
|
2 | 449 |
*/ |
5458 | 450 |
private int getRealmSpecificValue(String realm, String key, int defValue) { |
451 |
int v = defValue; |
|
2 | 452 |
|
5458 | 453 |
if (realm == null) return v; |
2 | 454 |
|
5458 | 455 |
int temp = -1; |
2 | 456 |
try { |
5458 | 457 |
String value = |
14327 | 458 |
Config.getInstance().get("realms", realm, key); |
24631
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
459 |
if (key.equals("kdc_timeout")) { |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
460 |
temp = parseTimeString(value); |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
461 |
} else { |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
462 |
temp = parsePositiveIntString(value); |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
463 |
} |
2 | 464 |
} catch (Exception exc) { |
5458 | 465 |
// Ignored, defValue will be picked up |
2 | 466 |
} |
467 |
||
5458 | 468 |
if (temp > 0) v = temp; |
2 | 469 |
|
5458 | 470 |
return v; |
2 | 471 |
} |
472 |
||
5458 | 473 |
private static int parsePositiveIntString(String intString) { |
2 | 474 |
if (intString == null) |
475 |
return -1; |
|
476 |
||
477 |
int ret = -1; |
|
478 |
||
479 |
try { |
|
480 |
ret = Integer.parseInt(intString); |
|
481 |
} catch (Exception exc) { |
|
482 |
return -1; |
|
483 |
} |
|
484 |
||
485 |
if (ret >= 0) |
|
486 |
return ret; |
|
487 |
||
488 |
return -1; |
|
489 |
} |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
490 |
|
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
491 |
/** |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
492 |
* Maintains a KDC accessible list. Unavailable KDCs are put into a |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
493 |
* blacklist, when a KDC in the blacklist is available, it's removed |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
494 |
* from there. No insertion order in the blacklist. |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
495 |
* |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
496 |
* There are two methods to deal with KDCs in the blacklist. 1. Only try |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
497 |
* them when there's no KDC not on the blacklist. 2. Still try them, but |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
498 |
* with lesser number of retries and smaller timeout value. |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
499 |
*/ |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
500 |
static class KdcAccessibility { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
501 |
// Known bad KDCs |
7977
f47f211cd627
7008713: diamond conversion of kerberos5 and security tools
smarks
parents:
7183
diff
changeset
|
502 |
private static Set<String> bads = new HashSet<>(); |
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
503 |
|
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
504 |
private static synchronized void addBad(String kdc) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
505 |
if (DEBUG) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
506 |
System.out.println(">>> KdcAccessibility: add " + kdc); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
507 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
508 |
bads.add(kdc); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
509 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
510 |
|
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
511 |
private static synchronized void removeBad(String kdc) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
512 |
if (DEBUG) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
513 |
System.out.println(">>> KdcAccessibility: remove " + kdc); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
514 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
515 |
bads.remove(kdc); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
516 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
517 |
|
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
518 |
private static synchronized boolean isBad(String kdc) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
519 |
return bads.contains(kdc); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
520 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
521 |
|
5458 | 522 |
private static synchronized void reset() { |
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
523 |
if (DEBUG) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
524 |
System.out.println(">>> KdcAccessibility: reset"); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
525 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
526 |
bads.clear(); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
527 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
528 |
|
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
529 |
// Returns a preferred KDC list by putting the bad ones at the end |
18549 | 530 |
private static synchronized List<String> list(String kdcList) { |
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
531 |
StringTokenizer st = new StringTokenizer(kdcList); |
7977
f47f211cd627
7008713: diamond conversion of kerberos5 and security tools
smarks
parents:
7183
diff
changeset
|
532 |
List<String> list = new ArrayList<>(); |
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
533 |
if (badPolicy == BpType.TRY_LAST) { |
7977
f47f211cd627
7008713: diamond conversion of kerberos5 and security tools
smarks
parents:
7183
diff
changeset
|
534 |
List<String> badkdcs = new ArrayList<>(); |
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
535 |
while (st.hasMoreTokens()) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
536 |
String t = st.nextToken(); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
537 |
if (bads.contains(t)) badkdcs.add(t); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
538 |
else list.add(t); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
539 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
540 |
// Bad KDCs are put at last |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
541 |
list.addAll(badkdcs); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
542 |
} else { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
543 |
// All KDCs are returned in their original order, |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
544 |
// This include TRY_LESS and NONE |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
545 |
while (st.hasMoreTokens()) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
546 |
list.add(st.nextToken()); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
547 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
548 |
} |
18549 | 549 |
return list; |
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
550 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
551 |
} |
2 | 552 |
} |
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
553 |