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-05 16:25:52 -0800 (Mon, 05 Mar 2001) $'
|
13
|
* '$Revision: 725 $'
|
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
|
// Search for objects in the current context
|
156
|
// All users should be put in subcontext like "ou=Users"
|
157
|
// NamingEnumeration answer = ctx.search("ou=Users", null, attrIDs);
|
158
|
NamingEnumeration enum = ctx.search("", null, attrIDs);
|
159
|
|
160
|
// Print the users
|
161
|
Vector uvec = new Vector();
|
162
|
while (enum.hasMore()) {
|
163
|
SearchResult sr = (SearchResult)enum.next();
|
164
|
Attributes attrs = sr.getAttributes();
|
165
|
NamingEnumeration enum1 = attrs.getAll(); // only "uid" attr
|
166
|
while (enum1.hasMore()) {
|
167
|
Attribute attr = (Attribute)enum1.next();
|
168
|
//System.out.print(attr.getID() + "=");
|
169
|
//System.out.println((String)attr.get());
|
170
|
uvec.add(attr.get());
|
171
|
}
|
172
|
}
|
173
|
|
174
|
// initialize users[]; fill users[]
|
175
|
users = new String[uvec.size()];
|
176
|
for (int i=0; i < uvec.size(); i++) {
|
177
|
users[i] = (String)uvec.elementAt(i);
|
178
|
}
|
179
|
|
180
|
// Close the context when we're done
|
181
|
ctx.close();
|
182
|
} else {
|
183
|
util.debugMessage("User not found!");
|
184
|
}
|
185
|
|
186
|
} catch (NamingException e) {
|
187
|
System.err.println("Problem getting users in AuthLdap.getUsers:" + e);
|
188
|
throw new ConnectException(
|
189
|
"Problem getting groups in AuthLdap.getUsers:" + e);
|
190
|
}
|
191
|
|
192
|
return users;
|
193
|
}
|
194
|
|
195
|
/**
|
196
|
* Get the users for a particular group from the authentication service
|
197
|
*/
|
198
|
public String[] getUsers(String user, String password, String group)
|
199
|
throws ConnectException
|
200
|
{
|
201
|
MetaCatUtil util = new MetaCatUtil();
|
202
|
String ldapUrl = util.getOption("ldapurl");
|
203
|
String ldapBase = util.getOption("ldapbase");
|
204
|
|
205
|
String[] users = null;
|
206
|
|
207
|
// Identify service provider to use
|
208
|
Hashtable env = new Hashtable(11);
|
209
|
env.put(Context.INITIAL_CONTEXT_FACTORY,
|
210
|
"com.sun.jndi.ldap.LdapCtxFactory");
|
211
|
env.put(Context.PROVIDER_URL, ldapUrl + ldapBase);
|
212
|
|
213
|
try {
|
214
|
|
215
|
// Get the dn for this uid or cn
|
216
|
String identifier = getIdentifyingName(user);
|
217
|
if (identifier != null) {
|
218
|
env.put(Context.SECURITY_AUTHENTICATION, "simple");
|
219
|
env.put(Context.SECURITY_PRINCIPAL, identifier + "," + ldapBase);
|
220
|
env.put(Context.SECURITY_CREDENTIALS, password);
|
221
|
|
222
|
// Create the initial directory context
|
223
|
DirContext ctx = new InitialDirContext(env);
|
224
|
|
225
|
// Specify the ids of the attributes to return
|
226
|
String[] attrIDs = {"uid"};
|
227
|
|
228
|
// Specify the attributes to match.
|
229
|
// Ask for objects that have the attribute gid == @group.
|
230
|
Attributes matchAttrs = new BasicAttributes(true); // ignore case
|
231
|
matchAttrs.put(new BasicAttribute("gid", group));
|
232
|
|
233
|
// Search for objects in the current context
|
234
|
// All users should be put in subcontext like "ou=Users"
|
235
|
// NamingEnumeration answer=ctx.search("ou=Users",matchAttrs,attrIDs);
|
236
|
NamingEnumeration enum = ctx.search("", matchAttrs, attrIDs);
|
237
|
|
238
|
// Print the users
|
239
|
Vector uvec = new Vector();
|
240
|
while (enum.hasMore()) {
|
241
|
SearchResult sr = (SearchResult)enum.next();
|
242
|
Attributes attrs = sr.getAttributes();
|
243
|
NamingEnumeration enum1 = attrs.getAll(); // only "uid" attr
|
244
|
while (enum1.hasMore()) {
|
245
|
Attribute attr = (Attribute)enum1.next();
|
246
|
//System.out.print(attr.getID() + "=");
|
247
|
//System.out.println((String)attr.get());
|
248
|
uvec.add(attr.get());
|
249
|
}
|
250
|
}
|
251
|
|
252
|
// initialize users[]; fill users[]
|
253
|
users = new String[uvec.size()];
|
254
|
for (int i=0; i < uvec.size(); i++) {
|
255
|
users[i] = (String)uvec.elementAt(i);
|
256
|
}
|
257
|
|
258
|
// Close the context when we're done
|
259
|
ctx.close();
|
260
|
} else {
|
261
|
util.debugMessage("User not found!");
|
262
|
}
|
263
|
|
264
|
} catch (NamingException e) {
|
265
|
System.err.println("Problem getting users in AuthLdap.getUsers:" + e);
|
266
|
throw new ConnectException(
|
267
|
"Problem getting groups in AuthLdap.getUsers:" + e);
|
268
|
}
|
269
|
|
270
|
return users;
|
271
|
}
|
272
|
|
273
|
/**
|
274
|
* Get all groups from the authentication service
|
275
|
*/
|
276
|
public String[] getGroups(String user, String password)
|
277
|
throws ConnectException
|
278
|
{
|
279
|
MetaCatUtil util = new MetaCatUtil();
|
280
|
String ldapUrl = util.getOption("ldapurl");
|
281
|
String ldapBase = util.getOption("ldapbase");
|
282
|
|
283
|
String[] groups = null;
|
284
|
|
285
|
// Identify service provider to use
|
286
|
Hashtable env = new Hashtable(11);
|
287
|
env.put(Context.INITIAL_CONTEXT_FACTORY,
|
288
|
"com.sun.jndi.ldap.LdapCtxFactory");
|
289
|
env.put(Context.PROVIDER_URL, ldapUrl + ldapBase);
|
290
|
|
291
|
try {
|
292
|
|
293
|
// Get the dn for this uid or cn
|
294
|
String identifier = getIdentifyingName(user);
|
295
|
if (identifier != null) {
|
296
|
env.put(Context.SECURITY_AUTHENTICATION, "simple");
|
297
|
env.put(Context.SECURITY_PRINCIPAL, identifier + "," + ldapBase);
|
298
|
env.put(Context.SECURITY_CREDENTIALS, password);
|
299
|
|
300
|
// Create the initial directory context
|
301
|
DirContext ctx = new InitialDirContext(env);
|
302
|
|
303
|
// Specify the ids of the attributes to return
|
304
|
String[] attrIDs = {"gid"};
|
305
|
SearchControls ctls = new SearchControls();
|
306
|
ctls.setReturningAttributes(attrIDs);
|
307
|
|
308
|
// Specify the search filter to match.
|
309
|
// Ask for objects that do not have "uid".
|
310
|
// This will assure that the directory entries are groups.
|
311
|
String filter = "(!(uid=*))";
|
312
|
|
313
|
// Search for objects in the current context
|
314
|
// All users should be put in subcontext like "ou=Users"
|
315
|
// NamingEnumeration answer = ctx.search("ou=Users", filter,ctls);
|
316
|
NamingEnumeration enum = ctx.search("", filter, ctls);
|
317
|
|
318
|
// Print the users
|
319
|
Vector uvec = new Vector();
|
320
|
while (enum.hasMore()) {
|
321
|
SearchResult sr = (SearchResult)enum.next();
|
322
|
Attributes attrs = sr.getAttributes();
|
323
|
NamingEnumeration enum1 = attrs.getAll(); // only "gid" attr
|
324
|
while (enum1.hasMore()) {
|
325
|
Attribute attr = (Attribute)enum1.next();
|
326
|
//System.out.print(attr.getID() + "=");
|
327
|
//System.out.println((String)attr.get());
|
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 = {"gid"};
|
385
|
|
386
|
// Specify the attributes to match.
|
387
|
// Ask for objects that have the attribute uid == @foruser.
|
388
|
Attributes matchAttrs = new BasicAttributes(true); // ignore case
|
389
|
matchAttrs.put(new BasicAttribute("uid", foruser));
|
390
|
|
391
|
// Search for objects in the current context
|
392
|
// All users should be put in subcontext like "ou=Users"
|
393
|
// NamingEnumeration answer=ctx.search("ou=Users",matchAttrs,attrIDs);
|
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
|
//System.out.print(attr.getID() + "=");
|
405
|
//System.out.println((String)attr.get());
|
406
|
uvec.add(attr.get());
|
407
|
}
|
408
|
}
|
409
|
|
410
|
// initialize groups[] and fill it
|
411
|
groups = new String[uvec.size()];
|
412
|
for (int i=0; i < uvec.size(); i++) {
|
413
|
groups[i] = (String)uvec.elementAt(i);
|
414
|
}
|
415
|
|
416
|
// Close the context when we're done
|
417
|
ctx.close();
|
418
|
} else {
|
419
|
util.debugMessage("User not found!");
|
420
|
}
|
421
|
|
422
|
} catch (NamingException e) {
|
423
|
System.err.println("Problem getting groups in AuthLdap.getGroups:" + e);
|
424
|
throw new ConnectException(
|
425
|
"Problem getting groups in AuthLdap.getGroups:" + e);
|
426
|
}
|
427
|
|
428
|
return groups;
|
429
|
}
|
430
|
|
431
|
/**
|
432
|
* Get attributes describing a user or group
|
433
|
*
|
434
|
* @param user the user for which the attribute list is requested
|
435
|
* @returns HashMap a map of attribute name to a Vector of values
|
436
|
*/
|
437
|
public HashMap getAttributes(String foruser)
|
438
|
throws ConnectException
|
439
|
{
|
440
|
return getAttributes(null, null, foruser);
|
441
|
}
|
442
|
|
443
|
/**
|
444
|
* Get attributes describing a user or group
|
445
|
*
|
446
|
* @param user the user for which the attribute list is requested
|
447
|
* @param authuser the user for authenticating against the service
|
448
|
* @param password the password for authenticating against the service
|
449
|
* @returns HashMap a map of attribute name to a Vector of values
|
450
|
*/
|
451
|
public HashMap getAttributes(String user, String password, String foruser)
|
452
|
throws ConnectException
|
453
|
{
|
454
|
MetaCatUtil util = new MetaCatUtil();
|
455
|
String ldapUrl = util.getOption("ldapurl");
|
456
|
String ldapBase = util.getOption("ldapbase");
|
457
|
|
458
|
HashMap attributes = new HashMap();
|
459
|
|
460
|
// Identify service provider to use
|
461
|
Hashtable env = new Hashtable(11);
|
462
|
env.put(Context.INITIAL_CONTEXT_FACTORY,
|
463
|
"com.sun.jndi.ldap.LdapCtxFactory");
|
464
|
env.put(Context.PROVIDER_URL, ldapUrl + ldapBase);
|
465
|
|
466
|
// Authentication information
|
467
|
|
468
|
try {
|
469
|
|
470
|
if ((user != null) && (password != null)) {
|
471
|
String identifier = getIdentifyingName(user);
|
472
|
env.put(Context.SECURITY_AUTHENTICATION, "simple");
|
473
|
env.put(Context.SECURITY_PRINCIPAL, identifier + "," + ldapBase);
|
474
|
env.put(Context.SECURITY_CREDENTIALS, password);
|
475
|
}
|
476
|
|
477
|
// Create the initial directory context
|
478
|
DirContext ctx = new InitialDirContext(env);
|
479
|
|
480
|
// Find out the identifying attribute for the user
|
481
|
String userident = getIdentifyingName(foruser);
|
482
|
|
483
|
// Ask for all attributes of the user
|
484
|
Attributes attrs = ctx.getAttributes(userident);
|
485
|
|
486
|
// Print all of the attributes
|
487
|
NamingEnumeration en = attrs.getAll();
|
488
|
while (en.hasMore()) {
|
489
|
Attribute att = (Attribute)en.next();
|
490
|
Vector values = new Vector();
|
491
|
String attName = att.getID();
|
492
|
NamingEnumeration attvalues = att.getAll();
|
493
|
while (attvalues.hasMore()) {
|
494
|
String value = (String)attvalues.next();
|
495
|
values.add(value);
|
496
|
}
|
497
|
attributes.put(attName, values);
|
498
|
}
|
499
|
|
500
|
// Close the context when we're done
|
501
|
ctx.close();
|
502
|
} catch (NamingException e) {
|
503
|
System.err.println("Problem getting attributes in AuthLdap.getAttributes:"
|
504
|
+ e);
|
505
|
throw new ConnectException(
|
506
|
"Problem getting attributes in AuthLdap.getAttributes:" + e);
|
507
|
}
|
508
|
|
509
|
return attributes;
|
510
|
}
|
511
|
|
512
|
/**
|
513
|
* Get the identifying name for a given userid or name. This is the name
|
514
|
* that is used in conjunction withthe LDAP BaseDN to create a
|
515
|
* distinguished name (dn) for the record
|
516
|
*
|
517
|
* @param user the user for which the identifying name is requested
|
518
|
* @returns String the identifying name for the user,
|
519
|
* or null if not found
|
520
|
*/
|
521
|
private String getIdentifyingName(String user)
|
522
|
throws NamingException
|
523
|
{
|
524
|
MetaCatUtil util = new MetaCatUtil();
|
525
|
String ldapUrl = util.getOption("ldapurl");
|
526
|
String ldapBase = util.getOption("ldapbase");
|
527
|
|
528
|
String identifier = null;
|
529
|
|
530
|
// Identify service provider to use
|
531
|
Hashtable env = new Hashtable(11);
|
532
|
env.put(Context.INITIAL_CONTEXT_FACTORY,
|
533
|
"com.sun.jndi.ldap.LdapCtxFactory");
|
534
|
env.put(Context.PROVIDER_URL, ldapUrl + ldapBase);
|
535
|
|
536
|
try {
|
537
|
|
538
|
// Bind to the LDAP server, in order to search for the right
|
539
|
// distinguished name (dn) based on userid (uid) or common name (cn)
|
540
|
DirContext ctx = new InitialDirContext(env);
|
541
|
|
542
|
// Search for the user id or name using the uid, then cn and sn attributes
|
543
|
// If we find a record, determine the dn for the record
|
544
|
util.debugMessage("\nStarting search phase...\n");
|
545
|
Attributes matchAttrs = new BasicAttributes(true);
|
546
|
matchAttrs.put(new BasicAttribute("uid", user));
|
547
|
NamingEnumeration answer = ctx.search("", matchAttrs);
|
548
|
if (answer.hasMore()) {
|
549
|
SearchResult sr = (SearchResult)answer.next();
|
550
|
identifier = sr.getName();
|
551
|
util.debugMessage("Found: " + identifier);
|
552
|
} else {
|
553
|
Attributes matchAttrs2 = new BasicAttributes(true);
|
554
|
matchAttrs2.put(new BasicAttribute("cn", user));
|
555
|
NamingEnumeration answer2 = ctx.search("", matchAttrs2);
|
556
|
if (answer2.hasMore()) {
|
557
|
SearchResult sr = (SearchResult)answer2.next();
|
558
|
identifier = sr.getName();
|
559
|
util.debugMessage("Found: " + identifier);
|
560
|
} else {
|
561
|
Attributes matchAttrs3 = new BasicAttributes(true);
|
562
|
matchAttrs3.put(new BasicAttribute("sn", user));
|
563
|
NamingEnumeration answer3 = ctx.search("", matchAttrs3);
|
564
|
if (answer3.hasMore()) {
|
565
|
SearchResult sr = (SearchResult)answer3.next();
|
566
|
identifier = sr.getName();
|
567
|
util.debugMessage("Found: " + identifier);
|
568
|
}
|
569
|
}
|
570
|
}
|
571
|
// Close the context when we're done the initial search
|
572
|
ctx.close();
|
573
|
} catch (NamingException e) {
|
574
|
util.debugMessage("Naming exception while getting dn: " + e);
|
575
|
System.out.println("Naming exception in AuthLdap.getIdentifyingName");
|
576
|
throw new NamingException(
|
577
|
"Naming exception in AuthLdap.getIdentifyingName: " + e);
|
578
|
}
|
579
|
|
580
|
return identifier;
|
581
|
}
|
582
|
|
583
|
/**
|
584
|
* Get list of all groups and users from authentication scheme.
|
585
|
* The output is formatted in XML.
|
586
|
*/
|
587
|
private String getPrincipals(String user, String password)
|
588
|
throws ConnectException
|
589
|
{
|
590
|
StringBuffer out = new StringBuffer();
|
591
|
String[] groups = getGroups(user, password);
|
592
|
|
593
|
out.append("<?xml version=\"1.0\"?>\n");
|
594
|
out.append("<principals>\n");
|
595
|
|
596
|
// for the groups and users that belong to them
|
597
|
if ( groups.length > 0 ) {
|
598
|
for (int i=0; i < groups.length; i++ ) {
|
599
|
out.append(" <group>\n");
|
600
|
out.append(" <groupname>" + groups[i] + "<groupname>\n");
|
601
|
String[] usersForGroup = getUsers(user,password,groups[i]);
|
602
|
for (int j=0; j <= usersForGroup.length; 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
|
// for the users only when there are no any groups defined
|
610
|
} else {
|
611
|
String[] users = getUsers(user, password);
|
612
|
for (int j=0; j < users.length; j++ ) {
|
613
|
out.append(" <user>\n");
|
614
|
out.append(" <username>" + users[j] + "<username>\n");
|
615
|
out.append(" </user>\n");
|
616
|
}
|
617
|
}
|
618
|
|
619
|
out.append("</principals>");
|
620
|
return out.toString();
|
621
|
}
|
622
|
|
623
|
/**
|
624
|
* Test method for the class
|
625
|
*/
|
626
|
public static void main(String[] args) {
|
627
|
|
628
|
// Provide a user, such as: "Matt Jones", or "jones"
|
629
|
String user = args[0];
|
630
|
String password = args[1];
|
631
|
|
632
|
AuthLdap authservice = new AuthLdap();
|
633
|
|
634
|
boolean isValid = false;
|
635
|
try {
|
636
|
isValid = authservice.authenticate(user, password);
|
637
|
if (isValid) {
|
638
|
System.out.println("Authentication successful for: " + user );
|
639
|
System.out.println(" ");
|
640
|
} else {
|
641
|
System.out.println("Authentication failed for: " + user);
|
642
|
}
|
643
|
|
644
|
if (isValid) {
|
645
|
HashMap userInfo = authservice.getAttributes(user, password, user);
|
646
|
|
647
|
// Print all of the attributes
|
648
|
Iterator attList = (Iterator)(((Set)userInfo.keySet()).iterator());
|
649
|
while (attList.hasNext()) {
|
650
|
String att = (String)attList.next();
|
651
|
Vector values = (Vector)userInfo.get(att);
|
652
|
Iterator attvalues = values.iterator();
|
653
|
while (attvalues.hasNext()) {
|
654
|
String value = (String)attvalues.next();
|
655
|
System.out.println(att + ": " + value);
|
656
|
}
|
657
|
}
|
658
|
|
659
|
}
|
660
|
|
661
|
/*
|
662
|
// get the whole list of users
|
663
|
if (isValid) {
|
664
|
String[] users = authservice.getUsers(user, password);
|
665
|
for (int i=0; i < users.length; i++) {
|
666
|
System.out.println(users[i]);
|
667
|
}
|
668
|
}
|
669
|
*/
|
670
|
/*
|
671
|
// get the whole list of users for a group
|
672
|
if (isValid) {
|
673
|
String group = args[2];
|
674
|
String[] users = authservice.getUsers(user, password, group);
|
675
|
for (int i=0; i < users.length; i++) {
|
676
|
System.out.println(users[i]);
|
677
|
}
|
678
|
}
|
679
|
*/
|
680
|
/* // get the whole list groups and users in XML format
|
681
|
if (isValid) {
|
682
|
String out = authservice.getPrincipals(user, password);
|
683
|
java.io.File f = new java.io.File("principals.txt");
|
684
|
java.io.FileWriter fw = new java.io.FileWriter(f);
|
685
|
java.io.BufferedWriter buff = new java.io.BufferedWriter(fw);
|
686
|
buff.write(out);
|
687
|
buff.flush();
|
688
|
buff.close();
|
689
|
fw.close();
|
690
|
}
|
691
|
*/
|
692
|
} catch (ConnectException ce) {
|
693
|
System.err.println("Error connecting to LDAP server in authldap.main");
|
694
|
} catch (java.io.IOException ioe) {
|
695
|
System.err.println("I/O Error writing to file principals.txt");
|
696
|
}
|
697
|
}
|
698
|
}
|