1/*
2 * Copyright (c) 2015, 2017, 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 8058290
27 * @summary JAAS Krb5LoginModule has suspect ticket-renewal logic,
28 *          relies on clockskew grace
29 * @compile -XDignore.symbol.file Renew.java
30 * @run main/othervm Renew 1
31 * @run main/othervm Renew 2
32 * @run main/othervm Renew 3
33 */
34
35import sun.security.krb5.Config;
36
37import java.nio.file.Files;
38import java.nio.file.Paths;
39import java.util.Arrays;
40import java.util.Date;
41import javax.security.auth.kerberos.KerberosTicket;
42
43public class Renew {
44
45    public static void main(String[] args) throws Exception {
46
47        // Three test cases:
48        // 1. renewTGT=false
49        // 2. renewTGT=true with a short life time, renew will happen
50        // 3. renewTGT=true with a long life time, renew won't happen
51        int test = Integer.parseInt(args[0]);
52
53        OneKDC k = new OneKDC(null);
54        KDC.saveConfig(OneKDC.KRB5_CONF, k,
55                "renew_lifetime = 1d",
56                "ticket_lifetime = " + (test == 2? "10s": "8h"));
57        Config.refresh();
58        k.writeJAASConf();
59
60        // KDC would save ccache in a file
61        System.setProperty("test.kdc.save.ccache", "cache.here");
62
63        Files.write(Paths.get(OneKDC.JAAS_CONF), Arrays.asList(
64                "first {",
65                "   com.sun.security.auth.module.Krb5LoginModule required;",
66                "};",
67                "second {",
68                "   com.sun.security.auth.module.Krb5LoginModule required",
69                "   doNotPrompt=true",
70                "   renewTGT=" + (test != 1),
71                "   useTicketCache=true",
72                "   ticketCache=cache.here;",
73                "};"
74        ));
75
76        Context c;
77
78        // The first login uses username and password
79        c = Context.fromUserPass(OneKDC.USER, OneKDC.PASS, false);
80        Date d1 = c.s().getPrivateCredentials(KerberosTicket.class).iterator().next().getAuthTime();
81
82        // 6s is longer than half of 10s
83        Thread.sleep(6000);
84
85        // The second login uses the cache
86        c = Context.fromJAAS("second");
87        Date d2 = c.s().getPrivateCredentials(KerberosTicket.class).iterator().next().getAuthTime();
88
89        if (test == 2) {
90            if (d1.equals(d2)) {
91                throw new Exception("Ticket not renewed");
92            }
93        } else {
94            if (!d1.equals(d2)) {
95                throw new Exception("Ticket renewed");
96            }
97        }
98    }
99}
100