Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/jgss/LoginConfigImpl.java
38830 views
/*1* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.jgss;2627import java.util.HashMap;28import javax.security.auth.login.AppConfigurationEntry;29import javax.security.auth.login.Configuration;30import org.ietf.jgss.Oid;31import sun.security.action.GetPropertyAction;3233/**34* A Configuration implementation especially designed for JGSS.35*36* @author weijun.wang37* @since 1.638*/39public class LoginConfigImpl extends Configuration {4041private final Configuration config;42private final GSSCaller caller;43private final String mechName;44private static final sun.security.util.Debug debug =45sun.security.util.Debug.getInstance("gssloginconfig", "\t[GSS LoginConfigImpl]");4647public static final boolean HTTP_USE_GLOBAL_CREDS;4849static {50String prop = GetPropertyAction51.privilegedGetProperty("http.use.global.creds");52//HTTP_USE_GLOBAL_CREDS = "true".equalsIgnoreCase(prop); // default false53HTTP_USE_GLOBAL_CREDS = !"false".equalsIgnoreCase(prop); // default true54}555657/**58* A new instance of LoginConfigImpl must be created for each login request59* since it's only used by a single (caller, mech) pair60* @param caller defined in GSSUtil as CALLER_XXX final fields61* @param mech defined in GSSUtil as XXX_MECH_OID final fields62*/63public LoginConfigImpl(GSSCaller caller, Oid mech) {6465this.caller = caller;6667if (mech.equals(GSSUtil.GSS_KRB5_MECH_OID)) {68mechName = "krb5";69} else {70throw new IllegalArgumentException(mech.toString() + " not supported");71}72config = java.security.AccessController.doPrivileged73(new java.security.PrivilegedAction <Configuration> () {74public Configuration run() {75return Configuration.getConfiguration();76}77});78}7980/**81* @param name Almost useless, since the (caller, mech) is already passed82* into constructor. The only use will be detecting OTHER which83* is called in LoginContext84*/85public AppConfigurationEntry[] getAppConfigurationEntry(String name) {8687AppConfigurationEntry[] entries = null;8889// This is the second call from LoginContext, which we will just ignore90if ("OTHER".equalsIgnoreCase(name)) {91return null;92}9394String[] alts = null;9596// Compatibility:97// For the 4 old callers, old entry names will be used if the new98// entry name is not provided.99100if ("krb5".equals(mechName)) {101if (caller == GSSCaller.CALLER_INITIATE) {102alts = new String[] {103"com.sun.security.jgss.krb5.initiate",104"com.sun.security.jgss.initiate",105};106} else if (caller == GSSCaller.CALLER_ACCEPT) {107alts = new String[] {108"com.sun.security.jgss.krb5.accept",109"com.sun.security.jgss.accept",110};111} else if (caller == GSSCaller.CALLER_SSL_CLIENT) {112alts = new String[] {113"com.sun.security.jgss.krb5.initiate",114"com.sun.net.ssl.client",115};116} else if (caller == GSSCaller.CALLER_SSL_SERVER) {117alts = new String[] {118"com.sun.security.jgss.krb5.accept",119"com.sun.net.ssl.server",120};121} else if (caller instanceof HttpCaller) {122alts = new String[] {123"com.sun.security.jgss.krb5.initiate",124};125} else if (caller == GSSCaller.CALLER_UNKNOWN) {126throw new AssertionError("caller not defined");127}128} else {129throw new IllegalArgumentException(mechName + " not supported");130// No other mech at the moment, maybe --131/*132switch (caller) {133case GSSUtil.CALLER_INITIATE:134case GSSUtil.CALLER_SSL_CLIENT:135case GSSUtil.CALLER_HTTP_NEGOTIATE:136alts = new String[] {137"com.sun.security.jgss." + mechName + ".initiate",138};139break;140case GSSUtil.CALLER_ACCEPT:141case GSSUtil.CALLER_SSL_SERVER:142alts = new String[] {143"com.sun.security.jgss." + mechName + ".accept",144};145break;146case GSSUtil.CALLER_UNKNOWN:147// should never use148throw new AssertionError("caller cannot be unknown");149default:150throw new AssertionError("caller not defined");151}152*/153}154for (String alt: alts) {155entries = config.getAppConfigurationEntry(alt);156if (debug != null) {157debug.println("Trying " + alt +158((entries == null)?": does not exist.":": Found!"));159}160if (entries != null) {161break;162}163}164165if (entries == null) {166if (debug != null) {167debug.println("Cannot read JGSS entry, use default values instead.");168}169entries = getDefaultConfigurationEntry();170}171return entries;172}173174/**175* Default value for a caller-mech pair when no entry is defined in176* the system-wide Configuration object.177*/178private AppConfigurationEntry[] getDefaultConfigurationEntry() {179HashMap <String, String> options = new HashMap <String, String> (2);180181if (mechName == null || mechName.equals("krb5")) {182if (isServerSide(caller)) {183// Assuming the keytab file can be found through184// krb5 config file or under user home directory185options.put("useKeyTab", "true");186options.put("storeKey", "true");187options.put("doNotPrompt", "true");188options.put("principal", "*");189options.put("isInitiator", "false");190} else {191if (caller instanceof HttpCaller && !HTTP_USE_GLOBAL_CREDS) {192options.put("useTicketCache", "false");193} else {194options.put("useTicketCache", "true");195}196options.put("doNotPrompt", "false");197}198return new AppConfigurationEntry[] {199new AppConfigurationEntry(200"com.sun.security.auth.module.Krb5LoginModule",201AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,202options)203};204}205return null;206}207208private static boolean isServerSide (GSSCaller caller) {209return GSSCaller.CALLER_ACCEPT == caller ||210GSSCaller.CALLER_SSL_SERVER == caller;211}212}213214215