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

lcmaps_utils.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 
00024 /*****************************************************************************
00025                             Include header files
00026 ******************************************************************************/
00027 #include <stdlib.h>
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <sys/types.h>
00031 #include <sys/stat.h>
00032 #include <unistd.h>
00033 #include <errno.h>
00034 #include <stdarg.h>
00035 #include <grp.h>
00036 #include "lcmaps_defines.h"
00037 #include "lcmaps_types.h"
00038 #include "lcmaps_log.h"
00039 
00040 /******************************************************************************
00041                              Define constants
00042 ******************************************************************************/
00043 
00044 /******************************************************************************
00045                           Module specific prototypes
00046 ******************************************************************************/
00047 static int    fexist(char *);
00048 
00049 /******************************************************************************
00050 Function:       lcmaps_genfilename() (copied from GLOBUS gatekeeper.c)
00051 Description:    generate an absolute file name given a starting prefix,
00052                 a relative or absolute path, and a suffix
00053                 Only use prefix if path is relative.
00054 Parameters:
00055 Returns:        a pointer to a string which could be freeded.
00056 ******************************************************************************/
00076 char * lcmaps_genfilename(
00077         char * prefixp,
00078         char * pathp,
00079         char * suffixp
00080 )
00081 {
00082     char * newfilename;
00083     int    prefixl, pathl, suffixl;
00084     char * prefix,  * path, * suffix;
00085 
00086     prefix = (prefixp) ? prefixp : "";
00087     path   = (pathp) ? pathp : "";
00088     suffix  = (suffixp) ? suffixp : "";
00089 
00090     prefixl = strlen(prefix);
00091     pathl   =  strlen(path);
00092     suffixl  =  strlen(suffix); 
00093 
00094     newfilename = (char *) calloc(1, (prefixl + pathl + suffixl + 3));
00095     if (newfilename) 
00096     {
00097         if (*path != '/')
00098         {
00099             strcat(newfilename, prefix);
00100             if ((prefixl != 0) && (prefix[prefixl-1] != '/'))
00101             {
00102                 strcat(newfilename, "/");
00103             }
00104         }
00105         strcat(newfilename, path);
00106         if ((pathl  != 0) &&
00107             (suffixl != 0) && 
00108             (path[pathl-1] != '/') && 
00109              suffix[0] != '/')
00110         {
00111             strcat(newfilename, "/");
00112         }
00113         strcat(newfilename, suffix);
00114     }
00115     return newfilename;
00116 }
00117 
00118 /******************************************************************************
00119 Function:       fexist()
00120 Description:    check the existence of file corresponding to <path>
00121 Parameters:     path
00122 Returns:        1, if file exists
00123 ******************************************************************************/
00133 static int fexist(
00134         char * path
00135 )
00136 {
00137   struct stat sbuf;
00138   int res;
00139   
00140   if(!path || !*path) return 0;
00141 
00142   res=stat(path,&sbuf);
00143   if (res)
00144   {
00145       if (errno==ENOENT)
00146       {
00147           return 0;
00148       }
00149       else
00150       {
00151           return -1;
00152       }
00153   }
00154   return 1;
00155 }
00156 
00157 /******************************************************************************
00158 Function:       lcmaps_getfexist()
00159 Description:    picks the first existing file in argument list
00160 Parameters:     n   : number of paths,
00161                 ... : list of paths
00162 Returns:        returns filename found or NULL
00163 ******************************************************************************/
00176 char * lcmaps_getfexist(
00177         int n,
00178         ...
00179 )
00180 {
00181   va_list pvar;
00182   int i;
00183   char *cfilenm=NULL;
00184 
00185   va_start(pvar, n);
00186 
00187   for(i=0;i<n;i++) {
00188     cfilenm=va_arg(pvar,char*);
00189     if(*cfilenm) if(fexist(cfilenm)) return cfilenm;
00190   }
00191   va_end(pvar);
00192   return NULL;
00193 }
00194 
00195 /******************************************************************************
00196 Function:       lcmaps_findfile()
00197 Description:    Checks for file in standard directories
00198 Parameters:     name
00199 Returns:        returns filename found (should be freeed) or NULL
00200 ******************************************************************************/
00218 char * lcmaps_findfile(
00219         char * name
00220 )
00221 {
00222     char * newname=NULL;
00223     char * tmpname=NULL;
00224     char * names[6]={NULL,NULL,NULL,NULL,NULL,NULL};
00225     int    i;
00226 
00227     names[0]=lcmaps_genfilename(NULL,name,NULL);
00228     names[1]=lcmaps_genfilename(getenv("LCMAPS_MODULES_DIR"),name,NULL);
00229     names[2]=lcmaps_genfilename("modules",name,NULL);
00230     names[3]=lcmaps_genfilename(LCMAPS_ETC_HOME,name,NULL);
00231     names[4]=lcmaps_genfilename(LCMAPS_MOD_HOME,name,NULL);
00232     names[5]=lcmaps_genfilename(LCMAPS_LIB_HOME,name,NULL);
00233 
00234     tmpname=lcmaps_getfexist(6,names[0],
00235                       names[1],names[2],
00236                       names[3],names[4],names[5]);
00237     if (tmpname != NULL)
00238         newname=strdup(tmpname);
00239     else
00240         newname=NULL;
00241 
00242     for (i=0; i < 6; i++)
00243     {
00244         if (names[i] != NULL) free(names[i]);
00245     }
00246 
00247     return newname;
00248 }
00249 
00250 /******************************************************************************
00251 Function:   lcmaps_tokenize() (in modified form from globus_gatekeeper_utils.c)
00252 
00253 Description:
00254     Breakup the command in to args, pointing the args array
00255     at the tokens. Replace white space at the end of each
00256     token with a null. A token maybe in quotes. 
00257 
00258 Parameters:
00259     command: The command line to be parsed.
00260     args:    A pointer to an array of pointers to be filled it
00261     n:       Size of the array, on input, and set to size used on output. 
00262     sep:     string of seperating characters
00263 
00264 Returns:
00265     0 on success. 
00266     -1 on to malloc
00267     -2 on to many args
00268     -3 on quote not matched
00269 ******************************************************************************/
00292 int lcmaps_tokenize(
00293         const char * command, 
00294         char ** args,
00295         int * n,
00296         char * sep
00297     )
00298 {
00299     int maxargs;
00300     int i;
00301     const char * cp;
00302     const char * pp;
00303     const char * qp;
00304     char ** arg;
00305 
00306     arg = args;
00307 /*    i = *n - 1; */
00308     i = 0;
00309     maxargs = *n;
00310 
00311     cp = command;
00312     while (*cp)
00313     {
00314     /* skip leading sep characters */
00315         while (*cp && strchr(sep, *cp))
00316         {
00317             cp++;
00318         }
00319         pp = NULL;
00320         if (*cp == '\"')
00321         {
00322             cp++;
00323             pp = cp;
00324             if ((qp = strchr(cp,'\"')) == NULL)
00325             {
00326                 *n = i;
00327                 return -3;
00328             }
00329             cp = qp + 1;
00330         }
00331         else if (*cp)
00332         {
00333             pp = cp;
00334             if ((qp = strpbrk(cp,sep)) == NULL)
00335             {
00336                 qp = strchr(cp,'\0');
00337             }
00338             cp = qp;
00339         }
00340         else
00341         {
00342             continue;
00343         }
00344         if (pp)
00345         {
00346             /*
00347              * fill at most maxargs-1 arguments; let the last one point to NULL
00348              */
00349             i++;
00350             if (i >= maxargs)
00351             {
00352                 i--;
00353                 *n = i;
00354                 return(-2); /* too many args */
00355             }
00356             *arg = (char*)malloc((qp - pp) + 1);
00357             if (*arg == NULL)
00358             {
00359                 i--;
00360                 *n = i;
00361                 return -1;
00362             }
00363             memcpy(*arg,pp,qp - pp);
00364             *(*arg + (qp - pp)) = '\0';
00365             arg++;
00366         }
00367     }
00368     *arg = NULL;
00369     *n = i;
00370     return(0);
00371 }
00372 /******************************************************************************
00373 Function:   lcmaps_get_gidlist()
00374 
00375 Description:
00376     Finds the list of gids for user in the group file (/etc/group)
00377     Returns a list of gid_t which should be freed by calling program.
00378 
00379 Parameters:
00380     username:   the name of the user
00381     ngroups:    ptr to int which will be filled with the number of gids.
00382     group_list: ptr to an array of gid_t.
00383 
00384 Returns:
00385     0 on success. 
00386     -1 on realloc failure
00387     -2 on getgrent failure
00388     1 on failure
00389 ******************************************************************************/
00407 int lcmaps_get_gidlist(
00408         const char * username,
00409         int * ngroups,
00410         gid_t ** group_list
00411     )
00412 {
00413     struct group        * group_info = NULL;
00414     gid_t               * groups = NULL;
00415     gid_t               * newgroups = NULL;
00416     int                   i = 0;
00417 
00418     /* rewind the file pointer to the beginning of the /etc/group file */
00419     setgrent();
00420 
00421     lcmaps_log_debug(2,"\tlcmaps_get_gidlist(): looping through group file\n");
00422     *ngroups = 0;
00423     while ( ( group_info = getgrent() ) )
00424     {
00425         char ** pgr_mem = group_info->gr_mem;
00426         char *  gr_mem = NULL;
00427 
00428         lcmaps_log_debug(4,"\tlcmaps_get_gidlist(): group %s\n", group_info->gr_name);
00429         while ( (gr_mem = *pgr_mem) )
00430         {
00431             lcmaps_log_debug(4,"\tlcmaps_get_gidlist(): \tgroup member %s\n", gr_mem);
00432             if (strncmp(username, gr_mem, strlen(username))==0)
00433             {
00434                 lcmaps_log_debug(2,"\tlcmaps_get_gidlist(): \t\tfound group %s for %s\n",
00435                     group_info->gr_name, username);
00436                 (*ngroups)++;
00437                 newgroups = (gid_t *) realloc(groups, ((*ngroups) * sizeof(gid_t)));
00438                 if (newgroups == NULL)
00439                 {
00440                     lcmaps_log(0,"lcmaps_get_gidlist(): cannot realloc\n");
00441                     free(groups);
00442                     return -1;
00443                 }
00444                 groups=newgroups;
00445                 groups[(*ngroups)-1] = group_info->gr_gid;
00446             }
00447             ++pgr_mem;
00448         }
00449     }
00450     if (errno==ENOMEM)
00451     {
00452         lcmaps_log(0,"lcmaps_get_gidlist(): Cannot read the group file, %s\n", strerror(errno));
00453         free(groups);
00454         groups=NULL;
00455         /* Close the group file */
00456         endgrent();
00457         return -2;
00458     }
00459     *group_list=groups;
00460     lcmaps_log_debug(4,"\tlcmaps_get_gidlist(): %d groups found for %s\n", *ngroups, username);
00461     for (i = 0; i < *ngroups; i++)
00462     {
00463         lcmaps_log_debug(4,"\tlcmaps_get_gidlist(): group nr %d ==> gid_t %d\n", i+1, groups[i]);
00464     }
00465     /* Close the group file */
00466     endgrent();
00467     return 0;
00468 }
00469 /******************************************************************************
00470 CVS Information:
00471     $Source: /cvs/jra1mw/org.glite.security.lcmaps/src/pluginmanager/lcmaps_utils.c,v $
00472     $Date: 2005/02/27 01:30:41 $
00473     $Revision: 1.10 $
00474     $Author: msteenba $
00475 ******************************************************************************/

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