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

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