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

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