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

lcas_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 <gssapi.h>
00036 #include "lcas_defines.h"
00037 #include "lcas_types.h"
00038 
00039 /******************************************************************************
00040                              Define constants
00041 ******************************************************************************/
00042 
00043 /******************************************************************************
00044                           Module specific prototypes
00045 ******************************************************************************/
00046 static char * cred_to_dn(gss_cred_id_t);
00047 static int    fexist(char *);
00048 
00049 
00050 /******************************************************************************
00051 Function:       lcas_fill_cred()
00052 Description:    Fill cedential from dn and globus credential
00053 Parameters:
00054                 dn: distinguished name
00055                 cred: globus credential
00056                 plcas_cred: pointer to lcas_credential
00057 Returns:        0: succes
00058                 1: failure
00059 ******************************************************************************/
00060 int lcas_fill_cred(
00061         char * dn,
00062         gss_cred_id_t cred,
00063         lcas_cred_id_t * plcas_cred
00064 )
00065 {
00066     /* Copy the credential */
00067     plcas_cred->cred = cred;
00068 
00069     if (cred == GSS_C_NO_CREDENTIAL) /* Empty credential: Fill DN with passed dn */
00070     {
00071         plcas_cred->dn = strdup(dn);
00072     }
00073     else
00074     {
00075         plcas_cred->dn = cred_to_dn(cred);
00076     }
00077     if (plcas_cred->dn == NULL)
00078         return 1; /* Cannot find user dn */
00079     return 0;
00080 }
00081 
00082 /******************************************************************************
00083 Function:       lcas_release_cred()
00084 Description:    release lcas credential
00085 Parameters:
00086                 plcas_cred: pointer to lcas_credential
00087 Returns:        0: succes
00088                 1: failure
00089 ******************************************************************************/
00090 int lcas_release_cred(
00091         lcas_cred_id_t * plcas_cred
00092 )
00093 {
00094     if (plcas_cred == NULL)
00095         return 0;
00096 
00097     if (plcas_cred->dn != NULL)
00098         free(plcas_cred->dn);
00099     /* Don't release globus credential (not copied) */
00100 
00101     return 0;
00102 }
00103 
00104 /******************************************************************************
00105 Function:       lcas_get_dn()
00106 Description:    returns user dn
00107 Parameters:
00108                 lcas_credential: lcas_credential
00109 Returns:        user dn
00110 ******************************************************************************/
00111 char * lcas_get_dn(
00112         lcas_cred_id_t lcas_credential
00113 )
00114 {
00115     return (lcas_credential.dn);
00116 }
00117 
00118 /******************************************************************************
00119 Function:       lcas_get_gss_cred()
00120 Description:    returns globus gss credential
00121 Parameters:
00122                 lcas_credential: lcas_credential
00123 Returns:        globus gss credential
00124 ******************************************************************************/
00125 gss_cred_id_t lcas_get_gss_cred(
00126         lcas_cred_id_t lcas_credential
00127 )
00128 {
00129     return (lcas_credential.cred);
00130 }
00131 
00132 /******************************************************************************
00133 Function:       cred_to_dn() (copied from GLOBUS gatekeeper.c)
00134 Description:    Get the globusid from gssapi
00135 Parameters:
00136                 globus_cred: globus credential
00137 Returns:        globusid string (which could be freeed)
00138 ******************************************************************************/
00150 static char * cred_to_dn(
00151         gss_cred_id_t globus_cred
00152 )
00153 {
00154     char*                         globusid = NULL;
00155     char*                         globusid_tmp = NULL;
00156     gss_name_t                    globus_name = GSS_C_NO_NAME;
00157     gss_buffer_desc               globus_buffer_desc = GSS_C_EMPTY_BUFFER;
00158     gss_buffer_t                  globus_buffer = &globus_buffer_desc;
00159     OM_uint32                     major_status = 0;
00160     OM_uint32                     minor_status = 0;
00161     OM_uint32                     minor_status2 = 0;
00162 
00163     if ((major_status = gss_inquire_cred(&minor_status,
00164                                          globus_cred,
00165                                          &globus_name,
00166                                          NULL, NULL, NULL)) == GSS_S_COMPLETE)
00167     {
00168         major_status = gss_display_name(&minor_status,
00169                                         globus_name, globus_buffer, NULL);
00170         gss_release_name(&minor_status2, &globus_name);
00171     }
00172     /*
00173      * The gssapi_cleartext does not implement gss_inquire_cred,
00174      * so fall back to using environment variable.
00175      */
00176     if (major_status == GSS_S_COMPLETE)
00177     {
00178         globusid = globus_buffer_desc.value;
00179     }
00180     else
00181     {
00182         globusid = getenv("GLOBUSID");
00183         globusid = (globusid ? globusid : "GLOBUSID");
00184     }
00185     globusid_tmp = strdup(globusid);
00186 
00187     if (globus_buffer_desc.value)
00188     {
00189         gss_release_buffer(&minor_status2, globus_buffer);
00190     }
00191     return globusid_tmp;
00192 }
00193 
00194 /******************************************************************************
00195 Function:       lcas_genfilename() (copied from GLOBUS gatekeeper.c)
00196 Description:    generate an absolute file name given a starting prefix,
00197                 a relative or absolute path, and a suffix
00198                 Only use prefix if path is relative.
00199 Parameters:
00200 Returns:        a pointer to a string which could be freeded.
00201 ******************************************************************************/
00202 char * lcas_genfilename(
00203         char * prefixp,
00204         char * pathp,
00205         char * suffixp
00206 )
00207 {
00208     char * newfilename;
00209     int    prefixl, pathl, suffixl;
00210     char * prefix,  * path, * suffix;
00211 
00212     prefix = (prefixp) ? prefixp : "";
00213     path   = (pathp) ? pathp : "";
00214     suffix  = (suffixp) ? suffixp : "";
00215 
00216     prefixl = strlen(prefix);
00217     pathl   =  strlen(path);
00218     suffixl  =  strlen(suffix); 
00219 
00220     newfilename = (char *) calloc(1, (prefixl + pathl + suffixl + 3));
00221     if (newfilename) 
00222     {
00223         if (*path != '/')
00224         {
00225             strcat(newfilename, prefix);
00226             if ((prefixl != 0) && (prefix[prefixl-1] != '/'))
00227             {
00228                 strcat(newfilename, "/");
00229             }
00230         }
00231         strcat(newfilename, path);
00232         if ((pathl  != 0) &&
00233             (suffixl != 0) && 
00234             (path[pathl-1] != '/') && 
00235              suffix[0] != '/')
00236         {
00237             strcat(newfilename, "/");
00238         }
00239         strcat(newfilename, suffix);
00240     }
00241     return newfilename;
00242 }
00243 
00244 /******************************************************************************
00245 Function:       fexist()
00246 Description:    check the existence of file corresponding to <path>
00247 Parameters:     path
00248 Returns:        1, if file exists
00249 ******************************************************************************/
00259 static int fexist(
00260         char * path
00261 )
00262 {
00263   struct stat sbuf;
00264   int res;
00265   
00266   if(!path || !*path) return 0;
00267 
00268   res=stat(path,&sbuf);
00269   if (res)
00270   {
00271       if (errno==ENOENT)
00272       {
00273           return 0;
00274       }
00275       else
00276       {
00277           return -1;
00278       }
00279   }
00280   return 1;
00281 }
00282 
00283 /******************************************************************************
00284 Function:       lcas_getfexist()
00285 Description:    picks the first existing file in argument list
00286 Parameters:     n   : number of paths,
00287                 ... : list of paths
00288 Returns:        returns filename found or NULL
00289 ******************************************************************************/
00290 char * lcas_getfexist(
00291         int n,
00292         ...
00293 )
00294 {
00295   va_list pvar;
00296   int i;
00297   char *cfilenm=NULL;
00298 
00299   va_start(pvar, n);
00300 
00301   for(i=0;i<n;i++) {
00302     cfilenm=va_arg(pvar,char*);
00303     if(*cfilenm) if(fexist(cfilenm)) return cfilenm;
00304   }
00305   va_end(pvar);
00306   return NULL;
00307 }
00308 
00309 /******************************************************************************
00310 Function:       lcas_findfile()
00311 Description:    Checks for file in standard directories
00312 Parameters:     name
00313 Returns:        returns filename found (should be freeed) or NULL
00314 ******************************************************************************/
00315 char * lcas_findfile(
00316         char * name
00317 )
00318 {
00319     char * newname=NULL;
00320     char * tmpname=NULL;
00321     char * names[5]={NULL,NULL,NULL,NULL,NULL};
00322     int    i;
00323 
00324     names[0]=lcas_genfilename(NULL,name,NULL);
00325     names[1]=lcas_genfilename("modules",name,NULL);
00326     names[2]=lcas_genfilename(LCAS_ETC_HOME,name,NULL);
00327     names[3]=lcas_genfilename(LCAS_MOD_HOME,name,NULL);
00328     names[4]=lcas_genfilename(LCAS_LIB_HOME,name,NULL);
00329 
00330     tmpname=lcas_getfexist(5,names[0],
00331                       names[1],names[2],
00332                       names[3],names[4]);
00333     if (tmpname != NULL)
00334         newname=strdup(tmpname);
00335     else
00336         newname=NULL;
00337 
00338     for (i=0; i < 5; i++)
00339     {
00340         if (names[i] != NULL) free(names[i]);
00341     }
00342 
00343     return newname;
00344 }
00345 
00346 /******************************************************************************
00347 Function:   lcas_tokenize() (in modified form from globus_gatekeeper_utils.c)
00348 
00349 Description:
00350     Breakup the command in to args, pointing the args array
00351     at the tokens. Replace white space at the end of each
00352     token with a null. A token maybe in quotes. 
00353 
00354 Parameters:
00355     command: The command line to be parsed.
00356     args:    A pointer to an array of pointers to be filled it
00357     n:       Size of the array, on input, and set to size used on output. 
00358     sep:     string of seperating characters
00359 
00360 Returns:
00361     0 on success. 
00362     -1 on to malloc
00363     -2 on to many args
00364     -3 on quote not matched
00365 ******************************************************************************/
00366 int lcas_tokenize(
00367         const char * command, 
00368         char ** args,
00369         int * n,
00370         char * sep
00371     )
00372 {
00373     int maxargs;
00374     int i;
00375     const char * cp;
00376     const char * pp;
00377     const char * qp;
00378     char ** arg;
00379 
00380     arg = args;
00381 /*    i = *n - 1; */
00382     i = 0;
00383     maxargs = *n;
00384 
00385     cp = command;
00386     while (*cp)
00387     {
00388     /* skip leading sep characters */
00389         while (*cp && strchr(sep, *cp))
00390         {
00391             cp++;
00392         }
00393         pp = NULL;
00394         if (*cp == '\"')
00395         {
00396             cp++;
00397             pp = cp;
00398             if ((qp = strchr(cp,'\"')) == NULL)
00399             {
00400                 *n = i;
00401                 return -3;
00402             }
00403             cp = qp + 1;
00404         }
00405         else if (*cp)
00406         {
00407             pp = cp;
00408             if ((qp = strpbrk(cp,sep)) == NULL)
00409             {
00410                 qp = strchr(cp,'\0');
00411             }
00412             cp = qp;
00413         }
00414         else
00415         {
00416             continue;
00417         }
00418         if (pp)
00419         {
00420             /*
00421              * fill at most maxargs-1 arguments; let the last one point to NULL
00422              */
00423             i++;
00424             if (i >= maxargs)
00425             {
00426                 i--;
00427                 *n = i;
00428                 return(-2); /* too many args */
00429             }
00430             *arg = (char*)malloc((qp - pp) + 1);
00431             if (*arg == NULL)
00432             {
00433                 i--;
00434                 *n = i;
00435                 return -1;
00436             }
00437             memcpy(*arg,pp,qp - pp);
00438             *(*arg + (qp - pp)) = '\0';
00439             arg++;
00440         }
00441     }
00442     *arg = NULL;
00443     *n = i;
00444     return(0);
00445 }
00446 
00447 /******************************************************************************
00448 CVS Information:
00449     $Source: /cvs/jra1mw/org.glite.security.lcas/src/lcas_utils.c,v $
00450     $Date: 2005/02/28 11:45:52 $
00451     $Revision: 2.12 $
00452     $Author: msteenba $
00453 ******************************************************************************/

Generated on Fri May 27 18:10:48 2005 for lcas by doxygen 1.3.5