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

Generated at Tue Sep 23 15:06:52 2003 for edg-lcas by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001