1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: An implementation of the AuthInterface interface that
|
4
|
* allows Metacat to use the LDAP protocol for
|
5
|
* directory services
|
6
|
* Copyright: 2000 Regents of the University of California and the
|
7
|
* National Center for Ecological Analysis and Synthesis
|
8
|
* Authors: Matt Jones
|
9
|
* Release: @release@
|
10
|
*
|
11
|
* '$Author: bojilova $'
|
12
|
* '$Date: 2001-03-09 16:41:19 -0800 (Fri, 09 Mar 2001) $'
|
13
|
* '$Revision: 726 $'
|
14
|
*
|
15
|
* This program is free software; you can redistribute it and/or modify
|
16
|
* it under the terms of the GNU General Public License as published by
|
17
|
* the Free Software Foundation; either version 2 of the License, or
|
18
|
* (at your option) any later version.
|
19
|
*
|
20
|
* This program is distributed in the hope that it will be useful,
|
21
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
22
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
23
|
* GNU General Public License for more details.
|
24
|
*
|
25
|
* You should have received a copy of the GNU General Public License
|
26
|
* along with this program; if not, write to the Free Software
|
27
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
28
|
*/
|
29
|
|
30
|
package edu.ucsb.nceas.metacat;
|
31
|
|
32
|
import java.net.ConnectException;
|
33
|
|
34
|
import javax.naming.AuthenticationException;
|
35
|
import javax.naming.Context;
|
36
|
import javax.naming.directory.Attribute;
|
37
|
import javax.naming.directory.Attributes;
|
38
|
import javax.naming.directory.BasicAttribute;
|
39
|
import javax.naming.directory.BasicAttributes;
|
40
|
import javax.naming.directory.DirContext;
|
41
|
import javax.naming.directory.InitialDirContext;
|
42
|
import javax.naming.directory.SearchResult;
|
43
|
import javax.naming.directory.SearchControls;
|
44
|
import javax.naming.NamingEnumeration;
|
45
|
import javax.naming.NamingException;
|
46
|
import java.util.Iterator;
|
47
|
import java.util.HashMap;
|
48
|
import java.util.Hashtable;
|
49
|
import java.util.Set;
|
50
|
import java.util.Vector;
|
51
|
|
52
|
/**
|
53
|
* An implementation of the AuthInterface interface that
|
54
|
* allows Metacat to use the LDAP protocol for directory services.
|
55
|
* The LDAP authentication service is used to determine if a user
|
56
|
* is authenticated, and whether they are a member of a particular group.
|
57
|
*/
|
58
|
public class AuthLdap implements AuthInterface {
|
59
|
|
60
|
/**
|
61
|
* Determine if a user/password are valid according to the authentication
|
62
|
* service.
|
63
|
*
|
64
|
* @param user the name of the principal to authenticate
|
65
|
* @param password the password to use for authentication
|
66
|
* @returns boolean true if authentication successful, false otherwise
|
67
|
*/
|
68
|
public boolean authenticate(String user, String password)
|
69
|
throws ConnectException
|
70
|
{
|
71
|
MetaCatUtil util = new MetaCatUtil();
|
72
|
String ldapUrl = util.getOption("ldapurl");
|
73
|
String ldapBase = util.getOption("ldapbase");
|
74
|
|
75
|
String distName = null;
|
76
|
boolean authenticated = false;
|
77
|
|
78
|
// Identify service provider to use
|
79
|
Hashtable env = new Hashtable(11);
|
80
|
env.put(Context.INITIAL_CONTEXT_FACTORY,
|
81
|
"com.sun.jndi.ldap.LdapCtxFactory");
|
82
|
env.put(Context.PROVIDER_URL, ldapUrl + ldapBase);
|
83
|
|
84
|
try {
|
85
|
|
86
|
// Get the dn for this uid or cn
|
87
|
String identifier = getIdentifyingName(user);
|
88
|
if (identifier != null) {
|
89
|
distName = identifier + "," + ldapBase;
|
90
|
// Now that we have the dn, we can authenticate, so
|
91
|
// authenticate this time when opening the DirContext
|
92
|
env.put(Context.SECURITY_AUTHENTICATION, "simple");
|
93
|
env.put(Context.SECURITY_PRINCIPAL, distName);
|
94
|
env.put(Context.SECURITY_CREDENTIALS, password);
|
95
|
// If our auth credentials are invalid, an exception will be thrown
|
96
|
DirContext ctx = new InitialDirContext(env);
|
97
|
|
98
|
// If no exception is thrown then authentication succeeded
|
99
|
authenticated = true;
|
100
|
|
101
|
// Close the context when we're done
|
102
|
ctx.close();
|
103
|
} else {
|
104
|
util.debugMessage("User not found!");
|
105
|
}
|
106
|
|
107
|
} catch (AuthenticationException ae) {
|
108
|
util.debugMessage("Error authenticating in AuthLdap.authenticate: " + ae);
|
109
|
throw new ConnectException(
|
110
|
"Error authenticating in AuthLdap.authenticate: " + ae);
|
111
|
} catch (NamingException e) {
|
112
|
util.debugMessage("Naming exception while authenticating in " +
|
113
|
"AuthLdap.authenticate: " + e);
|
114
|
throw new ConnectException(
|
115
|
"Naming exception while authenticating in " +
|
116
|
"AuthLdap.authenticate: " + e);
|
117
|
}
|
118
|
|
119
|
return authenticated;
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* Get all users from the authentication service
|
124
|
*/
|
125
|
public String[] getUsers(String user, String password)
|
126
|
throws ConnectException
|
127
|
{
|
128
|
MetaCatUtil util = new MetaCatUtil();
|
129
|
String ldapUrl = util.getOption("ldapurl");
|
130
|
String ldapBase = util.getOption("ldapbase");
|
131
|
|
132
|
String[] users = null;
|
133
|
|
134
|
// Identify service provider to use
|
135
|
Hashtable env = new Hashtable(11);
|
136
|
env.put(Context.INITIAL_CONTEXT_FACTORY,
|
137
|
"com.sun.jndi.ldap.LdapCtxFactory");
|
138
|
env.put(Context.PROVIDER_URL, ldapUrl + ldapBase);
|
139
|
|
140
|
try {
|
141
|
|
142
|
// Get the dn for this uid or cn
|
143
|
String identifier = getIdentifyingName(user);
|
144
|
if (identifier != null) {
|
145
|
env.put(Context.SECURITY_AUTHENTICATION, "simple");
|
146
|
env.put(Context.SECURITY_PRINCIPAL, identifier + "," + ldapBase);
|
147
|
env.put(Context.SECURITY_CREDENTIALS, password);
|
148
|
|
149
|
// Create the initial directory context
|
150
|
DirContext ctx = new InitialDirContext(env);
|
151
|
|
152
|
// Specify the ids of the attributes to return
|
153
|
String[] attrIDs = {"uid"};
|
154
|
|
155
|
// Specify the attributes to match.
|
156
|
// Users are objects that have the attribute objectclass=InetOrgPerson.
|
157
|
Attributes matchAttrs = new BasicAttributes(true); // ignore case
|
158
|
matchAttrs.put(new BasicAttribute("objectclass", "inetOrgPerson"));
|
159
|
|
160
|
// Search for objects in the current context
|
161
|
NamingEnumeration enum = ctx.search("", matchAttrs, attrIDs);
|
162
|
|
163
|
// Print the users
|
164
|
Vector uvec = new Vector();
|
165
|
while (enum.hasMore()) {
|
166
|
SearchResult sr = (SearchResult)enum.next();
|
167
|
Attributes attrs = sr.getAttributes();
|
168
|
NamingEnumeration enum1 = attrs.getAll(); // only "uid" attr
|
169
|
while (enum1.hasMore()) {
|
170
|
Attribute attr = (Attribute)enum1.next();
|
171
|
uvec.add(attr.get());
|
172
|
}
|
173
|
}
|
174
|
|
175
|
// initialize users[]; fill users[]
|
176
|
users = new String[uvec.size()];
|
177
|
for (int i=0; i < uvec.size(); i++) {
|
178
|
users[i] = (String)uvec.elementAt(i);
|
179
|
}
|
180
|
|
181
|
// Close the context when we're done
|
182
|
ctx.close();
|
183
|
} else {
|
184
|
util.debugMessage("User not found!");
|
185
|
}
|
186
|
|
187
|
} catch (NamingException e) {
|
188
|
System.err.println("Problem getting users in AuthLdap.getUsers:" + e);
|
189
|
throw new ConnectException(
|
190
|
"Problem getting groups in AuthLdap.getUsers:" + e);
|
191
|
}
|
192
|
|
193
|
return users;
|
194
|
}
|
195
|
|
196
|
/**
|
197
|
* Get the users for a particular group from the authentication service
|
198
|
*/
|
199
|
public String[] getUsers(String user, String password, String group)
|
200
|
throws ConnectException
|
201
|
{
|
202
|
MetaCatUtil util = new MetaCatUtil();
|
203
|
String ldapUrl = util.getOption("ldapurl");
|
204
|
String ldapBase = util.getOption("ldapbase");
|
205
|
|
206
|
String[] users = null;
|
207
|
|
208
|
// Identify service provider to use
|
209
|
Hashtable env = new Hashtable(11);
|
210
|
env.put(Context.INITIAL_CONTEXT_FACTORY,
|
211
|
"com.sun.jndi.ldap.LdapCtxFactory");
|
212
|
env.put(Context.PROVIDER_URL, ldapUrl + ldapBase);
|
213
|
|
214
|
try {
|
215
|
|
216
|
// Get the dn for this uid or cn
|
217
|
String identifier = getIdentifyingName(user);
|
218
|
if (identifier != null) {
|
219
|
env.put(Context.SECURITY_AUTHENTICATION, "simple");
|
220
|
env.put(Context.SECURITY_PRINCIPAL, identifier + "," + ldapBase);
|
221
|
env.put(Context.SECURITY_CREDENTIALS, password);
|
222
|
|
223
|
// Create the initial directory context
|
224
|
DirContext ctx = new InitialDirContext(env);
|
225
|
|
226
|
// Specify the ids of the attributes to return
|
227
|
String[] attrIDs = {"uniquemember"};
|
228
|
|
229
|
// Get the dn for this group
|
230
|
identifier = getIdentifyingName(group);
|
231
|
|
232
|
// Specify the attributes to match.
|
233
|
// Groups are objects with attribute objectclass=groupofuniquenames.
|
234
|
Attributes matchAttrs = new BasicAttributes(true); // ignore case
|
235
|
matchAttrs.put(new BasicAttribute("objectclass", "groupofuniquenames"));
|
236
|
matchAttrs.put(new BasicAttribute("cn", group));
|
237
|
|
238
|
// Search for objects in the current context
|
239
|
NamingEnumeration enum = ctx.search("", matchAttrs, attrIDs);
|
240
|
|
241
|
// Print the users
|
242
|
Vector uvec = new Vector();
|
243
|
while (enum.hasMore()) {
|
244
|
SearchResult sr = (SearchResult)enum.next();
|
245
|
Attributes attrs = sr.getAttributes();
|
246
|
// return all attributes
|
247
|
NamingEnumeration enum1 = attrs.getAll(); // only "uniquemember" attr
|
248
|
while (enum1.hasMore()) {
|
249
|
Attribute attr = (Attribute)enum1.next();
|
250
|
// return all values of that attribute
|
251
|
NamingEnumeration enum2 = attr.getAll();
|
252
|
while (enum2.hasMore()) {
|
253
|
uvec.add((String)enum2.next());
|
254
|
}
|
255
|
}
|
256
|
}
|
257
|
|
258
|
// initialize users[]; fill users[]
|
259
|
users = new String[uvec.size()];
|
260
|
for (int i=0; i < uvec.size(); i++) {
|
261
|
users[i] = (String)uvec.elementAt(i);
|
262
|
}
|
263
|
|
264
|
// Close the context when we're done
|
265
|
ctx.close();
|
266
|
} else {
|
267
|
util.debugMessage("User not found!");
|
268
|
}
|
269
|
|
270
|
} catch (NamingException e) {
|
271
|
System.err.println("Problem getting users in AuthLdap.getUsers:" + e);
|
272
|
throw new ConnectException(
|
273
|
"Problem getting groups in AuthLdap.getUsers:" + e);
|
274
|
}
|
275
|
|
276
|
return users;
|
277
|
}
|
278
|
|
279
|
/**
|
280
|
* Get all groups from the authentication service
|
281
|
*/
|
282
|
public String[] getGroups(String user, String password)
|
283
|
throws ConnectException
|
284
|
{
|
285
|
MetaCatUtil util = new MetaCatUtil();
|
286
|
String ldapUrl = util.getOption("ldapurl");
|
287
|
String ldapBase = util.getOption("ldapbase");
|
288
|
|
289
|
String[] groups = null;
|
290
|
|
291
|
// Identify service provider to use
|
292
|
Hashtable env = new Hashtable(11);
|
293
|
env.put(Context.INITIAL_CONTEXT_FACTORY,
|
294
|
"com.sun.jndi.ldap.LdapCtxFactory");
|
295
|
env.put(Context.PROVIDER_URL, ldapUrl + ldapBase);
|
296
|
|
297
|
try {
|
298
|
|
299
|
// Get the dn for this uid or cn
|
300
|
String identifier = getIdentifyingName(user);
|
301
|
if (identifier != null) {
|
302
|
env.put(Context.SECURITY_AUTHENTICATION, "simple");
|
303
|
env.put(Context.SECURITY_PRINCIPAL, identifier + "," + ldapBase);
|
304
|
env.put(Context.SECURITY_CREDENTIALS, password);
|
305
|
|
306
|
// Create the initial directory context
|
307
|
DirContext ctx = new InitialDirContext(env);
|
308
|
|
309
|
// Specify the ids of the attributes to return
|
310
|
String[] attrIDs = {"cn"};
|
311
|
|
312
|
// Specify the attributes to match.
|
313
|
// Groups are objects with attribute objectclass=groupofuniquenames.
|
314
|
Attributes matchAttrs = new BasicAttributes(true); // ignore case
|
315
|
matchAttrs.put(new BasicAttribute("objectclass", "groupofuniquenames"));
|
316
|
|
317
|
// Search for objects in the current context
|
318
|
NamingEnumeration enum = ctx.search("", matchAttrs, attrIDs);
|
319
|
|
320
|
// Print the users
|
321
|
Vector uvec = new Vector();
|
322
|
while (enum.hasMore()) {
|
323
|
SearchResult sr = (SearchResult)enum.next();
|
324
|
Attributes attrs = sr.getAttributes();
|
325
|
NamingEnumeration enum1 = attrs.getAll(); // only "cn" attr
|
326
|
while (enum1.hasMore()) {
|
327
|
Attribute attr = (Attribute)enum1.next();
|
328
|
uvec.add(attr.get());
|
329
|
}
|
330
|
}
|
331
|
|
332
|
// initialize groups[] and fill it
|
333
|
groups = new String[uvec.size()];
|
334
|
for (int i=0; i < uvec.size(); i++) {
|
335
|
groups[i] = (String)uvec.elementAt(i);
|
336
|
}
|
337
|
|
338
|
// Close the context when we're done
|
339
|
ctx.close();
|
340
|
} else {
|
341
|
util.debugMessage("User not found!");
|
342
|
}
|
343
|
|
344
|
} catch (NamingException e) {
|
345
|
System.err.println("Problem getting groups in AuthLdap.getGroups:" + e);
|
346
|
throw new ConnectException(
|
347
|
"Problem getting groups in AuthLdap.getGroups:" + e);
|
348
|
}
|
349
|
|
350
|
return groups;
|
351
|
}
|
352
|
|
353
|
/**
|
354
|
* Get the groups for a particular user from the authentication service
|
355
|
*/
|
356
|
public String[] getGroups(String user, String password, String foruser)
|
357
|
throws ConnectException
|
358
|
{
|
359
|
MetaCatUtil util = new MetaCatUtil();
|
360
|
String ldapUrl = util.getOption("ldapurl");
|
361
|
String ldapBase = util.getOption("ldapbase");
|
362
|
|
363
|
String[] groups = null;
|
364
|
|
365
|
// Identify service provider to use
|
366
|
Hashtable env = new Hashtable(11);
|
367
|
env.put(Context.INITIAL_CONTEXT_FACTORY,
|
368
|
"com.sun.jndi.ldap.LdapCtxFactory");
|
369
|
env.put(Context.PROVIDER_URL, ldapUrl + ldapBase);
|
370
|
|
371
|
try {
|
372
|
|
373
|
// Get the dn for this uid or cn
|
374
|
String identifier = getIdentifyingName(user);
|
375
|
if (identifier != null) {
|
376
|
env.put(Context.SECURITY_AUTHENTICATION, "simple");
|
377
|
env.put(Context.SECURITY_PRINCIPAL, identifier + "," + ldapBase);
|
378
|
env.put(Context.SECURITY_CREDENTIALS, password);
|
379
|
|
380
|
// Create the initial directory context
|
381
|
DirContext ctx = new InitialDirContext(env);
|
382
|
|
383
|
// Specify the ids of the attributes to return
|
384
|
String[] attrIDs = {"cn"};
|
385
|
|
386
|
// Specify the attributes to match.
|
387
|
// Groups are objects with attribute objectclass=groupofuniquenames.
|
388
|
// and have attribute uniquemember=foruser,ldapbase.
|
389
|
Attributes matchAttrs = new BasicAttributes(true); // ignore case
|
390
|
matchAttrs.put(new BasicAttribute("objectclass", "groupofuniquenames"));
|
391
|
matchAttrs.put(new BasicAttribute("uniquemember",foruser+","+ldapBase));
|
392
|
|
393
|
// Search for objects in the current context
|
394
|
NamingEnumeration enum = ctx.search("", matchAttrs, attrIDs);
|
395
|
|
396
|
// Print the users
|
397
|
Vector uvec = new Vector();
|
398
|
while (enum.hasMore()) {
|
399
|
SearchResult sr = (SearchResult)enum.next();
|
400
|
Attributes attrs = sr.getAttributes();
|
401
|
NamingEnumeration enum1 = attrs.getAll(); // only "gid" attr
|
402
|
while (enum1.hasMore()) {
|
403
|
Attribute attr = (Attribute)enum1.next();
|
404
|
uvec.add(attr.get());
|
405
|
}
|
406
|
}
|
407
|
|
408
|
// initialize groups[] and fill it
|
409
|
groups = new String[uvec.size()];
|
410
|
for (int i=0; i < uvec.size(); i++) {
|
411
|
groups[i] = (String)uvec.elementAt(i);
|
412
|
}
|
413
|
|
414
|
// Close the context when we're done
|
415
|
ctx.close();
|
416
|
} else {
|
417
|
util.debugMessage("User not found!");
|
418
|
}
|
419
|
|
420
|
} catch (NamingException e) {
|
421
|
System.err.println("Problem getting groups in AuthLdap.getGroups:" + e);
|
422
|
throw new ConnectException(
|
423
|
"Problem getting groups in AuthLdap.getGroups:" + e);
|
424
|
}
|
425
|
|
426
|
return groups;
|
427
|
}
|
428
|
|
429
|
/**
|
430
|
* Get attributes describing a user or group
|
431
|
*
|
432
|
* @param user the user for which the attribute list is requested
|
433
|
* @returns HashMap a map of attribute name to a Vector of values
|
434
|
*/
|
435
|
public HashMap getAttributes(String foruser)
|
436
|
throws ConnectException
|
437
|
{
|
438
|
return getAttributes(null, null, foruser);
|
439
|
}
|
440
|
|
441
|
/**
|
442
|
* Get attributes describing a user or group
|
443
|
*
|
444
|
* @param user the user for which the attribute list is requested
|
445
|
* @param authuser the user for authenticating against the service
|
446
|
* @param password the password for authenticating against the service
|
447
|
* @returns HashMap a map of attribute name to a Vector of values
|
448
|
*/
|
449
|
public HashMap getAttributes(String user, String password, String foruser)
|
450
|
throws ConnectException
|
451
|
{
|
452
|
MetaCatUtil util = new MetaCatUtil();
|
453
|
String ldapUrl = util.getOption("ldapurl");
|
454
|
String ldapBase = util.getOption("ldapbase");
|
455
|
|
456
|
HashMap attributes = new HashMap();
|
457
|
|
458
|
// Identify service provider to use
|
459
|
Hashtable env = new Hashtable(11);
|
460
|
env.put(Context.INITIAL_CONTEXT_FACTORY,
|
461
|
"com.sun.jndi.ldap.LdapCtxFactory");
|
462
|
env.put(Context.PROVIDER_URL, ldapUrl + ldapBase);
|
463
|
|
464
|
// Authentication information
|
465
|
|
466
|
try {
|
467
|
|
468
|
if ((user != null) && (password != null)) {
|
469
|
String identifier = getIdentifyingName(user);
|
470
|
env.put(Context.SECURITY_AUTHENTICATION, "simple");
|
471
|
env.put(Context.SECURITY_PRINCIPAL, identifier + "," + ldapBase);
|
472
|
env.put(Context.SECURITY_CREDENTIALS, password);
|
473
|
}
|
474
|
|
475
|
// Create the initial directory context
|
476
|
DirContext ctx = new InitialDirContext(env);
|
477
|
|
478
|
// Find out the identifying attribute for the user
|
479
|
String userident = getIdentifyingName(foruser);
|
480
|
|
481
|
// Ask for all attributes of the user
|
482
|
Attributes attrs = ctx.getAttributes(userident);
|
483
|
|
484
|
// Print all of the attributes
|
485
|
NamingEnumeration en = attrs.getAll();
|
486
|
while (en.hasMore()) {
|
487
|
Attribute att = (Attribute)en.next();
|
488
|
Vector values = new Vector();
|
489
|
String attName = att.getID();
|
490
|
NamingEnumeration attvalues = att.getAll();
|
491
|
while (attvalues.hasMore()) {
|
492
|
String value = (String)attvalues.next();
|
493
|
values.add(value);
|
494
|
}
|
495
|
attributes.put(attName, values);
|
496
|
}
|
497
|
|
498
|
// Close the context when we're done
|
499
|
ctx.close();
|
500
|
} catch (NamingException e) {
|
501
|
System.err.println("Problem getting attributes in AuthLdap.getAttributes:"
|
502
|
+ e);
|
503
|
throw new ConnectException(
|
504
|
"Problem getting attributes in AuthLdap.getAttributes:" + e);
|
505
|
}
|
506
|
|
507
|
return attributes;
|
508
|
}
|
509
|
|
510
|
/**
|
511
|
* Get the identifying name for a given userid or name. This is the name
|
512
|
* that is used in conjunction withthe LDAP BaseDN to create a
|
513
|
* distinguished name (dn) for the record
|
514
|
*
|
515
|
* @param user the user for which the identifying name is requested
|
516
|
* @returns String the identifying name for the user,
|
517
|
* or null if not found
|
518
|
*/
|
519
|
private String getIdentifyingName(String user)
|
520
|
throws NamingException
|
521
|
{
|
522
|
MetaCatUtil util = new MetaCatUtil();
|
523
|
String ldapUrl = util.getOption("ldapurl");
|
524
|
String ldapBase = util.getOption("ldapbase");
|
525
|
|
526
|
String identifier = null;
|
527
|
|
528
|
// Identify service provider to use
|
529
|
Hashtable env = new Hashtable(11);
|
530
|
env.put(Context.INITIAL_CONTEXT_FACTORY,
|
531
|
"com.sun.jndi.ldap.LdapCtxFactory");
|
532
|
env.put(Context.PROVIDER_URL, ldapUrl + ldapBase);
|
533
|
|
534
|
try {
|
535
|
|
536
|
// Bind to the LDAP server, in order to search for the right
|
537
|
// distinguished name (dn) based on userid (uid) or common name (cn)
|
538
|
DirContext ctx = new InitialDirContext(env);
|
539
|
|
540
|
// Search for the user id or name using the uid, then cn and sn attributes
|
541
|
// If we find a record, determine the dn for the record
|
542
|
util.debugMessage("\nStarting search phase...\n");
|
543
|
Attributes matchAttrs = new BasicAttributes(true);
|
544
|
matchAttrs.put(new BasicAttribute("uid", user));
|
545
|
NamingEnumeration answer = ctx.search("", matchAttrs);
|
546
|
if (answer.hasMore()) {
|
547
|
SearchResult sr = (SearchResult)answer.next();
|
548
|
identifier = sr.getName();
|
549
|
util.debugMessage("Found: " + identifier);
|
550
|
} else {
|
551
|
Attributes matchAttrs2 = new BasicAttributes(true);
|
552
|
matchAttrs2.put(new BasicAttribute("cn", user));
|
553
|
NamingEnumeration answer2 = ctx.search("", matchAttrs2);
|
554
|
if (answer2.hasMore()) {
|
555
|
SearchResult sr = (SearchResult)answer2.next();
|
556
|
identifier = sr.getName();
|
557
|
util.debugMessage("Found: " + identifier);
|
558
|
} else {
|
559
|
Attributes matchAttrs3 = new BasicAttributes(true);
|
560
|
matchAttrs3.put(new BasicAttribute("sn", user));
|
561
|
NamingEnumeration answer3 = ctx.search("", matchAttrs3);
|
562
|
if (answer3.hasMore()) {
|
563
|
SearchResult sr = (SearchResult)answer3.next();
|
564
|
identifier = sr.getName();
|
565
|
util.debugMessage("Found: " + identifier);
|
566
|
}
|
567
|
}
|
568
|
}
|
569
|
// Close the context when we're done the initial search
|
570
|
ctx.close();
|
571
|
} catch (NamingException e) {
|
572
|
util.debugMessage("Naming exception while getting dn: " + e);
|
573
|
System.out.println("Naming exception in AuthLdap.getIdentifyingName");
|
574
|
throw new NamingException(
|
575
|
"Naming exception in AuthLdap.getIdentifyingName: " + e);
|
576
|
}
|
577
|
|
578
|
return identifier;
|
579
|
}
|
580
|
|
581
|
/**
|
582
|
* Get list of all groups and users from authentication scheme.
|
583
|
* The output is formatted in XML.
|
584
|
*/
|
585
|
private String getPrincipals(String user, String password)
|
586
|
throws ConnectException
|
587
|
{
|
588
|
StringBuffer out = new StringBuffer();
|
589
|
String[] groups = getGroups(user, password);
|
590
|
Vector usersIn = new Vector();
|
591
|
|
592
|
out.append("<?xml version=\"1.0\"?>\n");
|
593
|
out.append("<principals>\n");
|
594
|
|
595
|
// for the groups and users that belong to them
|
596
|
if ( groups.length > 0 ) {
|
597
|
for (int i=0; i < groups.length; i++ ) {
|
598
|
out.append(" <group>\n");
|
599
|
out.append(" <groupname>" + groups[i] + "<groupname>\n");
|
600
|
String[] usersForGroup = getUsers(user,password,groups[i]);
|
601
|
for (int j=0; j < usersForGroup.length; j++ ) {
|
602
|
usersIn.addElement(usersForGroup[j]);
|
603
|
out.append(" <user>\n");
|
604
|
out.append(" <username>" + usersForGroup[j] + "<username>\n");
|
605
|
out.append(" </user>\n");
|
606
|
}
|
607
|
out.append(" </group>\n");
|
608
|
}
|
609
|
}
|
610
|
// for the users not belonging to any group
|
611
|
String[] users = getUsers(user, password);
|
612
|
for (int j=0; j < users.length; j++ ) {
|
613
|
if ( !usersIn.contains(users[j]) ) {
|
614
|
out.append(" <user>\n");
|
615
|
out.append(" <username>" + users[j] + "<username>\n");
|
616
|
out.append(" </user>\n");
|
617
|
}
|
618
|
}
|
619
|
|
620
|
out.append("</principals>");
|
621
|
return out.toString();
|
622
|
}
|
623
|
|
624
|
/**
|
625
|
* Test method for the class
|
626
|
*/
|
627
|
public static void main(String[] args) {
|
628
|
|
629
|
// Provide a user, such as: "Matt Jones", or "jones"
|
630
|
String user = args[0];
|
631
|
String password = args[1];
|
632
|
|
633
|
AuthLdap authservice = new AuthLdap();
|
634
|
|
635
|
boolean isValid = false;
|
636
|
try {
|
637
|
isValid = authservice.authenticate(user, password);
|
638
|
if (isValid) {
|
639
|
System.out.println("Authentication successful for: " + user );
|
640
|
System.out.println(" ");
|
641
|
} else {
|
642
|
System.out.println("Authentication failed for: " + user);
|
643
|
}
|
644
|
|
645
|
if (isValid) {
|
646
|
//String group = args[2];
|
647
|
HashMap userInfo = authservice.getAttributes(user, password, user);
|
648
|
// Print all of the attributes
|
649
|
Iterator attList = (Iterator)(((Set)userInfo.keySet()).iterator());
|
650
|
while (attList.hasNext()) {
|
651
|
String att = (String)attList.next();
|
652
|
Vector values = (Vector)userInfo.get(att);
|
653
|
Iterator attvalues = values.iterator();
|
654
|
while (attvalues.hasNext()) {
|
655
|
String value = (String)attvalues.next();
|
656
|
System.out.println(att + ": " + value);
|
657
|
}
|
658
|
}
|
659
|
|
660
|
}
|
661
|
|
662
|
/*
|
663
|
// get the whole list of users
|
664
|
if (isValid) {
|
665
|
String[] users = authservice.getUsers(user, password);
|
666
|
for (int i=0; i < users.length; i++) {
|
667
|
System.out.println(users[i]);
|
668
|
}
|
669
|
System.out.println("Total " + users.length + " users.");
|
670
|
}
|
671
|
*/
|
672
|
/*
|
673
|
// get the whole list of users for a group
|
674
|
if (isValid) {
|
675
|
String group = args[2];
|
676
|
String[] users = authservice.getUsers(user, password, group);
|
677
|
for (int i=0; i < users.length; i++) {
|
678
|
System.out.println(users[i]);
|
679
|
}
|
680
|
}
|
681
|
*/
|
682
|
// get the whole list groups and users in XML format
|
683
|
if (isValid) {
|
684
|
String out = authservice.getPrincipals(user, password);
|
685
|
java.io.File f = new java.io.File("principals.txt");
|
686
|
java.io.FileWriter fw = new java.io.FileWriter(f);
|
687
|
java.io.BufferedWriter buff = new java.io.BufferedWriter(fw);
|
688
|
buff.write(out);
|
689
|
buff.flush();
|
690
|
buff.close();
|
691
|
fw.close();
|
692
|
}
|
693
|
|
694
|
} catch (ConnectException ce) {
|
695
|
System.err.println("Error connecting to LDAP server in authldap.main");
|
696
|
} catch (java.io.IOException ioe) {
|
697
|
System.err.println("I/O Error writing to file principals.txt");
|
698
|
}
|
699
|
}
|
700
|
}
|