author | ohair |
Wed, 06 Apr 2011 22:06:11 -0700 | |
changeset 9035 | 1255eb81cc2f |
parent 7977 | f47f211cd627 |
child 12842 | f6f9cb8f6b97 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
7977
diff
changeset
|
2 |
* Copyright (c) 2000, 2011, 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; |
7183 | 49 |
import sun.security.krb5.internal.KRBError; |
2 | 50 |
|
7183 | 51 |
/** |
52 |
* KDC-REQ/KDC-REP communication. No more base class for KrbAsReq and |
|
53 |
* KrbTgsReq. This class is now communication only. |
|
54 |
*/ |
|
55 |
public final class KdcComm { |
|
2 | 56 |
|
5458 | 57 |
// The following settings can be configured in [libdefaults] |
58 |
// section of krb5.conf, which are global for all realms. Each of |
|
59 |
// them can also be defined in a realm, which overrides value here. |
|
2 | 60 |
|
61 |
/** |
|
5458 | 62 |
* max retry time for a single KDC, default Krb5.KDC_RETRY_LIMIT (3) |
63 |
*/ |
|
64 |
private static int defaultKdcRetryLimit; |
|
65 |
/** |
|
66 |
* timeout requesting a ticket from KDC, in millisec, default 30 sec |
|
2 | 67 |
*/ |
5458 | 68 |
private static int defaultKdcTimeout; |
69 |
/** |
|
70 |
* max UDP packet size, default unlimited (-1) |
|
71 |
*/ |
|
72 |
private static int defaultUdpPrefLimit; |
|
2 | 73 |
|
74 |
private static final boolean DEBUG = Krb5.DEBUG; |
|
75 |
||
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
76 |
private static final String BAD_POLICY_KEY = "krb5.kdc.bad.policy"; |
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 |
/** |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
79 |
* 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
|
80 |
* java.security file with key krb5.kdc.bad.policy. |
5458 | 81 |
* 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
|
82 |
*/ |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
83 |
private enum BpType { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
84 |
NONE, TRY_LAST, TRY_LESS |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
85 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
86 |
private static int tryLessMaxRetries = 1; |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
87 |
private static int tryLessTimeout = 5000; |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
88 |
|
5458 | 89 |
private static BpType badPolicy; |
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
90 |
|
2 | 91 |
static { |
5458 | 92 |
initStatic(); |
93 |
} |
|
94 |
||
95 |
/** |
|
96 |
* Read global settings |
|
97 |
*/ |
|
98 |
public static void initStatic() { |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
99 |
String value = AccessController.doPrivileged( |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
100 |
new PrivilegedAction<String>() { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
101 |
public String run() { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
102 |
return Security.getProperty(BAD_POLICY_KEY); |
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 |
}); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
105 |
if (value != null) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
106 |
value = value.toLowerCase(Locale.ENGLISH); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
107 |
String[] ss = value.split(":"); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
108 |
if ("tryless".equals(ss[0])) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
109 |
if (ss.length > 1) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
110 |
String[] params = ss[1].split(","); |
5458 | 111 |
try { |
112 |
int tmp0 = Integer.parseInt(params[0]); |
|
113 |
if (params.length > 1) { |
|
114 |
tryLessTimeout = Integer.parseInt(params[1]); |
|
115 |
} |
|
116 |
// Assign here in case of exception at params[1] |
|
117 |
tryLessMaxRetries = tmp0; |
|
118 |
} catch (NumberFormatException nfe) { |
|
119 |
// Ignored. Please note that tryLess is recognized and |
|
120 |
// used, parameters using default values |
|
121 |
if (DEBUG) { |
|
122 |
System.out.println("Invalid " + BAD_POLICY_KEY + |
|
123 |
" parameter for tryLess: " + |
|
124 |
value + ", use default"); |
|
125 |
} |
|
4531
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 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
128 |
badPolicy = BpType.TRY_LESS; |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
129 |
} else if ("trylast".equals(ss[0])) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
130 |
badPolicy = BpType.TRY_LAST; |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
131 |
} else { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
132 |
badPolicy = BpType.NONE; |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
133 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
134 |
} else { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
135 |
badPolicy = BpType.NONE; |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
136 |
} |
2 | 137 |
|
138 |
||
139 |
int timeout = -1; |
|
5458 | 140 |
int max_retries = -1; |
141 |
int udf_pref_limit = -1; |
|
142 |
||
2 | 143 |
try { |
144 |
Config cfg = Config.getInstance(); |
|
145 |
String temp = cfg.getDefault("kdc_timeout", "libdefaults"); |
|
146 |
timeout = parsePositiveIntString(temp); |
|
5458 | 147 |
temp = cfg.getDefault("max_retries", "libdefaults"); |
148 |
max_retries = parsePositiveIntString(temp); |
|
2 | 149 |
temp = cfg.getDefault("udp_preference_limit", "libdefaults"); |
5458 | 150 |
udf_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; |
|
162 |
defaultUdpPrefLimit = udf_pref_limit; |
|
2 | 163 |
|
5458 | 164 |
KdcAccessibility.reset(); |
2 | 165 |
} |
166 |
||
7183 | 167 |
/** |
168 |
* The instance fields |
|
169 |
*/ |
|
170 |
private String realm; |
|
2 | 171 |
|
7183 | 172 |
public KdcComm(String realm) throws KrbException { |
173 |
if (realm == null) { |
|
174 |
realm = Config.getInstance().getDefaultRealm(); |
|
175 |
if (realm == null) { |
|
176 |
throw new KrbException(Krb5.KRB_ERR_GENERIC, |
|
177 |
"Cannot find default realm"); |
|
178 |
} |
|
179 |
} |
|
180 |
this.realm = realm; |
|
181 |
} |
|
2 | 182 |
|
7183 | 183 |
public byte[] send(byte[] obuf) |
2 | 184 |
throws IOException, KrbException { |
5458 | 185 |
int udpPrefLimit = getRealmSpecificValue( |
186 |
realm, "udp_preference_limit", defaultUdpPrefLimit); |
|
187 |
||
2 | 188 |
boolean useTCP = (udpPrefLimit > 0 && |
189 |
(obuf != null && obuf.length > udpPrefLimit)); |
|
190 |
||
7183 | 191 |
return send(obuf, useTCP); |
2 | 192 |
} |
193 |
||
7183 | 194 |
private byte[] send(byte[] obuf, boolean useTCP) |
2 | 195 |
throws IOException, KrbException { |
196 |
||
197 |
if (obuf == null) |
|
198 |
return null; |
|
199 |
Exception savedException = null; |
|
200 |
Config cfg = Config.getInstance(); |
|
201 |
||
202 |
if (realm == null) { |
|
203 |
realm = cfg.getDefaultRealm(); |
|
204 |
if (realm == null) { |
|
205 |
throw new KrbException(Krb5.KRB_ERR_GENERIC, |
|
206 |
"Cannot find default realm"); |
|
207 |
} |
|
208 |
} |
|
209 |
||
210 |
String kdcList = cfg.getKDCList(realm); |
|
211 |
if (kdcList == null) { |
|
212 |
throw new KrbException("Cannot get kdc for realm " + realm); |
|
213 |
} |
|
214 |
String tempKdc = null; // may include the port number also |
|
7183 | 215 |
byte[] ibuf = null; |
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
216 |
for (String tmp: KdcAccessibility.list(kdcList)) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
217 |
tempKdc = tmp; |
2 | 218 |
try { |
7183 | 219 |
ibuf = send(obuf,tempKdc,useTCP); |
220 |
KRBError ke = null; |
|
221 |
try { |
|
222 |
ke = new KRBError(ibuf); |
|
223 |
} catch (Exception e) { |
|
224 |
// OK |
|
225 |
} |
|
226 |
if (ke != null && ke.getErrorCode() == |
|
227 |
Krb5.KRB_ERR_RESPONSE_TOO_BIG) { |
|
228 |
ibuf = send(obuf, tempKdc, true); |
|
229 |
} |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
230 |
KdcAccessibility.removeBad(tempKdc); |
2 | 231 |
break; |
232 |
} catch (Exception e) { |
|
3864 | 233 |
if (DEBUG) { |
234 |
System.out.println(">>> KrbKdcReq send: error trying " + |
|
235 |
tempKdc); |
|
236 |
e.printStackTrace(System.out); |
|
237 |
} |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
238 |
KdcAccessibility.addBad(tempKdc); |
2 | 239 |
savedException = e; |
240 |
} |
|
241 |
} |
|
242 |
if (ibuf == null && savedException != null) { |
|
243 |
if (savedException instanceof IOException) { |
|
244 |
throw (IOException) savedException; |
|
245 |
} else { |
|
246 |
throw (KrbException) savedException; |
|
247 |
} |
|
248 |
} |
|
7183 | 249 |
return ibuf; |
2 | 250 |
} |
251 |
||
252 |
// send the AS Request to the specified KDC |
|
253 |
||
7183 | 254 |
private byte[] send(byte[] obuf, String tempKdc, boolean useTCP) |
2 | 255 |
throws IOException, KrbException { |
256 |
||
257 |
if (obuf == null) |
|
7183 | 258 |
return null; |
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
259 |
|
2 | 260 |
int port = Krb5.KDC_INET_DEFAULT_PORT; |
5458 | 261 |
int retries = getRealmSpecificValue( |
262 |
realm, "max_retries", defaultKdcRetryLimit); |
|
263 |
int timeout = getRealmSpecificValue( |
|
264 |
realm, "kdc_timeout", defaultKdcTimeout); |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
265 |
if (badPolicy == BpType.TRY_LESS && |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
266 |
KdcAccessibility.isBad(tempKdc)) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
267 |
if (retries > tryLessMaxRetries) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
268 |
retries = tryLessMaxRetries; // less retries |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
269 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
270 |
if (timeout > tryLessTimeout) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
271 |
timeout = tryLessTimeout; // less time |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
272 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
273 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
274 |
|
3864 | 275 |
String kdc = null; |
276 |
String portStr = null; |
|
277 |
||
278 |
if (tempKdc.charAt(0) == '[') { // Explicit IPv6 in [] |
|
279 |
int pos = tempKdc.indexOf(']', 1); |
|
280 |
if (pos == -1) { |
|
281 |
throw new IOException("Illegal KDC: " + tempKdc); |
|
282 |
} |
|
283 |
kdc = tempKdc.substring(1, pos); |
|
284 |
if (pos != tempKdc.length() - 1) { // with port number |
|
285 |
if (tempKdc.charAt(pos+1) != ':') { |
|
286 |
throw new IOException("Illegal KDC: " + tempKdc); |
|
287 |
} |
|
288 |
portStr = tempKdc.substring(pos+2); |
|
289 |
} |
|
290 |
} else { |
|
291 |
int colon = tempKdc.indexOf(':'); |
|
292 |
if (colon == -1) { // Hostname or IPv4 host only |
|
293 |
kdc = tempKdc; |
|
294 |
} else { |
|
295 |
int nextColon = tempKdc.indexOf(':', colon+1); |
|
296 |
if (nextColon > 0) { // >=2 ":", IPv6 with no port |
|
297 |
kdc = tempKdc; |
|
298 |
} else { // 1 ":", hostname or IPv4 with port |
|
299 |
kdc = tempKdc.substring(0, colon); |
|
300 |
portStr = tempKdc.substring(colon+1); |
|
301 |
} |
|
302 |
} |
|
303 |
} |
|
304 |
if (portStr != null) { |
|
2 | 305 |
int tempPort = parsePositiveIntString(portStr); |
306 |
if (tempPort > 0) |
|
307 |
port = tempPort; |
|
308 |
} |
|
309 |
||
310 |
if (DEBUG) { |
|
311 |
System.out.println(">>> KrbKdcReq send: kdc=" + kdc |
|
312 |
+ (useTCP ? " TCP:":" UDP:") |
|
313 |
+ port + ", timeout=" |
|
314 |
+ timeout |
|
315 |
+ ", number of retries =" |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
316 |
+ retries |
2 | 317 |
+ ", #bytes=" + obuf.length); |
318 |
} |
|
319 |
||
320 |
KdcCommunication kdcCommunication = |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
321 |
new KdcCommunication(kdc, port, useTCP, timeout, retries, obuf); |
2 | 322 |
try { |
7183 | 323 |
byte[] ibuf = AccessController.doPrivileged(kdcCommunication); |
2 | 324 |
if (DEBUG) { |
325 |
System.out.println(">>> KrbKdcReq send: #bytes read=" |
|
326 |
+ (ibuf != null ? ibuf.length : 0)); |
|
327 |
} |
|
7183 | 328 |
return ibuf; |
2 | 329 |
} catch (PrivilegedActionException e) { |
330 |
Exception wrappedException = e.getException(); |
|
331 |
if (wrappedException instanceof IOException) { |
|
332 |
throw (IOException) wrappedException; |
|
333 |
} else { |
|
334 |
throw (KrbException) wrappedException; |
|
335 |
} |
|
336 |
} |
|
337 |
} |
|
338 |
||
339 |
private static class KdcCommunication |
|
340 |
implements PrivilegedExceptionAction<byte[]> { |
|
341 |
||
342 |
private String kdc; |
|
343 |
private int port; |
|
344 |
private boolean useTCP; |
|
345 |
private int timeout; |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
346 |
private int retries; |
2 | 347 |
private byte[] obuf; |
348 |
||
349 |
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
|
350 |
int timeout, int retries, byte[] obuf) { |
2 | 351 |
this.kdc = kdc; |
352 |
this.port = port; |
|
353 |
this.useTCP = useTCP; |
|
354 |
this.timeout = timeout; |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
355 |
this.retries = retries; |
2 | 356 |
this.obuf = obuf; |
357 |
} |
|
358 |
||
359 |
// The caller only casts IOException and KrbException so don't |
|
360 |
// add any new ones! |
|
361 |
||
362 |
public byte[] run() throws IOException, KrbException { |
|
363 |
||
364 |
byte[] ibuf = null; |
|
365 |
||
7175
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
366 |
for (int i=1; i <= retries; i++) { |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
367 |
String proto = useTCP?"TCP":"UDP"; |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
368 |
NetClient kdcClient = NetClient.getInstance( |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
369 |
proto, kdc, port, timeout); |
5458 | 370 |
if (DEBUG) { |
371 |
System.out.println(">>> KDCCommunication: kdc=" + kdc |
|
7175
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
372 |
+ " " + proto + ":" |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
373 |
+ port + ", timeout=" |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
374 |
+ timeout |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
375 |
+ ",Attempt =" + i |
5458 | 376 |
+ ", #bytes=" + obuf.length); |
377 |
} |
|
2 | 378 |
try { |
379 |
/* |
|
380 |
* Send the data to the kdc. |
|
381 |
*/ |
|
382 |
kdcClient.send(obuf); |
|
383 |
/* |
|
384 |
* And get a response. |
|
385 |
*/ |
|
386 |
ibuf = kdcClient.receive(); |
|
7175
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
387 |
break; |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
388 |
} catch (SocketTimeoutException se) { |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
389 |
if (DEBUG) { |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
390 |
System.out.println ("SocketTimeOutException with " + |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
391 |
"attempt: " + i); |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
392 |
} |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
393 |
if (i == retries) { |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
394 |
ibuf = null; |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
395 |
throw se; |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
396 |
} |
2 | 397 |
} finally { |
398 |
kdcClient.close(); |
|
399 |
} |
|
400 |
} |
|
401 |
return ibuf; |
|
402 |
} |
|
403 |
} |
|
404 |
||
405 |
/** |
|
5458 | 406 |
* Returns krb5.conf setting of {@code key} for a specfic realm, |
407 |
* which can be: |
|
408 |
* 1. defined in the sub-stanza for the given realm inside [realms], or |
|
409 |
* 2. defined in [libdefaults], or |
|
410 |
* 3. defValue |
|
411 |
* @param realm the given realm in which the setting is requested. Returns |
|
412 |
* the global setting if null |
|
413 |
* @param key the key for the setting |
|
414 |
* @param defValue default value |
|
415 |
* @return a value for the key |
|
2 | 416 |
*/ |
5458 | 417 |
private int getRealmSpecificValue(String realm, String key, int defValue) { |
418 |
int v = defValue; |
|
2 | 419 |
|
5458 | 420 |
if (realm == null) return v; |
2 | 421 |
|
5458 | 422 |
int temp = -1; |
2 | 423 |
try { |
5458 | 424 |
String value = |
425 |
Config.getInstance().getDefault(key, realm); |
|
426 |
temp = parsePositiveIntString(value); |
|
2 | 427 |
} catch (Exception exc) { |
5458 | 428 |
// Ignored, defValue will be picked up |
2 | 429 |
} |
430 |
||
5458 | 431 |
if (temp > 0) v = temp; |
2 | 432 |
|
5458 | 433 |
return v; |
2 | 434 |
} |
435 |
||
5458 | 436 |
private static int parsePositiveIntString(String intString) { |
2 | 437 |
if (intString == null) |
438 |
return -1; |
|
439 |
||
440 |
int ret = -1; |
|
441 |
||
442 |
try { |
|
443 |
ret = Integer.parseInt(intString); |
|
444 |
} catch (Exception exc) { |
|
445 |
return -1; |
|
446 |
} |
|
447 |
||
448 |
if (ret >= 0) |
|
449 |
return ret; |
|
450 |
||
451 |
return -1; |
|
452 |
} |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
453 |
|
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
454 |
/** |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
455 |
* 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
|
456 |
* 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
|
457 |
* 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
|
458 |
* |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
459 |
* 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
|
460 |
* 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
|
461 |
* 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
|
462 |
*/ |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
463 |
static class KdcAccessibility { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
464 |
// Known bad KDCs |
7977
f47f211cd627
7008713: diamond conversion of kerberos5 and security tools
smarks
parents:
7183
diff
changeset
|
465 |
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
|
466 |
|
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
467 |
private static synchronized void addBad(String kdc) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
468 |
if (DEBUG) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
469 |
System.out.println(">>> KdcAccessibility: add " + kdc); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
470 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
471 |
bads.add(kdc); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
472 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
473 |
|
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
474 |
private static synchronized void removeBad(String kdc) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
475 |
if (DEBUG) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
476 |
System.out.println(">>> KdcAccessibility: remove " + kdc); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
477 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
478 |
bads.remove(kdc); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
479 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
480 |
|
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
481 |
private static synchronized boolean isBad(String kdc) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
482 |
return bads.contains(kdc); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
483 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
484 |
|
5458 | 485 |
private static synchronized void reset() { |
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
486 |
if (DEBUG) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
487 |
System.out.println(">>> KdcAccessibility: reset"); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
488 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
489 |
bads.clear(); |
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 |
// Returns a preferred KDC list by putting the bad ones at the end |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
493 |
private static synchronized String[] list(String kdcList) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
494 |
StringTokenizer st = new StringTokenizer(kdcList); |
7977
f47f211cd627
7008713: diamond conversion of kerberos5 and security tools
smarks
parents:
7183
diff
changeset
|
495 |
List<String> list = new ArrayList<>(); |
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
496 |
if (badPolicy == BpType.TRY_LAST) { |
7977
f47f211cd627
7008713: diamond conversion of kerberos5 and security tools
smarks
parents:
7183
diff
changeset
|
497 |
List<String> badkdcs = new ArrayList<>(); |
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
498 |
while (st.hasMoreTokens()) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
499 |
String t = st.nextToken(); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
500 |
if (bads.contains(t)) badkdcs.add(t); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
501 |
else list.add(t); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
502 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
503 |
// Bad KDCs are put at last |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
504 |
list.addAll(badkdcs); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
505 |
} else { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
506 |
// 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
|
507 |
// This include TRY_LESS and NONE |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
508 |
while (st.hasMoreTokens()) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
509 |
list.add(st.nextToken()); |
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 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
512 |
return list.toArray(new String[list.size()]); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
513 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
514 |
} |
2 | 515 |
} |
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
3864
diff
changeset
|
516 |