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