Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/krb5/auto/KdcPolicy.java
41152 views
1
/*
2
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.io.*;
25
import java.net.DatagramSocket;
26
import java.net.ServerSocket;
27
import java.nio.file.Files;
28
import java.nio.file.Paths;
29
import java.security.Security;
30
import java.util.ArrayList;
31
import java.util.List;
32
import java.util.Random;
33
import java.util.regex.Matcher;
34
import java.util.regex.Pattern;
35
import javax.security.auth.login.LoginException;
36
import sun.security.krb5.Asn1Exception;
37
import sun.security.krb5.Config;
38
39
/*
40
* @test
41
* @bug 8164656 8181461 8194486
42
* @summary krb5.kdc.bad.policy test
43
* @library /test/lib
44
* @run main jdk.test.lib.FileInstaller TestHosts TestHosts
45
* @run main/othervm -Djdk.net.hosts.file=TestHosts KdcPolicy udp
46
* @run main/othervm -Djdk.net.hosts.file=TestHosts KdcPolicy tcp
47
*/
48
public class KdcPolicy {
49
50
// Is this test on UDP?
51
static boolean udp;
52
53
public static void main(String[] args) throws Exception {
54
55
udp = args[0].equals("udp");
56
57
try {
58
main0();
59
} catch (LoginException le) {
60
Throwable cause = le.getCause();
61
if (cause instanceof Asn1Exception) {
62
System.out.println("Another process sends a packet to " +
63
"this server. Ignored.");
64
return;
65
}
66
throw le;
67
}
68
}
69
70
static DebugMatcher cm = new DebugMatcher();
71
72
static void main0() throws Exception {
73
74
System.setProperty("sun.security.krb5.debug", "true");
75
76
// One real KDC. Must be created before fake KDCs
77
// to read the TestHosts file.
78
OneKDC kdc = new OneKDC(null);
79
80
// Two fake KDCs, d1 and d2 only listen but do not respond.
81
82
if (udp) {
83
try (DatagramSocket d1 = new DatagramSocket();
84
DatagramSocket d2 = new DatagramSocket()) {
85
run(d1.getLocalPort(), d2.getLocalPort(), kdc.getPort());
86
}
87
} else {
88
try (ServerSocket d1 = new ServerSocket(0);
89
ServerSocket d2 = new ServerSocket(0)) {
90
run(d1.getLocalPort(), d2.getLocalPort(), kdc.getPort());
91
}
92
}
93
}
94
95
static void run(int p1, int p2, int p3) throws Exception {
96
97
// cm.kdc() will return a and b for fake KDCs, and c for real KDC.
98
cm.addPort(-1).addPort(p1).addPort(p2).addPort(p3);
99
100
System.setProperty("java.security.krb5.conf", "alternative-krb5.conf");
101
102
// Check default timeout is 30s. Use real KDC only, otherwise too
103
// slow to wait for timeout. Each request (without preauth and with
104
// preauth) might be retried 3 times, and could fail if one fails for
105
// all 3 times.
106
writeConf(-1, -1, p3);
107
test("(c30000){2,6}|(c30000){3,6}-");
108
109
// 1. Default policy is tryLast
110
//Security.setProperty("krb5.kdc.bad.policy", "tryLast");
111
112
// Need a real KDC, otherwise there is no last good.
113
// This test waste 3 seconds waiting for d1 to timeout.
114
// It is possible the real KDC cannot fulfil the request
115
// in 3s, so it might fail (either 1st time or 2nd time).
116
writeConf(1, 3000, p1, p3);
117
test("a3000c3000c3000|a3000c3000-|a3000c3000c3000a3000-");
118
119
// If a test case won't use a real KDC, it can be sped up.
120
writeConf(3, 5, p1, p2);
121
test("a5a5a5b5b5b5-"); // default max_retries == 3
122
test("a5a5a5b5b5b5-"); // all bad means no bad
123
124
// 2. No policy.
125
Security.setProperty("krb5.kdc.bad.policy", "");
126
Config.refresh();
127
128
// This case needs a real KDC, otherwise, all bad means no
129
// bad and we cannot tell the difference. This case waste 3
130
// seconds on d1 to timeout twice. It is possible the real KDC
131
// cannot fulfil the request within 3s, so it might fail
132
// (either 1st time or 2nd time).
133
writeConf(1, 3000, p1, p3);
134
test("a3000c3000a3000c3000|a3000c3000-|a3000c3000a3000c3000-");
135
136
// 3. tryLess with no argument means tryLess:1,5000
137
Security.setProperty("krb5.kdc.bad.policy", "tryLess");
138
139
// This case will waste 11s. We are checking that the default
140
// value of 5000 in tryLess is only used if it's less than timeout
141
// in krb5.conf
142
writeConf(1, 6000, p1);
143
test("a6000-"); // timeout in krb5.conf is 6s
144
test("a5000-"); // tryLess to 5s. This line can be made faster if
145
// d1 is a read KDC, but we have no existing method
146
// to start KDC on an existing ServerSocket (port).
147
148
writeConf(-1, 4, p1, p2);
149
test("a4a4a4b4b4b4-"); // default max_retries == 3
150
test("a4b4-"); // tryLess to 1. And since 4 < 5000, use 4.
151
Config.refresh();
152
test("a4a4a4b4b4b4-");
153
154
writeConf(5, 4, p1, p2);
155
test("a4a4a4a4a4b4b4b4b4b4-"); // user-provided max_retries == 5
156
test("a4b4-");
157
Config.refresh();
158
test("a4a4a4a4a4b4b4b4b4b4-");
159
160
// 3. tryLess with arguments
161
Security.setProperty("krb5.kdc.bad.policy",
162
"tryLess:2,5");
163
164
writeConf(-1, 6, p1, p2);
165
test("a6a6a6b6b6b6-"); // default max_retries == 3
166
test("a5a5b5b5-"); // tryLess to 2
167
Config.refresh();
168
test("a6a6a6b6b6b6-");
169
170
writeConf(5, 4, p1, p2);
171
test("a4a4a4a4a4b4b4b4b4b4-"); // user-provided max_retries == 5
172
test("a4a4b4b4-"); // tryLess to 2
173
Config.refresh();
174
test("a4a4a4a4a4b4b4b4b4b4-");
175
}
176
177
/**
178
* Writes a krb5.conf file.
179
* @param max max_retries, -1 if not set
180
* @param to kdc_timeout, -1 if not set
181
* @param ports where KDCs listen on
182
*/
183
static void writeConf(int max, int to, int... ports) throws Exception {
184
185
// content of krb5.conf
186
String conf = "";
187
188
// Extra settings in [libdefaults]
189
String inDefaults = "";
190
191
// Extra settings in [realms]
192
String inRealm = "";
193
194
// We will randomly put extra settings only in [libdefaults],
195
// or in [realms] but with different values in [libdefaults],
196
// to prove that settings in [realms] override those in [libdefaults].
197
Random r = new Random();
198
199
if (max > 0) {
200
if (r.nextBoolean()) {
201
inDefaults += "max_retries = " + max + "\n";
202
} else {
203
inRealm += " max_retries = " + max + "\n";
204
inDefaults += "max_retries = " + (max + 1) + "\n";
205
}
206
}
207
208
if (to > 0) {
209
if (r.nextBoolean()) {
210
inDefaults += "kdc_timeout = " + to + "\n";
211
} else {
212
inRealm += " kdc_timeout = " + to + "\n";
213
inDefaults += "kdc_timeout = " + (to + 1) + "\n";
214
}
215
}
216
217
if (udp) {
218
if (r.nextBoolean()) {
219
inDefaults += "udp_preference_limit = 10000\n";
220
} else if (r.nextBoolean()) {
221
inRealm += " udp_preference_limit = 10000\n";
222
inDefaults += "udp_preference_limit = 1\n";
223
} // else no settings means UDP
224
} else {
225
if (r.nextBoolean()) {
226
inDefaults += "udp_preference_limit = 1\n";
227
} else {
228
inRealm += " udp_preference_limit = 1\n";
229
inDefaults += "udp_preference_limit = 10000\n";
230
}
231
}
232
233
conf = "[libdefaults]\n" +
234
"default_realm = " + OneKDC.REALM + "\n" +
235
inDefaults +
236
"\n" +
237
"[realms]\n" +
238
OneKDC.REALM + " = {\n";
239
240
for (int port : ports) {
241
conf += " kdc = " + OneKDC.KDCHOST + ":" + port + "\n" +
242
inRealm;
243
}
244
245
conf += "}\n";
246
247
Files.write(Paths.get("alternative-krb5.conf"), conf.getBytes());
248
Config.refresh();
249
}
250
251
/**
252
* One call of krb5 login. As long as the result matches one of expected,
253
* the test is considered as success. The grammar of expected is
254
*
255
* kdc#, timeout, kdc#, timeout, ..., optional "-" for failure
256
*/
257
static void test(String... expected) throws Exception {
258
259
System.out.println("------------------TEST----------------------");
260
PrintStream oldOut = System.out;
261
boolean failed = false;
262
ByteArrayOutputStream bo = new ByteArrayOutputStream();
263
System.setOut(new PrintStream(bo));
264
try {
265
Context.fromUserPass(OneKDC.USER, OneKDC.PASS, false);
266
} catch (Exception e) {
267
failed = true;
268
} finally {
269
System.setOut(oldOut);
270
}
271
272
String[] lines = new String(bo.toByteArray()).split("\n");
273
StringBuilder sb = new StringBuilder();
274
for (String line: lines) {
275
if (cm.match(line)) {
276
if (udp != cm.isUDP()) {
277
sb.append("x");
278
}
279
sb.append(cm.kdc()).append(cm.timeout());
280
}
281
}
282
if (failed) sb.append('-');
283
284
String output = sb.toString();
285
286
boolean found = false;
287
for (String ex : expected) {
288
if (output.matches(ex)) {
289
System.out.println("Expected: " + ex + ", actual " + output);
290
found = true;
291
break;
292
}
293
}
294
295
if (!found) {
296
System.out.println("--------------- ERROR START -------------");
297
System.out.println(new String(bo.toByteArray()));
298
System.out.println("--------------- ERROR END ---------------");
299
throw new Exception("Does not match. Output is " + output);
300
}
301
}
302
303
/**
304
* A helper class to match the krb5 debug output:
305
* >>> KDCCommunication: kdc=host UDP:11555, timeout=200,Attempt =1, #bytes=138
306
*
307
* Example:
308
* DebugMatcher cm = new DebugMatcher();
309
* cm.addPort(12345).addPort(11555);
310
* for (String line : debugOutput) {
311
* if (cm.match(line)) {
312
* System.out.printf("%c%d\n", cm.kdc(), cm.timeout());
313
* // shows b200 for the example above
314
* }
315
* }
316
*/
317
static class DebugMatcher {
318
319
static final Pattern re = Pattern.compile(
320
">>> KDCCommunication: kdc=\\S+ (TCP|UDP):(\\d+), " +
321
"timeout=(\\d+),Attempt\\s*=(\\d+)");
322
323
List<Integer> kdcPorts = new ArrayList<>();
324
Matcher matcher;
325
326
/**
327
* Add KDC ports one by one. See {@link #kdc()}.
328
*/
329
DebugMatcher addPort(int port) {
330
if (port > 0) {
331
kdcPorts.add(port);
332
} else {
333
kdcPorts.clear();
334
}
335
return this;
336
}
337
338
/**
339
* When a line matches the ">>> KDCCommunication:" pattern. After a
340
* match, the getters below can be called on this match.
341
*/
342
boolean match(String line) {
343
matcher = re.matcher(line);
344
return matcher.find();
345
}
346
347
/**
348
* Protocol of this match, "UDP" or "TCP".
349
*/
350
boolean isUDP() {
351
return matcher.group(1).equals("UDP");
352
}
353
354
/**
355
* KDC for this match, "a" for the one 1st added bt addPort(), "b"
356
* for second, etc. Undefined for not added.
357
*/
358
char kdc() {
359
int port = Integer.parseInt(matcher.group(2));
360
return (char) (kdcPorts.indexOf(port) + 'a');
361
}
362
363
/**
364
* Timeout value for this match.
365
*/
366
int timeout() {
367
return Integer.parseInt(matcher.group(3));
368
}
369
}
370
}
371
372