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

lcmaps_poolaccount.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 
00122 /*****************************************************************************
00123                             Include header files
00124 ******************************************************************************/
00125 #include <stdio.h>
00126 #include <stdlib.h>
00127 #include <string.h>
00128 #include <pwd.h>
00129 
00130 #include "lcmaps_config.h"
00131 #include "lcmaps_modules.h"
00132 #include "lcmaps_arguments.h"
00133 #include "lcmaps_cred_data.h"
00134 #include "lcmaps_gridlist.h"
00135 
00136 /******************************************************************************
00137                                 Definitions
00138 ******************************************************************************/
00139 
00140 /******************************************************************************
00141                           Module specific prototypes
00142 ******************************************************************************/
00143 
00144 /******************************************************************************
00145                        Define module specific variables
00146 ******************************************************************************/
00147 
00148 static char *gridmapfile = NULL;
00149 static char *gridmapdir  = NULL;
00150 static int  override_inconsistency = 0;
00151 
00152 /******************************************************************************
00153 Function:   plugin_initialize
00154 Description:
00155     Initialize plugin
00156 Parameters:
00157     argc, argv
00158     argv[0]: the name of the plugin
00159 Returns:
00160     LCMAPS_MOD_SUCCESS : succes
00161     LCMAPS_MOD_FAIL    : failure
00162     LCMAPS_MOD_NOFILE  : db file not found (will halt LCMAPS initialization)
00163 ******************************************************************************/
00164 int plugin_initialize(
00165         int argc,
00166         char ** argv
00167 )
00168 {
00169     char * logstr = "\tlcmaps_plugin_poolaccount-plugin_initialize()";
00170     int i;
00171 
00172     lcmaps_log_debug(1,"%s: passed arguments:\n", logstr);
00173     for (i=0; i < argc; i++)
00174     {
00175        lcmaps_log_debug(2,"%s: arg %d is %s\n", logstr, i, argv[i]);
00176     }
00177 
00178     /*
00179      * the first will be the thing to edit/select (gridmap(file))
00180      * the second will be the path && filename of the gridmapfile
00181      */
00182 
00183     /*
00184      * Parse arguments, argv[0] = name of plugin, so start with i = 1
00185      */
00186     for (i = 1; i < argc; i++)
00187     {
00188         if ( ((strcmp(argv[i], "-gridmap") == 0) ||
00189               (strcmp(argv[i], "-GRIDMAP") == 0) ||
00190               (strcmp(argv[i], "-gridmapfile") == 0) ||
00191               (strcmp(argv[i], "-GRIDMAPFILE") == 0))
00192              && (i + 1 < argc))
00193         {
00194             if ((argv[i + 1] != NULL) && (strlen(argv[i + 1]) > 0))
00195             {
00196                  gridmapfile = strdup(argv[i + 1]);
00197             }
00198             i++;
00199         }
00200         else if ( ((strcmp(argv[i], "-gridmapdir") == 0) ||
00201               (strcmp(argv[i], "-GRIDMAPDIR") == 0))
00202              && (i + 1 < argc))
00203         {
00204             if ((argv[i + 1] != NULL) && (strlen(argv[i + 1]) > 0))
00205             {
00206                  gridmapdir = strdup(argv[i + 1]);
00207             }
00208             i++;
00209         }
00210         else if ( (strcmp(argv[i], "-override_inconsistency") == 0) ||
00211                   (strcmp(argv[i], "-OVERRIDE_INCONSISTENCY") == 0))
00212         {
00213             override_inconsistency = 1;
00214         }
00215         else
00216         {
00217             lcmaps_log(0,"%s: Error in initialization parameter: %s (failure)\n", logstr,
00218                        argv[i]);
00219             return LCMAPS_MOD_FAIL;
00220         }
00221     }
00222 
00223     return LCMAPS_MOD_SUCCESS;
00224 }
00225 
00226 /******************************************************************************
00227 Function:   plugin_introspect
00228 Description:
00229     return list of required arguments
00230 Parameters:
00231 
00232 Returns:
00233     LCMAPS_MOD_SUCCESS : succes
00234     LCMAPS_MOD_FAIL    : failure
00235 ******************************************************************************/
00236 int plugin_introspect(
00237         int * argc,
00238         lcmaps_argument_t ** argv
00239 )
00240 {
00241     char * logstr = "\tlcmaps_plugin_poolaccount-plugin_introspect()";
00242     static lcmaps_argument_t argList[] = {
00243         {"user_dn"      ,       "char *"        , 1,   NULL},
00244         {NULL           ,       NULL            , -1,   NULL}
00245     };
00246 
00247     lcmaps_log_debug(1,"%s: introspecting\n", logstr);
00248 
00249     *argv = argList;
00250     *argc = lcmaps_cntArgs(argList);
00251     lcmaps_log_debug(1,"%s: address first argument: 0x%x\n", logstr,argList);
00252 
00253     return LCMAPS_MOD_SUCCESS;
00254 }
00255 
00256 
00257 /******************************************************************************
00258 Function:   plugin_run
00259 Description:
00260     Gather credentials for LCMAPS
00261 Parameters:
00262     argc: number of arguments
00263     argv: list of arguments
00264 Returns:
00265     LCMAPS_MOD_SUCCESS: authorization succeeded
00266     LCMAPS_MOD_FAIL   : authorization failed
00267 ******************************************************************************/
00268 int plugin_run(
00269         int argc,
00270         lcmaps_argument_t * argv
00271 )
00272 {
00273     char *              logstr = "\tlcmaps_plugin_poolaccount-plugin_run()";
00274     char *              dn          = NULL; 
00275     char *              username    = NULL;
00276     struct passwd       *user_info  = NULL;
00277     int                 i           = 0;
00278     int                 cnt_sec_gid = 0;
00279     gid_t *             sec_gid     = NULL;
00280     unsigned short      matching_type = ((unsigned short)0x0000);
00281     int                 rc = 0;
00282 
00283     /*
00284      * The beginning
00285      */
00286     lcmaps_log_debug(1,"%s:\n", logstr);
00287 
00288     /*
00289      * Try to get the ordered values:
00290      */
00291     if ( ( dn = *(char **) lcmaps_getArgValue("user_dn", "char *", argc, argv) ) )
00292         lcmaps_log_debug(1,"%s: found dn: %s\n", logstr,dn);
00293     else
00294         lcmaps_log_debug(1,"%s: could not get value of dn !\n", logstr);
00295 
00296 
00297     /*
00298      * Check the gridmapfile
00299      */
00300 
00301     if ((gridmapfile != NULL) && (strlen(gridmapfile) > 0))
00302         lcmaps_log_debug(1,"%s: gridmapfile is: %s\n", logstr, gridmapfile);
00303     else
00304     {
00305         if (gridmapfile) free(gridmapfile);
00306         gridmapfile = NULL;
00307         lcmaps_log_debug(1,"%s: No gridmapfile assigned, so function must find out for it self\n",
00308         logstr);
00309     }
00310 
00311     /*
00312      * Check gridmapdir
00313      */
00314     if (gridmapdir == NULL) /* try if GRIDMAPDIR is already set */
00315     {
00316         char * tmpptr=NULL;
00317         if ((tmpptr = getenv("GRIDMAPDIR")) == NULL)
00318         {
00319             lcmaps_log(0,"%s: GRIDMAPDIR unknown !\n", logstr);
00320             lcmaps_log(0,"%s:  specify as option or set GRIDMAPDIR\n", logstr);
00321             goto fail_poolaccount;
00322         }
00323         else
00324         {
00325             gridmapdir = strdup(tmpptr);
00326         }
00327     }
00328     if (strlen(gridmapdir) == 0)
00329     {
00330         lcmaps_log(0,"%s: cannot set MAPDIR (strlen(gridmapdir) == 0)\n", logstr);
00331         goto fail_poolaccount;
00332     }
00333     lcmaps_log_debug(1,"%s: setting MAPDIR to %s\n", logstr, gridmapdir);
00334     if (setenv("MAPDIR", gridmapdir, 1))
00335     {
00336         lcmaps_log(0,"%s: cannot set MAPDIR\n", logstr);
00337         goto fail_poolaccount;
00338     }
00339 
00340     /*
00341      * Try to find the dn in the gridmapfile
00342      */
00343     matching_type = MATCH_INCLUDE|MATCH_NO_WILD_CHARS;
00344     
00345     /* if override_consistency is set add this to the matchin_type so it will take effect */
00346     if (override_inconsistency)
00347         matching_type = matching_type|OVERRIDE_INCONSISTANCY;
00348 
00349     if ( (rc = lcmaps_gridlist(dn, &username, gridmapfile, matching_type, ".", NULL)) == LCMAPS_MOD_SUCCESS)
00350         lcmaps_log_debug(1,"%s: found username: %s\n", logstr, username);
00351     else if (rc == LCMAPS_MOD_NOFILE)
00352     {
00353         lcmaps_log(0, "%s: Could not find the gridmapfile %s\n", logstr, gridmapfile);
00354         goto fail_poolaccount;
00355     }
00356     else if (rc == LCMAPS_MOD_NOENTRY)
00357     {
00358         lcmaps_log_debug(1, "%s: No entry found for %s in %s\n", logstr, dn, gridmapfile);
00359         goto fail_poolaccount;
00360     }
00361     else
00362     {
00363         lcmaps_log_debug(1,"%s: could not get value of username !\n", logstr);
00364         goto fail_poolaccount;
00365     }
00366 
00367 
00368     /*
00369      * Get userid to pwd_t structure
00370      */
00371     if (username && (strlen(username) > 0))
00372     {
00373 
00374         if ( ( user_info = getpwnam(username) ) )
00375         {
00376             lcmaps_log_debug(2,"%s: address user_info: %p\n", logstr, user_info);
00377             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));
00378             lcmaps_log_debug(2,"%s: password : %s\n", logstr, user_info->pw_passwd, &(user_info->pw_passwd));
00379             lcmaps_log_debug(2,"%s: user_id  : %d, address uid: %p\n", logstr, user_info->pw_uid, &(user_info->pw_uid));
00380             lcmaps_log_debug(2,"%s: group_id : %d\n", logstr, user_info->pw_gid);
00381             lcmaps_log_debug(2,"%s: realname : %s\n", logstr, user_info->pw_gecos);
00382             lcmaps_log_debug(2,"%s: home dir : %s\n", logstr, user_info->pw_dir);
00383             lcmaps_log_debug(2,"%s: shellprg : %s\n", logstr, user_info->pw_shell);
00384 
00385             /* 
00386              * Add this credential data to the credential data repository in the plugin manager
00387              */
00388             addCredentialData(DN,  &dn);
00389             addCredentialData(UID, &(user_info->pw_uid));
00390             addCredentialData(PRI_GID, &(user_info->pw_gid));
00391 
00392             /*
00393              * Retrieve secondary group id's
00394              */
00395             if (lcmaps_get_gidlist(username, &cnt_sec_gid, &sec_gid)==0)
00396             {
00397                 for (i = 0; i < cnt_sec_gid; i++)
00398                 {
00399                     addCredentialData(SEC_GID, &(sec_gid[i]));
00400                 }
00401                 free(sec_gid);
00402             }
00403         }
00404         else
00405         {
00406             lcmaps_log(0,"%s: no user account found name \"%s\"\n", logstr, username);
00407             goto fail_poolaccount;
00408         }
00409     }
00410     else
00411     {   // error (msg is already given)
00412         goto fail_poolaccount;
00413     }
00414 
00415     /* succes */
00416  success_poolaccount:
00417     if (username) free(username);
00418     lcmaps_log_time(0,"%s: poolaccount plugin succeeded\n", logstr);
00419     return LCMAPS_MOD_SUCCESS;
00420 
00421  fail_poolaccount:
00422     if (username) free(username);
00423     lcmaps_log_time(0,"%s: poolaccount plugin failed\n", logstr);
00424     return LCMAPS_MOD_FAIL;
00425 }
00426 
00427 /******************************************************************************
00428 Function:   plugin_terminate
00429 Description:
00430     Terminate plugin
00431 Parameters:
00432 
00433 Returns:
00434     LCMAPS_MOD_SUCCESS : succes
00435     LCMAPS_MOD_FAIL    : failure
00436 ******************************************************************************/
00437 int plugin_terminate()
00438 {
00439     char *              logstr = "\tlcmaps_plugin_poolaccount-plugin_terminate()";
00440     lcmaps_log_debug(1,"%s: terminating\n", logstr);
00441 
00442     if (gridmapfile) free(gridmapfile);
00443     if (gridmapdir) free(gridmapdir);
00444 
00445     return LCMAPS_MOD_SUCCESS;
00446 }
00447 
00448 /******************************************************************************
00449 CVS Information:
00450     $Source: /cvs/fabric_mgt/gridification/lcmaps/modules/poolaccount/lcmaps_poolaccount.c,v $
00451     $Date: 2003/08/12 13:04:15 $
00452     $Revision: 1.17 $
00453     $Author: martijn $
00454 ******************************************************************************/

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