00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00083 
00084 
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 
00108 
00109 #ifdef TESTBIO
00110 #    include <openssl/pem.h>
00111 #    include <openssl/bio.h>
00112 #endif
00113 
00114 
00115 
00116 
00117 
00118 
00119 #define VOMS_BUFFER_SIZE  1024
00120 
00121 
00122 #define GMT               1
00123 #define LOCAL             2
00124 #define LEAVE_TIME_ITS_OK 3
00125 
00126 
00127 
00128 
00129 
00130 void print_vomsdata(struct vomsdata *);
00131 
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 
00160 
00161 
00162 static char * jobrep_config = NULL;
00163 static char * connStr = NULL;
00164 static char * certdir = NULL;
00165 static char * vomsdir = NULL;
00166 
00167 
00168 
00169 
00170 
00171 
00172 
00173 
00174 
00175 
00176 
00177 
00178 
00179 
00180 
00181 
00182 
00183 
00184 
00185 
00186 
00187 
00188 
00189 
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 
00207 
00208     while ( ((pTL-tripletLine) < (strlen(tripletLine)+1)) && strlen(pTL) > 0)
00209     {
00210         i = 0;
00211         if (strncmp(pTL, "/VO=", strlen("/VO=")) == 0)
00212         {
00213             
00214             pTL += strlen("/VO=");
00215 
00216             
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             
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             
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             
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 
00353 
00354 
00355 
00356 
00357 
00358 
00359 
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     
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     
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     
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     
00420     while (sk_X509_INFO_num(sk))
00421     {
00422         
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 
00452 
00453 
00454 
00455 
00456 
00457 
00458 
00459 
00460 
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 
00481 
00482 
00483  
00484     
00485 
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         
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 
00575 
00576 
00577 
00578 
00579 
00580 
00581 
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 
00610 
00611 
00612 
00613 
00614 
00615 
00616 
00617 
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 
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 
00699 
00700 
00701 
00702 #ifdef TESTBIO
00703 #else
00704     
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     
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 
00763 
00764 
00765 
00766 #ifdef TESTBIO
00767 #else
00768     
00769 
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         
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 
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     
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 
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     
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 
00982 
00983 
00984 
00985 
00986 
00987 
00988 
00989 
00990 
00991 
00992 
00993 
00994 
00995 
00996 
00997 
00998 
00999 
01000 
01001 
01002 
01003 
01004 
01005 
01006 
01007 
01008 
01009 
01010 
01011                 
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 
01023 
01024 
01025 
01026 
01027 
01028 
01029 
01030                     
01031                     if (kar == '\'')
01032                     {
01033                         
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 
01044 
01045 
01046 
01047 
01048 
01049 
01050 
01051 
01052 
01053 
01054 
01055 
01056 
01057                 
01058                 
01059                 X509_NAME_oneline(X509_get_subject_name(MySimpleStack[ctCnt].X509cert), buffer, 999);
01060                 MySimpleStack[ctCnt].subject_name = malloc(sizeof(char) * 1000);
01061 
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 
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                 
01079                 
01080                 
01081                 
01082                 
01083                 
01084                 
01085                 
01086                 
01087                 
01088                 
01089                 
01090                 
01091                 
01092                 
01093                 
01094                 
01095                 
01096                 
01097                 
01098                 
01099                 
01100                 
01101                 
01102                 
01103                 
01104                 
01105                 
01106                 
01107                 
01109             }
01110         }
01111     }
01112 
01113 
01114 #ifdef TESTBIO
01115     if (chain) sk_X509_free(chain);
01116 #endif
01117 
01118     
01119 
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             
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     
01150     lcmaps_log_debug(1, "%s: Connected to the Job Repository Database\n", logstr); 
01151 
01152 
01153 
01154 
01155 
01156 
01157 
01158 
01159  
01160  
01161     
01162 
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 
01192 
01193 
01194 
01195     
01196     
01197 
01198     sprintf(query, "insert into users (dn) values ('%s')", dn);
01199     if (SQL_Query(query) != 0)
01200     {
01201         
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         
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 
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 
01295 
01296 
01297 
01298 
01299 
01300 
01301 
01302 
01303 
01304 
01305 
01306 
01307 
01308 
01309 
01310 
01311 
01312 
01313 
01314 
01315 
01316 
01317 
01318 
01319 
01320 
01321 
01322 
01323 
01324 
01325 
01326 
01327 
01328 
01329 
01330 
01331 
01332 
01333 
01334 
01335 
01336 
01337 
01338         if (SQL_Query(query) != 0)
01339         {
01340             
01341             
01342             
01343             
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             
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 
01408 
01409 
01410 
01411     
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             
01421 
01422 
01423 
01424 
01425 
01426 
01427 
01428             
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     
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 
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 
01478 
01479 
01480 
01481 
01482     
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         
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         
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     
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     
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             
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             
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         
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     
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         
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 
01685 
01686 
01687 
01688 
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 
01718 
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 
01726 
01727 
01728 
01729 
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                             
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                         
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                             
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                         
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                             
01829                         }
01830 
01831 
01832                         
01833                         
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 
01843 
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 
01861 
01862 
01863 
01864 
01865 
01866                         }
01867                         else
01868                         {
01869                             
01872 
01873 
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 
01883  
01884                             
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                         
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             
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             
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     
01959 
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     
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 
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 
01986 
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     
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     
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 
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 
02023 
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 
02034 
02035 
02036 
02037 
02038 
02039 
02040 
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 
02056 
02057 
02058 
02059 
02060 
02061 
02062 
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 
02081 
02082 
02083 
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 
02114 
02115 
02116 
02117 
02118 
02119 
02120 
02121 
02122 
02123 
02124 
02125 
02126 
02127 
02128 
02129 
02130 
02131 
02132 
02133 
02134 static struct tm * ASN1_TIME_2_time_t(ASN1_TIME *asn1_time)
02135 {
02136         
02137         int i;
02138         static struct tm newTimeSpace;
02139 
02140         if (asn1_time->length != 13) 
02141             return NULL;
02142             
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         
02167         
02168 
02169         return (&newTimeSpace);
02170 }
02171 
02172 
02173 
02174 
02175 
02176 
02177 
02178 
02179 
02180 
02181 
02182 
02183 
02184 
02185 
02186 
02187 
02188 
02189 
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 
02207 
02208 
02209 
02210 
02211 
02212 
02213 
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 
02237 
02238 
02239 
02240 
02241 
02242 
02243 
02244 
02245 
02246 int strclean (char **s)
02247 {
02248     return strnclean(s, strlen(*s));
02249 }
02250 
02251 
02252 
02253 
02254 
02255 
02256 
02257 
02258 
02259 
02260 
02261 
02262 
02263 
02264 
02265 
02266 
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     
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     
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     
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     
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     
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     
02334     
02335 
02336 
02337 
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 
02358     *jobrep_config = strdup(tempconfig);
02359 
02360     
02361     if (tempconfig) free(tempconfig);
02362 
02363     return 0;
02364 }