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

lcmaps.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 
00066 #ifndef LCMAPS_C
00067 #define LCMAPS_C
00068 
00069 #include "lcmaps_config.h"
00070 #include <stdio.h>
00071 #include <stdlib.h>
00072 #include <string.h>
00073 #ifdef LCMAPS_GSI_MODE
00074 #   include <gssapi.h>
00075 #endif
00076 
00077 /* LCMAPS includes */
00078 #include "lcmaps_types.h"
00079 #include "lcmaps_utils.h"
00080 #include "lcmaps_cred_data.h"
00081 #include "_lcmaps_utils.h"
00082 #include "_lcmaps_pluginmanager.h"
00083 #include "_lcmaps_log.h"
00084 #include "_lcmaps_credential.h"
00085 
00086 /******************************************************************************
00087                        Define module specific variables
00088 ******************************************************************************/
00089 static lcmaps_cred_id_t    lcmaps_cred; 
00090 static int                 lcmaps_initialized = 0; 
00092 /******************************************************************************
00093 Function:   lcmaps_init_and_logfile
00094 Description:
00095     Select logging type
00096     Setup logging by providing a file handle or file name, error handling (not yet).
00097     Start PluginManager:
00098     read from LCMAPS config file, the plugins to be loaded
00099 
00100 Parameters:
00101     logfile: name of logfile 
00102     fp: file handle for logging (from gatekeeper or other previously opened file handle)
00103         If the file handle is zero, assume that only syslogging is requested
00104     logtype: type of logging (usrlog and/or syslog)
00105 
00106 Returns:
00107     0: initialization succeeded
00108     1: initialization failed
00109 ******************************************************************************/
00110 int lcmaps_init_and_logfile(
00111         char * logfile,
00112         FILE* fp,
00113         unsigned short logtype
00114 )
00115 {
00116     if (lcmaps_initialized)
00117     {
00118         if (lcmaps_log(0,"LCMAPS already initialized\n") != 0)
00119         {
00120             fprintf(stderr,"LCMAPS already initialized, but wrongly\n");
00121             goto fail_lcmaps_init_and_logfile;
00122         }
00123         return 0;
00124     }
00125 
00126     /* start logging */
00127     if (lcmaps_log_open(logfile,fp,logtype))
00128         goto fail_lcmaps_init_and_logfile;
00129     lcmaps_log_debug(0,"\n");
00130     lcmaps_log_time(LOG_DEBUG,"Initialization LCMAPS version %s\n",VERSION);
00131 
00132     /* Start PluginManager */
00133     if (startPluginManager()) {
00134         lcmaps_log(0,"lcmaps.mod-lcmaps_init() error: could not start plugin manager\n");
00135         goto fail_lcmaps_init_and_logfile;
00136     }
00137 
00138     /* success */
00139     lcmaps_initialized++;
00140     return 0;
00141 
00142  fail_lcmaps_init_and_logfile:
00143     return 1;
00144 
00145 }
00146 /******************************************************************************
00147 Function:   lcmaps_init_and_log
00148 Description:
00149     Select logging type
00150     Start PluginManager:
00151     read from LCMAPS config file, the plugins to be loaded
00152 
00153 Parameters:
00154     fp: file handle for logging (from gatekeeper or other previously opened file handle)
00155         If the file handle is zero, assume that only syslogging is requested
00156     logtype: type of logging (usrlog and/or syslog)
00157 
00158 Returns:
00159     0: initialization succeeded
00160     1: initialization failed
00161 ******************************************************************************/
00162 int lcmaps_init_and_log(
00163         FILE* fp,
00164         unsigned short logtype
00165 )
00166 {
00167     return lcmaps_init_and_logfile(NULL, fp, logtype);
00168 }
00169 
00170 /******************************************************************************
00171 Function:   lcmaps_init
00172 Description:
00173     Start PluginManager:
00174     read from LCMAPS config file, the plugins to be loaded
00175 
00176 Parameters:
00177     fp: file handle for logging (from gatekeeper or other previously opened file handle)
00178         If the file handle is zero, assume that only syslogging is requested
00179 Returns:
00180     0: initialization succeeded
00181     1: initialization failed
00182 ******************************************************************************/
00183 int lcmaps_init(
00184         FILE* fp
00185 )
00186 {
00187     /* set logging file descriptor, for the moment the gatekeeper logfile */
00188     /* if fp == NULL --> syslogging, otherwise only DO_USRLOG */
00189     if (fp)
00190     {
00191         return lcmaps_init_and_log(fp,DO_USRLOG);
00192     }
00193     else
00194     {
00195         return lcmaps_init_and_log(NULL,DO_SYSLOG);
00196     }
00197 }
00198 
00199 /******************************************************************************
00200 Function:   lcmaps_term
00201 Description:
00202     Terminate LCMAPS module: 
00203 
00204 Parameters:
00205 Returns:
00206     0: termination succeeded
00207     1: termination failed
00208 ******************************************************************************/
00209 int lcmaps_term()
00210 {
00211     lcmaps_log_time(LOG_DEBUG,"Termination LCMAPS\n");
00212     lcmaps_log_time(0,"lcmaps.mod-lcmaps_term(): terminating\n");
00213     if (stopPluginManager() != 0)
00214         return 1;
00215     if (lcmaps_log_close() != 0)
00216         return 1;
00217     if (lcmaps_initialized > 0)
00218         lcmaps_initialized--;
00219     return 0;
00220 }
00221 
00222 /******************************************************************************
00223 Function:   lcmaps_run_without_credentials_and_return_username
00224 Description:
00225     Based on the only the user DN do the following:
00226     Do the user mapping based on the provided list of policies (first successful
00227     policy found in the lcmaps policy file (lcmaps.db) will result in the user
00228     mapping) and return user name.
00229     This interface can be used to provide the legacy
00230         GLOBUS_GSS_ASSIST_GRIDMAP()
00231     interface.
00232 
00233 Parameters:
00234     user_dn_tmp : user DN
00235     request     : RSL string
00236     usernamep   : pointer to user name (to be freed by calling application)
00237     npols       : number of policies to be considered for evaluation
00238     policynames : the names of the policies to be considered for evaluation
00239 
00240 Returns:
00241     0: mapping succeeded
00242     1: mapping failed
00243 ******************************************************************************/
00244 int lcmaps_run_without_credentials_and_return_username(
00245         char * user_dn_tmp,
00246         lcmaps_request_t request,
00247         char ** usernamep,
00248         int npols,
00249         char ** policynames
00250 )
00251 {
00252     char *            user_dn     = NULL;
00253     uid_t *           uid;
00254     int               cntUid;
00255     struct passwd *   user_info   = NULL;
00256     char *            logstr = "lcmaps.mod-lcmaps_run_without_credentials_and_return_username()";
00257     int               rc          = 0;
00258 
00259 
00260     if (lcmaps_initialized == 0)
00261     {
00262         fprintf(stderr,"LCMAPS has to be initialized first !\n");
00263         goto fail_lcmaps_run_without_credentials_and_return_username;
00264     }
00265     lcmaps_log_time(LOG_NOTICE,"LCMAPS credential mapping request\n");
00266     
00267     lcmaps_log_debug(1, "Using \"lcmaps_run_without_credentials_and_return_username\" interface of LCMAPS\n");
00268     if (usernamep == NULL)
00269         goto fail_lcmaps_run_without_credentials_and_return_username;
00270     *usernamep = NULL;
00271 
00272     /*
00273      * Create lcmaps credential:
00274      *     fill it with the dn and extract it again
00275      */
00276     if ((rc = lcmaps_credential_init(&lcmaps_cred)) != 0)
00277     {
00278         if (rc == -1)
00279         {
00280             lcmaps_log(0, "%s error: lcmaps_cred does not exist\n", logstr);
00281             goto fail_lcmaps_run_without_credentials_and_return_username;
00282         }
00283         else
00284         {
00285             lcmaps_log(0, "%s error: in initializing lcmaps_cred\n", logstr);
00286             goto fail_lcmaps_run_without_credentials_and_return_username;
00287         }
00288     }
00289     if (lcmaps_credential_store_dn(user_dn_tmp, &lcmaps_cred) != 0)
00290     {
00291         lcmaps_log(0, "%s error: storing dn in lcmaps_cred\n");
00292         goto fail_lcmaps_run_without_credentials_and_return_username;
00293     }
00294     user_dn = lcmaps_credential_get_dn(lcmaps_cred);
00295     if (user_dn == NULL)
00296     {
00297         lcmaps_log(0, "lcmaps.mod-lcmaps_run_without_credentials_and_return_username() error: user DN empty\n");
00298         goto fail_lcmaps_run_without_credentials_and_return_username;
00299     }
00300 
00301     /* Run PluginManager */
00302     if (runPluginManager(request, lcmaps_cred, npols, policynames)) {
00303         lcmaps_log(0, "%s error: could not run plugin manager\n");
00304         goto fail_lcmaps_run_without_credentials_and_return_username;
00305     }
00306 
00307     /* Now try to get the userid drom the credential data */
00308     uid    = getCredentialData(UID,     &cntUid);
00309     if (uid)
00310     {
00311         if ( (user_info = getpwuid(uid[0])) == NULL )
00312         {
00313             fprintf(stderr,"LCMAPS could not find the username related to uid: %d\n",uid[0]);
00314             return 1;
00315         }
00316         (*usernamep) = strdup(user_info->pw_name);
00317     }
00318     else
00319     {
00320         fprintf(stderr,"LCMAPS could not find any uid\n");
00321         return 1;
00322     }
00323 
00324     /* succes */
00325     lcmaps_release_cred(&lcmaps_cred);
00326     lcmaps_log_debug(0,"lcmaps.mod-lcmaps_run_without_credentials_and_return_username(): succeeded\n");
00327     return 0;
00328 
00329  fail_lcmaps_run_without_credentials_and_return_username:
00330     lcmaps_release_cred(&lcmaps_cred);
00331     lcmaps_log_debug(0, "%s: failed\n", logstr);
00332     return 1;
00333 }
00334 
00335 
00336 /******************************************************************************
00337 Function:   lcmaps_run_with_fqans_and_return_account
00338 Description:
00339     LCMAPS will run with a list of FQANs and the DN as an input. In addition a
00340     list of policies may be provided.
00341     The allocated uid, gids and the poolindex will be returned to the calling
00342     application.
00343     This interface is intended to be used by a wrapper function for the 
00344         DYNAMIC ACCOUNTS SERVICE (DAS).
00345 
00346 Parameters:
00347     user_dn     : the DN of the user
00348     fqan_list   : the list of (VOMS) FQANs that have been asserted to the user
00349     nfqan       : the number of FQANs in fqan_list
00350     request     : RSL string
00351     npols       : number of policies to be considered for evaluation
00352     policynames : the names of the policies to be considered for evaluation
00353     puid        : pointer to the uid found (output parameter)
00354     ppgid_list  : pointer to the list of primary gids found (output parameter)
00355     pnpgid      : pointer to the number of primary gids found (output parameter)
00356     psgid_list  : pointer to the list of secondary gids found (output parameter)
00357     pnsgid      : pointer to the number of secondary gids found (output parameter)
00358     poolindexp  : pointer to poolindex string (output parameter)
00359 
00360 Returns:
00361     0: mapping succeeded
00362     1: mapping failed
00363 ******************************************************************************/
00364 int lcmaps_run_with_fqans_and_return_account(
00365         char *            user_dn,
00366         char **           fqan_list,
00367         int               nfqan,
00368         lcmaps_request_t  request,
00369         int               npols,
00370         char **           policynames,
00371         uid_t *           puid,
00372         gid_t **          ppgid_list,
00373         int *             pnpgid,
00374         gid_t **          psgid_list,
00375         int *             pnsgid,
00376         char **           poolindexp
00377 )
00378 {
00379     char *   logstr         = "lcmaps.mod-lcmaps_run_with_fqans_and_return_account()";
00380     int      rc             = 0;
00381     uid_t *  uid            = NULL;
00382     int      cntUid         = -1;
00383     gid_t *  priGid         = NULL;
00384     int      cntPriGid      = -1;
00385     gid_t *  secGid         = NULL;
00386     int      cntSecGid      = -1;
00387     char **  poolindex_list = NULL;
00388     char *   poolindex      = NULL;
00389     int      cntpoolindex   = 0;
00390 
00391     if (lcmaps_initialized == 0)
00392     {
00393         fprintf(stderr,"LCMAPS has to be initialized first !\n");
00394         goto fail_lcmaps_run_with_fqans_and_return_account;
00395     }
00396     lcmaps_log_time(LOG_NOTICE,"LCMAPS credential mapping request\n");
00397 
00398     lcmaps_log_debug(1, "Using \"lcmaps_run_with_fqans_and_return_account\" interface of LCMAPS\n");
00399     /*
00400      * Create lcmaps credential:
00401      *     fill it with the dn and extract it again
00402      */
00403     if ((rc = lcmaps_credential_init(&lcmaps_cred)) != 0)
00404     {
00405         if (rc == -1)
00406         {
00407             lcmaps_log(0, "%s error: lcmaps_cred does not exist\n", logstr);
00408             goto fail_lcmaps_run_with_fqans_and_return_account;
00409         }
00410         else
00411         {
00412             lcmaps_log(0, "%s error: in initializing lcmaps_cred\n", logstr);
00413             goto fail_lcmaps_run_with_fqans_and_return_account;
00414         }
00415     }
00416     if (lcmaps_credential_store_dn(user_dn, &lcmaps_cred) != 0)
00417     {
00418         lcmaps_log(0, "%s error: storing dn in lcmaps_cred\n");
00419         goto fail_lcmaps_run_with_fqans_and_return_account;
00420     }
00421     if ((rc = lcmaps_credential_store_fqan_list(nfqan, fqan_list, &lcmaps_cred)) != 0)
00422     {
00423         if (rc == -1)
00424         {
00425             lcmaps_log(0, "%s error: lcmaps_cred does not exist\n", logstr);
00426             goto fail_lcmaps_run_with_fqans_and_return_account;
00427         }
00428         else if (rc == 1)
00429         {
00430             lcmaps_log(0, "%s error: while storing a malformed fqan list\n", logstr);
00431             goto fail_lcmaps_run_with_fqans_and_return_account;
00432         }
00433         else if (rc == 2)
00434         {
00435             lcmaps_log(0, "%s error: Malloc error while storing fqan list!\n", logstr);
00436             goto fail_lcmaps_run_with_fqans_and_return_account;
00437         }
00438         else
00439         {
00440             lcmaps_log(0, "%s error: storing fqan list\n", logstr);
00441             goto fail_lcmaps_run_with_fqans_and_return_account;
00442         }
00443     }
00444 
00445     /* Run PluginManager */
00446     if (runPluginManager(request, lcmaps_cred, npols, policynames)) {
00447         lcmaps_log(0, "%s error: could not run plugin manager\n", logstr);
00448         goto fail_lcmaps_run_with_fqans_and_return_account;
00449     }
00450 
00451     /* Hier even de uid, gids and poolindex uitpeuteren */
00452     /* First the uid */
00453     uid              = (uid_t *) getCredentialData(UID,     &cntUid);
00454     if (uid == NULL)
00455     {
00456         lcmaps_log(0, "%s error: LCMAPS could not find any uid\n", logstr);
00457         goto fail_lcmaps_run_with_fqans_and_return_account;
00458     }
00459     else if (cntUid != 1)
00460     {
00461         lcmaps_log(0, "%s error: LCMAPS found %d uids, only 1 allowed\n", logstr, cntUid);
00462         goto fail_lcmaps_run_with_fqans_and_return_account;
00463     }
00464     else
00465     {
00466         *puid = uid[0];
00467     }
00468     /* Then the primary gids */
00469     priGid           = (gid_t *) getCredentialData(PRI_GID, &cntPriGid);
00470     if (priGid == NULL)
00471     {
00472         lcmaps_log(0, "%s error: LCMAPS could not find any gid, at least one required!\n", logstr);
00473         goto fail_lcmaps_run_with_fqans_and_return_account;
00474     }
00475     else
00476     {
00477         *pnpgid = cntPriGid;
00478         *ppgid_list = priGid;
00479     }
00480     /* Then the secondary gids */
00481     secGid           = (gid_t *) getCredentialData(SEC_GID, &cntSecGid);
00482     if (secGid == NULL)
00483     {
00484         lcmaps_log_debug(1, "%s: LCMAPS found no secondary groups\n", logstr);
00485     }
00486     else
00487     {
00488         *pnsgid = cntSecGid;
00489         *psgid_list = secGid;
00490     }
00491     /* And finally the poolindex */
00492     poolindex_list = (char **) getCredentialData(POOL_INDEX, &cntpoolindex);
00493     if (poolindex_list && (cntpoolindex > 0))
00494     {
00495         poolindex = (char *)(*(poolindex_list));
00496         lcmaps_log_debug(3, "%s: found %d poolindeces starting at address = %p\n",
00497             logstr, cntpoolindex, poolindex);
00498         /*
00499          * use lcmaps_log_a_string_debug() to get rid of the vsnprintf problems with
00500          * strings containing strange characters, such as %
00501          */
00502         lcmaps_log_a_string_debug(3,
00503             "lcmaps.mod-lcmaps_run_with_fqans_and_return_account(): found this poolindex %s\n",
00504             poolindex);
00505         *poolindexp = poolindex;
00506     }
00507     else
00508     {
00509         lcmaps_log(0, "%s: LCMAPS could not find the poolindex\n", logstr);
00510         goto fail_lcmaps_run_with_fqans_and_return_account;
00511     }
00512 
00513     /* succes */
00514     lcmaps_release_cred(&lcmaps_cred);
00515     lcmaps_log_time(0, "%s: succeeded\n", logstr);
00516     return 0;
00517 
00518  fail_lcmaps_run_with_fqans_and_return_account:
00519     lcmaps_release_cred(&lcmaps_cred);
00520     lcmaps_log_time(0, "%s: failed\n", logstr);
00521     return 1;
00522 }
00523 
00524 /*
00525  * HERE FOLLOW THE GSI DEPENDENT INTERFACES
00526  */
00527 
00528 #ifdef LCMAPS_GSI_MODE
00529 /******************************************************************************
00530 Function:   lcmaps_run
00531 Description:
00532     Do the user mapping based on the user's gss (gsi) credential and the job
00533     request.
00534     This is the legacy lcmaps interface and is used by
00535         the GATEKEEPER.
00536 
00537 Parameters:
00538     request: RSL string
00539     user_cred : user globus credential handle
00540 Returns:
00541     0: mapping succeeded
00542     1: mapping failed
00543 ******************************************************************************/
00544 #if ALLOW_EMPTY_CREDENTIALS
00545 int lcmaps_run(
00546         char * user_dn_tmp,
00547         gss_cred_id_t user_cred,
00548         lcmaps_request_t request
00549 )
00550 #else
00551 int lcmaps_run(
00552         gss_cred_id_t user_cred,
00553         lcmaps_request_t request
00554 )
00555 #endif
00556 {
00557     char *  user_dn = NULL;
00558     char *  logstr = "lcmaps.mod-lcmaps_run()";
00559     int     rc = 0;
00560 
00561     if (lcmaps_initialized == 0)
00562     {
00563         fprintf(stderr,"LCMAPS has to be initialized first !\n");
00564         goto fail_lcmaps_run;
00565     }
00566     lcmaps_log_time(LOG_NOTICE,"LCMAPS credential mapping request\n");
00567 
00568     /*
00569      * Create lcmaps credential:
00570      *     fill it with the dn and extract it again
00571      */
00572     if ((rc = lcmaps_credential_init(&lcmaps_cred)) != 0)
00573     {
00574         if (rc == -1)
00575         {
00576             lcmaps_log(0, "%s error: lcmaps_cred does not exist\n", logstr);
00577             goto fail_lcmaps_run;
00578         }
00579         else
00580         {
00581             lcmaps_log(0, "%s error: in initializing lcmaps_cred\n", logstr);
00582             goto fail_lcmaps_run;
00583         }
00584     }
00585 #if ALLOW_EMPTY_CREDENTIALS
00586     if (lcmaps_credential_store_dn(user_dn_tmp, &lcmaps_cred) != 0)
00587     {
00588         lcmaps_log(0, "%s error: storing dn in lcmaps_cred\n");
00589         goto fail_lcmaps_run;
00590     }
00591 #endif
00592     if ((rc = lcmaps_credential_store_gss_cred_id_t(user_cred, &lcmaps_cred)) != 0)
00593     {
00594         if (rc == -1)
00595         {
00596             lcmaps_log(0, "%s error: lcmaps_cred does not exist\n", logstr);
00597             goto fail_lcmaps_run;
00598         }
00599         else if (rc == 1)
00600         {
00601             lcmaps_log(0, "%s error: cannot retrieve dn while storing gss_credential\n", logstr);
00602             goto fail_lcmaps_run;
00603         }
00604         else if (rc == 2)
00605         {
00606             lcmaps_log(0, "%s: No FQANs found while storing gss_credential (non fatal)\n", logstr);
00607         }
00608         else if (rc == 3)
00609         {
00610             lcmaps_log(0, "%s error: Retrieving VOMS FQANs while storing gss_credential\n", logstr);
00611             goto fail_lcmaps_run;
00612         }
00613         else
00614         {
00615             lcmaps_log(0, "%s error: storing gss_credential\n", logstr);
00616             goto fail_lcmaps_run;
00617         }
00618     }
00619     user_dn = lcmaps_credential_get_dn(lcmaps_cred);
00620     if (user_dn == NULL)
00621     {
00622         lcmaps_log(0, "%s error: user DN empty\n", logstr);
00623         goto fail_lcmaps_run;
00624     }
00625 
00626     /* Run PluginManager */
00627     if (runPluginManager(request, lcmaps_cred, 0, NULL)) {
00628         lcmaps_log(0, "%s error: could not run plugin manager\n", logstr);
00629         goto fail_lcmaps_run;
00630     }
00631 
00632     /* succes */
00633     lcmaps_release_cred(&lcmaps_cred);
00634     lcmaps_log_time(0, "%s: succeeded\n", logstr);
00635     return 0;
00636 
00637  fail_lcmaps_run:
00638     lcmaps_release_cred(&lcmaps_cred);
00639     lcmaps_log_time(0, "%s: failed\n", logstr);
00640     return 1;
00641 }
00642 
00643 /******************************************************************************
00644 Function:   lcmaps_run_and_return_username
00645 Description:
00646     do the user mapping based on the provided list of policies (first successful
00647     policy found in the lcmaps policy file (lcmaps.db) will result in the user
00648     mapping) and return user name.
00649     This interface is used by the
00650         GRIDFTP SERVER.
00651 
00652 Parameters:
00653     request     : RSL string
00654     user_cred   : user globus credential handle
00655     usernamep   : pointer to user name (to be freed by calling application)
00656     npols       : number of policies to be considered for evaluation
00657     policynames : the names of the policies to be considered for evaluation
00658 
00659 Returns:
00660     0: mapping succeeded
00661     1: mapping failed
00662 ******************************************************************************/
00663 #if ALLOW_EMPTY_CREDENTIALS
00664 int lcmaps_run_and_return_username(
00665         char * user_dn_tmp,
00666         gss_cred_id_t user_cred,
00667         lcmaps_request_t request,
00668         char ** usernamep,
00669         int npols,
00670         char ** policynames
00671 )
00672 #else
00673 int lcmaps_run_and_return_username(
00674         gss_cred_id_t user_cred,
00675         lcmaps_request_t request,
00676         char ** usernamep,
00677         int npols,
00678         char ** policynames
00679 )
00680 #endif
00681 {
00682     char *  logstr  = "lcmaps.mod-lcmaps_run_and_return_username()";
00683     char *  user_dn = NULL;
00684     int     rc      = 0;
00685 
00686     uid_t *          uid;
00687     int              cntUid;
00688     struct passwd *  user_info   = NULL;
00689 
00690     if (lcmaps_initialized == 0)
00691     {
00692         fprintf(stderr,"LCMAPS has to be initialized first !\n");
00693         goto fail_lcmaps_run_and_return_username;
00694     }
00695     lcmaps_log_time(LOG_NOTICE,"LCMAPS credential mapping request\n");
00696 
00697     lcmaps_log_debug(1, "Using lcmaps_run_and_return_username interface of LCMAPS\n");
00698     if (usernamep == NULL)
00699         goto fail_lcmaps_run_and_return_username;
00700 
00701     *usernamep = NULL;
00702 
00703     /*
00704      * Create lcmaps credential:
00705      *     fill it with the dn and extract it again
00706      */
00707     if ((rc = lcmaps_credential_init(&lcmaps_cred)) != 0)
00708     {
00709         if (rc == -1)
00710         {
00711             lcmaps_log(0, "%s error: lcmaps_cred does not exist\n", logstr);
00712             goto fail_lcmaps_run_and_return_username;
00713         }
00714         else
00715         {
00716             lcmaps_log(0, "%s error: in initializing lcmaps_cred\n", logstr);
00717             goto fail_lcmaps_run_and_return_username;
00718         }
00719     }
00720 #if ALLOW_EMPTY_CREDENTIALS
00721     if (lcmaps_credential_store_dn(user_dn_tmp, &lcmaps_cred) != 0)
00722     {
00723         lcmaps_log(0, "%s error: storing dn in lcmaps_cred\n");
00724         goto fail_lcmaps_run_and_return_username;
00725     }
00726 #endif
00727     if ((rc = lcmaps_credential_store_gss_cred_id_t(user_cred, &lcmaps_cred)) != 0)
00728     {
00729         if (rc == -1)
00730         {
00731             lcmaps_log(0, "%s error: lcmaps_cred does not exist\n", logstr);
00732             goto fail_lcmaps_run_and_return_username;
00733         }
00734         else if (rc == 1)
00735         {
00736             lcmaps_log(0, "%s error: cannot retrieve dn while storing gss_credential\n", logstr);
00737             goto fail_lcmaps_run_and_return_username;
00738         }
00739         else if (rc == 2)
00740         {
00741             lcmaps_log(0, "%s: No FQANs found while storing gss_credential (non fatal)\n", logstr);
00742         }
00743         else if (rc == 3)
00744         {
00745             lcmaps_log(0, "%s error: Retrieving VOMS FQANs while storing gss_credential\n", logstr);
00746             goto fail_lcmaps_run_and_return_username;
00747         }
00748         else
00749         {
00750             lcmaps_log(0, "%s error: storing gss_credential\n", logstr);
00751             goto fail_lcmaps_run_and_return_username;
00752         }
00753     }
00754     user_dn = lcmaps_credential_get_dn(lcmaps_cred);
00755     if (user_dn == NULL)
00756     {
00757         lcmaps_log(0, "%s error: user DN empty\n", logstr);
00758         goto fail_lcmaps_run_and_return_username;
00759     }
00760 
00761     /* Run PluginManager */
00762     if (runPluginManager(request, lcmaps_cred, npols, policynames)) {
00763         lcmaps_log(0,"%s error: could not run plugin manager\n", logstr);
00764         goto fail_lcmaps_run_and_return_username;
00765     }
00766 
00767     /*
00768      * Apparently lcmaps succeeded, so let's get the username from the credential repository
00769      * and return happily
00770      */
00771 
00772     /* Now try to get the userid drom the credential data */
00773     uid    = getCredentialData(UID,     &cntUid);
00774     if (uid)
00775     {
00776         if ( (user_info = getpwuid(uid[0])) == NULL )
00777         {
00778             fprintf(stderr,"LCMAPS could not find the username related to uid: %d\n",uid[0]);
00779             return 1;
00780         }
00781         (*usernamep) = strdup(user_info->pw_name);
00782     }
00783     else
00784     {
00785         fprintf(stderr,"LCMAPS could not find any uid\n");
00786         return 1;
00787     }
00788 
00789     /* succes */
00790     lcmaps_release_cred(&lcmaps_cred);
00791     lcmaps_log_time(0,"%s: succeeded\n", logstr);
00792     return 0;
00793 
00794  fail_lcmaps_run_and_return_username:
00795     lcmaps_release_cred(&lcmaps_cred);
00796     lcmaps_log_time(0,"%s: failed\n", logstr);
00797     return 1;
00798 }
00799 
00800 
00801 /******************************************************************************
00802 Function:   lcmaps_run_and_return_poolindex
00803 Description:
00804     do the user mapping based on the provided list of policies (first successful
00805     policy found in the lcmaps policy file (lcmaps.db) will result in the user
00806     mapping) and return the poolindex
00807     This interface was intended to be used by a wrapper function for the 
00808         DYNAMIC ACCOUNTS SERVICE (DAS).
00809 
00810 Parameters:
00811     request     : RSL string
00812     user_cred   : user globus credential handle
00813     poolindexp  : pointer to poolindex (to be freed by calling application).
00814                   Note: poolindexp should be non-NULL at the start !
00815     npols       : number of policies to be considered for evaluation
00816     policynames : the names of the policies to be considered for evaluation
00817 
00818 Returns:
00819     0: mapping succeeded
00820     1: mapping failed
00821 ******************************************************************************/
00822 #if ALLOW_EMPTY_CREDENTIALS
00823 int lcmaps_run_and_return_poolindex(
00824         char * user_dn_tmp,
00825         gss_cred_id_t user_cred,
00826         lcmaps_request_t request,
00827         char ** poolindexp,
00828         int npols,
00829         char ** policynames
00830 )
00831 #else
00832 int lcmaps_run_and_return_poolindex(
00833         gss_cred_id_t user_cred,
00834         lcmaps_request_t request,
00835         char ** poolindexp,
00836         int npols,
00837         char ** policynames
00838 )
00839 #endif
00840 {
00841     char *           user_dn      = NULL;
00842     char *           poolindex    = NULL;
00843     char **          tmpptr       = NULL;
00844     int              cntpoolindex = -1;
00845     char *           logstr       = "lcmaps.mod-lcmaps_run_and_return_poolindex()";
00846     int              rc           = 0;
00847 
00848     if (lcmaps_initialized == 0)
00849     {
00850         fprintf(stderr,"LCMAPS has to be initialized first !\n");
00851         goto fail_lcmaps_run_and_return_poolindex;
00852     }
00853     lcmaps_log_time(LOG_NOTICE,"LCMAPS credential mapping request\n");
00854 
00855     lcmaps_log_debug(1, "Using lcmaps_run_and_return_poolindex interface of LCMAPS\n");
00856     if (poolindexp == NULL)
00857         goto fail_lcmaps_run_and_return_poolindex;
00858 
00859     *poolindexp = NULL;
00860 
00861     /*
00862      * Create lcmaps credential:
00863      *     fill it with the dn and extract it again
00864      */
00865     if ((rc = lcmaps_credential_init(&lcmaps_cred)) != 0)
00866     {
00867         if (rc == -1)
00868         {
00869             lcmaps_log(0, "%s error: lcmaps_cred does not exist\n", logstr);
00870             goto fail_lcmaps_run_and_return_poolindex;
00871         }
00872         else
00873         {
00874             lcmaps_log(0, "%s error: in initializing lcmaps_cred\n", logstr);
00875             goto fail_lcmaps_run_and_return_poolindex;
00876         }
00877     }
00878 #if ALLOW_EMPTY_CREDENTIALS
00879     if (lcmaps_credential_store_dn(user_dn_tmp, &lcmaps_cred) != 0)
00880     {
00881         lcmaps_log(0, "%s error: storing dn in lcmaps_cred\n");
00882         goto fail_lcmaps_run_and_return_poolindex;
00883     }
00884 #endif
00885     if ((rc = lcmaps_credential_store_gss_cred_id_t(user_cred, &lcmaps_cred)) != 0)
00886     {
00887         if (rc == -1)
00888         {
00889             lcmaps_log(0, "%s error: lcmaps_cred does not exist\n", logstr);
00890             goto fail_lcmaps_run_and_return_poolindex;
00891         }
00892         else if (rc == 1)
00893         {
00894             lcmaps_log(0, "%s error: cannot retrieve dn while storing gss_credential\n", logstr);
00895             goto fail_lcmaps_run_and_return_poolindex;
00896         }
00897         else if (rc == 2)
00898         {
00899             lcmaps_log(0, "%s: No FQANs found while storing gss_credential (non fatal)\n", logstr);
00900         }
00901         else if (rc == 3)
00902         {
00903             lcmaps_log(0, "%s error: Retrieving VOMS FQANs while storing gss_credential\n", logstr);
00904             goto fail_lcmaps_run_and_return_poolindex;
00905         }
00906         else
00907         {
00908             lcmaps_log(0, "%s error: storing gss_credential\n", logstr);
00909             goto fail_lcmaps_run_and_return_poolindex;
00910         }
00911     }
00912     user_dn = lcmaps_credential_get_dn(lcmaps_cred);
00913     if (user_dn == NULL)
00914     {
00915         lcmaps_log(0, "%s error: user DN empty\n", logstr);
00916         goto fail_lcmaps_run_and_return_poolindex;
00917     }
00918 
00919     /* Run PluginManager */
00920     if (runPluginManager(request, lcmaps_cred, npols, policynames)) {
00921         lcmaps_log(0,"%s error: could not run plugin manager\n", logstr);
00922         goto fail_lcmaps_run_and_return_poolindex;
00923     }
00924 
00925     /*
00926      * Apparently lcmaps succeeded, so let's get the poolindex from the credential repository
00927      * and return happily
00928      */
00929     tmpptr = (char **) getCredentialData(POOL_INDEX, &cntpoolindex);
00930     if (tmpptr && (cntpoolindex > 0))
00931     {
00932         poolindex = (char *)(*(tmpptr));
00933         lcmaps_log_debug(3,
00934             "%s: found %d poolindeces at address = %p\n",
00935             logstr, cntpoolindex, poolindex);
00936         /*
00937          * use lcmaps_log_a_string_debug() to get rid of the vsnprintf problems with
00938          * strings containing strange characters, such as %
00939          */
00940         lcmaps_log_a_string_debug(3,
00941             "lcmaps.mod-lcmaps_run_and_return_poolindex(): found this poolindex %s\n",
00942             poolindex);
00943         *poolindexp = strdup(poolindex);
00944     }
00945     else
00946     {
00947         lcmaps_log(0, "%s: LCMAPS could not find the poolindex\n", logstr);
00948         goto fail_lcmaps_run_and_return_poolindex;
00949     }
00950 
00951     /* succes */
00952     lcmaps_release_cred(&lcmaps_cred);
00953     lcmaps_log_time(0,"%s: succeeded\n", logstr);
00954     return 0;
00955 
00956  fail_lcmaps_run_and_return_poolindex:
00957     lcmaps_release_cred(&lcmaps_cred);
00958     lcmaps_log_time(0, "%s: failed\n", logstr);
00959     return 1;
00960 }
00961 #endif /* LCMAPS_GSI_MODE */
00962 /*
00963  * END OF THE GSI DEPENDENT INTERFACES
00964  */
00965 
00966 #endif /* LCMAPS_C */
00967 
00968 /******************************************************************************
00969 CVS Information:
00970     $Source: /cvs/jra1mw/org.glite.security.lcmaps/src/lcmaps.c,v $
00971     $Date: 2005/04/04 15:23:58 $
00972     $Revision: 1.18 $
00973     $Author: msteenba $
00974 ******************************************************************************/

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