Bdb packages | Design docs | Source docs | Guidelines | Recent releases

Search | Site Map .

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/BdbEvsLoaders/BdbAuthCmd.cc

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //         $Id: BdbAuthCmd.cc,v 1.13 2002/08/23 17:20:22 salnikov Exp $
00004 //
00005 // Description:
00006 //        Main program for stand-alone program to populate OODB
00007 //      with authorization records.
00008 //
00009 // Environment:
00010 //        Software developed for the BaBar Detector at the SLAC B-Factory.
00011 //
00012 // Author List:
00013 //        Igor A. Gaponenko                Original Author
00014 //
00015 //------------------------------------------------------------------------
00016 #include "BaBar/BaBar.hh"
00017 //-------------
00018 // C Headers --
00019 //-------------
00020 
00021 #include <stdio.h>
00022 #include <string.h>
00023 #include <assert.h>
00024 
00025 extern "C" {
00026 #include <ctype.h>
00027 }
00028 
00029 //---------------
00030 // C++ Headers --
00031 //---------------
00032 
00033 #include <iostream.h>
00034 #include <string>
00035 using std::string;
00036 
00037 #include "BbrStdUtils/Tokenize.hh"
00038 #include "BbrStdUtils/String.hh"
00039 using namespace babar::String; 
00040 
00041 //-------------------------------
00042 // Collaborating Class Headers --
00043 //-------------------------------
00044 
00045 #include "BdbUtil/Bdb.hh"
00046 
00047 #include "BdbAccess/BdbAuth.hh"
00048 #include "BdbAccess/BdbAuthItr.hh"
00049 #include "BdbApplication/BdbDomain.hh"
00050 
00051 #include "BdbAccess/BdbDbTreeSingleton.hh"
00052 #include "BdbTrees/BdbDbRegistrator.hh"
00053 
00054 #include "BdbEventStore/BdbEventStore.hh"
00055 
00056 //-----------------------------------------------------------------------
00057 // Local Macros, Typedefs, Structures, Unions and Forward Declarations --
00058 //-----------------------------------------------------------------------
00059 
00060 static const char rcsid[] = "$Id: BdbAuthCmd.cc,v 1.13 2002/08/23 17:20:22 salnikov Exp $";
00061 
00062 static const int MAX_USER_NAME   = 12;
00063 static const int MAX_GROUP_NAME  = 19;
00064 static const int MAX_NUM_OF_ARGS = 100;
00065 static const int MAX_BUF_LEN     = 255;
00066 
00067 static char* verbs[] = { "isauth",
00068                          "dinit",
00069                          "ddelete",
00070                          "ucreate",
00071                          "udelete",
00072                          "gcreate",
00073                          "gdelete",
00074                          "uinclude",
00075                          "uexclude",
00076                          "setsysmgr",
00077                          "setgrpmgr",
00078                          "lsusers",
00079                          "lsmembers",
00080                          "lsgroups",
00081                          "lsmembership",
00082                          "help"
00083 };
00084 
00085 enum VerbType { VERB_UNKNOWN = 9999,
00086                 VERB_MIN     = 0,
00087                 VERB_ISAUTH  = VERB_MIN,
00088                 VERB_DINIT,
00089                 VERB_DDELETE,
00090                 VERB_UCREATE,
00091                 VERB_UDELETE,
00092                 VERB_GCREATE,
00093                 VERB_GDELETE,
00094                 VERB_UINCLUDE,
00095                 VERB_UEXCLUDE,
00096                 VERB_SETSYSMGR,
00097                 VERB_SETGRPMGR,
00098                 VERB_LSUSERS,
00099                 VERB_LSMEMBERS,
00100                 VERB_LSGROUPS,
00101                 VERB_LSMEMBERSHIP,
00102                 VERB_HELP,
00103                 VERB_MAX     = VERB_HELP
00104 };
00105 
00106 static void print_title( );
00107 static void usage( const char* str = 0 );
00108 static void help( );
00109 
00110 static int translate_and_execute( BdbAuth* theAuth, int vargc, const char* vargv[] );
00111 
00112 static BdbDomain::Domains    get_domain   ( const char* );
00113 static BdbDomain::AuthLevels get_authLevel( const char* );
00114 static d_Boolean             get_boolean  ( const char* );
00115 
00116 static int cmd_check        ( BdbAuth*, const char*, const char*, const char* );
00117 static int cmd_init_domain  ( BdbAuth*, const char* );
00118 static int cmd_delete_domain( BdbAuth*, const char* );
00119 static int cmd_create_user  ( BdbAuth*, const char*, const char*, const char*, const char*, const char* );
00120 static int cmd_delete_user  ( BdbAuth*, const char*, const char* );
00121 static int cmd_create_group ( BdbAuth*, const char*, const char*, const char* );
00122 static int cmd_delete_group ( BdbAuth*, const char*, const char* );
00123 static int cmd_include_user ( BdbAuth*, const char*, const char*, const char* );
00124 static int cmd_exclude_user ( BdbAuth*, const char*, const char*, const char* );
00125 
00126 static int cmd_set_system_manager( BdbAuth*, const char*, const char*, const char* );
00127 static int cmd_set_group_manager ( BdbAuth*, const char*, const char*, const char*, const char* );
00128 
00129 static int cmd_list_users          ( BdbAuth*, const char* );
00130 static int cmd_list_group_members  ( BdbAuth*, const char*, const char* );
00131 static int cmd_list_groups         ( BdbAuth*, const char* );
00132 static int cmd_list_user_membership( BdbAuth*, const char*, const char* );
00133 
00134 //-----------------
00135 // Main Function --
00136 //-----------------
00137 
00138 int
00139 main( int argc, char* argv[] )
00140 {
00141     int result = 1;
00142     BdbAuth* theAuth;
00143 
00144   // Simplest check for the number of arguments.
00145 
00146     if( argc < 2 ) {
00147         usage( );
00148         return 1;
00149     }
00150 
00151     // Start a domain (any domain)
00152     BdbEventStore::instance()->activate() ;
00153 
00154   // Start transaction
00155 
00156     BdbDbRegistrator* registrator = new BdbDbRegistrator ;
00157     BdbDbTreeSingleton::instance()->setRegistrator(registrator) ;
00158 
00159     theAuth = BdbAuth::instance( );
00160     theAuth->startUpdate( );
00161 
00162   // Check if we are loading the commands from an input file
00163   // or just from the standard input.
00164   
00165         if( ! strcmp( "-f", argv[1] )) {
00166 
00167         if( argc != 3 ) {
00168             usage( );
00169         } else {
00170             
00171             FILE* infile;
00172 
00173             if(( infile = fopen( argv[2], "r" )) == 0 ) {
00174                 cout << "BdbAuthCmd: file open error for \"" << argv[2] << "\"" << endl;
00175             } else {
00176 
00177                 int line  = 1;
00178 
00179                 const char* vargv[MAX_NUM_OF_ARGS];
00180                 char        buf  [MAX_BUF_LEN];
00181                 
00182                 vargv[0] = "BdbAuthCmd";  // In order to be compartible with "standard" ARGC/ARGV.
00183                 
00184                 while( fgets( buf, MAX_BUF_LEN, infile ) != (char*) 0 ) {
00185                 
00186                     ++line;
00187 
00188                   // Replace the '\n' by '\0' at the end.
00189                   
00190                     if( strlen(buf)) {
00191                         buf[strlen(buf)-1] = '\0';
00192                     }
00193                     
00194                   // Extract tokens from the string.
00195                   
00196                     string str(buf);
00197                     Tokenize next(str);
00198                     string token[MAX_NUM_OF_ARGS]; 
00199                     
00200                     int vargc = 1;
00201 
00202                     while( 1 ) {
00203                         
00204                         token[vargc] = next( );
00205                         
00206                         if( token[vargc].empty( )) {
00207                             break;
00208                         }
00209                         vargv[vargc] = token[vargc].c_str( );
00210                         
00211                         ++vargc;
00212                         
00213                         if( vargc >= MAX_NUM_OF_ARGS ) {
00214                             cout << "BdbAuthCmd:" << line << ":ERROR: too many arguments." << endl;
00215                             break;
00216                         }
00217                     }
00218                     
00219                   // Ignore too complex lines.
00220                   
00221                     if( vargc >= MAX_NUM_OF_ARGS ) {
00222                         continue;
00223                     }
00224                     
00225                   // Ignore comment lines.
00226 
00227                     if(( vargc >= 2 )&&( '#' == *vargv[1] )) {
00228                         continue;
00229                     }
00230                     
00231                   // Skeep the empty (containing no tokens) lines.
00232 
00233                     if( vargc <= 1 ) {
00234                         continue;
00235                     }
00236 
00237                   // Translate and execute the command.
00238                   
00239                     result = translate_and_execute( theAuth, vargc, vargv );
00240                     if( result ) {
00241                         cout << "BdbAuthCmd:" << line << ":ERROR: " << buf << endl;
00242                     } else {
00243                         cout << "BdbAuthCmd:" << line << ":OK:    " << buf << endl;
00244                     }
00245                 }
00246             }
00247         }
00248     } else {
00249         result = translate_and_execute( theAuth, argc, (const char**) argv ) ;
00250     }
00251 
00252   // Complete the transaction
00253 
00254     theAuth->commit( );
00255 
00256     return result;
00257 }
00258 
00259 //-----------------------
00260 // Commands dispatcher --
00261 //-----------------------
00262 
00263 int
00264 translate_and_execute( BdbAuth* theAuth,
00265                        int vargc,
00266                        const char* vargv[] )
00267 {
00268     int result = 1;
00269     int verb = VERB_UNKNOWN;
00270         
00271   // Get the command verb.
00272 
00273     for( int i = VERB_MIN; i <= VERB_MAX; i++ ) {
00274       if( ! strcmp( vargv[1], verbs[i] )) {
00275           verb = i;
00276           break;
00277       }
00278     }
00279 
00280   // Get additional command parameters according
00281   // to the recognized verb.
00282   // Then execute the commands.
00283 
00284     switch( verb ) {
00285 
00286         case VERB_ISAUTH:
00287 
00288             if(( vargc < 4 )||( vargc > 5 )) {
00289                 usage( "isauth <domain> System" );
00290                 usage( "isauth <domain> Group  <group>" );
00291                 usage( "isauth <domain> User" );
00292             } else {
00293             
00294               // WARNING: We commit current "Update"-mode transaction for
00295               //          the time of execution of this method.
00296               //          Then we start read-mode transaction in order
00297               //          to prevent possible conflicts with "short nested
00298               //          transactions" inside BdbAuth::isAuthorized().
00299               //
00300               //            NOTE:   We start Read transaction to be able
00301               //                    BdbAuth:::isAuthorized() method running
00302               //                    without their own transaction management.
00303 
00304                 theAuth->commit( );
00305                 theAuth->startRead( );            
00306 
00307                 result = cmd_check( theAuth, vargv[2], vargv[3], vargv[4] );
00308                 
00309                 theAuth->commit( );
00310                 theAuth->startUpdate( );
00311             }
00312             break;
00313 
00314         case VERB_DINIT:
00315 
00316             if( vargc != 3 ) {
00317                 usage( "dinit <domain>" );
00318             } else {
00319                 result = cmd_init_domain( theAuth, vargv[2] );
00320             }
00321             break;
00322 
00323         case VERB_DDELETE:
00324 
00325             if( vargc != 3 ) {
00326                 usage( "ddelete <domain>" );
00327             } else {
00328                 result = cmd_delete_domain( theAuth, vargv[2] );
00329             }
00330             break;
00331 
00332         case VERB_UCREATE:
00333 
00334             if(( vargc < 6 )||( vargc > 7 )) {
00335                 usage( "ucreate <domain> System <user>         <description>" );
00336                 usage( "ucreate <domain> Group  <user> <group> <description>" );
00337                 usage( "ucreate <domain> User   <user>         <description>" );
00338             } else {
00339                 if( vargc == 6 )
00340                     result = cmd_create_user( theAuth, vargv[2], vargv[3], vargv[4], 0,        vargv[5] );
00341                 else
00342                     result = cmd_create_user( theAuth, vargv[2], vargv[3], vargv[4], vargv[5], vargv[6] );
00343             }
00344             break;
00345 
00346         case VERB_UDELETE:
00347 
00348             if( vargc != 4 ) {
00349                 usage( "udelete <domain> <user>" );
00350             } else {
00351                 result = cmd_delete_user( theAuth, vargv[2], vargv[3] );
00352             }
00353             break;
00354 
00355         case VERB_GCREATE:
00356 
00357             if( vargc != 5 ) {
00358                 usage( "gcreate <domain> <group> <description>" );
00359             } else {
00360                 result = cmd_create_group( theAuth, vargv[2], vargv[3], vargv[4] );
00361             }
00362             break;
00363 
00364         case VERB_GDELETE:
00365 
00366             if( vargc != 4 ) {
00367                 usage( "gdelete <domain> <group>" );
00368             } else {
00369                 result = cmd_delete_group( theAuth, vargv[2], vargv[3] );
00370             }
00371             break;
00372 
00373         case VERB_UINCLUDE:
00374 
00375             if( vargc != 5 ) {
00376                 usage( "uinclude <domain> <user> <group>" );
00377             } else {
00378                 result = cmd_include_user( theAuth, vargv[2], vargv[3], vargv[4] );
00379             }
00380             break;
00381 
00382         case VERB_UEXCLUDE:
00383 
00384             if( vargc != 5 ) {
00385                 usage( "uexclude <domain> <user> <group>" );
00386             } else {
00387                 result = cmd_exclude_user( theAuth, vargv[2], vargv[3], vargv[4] );
00388             }
00389             break;
00390 
00391         case VERB_SETSYSMGR:
00392 
00393             if( vargc != 5 ) {
00394                 usage( "setsysmgr <domain> <user> { Yes | No }" );
00395             } else {
00396                 result = cmd_set_system_manager( theAuth, vargv[2], vargv[3], vargv[4] );
00397             }
00398             break;
00399 
00400         case VERB_SETGRPMGR:
00401 
00402             if( vargc != 6 ) {
00403                 usage( "setgrpmgr <domain> <user> <group> { Yes | No >" );
00404             } else {
00405                 result = cmd_set_group_manager( theAuth, vargv[2], vargv[3], vargv[4], vargv[5] );
00406             }
00407             break;
00408 
00409         case VERB_LSUSERS:
00410 
00411             if( vargc != 3 ) {
00412                 usage( "lsusers <domain>" );
00413             } else {
00414                 result = cmd_list_users( theAuth, vargv[2] );
00415             }
00416             break;
00417 
00418         case VERB_LSMEMBERS:
00419 
00420             if( vargc != 4 ) {
00421                 usage( "lsmembers <domain> <group>" );
00422             } else {
00423                 result = cmd_list_group_members( theAuth, vargv[2], vargv[3] );
00424             }
00425             break;
00426 
00427         case VERB_LSGROUPS:
00428 
00429             if( vargc != 3 ) {
00430                 usage( "lsgroups <domain>" );
00431             } else {
00432                 result = cmd_list_groups( theAuth, vargv[2] );
00433             }
00434             break;
00435 
00436         case VERB_LSMEMBERSHIP:
00437 
00438             if( vargc != 4 ) {
00439                 usage( "lsmembership <domain> <user>" );
00440             } else {
00441                 result = cmd_list_user_membership( theAuth, vargv[2], vargv[3] );
00442             }
00443             break;
00444 
00445         case VERB_HELP:
00446 
00447             help( );
00448             break;
00449 
00450         default:
00451 
00452             usage( );
00453     }
00454 
00455         return result ;
00456 }
00457 
00458 
00459 //---------------------
00460 // Utility functions --
00461 //---------------------
00462 
00463 static void
00464 usage( const char* str )
00465 {
00466     cout << "Usage: BdbAuthCmd ";
00467     if( str ) {
00468         cout << str << '\n';
00469         cout << "    Valid domain names are: Events, Conditions, Configuration, Spatial, Temporal, Ambient"; 
00470     } else {
00471         cout << "{ -f <filename> | help | <command> [par1] [par2] ... }";
00472     }
00473     cout << endl;
00474 }
00475 
00476 static void
00477 print_title( )
00478 {
00479     cout << endl
00480          << "####################################################" << endl
00481          << "######## BaBar Database Authorization Utility ######" << endl
00482          << "####################################################" << endl
00483          << endl;
00484 }
00485 
00486 static void
00487 help( )
00488 {
00489     print_title( );
00490 
00491     cout << "SYNTAX:" << endl
00492          << endl
00493          << "  BdbAuthCmd { -f <filename> | help | <command> [par1] [par2] ... }" << endl
00494          << endl
00495          << "COMMANDS:" << endl
00496          << endl
00497          << "  isauth       - check if your process is authorized" << endl
00498          << "  dinit        - initialize DOMAIN" << endl
00499          << "  ddelete      - delete all the records inside DOMAIN" << endl
00500          << "  ucreate      - create new USER record" << endl
00501          << "  udelete      - delete existing USER record" << endl
00502          << "  gcreate      - create new GROUP record" << endl
00503          << "  gdelete      - delete existing GROUP record" << endl
00504          << "  uinclude     - include USER into GROUP" << endl
00505          << "  uexclude     - exclude USER from GROUP" << endl
00506          << "  setsysmgr    - assign/deassign USER as a system manager in DOMAIN" << endl
00507          << "  setgrpmgr    - assign/deassign USER as a GROUP  manager" << endl
00508          << "  lsusers      - list all registered USERs" << endl
00509          << "  lsmembers    - list USERs - the members of a GROUP" << endl
00510          << "  lsgroups     - list all registered GROUPs" << endl
00511          << "  lsmembership - list the GROUPs having a USER as a member" << endl
00512          << "  help         - print this page, if you don't know yet how to do it" << endl
00513          << endl
00514          << "EXECUTING A LIST OF COMMANDS:" << endl
00515          << endl
00516          << "  This feature is available with \"-f <filename>\" form of the input," << endl
00517          << "  where: <filename> is a text formatted file containing a list" << endl
00518          << "  of commands to be executed." << endl
00519          << endl
00520          << "  The syntax of these commands is very much the same as for a command line form" << endl
00521          << "  of the input, but with the following exceptions:" << endl
00522          << endl
00523          << "    1. The spaces are not allowed in a user or a group descriptions." << endl
00524          << "    2. The apostrophs are treated as a part of the description." << endl
00525          << "    3. The first met '#' symbol at the beginning of the line indicates" << endl
00526          << "       command line parser to treat corresponding line as a comment line." << endl
00527          << "    4. The empty lines (containing nothing but SPACEs, TABs) are ignored." << endl
00528          << endl
00529          << "  From the practical point of view the rules (1) and (2) mean that" << endl
00530          << "  the following sample descriptions:" << endl
00531          << endl
00532          << "    \"This is a new group member\"" << endl
00533          << "    \"VIP user of our Federation\"" << endl
00534          << endl
00535          << "  should be replaced with:" << endl
00536          << endl
00537          << "    This_is_a_new_group_member" << endl
00538          << "    VIP_user_of_our_Federation" << endl
00539          << endl
00540          << "STATUS RETURN:" << endl
00541          << endl
00542          << "  When the commands are executed one-by-one in a command line, the result" << endl
00543          << "  is returned to a shell, as 0 - when OK, or 1 - when a ERROR condition is met." << endl
00544          << endl
00545          << "  When the commands are executed from the file the result of each command's" << endl
00546          << "  execution is printed onto a standard output as a message, such as:" << endl
00547          << endl
00548          << "    \"BdbAuthCmd:<line number>:{OK|ERROR}: <command sequence>\"" << endl
00549          << endl;
00550 }
00551 
00552 static BdbDomain::Domains
00553 get_domain( const char* name )
00554 {
00555     std::string nn ( babar::String::toLower(name) ) ;
00556 
00557     if ( nn.size() == 1 ) {
00558       switch( nn[0] ) {
00559           case 'a':  return BdbDomain::Ambient;
00560           case 'c':  return BdbDomain::Conditions;
00561           case 'o':  return BdbDomain::Configuration;
00562           case 'e':  return BdbDomain::Events;
00563           case 's':  return BdbDomain::Spatial;
00564           case 't':  return BdbDomain::Temporal;
00565       }
00566     } else {
00567       if ( nn == "ambient"      ) {   return BdbDomain::Ambient;       }
00568       if ( nn == "conditions"   ) {   return BdbDomain::Conditions;    }
00569       if ( nn == "configuration") {   return BdbDomain::Configuration; }
00570       if ( nn == "events"       ) {   return BdbDomain::Events;        }
00571       if ( nn == "spatial"      ) {   return BdbDomain::Spatial;       }
00572       if ( nn == "temporal"     ) {   return BdbDomain::Temporal;      }
00573     }
00574     
00575     cout << "Invalid DOMAIN parameter: \"" << name << "\"" << endl;
00576 
00577     return BdbDomain::IllegalDomain;
00578 }
00579 
00580 static BdbDomain::AuthLevels
00581 get_authLevel( const char* name )
00582 {
00583     switch( *name ) {
00584 
00585         case 's':
00586         case 'S':
00587             return BdbDomain::System;
00588 
00589         case 'g':
00590         case 'G':
00591             return BdbDomain::Group;
00592 
00593         case 'u':
00594         case 'U':
00595             return BdbDomain::User;
00596     }
00597     cout << "Invalid AUTHORIZATION LEVEL parameter" << endl;
00598 
00599     return BdbDomain::IllegalAuth;
00600 }
00601 
00602 static d_Boolean
00603 get_boolean( const char* str )
00604 {
00605     switch( *str ) {
00606 
00607         case 'y':
00608         case 'Y':
00609             return true;
00610     }
00611     return false;
00612 }
00613 
00614 
00615 //------------
00616 // Commands --
00617 //------------
00618 
00619 static int
00620 cmd_check( BdbAuth*    theAuth,
00621            const char* domain,
00622            const char* level,
00623            const char* group )
00624 {
00625     BdbDomain::Domains    theDomain    = get_domain( domain );
00626     BdbDomain::AuthLevels theAuthLevel = get_authLevel( level );
00627 
00628     if( theDomain    == BdbDomain::IllegalDomain ) return 1;
00629     if( theAuthLevel == BdbDomain::IllegalAuth   ) return 1;
00630 
00631     const char* theAuthName = 0;
00632 
00633     switch( theAuthLevel ) {
00634         case BdbDomain::Group:
00635             theAuthName = group;
00636             break;
00637     }
00638 
00639     if( theAuth->isAuthorized( theDomain,
00640                                theAuthLevel,
00641                                theAuthName )) return 0;
00642 
00643     return 1;
00644 }
00645 
00646 static int
00647 cmd_init_domain( BdbAuth*    theAuth,
00648                  const char* domain )
00649 {
00650     BdbDomain::Domains theDomain = get_domain( domain );
00651     if( theDomain == BdbDomain::IllegalDomain ) return 1;
00652 
00653     if( theAuth->initialize( theDomain )) return 0;
00654 
00655     return 1;
00656 }
00657 
00658 static int
00659 cmd_delete_domain( BdbAuth*    theAuth,
00660                    const char* domain )
00661 {
00662     BdbDomain::Domains theDomain = get_domain( domain );
00663     if( theDomain == BdbDomain::IllegalDomain ) return 1;
00664 
00665     if( theAuth->deleteDomain( theDomain )) return 0;
00666 
00667     return 1;
00668 }
00669 
00670 static int
00671 cmd_create_user( BdbAuth*    theAuth,
00672                  const char* domain,
00673                  const char* level,
00674                  const char* user,
00675                  const char* group,
00676                  const char* description )
00677 {
00678     BdbDomain::Domains    theDomain    = get_domain( domain );
00679     BdbDomain::AuthLevels theAuthLevel = get_authLevel( level );
00680 
00681     if( theDomain    == BdbDomain::IllegalDomain ) return 1;
00682     if( theAuthLevel == BdbDomain::IllegalAuth   ) return 1;
00683 
00684     const char* theGroupName = 0;
00685 
00686     switch( theAuthLevel ) {
00687         case BdbDomain::Group:
00688             theGroupName = group;
00689             break;
00690     }
00691 
00692     if( theAuth->createUser( theDomain,
00693                              theAuthLevel,
00694                              user,
00695                              theGroupName,
00696                              description )) return 0;
00697 
00698    return 1;
00699 }
00700 
00701 static int
00702 cmd_delete_user( BdbAuth*    theAuth,
00703                  const char* domain,
00704                  const char* user )
00705 {
00706     BdbDomain::Domains theDomain = get_domain( domain );
00707     if( theDomain == BdbDomain::IllegalDomain ) return 1;
00708 
00709     if( theAuth->deleteUser( theDomain,
00710                              user )) return 0;
00711 
00712     return 1;
00713 }
00714 
00715 static int
00716 cmd_create_group( BdbAuth*    theAuth,
00717                   const char* domain,
00718                   const char* group,
00719                   const char* description )
00720 {
00721     BdbDomain::Domains theDomain = get_domain( domain );
00722     if( theDomain == BdbDomain::IllegalDomain ) return 1;
00723 
00724     if( theAuth->createGroup( theDomain,
00725                               group,
00726                               description )) return 0;
00727 
00728     return 1;
00729 }
00730 
00731 static int
00732 cmd_delete_group( BdbAuth*    theAuth,
00733                   const char* domain,
00734                   const char* group )
00735 {
00736     BdbDomain::Domains theDomain = get_domain( domain );
00737     if( theDomain == BdbDomain::IllegalDomain ) return 1;
00738 
00739     if( theAuth->deleteGroup( theDomain,
00740                               group )) return 0;
00741 
00742     return 1;
00743 }
00744 
00745 static int
00746 cmd_include_user( BdbAuth*    theAuth,
00747                   const char* domain,
00748                   const char* user,
00749                   const char* group )
00750 {
00751     BdbDomain::Domains theDomain = get_domain( domain );
00752     if( theDomain == BdbDomain::IllegalDomain ) return 1;
00753 
00754     if( theAuth->includeUser( theDomain,
00755                               user,
00756                               group )) return 0;
00757 
00758     return 1;
00759 }
00760 
00761 static int
00762 cmd_set_system_manager( BdbAuth* theAuth,
00763                         const char* domain,
00764                         const char* user,
00765                         const char* onoff )
00766 {
00767     BdbDomain::Domains theDomain = get_domain ( domain );
00768     if( theDomain == BdbDomain::IllegalDomain ) return 1;
00769 
00770     if( theAuth->setSystemManager( theDomain,
00771                                    user,
00772                                    get_boolean( onoff ))) return 0;
00773 
00774     return 1;
00775 }
00776 
00777 static int
00778 cmd_set_group_manager( BdbAuth*    theAuth,
00779                        const char* domain,
00780                        const char* user,
00781                        const char* group,
00782                        const char* onoff )
00783 {
00784     BdbDomain::Domains theDomain = get_domain ( domain );
00785     if( theDomain == BdbDomain::IllegalDomain ) return 1;
00786 
00787     if( theAuth->setGroupManager( theDomain,
00788                                   user,
00789                                   group,
00790                                   get_boolean( onoff ))) return 0;
00791 
00792     return 1;
00793 }
00794 
00795 static int
00796 cmd_exclude_user( BdbAuth*    theAuth,
00797                   const char* domain,
00798                   const char* user,
00799                   const char* group  )
00800 {
00801     BdbDomain::Domains theDomain = get_domain( domain );
00802     if( theDomain == BdbDomain::IllegalDomain ) return 1;
00803 
00804     if( theAuth->excludeUser( theDomain,
00805                               user,
00806                               group )) return 0;
00807 
00808     return 1;
00809 }
00810 
00811 static int
00812 cmd_list_users( BdbAuth*    theAuth,
00813                 const char* domain )
00814 {
00815     print_title( );
00816 
00817     BdbDomain::Domains theDomain = get_domain( domain );
00818     if( theDomain == BdbDomain::IllegalDomain ) return 1;
00819 
00820     cout << "Users in domain \"" << BdbDomain::domainName(theDomain) << "\" :" << endl;
00821     cout << " " << endl;
00822 
00823     int total = 0;
00824 
00825     BdbAuthItr* itr = theAuth->createUsersItr( theDomain );
00826     if ( ! itr ) {
00827       cerr << "Domain or group may not exist." << endl;
00828       return 2 ; 
00829     }
00830     {
00831         while( itr->next( )) {
00832 
00833             const char* theUserName = itr->current( );
00834             if( 0 == theUserName ) break;
00835 
00836             char buf[ 1 + MAX_USER_NAME ];
00837             strncpy( buf, theUserName, MAX_USER_NAME );
00838 
00839             buf[MAX_USER_NAME] = '\0';
00840 
00841             for( int bytes = strlen( theUserName );
00842                  bytes < MAX_USER_NAME;
00843                  bytes++ ) buf[bytes] = ' ';
00844 
00845             cout << "  " << buf << "  (";
00846             if( theAuth->isSystemManager( theDomain, theUserName ))
00847                 cout << "SYS";
00848             else
00849                 cout << "   ";
00850             cout << ")  " << theAuth->getUserCreated    ( theDomain, theUserName )
00851                  << "  "  << theAuth->getUserDescription( theDomain, theUserName )
00852                  << endl;
00853 
00854             total++;
00855         }
00856     }
00857     delete itr;
00858 
00859     cout << endl
00860          << "Total of " << total << " users were found." << endl
00861          << endl;
00862 
00863     return 0;
00864 }
00865 
00866 static int
00867 cmd_list_group_members( BdbAuth*    theAuth,
00868                         const char* domain,
00869                         const char* group )
00870 {
00871     print_title( );
00872 
00873     BdbDomain::Domains theDomain = get_domain( domain );
00874     if( theDomain == BdbDomain::IllegalDomain ) return 1;
00875 
00876     cout << "Users in domain \"" << BdbDomain::domainName(theDomain) << "\" and group \"" << group << "\" :" << endl;
00877     cout << " " << endl;
00878 
00879     int total = 0;
00880 
00881     BdbAuthItr* itr = theAuth->createUsersItr( theDomain, group );
00882     if ( ! itr ) {
00883       cerr << "Domain or group may not exist." << endl;
00884       return 2 ; 
00885     }
00886     {
00887         while( itr->next( )) {
00888 
00889             const char* theUserName = itr->current( );
00890             if( 0 == theUserName ) break;
00891 
00892             char buf[ 1 + MAX_USER_NAME ];
00893             strncpy( buf, theUserName, MAX_USER_NAME );
00894 
00895             buf[MAX_USER_NAME] = '\0';
00896             
00897             for( int bytes = strlen( theUserName );
00898                  bytes < MAX_USER_NAME;
00899                  bytes++ ) buf[bytes] = ' ';
00900 
00901             cout << "  " << buf << "  (";
00902             if( theAuth->isSystemManager( theDomain, theUserName ))
00903                 cout << "SYS";
00904             else
00905                 cout << "   ";
00906             cout << ",";
00907             if( theAuth->isGroupManager( theDomain, theUserName, group ))
00908                 cout << "GRP";
00909             else
00910                 cout << "   ";
00911             cout << ")  " << theAuth->getUserCreated    ( theDomain, theUserName )
00912                  << "  "  << theAuth->getUserDescription( theDomain, theUserName )
00913                  << endl;
00914 
00915             total++;
00916         }
00917     }
00918     delete itr;
00919 
00920     cout << " " << endl;
00921     cout << "Total of " << total << " users were found." << endl;
00922     cout << " " << endl;
00923 
00924     return 0;
00925 }
00926 
00927 static int
00928 print_groups( BdbAuthItr*        itr,
00929               BdbAuth*           theAuth,
00930               BdbDomain::Domains theDomain )
00931 {
00932     int num = 0;
00933 
00934     if(( 0 != itr ) && itr->isValid( )) {
00935 
00936         while( itr->next( )) {
00937 
00938             const char* theGroupName = itr->current( );
00939             if( 0 == theGroupName ) break;
00940 
00941             char buf[ 1 + MAX_GROUP_NAME ];
00942             strncpy( buf, theGroupName, MAX_GROUP_NAME );
00943 
00944             buf[MAX_GROUP_NAME] = '\0';
00945 
00946             for( int bytes = strlen( theGroupName );
00947                  bytes < MAX_GROUP_NAME;
00948                  bytes++ ) buf[bytes] = ' ';
00949 
00950             cout << "  " << buf
00951                  << "  " << theAuth->getGroupCreated    ( theDomain, theGroupName )
00952                  << "  " << theAuth->getGroupDescription( theDomain, theGroupName )
00953                  << endl;
00954 
00955             num++;
00956         }
00957     }
00958     return num;
00959 }
00960 
00961 static int
00962 cmd_list_groups( BdbAuth*    theAuth,
00963                  const char* domain )
00964 {
00965     print_title( );
00966 
00967     BdbDomain::Domains theDomain = get_domain( domain );
00968     if( theDomain == BdbDomain::IllegalDomain ) return 1;
00969 
00970     cout << "Groups in domain \"" << BdbDomain::domainName(theDomain) << "\" :" << endl;
00971     cout << " " << endl;
00972 
00973     int total = 0;
00974 
00975     BdbAuthItr* itr = theAuth->createGroupsItr( theDomain );
00976     if ( ! itr ) {
00977       cerr << "Domain or group may not exist." << endl;
00978       return 2 ; 
00979     }
00980     {
00981         total = print_groups( itr, theAuth, theDomain );
00982     }
00983     delete itr;
00984 
00985     cout << " " << endl;
00986     cout << "Total of " << total << " groups were found." << endl;
00987     cout << " " << endl;
00988 
00989     return 0;
00990 }
00991 
00992 static int
00993 cmd_list_user_membership( BdbAuth*    theAuth,
00994                           const char* domain,
00995                           const char* user )
00996 {
00997     print_title( );
00998 
00999     BdbDomain::Domains theDomain = get_domain( domain );
01000     if( theDomain == BdbDomain::IllegalDomain ) return 1;
01001 
01002     cout << "Groups in domain \"" << BdbDomain::domainName(theDomain) << "\" related to user \"" << user << "\" :" << endl;
01003     cout << " " << endl;
01004 
01005     int total = 0;
01006 
01007     BdbAuthItr* itr = theAuth->createGroupsItr( theDomain, user );
01008     if ( ! itr ) {
01009       cerr << "Domain or group may not exist." << endl;
01010       return 2 ; 
01011     }
01012     {
01013         total = print_groups( itr, theAuth, theDomain );
01014     }
01015     delete itr;
01016 
01017     cout << " " << endl;
01018     cout << "Total of " << total << " groups were found." << endl;
01019     cout << " " << endl;
01020 
01021     return 0;
01022 }
01023 
01024 /////////////////
01025 // End Of File //
01026 /////////////////

 


BaBar Public Site | SLAC | News | Links | Who's Who | Contact Us

Page Owner: Jacek Becla
Last Update: October 04, 2002