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