00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00034 #ifndef LCMAPS_SETUP_C
00035 #define LCMAPS_SETUP_C
00036 
00037 
00038 
00039 
00040 
00041 #include "lcmaps_config.h"
00042 #include <syslog.h>
00043 #include <errno.h>
00044 #include <ctype.h>
00045 #include <stdio.h>
00046 #include <stdlib.h>
00047 #include <string.h>
00048 #include <stdarg.h>
00049 
00050 #include <sys/types.h>
00051 #include <sys/stat.h>
00052 #include <fcntl.h>
00053 #include <unistd.h>
00054 #include "lcmaps_setup.h"
00055 #include "_lcmaps_utils.h"
00056 #include "_lcmaps_log.h"
00057 
00058 
00059 
00060 
00061 
00062 
00063 #define LCMAPS_MAXARGS 52
00064 #define LCMAPS_BUFSIZ 8192
00065 
00066 
00067 #define LCMAPS_MAXPOLS 10
00068 #define LCMAPS_DEFAULT_POLICY_STRING ""
00069 #define LCMAPS_DEFAULT_POLICY_ACQ_STRING "acquisition_policy"
00070 #define LCMAPS_DEFAULT_POLICY_ENF_STRING "enforcement_policy"
00071 
00072 
00073 #ifndef LCMAPS_LOCATION
00074 #define LCMAPS_LOCATION "/opt/glite/"
00075 #endif
00076 
00077 
00078 #ifndef LCMAPS_ETC_DIR
00079 #define LCMAPS_ETC_DIR "etc/lcmaps"
00080 #endif
00081 
00082 
00083 #ifndef LCMAPS_MOD_DIR
00084 #define LCMAPS_MOD_DIR "lib"
00085 #endif
00086 
00087 
00088 #ifndef LCMAPS_LOG_FILE
00089 #define LCMAPS_LOG_FILE "-"
00090 #endif
00091 
00092 
00093 #ifndef LCMAPS_DB_FILE
00094 #define LCMAPS_DB_FILE "lcmaps.db"
00095 #endif
00096 
00097 
00098 
00099 
00100 
00101 
00102 static void           print_settings(void);
00103 
00104 
00105 
00106 
00107 
00108 static char *           lcmaps_log_file = NULL;          
00109        int              lcmaps_use = 1;                  
00110 static char *           lcmaps_debug_level = NULL;       
00111 static char *           lcmaps_db_file = NULL;           
00112 static char *           lcmaps_etc_dir = NULL;           
00113 static char *           lcmaps_mod_dir = NULL;           
00114 static char *           tmpname=NULL;                    
00115 static int              do_log = 1;                      
00116 static int              do_syslog = 1;                   
00117 static unsigned short   log_flag = 0;                    
00118 static char *           lcmaps_policy_string = NULL;     
00119 static char *           lcmaps_policy_acq_string = NULL; 
00120 static char *           lcmaps_policy_enf_string = NULL; 
00121 static char *           lcmaps_log_string = NULL;        
00122 
00123 
00124 
00125 
00126 
00127 
00128 
00129 void print_settings(void)
00130 {
00131     fprintf(stderr, "LCMAPS settings:\n");
00132     fprintf(stderr, "    use logfile    = %s\n", do_log?"yes":"no");
00133     fprintf(stderr, "    logfile        = %s\n", lcmaps_log_file);
00134     fprintf(stderr, "    use LCMAPS: %s\n", lcmaps_use?"yes":"no");
00135     fprintf(stderr, "      lcmaps_debug_level   = %s\n", lcmaps_debug_level?lcmaps_debug_level:"0 (default)");
00136     fprintf(stderr, "      lcmaps_db_file       = %s%s\n", lcmaps_db_file?lcmaps_db_file:LCMAPS_DB_FILE, lcmaps_db_file?"":" (default)");
00137     fprintf(stderr, "      lcmaps_etc_dir       = %s\n", lcmaps_etc_dir?lcmaps_etc_dir:"(undefined)");
00138     fprintf(stderr, "      lcmaps_mod_dir       = %s%s\n",
00139     lcmaps_mod_dir?lcmaps_mod_dir:LCMAPS_MOD_DIR, lcmaps_mod_dir?"":" (default)");
00140 }
00141 
00142 
00143 
00144 
00145 
00146 
00147 
00148 
00149 
00150 
00151 
00152 
00153 
00154 
00155 
00156 
00157 
00158 
00159 
00160 
00161 
00162 
00163 
00164 
00165 
00166 int
00167 lcmaps_setup(const char * config_file)
00168 {
00169     char ** arguments = NULL;
00170     char argbuf[LCMAPS_BUFSIZ];
00171     int nread = 0;
00172     int nargs = LCMAPS_MAXARGS;
00173     int config_fd = -1;
00174     int i = 0;
00175     int retval;
00176     char * glite_location = getenv("GLITE_LOCATION");
00177     char * lcmaps_location = (glite_location ? glite_location : LCMAPS_LOCATION);
00178     
00179     
00180 
00181 
00182 
00183     if (config_file)
00184     {
00185         
00186         arguments = (char**) malloc(nargs * sizeof(char *));
00187         if ( (config_fd = open(config_file, O_RDONLY)) < 0 )
00188         {
00189             fprintf(stderr, "Error getting file descriptor from stream of %s: %s\n",
00190                             config_file, strerror(errno));
00191             return LCMAPS_SETUP_ERROR_OPEN;
00192         }
00193         if ( (nread = read(config_fd, argbuf, LCMAPS_BUFSIZ)) < 0)
00194         {
00195             fprintf(stderr, "Error during read of lcmaps config file %s: %s\n",
00196                    config_file, strerror(errno));
00197             return LCMAPS_SETUP_ERROR_READ;
00198         }
00199         else if (nread >= LCMAPS_BUFSIZ)
00200         {
00201             fprintf(stderr, "Size of lcmaps config file should be less than %d bytes !\n",  LCMAPS_BUFSIZ);
00202             return LCMAPS_SETUP_ERROR_READ_SIZE;
00203         }
00204         argbuf[nread] = '\0';
00205         if (close(config_fd))
00206         {
00207             fprintf(stderr, "Unable to close file descriptor %d: %s\n", config_fd, strerror(errno));
00208             return LCMAPS_SETUP_ERROR_CLOSE;
00209         }
00210     
00211         
00212         if ( (retval = lcmaps_tokenize(argbuf, arguments, &nargs, " \t\n")) != 0)
00213         {
00214             fprintf(stderr, "Error tokenizing lcmaps config file (retval = %d) from \"%s\".\n",
00215                    retval, arguments[i + 1]);
00216             switch (retval)
00217             {
00218                 case -1: fprintf(stderr, "    malloc error\n"); break;
00219                 case -2: fprintf(stderr, "    too many arguments, max = %d\n", LCMAPS_MAXARGS); break;
00220                 case -3: fprintf(stderr, "    cannot match quotes\n"); break;
00221             }
00222             return LCMAPS_SETUP_ERROR_PARSE;
00223         }
00224         for (i = 0; i < nargs; i++)
00225         {
00226             if ( (strcmp(arguments[i], "-lcmaps_db_file") == 0)
00227                  && (i + 1 < nargs) )
00228             {
00229                 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00230                 {
00231                     lcmaps_db_file = arguments[i + 1];
00232                 }
00233                 else
00234                 {
00235                     fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00236                     return 1;
00237                     return LCMAPS_SETUP_ERROR_ARGUMENT;
00238                 }
00239                 i++;
00240             }
00241             else if (  ( ((strcmp(arguments[i], "-lcmaps_etc_dir") == 0) || (strcmp(arguments[i], "-lcmaps_dir") == 0)) )
00242                  && (i + 1 < nargs)  )
00243             {
00244                 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00245                 {
00246                     lcmaps_etc_dir = arguments[i + 1];
00247                 }
00248                 else
00249                 {
00250                     fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00251                     return LCMAPS_SETUP_ERROR_ARGUMENT;
00252                 }
00253                 i++;
00254             }
00255             else if (  ( ((strcmp(arguments[i], "-lcmaps_mod_dir") == 0) || (strcmp(arguments[i], "-lcmapsmod_dir") == 0)) )
00256                  && (i + 1 < nargs)  )
00257             {
00258                 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00259                 {
00260                     lcmaps_mod_dir = arguments[i + 1];
00261                 }
00262                 else
00263                 {
00264                     fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00265                     return LCMAPS_SETUP_ERROR_ARGUMENT;
00266                 }
00267                 i++;
00268             }
00269             else if ( (strcmp(arguments[i], "-lcmaps_debug_level") == 0)
00270                  && (i + 1 < nargs) )
00271             {
00272                 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00273                 {
00274                     lcmaps_debug_level = arguments[i + 1];
00275                 }
00276                 else
00277                 {
00278                     fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00279                     return LCMAPS_SETUP_ERROR_ARGUMENT;
00280                 }
00281                 i++;
00282             }
00283             else if ( (strcmp(arguments[i], "-lcmaps_log_file") == 0)
00284                  && (i + 1 < nargs) )
00285             {
00286                 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00287                 {
00288                     lcmaps_log_file = arguments[i + 1];
00289                 }
00290                 else
00291                 {
00292                     fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00293                     return LCMAPS_SETUP_ERROR_ARGUMENT;
00294                 }
00295                 i++;
00296             }
00297             else if ( (strcmp(arguments[i], "-lcmaps_log_string") == 0)
00298                  && (i + 1 < nargs) )
00299             {
00300                 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00301                 {
00302                     lcmaps_log_string = arguments[i + 1];
00303                 }
00304                 else
00305                 {
00306                     fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00307                     return LCMAPS_SETUP_ERROR_ARGUMENT;
00308                 }
00309                 i++;
00310             }
00311             else if ( (strcmp(arguments[i], "-lcmaps_policies") == 0)
00312                  && (i + 1 < nargs) )
00313             {
00314                 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00315                 {
00316                     lcmaps_policy_string = arguments[i + 1];
00317                 }
00318                 else
00319                 {
00320                     fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00321                     return LCMAPS_SETUP_ERROR_ARGUMENT;
00322                 }
00323                 i++;
00324             } 
00325             else if ( (strcmp(arguments[i], "-lcmaps_policies_acq") == 0)
00326                  && (i + 1 < nargs) )
00327             {
00328                 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00329                 {
00330                     lcmaps_policy_acq_string = arguments[i + 1];
00331                 }
00332                 else
00333                 {
00334                     fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00335                     return LCMAPS_SETUP_ERROR_ARGUMENT;
00336                 }
00337                 i++;
00338             }  
00339             else if ( (strcmp(arguments[i], "-lcmaps_policies_enf") == 0)
00340                  && (i + 1 < nargs) )
00341             {
00342                 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00343                 {
00344                     lcmaps_policy_enf_string = arguments[i + 1];
00345                 }
00346                 else
00347                 {
00348                     fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00349                     return LCMAPS_SETUP_ERROR_ARGUMENT;
00350                 }
00351                 i++;
00352             }  
00353             else if ( (strcmp(arguments[i], "-lcmaps_do_log") == 0)
00354                  && (i + 1 < nargs) )
00355             {
00356                 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00357                 {
00358                      if ( (strcmp(arguments[i+1],"yes") == 0) || (strcmp(arguments[i+1],"YES") == 0) ||
00359                         (strcmp(arguments[i+1],"y") == 0) || (strcmp(arguments[i+1],"Y") ==0) )
00360                      {
00361                          do_log = 1;
00362                      }
00363                      else if ( (strcmp(arguments[i+1],"no") == 0) || (strcmp(arguments[i+1],"NO") == 0) ||
00364                         (strcmp(arguments[i+1],"n") == 0) || (strcmp(arguments[i+1],"N") ==0) )
00365                      {
00366                          do_log = 0;
00367                      }
00368                      else
00369                      {
00370                          fprintf(stderr, "use \"yes\" or \"no\" for option %s\n", arguments[i]);
00371                          return LCMAPS_SETUP_ERROR_ARGUMENT;
00372                      }
00373     
00374                 }
00375                 else
00376                 {
00377                     fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00378                     return LCMAPS_SETUP_ERROR_ARGUMENT;
00379                 }
00380                 i++;
00381             }
00382             
00383 
00384 
00385 
00386             else if ((strncmp(arguments[i], "-", 1) == 0) && (strncmp(arguments[i], "-lcas", 5) != 0))
00387             {
00388                 fprintf(stderr, "Error: \"%s\" no such option\n", arguments[i]);
00389                 return LCMAPS_SETUP_ERROR_ARGUMENT;
00390             }
00391         }
00392     }
00393     else
00394     {
00395         
00396 
00397     }
00398 
00399     if (lcmaps_debug_level)
00400     {
00401         setenv("LCMAPS_DEBUG_LEVEL", lcmaps_debug_level, 1);
00402     }
00403     else if (getenv("LCMAPS_DEBUG_LEVEL") == NULL)
00404     {
00405         setenv("LCMAPS_DEBUG_LEVEL", 0, 1);
00406     }
00407 
00408     if (lcmaps_etc_dir)
00409     {
00410         tmpname=lcmaps_genfilename(lcmaps_location, lcmaps_etc_dir, NULL);
00411         setenv("LCMAPS_DIR", tmpname, 1);
00412         setenv("LCMAPS_ETC_DIR", tmpname, 1);
00413         if (tmpname) { free(tmpname); tmpname=NULL; }
00414     }
00415     else if ((getenv("LCMAPS_DIR") == NULL) && (getenv("LCMAPS_ETC_DIR") == NULL))
00416     {
00417         tmpname=lcmaps_genfilename(lcmaps_location, LCMAPS_ETC_DIR, NULL);
00418         setenv("LCMAPS_DIR", tmpname, 1);
00419         setenv("LCMAPS_ETC_DIR", tmpname, 1);
00420         if (tmpname) { free(tmpname); tmpname=NULL; }
00421     }
00422 
00423     if (lcmaps_db_file)
00424     {
00425         tmpname=lcmaps_genfilename(lcmaps_etc_dir,lcmaps_db_file,NULL);
00426         setenv("LCMAPS_DB_FILE", tmpname, 1);
00427         if (tmpname) { free(tmpname); tmpname=NULL; }
00428     }
00429     else if (getenv("LCMAPS_DB_FILE") == NULL)
00430     {
00431         tmpname=lcmaps_genfilename(lcmaps_etc_dir,LCMAPS_DB_FILE,NULL);
00432         setenv("LCMAPS_DB_FILE", tmpname, 1);
00433         if (tmpname) { free(tmpname); tmpname=NULL; }
00434     }
00435 
00436     if (lcmaps_mod_dir)
00437     {
00438         tmpname=lcmaps_genfilename(lcmaps_location, lcmaps_mod_dir, NULL);
00439         setenv("LCMAPS_MOD_DIR", tmpname, 1);
00440         if (tmpname) { free(tmpname); tmpname=NULL; }
00441     }
00442     else if (getenv("LCMAPS_MOD_DIR") == NULL)
00443     {
00444         tmpname=lcmaps_genfilename(lcmaps_location, LCMAPS_MOD_DIR, NULL);
00445         setenv("LCMAPS_MOD_DIR", tmpname, 1);
00446         if (tmpname) { free(tmpname); tmpname=NULL; }
00447     }
00448 
00449     
00450     if (lcmaps_policy_string)
00451     {
00452         setenv("LCMAPS_POLICY_STRING", lcmaps_policy_string, 1);
00453     }
00454     else if (getenv("LCMAPS_POLICY_STRING") == NULL)
00455     {
00456         setenv("LCMAPS_POLICY_STRING", LCMAPS_DEFAULT_POLICY_STRING, 1);
00457     }
00458     if (lcmaps_policy_acq_string)
00459     {
00460         setenv("LCMAPS_POLICY_ACQ_STRING", lcmaps_policy_acq_string, 1);
00461     }
00462     else if (getenv("LCMAPS_POLICY_ACQ_STRING") == NULL)
00463     {
00464         setenv("LCMAPS_POLICY_ACQ_STRING", LCMAPS_DEFAULT_POLICY_ACQ_STRING, 1);
00465     }
00466     if (lcmaps_policy_enf_string)
00467     {
00468         setenv("LCMAPS_POLICY_ENF_STRING", lcmaps_policy_enf_string, 1);
00469     }
00470     else if (getenv("LCMAPS_POLICY_ENF_STRING") == NULL)
00471     {
00472         setenv("LCMAPS_POLICY_ENF_STRING", LCMAPS_DEFAULT_POLICY_ENF_STRING, 1);
00473     }
00474 
00475     if (do_log)
00476     {
00477         if (lcmaps_log_file)
00478         {
00479             tmpname=lcmaps_genfilename(lcmaps_location, lcmaps_log_file, NULL);
00480             setenv("LCMAPS_LOG_FILE", tmpname, 1);
00481             if (tmpname) { free(tmpname); tmpname=NULL; }
00482         }
00483         else if (getenv("LCMAPS_LOG_FILE") == NULL)
00484         {
00485             tmpname=lcmaps_genfilename(NULL, LCMAPS_LOG_FILE, NULL);
00486             setenv("LCMAPS_LOG_FILE", tmpname, 1);
00487             if (tmpname) { free(tmpname); tmpname=NULL; }
00488         }
00489     }
00490     log_flag = (unsigned short) 0;
00491     if (do_log)
00492         log_flag |= DO_USRLOG;
00493     if (do_syslog)
00494         log_flag |= DO_SYSLOG;
00495     if (getenv("LCMAPS_LOG_TYPE") == NULL)
00496     {
00497         char tmpstr[6];
00498         snprintf(tmpstr, 6, "%u", log_flag);
00499         setenv("LCMAPS_LOG_FILE", tmpstr, 1);
00500     }
00501 
00502     
00503 
00504 
00505 
00506 
00507     if (lcmaps_log_string)
00508     {
00509         setenv("LCMAPS_LOG_STRING", lcmaps_log_string, 0);
00510     }
00511 
00512     print_settings();
00513 
00514     return 0;
00515 }
00516 
00517 #endif 
00518 
00519 
00520 
00521 
00522 
00523 
00524 
00525