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