Path: blob/master/src/java.security.jgss/share/classes/sun/security/jgss/LoginConfigImpl.java
67696 views
/*1* Copyright (c) 2005, 2021, 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*/63@SuppressWarnings("removal")64public LoginConfigImpl(GSSCaller caller, Oid mech) {6566this.caller = caller;6768if (mech.equals(GSSUtil.GSS_KRB5_MECH_OID)) {69mechName = "krb5";70} else {71throw new IllegalArgumentException(mech.toString() + " not supported");72}73config = java.security.AccessController.doPrivileged74(new java.security.PrivilegedAction <Configuration> () {75public Configuration run() {76return Configuration.getConfiguration();77}78});79}8081/**82* @param name Almost useless, since the (caller, mech) is already passed83* into constructor. The only use will be detecting OTHER which84* is called in LoginContext85*/86public AppConfigurationEntry[] getAppConfigurationEntry(String name) {8788AppConfigurationEntry[] entries = null;8990// This is the second call from LoginContext, which we will just ignore91if ("OTHER".equalsIgnoreCase(name)) {92return null;93}9495String[] alts = null;9697// Compatibility:98// For the 4 old callers, old entry names will be used if the new99// entry name is not provided.100101if ("krb5".equals(mechName)) {102if (caller == GSSCaller.CALLER_INITIATE) {103alts = new String[] {104"com.sun.security.jgss.krb5.initiate",105"com.sun.security.jgss.initiate",106};107} else if (caller == GSSCaller.CALLER_ACCEPT) {108alts = new String[] {109"com.sun.security.jgss.krb5.accept",110"com.sun.security.jgss.accept",111};112} else if (caller instanceof HttpCaller) {113alts = new String[] {114"com.sun.security.jgss.krb5.initiate",115};116} else if (caller == GSSCaller.CALLER_UNKNOWN) {117throw new AssertionError("caller not defined");118}119} else {120throw new IllegalArgumentException(mechName + " not supported");121// No other mech at the moment, maybe --122/*123switch (caller) {124case GSSUtil.CALLER_INITIATE:125case GSSUtil.CALLER_HTTP_NEGOTIATE:126alts = new String[] {127"com.sun.security.jgss." + mechName + ".initiate",128};129break;130case GSSUtil.CALLER_ACCEPT:131alts = new String[] {132"com.sun.security.jgss." + mechName + ".accept",133};134break;135case GSSUtil.CALLER_UNKNOWN:136// should never use137throw new AssertionError("caller cannot be unknown");138default:139throw new AssertionError("caller not defined");140}141*/142}143for (String alt: alts) {144entries = config.getAppConfigurationEntry(alt);145if (debug != null) {146debug.println("Trying " + alt +147((entries == null)?": does not exist.":": Found!"));148}149if (entries != null) {150break;151}152}153154if (entries == null) {155if (debug != null) {156debug.println("Cannot read JGSS entry, use default values instead.");157}158entries = getDefaultConfigurationEntry();159}160return entries;161}162163/**164* Default value for a caller-mech pair when no entry is defined in165* the system-wide Configuration object.166*/167private AppConfigurationEntry[] getDefaultConfigurationEntry() {168HashMap <String, String> options = new HashMap <String, String> (2);169170if (mechName == null || mechName.equals("krb5")) {171if (isServerSide(caller)) {172// Assuming the keytab file can be found through173// krb5 config file or under user home directory174options.put("useKeyTab", "true");175options.put("storeKey", "true");176options.put("doNotPrompt", "true");177options.put("principal", "*");178options.put("isInitiator", "false");179} else {180if (caller instanceof HttpCaller && !HTTP_USE_GLOBAL_CREDS) {181options.put("useTicketCache", "false");182} else {183options.put("useTicketCache", "true");184}185options.put("doNotPrompt", "false");186}187return new AppConfigurationEntry[] {188new AppConfigurationEntry(189"com.sun.security.auth.module.Krb5LoginModule",190AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,191options)192};193}194return null;195}196197private static boolean isServerSide (GSSCaller caller) {198return GSSCaller.CALLER_ACCEPT == caller;199}200}201202203