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

Generated on Sun May 29 21:22:11 2005 for lcmaps by doxygen 1.3.5