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

lcmaps_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 "lcmaps_vo_data.h"
00034 #include "lcmaps_log.h"
00035 
00036 /******************************************************************************
00037                                 Definitions
00038 ******************************************************************************/
00039 #define VO_DATA_WHITESPACE_CHARS " \t\n"
00040 
00041 /*
00042  * VO mapping section
00043  */
00044 /******************************************************************************
00045 Function:   lcmaps_createVoData
00046 Description:
00047     Create a VoData structure (store a VO, group, (subgroup,) role, capability
00048     combination). Allocate the memory. To be freed with lcmaps_deleteVoData().
00049 
00050 Parameters:
00051     vo: name of the VO
00052     group: name of the group
00053     subgroup: name of the subgroup (ignored for the moment)
00054     role: the role
00055     capability: the capability (whatever it is)
00056 Returns:
00057     pointer to the VoData structure or NULL
00058 ******************************************************************************/
00080 lcmaps_vo_data_t *
00081 lcmaps_createVoData(
00082     const char * vo,
00083     const char * group,
00084     const char * subgroup,
00085     const char * role,
00086     const char * capability
00087 )
00088 {
00089     lcmaps_vo_data_t * newVoData=NULL;
00090 
00091     newVoData = (lcmaps_vo_data_t *)malloc(sizeof(lcmaps_vo_data_t));
00092     if (!newVoData)
00093     {
00094         lcmaps_log(0,"lcmaps_createVoData(): error in malloc for new VoData structure\n");
00095         return NULL;
00096     }
00097 
00098     newVoData->vo = NULL;
00099     newVoData->group = NULL;
00100     newVoData->subgroup = NULL;
00101     newVoData->role = NULL;
00102     newVoData->capability = NULL;
00103 
00104     if (vo) newVoData->vo = strdup(vo);
00105     if (group) newVoData->group = strdup(group);
00106     if (subgroup) newVoData->subgroup = strdup(subgroup);
00107     if (role) newVoData->role = strdup(role);
00108     if (capability) newVoData->capability = strdup(capability);
00109 
00110     return newVoData;
00111 }
00112 
00113 /******************************************************************************
00114 Function:   lcmaps_deleteVoData
00115 Description:
00116     Delete a VoData structure that was previously created with lcmaps_createVoData().
00117     The pointer to the VoData structure is finally set to NULL;
00118 
00119 Parameters:
00120     vo_data: pointer to a pointer to a VoData structure
00121 
00122 Returns:
00123     0: success
00124     -1: failure (could not delete the structure or no structure found)
00125 ******************************************************************************/
00140 int
00141 lcmaps_deleteVoData(
00142     lcmaps_vo_data_t ** vo_data
00143 )
00144 {
00145     if (!vo_data) {
00146         lcmaps_log(0, "lcmaps_deleteVoData(): empty pointer as input !\n");
00147         return -1;
00148     }
00149 
00150     if ( (*vo_data) )
00151     {
00152         if ( (*vo_data)->vo) free( (*vo_data)->vo );
00153         if ( (*vo_data)->group) free( (*vo_data)->group );
00154         if ( (*vo_data)->subgroup) free( (*vo_data)->subgroup );
00155         if ( (*vo_data)->role) free( (*vo_data)->role );
00156         if ( (*vo_data)->capability) free( (*vo_data)->capability );
00157         free( (*vo_data) );
00158     }
00159     else
00160     {
00161         lcmaps_log_debug(2,"lcmaps_deleteVoData(): no lcmaps_vo_data_t found\n");
00162     }
00163     *vo_data=NULL;
00164     return 0;
00165 }
00166 
00167 /******************************************************************************
00168 Function:   lcmaps_cleanVoData
00169 Description:
00170     Clean a VoData structure that was previously filled with lcmaps_copyVoData().
00171     The contents are freed and set to zero.
00172 
00173 Parameters:
00174     vo_data: a pointer to a VoData structure
00175 
00176 Returns:
00177     0: success
00178     -1: failure (could not clean the structure or no structure found)
00179 ******************************************************************************/
00194 int
00195 lcmaps_cleanVoData(
00196     lcmaps_vo_data_t * vo_data
00197 )
00198 {
00199     if (!vo_data) {
00200         lcmaps_log(0, "lcmaps_cleanVoData():: no lcmaps_vo_data_t found\n");
00201         return -1;
00202     }
00203     else
00204     {
00205         if ( (vo_data)->vo)
00206         {
00207             free( (vo_data)->vo );
00208             vo_data->vo = NULL;
00209         }
00210         if ( (vo_data)->group) 
00211         {
00212             free( (vo_data)->group );
00213             vo_data->group = NULL;
00214         }
00215         if ( (vo_data)->subgroup) 
00216         {
00217             free( (vo_data)->subgroup );
00218             vo_data->subgroup = NULL;
00219         }
00220         if ( (vo_data)->role) 
00221         {
00222             free( (vo_data)->role );
00223             vo_data->role = NULL;
00224         }
00225         if ( (vo_data)->capability) 
00226         {
00227             free( (vo_data)->capability );
00228             vo_data->capability = NULL;
00229         }
00230     }
00231     return 0;
00232 }
00233 
00234 /******************************************************************************
00235 Function:   lcmaps_copyVoData
00236 Description:
00237     Copy a VoData structure into an empty VoData structure which has to exist.
00238 
00239 Parameters:
00240     dst_vo_data pointer to a empty VoData structure that should be filled
00241     src_vo_data pointer to the VoData structure that should be copied
00242 
00243 Returns:
00244     0: success
00245     -1: failure (either src_vo_data or dst_vo_data was empty)
00246 ******************************************************************************/
00262 int
00263 lcmaps_copyVoData(
00264     lcmaps_vo_data_t * dst_vo_data,
00265     const lcmaps_vo_data_t * src_vo_data
00266 )
00267 {
00268     if ( (dst_vo_data) && (src_vo_data) )
00269     {
00270         if (src_vo_data->vo)
00271             dst_vo_data->vo = strdup(src_vo_data->vo);
00272         else
00273             dst_vo_data->vo = NULL;
00274         if (src_vo_data->group)
00275             dst_vo_data->group = strdup(src_vo_data->group);
00276         else
00277             dst_vo_data->group = NULL;
00278         if (src_vo_data->subgroup)
00279             dst_vo_data->subgroup = strdup(src_vo_data->subgroup);
00280         else
00281             dst_vo_data->subgroup = NULL;
00282         if (src_vo_data->role)
00283             dst_vo_data->role = strdup(src_vo_data->role);
00284         else
00285             dst_vo_data->role = NULL;
00286         if (src_vo_data->capability)
00287             dst_vo_data->capability = strdup(src_vo_data->capability);
00288         else
00289             dst_vo_data->capability = NULL;
00290 
00291         return 0;
00292     }
00293     else
00294     {
00295         return -1;
00296     }
00297 }
00298 
00299 /******************************************************************************
00300 Function:   lcmaps_printVoData
00301 Description:
00302     Print the contents of a VoData structure
00303 
00304 Parameters:
00305     debug_level: debug_level for which the contents will be printed
00306     vo_data: pointer to a VoData structure
00307 
00308 Returns:
00309      0 (always)
00310 ******************************************************************************/
00323 int
00324 lcmaps_printVoData(
00325     int debug_level,
00326     const lcmaps_vo_data_t * vo_data
00327 )
00328 {
00329     if (vo_data)
00330     {
00331         lcmaps_log_debug(debug_level,"lcmaps_printVoData(): address of vo data struct: %p\n", vo_data);
00332         lcmaps_log_debug(debug_level,"lcmaps_printVoData():                        VO: %s\n", vo_data->vo);
00333         lcmaps_log_debug(debug_level,"lcmaps_printVoData():                     GROUP: %s\n", vo_data->group);
00334         lcmaps_log_debug(debug_level,"lcmaps_printVoData():                  SUBGROUP: %s\n", vo_data->subgroup);
00335         lcmaps_log_debug(debug_level,"lcmaps_printVoData():                      ROLE: %s\n", vo_data->role);
00336         lcmaps_log_debug(debug_level,"lcmaps_printVoData():                CAPABILITY: %s\n", vo_data->capability);
00337     }
00338     else
00339     {
00340         lcmaps_log_debug(debug_level,"lcmaps_printVoData(): empty pointer to vo data struct\n");
00341     }
00342     return 0;
00343 }
00344 
00345 /******************************************************************************
00346 Function:   lcmaps_stringVoData
00347 Description:
00348     Cast a VoData structure into a string
00349 
00350     The user of this function should create the buffer of size nchars beforehand.
00351     In buffer a string like the following will be written:
00352     "/VO=fred/GROUP=fred/flintstone/ROLE=director/CAPABILITY=destroy"
00353 
00354     Currently the SUBGROUP entry is ignored. Only if the information is present in the 
00355     VoData structure, it is added to the string.
00356     Both data for VO and GROUP are required (might change).
00357 
00358 
00359 Parameters:
00360     vo_data: pointer to a VoData structure
00361     buffer: pointer to character array of size nchars
00362     nchars: size of character array
00363 
00364 Returns:
00365     0: success
00366     -1: failure
00367 ******************************************************************************/
00391 int
00392 lcmaps_stringVoData(
00393     const lcmaps_vo_data_t * vo_data,
00394     char * buffer,
00395     int nchars
00396 )
00397 {
00398     int totalchars;
00399     char * strptr=NULL;
00400     char * bufptr=NULL;
00401     int    buflen=0;
00402 
00403     bufptr=buffer;
00404     buflen=nchars;
00405 
00406     /* write "VO=" */
00407     if ( vo_data->vo == NULL )
00408     {
00409         lcmaps_log(0,"lcmaps_stringVoData(): error no VO found\n");
00410         return -1;
00411     }
00412     /* Skip over leading whitespace */
00413     strptr=vo_data->vo;
00414     strptr += strspn(strptr, VO_DATA_WHITESPACE_CHARS);
00415     if ( (*strptr!='\0') && (strncmp(strptr,"NULL",4)) )
00416     {
00417         /* Convention for solaris 2.8 and glibc-2.1 and higher:
00418          * totalchars is the number of chars written (excluding \0) if enough space had been 
00419          * available.
00420          * negative: error
00421          */
00422         totalchars=snprintf(bufptr,(size_t)buflen,"/VO=%s",strptr);
00423         if ( (totalchars+1) > buflen )
00424         {
00425             lcmaps_log(0,"lcmaps_stringVoData(): could not write all characters into buffer for VO\n");
00426             lcmaps_log(0,"lcmaps_stringVoData(): excess of characters: %d\n",totalchars+1-buflen);
00427             return -1;
00428         }
00429         else if ( totalchars < 0 )
00430         {
00431             lcmaps_log(0,"lcmaps_stringVoData(): error in snprintf()\n");
00432             return -1;
00433         }
00434         else
00435         {
00436             bufptr+=totalchars;
00437             buflen-=totalchars;
00438         }
00439     }
00440     else
00441     {
00442         lcmaps_log(0,"lcmaps_stringVoData(): error no VO found\n");
00443         return -1;
00444     }
00445 
00446     /* write "GROUP=" */
00447     if ( vo_data->group == NULL )
00448     {
00449         lcmaps_log(0,"lcmaps_stringVoData(): error no VO-group found\n");
00450         return -1;
00451     }
00452     /* Skip over leading whitespace */
00453     strptr=vo_data->group;
00454     strptr += strspn(strptr, VO_DATA_WHITESPACE_CHARS);
00455     if ( (*strptr!='\0') && (strncmp(strptr,"NULL",4)) )
00456     {
00457         totalchars=snprintf(bufptr,(size_t)buflen,"/GROUP=%s",strptr);
00458         if ( (totalchars+1) > buflen )
00459         {
00460             lcmaps_log(0,"lcmaps_stringVoData(): could not write all characters into buffer for GROUP\n");
00461             lcmaps_log(0,"lcmaps_stringVoData(): excess of characters: %d\n",totalchars+1-buflen);
00462             return -1;
00463         }
00464         else if ( totalchars < 0 )
00465         {
00466             lcmaps_log(0,"lcmaps_stringVoData(): error in snprintf()\n");
00467             return -1;
00468         }
00469         else
00470         {
00471             bufptr+=totalchars;
00472             buflen-=totalchars;
00473         }
00474     }
00475     else
00476     {
00477         lcmaps_log(0,"lcmaps_stringVoData(): error no VO-group found\n");
00478         return -1;
00479     }
00480 
00481     /* Skip subgroup for the moment (not clear how VOMS will evolve in this respect) */
00482 
00483     /* write "ROLE=" */
00484     if ( vo_data->role != NULL )
00485     {
00486         /* Skip over leading whitespace */
00487         strptr=vo_data->role;
00488         strptr += strspn(strptr, VO_DATA_WHITESPACE_CHARS);
00489         if ( (*strptr!='\0') && (strncmp(strptr,"NULL",4)) )
00490         {
00491             totalchars=snprintf(bufptr,(size_t)buflen,"/ROLE=%s",strptr);
00492             if ( (totalchars+1) > buflen )
00493             {
00494                 lcmaps_log(0,"lcmaps_stringVoData(): could not write all characters into buffer for ROLE\n");
00495                 lcmaps_log(0,"lcmaps_stringVoData(): excess of characters: %d\n",totalchars+1-buflen);
00496                 return -1;
00497             }
00498             else if ( totalchars < 0 )
00499             {
00500                 lcmaps_log(0,"lcmaps_stringVoData(): error in snprintf()\n");
00501                 return -1;
00502             }
00503             else
00504             {
00505                 bufptr+=totalchars;
00506                 buflen-=totalchars;
00507             }
00508         }
00509     }
00510 
00511     /* write "CAPABILITY=" */
00512     if ( vo_data->capability != NULL )
00513     {
00514         /* Skip over leading whitespace */
00515         strptr=vo_data->capability;
00516         strptr += strspn(strptr, VO_DATA_WHITESPACE_CHARS);
00517         if ( (*strptr!='\0') && (strncmp(strptr,"NULL",4)) )
00518         {
00519             totalchars=snprintf(bufptr,(size_t)buflen,"/CAPABILITY=%s",strptr);
00520             if ( (totalchars+1) > buflen )
00521             {
00522                 lcmaps_log(0,"lcmaps_stringVoData(): could not write all characters into buffer for CAPABILITY\n");
00523                 lcmaps_log(0,"lcmaps_stringVoData(): excess of characters: %d\n",totalchars+1-buflen);
00524                 return -1;
00525             }
00526             else if ( totalchars < 0 )
00527             {
00528                 lcmaps_log(0,"lcmaps_stringVoData(): error in snprintf()\n");
00529                 return -1;
00530             }
00531             else
00532             {
00533                 bufptr+=totalchars;
00534                 buflen-=totalchars;
00535             }
00536         }
00537     }
00538     return 0;
00539 }
00540 
00541 /*
00542  * VO mapping section
00543  */
00544 /******************************************************************************
00545 Function:   lcmaps_createVoMapping
00546 Description:
00547     Create a VoMapping structure store the VO information string (or FQAN) in combination
00548     with the corresponding gid and groupname.
00549     Allocate the memory. To be freed with lcmaps_deleteVoMapping().
00550 
00551 Parameters:
00552     vo_data_string: the VO information string (or FQAN)
00553     groupname:      the (POSIX) group name
00554     gid:            the (POSIX) gid
00555 Returns:
00556     pointer to the VoMapping structure or NULL
00557 ******************************************************************************/
00576 lcmaps_vo_mapping_t *
00577 lcmaps_createVoMapping(
00578     const char * vo_data_string,
00579     const char * groupname,
00580     const gid_t  gid
00581 )
00582 {
00583     lcmaps_vo_mapping_t * newVoMapping=NULL;
00584 
00585     newVoMapping = (lcmaps_vo_mapping_t *)malloc(sizeof(lcmaps_vo_mapping_t));
00586     if (!newVoMapping)
00587     {
00588         lcmaps_log(0,"lcmaps_createVoMapping(): error in malloc for new VoMapping structure\n");
00589         return NULL;
00590     }
00591 
00592     newVoMapping->vostring  = NULL;
00593     newVoMapping->groupname = NULL;
00594     newVoMapping->gid       = LCMAPS_NO_GID;
00595 
00596     if (vo_data_string) newVoMapping->vostring = strdup(vo_data_string);
00597     if (vo_data_string) newVoMapping->groupname = strdup(groupname);
00598     if (gid) newVoMapping->gid = gid;
00599 
00600     return newVoMapping;
00601 }
00602 
00603 /******************************************************************************
00604 Function:   lcmaps_deleteVoMapping
00605 Description:
00606     Delete a VoMapping structure that was previously created with lcmaps_createVoMapping().
00607     The pointer to the VoMapping structure is finally set to NULL;
00608 
00609 Parameters:
00610     vo_mapping: pointer to a pointer to a VoMapping structure
00611 
00612 Returns:
00613     0: success
00614     -1: failure (could not delete the structure or no structure found)
00615 ******************************************************************************/
00630 int
00631 lcmaps_deleteVoMapping(
00632     lcmaps_vo_mapping_t ** vo_mapping
00633 )
00634 {
00635     if (!vo_mapping) {
00636         lcmaps_log(0, "lcmaps_deleteVoMapping(): empty pointer as input !\n");
00637         return -1;
00638     }
00639 
00640     if ( (*vo_mapping) )
00641     {
00642         if ( (*vo_mapping)->vostring) free( (*vo_mapping)->vostring );
00643         if ( (*vo_mapping)->groupname) free( (*vo_mapping)->groupname );
00644         free( (*vo_mapping) );
00645     }
00646     else
00647     {
00648         lcmaps_log_debug(2,"lcmaps_deleteVoMapping(): no lcmaps_vo_mapping_t found\n");
00649     }
00650     *vo_mapping=NULL;
00651     return 0;
00652 }
00653 
00654 /******************************************************************************
00655 Function:   lcmaps_cleanVoMapping
00656 Description:
00657     Clean a VoMapping structure that was previously filled with lcmaps_copyVoMapping().
00658     The contents are freed and set to zero.
00659 
00660 Parameters:
00661     vo_mapping: a pointer to a VoMapping structure
00662 
00663 Returns:
00664     0: success
00665     -1: failure (could not clean the structure or no structure found)
00666 ******************************************************************************/
00681 int
00682 lcmaps_cleanVoMapping(
00683     lcmaps_vo_mapping_t * vo_mapping
00684 )
00685 {
00686     if (!vo_mapping) {
00687         lcmaps_log(0, "lcmaps_cleanVoMapping():: no lcmaps_vo_mapping_t found\n");
00688         return -1;
00689     }
00690     else
00691     {
00692         if ( (vo_mapping)->vostring)
00693         {
00694             free( (vo_mapping)->vostring );
00695             vo_mapping->vostring = NULL;
00696         }
00697 
00698         if ( (vo_mapping)->groupname)
00699         {
00700             free( (vo_mapping)->groupname );
00701             vo_mapping->groupname = NULL;
00702         }
00703     }
00704     return 0;
00705 }
00706 
00707 /******************************************************************************
00708 Function:   lcmaps_copyVoMapping
00709 Description:
00710     Copy a VoMapping structure into an empty VoMapping structure which has to exist.
00711 
00712 Parameters:
00713     dst_vo_mapping pointer to a empty VoMapping structure that should be filled
00714     src_vo_mapping pointer to the VoMapping structure that should be copied
00715 
00716 Returns:
00717     0: success
00718     -1: failure (either src_vo_mapping or dst_vo_mapping was empty)
00719 ******************************************************************************/
00735 int
00736 lcmaps_copyVoMapping(
00737     lcmaps_vo_mapping_t * dst_vo_mapping,
00738     const lcmaps_vo_mapping_t * src_vo_mapping
00739 )
00740 {
00741     if ( (dst_vo_mapping) && (src_vo_mapping) )
00742     {
00743         dst_vo_mapping->gid = src_vo_mapping->gid;
00744         if (src_vo_mapping->vostring)
00745             dst_vo_mapping->vostring = strdup(src_vo_mapping->vostring);
00746         else
00747             dst_vo_mapping->vostring = NULL;
00748 
00749         if (src_vo_mapping->groupname)
00750             dst_vo_mapping->groupname = strdup(src_vo_mapping->groupname);
00751         else
00752             dst_vo_mapping->groupname = NULL;
00753 
00754         return 0;
00755     }
00756     else
00757     {
00758         return -1;
00759     }
00760 }
00761 
00762 /******************************************************************************
00763 Function:   lcmaps_printVoMapping
00764 Description:
00765     Print the contents of a VoMapping structure
00766 
00767 Parameters:
00768     debug_level: debug_level for which the contents will be printed
00769     vo_mapping:  pointer to a VoMapping structure
00770 
00771 Returns:
00772      0 (always)
00773 ******************************************************************************/
00786 int
00787 lcmaps_printVoMapping(
00788     int debug_level,
00789     const lcmaps_vo_mapping_t * vo_mapping
00790 )
00791 {
00792     if (vo_mapping)
00793     {
00794         lcmaps_log_debug(debug_level,"lcmaps_printVoMapping(): address of vo mapping struct: %p\n", vo_mapping);
00795         lcmaps_log_debug(debug_level,"lcmaps_printVoMapping():                    VO string: %s\n", vo_mapping->vostring);
00796         lcmaps_log_debug(debug_level,"lcmaps_printVoMapping():             mapped groupname: %s\n", vo_mapping->groupname);
00797         lcmaps_log_debug(debug_level,"lcmaps_printVoMapping():                   mapped GID: %d\n", (int)vo_mapping->gid);
00798     }
00799     else
00800     {
00801         lcmaps_log_debug(debug_level,"lcmaps_printVoMapping(): empty pointer to vo mapping struct\n");
00802     }
00803     return 0;
00804 }
00805 
00806 /******************************************************************************
00807 CVS Information:
00808     $Source: /cvs/fabric_mgt/gridification/lcmaps/src/pluginmanager/lcmaps_vo_data.c,v $
00809     $Date: 2004/01/05 16:44:10 $
00810     $Revision: 1.6 $
00811     $Author: martijn $
00812 ******************************************************************************/

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