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

lcmaps_setup.c

Go to the documentation of this file.
00001 /*                                                                                                            
00002  * Copyright (c) Members of the EGEE Collaboration. 2004.
00003  * See http://eu-egee.org/partners/ for details on the copyright holders.
00004  * For license conditions see the license file or
00005  * http://eu-egee.org/license.html
00006  *
00007  * Copyright (c) 2004 by 
00008  *     David Groep <davidg@nikhef.nl>,
00009  *     Oscar Koeroo <okoeroo@nikhef.nl>,
00010  *     Martijn Steenbakkers <martijn@nikhef.nl>,
00011  *     Gerben Venekamp <venekamp@nikhef.nl>,
00012  *     NIKHEF Amsterdam, the Netherlands
00013  *     Universiteit van Amsterdam, the Netherlands
00014  */
00015 
00034 #ifndef LCMAPS_SETUP_C
00035 #define LCMAPS_SETUP_C
00036 
00037 /*****************************************************************************
00038                             Include header files
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 /* The following headers are needed for open, close, read */
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                              Define constants
00060 ******************************************************************************/
00061 
00062 /* Maximum number of arguments in lcmaps configuration file */
00063 #define LCMAPS_MAXARGS 52
00064 #define LCMAPS_BUFSIZ 8192
00065 
00066 /* Defaults for LCMAPS policies */
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 /* Root directory for LCMAPS */
00073 #ifndef LCMAPS_LOCATION
00074 #define LCMAPS_LOCATION "/opt/glite/"
00075 #endif
00076 
00077 /* Default directory where LCMAPS configuration files can be found */
00078 #ifndef LCMAPS_ETC_DIR
00079 #define LCMAPS_ETC_DIR "etc/lcmaps"
00080 #endif
00081 
00082 /* Default directory where LCMAPS module can be found */
00083 #ifndef LCMAPS_MOD_DIR
00084 #define LCMAPS_MOD_DIR "lib"
00085 #endif
00086 
00087 /* Default log file */
00088 #ifndef LCMAPS_LOG_FILE
00089 #define LCMAPS_LOG_FILE "-"
00090 #endif
00091 
00092 /* Default lcmaps db file */
00093 #ifndef LCMAPS_DB_FILE
00094 #define LCMAPS_DB_FILE "lcmaps.db"
00095 #endif
00096 
00097 
00098 /******************************************************************************
00099                           Module specific prototypes
00100 ******************************************************************************/
00101 
00102 static void           print_settings(void);
00103 
00104 /******************************************************************************
00105                        Define module specific variables
00106 ******************************************************************************/
00107 
00108 static char *           lcmaps_log_file = NULL;          /* LCMAPS logging file */
00109        int              lcmaps_use = 1;                  /* Use LCMAPS (1) or not (0)   */
00110 static char *           lcmaps_debug_level = NULL;       /* Debug level for LCMAPS (default 0) */
00111 static char *           lcmaps_db_file = NULL;           /* File name of the LCMAPS policy file with or without path */
00112 static char *           lcmaps_etc_dir = NULL;           /* Directory where lcmaps conf/data files can be found */
00113 static char *           lcmaps_mod_dir = NULL;           /* Directory where lcmaps module pic,dl can be found */
00114 static char *           tmpname=NULL;                    /* Needed to get rid of memory leaks caused by ignoring return value of lcmaps_genfilename() */
00115 static int              do_log = 1;                      /* do LCMAPS logging in a logfile */
00116 static int              do_syslog = 1;                   /* use syslog for LCMAPS logging */
00117 static unsigned short   log_flag = 0;                    /* Flag indicating which type(s) of logging are preferred */
00118 static char *           lcmaps_policy_string = NULL;     /* String that contains a list of LCMAPS policies */
00119 static char *           lcmaps_policy_acq_string = NULL; /* String that contains a list of LCMAPS acquisition policies */
00120 static char *           lcmaps_policy_enf_string = NULL; /* String that contains a list of LCMAPS enforcement policies */
00121 static char *           lcmaps_log_string = NULL;        /* (constant) log string to be prepended to log information */
00122 
00123 
00124 /******************************************************************************
00125                        Import external variables
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 Function:   lcmaps_setup
00144 Description:
00145     Setup of the LCMAPS environment.
00146 
00147     This function sets the following environment variables:
00148       LCMAPS_LOG_FILE:          Name and location of the logfile
00149       LCMAPS_LOG_TYPE:          Type of logging (logfile, syslog, both or none)
00150       LCMAPS_LOG_STRING:        Log string to be prepended to the logging lines
00151       LCMAPS_DB_FILE:           Name of the lcmaps policy file
00152       LCMAPS_DEBUG_LEVEL:       Debug level (0-5)
00153       LCMAPS_MOD_DIR:           Location of the LCMAPS library
00154       LCMAPS_DIR:               Location of the LCMAPS config files
00155       LCMAPS_ETC_DIR:           Location of the LCMAPS config files (=LCMAPS_DIR)
00156       LCMAPS_POLICY_STRING      The list of policies (default = NULL)
00157       LCMAPS_POLICY_ACQ_STRING  The list of acquisition policies (default = acquisition_policy)
00158       LCMAPS_POLICY_ENF_STRING  The list of enforcement policies (default = enforcement_policy)
00159 
00160 Parameters:
00161     config_file:  name of the configuration file that contains the parameters to be read
00162 Returns:
00163     0: setup succeeded
00164     1: setup failed
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      * Read the lcmaps config file and store it
00181      * in argbuf
00182      */
00183     if (config_file)
00184     {
00185         /* Read argument buffer */
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         /* Parse argument buffers */
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              * If an option is found (argument starting with a dash) and it does not start with "-lcas": Failure
00384              * otherwise: ignore (could be arguments for lcas)
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         /* No config file: take defaults */
00396 //        syslog(LOG_WARNING, "No configuration file found for LCMAPS (use \"-C\" option), taking default values instead\n");
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     /* Set the policy strings */
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      * Set the log string. In this case do not override the existing value
00504      * because it may contain a more elaborate logging string including the
00505      * calling service and timestring
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 /* LCMAPS_SETUP_C */
00518 
00519 /******************************************************************************
00520 CVS Information:
00521     $Source: /cvs/jra1mw/org.glite.security.lcmaps/src/lcmaps_setup.c,v $
00522     $Date: 2005/04/04 15:23:58 $
00523     $Revision: 1.1 $
00524     $Author: msteenba $
00525 ******************************************************************************/

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