Path: blob/master/test/jdk/sun/security/krb5/auto/BasicProc.java
41152 views
/*1* Copyright (c) 2013, 2019, 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*/2223/*24* @test25* @bug 8009977 8186884 8194486 820162726* @summary A test to launch multiple Java processes using either Java GSS27* or native GSS28* @library /test/lib29* @compile -XDignore.symbol.file BasicProc.java30* @run main jdk.test.lib.FileInstaller TestHosts TestHosts31* @run main/othervm -Djdk.net.hosts.file=TestHosts BasicProc launcher32*/3334import java.nio.file.Files;35import java.nio.file.Paths;36import java.nio.file.attribute.PosixFilePermission;37import java.util.Arrays;38import java.util.PropertyPermission;39import java.util.Set;4041import jdk.test.lib.Asserts;42import jdk.test.lib.Platform;43import jdk.test.lib.process.Proc;44import org.ietf.jgss.Oid;45import sun.security.krb5.Config;4647import javax.security.auth.PrivateCredentialPermission;4849/**50* Run this test automatically and test Java GSS with embedded KDC.51*52* Run with customized native.krb5.libs to test interop between Java GSS53* and native GSS, and native.kdc.path with a native KDC. For example,54* run the following command to test interop among Java, default native,55* MIT, and Heimdal krb5 libraries with the Heimdal KDC:56*57* jtreg -Dnative.krb5.libs=j=,58* n=,59* k=/usr/local/krb5/lib/libgssapi_krb5.so,60* h=/space/install/heimdal/lib/libgssapi.so \61* -Dnative.kdc.path=/usr/local/heimdal \62* BasicProc.java63*64* Note: The first 4 lines should be concatenated to make a long system65* property value with no blank around ",". This comma-separated value66* has each element being name=libpath. The special name "j" means the67* Java library and libpath is ignored. Otherwise it means a native library,68* and libpath (can be empty) will be the value for the sun.security.jgss.lib69* system property. If this system property is not set, only the Java70* library will be tested.71*/7273public class BasicProc {7475private static final String CONF = "krb5.conf";76private static final String KTAB_S = "server.ktab";77private static final String KTAB_B = "backend.ktab";7879private static final String HOST = "localhost";80private static final String SERVER = "server/" + HOST;81private static final String BACKEND = "backend/" + HOST;82private static final String USER = "user";83private static final char[] PASS = "password".toCharArray();84private static final String REALM = "REALM";8586private static final int MSGSIZE = 1024;87private static final byte[] MSG = new byte[MSGSIZE];8889public static void main(String[] args) throws Exception {9091Oid oid = new Oid("1.2.840.113554.1.2.2");92byte[] token, msg;9394switch (args[0]) {95case "launcher":96KDC kdc = KDC.create(REALM, HOST, 0, true);97try {98kdc.addPrincipal(USER, PASS);99kdc.addPrincipalRandKey("krbtgt/" + REALM);100kdc.addPrincipalRandKey(SERVER);101kdc.addPrincipalRandKey(BACKEND);102103// Native lib might do some name lookup104KDC.saveConfig(CONF, kdc,105"dns_lookup_kdc = no",106"ticket_lifetime = 1h",107"dns_lookup_realm = no",108"dns_canonicalize_hostname = false",109"forwardable = true");110System.setProperty("java.security.krb5.conf", CONF);111Config.refresh();112kdc.writeKtab(KTAB_S, false, SERVER);113kdc.writeKtab(KTAB_B, false, BACKEND);114115String[] tmp = System.getProperty("native.krb5.libs", "j=")116.split(",");117118// Library paths. The 1st one is always null which means119// Java, "" means the default native lib.120String[] libs = new String[tmp.length];121122// Names for each lib above. Use in file names.123String[] names = new String[tmp.length];124125boolean hasNative = false;126127for (int i = 0; i < tmp.length; i++) {128if (tmp[i].isEmpty()) {129throw new Exception("Invalid native.krb5.libs");130}131String[] pair = tmp[i].split("=", 2);132names[i] = pair[0];133if (!pair[0].equals("j")) {134libs[i] = pair.length > 1 ? pair[1] : "";135hasNative = true;136}137}138139if (hasNative) {140kdc.kinit(USER, "base.ccache");141}142143// Try the same lib first144for (int i = 0; i < libs.length; i++) {145once(names[i] + names[i] + names[i],146libs[i], libs[i], libs[i]);147}148149for (int i = 0; i < libs.length; i++) {150for (int j = 0; j < libs.length; j++) {151for (int k = 0; k < libs.length; k++) {152if (i != j || i != k) {153once(names[i] + names[j] + names[k],154libs[i], libs[j], libs[k]);155}156}157}158}159} finally {160kdc.terminate();161}162break;163case "client":164Context c = args[1].equals("n") ?165Context.fromThinAir() :166Context.fromUserPass(USER, PASS, false);167c.startAsClient(SERVER, oid);168c.x().requestCredDeleg(true);169c.x().requestMutualAuth(true);170Proc.binOut(c.take(new byte[0])); // AP-REQ171c.take(Proc.binIn()); // AP-REP172Proc.binOut(c.wrap(MSG, true));173Proc.binOut(c.getMic(MSG));174break;175case "server":176Context s = args[1].equals("n") ?177Context.fromThinAir() :178Context.fromUserKtab(SERVER, KTAB_S, true);179s.startAsServer(oid);180token = Proc.binIn(); // AP-REQ181Proc.binOut(s.take(token)); // AP-REP182msg = s.unwrap(Proc.binIn(), true);183Asserts.assertTrue(Arrays.equals(msg, MSG));184s.verifyMic(Proc.binIn(), msg);185Context s2 = s.delegated();186s2.startAsClient(BACKEND, oid);187s2.x().requestMutualAuth(false);188Proc.binOut(s2.take(new byte[0])); // AP-REQ189msg = s2.unwrap(Proc.binIn(), true);190Asserts.assertTrue(Arrays.equals(msg, MSG));191s2.verifyMic(Proc.binIn(), msg);192break;193case "backend":194Context b = args[1].equals("n") ?195Context.fromThinAir() :196Context.fromUserKtab(BACKEND, KTAB_B, true);197b.startAsServer(oid);198token = b.take(Proc.binIn()); // AP-REQ199Asserts.assertTrue(token == null);200Proc.binOut(b.wrap(MSG, true));201Proc.binOut(b.getMic(MSG));202break;203}204}205206/**207* One test run.208*209* @param label test label210* @param lc lib of client211* @param ls lib of server212* @param lb lib of backend213*/214private static void once(String label, String lc, String ls, String lb)215throws Exception {216217Proc pc = proc(lc)218.args("client", lc == null ? "j" : "n")219.perm(new javax.security.auth.kerberos.ServicePermission(220"krbtgt/" + REALM + "@" + REALM, "initiate"))221.perm(new javax.security.auth.kerberos.ServicePermission(222SERVER + "@" + REALM, "initiate"))223.perm(new javax.security.auth.kerberos.DelegationPermission(224"\"" + SERVER + "@" + REALM + "\" " +225"\"krbtgt/" + REALM + "@" + REALM + "\""))226.debug(label + "-C");227if (lc == null) {228// for Krb5LoginModule::promptForName229pc.perm(new PropertyPermission("user.name", "read"));230} else {231Files.copy(Paths.get("base.ccache"), Paths.get(label + ".ccache"));232if (!Platform.isWindows()) {233Files.setPosixFilePermissions(Paths.get(label + ".ccache"),234Set.of(PosixFilePermission.OWNER_READ,235PosixFilePermission.OWNER_WRITE));236}237pc.env("KRB5CCNAME", "FILE:" + label + ".ccache");238// Do not try system ktab if ccache fails239pc.env("KRB5_KTNAME", "none");240}241pc.start();242243Proc ps = proc(ls)244.args("server", ls == null ? "j" : "n")245.perm(new javax.security.auth.kerberos.ServicePermission(246SERVER + "@" + REALM, "accept"))247.perm(new javax.security.auth.kerberos.ServicePermission(248BACKEND + "@" + REALM, "initiate"))249.debug(label + "-S");250if (ls == null) {251ps.perm(new PrivateCredentialPermission(252"javax.security.auth.kerberos.KeyTab * \"*\"", "read"))253.perm(new java.io.FilePermission(KTAB_S, "read"));254} else {255ps.env("KRB5_KTNAME", KTAB_S);256}257ps.start();258259Proc pb = proc(lb)260.args("backend", lb == null ? "j" : "n")261.perm(new javax.security.auth.kerberos.ServicePermission(262BACKEND + "@" + REALM, "accept"))263.debug(label + "-B");264if (lb == null) {265pb.perm(new PrivateCredentialPermission(266"javax.security.auth.kerberos.KeyTab * \"*\"", "read"))267.perm(new java.io.FilePermission(KTAB_B, "read"));268} else {269pb.env("KRB5_KTNAME", KTAB_B);270}271pb.start();272273// Client and server274ps.println(pc.readData()); // AP-REQ275pc.println(ps.readData()); // AP-REP276277ps.println(pc.readData()); // KRB-PRIV278ps.println(pc.readData()); // KRB-SAFE279280// Server and backend281pb.println(ps.readData()); // AP-REQ282283ps.println(pb.readData()); // KRB-PRIV284ps.println(pb.readData()); // KRB-SAFE285286if ((pc.waitFor() | ps.waitFor() | pb.waitFor()) != 0) {287throw new Exception("Process failed");288}289}290291/**292* A Proc for a child process.293*294* @param lib the library. Null is Java. "" is default native lib.295*/296private static Proc proc(String lib) throws Exception {297Proc p = Proc.create("BasicProc")298.inheritProp("jdk.net.hosts.file")299.prop("java.security.manager", "")300.perm(new javax.security.auth.AuthPermission("doAs"));301if (lib != null) {302p.env("KRB5_CONFIG", CONF)303.env("KRB5_TRACE", Platform.isWindows() ? "CON" : "/dev/stderr")304.prop("sun.security.jgss.native", "true")305.prop("sun.security.jgss.lib", lib)306.prop("javax.security.auth.useSubjectCredsOnly", "false")307.prop("sun.security.nativegss.debug", "true");308int pos = lib.lastIndexOf('/');309if (pos > 0) {310p.env(Platform.sharedLibraryPathVariableName(), lib.substring(0, pos));311}312} else {313p.perm(new java.util.PropertyPermission(314"sun.security.krb5.principal", "read"))315// For Krb5LoginModule::login.316.perm(new javax.security.auth.AuthPermission(317"modifyPrincipals"))318.perm(new javax.security.auth.AuthPermission(319"modifyPrivateCredentials"))320.prop("sun.security.krb5.debug", "true")321.prop("java.security.krb5.conf", CONF);322}323return p;324}325}326327328