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

lcmaps_cred_data.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  *     Oscar Koeroo <okoeroo@nikhef.nl>,
00007  *     Martijn Steenbakkers <martijn@nikhef.nl>,
00008  *     David Groep <davidg@nikhef.nl>,
00009  *     NIKHEF Amsterdam, the Netherlands
00010  */
00011 
00019 /*****************************************************************************
00020                             Include header files
00021 ******************************************************************************/
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <malloc.h>
00025 #include <string.h>
00026 
00027 #include "_lcmaps_cred_data.h"
00028 #include "lcmaps_log.h"
00029 #include "lcmaps_vo_data.h"
00030 
00031 
00032 /******************************************************************************
00033                           Module specific prototypes
00034 ******************************************************************************/
00035 static int compare_gids(const void *, const void *);
00036 
00037 
00038 /******************************************************************************
00039                        Define module specific variables
00040 ******************************************************************************/
00041 static cred_data_t credData =
00042 {
00043     (char *) NULL,                
00044     (uid_t *) NULL,               
00045     (gid_t *) NULL,               
00046     (gid_t *) NULL,               
00047     (lcmaps_vo_data_t *) NULL,    
00048     (char **) NULL,               
00049     (lcmaps_vo_mapping_t *) NULL, 
00050     0,                            
00051     0,                            
00052     0,                            
00053     0,                            
00054     0,                            
00055     0,                            
00056 };
00057 
00058 /******************************************************************************
00059 Function:   addCredentialData
00060 Description:
00061     Add a credential to the list of found credentials (uids, gids etc)
00062 
00063 Parameters:
00064     datatype: type of credential
00065     data:     pointer to credential
00066 Returns:
00067     0:  success
00068     -1: failure (unknown data type, realloc error)
00069 ******************************************************************************/
00084 int addCredentialData(
00085         int datatype,
00086         void *data
00087 )
00088 {
00089     switch(datatype) 
00090     {
00091         case DN                    :
00092                           if (data)
00093                           {
00094                               credData.dn = strdup(*(char **)data);
00095                           }
00096                           break;
00097 
00098         case UID                   :
00099                           if (data)
00100                           {
00101                               credData.uid = (uid_t *) realloc(credData.uid, ((credData.cntUid+1) * sizeof(uid_t)));
00102                               credData.uid[credData.cntUid] = *((uid_t *)data);
00103                               credData.cntUid++;
00104                           }
00105                           break;
00106  
00107         case PRI_GID               :
00108                           if (data)
00109                           {
00110                               credData.cntPriGid++;
00111                               credData.priGid = (gid_t *) realloc(credData.priGid, (credData.cntPriGid * sizeof(gid_t)));
00112                               credData.priGid[credData.cntPriGid - 1] = *((gid_t *)data);
00113                           }
00114                           break;
00115 
00116         case SEC_GID               :
00117                           if (data)
00118                           {
00119                               int foundgid;
00120                               int igid = 0;
00121                               gid_t newgid;
00122 
00123                               newgid = *((gid_t *)data);
00124 
00125                               /* Check if gid is already stored in the list */
00126                               foundgid = 0;
00127                               for (igid = 0; igid < credData.cntSecGid; igid++)
00128                               {
00129                                   if (newgid == credData.secGid[igid])
00130                                   {
00131                                       foundgid = 1;
00132                                       break;
00133                                   }
00134                               }
00135                               if (foundgid) break;
00136 
00137                               /* New gid so increase cntSecGid by 1, reallocate the array and sort it */
00138                               credData.cntSecGid++;
00139                               credData.secGid = (gid_t *) realloc(credData.secGid, (credData.cntSecGid * sizeof(gid_t)));
00140                               credData.secGid[credData.cntSecGid - 1] = newgid;
00141 
00142                               /* sort the secondaries with qsort */
00143                               if (credData.cntSecGid > 1)
00144                               {
00145                                   qsort(
00146                                       credData.secGid,
00147                                       credData.cntSecGid,
00148                                       sizeof(gid_t),
00149                                       (int (*)(const void *,const void *))compare_gids
00150                                   );
00151                               }
00152                           }
00153                           break;
00154 
00155         case LCMAPS_VO_CRED        :
00156                           if (data)
00157                           {
00158                               credData.VoCred = (lcmaps_vo_data_t *) realloc(credData.VoCred, ((credData.cntVoCred+1) * sizeof(lcmaps_vo_data_t)));
00159                               lcmaps_copyVoData(&(credData.VoCred[credData.cntVoCred]), (lcmaps_vo_data_t *) data);
00160                               credData.cntVoCred++;
00161                           }
00162                           break;
00163 
00164         case LCMAPS_VO_CRED_STRING :
00165                           if (data)
00166                           {
00167                               credData.VoCredString = (char **) realloc(credData.VoCredString, ((credData.cntVoCredString+1) * sizeof(char *)));
00168                               credData.VoCredString[credData.cntVoCredString] = strdup(*((char **)data));
00169                               credData.cntVoCredString++;
00170                           }
00171                           break;
00172 
00173         case LCMAPS_VO_CRED_MAPPING:
00174                           if (data)
00175                           {
00176                               credData.VoCredMapping = (lcmaps_vo_mapping_t *) realloc(credData.VoCredMapping, ((credData.cntVoCredMapping+1) * sizeof(lcmaps_vo_mapping_t)));
00177                               lcmaps_copyVoMapping(&(credData.VoCredMapping[credData.cntVoCredMapping]), (lcmaps_vo_mapping_t *) data);
00178                               credData.cntVoCredMapping++;
00179                           }
00180                           break;
00181 
00182         default         :
00183                           return -1;
00184     }
00185     return 0;
00186 }
00187 
00188 /******************************************************************************
00189 Function:   getCredentialData
00190 Description:
00191     Get pointer to a list of credential data of a certain type
00192 
00193 Parameters:
00194     datatype: type of credential
00195     count:    number of credentials found in list of datatype
00196 Returns:
00197     pointer to list of credential data or NULL in case of failure
00198 ******************************************************************************/
00211 void *getCredentialData(
00212         int datatype,
00213         int *count
00214 )
00215 {
00216     switch(datatype)
00217     {
00218         case DN:
00219             *count = 1;
00220             return &(credData.dn);
00221 
00222         case UID: 
00223             *count = credData.cntUid;
00224             return (credData.uid);
00225 
00226         case PRI_GID: 
00227             *count = credData.cntPriGid;
00228             return (credData.priGid);
00229 
00230         case SEC_GID:
00231             *count = credData.cntSecGid;
00232             return (credData.secGid);
00233 
00234         case LCMAPS_VO_CRED: 
00235             *count = credData.cntVoCred;
00236             return (credData.VoCred);
00237 
00238         case LCMAPS_VO_CRED_STRING: 
00239             *count = credData.cntVoCredString;
00240             return (credData.VoCredString);
00241 
00242         case LCMAPS_VO_CRED_MAPPING : 
00243             *count = credData.cntVoCredMapping;
00244             return (credData.VoCredMapping);
00245 
00246         default         : return NULL;
00247     }
00248 }
00249 
00250 /******************************************************************************
00251 Function:   cleanCredentialData
00252 Description:
00253     Clean the credData structure
00254 
00255 Parameters: none
00256 Returns: 0
00257 ******************************************************************************/
00265 int cleanCredentialData()
00266 {
00267     int i = 0;
00268 
00269     for (i = 0; i < credData.cntVoCred; i++)
00270         lcmaps_cleanVoData(&(credData.VoCred[i]));
00271     for (i = 0; i < credData.cntVoCredString; i++)
00272         if (credData.VoCredString[i]) free(credData.VoCredString[i]);
00273     for (i = 0; i < credData.cntVoCredMapping; i++)
00274         lcmaps_cleanVoMapping(&(credData.VoCredMapping[i]));
00275 
00276     if (credData.dn)            free(credData.dn);
00277     if (credData.uid)           free(credData.uid);
00278     if (credData.priGid)        free(credData.priGid);
00279     if (credData.secGid)        free(credData.secGid);
00280     if (credData.VoCred)        free(credData.VoCred);
00281     if (credData.VoCredString)  free(credData.VoCredString);
00282     if (credData.VoCredMapping) free(credData.VoCredMapping);
00283 
00284     credData.dn               = NULL;
00285     credData.uid              = NULL;
00286     credData.priGid           = NULL;
00287     credData.secGid           = NULL;
00288     credData.VoCred           = NULL;
00289     credData.VoCredString     = NULL;
00290     credData.VoCredMapping    = NULL;
00291 
00292     credData.cntUid           = 0;
00293     credData.cntPriGid        = 0;
00294     credData.cntSecGid        = 0;
00295     credData.cntVoCred        = 0;
00296     credData.cntVoCredString  = 0;
00297     credData.cntVoCredMapping = 0;
00298 
00299     return 0;
00300 }
00301 
00302 /******************************************************************************
00303 Function:   printCredData
00304 Description:
00305     print out the credData structure
00306 
00307 Parameters:
00308     debug_level: the debug level
00309 Returns:
00310     nothing
00311 ******************************************************************************/
00320 void printCredData(int debug_level)
00321 {
00322     int i;
00323 
00324     lcmaps_log_debug(debug_level, "\nCredential Print:\n");
00325 
00326     if (credData.dn != NULL) 
00327         lcmaps_log_debug(debug_level, "dn                    : %s\n", credData.dn);
00328     for (i = 0; i < credData.cntUid; i++)
00329         lcmaps_log_debug(debug_level, "uid                   : %d  [%d/%d]\n", credData.uid[i], i+1, credData.cntUid);
00330     for (i = 0; i < credData.cntPriGid; i++)
00331         lcmaps_log_debug(debug_level, "pgid                  : %d  [%d/%d]\n", credData.priGid[i], i+1, credData.cntPriGid);
00332     for (i = 0; i < credData.cntSecGid; i++)
00333         lcmaps_log_debug(debug_level, "sgid                  : %d  [%d/%d]\n", credData.secGid[i], i+1, credData.cntSecGid);
00334     for (i = 0; i < credData.cntVoCred; i++)
00335     {
00336         lcmaps_log_debug(debug_level, "VO credential         :     [%d/%d]\n", i+1, credData.cntVoCred);
00337         lcmaps_printVoData(debug_level, &(credData.VoCred[i]));
00338     }
00339     for (i = 0; i < credData.cntVoCredString; i++)
00340         lcmaps_log_debug(debug_level, "VO credential string  : %s  [%d/%d]\n", credData.VoCredString[i], i+1, credData.cntVoCredString);
00341     for (i = 0; i < credData.cntVoCredMapping; i++)
00342     {
00343         lcmaps_log_debug(debug_level, "VO credential mapping :     [%d/%d]\n", i+1, credData.cntVoCredMapping);
00344         lcmaps_printVoMapping(debug_level, &(credData.VoCredMapping[i]));
00345     }
00346 }
00347 
00348 static int compare_gids(const void * pgid1, const void * pgid2)
00349 {
00350     gid_t gid1, gid2;
00351 
00352     if (pgid1 == NULL) return 0;
00353     if (pgid2 == NULL) return 0;
00354     
00355     gid1 = *((gid_t *) (pgid1));
00356     gid2 = *((gid_t *) (pgid2));
00357     if (gid1 < gid2)
00358         return -1;
00359     else if (gid1 > gid2)
00360         return 1;
00361     return 0;
00362 }
00363 
00364 
00365 /******************************************************************************
00366 CVS Information:
00367     $Source: /cvs/fabric_mgt/gridification/lcmaps/src/pluginmanager/lcmaps_cred_data.c,v $
00368     $Date: 2004/01/05 16:44:10 $
00369     $Revision: 1.11 $
00370     $Author: martijn $
00371 ******************************************************************************/

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