jdk/src/java.management/share/classes/com/sun/jmx/remote/security/FileLoginModule.java
author chegar
Wed, 03 Dec 2014 14:22:58 +0000
changeset 27565 729f9700483a
parent 25859 3317bb8137f4
child 43494 11801b2ff456
permissions -rw-r--r--
8049367: Modular Run-Time Images Reviewed-by: chegar, dfuchs, ihse, joehw, mullan, psandoz, wetmore Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, bradford.wetmore@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, james.laskey@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com, sundararajan.athijegannathan@oracle.com
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: 1510
diff changeset
     2
 * Copyright (c) 2004, 2008, 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: 1510
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: 1510
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: 1510
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1510
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1510
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 com.sun.jmx.remote.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
    28
import com.sun.jmx.mbeanserver.GetPropertyAction;
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1004
diff changeset
    29
import com.sun.jmx.mbeanserver.Util;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.FilePermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.AccessControlException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.security.auth.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.security.auth.callback.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.security.auth.login.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.security.auth.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.remote.JMXPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import com.sun.jmx.remote.util.ClassLogger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import com.sun.jmx.remote.util.EnvHelp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.management.jmxremote.ConnectorBootstrap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * This {@link LoginModule} performs file-based authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p> A supplied username and password is verified against the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * corresponding user credentials stored in a designated password file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * If successful then a new {@link JMXPrincipal} is created with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * user's name and it is associated with the current {@link Subject}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Such principals may be identified and granted management privileges in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * the access control file for JMX remote management or in a Java security
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p> The password file comprises a list of key-value pairs as specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * {@link Properties}. The key represents a user's name and the value is its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * associated cleartext password. By default, the following password file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * used:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <pre>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    68
 *     ${java.home}/conf/management/jmxremote.password
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * A different password file can be specified via the <code>passwordFile</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * configuration option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p> This module recognizes the following <code>Configuration</code> options:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <dt> <code>passwordFile</code> </dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <dd> the path to an alternative password file. It is used instead of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *      the default password file.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <dt> <code>useFirstPass</code> </dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <dd> if <code>true</code>, this module retrieves the username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *      from the module's shared state, using "javax.security.auth.login.name"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *      and "javax.security.auth.login.password" as the respective keys. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *      retrieved values are used for authentication. If authentication fails,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *      no attempt for a retry is made, and the failure is reported back to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *      the calling application.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <dt> <code>tryFirstPass</code> </dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <dd> if <code>true</code>, this module retrieves the username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *      from the module's shared state, using "javax.security.auth.login.name"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *       and "javax.security.auth.login.password" as the respective keys.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *      retrieved values are used for authentication. If authentication fails,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *      the module uses the CallbackHandler to retrieve a new username and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *      password, and another attempt to authenticate is made. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *      authentication fails, the failure is reported back to the calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *      application.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <dt> <code>storePass</code> </dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * <dd> if <code>true</code>, this module stores the username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *      obtained from the CallbackHandler in the module's shared state, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *      "javax.security.auth.login.name" and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *      "javax.security.auth.login.password" as the respective keys.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *      not performed if existing values already exist for the username and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *      password in the shared state, or if authentication fails.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <dt> <code>clearPass</code> </dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * <dd> if <code>true</code>, this module clears the username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *      stored in the module's shared state after both phases of authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *      (login and commit) have completed.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
public class FileLoginModule implements LoginModule {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    // Location of the default password file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private static final String DEFAULT_PASSWORD_FILE_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        AccessController.doPrivileged(new GetPropertyAction("java.home")) +
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   116
        File.separatorChar + "conf" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        File.separatorChar + "management" + File.separatorChar +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        ConnectorBootstrap.DefaultValues.PASSWORD_FILE_NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    // Key to retrieve the stored username
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private static final String USERNAME_KEY =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        "javax.security.auth.login.name";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    // Key to retrieve the stored password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private static final String PASSWORD_KEY =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        "javax.security.auth.login.password";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    // Log messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private static final ClassLogger logger =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        new ClassLogger("javax.management.remote.misc", "FileLoginModule");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    // Configurable options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private boolean useFirstPass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private boolean tryFirstPass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private boolean storePass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private boolean clearPass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    // Authentication status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private boolean succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private boolean commitSucceeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    // Supplied username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private String username;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private char[] password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private JMXPrincipal user;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    // Initial state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private Subject subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private CallbackHandler callbackHandler;
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1004
diff changeset
   150
    private Map<String, Object> sharedState;
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1004
diff changeset
   151
    private Map<String, ?> options;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private String passwordFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private String passwordFileDisplayName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private boolean userSuppliedPasswordFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private boolean hasJavaHomePermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private Properties userCredentials;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Initialize this <code>LoginModule</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param subject the <code>Subject</code> to be authenticated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param callbackHandler a <code>CallbackHandler</code> to acquire the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *                  user's name and password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @param sharedState shared <code>LoginModule</code> state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param options options specified in the login
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *                  <code>Configuration</code> for this particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *                  <code>LoginModule</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public void initialize(Subject subject, CallbackHandler callbackHandler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                           Map<String,?> sharedState,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                           Map<String,?> options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        this.subject = subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        this.callbackHandler = callbackHandler;
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1004
diff changeset
   176
        this.sharedState = Util.cast(sharedState);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        this.options = options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // initialize any configured options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        tryFirstPass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                "true".equalsIgnoreCase((String)options.get("tryFirstPass"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        useFirstPass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                "true".equalsIgnoreCase((String)options.get("useFirstPass"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        storePass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                "true".equalsIgnoreCase((String)options.get("storePass"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        clearPass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                "true".equalsIgnoreCase((String)options.get("clearPass"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        passwordFile = (String)options.get("passwordFile");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        passwordFileDisplayName = passwordFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        userSuppliedPasswordFile = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        // set the location of the password file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (passwordFile == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            passwordFile = DEFAULT_PASSWORD_FILE_NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            userSuppliedPasswordFile = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                System.getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                hasJavaHomePermission = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                passwordFileDisplayName = passwordFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                hasJavaHomePermission = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                passwordFileDisplayName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                        ConnectorBootstrap.DefaultValues.PASSWORD_FILE_NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Begin user authentication (Authentication Phase 1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * <p> Acquire the user's name and password and verify them against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * the corresponding credentials from the password file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @return true always, since this <code>LoginModule</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *          should not be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @exception FailedLoginException if the authentication fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @exception LoginException if this <code>LoginModule</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *          is unable to perform the authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public boolean login() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            loadPasswordFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            LoginException le = new LoginException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    "Error: unable to load the password file: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    passwordFileDisplayName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            throw EnvHelp.initCause(le, ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (userCredentials == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                ("Error: unable to locate the users' credentials.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (logger.debugOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            logger.debug("login",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    "Using password file: " + passwordFileDisplayName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        // attempt the authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (tryFirstPass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                // attempt the authentication by getting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                // username and password from shared state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                attemptAuthentication(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                // authentication succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                succeeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                if (logger.debugOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    logger.debug("login",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                        "Authentication using cached password has succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            } catch (LoginException le) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                // authentication failed -- try again below by prompting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                logger.debug("login",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    "Authentication using cached password has failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        } else if (useFirstPass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                // attempt the authentication by getting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                // username and password from shared state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                attemptAuthentication(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                // authentication succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                succeeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                if (logger.debugOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    logger.debug("login",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                        "Authentication using cached password has succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            } catch (LoginException le) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                // authentication failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                logger.debug("login",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    "Authentication using cached password has failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (logger.debugOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            logger.debug("login", "Acquiring password");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        // attempt the authentication using the supplied username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            attemptAuthentication(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            // authentication succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            succeeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            if (logger.debugOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                logger.debug("login", "Authentication has succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        } catch (LoginException le) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            logger.debug("login", "Authentication has failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Complete user authentication (Authentication Phase 2).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * <p> This method is called if the LoginContext's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * overall authentication has succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * (all the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * LoginModules have succeeded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * <p> If this LoginModule's own authentication attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * succeeded (checked by retrieving the private state saved by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * <code>login</code> method), then this method associates a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * <code>JMXPrincipal</code> with the <code>Subject</code> located in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * <code>LoginModule</code>.  If this LoginModule's own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * authentication attempted failed, then this method removes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * any state that was originally saved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @exception LoginException if the commit fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @return true if this LoginModule's own login and commit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *          attempts succeeded, or false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public boolean commit() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (succeeded == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            if (subject.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                throw new LoginException("Subject is read-only");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            // add Principals to the Subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            if (!subject.getPrincipals().contains(user)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                subject.getPrincipals().add(user);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (logger.debugOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                logger.debug("commit",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    "Authentication has completed successfully");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        // in any case, clean out state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        commitSucceeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Abort user authentication (Authentication Phase 2).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * <p> This method is called if the LoginContext's overall authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * failed (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * LoginModules did not succeed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * <p> If this LoginModule's own authentication attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * succeeded (checked by retrieving the private state saved by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * <code>login</code> and <code>commit</code> methods),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * then this method cleans up any state that was originally saved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @exception LoginException if the abort fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @return false if this LoginModule's own login and/or commit attempts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *          failed, and true otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    public boolean abort() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (logger.debugOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            logger.debug("abort",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                "Authentication has not completed successfully");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (succeeded == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        } else if (succeeded == true && commitSucceeded == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            // Clean out state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            user = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            // overall authentication succeeded and commit succeeded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            // but someone else's commit failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            logout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Logout a user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * <p> This method removes the Principals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * that were added by the <code>commit</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @exception LoginException if the logout fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @return true in all cases since this <code>LoginModule</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *          should not be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    public boolean logout() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (subject.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            throw new LoginException ("Subject is read-only");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        subject.getPrincipals().remove(user);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        // clean out state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        commitSucceeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        user = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        if (logger.debugOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            logger.debug("logout", "Subject is being logged out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * Attempt authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @param usePasswdFromSharedState a flag to tell this method whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *          to retrieve the password from the sharedState.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    @SuppressWarnings("unchecked")  // sharedState used as Map<String,Object>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    private void attemptAuthentication(boolean usePasswdFromSharedState)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        // get the username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        getUsernamePassword(usePasswdFromSharedState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   440
        String localPassword;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        // userCredentials is initialized in login()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        if (((localPassword = userCredentials.getProperty(username)) == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            (! localPassword.equals(new String(password)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            // username not found or passwords do not match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            if (logger.debugOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                logger.debug("login", "Invalid username or password");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            throw new FailedLoginException("Invalid username or password");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        // Save the username and password in the shared state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        // only if authentication succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        if (storePass &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            !sharedState.containsKey(USERNAME_KEY) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            !sharedState.containsKey(PASSWORD_KEY)) {
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1004
diff changeset
   458
            sharedState.put(USERNAME_KEY, username);
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1004
diff changeset
   459
            sharedState.put(PASSWORD_KEY, password);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        // Create a new user principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        user = new JMXPrincipal(username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        if (logger.debugOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            logger.debug("login",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                "User '" + username + "' successfully validated");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * Read the password file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    private void loadPasswordFile() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        FileInputStream fis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            fis = new FileInputStream(passwordFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            if (userSuppliedPasswordFile || hasJavaHomePermission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            } else {
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   482
                final FilePermission fp =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                        new FilePermission(passwordFileDisplayName, "read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                AccessControlException ace = new AccessControlException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                        "access denied " + fp.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                ace.setStackTrace(e.getStackTrace());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                throw ace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   490
        try {
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   491
            final BufferedInputStream bis = new BufferedInputStream(fis);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   492
            try {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   493
                userCredentials = new Properties();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   494
                userCredentials.load(bis);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   495
            } finally {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   496
                bis.close();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   497
            }
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   498
        } finally {
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   499
            fis.close();
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   500
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * Get the username and password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * This method does not return any value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * Instead, it sets global name and password variables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * <p> Also note that this method will set the username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * values in the shared state in case subsequent LoginModules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * want to use them via use/tryFirstPass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @param usePasswdFromSharedState boolean that tells this method whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *          to retrieve the password from the sharedState.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    private void getUsernamePassword(boolean usePasswdFromSharedState)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        if (usePasswdFromSharedState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            // use the password saved by the first module in the stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            username = (String)sharedState.get(USERNAME_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            password = (char[])sharedState.get(PASSWORD_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        // acquire username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        if (callbackHandler == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            throw new LoginException("Error: no CallbackHandler available " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                "to garner authentication information from the user");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        Callback[] callbacks = new Callback[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        callbacks[0] = new NameCallback("username");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        callbacks[1] = new PasswordCallback("password", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            callbackHandler.handle(callbacks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            username = ((NameCallback)callbacks[0]).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            char[] tmpPassword = ((PasswordCallback)callbacks[1]).getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            password = new char[tmpPassword.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            System.arraycopy(tmpPassword, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                                password, 0, tmpPassword.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            ((PasswordCallback)callbacks[1]).clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            LoginException le = new LoginException(ioe.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            throw EnvHelp.initCause(le, ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        } catch (UnsupportedCallbackException uce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            LoginException le = new LoginException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                                    "Error: " + uce.getCallback().toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                                    " not available to garner authentication " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                                    "information from the user");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            throw EnvHelp.initCause(le, uce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Clean out state because of a failed authentication attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    private void cleanState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        username = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        if (password != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            Arrays.fill(password, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            password = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        if (clearPass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            sharedState.remove(USERNAME_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            sharedState.remove(PASSWORD_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
}