|
1 /* |
|
2 * Copyright 2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 import com.sun.security.jgss.ExtendedGSSContext; |
|
25 import java.io.File; |
|
26 import java.io.FileOutputStream; |
|
27 import java.io.IOException; |
|
28 import java.security.Security; |
|
29 import javax.security.auth.callback.Callback; |
|
30 import javax.security.auth.callback.CallbackHandler; |
|
31 import javax.security.auth.callback.NameCallback; |
|
32 import javax.security.auth.callback.PasswordCallback; |
|
33 import javax.security.auth.callback.UnsupportedCallbackException; |
|
34 import org.ietf.jgss.GSSContext; |
|
35 import org.ietf.jgss.GSSCredential; |
|
36 import org.ietf.jgss.GSSException; |
|
37 import org.ietf.jgss.GSSManager; |
|
38 import org.ietf.jgss.GSSName; |
|
39 import sun.security.jgss.GSSUtil; |
|
40 import sun.security.krb5.Config; |
|
41 |
|
42 public class OkAsDelegateXRealm implements CallbackHandler { |
|
43 |
|
44 /** |
|
45 * @param args boolean if the program should succeed |
|
46 */ |
|
47 public static void main(String[] args) |
|
48 throws Exception { |
|
49 |
|
50 // Create and start the KDCs. Here we have 3 realms: R1, R2 and R3. |
|
51 // R1 is trusted by R2, and R2 trusted by R3. |
|
52 KDC kdc1 = KDC.create("R1"); |
|
53 kdc1.setPolicy("ok-as-delegate", |
|
54 System.getProperty("test.kdc.policy.ok-as-delegate")); |
|
55 kdc1.addPrincipal("dummy", "bogus".toCharArray()); |
|
56 kdc1.addPrincipalRandKey("krbtgt/R1"); |
|
57 kdc1.addPrincipal("krbtgt/R2@R1", "r1->r2".toCharArray()); |
|
58 |
|
59 KDC kdc2 = KDC.create("R2"); |
|
60 kdc2.setPolicy("ok-as-delegate", |
|
61 System.getProperty("test.kdc.policy.ok-as-delegate")); |
|
62 kdc2.addPrincipalRandKey("krbtgt/R2"); |
|
63 kdc2.addPrincipal("krbtgt/R2@R1", "r1->r2".toCharArray()); |
|
64 kdc2.addPrincipal("krbtgt/R3@R2", "r2->r3".toCharArray()); |
|
65 |
|
66 KDC kdc3 = KDC.create("R3"); |
|
67 kdc3.setPolicy("ok-as-delegate", |
|
68 System.getProperty("test.kdc.policy.ok-as-delegate")); |
|
69 kdc3.addPrincipalRandKey("krbtgt/R3"); |
|
70 kdc3.addPrincipal("krbtgt/R3@R2", "r2->r3".toCharArray()); |
|
71 kdc3.addPrincipalRandKey("host/host.r3.local"); |
|
72 |
|
73 KDC.saveConfig("krb5-localkdc.conf", kdc1, kdc2, kdc3, |
|
74 "forwardable=true", |
|
75 "[capaths]", |
|
76 "R1 = {", |
|
77 " R2 = .", |
|
78 " R3 = R2", |
|
79 "}", |
|
80 "[domain_realm]", |
|
81 ".r3.local=R3" |
|
82 ); |
|
83 |
|
84 System.setProperty("java.security.krb5.conf", "krb5-localkdc.conf"); |
|
85 kdc3.writeKtab("localkdc.ktab"); |
|
86 |
|
87 FileOutputStream fos = new FileOutputStream("jaas-localkdc.conf"); |
|
88 |
|
89 // Defines the client and server on R1 and R3 respectively. |
|
90 fos.write(("com.sun.security.jgss.krb5.initiate {\n" + |
|
91 " com.sun.security.auth.module.Krb5LoginModule\n" + |
|
92 " required\n" + |
|
93 " principal=dummy\n" + |
|
94 " doNotPrompt=false\n" + |
|
95 " useTicketCache=false\n" + |
|
96 " ;\n};\n" + |
|
97 "com.sun.security.jgss.krb5.accept {\n" + |
|
98 " com.sun.security.auth.module.Krb5LoginModule required\n" + |
|
99 " principal=\"host/host.r3.local@R3\"\n" + |
|
100 " useKeyTab=true\n" + |
|
101 " keyTab=localkdc.ktab\n" + |
|
102 " isInitiator=false\n" + |
|
103 " storeKey=true;\n};\n" + |
|
104 "\n").getBytes()); |
|
105 fos.close(); |
|
106 |
|
107 Security.setProperty("auth.login.defaultCallbackHandler", |
|
108 "OkAsDelegateXRealm"); |
|
109 |
|
110 System.setProperty("java.security.auth.login.config", "jaas-localkdc.conf"); |
|
111 |
|
112 new File("krb5-localkdc.conf").deleteOnExit(); |
|
113 new File("localkdc.ktab").deleteOnExit(); |
|
114 new File("jaas-localkdc.conf").deleteOnExit(); |
|
115 Config.refresh(); |
|
116 |
|
117 Context c = Context.fromJAAS("com.sun.security.jgss.krb5.initiate"); |
|
118 Context s = Context.fromJAAS("com.sun.security.jgss.krb5.accept"); |
|
119 |
|
120 // Test twice. The frist time the whole cross realm process is tried, |
|
121 // the second time the cached service ticket is used. This is to make sure |
|
122 // the behaviors are the same, especailly for the case when one of the |
|
123 // cross-realm TGTs does not have OK-AS-DELEGATE on. |
|
124 |
|
125 for (int i=0; i<2; i++) { |
|
126 c.startAsClient("host@host.r3.local", GSSUtil.GSS_KRB5_MECH_OID); |
|
127 s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID); |
|
128 c.x().requestDelegPolicy(true); |
|
129 |
|
130 Context.handshake(c, s); |
|
131 boolean succeed = true; |
|
132 try { |
|
133 s.x().getDelegCred(); |
|
134 } catch (GSSException gsse) { |
|
135 succeed = false; |
|
136 } |
|
137 if (succeed != Boolean.parseBoolean(args[0])) { |
|
138 throw new Exception("Test fail at round #" + i); |
|
139 } |
|
140 } |
|
141 } |
|
142 |
|
143 @Override |
|
144 public void handle(Callback[] callbacks) |
|
145 throws IOException, UnsupportedCallbackException { |
|
146 for (Callback callback : callbacks) { |
|
147 if (callback instanceof NameCallback) { |
|
148 ((NameCallback) callback).setName("dummy"); |
|
149 } |
|
150 if (callback instanceof PasswordCallback) { |
|
151 ((PasswordCallback) callback).setPassword("bogus".toCharArray()); |
|
152 } |
|
153 } |
|
154 } |
|
155 } |
|
156 |