author | avstepan |
Wed, 29 Apr 2015 17:29:14 +0400 | |
changeset 30044 | bab15bbe2ca3 |
parent 25859 | 3317bb8137f4 |
child 31538 | 0981099a3e54 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
20742
diff
changeset
|
2 |
* Copyright (c) 2000, 2013, 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 javax.security.auth.*; |
|
29 |
import javax.security.auth.callback.*; |
|
30 |
import javax.security.auth.login.*; |
|
31 |
import javax.security.auth.spi.*; |
|
32 |
import javax.naming.*; |
|
33 |
import javax.naming.directory.*; |
|
34 |
||
16503
03d7a6155092
8009970: Several LoginModule classes need extra permission to load AuthResources
weijun
parents:
10336
diff
changeset
|
35 |
import java.security.AccessController; |
03d7a6155092
8009970: Several LoginModule classes need extra permission to load AuthResources
weijun
parents:
10336
diff
changeset
|
36 |
import java.security.PrivilegedAction; |
2 | 37 |
import java.util.Map; |
38 |
import java.util.LinkedList; |
|
16503
03d7a6155092
8009970: Several LoginModule classes need extra permission to load AuthResources
weijun
parents:
10336
diff
changeset
|
39 |
import java.util.ResourceBundle; |
2 | 40 |
|
41 |
import com.sun.security.auth.UnixPrincipal; |
|
42 |
import com.sun.security.auth.UnixNumericUserPrincipal; |
|
43 |
import com.sun.security.auth.UnixNumericGroupPrincipal; |
|
44 |
||
45 |
||
46 |
/** |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
47 |
* The module prompts for a username and password |
2 | 48 |
* and then verifies the password against the password stored in |
49 |
* a directory service configured under JNDI. |
|
50 |
* |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
51 |
* <p> This {@code LoginModule} interoperates with |
2 | 52 |
* any conformant JNDI service provider. To direct this |
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
53 |
* {@code LoginModule} to use a specific JNDI service provider, |
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
54 |
* two options must be specified in the login {@code Configuration} |
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
55 |
* for this {@code LoginModule}. |
2 | 56 |
* <pre> |
57 |
* user.provider.url=<b>name_service_url</b> |
|
58 |
* group.provider.url=<b>name_service_url</b> |
|
59 |
* </pre> |
|
60 |
* |
|
61 |
* <b>name_service_url</b> specifies |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
62 |
* the directory service and path where this {@code LoginModule} |
2 | 63 |
* can access the relevant user and group information. Because this |
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
64 |
* {@code LoginModule} only performs one-level searches to |
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
65 |
* find the relevant user information, the {@code URL} |
2 | 66 |
* must point to a directory one level above where the user and group |
67 |
* information is stored in the directory service. |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
68 |
* For example, to instruct this {@code LoginModule} |
2 | 69 |
* to contact a NIS server, the following URLs must be specified: |
70 |
* <pre> |
|
71 |
* user.provider.url="nis://<b>NISServerHostName</b>/<b>NISDomain</b>/user" |
|
72 |
* group.provider.url="nis://<b>NISServerHostName</b>/<b>NISDomain</b>/system/group" |
|
73 |
* </pre> |
|
74 |
* |
|
75 |
* <b>NISServerHostName</b> specifies the server host name of the |
|
76 |
* NIS server (for example, <i>nis.sun.com</i>, and <b>NISDomain</b> |
|
77 |
* specifies the domain for that NIS server (for example, <i>jaas.sun.com</i>. |
|
78 |
* To contact an LDAP server, the following URLs must be specified: |
|
79 |
* <pre> |
|
80 |
* user.provider.url="ldap://<b>LDAPServerHostName</b>/<b>LDAPName</b>" |
|
81 |
* group.provider.url="ldap://<b>LDAPServerHostName</b>/<b>LDAPName</b>" |
|
82 |
* </pre> |
|
83 |
* |
|
84 |
* <b>LDAPServerHostName</b> specifies the server host name of the |
|
85 |
* LDAP server, which may include a port number |
|
86 |
* (for example, <i>ldap.sun.com:389</i>), |
|
87 |
* and <b>LDAPName</b> specifies the entry name in the LDAP directory |
|
88 |
* (for example, <i>ou=People,o=Sun,c=US</i> and <i>ou=Groups,o=Sun,c=US</i> |
|
89 |
* for user and group information, respectively). |
|
90 |
* |
|
91 |
* <p> The format in which the user's information must be stored in |
|
92 |
* the directory service is specified in RFC 2307. Specifically, |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
93 |
* this {@code LoginModule} will search for the user's entry in the |
2 | 94 |
* directory service using the user's <i>uid</i> attribute, |
95 |
* where <i>uid=<b>username</b></i>. If the search succeeds, |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
96 |
* this {@code LoginModule} will then |
2 | 97 |
* obtain the user's encrypted password from the retrieved entry |
98 |
* using the <i>userPassword</i> attribute. |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
99 |
* This {@code LoginModule} assumes that the password is stored |
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
100 |
* as a byte array, which when converted to a {@code String}, |
2 | 101 |
* has the following format: |
102 |
* <pre> |
|
103 |
* "{crypt}<b>encrypted_password</b>" |
|
104 |
* </pre> |
|
105 |
* |
|
106 |
* The LDAP directory server must be configured |
|
107 |
* to permit read access to the userPassword attribute. |
|
108 |
* If the user entered a valid username and password, |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
109 |
* this {@code LoginModule} associates a |
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
110 |
* {@code UnixPrincipal}, {@code UnixNumericUserPrincipal}, |
2 | 111 |
* and the relevant UnixNumericGroupPrincipals with the |
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
112 |
* {@code Subject}. |
2 | 113 |
* |
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
114 |
* <p> This LoginModule also recognizes the following {@code Configuration} |
2 | 115 |
* options: |
116 |
* <pre> |
|
117 |
* debug if, true, debug messages are output to System.out. |
|
118 |
* |
|
119 |
* useFirstPass if, true, this LoginModule retrieves the |
|
120 |
* username and password from the module's shared state, |
|
121 |
* using "javax.security.auth.login.name" and |
|
122 |
* "javax.security.auth.login.password" as the respective |
|
123 |
* keys. The retrieved values are used for authentication. |
|
124 |
* If authentication fails, no attempt for a retry is made, |
|
125 |
* and the failure is reported back to the calling |
|
126 |
* application. |
|
127 |
* |
|
128 |
* tryFirstPass if, true, this LoginModule retrieves the |
|
129 |
* the username and password from the module's shared state, |
|
130 |
* using "javax.security.auth.login.name" and |
|
131 |
* "javax.security.auth.login.password" as the respective |
|
132 |
* keys. The retrieved values are used for authentication. |
|
133 |
* If authentication fails, the module uses the |
|
134 |
* CallbackHandler to retrieve a new username and password, |
|
135 |
* and another attempt to authenticate is made. |
|
136 |
* If the authentication fails, the failure is reported |
|
137 |
* back to the calling application. |
|
138 |
* |
|
139 |
* storePass if, true, this LoginModule stores the username and password |
|
140 |
* obtained from the CallbackHandler in the module's |
|
141 |
* shared state, using "javax.security.auth.login.name" and |
|
142 |
* "javax.security.auth.login.password" as the respective |
|
143 |
* keys. This is not performed if existing values already |
|
144 |
* exist for the username and password in the shared state, |
|
145 |
* or if authentication fails. |
|
146 |
* |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
147 |
* clearPass if, true, this {@code LoginModule} clears the |
2 | 148 |
* username and password stored in the module's shared state |
149 |
* after both phases of authentication (login and commit) |
|
150 |
* have completed. |
|
151 |
* </pre> |
|
152 |
* |
|
153 |
*/ |
|
20742
4ae78e8060d6
8008662: Add @jdk.Exported to JDK-specific/exported APIs
alanb
parents:
16503
diff
changeset
|
154 |
@jdk.Exported |
2 | 155 |
public class JndiLoginModule implements LoginModule { |
156 |
||
16503
03d7a6155092
8009970: Several LoginModule classes need extra permission to load AuthResources
weijun
parents:
10336
diff
changeset
|
157 |
private static final ResourceBundle rb = AccessController.doPrivileged( |
03d7a6155092
8009970: Several LoginModule classes need extra permission to load AuthResources
weijun
parents:
10336
diff
changeset
|
158 |
new PrivilegedAction<ResourceBundle>() { |
03d7a6155092
8009970: Several LoginModule classes need extra permission to load AuthResources
weijun
parents:
10336
diff
changeset
|
159 |
public ResourceBundle run() { |
03d7a6155092
8009970: Several LoginModule classes need extra permission to load AuthResources
weijun
parents:
10336
diff
changeset
|
160 |
return ResourceBundle.getBundle( |
03d7a6155092
8009970: Several LoginModule classes need extra permission to load AuthResources
weijun
parents:
10336
diff
changeset
|
161 |
"sun.security.util.AuthResources"); |
03d7a6155092
8009970: Several LoginModule classes need extra permission to load AuthResources
weijun
parents:
10336
diff
changeset
|
162 |
} |
03d7a6155092
8009970: Several LoginModule classes need extra permission to load AuthResources
weijun
parents:
10336
diff
changeset
|
163 |
} |
03d7a6155092
8009970: Several LoginModule classes need extra permission to load AuthResources
weijun
parents:
10336
diff
changeset
|
164 |
); |
2 | 165 |
|
166 |
/** JNDI Provider */ |
|
167 |
public final String USER_PROVIDER = "user.provider.url"; |
|
168 |
public final String GROUP_PROVIDER = "group.provider.url"; |
|
169 |
||
170 |
// configurable options |
|
171 |
private boolean debug = false; |
|
172 |
private boolean strongDebug = false; |
|
173 |
private String userProvider; |
|
174 |
private String groupProvider; |
|
175 |
private boolean useFirstPass = false; |
|
176 |
private boolean tryFirstPass = false; |
|
177 |
private boolean storePass = false; |
|
178 |
private boolean clearPass = false; |
|
179 |
||
180 |
// the authentication status |
|
181 |
private boolean succeeded = false; |
|
182 |
private boolean commitSucceeded = false; |
|
183 |
||
184 |
// username, password, and JNDI context |
|
185 |
private String username; |
|
186 |
private char[] password; |
|
187 |
DirContext ctx; |
|
188 |
||
189 |
// the user (assume it is a UnixPrincipal) |
|
190 |
private UnixPrincipal userPrincipal; |
|
191 |
private UnixNumericUserPrincipal UIDPrincipal; |
|
192 |
private UnixNumericGroupPrincipal GIDPrincipal; |
|
193 |
private LinkedList<UnixNumericGroupPrincipal> supplementaryGroups = |
|
7970
af1579474d16
7008728: diamond conversion of basic security, permissions, authentication
smarks
parents:
7179
diff
changeset
|
194 |
new LinkedList<>(); |
2 | 195 |
|
196 |
// initial state |
|
197 |
private Subject subject; |
|
198 |
private CallbackHandler callbackHandler; |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
199 |
private Map<String, Object> sharedState; |
2 | 200 |
private Map<String, ?> options; |
201 |
||
202 |
private static final String CRYPT = "{crypt}"; |
|
203 |
private static final String USER_PWD = "userPassword"; |
|
204 |
private static final String USER_UID = "uidNumber"; |
|
205 |
private static final String USER_GID = "gidNumber"; |
|
206 |
private static final String GROUP_ID = "gidNumber"; |
|
207 |
private static final String NAME = "javax.security.auth.login.name"; |
|
208 |
private static final String PWD = "javax.security.auth.login.password"; |
|
209 |
||
210 |
/** |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
211 |
* Initialize this {@code LoginModule}. |
2 | 212 |
* |
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
213 |
* @param subject the {@code Subject} to be authenticated. |
2 | 214 |
* |
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
215 |
* @param callbackHandler a {@code CallbackHandler} for communicating |
2 | 216 |
* with the end user (prompting for usernames and |
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
217 |
* passwords, for example). |
2 | 218 |
* |
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
219 |
* @param sharedState shared {@code LoginModule} state. |
2 | 220 |
* |
221 |
* @param options options specified in the login |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
222 |
* {@code Configuration} for this particular |
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
223 |
* {@code LoginModule}. |
2 | 224 |
*/ |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
225 |
// Unchecked warning from (Map<String, Object>)sharedState is safe |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
226 |
// 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:
9035
diff
changeset
|
227 |
// Unchecked warnings from options.get(String) are safe since we are |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
228 |
// passing known keys. |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
229 |
@SuppressWarnings("unchecked") |
2 | 230 |
public void initialize(Subject subject, CallbackHandler callbackHandler, |
231 |
Map<String,?> sharedState, |
|
232 |
Map<String,?> options) { |
|
233 |
||
234 |
this.subject = subject; |
|
235 |
this.callbackHandler = callbackHandler; |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
236 |
this.sharedState = (Map<String, Object>)sharedState; |
2 | 237 |
this.options = options; |
238 |
||
239 |
// initialize any configured options |
|
240 |
debug = "true".equalsIgnoreCase((String)options.get("debug")); |
|
241 |
strongDebug = |
|
242 |
"true".equalsIgnoreCase((String)options.get("strongDebug")); |
|
243 |
userProvider = (String)options.get(USER_PROVIDER); |
|
244 |
groupProvider = (String)options.get(GROUP_PROVIDER); |
|
245 |
tryFirstPass = |
|
246 |
"true".equalsIgnoreCase((String)options.get("tryFirstPass")); |
|
247 |
useFirstPass = |
|
248 |
"true".equalsIgnoreCase((String)options.get("useFirstPass")); |
|
249 |
storePass = |
|
250 |
"true".equalsIgnoreCase((String)options.get("storePass")); |
|
251 |
clearPass = |
|
252 |
"true".equalsIgnoreCase((String)options.get("clearPass")); |
|
253 |
} |
|
254 |
||
255 |
/** |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
256 |
* Prompt for username and password. |
2 | 257 |
* Verify the password against the relevant name service. |
258 |
* |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
259 |
* @return true always, since this {@code LoginModule} |
2 | 260 |
* should not be ignored. |
261 |
* |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
262 |
* @exception FailedLoginException if the authentication fails. |
2 | 263 |
* |
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
264 |
* @exception LoginException if this {@code LoginModule} |
2 | 265 |
* is unable to perform the authentication. |
266 |
*/ |
|
267 |
public boolean login() throws LoginException { |
|
268 |
||
269 |
if (userProvider == null) { |
|
270 |
throw new LoginException |
|
271 |
("Error: Unable to locate JNDI user provider"); |
|
272 |
} |
|
273 |
if (groupProvider == null) { |
|
274 |
throw new LoginException |
|
275 |
("Error: Unable to locate JNDI group provider"); |
|
276 |
} |
|
277 |
||
278 |
if (debug) { |
|
279 |
System.out.println("\t\t[JndiLoginModule] user provider: " + |
|
280 |
userProvider); |
|
281 |
System.out.println("\t\t[JndiLoginModule] group provider: " + |
|
282 |
groupProvider); |
|
283 |
} |
|
284 |
||
285 |
// attempt the authentication |
|
286 |
if (tryFirstPass) { |
|
287 |
||
288 |
try { |
|
289 |
// attempt the authentication by getting the |
|
290 |
// username and password from shared state |
|
291 |
attemptAuthentication(true); |
|
292 |
||
293 |
// authentication succeeded |
|
294 |
succeeded = true; |
|
295 |
if (debug) { |
|
296 |
System.out.println("\t\t[JndiLoginModule] " + |
|
297 |
"tryFirstPass succeeded"); |
|
298 |
} |
|
299 |
return true; |
|
300 |
} catch (LoginException le) { |
|
301 |
// authentication failed -- try again below by prompting |
|
302 |
cleanState(); |
|
303 |
if (debug) { |
|
304 |
System.out.println("\t\t[JndiLoginModule] " + |
|
305 |
"tryFirstPass failed with:" + |
|
306 |
le.toString()); |
|
307 |
} |
|
308 |
} |
|
309 |
||
310 |
} else if (useFirstPass) { |
|
311 |
||
312 |
try { |
|
313 |
// attempt the authentication by getting the |
|
314 |
// username and password from shared state |
|
315 |
attemptAuthentication(true); |
|
316 |
||
317 |
// authentication succeeded |
|
318 |
succeeded = true; |
|
319 |
if (debug) { |
|
320 |
System.out.println("\t\t[JndiLoginModule] " + |
|
321 |
"useFirstPass succeeded"); |
|
322 |
} |
|
323 |
return true; |
|
324 |
} catch (LoginException le) { |
|
325 |
// authentication failed |
|
326 |
cleanState(); |
|
327 |
if (debug) { |
|
328 |
System.out.println("\t\t[JndiLoginModule] " + |
|
329 |
"useFirstPass failed"); |
|
330 |
} |
|
331 |
throw le; |
|
332 |
} |
|
333 |
} |
|
334 |
||
335 |
// attempt the authentication by prompting for the username and pwd |
|
336 |
try { |
|
337 |
attemptAuthentication(false); |
|
338 |
||
339 |
// authentication succeeded |
|
340 |
succeeded = true; |
|
341 |
if (debug) { |
|
342 |
System.out.println("\t\t[JndiLoginModule] " + |
|
343 |
"regular authentication succeeded"); |
|
344 |
} |
|
345 |
return true; |
|
346 |
} catch (LoginException le) { |
|
347 |
cleanState(); |
|
348 |
if (debug) { |
|
349 |
System.out.println("\t\t[JndiLoginModule] " + |
|
350 |
"regular authentication failed"); |
|
351 |
} |
|
352 |
throw le; |
|
353 |
} |
|
354 |
} |
|
355 |
||
356 |
/** |
|
357 |
* Abstract method to commit the authentication process (phase 2). |
|
358 |
* |
|
359 |
* <p> This method is called if the LoginContext's |
|
360 |
* overall authentication succeeded |
|
361 |
* (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules |
|
362 |
* succeeded). |
|
363 |
* |
|
364 |
* <p> If this LoginModule's own authentication attempt |
|
365 |
* 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
|
366 |
* {@code login} method), then this method associates a |
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
367 |
* {@code UnixPrincipal} |
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
368 |
* with the {@code Subject} located in the |
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
369 |
* {@code LoginModule}. If this LoginModule's own |
2 | 370 |
* authentication attempted failed, then this method removes |
371 |
* any state that was originally saved. |
|
372 |
* |
|
373 |
* @exception LoginException if the commit fails |
|
374 |
* |
|
375 |
* @return true if this LoginModule's own login and commit |
|
376 |
* attempts succeeded, or false otherwise. |
|
377 |
*/ |
|
378 |
public boolean commit() throws LoginException { |
|
379 |
||
380 |
if (succeeded == false) { |
|
381 |
return false; |
|
382 |
} else { |
|
383 |
if (subject.isReadOnly()) { |
|
384 |
cleanState(); |
|
385 |
throw new LoginException ("Subject is Readonly"); |
|
386 |
} |
|
387 |
// add Principals to the Subject |
|
388 |
if (!subject.getPrincipals().contains(userPrincipal)) |
|
389 |
subject.getPrincipals().add(userPrincipal); |
|
390 |
if (!subject.getPrincipals().contains(UIDPrincipal)) |
|
391 |
subject.getPrincipals().add(UIDPrincipal); |
|
392 |
if (!subject.getPrincipals().contains(GIDPrincipal)) |
|
393 |
subject.getPrincipals().add(GIDPrincipal); |
|
394 |
for (int i = 0; i < supplementaryGroups.size(); i++) { |
|
395 |
if (!subject.getPrincipals().contains |
|
396 |
(supplementaryGroups.get(i))) |
|
397 |
subject.getPrincipals().add(supplementaryGroups.get(i)); |
|
398 |
} |
|
399 |
||
400 |
if (debug) { |
|
401 |
System.out.println("\t\t[JndiLoginModule]: " + |
|
402 |
"added UnixPrincipal,"); |
|
403 |
System.out.println("\t\t\t\tUnixNumericUserPrincipal,"); |
|
404 |
System.out.println("\t\t\t\tUnixNumericGroupPrincipal(s),"); |
|
405 |
System.out.println("\t\t\t to Subject"); |
|
406 |
} |
|
407 |
} |
|
408 |
// in any case, clean out state |
|
409 |
cleanState(); |
|
410 |
commitSucceeded = true; |
|
411 |
return true; |
|
412 |
} |
|
413 |
||
414 |
/** |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
415 |
* This method is called if the LoginContext's |
2 | 416 |
* overall authentication failed. |
417 |
* (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules |
|
418 |
* did not succeed). |
|
419 |
* |
|
420 |
* <p> If this LoginModule's own authentication attempt |
|
421 |
* 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
|
422 |
* {@code login} and {@code commit} methods), |
2 | 423 |
* then this method cleans up any state that was originally saved. |
424 |
* |
|
425 |
* @exception LoginException if the abort fails. |
|
426 |
* |
|
427 |
* @return false if this LoginModule's own login and/or commit attempts |
|
428 |
* failed, and true otherwise. |
|
429 |
*/ |
|
430 |
public boolean abort() throws LoginException { |
|
431 |
if (debug) |
|
432 |
System.out.println("\t\t[JndiLoginModule]: " + |
|
433 |
"aborted authentication failed"); |
|
434 |
||
435 |
if (succeeded == false) { |
|
436 |
return false; |
|
437 |
} else if (succeeded == true && commitSucceeded == false) { |
|
438 |
||
439 |
// Clean out state |
|
440 |
succeeded = false; |
|
441 |
cleanState(); |
|
442 |
||
443 |
userPrincipal = null; |
|
444 |
UIDPrincipal = null; |
|
445 |
GIDPrincipal = null; |
|
446 |
supplementaryGroups = new LinkedList<UnixNumericGroupPrincipal>(); |
|
447 |
} else { |
|
448 |
// overall authentication succeeded and commit succeeded, |
|
449 |
// but someone else's commit failed |
|
450 |
logout(); |
|
451 |
} |
|
452 |
return true; |
|
453 |
} |
|
454 |
||
455 |
/** |
|
456 |
* Logout a user. |
|
457 |
* |
|
458 |
* <p> This method removes the Principals |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
459 |
* that were added by the {@code commit} method. |
2 | 460 |
* |
461 |
* @exception LoginException if the logout fails. |
|
462 |
* |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
463 |
* @return true in all cases since this {@code LoginModule} |
2 | 464 |
* should not be ignored. |
465 |
*/ |
|
466 |
public boolean logout() throws LoginException { |
|
467 |
if (subject.isReadOnly()) { |
|
468 |
cleanState(); |
|
469 |
throw new LoginException ("Subject is Readonly"); |
|
470 |
} |
|
471 |
subject.getPrincipals().remove(userPrincipal); |
|
472 |
subject.getPrincipals().remove(UIDPrincipal); |
|
473 |
subject.getPrincipals().remove(GIDPrincipal); |
|
474 |
for (int i = 0; i < supplementaryGroups.size(); i++) { |
|
475 |
subject.getPrincipals().remove(supplementaryGroups.get(i)); |
|
476 |
} |
|
477 |
||
478 |
||
479 |
// clean out state |
|
480 |
cleanState(); |
|
481 |
succeeded = false; |
|
482 |
commitSucceeded = false; |
|
483 |
||
484 |
userPrincipal = null; |
|
485 |
UIDPrincipal = null; |
|
486 |
GIDPrincipal = null; |
|
487 |
supplementaryGroups = new LinkedList<UnixNumericGroupPrincipal>(); |
|
488 |
||
489 |
if (debug) { |
|
490 |
System.out.println("\t\t[JndiLoginModule]: " + |
|
491 |
"logged out Subject"); |
|
492 |
} |
|
493 |
return true; |
|
494 |
} |
|
495 |
||
496 |
/** |
|
497 |
* Attempt authentication |
|
498 |
* |
|
499 |
* @param getPasswdFromSharedState boolean that tells this method whether |
|
500 |
* to retrieve the password from the sharedState. |
|
501 |
*/ |
|
502 |
private void attemptAuthentication(boolean getPasswdFromSharedState) |
|
503 |
throws LoginException { |
|
504 |
||
505 |
String encryptedPassword = null; |
|
506 |
||
507 |
// first get the username and password |
|
508 |
getUsernamePassword(getPasswdFromSharedState); |
|
509 |
||
510 |
try { |
|
511 |
||
512 |
// get the user's passwd entry from the user provider URL |
|
513 |
InitialContext iCtx = new InitialContext(); |
|
514 |
ctx = (DirContext)iCtx.lookup(userProvider); |
|
515 |
||
516 |
/* |
|
517 |
SearchControls controls = new SearchControls |
|
518 |
(SearchControls.ONELEVEL_SCOPE, |
|
519 |
0, |
|
520 |
5000, |
|
521 |
new String[] { USER_PWD }, |
|
522 |
false, |
|
523 |
false); |
|
524 |
*/ |
|
525 |
||
526 |
SearchControls controls = new SearchControls(); |
|
527 |
NamingEnumeration<SearchResult> ne = ctx.search("", |
|
528 |
"(uid=" + username + ")", |
|
529 |
controls); |
|
530 |
if (ne.hasMore()) { |
|
531 |
SearchResult result = ne.next(); |
|
532 |
Attributes attributes = result.getAttributes(); |
|
533 |
||
534 |
// get the password |
|
535 |
||
536 |
// this module works only if the LDAP directory server |
|
537 |
// is configured to permit read access to the userPassword |
|
538 |
// attribute. The directory administrator need to grant |
|
539 |
// this access. |
|
540 |
// |
|
541 |
// A workaround would be to make the server do authentication |
|
542 |
// by setting the Context.SECURITY_PRINCIPAL |
|
543 |
// and Context.SECURITY_CREDENTIALS property. |
|
544 |
// However, this would make it not work with systems that |
|
545 |
// don't do authentication at the server (like NIS). |
|
546 |
// |
|
547 |
// Setting the SECURITY_* properties and using "simple" |
|
548 |
// authentication for LDAP is recommended only for secure |
|
549 |
// channels. For nonsecure channels, SSL is recommended. |
|
550 |
||
551 |
Attribute pwd = attributes.get(USER_PWD); |
|
552 |
String encryptedPwd = new String((byte[])pwd.get(), "UTF8"); |
|
553 |
encryptedPassword = encryptedPwd.substring(CRYPT.length()); |
|
554 |
||
555 |
// check the password |
|
556 |
if (verifyPassword |
|
557 |
(encryptedPassword, new String(password)) == true) { |
|
558 |
||
559 |
// authentication succeeded |
|
560 |
if (debug) |
|
561 |
System.out.println("\t\t[JndiLoginModule] " + |
|
562 |
"attemptAuthentication() succeeded"); |
|
563 |
||
564 |
} else { |
|
565 |
// authentication failed |
|
566 |
if (debug) |
|
567 |
System.out.println("\t\t[JndiLoginModule] " + |
|
568 |
"attemptAuthentication() failed"); |
|
569 |
throw new FailedLoginException("Login incorrect"); |
|
570 |
} |
|
571 |
||
572 |
// save input as shared state only if |
|
573 |
// authentication succeeded |
|
574 |
if (storePass && |
|
575 |
!sharedState.containsKey(NAME) && |
|
576 |
!sharedState.containsKey(PWD)) { |
|
577 |
sharedState.put(NAME, username); |
|
578 |
sharedState.put(PWD, password); |
|
579 |
} |
|
580 |
||
581 |
// create the user principal |
|
582 |
userPrincipal = new UnixPrincipal(username); |
|
583 |
||
584 |
// get the UID |
|
585 |
Attribute uid = attributes.get(USER_UID); |
|
586 |
String uidNumber = (String)uid.get(); |
|
587 |
UIDPrincipal = new UnixNumericUserPrincipal(uidNumber); |
|
588 |
if (debug && uidNumber != null) { |
|
589 |
System.out.println("\t\t[JndiLoginModule] " + |
|
590 |
"user: '" + username + "' has UID: " + |
|
591 |
uidNumber); |
|
592 |
} |
|
593 |
||
594 |
// get the GID |
|
595 |
Attribute gid = attributes.get(USER_GID); |
|
596 |
String gidNumber = (String)gid.get(); |
|
597 |
GIDPrincipal = new UnixNumericGroupPrincipal |
|
598 |
(gidNumber, true); |
|
599 |
if (debug && gidNumber != null) { |
|
600 |
System.out.println("\t\t[JndiLoginModule] " + |
|
601 |
"user: '" + username + "' has GID: " + |
|
602 |
gidNumber); |
|
603 |
} |
|
604 |
||
605 |
// get the supplementary groups from the group provider URL |
|
606 |
ctx = (DirContext)iCtx.lookup(groupProvider); |
|
607 |
ne = ctx.search("", new BasicAttributes("memberUid", username)); |
|
608 |
||
609 |
while (ne.hasMore()) { |
|
610 |
result = ne.next(); |
|
611 |
attributes = result.getAttributes(); |
|
612 |
||
613 |
gid = attributes.get(GROUP_ID); |
|
614 |
String suppGid = (String)gid.get(); |
|
615 |
if (!gidNumber.equals(suppGid)) { |
|
616 |
UnixNumericGroupPrincipal suppPrincipal = |
|
617 |
new UnixNumericGroupPrincipal(suppGid, false); |
|
618 |
supplementaryGroups.add(suppPrincipal); |
|
619 |
if (debug && suppGid != null) { |
|
620 |
System.out.println("\t\t[JndiLoginModule] " + |
|
621 |
"user: '" + username + |
|
622 |
"' has Supplementary Group: " + |
|
623 |
suppGid); |
|
624 |
} |
|
625 |
} |
|
626 |
} |
|
627 |
||
628 |
} else { |
|
629 |
// bad username |
|
630 |
if (debug) { |
|
631 |
System.out.println("\t\t[JndiLoginModule]: User not found"); |
|
632 |
} |
|
633 |
throw new FailedLoginException("User not found"); |
|
634 |
} |
|
635 |
} catch (NamingException ne) { |
|
636 |
// bad username |
|
637 |
if (debug) { |
|
638 |
System.out.println("\t\t[JndiLoginModule]: User not found"); |
|
639 |
ne.printStackTrace(); |
|
640 |
} |
|
641 |
throw new FailedLoginException("User not found"); |
|
642 |
} catch (java.io.UnsupportedEncodingException uee) { |
|
643 |
// password stored in incorrect format |
|
644 |
if (debug) { |
|
645 |
System.out.println("\t\t[JndiLoginModule]: " + |
|
646 |
"password incorrectly encoded"); |
|
647 |
uee.printStackTrace(); |
|
648 |
} |
|
649 |
throw new LoginException("Login failure due to incorrect " + |
|
650 |
"password encoding in the password database"); |
|
651 |
} |
|
652 |
||
653 |
// authentication succeeded |
|
654 |
} |
|
655 |
||
656 |
/** |
|
657 |
* Get the username and password. |
|
658 |
* This method does not return any value. |
|
659 |
* Instead, it sets global name and password variables. |
|
660 |
* |
|
661 |
* <p> Also note that this method will set the username and password |
|
662 |
* values in the shared state in case subsequent LoginModules |
|
663 |
* want to use them via use/tryFirstPass. |
|
664 |
* |
|
665 |
* @param getPasswdFromSharedState boolean that tells this method whether |
|
666 |
* to retrieve the password from the sharedState. |
|
667 |
*/ |
|
668 |
private void getUsernamePassword(boolean getPasswdFromSharedState) |
|
669 |
throws LoginException { |
|
670 |
||
671 |
if (getPasswdFromSharedState) { |
|
672 |
// use the password saved by the first module in the stack |
|
673 |
username = (String)sharedState.get(NAME); |
|
674 |
password = (char[])sharedState.get(PWD); |
|
675 |
return; |
|
676 |
} |
|
677 |
||
678 |
// prompt for a username and password |
|
679 |
if (callbackHandler == null) |
|
680 |
throw new LoginException("Error: no CallbackHandler available " + |
|
681 |
"to garner authentication information from the user"); |
|
682 |
||
24685
215fa91e1b4c
8044461: Cleanup new Boolean and single character strings
rriggs
parents:
23010
diff
changeset
|
683 |
String protocol = userProvider.substring(0, userProvider.indexOf(':')); |
2 | 684 |
|
685 |
Callback[] callbacks = new Callback[2]; |
|
686 |
callbacks[0] = new NameCallback(protocol + " " |
|
7179
4afb81e50183
6987827: security/util/Resources.java needs improvement
weijun
parents:
5506
diff
changeset
|
687 |
+ rb.getString("username.")); |
2 | 688 |
callbacks[1] = new PasswordCallback(protocol + " " + |
7179
4afb81e50183
6987827: security/util/Resources.java needs improvement
weijun
parents:
5506
diff
changeset
|
689 |
rb.getString("password."), |
2 | 690 |
false); |
691 |
||
692 |
try { |
|
693 |
callbackHandler.handle(callbacks); |
|
694 |
username = ((NameCallback)callbacks[0]).getName(); |
|
695 |
char[] tmpPassword = ((PasswordCallback)callbacks[1]).getPassword(); |
|
696 |
password = new char[tmpPassword.length]; |
|
697 |
System.arraycopy(tmpPassword, 0, |
|
698 |
password, 0, tmpPassword.length); |
|
699 |
((PasswordCallback)callbacks[1]).clearPassword(); |
|
700 |
||
701 |
} catch (java.io.IOException ioe) { |
|
702 |
throw new LoginException(ioe.toString()); |
|
703 |
} catch (UnsupportedCallbackException uce) { |
|
704 |
throw new LoginException("Error: " + uce.getCallback().toString() + |
|
705 |
" not available to garner authentication information " + |
|
706 |
"from the user"); |
|
707 |
} |
|
708 |
||
709 |
// print debugging information |
|
710 |
if (strongDebug) { |
|
711 |
System.out.println("\t\t[JndiLoginModule] " + |
|
712 |
"user entered username: " + |
|
713 |
username); |
|
714 |
System.out.print("\t\t[JndiLoginModule] " + |
|
715 |
"user entered password: "); |
|
716 |
for (int i = 0; i < password.length; i++) |
|
717 |
System.out.print(password[i]); |
|
718 |
System.out.println(); |
|
719 |
} |
|
720 |
} |
|
721 |
||
722 |
/** |
|
723 |
* Verify a password against the encrypted passwd from /etc/shadow |
|
724 |
*/ |
|
725 |
private boolean verifyPassword(String encryptedPassword, String password) { |
|
726 |
||
727 |
if (encryptedPassword == null) |
|
728 |
return false; |
|
729 |
||
730 |
Crypt c = new Crypt(); |
|
731 |
try { |
|
732 |
byte oldCrypt[] = encryptedPassword.getBytes("UTF8"); |
|
733 |
byte newCrypt[] = c.crypt(password.getBytes("UTF8"), |
|
734 |
oldCrypt); |
|
735 |
if (newCrypt.length != oldCrypt.length) |
|
736 |
return false; |
|
737 |
for (int i = 0; i < newCrypt.length; i++) { |
|
738 |
if (oldCrypt[i] != newCrypt[i]) |
|
739 |
return false; |
|
740 |
} |
|
741 |
} catch (java.io.UnsupportedEncodingException uee) { |
|
742 |
// cannot happen, but return false just to be safe |
|
743 |
return false; |
|
744 |
} |
|
745 |
return true; |
|
746 |
} |
|
747 |
||
748 |
/** |
|
749 |
* Clean out state because of a failed authentication attempt |
|
750 |
*/ |
|
751 |
private void cleanState() { |
|
752 |
username = null; |
|
753 |
if (password != null) { |
|
754 |
for (int i = 0; i < password.length; i++) |
|
755 |
password[i] = ' '; |
|
756 |
password = null; |
|
757 |
} |
|
758 |
ctx = null; |
|
759 |
||
760 |
if (clearPass) { |
|
761 |
sharedState.remove(NAME); |
|
762 |
sharedState.remove(PWD); |
|
763 |
} |
|
764 |
} |
|
765 |
} |