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