author | duke |
Wed, 05 Jul 2017 21:06:18 +0200 | |
changeset 34465 | 41a1258588da |
parent 30044 | bab15bbe2ca3 |
child 34894 | 3248b89d1921 |
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 java.util.*; |
|
29 |
import java.io.IOException; |
|
30 |
import javax.security.auth.*; |
|
31 |
import javax.security.auth.callback.*; |
|
32 |
import javax.security.auth.login.*; |
|
33 |
import javax.security.auth.spi.*; |
|
34 |
import com.sun.security.auth.SolarisPrincipal; |
|
35 |
import com.sun.security.auth.SolarisNumericUserPrincipal; |
|
36 |
import com.sun.security.auth.SolarisNumericGroupPrincipal; |
|
37 |
||
38 |
/** |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
39 |
* This {@code LoginModule} imports a user's Solaris |
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
40 |
* {@code Principal} information ({@code SolarisPrincipal}, |
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
41 |
* {@code SolarisNumericUserPrincipal}, |
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
42 |
* and {@code SolarisNumericGroupPrincipal}) |
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
43 |
* and associates them with the current {@code Subject}. |
2 | 44 |
* |
45 |
* <p> This LoginModule recognizes the debug option. |
|
46 |
* If set to true in the login Configuration, |
|
47 |
* debug messages will be output to the output stream, System.out. |
|
48 |
* @deprecated As of JDK1.4, replaced by |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
49 |
* {@code com.sun.security.auth.module.UnixLoginModule}. |
2 | 50 |
* This LoginModule is entirely deprecated and |
51 |
* is here to allow for a smooth transition to the new |
|
52 |
* UnixLoginModule. |
|
53 |
* |
|
54 |
*/ |
|
20742
4ae78e8060d6
8008662: Add @jdk.Exported to JDK-specific/exported APIs
alanb
parents:
9035
diff
changeset
|
55 |
@jdk.Exported(false) |
2 | 56 |
@Deprecated |
57 |
public class SolarisLoginModule implements LoginModule { |
|
58 |
||
59 |
// initial state |
|
60 |
private Subject subject; |
|
61 |
private CallbackHandler callbackHandler; |
|
62 |
private Map<String, ?> sharedState; |
|
63 |
private Map<String, ?> options; |
|
64 |
||
65 |
// configurable option |
|
66 |
private boolean debug = true; |
|
67 |
||
68 |
// SolarisSystem to retrieve underlying system info |
|
69 |
private SolarisSystem ss; |
|
70 |
||
71 |
// the authentication status |
|
72 |
private boolean succeeded = false; |
|
73 |
private boolean commitSucceeded = false; |
|
74 |
||
75 |
// Underlying system info |
|
76 |
private SolarisPrincipal userPrincipal; |
|
77 |
private SolarisNumericUserPrincipal UIDPrincipal; |
|
78 |
private SolarisNumericGroupPrincipal GIDPrincipal; |
|
79 |
private LinkedList<SolarisNumericGroupPrincipal> supplementaryGroups = |
|
7970
af1579474d16
7008728: diamond conversion of basic security, permissions, authentication
smarks
parents:
5506
diff
changeset
|
80 |
new LinkedList<>(); |
2 | 81 |
|
82 |
/** |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
83 |
* Initialize this {@code LoginModule}. |
2 | 84 |
* |
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
85 |
* @param subject the {@code Subject} to be authenticated. |
2 | 86 |
* |
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
87 |
* @param callbackHandler a {@code CallbackHandler} for communicating |
2 | 88 |
* with the end user (prompting for usernames and |
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
89 |
* passwords, for example). |
2 | 90 |
* |
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
91 |
* @param sharedState shared {@code LoginModule} state. |
2 | 92 |
* |
93 |
* @param options options specified in the login |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
94 |
* {@code Configuration} for this particular |
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
95 |
* {@code LoginModule}. |
2 | 96 |
*/ |
97 |
public void initialize(Subject subject, CallbackHandler callbackHandler, |
|
98 |
Map<String,?> sharedState, |
|
99 |
Map<String,?> options) |
|
100 |
{ |
|
101 |
||
102 |
this.subject = subject; |
|
103 |
this.callbackHandler = callbackHandler; |
|
104 |
this.sharedState = sharedState; |
|
105 |
this.options = options; |
|
106 |
||
107 |
// initialize any configured options |
|
108 |
debug = "true".equalsIgnoreCase((String)options.get("debug")); |
|
109 |
} |
|
110 |
||
111 |
/** |
|
112 |
* Authenticate the user (first phase). |
|
113 |
* |
|
114 |
* <p> The implementation of this method attempts to retrieve the user's |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
115 |
* Solaris {@code Subject} information by making a native Solaris |
2 | 116 |
* system call. |
117 |
* |
|
118 |
* @exception FailedLoginException if attempts to retrieve the underlying |
|
119 |
* system information fail. |
|
120 |
* |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
121 |
* @return true in all cases (this {@code LoginModule} |
2 | 122 |
* should not be ignored). |
123 |
*/ |
|
124 |
public boolean login() throws LoginException { |
|
125 |
||
126 |
long[] solarisGroups = null; |
|
127 |
||
24271
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
128 |
try { |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
129 |
ss = new SolarisSystem(); |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
130 |
} catch (UnsatisfiedLinkError ule) { |
2 | 131 |
succeeded = false; |
132 |
throw new FailedLoginException |
|
133 |
("Failed in attempt to import " + |
|
24271
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
134 |
"the underlying system identity information" + |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
135 |
" on " + System.getProperty("os.name")); |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
136 |
} |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
137 |
userPrincipal = new SolarisPrincipal(ss.getUsername()); |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
138 |
UIDPrincipal = new SolarisNumericUserPrincipal(ss.getUid()); |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
139 |
GIDPrincipal = new SolarisNumericGroupPrincipal(ss.getGid(), true); |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
140 |
if (ss.getGroups() != null && ss.getGroups().length > 0) |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
141 |
solarisGroups = ss.getGroups(); |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
142 |
for (int i = 0; i < solarisGroups.length; i++) { |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
143 |
SolarisNumericGroupPrincipal ngp = |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
144 |
new SolarisNumericGroupPrincipal |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
145 |
(solarisGroups[i], false); |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
146 |
if (!ngp.getName().equals(GIDPrincipal.getName())) |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
147 |
supplementaryGroups.add(ngp); |
2 | 148 |
} |
24271
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
149 |
if (debug) { |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
150 |
System.out.println("\t\t[SolarisLoginModule]: " + |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
151 |
"succeeded importing info: "); |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
152 |
System.out.println("\t\t\tuid = " + ss.getUid()); |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
153 |
System.out.println("\t\t\tgid = " + ss.getGid()); |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
154 |
solarisGroups = ss.getGroups(); |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
155 |
for (int i = 0; i < solarisGroups.length; i++) { |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
156 |
System.out.println("\t\t\tsupp gid = " + solarisGroups[i]); |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
157 |
} |
2 | 158 |
} |
24271
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
159 |
succeeded = true; |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
23010
diff
changeset
|
160 |
return true; |
2 | 161 |
} |
162 |
||
163 |
/** |
|
164 |
* Commit the authentication (second phase). |
|
165 |
* |
|
166 |
* <p> This method is called if the LoginContext's |
|
167 |
* overall authentication succeeded |
|
168 |
* (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules |
|
169 |
* succeeded). |
|
170 |
* |
|
171 |
* <p> If this LoginModule's own authentication attempt |
|
172 |
* succeeded (the importing of the Solaris authentication information |
|
173 |
* succeeded), then this method associates the Solaris Principals |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
174 |
* with the {@code Subject} currently tied to the |
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
175 |
* {@code LoginModule}. If this LoginModule's |
2 | 176 |
* authentication attempted failed, then this method removes |
177 |
* any state that was originally saved. |
|
178 |
* |
|
179 |
* @exception LoginException if the commit fails |
|
180 |
* |
|
181 |
* @return true if this LoginModule's own login and commit attempts |
|
182 |
* succeeded, or false otherwise. |
|
183 |
*/ |
|
184 |
public boolean commit() throws LoginException { |
|
185 |
if (succeeded == false) { |
|
186 |
if (debug) { |
|
187 |
System.out.println("\t\t[SolarisLoginModule]: " + |
|
188 |
"did not add any Principals to Subject " + |
|
189 |
"because own authentication failed."); |
|
190 |
} |
|
191 |
return false; |
|
192 |
} |
|
193 |
if (subject.isReadOnly()) { |
|
194 |
throw new LoginException ("Subject is Readonly"); |
|
195 |
} |
|
196 |
if (!subject.getPrincipals().contains(userPrincipal)) |
|
197 |
subject.getPrincipals().add(userPrincipal); |
|
198 |
if (!subject.getPrincipals().contains(UIDPrincipal)) |
|
199 |
subject.getPrincipals().add(UIDPrincipal); |
|
200 |
if (!subject.getPrincipals().contains(GIDPrincipal)) |
|
201 |
subject.getPrincipals().add(GIDPrincipal); |
|
202 |
for (int i = 0; i < supplementaryGroups.size(); i++) { |
|
203 |
if (!subject.getPrincipals().contains(supplementaryGroups.get(i))) |
|
204 |
subject.getPrincipals().add(supplementaryGroups.get(i)); |
|
205 |
} |
|
206 |
||
207 |
if (debug) { |
|
208 |
System.out.println("\t\t[SolarisLoginModule]: " + |
|
209 |
"added SolarisPrincipal,"); |
|
210 |
System.out.println("\t\t\t\tSolarisNumericUserPrincipal,"); |
|
211 |
System.out.println("\t\t\t\tSolarisNumericGroupPrincipal(s),"); |
|
212 |
System.out.println("\t\t\t to Subject"); |
|
213 |
} |
|
214 |
||
215 |
commitSucceeded = true; |
|
216 |
return true; |
|
217 |
} |
|
218 |
||
219 |
||
220 |
/** |
|
221 |
* Abort the authentication (second phase). |
|
222 |
* |
|
223 |
* <p> This method is called if the LoginContext's |
|
224 |
* overall authentication failed. |
|
225 |
* (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules |
|
226 |
* did not succeed). |
|
227 |
* |
|
228 |
* <p> This method cleans up any state that was originally saved |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
229 |
* as part of the authentication attempt from the {@code login} |
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
230 |
* and {@code commit} methods. |
2 | 231 |
* |
232 |
* @exception LoginException if the abort fails |
|
233 |
* |
|
234 |
* @return false if this LoginModule's own login and/or commit attempts |
|
235 |
* failed, and true otherwise. |
|
236 |
*/ |
|
237 |
public boolean abort() throws LoginException { |
|
238 |
if (debug) { |
|
239 |
System.out.println("\t\t[SolarisLoginModule]: " + |
|
240 |
"aborted authentication attempt"); |
|
241 |
} |
|
242 |
||
243 |
if (succeeded == false) { |
|
244 |
return false; |
|
245 |
} else if (succeeded == true && commitSucceeded == false) { |
|
246 |
||
247 |
// Clean out state |
|
248 |
succeeded = false; |
|
249 |
ss = null; |
|
250 |
userPrincipal = null; |
|
251 |
UIDPrincipal = null; |
|
252 |
GIDPrincipal = null; |
|
253 |
supplementaryGroups = |
|
254 |
new LinkedList<SolarisNumericGroupPrincipal>(); |
|
255 |
} else { |
|
256 |
// overall authentication succeeded and commit succeeded, |
|
257 |
// but someone else's commit failed |
|
258 |
logout(); |
|
259 |
} |
|
260 |
return true; |
|
261 |
} |
|
262 |
||
263 |
/** |
|
264 |
* Logout the user |
|
265 |
* |
|
266 |
* <p> This method removes the Principals associated |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
267 |
* with the {@code Subject}. |
2 | 268 |
* |
269 |
* @exception LoginException if the logout fails |
|
270 |
* |
|
30044
bab15bbe2ca3
8078528: clean out tidy warnings from security.auth
avstepan
parents:
25859
diff
changeset
|
271 |
* @return true in all cases (this {@code LoginModule} |
2 | 272 |
* should not be ignored). |
273 |
*/ |
|
274 |
public boolean logout() throws LoginException { |
|
275 |
if (debug) { |
|
276 |
System.out.println("\t\t[SolarisLoginModule]: " + |
|
277 |
"Entering logout"); |
|
278 |
} |
|
279 |
if (subject.isReadOnly()) { |
|
280 |
throw new LoginException ("Subject is Readonly"); |
|
281 |
} |
|
282 |
// remove the added Principals from the Subject |
|
283 |
subject.getPrincipals().remove(userPrincipal); |
|
284 |
subject.getPrincipals().remove(UIDPrincipal); |
|
285 |
subject.getPrincipals().remove(GIDPrincipal); |
|
286 |
for (int i = 0; i < supplementaryGroups.size(); i++) { |
|
287 |
subject.getPrincipals().remove(supplementaryGroups.get(i)); |
|
288 |
} |
|
289 |
||
290 |
// clean out state |
|
291 |
ss = null; |
|
292 |
succeeded = false; |
|
293 |
commitSucceeded = false; |
|
294 |
userPrincipal = null; |
|
295 |
UIDPrincipal = null; |
|
296 |
GIDPrincipal = null; |
|
297 |
supplementaryGroups = new LinkedList<SolarisNumericGroupPrincipal>(); |
|
298 |
||
299 |
if (debug) { |
|
300 |
System.out.println("\t\t[SolarisLoginModule]: " + |
|
301 |
"logged out Subject"); |
|
302 |
} |
|
303 |
return true; |
|
304 |
} |
|
305 |
} |