9499
|
1 |
/*
|
|
2 |
* Copyright (c) 2011, 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 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 6894072
|
|
27 |
* @compile -XDignore.symbol.file TwoPrinces.java
|
|
28 |
* @run main/othervm TwoPrinces
|
|
29 |
* @summary always refresh keytab
|
|
30 |
*/
|
|
31 |
|
|
32 |
import java.io.File;
|
|
33 |
import java.io.FileOutputStream;
|
|
34 |
import sun.security.jgss.GSSUtil;
|
|
35 |
import sun.security.krb5.Config;
|
|
36 |
|
|
37 |
public class TwoPrinces {
|
|
38 |
|
|
39 |
public static void main(String[] args)
|
|
40 |
throws Exception {
|
|
41 |
|
|
42 |
KDC k1 = KDC.create("R1");
|
|
43 |
k1.addPrincipal("u1", "hello".toCharArray());
|
|
44 |
k1.addPrincipalRandKey("krbtgt/R1");
|
|
45 |
k1.addPrincipalRandKey("host/same.host");
|
|
46 |
|
|
47 |
KDC k2 = KDC.create("R2");
|
|
48 |
k2.addPrincipal("u2", "hello".toCharArray());
|
|
49 |
k2.addPrincipalRandKey("krbtgt/R2");
|
|
50 |
k2.addPrincipalRandKey("host/same.host");
|
|
51 |
|
|
52 |
System.setProperty("java.security.krb5.conf", "krb5.conf");
|
|
53 |
|
|
54 |
// R1 is the default realm now
|
|
55 |
KDC.saveConfig("krb5.conf", k1, k2);
|
|
56 |
Config.refresh();
|
|
57 |
|
|
58 |
k1.writeKtab("ktab1");
|
|
59 |
k2.writeKtab("ktab2");
|
|
60 |
|
|
61 |
// A JAAS config file with 2 Krb5LoginModules, after commit, the
|
|
62 |
// subject with have principals and keytabs from both sides
|
|
63 |
System.setProperty("java.security.auth.login.config", "jaas.conf");
|
|
64 |
File f = new File("jaas.conf");
|
|
65 |
FileOutputStream fos = new FileOutputStream(f);
|
|
66 |
fos.write((
|
|
67 |
"me {\n"
|
|
68 |
+ " com.sun.security.auth.module.Krb5LoginModule required"
|
|
69 |
+ " isInitiator=true principal=\"host/same.host@R1\""
|
|
70 |
+ " useKeyTab=true keyTab=ktab1 storeKey=true;\n"
|
|
71 |
+ " com.sun.security.auth.module.Krb5LoginModule required"
|
|
72 |
+ " isInitiator=true principal=\"host/same.host@R2\""
|
|
73 |
+ " useKeyTab=true keyTab=ktab2 storeKey=true;\n"
|
|
74 |
+ "};\n"
|
|
75 |
).getBytes());
|
|
76 |
fos.close();
|
|
77 |
|
|
78 |
/*
|
|
79 |
* This server side context will be able to act as services in both
|
|
80 |
* realms. Please note that we still don't support a single instance
|
|
81 |
* of server to accept connections from two realms at the same time.
|
|
82 |
* Therefore, we must call startAsServer in a given realm to start
|
|
83 |
* working there. The same Subject never changes anyway.
|
|
84 |
*/
|
|
85 |
Context s = Context.fromJAAS("me");
|
|
86 |
|
|
87 |
// Default realm still R1
|
|
88 |
s.startAsServer("host@same.host", GSSUtil.GSS_KRB5_MECH_OID);
|
|
89 |
Context c1 = Context.fromUserPass("u1", "hello".toCharArray(), false);
|
|
90 |
c1.startAsClient("host@same.host", GSSUtil.GSS_KRB5_MECH_OID);
|
|
91 |
Context.handshake(c1, s);
|
|
92 |
|
|
93 |
KDC.saveConfig("krb5.conf", k2, k1);
|
|
94 |
Config.refresh();
|
|
95 |
|
|
96 |
// Default realm now R2
|
|
97 |
s.startAsServer("host@same.host", GSSUtil.GSS_KRB5_MECH_OID);
|
|
98 |
Context c2 = Context.fromUserPass("u2", "hello".toCharArray(), false);
|
|
99 |
c2.startAsClient("host@same.host", GSSUtil.GSS_KRB5_MECH_OID);
|
|
100 |
Context.handshake(c2, s);
|
|
101 |
}
|
|
102 |
}
|