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) 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  *     Oscar Koeroo <okoeroo@nikhef.nl>,
00014  *     Martijn Steenbakkers <martijn@nikhef.nl>,
00015  *     David Groep <davidg@nikhef.nl>,
00016  *     NIKHEF Amsterdam, the Netherlands
00017  */
00018 
00026 /*****************************************************************************
00027                             Include header files
00028 ******************************************************************************/
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <malloc.h>
00032 #include <string.h>
00033 
00034 #include "_lcmaps_cred_data.h"
00035 #include "lcmaps_log.h"
00036 #include "lcmaps_vo_data.h"
00037 
00038 
00039 /******************************************************************************
00040                           Module specific prototypes
00041 ******************************************************************************/
00042 static int compare_gids(const void *, const void *);
00043 
00044 
00045 /******************************************************************************
00046                        Define module specific variables
00047 ******************************************************************************/
00048 static cred_data_t credData =
00049 {
00050     (char *) NULL,                
00051     (uid_t *) NULL,               
00052     (gid_t *) NULL,               
00053     (gid_t *) NULL,               
00054     (lcmaps_vo_data_t *) NULL,    
00055     (char **) NULL,               
00056     (lcmaps_vo_mapping_t *) NULL, 
00057     0,                            
00058     0,                            
00059     0,                            
00060     0,                            
00061     0,                            
00062     0,                            
00063     (char *) NULL,                
00064 };
00065 
00066 /******************************************************************************
00067 Function:   addCredentialData
00068 Description:
00069     Add a credential to the list of found credentials (uids, gids etc)
00070 
00071 Parameters:
00072     datatype: type of credential
00073     data:     pointer to credential
00074 Returns:
00075     0:  success
00076     -1: failure (unknown data type, realloc error)
00077 ******************************************************************************/
00092 int addCredentialData(
00093         int datatype,
00094         void *data
00095 )
00096 {
00097     switch(datatype) 
00098     {
00099         case DN                    :
00100                           if (data)
00101                           {
00102                               credData.dn = strdup(*(char **)data);
00103                           }
00104                           break;
00105 
00106         case UID                   :
00107                           if (data)
00108                           {
00109                               credData.uid = (uid_t *) realloc(credData.uid, ((credData.cntUid+1) * sizeof(uid_t)));
00110                               credData.uid[credData.cntUid] = *((uid_t *)data);
00111                               credData.cntUid++;
00112                           }
00113                           break;
00114  
00115         case PRI_GID               :
00116                           if (data)
00117                           {
00118                               credData.cntPriGid++;
00119                               credData.priGid = (gid_t *) realloc(credData.priGid, (credData.cntPriGid * sizeof(gid_t)));
00120                               credData.priGid[credData.cntPriGid - 1] = *((gid_t *)data);
00121                           }
00122                           break;
00123 
00124         case SEC_GID               :
00125                           if (data)
00126                           {
00127                               int foundgid;
00128                               int igid = 0;
00129                               gid_t newgid;
00130 
00131                               newgid = *((gid_t *)data);
00132 
00133                               /* Check if gid is already stored in the list */
00134                               foundgid = 0;
00135                               for (igid = 0; igid < credData.cntSecGid; igid++)
00136                               {
00137                                   if (newgid == credData.secGid[igid])
00138                                   {
00139                                       foundgid = 1;
00140                                       break;
00141                                   }
00142                               }
00143                               if (foundgid) break;
00144 
00145                               /* New gid so increase cntSecGid by 1, reallocate the array and sort it */
00146                               credData.cntSecGid++;
00147                               credData.secGid = (gid_t *) realloc(credData.secGid, (credData.cntSecGid * sizeof(gid_t)));
00148                               credData.secGid[credData.cntSecGid - 1] = newgid;
00149 
00150                               /* sort the secondaries with qsort */
00151                               if (credData.cntSecGid > 1)
00152                               {
00153                                   qsort(
00154                                       credData.secGid,
00155                                       credData.cntSecGid,
00156                                       sizeof(gid_t),
00157                                       (int (*)(const void *,const void *))compare_gids
00158                                   );
00159                               }
00160                           }
00161                           break;
00162 
00163         case LCMAPS_VO_CRED        :
00164                           if (data)
00165                           {
00166                               credData.VoCred = (lcmaps_vo_data_t *) realloc(credData.VoCred, ((credData.cntVoCred+1) * sizeof(lcmaps_vo_data_t)));
00167                               lcmaps_copyVoData(&(credData.VoCred[credData.cntVoCred]), (lcmaps_vo_data_t *) data);
00168                               credData.cntVoCred++;
00169                           }
00170                           break;
00171 
00172         case LCMAPS_VO_CRED_STRING :
00173                           if (data)
00174                           {
00175                               credData.VoCredString = (char **) realloc(credData.VoCredString, ((credData.cntVoCredString+1) * sizeof(char *)));
00176                               credData.VoCredString[credData.cntVoCredString] = strdup(*((char **)data));
00177                               credData.cntVoCredString++;
00178                           }
00179                           break;
00180 
00181         case LCMAPS_VO_CRED_MAPPING:
00182                           if (data)
00183                           {
00184                               credData.VoCredMapping = (lcmaps_vo_mapping_t *) realloc(credData.VoCredMapping, ((credData.cntVoCredMapping+1) * sizeof(lcmaps_vo_mapping_t)));
00185                               lcmaps_copyVoMapping(&(credData.VoCredMapping[credData.cntVoCredMapping]), (lcmaps_vo_mapping_t *) data);
00186                               credData.cntVoCredMapping++;
00187                           }
00188                           break;
00189 
00190         case POOL_INDEX            :
00191                           if (data)
00192                           {
00193                               credData.pool_index = strdup(*(char **)data);
00194                           }
00195                           break;
00196 
00197 
00198 
00199         default         :
00200                           return -1;
00201     }
00202     return 0;
00203 }
00204 
00205 /******************************************************************************
00206 Function:   getCredentialData
00207 Description:
00208     Get pointer to a list of credential data of a certain type
00209 
00210 Parameters:
00211     datatype: type of credential
00212     count:    number of credentials found in list of datatype
00213 Returns:
00214     pointer to list of credential data or NULL in case of failure
00215 ******************************************************************************/
00228 void *getCredentialData(
00229         int datatype,
00230         int *count
00231 )
00232 {
00233     switch(datatype)
00234     {
00235         case DN:
00236             *count = 1;
00237             return &(credData.dn);
00238 
00239         case UID: 
00240             *count = credData.cntUid;
00241             return (credData.uid);
00242 
00243         case PRI_GID: 
00244             *count = credData.cntPriGid;
00245             return (credData.priGid);
00246 
00247         case SEC_GID:
00248             *count = credData.cntSecGid;
00249             return (credData.secGid);
00250 
00251         case LCMAPS_VO_CRED: 
00252             *count = credData.cntVoCred;
00253             return (credData.VoCred);
00254 
00255         case LCMAPS_VO_CRED_STRING: 
00256             *count = credData.cntVoCredString;
00257             return (credData.VoCredString);
00258 
00259         case LCMAPS_VO_CRED_MAPPING : 
00260             *count = credData.cntVoCredMapping;
00261             return (credData.VoCredMapping);
00262 
00263         case POOL_INDEX:
00264             *count = 1;
00265             return &(credData.pool_index);
00266 
00267 
00268         default         : return NULL;
00269     }
00270 }
00271 
00272 /******************************************************************************
00273 Function:   cleanCredentialData
00274 Description:
00275     Clean the credData structure
00276 
00277 Parameters: none
00278 Returns: 0
00279 ******************************************************************************/
00287 int cleanCredentialData()
00288 {
00289     int i = 0;
00290 
00291     for (i = 0; i < credData.cntVoCred; i++)
00292         lcmaps_cleanVoData(&(credData.VoCred[i]));
00293     for (i = 0; i < credData.cntVoCredString; i++)
00294         if (credData.VoCredString[i]) free(credData.VoCredString[i]);
00295     for (i = 0; i < credData.cntVoCredMapping; i++)
00296         lcmaps_cleanVoMapping(&(credData.VoCredMapping[i]));
00297 
00298     if (credData.dn)            free(credData.dn);
00299     if (credData.uid)           free(credData.uid);
00300     if (credData.priGid)        free(credData.priGid);
00301     if (credData.secGid)        free(credData.secGid);
00302     if (credData.VoCred)        free(credData.VoCred);
00303     if (credData.VoCredString)  free(credData.VoCredString);
00304     if (credData.VoCredMapping) free(credData.VoCredMapping);
00305     if (credData.pool_index)    free(credData.pool_index);
00306 
00307 
00308     credData.dn               = NULL;
00309     credData.uid              = NULL;
00310     credData.priGid           = NULL;
00311     credData.secGid           = NULL;
00312     credData.VoCred           = NULL;
00313     credData.VoCredString     = NULL;
00314     credData.VoCredMapping    = NULL;
00315     credData.pool_index       = NULL;
00316 
00317     credData.cntUid           = 0;
00318     credData.cntPriGid        = 0;
00319     credData.cntSecGid        = 0;
00320     credData.cntVoCred        = 0;
00321     credData.cntVoCredString  = 0;
00322     credData.cntVoCredMapping = 0;
00323 
00324     return 0;
00325 }
00326 
00327 /******************************************************************************
00328 Function:   printCredData
00329 Description:
00330     print out the credData structure
00331 
00332 Parameters:
00333     debug_level: the debug level
00334 Returns:
00335     nothing
00336 ******************************************************************************/
00345 void printCredData(int debug_level)
00346 {
00347     int i;
00348 
00349     lcmaps_log_debug(debug_level, "\nCredential Print:\n");
00350 
00351     if (credData.dn != NULL) 
00352         lcmaps_log_debug(debug_level, "dn                    : %s\n", credData.dn);
00353     for (i = 0; i < credData.cntUid; i++)
00354         lcmaps_log_debug(debug_level, "uid                   : %d  [%d/%d]\n", credData.uid[i], i+1, credData.cntUid);
00355     for (i = 0; i < credData.cntPriGid; i++)
00356         lcmaps_log_debug(debug_level, "pgid                  : %d  [%d/%d]\n", credData.priGid[i], i+1, credData.cntPriGid);
00357     for (i = 0; i < credData.cntSecGid; i++)
00358         lcmaps_log_debug(debug_level, "sgid                  : %d  [%d/%d]\n", credData.secGid[i], i+1, credData.cntSecGid);
00359     for (i = 0; i < credData.cntVoCred; i++)
00360     {
00361         lcmaps_log_debug(debug_level, "VO credential         :     [%d/%d]\n", i+1, credData.cntVoCred);
00362         lcmaps_printVoData(debug_level, &(credData.VoCred[i]));
00363     }
00364     for (i = 0; i < credData.cntVoCredString; i++)
00365         lcmaps_log_debug(debug_level, "VO credential string  : %s  [%d/%d]\n", credData.VoCredString[i], i+1, credData.cntVoCredString);
00366     for (i = 0; i < credData.cntVoCredMapping; i++)
00367     {
00368         lcmaps_log_debug(debug_level, "VO credential mapping :     [%d/%d]\n", i+1, credData.cntVoCredMapping);
00369         lcmaps_printVoMapping(debug_level, &(credData.VoCredMapping[i]));
00370     }
00371     if (credData.pool_index != NULL)
00372     {
00373         lcmaps_log_a_string_debug(debug_level, "pool_index            : %s\n", credData.pool_index);
00374     }
00375 }
00376 
00377 static int compare_gids(const void * pgid1, const void * pgid2)
00378 {
00379     gid_t gid1, gid2;
00380 
00381     if (pgid1 == NULL) return 0;
00382     if (pgid2 == NULL) return 0;
00383     
00384     gid1 = *((gid_t *) (pgid1));
00385     gid2 = *((gid_t *) (pgid2));
00386     if (gid1 < gid2)
00387         return -1;
00388     else if (gid1 > gid2)
00389         return 1;
00390     return 0;
00391 }
00392 
00393 
00394 /******************************************************************************
00395 CVS Information:
00396     $Source: /cvs/jra1mw/org.glite.security.lcmaps/src/pluginmanager/lcmaps_cred_data.c,v $
00397     $Date: 2004/12/02 17:14:22 $
00398     $Revision: 1.15 $
00399     $Author: msteenba $
00400 ******************************************************************************/

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