Project

General

Profile

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-10-31 15:26:43 -0800 (Tue, 31 Oct 2000) $'
12
 * '$Revision: 509 $'
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 RMIControllerInterface;
27
import java.util.PropertyResourceBundle;
28

    
29
/**
30
 * An implementation of the AuthInterface interface that
31
 * allows Metacat to use the SRB/MCAT for authentication.
32
 * The SRB/MCAT authentication service is used to determine if a user 
33
 * is authenticated, and whether they are a member of a particular group.
34
 */
35
class AuthMcat implements AuthInterface {
36
  
37
  String username = null;
38
  String password = null;
39
  
40
  // DCS-ATTRIBUTE-INDEX
41
  // They are from mdasC_db2_externs.h.
42
  final static int USER_NAME = 7;
43
  final static int USER_GROUP_NAME = 3;
44
    
45
  // JNI (Java Native Interface) routines for SRB
46
  static String srbHost;
47
  static String srbPort;
48
  static String RMIhost;
49
  static RMIControllerInterface rmicon;
50
  static SrbJavaGlueInterface srbJG;
51
  static {
52
    try { 
53
      PropertyResourceBundle SRBProps = null;
54
      // SRB properties that tells about the location of SRB RMI Server
55
      // srbProps.properties should reside on metacat server
56
      SRBProps = (PropertyResourceBundle)
57
           PropertyResourceBundle.getBundle("edu.ucsb.nceas.metacat.srbProps");
58
      srbHost = (String)SRBProps.handleGetObject("host");
59
      srbPort = (String)SRBProps.handleGetObject("port");
60
      // should handle missing RMIhost name here
61
      RMIhost = (String)SRBProps.handleGetObject("RMIhost");
62
      String name = "//" + RMIhost + "/RMIController";
63
      rmicon = (RMIControllerInterface)Naming.lookup(name);
64
      name = "//" + RMIhost + "/SrbJavaGlue";
65
      srbJG = (SrbJavaGlueInterface)Naming.lookup(name);
66
    } catch (Exception e) {
67
      System.err.println("AuthMcat static: " + e.getMessage());
68
    }    
69
  }    
70

    
71

    
72
  /**
73
   * Determine if a user/password are valid according to the authentication
74
   * service.
75
   *
76
   * @param user the name of the principal to authenticate
77
   * @param password the password to use for authentication
78
   * @returns boolean true if authentication successful, false otherwise
79
   */
80
  public boolean authenticate(String user, String password)
81
                              throws ConnectException
82
  {
83
      try { 
84
        // make connection to SRB.
85
        int srbconn = srbConnect(user, password);
86
        if ( srbconn > 0 ) {
87
          // disconnect from SRB
88
          srbDisconnect(srbconn);
89
          return true;
90
        }
91

    
92
      } catch ( RemoteException re) {
93
        throw new ConnectException(re.getMessage());
94
      }
95
      
96
      return false;
97
  }
98

    
99
  /**
100
   * Get all users from the authentication service
101
   */
102
  public String[] getUsers(String user, String password)
103
                            throws ConnectException
104
  {
105
                              
106
      try { 
107
        // make connection to SRB.
108
        int srbconn = srbConnect(user, password);
109
        if ( srbconn > 0 ) {
110
          // get all users
111
          String[] userlist = getUsernames(srbconn);
112
          // disconnect from SRB
113
          srbDisconnect(srbconn);
114
          return userlist;
115
        }
116

    
117
      } catch ( RemoteException re) {
118
        throw new ConnectException(re.getMessage());
119
      }
120
      
121
      return null;
122
  }
123

    
124
  /**
125
   * Get the users for a particular group from the authentication service
126
   */
127
  public String[] getUsers(String user, String password, String group)
128
                           throws ConnectException
129
  {
130
      try { 
131
        // make connection to SRB.
132
        int srbconn = srbConnect(user, password);
133
        if ( srbconn > 0 ) {
134
          // get users for @group
135
          String[] userlist = getUsernames(srbconn, group);
136
          // disconnect from SRB
137
          srbDisconnect(srbconn);
138
          return userlist;
139
        }
140

    
141
      } catch ( RemoteException re) {
142
        throw new ConnectException(re.getMessage());
143
      }
144
      
145
      return null;
146
  }
147

    
148
  /**
149
   * Get all groups from the authentication service
150
   */
151
  public String[] getGroups(String user, String password)
152
                            throws ConnectException
153
  {
154
      try { 
155
        // make connection to SRB.
156
        int srbconn = srbConnect(user, password);
157
        if ( srbconn > 0 ) {
158
          // get all group
159
          String[] userlist = getGroupnames(srbconn);
160
          // disconnect from SRB
161
          srbDisconnect(srbconn);
162
          return userlist;
163
        }
164
 
165
      } catch ( RemoteException re) {
166
        throw new ConnectException(re.getMessage());
167
      }
168
      
169
      return null;
170
  }
171

    
172
  /**
173
   * Get the groups for a particular user from the authentication service
174
   */
175
  public String[] getGroups(String user, String password, String foruser)
176
                            throws ConnectException
177
  {
178
      try { 
179
        // make connection to SRB.
180
        int srbconn = srbConnect(user, password);
181
        if ( srbconn > 0 ) {
182
          // get groups for @foruser
183
          String[] userlist = getGroupnames(srbconn, foruser);
184
          // disconnect from SRB
185
          srbDisconnect(srbconn);
186
          return userlist;
187
        }
188

    
189
      } catch ( RemoteException re) {
190
        throw new ConnectException(re.getMessage());
191
      }
192
      
193
      return null;
194
  }
195

    
196
  /**
197
   * Get attributes describing a user or group
198
   *
199
   * @param user the user for which the attribute list is requested
200
   * @returns HashMap a map of attribute name to a Vector of values
201
   */
202
  public HashMap getAttributes(String user) 
203
         throws ConnectException
204
  {
205
    return getAttributes(user, null, null);
206
  }
207

    
208
  /**
209
   * Get attributes describing a user or group
210
   *
211
   * @param user the user for which the attribute list is requested
212
   * @param authuser the user for authenticating against the service
213
   * @param password the password for authenticating against the service
214
   * @returns HashMap a map of attribute name to a Vector of values
215
   */
216
  public HashMap getAttributes(String user, String authuser, String password) 
217
         throws ConnectException
218
  {
219
    // NOT IMPLEMENTED YET
220
    return null;
221
  }
222
  
223
  /**
224
   * Get SRB RMI Connection 
225
   */
226
  private int srbConnect(String username, String password)
227
                                 throws RemoteException  { 
228

    
229
    int srbconn = 0;
230

    
231
    // look up for SRBJavaGlue
232
    try {
233
      String name = "//" + RMIhost + "/SrbJavaGlue";
234
      srbJG = (SrbJavaGlueInterface)Naming.lookup(name);
235
    } catch (Exception e) {
236
      try {
237
        rmicon.restart();
238
      } catch (Exception rmie) {}
239
      // The SRB server is not running or it is busy now
240
      throw new 
241
      RemoteException("AuthMcat look up for SrbJavaGlue failed");
242
    }
243

    
244
    // try SRB RMI Connection
245
    // integer value of the SBR Connection ID is returned only
246
    try { 
247
      srbconn = srbJG.rmiConnectJ( srbHost, srbPort, password, username );
248
    } catch (RemoteException e) {
249
      try {
250
        rmicon.restart();
251
      } catch (Exception rmie) {}
252
      throw new RemoteException("AuthMcat.userAuth() - " +
253
                                "Error on rmiConnectJ(): " + e.getMessage());
254
    }
255
      
256
    // check if successfull
257
    if ( srbconn == 0 ) {
258
      throw new RemoteException("The SRB Server is not running or it is busy now");
259
    } else if ( srbconn < 0 ) {
260
      try {
261
        rmicon.restart();
262
      } catch (Exception rmie) {}
263
      return srbconn;
264
    }
265

    
266
    return srbconn; 
267

    
268
  }
269

    
270
  /**
271
   * Close SRB RMI Connection 
272
   */
273
  private void srbDisconnect(int srbconn) throws RemoteException
274
  { 
275
    try {
276
      int err = srbJG.rmiFinishJ( srbconn );
277
    } catch (RemoteException e) {}
278

    
279
  }
280

    
281
  // get the names of groups for given user
282
  private String[] getGroupnames (int conn, String username) 
283
                                throws RemoteException    {
284
    String[] qval;
285
	  int[] qvalInx;
286
    int[] selVal;
287

    
288
    qval = new String [2];
289
    qval[0] = new String (" not like  '%$deleted%'");
290
    qval[1] = new String (" = '" + username + "'");
291
//    qval[2] = new String (" <> 'public'");
292

    
293
    qvalInx = new int[2];
294
    qvalInx[0] = USER_NAME;
295
    qvalInx[1] = USER_NAME;
296
//    qvalInx[2] = USER_GROUP_NAME;
297

    
298
    selVal = new int[1];
299
    selVal[0] = USER_GROUP_NAME;
300
	
301
    // generate a query and get the resultset
302
    String[] selRes = getGenQueResult (conn, qval, qvalInx, selVal);
303
    return selRes;
304
  }
305

    
306
  // get the names of all groups
307
  private String[] getGroupnames (int conn) 
308
                            throws RemoteException    {
309
    String[] qval;
310
	  int[] qvalInx;
311
    int[] selVal;
312

    
313
    qval = new String [1];
314
    qval[0] = new String (" not like  '%$deleted%'");
315
//    qval[1] = new String (" <> 'public'");
316

    
317
    qvalInx = new int[1];
318
    qvalInx[0] = USER_NAME;
319
//    qvalInx[1] = USER_GROUP_NAME;
320

    
321
    selVal = new int[1];
322
    selVal[0] = USER_GROUP_NAME;
323
	
324
    // generate a query and get the resultset
325
    String[] selRes = getGenQueResult (conn, qval, qvalInx, selVal);
326
    return selRes;
327
  }
328

    
329
  // get the names of users for given group
330
  private String[] getUsernames (int conn, String groupname) 
331
                              throws RemoteException    {
332
    String[] qval;
333
	  int[] qvalInx;
334
    int[] selVal;
335

    
336
    qval = new String [2];
337
    qval[0] = new String (" not like  '%$deleted%'");
338
    qval[1] = new String (" = '" + groupname + "'");
339
//    qval[2] = new String (" <> 'public'");
340

    
341
    qvalInx = new int[2];
342
    qvalInx[0] = USER_NAME;
343
    qvalInx[1] = USER_GROUP_NAME;
344
//    qvalInx[2] = USER_GROUP_NAME;
345

    
346
    selVal = new int[1];
347
    selVal[0] = USER_NAME;
348
	
349
    // generate a query and get the resultset
350
    String[] selRes = getGenQueResult (conn, qval, qvalInx, selVal);
351
    return selRes;
352
  }
353

    
354
  // get the names of all users
355
  private String[] getUsernames (int conn) 
356
                            throws RemoteException    {
357
    String[] qval;
358
	  int[] qvalInx;
359
    int[] selVal;
360

    
361
    qval = new String [1];
362
    qval[0] = new String (" not like  '%$deleted%'");
363
//    qval[1] = new String (" <> 'public'");
364

    
365
    qvalInx = new int[1];
366
    qvalInx[0] = USER_NAME;
367
//    qvalInx[1] = USER_GROUP_NAME;
368

    
369
    selVal = new int[1];
370
    selVal[0] = USER_NAME;
371
	
372
    // generate a query and get the resultset
373
    String[] selRes = getGenQueResult (conn, qval, qvalInx, selVal);
374
    return selRes;
375
  }
376

    
377

    
378
  // try to generate query and run and retrieve all rows
379
  private String[] getGenQueResult(int conn, String[] qval,
380
                                int qvalInx[], int selVal[])
381
                                throws RemoteException      {
382
    
383
    int inx = 0;
384
    int queResultCount = 0;
385
    int queResultIndex = 0;
386
    int selLen = selVal.length;
387
    String[] queResult = new String[selLen];
388
    String[] resultset;
389
 
390
    try {
391
      queResultCount = srbJG.srbGenQuery( conn, 0, qval, qvalInx, selVal);
392
      resultset = new String[queResultCount];
393
      
394
      // get first rows
395
      for (int i = 0; i < queResultCount; i++) {
396
        for (int j = 0; j < selLen; j++) {
397
          queResult[j] = srbJG.getGenQueResultJ(j, queResultIndex);
398
        }
399
        resultset[inx++] = queResult[0]; 
400
        queResultIndex++;
401
      } 
402

    
403
      // get next rows
404
      while (queResult != null) {
405
        if ( queResultIndex >= queResultCount ) {
406
          queResultIndex = 0;
407
          queResultCount = srbJG.getMoreGenQueRowsJ(conn, 0);
408
          if (queResultCount <= 0) {
409
            return resultset;
410
          }  
411
        } 
412

    
413
        for (int i = 0; i < selLen; i++) {
414
          queResult[i] = srbJG.getGenQueResultJ(i, queResultIndex);
415
        }    
416
        resultset[inx++] = queResult[0];
417
        queResultIndex++;
418
      }  
419

    
420
    } catch (RemoteException re) {
421
      throw re;
422
    }
423
        
424
    return resultset;
425
  }
426

    
427
  /**
428
   * Test method for the class
429
   */
430
  public static void main(String[] args) {
431

    
432
    // Provide a user, such as: "cn=Matt Jones"
433
    String user = args[0];
434
    String password = args[1];
435

    
436
    AuthMcat authservice = new AuthMcat();
437

    
438
    boolean isValid = false;
439
    try {
440
  //    isValid = authservice.authenticate(user, password);
441
      if (isValid) {
442
        System.out.println("Authentication successful for: " + user );
443
        System.out.println(" ");
444
      } else {
445
        System.out.println("Authentication failed for: " + user);
446
      }
447

    
448
      String[] userlist = authservice.getUsers(user, password);
449
      for (int i = 0; i < userlist.length; i++) {
450
        System.out.println(userlist[i]);
451
      }
452
      System.out.println("All Users: " + userlist.length);
453
    
454
/*
455
      if (isValid) {
456
        HashMap userInfo = authservice.getAttributes(user);
457

    
458
        // Print all of the attributes
459
        Iterator attList = (Iterator)(((Set)userInfo.keySet()).iterator());
460
        while (attList.hasNext()) {
461
          String att = (String)attList.next();
462
          Vector values = (Vector)userInfo.get(att);
463
          System.out.println(att + ": " );
464
          Iterator attvalues = values.iterator();
465
          while (attvalues.hasNext()) {
466
            String value = (String)attvalues.next();
467
            System.out.println("  value: " + value);
468
          }
469
        }
470
      }
471
*/
472
    } catch (ConnectException ce) {
473
      System.err.println(ce.getMessage());
474
    }
475
  }
476
}
(6-6/33)