src/jdk.security.auth/share/classes/com/sun/security/auth/module/LdapLoginModule.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 44853 jdk/src/jdk.security.auth/share/classes/com/sun/security/auth/module/LdapLoginModule.java@4c752de98ce4
child 48573 e6b173e04545
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
44853
4c752de98ce4 8178298: (LdapLoginModule)fix the JNDI properties technote
vtewari
parents: 43243
diff changeset
     2
 * Copyright (c) 2005, 2017, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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.security.auth.module;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.SocketPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.regex.Matcher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.regex.Pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.naming.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.naming.directory.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.naming.ldap.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.security.auth.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.security.auth.callback.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.security.auth.login.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.security.auth.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import com.sun.security.auth.LdapPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import com.sun.security.auth.UserPrincipal;
43243
a48dab17a356 8173024: Replace direct use of AuthResources resource bundle from jdk.security.auth
mchung
parents: 43198
diff changeset
    47
import static sun.security.util.ResourcesMgr.getAuthResourceString;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * This {@link LoginModule} performs LDAP-based authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * A username and password is verified against the corresponding user
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * credentials stored in an LDAP directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * This module requires the supplied {@link CallbackHandler} to support a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * {@link NameCallback} and a {@link PasswordCallback}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * If authentication is successful then a new {@link LdapPrincipal} is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * using the user's distinguished name and a new {@link UserPrincipal} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * created using the user's username and both are associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * with the current {@link Subject}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p> This module operates in one of three modes: <i>search-first</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <i>authentication-first</i> or <i>authentication-only</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * A mode is selected by specifying a particular set of options.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p> In search-first mode, the LDAP directory is searched to determine the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * user's distinguished name and then authentication is attempted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * An (anonymous) search is performed using the supplied username in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * conjunction with a specified search filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * If successful then authentication is attempted using the user's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * distinguished name and the supplied password.
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    71
 * To enable this mode, set the {@code userFilter} option and omit the
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    72
 * {@code authIdentity} option.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * Use search-first mode when the user's distinguished name is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * known in advance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p> In authentication-first mode, authentication is attempted using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * supplied username and password and then the LDAP directory is searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * If authentication is successful then a search is performed using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * supplied username in conjunction with a specified search filter.
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    80
 * To enable this mode, set the {@code authIdentity} and the
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    81
 * {@code userFilter} options.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * Use authentication-first mode when accessing an LDAP directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * that has been configured to disallow anonymous searches.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <p> In authentication-only mode, authentication is attempted using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * supplied username and password. The LDAP directory is not searched because
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * the user's distinguished name is already known.
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    88
 * To enable this mode, set the {@code authIdentity} option to a valid
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    89
 * distinguished name and omit the {@code userFilter} option.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * Use authentication-only mode when the user's distinguished name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * known in advance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <p> The following option is mandatory and must be specified in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * module's login {@link Configuration}:
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    95
 * <dl><dd>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <dt> <code>userProvider=<b>ldap_urls</b></code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * </dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <dd> This option identifies the LDAP directory that stores user entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *      <b>ldap_urls</b> is a list of space-separated LDAP URLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *      (<a href="http://www.ietf.org/rfc/rfc2255.txt">RFC 2255</a>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *      that identifies the LDAP server to use and the position in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *      its directory tree where user entries are located.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *      When several LDAP URLs are specified then each is attempted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *      in turn, until the first successful connection is established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *      Spaces in the distinguished name component of the URL must be escaped
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   107
 *      using the standard mechanism of percent character ('{@code %}')
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *      followed by two hexadecimal digits (see {@link java.net.URI}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *      Query components must also be omitted from the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *      <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *      Automatic discovery of the LDAP server via DNS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *      (<a href="http://www.ietf.org/rfc/rfc2782.txt">RFC 2782</a>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *      is supported (once DNS has been configured to support such a service).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *      It is enabled by omitting the hostname and port number components from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *      the LDAP URL. </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * </dl></dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <p> This module also recognizes the following optional {@link Configuration}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *     options:
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   121
 * <dl><dd>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * <dt> <code>userFilter=<b>ldap_filter</b></code> </dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * <dd> This option specifies the search filter to use to locate a user's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *      entry in the LDAP directory. It is used to determine a user's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *      distinguished name.
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   127
 *      <b>{@code ldap_filter}</b> is an LDAP filter string
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *      (<a href="http://www.ietf.org/rfc/rfc2254.txt">RFC 2254</a>).
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   129
 *      If it contains the special token "<b>{@code {USERNAME}}</b>"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *      then that token will be replaced with the supplied username value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *      before the filter is used to search the directory. </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * <dt> <code>authIdentity=<b>auth_id</b></code> </dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * <dd> This option specifies the identity to use when authenticating a user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *      to the LDAP directory.
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   136
 *      <b>{@code auth_id}</b> may be an LDAP distinguished name string
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *      (<a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>) or some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *      other string name.
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   139
 *      It must contain the special token "<b>{@code {USERNAME}}</b>"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *      which will be replaced with the supplied username value before the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *      name is used for authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *      Note that if this option does not contain a distinguished name then
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   143
 *      the {@code userFilter} option must also be specified. </dd>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * <dt> <code>authzIdentity=<b>authz_id</b></code> </dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * <dd> This option specifies an authorization identity for the user.
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   147
 *      <b>{@code authz_id}</b> is any string name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *      If it comprises a single special token with curly braces then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 *      that token is treated as a attribute name and will be replaced with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *      single value of that attribute from the user's LDAP entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 *      If the attribute cannot be found then the option is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *      When this option is supplied and the user has been successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *      authenticated then an additional {@link UserPrincipal}
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20742
diff changeset
   154
 *      is created using the authorization identity and it is associated with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 *      the current {@link Subject}. </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   157
 * <dt> {@code useSSL} </dt>
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   158
 * <dd> if {@code false}, this module does not establish an SSL connection
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 *      to the LDAP server before attempting authentication. SSL is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 *      protect the privacy of the user's password because it is transmitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 *      in the clear over LDAP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 *      By default, this module uses SSL. </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   164
 * <dt> {@code useFirstPass} </dt>
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   165
 * <dd> if {@code true}, this module retrieves the username and password
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 *      from the module's shared state, using "javax.security.auth.login.name"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 *      and "javax.security.auth.login.password" as the respective keys. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 *      retrieved values are used for authentication. If authentication fails,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 *      no attempt for a retry is made, and the failure is reported back to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *      the calling application.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   172
 * <dt> {@code tryFirstPass} </dt>
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   173
 * <dd> if {@code true}, this module retrieves the username and password
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 *      from the module's shared state, using "javax.security.auth.login.name"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 *       and "javax.security.auth.login.password" as the respective keys.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *      retrieved values are used for authentication. If authentication fails,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 *      the module uses the {@link CallbackHandler} to retrieve a new username
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 *      and password, and another attempt to authenticate is made. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 *      authentication fails, the failure is reported back to the calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 *      application.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   182
 * <dt> {@code storePass} </dt>
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   183
 * <dd> if {@code true}, this module stores the username and password
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 *      obtained from the {@link CallbackHandler} in the module's shared state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 *      using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 *      "javax.security.auth.login.name" and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 *      "javax.security.auth.login.password" as the respective keys.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 *      not performed if existing values already exist for the username and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 *      password in the shared state, or if authentication fails.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   191
 * <dt> {@code clearPass} </dt>
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   192
 * <dd> if {@code true}, this module clears the username and password
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 *      stored in the module's shared state after both phases of authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 *      (login and commit) have completed.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   196
 * <dt> {@code debug} </dt>
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   197
 * <dd> if {@code true}, debug messages are displayed on the standard
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 *      output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * Arbitrary
44853
4c752de98ce4 8178298: (LdapLoginModule)fix the JNDI properties technote
vtewari
parents: 43243
diff changeset
   204
 * {@extLink jndi_ldap_gl_prop "JNDI properties"}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * may also be specified in the {@link Configuration}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * They are added to the environment and passed to the LDAP provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 * Note that the following four JNDI properties are set by this module directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 * and are ignored if also present in the configuration:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 * <ul>
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   210
 * <li> {@code java.naming.provider.url}
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   211
 * <li> {@code java.naming.security.principal}
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   212
 * <li> {@code java.naming.security.credentials}
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   213
 * <li> {@code java.naming.security.protocol}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 * Three sample {@link Configuration}s are shown below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 * The first one activates search-first mode. It identifies the LDAP server
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   219
 * and specifies that users' entries be located by their {@code uid} and
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   220
 * {@code objectClass} attributes. It also specifies that an identity
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   221
 * based on the user's {@code employeeNumber} attribute should be created.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 * The second one activates authentication-first mode. It requests that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 * LDAP server be located dynamically, that authentication be performed using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 * the supplied username directly but without the protection of SSL and that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 * users' entries be located by one of three naming attributes and their
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   226
 * {@code objectClass} attribute.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 * The third one activates authentication-only mode. It identifies alternative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 * LDAP servers, it specifies the distinguished name to use for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 * authentication and a fixed identity to use for authorization. No directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 * search is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   232
 * <pre>{@literal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 *     ExampleApplication {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 *         com.sun.security.auth.module.LdapLoginModule REQUIRED
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   236
 *              userProvider="ldap://ldap-svr/ou=people,dc=example,dc=com"
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   237
 *              userFilter="(&(uid={USERNAME})(objectClass=inetOrgPerson))"
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   238
 *              authzIdentity="{EMPLOYEENUMBER}"
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   239
 *              debug=true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 *     };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 *     ExampleApplication {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 *         com.sun.security.auth.module.LdapLoginModule REQUIRED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 *             userProvider="ldap:///cn=users,dc=example,dc=com"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 *             authIdentity="{USERNAME}"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 *             userFilter="(&(|(samAccountName={USERNAME})(userPrincipalName={USERNAME})(cn={USERNAME}))(objectClass=user))"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 *             useSSL=false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 *             debug=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 *     };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 *     ExampleApplication {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 *         com.sun.security.auth.module.LdapLoginModule REQUIRED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 *             userProvider="ldap://ldap-svr1 ldap://ldap-svr2"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 *             authIdentity="cn={USERNAME},ou=people,dc=example,dc=com"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 *             authzIdentity="staff"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 *             debug=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 *     };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   259
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 * <dt><b>Note:</b> </dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 * <dd>When a {@link SecurityManager} is active then an application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 *     that creates a {@link LoginContext} and uses a {@link LoginModule}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 *     must be granted certain permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
 *     <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 *     If the application creates a login context using an <em>installed</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
 *     {@link Configuration} then the application must be granted the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
 *     {@link AuthPermission} to create login contexts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
 *     For example, the following security policy allows an application in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
 *     the user's current directory to instantiate <em>any</em> login context:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 *     <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
 *     grant codebase "file:${user.dir}/" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
 *         permission javax.security.auth.AuthPermission "createLoginContext.*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
 *     };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
 *     </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
 *     Alternatively, if the application creates a login context using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
 *     <em>caller-specified</em> {@link Configuration} then the application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
 *     must be granted the permissions required by the {@link LoginModule}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
 *     <em>This</em> module requires the following two permissions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
 *     <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
 *     <li> The {@link SocketPermission} to connect to an LDAP server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
 *     <li> The {@link AuthPermission} to modify the set of {@link Principal}s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
 *          associated with a {@link Subject}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
 *     </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
 *     <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
 *     For example, the following security policy grants an application in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
 *     user's current directory all the permissions required by this module:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
 *     <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
 *     grant codebase "file:${user.dir}/" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
 *         permission java.net.SocketPermission "*:389", "connect";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
 *         permission java.net.SocketPermission "*:636", "connect";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
 *         permission javax.security.auth.AuthPermission "modifyPrincipals";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
 *     };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
 *     </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
 * </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
 * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
public class LdapLoginModule implements LoginModule {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    // Keys to retrieve the stored username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    private static final String USERNAME_KEY = "javax.security.auth.login.name";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    private static final String PASSWORD_KEY =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        "javax.security.auth.login.password";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    // Option names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    private static final String USER_PROVIDER = "userProvider";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    private static final String USER_FILTER = "userFilter";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    private static final String AUTHC_IDENTITY = "authIdentity";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    private static final String AUTHZ_IDENTITY = "authzIdentity";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    // Used for the username token replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    private static final String USERNAME_TOKEN = "{USERNAME}";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    private static final Pattern USERNAME_PATTERN =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        Pattern.compile("\\{USERNAME\\}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    // Configurable options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    private String userProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    private String userFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private String authcIdentity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    private String authzIdentity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    private String authzIdentityAttr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    private boolean useSSL = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    private boolean authFirst = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    private boolean authOnly = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    private boolean useFirstPass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    private boolean tryFirstPass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    private boolean storePass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    private boolean clearPass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    private boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    // Authentication status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    private boolean succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    private boolean commitSucceeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    // Supplied username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    private String username;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    private char[] password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    // User's identities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    private LdapPrincipal ldapPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    private UserPrincipal userPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    private UserPrincipal authzPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    // Initial state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    private Subject subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    private CallbackHandler callbackHandler;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7179
diff changeset
   353
    private Map<String, Object> sharedState;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    private Map<String, ?> options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    private LdapContext ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    private Matcher identityMatcher = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    private Matcher filterMatcher = null;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7179
diff changeset
   358
    private Hashtable<String, Object> ldapEnvironment;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    private SearchControls constraints = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   362
     * Initialize this {@code LoginModule}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   364
     * @param subject the {@code Subject} to be authenticated.
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   365
     * @param callbackHandler a {@code CallbackHandler} to acquire the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *                  username and password.
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   367
     * @param sharedState shared {@code LoginModule} state.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param options options specified in the login
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   369
     *                  {@code Configuration} for this particular
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   370
     *                  {@code LoginModule}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7179
diff changeset
   372
    // Unchecked warning from (Map<String, Object>)sharedState is safe
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7179
diff changeset
   373
    // since javax.security.auth.login.LoginContext passes a raw HashMap.
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7179
diff changeset
   374
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public void initialize(Subject subject, CallbackHandler callbackHandler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                        Map<String, ?> sharedState, Map<String, ?> options) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        this.subject = subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        this.callbackHandler = callbackHandler;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7179
diff changeset
   380
        this.sharedState = (Map<String, Object>)sharedState;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        this.options = options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7179
diff changeset
   383
        ldapEnvironment = new Hashtable<String, Object>(9);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        ldapEnvironment.put(Context.INITIAL_CONTEXT_FACTORY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            "com.sun.jndi.ldap.LdapCtxFactory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        // Add any JNDI properties to the environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        for (String key : options.keySet()) {
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 23010
diff changeset
   389
            if (key.indexOf('.') > -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                ldapEnvironment.put(key, options.get(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        // initialize any configured options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        userProvider = (String)options.get(USER_PROVIDER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (userProvider != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            ldapEnvironment.put(Context.PROVIDER_URL, userProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        authcIdentity = (String)options.get(AUTHC_IDENTITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if (authcIdentity != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            (authcIdentity.indexOf(USERNAME_TOKEN) != -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            identityMatcher = USERNAME_PATTERN.matcher(authcIdentity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        userFilter = (String)options.get(USER_FILTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (userFilter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            if (userFilter.indexOf(USERNAME_TOKEN) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                filterMatcher = USERNAME_PATTERN.matcher(userFilter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            constraints = new SearchControls();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            constraints.setReturningAttributes(new String[0]); //return no attrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        authzIdentity = (String)options.get(AUTHZ_IDENTITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        if (authzIdentity != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            authzIdentity.startsWith("{") && authzIdentity.endsWith("}")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            if (constraints != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                authzIdentityAttr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    authzIdentity.substring(1, authzIdentity.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                constraints.setReturningAttributes(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    new String[]{authzIdentityAttr});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            authzIdentity = null; // set later, from the specified attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        // determine mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (authcIdentity != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            if (userFilter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                authFirst = true; // authentication-first mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                authOnly = true; // authentication-only mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        if ("false".equalsIgnoreCase((String)options.get("useSSL"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            useSSL = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            ldapEnvironment.remove(Context.SECURITY_PROTOCOL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            ldapEnvironment.put(Context.SECURITY_PROTOCOL, "ssl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        tryFirstPass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                "true".equalsIgnoreCase((String)options.get("tryFirstPass"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        useFirstPass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                "true".equalsIgnoreCase((String)options.get("useFirstPass"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        storePass = "true".equalsIgnoreCase((String)options.get("storePass"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        clearPass = "true".equalsIgnoreCase((String)options.get("clearPass"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        debug = "true".equalsIgnoreCase((String)options.get("debug"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            if (authFirst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    "authentication-first mode; " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    (useSSL ? "SSL enabled" : "SSL disabled"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            } else if (authOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    "authentication-only mode; " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    (useSSL ? "SSL enabled" : "SSL disabled"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    "search-first mode; " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    (useSSL ? "SSL enabled" : "SSL disabled"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * Begin user authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * <p> Acquire the user's credentials and verify them against the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * specified LDAP directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   480
     * @return true always, since this {@code LoginModule}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *          should not be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @exception FailedLoginException if the authentication fails.
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   483
     * @exception LoginException if this {@code LoginModule}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *          is unable to perform the authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    public boolean login() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        if (userProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                ("Unable to locate the LDAP directory service");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            System.out.println("\t\t[LdapLoginModule] user provider: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                userProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        // attempt the authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        if (tryFirstPass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                // attempt the authentication by getting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                // username and password from shared state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                attemptAuthentication(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                // authentication succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                succeeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                                "tryFirstPass succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            } catch (LoginException le) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                // authentication failed -- try again below by prompting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                                "tryFirstPass failed: " + le.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        } else if (useFirstPass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                // attempt the authentication by getting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                // username and password from shared state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                attemptAuthentication(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                // authentication succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                succeeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                                "useFirstPass succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            } catch (LoginException le) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                // authentication failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                                "useFirstPass failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        // attempt the authentication by prompting for the username and pwd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            attemptAuthentication(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            // authentication succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
           succeeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                                "authentication succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        } catch (LoginException le) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                                "authentication failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * Complete user authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * <p> This method is called if the LoginContext's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * overall authentication succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * succeeded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * <p> If this LoginModule's own authentication attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * succeeded (checked by retrieving the private state saved by the
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   581
     * {@code login} method), then this method associates an
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   582
     * {@code LdapPrincipal} and one or more {@code UserPrincipal}s
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   583
     * with the {@code Subject} located in the
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   584
     * {@code LoginModule}.  If this LoginModule's own
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * authentication attempted failed, then this method removes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * any state that was originally saved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @exception LoginException if the commit fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @return true if this LoginModule's own login and commit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *          attempts succeeded, or false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    public boolean commit() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        if (succeeded == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            if (subject.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                throw new LoginException ("Subject is read-only");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            // add Principals to the Subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            Set<Principal> principals = subject.getPrincipals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            if (! principals.contains(ldapPrincipal)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                principals.add(ldapPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                                   "added LdapPrincipal \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                                   ldapPrincipal +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                                   "\" to Subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            if (! principals.contains(userPrincipal)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                principals.add(userPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                                   "added UserPrincipal \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                                   userPrincipal +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                                   "\" to Subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            if (authzPrincipal != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                (! principals.contains(authzPrincipal))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                principals.add(authzPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                    System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                                   "added UserPrincipal \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                                   authzPrincipal +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                                   "\" to Subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        // in any case, clean out state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        commitSucceeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * Abort user authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * <p> This method is called if the overall authentication failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * did not succeed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * <p> If this LoginModule's own authentication attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * succeeded (checked by retrieving the private state saved by the
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   650
     * {@code login} and {@code commit} methods),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * then this method cleans up any state that was originally saved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * @exception LoginException if the abort fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * @return false if this LoginModule's own login and/or commit attempts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     *          failed, and true otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    public boolean abort() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                "aborted authentication");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        if (succeeded == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        } else if (succeeded == true && commitSucceeded == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            // Clean out state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            ldapPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            userPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            authzPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            // overall authentication succeeded and commit succeeded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            // but someone else's commit failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            logout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * Logout a user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * <p> This method removes the Principals
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   685
     * that were added by the {@code commit} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @exception LoginException if the logout fails.
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   688
     * @return true in all cases since this {@code LoginModule}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *          should not be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    public boolean logout() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        if (subject.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            throw new LoginException ("Subject is read-only");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        Set<Principal> principals = subject.getPrincipals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        principals.remove(ldapPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        principals.remove(userPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        if (authzIdentity != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            principals.remove(authzPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        // clean out state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        commitSucceeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        ldapPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        userPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        authzPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            System.out.println("\t\t[LdapLoginModule] logged out Subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * Attempt authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * @param getPasswdFromSharedState boolean that tells this method whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     *          to retrieve the password from the sharedState.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * @exception LoginException if the authentication attempt fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    private void attemptAuthentication(boolean getPasswdFromSharedState)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        // first get the username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        getUsernamePassword(getPasswdFromSharedState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        if (password == null || password.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            throw (LoginException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                new FailedLoginException("No password was supplied");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        String dn = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        if (authFirst || authOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            String id = replaceUsernameToken(identityMatcher, authcIdentity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            // Prepare to bind using user's username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            ldapEnvironment.put(Context.SECURITY_CREDENTIALS, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            ldapEnvironment.put(Context.SECURITY_PRINCIPAL, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                    "attempting to authenticate user: " + username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                // Connect to the LDAP server (using simple bind)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                ctx = new InitialLdapContext(ldapEnvironment, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                throw (LoginException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                    new FailedLoginException("Cannot bind to LDAP server")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                        .initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            // Authentication has succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            // Locate the user's distinguished name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            if (userFilter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                dn = findUserDN(ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                dn = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                // Connect to the LDAP server (using anonymous bind)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                ctx = new InitialLdapContext(ldapEnvironment, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                throw (LoginException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                    new FailedLoginException("Cannot connect to LDAP server")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                        .initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            // Locate the user's distinguished name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            dn = findUserDN(ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                // Prepare to bind using user's distinguished name and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, "simple");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, dn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                    System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                        "attempting to authenticate user: " + username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                // Connect to the LDAP server (using simple bind)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                ctx.reconnect(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                // Authentication has succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                throw (LoginException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                    new FailedLoginException("Cannot bind to LDAP server")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                        .initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        // Save input as shared state only if authentication succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        if (storePass &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            !sharedState.containsKey(USERNAME_KEY) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            !sharedState.containsKey(PASSWORD_KEY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            sharedState.put(USERNAME_KEY, username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            sharedState.put(PASSWORD_KEY, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        // Create the user principals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        userPrincipal = new UserPrincipal(username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        if (authzIdentity != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            authzPrincipal = new UserPrincipal(authzIdentity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            ldapPrincipal = new LdapPrincipal(dn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        } catch (InvalidNameException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                                   "cannot create LdapPrincipal: bad DN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            throw (LoginException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                new FailedLoginException("Cannot create LdapPrincipal")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                    .initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * Search for the user's entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * Determine the distinguished name of the user's entry and optionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * an authorization identity for the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * @param ctx an LDAP context to use for the search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * @return the user's distinguished name or an empty string if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     *         was found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * @exception LoginException if the user's entry cannot be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    private String findUserDN(LdapContext ctx) throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        String userDN = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        // Locate the user's LDAP entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        if (userFilter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                    "searching for entry belonging to user: " + username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                    "cannot search for entry belonging to user: " + username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            throw (LoginException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                new FailedLoginException("Cannot find user's LDAP entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            NamingEnumeration<SearchResult> results = ctx.search("",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                replaceUsernameToken(filterMatcher, userFilter), constraints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            // Extract the distinguished name of the user's entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            // (Use the first entry if more than one is returned)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            if (results.hasMore()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                SearchResult entry = results.next();
43198
7fc878d49203 8161743: Provide proper login context
weijun
parents: 42338
diff changeset
   874
                userDN = entry.getNameInNamespace();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                    System.out.println("\t\t[LdapLoginModule] found entry: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                        userDN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                // Extract a value from user's authorization identity attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                if (authzIdentityAttr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                    Attribute attr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                        entry.getAttributes().get(authzIdentityAttr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                    if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                        Object val = attr.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                        if (val instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                            authzIdentity = (String) val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                results.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                // Bad username
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                    System.out.println("\t\t[LdapLoginModule] user's entry " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                        "not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        if (userDN.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            throw (LoginException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                new FailedLoginException("Cannot find user's LDAP entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            return userDN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * Replace the username token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * @param string the target string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * @return the modified string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    private String replaceUsernameToken(Matcher matcher, String string) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        return matcher != null ? matcher.replaceAll(username) : string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * Get the username and password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * This method does not return any value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * Instead, it sets global name and password variables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * <p> Also note that this method will set the username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * values in the shared state in case subsequent LoginModules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * want to use them via use/tryFirstPass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * @param getPasswdFromSharedState boolean that tells this method whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     *          to retrieve the password from the sharedState.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * @exception LoginException if the username/password cannot be acquired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    private void getUsernamePassword(boolean getPasswdFromSharedState)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        if (getPasswdFromSharedState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            // use the password saved by the first module in the stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            username = (String)sharedState.get(USERNAME_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            password = (char[])sharedState.get(PASSWORD_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        // prompt for a username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        if (callbackHandler == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            throw new LoginException("No CallbackHandler available " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                "to acquire authentication information from the user");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        Callback[] callbacks = new Callback[2];
43243
a48dab17a356 8173024: Replace direct use of AuthResources resource bundle from jdk.security.auth
mchung
parents: 43198
diff changeset
   954
        callbacks[0] = new NameCallback(getAuthResourceString("username."));
a48dab17a356 8173024: Replace direct use of AuthResources resource bundle from jdk.security.auth
mchung
parents: 43198
diff changeset
   955
        callbacks[1] = new PasswordCallback(getAuthResourceString("password."), false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            callbackHandler.handle(callbacks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            username = ((NameCallback)callbacks[0]).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            char[] tmpPassword = ((PasswordCallback)callbacks[1]).getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            password = new char[tmpPassword.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            System.arraycopy(tmpPassword, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                                password, 0, tmpPassword.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            ((PasswordCallback)callbacks[1]).clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        } catch (java.io.IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            throw new LoginException(ioe.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        } catch (UnsupportedCallbackException uce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            throw new LoginException("Error: " + uce.getCallback().toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                        " not available to acquire authentication information" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                        " from the user");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * Clean out state because of a failed authentication attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    private void cleanState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        username = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        if (password != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            Arrays.fill(password, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            password = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            if (ctx != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                ctx.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        ctx = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        if (clearPass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            sharedState.remove(USERNAME_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            sharedState.remove(PASSWORD_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
}