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

Generated at Tue Sep 23 15:06:52 2003 for edg-lcas by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001