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