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

lcmaps_voms_localgroup.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 
00109 /*****************************************************************************
00110                             Include header files
00111 ******************************************************************************/
00112 #include <stdio.h>
00113 #include <stdlib.h>
00114 #include <string.h>
00115 #include <pwd.h>
00116 #include <ctype.h>
00117 
00118 #include "lcmaps_config.h"
00119 #include "lcmaps_modules.h"
00120 #include "lcmaps_arguments.h"
00121 #include "lcmaps_cred_data.h"
00122 #include "lcmaps_gridlist.h"
00123 
00124 /******************************************************************************
00125                                 Definitions
00126 ******************************************************************************/
00127 
00128 /******************************************************************************
00129                           Module specific prototypes
00130 ******************************************************************************/
00131 
00132 /******************************************************************************
00133                        Define module specific variables
00134 ******************************************************************************/
00135 
00136 static char *groupmapfile = NULL;
00137 static int   mapall       = 0;
00138 static int   mapmin       = 0;
00139 
00140 /******************************************************************************
00141 Function:   plugin_initialize
00142 Description:
00143     Initialize plugin
00144 Parameters:
00145     argc, argv
00146     argv[0]: the name of the plugin
00147 Returns:
00148     LCMAPS_MOD_SUCCESS : succes
00149     LCMAPS_MOD_FAIL    : failure
00150     LCMAPS_MOD_NOFILE  : db file not found (will halt LCMAPS initialization)
00151 ******************************************************************************/
00152 int plugin_initialize(
00153         int argc,
00154         char ** argv
00155 )
00156 {
00157     char * logstr = "\tlcmaps_plugin_voms_localgroup-plugin_initialize()";
00158     int i, j;
00159 
00160     lcmaps_log_debug(1,"%s: passed arguments:\n", logstr);
00161     for (i=0; i < argc; i++)
00162     {
00163        lcmaps_log_debug(2,"%s: arg %d is %s\n", logstr, i, argv[i]);
00164     }
00165 
00166     /*
00167      * the first will be the thing to edit/select (groupmap(file))
00168      */
00169 
00170     /*
00171      * Parse arguments, argv[0] = name of plugin, so start with i = 1
00172      */
00173     for (i = 1; i < argc; i++)
00174     {
00175         if ( ((strcmp(argv[i], "-groupmap") == 0) ||
00176               (strcmp(argv[i], "-GROUPMAP") == 0) ||
00177               (strcmp(argv[i], "-groupmapfile") == 0) ||
00178               (strcmp(argv[i], "-GROUPMAPFILE") == 0))
00179              && (i + 1 < argc))
00180         {
00181             if ((argv[i + 1] != NULL) && (strlen(argv[i + 1]) > 0))
00182             {
00183                  groupmapfile = strdup(argv[i + 1]);
00184             }
00185             i++;
00186         }
00187         else if (strcmp(argv[i], "-mapall") == 0)
00188         {
00189              mapall = 1;
00190         }
00191         else if ((strcmp(argv[i], "-mapmin") == 0)
00192                  && (i + 1 < argc))
00193         {
00194             if ((argv[i + 1] != NULL) && (strlen(argv[i + 1]) > 0))
00195             {
00196                  /* check parameter integrety */
00197                  for (j = 0; j < (strlen(argv[i + 1])); j++)
00198                  {
00199                      if (isdigit((argv[i + 1])[j]) == 0)
00200                      {
00201                          lcmaps_log(0,"%s: Error in initialization parameter: %s (%s is not a number)\n", logstr, argv[i], argv[i + 1]);
00202                          return LCMAPS_MOD_FAIL;
00203                      }
00204                  }
00205  
00206                  mapmin = atoi(argv[i + 1]);
00207             }
00208             i++;
00209         }
00210         else
00211         {
00212             lcmaps_log(0,"%s: Error in initialization parameter: %s (failure)\n", logstr,
00213                        argv[i]);
00214             return LCMAPS_MOD_FAIL;
00215         }
00216     }
00217 
00218     return LCMAPS_MOD_SUCCESS;
00219 } 
00220 
00221 /******************************************************************************
00222 Function:   plugin_introspect
00223 Description:
00224     return list of required arguments
00225 Parameters:
00226 
00227 Returns:
00228     LCMAPS_MOD_SUCCESS : succes
00229     LCMAPS_MOD_FAIL    : failure
00230 ******************************************************************************/
00231 int plugin_introspect(
00232         int * argc,
00233         lcmaps_argument_t ** argv
00234 )
00235 {
00236     char *                   logstr = "\tlcmaps_plugin_voms_localgroup-plugin_introspect()";
00237     static lcmaps_argument_t argList[] = {
00238         {"user_dn"      ,       "char *"        , 1,   NULL},
00239         {NULL           ,       NULL            , -1,   NULL}
00240     };
00241 
00242     lcmaps_log_debug(1,"%s: introspecting\n", logstr);
00243 
00244     *argv = argList;
00245     *argc = lcmaps_cntArgs(argList);
00246     lcmaps_log_debug(1,"%s: address first argument: 0x%x\n", logstr,argList);
00247 
00248     return LCMAPS_MOD_SUCCESS;
00249 }
00250 
00251 
00252 /******************************************************************************
00253 Function:   plugin_run
00254 Description:
00255     Gather credentials for LCMAPS
00256 Parameters:
00257     argc: number of arguments
00258     argv: list of arguments
00259 Returns:
00260     LCMAPS_MOD_SUCCESS: authorization succeeded
00261     LCMAPS_MOD_FAIL   : authorization failed
00262 ******************************************************************************/
00263 int plugin_run(
00264         int argc,
00265         lcmaps_argument_t * argv
00266 )
00267 {
00268     char *                logstr = "\tlcmaps_plugin_voms_localgroup-plugin_run()";
00269     char *                dn                  = NULL; 
00270     char *                groupname           = NULL;
00271     struct group *        group_info          = NULL;
00272     int                   i                   = 0;
00273     char **               vo_cred_string_list = NULL;
00274     int                   cnt_vo_cred_string  = 0;
00275     int                   group_counter       = 0;
00276     int                   rc                  = 0;
00277     lcmaps_vo_mapping_t * lcmaps_vo_mapping   = NULL;
00278     
00279     /*
00280      * The beginning
00281      */
00282     lcmaps_log_debug(1,"%s:\n", logstr);
00283 
00284     /*
00285      * Try to get the ordered values:
00286      */
00287     if ( (dn = *(char **) lcmaps_getArgValue("user_dn", "char *", argc, argv) ) )
00288         lcmaps_log_debug(1,"%s: found dn: %s\n", logstr,dn);
00289     else
00290         lcmaps_log_debug(1,"%s: could not get value of dn !\n", logstr);
00291 
00292     /*
00293      * Get the VO user information.
00294      * We cannot order it by lcmaps_argument_t, but have to use the getCredentialData() function
00295      * since it was stored there by a plugin (lcmaps_voms.mod)
00296      */
00297     vo_cred_string_list = getCredentialData(LCMAPS_VO_CRED_STRING, &cnt_vo_cred_string);
00298     if (cnt_vo_cred_string == 0)
00299     {
00300         lcmaps_log(0,"%s: warning: no VOMS group info --> no mapping\n", logstr);
00301         goto success_voms_localgroup;
00302     }
00303     else if (cnt_vo_cred_string < 0)
00304     {
00305         lcmaps_log(0,"%s: negative number of VOMS groups found ! (failure)\n", logstr);
00306         goto fail_voms_localgroup;
00307     }
00308 
00309     /*
00310      * Check the groupmapfile
00311      */
00312 
00313     if ((groupmapfile != NULL) && (strlen(groupmapfile) > 0))
00314         lcmaps_log_debug(1,"%s: groupmapfile is: %s\n", logstr, groupmapfile);
00315     else
00316     {
00317         lcmaps_log(0,"%s: error finding the groupmapfile: %s\n", logstr, groupmapfile);
00318         lcmaps_log(0,"%s: (use the option \"-groupmapfile <groupmapfile>\")\n", logstr);
00319         goto fail_voms_localgroup;
00320     }
00321 
00322     /*
00323      * Try to find the unix groups from the VO info in the groupmapfile
00324      * The first group (if found) should become the primary group
00325      */
00326     for (i = 0; i < cnt_vo_cred_string; i++)
00327     {
00328         if ( (rc = lcmaps_gridlist(vo_cred_string_list[i], &groupname, groupmapfile, MATCH_EXCLUDE|MATCH_WILD_CHARS, ".", NULL) ) == 0)
00329         {
00330             lcmaps_log_debug(1,"%s: found groupname: %s\n", logstr, groupname);
00331             group_counter++;   
00332 
00333             if (groupname && (strlen(groupname) > 0))
00334             {
00335                 if ( ( group_info = getgrnam(groupname) ) )
00336                 {
00337                     if (i == 0)
00338                     {
00339                         /* First VO group */
00340                         addCredentialData(PRI_GID, (void *) &(group_info->gr_gid));
00341                     }
00342                     else
00343                     {
00344                         /* Other VO groups */
00345                         addCredentialData(SEC_GID, (void *) &(group_info->gr_gid));
00346                     }
00347                     /*
00348                      * The coupling between VO information and the GID is maintained
00349                      * in the lcmaps_vo_mapping structure, which is added to the credential data
00350                      */
00351                     lcmaps_vo_mapping=lcmaps_createVoMapping(
00352                         vo_cred_string_list[i],
00353                         groupname,
00354                         group_info->gr_gid
00355                     );
00356                     if (! lcmaps_vo_mapping)
00357                     {
00358                         lcmaps_log(0,"%s: could not create VoMapping structure (failure)\n", logstr);
00359                         goto fail_voms_localgroup;
00360                     }
00361 //                        lcmaps_printVoMapping(2, lcmaps_vo_mapping);
00362                     /* Add credential */
00363                     addCredentialData(LCMAPS_VO_CRED_MAPPING, (void *) lcmaps_vo_mapping);
00364                     if ( lcmaps_deleteVoMapping(&lcmaps_vo_mapping) )
00365                     {
00366                         lcmaps_log(0,"%s: error while deleting VoMapping structure (failure)\n", logstr);
00367                         goto fail_voms_localgroup;
00368                     }
00369                 }
00370                 else
00371                 {
00372                     lcmaps_log(0,"%s: no group id found for groupname = \"%s\"\n", logstr, groupname);
00373                     goto fail_voms_localgroup;
00374                 }
00375             }
00376             else
00377             {
00378                 lcmaps_log(0,"%s: error getting value of groupname (failure)!\n", logstr);
00379                 goto fail_voms_localgroup;
00380             }
00381         }
00382         else if (rc == LCMAPS_MOD_NOFILE)
00383         {
00384             lcmaps_log(0, "%s: Could not find the groupmapfile %s\n", logstr, groupmapfile);
00385             goto fail_voms_localgroup;
00386         }
00387         else
00388         {
00389             lcmaps_log_debug(1,"%s: could not get value of groupname !\n", logstr);
00390             if (mapall)
00391             {
00392                 lcmaps_log(0,"%s: no mapping for VO group %s\n", logstr,
00393                            vo_cred_string_list[i]);
00394                 goto fail_voms_localgroup;
00395             }
00396         }
00397     }
00398 
00399     if (group_counter < mapmin)
00400     {
00401         lcmaps_log(0,"%s: Not enough groups found. The minimum is set to %d. The plugin found %d\n", logstr, mapmin, group_counter);
00402         goto fail_voms_localgroup;
00403     }
00404 
00405     /* success */
00406  success_voms_localgroup:
00407     if (groupname) free(groupname);
00408     lcmaps_log_time(0,"%s: voms_localgroup plugin succeeded\n", logstr);
00409     return LCMAPS_MOD_SUCCESS;
00410 
00411  fail_voms_localgroup:
00412     if (groupname) free(groupname);
00413     lcmaps_log_time(0,"%s: voms_localgroup plugin failed\n", logstr);
00414     return LCMAPS_MOD_FAIL;
00415 }
00416 
00417 /******************************************************************************
00418 Function:   plugin_terminate
00419 Description:
00420     Terminate plugin
00421 Parameters:
00422 
00423 Returns:
00424     LCMAPS_MOD_SUCCESS : succes
00425     LCMAPS_MOD_FAIL    : failure
00426 ******************************************************************************/
00427 int plugin_terminate()
00428 {
00429     char *           logstr = "\tlcmaps_plugin_voms_localgroup-plugin_terminate()";
00430 
00431     lcmaps_log_debug(1,"%s: terminating\n", logstr);
00432 
00433     if (groupmapfile) free(groupmapfile);
00434 
00435     return LCMAPS_MOD_SUCCESS;
00436 }
00437 
00438 /******************************************************************************
00439 CVS Information:
00440     $Source: /cvs/fabric_mgt/gridification/lcmaps/modules/voms/lcmaps_voms_localgroup.c,v $
00441     $Date: 2004/01/05 16:45:04 $
00442     $Revision: 1.14 $
00443     $Author: martijn $
00444 ******************************************************************************/

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