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     
00341     /*
00342      * The beginning
00343      */
00344     lcmaps_log_debug(1,"%s:\n", logstr);
00345 
00346     /*
00347      * Try to get the ordered values:
00348      */
00349     if ( ( dn = *(char **) lcmaps_getArgValue("user_dn", "char *", argc, argv) ) )
00350         lcmaps_log_debug(1,"%s: found dn: %s\n", logstr, dn);
00351     else
00352         lcmaps_log_debug(1,"%s: could not get value of dn !\n", logstr);
00353 
00354     /*
00355      * Get the VO user information.
00356      * We cannot order it by lcmaps_argument_t, but have to use the getCredentialData() function
00357      * since it was stored there by a plugin (lcmaps_voms.mod)
00358      */
00359     vo_cred_string_list = getCredentialData(LCMAPS_VO_CRED_STRING, &cnt_vo_cred_string);
00360     if (cnt_vo_cred_string == 0)
00361     {
00362         lcmaps_log(0,"%s: warning: no VOMS group info --> no mapping\n", logstr);
00363         goto success_voms_poolgroup;
00364     }
00365     else if (cnt_vo_cred_string < 0)
00366     {
00367         lcmaps_log(0,"%s: negative number of VOMS groups found ! (failure)\n", logstr);
00368         goto fail_voms_poolgroup;
00369     }
00370 
00371     /*
00372      * Check the groupmapfile
00373      */
00374 
00375     if ((groupmapfile != NULL) && (strlen(groupmapfile) > 0))
00376         lcmaps_log_debug(1,"%s: groupmapfile is: %s\n", logstr, groupmapfile);
00377     else
00378     {
00379         lcmaps_log(0,"%s: error finding the groupmapfile: %s\n", logstr, groupmapfile);
00380         lcmaps_log(0,"%s: (use the option \"-groupmapfile <groupmapfile>\")\n", logstr);
00381         goto fail_voms_poolgroup;
00382     }
00383 
00384     /*
00385      * Check groupmapdir
00386      */
00387     if (groupmapdir == NULL) /* try if GROUPMAPDIR is already set */
00388     {
00389         char * tmpptr=NULL;
00390         if ((tmpptr = getenv("GROUPMAPDIR")) == NULL)
00391         {
00392             lcmaps_log(0,"%s: GROUPMAPDIR unknown !\n", logstr);
00393             lcmaps_log(0,"%s:  specify as option or set GROUPMAPDIR\n", logstr);
00394             goto fail_voms_poolgroup;
00395         }
00396         else
00397         {
00398             groupmapdir = strdup(tmpptr);
00399         }
00400     }
00401     if (strlen(groupmapdir) == 0)
00402     {
00403         lcmaps_log(0,"%s: cannot set MAPDIR (strlen(groupmapdir) == 0)\n", logstr);
00404         goto fail_voms_poolgroup;
00405     }
00406     lcmaps_log_debug(1,"%s: setting MAPDIR to %s\n", logstr, groupmapdir);
00407     if (setenv("MAPDIR", groupmapdir, 1))
00408     {
00409         lcmaps_log(0,"%s: cannot set MAPDIR\n", logstr);
00410         goto fail_voms_poolgroup;
00411     }
00412 
00413     /*
00414      * Try to find the unix groups from the VO info in the groupmapfile
00415      * The first group (if found) should become the primary group
00416      */
00417 
00418     matching_type = MATCH_INCLUDE|MATCH_WILD_CHARS;
00419 
00420     /* if override_consistency is set add this to the matchin_type so it will take effect */
00421     if (override_inconsistency)
00422         matching_type = matching_type|OVERRIDE_INCONSISTANCY;
00423 
00424     for (i = 0; i < cnt_vo_cred_string; i++)
00425     {
00426         if ( (rc = lcmaps_gridlist(vo_cred_string_list[i], &groupname, groupmapfile, matching_type, ".", NULL)) == 0)
00427         {
00428             lcmaps_log_debug(1,"%s: found groupname: %s\n", logstr, groupname);
00429             group_counter++;
00430 
00431             if (groupname && (strlen(groupname) > 0))
00432             {
00433                 if ( ( group_info = getgrnam(groupname) ) )
00434                 {
00435                     if (i == 0)
00436                     {
00437                         /* First VO group */
00438                         addCredentialData(PRI_GID, (void *) &(group_info->gr_gid));
00439                     }
00440                     else
00441                     {
00442                         /* Other VO groups */
00443                         addCredentialData(SEC_GID, (void *) &(group_info->gr_gid));
00444                     }
00445                 }
00446                 else
00447                 {
00448                     lcmaps_log(0,"%s: no group id found for groupname = \"%s\"\n", logstr, groupname);
00449                     goto fail_voms_poolgroup;
00450                 }
00451             }
00452             else
00453             {
00454                 lcmaps_log(0,"%s: error getting value of groupname (failure)!\n", logstr);
00455                 goto fail_voms_poolgroup;
00456             }
00457         }
00458         else if (rc == LCMAPS_MOD_NOFILE)
00459         {
00460             lcmaps_log(0, "%s: Could not find the groupmapfile %s\n", logstr, groupmapfile);
00461             goto fail_voms_poolgroup;
00462         }
00463         else
00464         {
00465             lcmaps_log_debug(1,"%s: could not get value of groupname !\n", logstr);
00466             if (mapall)
00467             {
00468                 lcmaps_log(0,"%s: no mapping for VO group %s\n", logstr,
00469                            vo_cred_string_list[i]);
00470                 goto fail_voms_poolgroup;
00471             }
00472         }
00473     }
00474 
00475     if (group_counter < mapmin)
00476     {
00477         lcmaps_log(0,"%s: Not enough groups found. The minimum is set to %d. The plugin found %d\n", logstr, mapmin, group_counter);
00478         goto fail_voms_poolgroup;
00479     }
00480 
00481     /* success */
00482  success_voms_poolgroup:
00483     if (groupname) free(groupname);
00484     lcmaps_log_time(0,"%s: voms_poolgroup plugin succeeded\n", logstr);
00485     return LCMAPS_MOD_SUCCESS;
00486 
00487  fail_voms_poolgroup:
00488     if (groupname) free(groupname);
00489     lcmaps_log_time(0,"%s: voms_poolgroup plugin failed\n", logstr);
00490     return LCMAPS_MOD_FAIL;
00491 }
00492 
00493 /******************************************************************************
00494 Function:   plugin_terminate
00495 Description:
00496     Terminate plugin
00497 Parameters:
00498 
00499 Returns:
00500     LCMAPS_MOD_SUCCESS : succes
00501     LCMAPS_MOD_FAIL    : failure
00502 ******************************************************************************/
00503 int plugin_terminate()
00504 {
00505     char * logstr = "\tlcmaps_plugin_voms_poolgroup-plugin_terminate()";
00506 
00507     lcmaps_log_debug(1,"%s: terminating\n", logstr);
00508 
00509     if (groupmapfile) free(groupmapfile);
00510     if (groupmapdir) free(groupmapdir);
00511 
00512     return LCMAPS_MOD_SUCCESS;
00513 }
00514 
00515 /******************************************************************************
00516 CVS Information:
00517     $Source: /cvs/fabric_mgt/gridification/lcmaps/modules/voms/lcmaps_voms_poolgroup.c,v $
00518     $Date: 2003/08/15 13:06:32 $
00519     $Revision: 1.18 $
00520     $Author: martijn $
00521 ******************************************************************************/

Generated at Tue Sep 23 15:48:09 2003 for edg-lcmaps by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001