1/*
2 * Copyright (c) 2012, 2013, 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 8005447
27 * @summary default principal can act as anyone
28 * @compile -XDignore.symbol.file TwoOrThree.java
29 * @run main/othervm TwoOrThree first first
30 * @run main/othervm/fail TwoOrThree first second
31 * @run main/othervm TwoOrThree - first
32 * @run main/othervm TwoOrThree - second
33 * @run main/othervm/fail TwoOrThree - third
34 */
35
36import java.nio.file.Files;
37import java.nio.file.Paths;
38import java.nio.file.StandardOpenOption;
39import javax.security.auth.Subject;
40import sun.security.jgss.GSSUtil;
41
42/*
43 * The JAAS login has two krb5 modules
44 *   1. principal is A
45 *   2. principal is B
46 * A named principal can only accept itself. The default principal can accept
47 * either, but not any other service even if the keytab also include its keys.
48 */
49public class TwoOrThree {
50
51    public static void main(String[] args) throws Exception {
52
53        String server = args[0].equals("-") ? null : args[0];
54        String target = args[1];
55        OneKDC kdc = new OneKDC(null);
56        kdc.addPrincipal("first", "first".toCharArray());
57        kdc.addPrincipal("second", "second".toCharArray());
58        kdc.addPrincipal("third", "third".toCharArray());
59        kdc.writeKtab(OneKDC.KTAB);
60
61        Context c = Context.fromUserPass(OneKDC.USER, OneKDC.PASS, false);
62
63        // Using keytabs
64        Subject sub4s = new Subject();
65        Context.fromUserKtab(sub4s, "first", OneKDC.KTAB, true);
66        Context s = Context.fromUserKtab(sub4s, "second", OneKDC.KTAB, true);
67        c.startAsClient(target, GSSUtil.GSS_KRB5_MECH_OID);
68        s.startAsServer(server, GSSUtil.GSS_KRB5_MECH_OID);
69        Context.handshake(c, s);
70
71        // Using keys
72        sub4s = new Subject();
73        Context.fromUserPass(sub4s, "first", "first".toCharArray(), true);
74        s = Context.fromUserPass(sub4s, "second", "second".toCharArray(), true);
75        c.startAsClient(target, GSSUtil.GSS_KRB5_MECH_OID);
76        s.startAsServer(server, GSSUtil.GSS_KRB5_MECH_OID);
77        Context.handshake(c, s);
78
79        s.dispose();
80        c.dispose();
81    }
82}
83