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-11-03 12:02:20 -0800 (Fri, 03 Nov 2000) $'
12
 * '$Revision: 512 $'
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 
259
      RemoteException("The SRB Server is not running or it is busy now");
260
    } else if ( srbconn < 0 ) {
261
      try {
262
        rmicon.restart();
263
      } catch (Exception rmie) {}
264
      if ( srbconn == -1003 ) {
265
        throw new 
266
        RemoteException("SrbJavaGlue.c: Connection to srbMaster failed.");
267
      }
268
      return srbconn;
269
    }
270

    
271
    return srbconn; 
272

    
273
  }
274

    
275
  /**
276
   * Close SRB RMI Connection 
277
   */
278
  private void srbDisconnect(int srbconn) throws RemoteException
279
  { 
280
    try {
281
      int err = srbJG.rmiFinishJ( srbconn );
282
    } catch (RemoteException e) {}
283

    
284
  }
285

    
286
  // get the names of groups for given user
287
  private String[] getGroupnames (int conn, String username) 
288
                                throws RemoteException    {
289
    String[] qval;
290
	  int[] qvalInx;
291
    int[] selVal;
292

    
293
    qval = new String [2];
294
    qval[0] = new String (" not like  '%$deleted%'");
295
    qval[1] = new String (" = '" + username + "'");
296
//    qval[2] = new String (" <> 'public'");
297

    
298
    qvalInx = new int[2];
299
    qvalInx[0] = USER_NAME;
300
    qvalInx[1] = USER_NAME;
301
//    qvalInx[2] = 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 all groups
312
  private String[] getGroupnames (int conn) 
313
                            throws RemoteException    {
314
    String[] qval;
315
	  int[] qvalInx;
316
    int[] selVal;
317

    
318
    qval = new String [1];
319
    qval[0] = new String (" not like  '%$deleted%'");
320
//    qval[1] = new String (" <> 'public'");
321

    
322
    qvalInx = new int[1];
323
    qvalInx[0] = USER_NAME;
324
//    qvalInx[1] = USER_GROUP_NAME;
325

    
326
    selVal = new int[1];
327
    selVal[0] = USER_GROUP_NAME;
328
	
329
    // generate a query and get the resultset
330
    String[] selRes = getGenQueResult (conn, qval, qvalInx, selVal);
331
    return selRes;
332
  }
333

    
334
  // get the names of users for given group
335
  private String[] getUsernames (int conn, String groupname) 
336
                              throws RemoteException    {
337
    String[] qval;
338
	  int[] qvalInx;
339
    int[] selVal;
340

    
341
    qval = new String [2];
342
    qval[0] = new String (" not like  '%$deleted%'");
343
    qval[1] = new String (" = '" + groupname + "'");
344
//    qval[2] = new String (" <> 'public'");
345

    
346
    qvalInx = new int[2];
347
    qvalInx[0] = USER_NAME;
348
    qvalInx[1] = USER_GROUP_NAME;
349
//    qvalInx[2] = 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
  // get the names of all users
360
  private String[] getUsernames (int conn) 
361
                            throws RemoteException    {
362
    String[] qval;
363
	  int[] qvalInx;
364
    int[] selVal;
365

    
366
    qval = new String [1];
367
    qval[0] = new String (" not like  '%$deleted%'");
368
//    qval[1] = new String (" <> 'public'");
369

    
370
    qvalInx = new int[1];
371
    qvalInx[0] = USER_NAME;
372
//    qvalInx[1] = USER_GROUP_NAME;
373

    
374
    selVal = new int[1];
375
    selVal[0] = USER_NAME;
376
	
377
    // generate a query and get the resultset
378
    String[] selRes = getGenQueResult (conn, qval, qvalInx, selVal);
379
    return selRes;
380
  }
381

    
382

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

    
408
      // get next rows
409
      while (queResult != null) {
410
        if ( queResultIndex >= queResultCount ) {
411
          queResultIndex = 0;
412
          queResultCount = srbJG.getMoreGenQueRowsJ(conn, 0);
413
          if (queResultCount <= 0) {
414
            return resultset;
415
          }  
416
        } 
417

    
418
        for (int i = 0; i < selLen; i++) {
419
          queResult[i] = srbJG.getGenQueResultJ(i, queResultIndex);
420
        }    
421
        resultset[inx++] = queResult[0];
422
        queResultIndex++;
423
      }  
424

    
425
    } catch (RemoteException re) {
426
      throw re;
427
    }
428
        
429
    return resultset;
430
  }
431

    
432
  /**
433
   * Test method for the class
434
   */
435
  public static void main(String[] args) {
436

    
437
    // Provide a user, such as: "cn=Matt Jones"
438
    String user = args[0];
439
    String password = args[1];
440

    
441
    AuthMcat authservice = new AuthMcat();
442

    
443
    boolean isValid = false;
444
    try {
445
  //    isValid = authservice.authenticate(user, password);
446
      if (isValid) {
447
        System.out.println("Authentication successful for: " + user );
448
        System.out.println(" ");
449
      } else {
450
        System.out.println("Authentication failed for: " + user);
451
      }
452

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

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