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, (gid_t *) NULL, (gid_t *) NULL,
00045     (lcmaps_vo_data_t *) NULL, (char **) NULL,
00046     0, 0, 0, 0, 0,
00047 };
00048 
00049 /******************************************************************************
00050 Function:   addCredentialData
00051 Description:
00052     Add a credential to the list of found credentials (uids, gids etc)
00053 
00054 Parameters:
00055     datatype: type of credential
00056     data:     pointer to credential
00057 Returns:
00058     0:  success
00059     -1: failure (unknown data type, realloc error)
00060 ******************************************************************************/
00075 int addCredentialData(
00076         int datatype,
00077         void *data
00078 )
00079 {
00080     switch(datatype) 
00081     {
00082         case DN                    :
00083                           if (data)
00084                           {
00085                               credData.dn = strdup(*(char **)data);
00086                           }
00087                           break;
00088 
00089         case UID                   :
00090                           if (data)
00091                           {
00092                               credData.uid = (uid_t *) realloc(credData.uid, ((credData.cntUid+1) * sizeof(uid_t)));
00093                               credData.uid[credData.cntUid] = *((uid_t *)data);
00094                               credData.cntUid++;
00095                           }
00096                           break;
00097  
00098         case PRI_GID               :
00099                           if (data)
00100                           {
00101                               credData.cntPriGid++;
00102                               credData.priGid = (gid_t *) realloc(credData.priGid, (credData.cntPriGid * sizeof(gid_t)));
00103                               credData.priGid[credData.cntPriGid - 1] = *((gid_t *)data);
00104                           }
00105                           break;
00106 
00107         case SEC_GID               :
00108                           if (data)
00109                           {
00110                               int foundgid;
00111                               int igid = 0;
00112                               gid_t newgid;
00113 
00114                               newgid = *((gid_t *)data);
00115 
00116                               /* Check if gid is already stored in the list */
00117                               foundgid = 0;
00118                               for (igid = 0; igid < credData.cntSecGid; igid++)
00119                               {
00120                                   if (newgid == credData.secGid[igid])
00121                                   {
00122                                       foundgid = 1;
00123                                       break;
00124                                   }
00125                               }
00126                               if (foundgid) break;
00127 
00128                               /* New gid so increase cntSecGid by 1, reallocate the array and sort it */
00129                               credData.cntSecGid++;
00130                               credData.secGid = (gid_t *) realloc(credData.secGid, (credData.cntSecGid * sizeof(gid_t)));
00131                               credData.secGid[credData.cntSecGid - 1] = newgid;
00132 
00133                               /* sort the secondaries with qsort */
00134                               if (credData.cntSecGid > 1)
00135                               {
00136                                   qsort(
00137                                       credData.secGid,
00138                                       credData.cntSecGid,
00139                                       sizeof(gid_t),
00140                                       (int (*)(const void *,const void *))compare_gids
00141                                   );
00142                               }
00143                           }
00144                           break;
00145 
00146         case LCMAPS_VO_CRED        :
00147                           if (data)
00148                           {
00149                               credData.VoCred = (lcmaps_vo_data_t *) realloc(credData.VoCred, ((credData.cntVoCred+1) * sizeof(lcmaps_vo_data_t)));
00150                               lcmaps_copyVoData(&(credData.VoCred[credData.cntVoCred]), (lcmaps_vo_data_t *) data);
00151                               credData.cntVoCred++;
00152                           }
00153                           break;
00154 
00155         case LCMAPS_VO_CRED_STRING :
00156                           if (data)
00157                           {
00158                               credData.VoCredString = (char **) realloc(credData.VoCredString, ((credData.cntVoCredString+1) * sizeof(char *)));
00159                               credData.VoCredString[credData.cntVoCredString] = strdup(*((char **)data));
00160                               credData.cntVoCredString++;
00161                           }
00162                           break;
00163 
00164         default         :
00165                           return -1;
00166     }
00167     return 0;
00168 }
00169 
00170 /******************************************************************************
00171 Function:   getCredentialData
00172 Description:
00173     Get pointer to a list of credential data of a certain type
00174 
00175 Parameters:
00176     datatype: type of credential
00177     count:    number of credentials found in list of datatype
00178 Returns:
00179     pointer to list of credential data or NULL in case of failure
00180 ******************************************************************************/
00193 void *getCredentialData(
00194         int datatype,
00195         int *count
00196 )
00197 {
00198     switch(datatype)
00199     {
00200         case DN         : *count = 1;
00201                           return &(credData.dn);
00202 
00203         case UID        : *count = credData.cntUid;
00204                           return (credData.uid);
00205 
00206         case PRI_GID    : *count = credData.cntPriGid;
00207                           return (credData.priGid);
00208 
00209         case SEC_GID    : *count = credData.cntSecGid;
00210                           return (credData.secGid);
00211 
00212         case LCMAPS_VO_CRED : *count = credData.cntVoCred;
00213                           return (credData.VoCred);
00214 
00215         case LCMAPS_VO_CRED_STRING : *count = credData.cntVoCredString;
00216                           return (credData.VoCredString);
00217 
00218         default         : return NULL;
00219     }
00220 }
00221 
00222 /******************************************************************************
00223 Function:   cleanCredentialData
00224 Description:
00225     Clean the credData structure
00226 
00227 Parameters: none
00228 Returns: 0
00229 ******************************************************************************/
00237 int cleanCredentialData()
00238 {
00239     int i = 0;
00240 
00241     for (i = 0; i < credData.cntVoCredString; i++)
00242         if (credData.VoCredString[i]) free(credData.VoCredString[i]);
00243     for (i = 0; i < credData.cntVoCred; i++)
00244         lcmaps_cleanVoData(&(credData.VoCred[i]));
00245 
00246     free(credData.dn);
00247     free(credData.uid);
00248     free(credData.priGid);
00249     free(credData.secGid);
00250     free(credData.VoCredString);
00251     free(credData.VoCred);
00252 
00253     credData.dn              = NULL;
00254     credData.uid             = NULL;
00255     credData.priGid          = NULL;
00256     credData.secGid          = NULL;
00257     credData.VoCredString    = NULL;
00258     credData.VoCred          = NULL;
00259 
00260     credData.cntUid          = 0;
00261     credData.cntPriGid       = 0;
00262     credData.cntSecGid       = 0;
00263     credData.cntVoCredString = 0;
00264     credData.cntVoCred       = 0;
00265 
00266     return 0;
00267 }
00268 
00269 /******************************************************************************
00270 Function:   printCredData
00271 Description:
00272     print out the credData structure
00273 
00274 Parameters:
00275     debug_level: the debug level
00276 Returns:
00277     nothing
00278 ******************************************************************************/
00287 void printCredData(int debug_level)
00288 {
00289     int i;
00290 
00291     lcmaps_log_debug(debug_level, "\nCredential Print:\n");
00292 
00293     if (credData.dn != NULL) 
00294         lcmaps_log_debug(debug_level, "dn                   : %s\n", credData.dn);
00295     for (i = 0; i < credData.cntUid; i++)
00296         lcmaps_log_debug(debug_level, "uid                  : %d  [%d/%d]\n", credData.uid[i], i+1, credData.cntUid);
00297     for (i = 0; i < credData.cntPriGid; i++)
00298         lcmaps_log_debug(debug_level, "pgid                 : %d  [%d/%d]\n", credData.priGid[i], i+1, credData.cntPriGid);
00299     for (i = 0; i < credData.cntSecGid; i++)
00300         lcmaps_log_debug(debug_level, "sgid                 : %d  [%d/%d]\n", credData.secGid[i], i+1, credData.cntSecGid);
00301     for (i = 0; i < credData.cntVoCredString; i++)
00302         lcmaps_log_debug(debug_level, "VO credential string : %s  [%d/%d]\n", credData.VoCredString[i], i+1, credData.cntVoCredString);
00303     for (i = 0; i < credData.cntVoCred; i++)
00304     {
00305         lcmaps_log_debug(debug_level, "VO credential        :     [%d/%d]\n", i+1, credData.cntVoCred);
00306         lcmaps_printVoData(debug_level, &(credData.VoCred[i]));
00307     }
00308 }
00309 
00310 static int compare_gids(const void * pgid1, const void * pgid2)
00311 {
00312     gid_t gid1, gid2;
00313 
00314     if (pgid1 == NULL) return 0;
00315     if (pgid2 == NULL) return 0;
00316     
00317     gid1 = *((gid_t *) (pgid1));
00318     gid2 = *((gid_t *) (pgid2));
00319     if (gid1 < gid2)
00320         return -1;
00321     else if (gid1 > gid2)
00322         return 1;
00323     return 0;
00324 }
00325 
00326 
00327 /******************************************************************************
00328 CVS Information:
00329     $Source: /cvs/fabric_mgt/gridification/lcmaps/src/pluginmanager/lcmaps_cred_data.c,v $
00330     $Date: 2003/07/30 17:10:24 $
00331     $Revision: 1.9 $
00332     $Author: martijn $
00333 ******************************************************************************/

Generated at Tue Sep 23 15:48:08 2003 for edg-lcmaps by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001