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