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