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

voms2gacl.c

00001 /*
00002  * Copyright (c) 2001 EU DataGrid.
00003  * For license conditions see http://www.eu-datagrid.org/license.html
00004  *
00005  * Copyright (c) 2001, 2002 by
00006  *     Oscar Koeroo <okoeroo@nikhef.nl>,
00007  *     NIKHEF Amsterdam, the Netherlands
00008  */
00009 
00010 
00011 /*****************************************************************************
00012                             Include header files
00013 ******************************************************************************/
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include <string.h>
00017 #include <gacl.h>
00018 
00019 /******************************************************************************
00020                                 Definitions
00021 ******************************************************************************/
00022 
00023 #define SUCCESS 0
00024 #define FAILURE 1
00025 #define SEARCH_START_QUOTE 1
00026 #define SEARCH_END_QUOTE   2
00027 #define QUOTE_CHAR  '\"'
00028 #define LINESIZE 5000
00029 #define MAXLINES 6000
00030 #define REM "#"
00031 #define DEFAULTOUTPUTFILE "output.gacl"
00032 #define PREFIX_VO        "/VO="
00033 #define PREFIX_GROUP     "/GROUP="
00034 #define PREFIX_SUBGROUP  "/SUBGROUP="
00035 #define PREFIX_ROLE      "/ROLE="
00036 #define PREFIX_CAPS      "/CAPABILITY="
00037 #define PREFIX_SLASHSTAR "/*"
00038 #define PREFIX_STAR      "*"
00039 #define VO_DATA_WHITESPACE_CHARS " \t\n"   
00040 
00041 /******************************************************************************
00042                           Module specific prototypes
00043 ******************************************************************************/
00044 
00045 void cleanupStatics (void);
00046 void showhelp (void); 
00047 int strclean  (char **s);
00048 int strnclean (char **s, int bufsize);
00049 int handle_parameters (int argc, char **argv);
00050 int handle_gridmapfile (char * gridmapfile,  GACLacl **acl);
00051 int handle_groupmapfile(char * groupmapfile, GACLacl **acl);
00052 int getdnfromvo (char *vo, char ***dn, int *count);
00053 /*
00054 int strsepvoms (const char *voms,
00055                 char **vo, char ***group, char ***subgroup, 
00056                 char ***role, char ***caps,
00057                 int  *ngroup, int *nsubgroup, int *nrole, int *ncaps);
00058 */
00059 int strsepvoms (const char *voms, char **result, const char *prefix);
00060 
00061 int filetest();
00062 
00063 /******************************************************************************/
00064 
00065 typedef struct {
00066    char *vo;
00067    char *dn;
00068 } dnvoms_s;
00069 
00070 static char     *gridmapfile     = NULL;
00071 static char     *vomapfile       = NULL;
00072 static char     *groupmapfile    = NULL;
00073 static char     *gaclfile        = NULL;
00074 static int       verboselevel    = 0;
00075 static dnvoms_s *dnvoms          = NULL;
00076 static int       dnvoms_cnt      = 0;
00077 static int       with_star       = 0;
00078 static int       with_slash_star = 0;
00079 static int       warning         = 0;
00080 
00081 /******************************************************************************
00082 Function:   main
00083 Description:
00084     main start of the tool
00085 Parameters:
00086     argc, argv
00087     argv[0]: The name of the tool
00088     argv[?]: parameters
00089 Returns:
00090     SUCCESS
00091     FAILURE
00092 ******************************************************************************/
00093 
00094 int main (int argc,
00095           char **argv)
00096 {
00097     GACLacl   *acl = NULL;
00098 
00099 
00100     if (handle_parameters(argc, argv) != SUCCESS)
00101     {
00102         return FAILURE;
00103     }
00104     
00105     if (verboselevel > 0)
00106     {
00107         printf("Parameters successfully gathered\n");
00108     }
00109 
00110        
00111     if (filetest() != SUCCESS)
00112         return FAILURE;
00113 
00114 
00115     GACLinit();
00116 
00117     acl = GACLnewAcl();
00118 
00119     if (gridmapfile != NULL)
00120         handle_gridmapfile(gridmapfile,   &acl);
00121 
00122     if (groupmapfile != NULL)
00123         handle_groupmapfile(groupmapfile, &acl);
00124 
00125     if (vomapfile != NULL)
00126         handle_groupmapfile(vomapfile, &acl);
00127 
00128     if ( (gridmapfile  != NULL) || 
00129          (groupmapfile != NULL) ||
00130          (vomapfile    != NULL) )
00131         GACLsaveAcl(gaclfile, acl);
00132 
00133 /*
00134     if ( (gridmapfile  != NULL) || 
00135          (groupmapfile != NULL) )
00136         GACLsaveAcl(gaclfile, acl);
00137 */
00138 
00139     if (verboselevel > 0)
00140         printf ("Program ended successfully\n");
00141 
00142 
00143     GACLfreeAcl(acl);
00144     free(acl);
00145     cleanupStatics();
00146 
00147     return SUCCESS;
00148 }
00149 
00150 /******************************************************************************
00151 Function:   cleanupStatics
00152 Description:
00153             Cleans the static variables
00154 Parameters:
00155 Returns:
00156     SUCCESS
00157     FAILURE
00158 ******************************************************************************/
00159 
00160 void cleanupStatics ()
00161 {
00162     int i = 0;
00163 
00164     if (gridmapfile != NULL)
00165         free(gridmapfile);
00166 
00167     if (vomapfile != NULL)
00168         free(vomapfile);
00169 
00170     if (groupmapfile != NULL)
00171         free(groupmapfile);
00172 
00173     if ( (strncmp(gaclfile, DEFAULTOUTPUTFILE, strlen(DEFAULTOUTPUTFILE)) != 0) &&
00174          (gaclfile != NULL) )
00175         free(gaclfile);
00176 
00177     for (i = 0; i < dnvoms_cnt; i++)
00178     {
00179         free((dnvoms[i]).vo);
00180         free((dnvoms[i]).dn);
00181     }
00182     free(dnvoms);
00183 }
00184 
00185 
00186 /******************************************************************************
00187 Function:   handle_gridmapfile
00188 Description:
00189 Parameters:
00190     gridmapfile
00191 Returns:
00192     SUCCESS
00193     FAILURE
00194 ******************************************************************************/
00195 
00196 int handle_gridmapfile (char * gridmapfile, GACLacl **acl)
00197 {
00198     GACLcred  *cred      = NULL;
00199     GACLentry *entry     = NULL;
00200     FILE      *gridmap   = NULL;
00201     int        i         = 0;
00202     char     **lines     = NULL;
00203     char     **dn        = NULL;
00204     int        linecount = 0;
00205     int        dncount   = 0;
00206 
00207     gridmap = fopen(gridmapfile, "r");
00208 
00209     if (gridmap == NULL)
00210     {
00211         printf("Error: gridmapfile \"%s\" cannot be opened.\n", gridmapfile);
00212         return FAILURE;
00213     }
00214 
00215     /* Dev */
00216     lines     = NULL; // The first run must be set to NULL
00217     linecount = 0;    // The first run must start at zero
00218 
00219     do
00220     {
00221        linecount++;
00222 
00223        /* Create a line pointer */
00224        lines = (char **) realloc (lines, (sizeof(char *) * linecount));
00225 
00226        /* Create mem space for a line */
00227        lines[linecount - 1] = (char *)  malloc (sizeof(char) * LINESIZE);
00228     }
00229     while (fgets(lines[linecount - 1], LINESIZE, gridmap) != NULL);
00230 
00231 
00232     /* Close file */
00233     fclose(gridmap);
00234 
00235     /* Clean the lines */
00236     for (i = 0; i < linecount; i++)
00237     {
00238         // No space plz
00239         lines[i] += strspn(lines[i], VO_DATA_WHITESPACE_CHARS);
00240 
00241         // Clear a empty line
00242         if (strlen(lines[i]) <= 1)
00243         {
00244             continue;
00245         }
00246 
00247         // Clear lines prefixing a REM block (usually '#')
00248         if (strncmp(strtok(lines[i], "\"\n"), REM, strlen(REM)) != 0)
00249         {
00250             dn = realloc (dn, (sizeof(char *) * ++dncount));
00251             dn[dncount - 1] = malloc(sizeof(char) * LINESIZE);
00252             
00253             dn[dncount - 1] = strtok(lines[i], "\"\n");
00254         }
00255     }
00256 
00257     /* DNs */
00258     if (verboselevel > 1)
00259     {
00260         for (i = 0; i < dncount; i++)
00261         {
00262             printf("%s\n", dn[i]);
00263         }
00264     }
00265 
00266     for (i = 0; i < dncount; i++)
00267     {
00268         if (strncmp("/VO", dn[i], strlen("/VO")) != 0)
00269         {
00270             cred = GACLnewCred("person");
00271             GACLaddToCred(cred, "dn", dn[i]);
00272             
00273             entry = GACLnewEntry();
00274             GACLaddCred(entry, cred);
00275 
00276             GACLallowPerm(entry, GACL_PERM_READ);            
00277             GACLallowPerm(entry, GACL_PERM_WRITE);            
00278             
00279             GACLdenyPerm(entry, GACL_PERM_ADMIN);
00280 
00281             GACLaddEntry(*acl, entry);
00282         }
00283     }
00284 
00285 /*
00286     // Clean up    
00287     for (i = 0; i < MAXLINES; i++)
00288     {
00289         free(lines[i]);
00290     }
00291     free(lines);
00292 */
00293 
00294 /*  Anti Seg. Fault : Probably still in use of GACL lib components.
00295     for (i = 0; i < dncount; i++)
00296     {
00297         free(dn[i]); // Why does Valgrind complain about this free?
00298     }
00299     free(dn);
00300 */
00301 
00302     if (verboselevel > 2)
00303         printf("End Marker Parsing and cleaning\n");
00304 
00305     // End
00306     return SUCCESS;
00307 }
00308 
00309 
00310 
00311 /******************************************************************************
00312 Function:   handle_groupmapfile
00313 Description:
00314 Parameters:
00315     gridmapfile
00316 Returns:
00317     SUCCESS
00318     FAILURE
00319 ******************************************************************************/
00320 
00321 int handle_groupmapfile (char * groupmapfile, GACLacl **acl)
00322 {
00323     GACLcred  *cred      = NULL;
00324     GACLentry *entry     = NULL;
00325     FILE      *groupmap  = NULL;
00326     int        i         = 0;
00327     int        t         = 0;
00328     char     **lines     = NULL;
00329     char     **vo        = NULL;
00330     int        linecount = 0;
00331     int        vocount   = 0;
00332 
00333     /* voms separation data */
00334     char      *sepvo       = NULL;
00335     char      *sepgroup    = NULL;
00336     char      *sepsubgroup = NULL;
00337     char      *seprole     = NULL;    
00338     char      *sepcaps     = NULL;
00339 /*
00340     int        nsubgroup   = 0;
00341     int        nrole       = 0;
00342     int        ncaps       = 0; 
00343     int        ngroup      = 0;
00344 */   
00345     /* voms and DNs */
00346     char     **voms        = 0;
00347     int        count       = 0;
00348 
00349 
00350     groupmap = fopen(groupmapfile, "r");
00351 
00352 
00353     if (groupmap == NULL)
00354     {
00355         printf("Error: groupmapfile \"%s\" cannot be opened.\n", groupmapfile);
00356         return FAILURE;
00357     }
00358 
00359 /*
00360     lines   = (char **) malloc (sizeof(char *) * MAXLINES);
00361     for (i = 0; i < MAXLINES; i++) { lines[i] = (char *)  malloc (sizeof(char) * LINESIZE); }
00362 
00363     linecount = 0;
00364     while (fgets(lines[linecount], LINESIZE, groupmap) != NULL) { linecount++; }
00365 */
00366 
00367     do
00368     {
00369        linecount++;
00370 
00371        /* Create a line pointer */
00372        lines = (char **) realloc (lines, (sizeof(char *) * linecount));
00373 
00374        /* Create mem space for a line */
00375        lines[linecount - 1] = (char *)  malloc (sizeof(char) * LINESIZE);
00376     }
00377     while (fgets(lines[linecount - 1], LINESIZE, groupmap) != NULL);
00378 
00379 
00380     /* close file */
00381     fclose(groupmap);
00382 
00383 
00384     /* Clean the lines */
00385     for (i = 0; i < linecount; i++)
00386     {
00387         // No space plz
00388         lines[i] += strspn(lines[i], VO_DATA_WHITESPACE_CHARS); 
00389 
00390         // Clear a empty line
00391         if (strlen(lines[i]) <= 1)
00392         {
00393             continue;
00394         }
00395  
00396         // Clear lines prefixing a REM block (usually '#')
00397         if (strncmp(strtok(lines[i], "\"\n"), REM, strlen(REM)) != 0)
00398         {
00399             vo = realloc (vo, (sizeof(char *) * ++vocount));
00400             vo[vocount - 1] = malloc(sizeof(char) * LINESIZE);
00401             
00402             vo[vocount - 1] = strtok(lines[i], "\"\n");
00403         }
00404     }
00405 
00406 
00407     /* VO-GROUP-ROLEs */
00408     if (verboselevel > 0)   
00409     {
00410         for (i = 0; i < vocount; i++)
00411         {
00412             printf("%s\n", vo[i]);
00413         }
00414     }
00415 
00416     sepvo       = (char *) malloc(sizeof(char) * LINESIZE);
00417     sepgroup    = (char *) malloc(sizeof(char) * LINESIZE);
00418     sepsubgroup = (char *) malloc(sizeof(char) * LINESIZE);
00419     seprole     = (char *) malloc(sizeof(char) * LINESIZE);
00420     sepcaps     = (char *) malloc(sizeof(char) * LINESIZE);
00421 
00422 
00423     /* VO-GROUP-ROLE parsing */
00424     for (i = 0; i < vocount; i++)
00425     {
00426         if (strncmp("/VO", vo[i], strlen("/VO")) == 0)
00427         {
00428 
00429             strnclean (&sepvo, LINESIZE);
00430             strnclean (&sepgroup, LINESIZE);
00431             strnclean (&sepsubgroup, LINESIZE);
00432             strnclean (&seprole, LINESIZE);
00433             strnclean (&sepcaps, LINESIZE);
00434 
00435 
00436             strsepvoms(vo[i], &sepvo,       "/VO=");
00437             strsepvoms(vo[i], &sepgroup,    "/GROUP=");
00438             strsepvoms(vo[i], &sepsubgroup, "/SUBGROUP=");
00439             strsepvoms(vo[i], &seprole,     "/ROLE=");
00440             strsepvoms(vo[i], &sepcaps,     "/CAPABILITY=");
00441 
00442             if (voms != NULL)
00443             {
00444                 for (t = 0; t < count; t++)
00445                 {
00446                     strclean(&(voms[t]));
00447                     free(voms[t]);
00448                 }
00449                 free(voms);
00450                 voms = NULL;
00451             }
00452 
00453             if (getdnfromvo (sepvo, &voms, &count) == SUCCESS)
00454             {
00455                 for (t = 0; t < count; t++)
00456                 {
00457                     cred = GACLnewCred("voms-cred");
00458 
00459                     GACLaddToCred(cred, "voms", voms[t]);
00460             
00461                     if (strlen(sepvo) > 0)
00462                         GACLaddToCred(cred, "vo", sepvo);
00463                     if (strlen(sepgroup) > 0)
00464                         GACLaddToCred(cred, "group", sepgroup);
00465                     if (strlen(sepsubgroup) > 0)
00466                         GACLaddToCred(cred, "subgroup", sepsubgroup);
00467                     if (strlen(seprole) > 0)
00468                         GACLaddToCred(cred, "role", seprole);
00469                     if (strlen(sepcaps) > 0)
00470                         GACLaddToCred(cred, "capability", sepcaps);
00471                 
00472                     entry = GACLnewEntry();
00473                     GACLaddCred(entry, cred);
00474                     GACLallowPerm(entry, GACL_PERM_READ);            
00475                     GACLallowPerm(entry, GACL_PERM_WRITE);            
00476                 
00477                     GACLdenyPerm(entry, GACL_PERM_ADMIN);
00478     
00479                     GACLaddEntry(*acl, entry);
00480                 }        
00481             }
00482             else
00483             {
00484                 if (!warning)
00485                     printf("Warning: This entry with VO '%s' contains no data about the DN of the VOMS server.\n", sepvo);
00486 
00487                 cred = GACLnewCred("voms-cred");
00488     
00489 //                GACLaddToCred(cred, "voms", vo[i]);  // No knowledge yet about which VOMS server should be inserted here
00490 //                GACLaddToCred(cred, "voms", "");
00491     
00492                 if (strlen(sepvo) > 0)
00493                     GACLaddToCred(cred, "vo", sepvo);
00494                 if (strlen(sepgroup) > 0)
00495                     GACLaddToCred(cred, "group", sepgroup);
00496                 if (strlen(sepsubgroup) > 0)
00497                     GACLaddToCred(cred, "subgroup", sepsubgroup);
00498                 if (strlen(seprole) > 0)
00499                     GACLaddToCred(cred, "role", seprole);
00500                 if (strlen(sepcaps) > 0)
00501                     GACLaddToCred(cred, "capability", sepcaps);
00502                 
00503                 entry = GACLnewEntry();
00504                 GACLaddCred(entry, cred);
00505                 GACLallowPerm(entry, GACL_PERM_READ);            
00506                 GACLallowPerm(entry, GACL_PERM_WRITE);            
00507                 
00508                 GACLdenyPerm(entry, GACL_PERM_LIST);            
00509                 GACLdenyPerm(entry, GACL_PERM_ADMIN);
00510     
00511                 GACLaddEntry(*acl, entry);
00512             }
00513 
00514             strnclean (&sepvo, LINESIZE);
00515             strnclean (&sepgroup, LINESIZE);
00516             strnclean (&sepsubgroup, LINESIZE);
00517             strnclean (&seprole, LINESIZE);
00518             strnclean (&sepcaps, LINESIZE);
00519         }
00520     }
00521 
00522     free(sepvo);
00523     free(sepgroup);
00524     free(sepsubgroup);
00525     free(seprole);
00526     free(sepcaps);
00527 
00528 /*
00529     // Clean up    
00530     for (i = 0; i < MAXLINES; i++)
00531     {
00532         free(lines[i]);
00533     }
00534     free(lines);
00535 */
00536 
00537 
00538 /*  For details see handle_gridmapfile function
00539     for (i = 0; i < vocount; i++)
00540     {
00541         free(vo[i]);
00542     }
00543     free(vo);
00544 */
00545 
00546 
00547     if (verboselevel > 2)
00548         printf("End Marker Parsing and cleaning\n");
00549 
00550     return SUCCESS;
00551 }
00552 
00553 
00554 /******************************************************************************
00555 Function:    strsepvoms
00556 Description: Separates a string of voms credentials into different 
00557              parameters starting with the 'prefix'
00558 Parameters:
00559      input : char *voms,
00560              const char *prefix 
00561      output: char ** result 
00562 Returns:
00563     SUCCESS
00564     FAILURE
00565 BUGS:
00566     -    It doesn't get the second- or subgroup like: .../GROUP=grp1/grp2
00567          This will be fixed.
00568     -    Multiple Groups, Roles, Capabilities cannot yet be gathered and transformed
00569          into GACL
00570 
00571 ******************************************************************************/
00572 
00573 int strsepvoms (const char *voms, char **result, const char *prefix)
00574 {
00575     int i = 0;
00576     char *tmpvoms   = NULL;
00577     char *tmpresult = NULL;
00578     char *oldtmpvoms = NULL;
00579 
00580     tmpvoms   = strdup(voms);
00581     tmpresult = (char *) malloc(sizeof(char) * LINESIZE);
00582     strnclean(&tmpresult, LINESIZE);
00583 
00584     // debug
00585     oldtmpvoms = tmpvoms;
00586 
00587     do
00588     {
00589         // Determine case-insensitive if the prefix is at the beginning of the string
00590         if (strncasecmp(tmpvoms, prefix, strlen(prefix)) == 0)
00591         {
00592             if (verboselevel > 3)
00593                 printf("Prefix \"%s\" found, stepping over prefix string\n", prefix);
00594 
00595             // Stepping over prefix
00596             for (i = 0; i < strlen(prefix); i++)
00597             {
00598                 tmpvoms++;
00599             }
00600 
00601             // Determining the length of the found string by searching for an end character
00602             i = 0;
00603             while (tmpvoms[i] != '\0')
00604             {
00605                 if (strlen(tmpvoms) - i >= (strlen(PREFIX_VO)))
00606                 {
00607                     if (strncmp(PREFIX_VO, &tmpvoms[i], strlen(PREFIX_VO)) == 0)
00608                     {
00609                         // You have found the end character of the wanted substring
00610                         break;
00611                     }
00612                 }
00613 
00614                 if (strlen(tmpvoms) - i >= (strlen(PREFIX_GROUP)))
00615                 {
00616                     if (strncmp(PREFIX_GROUP, &tmpvoms[i], strlen(PREFIX_GROUP)) == 0)
00617                     {
00618                         break;
00619                     }
00620                 }
00621 
00622                 if (strlen(tmpvoms) - i >= (strlen(PREFIX_SUBGROUP)))
00623                 {
00624                     if (strncmp(PREFIX_SUBGROUP, &tmpvoms[i], strlen(PREFIX_SUBGROUP)) == 0)
00625                     {
00626                         break;
00627                     }
00628                 }
00629 
00630                 if (strlen(tmpvoms) - i >= (strlen(PREFIX_ROLE)))
00631                 {
00632                     if (strncmp(PREFIX_ROLE, &tmpvoms[i], strlen(PREFIX_ROLE)) == 0)
00633                     {
00634                         break;
00635                     }
00636                 }
00637 
00638                 if (strlen(tmpvoms) - i >= (strlen(PREFIX_CAPS)))
00639                 {
00640                     if (strncmp(PREFIX_CAPS, &tmpvoms[i], strlen(PREFIX_CAPS)) == 0)
00641                     {
00642                         break;
00643                     }
00644                 }
00645 
00646                 // if the cmdline parameter -with_slash_star is set it will add the star behind the group name
00647                 // like    VOMS :  /GROUP=fred/*  =>  GACL :  <GROUP>fred/*</GROUP>
00648                 // Default VOMS :  /GROUP=fred/*  =>  GACL :  <GROUP>fred</GROUP>
00649                 if (!with_slash_star)
00650                 {
00651                     if (strlen(tmpvoms) - i >= (strlen(PREFIX_SLASHSTAR)))
00652                     {
00653                         if (strncmp(PREFIX_SLASHSTAR, &tmpvoms[i], strlen(PREFIX_SLASHSTAR)) == 0)
00654                         {
00655                             break;
00656                         }
00657                     }
00658                 }
00659 
00660                 // if the cmdline parameter -with_star is set it will add the star behind the group name
00661                 // like    VOMS :  /GROUP=fred*  =>  GACL :  <GROUP>fred*</GROUP>
00662                 // Default VOMS :  /GROUP=fred*  =>  GACL :  <GROUP>fred</GROUP>
00663                 if (!with_star)
00664                 {
00665                     if (strlen(tmpvoms) - i >= (strlen(PREFIX_STAR)))
00666                     {
00667                         if ( (strncmp(PREFIX_STAR, &tmpvoms[i], strlen(PREFIX_STAR)) == 0) && 
00668                              (i > 0) && 
00669                              (strncmp(PREFIX_SLASHSTAR, &tmpvoms[i - 1], strlen(PREFIX_SLASHSTAR)) != 0) )
00670                         {
00671                             break;
00672                         }
00673                     }
00674                 }
00675 
00676                 // If none of the above aprove ... keep on counting
00677                 i++;
00678             }
00679 
00680             // Determining the length of the found string by searching for an end character
00681 //            i = 0;
00682 //            while ((tmpvoms[i] != '/') && (tmpvoms[i] != '\0') && (tmpvoms[i] != '*'))
00683 //            {
00684 //                i++;
00685 //            }
00686 
00687             // Copy found string to a result of determined length
00688             strncpy(tmpresult, tmpvoms, i);
00689             
00690             if (verboselevel > 3)
00691             {
00692                 printf ("\ttmpresult = \"%s\"\n", strlen(tmpresult) ? tmpresult : "(null)");
00693             }
00694 
00695             // Stop do-while loop because we already found something and 
00696             // we can only find the first occurence of the prefix in the string
00697             break;
00698         }
00699         else
00700         {
00701             strnclean(&tmpresult, LINESIZE);
00702         }
00703 
00704         // Stepping thru a line to be parsed
00705         strcpy(tmpvoms, &tmpvoms[1]); // Step forwards
00706     }
00707     while (i < strlen(tmpvoms));
00708 
00709     strncpy(*result, tmpresult, strlen(tmpresult));
00710 
00711     free(tmpresult);
00712     free(oldtmpvoms);
00713 
00714     return SUCCESS;
00715 
00716 }
00717 
00718 /******************************************************************************
00719 Function:   showhelp
00720 Description:
00721 Parameters:
00722 Returns:
00723 ******************************************************************************/
00724 void showhelp () 
00725 {
00726     printf("\n");
00727     printf("\t-gridmapfile  <filepath>  -  Handles a 'gridmapfile' or 'grid-mapfile' as \n");
00728     printf("\t                             we use it with for local- and poolaccount\n");
00729     printf("\t-groupmapfile <filepath>  -  Handles a 'groupmapfile' with VO-GROUP-ROLEs and\n");
00730     printf("\t                             map-information for the local- and poolgroups\n");
00731 //    printf("\t-vomapfile    <filepath>  -  Handles a 'vomapfile' (this information is sometimes\n");
00732 //    printf("\t                             at the end of a 'gridmapfile') containing\n");
00733 //    printf("\t                             VO-GROUP-ROLE map-information to poolaccounts\n");
00734     printf("\t-gaclfile     <filepath>  -  Assigned output file (default: '%s')\n", DEFAULTOUTPUTFILE);
00735     printf("\t-vomsdn  <VO> <voms dn>   -  Overrides the DN with <voms dn> for every entry that is\n");
00736     printf("\t                             assosiated with the VO <VO> of the voms server\n");
00737     printf("\t                             NOTE: a VO can have multiple voms servers\n");
00738     printf("\t-v[v[v[v]]]               -  Extra diagnostic information\n");
00739     printf("\t                             NOTE: a verboselevel above 1 will trigger a file check\n");
00740     printf("\t                                   a verboselevel above 3 will give extra parsing information\n");
00741     printf("\t-with_star                -  When this parameter is set, the GACL file will include the wildcard\n");
00742     printf("\t                             in the credential part of the ACL Entry.\n");
00743     printf("\t                             NOTE: this is for all known VOMS credential types: VO, GROUP, SUBGROUP, ROLE or CAPABILITY\n");
00744     printf("\t-with_slash_star          -  When this parameter is set, the GACL file will include the wildcard\n");
00745     printf("\t                             in the credential part of the ACL Entry.\n");
00746     printf("\t                             NOTE: this is for all known VOMS credential types: VO, GROUP, SUBGROUP, ROLE or CAPABILITY\n");
00747     printf("\t-nowarn                   -  Skips the warning messages if they do not indicate an error.\n");
00748     printf("\n");
00749 }
00750 
00751 
00752 
00753 /******************************************************************************
00754 Function:   handle_parameters
00755 Description:
00756 Parameters:
00757     argc, argv
00758 Returns:
00759     SUCCESS
00760     FAILURE
00761 ******************************************************************************/
00762 
00763 int handle_parameters (int argc, char **argv)
00764 {
00765     int i;
00766 
00767     /*
00768      * Atleast one parameter must be present to do 'something'
00769      */
00770 
00771     if (!(argc > 1))
00772     {
00773         printf("Error: no parameters found, nothing to do...\n");   
00774         showhelp();
00775 
00776         return FAILURE;
00777     }
00778 
00779     for (i = 1; i < argc; i++)
00780     {
00781         if ( (strcmp(argv[i], "-vomsdn") == 0) && (i + 2< argc))
00782         {
00783             if ((argv[i + 1] != NULL) && 
00784                 (strlen(argv[i + 1]) > 0) &&
00785                 (argv[i + 2] != NULL) &&
00786                 (strlen(argv[i + 2]) > 0))
00787             {
00788                  dnvoms = realloc(dnvoms, sizeof(dnvoms_s) * ++dnvoms_cnt);
00789                  (dnvoms[dnvoms_cnt - 1]).vo = strdup(argv[i + 1]);
00790                  (dnvoms[dnvoms_cnt - 1]).dn = strdup(argv[i + 2]);
00791 
00792             }
00793             i += 2;;
00794             
00795         }
00796 
00797         else if ( (strcmp(argv[i], "-gridmapfile") == 0) && (i + 1 < argc))
00798         {
00799             if ((argv[i + 1] != NULL) && (strlen(argv[i + 1]) > 0))
00800             {
00801                  gridmapfile = strdup(argv[i + 1]);
00802             }
00803             i++;
00804         }
00805 
00806         else if ( (strcmp(argv[i], "-groupmapfile") == 0) && (i + 1 < argc))
00807         {
00808             if ((argv[i + 1] != NULL) && (strlen(argv[i + 1]) > 0))
00809             {
00810                  groupmapfile = strdup(argv[i + 1]);
00811             }
00812             i++;
00813         }
00814 
00815         else if ( (strcmp(argv[i], "-vomapfile") == 0) && (i + 1 < argc))
00816         {
00817             if ((argv[i + 1] != NULL) && (strlen(argv[i + 1]) > 0))
00818             {
00819                  vomapfile = strdup(argv[i + 1]);
00820             }
00821             i++;
00822         }
00823 
00824         else if ( (strcmp(argv[i], "-gaclfile") == 0) && (i + 1 < argc))
00825         {
00826             if ((argv[i + 1] != NULL) && (strlen(argv[i + 1]) > 0))
00827             {
00828                  gaclfile = strdup(argv[i + 1]);
00829             }
00830             i++;
00831         }
00832 
00833         else if (strncmp(argv[i], "-v", 2) == 0)
00834         {
00835             int j = 0;
00836 
00837             verboselevel = 0;
00838             while (j < strlen(argv[i]))
00839             {
00840                 if (((argv[i])[j]) == 'v')
00841                     verboselevel++;
00842 
00843                 j++;
00844             }
00845         }
00846 
00847         else if ( (strcmp(argv[i], "-h") == 0) || (strcmp(argv[i], "-help") == 0) )
00848         {
00849             showhelp();
00850         }
00851 
00852         else if ( (strcmp(argv[i], "-with_star") == 0) || (strcmp(argv[i], "-with-star") == 0) )
00853         {
00854             with_star = 1;
00855         }
00856 
00857         else if ( (strcmp(argv[i], "-with_slash_star") == 0) || (strcmp(argv[i], "-with-slash-star") == 0) )
00858         {
00859             with_slash_star = 1;
00860         }
00861 
00862         else if (strcmp(argv[i], "-nowarn") == 0) 
00863         {
00864             warning = 1;
00865         }
00866 
00867         else
00868         {
00869             printf("\n\tError: something is wrong with the parameters.\n");
00870               printf("\t       The error has been detected on \"%s\"\n", argv[i]);
00871               printf("\t       The last two parameters are : \"%s\" and \"%s\"\n\n", argv[i - 1], argv[i]);
00872             showhelp();
00873             return FAILURE;
00874         }
00875     }
00876 
00877     if (verboselevel > 0)
00878     {
00879        printf("verbose level = %d\n", verboselevel);
00880     }
00881 
00882     if (gaclfile == NULL)
00883     {
00884         gaclfile = DEFAULTOUTPUTFILE;
00885     }
00886 
00887     if (verboselevel > 2)
00888     {
00889        printf("Files initialized as:\n");
00890        printf("-gridmapfile  = %s\n", gridmapfile);
00891        printf("-groupmapfile = %s\n", groupmapfile);
00892        printf("-vomapfile    = %s\n", vomapfile);
00893        printf("-gaclfile     = %s\n", gaclfile);
00894     }
00895     else if ( (verboselevel > 0) && (verboselevel <= 2) )
00896     {
00897         printf("Output is set to: %s\n", gaclfile);
00898     } 
00899 
00900     if (verboselevel > 0)
00901     {
00902         for (i = 0; i < dnvoms_cnt; i++)
00903             printf("<VO> : %s \tis assosiated with \t<DN> : %s\n", (dnvoms[i]).vo, (dnvoms[i]).dn);
00904     }
00905 
00906     return SUCCESS;
00907 }
00908 
00909 /******************************************************************************
00910 Function:   filetest
00911 Description:
00912 Parameters:
00913     argc, argv
00914 Returns:
00915     SUCCESS
00916     FAILURE
00917 ******************************************************************************/
00918 
00919 int filetest()
00920 {
00921     FILE *test = NULL;
00922         
00923     if (gridmapfile != NULL)
00924     {
00925         if ((test = fopen(gridmapfile, "r")) != NULL)
00926         {
00927             if (verboselevel > 0)
00928                 printf("Success: gridmapfile \"%s\" readable\n", gridmapfile);
00929 
00930             fclose(test);
00931         }
00932         else
00933         {
00934             printf("Error: Can't open gridmapfile \"%s\"\n", gridmapfile);
00935             return FAILURE;
00936         }
00937     }
00938 
00939     if (groupmapfile != NULL)
00940     {
00941         if ((test = fopen(groupmapfile, "r")) != NULL)
00942         {
00943             if (verboselevel > 0)
00944                 printf("Success: groupmapfile \"%s\" readable\n", groupmapfile);
00945 
00946             fclose(test);
00947         }
00948         else
00949         {
00950             printf("Error: Can't open groupmapfile \"%s\"\n", groupmapfile);
00951             return FAILURE;
00952         }
00953     }
00954 
00955     if (vomapfile != NULL)
00956     {
00957         if ((test = fopen(vomapfile, "r")) != NULL)
00958         {
00959             if (verboselevel > 0)
00960                 printf("Success: vomapfile \"%s\" readable\n", vomapfile);
00961 
00962             fclose(test);
00963         }
00964         else
00965         {
00966             printf("Error: Can't open vomapfile \"%s\"\n", vomapfile);
00967             return FAILURE;
00968         }
00969     }
00970 
00971     if (verboselevel > 1)
00972     {
00973         if (gaclfile != NULL)
00974         {
00975             if ((test = fopen(gaclfile, "r")) != NULL)
00976                 fclose(test);
00977             else
00978             {
00979                 printf("Warning: Can't open gaclfile \"%s\"\nA new gaclfile will be used.", gaclfile);
00980             }
00981         }
00982     }
00983 
00984     return SUCCESS;
00985 }
00986 
00987 
00988 
00989 
00990 /******************************************************************************
00991 Function:    strnclean
00992 Description: cleans a dirty string, forces '\0' on the bufsize of **s
00993 Parameters:
00994      input  : char **s
00995      output : char **s (cleaned string and NULLified)
00996 Returns:
00997     SUCCESS
00998     FAILURE
00999 
01000 ******************************************************************************/
01001 int strnclean (char **s, int bufsize)
01002 {
01003     int i = 0;
01004 
01005     if (s == NULL)
01006         return FAILURE;
01007 
01008     if (*s == NULL)
01009         return FAILURE;
01010 
01011     for (i = 0; i < bufsize; i++)
01012     {
01013         (*s)[i] = '\0';
01014     }
01015 
01016     return SUCCESS;
01017 }
01018 
01019 
01020 /******************************************************************************
01021 Function:    strclean
01022 Description: cleans a dirty string, forces '\0' on the strlen() of **s
01023 Parameters:
01024      input  : char **s
01025      output : char **s (cleaned string and NULLified)
01026 Returns:
01027     SUCCESS
01028     FAILURE
01029 
01030 ******************************************************************************/
01031 int strclean (char **s)
01032 {
01033     if (s == NULL)
01034         return FAILURE;
01035 
01036     if (*s == NULL)
01037         return FAILURE;
01038 
01039     if (strlen(*s) > 0)
01040     {
01041         int i = 0;
01042         
01043         for (i = 0; i < strlen(*s); i++)
01044             (*s)[i] = '\0';
01045     }
01046 
01047     return SUCCESS;
01048 }
01049 /*****************************************************************************/
01050 
01051 
01052 /******************************************************************************
01053 Function:    getdnfromvo
01054 Description: searches for the VO in the dnvoms array. The function assigns a DN of
01055              a VOMS server to a VO.
01056 Parameters:
01057      input  : char *vo
01058      output : char ***dn, int *count
01059 Returns:
01060     SUCCESS
01061     FAILURE
01062  
01063 ******************************************************************************/
01064 int getdnfromvo (char *vo, char ***dn, int *count)
01065 {
01066     int i;
01067     int resultvalue = FAILURE;
01068     char **tmp = NULL;
01069 
01070     if (*dn != NULL)
01071         return FAILURE;
01072 
01073     if (dnvoms_cnt < 1)
01074         return FAILURE;
01075 
01076     *count = 0;
01077 
01078     for (i = 0; i < dnvoms_cnt; i++)
01079     {
01080          if (strncmp((dnvoms[i]).vo, vo, strlen((dnvoms[i]).vo)) == 0)
01081          {
01082              tmp = (char **) realloc(*dn, ++(*count) * sizeof(char *));
01083 
01084              tmp[*count - 1] = strdup((dnvoms[i]).dn);
01085 //             *(dn[*count - 1]) = strdup((dnvoms[i]).dn);
01086 
01087              *dn = tmp;
01088              resultvalue = SUCCESS;
01089          }
01090     }
01091     return resultvalue;
01092 }

Generated on Mon Sep 20 15:14:15 2004 for edg-lcas by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002