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: 2001-04-17 09:32:07 -0700 (Tue, 17 Apr 2001) $'
12
 * '$Revision: 730 $'
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("error in 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("Error in AuthMcat.authenticate: " + 
104
                                   re.getMessage());
105
      }
106
      
107
      return false;
108
  }
109

    
110
  /**
111
   * Get all users from the authentication service
112
   */
113
  public String[] getUsers(String user, String password)
114
                            throws ConnectException
115
  {
116
                              
117
      try { 
118
        // make connection to SRB.
119
        int srbconn = srbConnect(user, password);
120
        if ( srbconn > 0 ) {
121
          // get all users
122
          String[] userlist = getUsernames(srbconn);
123
          // disconnect from SRB
124
          srbDisconnect(srbconn);
125
          return userlist;
126
        }
127

    
128
      } catch ( RemoteException re) {
129
        throw new ConnectException("Error in AuthMcat.getUsers " + 
130
                                   re.getMessage());
131
      }
132
      
133
      return null;
134
  }
135

    
136
  /**
137
   * Get the users for a particular group from the authentication service
138
   */
139
  public String[] getUsers(String user, String password, String group)
140
                           throws ConnectException
141
  {
142
      try { 
143
        // make connection to SRB.
144
        int srbconn = srbConnect(user, password);
145
        if ( srbconn > 0 ) {
146
          // get users for @group
147
          String[] userlist = getUsernames(srbconn, group);
148
          // disconnect from SRB
149
          srbDisconnect(srbconn);
150
          return userlist;
151
        }
152

    
153
      } catch ( RemoteException re) {
154
        throw new ConnectException("Error in AuthMcat.getUsers: " + 
155
                                    re.getMessage());
156
      }
157
      
158
      return null;
159
  }
160

    
161
  /**
162
   * Get all groups from the authentication service
163
   */
164
  public String[] getGroups(String user, String password)
165
                            throws ConnectException
166
  {
167
      try { 
168
        // make connection to SRB.
169
        int srbconn = srbConnect(user, password);
170
        if ( srbconn > 0 ) {
171
          // get all group
172
          String[] userlist = getGroupnames(srbconn);
173
          // disconnect from SRB
174
          srbDisconnect(srbconn);
175
          return userlist;
176
        }
177
 
178
      } catch ( RemoteException re) {
179
        throw new ConnectException("Error in authMcat.getGroups: " + 
180
                                   re.getMessage());
181
      }
182
      
183
      return null;
184
  }
185

    
186
  /**
187
   * Get the groups for a particular user from the authentication service
188
   */
189
  public String[] getGroups(String user, String password, String foruser)
190
                            throws ConnectException
191
  {
192
      try { 
193
        // make connection to SRB.
194
        int srbconn = srbConnect(user, password);
195
        if ( srbconn > 0 ) {
196
          // get groups for @foruser
197
          String[] userlist = getGroupnames(srbconn, foruser);
198
          // disconnect from SRB
199
          srbDisconnect(srbconn);
200
          return userlist;
201
        }
202

    
203
      } catch ( RemoteException re) {
204
        throw new ConnectException("Error in AuthMcat.getGroupds: " + 
205
                                    re.getMessage());
206
      }
207
      
208
      return null;
209
  }
210

    
211
  /**
212
   * Get attributes describing a user or group
213
   *
214
   * @param user the user for which the attribute list is requested
215
   * @returns HashMap a map of attribute name to a Vector of values
216
   */
217
  public HashMap getAttributes(String foruser) 
218
         throws ConnectException
219
  {
220
    return getAttributes(null, null, foruser);
221
  }
222

    
223
  /**
224
   * Get attributes describing a user or group
225
   *
226
   * @param user the user for which the attribute list is requested
227
   * @param authuser the user for authenticating against the service
228
   * @param password the password for authenticating against the service
229
   * @returns HashMap a map of attribute name to a Vector of values
230
   */
231
  public HashMap getAttributes(String user, String password, String foruser) 
232
         throws ConnectException
233
  {
234
    // NOT IMPLEMENTED YET
235
    return null;
236
  }
237
  
238
  /**
239
   * Get SRB RMI Connection 
240
   */
241
  private int srbConnect(String username, String password)
242
                                 throws RemoteException  { 
243

    
244
    synchronized(this) {
245
      int srbconn = 0;
246

    
247
      // try SRB RMI Connection
248
      // integer value of the SBR Connection ID is returned only
249
      try {
250
        String name = "//" + srbHost + "/SrbJavaGlue";
251
        srbJG = (SrbJavaGlueInterface)Naming.lookup(name);
252
        srbconn=srbJG.clConnectJ(srbHost,srbPort,password,username,srbDomain);
253
      } catch (Exception e) {
254
        throw new
255
        RemoteException("AuthMcat.srbConnect():" + e.getMessage());
256
      }
257

    
258
      // check if successfull
259
      if ( srbconn == 0 ) {
260
        throw new 
261
        RemoteException("The SRB Server is not running or it is busy now");
262
      } else if ( srbconn < 0 ) {
263
        return srbconn;
264
      }
265

    
266
      return srbconn; 
267

    
268
    } // end of synchronized(this)
269
  }
270

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

    
280
  }
281

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
378

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

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

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

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

    
428
  /**
429
   * Get all groups and users from authentication scheme.
430
   * The output is formatted in XML.
431
   * @param user the user which requests the information
432
   * @param password the user's password
433
   */
434
  public String getPrincipals(String user, String password)
435
                throws ConnectException
436
  {
437
    // NOT IMPLEMENTED YET
438
    return null;
439
  }
440

    
441
  /**
442
   * Test method for the class
443
   */
444
  public static void main(String[] args) {
445

    
446
    // Provide a user, such as: "cn=Matt Jones"
447
    String user = args[0];
448
    String password = args[1];
449

    
450
    AuthMcat authservice = new AuthMcat();
451

    
452
    boolean isValid = false;
453
    try {
454
      isValid = authservice.authenticate(user, password);
455
      if (isValid) {
456
        System.out.println("Authentication successful for: " + user );
457
        System.out.println(" ");
458
      } else {
459
        System.out.println("Authentication failed for: " + user);
460
      }
461

    
462
      String[] userlist = authservice.getUsers(user, password);
463
      for (int i = 0; i < userlist.length; i++) {
464
        System.out.println(userlist[i]);
465
      }
466
      System.out.println("All Users: " + userlist.length);
467
    
468
/*
469
      if (isValid) {
470
        HashMap userInfo = authservice.getAttributes(user);
471

    
472
        // Print all of the attributes
473
        Iterator attList = (Iterator)(((Set)userInfo.keySet()).iterator());
474
        while (attList.hasNext()) {
475
          String att = (String)attList.next();
476
          Vector values = (Vector)userInfo.get(att);
477
          System.out.println(att + ": " );
478
          Iterator attvalues = values.iterator();
479
          while (attvalues.hasNext()) {
480
            String value = (String)attvalues.next();
481
            System.out.println("  value: " + value);
482
          }
483
        }
484
      }
485
*/
486
    } catch (ConnectException ce) {
487
      System.err.println(ce.getMessage());
488
    }
489
  }
490
}
(7-7/43)