1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2013 Regents of the University of California and the
|
4
|
* National Center for Ecological Analysis and Synthesis
|
5
|
*
|
6
|
*
|
7
|
* This program is free software; you can redistribute it and/or modify
|
8
|
* it under the terms of the GNU General Public License as published by
|
9
|
* the Free Software Foundation; either version 2 of the License, or
|
10
|
* (at your option) any later version.
|
11
|
*
|
12
|
* This program is distributed in the hope that it will be useful,
|
13
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15
|
* GNU General Public License for more details.
|
16
|
*
|
17
|
* You should have received a copy of the GNU General Public License
|
18
|
* along with this program; if not, write to the Free Software
|
19
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
20
|
*/
|
21
|
package edu.ucsb.nceas.metacat.authentication;
|
22
|
|
23
|
import java.io.Console;
|
24
|
import java.io.File;
|
25
|
import java.io.FileOutputStream;
|
26
|
import java.io.IOException;
|
27
|
import java.io.OutputStreamWriter;
|
28
|
import java.io.UnsupportedEncodingException;
|
29
|
import java.net.ConnectException;
|
30
|
import java.util.HashMap;
|
31
|
import java.util.List;
|
32
|
import java.util.Vector;
|
33
|
|
34
|
|
35
|
|
36
|
import org.apache.commons.configuration.ConfigurationException;
|
37
|
import org.apache.commons.configuration.XMLConfiguration;
|
38
|
import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
|
39
|
import org.apache.commons.logging.Log;
|
40
|
import org.apache.commons.logging.LogFactory;
|
41
|
|
42
|
import edu.ucsb.nceas.metacat.AuthInterface;
|
43
|
import edu.ucsb.nceas.metacat.AuthLdap;
|
44
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
45
|
import edu.ucsb.nceas.metacat.util.SystemUtil;
|
46
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
47
|
|
48
|
/**
|
49
|
* This an authentication class base on a username/password file.
|
50
|
* It is an alternative authentication mechanism of the ldap authentication.
|
51
|
* This is a singleton class and the password file looks like:
|
52
|
*<?xml version="1.0" encoding="UTF-8" ?>
|
53
|
* <subjects>
|
54
|
* <users>
|
55
|
* <user dn="uid=tao,o=NCEAS,dc=ecoinformatics,dc=org">
|
56
|
* <password>*******</password>
|
57
|
* <email>foo@foo.com</email>
|
58
|
* <surName>Smith</surName>
|
59
|
* <givenName>John</givenName>
|
60
|
* <group>nceas-dev</group>
|
61
|
* </user>
|
62
|
* </users>
|
63
|
* <groups>
|
64
|
* <group name="nceas-dev">
|
65
|
* <description>developers at NCEAS</description>
|
66
|
* </group>
|
67
|
* </groups>
|
68
|
* </subjects>
|
69
|
* http://commons.apache.org/proper/commons-configuration/userguide/howto_xml.html
|
70
|
* @author tao
|
71
|
*
|
72
|
*/
|
73
|
public class AuthFile implements AuthInterface {
|
74
|
private static final String ORGANIZATIONNAME = "UNkown";
|
75
|
private static final String ORGANIZATION = "organization";
|
76
|
private static final String NAME = "name";
|
77
|
private static final String DN = "dn";
|
78
|
private static final String DESCRIPTION = "description";
|
79
|
private static final String PASSWORD = "password";
|
80
|
private static final String SLASH = "/";
|
81
|
private static final String AT = "@";
|
82
|
private static final String SUBJECTS = "subjects";
|
83
|
private static final String USERS = "users";
|
84
|
private static final String USER = "user";
|
85
|
private static final String GROUPS = "groups";
|
86
|
private static final String GROUP = "group";
|
87
|
private static final String EMAIL = "email";
|
88
|
private static final String SURNAME = "surName";
|
89
|
private static final String GIVENNAME = "givenName";
|
90
|
private static final String INITCONTENT = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"+
|
91
|
"<"+SUBJECTS+">\n"+"<"+USERS+">\n"+"</"+USERS+">\n"+"<"+GROUPS+">\n"+"</"+GROUPS+">\n"+"</"+SUBJECTS+">\n";
|
92
|
|
93
|
|
94
|
|
95
|
private static Log log = LogFactory.getLog(AuthFile.class);
|
96
|
private static AuthFile authFile = null;
|
97
|
private static XMLConfiguration userpassword = null;
|
98
|
private String authURI = null;
|
99
|
private static String passwordFilePath = null;
|
100
|
private static AuthFileHashInterface hashClass = null;
|
101
|
/**
|
102
|
* Get the instance of the AuthFile
|
103
|
* @return
|
104
|
* @throws AuthenticationException
|
105
|
*/
|
106
|
public static AuthFile getInstance() throws AuthenticationException {
|
107
|
if(authFile == null) {
|
108
|
authFile = new AuthFile();
|
109
|
}
|
110
|
return authFile;
|
111
|
}
|
112
|
|
113
|
/**
|
114
|
* Get the instance of the AuthFile from specified password file
|
115
|
* @return
|
116
|
* @throws AuthenticationException
|
117
|
*/
|
118
|
public static AuthFile getInstance(String passwordFile) throws AuthenticationException {
|
119
|
passwordFilePath = passwordFile;
|
120
|
if(authFile == null) {
|
121
|
authFile = new AuthFile();
|
122
|
}
|
123
|
return authFile;
|
124
|
}
|
125
|
|
126
|
/**
|
127
|
* Constructor
|
128
|
*/
|
129
|
private AuthFile() throws AuthenticationException {
|
130
|
try {
|
131
|
init();
|
132
|
} catch (Exception e) {
|
133
|
e.printStackTrace();
|
134
|
throw new AuthenticationException(e.getMessage());
|
135
|
}
|
136
|
|
137
|
}
|
138
|
|
139
|
/*
|
140
|
* Initialize the user/password configuration
|
141
|
*/
|
142
|
private void init() throws PropertyNotFoundException, IOException, ConfigurationException, ClassNotFoundException, InstantiationException, IllegalAccessException {
|
143
|
if(passwordFilePath == null) {
|
144
|
passwordFilePath = PropertyService.getProperty("auth.file.path");
|
145
|
}
|
146
|
File passwordFile = new File(passwordFilePath);
|
147
|
|
148
|
authURI = SystemUtil.getContextURL();
|
149
|
String hashClassName = PropertyService.getProperty("auth.file.hashClassName");
|
150
|
Class classDefinition = Class.forName(hashClassName);
|
151
|
Object object = classDefinition.newInstance();
|
152
|
hashClass = (AuthFileHashInterface) object;
|
153
|
|
154
|
//if the password file doesn't exist, create a new one and set the initial content
|
155
|
if(!passwordFile.exists()) {
|
156
|
File parent = passwordFile.getParentFile();
|
157
|
if(!parent.exists()) {
|
158
|
parent.mkdirs();
|
159
|
}
|
160
|
passwordFile.createNewFile();
|
161
|
OutputStreamWriter writer = null;
|
162
|
FileOutputStream output = null;
|
163
|
try {
|
164
|
output = new FileOutputStream(passwordFile);
|
165
|
writer = new OutputStreamWriter(output, "UTF-8");
|
166
|
writer.write(INITCONTENT);
|
167
|
} finally {
|
168
|
writer.close();
|
169
|
output.close();
|
170
|
}
|
171
|
}
|
172
|
userpassword = new XMLConfiguration(passwordFile);
|
173
|
userpassword.setExpressionEngine(new XPathExpressionEngine());
|
174
|
userpassword.setAutoSave(true);
|
175
|
userpassword.setDelimiterParsingDisabled(true);
|
176
|
userpassword.setAttributeSplittingDisabled(true);
|
177
|
}
|
178
|
|
179
|
@Override
|
180
|
public boolean authenticate(String user, String password)
|
181
|
throws AuthenticationException {
|
182
|
boolean match = false;
|
183
|
String passwordRecord = userpassword.getString(USERS+SLASH+USER+"["+AT+DN+"='"+user+"']"+SLASH+PASSWORD);
|
184
|
if(passwordRecord != null) {
|
185
|
try {
|
186
|
match = hashClass.match(password, passwordRecord);
|
187
|
} catch (Exception e) {
|
188
|
throw new AuthenticationException(e.getMessage());
|
189
|
}
|
190
|
|
191
|
}
|
192
|
return match;
|
193
|
}
|
194
|
|
195
|
@Override
|
196
|
/**
|
197
|
* Get all users. This is two-dimmention array. Each row is a user. The first element of
|
198
|
* a row is the user name. The second element is common name. The third one is the organization name (null).
|
199
|
* The fourth one is the organization unit name (null). The fifth one is the email address.
|
200
|
*/
|
201
|
public String[][] getUsers(String user, String password)
|
202
|
throws ConnectException {
|
203
|
List<Object> users = userpassword.getList(USERS+SLASH+USER+SLASH+AT+DN);
|
204
|
if(users != null && users.size() > 0) {
|
205
|
String[][] usersArray = new String[users.size()][5];
|
206
|
for(int i=0; i<users.size(); i++) {
|
207
|
|
208
|
String dn = (String)users.get(i);
|
209
|
usersArray[i][AuthInterface.USERDNINDEX] = dn; //dn
|
210
|
String[] userInfo = getUserInfo(dn, password);
|
211
|
usersArray[i][AuthInterface.USERCNINDEX] = userInfo[AuthInterface.USERINFOCNINDEX];//common name
|
212
|
usersArray[i][AuthInterface.USERORGINDEX] = userInfo[AuthInterface.USERINFOORGANIDEX];//organization name. We set null
|
213
|
usersArray[i][AuthInterface.USERORGUNITINDEX] = null;//organization ou name. We set null.
|
214
|
usersArray[i][AuthInterface.USEREMAILINDEX] = userInfo[AuthInterface.USERINFOEMAILINDEX];
|
215
|
|
216
|
}
|
217
|
return usersArray;
|
218
|
}
|
219
|
return null;
|
220
|
}
|
221
|
|
222
|
@Override
|
223
|
/**
|
224
|
* Get an array about the user. The first column is the common name, the second column is the organization name.
|
225
|
* The third column is the email address. It always returns an array. But the elements of the array can be null.
|
226
|
*/
|
227
|
public String[] getUserInfo(String user, String password)
|
228
|
throws ConnectException {
|
229
|
String[] userinfo = new String[3];
|
230
|
User aUser = new User();
|
231
|
aUser.setDN(user);
|
232
|
String surname = null;
|
233
|
List<Object> surNames = userpassword.getList(USERS+SLASH+USER+"["+AT+DN+"='"+user+"']"+SLASH+SURNAME);
|
234
|
if(surNames != null && !surNames.isEmpty()) {
|
235
|
surname = (String)surNames.get(0);
|
236
|
}
|
237
|
aUser.setSurName(surname);
|
238
|
String givenName = null;
|
239
|
List<Object> givenNames = userpassword.getList(USERS+SLASH+USER+"["+AT+DN+"='"+user+"']"+SLASH+GIVENNAME);
|
240
|
if(givenNames != null && !givenNames.isEmpty()) {
|
241
|
givenName = (String)givenNames.get(0);
|
242
|
}
|
243
|
aUser.setGivenName(givenName);
|
244
|
userinfo[AuthInterface.USERINFOCNINDEX] = aUser.getCn();//common name
|
245
|
String organization = null;
|
246
|
List<Object> organizations = userpassword.getList(USERS+SLASH+USER+"["+AT+DN+"='"+user+"']"+SLASH+ORGANIZATION);
|
247
|
if(organizations != null && !organizations.isEmpty()) {
|
248
|
organization = (String)organizations.get(0);
|
249
|
}
|
250
|
userinfo[AuthInterface.USERINFOORGANIDEX] = organization;//organization name.
|
251
|
aUser.setOrganization(organization);
|
252
|
List<Object> emails = userpassword.getList(USERS+SLASH+USER+"["+AT+DN+"='"+user+"']"+SLASH+EMAIL);
|
253
|
String email = null;
|
254
|
if(emails != null && !emails.isEmpty() ) {
|
255
|
email = (String)emails.get(0);
|
256
|
}
|
257
|
aUser.setEmail(email);
|
258
|
userinfo[AuthInterface.USERINFOEMAILINDEX] = email;
|
259
|
return userinfo;
|
260
|
}
|
261
|
|
262
|
|
263
|
@Override
|
264
|
/**
|
265
|
* Get the users for a particular group from the authentication service
|
266
|
* The null will return if there is no user.
|
267
|
* @param user
|
268
|
* the user for authenticating against the service
|
269
|
* @param password
|
270
|
* the password for authenticating against the service
|
271
|
* @param group
|
272
|
* the group whose user list should be returned
|
273
|
* @returns string array of the user names belonging to the group
|
274
|
*/
|
275
|
public String[] getUsers(String user, String password, String group)
|
276
|
throws ConnectException {
|
277
|
List<Object> users = userpassword.getList(USERS+SLASH+USER+"["+GROUP+"='"+group+"']"+SLASH+AT+DN);
|
278
|
if(users != null && users.size() > 0) {
|
279
|
String[] usersArray = new String[users.size()];
|
280
|
for(int i=0; i<users.size(); i++) {
|
281
|
usersArray[i] = (String) users.get(i);
|
282
|
}
|
283
|
return usersArray;
|
284
|
}
|
285
|
return null;
|
286
|
}
|
287
|
|
288
|
@Override
|
289
|
/**
|
290
|
* Get all groups from the authentication service. It returns a two dimmension array. Each row is a
|
291
|
* group. The first column is the group name. The second column is the description. The null will return if no group found.
|
292
|
*/
|
293
|
public String[][] getGroups(String user, String password)
|
294
|
throws ConnectException {
|
295
|
List<Object> groups = userpassword.getList(GROUPS+SLASH+GROUP+SLASH+AT+NAME);
|
296
|
if(groups!= null && groups.size() >0) {
|
297
|
String[][] groupsArray = new String[groups.size()][2];
|
298
|
for(int i=0; i<groups.size(); i++) {
|
299
|
String groupName = (String) groups.get(i);
|
300
|
groupsArray[i][AuthInterface.GROUPNAMEINDEX] = groupName;
|
301
|
String description = null;
|
302
|
List<Object>descriptions = userpassword.getList(GROUPS+SLASH+GROUP+"["+AT+NAME+"='"+groupName+"']"+SLASH+DESCRIPTION);
|
303
|
if(descriptions != null && !descriptions.isEmpty()) {
|
304
|
description = (String)descriptions.get(0);
|
305
|
}
|
306
|
groupsArray[i][AuthInterface.GROUPDESINDEX] = description;
|
307
|
}
|
308
|
return groupsArray;
|
309
|
}
|
310
|
return null;
|
311
|
}
|
312
|
|
313
|
@Override
|
314
|
/**
|
315
|
* Get groups from a specified user. It returns two dimmension array. Each row is a
|
316
|
* group. The first column is the group name. The null will return if no group found.
|
317
|
*/
|
318
|
public String[][] getGroups(String user, String password, String foruser)
|
319
|
throws ConnectException {
|
320
|
List<Object> groups = userpassword.getList(USERS+SLASH+USER+"["+AT+DN+"='"+foruser+"']"+SLASH+GROUP);
|
321
|
if(groups != null && groups.size() > 0) {
|
322
|
String[][] groupsArray = new String[groups.size()][2];
|
323
|
for(int i=0; i<groups.size(); i++) {
|
324
|
String groupName = (String) groups.get(i);
|
325
|
groupsArray[i][AuthInterface.GROUPNAMEINDEX] = groupName;
|
326
|
String description = null;
|
327
|
List<Object>descriptions = userpassword.getList(GROUPS+SLASH+GROUP+"["+AT+NAME+"='"+groupName+"']"+SLASH+DESCRIPTION);
|
328
|
if(descriptions != null && !descriptions.isEmpty()) {
|
329
|
description = (String)descriptions.get(0);
|
330
|
}
|
331
|
groupsArray[i][AuthInterface.GROUPDESINDEX] = description;
|
332
|
}
|
333
|
return groupsArray;
|
334
|
}
|
335
|
return null;
|
336
|
}
|
337
|
|
338
|
@Override
|
339
|
public HashMap<String, Vector<String>> getAttributes(String foruser)
|
340
|
throws ConnectException {
|
341
|
// TODO Auto-generated method stub
|
342
|
return null;
|
343
|
}
|
344
|
|
345
|
@Override
|
346
|
public HashMap<String, Vector<String>> getAttributes(String user,
|
347
|
String password, String foruser) throws ConnectException {
|
348
|
// TODO Auto-generated method stub
|
349
|
return null;
|
350
|
}
|
351
|
|
352
|
@Override
|
353
|
public String getPrincipals(String user, String password)
|
354
|
throws ConnectException {
|
355
|
StringBuffer out = new StringBuffer();
|
356
|
|
357
|
out.append("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
|
358
|
out.append("<principals>\n");
|
359
|
out.append(" <authSystem URI=\"" +authURI
|
360
|
+ "\" organization=\"" + ORGANIZATIONNAME + "\">\n");
|
361
|
|
362
|
// get all groups for directory context
|
363
|
String[][] groups = getGroups(user, password);
|
364
|
String[][] users = getUsers(user, password);
|
365
|
int userIndex = 0;
|
366
|
|
367
|
// for the groups and users that belong to them
|
368
|
if (groups != null && users != null && groups.length > 0) {
|
369
|
for (int i = 0; i < groups.length; i++) {
|
370
|
out.append(" <group>\n");
|
371
|
out.append(" <groupname>" + groups[i][AuthInterface.GROUPNAMEINDEX] + "</groupname>\n");
|
372
|
if(groups[i].length > 1) {
|
373
|
out.append(" <description>" + groups[i][AuthInterface.GROUPDESINDEX] + "</description>\n");
|
374
|
}
|
375
|
String[] usersForGroup = getUsers(user, password, groups[i][0]);
|
376
|
if(usersForGroup != null) {
|
377
|
for (int j = 0; j < usersForGroup.length; j++) {
|
378
|
userIndex = AuthLdap.searchUser(usersForGroup[j], users);
|
379
|
out.append(" <user>\n");
|
380
|
|
381
|
if (userIndex < 0) {
|
382
|
out.append(" <username>" + usersForGroup[j]
|
383
|
+ "</username>\n");
|
384
|
} else {
|
385
|
out.append(" <username>" + users[userIndex][0]
|
386
|
+ "</username>\n");
|
387
|
if(users[userIndex][AuthInterface.USERCNINDEX] != null) {
|
388
|
out.append(" <name>" + users[userIndex][AuthInterface.USERCNINDEX]
|
389
|
+ "</name>\n");
|
390
|
}
|
391
|
if(users[userIndex][AuthInterface.USERORGINDEX] != null) {
|
392
|
out.append(" <organization>" + users[userIndex][AuthInterface.USERORGINDEX]
|
393
|
+ "</organization>\n");
|
394
|
}
|
395
|
|
396
|
if(users[userIndex][AuthInterface.USERORGUNITINDEX] != null) {
|
397
|
out.append(" <organizationUnitName>"
|
398
|
+ users[userIndex][AuthInterface.USERORGUNITINDEX]
|
399
|
+ "</organizationUnitName>\n");
|
400
|
}
|
401
|
if(users[userIndex][AuthInterface.USEREMAILINDEX] != null) {
|
402
|
out.append(" <email>" + users[userIndex][AuthInterface.USEREMAILINDEX]
|
403
|
+ "</email>\n");
|
404
|
}
|
405
|
|
406
|
}
|
407
|
|
408
|
out.append(" </user>\n");
|
409
|
}
|
410
|
}
|
411
|
|
412
|
out.append(" </group>\n");
|
413
|
}
|
414
|
}
|
415
|
|
416
|
if (users != null) {
|
417
|
// for the users not belonging to any grou8p
|
418
|
for (int j = 0; j < users.length; j++) {
|
419
|
out.append(" <user>\n");
|
420
|
out.append(" <username>" + users[j][0] + "</username>\n");
|
421
|
if(users[j][AuthInterface.USERCNINDEX] != null) {
|
422
|
out.append(" <name>" + users[j][AuthInterface.USERCNINDEX]
|
423
|
+ "</name>\n");
|
424
|
}
|
425
|
if(users[j][AuthInterface.USERORGINDEX] != null) {
|
426
|
out.append(" <organization>" + users[j][AuthInterface.USERORGINDEX]
|
427
|
+ "</organization>\n");
|
428
|
}
|
429
|
|
430
|
if(users[j][AuthInterface.USERORGUNITINDEX] != null) {
|
431
|
out.append(" <organizationUnitName>"
|
432
|
+ users[j][AuthInterface.USERORGUNITINDEX]
|
433
|
+ "</organizationUnitName>\n");
|
434
|
}
|
435
|
if(users[j][AuthInterface.USEREMAILINDEX] != null) {
|
436
|
out.append(" <email>" + users[j][AuthInterface.USEREMAILINDEX]
|
437
|
+ "</email>\n");
|
438
|
}
|
439
|
|
440
|
out.append(" </user>\n");
|
441
|
}
|
442
|
}
|
443
|
|
444
|
out.append(" </authSystem>\n");
|
445
|
|
446
|
out.append("</principals>");
|
447
|
return out.toString();
|
448
|
}
|
449
|
|
450
|
/**
|
451
|
* Add a user to the file
|
452
|
* @param userName the name of the user
|
453
|
* @param groups the groups the user belong to. The group should exist in the file
|
454
|
* @param password the password of the user
|
455
|
*/
|
456
|
public void addUser(String dn, String[] groups, String plainPass, String hashedPass, String email, String surName, String givenName, String organization) throws AuthenticationException{
|
457
|
User user = new User();
|
458
|
user.setDN(dn);
|
459
|
user.setGroups(groups);
|
460
|
user.setPlainPass(plainPass);
|
461
|
user.setHashedPass(hashedPass);
|
462
|
user.setEmail(email);
|
463
|
user.setSurName(surName);
|
464
|
user.setGivenName(givenName);
|
465
|
user.setOrganization(organization);
|
466
|
user.serialize();
|
467
|
}
|
468
|
|
469
|
/**
|
470
|
* Add a group into the file
|
471
|
* @param groupName the name of group
|
472
|
*/
|
473
|
public void addGroup(String groupName, String description) throws AuthenticationException{
|
474
|
if(groupName == null || groupName.trim().equals("")) {
|
475
|
throw new AuthenticationException("AuthFile.addGroup - can't add a group whose name is null or blank.");
|
476
|
}
|
477
|
if(!groupExists(groupName)) {
|
478
|
if(userpassword != null) {
|
479
|
userpassword.addProperty(GROUPS+" "+GROUP+AT+NAME, groupName);
|
480
|
if(description != null && !description.trim().equals("")) {
|
481
|
userpassword.addProperty(GROUPS+SLASH+GROUP+"["+AT+NAME+"='"+groupName+"']"+" "+DESCRIPTION, description);
|
482
|
}
|
483
|
//userpassword.reload();
|
484
|
}
|
485
|
} else {
|
486
|
throw new AuthenticationException("AuthFile.addGroup - can't add the group "+groupName+" since it already exists.");
|
487
|
}
|
488
|
}
|
489
|
|
490
|
|
491
|
|
492
|
/**
|
493
|
* Change the password of the user to the new one which is hashed
|
494
|
* @param usrName the specified user.
|
495
|
* @param newPassword the new password which will be set
|
496
|
*/
|
497
|
public void modifyPassWithHash(String userName, String newHashPassword) throws AuthenticationException {
|
498
|
User user = new User();
|
499
|
user.setDN(userName);
|
500
|
user.modifyHashPass(newHashPassword);
|
501
|
}
|
502
|
|
503
|
/**
|
504
|
* Change the password of the user to the new one which is plain. However, only the hashed version will be serialized.
|
505
|
* @param usrName the specified user.
|
506
|
* @param newPassword the new password which will be set
|
507
|
*/
|
508
|
public void modifyPassWithPlain(String userName, String newPlainPassword) throws AuthenticationException {
|
509
|
User user = new User();
|
510
|
user.setDN(userName);
|
511
|
user.modifyPlainPass(newPlainPassword);
|
512
|
}
|
513
|
|
514
|
|
515
|
/**
|
516
|
* Add a user to a group
|
517
|
* @param userName the name of the user. the user should already exist
|
518
|
* @param group the name of the group. the group should already exist
|
519
|
*/
|
520
|
public void addUserToGroup(String userName, String group) throws AuthenticationException {
|
521
|
User user = new User();
|
522
|
user.setDN(userName);
|
523
|
user.addToGroup(group);
|
524
|
}
|
525
|
|
526
|
/**
|
527
|
* Remove a user from a group.
|
528
|
* @param userName the name of the user. the user should already exist.
|
529
|
* @param group the name of the group
|
530
|
*/
|
531
|
public void removeUserFromGroup(String userName, String group) throws AuthenticationException{
|
532
|
User user = new User();
|
533
|
user.setDN(userName);
|
534
|
user.removeFromGroup(group);
|
535
|
}
|
536
|
|
537
|
|
538
|
|
539
|
/**
|
540
|
* If the specified user name exist or not
|
541
|
* @param userName the name of the user
|
542
|
* @return true if the user eixsit
|
543
|
*/
|
544
|
private synchronized boolean userExists(String userName) throws AuthenticationException{
|
545
|
if(userName == null || userName.trim().equals("")) {
|
546
|
throw new AuthenticationException("AuthFile.userExist - can't judge if a user exists when its name is null or blank.");
|
547
|
}
|
548
|
List<Object> users = userpassword.getList(USERS+SLASH+USER+SLASH+AT+DN);
|
549
|
if(users != null && users.contains(userName)) {
|
550
|
return true;
|
551
|
} else {
|
552
|
return false;
|
553
|
}
|
554
|
}
|
555
|
|
556
|
/**
|
557
|
* If the specified group exist or not
|
558
|
* @param groupName the name of the group
|
559
|
* @return true if the user exists
|
560
|
*/
|
561
|
private synchronized boolean groupExists(String groupName) throws AuthenticationException{
|
562
|
if(groupName == null || groupName.trim().equals("")) {
|
563
|
throw new AuthenticationException("AuthFile.groupExist - can't judge if a group exists when its name is null or blank.");
|
564
|
}
|
565
|
List<Object> groups = userpassword.getList(GROUPS+SLASH+GROUP+SLASH+AT+NAME);
|
566
|
if(groups != null && groups.contains(groupName)) {
|
567
|
return true;
|
568
|
} else {
|
569
|
return false;
|
570
|
}
|
571
|
}
|
572
|
|
573
|
/*
|
574
|
* Encrypt a plain text
|
575
|
*/
|
576
|
private static String encrypt(String plain) {
|
577
|
return hashClass.hash(plain);
|
578
|
}
|
579
|
|
580
|
|
581
|
/**
|
582
|
* A method is used to help administrator to manage users and groups
|
583
|
* @param argus
|
584
|
* @throws Exception
|
585
|
*/
|
586
|
public static void main(String[] argus) throws Exception {
|
587
|
String USERADD = "useradd";
|
588
|
String USERMOD = "usermod";
|
589
|
String GROUPADD = "groupadd";
|
590
|
String USAGE = "usage";
|
591
|
if(argus == null || argus.length ==0) {
|
592
|
System.out.println("Please make sure that there are two arguments - \"$BASE_WEB_INF\" and\" $@\" after the class name edu.ucsb.nceas.metacat.authentication.AuthFile in the script file.");
|
593
|
System.exit(1);
|
594
|
} else if(argus.length ==1) {
|
595
|
printUsage();
|
596
|
System.exit(1);
|
597
|
}
|
598
|
PropertyService.getInstance(argus[0]);
|
599
|
AuthFile authFile = AuthFile.getInstance();
|
600
|
if(argus[1] != null && argus[1].equals(GROUPADD)) {
|
601
|
handleGroupAdd(authFile,argus);
|
602
|
} else if (argus[1] != null && argus[1].equals(USERADD)) {
|
603
|
handleUserAdd(authFile,argus);
|
604
|
} else if (argus[1] != null && argus[1].equals(USERMOD)) {
|
605
|
|
606
|
} else if (argus[1] != null && argus[1].equals(USAGE)) {
|
607
|
printUsage();
|
608
|
} else {
|
609
|
System.out.print("Error: the unknown action "+argus[1]);
|
610
|
}
|
611
|
}
|
612
|
|
613
|
/*
|
614
|
* Handle the groupAdd action in the main method
|
615
|
*/
|
616
|
private static void handleGroupAdd(AuthFile authFile, String[]argus) throws AuthenticationException {
|
617
|
HashMap<String, String> map = new <String, String>HashMap();
|
618
|
String DASHG = "-g";
|
619
|
String DASHD = "-d";
|
620
|
for(int i=2; i<argus.length; i++) {
|
621
|
String arg = argus[i];
|
622
|
|
623
|
if(map.containsKey(arg)) {
|
624
|
System.out.println("Error: the command line for groupadd can't have the duplicated options "+arg+".");
|
625
|
System.exit(1);
|
626
|
}
|
627
|
|
628
|
if(arg.equals(DASHG) && i<argus.length-1) {
|
629
|
map.put(arg, argus[i+1]);
|
630
|
} else if (arg.equals(DASHD) && i<argus.length-1) {
|
631
|
map.put(arg, argus[i+1]);
|
632
|
} else if(!arg.equals(DASHG) && !arg.equals(DASHD)) {
|
633
|
//check if the previous argument is -g or -d
|
634
|
if(!argus[i-1].equals(DASHG) && !argus[i-1].equals(DASHD)) {
|
635
|
System.out.println("Error: an illegal argument "+arg+" in the groupadd command ");
|
636
|
System.exit(1);
|
637
|
}
|
638
|
}
|
639
|
}
|
640
|
String groupName = null;
|
641
|
String description = null;
|
642
|
if(map.keySet().size() ==1 || map.keySet().size() ==2) {
|
643
|
groupName = map.get(DASHG);
|
644
|
if(groupName == null) {
|
645
|
System.out.println("Error: the "+DASHG+" group-name is required in the groupadd command line.");
|
646
|
}
|
647
|
description = map.get(DASHD);
|
648
|
authFile.addGroup(groupName, description);
|
649
|
System.out.println("Successfully added a group "+groupName+" to the file authentication system");
|
650
|
} else {
|
651
|
printError(argus);
|
652
|
System.exit(1);
|
653
|
}
|
654
|
}
|
655
|
|
656
|
/*
|
657
|
* Handle the userAdd action in the main method
|
658
|
*/
|
659
|
private static void handleUserAdd(AuthFile authFile,String[]argus) throws UnsupportedEncodingException, AuthenticationException{
|
660
|
boolean inputPassword = false;
|
661
|
boolean passingHashedPassword = false;
|
662
|
boolean hasDN = false;
|
663
|
String I = "-i";
|
664
|
String H = "-h";
|
665
|
String DN = "-dn";
|
666
|
String G = "-g";
|
667
|
String E = "-e";
|
668
|
String S = "-s";
|
669
|
String F = "-f";
|
670
|
String O= "-o";
|
671
|
Vector<String> possibleOptions = new <String>Vector();
|
672
|
possibleOptions.add(I);
|
673
|
possibleOptions.add(H);
|
674
|
possibleOptions.add(DN);
|
675
|
possibleOptions.add(G);
|
676
|
possibleOptions.add(E);
|
677
|
possibleOptions.add(S);
|
678
|
possibleOptions.add(F);
|
679
|
possibleOptions.add(O);
|
680
|
|
681
|
HashMap<String, String> map = new <String, String>HashMap();
|
682
|
for(int i=2; i<argus.length; i++) {
|
683
|
String arg = argus[i];
|
684
|
|
685
|
if(map.containsKey(arg)) {
|
686
|
System.out.println("Error: the command line for useradd can't have the duplicated options "+arg+".");
|
687
|
System.exit(1);
|
688
|
}
|
689
|
|
690
|
//this is the scenario that "-i" is at the end of the arguments.
|
691
|
if(arg.equals(I) && i==argus.length-1) {
|
692
|
map.put(I, I);//we need to input password.
|
693
|
inputPassword = true;
|
694
|
}
|
695
|
|
696
|
if(possibleOptions.contains(arg) && i<argus.length-1) {
|
697
|
//System.out.println("find the option "+arg);
|
698
|
if(arg.equals(I)) {
|
699
|
//this is the scenario that "-i" is NOT at the end of the arguments.
|
700
|
if(!possibleOptions.contains(argus[i+1])) {
|
701
|
System.out.println("Error: The option \"-i\" means the user will input a password in the useradd command. So it can't be followed by a value. It only can be followed by another option.");
|
702
|
System.exit(1);
|
703
|
}
|
704
|
map.put(I, I);//we need to input password.
|
705
|
inputPassword = true;
|
706
|
} else {
|
707
|
if(arg.equals(H)) {
|
708
|
passingHashedPassword = true;
|
709
|
} else if (arg.equals(DN)) {
|
710
|
hasDN = true;
|
711
|
}
|
712
|
map.put(arg, argus[i+1]);
|
713
|
}
|
714
|
|
715
|
} else if(!possibleOptions.contains(arg)) {
|
716
|
//check if the previous argument is an option
|
717
|
if(!possibleOptions.contains(argus[i-1])) {
|
718
|
System.out.println("Error: an illegal argument "+arg+" in the useradd command ");
|
719
|
System.exit(1);
|
720
|
}
|
721
|
}
|
722
|
}
|
723
|
|
724
|
String dn = null;
|
725
|
String plainPassword = null;
|
726
|
String hashedPassword = null;
|
727
|
if(!hasDN) {
|
728
|
System.out.println("The \"-dn user-distinguish-name\" is requried in the useradd command .");
|
729
|
System.exit(1);
|
730
|
} else {
|
731
|
dn = map.get(DN);
|
732
|
}
|
733
|
|
734
|
|
735
|
if(inputPassword && passingHashedPassword) {
|
736
|
System.out.println("Error: you can choose either \"-i\"(input a password) or \"-d dashed-passpwrd\"(pass through a hashed passwprd) in the useradd command.");
|
737
|
System.exit(1);
|
738
|
} else if (!inputPassword && !passingHashedPassword) {
|
739
|
System.out.println("Error: you must choose either \"-i\"(input a password) or \"-d dashed-passpwrd\"(pass through a hashed passwprd) in the useradd command.");
|
740
|
System.exit(1);
|
741
|
} else if(inputPassword) {
|
742
|
plainPassword = inputPassword();
|
743
|
//System.out.println("============the plain password is "+plainPassword);
|
744
|
} else if(passingHashedPassword) {
|
745
|
hashedPassword = map.get(H);
|
746
|
}
|
747
|
|
748
|
String group = map.get(G);
|
749
|
String[] groups = null;
|
750
|
if(group != null && !group.trim().equals("")) {
|
751
|
groups = new String[1];
|
752
|
groups[0]=group;
|
753
|
}
|
754
|
String email = map.get(E);
|
755
|
String surname = map.get(S);
|
756
|
String givenname = map.get(F);
|
757
|
String organization = map.get(O);
|
758
|
authFile.addUser(dn, groups, plainPassword, hashedPassword, email, surname, givenname, organization);
|
759
|
System.out.println("Successfully added a user "+dn+" to the file authentication system ");
|
760
|
}
|
761
|
|
762
|
|
763
|
/*
|
764
|
* Input the password
|
765
|
*/
|
766
|
private static String inputPassword() throws UnsupportedEncodingException {
|
767
|
String password = null;
|
768
|
String quit = "q";
|
769
|
Console console = System.console();
|
770
|
if (console == null) {
|
771
|
System.out.println("Sorry, we can't fetch the console from the system. You can't use the option \"-i\" to input a password. You have to use the option \"-d dashed-passpwrd\" to pass through a hashed passwprd in the useradd command. ");
|
772
|
System.exit(1);
|
773
|
}
|
774
|
|
775
|
while(true) {
|
776
|
System.out.print("Enter your password(input 'q' to quite): ");
|
777
|
String password1 = new String(console.readPassword());
|
778
|
if(password1== null || password1.trim().equals("")) {
|
779
|
System.out.println("Eorror: the password can't be blank or null. Please try again.");
|
780
|
continue;
|
781
|
} else if (password1.equals(quit)) {
|
782
|
System.exit(0);
|
783
|
}
|
784
|
System.out.print("Confirm your password(input 'q' to quite): ");
|
785
|
String password2 = new String(console.readPassword());
|
786
|
if(password2 == null || password2.trim().equals("")) {
|
787
|
System.out.println("Eorror: the password can't be blank or null. Please try again.");
|
788
|
continue;
|
789
|
} else if (password2.equals(quit)) {
|
790
|
System.exit(0);
|
791
|
}
|
792
|
|
793
|
if(!password1.equals(password2)) {
|
794
|
System.out.println("Eorror: The second passwords does't match the first one. Please try again.");
|
795
|
} else {
|
796
|
password = password1;
|
797
|
break;
|
798
|
}
|
799
|
|
800
|
|
801
|
}
|
802
|
|
803
|
return password;
|
804
|
|
805
|
}
|
806
|
/*
|
807
|
* Print out the usage statement
|
808
|
*/
|
809
|
private static void printUsage() {
|
810
|
System.out.println("Usage:\n"+
|
811
|
"./authFileManager.sh useradd -i -dn user-distinguish-name -g groupname -e email-address -s surname -f given-name -o organizationName\n" +
|
812
|
"./authFileManager.sh useradd -h hashed-password -dn user-distinguish-name -g groupname -e email-address -s surname -f given-name -o organizationName\n"+
|
813
|
"./authFileManager.sh groupadd -g group-name -d description\n" +
|
814
|
"./authFileManager.sh usermod -password -dn user-distinguish-name -i\n"+
|
815
|
"./authFileManager.sh usermod -password -dn user-distinguish-name -h new-hashed-password\n"+
|
816
|
"./authFileManager.sh usermod -group -a -dn user-disinguish-name -g added-group-name\n" +
|
817
|
"./authFileManager.sh usermod -group -r -dn user-distinguish-name -g removed-group-name\n"+
|
818
|
"Note:\n1. if a value of an option has spaces, the value should be enclosed by the double quotes.\n"+
|
819
|
" For example: ./authFileManager.sh groupadd -g nceas-dev -d \"Developers at NCEAS\"\n"+
|
820
|
"2. \"-d description\" in groupadd is optional; \"-g groupname -e email-address -s surname -f given-name -o organizationName\" in useradd are optional as well.");
|
821
|
|
822
|
|
823
|
}
|
824
|
|
825
|
/*
|
826
|
* Print out the statement to say it is a illegal command
|
827
|
*/
|
828
|
private static void printError(String[] argus) {
|
829
|
if(argus != null) {
|
830
|
System.out.println("Error: it is an illegal command: ");
|
831
|
for(int i=0; i<argus.length; i++) {
|
832
|
if(i!= 0) {
|
833
|
System.out.print(argus[i]+" ");
|
834
|
}
|
835
|
}
|
836
|
System.out.println("");
|
837
|
}
|
838
|
|
839
|
}
|
840
|
|
841
|
|
842
|
/**
|
843
|
* An class represents the information for a user.
|
844
|
* @author tao
|
845
|
*
|
846
|
*/
|
847
|
private class User {
|
848
|
private String dn = null;//the distinguish name
|
849
|
private String plainPass = null;
|
850
|
private String hashedPass = null;
|
851
|
private String email = null;
|
852
|
private String surName = null;
|
853
|
private String givenName = null;
|
854
|
private String cn = null;//the common name
|
855
|
private String[] groups = null;
|
856
|
private String organization = null;
|
857
|
|
858
|
/**
|
859
|
* Get the organization of the user
|
860
|
* @return
|
861
|
*/
|
862
|
public String getOrganization() {
|
863
|
return organization;
|
864
|
}
|
865
|
|
866
|
/**
|
867
|
* Set the organization for the user.
|
868
|
* @param organization
|
869
|
*/
|
870
|
public void setOrganization(String organization) {
|
871
|
this.organization = organization;
|
872
|
}
|
873
|
/**
|
874
|
* Get the distinguish name of the user
|
875
|
* @return the distinguish name
|
876
|
*/
|
877
|
public String getDN() {
|
878
|
return this.dn;
|
879
|
}
|
880
|
|
881
|
/**
|
882
|
* Set the distinguish name for the user
|
883
|
* @param dn the specified dn
|
884
|
*/
|
885
|
public void setDN(String dn) {
|
886
|
this.dn = dn;
|
887
|
}
|
888
|
|
889
|
/**
|
890
|
* Get the plain password for the user. This value will NOT be serialized to
|
891
|
* the password file
|
892
|
* @return the plain password for the user
|
893
|
*/
|
894
|
public String getPlainPass() {
|
895
|
return plainPass;
|
896
|
}
|
897
|
|
898
|
/**
|
899
|
* Set the plain password for the user.
|
900
|
* @param plainPass the plain password will be set.
|
901
|
*/
|
902
|
public void setPlainPass(String plainPass) {
|
903
|
this.plainPass = plainPass;
|
904
|
}
|
905
|
|
906
|
/**
|
907
|
* Get the hashed password of the user
|
908
|
* @return the hashed password of the user
|
909
|
*/
|
910
|
public String getHashedPass() {
|
911
|
return hashedPass;
|
912
|
}
|
913
|
|
914
|
/**
|
915
|
* Set the hashed the password for the user.
|
916
|
* @param hashedPass the hashed password will be set.
|
917
|
*/
|
918
|
public void setHashedPass(String hashedPass) {
|
919
|
this.hashedPass = hashedPass;
|
920
|
}
|
921
|
|
922
|
/**
|
923
|
* Get the email of the user
|
924
|
* @return the email of the user
|
925
|
*/
|
926
|
public String getEmail() {
|
927
|
return email;
|
928
|
}
|
929
|
|
930
|
/**
|
931
|
* Set the email address for the user
|
932
|
* @param email the eamil address will be set
|
933
|
*/
|
934
|
public void setEmail(String email) {
|
935
|
this.email = email;
|
936
|
}
|
937
|
|
938
|
/**
|
939
|
* Get the surname of the user
|
940
|
* @return the surname of the user
|
941
|
*/
|
942
|
public String getSurName() {
|
943
|
return surName;
|
944
|
}
|
945
|
|
946
|
/**
|
947
|
* Set the surname of the user
|
948
|
* @param surName
|
949
|
*/
|
950
|
public void setSurName(String surName) {
|
951
|
this.surName = surName;
|
952
|
}
|
953
|
|
954
|
/**
|
955
|
* Get the given name of the user
|
956
|
* @return the given name of the user
|
957
|
*/
|
958
|
public String getGivenName() {
|
959
|
return givenName;
|
960
|
}
|
961
|
|
962
|
/**
|
963
|
* Set the GivenName of the user
|
964
|
* @param givenName
|
965
|
*/
|
966
|
public void setGivenName(String givenName) {
|
967
|
this.givenName = givenName;
|
968
|
}
|
969
|
|
970
|
/**
|
971
|
* Get the common name of the user. If the cn is null, the GivenName +SurName will
|
972
|
* be returned
|
973
|
* @return the common name
|
974
|
*/
|
975
|
public String getCn() {
|
976
|
if(cn != null) {
|
977
|
return cn;
|
978
|
} else {
|
979
|
if (givenName != null && surName != null) {
|
980
|
return givenName+" "+surName;
|
981
|
} else if (givenName != null) {
|
982
|
return givenName;
|
983
|
} else if (surName != null ) {
|
984
|
return surName;
|
985
|
} else {
|
986
|
return null;
|
987
|
}
|
988
|
}
|
989
|
}
|
990
|
|
991
|
/**
|
992
|
* Set the common name for the user
|
993
|
* @param cn
|
994
|
*/
|
995
|
public void setCn(String cn) {
|
996
|
this.cn = cn;
|
997
|
}
|
998
|
|
999
|
/**
|
1000
|
* Get the groups of the user belong to
|
1001
|
* @return
|
1002
|
*/
|
1003
|
public String[] getGroups() {
|
1004
|
return groups;
|
1005
|
}
|
1006
|
|
1007
|
/**
|
1008
|
* Set the groups of the user belong to
|
1009
|
* @param groups
|
1010
|
*/
|
1011
|
public void setGroups(String[] groups) {
|
1012
|
this.groups = groups;
|
1013
|
}
|
1014
|
|
1015
|
/**
|
1016
|
* Add the user to a group and serialize the change to the password file.
|
1017
|
* @param group the group which the user will join
|
1018
|
* @throws AuthenticationException
|
1019
|
*/
|
1020
|
public void addToGroup(String group) throws AuthenticationException {
|
1021
|
if(group == null || group.trim().equals("")) {
|
1022
|
throw new IllegalArgumentException("AuthFile.User.addToGroup - the group can't be null or blank");
|
1023
|
}
|
1024
|
if(!userExists(dn)) {
|
1025
|
throw new AuthenticationException("AuthFile.User.addUserToGroup - the user "+dn+ " doesn't exist.");
|
1026
|
}
|
1027
|
if(!groupExists(group)) {
|
1028
|
throw new AuthenticationException("AuthFile.User.addUserToGroup - the group "+group+ " doesn't exist.");
|
1029
|
}
|
1030
|
List<Object> existingGroups = userpassword.getList(USERS+SLASH+USER+"["+AT+DN+"='"+dn+"']"+SLASH+GROUP);
|
1031
|
if(existingGroups != null && existingGroups.contains(group)) {
|
1032
|
throw new AuthenticationException("AuthFile.User.addUserToGroup - the user "+dn+ " already is the memember of the group "+group);
|
1033
|
}
|
1034
|
userpassword.addProperty(USERS+SLASH+USER+"["+AT+DN+"='"+dn+"']"+" "+GROUP, group);
|
1035
|
//add information to the memory
|
1036
|
if(groups == null) {
|
1037
|
if(existingGroups == null || existingGroups.isEmpty()) {
|
1038
|
groups = new String[1];
|
1039
|
groups[0] = group;
|
1040
|
} else {
|
1041
|
groups = new String[existingGroups.size()+1];
|
1042
|
for(int i=0; i<existingGroups.size(); i++) {
|
1043
|
groups[i] = (String)existingGroups.get(i);
|
1044
|
}
|
1045
|
groups[existingGroups.size()] = group;
|
1046
|
}
|
1047
|
|
1048
|
} else {
|
1049
|
String[] oldGroups = groups;
|
1050
|
groups = new String[oldGroups.length+1];
|
1051
|
for(int i=0; i<oldGroups.length; i++) {
|
1052
|
groups[i]= oldGroups[i];
|
1053
|
}
|
1054
|
groups[oldGroups.length] = group;
|
1055
|
|
1056
|
}
|
1057
|
}
|
1058
|
|
1059
|
/**
|
1060
|
* Remove the user from a group and serialize the change to the password file
|
1061
|
* @param group
|
1062
|
* @throws AuthenticationException
|
1063
|
*/
|
1064
|
public void removeFromGroup(String group) throws AuthenticationException {
|
1065
|
if(!userExists(dn)) {
|
1066
|
throw new AuthenticationException("AuthFile.User.removeUserFromGroup - the user "+dn+ " doesn't exist.");
|
1067
|
}
|
1068
|
if(!groupExists(group)) {
|
1069
|
throw new AuthenticationException("AuthFile.User.removeUserFromGroup - the group "+group+ " doesn't exist.");
|
1070
|
}
|
1071
|
String key = USERS+SLASH+USER+"["+AT+DN+"='"+dn+"']"+SLASH+GROUP;
|
1072
|
List<Object> existingGroups = userpassword.getList(key);
|
1073
|
if(!existingGroups.contains(group)) {
|
1074
|
throw new AuthenticationException("AuthFile.User.removeUserFromGroup - the user "+dn+ " isn't the memember of the group "+group);
|
1075
|
} else {
|
1076
|
userpassword.clearProperty(key+"[.='"+group+"']");
|
1077
|
}
|
1078
|
//change the value in the memory.
|
1079
|
if(groups != null) {
|
1080
|
boolean contains = false;
|
1081
|
for(int i=0; i<groups.length; i++) {
|
1082
|
if(groups[i].equals(group)) {
|
1083
|
contains = true;
|
1084
|
break;
|
1085
|
}
|
1086
|
}
|
1087
|
String[] newGroups = new String[groups.length-1];
|
1088
|
int k =0;
|
1089
|
for(int i=0; i<groups.length; i++) {
|
1090
|
if(!groups[i].equals(group)) {
|
1091
|
newGroups[k] = groups[i];
|
1092
|
k++;
|
1093
|
}
|
1094
|
}
|
1095
|
groups = newGroups;
|
1096
|
}
|
1097
|
}
|
1098
|
|
1099
|
/**
|
1100
|
* Modify the hash password and serialize it to the password file
|
1101
|
* @param hashPass
|
1102
|
* @throws AuthenticationException
|
1103
|
*/
|
1104
|
public void modifyHashPass(String hashPass) throws AuthenticationException {
|
1105
|
if(hashPass == null || hashPass.trim().equals("")) {
|
1106
|
throw new AuthenticationException("AuthFile.User.modifyHashPass - can't change the password to the null or blank.");
|
1107
|
}
|
1108
|
if(!userExists(dn)) {
|
1109
|
throw new AuthenticationException("AuthFile.User.modifyHashPass - can't change the password for the user "+dn+" since it doesn't eixt.");
|
1110
|
}
|
1111
|
userpassword.setProperty(USERS+SLASH+USER+"["+AT+DN+"='"+dn+"']"+SLASH+PASSWORD, hashPass);
|
1112
|
setHashedPass(hashPass);
|
1113
|
|
1114
|
}
|
1115
|
|
1116
|
/**
|
1117
|
* Modify the plain password and serialize its hash version to the password file
|
1118
|
* @param plainPass
|
1119
|
* @throws AuthenticationException
|
1120
|
*/
|
1121
|
public void modifyPlainPass(String plainPass) throws AuthenticationException {
|
1122
|
if(plainPass == null || plainPass.trim().equals("")) {
|
1123
|
throw new AuthenticationException("AuthFile.User.modifyPlainPass - can't change the password to the null or blank.");
|
1124
|
}
|
1125
|
if(!userExists(dn)) {
|
1126
|
throw new AuthenticationException("AuthFile.User.modifyPlainPass - can't change the password for the user "+dn+" since it doesn't eixt.");
|
1127
|
}
|
1128
|
String hashPassword = null;
|
1129
|
try {
|
1130
|
hashPassword = encrypt(plainPass);
|
1131
|
} catch (Exception e) {
|
1132
|
throw new AuthenticationException("AuthFile.User.modifyPlainPass - can't encript the password since "+e.getMessage());
|
1133
|
}
|
1134
|
userpassword.setProperty(USERS+SLASH+USER+"["+AT+DN+"='"+dn+"']"+SLASH+PASSWORD, hashPassword);
|
1135
|
setPlainPass(plainPass);
|
1136
|
}
|
1137
|
|
1138
|
/**
|
1139
|
* Add the user to the password file.
|
1140
|
*/
|
1141
|
public void serialize() throws AuthenticationException {
|
1142
|
if(dn == null || dn.trim().equals("")) {
|
1143
|
throw new AuthenticationException("AuthFile.User.serialize - can't add a user whose name is null or blank.");
|
1144
|
}
|
1145
|
if(hashedPass == null || hashedPass.trim().equals("")) {
|
1146
|
if(plainPass == null || plainPass.trim().equals("")) {
|
1147
|
throw new AuthenticationException("AuthFile.User.serialize - can't add a user whose password is null or blank.");
|
1148
|
} else {
|
1149
|
try {
|
1150
|
hashedPass = encrypt(plainPass);
|
1151
|
} catch (Exception e) {
|
1152
|
throw new AuthenticationException("AuthFile.User.serialize - can't encript the password since "+e.getMessage());
|
1153
|
}
|
1154
|
}
|
1155
|
}
|
1156
|
|
1157
|
if(!userExists(dn)) {
|
1158
|
if(userpassword != null) {
|
1159
|
userpassword.addProperty(USERS+" "+USER+AT+DN, dn);
|
1160
|
userpassword.addProperty(USERS+SLASH+USER+"["+AT+DN+"='"+dn+"']"+" "+PASSWORD, hashedPass);
|
1161
|
|
1162
|
if(email != null && !email.trim().equals("")) {
|
1163
|
userpassword.addProperty(USERS+SLASH+USER+"["+AT+DN+"='"+dn+"']"+" "+EMAIL, email);
|
1164
|
}
|
1165
|
|
1166
|
if(surName != null && !surName.trim().equals("")) {
|
1167
|
userpassword.addProperty(USERS+SLASH+USER+"["+AT+DN+"='"+dn+"']"+" "+SURNAME, surName);
|
1168
|
}
|
1169
|
|
1170
|
if(givenName != null && !givenName.trim().equals("")) {
|
1171
|
userpassword.addProperty(USERS+SLASH+USER+"["+AT+DN+"='"+dn+"']"+" "+GIVENNAME, givenName);
|
1172
|
}
|
1173
|
|
1174
|
if(organization != null && !organization.trim().equals("")) {
|
1175
|
userpassword.addProperty(USERS+SLASH+USER+"["+AT+DN+"='"+dn+"']"+" "+ORGANIZATION, organization);
|
1176
|
}
|
1177
|
|
1178
|
if(groups != null) {
|
1179
|
for(int i=0; i<groups.length; i++) {
|
1180
|
String group = groups[i];
|
1181
|
if(group != null && !group.trim().equals("")) {
|
1182
|
if(groupExists(group)) {
|
1183
|
userpassword.addProperty(USERS+SLASH+USER+"["+AT+DN+"='"+dn+"']"+" "+GROUP, group);
|
1184
|
}
|
1185
|
}
|
1186
|
}
|
1187
|
}
|
1188
|
//userpassword.reload();
|
1189
|
}
|
1190
|
} else {
|
1191
|
throw new AuthenticationException("AuthFile.User.serialize - can't add the user "+dn+" since it already exists.");
|
1192
|
}
|
1193
|
}
|
1194
|
}
|
1195
|
|
1196
|
}
|