00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00024 
00025 
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 
00042 
00043 
00044 
00045 
00046 
00047 static int    fexist(char *);
00048 
00049 
00050 
00051 
00052 
00053 
00054 
00055 
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 
00120 
00121 
00122 
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 
00159 
00160 
00161 
00162 
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 
00197 
00198 
00199 
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 
00252 
00253 
00254 
00255 
00256 
00257 
00258 
00259 
00260 
00261 
00262 
00263 
00264 
00265 
00266 
00267 
00268 
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 
00308     i = 0;
00309     maxargs = *n;
00310 
00311     cp = command;
00312     while (*cp)
00313     {
00314     
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 
00348 
00349             i++;
00350             if (i >= maxargs)
00351             {
00352                 i--;
00353                 *n = i;
00354                 return(-2); 
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 
00374 
00375 
00376 
00377 
00378 
00379 
00380 
00381 
00382 
00383 
00384 
00385 
00386 
00387 
00388 
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     
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         
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     
00466     endgrent();
00467     return 0;
00468 }
00469 
00470 
00471 
00472 
00473 
00474 
00475