Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages  

lcmaps_voms_poolgroup.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001 EU DataGrid.
00003  * For license conditions see http://www.eu-datagrid.org/license.html
00004  *
00005  * Copyright (c) 2001, 2002 by
00006  *     Martijn Steenbakkers <martijn@nikhef.nl>,
00007  *     David Groep <davidg@nikhef.nl>,
00008  *     NIKHEF Amsterdam, the Netherlands
00009  */
00010 
00153 /*****************************************************************************
00154                             Include header files
00155 ******************************************************************************/
00156 #include <stdio.h>
00157 #include <stdlib.h>
00158 #include <string.h>
00159 #include <pwd.h>
00160 #include <ctype.h>
00161 
00162 #include "lcmaps_config.h"
00163 #include "lcmaps_modules.h"
00164 #include "lcmaps_arguments.h"
00165 #include "lcmaps_cred_data.h"
00166 #include "lcmaps_gridlist.h"
00167 
00168 /******************************************************************************
00169                                 Definitions
00170 ******************************************************************************/
00171 
00172 /******************************************************************************
00173                           Module specific prototypes
00174 ******************************************************************************/
00175 
00176 /******************************************************************************
00177                        Define module specific variables
00178 ******************************************************************************/
00179 
00180 static char *groupmapfile = NULL;
00181 static char *groupmapdir  = NULL;
00182 static int   mapall       = 0;
00183 static int   override_inconsistency = 0; 
00184 static int   mapmin       = 0;
00185 /******************************************************************************
00186 Function:   plugin_initialize
00187 Description:
00188     Initialize plugin
00189 Parameters:
00190     argc, argv
00191     argv[0]: the name of the plugin
00192 Returns:
00193     LCMAPS_MOD_SUCCESS : succes
00194     LCMAPS_MOD_FAIL    : failure
00195     LCMAPS_MOD_NOFILE  : db file not found (will halt LCMAPS initialization)
00196 ******************************************************************************/
00197 int plugin_initialize(
00198         int argc,
00199         char ** argv
00200 )
00201 {
00202     char *  logstr = "\tlcmaps_plugin_voms_poolgroup-plugin_initialize()";
00203     int i, j;
00204 
00205     lcmaps_log_debug(1,"%s: passed arguments:\n", logstr);
00206     for (i=0; i < argc; i++)
00207     {
00208        lcmaps_log_debug(2,"%s: arg %d is %s\n", logstr, i, argv[i]);
00209     }
00210 
00211     /*
00212      * the first will be the thing to edit/select (groupmap(file))
00213      */
00214 
00215     /*
00216      * Parse arguments, argv[0] = name of plugin, so start with i = 1
00217      */
00218     for (i = 1; i < argc; i++)
00219     {
00220         if ( ((strcmp(argv[i], "-groupmap") == 0) ||
00221               (strcmp(argv[i], "-GROUPMAP") == 0) ||
00222               (strcmp(argv[i], "-groupmapfile") == 0) ||
00223               (strcmp(argv[i], "-GROUPMAPFILE") == 0))
00224              && (i + 1 < argc))
00225         {
00226             if ((argv[i + 1] != NULL) && (strlen(argv[i + 1]) > 0))
00227             {
00228                  groupmapfile = strdup(argv[i + 1]);
00229             }
00230             i++;
00231         }
00232         else if ( ((strcmp(argv[i], "-groupmapdir") == 0) ||
00233               (strcmp(argv[i], "-GROUPMAPDIR") == 0))
00234              && (i + 1 < argc))
00235         {
00236             if ((argv[i + 1] != NULL) && (strlen(argv[i + 1]) > 0))
00237             {
00238                  groupmapdir = strdup(argv[i + 1]);
00239             }
00240             i++;
00241         }
00242         else if (strcmp(argv[i], "-mapall") == 0)
00243         {
00244              mapall = 1;
00245         }
00246         else if ( (strcmp(argv[i], "-override_inconsistency") == 0) ||
00247                   (strcmp(argv[i], "-OVERRIDE_INCONSISTENCY") == 0))
00248         {
00249             override_inconsistency = 1;
00250         }
00251 
00252         else if ((strcmp(argv[i], "-mapmin") == 0) 
00253                  && (i + 1 < argc))
00254         {
00255             if ((argv[i + 1] != NULL) && (strlen(argv[i + 1]) > 0))
00256             {
00257                  /* check parameter integrety */
00258                  for (j = 0; j < (strlen(argv[i + 1])); j++)
00259                  {
00260                      if (isdigit((argv[i + 1])[j]) == 0)
00261                      {
00262                          lcmaps_log(0,"%s: Error in initialization parameter: %s (%s is not a number)\n", logstr, argv[i], argv[i + 1]);
00263                          return LCMAPS_MOD_FAIL;
00264                      }
00265                  }
00266 
00267                  mapmin = atoi(argv[i + 1]);
00268             }
00269             i++;
00270         }
00271 
00272         else
00273         {
00274             lcmaps_log(0,"%s: Error in initialization parameter: %s (failure)\n", logstr,
00275                        argv[i]);
00276             return LCMAPS_MOD_FAIL;
00277         }
00278     }
00279 
00280     return LCMAPS_MOD_SUCCESS;
00281 } 
00282 
00283 /******************************************************************************
00284 Function:   plugin_introspect
00285 Description:
00286     return list of required arguments
00287 Parameters:
00288 
00289 Returns:
00290     LCMAPS_MOD_SUCCESS : succes
00291     LCMAPS_MOD_FAIL    : failure
00292 ******************************************************************************/
00293 int plugin_introspect(
00294         int * argc,
00295         lcmaps_argument_t ** argv
00296 )
00297 {
00298     char *                   logstr = "\tlcmaps_plugin_voms_poolgroup-plugin_introspect()";
00299     static lcmaps_argument_t argList[] = {
00300         {"user_dn"      ,       "char *"        , 1,   NULL},
00301         {NULL           ,       NULL            , -1,   NULL}
00302     };
00303 
00304     lcmaps_log_debug(1,"%s: introspecting\n", logstr);
00305 
00306     *argv = argList;
00307     *argc = lcmaps_cntArgs(argList);
00308     lcmaps_log_debug(1,"%s: address first argument: 0x%x\n", logstr, argList);
00309 
00310     return LCMAPS_MOD_SUCCESS;
00311 }
00312 
00313 
00314 /******************************************************************************
00315 Function:   plugin_run
00316 Description:
00317     Gather credentials for LCMAPS
00318 Parameters:
00319     argc: number of arguments
00320     argv: list of arguments
00321 Returns:
00322     LCMAPS_MOD_SUCCESS: authorization succeeded
00323     LCMAPS_MOD_FAIL   : authorization failed
00324 ******************************************************************************/
00325 int plugin_run(
00326         int argc,
00327         lcmaps_argument_t * argv
00328 )
00329 {
00330     char *                logstr = "\tlcmaps_plugin_voms_poolgroup-plugin_run()";
00331     char *                dn                  = NULL; 
00332     char *                groupname           = NULL;
00333     struct group *        group_info          = NULL;
00334     int                   i                   = 0;
00335     char **               vo_cred_string_list = NULL;
00336     int                   cnt_vo_cred_string  = 0;
00337     unsigned short        matching_type       = ((unsigned short)0x0000);
00338     int                   group_counter       = 0;
00339     int                   rc                  = 0;
00340     lcmaps_vo_mapping_t * lcmaps_vo_mapping   = NULL;
00341     
00342     /*
00343      * The beginning
00344      */
00345     lcmaps_log_debug(1,"%s:\n", logstr);
00346 
00347     /*
00348      * Try to get the ordered values:
00349      */
00350     if ( ( dn = *(char **) lcmaps_getArgValue("user_dn", "char *", argc, argv) ) )
00351         lcmaps_log_debug(1,"%s: found dn: %s\n", logstr, dn);
00352     else
00353         lcmaps_log_debug(1,"%s: could not get value of dn !\n", logstr);
00354 
00355     /*
00356      * Get the VO user information.
00357      * We cannot order it by lcmaps_argument_t, but have to use the getCredentialData() function
00358      * since it was stored there by a plugin (lcmaps_voms.mod)
00359      */
00360     vo_cred_string_list = getCredentialData(LCMAPS_VO_CRED_STRING, &cnt_vo_cred_string);
00361     if (cnt_vo_cred_string == 0)
00362     {
00363         lcmaps_log(0,"%s: warning: no VOMS group info --> no mapping\n", logstr);
00364         goto success_voms_poolgroup;
00365     }
00366     else if (cnt_vo_cred_string < 0)
00367     {
00368         lcmaps_log(0,"%s: negative number of VOMS groups found ! (failure)\n", logstr);
00369         goto fail_voms_poolgroup;
00370     }
00371 
00372     /*
00373      * Check the groupmapfile
00374      */
00375 
00376     if ((groupmapfile != NULL) && (strlen(groupmapfile) > 0))
00377         lcmaps_log_debug(1,"%s: groupmapfile is: %s\n", logstr, groupmapfile);
00378     else
00379     {
00380         lcmaps_log(0,"%s: error finding the groupmapfile: %s\n", logstr, groupmapfile);
00381         lcmaps_log(0,"%s: (use the option \"-groupmapfile <groupmapfile>\")\n", logstr);
00382         goto fail_voms_poolgroup;
00383     }
00384 
00385     /*
00386      * Check groupmapdir
00387      */
00388     if (groupmapdir == NULL) /* try if GROUPMAPDIR is already set */
00389     {
00390         char * tmpptr=NULL;
00391         if ((tmpptr = getenv("GROUPMAPDIR")) == NULL)
00392         {
00393             lcmaps_log(0,"%s: GROUPMAPDIR unknown !\n", logstr);
00394             lcmaps_log(0,"%s:  specify as option or set GROUPMAPDIR\n", logstr);
00395             goto fail_voms_poolgroup;
00396         }
00397         else
00398         {
00399             groupmapdir = strdup(tmpptr);
00400         }
00401     }
00402     if (strlen(groupmapdir) == 0)
00403     {
00404         lcmaps_log(0,"%s: cannot set MAPDIR (strlen(groupmapdir) == 0)\n", logstr);
00405         goto fail_voms_poolgroup;
00406     }
00407     lcmaps_log_debug(1,"%s: setting MAPDIR to %s\n", logstr, groupmapdir);
00408     if (setenv("MAPDIR", groupmapdir, 1))
00409     {
00410         lcmaps_log(0,"%s: cannot set MAPDIR\n", logstr);
00411         goto fail_voms_poolgroup;
00412     }
00413 
00414     /*
00415      * Try to find the unix groups from the VO info in the groupmapfile
00416      * The first group (if found) should become the primary group
00417      */
00418 
00419     matching_type = MATCH_INCLUDE|MATCH_WILD_CHARS;
00420 
00421     /* if override_consistency is set add this to the matchin_type so it will take effect */
00422     if (override_inconsistency)
00423         matching_type = matching_type|OVERRIDE_INCONSISTANCY;
00424 
00425     for (i = 0; i < cnt_vo_cred_string; i++)
00426     {
00427         if ( (rc = lcmaps_gridlist(vo_cred_string_list[i], &groupname, groupmapfile, matching_type, ".", NULL)) == 0)
00428         {
00429             lcmaps_log_debug(1,"%s: found groupname: %s\n", logstr, groupname);
00430             group_counter++;
00431 
00432             if (groupname && (strlen(groupname) > 0))
00433             {
00434                 if ( ( group_info = getgrnam(groupname) ) )
00435                 {
00436                     if (i == 0)
00437                     {
00438                         /* First VO group */
00439                         addCredentialData(PRI_GID, (void *) &(group_info->gr_gid));
00440                     }
00441                     else
00442                     {
00443                         /* Other VO groups */
00444                         addCredentialData(SEC_GID, (void *) &(group_info->gr_gid));
00445                     }
00446                     /*
00447                      * The coupling between VO information and the GID is maintained
00448                      * in the lcmaps_vo_mapping structure, which is added to the credential data
00449                      */
00450                     lcmaps_vo_mapping=lcmaps_createVoMapping(
00451                         vo_cred_string_list[i],
00452                         groupname,
00453                         group_info->gr_gid
00454                     );
00455                     if (! lcmaps_vo_mapping)
00456                     {
00457                         lcmaps_log(0,"%s: could not create VoMapping structure (failure)\n", logstr);
00458                         goto fail_voms_poolgroup;
00459                     }
00460 //                        lcmaps_printVoMapping(2, lcmaps_vo_mapping);
00461                     /* Add credential */
00462                     addCredentialData(LCMAPS_VO_CRED_MAPPING, (void *) lcmaps_vo_mapping);
00463                     if ( lcmaps_deleteVoMapping(&lcmaps_vo_mapping) )
00464                     {
00465                         lcmaps_log(0,"%s: error while deleting VoMapping structure (failure)\n", logstr);
00466                         goto fail_voms_poolgroup;
00467                     }
00468                 }
00469                 else
00470                 {
00471                     lcmaps_log(0,"%s: no group id found for groupname = \"%s\"\n", logstr, groupname);
00472                     goto fail_voms_poolgroup;
00473                 }
00474             }
00475             else
00476             {
00477                 lcmaps_log(0,"%s: error getting value of groupname (failure)!\n", logstr);
00478                 goto fail_voms_poolgroup;
00479             }
00480         }
00481         else if (rc == LCMAPS_MOD_NOFILE)
00482         {
00483             lcmaps_log(0, "%s: Could not find the groupmapfile %s\n", logstr, groupmapfile);
00484             goto fail_voms_poolgroup;
00485         }
00486         else
00487         {
00488             lcmaps_log_debug(1,"%s: could not get value of groupname !\n", logstr);
00489             if (mapall)
00490             {
00491                 lcmaps_log(0,"%s: no mapping for VO group %s\n", logstr,
00492                            vo_cred_string_list[i]);
00493                 goto fail_voms_poolgroup;
00494             }
00495         }
00496     }
00497 
00498     if (group_counter < mapmin)
00499     {
00500         lcmaps_log(0,"%s: Not enough groups found. The minimum is set to %d. The plugin found %d\n", logstr, mapmin, group_counter);
00501         goto fail_voms_poolgroup;
00502     }
00503 
00504     /* success */
00505  success_voms_poolgroup:
00506     if (groupname) free(groupname);
00507     lcmaps_log_time(0,"%s: voms_poolgroup plugin succeeded\n", logstr);
00508     return LCMAPS_MOD_SUCCESS;
00509 
00510  fail_voms_poolgroup:
00511     if (groupname) free(groupname);
00512     lcmaps_log_time(0,"%s: voms_poolgroup plugin failed\n", logstr);
00513     return LCMAPS_MOD_FAIL;
00514 }
00515 
00516 /******************************************************************************
00517 Function:   plugin_terminate
00518 Description:
00519     Terminate plugin
00520 Parameters:
00521 
00522 Returns:
00523     LCMAPS_MOD_SUCCESS : succes
00524     LCMAPS_MOD_FAIL    : failure
00525 ******************************************************************************/
00526 int plugin_terminate()
00527 {
00528     char * logstr = "\tlcmaps_plugin_voms_poolgroup-plugin_terminate()";
00529 
00530     lcmaps_log_debug(1,"%s: terminating\n", logstr);
00531 
00532     if (groupmapfile) free(groupmapfile);
00533     if (groupmapdir) free(groupmapdir);
00534 
00535     return LCMAPS_MOD_SUCCESS;
00536 }
00537 
00538 /******************************************************************************
00539 CVS Information:
00540     $Source: /cvs/fabric_mgt/gridification/lcmaps/modules/voms/lcmaps_voms_poolgroup.c,v $
00541     $Date: 2004/01/05 16:45:04 $
00542     $Revision: 1.19 $
00543     $Author: martijn $
00544 ******************************************************************************/

Generated at Thu Mar 4 17:39:03 2004 for edg-lcmaps by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001