1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: An implementation of the AuthInterface interface that
|
4
|
* allows Metacat to use the SRB/MCAT for authentication
|
5
|
* Copyright: 2000 Regents of the University of California and the
|
6
|
* National Center for Ecological Analysis and Synthesis
|
7
|
* Authors: Jivka Bojilova
|
8
|
* Release: @release@
|
9
|
*
|
10
|
* '$Author: bojilova $'
|
11
|
* '$Date: 2000-11-13 10:11:03 -0800 (Mon, 13 Nov 2000) $'
|
12
|
* '$Revision: 527 $'
|
13
|
*/
|
14
|
|
15
|
package edu.ucsb.nceas.metacat;
|
16
|
|
17
|
import java.net.ConnectException;
|
18
|
|
19
|
import java.util.Iterator;
|
20
|
import java.util.HashMap;
|
21
|
import java.util.Hashtable;
|
22
|
import java.util.Set;
|
23
|
import java.util.Vector;
|
24
|
import java.rmi.*;
|
25
|
import SrbJavaGlueInterface;
|
26
|
import java.util.PropertyResourceBundle;
|
27
|
|
28
|
/**
|
29
|
* An implementation of the AuthInterface interface that
|
30
|
* allows Metacat to use the SRB/MCAT for authentication.
|
31
|
* The SRB/MCAT authentication service is used to determine if a user
|
32
|
* is authenticated, and whether they are a member of a particular group.
|
33
|
*/
|
34
|
public class AuthMcat implements AuthInterface {
|
35
|
|
36
|
String username = null;
|
37
|
String password = null;
|
38
|
|
39
|
// DCS-ATTRIBUTE-INDEX
|
40
|
// They are from mdasC_db2_externs.h.
|
41
|
final static int USER_NAME = 7;
|
42
|
final static int USER_GROUP_NAME = 3;
|
43
|
|
44
|
// JNI (Java Native Interface) routines for SRB
|
45
|
static String srbHost;
|
46
|
static String srbPort;
|
47
|
static String srbDomain;
|
48
|
static SrbJavaGlueInterface srbJG;
|
49
|
static {
|
50
|
try {
|
51
|
PropertyResourceBundle SRBProps = null;
|
52
|
// SRB properties that tells about the location of SRB RMI Server
|
53
|
// srbProps.properties should reside on metacat server
|
54
|
SRBProps = (PropertyResourceBundle)
|
55
|
PropertyResourceBundle.getBundle("edu.ucsb.nceas.metacat.srbProps");
|
56
|
srbHost = (String)SRBProps.handleGetObject("host");
|
57
|
srbPort = (String)SRBProps.handleGetObject("port");
|
58
|
srbDomain = (String)SRBProps.handleGetObject("domain");
|
59
|
// should handle missing RMIhost name here
|
60
|
String name = "//" + srbHost + "/SrbJavaGlue";
|
61
|
srbJG = (SrbJavaGlueInterface)Naming.lookup(name);
|
62
|
} catch (Exception e) {
|
63
|
System.err.println("AuthMcat static: " + e.getMessage());
|
64
|
}
|
65
|
}
|
66
|
|
67
|
|
68
|
/**
|
69
|
* Determine if a user/password are valid according to the authentication
|
70
|
* service.
|
71
|
*
|
72
|
* @param user the name of the principal to authenticate
|
73
|
* @param password the password to use for authentication
|
74
|
* @returns boolean true if authentication successful, false otherwise
|
75
|
*/
|
76
|
public boolean authenticate(String user, String password)
|
77
|
throws ConnectException
|
78
|
{
|
79
|
try {
|
80
|
// make connection to SRB.
|
81
|
int srbconn = srbConnect(user, password);
|
82
|
if ( srbconn > 0 ) {
|
83
|
// disconnect from SRB
|
84
|
srbDisconnect(srbconn);
|
85
|
return true;
|
86
|
}
|
87
|
|
88
|
} catch ( RemoteException re) {
|
89
|
throw new ConnectException(re.getMessage());
|
90
|
}
|
91
|
|
92
|
return false;
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
* Get all users from the authentication service
|
97
|
*/
|
98
|
public String[] getUsers(String user, String password)
|
99
|
throws ConnectException
|
100
|
{
|
101
|
|
102
|
try {
|
103
|
// make connection to SRB.
|
104
|
int srbconn = srbConnect(user, password);
|
105
|
if ( srbconn > 0 ) {
|
106
|
// get all users
|
107
|
String[] userlist = getUsernames(srbconn);
|
108
|
// disconnect from SRB
|
109
|
srbDisconnect(srbconn);
|
110
|
return userlist;
|
111
|
}
|
112
|
|
113
|
} catch ( RemoteException re) {
|
114
|
throw new ConnectException(re.getMessage());
|
115
|
}
|
116
|
|
117
|
return null;
|
118
|
}
|
119
|
|
120
|
/**
|
121
|
* Get the users for a particular group from the authentication service
|
122
|
*/
|
123
|
public String[] getUsers(String user, String password, String group)
|
124
|
throws ConnectException
|
125
|
{
|
126
|
try {
|
127
|
// make connection to SRB.
|
128
|
int srbconn = srbConnect(user, password);
|
129
|
if ( srbconn > 0 ) {
|
130
|
// get users for @group
|
131
|
String[] userlist = getUsernames(srbconn, group);
|
132
|
// disconnect from SRB
|
133
|
srbDisconnect(srbconn);
|
134
|
return userlist;
|
135
|
}
|
136
|
|
137
|
} catch ( RemoteException re) {
|
138
|
throw new ConnectException(re.getMessage());
|
139
|
}
|
140
|
|
141
|
return null;
|
142
|
}
|
143
|
|
144
|
/**
|
145
|
* Get all groups from the authentication service
|
146
|
*/
|
147
|
public String[] getGroups(String user, String password)
|
148
|
throws ConnectException
|
149
|
{
|
150
|
try {
|
151
|
// make connection to SRB.
|
152
|
int srbconn = srbConnect(user, password);
|
153
|
if ( srbconn > 0 ) {
|
154
|
// get all group
|
155
|
String[] userlist = getGroupnames(srbconn);
|
156
|
// disconnect from SRB
|
157
|
srbDisconnect(srbconn);
|
158
|
return userlist;
|
159
|
}
|
160
|
|
161
|
} catch ( RemoteException re) {
|
162
|
throw new ConnectException(re.getMessage());
|
163
|
}
|
164
|
|
165
|
return null;
|
166
|
}
|
167
|
|
168
|
/**
|
169
|
* Get the groups for a particular user from the authentication service
|
170
|
*/
|
171
|
public String[] getGroups(String user, String password, String foruser)
|
172
|
throws ConnectException
|
173
|
{
|
174
|
try {
|
175
|
// make connection to SRB.
|
176
|
int srbconn = srbConnect(user, password);
|
177
|
if ( srbconn > 0 ) {
|
178
|
// get groups for @foruser
|
179
|
String[] userlist = getGroupnames(srbconn, foruser);
|
180
|
// disconnect from SRB
|
181
|
srbDisconnect(srbconn);
|
182
|
return userlist;
|
183
|
}
|
184
|
|
185
|
} catch ( RemoteException re) {
|
186
|
throw new ConnectException(re.getMessage());
|
187
|
}
|
188
|
|
189
|
return null;
|
190
|
}
|
191
|
|
192
|
/**
|
193
|
* Get attributes describing a user or group
|
194
|
*
|
195
|
* @param user the user for which the attribute list is requested
|
196
|
* @returns HashMap a map of attribute name to a Vector of values
|
197
|
*/
|
198
|
public HashMap getAttributes(String foruser)
|
199
|
throws ConnectException
|
200
|
{
|
201
|
return getAttributes(null, null, foruser);
|
202
|
}
|
203
|
|
204
|
/**
|
205
|
* Get attributes describing a user or group
|
206
|
*
|
207
|
* @param user the user for which the attribute list is requested
|
208
|
* @param authuser the user for authenticating against the service
|
209
|
* @param password the password for authenticating against the service
|
210
|
* @returns HashMap a map of attribute name to a Vector of values
|
211
|
*/
|
212
|
public HashMap getAttributes(String user, String password, String foruser)
|
213
|
throws ConnectException
|
214
|
{
|
215
|
// NOT IMPLEMENTED YET
|
216
|
return null;
|
217
|
}
|
218
|
|
219
|
/**
|
220
|
* Get SRB RMI Connection
|
221
|
*/
|
222
|
private int srbConnect(String username, String password)
|
223
|
throws RemoteException {
|
224
|
|
225
|
synchronized(this) {
|
226
|
int srbconn = 0;
|
227
|
|
228
|
// try SRB RMI Connection
|
229
|
// integer value of the SBR Connection ID is returned only
|
230
|
try {
|
231
|
String name = "//" + srbHost + "/SrbJavaGlue";
|
232
|
srbJG = (SrbJavaGlueInterface)Naming.lookup(name);
|
233
|
srbconn=srbJG.clConnectJ(srbHost,srbPort,password,username,srbDomain);
|
234
|
} catch (Exception e) {
|
235
|
throw new
|
236
|
RemoteException("AuthMcat.srbConnect():" + e.getMessage());
|
237
|
}
|
238
|
|
239
|
// check if successfull
|
240
|
if ( srbconn == 0 ) {
|
241
|
throw new
|
242
|
RemoteException("The SRB Server is not running or it is busy now");
|
243
|
} else if ( srbconn < 0 ) {
|
244
|
return srbconn;
|
245
|
}
|
246
|
|
247
|
return srbconn;
|
248
|
|
249
|
} // end of synchronized(this)
|
250
|
}
|
251
|
|
252
|
/**
|
253
|
* Close SRB RMI Connection
|
254
|
*/
|
255
|
private void srbDisconnect(int srbconn) throws RemoteException
|
256
|
{
|
257
|
try {
|
258
|
int err = srbJG.clFinishJ( srbconn );
|
259
|
} catch (RemoteException e) {}
|
260
|
|
261
|
}
|
262
|
|
263
|
// get the names of groups for given user
|
264
|
private String[] getGroupnames (int conn, String username)
|
265
|
throws RemoteException {
|
266
|
String[] qval;
|
267
|
int[] qvalInx;
|
268
|
int[] selVal;
|
269
|
|
270
|
qval = new String [2];
|
271
|
qval[0] = new String (" not like '%$deleted%'");
|
272
|
qval[1] = new String (" = '" + username + "'");
|
273
|
// qval[2] = new String (" <> 'public'");
|
274
|
|
275
|
qvalInx = new int[2];
|
276
|
qvalInx[0] = USER_NAME;
|
277
|
qvalInx[1] = USER_NAME;
|
278
|
// qvalInx[2] = USER_GROUP_NAME;
|
279
|
|
280
|
selVal = new int[1];
|
281
|
selVal[0] = USER_GROUP_NAME;
|
282
|
|
283
|
// generate a query and get the resultset
|
284
|
String[] selRes = getGenQueResult (conn, qval, qvalInx, selVal);
|
285
|
return selRes;
|
286
|
}
|
287
|
|
288
|
// get the names of all groups
|
289
|
private String[] getGroupnames (int conn)
|
290
|
throws RemoteException {
|
291
|
String[] qval;
|
292
|
int[] qvalInx;
|
293
|
int[] selVal;
|
294
|
|
295
|
qval = new String [1];
|
296
|
qval[0] = new String (" not like '%$deleted%'");
|
297
|
// qval[1] = new String (" <> 'public'");
|
298
|
|
299
|
qvalInx = new int[1];
|
300
|
qvalInx[0] = USER_NAME;
|
301
|
// qvalInx[1] = USER_GROUP_NAME;
|
302
|
|
303
|
selVal = new int[1];
|
304
|
selVal[0] = USER_GROUP_NAME;
|
305
|
|
306
|
// generate a query and get the resultset
|
307
|
String[] selRes = getGenQueResult (conn, qval, qvalInx, selVal);
|
308
|
return selRes;
|
309
|
}
|
310
|
|
311
|
// get the names of users for given group
|
312
|
private String[] getUsernames (int conn, String groupname)
|
313
|
throws RemoteException {
|
314
|
String[] qval;
|
315
|
int[] qvalInx;
|
316
|
int[] selVal;
|
317
|
|
318
|
qval = new String [2];
|
319
|
qval[0] = new String (" not like '%$deleted%'");
|
320
|
qval[1] = new String (" = '" + groupname + "'");
|
321
|
// qval[2] = new String (" <> 'public'");
|
322
|
|
323
|
qvalInx = new int[2];
|
324
|
qvalInx[0] = USER_NAME;
|
325
|
qvalInx[1] = USER_GROUP_NAME;
|
326
|
// qvalInx[2] = USER_GROUP_NAME;
|
327
|
|
328
|
selVal = new int[1];
|
329
|
selVal[0] = USER_NAME;
|
330
|
|
331
|
// generate a query and get the resultset
|
332
|
String[] selRes = getGenQueResult (conn, qval, qvalInx, selVal);
|
333
|
return selRes;
|
334
|
}
|
335
|
|
336
|
// get the names of all users
|
337
|
private String[] getUsernames (int conn)
|
338
|
throws RemoteException {
|
339
|
String[] qval;
|
340
|
int[] qvalInx;
|
341
|
int[] selVal;
|
342
|
|
343
|
qval = new String [1];
|
344
|
qval[0] = new String (" not like '%$deleted%'");
|
345
|
// qval[1] = new String (" <> 'public'");
|
346
|
|
347
|
qvalInx = new int[1];
|
348
|
qvalInx[0] = USER_NAME;
|
349
|
// qvalInx[1] = USER_GROUP_NAME;
|
350
|
|
351
|
selVal = new int[1];
|
352
|
selVal[0] = USER_NAME;
|
353
|
|
354
|
// generate a query and get the resultset
|
355
|
String[] selRes = getGenQueResult (conn, qval, qvalInx, selVal);
|
356
|
return selRes;
|
357
|
}
|
358
|
|
359
|
|
360
|
// try to generate query and run and retrieve all rows
|
361
|
private String[] getGenQueResult(int conn, String[] qval,
|
362
|
int qvalInx[], int selVal[])
|
363
|
throws RemoteException {
|
364
|
|
365
|
int inx = 0;
|
366
|
int queResultCount = 0;
|
367
|
int queResultIndex = 0;
|
368
|
int selLen = selVal.length;
|
369
|
String[] queResult = new String[selLen];
|
370
|
String[] resultset;
|
371
|
|
372
|
try {
|
373
|
queResultCount = srbJG.srbGenQuery( conn, 0, qval, qvalInx, selVal);
|
374
|
resultset = new String[queResultCount];
|
375
|
|
376
|
// get first rows
|
377
|
for (int i = 0; i < queResultCount; i++) {
|
378
|
for (int j = 0; j < selLen; j++) {
|
379
|
queResult[j] = srbJG.getGenQueResultJ(j, queResultIndex);
|
380
|
}
|
381
|
resultset[inx++] = queResult[0];
|
382
|
queResultIndex++;
|
383
|
}
|
384
|
|
385
|
// get next rows
|
386
|
while (queResult != null) {
|
387
|
if ( queResultIndex >= queResultCount ) {
|
388
|
queResultIndex = 0;
|
389
|
queResultCount = srbJG.getMoreGenQueRowsJ(conn, 0);
|
390
|
if (queResultCount <= 0) {
|
391
|
return resultset;
|
392
|
}
|
393
|
}
|
394
|
|
395
|
for (int i = 0; i < selLen; i++) {
|
396
|
queResult[i] = srbJG.getGenQueResultJ(i, queResultIndex);
|
397
|
}
|
398
|
resultset[inx++] = queResult[0];
|
399
|
queResultIndex++;
|
400
|
}
|
401
|
|
402
|
} catch (RemoteException re) {
|
403
|
throw re;
|
404
|
}
|
405
|
|
406
|
return resultset;
|
407
|
}
|
408
|
|
409
|
/**
|
410
|
* Test method for the class
|
411
|
*/
|
412
|
public static void main(String[] args) {
|
413
|
|
414
|
// Provide a user, such as: "cn=Matt Jones"
|
415
|
String user = args[0];
|
416
|
String password = args[1];
|
417
|
|
418
|
AuthMcat authservice = new AuthMcat();
|
419
|
|
420
|
boolean isValid = false;
|
421
|
try {
|
422
|
isValid = authservice.authenticate(user, password);
|
423
|
if (isValid) {
|
424
|
System.out.println("Authentication successful for: " + user );
|
425
|
System.out.println(" ");
|
426
|
} else {
|
427
|
System.out.println("Authentication failed for: " + user);
|
428
|
}
|
429
|
|
430
|
String[] userlist = authservice.getUsers(user, password);
|
431
|
for (int i = 0; i < userlist.length; i++) {
|
432
|
System.out.println(userlist[i]);
|
433
|
}
|
434
|
System.out.println("All Users: " + userlist.length);
|
435
|
|
436
|
/*
|
437
|
if (isValid) {
|
438
|
HashMap userInfo = authservice.getAttributes(user);
|
439
|
|
440
|
// Print all of the attributes
|
441
|
Iterator attList = (Iterator)(((Set)userInfo.keySet()).iterator());
|
442
|
while (attList.hasNext()) {
|
443
|
String att = (String)attList.next();
|
444
|
Vector values = (Vector)userInfo.get(att);
|
445
|
System.out.println(att + ": " );
|
446
|
Iterator attvalues = values.iterator();
|
447
|
while (attvalues.hasNext()) {
|
448
|
String value = (String)attvalues.next();
|
449
|
System.out.println(" value: " + value);
|
450
|
}
|
451
|
}
|
452
|
}
|
453
|
*/
|
454
|
} catch (ConnectException ce) {
|
455
|
System.err.println(ce.getMessage());
|
456
|
}
|
457
|
}
|
458
|
}
|