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

lcmaps_jobrep.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  *     Oscar Koeroo <okoeroo@nikhef.nl>
00007  *     NIKHEF Amsterdam, the Netherlands
00008  */
00009 
00074 /*****************************************************************************
00075                             Include header files
00076 ******************************************************************************/
00077 #include "lcmaps_config.h"
00078 #include <stdio.h>
00079 #include <stdlib.h>
00080 #include <ctype.h>
00081 #include <string.h>
00082 #include <pwd.h>
00083 #include <openssl/x509.h>
00084 #include <openssl/asn1.h>
00085 #include "gssapi.h"
00086 
00087 #include "lcmaps_modules.h"
00088 #include "lcmaps_arguments.h"
00089 #include "lcmaps_cred_data.h"
00090 #include "lcmaps_voms_utils.h"
00091 #include "lcmaps_vo_data.h"
00092 
00093 #include "voms_apic.h"
00094 #include "globus_gss_assist.h"
00095 
00096 #include "jobrep_api.h"
00097 
00098 //#undef TESTBIO
00099 //#define TESTBIO
00100 #ifdef TESTBIO
00101 #    include <openssl/pem.h>
00102 #    include <openssl/bio.h>
00103 #endif
00104 
00105 
00106 /******************************************************************************
00107                                 Definitions
00108 ******************************************************************************/
00109 
00110 #define VOMS_BUFFER_SIZE  1024
00111 
00112 // What do we want to do with this time presentation?
00113 #define GMT               1
00114 #define LOCAL             2
00115 #define LEAVE_TIME_ITS_OK 3
00116 
00117 /******************************************************************************
00118                           Module specific prototypes
00119 ******************************************************************************/
00120 //static void print_vomsdata(struct vomsdata *);
00121 void print_vomsdata(struct vomsdata *);
00122 //static char *gridmapdir_urlencode(char *);
00123 static struct tm * ASN1_TIME_2_time_t(ASN1_TIME *);
00124 char * sttm_2_char (const struct tm *);
00125 
00126 int strnclean (char **s, int bufsize);
00127 int strclean  (char **s);
00128 
00129 
00130 char * createFQAN(char *, char *, char *, char *);
00131 char * tripletLine2FQAN (char *);
00132 
00133 static int lcmaps_get_jobrep_config(
00134         const char *,
00135         char **
00136 );
00137 
00138 #ifdef TESTBIO
00139 static STACK_OF(X509) *load_chain(char *certfile);
00140 static char *retmsg[] = { "VERR_NONE", "VERR_NOSOCKET", "VERR_NOIDENT", "VERR_COMM", 
00141                           "VERR_PARAM", "VERR_NOEXT", "VERR_NOINIT",
00142                           "VERR_TIME", "VERR_IDCHECK", "VERR_EXTRAINFO",
00143                           "VERR_FORMAT", "VERR_NODATA", "VERR_PARSE",
00144                           "VERR_DIR", "VERR_SIGN", "VERR_SERVER", 
00145                           "VERR_MEM", "VERR_VERIFY", "VERR_IDENT",
00146                           "VERR_TYPE", "VERR_ORDER" };
00147 #endif
00148 
00149 /******************************************************************************
00150                        Define module specific variables
00151 ******************************************************************************/
00152 
00153 static char * jobrep_config = NULL;
00154 static char * connStr = NULL;
00155 static char * certdir = NULL;
00156 static char * vomsdir = NULL;
00157 //static char   voms_buffer[VOMS_BUFFER_SIZE];
00158 
00159 
00160 /*****************************************************************************/
00161 
00162 
00163 
00164 
00165 /******************************************************************************
00166 Function:   tripletLine2FQAN
00167 Description:
00168          This function extracts the triplet information from the tripletLine 
00169          string and returns a FQAN. The tripletLine is in the same format as
00170          the vomapfile (or grid-mapfile) and the groupmapfile.
00171          This format is also supported with voms2gacl to get the VOMS info
00172          in a GACL file. Here it functions to convert the compare the different
00173          formats to the (standard) FQAN.
00174 
00175 Parameters:
00176          tripletLine
00177  
00178 Returns:
00179     !NULL              : succes
00180     NULL               : failure
00181 ******************************************************************************/
00182 
00183 char * tripletLine2FQAN (char * tripletLine)
00184 {
00185     char * pTL   = tripletLine;
00186     char   vo    [100];
00187     char   group [100];
00188     char   role  [100];
00189     char   cap   [100];
00190     int    i     = 0;
00191     char * search = NULL;
00192 
00193     for (i = 0; i < 100; i++) { vo[i] = group[i] = role[i] = cap[i] = '\0'; }
00194 
00195 
00196     while (strlen(pTL) > 0)
00197     {
00198         i = 0;
00199         if (strncmp(pTL, "/VO=", strlen("/VO=")) == 0)
00200         {
00201             // Shift prefix
00202             pTL += strlen("/VO=");
00203 
00204             // Search for next prefix, if found then copy the offset
00205             search = pTL;
00206             while (strlen(search) > 0)
00207             {
00208                 if ((strncmp(search, "/VO=", strlen("/VO=")) == 0) || 
00209                     (strncmp(search, "/GROUP=", strlen("/GROUP=")) == 0) || 
00210                     (strncmp(search, "/ROLE=", strlen("/ROLE=")) == 0)   ||
00211                     (strncmp(search, "/CAPABILITY=", strlen("/CAPABILITY=")) == 0))
00212                 {
00213                     break;
00214                 }
00215                 i++;
00216                 search++;
00217             }
00218 
00219             strncpy(vo, pTL, i);
00220             pTL += strlen(vo);
00221         }
00222 
00223         i = 0;
00224 
00225         if (strncmp(pTL, "/GROUP=", strlen("/GROUP=")) == 0)
00226         {
00227             pTL += strlen("/GROUP=");
00228 
00229             // Search for next prefix, if found then copy the offset
00230             search = pTL;
00231             while (strlen(search) > 0)
00232             {
00233                 if ((strncmp(search, "/VO=", strlen("/VO=")) == 0) ||
00234                     (strncmp(search, "/GROUP=", strlen("/GROUP=")) == 0) ||
00235                     (strncmp(search, "/ROLE=", strlen("/ROLE=")) == 0)   ||
00236                     (strncmp(search, "/CAPABILITY=", strlen("/CAPABILITY=")) == 0))
00237                 {
00238                     break;
00239                 }
00240 
00241                 i++;
00242                 search++;
00243             }
00244  
00245             strncpy(group, pTL, i);
00246             pTL += strlen(group);
00247         }
00248 
00249         i = 0;
00250         if (strncmp(pTL, "/ROLE=", strlen("/ROLE=")) == 0)
00251         {
00252             pTL += strlen("/ROLE=");
00253 
00254             // Search for next prefix, if found then copy the offset
00255             search = pTL;
00256             while (strlen(search) > 0)
00257             {
00258                 if ((strncmp(search, "/VO=", strlen("/VO=")) == 0) ||
00259                     (strncmp(search, "/GROUP=", strlen("/GROUP=")) == 0) ||
00260                     (strncmp(search, "/ROLE=", strlen("/ROLE=")) == 0)   ||
00261                     (strncmp(search, "/CAPABILITY=", strlen("/CAPABILITY=")) == 0))
00262                 {
00263                     break;
00264                 }
00265 
00266                 i++;
00267                 search++;
00268             }
00269 
00270             strncpy(role, pTL, i);
00271             pTL +=  strlen(role) - 1;
00272         }
00273 
00274         i = 0;
00275         if (strncmp(pTL, "/CAPABILITY=", strlen("/CAPABILITY=")) == 0)
00276         {
00277             pTL += strlen("/CAPABILITY=");
00278 
00279             // Search for next prefix, if found then copy the offset
00280             search = pTL;
00281             while (strlen(search) > 0)
00282             {
00283                 if ((strncmp(search, "/VO=", strlen("/VO=")) == 0) ||
00284                     (strncmp(search, "/GROUP=", strlen("/GROUP=")) == 0) ||
00285                     (strncmp(search, "/ROLE=", strlen("/ROLE=")) == 0)   ||
00286                     (strncmp(search, "/CAPABILITY=", strlen("/CAPABILITY=")) == 0))
00287                 {
00288                     break;
00289                 }
00290                 i++;
00291                 search++;
00292             }
00293  
00294             strncpy(cap, pTL, i);
00295             pTL +=  strlen(cap) - 1;
00296         }
00297 
00298         pTL++;
00299     }
00300 //    lcmaps_log(0, " >> vo: %s (%d)  group: %s (%d)  role: %s (%d)  cap: %s (%d)  <<\n", vo, strlen(vo), group, strlen(group), role, strlen(role), cap, strlen(cap));
00301 
00302     return createFQAN (vo, group, role, cap);
00303 }
00304 
00305 
00306 
00307 /******************************************************************************
00308 Function:   createFQAN
00309 Description:
00310             Builds a FQAN of the vo, group, role and cap (capability) parameters
00311 Parameters:
00312             char * vo, group, role, cap
00313 Returns:
00314     !NULL              : succes
00315     NULL               : failure
00316 ******************************************************************************/ 
00317 char * createFQAN(char * vo, char * group, char * role, char * cap)
00318 {
00319     static char * fqan = NULL;
00320     int    i    = 0;
00321 
00322     if ((group == NULL) || (strlen(group) < 1))
00323         return NULL;
00324 
00325 
00326     // EVIL BUG FIX! - Because of " " in front of the Role
00327     if ((role != NULL) && (strlen(role) > 1) && (role[0] == ' ')) {  role++;  }
00328 
00329  
00330     fqan = malloc (250 * sizeof(char));
00331     for (i = 0; i < 250; i++)  { fqan[i] = '\0'; }
00332 
00333     // FQAN building ...
00334     sprintf(fqan, "%s", group);
00335     
00336     if ((role != NULL) && (strcmp(role, "NULL") != 0) && (strlen(role) > 0))
00337         sprintf(fqan, "%s/Role=%s", fqan, role);
00338 
00339     if ((cap != NULL)  && (strcmp(cap, "NULL") != 0)  && (strlen(cap) > 0)) 
00340         sprintf(fqan, "%s/Capability=%s", fqan, cap);
00341 
00342     return fqan;
00343 }
00344 
00345 
00346 #ifdef TESTBIO
00347 static STACK_OF(X509) *load_chain(char *certfile)
00348 {
00349     STACK_OF(X509_INFO) *sk=NULL;
00350     STACK_OF(X509) *stack=NULL, *ret=NULL;
00351     BIO *in=NULL;
00352     X509_INFO *xi;
00353     int first = 1;
00354     char * logstr = "load_chain";
00355 
00356     if(!(stack = sk_X509_new_null()))
00357     {
00358         lcmaps_log(2, "%s: memory allocation failure\n", logstr);
00359         goto end;
00360     }
00361 
00362     if(!(in=BIO_new_file(certfile, "r")))
00363     {
00364         lcmaps_log(2, "%s: error opening the file, %s\n", logstr,certfile);
00365         goto end;
00366     }
00367 
00368     /* This loads from a file, a stack of x509/crl/pkey sets */
00369     if(!(sk=PEM_X509_INFO_read_bio(in,NULL,NULL,NULL)))
00370     {
00371         lcmaps_log(2, "%s: error reading the file, %s\n", logstr,certfile);
00372         goto end;
00373     }
00374 
00375     /* scan over it and pull out the certs */
00376     while (sk_X509_INFO_num(sk))
00377     {
00378         /* skip first cert */
00379         if (first)
00380         {
00381             first = 0;
00382             continue;
00383         }
00384         xi=sk_X509_INFO_shift(sk);
00385         if (xi->x509 != NULL)
00386         {
00387             sk_X509_push(stack,xi->x509);
00388             xi->x509=NULL;
00389         }
00390         X509_INFO_free(xi);
00391     }
00392     if(!sk_X509_num(stack))
00393     {
00394         lcmaps_log(2, "%s: no certificates in file, %s\n", logstr,certfile);
00395         sk_X509_free(stack);
00396         goto end;
00397     }
00398     ret=stack;
00399 end:
00400     BIO_free(in);
00401     sk_X509_INFO_free(sk);
00402     return(ret);
00403 }
00404 #endif
00405 
00406 /******************************************************************************
00407 Function:   plugin_initialize
00408 Description:
00409     Initialize plugin
00410 Parameters:
00411     argc, argv
00412     argv[0]: the name of the plugin
00413 Returns:
00414     LCMAPS_MOD_SUCCESS : succes
00415     LCMAPS_MOD_FAIL    : failure
00416     LCMAPS_MOD_NOFILE  : db file not found (will halt LCMAPS initialization)
00417 ******************************************************************************/
00418 int plugin_initialize(
00419         int argc,
00420         char ** argv
00421 )
00422 {
00423     char * logstr = "\tlcmaps_plugin_jobrepository-plugin_initialize()";
00424     int i;
00425 
00426     connStr = (char *) malloc (sizeof(char) * 1000);
00427     for (i = 0; i < 1000; i++) connStr[i] = '\0';
00428  
00429     lcmaps_log_debug(1,"%s: passed arguments:\n", logstr);
00430     for (i=0; i < argc; i++)
00431     {
00432        lcmaps_log_debug(2,"%s: arg %d is %s\n", logstr, i, argv[i]);
00433     }
00434  
00435     /*
00436      * CERTDIR = The directory which contains the CA certificates
00437      * VOMSDIR = The directory which contains the certificates of the VOMS servers 
00438      */
00439  
00440     /*
00441      * Parse arguments, argv[0] = name of plugin, so start with i = 1
00442      */
00443     for (i = 1; i < argc; i++)
00444     {
00445         if ( ((strcmp(argv[i], "-vomsdir") == 0) ||
00446               (strcmp(argv[i], "-VOMSDIR") == 0))
00447              && (i + 1 < argc))
00448         {
00449             if ((argv[i + 1] != NULL) && (strlen(argv[i + 1]) > 0))
00450             {
00451                  vomsdir = strdup(argv[i + 1]);
00452             }
00453             i++;
00454         }
00455         else if ( ((strcmp(argv[i], "-certdir") == 0) ||
00456                    (strcmp(argv[i], "-CERTDIR") == 0))
00457                    && (i + 1 < argc))
00458         {
00459             if ((argv[i + 1] != NULL) && (strlen(argv[i + 1]) > 0))
00460             {
00461                  certdir = strdup(argv[i + 1]);
00462             }
00463             i++;
00464         }
00465 
00466         else if ( ( (strcmp(argv[i], "-JR_CONFIG") == 0) || (strcmp(argv[i], "-jr_config") == 0) )
00467                   && (i + 1 < argc) )
00468         {
00469             lcmaps_get_jobrep_config(argv[i + 1], &jobrep_config);
00470 
00471             if (strlen(connStr) == 0)
00472                 strncpy(connStr, jobrep_config, strlen(jobrep_config));
00473             else
00474                 sprintf(connStr, "%s;%s", connStr, jobrep_config);
00475 
00476             i++;
00477         }
00478 
00479 
00480         // Due to security compromise reasons, this should not be possible to be connecting with:
00481         else if ( (strncmp(argv[i], "DSN=",      4) == 0) ||
00482                   (strncmp(argv[i], "HOST=",     5) == 0) ||
00483                   (strncmp(argv[i], "USER=",     5) == 0) ||
00484                   (strncmp(argv[i], "PASS=",     5) == 0) ||
00485                   (strncmp(argv[i], "DRIVER=",   7) == 0) ||
00486                   (strncmp(argv[i], "DATABASE=", 9) == 0) ||
00487                   (strncmp(argv[i], "PORT=",     5) == 0) ||
00488                   (strncmp(argv[i], "PASSWORD=", 9) == 0) ||
00489                   (strncmp(argv[i], "SERVER=",   7) == 0) ||
00490                   (strncmp(argv[i], "driver=",   7) == 0) ||
00491                   (strncmp(argv[i], "dsn=",      4) == 0) ||
00492                   (strncmp(argv[i], "host=",     5) == 0) ||
00493                   (strncmp(argv[i], "user=",     5) == 0) ||
00494                   (strncmp(argv[i], "port=",     5) == 0) ||
00495                   (strncmp(argv[i], "database=", 9) == 0) ||
00496                   (strncmp(argv[i], "server=",   7) == 0) ||
00497                   (strncmp(argv[i], "port=",     5) == 0) ||
00498                   (strncmp(argv[i], "password=", 9) == 0) ||
00499                   (strncmp(argv[i], "pass=",     5) == 0) )
00500         {
00501             if (strlen(connStr) == 0)
00502                 strncpy(connStr, argv[i], strlen(argv[i]));
00503             else
00504                 sprintf(connStr, "%s;%s", connStr, argv[i]);
00505         }
00506         else
00507         {
00508             lcmaps_log(0,"%s: Error in initialization parameter: %s (failure)\n", logstr,
00509                        argv[i]);
00510             return LCMAPS_MOD_FAIL;
00511         }
00512     }
00513 
00514     lcmaps_log(1,"%s: Initialization succeeded\n", logstr);
00515     return LCMAPS_MOD_SUCCESS;
00516 } 
00517 
00518 
00519 /******************************************************************************
00520 Function:   plugin_introspect
00521 Description:
00522     return list of required arguments
00523 Parameters:
00524 
00525 Returns:
00526     LCMAPS_MOD_SUCCESS : succes
00527     LCMAPS_MOD_FAIL    : failure
00528 ******************************************************************************/
00529 int plugin_introspect(
00530         int * argc,
00531         lcmaps_argument_t ** argv
00532 )
00533 {
00534     char * logstr = "\tlcmaps_plugin_voms-plugin_introspect()";
00535     static lcmaps_argument_t argList[] = {
00536         { "user_dn"     , "char *"           ,  1, NULL},
00537         { "user_cred"   , "gss_cred_id_t"    ,  0, NULL},
00538         { "job_request" , "char *"           ,  0, NULL},
00539         { NULL          , NULL               , -1, NULL}
00540     };
00541 
00542     lcmaps_log_debug(1,"%s: introspecting\n", logstr);
00543 
00544     *argv = argList;
00545     lcmaps_log_debug(4,"%s: before lcmaps_cntArgs()\n", logstr);
00546     *argc = lcmaps_cntArgs(argList);
00547     lcmaps_log_debug(1,"%s: address first argument: 0x%x\n", logstr,argList);
00548 
00549     lcmaps_log(1,"%s: Introspect succeeded\n", logstr);
00550     return LCMAPS_MOD_SUCCESS;
00551 }
00552 
00553 
00554 /******************************************************************************
00555 Function:   plugin_run
00556 Description:
00557     Gather credentials for LCMAPS
00558 Parameters:
00559     argc: number of arguments
00560     argv: list of arguments
00561 Returns:
00562     LCMAPS_MOD_SUCCESS: authorization succeeded
00563     LCMAPS_MOD_FAIL   : authorization failed
00564 ******************************************************************************/
00565 int plugin_run(
00566         int argc,
00567         lcmaps_argument_t * argv
00568 )
00569 {
00570     char             *  logstr      = "\tlcmaps_plugin_jobrepository-plugin_run()";
00571     int                 tryCnt      = 0;
00572     int                 connected   = 0;
00573     FILE             *  tmpFile     = NULL;
00574     char             *  dn          = NULL;
00575     char             *  rsl         = NULL; 
00576     struct vomsdata  *  vd          = NULL;
00577     struct voms      ** vo          = NULL;
00578     struct voms      *  v           = NULL;
00579 
00580     uid_t            *  uid         = NULL;
00581     int                 cntUid = 0;
00582     gid_t            *  priGid      = NULL;
00583     int                 cntPriGid = 0;
00584     gid_t            *  secGid      = NULL;
00585     int                 cntSecGid = 0;
00586     struct passwd    *  user_info   = NULL;
00587     struct group     *  group_info  = NULL;
00588 
00589     char             *  datetime;
00590     time_t              clock;
00591     struct tm        *  tmpTime;
00592 
00593     TResultSet       *  resultSet = NULL;
00594     char                query[1000000];
00595     char             *  user_id = NULL;
00596     char             *  credential_id = NULL;
00597     char             *  credential_group_id = NULL;
00598     char             *  cert_id = NULL;
00599 
00600     STACK_OF(X509)   *  dupChain = NULL;
00601     TSimpleCert      *  MySimpleStack= NULL;
00602     int                 certCount = 0;
00603 
00604     int                 i = 0, 
00605                         j = 0, 
00606                         k = 0;
00607 
00608     lcmaps_vo_mapping_t * vo_mapping = NULL;
00609 
00610 
00611 
00612 
00613 #ifdef TESTBIO
00614     int err;
00615     int res = 0;
00616     BIO *in = NULL;
00617     X509 *x = NULL;
00618     STACK_OF(X509) *chain = NULL;
00619 #else
00620     int                 errNo       = 0;
00621     gss_cred_id_t    *  pcred       = NULL;
00622     gss_cred_id_t       cred        = GSS_C_NO_CREDENTIAL;
00623     X509             *  px509_cred  = NULL;
00624     STACK_OF(X509)   *  px509_chain = NULL;
00625 #endif
00626 
00627 
00628  
00629     /*
00630      * The beginning
00631      */
00632 
00633     time(&clock);
00634     tmpTime = gmtime(&clock);
00635     datetime = malloc(sizeof(char) * 20);
00636     snprintf(datetime, 20, "%04d-%02d-%02d %02d:%02d:%02d",
00637            tmpTime->tm_year + 1900, tmpTime->tm_mon + 1, tmpTime->tm_mday,
00638            tmpTime->tm_hour, tmpTime->tm_min, tmpTime->tm_sec);
00639 
00640 
00641     /*
00642      *
00643      * Inserting a user's certificate chain when needed (if it's not there yet... insert it))
00644      *
00645      */
00646 
00647 #ifdef TESTBIO
00648 #else
00649     /* Fetch user gss credential */
00650     if ( ( pcred = (gss_cred_id_t *) lcmaps_getArgValue("user_cred", "gss_cred_id_t", argc, argv) ) )
00651     {
00652         lcmaps_log_debug(2,"%s: address user_cred: %p\n", logstr,pcred);
00653         cred = *pcred;
00654         if (cred == GSS_C_NO_CREDENTIAL)
00655         {
00656             lcmaps_log(0,"%s: user gss credential is empty ! (exit voms)\n", logstr);
00657             goto fail_jobrep;
00658         }
00659     }
00660     else
00661     {
00662         lcmaps_log(0,"%s: could not get address of user_cred (exit voms)!\n", logstr);
00663         goto fail_jobrep;
00664     }
00665 #endif
00666 
00667 
00668     /* Testing */
00669 #undef EXPORT_CREDENTIAL
00670 #if EXPORT_CREDENTIAL
00671     if (cred)
00672     {
00673         gss_buffer_desc                 deleg_proxy_filename;
00674         OM_uint32         major_status = 0;
00675         OM_uint32         minor_status = 0;
00676         
00677         major_status = gss_export_cred(&minor_status,
00678                                        cred,
00679                                        NULL,
00680                                        1,
00681                                        &deleg_proxy_filename);
00682 
00683         if (major_status == GSS_S_COMPLETE)
00684         {
00685             char *                     cp;
00686 
00687             lcmaps_log_debug(1,"%s: deleg_proxy_filename.value: %s\n", logstr,
00688                                deleg_proxy_filename.value);
00689             cp = strchr((char *)deleg_proxy_filename.value, '=');
00690             *cp = '\0';
00691             cp++;
00692             setenv((char *)deleg_proxy_filename.value, cp, 1);
00693             free(deleg_proxy_filename.value);
00694         }
00695         else
00696         {
00697             char *                      error_str = NULL;
00698             globus_object_t *           error_obj;
00699 
00700             error_obj = globus_error_get((globus_result_t) minor_status);
00701             
00702             error_str = globus_error_print_chain(error_obj);
00703             lcmaps_log(0,"%s: Error, gss_export_cred(): %s\n", logstr,error_str);
00704             goto fail_jobrep;
00705         }
00706     }
00707 #endif /* EXPORT_CREDENTIAL */
00708 
00709 
00710 
00711 #ifdef TESTBIO
00712 #else
00713     /*
00714      * Retrieve a newly created X509 struct and X509 chain from gss credential (should be freed)
00715      */
00716     if ( ( px509_cred = lcmaps_cred_to_x509(cred) ) )
00717     {
00718         lcmaps_log_debug(1,"%s: found X509 struct inside gss credential\n", logstr);
00719         lcmaps_log_debug(5,"%s: just for kicks: X509->name %s\n", logstr,px509_cred->name);
00720     }
00721     else
00722     {
00723         lcmaps_log(0,"%s: could not get X509 cred (exit jobrep)!\n", logstr);
00724         goto fail_jobrep;
00725     }
00726     if ( ( px509_chain = lcmaps_cred_to_x509_chain(cred) ) )
00727     {
00728         lcmaps_log_debug(1,"%s: found X509 chain inside gss credential\n", logstr);
00729     }
00730     else
00731     {
00732         lcmaps_log(0,"%s: could not get X509 chain (exit voms)!\n", logstr);
00733         goto fail_jobrep;
00734     }
00735 #endif
00736 
00737     lcmaps_log_debug(1,"%s: vomsdir = %s\n", logstr, vomsdir);
00738     lcmaps_log_debug(1,"%s: certdir = %s\n", logstr, certdir);
00739     if ((vd = VOMS_Init(vomsdir, certdir)) == NULL)
00740     {
00741         lcmaps_log(0,"%s: failed to initialize voms data structure, so we can skip the gathering of al the VOMS stuff and go with classic support\n", logstr);
00742         // Go Classic Mode!
00743     }
00744     lcmaps_log_debug(1,"%s: voms data structure initialized\n", logstr);
00745 
00746 
00747     
00748 
00749 
00750 #ifdef TESTBIO
00751     in = BIO_new(BIO_s_file());
00752 //    chain = load_chain("/home/gridtest/cvs/fabric_mgt/gridification/lcmaps/modules/voms/x509up_u500");
00753 
00754     lcmaps_log(5, "%s: load chain...\n", logstr);
00755     chain = load_chain(getenv("X509_USER_PROXY"));
00756     lcmaps_log(5, "%s: loaded chain\n", logstr);
00757     if (in)
00758     {
00759         if (BIO_read_filename(in, getenv("X509_USER_PROXY")) > 0)
00760         {
00761             x = PEM_read_bio_X509(in, NULL, 0, NULL);
00762 
00763             res = VOMS_Retrieve(x, chain, RECURSE_CHAIN, vd, &err);
00764 
00765             if (res)
00766                 print_vomsdata(vd);
00767             else
00768                 lcmaps_log(1, "%s: ERROR!\n", logstr);
00769         }
00770     }
00771     else
00772     {
00773         lcmaps_log(1, "Error: No BIO File!\n");
00774     }
00775 
00776     if (!res)
00777     {
00778         lcmaps_log(1, "%s: err: %s\n", logstr, retmsg[err]);
00779     }
00780 
00781 
00782     // Check is the chain is there
00783     if (chain != NULL)
00784 #else
00785     if (VOMS_Retrieve(px509_cred, px509_chain, RECURSE_CHAIN, vd, &errNo))
00786     {
00787         lcmaps_log_debug(3, "%s: VOMS extensions extracted from proxy certificate\n", logstr);
00788     }
00789 
00790     else if (errNo == VERR_NOEXT)
00791     {
00792         lcmaps_log(0,"%s: VOMS extensions missing from certificate (no yet failure)!\n", logstr);
00793 //        goto fail_voms;
00794     }
00795     else if (errNo == VERR_IDCHECK)
00796     {
00797         lcmaps_log(0,"%s: VOMS User data in extension different from the real ones (failure)!\n", logstr);
00798         goto fail_jobrep;
00799     }
00800     else if (errNo == VERR_TIME)
00801     {
00802         lcmaps_log(0,"%s: VOMS extensions expired for at least one of the VOs (failure)!\n", logstr);
00803         goto fail_jobrep;
00804     }
00805     else if (errNo == VERR_ORDER)
00806     {
00807         lcmaps_log(0,"%s: The ordering of the VOMS groups, as required by the client, was not delivered by VOMS (failure)!\n", logstr);
00808         goto fail_jobrep;
00809     }
00810     else if (errNo == VERR_NOSOCKET)
00811     {
00812         lcmaps_log(0,"%s: VOMS Socket problem (failure)!\n", logstr);
00813         goto fail_jobrep;
00814     }
00815     else if (errNo == VERR_NOIDENT)
00816     {
00817         lcmaps_log(0,"%s: VOMS Cannot identify itself (certificate problem) (failure)!\n", logstr);
00818         goto fail_jobrep;
00819     }
00820     else if (errNo == VERR_COMM)
00821     {
00822         lcmaps_log(0,"%s: VOMS server problem (failure)!\n", logstr);
00823         goto fail_jobrep;
00824     }
00825     else if (errNo == VERR_PARAM)
00826     {
00827         lcmaps_log(0,"%s: Wrong parameters for VOMS (failure)!\n", logstr);
00828         goto fail_jobrep;
00829     }
00830     else if (errNo == VERR_NOINIT)
00831     {
00832         lcmaps_log(0,"%s: VOMS initialization error (failure)!\n", logstr);
00833         goto fail_jobrep;
00834     }
00835     else if (errNo == VERR_EXTRAINFO)
00836     {
00837         lcmaps_log(0,"%s: VO name and URI missing (in proxy ?) (failure)!\n", logstr);
00838         goto fail_jobrep;
00839     }
00840     else if (errNo == VERR_FORMAT)
00841     {
00842         lcmaps_log(0,"%s: Wrong VOMS data format (in proxy ?) (failure)!\n", logstr);
00843         goto fail_jobrep;
00844     }
00845     else if (errNo == VERR_NODATA)
00846     {
00847         lcmaps_log(0,"%s: Empty VOMS extension (failure)!\n", logstr);
00848         goto fail_jobrep;
00849     }
00850     else if (errNo == VERR_PARSE)
00851     {
00852         lcmaps_log(0,"%s: VOMS parse error (failure)!\n", logstr);
00853         goto fail_jobrep;
00854     }
00855     else if (errNo == VERR_DIR)
00856     {
00857         lcmaps_log(0,"%s: VOMS directory error (failure)!\n", logstr);
00858         goto fail_jobrep;
00859     }
00860     else if (errNo == VERR_SIGN)
00861     {
00862         lcmaps_log(0,"%s: VOMS Signature error (failure)!\n", logstr);
00863         goto fail_jobrep;
00864     }
00865     else if (errNo == VERR_SERVER)
00866     {
00867         lcmaps_log(0,"%s: Unidentifiable VOMS server (failure)!\n", logstr);
00868         goto fail_jobrep;
00869     }
00870     else if (errNo == VERR_MEM)
00871     {
00872         lcmaps_log(0,"%s: Memory problems in VOMS_Retrieve() (failure)!\n", logstr);
00873         goto fail_jobrep;
00874     }
00875     else if (errNo == VERR_VERIFY)
00876     {
00877         lcmaps_log(0,"%s: Generic verification error for VOMS (failure)!\n", logstr);
00878         goto fail_jobrep;
00879     }
00880     else if (errNo == VERR_TYPE)
00881     {
00882         lcmaps_log(0,"%s: Returned VOMS data of unknown type (failure)!\n", logstr);
00883         goto fail_jobrep;
00884     }
00885     else
00886     {
00887         lcmaps_log(0,"%s: VOMS_Retrieve() error --> %d (failure)!\n", logstr, errNo);
00888         goto fail_jobrep;
00889     }
00890 
00891 
00892     // Check if this chain is there
00893     if (px509_chain != NULL)
00894 #endif
00895     {
00896         int ctCnt = 0;
00897 
00898         lcmaps_log_debug(5, "%s: The certificate chain is correct\n", logstr);
00899 
00900  
00901 #ifdef TESTBIO
00902         dupChain = (STACK_OF(type) *) sk_X509_dup(chain);
00903 #else
00904         dupChain = (STACK_OF(type) *) sk_X509_dup(px509_chain);
00905 #endif
00906 
00907 
00908         certCount = sk_X509_num(dupChain);
00909         lcmaps_log_debug (1, "%s: The chain consist of %d certificates.\n", logstr, certCount);
00910 
00911         MySimpleStack = malloc (sizeof(TSimpleCert) * (certCount + 1));
00912         for (ctCnt = 0; ctCnt < certCount; ctCnt++)
00913         {
00914             MySimpleStack[ctCnt].X509cert = sk_X509_pop(dupChain);
00915             if (MySimpleStack[ctCnt].X509cert != NULL)
00916             {
00917                 char          buffer[1000];
00918                 int           kar    = '\0';
00919                 int           offset = 0;
00920 /*
00921 //      This still needs a lot of work (later, lower-prio)
00922 //      It's better to get the plugin working then full-functs, 
00923 //      because of LCG waiting for the funcs.
00924 
00925                 int uitslag;
00926                 BIO         * nbp = NULL;
00927                 unsigned char * thingy;
00928 
00929 
00930                 uitslag = i2d_X509_bio(nbp, MySimpleStack[ctCnt].X509cert);
00931                 if (nbp != NULL)
00932                     printf("true : nbp != NULL\n");
00933                 else
00934                     printf("true : nbp == NULL\n");
00935 
00936                 printf ("Before malloc\n");
00937                 thingy  = malloc(1 + sizeof(char) * uitslag);
00938 
00939                 
00940                 printf ("Before malloc2\n");
00941                 MySimpleStack[ctCnt].PEM = malloc(1 + sizeof(char) * uitslag);
00942                 printf("Before cleaning...\n");
00943                 for (i = 0; i < uitslag; i++) MySimpleStack[ctCnt].PEM[i] = '\0';
00944                  
00945                 printf("Before i2d_X509\n");
00946                 uitslag = i2d_X509(MySimpleStack[ctCnt].X509cert, &thingy);
00947 
00948                 printf(">>>>>>>> uitslag is:    %d\n", uitslag);
00949 */
00950 
00951                 // Make a (char *) out of a certificate
00952                 tmpFile = tmpfile();
00953 
00954                 X509_print_fp(tmpFile, MySimpleStack[ctCnt].X509cert);
00955                 rewind(tmpFile);
00956 
00957 
00958                 MySimpleStack[ctCnt].certStr = malloc(sizeof(char) * 1000000);
00959                 for (i = 0; i < 1000000; i++) MySimpleStack[ctCnt].certStr[i] = '\0';
00960                 for (i = 0; (kar = fgetc(tmpFile)) != EOF; i++)
00961                 {
00962                     // VERY UGLY BUG FIX !!!
00963                     if (kar == '\'') // a '\'' = decimal 39 = hex 27 = oct 047 = html &#39; => Using Hex encoding
00964                     {
00965                         MySimpleStack[ctCnt].certStr[i + offset]   = '%';
00966                         MySimpleStack[ctCnt].certStr[i + ++offset] = '2';
00967                         MySimpleStack[ctCnt].certStr[i + ++offset] = '7';
00968                         lcmaps_log_debug(2, "%s: \tWarning: char \' encoded into %c27\n\t\t\t\t\t\t\t\tbecause of SQL problems. (A workaround needs to be developed)\n", logstr, '%');
00969                     }
00970                     else
00971                     {
00972                         MySimpleStack[ctCnt].certStr[i + offset] = kar;
00973                     }
00974                 }
00975 
00976 //                while ((kar = fgetc(tmpFile)) != EOF)
00977 //                {   
00978 //                    if (certStr == NULL)
00979 //                    {
00980 //                        certStr = malloc(sizeof(char));
00981 //                        certStr[0] = '\0';
00982 //                    }
00983 //                    certStr = realloc(certStr, (sizeof(char) * strlen(certStr)) + 2);
00984 //                    sprintf(certStr, "%s%c", certStr, ch);
00985 // 
00986 //                    certStr[i] = kar;
00987 //                    i++;
00988 //                }
00989 
00990                 //MySimpleStack[ctCnt].certStr = strdup(certStr);
00991                 
00992                 X509_NAME_oneline(X509_get_subject_name(MySimpleStack[ctCnt].X509cert), buffer, 999);
00993                 MySimpleStack[ctCnt].subject_name = malloc(sizeof(char) * 1000);
00994 //                strncpy(MySimpleStack[ctCnt].subject_name, buffer, strlen(buffer));
00995                 strcpy(MySimpleStack[ctCnt].subject_name, buffer);
00996 
00997                 X509_NAME_oneline(X509_get_issuer_name(MySimpleStack[ctCnt].X509cert), buffer, 999);
00998                 MySimpleStack[ctCnt].issuer_name = malloc(sizeof(char) * 1000);
00999 //                strncpy(MySimpleStack[ctCnt].issuer_name, buffer, strlen(buffer));
01000                 strcpy(MySimpleStack[ctCnt].issuer_name, buffer);
01001 
01002                 MySimpleStack[ctCnt].strNotBefore = malloc(sizeof(char) * 20);
01003                 MySimpleStack[ctCnt].strNotAfter  = malloc(sizeof(char) * 20);
01004                 strncpy(MySimpleStack[ctCnt].strNotBefore, 
01005                             sttm_2_char (ASN1_TIME_2_time_t(X509_get_notBefore(MySimpleStack[ctCnt].X509cert))), 20);
01006                 strncpy(MySimpleStack[ctCnt].strNotAfter, 
01007                             sttm_2_char (ASN1_TIME_2_time_t(X509_get_notAfter(MySimpleStack[ctCnt].X509cert))),  20);
01008 
01010                 //  
01011                 //  X509_NAME * X509_get_issuer_name(X509 *a);
01012                 //  X509_NAME * X509_get_subject_name(X509 *a);
01013                 //
01014                 //              X509_get_version(x) ASN1_INTEGER_get((x)->cert_info->version)
01015                 //  /*          X509_get_serialNumber(x) ((x)->cert_info->serialNumber) */
01016                 //  ASN1_TIME   X509_get_notBefore(x) ((x)->cert_info->validity->notBefore)
01017                 //  ASN1_TIME   X509_get_notAfter(x) ((x)->cert_info->validity->notAfter)
01018                 //              X509_extract_key(x)     X509_get_pubkey(x) /*****/
01019                 //              X509_REQ_get_version(x) ASN1_INTEGER_get((x)->req_info->version)
01020                 //  X509_NAME * X509_REQ_get_subject_name(x) ((x)->req_info->subject)
01021                 //              X509_REQ_extract_key(a) X509_REQ_get_pubkey(a)
01022                 //
01023                 //              X509_CRL_get_version(x) ASN1_INTEGER_get((x)->crl->version)
01024                 //  ASN1_TIME   X509_CRL_get_lastUpdate(x) ((x)->crl->lastUpdate)
01025                 //  ASN1_TIME   X509_CRL_get_nextUpdate(x) ((x)->crl->nextUpdate)
01026                 //  X509_NAME * X509_CRL_get_issuer(x) ((x)->crl->issuer)
01027                 //              X509_CRL_get_REVOKED(x) ((x)->crl->revoked)
01028                 //
01029                 //
01030                 //
01031                 //
01032                 //  Put here the code that will make a user_certificate
01033                 //
01034                 //  Select existance in user_certificates
01035                 //  if exists check parent ... if same parent ... do nothing
01036                 //  else insert it with the parent_cert
01037                 //  
01038                 //  update jobs
01039                 //  update users
01040                 //  
01042             }
01043         }
01044     }
01045 
01046 
01047 #ifdef TESTBIO
01048     if (chain) sk_X509_free(chain);
01049 #endif
01050 
01051     /*
01052      * Setting up Database connection
01053      */
01054 
01055 
01056     lcmaps_log_debug(1, "%s: \tTrying to connect to the DB...\n", logstr);
01057 
01058     srandom(clock);
01059     for (tryCnt = 0; (tryCnt < 100) && (!connected); tryCnt++)
01060     {
01061         if (ODBC_Connect (connStr) != 0)
01062         {
01063             //ODBC_Errors ("ODBC_Connect, retry...");
01064             ODBC_Disconnect();
01065         }
01066         else
01067         {
01068             connected = 1;
01069             break;
01070         }
01071     }
01072 
01073     if (!connected)
01074     {
01075         lcmaps_log(1, "%s\n", ODBC_Errors ("ODBC_Connect"));
01076         ODBC_Disconnect ();
01077         goto fail_jobrep;
01078     }
01079 
01080 
01081     //Debug
01082     lcmaps_log_debug(1, "%s: Connected to the Job Repository Database\n", logstr); 
01083 
01084 
01085 //    if (ODBC_Connect (connStr) != 0)
01086 //    {
01087 //        ODBC_Errors ("ODBC_Connect");
01088 //        ODBC_Disconnect ();
01089 //        goto fail_jobrep;
01090 //    }
01091  
01092  
01093     /*
01094      * Acquiring information
01095      */
01096 
01097     if ( (rsl = *(char **) lcmaps_getArgValue("job_request", "char *", argc, argv)) )
01098         lcmaps_log_debug(1,"%s: found job_request: %s\n", logstr, rsl);
01099     else
01100     {
01101         lcmaps_log_debug(1,"%s: could not get value of job_request !\n", logstr);
01102         goto fail_jobrep;
01103     }
01104 
01105     if ( (dn = *(char **) lcmaps_getArgValue("user_dn", "char *", argc, argv)) )
01106         lcmaps_log_debug(1,"%s: found dn: %s\n", logstr,dn);
01107     else
01108     {
01109         lcmaps_log_debug(1,"%s: could not get value of dn !\n", logstr);
01110         goto fail_jobrep;
01111     }
01112  
01113     uid    = getCredentialData(UID,     &cntUid);
01114     priGid = getCredentialData(PRI_GID, &cntPriGid);
01115     secGid = getCredentialData(SEC_GID, &cntSecGid);
01116  
01117     lcmaps_log_debug(1, "%s\n", dn);
01118 
01119 
01120 
01121     /*
01122      *
01123      * Inserting a user when needed (this is when it's DN is unknown... yet)
01124      *
01125      */
01126 
01127     // Directly try to do the hardest actions in the database. The insertion of a record via various
01128     // database rules that take time to consider the vality of the data.
01129 
01130     sprintf(query, "insert into users (dn) values ('%s')", dn);
01131     if (SQL_Query(query) != 0)
01132     {
01133         // With a failure of this last query, we can assume that the 'dn' in 'users' is already there
01134         sprintf(query, "select user_id from users where dn = '%s'", dn);
01135         if (SQL_Query(query) != 0)
01136         {
01137             SQL_Rollback();
01138             goto fail_jobrep;
01139         }
01140         if (SQL_GetQueryResult(&resultSet) > 0)
01141         {
01142             if (SQL_GetValue(resultSet, "user_id", &user_id) != 0)
01143             {
01144                 lcmaps_log_debug(1, "Could not get value: %s from the DB resultset with query: %s\n", "user_id", query);
01145                 SQL_Rollback();
01146                 goto fail_jobrep;
01147             }
01148         }
01149     }
01150     else
01151     {
01152         // Auto Increment ID retrieval
01153         sprintf(query, "select last_insert_id()");
01154         if (SQL_Query(query) != 0)
01155         {
01156             SQL_Rollback();
01157             goto fail_jobrep;
01158         }
01159  
01160         if (SQL_GetQueryResult(&resultSet) > 0)
01161         {
01162             if (SQL_GetValue(resultSet, "last_insert_id()", &user_id) != 0)
01163             {
01164                 lcmaps_log_debug(1, "Could not get value: %s from resultset with query: %s\n", "last_insert_id()", query);
01165                 SQL_Rollback();
01166                 goto fail_jobrep;
01167             }
01168         }
01169         else
01170         {
01171             lcmaps_log_debug(1, "ResultSet is empty for query: %s\n", query);
01172             SQL_Rollback();
01173             goto fail_jobrep;
01174         }
01175     } 
01176 
01177 
01178     /*
01179      *
01180      *  Insert of User Certificates
01181      *
01182      */
01183 
01184     for (i = 0; i < certCount; i++)
01185     {
01186 
01187         if ((cert_id != NULL) && (strlen(cert_id) > 0))
01188         {
01189             sprintf(query, "insert into user_certificates (
01190                             user_id,
01191                             subject,
01192                             cert,
01193                             first_use,
01194                             valid_from,
01195                             valid_until,
01196                             parent_cert_id)
01197                             values (%s, '%s', '%s', '%s', '%s', '%s', %s)",
01198                                     user_id,
01199                                     MySimpleStack[i].subject_name,
01200                                     MySimpleStack[i].certStr,
01201                                     datetime,
01202                                     MySimpleStack[i].strNotBefore,
01203                                     MySimpleStack[i].strNotAfter,
01204                                     cert_id);
01205         }
01206         else
01207         {
01208             sprintf(query, "insert into user_certificates (
01209                             user_id,
01210                             subject,
01211                             cert,
01212                             first_use,
01213                             valid_from,
01214                             valid_until)
01215                             values (%s, '%s', '%s', '%s', '%s', '%s')",
01216                                     user_id,
01217                                     MySimpleStack[i].subject_name,
01218                                     MySimpleStack[i].certStr,
01219                                     datetime,
01220                                     MySimpleStack[i].strNotBefore,
01221                                     MySimpleStack[i].strNotAfter);
01222         }
01223 
01224 
01225 
01226 //      Still have to find out the correct way of using DB parameters in this setup
01227 /*
01228         int errCode = 0;
01229 
01230         if ((errCode = SQL_SetParameter (MySimpleStack[i].certStr)) != 0)
01231             printf("Error: SQL_SetParameter [code:%d]\n", errCode);
01232 
01233         if ((cert_id != NULL) && (strlen(cert_id) > 0))
01234         {
01235             sprintf(query, "insert into user_certificates (
01236                             user_id,
01237                             subject,
01238                             cert,
01239                             first_use,
01240                             valid_from,
01241                             valid_until,
01242                             parent_cert_id)
01243                             values (%s, '%s', ?, '%s', '%s', '%s', %s)",
01244                                     user_id,
01245                                     MySimpleStack[i].subject_name,
01246                                     datetime,
01247                                     MySimpleStack[i].strNotBefore,
01248                                     MySimpleStack[i].strNotAfter,
01249                                     cert_id);
01250         }
01251         else
01252         {
01253             sprintf(query, "insert into user_certificates (
01254                             user_id,
01255                             subject,
01256                             cert,
01257                             first_use,
01258                             valid_from,
01259                             valid_until)
01260                             values (%s, '%s', ?, '%s', '%s', '%s')",
01261                                     user_id,
01262                                     MySimpleStack[i].subject_name,
01263                                     datetime,
01264                                     MySimpleStack[i].strNotBefore,
01265                                     MySimpleStack[i].strNotAfter);
01266         }
01267 */
01268 
01269 
01270         if (SQL_Query(query) != 0)
01271         {
01272             // Insert failed, we presume a record was already present, so let's catch the cert_id
01273             //                BUT if it is NOT selectable and this fails down here (because the searched record doesn't exist... then
01274             //                The insertion did really fail. For the moment this is caused by the ' character in the certificates signature(s).
01275             //                Which is a delimiter for strings that should be inserted into a varchar kinda field in a database.
01276             sprintf(query, "select cert_id
01277                               from user_certificates
01278                              where subject = '%s'
01279                                and valid_from = '%s'
01280                                and valid_until = '%s'", 
01281                                    MySimpleStack[i].subject_name, 
01282                                    MySimpleStack[i].strNotBefore,
01283                                    MySimpleStack[i].strNotAfter);
01284 
01285             if (SQL_Query(query) != 0)
01286             {
01287                 SQL_Rollback();
01288                 goto fail_jobrep;
01289             }
01290 
01291             if (SQL_GetQueryResult(&resultSet) > 0)
01292             {
01293                 if (SQL_GetValue(resultSet, "cert_id", &cert_id) != 0)
01294                 {
01295                     lcmaps_log_debug(1, "Could not get value: %s from the DB resultset with query: %s\n", "cert_id", query);
01296                     SQL_Rollback();
01297                     goto fail_jobrep;
01298                 }
01299             }
01300             else
01301             {
01302                 lcmaps_log_debug(1, "Empty resultset for query: %s\n", query);
01303                 SQL_Rollback();
01304                 goto fail_jobrep;
01305             }
01306         }
01307         else
01308         {
01309             // Insert succeeded
01310             sprintf(query, "select last_insert_id()");
01311             if (SQL_Query(query) != 0)
01312             {
01313                 SQL_Rollback();
01314                 goto fail_jobrep;
01315             }
01316 
01317             if (SQL_GetQueryResult(&resultSet) > 0)
01318             {
01319                 if (SQL_GetValue(resultSet, "last_insert_id()", &cert_id) != 0)
01320                 {
01321                     lcmaps_log_debug(1, "Could not get value: %s from the resultset\n", "last_insert_id()");
01322                     SQL_Rollback();
01323                     goto fail_jobrep;
01324                 }
01325             }
01326             else
01327             {
01328                 lcmaps_log_debug(1, "ResultSet is empty for query: %s\n", query);
01329                 SQL_Rollback();
01330                 goto fail_jobrep;
01331             }
01332         }
01333     }
01334 
01335 
01336 
01337     /*
01338      *
01339      * Insert a half filled job record into the database
01340      *
01341      */
01342 
01343 
01344     sprintf(query, "insert into jobs (job_id, user_id, submit_cert_id, rsl, creation_time) values ('%s', %s, %s, '%s', '%s')", getenv("JOB_REPOSITORY_ID"), user_id, cert_id, rsl, datetime);
01345     if (SQL_Query(query) != 0)
01346     {
01347         lcmaps_log(1, "Error: possible problem is an already existing job_id: %s with query: %s\n",  getenv("JOB_REPOSITORY_ID"), query);
01348         SQL_Rollback();
01349         goto fail_jobrep;
01350     }
01351 
01352     /*
01353      *
01354      * Insert a status 'gatekeeper' next with the job record
01355      *
01356      */
01357 
01358 
01359     sprintf(query, "insert into jobstatus (job_id, status, status_change) values ('%s', '%s', '%s')", 
01360             getenv("JOB_REPOSITORY_ID"), "gatekeeper", datetime);
01361     if (SQL_Query(query) != 0)
01362     {
01363         SQL_Rollback();
01364         goto fail_jobrep;
01365     }
01366 
01367     
01368     /*
01369      *
01370      * Insert credential data with its groups next with the job record
01371      *
01372      */
01373 
01374 
01375     // Fill 'credentials' and 'job_credentials'
01376    
01377     sprintf(query, "select credential_id from credentials where uid = %d and gid = %d", uid[0], priGid[0]); 
01378     if ((user_info = getpwuid(uid[0])) != NULL)
01379         sprintf(query, "%s and uid_name = '%s'", query, user_info->pw_name);
01380 
01381     if ((group_info = getgrgid(priGid[0])) != NULL)
01382         sprintf(query, "%s and gid_name = '%s'", query, group_info->gr_name);
01383 
01384     if (SQL_Query(query) != 0)
01385     {
01386         SQL_Rollback();
01387         goto fail_jobrep;
01388     }
01389 
01390     if (SQL_GetQueryResult(&resultSet) > 0)
01391     {
01392         if (SQL_GetValue(resultSet, "credential_id", &credential_id) != 0)
01393         {
01394              SQL_Rollback();
01395              goto fail_jobrep;
01396         }
01397     }
01398     else
01399     {
01400         // Doesn't exist so create it.
01401         sprintf(query, "insert into credentials (uid, gid");
01402         if (user_info != NULL)
01403             sprintf(query, "%s, uid_name", query);
01404         if (group_info != NULL)
01405             sprintf(query, "%s, gid_name", query);
01406 
01407         sprintf(query, "%s) values (%d, %d", query, uid[0], priGid[0]);
01408         if (user_info != NULL)
01409             sprintf(query, "%s, '%s'", query, user_info->pw_name);
01410         if (group_info != NULL)
01411             sprintf(query, "%s, '%s'", query, group_info->gr_name);
01412         sprintf(query, "%s)", query);
01413         if (SQL_Query(query) != 0)
01414         {
01415             SQL_Rollback();
01416             goto fail_jobrep;
01417         }
01418 
01419         // Auto Increment ID retrieval
01420         sprintf(query, "select last_insert_id()");
01421         if (SQL_Query(query) != 0)
01422         {
01423             SQL_Rollback();
01424             goto fail_jobrep;
01425         }
01426 
01427         if (SQL_GetQueryResult(&resultSet) > 0)
01428         {
01429             if (SQL_GetValue(resultSet, "last_insert_id()", &credential_id) != 0)
01430             {
01431                 lcmaps_log_debug(1, "Could not get value: %s or %s (possibly the asked value doesn't exist in resultSet) from the DB resultset with query: %s\n", "credential_id", "last_insert_id()", query);
01432  
01433                 SQL_Rollback();
01434                 goto fail_jobrep;
01435             }
01436         }
01437         else
01438         {
01439             lcmaps_log_debug(1, "ResultSet is empty for query: %s\n", query);
01440             SQL_Rollback();
01441             goto fail_jobrep;
01442         } 
01443     }
01444 
01445     // make a coupling between jobs and credentials
01446     sprintf(query, "insert into job_credentials (job_id, credential_id, use_time) values ('%s', %s, '%s')", 
01447                     getenv("JOB_REPOSITORY_ID"), credential_id, datetime);
01448     if (SQL_Query(query) != 0)
01449     {
01450         SQL_Rollback();
01451         goto fail_jobrep;
01452     }
01453 
01454 
01455     // Fill credential_groups
01456     for (i = 0; i < cntSecGid; i++)
01457     {
01458         sprintf(query, "select credential_group_id from credential_groups where sgid = %d", secGid[i]);
01459         if ((group_info = getgrgid(secGid[i])) != NULL)
01460             sprintf(query, "%s and sgid_name = '%s'", query, group_info->gr_name);
01461  
01462         if (SQL_Query(query) != 0)
01463         {
01464             SQL_Rollback();
01465             goto fail_jobrep;
01466         }
01467  
01468         if (SQL_GetQueryResult(&resultSet) > 0)
01469         {
01470             if (SQL_GetValue(resultSet, "credential_group_id", &credential_group_id) != 0)
01471             {
01472                 SQL_Rollback();
01473                 goto fail_jobrep;
01474             }
01475         }
01476         else
01477         {
01478             // Doesn't exist so create it.
01479             sprintf(query, "insert into credential_groups (sgid");
01480             if (group_info != NULL)
01481                 sprintf(query, "%s, sgid_name", query);
01482  
01483             sprintf(query, "%s) values (%d", query, secGid[i]);
01484             if (group_info != NULL)
01485                 sprintf(query, "%s, '%s'", query, group_info->gr_name);
01486             sprintf(query, "%s)", query);
01487             if (SQL_Query(query) != 0)
01488             {
01489                 SQL_Rollback();
01490                 goto fail_jobrep;
01491             }
01492  
01493             // Auto Increment ID retrieval
01494             sprintf(query, "select last_insert_id()");
01495             if (SQL_Query(query) != 0)
01496             {
01497                 SQL_Rollback();
01498                 goto fail_jobrep;
01499             }
01500  
01501             if (SQL_GetQueryResult(&resultSet) > 0)
01502             {
01503                 if (SQL_GetValue(resultSet, "credential_group_id", &credential_group_id) != 0)
01504                 {
01505                     if (SQL_GetValue(resultSet, "last_insert_id()", &credential_group_id) != 0)
01506                     {
01507                         lcmaps_log_debug(1, "Could not get value: %s or %s (possibly the asked value doesn't exist in resultSet) from the DB resultset with query: %s\n", "credential_group_id", "last_insert_id()", query);
01508  
01509                         SQL_Rollback();
01510                         goto fail_jobrep;
01511                     }
01512                 }
01513             }
01514             else
01515             {
01516                 lcmaps_log_debug(1, "ResultSet is empty for query: %s\n", query);
01517                 SQL_Rollback();
01518                 goto fail_jobrep;
01519             }
01520         }
01521  
01522         // make cuppling between jobs and credentials
01523         sprintf(query, "insert into job_credential_groups (job_id, credential_group_id, use_time) values ('%s', %s, '%s')",
01524                        getenv("JOB_REPOSITORY_ID"), credential_group_id, datetime);
01525         if (SQL_Query(query) != 0)
01526         {
01527             SQL_Rollback();
01528             goto fail_jobrep;
01529         }
01530     }
01531 
01532 
01533 
01534 
01535     // Get the VO Data into the database
01536     if ((vd != NULL) && (vd->data != NULL))
01537     {
01538         char             * voms_id           = NULL;
01539         char             * user_voms_id      = NULL;
01540         char             * issuer_id         = NULL;
01541         char             * cnt               = NULL;
01542 
01543         k = 0;
01544         vo = vd->data;
01545 
01546         // Misschien deze cyclus gebruiken?
01547         while(vo[k] != NULL)
01548         {
01549             v = vo[k++];
01550 
01551             switch (v->type)
01552             {
01553                 case TYPE_NODATA:
01554                     lcmaps_log_debug(1,"%s: NO DATA\n", logstr);
01555                     break;
01556                 case TYPE_CUSTOM:
01557                     lcmaps_log_debug(1,"%s: %*s\n", logstr, v->datalen - 10, v->custom);
01558                     break;
01559                 case TYPE_STD:
01560                     j = 0;
01561                     while (v->std[j])
01562                     {
01563                         lcmaps_log_debug(1,">>>  Triplet: GROUP: %s\tROLE: %s\tCAP: %s\n", v->std[j]->group,
01564                         v->std[j]->role,v->std[j]->cap);
01565 
01566                             
01567                         sprintf(query, "select voms_id from voms where vo = '%s' and vo_group = '%s'",
01568                                        v->voname,
01569                                        v->std[j]->group);
01570 // No Subgroup support 
01571 //                        if ((lcmaps_vo_data[i].subgroup != NULL) && (strlen(lcmaps_vo_data[i].subgroup) > 1))
01572 //                            sprintf(query, "%s and vo_subgroup = '%s'", query, lcmaps_vo_data[i].subgroup);
01573 //                        else
01574 //                            sprintf(query, "%s and vo_subgroup = ''", query);
01575  
01576                         if ((v->std[j]->role != NULL) && (strlen(v->std[j]->role) > 1))
01577                             sprintf(query, "%s and vo_role = '%s'", query, v->std[j]->role);
01578                         else
01579                             sprintf(query, "%s and vo_role = ''", query);
01580  
01581                         if ((v->std[j]->cap != NULL) && (strlen(v->std[j]->cap) > 1))
01582                             sprintf(query, "%s and vo_capability = '%s'", query, v->std[j]->cap);
01583                         else
01584                             sprintf(query, "%s and vo_capability = ''", query);
01585  
01586                         if (SQL_Query(query) != 0)
01587                         {
01588                             SQL_Rollback();
01589                             goto fail_jobrep;
01590                         }
01591  
01592                         if (SQL_GetQueryResult(&resultSet) > 0)
01593                         {
01594                             if (SQL_GetValue(resultSet, "voms_id", &voms_id) != 0)
01595                             {
01596                                 lcmaps_log_debug(1, "Could not get value: %s  (possibly the asked value doesn't exist in resultSet) from the DB resultset with query: %s\n", "voms_id", query);
01597                                 SQL_Rollback();
01598                                 goto fail_jobrep;
01599                             }
01600                         }
01601                         else
01602                         {
01603 //                            sprintf(query, "insert into voms (vo, vo_group, vo_subgroup, vo_role, vo_capability, last_use) ");
01604 //                            sprintf(query, "insert into voms (vo, vo_group, vo_role, vo_capability) ");
01605                             sprintf(query, "insert into voms (vo, vo_group, vo_role, vo_capability, fqan) ");
01606  
01607                             sprintf(query, "%s values ('%s', '%s'",
01608                                             query,
01609                                             v->voname,
01610                                             v->std[j]->group);
01611 // No Subgroup support 
01612 //                            if ((lcmaps_vo_data[i].subgroup != NULL) && (strlen(lcmaps_vo_data[i].subgroup) > 1))
01613 //                                sprintf(query, "%s, '%s'", query, lcmaps_vo_data[i].subgroup);
01614 //                            else
01615 //                                sprintf(query, "%s, ''", query);
01616  
01617                             if ((v->std[j]->role != NULL) && (strlen(v->std[j]->role) > 1))
01618                                 sprintf(query, "%s, '%s'", query, v->std[j]->role);
01619                             else
01620                                 sprintf(query, "%s, ''", query);
01621  
01622                             if ((v->std[j]->cap != NULL) && (strlen(v->std[j]->cap) > 1))
01623                                 sprintf(query, "%s, '%s'", query, v->std[j]->cap);
01624                             else
01625                                 sprintf(query, "%s, ''", query);
01626 
01627                             sprintf(query, "%s, '%s'", query, createFQAN(v->voname, v->std[j]->group, v->std[j]->role, v->std[j]->cap));
01628                             sprintf(query, "%s)", query);
01629 
01630                             if (SQL_Query(query) != 0)
01631                             {
01632                                 SQL_Rollback();
01633                                 goto fail_jobrep;
01634                             }
01635  
01636  
01637                             // Auto Increment ID retrieval
01638                             sprintf(query, "select last_insert_id()");
01639                             if (SQL_Query(query) != 0)
01640                             {
01641                                 SQL_Rollback();
01642                                 goto fail_jobrep;
01643                             }
01644  
01645                             if (SQL_GetQueryResult(&resultSet) > 0)
01646                             {
01647                                 if (SQL_GetValue(resultSet, "last_insert_id()", &voms_id) != 0)
01648                                 {
01649                                     lcmaps_log_debug(1, "Could not get value: %s or %s (possibly the asked value doesn't exist in resultSet) from the DB resultset with query: %s\n", "voms_id", "last_insert_id()", query);
01650  
01651                                     SQL_Rollback();
01652                                     goto fail_jobrep;
01653                                 }
01654                             }
01655                             else
01656                             {
01657                                 lcmaps_log_debug(1, "Strange error at a strange location\n");
01658                                 SQL_Rollback();
01659                                 goto fail_jobrep;
01660                             }
01661 
01662                         }
01663 
01664                         // Register the issuer of this voms
01665                         sprintf(query, "select issuer_id from issuers where dnsname = '%s' and serverca = '%s'", v->uri, v->serverca);
01666                         if (SQL_Query(query) != 0)
01667                         {
01668                             SQL_Rollback();
01669                             goto fail_jobrep;
01670                         }
01671 
01672                         if (SQL_GetQueryResult(&resultSet) > 0)
01673                         {
01674                             if (SQL_GetValue(resultSet, "issuer_id", &issuer_id) != 0)
01675                             {
01676                                 lcmaps_log_debug(1, "Could not get value: %s (possibly the asked value doesn't exist in resultSet) from the DB resultset with query: %s\n", "issuer_id", query);
01677                                 SQL_Rollback();
01678                                 goto fail_jobrep;
01679                             }
01680                         }                        
01681                         else
01682                         {
01683                             // insert 'issuer'
01684                             sprintf(query, "insert into issuers (dnsname, serverca) values ('%s', '%s')", v->uri, v->serverca);
01685                             if (SQL_Query(query) != 0)
01686                             {
01687                                 SQL_Rollback();
01688                                 goto fail_jobrep;
01689                             }
01690 
01691                             sprintf(query, "select last_insert_id()");
01692                             if (SQL_Query(query) != 0)
01693                             {
01694                                 SQL_Rollback();
01695                                 goto fail_jobrep;
01696                             }
01697  
01698                             if (SQL_GetQueryResult(&resultSet) > 0)
01699                             {
01700                                 if (SQL_GetValue(resultSet, "last_insert_id()", &issuer_id) != 0)
01701                                 {
01702                                     lcmaps_log_debug(1, "Could not get value: %s or %s (possibly the asked value doesn't exist in resultSet) from the DB resultset with query: %s\n", "issuer_id", "last_insert_id()", query);
01703  
01704                                     SQL_Rollback();
01705                                     goto fail_jobrep;
01706                                 }
01707                             }
01708                         }
01709 
01710                         // insert 'voms_issuer'
01711                         sprintf(query, "insert into voms_issuer (voms_id, issuer_id) values (%s, %s)", voms_id, issuer_id);
01712                         if (SQL_Query(query) != 0)
01713                         {
01714                             // Deze bestond dus al...
01715                         }
01716 
01717 
01718                         // The 'uservoms' connects a user with voms ... but first check if it has been created in another space/time
01719                         // Warning ... Issuer stuff has to be added to the query!
01720  
01721                         sprintf(query, "select user_voms_id 
01722                                           from uservoms 
01723                                          where voms_id = %s 
01724                                            and user_id = %s 
01725                                            and issuer_id = %s 
01726                                       order by first_use desc",
01727                                         voms_id, user_id, issuer_id);
01728 //                      sprintf(query, "select user_voms_id from uservoms where voms_id = %s and user_id = %s",
01729 //                                      voms_id, user_id);
01730                         if (SQL_Query(query) != 0)
01731                         {
01732                             SQL_Rollback();
01733                             goto fail_jobrep;
01734                         }
01735                         if (SQL_GetQueryResult(&resultSet) > 0)
01736                         {
01737                             if (SQL_GetValue(resultSet, "user_voms_id", &user_voms_id) != 0)
01738                             {
01739                                 lcmaps_log_debug(1, "Could not get value: %s (possibly the asked value doesn't exist in resultSet) from the DB resultset with query: %s\n", "user_voms_id", query);
01740                                 SQL_Rollback();
01741                                 goto fail_jobrep;
01742                             }
01743  
01746 //                            sprintf(query, "update uservoms set last_change = '%s' where voms_id = %s and user_id = %s", datetime, voms_id, user_id);
01747 //                            if (SQL_Query(query) != 0)
01748 //                            {
01749 //                                SQL_Rollback();
01750 //                                goto fail_jobrep;
01751 //                            }
01752                         }
01753                         else
01754                         {
01755                             // It doesn't exist, so create it...
01758 //                            sprintf(query, "insert into uservoms (voms_id, user_id, issuer_id, last_change) values (%s, %s, %s, '%s')",
01759 //                                            voms_id, user_id, issuer_id, datetime);
01760                             sprintf(query, "insert into uservoms (voms_id, user_id, issuer_id, first_use) values (%s, %s, %s, '%s')",
01761                                             voms_id, user_id, issuer_id, datetime);
01762                             if (SQL_Query(query) != 0)
01763                             {
01764                                 SQL_Rollback();
01765                                 goto fail_jobrep;
01766                             }
01767 //                            SQL_GetQueryResult(&resultSet);
01768  
01769                             // Get the 'uservoms' just inserted primary key user_voms_id
01770                             sprintf(query, "select last_insert_id()");
01771                             if (SQL_Query(query) != 0)
01772                             {
01773                                 SQL_Rollback();
01774                                 goto fail_jobrep;
01775                             }
01776  
01777                             if (SQL_GetQueryResult(&resultSet) > 0)
01778                             {
01779                                 if (SQL_GetValue(resultSet, "last_insert_id()", &user_voms_id) != 0)
01780                                 {
01781                                     lcmaps_log_debug(1, "Could not get value: %s or %s (possibly the asked value doesn't exist in resultSet) from the DB resultset with query: %s\n", "user_voms_id", "last_insert_id()", query);
01782  
01783                                     SQL_Rollback();
01784                                      goto fail_jobrep;
01785                                 }
01786                             }
01787                             else
01788                             {
01789                                 lcmaps_log_debug(1, "Strange error at a strange location\n");
01790                                 SQL_Rollback();
01791                                 goto fail_jobrep;
01792                             }
01793                         }
01794  
01795                         // The 'vomsjob' connects a user and his vomsinfo, that he gathered this time, with a job
01796                         sprintf(query, "insert into vomsjobs (user_voms_id, job_id, use_time) values(%s, '%s', '%s')",
01797                                         user_voms_id, getenv("JOB_REPOSITORY_ID"), datetime);
01798                         if (SQL_Query(query) != 0)
01799                         {
01800                             SQL_Rollback();
01801                             goto fail_jobrep;
01802                         }
01803                         j++;
01804                     }
01805                     break;
01806             }
01807         }
01808         free(voms_id);
01809         free(user_voms_id);
01810         free(issuer_id);
01811         free(cnt);
01812     }
01813 
01814 
01815 
01816 
01817 
01818 
01819     vo_mapping = (lcmaps_vo_mapping_t *)getCredentialData(LCMAPS_VO_CRED_MAPPING, &k);
01820     if (k > 0)
01821     {
01822         for (i = 0; i < k; i++)
01823         {
01824             // Mapping between voms triplet/fqan and pgid
01825             sprintf(query, "insert into mapping_voms_pgid (pgid, voms_id, use_time)
01826                                  select credentials.gid, voms.voms_id, '%s' from voms, credentials where credentials.gid = %d and voms.fqan = '%s'",
01827                                                            datetime, vo_mapping[i].gid, tripletLine2FQAN (vo_mapping[i].vostring));
01828             SQL_Query(query);
01829 
01830 
01831             // Mapping between voms triplet/fqan and sgid
01832             sprintf(query, "insert into mapping_voms_sgid (sgid, voms_id, use_time)
01833                                  select credential_groups.sgid, voms.voms_id, '%s' from voms, credential_groups where credential_groups.sgid = %d and voms.fqan = '%s'",
01834                                                            datetime, vo_mapping[i].gid, tripletLine2FQAN (vo_mapping[i].vostring));
01835 
01836             SQL_Query(query);
01837         }
01838     }
01839 
01840 
01841     /* succes */
01842 // success_voms:
01843 
01844     lcmaps_log_debug(1,"%s: doing VOMS_Destroy\n", logstr);
01845     VOMS_Destroy(vd);
01846     lcmaps_log_debug(1,"%s: done\n", logstr);
01847 
01848     SQL_Commit();
01849     ODBC_Disconnect();
01850 
01851     // Free stuff
01852     if (dupChain) sk_X509_free(dupChain);
01853     if (tmpFile) fclose(tmpFile);
01854 #ifdef TESTBIO
01855     if (x) X509_free(x);
01856     if (in) BIO_free(in);
01857 #endif
01858 
01859 //    if (resultSet) TResultSet_free(resultSet);
01860     if (user_id) free(user_id);
01861     if (credential_id) free(credential_id);
01862 
01863     if (credential_group_id) free(credential_group_id);
01864     if (cert_id) free(cert_id);
01865     if (MySimpleStack) TSimpleCert_free(MySimpleStack, certCount);
01866 
01867     if (datetime) free(datetime);
01868 //    if (px509_cred) X509_free(px509_cred);
01869 //    if (px509_chain) sk_X509_free(px509_chain);
01870 
01871     lcmaps_log_time(0,"%s: job repository plugin succeeded\n", logstr);
01872 
01873     return LCMAPS_MOD_SUCCESS;
01874 
01875  fail_jobrep:
01876 
01877     if (connected)
01878         SQL_Rollback();
01879 
01880     ODBC_Disconnect();
01881 
01882     // Free stuff
01883 
01884     lcmaps_log_debug(1,"%s: doing VOMS_Destroy\n", logstr);
01885     VOMS_Destroy(vd);
01886     lcmaps_log_debug(1,"%s: done\n", logstr);
01887 
01888     // Free stuff
01889     if (dupChain) sk_X509_free(dupChain);
01890     if (tmpFile) fclose(tmpFile);
01891 #ifdef TESTBIO
01892     if (x) X509_free(x);
01893     if (in) BIO_free(in);
01894 #endif
01895  
01896 //    if (resultSet) TResultSet_free(resultSet);
01897     if (user_id) free(user_id);
01898     if (credential_id) free(credential_id);
01899  
01900     if (credential_group_id) free(credential_group_id);
01901     if (cert_id) free(cert_id);
01902     if (MySimpleStack) TSimpleCert_free(MySimpleStack, certCount);
01903  
01904     if (datetime) free(datetime);
01905 //    if (px509_cred) X509_free(px509_cred);
01906 //    if (px509_chain) sk_X509_free(px509_chain);
01907 
01908     lcmaps_log_time(0,"%s: job repositroy plugin failed\n", logstr);
01909 
01910     return LCMAPS_MOD_FAIL;
01911 }
01912 
01913 
01914 
01915 /******************************************************************************
01916 Function:   plugin_terminate
01917 Description:
01918     Terminate plugin
01919 Parameters:
01920 
01921 Returns:
01922     LCMAPS_MOD_SUCCESS : succes
01923     LCMAPS_MOD_FAIL    : failure
01924 ******************************************************************************/
01925 int plugin_terminate()
01926 {
01927     char * logstr = "\tlcmaps_plugin_voms-plugin_terminate()";
01928     lcmaps_log_debug(1,"%s: terminating\n", logstr);
01929     if (vomsdir) free(vomsdir);
01930     if (certdir) free(certdir);
01931     if (connStr) free(connStr);
01932 
01933     return LCMAPS_MOD_SUCCESS;
01934 }
01935 
01936 
01937 /******************************************************************************
01938 Function:   print_vomsdata
01939 Description:
01940     prints vomsdata from a 'struct vomsdata *'
01941 Parameters:
01942     struct vomsdata *
01943 Returns:
01944     LCMAPS_MOD_SUCCESS : succes
01945     LCMAPS_MOD_FAIL    : failure
01946 ******************************************************************************/
01947 void print_vomsdata(struct vomsdata *d)
01948 {
01949     char * logstr = "\tlcmaps_plugin_voms-print_vomsdata()";
01950     struct voms **vo = d->data;
01951     struct voms *v;
01952     int k = 0;
01953     int j =0;
01954   
01955     while(vo[k])
01956     {
01957         v = vo[k++];
01958         lcmaps_log_debug(1,"%s: %d *******************************************\n", logstr,k);
01959         lcmaps_log_debug(1,"%s: SIGLEN: %d\nUSER: %s\n", logstr, v->siglen, v->user);
01960         lcmaps_log_debug(1,"%s: UCA: %s\nSERVER: %s\n", logstr, v->userca, v->server);
01961         lcmaps_log_debug(1,"%s: SCA: %s\nVO: %s\n", logstr, v->serverca, v->voname);
01962         lcmaps_log_debug(1,"%s: URI: %s\n", logstr, v->uri);
01963 //        fprintf(stderr,"%s: DATE1: %s\n", logstr, d2i_ASN1_TIME(v->date1));
01964 //        fprintf(stderr,"%s: DATE2: %s\n", logstr, d2i_ASN1_TIME(v->date2));
01965 //        lcmaps_log_debug(1,"%s: DATE1: %s\n", logstr, v->date1);
01966 //        lcmaps_log_debug(1,"%s: DATE2: %s\n", logstr, v->date2);
01967 
01968         switch (v->type)
01969         {
01970         case TYPE_NODATA:
01971             lcmaps_log_debug(1,"%s: NO DATA\n", logstr);
01972             break;
01973         case TYPE_CUSTOM:
01974             lcmaps_log_debug(1,"%s: %*s\n", logstr, v->datalen - 10, v->custom);
01975             break;
01976         case TYPE_STD:
01977             j = 0;
01978             while (v->std[j])
01979             {
01980                 lcmaps_log_debug(1,"%s: GROUP: %s\tROLE: %s\tCAP: %s\n", logstr,v->std[j]->group,
01981                      v->std[j]->role,v->std[j]->cap);
01982                 j++;
01983             }
01984             break;
01985         }
01986     }
01987 
01988     if (d->workvo)
01989         lcmaps_log_debug(1,"%s: WORKVO: %s\n", logstr, d->workvo);
01990 
01991     if (d->extra_data)
01992         lcmaps_log_debug(1,"%s: EXTRA: %s\n", logstr, d->extra_data);
01993 }
01994 
01995 /******************************************************************************
01996 CVS Information:
01997     $Source: /cvs/fabric_mgt/gridification/lcmaps/modules/jobrepository/lcmaps_jobrep.c,v $
01998     $Date: 2004/03/04 14:21:21 $
01999     $Revision: 1.10 $
02000     $Author: okoeroo $
02001 ******************************************************************************/
02002 
02003 
02004 /******************************************************************************
02005 Function:   ASN1_GENERALIZEDTIME_2ilb
02006 Description:
02007         converts a ASN1_TIME into a time_t
02008         (It's a little hack, but should work nice and neat).
02009 
02010 Parameters:
02011         a ASN1_TIME, 
02012 
02013 Returns:
02014         a time_t
02015 
02016 ******************************************************************************/
02017 static struct tm * ASN1_TIME_2_time_t(ASN1_TIME *asn1_time)
02018 {
02019         //time_t MyTime = (time_t) 0;  
02020         int i;
02021         static struct tm newTimeSpace;
02022 
02023         if (asn1_time->length != 13) 
02024             return NULL;
02025             //return (time_t) -1;
02026 
02027         for (i = 0; i < asn1_time->length / 2; i++)
02028         {
02029             char tmp[3];
02030             
02031             tmp[0] = asn1_time->data[0 + (i*2)];
02032             tmp[1] = asn1_time->data[1 + (i*2)];
02033             tmp[2] = '\0';
02034             
02035             switch(i)
02036             {
02037                 case 0 : if (atoi(tmp) > 69)
02038                               newTimeSpace.tm_year = atoi(tmp) + 1900;
02039                          else
02040                               newTimeSpace.tm_year = atoi(tmp) + 100;
02041                          break;
02042                 case 1 : newTimeSpace.tm_mon  = atoi(tmp) - 1; break;
02043                 case 2 : newTimeSpace.tm_mday = atoi(tmp); break;
02044                 case 3 : newTimeSpace.tm_hour = atoi(tmp); break;
02045                 case 4 : newTimeSpace.tm_min  = atoi(tmp); break;
02046                 case 5 : newTimeSpace.tm_sec  = atoi(tmp); break;
02047             }
02048         }
02049         //MyTime = mktime(&newTimeSpace);
02050         //return MyTime;
02051 
02052         return (&newTimeSpace);
02053 }
02054 
02055 
02056 /******************************************************************************
02057 Function:   sttm_2_char
02058 Description:
02059         converts a struct tm into a humanly readable char * of the time it's
02060         presenting.
02061         The representation will be in a GMT time, not Local.
02062  
02063 Parameters:
02064         time
02065  
02066 Returns:
02067         The time in a humanly readle format (and compatible to MySQL) as known
02068         in the variations of GMT (GMT conversion of the time parameter seen as
02069         a localtime), LOCAL (plain old local time) or LEAVE_TIME_ITS_OK (this
02070         means you should not convert/change the time but just leave it, in
02071         reality this means give me the time as a localtime thus without
02072         conversion).
02073  
02074 ******************************************************************************/
02075 char * sttm_2_char (const struct tm * MyDateTime)
02076 {
02077     char  * datetime = malloc(sizeof(char) * 21);
02078 
02079     snprintf(datetime, 21, "%04d-%02d-%02d %02d:%02d:%02d",
02080              MyDateTime->tm_year + 1900, MyDateTime->tm_mon + 1, MyDateTime->tm_mday,
02081              MyDateTime->tm_hour, MyDateTime->tm_min, MyDateTime->tm_sec);
02082  
02083     return datetime;
02084 }
02085 
02086 
02087 
02088 /******************************************************************************
02089 Function:    strnclean
02090 Description: cleans a dirty string, forces '\0' on the bufsize of **s
02091 Parameters:
02092      input  : char **s
02093      output : char **s (cleaned string and NULLified)
02094 Returns:
02095     SUCCESS
02096     FAILURE
02097  
02098 ******************************************************************************/
02099 int strnclean (char **s, int bufsize)
02100 {
02101     int i = 0;
02102  
02103     if (s == NULL)
02104         return -1;
02105  
02106     if (*s == NULL)
02107         return -1; 
02108  
02109     for (i = 0; i < bufsize; i++)
02110     {
02111         (*s)[i] = '\0';
02112     }
02113  
02114     return 0;
02115 }
02116  
02117  
02118 /******************************************************************************
02119 Function:    strclean
02120 Description: cleans a dirty string, forces '\0' on the strlen() of **s
02121 Parameters:
02122      input  : char **s
02123      output : char **s (cleaned string and NULLified)
02124 Returns:
02125     SUCCESS
02126     FAILURE
02127  
02128 ******************************************************************************/
02129 int strclean (char **s)
02130 {
02131     return strnclean(s, strlen(*s));
02132 }
02133 /*****************************************************************************/
02134 
02135 
02136 /******************************************************************************
02137 Function:   lcmaps_get_jobrep_config()
02138 
02139 Description:
02140     Get the Job Repository configuration string from file
02141     This contains the DNS of the server, the database name, username and password.
02142 
02143 Parameters:
02144     path:   path to the jobrep_config file containing the configuration
02145     jobrep_config : variable to set the config string in.
02146 
02147 Returns:
02148     0 on success.
02149     1 on failure
02150 ******************************************************************************/
02170 static int lcmaps_get_jobrep_config(
02171         const char * path,
02172         char       ** jobrep_config
02173     )
02174 {
02175     char *        logstr = "\tlcmaps_plugin_jobrepository-lcmaps_get_jobrep_config()";
02176     FILE        * fp = NULL;
02177     struct stat   buf;
02178     int           c;
02179     int           count = 0;
02180     char        * tempconfig = NULL;
02181 
02182     lcmaps_log_debug(3,"%s: path: %s \n", logstr, path);
02183 
02184     /* get the stats of the file */
02185     if ((stat(path, &buf)) == -1)
02186     {
02187         lcmaps_log(0, "%s: unable to get stat of jobrep_config file: %s\n", logstr, path);
02188         return 1;
02189     }
02190 
02191     /* test if it's a file */
02192     if ( !(S_ISREG(buf.st_mode)))
02193     {
02194         lcmaps_log(0, "%s: jobrep_config file is not a file: %s\n", logstr, path);
02195         return 1;
02196     }
02197     /* test if root is owner */
02198     if (buf.st_uid != 0) {
02199         lcmaps_log(0, "%s: jobrep_config file is not owned by root (readonly for root) : %s\n", logstr, path);
02200         return 1;
02201     }
02202 
02203     /* test if file has the access bits set correctly */
02204     if ((0000777  & (buf.st_mode) ) != S_IRUSR )
02205     {
02206         lcmaps_log(0, "%s: jobrep_config file has incorrect accessibility (readonly for root) : %s\n", logstr, path);
02207         return 1;
02208     }
02209 
02210     /* try to open the file */
02211     if ((fp = fopen(path, "r")) == NULL)
02212     {
02213         lcmaps_log(0, "%s: unable to open jobrep_config file: %s\n", logstr, path);
02214         return 1;
02215     }
02216     /* read the passwd from the file */
02217     /* I thought tabs and spaces were not allowed in the passwd, but they are, so
02218      * restore original line
02219      */
02220 //    while ((c = getc(fp)) != EOF && (strchr(WHITESPACE_CHARS, c) == NULL)) count++;
02221     while ((c = getc(fp)) != EOF && (c != '\n')) count++;
02222     tempconfig = (char *) malloc(count * sizeof(char) + 2);
02223 
02224     if ((fseek(fp, 0L, SEEK_SET)) != 0)
02225     {
02226         lcmaps_log(0, "%s: unable to reset the fp\n", logstr);
02227         fclose(fp);
02228         return 1;
02229     }
02230 
02231     if ((fgets(tempconfig, count+1, fp)) == NULL )
02232     {
02233         lcmaps_log(0, "%s: unable to read the configuration: fgets failed\n", logstr);
02234         fclose(fp);
02235         return 1;
02236     }
02237 
02238     fclose(fp);
02239     lcmaps_log_debug(3,"%s: configuration: %s\n", logstr, tempconfig);
02240 //    *ldap_passwd = strdup(" s t\ts   ");
02241     *jobrep_config = strdup(tempconfig);
02242 
02243     /* free mem */
02244     if (tempconfig) free(tempconfig);
02245 
02246     return 0;
02247 }

Generated at Thu Mar 4 17:39:03 2004 for edg-lcmaps by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001