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 |
import java.io.File;
|
|
25 |
import java.io.FileOutputStream;
|
|
26 |
import java.io.IOException;
|
1456
|
27 |
import java.net.InetAddress;
|
|
28 |
import java.net.UnknownHostException;
|
1454
|
29 |
import java.security.Security;
|
|
30 |
import javax.security.auth.callback.Callback;
|
|
31 |
import javax.security.auth.callback.CallbackHandler;
|
|
32 |
import javax.security.auth.callback.NameCallback;
|
|
33 |
import javax.security.auth.callback.PasswordCallback;
|
|
34 |
import sun.security.krb5.Config;
|
|
35 |
|
|
36 |
/**
|
|
37 |
* This class starts a simple KDC with one realm, several typical principal
|
|
38 |
* names, generates delete-on-exit krb5.conf and keytab files, and setup
|
|
39 |
* system properties for them. There's also a helper method to generate a
|
|
40 |
* JAAS login config file that can be used for JAAS or JGSS apps.
|
|
41 |
* <p>
|
|
42 |
* Just call this line to start everything:
|
|
43 |
* <pre>
|
|
44 |
* new OneKDC(null).writeJaasConf();
|
|
45 |
* </pre>
|
|
46 |
*/
|
|
47 |
public class OneKDC extends KDC {
|
|
48 |
|
|
49 |
public static final String USER = "dummy";
|
|
50 |
public static final char[] PASS = "bogus".toCharArray();
|
|
51 |
public static final String KRB5_CONF = "localkdc-krb5.conf";
|
|
52 |
public static final String KTAB = "localkdc.ktab";
|
|
53 |
public static final String JAAS_CONF = "localkdc-jaas.conf";
|
|
54 |
public static final String REALM = "RABBIT.HOLE";
|
3046
|
55 |
public static String SERVER = "server/host." + REALM.toLowerCase();
|
|
56 |
public static String BACKEND = "backend/host." + REALM.toLowerCase();
|
|
57 |
public static String KDCHOST = "kdc." + REALM.toLowerCase();
|
1454
|
58 |
/**
|
|
59 |
* Creates the KDC and starts it.
|
|
60 |
* @param etype Encryption type, null if not specified
|
|
61 |
* @throws java.lang.Exception if there's anything wrong
|
|
62 |
*/
|
|
63 |
public OneKDC(String etype) throws Exception {
|
3046
|
64 |
super(REALM, KDCHOST, 0, true);
|
1454
|
65 |
addPrincipal(USER, PASS);
|
|
66 |
addPrincipalRandKey("krbtgt/" + REALM);
|
|
67 |
addPrincipalRandKey(SERVER);
|
|
68 |
addPrincipalRandKey(BACKEND);
|
|
69 |
KDC.saveConfig(KRB5_CONF, this,
|
|
70 |
"forwardable = true",
|
|
71 |
"default_keytab_name = " + KTAB,
|
|
72 |
etype == null ? "" : "default_tkt_enctypes=" + etype + "\ndefault_tgs_enctypes=" + etype);
|
|
73 |
System.setProperty("java.security.krb5.conf", KRB5_CONF);
|
|
74 |
// Whatever krb5.conf had been loaded before, we reload ours now.
|
|
75 |
Config.refresh();
|
|
76 |
|
|
77 |
writeKtab(KTAB);
|
|
78 |
new File(KRB5_CONF).deleteOnExit();
|
|
79 |
new File(KTAB).deleteOnExit();
|
|
80 |
}
|
|
81 |
|
|
82 |
/**
|
|
83 |
* Writes a JAAS login config file, which contains as many as useful
|
|
84 |
* entries, including JGSS style initiator/acceptor and normal JAAS
|
|
85 |
* entries with names using existing OneKDC principals.
|
|
86 |
* @throws java.lang.Exception if anything goes wrong
|
|
87 |
*/
|
|
88 |
public void writeJAASConf() throws IOException {
|
|
89 |
System.setProperty("java.security.auth.login.config", JAAS_CONF);
|
|
90 |
File f = new File(JAAS_CONF);
|
|
91 |
FileOutputStream fos = new FileOutputStream(f);
|
|
92 |
fos.write((
|
|
93 |
"com.sun.security.jgss.krb5.initiate {\n" +
|
|
94 |
" com.sun.security.auth.module.Krb5LoginModule required;\n};\n" +
|
|
95 |
"com.sun.security.jgss.krb5.accept {\n" +
|
|
96 |
" com.sun.security.auth.module.Krb5LoginModule required\n" +
|
|
97 |
" principal=\"" + SERVER + "\"\n" +
|
|
98 |
" useKeyTab=true\n" +
|
|
99 |
" isInitiator=false\n" +
|
|
100 |
" storeKey=true;\n};\n" +
|
|
101 |
"client {\n" +
|
|
102 |
" com.sun.security.auth.module.Krb5LoginModule required;\n};\n" +
|
|
103 |
"server {\n" +
|
|
104 |
" com.sun.security.auth.module.Krb5LoginModule required\n" +
|
|
105 |
" principal=\"" + SERVER + "\"\n" +
|
|
106 |
" useKeyTab=true\n" +
|
|
107 |
" storeKey=true;\n};\n" +
|
|
108 |
"backend {\n" +
|
|
109 |
" com.sun.security.auth.module.Krb5LoginModule required\n" +
|
|
110 |
" principal=\"" + BACKEND + "\"\n" +
|
|
111 |
" useKeyTab=true\n" +
|
|
112 |
" storeKey=true\n" +
|
|
113 |
" isInitiator=false;\n};\n"
|
|
114 |
).getBytes());
|
|
115 |
fos.close();
|
|
116 |
f.deleteOnExit();
|
|
117 |
Security.setProperty("auth.login.defaultCallbackHandler", "OneKDC$CallbackForClient");
|
|
118 |
}
|
|
119 |
|
|
120 |
/**
|
|
121 |
* The default callback handler for JAAS login. Note that this handler is
|
|
122 |
* hard coded to provide only info for USER1. If you need to provide info
|
|
123 |
* for another principal, please use Context.fromUserPass() instead.
|
|
124 |
*/
|
|
125 |
public static class CallbackForClient implements CallbackHandler {
|
|
126 |
public void handle(Callback[] callbacks) {
|
|
127 |
String user = OneKDC.USER;
|
|
128 |
char[] pass = OneKDC.PASS;
|
|
129 |
for (Callback callback : callbacks) {
|
|
130 |
if (callback instanceof NameCallback) {
|
|
131 |
System.out.println("Callback for name: " + user);
|
|
132 |
((NameCallback) callback).setName(user);
|
|
133 |
}
|
|
134 |
if (callback instanceof PasswordCallback) {
|
|
135 |
System.out.println("Callback for pass: "
|
|
136 |
+ new String(pass));
|
|
137 |
((PasswordCallback) callback).setPassword(pass);
|
|
138 |
}
|
|
139 |
}
|
|
140 |
}
|
|
141 |
}
|
|
142 |
}
|