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

lcas_vo_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  *     Martijn Steenbakkers <martijn@nikhef.nl>,
00014  *     David Groep <davidg@nikhef.nl>,
00015  *     NIKHEF Amsterdam, the Netherlands
00016  */
00017 
00031 /******************************************************************************
00032                              Include header files
00033 ******************************************************************************/
00034 
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <malloc.h>
00038 #include <string.h>
00039 
00040 #include "lcas_vo_data.h"
00041 #include "lcas_log.h"
00042 
00043 /******************************************************************************
00044                                 Definitions
00045 ******************************************************************************/
00046 #define VO_DATA_WHITESPACE_CHARS " \t\n"
00047 
00048 /******************************************************************************
00049 Function:   lcas_createVoData
00050 Description:
00051     Create a VoData structure (store a VO, group, (subgroup,) role, capability
00052     combination). Allocate the memory. To be freed with lcas_deleteVoData().
00053 
00054 Parameters:
00055     vo: name of the VO
00056     group: name of the group
00057     subgroup: name of the subgroup (ignored for the moment)
00058     role: the role
00059     capability: the capability (whatever it is)
00060 Returns:
00061     pointer to the VoData structure or NULL
00062 ******************************************************************************/
00084 lcas_vo_data_t *
00085 lcas_createVoData(
00086     const char * vo,
00087     const char * group,
00088     const char * subgroup,
00089     const char * role,
00090     const char * capability
00091 )
00092 {
00093     lcas_vo_data_t * newVoData=NULL;
00094 
00095     newVoData = (lcas_vo_data_t *)malloc(sizeof(lcas_vo_data_t));
00096     if (!newVoData)
00097     {
00098         lcas_log(0,"lcas_createVoData(): error in malloc for new VoData structure\n");
00099         return NULL;
00100     }
00101 
00102     newVoData->vo = NULL;
00103     newVoData->group = NULL;
00104     newVoData->subgroup = NULL;
00105     newVoData->role = NULL;
00106     newVoData->capability = NULL;
00107 
00108     if (vo) newVoData->vo = strdup(vo);
00109     if (group) newVoData->group = strdup(group);
00110     if (subgroup) newVoData->subgroup = strdup(subgroup);
00111     if (role) newVoData->role = strdup(role);
00112     if (capability) newVoData->capability = strdup(capability);
00113 
00114     return newVoData;
00115 }
00116 
00117 /******************************************************************************
00118 Function:   lcas_deleteVoData
00119 Description:
00120     Delete a VoData structure that was previously created with lcas_createVoData().
00121     The pointer to the VoData structure is finally set to NULL;
00122 
00123 Parameters:
00124     vo_data: pointer to a pointer to a VoData structure
00125 
00126 Returns:
00127     0: success
00128     -1: failure (could not delete the structure or no structure found)
00129 ******************************************************************************/
00144 int
00145 lcas_deleteVoData(
00146     lcas_vo_data_t ** vo_data
00147 )
00148 {
00149     if (!vo_data) {
00150         lcas_log(0, "lcas_deleteVoData(): empty pointer as input !\n");
00151         return -1;
00152     }
00153 
00154     if ( (*vo_data) )
00155     {
00156         if ( (*vo_data)->vo) free( (*vo_data)->vo );
00157         if ( (*vo_data)->group) free( (*vo_data)->group );
00158         if ( (*vo_data)->subgroup) free( (*vo_data)->subgroup );
00159         if ( (*vo_data)->role) free( (*vo_data)->role );
00160         if ( (*vo_data)->capability) free( (*vo_data)->capability );
00161         free( (*vo_data) );
00162     }
00163     else
00164     {
00165         lcas_log_debug(2,"lcas_deleteVoData(): no lcas_vo_data_t found\n");
00166     }
00167     *vo_data=NULL;
00168     return 0;
00169 }
00170 
00171 /******************************************************************************
00172 Function:   lcas_cleanVoData
00173 Description:
00174     Clean a VoData structure that was previously filled with lcas_copyVoData().
00175     The contents are freed and set to zero.
00176 
00177 Parameters:
00178     vo_data: a pointer to a VoData structure
00179 
00180 Returns:
00181     0: success
00182     -1: failure (could not clean the structure or no structure found)
00183 ******************************************************************************/
00198 int
00199 lcas_cleanVoData(
00200     lcas_vo_data_t * vo_data
00201 )
00202 {
00203     if (!vo_data) {
00204         lcas_log(0, "lcas_cleanVoData():: no lcas_vo_data_t found\n");
00205         return -1;
00206     }
00207     else
00208     {
00209         if ( (vo_data)->vo)
00210         {
00211             free( (vo_data)->vo );
00212             vo_data->vo = NULL;
00213         }
00214         if ( (vo_data)->group) 
00215         {
00216             free( (vo_data)->group );
00217             vo_data->group = NULL;
00218         }
00219         if ( (vo_data)->subgroup) 
00220         {
00221             free( (vo_data)->subgroup );
00222             vo_data->subgroup = NULL;
00223         }
00224         if ( (vo_data)->role) 
00225         {
00226             free( (vo_data)->role );
00227             vo_data->role = NULL;
00228         }
00229         if ( (vo_data)->capability) 
00230         {
00231             free( (vo_data)->capability );
00232             vo_data->capability = NULL;
00233         }
00234     }
00235     return 0;
00236 }
00237 
00238 /******************************************************************************
00239 Function:   lcas_copyVoData
00240 Description:
00241     Copy a VoData structure into an empty VoData structure which has to exist.
00242 
00243 Parameters:
00244     dst_vo_data pointer to a empty VoData structure that should be filled
00245     src_vo_data pointer to the VoData structure that should be copied
00246 
00247 Returns:
00248     0: success
00249     -1: failure (either src_vo_data or dst_vo_data was empty)
00250 ******************************************************************************/
00266 int
00267 lcas_copyVoData(
00268     lcas_vo_data_t * dst_vo_data,
00269     const lcas_vo_data_t * src_vo_data
00270 )
00271 {
00272     if ( (dst_vo_data) && (src_vo_data) )
00273     {
00274         if (src_vo_data->vo)
00275             dst_vo_data->vo = strdup(src_vo_data->vo);
00276         else
00277             dst_vo_data->vo = NULL;
00278         if (src_vo_data->group)
00279             dst_vo_data->group = strdup(src_vo_data->group);
00280         else
00281             dst_vo_data->group = NULL;
00282         if (src_vo_data->subgroup)
00283             dst_vo_data->subgroup = strdup(src_vo_data->subgroup);
00284         else
00285             dst_vo_data->subgroup = NULL;
00286         if (src_vo_data->role)
00287             dst_vo_data->role = strdup(src_vo_data->role);
00288         else
00289             dst_vo_data->role = NULL;
00290         if (src_vo_data->capability)
00291             dst_vo_data->capability = strdup(src_vo_data->capability);
00292         else
00293             dst_vo_data->capability = NULL;
00294 
00295         return 0;
00296     }
00297     else
00298     {
00299         return -1;
00300     }
00301 }
00302 
00303 /******************************************************************************
00304 Function:   lcas_printVoData
00305 Description:
00306     Print the contents of a VoData structure
00307 
00308 Parameters:
00309     debug_level: debug_level for which the contents will be printed
00310     vo_data: pointer to a VoData structure
00311 
00312 Returns:
00313      0 (always)
00314 ******************************************************************************/
00327 int
00328 lcas_printVoData(
00329     int debug_level,
00330     const lcas_vo_data_t * vo_data
00331 )
00332 {
00333     if (vo_data)
00334     {
00335         lcas_log_debug(debug_level,"lcas_printVoData(): address of vo data struct: %p\n", vo_data);
00336         lcas_log_debug(debug_level,"lcas_printVoData():                        VO: %s\n", vo_data->vo);
00337         lcas_log_debug(debug_level,"lcas_printVoData():                     GROUP: %s\n", vo_data->group);
00338         lcas_log_debug(debug_level,"lcas_printVoData():                  SUBGROUP: %s\n", vo_data->subgroup);
00339         lcas_log_debug(debug_level,"lcas_printVoData():                      ROLE: %s\n", vo_data->role);
00340         lcas_log_debug(debug_level,"lcas_printVoData():                CAPABILITY: %s\n", vo_data->capability);
00341     }
00342     else
00343     {
00344         lcas_log_debug(debug_level,"lcas_printVoData(): empty pointer to vo data struct\n");
00345     }
00346     return 0;
00347 }
00348 
00349 /******************************************************************************
00350 Function:   lcas_stringVoData
00351 Description:
00352     Cast a VoData structure into a string
00353 
00354     The user of this function should create the buffer of size nchars beforehand.
00355     In buffer a string like the following will be written:
00356     "/VO=fred/GROUP=fred/flintstone/ROLE=director/CAPABILITY=destroy"
00357 
00358     Currently the SUBGROUP entry is ignored. Only if the information is present in the 
00359     VoData structure, it is added to the string.
00360     Both data for VO and GROUP are required (might change).
00361 
00362 
00363 Parameters:
00364     vo_data: pointer to a VoData structure
00365     buffer: pointer to character array of size nchars
00366     nchars: size of character array
00367 
00368 Returns:
00369     0: success
00370     -1: failure
00371 ******************************************************************************/
00395 int
00396 lcas_stringVoData(
00397     const lcas_vo_data_t * vo_data,
00398     char * buffer,
00399     int nchars
00400 )
00401 {
00402     int totalchars;
00403     char * strptr=NULL;
00404     char * bufptr=NULL;
00405     int    buflen=0;
00406 
00407     bufptr=buffer;
00408     buflen=nchars;
00409 
00410     /* write "VO=" */
00411     /* Skip over leading whitespace */
00412     if ( (strptr=lcas_parseVostring(vo_data->vo)) )
00413     {
00414         /* Convention for solaris 2.8 and glibc-2.1 and higher:
00415          * totalchars is the number of chars written (excluding \0) if enough space had been 
00416          * available.
00417          * negative: error
00418          */
00419         totalchars=snprintf(bufptr,(size_t)buflen,"/VO=%s",strptr);
00420         if ( (totalchars+1) > buflen )
00421         {
00422             lcas_log(0,"lcas_stringVoData(): could not write all characters into buffer for VO\n");
00423             lcas_log(0,"lcas_stringVoData(): excess of characters: %d\n",totalchars+1-buflen);
00424             return -1;
00425         }
00426         else if ( totalchars < 0 )
00427         {
00428             lcas_log(0,"lcas_stringVoData(): error in snprintf()\n");
00429             return -1;
00430         }
00431         else
00432         {
00433             bufptr+=totalchars;
00434             buflen-=totalchars;
00435         }
00436     }
00437     else
00438     {
00439         lcas_log(0,"lcas_stringVoData(): error no VO found\n");
00440         return -1;
00441     }
00442 
00443     /* write "GROUP=" */
00444     /* Skip over leading whitespace */
00445     if ( (strptr=lcas_parseVostring(vo_data->group)) )
00446     {
00447         totalchars=snprintf(bufptr,(size_t)buflen,"/GROUP=%s",strptr);
00448         if ( (totalchars+1) > buflen )
00449         {
00450             lcas_log(0,"lcas_stringVoData(): could not write all characters into buffer for GROUP\n");
00451             lcas_log(0,"lcas_stringVoData(): excess of characters: %d\n",totalchars+1-buflen);
00452             return -1;
00453         }
00454         else if ( totalchars < 0 )
00455         {
00456             lcas_log(0,"lcas_stringVoData(): error in snprintf()\n");
00457             return -1;
00458         }
00459         else
00460         {
00461             bufptr+=totalchars;
00462             buflen-=totalchars;
00463         }
00464     }
00465     else
00466     {
00467         lcas_log(0,"lcas_stringVoData(): error no VO-group found\n");
00468         return -1;
00469     }
00470 
00471     /* Skip subgroup for the moment (not clear how VOMS will evolve in this respect) */
00472 
00473     /* write "ROLE=" */
00474     /* Skip over leading whitespace */
00475     if ( (strptr=lcas_parseVostring(vo_data->role)) )
00476     {
00477         {
00478             totalchars=snprintf(bufptr,(size_t)buflen,"/ROLE=%s",strptr);
00479             if ( (totalchars+1) > buflen )
00480             {
00481                 lcas_log(0,"lcas_stringVoData(): could not write all characters into buffer for ROLE\n");
00482                 lcas_log(0,"lcas_stringVoData(): excess of characters: %d\n",totalchars+1-buflen);
00483                 return -1;
00484             }
00485             else if ( totalchars < 0 )
00486             {
00487                 lcas_log(0,"lcas_stringVoData(): error in snprintf()\n");
00488                 return -1;
00489             }
00490             else
00491             {
00492                 bufptr+=totalchars;
00493                 buflen-=totalchars;
00494             }
00495         }
00496     }
00497 
00498     /* write "CAPABILITY=" */
00499     /* Skip over leading whitespace */
00500     if ( (strptr=lcas_parseVostring(vo_data->capability)) )
00501     {
00502         {
00503             totalchars=snprintf(bufptr,(size_t)buflen,"/CAPABILITY=%s",strptr);
00504             if ( (totalchars+1) > buflen )
00505             {
00506                 lcas_log(0,"lcas_stringVoData(): could not write all characters into buffer for CAPABILITY\n");
00507                 lcas_log(0,"lcas_stringVoData(): excess of characters: %d\n",totalchars+1-buflen);
00508                 return -1;
00509             }
00510             else if ( totalchars < 0 )
00511             {
00512                 lcas_log(0,"lcas_stringVoData(): error in snprintf()\n");
00513                 return -1;
00514             }
00515             else
00516             {
00517                 bufptr+=totalchars;
00518                 buflen-=totalchars;
00519             }
00520         }
00521     }
00522     return 0;
00523 }
00524 
00525 /******************************************************************************
00526 Function:   lcas_parseVostring
00527 Description:
00528     Strip leading whitespace and check if string != "NULL"
00529 
00530     This function is needed because VOMS server fills user credential sometimes
00531     with strings like "   NULL", which is a valid string, but the intention is that the
00532     data is empty. A string like this is translated into a NULL pointer by this function.
00533     
00534 Parameters:
00535     vo_string: string of VO credential
00536 
00537 Returns:
00538     pointer to the parsed string or NULL
00539 ******************************************************************************/
00554 char *
00555 lcas_parseVostring(
00556     char * vo_string
00557 )
00558 {
00559     char * strptr = NULL;
00560 
00561     if (vo_string == NULL) return NULL;
00562 
00563     strptr=vo_string;
00564     strptr += strspn(strptr, VO_DATA_WHITESPACE_CHARS);
00565     if ( (*strptr!='\0') && (strncmp(strptr,"NULL",4)) )
00566     {
00567         return strptr;
00568     }
00569     else
00570     {
00571         return NULL;
00572     }
00573 }
00574 
00575 
00576 /******************************************************************************
00577 CVS Information:
00578     $Source: /cvs/jra1mw/org.glite.security.lcas-plugins-voms/src/voms/lcas_vo_data.c,v $
00579     $Date: 2004/10/14 16:05:28 $
00580     $Revision: 1.4 $
00581     $Author: msteenba $
00582 ******************************************************************************/

Generated on Fri May 27 18:10:48 2005 for lcas by doxygen 1.3.5