1 /*
2  * Copyright (c) 2015, 2018, 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.FileWriter;
26 import java.io.IOException;
27 import java.io.PrintWriter;
28 import java.util.HashMap;
29 import java.util.Map;
30 import javax.security.auth.callback.CallbackHandler;
31 import javax.security.auth.login.LoginContext;
32 import javax.security.auth.login.LoginException;
33 
34 /*
35  * @test
36  * @bug 4515853 8075297 8194486
37  * @summary Checks that Kerberos client tries slave KDC
38  *          if master KDC is not responding
39  * @library /test/lib
40  * @run main jdk.test.lib.FileInstaller TestHosts TestHosts
41  * @run main/othervm -Djdk.net.hosts.file=TestHosts BogusKDC
42  */
43 public class BogusKDC {
44 
45     static final String TEST_SRC = System.getProperty("test.src", ".");
46     static final String HOST = "localhost";
47     static final String NOT_EXISTING_HOST = "not.existing.host";
48     static final String REALM = "TEST.REALM";
49     static final String USER = "USER";
50     static final String USER_PRINCIPAL = USER + "@" + REALM;
51     static final String USER_PASSWORD = "password";
52     static final String KRBTGT_PRINCIPAL = "krbtgt/" + REALM;
53     static final String KRB5_CONF = "krb5.conf";
54     static final int WRONG_KDC_PORT = 21;
55 
56     static final String KRB5_CONF_TEMPLATE = ""
57             + "[libdefaults]\n"
58             + "default_realm = TEST.REALM\n"
59             + "max_retries = 1\n"
60             + "\n"
61             + "[realms]\n"
62             + "TEST.REALM = {\n"
63             + "    kdc = %s\n"
64             + "    kdc = localhost:%d\n"
65             + "}";
66 
main(String[] args)67     public static void main(String[] args) throws LoginException, IOException {
68         Map<String, String> principals = new HashMap<>();
69         principals.put(USER_PRINCIPAL, USER_PASSWORD);
70         principals.put(KRBTGT_PRINCIPAL, null);
71 
72         System.setProperty("java.security.krb5.conf", KRB5_CONF);
73 
74         // start a local KDC
75         KDC kdc = KDC.startKDC(HOST, KRB5_CONF, REALM, principals, null, null);
76 
77         System.setProperty("java.security.auth.login.config",
78                 TEST_SRC + File.separator + "refreshKrb5Config.jaas");
79 
80         CallbackHandler handler = new Helper.UserPasswordHandler(
81                 USER, USER_PASSWORD);
82 
83         // create a krb5 config with non-existing host for master KDC,
84         // and wrong port for slave KDC
85         try (PrintWriter w = new PrintWriter(new FileWriter(KRB5_CONF))) {
86             w.write(String.format(KRB5_CONF_TEMPLATE,
87                     KDC.NOT_EXISTING_HOST, WRONG_KDC_PORT));
88             w.flush();
89         }
90 
91         // login with not-refreshable config
92         try {
93             new LoginContext("NotRefreshable", handler).login();
94             throw new RuntimeException("Expected exception not thrown");
95         } catch (LoginException le) {
96             System.out.println("Expected login failure: " + le);
97         }
98 
99         // create a krb5 config with non-existing host for master KDC,
100         // but correct port for slave KDC
101         try (PrintWriter w = new PrintWriter(new FileWriter(KRB5_CONF))) {
102             w.write(String.format(KRB5_CONF_TEMPLATE,
103                     KDC.NOT_EXISTING_HOST, kdc.getPort()));
104             w.flush();
105         }
106 
107         // login with not-refreshable config
108         try {
109             new LoginContext("NotRefreshable", handler).login();
110             throw new RuntimeException("Expected exception not thrown");
111         } catch (LoginException le) {
112             System.out.println("Expected login failure: " + le);
113         }
114 
115         // login with refreshable config
116         new LoginContext("Refreshable", handler).login();
117 
118         System.out.println("Test passed");
119     }
120 }
121