1 /*
   2  * Copyright (c) 2008, 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 6716534
  27  * @modules java.base/sun.net.spi.nameservice
  28  *          java.base/sun.security.util
  29  *          java.security.jgss/sun.security.krb5
  30  *          java.security.jgss/sun.security.krb5.internal
  31  *          java.security.jgss/sun.security.krb5.internal.ccache
  32  *          java.security.jgss/sun.security.krb5.internal.crypto
  33  *          java.security.jgss/sun.security.krb5.internal.ktab
  34  * @compile -XDignore.symbol.file CleanState.java
  35  * @run main/othervm CleanState
  36  * @summary Krb5LoginModule has not cleaned temp info between authentication attempts
  37  */
  38 import com.sun.security.auth.module.Krb5LoginModule;
  39 import java.util.HashMap;
  40 import java.util.Map;
  41 import javax.security.auth.Subject;
  42 import javax.security.auth.callback.Callback;
  43 import javax.security.auth.callback.CallbackHandler;
  44 import javax.security.auth.callback.NameCallback;
  45 import javax.security.auth.callback.PasswordCallback;
  46 
  47 public class CleanState {
  48     public static void main(String[] args) throws Exception {
  49         CleanState x = new CleanState();
  50         new OneKDC(null);
  51         x.go();
  52     }
  53 
  54     void go() throws Exception {
  55         Krb5LoginModule krb5 = new Krb5LoginModule();
  56 
  57         final String name = OneKDC.USER;
  58         final char[] password = OneKDC.PASS;
  59         char[] badpassword = "hellokitty".toCharArray();
  60 
  61         Map<String,String> map = new HashMap<>();
  62         map.put("useTicketCache", "false");
  63         map.put("doNotPrompt", "false");
  64         map.put("tryFirstPass", "true");
  65         Map<String,Object> shared = new HashMap<>();
  66         shared.put("javax.security.auth.login.name", name);
  67         shared.put("javax.security.auth.login.password", badpassword);
  68 
  69         krb5.initialize(new Subject(), new CallbackHandler() {
  70             @Override
  71             public void handle(Callback[] callbacks) {
  72                 for(Callback callback: callbacks) {
  73                     if (callback instanceof NameCallback) {
  74                         ((NameCallback)callback).setName(name);
  75                     }
  76                     if (callback instanceof PasswordCallback) {
  77                         ((PasswordCallback)callback).setPassword(password);
  78                     }
  79                 }
  80             }
  81         }, shared, map);
  82         krb5.login();
  83     }
  84 }