Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/krb5/auto/OneKDC.java
38853 views
/*1* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.File;24import java.io.FileOutputStream;25import java.io.IOException;26import java.security.Security;27import javax.security.auth.callback.Callback;28import javax.security.auth.callback.CallbackHandler;29import javax.security.auth.callback.NameCallback;30import javax.security.auth.callback.PasswordCallback;31import sun.security.krb5.Config;3233/**34* This class starts a simple KDC with one realm, several typical principal35* names, generates delete-on-exit krb5.conf and keytab files, and setup36* system properties for them. There's also a helper method to generate a37* JAAS login config file that can be used for JAAS or JGSS apps.38* <p>39* Just call this line to start everything:40* <pre>41* new OneKDC(null).writeJaasConf();42* </pre>43*/44public class OneKDC extends KDC {4546public static final String USER = "dummy";47public static final char[] PASS = "bogus".toCharArray();48public static final String USER2 = "foo";49public static final char[] PASS2 = "bar".toCharArray();50public static final String KRB5_CONF = "localkdc-krb5.conf";51public static final String KTAB = "localkdc.ktab";52public static final String JAAS_CONF = "localkdc-jaas.conf";53public static final String REALM = "RABBIT.HOLE";54public static String SERVER = "server/host." + REALM.toLowerCase();55public static String BACKEND = "backend/host." + REALM.toLowerCase();56public static String KDCHOST = "kdc." + REALM.toLowerCase();57/**58* Creates the KDC and starts it.59* @param etype Encryption type, null if not specified60* @throws java.lang.Exception if there's anything wrong61*/62public OneKDC(String etype) throws Exception {63super(REALM, KDCHOST, 0, true);64addPrincipal(USER, PASS);65addPrincipal(USER2, PASS2);66addPrincipalRandKey("krbtgt/" + REALM);67addPrincipalRandKey(SERVER);68addPrincipalRandKey(BACKEND);6970String extraConfig = "";71if (etype != null) {72extraConfig += "default_tkt_enctypes=" + etype73+ "\ndefault_tgs_enctypes=" + etype;74if (etype.startsWith("des")) {75extraConfig += "\nallow_weak_crypto = true";76}77}78KDC.saveConfig(KRB5_CONF, this,79"forwardable = true",80"default_keytab_name = " + KTAB,81extraConfig);82System.setProperty("java.security.krb5.conf", KRB5_CONF);83// Whatever krb5.conf had been loaded before, we reload ours now.84Config.refresh();8586writeKtab(KTAB);87Security.setProperty("auth.login.defaultCallbackHandler",88"OneKDC$CallbackForClient");89}9091/**92* Writes a JAAS login config file, which contains as many as useful93* entries, including JGSS style initiator/acceptor and normal JAAS94* entries with names using existing OneKDC principals.95* @throws java.lang.Exception if anything goes wrong96*/97public OneKDC writeJAASConf() throws IOException {98System.setProperty("java.security.auth.login.config", JAAS_CONF);99File f = new File(JAAS_CONF);100FileOutputStream fos = new FileOutputStream(f);101fos.write((102"com.sun.security.jgss.krb5.initiate {\n" +103" com.sun.security.auth.module.Krb5LoginModule required;\n};\n" +104"com.sun.security.jgss.krb5.accept {\n" +105" com.sun.security.auth.module.Krb5LoginModule required\n" +106" principal=\"*\"\n" +107" useKeyTab=true\n" +108" isInitiator=false\n" +109" storeKey=true;\n};\n" +110"client {\n" +111" com.sun.security.auth.module.Krb5LoginModule required;\n};\n" +112"server {\n" +113" com.sun.security.auth.module.Krb5LoginModule required\n" +114" principal=\"" + SERVER + "\"\n" +115" useKeyTab=true\n" +116" storeKey=true;\n};\n" +117"backend {\n" +118" com.sun.security.auth.module.Krb5LoginModule required\n" +119" principal=\"" + BACKEND + "\"\n" +120" useKeyTab=true\n" +121" storeKey=true\n" +122" isInitiator=false;\n};\n"123).getBytes());124fos.close();125return this;126}127128/**129* The default callback handler for JAAS login. Note that this handler is130* hard coded to provide only info for USER1. If you need to provide info131* for another principal, please use Context.fromUserPass() instead.132*/133public static class CallbackForClient implements CallbackHandler {134public void handle(Callback[] callbacks) {135String user = OneKDC.USER;136char[] pass = OneKDC.PASS;137for (Callback callback : callbacks) {138if (callback instanceof NameCallback) {139System.out.println("Callback for name: " + user);140((NameCallback) callback).setName(user);141}142if (callback instanceof PasswordCallback) {143System.out.println("Callback for pass: "144+ new String(pass));145((PasswordCallback) callback).setPassword(pass);146}147}148}149}150}151152153