1 /*
2  * Copyright (c) 2011, 2019, 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 /*
25  * @test
26  * @bug 7032354
27  * @library /test/lib
28  * @run main/othervm NoAddresses setup
29  * @run main/othervm -Djdk.net.hosts.file=TestHosts NoAddresses 1
30  * @run main/othervm -Djdk.net.hosts.file=TestHosts NoAddresses 2
31  * @run main/othervm/fail -Djdk.net.hosts.file=TestHosts NoAddresses 3
32  * @summary no-addresses should not be used on acceptor side
33  */
34 
35 import java.net.InetAddress;
36 import org.ietf.jgss.ChannelBinding;
37 import sun.security.jgss.GSSUtil;
38 import sun.security.krb5.Config;
39 import java.io.PrintWriter;
40 import java.io.FileWriter;
41 import java.io.BufferedWriter;
42 import java.nio.file.*;
43 
44 public class NoAddresses {
45 
main(String[] args)46     public static void main(String[] args)
47             throws Exception {
48 
49         if (args[0].equalsIgnoreCase("setup")) {
50             // add a mapping of test host name to 127.0.0.1 to test's hosts file
51             InetAddress localHost = InetAddress.getLocalHost();
52             String localHostName = localHost.getHostName();
53             String hostsFileName = System.getProperty("test.src", ".") + "/TestHosts";
54             String hostsFileNameLocal = "TestHosts";
55             String loopBackAddress = "127.0.0.1";
56             Files.copy(Paths.get(hostsFileName), Paths.get(hostsFileNameLocal));
57             addMappingToHostsFile(localHostName, loopBackAddress, hostsFileNameLocal, true);
58         } else {
59         OneKDC kdc = new OneKDC(null);
60         kdc.writeJAASConf();
61         KDC.saveConfig(OneKDC.KRB5_CONF, kdc,
62                 "noaddresses = false",
63                 "default_keytab_name = " + OneKDC.KTAB);
64         Config.refresh();
65 
66         Context c = Context.fromJAAS("client");
67         Context s = Context.fromJAAS("server");
68 
69         c.startAsClient(OneKDC.SERVER, GSSUtil.GSS_KRB5_MECH_OID);
70         s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID);
71 
72         InetAddress initiator = InetAddress.getLocalHost();
73         InetAddress acceptor = InetAddress.getLocalHost();
74         switch (args[0]) {
75             case "1":
76                 // no initiator host address available, should be OK
77                 break;
78             case "2":
79                 // correct initiator host address, still fine
80                 c.x().setChannelBinding(
81                         new ChannelBinding(initiator, acceptor, null));
82                 s.x().setChannelBinding(
83                         new ChannelBinding(initiator, acceptor, null));
84                 break;
85             case "3":
86                 // incorrect initiator host address, fail
87                 initiator = InetAddress.getByAddress(new byte[]{1,1,1,1});
88                 c.x().setChannelBinding(
89                         new ChannelBinding(initiator, acceptor, null));
90                 s.x().setChannelBinding(
91                         new ChannelBinding(initiator, acceptor, null));
92                 break;
93         }
94 
95         Context.handshake(c, s);
96     }
97 }
98 
addMappingToHostsFile(String host, String addr, String hostsFileName, boolean append)99     private static void addMappingToHostsFile (String host,
100                                                String addr,
101                                                String hostsFileName,
102                                                boolean append)
103                                              throws Exception {
104         String mapping = addr + " " + host;
105         try (PrintWriter hfPWriter = new PrintWriter(new BufferedWriter(
106                 new FileWriter(hostsFileName, append)))) {
107             hfPWriter.println(mapping);
108         }
109     }
110 }
111