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