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

lcmaps_voms_localaccount.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 
00097 /*****************************************************************************
00098                             Include header files
00099 ******************************************************************************/
00100 #include <stdio.h>
00101 #include <stdlib.h>
00102 #include <string.h>
00103 #include <pwd.h>
00104 
00105 #include "lcmaps_config.h"
00106 #include "lcmaps_modules.h"
00107 #include "lcmaps_arguments.h"
00108 #include "lcmaps_cred_data.h"
00109 #include "lcmaps_gridlist.h"
00110 
00111 /******************************************************************************
00112                                 Definitions
00113 ******************************************************************************/
00114 #define LCMAPS_MAXGIDBUFFER 256
00115 
00116 /******************************************************************************
00117                           Module specific prototypes
00118 ******************************************************************************/
00119 
00120 /******************************************************************************
00121                        Define module specific variables
00122 ******************************************************************************/
00123 
00124 static char *gridmapfile         = NULL;
00125 static int   use_voms_gid        = 0;
00126 
00127 /******************************************************************************
00128 Function:   plugin_initialize
00129 Description:
00130     Initialize plugin
00131 Parameters:
00132     argc, argv
00133     argv[0]: the name of the plugin
00134 Returns:
00135     LCMAPS_MOD_SUCCESS : succes
00136     LCMAPS_MOD_FAIL    : failure
00137     LCMAPS_MOD_NOFILE  : db file not found (will halt LCMAPS initialization)
00138 ******************************************************************************/
00139 int plugin_initialize(
00140         int argc,
00141         char ** argv
00142 )
00143 {
00144     char *  logstr = "\tlcmaps_plugin_voms_localaccount-plugin_initialize()";
00145     int i;
00146 
00147     lcmaps_log_debug(1,"%s: passed arguments:\n", logstr);
00148     for (i=0; i < argc; i++)
00149     {
00150        lcmaps_log_debug(2,"%s: arg %d is %s\n", logstr, i, argv[i]);
00151     }
00152 
00153     /*
00154      * the first will be the thing to edit/select (gridmap(file))
00155      * the second will be the path && filename of the gridmapfile
00156      */
00157 
00158     /*
00159      * Parse arguments, argv[0] = name of plugin, so start with i = 1
00160      */
00161     for (i = 1; i < argc; i++)
00162     {
00163         if ( ((strcmp(argv[i], "-gridmap") == 0) ||
00164               (strcmp(argv[i], "-GRIDMAP") == 0) ||
00165               (strcmp(argv[i], "-gridmapfile") == 0) ||
00166               (strcmp(argv[i], "-GRIDMAPFILE") == 0))
00167              && (i + 1 < argc))
00168         {
00169             if ((argv[i + 1] != NULL) && (strlen(argv[i + 1]) > 0))
00170             {
00171                  gridmapfile = strdup(argv[i + 1]);
00172             }
00173             i++;
00174         }
00175         else if (strcmp(argv[i], "-use_voms_gid") == 0)
00176         {
00177             use_voms_gid = 1;
00178         }
00179         else
00180         {
00181             lcmaps_log(0,"%s: Error in initialization parameter: %s (failure)\n", logstr,
00182                        argv[i]);
00183             return LCMAPS_MOD_FAIL;
00184         }
00185     }
00186     return LCMAPS_MOD_SUCCESS;
00187 } 
00188 
00189 /******************************************************************************
00190 Function:   plugin_introspect
00191 Description:
00192     return list of required arguments
00193 Parameters:
00194 
00195 Returns:
00196     LCMAPS_MOD_SUCCESS : succes
00197     LCMAPS_MOD_FAIL    : failure
00198 ******************************************************************************/
00199 int plugin_introspect(
00200         int * argc,
00201         lcmaps_argument_t ** argv
00202 )
00203 {
00204     char *                   logstr = "\tlcmaps_plugin_voms_localaccount-plugin_introspect()";
00205     static lcmaps_argument_t argList[] = {
00206         {"user_dn"      ,       "char *"        , 1,   NULL},
00207         {NULL           ,       NULL            , -1,   NULL}
00208     };
00209 
00210     lcmaps_log_debug(1,"%s: introspecting\n", logstr);
00211 
00212     *argv = argList;
00213     *argc = lcmaps_cntArgs(argList);
00214     lcmaps_log_debug(1,"%s: address first argument: 0x%x\n", logstr, argList);
00215 
00216     return LCMAPS_MOD_SUCCESS;
00217 }
00218 
00219 
00220 /******************************************************************************
00221 Function:   plugin_run
00222 Description:
00223     Gather credentials for LCMAPS
00224 Parameters:
00225     argc: number of arguments
00226     argv: list of arguments
00227 Returns:
00228     LCMAPS_MOD_SUCCESS: authorization succeeded
00229     LCMAPS_MOD_FAIL   : authorization failed
00230 ******************************************************************************/
00231 int plugin_run(
00232         int argc,
00233         lcmaps_argument_t * argv
00234 )
00235 {
00236     char *              logstr = "\tlcmaps_plugin_voms_localaccount-plugin_run()";
00237     char *              dn                  = NULL; 
00238     char *              username            = NULL;
00239     struct passwd       *user_info          = NULL;
00240     int                 i                   = 0;
00241     int                 cnt_sec_gid         = 0;
00242     gid_t *             sec_gid             = NULL;
00243     char **             vo_cred_string_list = NULL;
00244     int                 cnt_vo_cred_string  = 0;
00245     int                 found               = 0;
00246     unsigned short      matching_type       = ((unsigned short)0x0000);
00247     int                 rc                  = 0;
00248     
00249     /*
00250      * The beginning
00251      */
00252     lcmaps_log_debug(1,"%s:\n", logstr);
00253 
00254     /*
00255      * Try to get the ordered values:
00256      */
00257     if ( ( dn = *(char **) lcmaps_getArgValue("user_dn", "char *", argc, argv) ) )
00258         lcmaps_log_debug(1,"%s: found dn: %s\n", logstr, dn);
00259     else
00260         lcmaps_log_debug(1,"%s: could not get value of dn !\n", logstr);
00261 
00262 
00263     /*
00264      * Check the gridmapfile
00265      */
00266 
00267     if ((gridmapfile != NULL) && (strlen(gridmapfile) > 0))
00268         lcmaps_log_debug(1,"%s: gridmapfile is: %s\n", logstr, gridmapfile);
00269     else
00270     {
00271         if (gridmapfile) free(gridmapfile);
00272         gridmapfile = NULL;
00273         lcmaps_log_debug(1,"%s: No gridmapfile assigned, so function must find out for it self\n", logstr);
00274     }
00275 
00276     /*
00277      * Get the VO user information.
00278      * We cannot order it by lcmaps_argument_t, but have to use the getCredentialData() function
00279      * since it was stored there by a plugin (lcmaps_voms.mod)
00280      * The VOMS info has to matched against the info in the gridmapfile
00281      */
00282     vo_cred_string_list = getCredentialData(LCMAPS_VO_CRED_STRING, &cnt_vo_cred_string);
00283     if (cnt_vo_cred_string == 0)
00284     {
00285         lcmaps_log(0,"%s: warning: no VOMS group info --> no mapping\n", logstr);
00286         goto success_voms_localaccount;
00287     }
00288     else if (cnt_vo_cred_string < 0)
00289     {
00290         lcmaps_log(0,"%s: negative number of VOMS groups found ! (failure)\n", logstr);
00291         goto fail_voms_localaccount;
00292     }
00293 
00294 
00295     /*
00296      * Try to match the VO strings with the gridmapfile info
00297      * normally the first available VO string should match
00298      */
00299     found = 0;
00300 
00301     matching_type = MATCH_EXCLUDE|MATCH_WILD_CHARS;
00302  
00303     for (i = 0; i < cnt_vo_cred_string; i++)
00304     {
00305         if ( (rc = lcmaps_gridlist(vo_cred_string_list[i], &username, gridmapfile, matching_type, ".", NULL)) == 0)
00306         {
00307             found = 1;
00308             lcmaps_log_debug(1,"%s: found username: %s\n", logstr, username);
00309             break;
00310         }
00311         else if (rc == LCMAPS_MOD_NOFILE)
00312         {
00313             lcmaps_log(0, "%s: Could not find the gridmapfile %s\n", logstr, gridmapfile);
00314             goto fail_voms_localaccount;
00315         }
00316         else
00317         {
00318             lcmaps_log_debug(1, "%s: no localaccount available for group (%s) in %s\n", logstr, vo_cred_string_list[i], gridmapfile);
00319         }
00320     }
00321     if (found != 1)
00322     {
00323         lcmaps_log(0, "%s: Could not find a VOMS localaccount in %s (failure)\n", logstr, gridmapfile);
00324         goto fail_voms_localaccount;
00325     }
00326 
00327 
00328     /*
00329      * Get userid to pwd_t structure
00330      */
00331     if (username && (strlen(username) > 0))
00332     {
00333 
00334         if ( ( user_info = getpwnam(username) ) )
00335         {
00336             lcmaps_log_debug(2,"%s: address user_info: %p\n", logstr, user_info);
00337             lcmaps_log_debug(2,"%s: username : %s, char ptr: %p, address char ptr: %p\n", logstr, user_info->pw_name, user_info->pw_name, &(user_info->pw_name));
00338             lcmaps_log_debug(2,"%s: password : %s\n", logstr, user_info->pw_passwd, &(user_info->pw_passwd));
00339             lcmaps_log_debug(2,"%s: user_id  : %d, address uid: %p\n", logstr, user_info->pw_uid, &(user_info->pw_uid));
00340             lcmaps_log_debug(2,"%s: group_id : %d\n", logstr, user_info->pw_gid);
00341             lcmaps_log_debug(2,"%s: realname : %s\n", logstr, user_info->pw_gecos);
00342             lcmaps_log_debug(2,"%s: home dir : %s\n", logstr, user_info->pw_dir);
00343             lcmaps_log_debug(2,"%s: shellprg : %s\n", logstr, user_info->pw_shell);
00344 
00345             /* 
00346              * Add this credential data to the credential data repository in the plugin manager
00347              */
00348             addCredentialData(DN,  &dn);
00349             addCredentialData(UID, &(user_info->pw_uid));
00350             if (use_voms_gid == 0)
00351             {
00352                 lcmaps_log_debug(1,"%s: adding primary GID (%d) from local account to CredentialData\n",
00353                                  logstr, user_info->pw_gid);
00354                 addCredentialData(PRI_GID, &(user_info->pw_gid));
00355                 /*
00356                  * Retrieve secondary group id's
00357                  */
00358                 if (lcmaps_get_gidlist(username, &cnt_sec_gid, &sec_gid)==0)
00359                 {
00360                     for (i = 0; i < cnt_sec_gid; i++)
00361                     {
00362                         addCredentialData(SEC_GID, &(sec_gid[i]));
00363                     }
00364                     free(sec_gid);
00365                 }
00366             }
00367         }
00368         else
00369         {
00370             lcmaps_log(0,"%s: no user account found named \"%s\"\n", logstr, username);
00371             goto fail_voms_localaccount;
00372         }
00373     }
00374     else
00375     {   // error (msg is already given)
00376         goto fail_voms_localaccount;
00377     }
00378 
00379     /* succes */
00380  success_voms_localaccount:
00381     if (username) free(username);
00382     lcmaps_log_time(0,"%s: voms_localaccount plugin succeeded\n", logstr);
00383     return LCMAPS_MOD_SUCCESS;
00384 
00385  fail_voms_localaccount:
00386     if (username) free(username);
00387     lcmaps_log_time(0,"%s: voms_localaccount plugin failed\n", logstr);
00388     return LCMAPS_MOD_FAIL;
00389 }
00390 
00391 /******************************************************************************
00392 Function:   plugin_terminate
00393 Description:
00394     Terminate plugin
00395 Parameters:
00396 
00397 Returns:
00398     LCMAPS_MOD_SUCCESS : succes
00399     LCMAPS_MOD_FAIL    : failure
00400 ******************************************************************************/
00401 int plugin_terminate()
00402 {
00403     char * logstr = "\tlcmaps_plugin_voms_localaccount-plugin_terminate()";
00404 
00405     lcmaps_log_debug(1,"%s: terminating\n", logstr);
00406 
00407     if (gridmapfile) free(gridmapfile);
00408 
00409     return LCMAPS_MOD_SUCCESS;
00410 }
00411 
00412 /******************************************************************************
00413 CVS Information:
00414     $Source: /local/reps/lcgware/fabric_mgt/gridification/lcmaps/modules/voms/lcmaps_voms_localaccount.c,v $
00415     $Date: 2004/10/01 15:23:04 $
00416     $Revision: 1.1 $
00417     $Author: maart $
00418 ******************************************************************************/

Generated on Sat Oct 2 02:18:27 2004 for edg-lcmaps by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002