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