jdk/src/share/classes/sun/security/jgss/LoginConfigImpl.java
author weijun
Tue, 07 May 2013 12:30:36 +0800
changeset 17435 ec797e955dca
parent 15649 f6bd3d34f844
child 23010 6dadb192ad81
permissions -rw-r--r--
8010192: Enable native JGSS provider on Mac Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2942
diff changeset
     2
 * Copyright (c) 2005, 2009, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2942
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2942
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2942
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2942
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2942
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.jgss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.security.auth.login.AppConfigurationEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.security.auth.login.Configuration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import org.ietf.jgss.Oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * A Configuration implementation especially designed for JGSS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author weijun.wang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class LoginConfigImpl extends Configuration {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private final Configuration config;
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
    42
    private final GSSCaller caller;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private final String mechName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static final sun.security.util.Debug debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        sun.security.util.Debug.getInstance("gssloginconfig", "\t[GSS LoginConfigImpl]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * A new instance of LoginConfigImpl must be created for each login request
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * since it's only used by a single (caller, mech) pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * @param caller defined in GSSUtil as CALLER_XXX final fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * @param oid defined in GSSUtil as XXX_MECH_OID final fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
    53
    public LoginConfigImpl(GSSCaller caller, Oid mech) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        this.caller = caller;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (mech.equals(GSSUtil.GSS_KRB5_MECH_OID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            mechName = "krb5";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            throw new IllegalArgumentException(mech.toString() + " not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        config = java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                (new java.security.PrivilegedAction <Configuration> () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            public Configuration run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                return Configuration.getConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @param name Almost useless, since the (caller, mech) is already passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *             into constructor. The only use will be detecting OTHER which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *             is called in LoginContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        AppConfigurationEntry[] entries = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // This is the second call from LoginContext, which we will just ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if ("OTHER".equalsIgnoreCase(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        String[] alts = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        // Compatibility:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // For the 4 old callers, old entry names will be used if the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // entry name is not provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if ("krb5".equals(mechName)) {
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
    91
            if (caller == GSSCaller.CALLER_INITIATE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                alts = new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    "com.sun.security.jgss.krb5.initiate",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    "com.sun.security.jgss.initiate",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                };
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
    96
            } else if (caller == GSSCaller.CALLER_ACCEPT) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                alts = new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    "com.sun.security.jgss.krb5.accept",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    "com.sun.security.jgss.accept",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                };
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   101
            } else if (caller == GSSCaller.CALLER_SSL_CLIENT) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                alts = new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    "com.sun.security.jgss.krb5.initiate",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    "com.sun.net.ssl.client",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                };
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   106
            } else if (caller == GSSCaller.CALLER_SSL_SERVER) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                alts = new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    "com.sun.security.jgss.krb5.accept",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    "com.sun.net.ssl.server",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                };
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   111
            } else if (caller instanceof HttpCaller) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                alts = new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    "com.sun.security.jgss.krb5.initiate",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                };
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   115
            } else if (caller == GSSCaller.CALLER_UNKNOWN) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                throw new AssertionError("caller not defined");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            throw new IllegalArgumentException(mechName + " not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            // No other mech at the moment, maybe --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            switch (caller) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            case GSSUtil.CALLER_INITIATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            case GSSUtil.CALLER_SSL_CLIENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            case GSSUtil.CALLER_HTTP_NEGOTIATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                alts = new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    "com.sun.security.jgss." + mechName + ".initiate",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            case GSSUtil.CALLER_ACCEPT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            case GSSUtil.CALLER_SSL_SERVER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                alts = new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    "com.sun.security.jgss." + mechName + ".accept",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            case GSSUtil.CALLER_UNKNOWN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                // should never use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                throw new AssertionError("caller cannot be unknown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                throw new AssertionError("caller not defined");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        for (String alt: alts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            entries = config.getAppConfigurationEntry(alt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                debug.println("Trying " + alt +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        ((entries == null)?": does not exist.":": Found!"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (entries != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (entries == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                debug.println("Cannot read JGSS entry, use default values instead.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            entries = getDefaultConfigurationEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return entries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Default value for a caller-mech pair when no entry is defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * the system-wide Configuration object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private AppConfigurationEntry[] getDefaultConfigurationEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        HashMap <String, String> options = new HashMap <String, String> (2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (mechName == null || mechName.equals("krb5")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (isServerSide(caller)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                // Assuming the keytab file can be found through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                // krb5 config file or under user home directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                options.put("useKeyTab", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                options.put("storeKey", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                options.put("doNotPrompt", "true");
15649
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 5506
diff changeset
   178
                options.put("principal", "*");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                options.put("isInitiator", "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                options.put("useTicketCache", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                options.put("doNotPrompt", "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            return new AppConfigurationEntry[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                new AppConfigurationEntry(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        "com.sun.security.auth.module.Krb5LoginModule",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                        AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                        options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   194
    private static boolean isServerSide (GSSCaller caller) {
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   195
        return GSSCaller.CALLER_ACCEPT == caller ||
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   196
               GSSCaller.CALLER_SSL_SERVER == caller;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
}