jdk/src/java.management/share/classes/com/sun/jmx/remote/security/JMXPluggableAuthenticator.java
author chegar
Wed, 03 Dec 2014 14:22:58 +0000
changeset 27565 729f9700483a
parent 25859 3317bb8137f4
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.remote.JMXPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.remote.JMXAuthenticator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.security.auth.AuthPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.security.auth.callback.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.security.auth.login.AppConfigurationEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.security.auth.login.Configuration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.security.auth.login.LoginContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.security.auth.login.LoginException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.security.auth.spi.LoginModule;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>This class represents a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <a href="{@docRoot}/../guide/security/jaas/JAASRefGuide.html">JAAS</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * based implementation of the {@link JMXAuthenticator} interface.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>Authentication is performed by passing the supplied user's credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * to one or more authentication mechanisms ({@link LoginModule}) for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * verification. An authentication mechanism acquires the user's credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * by calling {@link NameCallback} and/or {@link PasswordCallback}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * If authentication is successful then an authenticated {@link Subject}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * filled in with a {@link Principal} is returned.  Authorization checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * will then be performed based on this <code>Subject</code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <p>By default, a single file-based authentication mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * {@link FileLoginModule} is configured (<code>FileLoginConfig</code>).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <p>To override the default configuration use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <code>com.sun.management.jmxremote.login.config</code> management property
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    69
 * described in the JRE/conf/management/management.properties file.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * Set this property to the name of a JAAS configuration entry and ensure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * the entry is loaded by the installed {@link Configuration}. In addition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * ensure that the authentication mechanisms specified in the entry acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * the user's credentials by calling {@link NameCallback} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * {@link PasswordCallback} and that they return a {@link Subject} filled-in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * with a {@link Principal}, for those users that are successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * authenticated.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
public final class JMXPluggableAuthenticator implements JMXAuthenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Creates an instance of <code>JMXPluggableAuthenticator</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * and initializes it with a {@link LoginContext}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param env the environment containing configuration properties for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *            authenticator. Can be null, which is equivalent to an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *            Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @exception SecurityException if the authentication mechanism cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *            initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
    90
    public JMXPluggableAuthenticator(Map<?, ?> env) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        String loginConfigName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        String passwordFile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (env != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            loginConfigName = (String) env.get(LOGIN_CONFIG_PROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            passwordFile = (String) env.get(PASSWORD_FILE_PROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if (loginConfigName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                // use the supplied JAAS login configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                loginContext =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    new LoginContext(loginConfigName, new JMXCallbackHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                // use the default JAAS login configuration (file-based)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    sm.checkPermission(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                            new AuthPermission("createLoginContext." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                               LOGIN_CONFIG_NAME));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                final String pf = passwordFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    loginContext = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        new PrivilegedExceptionAction<LoginContext>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                            public LoginContext run() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                return new LoginContext(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                                LOGIN_CONFIG_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                                null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                                new JMXCallbackHandler(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                                new FileLoginConfig(pf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                } catch (PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    throw (LoginException) pae.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } catch (LoginException le) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            authenticationFailure("authenticate", le);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            authenticationFailure("authenticate", se);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Authenticate the <code>MBeanServerConnection</code> client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * with the given client credentials.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param credentials the user-defined credentials to be passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * to the server in order to authenticate the user before creating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * the <code>MBeanServerConnection</code>.  This parameter must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * be a two-element <code>String[]</code> containing the client's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * username and password in that order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @return the authenticated subject containing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <code>JMXPrincipal(username)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @exception SecurityException if the server cannot authenticate the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * with the provided credentials.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public Subject authenticate(Object credentials) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        // Verify that credentials is of type String[].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (!(credentials instanceof String[])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            // Special case for null so we get a more informative message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (credentials == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                authenticationFailure("authenticate", "Credentials required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            final String message =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                "Credentials should be String[] instead of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                 credentials.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            authenticationFailure("authenticate", message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        // Verify that the array contains two elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        final String[] aCredentials = (String[]) credentials;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (aCredentials.length != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            final String message =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                "Credentials should have 2 elements not " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                aCredentials.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            authenticationFailure("authenticate", message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // Verify that username exists and the associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // password matches the one supplied by the client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        username = aCredentials[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        password = aCredentials[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (username == null || password == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            final String message = "Username or password is null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            authenticationFailure("authenticate", message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // Perform authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            loginContext.login();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            final Subject subject = loginContext.getSubject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            AccessController.doPrivileged(new PrivilegedAction<Void>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                        subject.setReadOnly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        } catch (LoginException le) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            authenticationFailure("authenticate", le);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    private static void authenticationFailure(String method, String message)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        final String msg = "Authentication failed! " + message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        final SecurityException e = new SecurityException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        logException(method, msg, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    private static void authenticationFailure(String method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                              Exception exception)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        String msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        SecurityException se;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (exception instanceof SecurityException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            msg = exception.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            se = (SecurityException) exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            msg = "Authentication failed! " + exception.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            final SecurityException e = new SecurityException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            EnvHelp.initCause(e, exception);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            se = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        logException(method, msg, se);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        throw se;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    private static void logException(String method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                     String message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                     Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (logger.traceOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            logger.trace(method, message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if (logger.debugOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            logger.debug(method, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    private LoginContext loginContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    private String username;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    private String password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    private static final String LOGIN_CONFIG_PROP =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        "jmx.remote.x.login.config";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    private static final String LOGIN_CONFIG_NAME = "JMXPluggableAuthenticator";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    private static final String PASSWORD_FILE_PROP =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        "jmx.remote.x.password.file";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    private static final ClassLogger logger =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        new ClassLogger("javax.management.remote.misc", LOGIN_CONFIG_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 * This callback handler supplies the username and password (which was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 * originally supplied by the JMX user) to the JAAS login module performing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 * the authentication. No interactive user prompting is required because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 * credentials are already available to this class (via its enclosing class).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
private final class JMXCallbackHandler implements CallbackHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Sets the username and password in the appropriate Callback object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public void handle(Callback[] callbacks)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        throws IOException, UnsupportedCallbackException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        for (int i = 0; i < callbacks.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            if (callbacks[i] instanceof NameCallback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                ((NameCallback)callbacks[i]).setName(username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            } else if (callbacks[i] instanceof PasswordCallback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                ((PasswordCallback)callbacks[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    .setPassword(password.toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                throw new UnsupportedCallbackException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    (callbacks[i], "Unrecognized Callback");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
 * This class defines the JAAS configuration for file-based authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
 * It is equivalent to the following textual configuration entry:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
 *     JMXPluggableAuthenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
 *         com.sun.jmx.remote.security.FileLoginModule required;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
 *     };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
private static class FileLoginConfig extends Configuration {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    // The JAAS configuration for file-based authentication
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   298
    private AppConfigurationEntry[] entries;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    // The classname of the login module for file-based authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    private static final String FILE_LOGIN_MODULE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        FileLoginModule.class.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    // The option that identifies the password file to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    private static final String PASSWORD_FILE_OPTION = "passwordFile";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Creates an instance of <code>FileLoginConfig</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @param passwordFile A filepath that identifies the password file to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *                     If null then the default password file is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public FileLoginConfig(String passwordFile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        Map<String, String> options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (passwordFile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            options = new HashMap<String, String>(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            options.put(PASSWORD_FILE_OPTION, passwordFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            options = Collections.emptyMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        entries = new AppConfigurationEntry[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            new AppConfigurationEntry(FILE_LOGIN_MODULE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Gets the JAAS configuration for file-based authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return name.equals(LOGIN_CONFIG_NAME) ? entries : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Refreshes the configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public void refresh() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        // the configuration is fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
}