1454
|
1 |
/*
|
3046
|
2 |
* Copyright 2008-2009 Sun Microsystems, Inc. All Rights Reserved.
|
1454
|
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 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 6706974
|
|
27 |
* @summary Add krb5 test infrastructure
|
|
28 |
*/
|
3046
|
29 |
import java.io.File;
|
1454
|
30 |
import java.io.FileOutputStream;
|
|
31 |
import java.io.IOException;
|
|
32 |
import java.security.Security;
|
|
33 |
import javax.security.auth.callback.Callback;
|
|
34 |
import javax.security.auth.callback.CallbackHandler;
|
|
35 |
import javax.security.auth.callback.NameCallback;
|
|
36 |
import javax.security.auth.callback.PasswordCallback;
|
|
37 |
import javax.security.auth.callback.UnsupportedCallbackException;
|
|
38 |
import org.ietf.jgss.GSSContext;
|
|
39 |
import org.ietf.jgss.GSSManager;
|
|
40 |
import org.ietf.jgss.GSSName;
|
|
41 |
import sun.security.jgss.GSSUtil;
|
|
42 |
|
|
43 |
public class CrossRealm implements CallbackHandler {
|
|
44 |
public static void main(String[] args) throws Exception {
|
|
45 |
startKDCs();
|
|
46 |
xRealmAuth();
|
|
47 |
}
|
|
48 |
|
|
49 |
static void startKDCs() throws Exception {
|
|
50 |
// Create and start the KDC
|
|
51 |
KDC kdc1 = KDC.create("RABBIT.HOLE");
|
|
52 |
kdc1.addPrincipal("dummy", "bogus".toCharArray());
|
|
53 |
kdc1.addPrincipalRandKey("krbtgt/RABBIT.HOLE");
|
3046
|
54 |
kdc1.addPrincipal("krbtgt/SNAKE.HOLE@RABBIT.HOLE",
|
|
55 |
"rabbit->snake".toCharArray());
|
1454
|
56 |
|
|
57 |
KDC kdc2 = KDC.create("SNAKE.HOLE");
|
|
58 |
kdc2.addPrincipalRandKey("krbtgt/SNAKE.HOLE");
|
3046
|
59 |
kdc2.addPrincipal("krbtgt/SNAKE.HOLE@RABBIT.HOLE",
|
|
60 |
"rabbit->snake".toCharArray());
|
1454
|
61 |
kdc2.addPrincipalRandKey("host/www.snake.hole");
|
|
62 |
|
|
63 |
KDC.saveConfig("krb5-localkdc.conf", kdc1, kdc2,
|
|
64 |
"forwardable=true",
|
|
65 |
"[domain_realm]",
|
|
66 |
".snake.hole=SNAKE.HOLE");
|
3046
|
67 |
new File("krb5-localkdc.conf").deleteOnExit();
|
1454
|
68 |
System.setProperty("java.security.krb5.conf", "krb5-localkdc.conf");
|
|
69 |
}
|
|
70 |
|
|
71 |
static void xRealmAuth() throws Exception {
|
|
72 |
Security.setProperty("auth.login.defaultCallbackHandler", "CrossRealm");
|
|
73 |
System.setProperty("java.security.auth.login.config", "jaas-localkdc.conf");
|
|
74 |
System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
|
3046
|
75 |
new File("jaas-localkdc.conf").deleteOnExit();
|
1454
|
76 |
FileOutputStream fos = new FileOutputStream("jaas-localkdc.conf");
|
|
77 |
fos.write(("com.sun.security.jgss.krb5.initiate {\n" +
|
|
78 |
" com.sun.security.auth.module.Krb5LoginModule\n" +
|
|
79 |
" required\n" +
|
|
80 |
" principal=dummy\n" +
|
|
81 |
" doNotPrompt=false\n" +
|
|
82 |
" useTicketCache=false\n" +
|
|
83 |
" ;\n" +
|
|
84 |
"};").getBytes());
|
|
85 |
fos.close();
|
|
86 |
|
|
87 |
GSSManager m = GSSManager.getInstance();
|
|
88 |
m.createContext(
|
|
89 |
m.createName("host@www.snake.hole", GSSName.NT_HOSTBASED_SERVICE),
|
|
90 |
GSSUtil.GSS_KRB5_MECH_OID,
|
|
91 |
null,
|
|
92 |
GSSContext.DEFAULT_LIFETIME).initSecContext(new byte[0], 0, 0);
|
|
93 |
}
|
|
94 |
|
|
95 |
@Override
|
|
96 |
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
|
|
97 |
for (Callback callback : callbacks) {
|
|
98 |
if (callback instanceof NameCallback) {
|
|
99 |
((NameCallback) callback).setName("dummy");
|
|
100 |
}
|
|
101 |
if (callback instanceof PasswordCallback) {
|
|
102 |
((PasswordCallback) callback).setPassword("bogus".toCharArray());
|
|
103 |
}
|
|
104 |
}
|
|
105 |
}
|
|
106 |
}
|