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