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

lcas_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 LCAS_SETUP_C
00035 #define LCAS_SETUP_C
00036 
00037 /*****************************************************************************
00038                             Include header files
00039 ******************************************************************************/
00040 
00041 #include "lcas_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 "lcas_setup.h"
00054 #include "_lcas_utils.h"
00055 #include "_lcas_log.h"
00056 
00057 /******************************************************************************
00058                              Define constants
00059 ******************************************************************************/
00060 
00061 /* Maximum number of arguments in lcas configuration file */
00062 #define LCAS_MAXARGS 52
00063 #define LCAS_BUFSIZ 8192
00064 
00065 /* Root directory for LCAS */
00066 #ifndef LCAS_LOCATION
00067 #define LCAS_LOCATION "/opt/glite/"
00068 #endif
00069 
00070 /* Default directory where LCAS configuration files can be found */
00071 #ifndef LCAS_ETC_DIR
00072 #define LCAS_ETC_DIR "etc/lcas"
00073 #endif
00074 
00075 /* Default directory where LCAS module can be found */
00076 #ifndef LCAS_MOD_DIR
00077 #define LCAS_MOD_DIR "lib"
00078 #endif
00079 
00080 /* Default log file */
00081 #ifndef LCAS_LOG_FILE
00082 #define LCAS_LOG_FILE "-"
00083 #endif
00084 
00085 /* Default lcas db file */
00086 #ifndef LCAS_DB_FILE
00087 #define LCAS_DB_FILE "lcas.db"
00088 #endif
00089 
00090 
00091 /******************************************************************************
00092                           Module specific prototypes
00093 ******************************************************************************/
00094 
00095 static void           print_settings(void);
00096 
00097 /******************************************************************************
00098                        Define module specific variables
00099 ******************************************************************************/
00100 
00101 static char *           lcas_log_file = NULL;          /* LCAS logging file */
00102        int              lcas_use = 1;                  /* Use LCAS (1) or not (0)   */
00103 static char *           lcas_debug_level = NULL;       /* Debug level for LCAS (default 0) */
00104 static char *           lcas_db_file = NULL;           /* File name of the LCAS policy file with or without path */
00105 static char *           lcas_etc_dir = NULL;           /* Directory where lcas conf/data files can be found */
00106 static char *           lcas_mod_dir = NULL;           /* Directory where lcas module pic,dl can be found */
00107 static char *           tmpname=NULL;                  /* Needed to get rid of memory leaks caused by ignoring return value of lcas_genfilename() */
00108 static int              do_log = 1;                    /* do LCAS logging in a logfile */
00109 static int              do_syslog = 1;                 /* use syslog for LCAS logging */
00110 static unsigned short   log_flag = 0;                  /* Flag indicating which type(s) of logging are preferred */
00111 static char *           lcas_log_string = NULL;        /* (constant) log string to be prepended to log information */
00112 
00113 
00114 /******************************************************************************
00115                        Import external variables
00116 ******************************************************************************/
00117 
00118 
00119 void print_settings(void)
00120 {
00121     fprintf(stderr, "LCAS settings:\n");
00122     fprintf(stderr, "    use logfile    = %s\n", do_log?"yes":"no");
00123     fprintf(stderr, "    logfile        = %s\n", lcas_log_file);
00124     fprintf(stderr, "    use LCAS: %s\n", lcas_use?"yes":"no");
00125     fprintf(stderr, "      lcas_debug_level   = %s\n", lcas_debug_level?lcas_debug_level:"0 (default)");
00126     fprintf(stderr, "      lcas_db_file       = %s%s\n", lcas_db_file?lcas_db_file:LCAS_DB_FILE, lcas_db_file?"":" (default)");
00127     fprintf(stderr, "      lcas_etc_dir       = %s\n", lcas_etc_dir?lcas_etc_dir:"(undefined)");
00128     fprintf(stderr, "      lcas_mod_dir       = %s%s\n",
00129     lcas_mod_dir?lcas_mod_dir:LCAS_MOD_DIR, lcas_mod_dir?"":" (default)");
00130 
00131 }
00132 
00133 /******************************************************************************
00134 Function:   lcas_setup
00135 Description:
00136     Setup of the LCAS environment.
00137 
00138     This function sets the following environment variables:
00139       LCAS_LOG_FILE:          Name and location of the logfile
00140       LCAS_LOG_TYPE:          Type of logging (logfile, syslog, both or none)
00141       LCAS_LOG_STRING:        Log string to be prepended to the logging lines
00142       LCAS_DB_FILE:           Name of the lcas policy file
00143       LCAS_DEBUG_LEVEL:       Debug level (0-5)
00144       LCAS_MOD_DIR:           Location of the LCAS library
00145       LCAS_DIR:               Location of the LCAS config files
00146       LCAS_ETC_DIR:           Location of the LCAS config files (=LCAS_DIR)
00147 
00148 Parameters:
00149     config_file:  name of the configuration file that contains the parameters to be read
00150 Returns:
00151     0: setup succeeded
00152     1: setup failed
00153 ******************************************************************************/
00154 int
00155 lcas_setup(const char * config_file)
00156 {
00157     char ** arguments = NULL;
00158     char argbuf[LCAS_BUFSIZ];
00159     int nread = 0;
00160     int nargs = LCAS_MAXARGS;
00161     int config_fd = -1;
00162     int i = 0;
00163     int retval;
00164     char * glite_location = getenv("GLITE_LOCATION");
00165     char * lcas_location = (glite_location ? glite_location : LCAS_LOCATION);
00166     
00167     /*
00168      * Read the lcas config file and store it
00169      * in argbuf
00170      */
00171     if (config_file)
00172     {
00173         /* Read argument buffer */
00174         arguments = (char**) malloc(nargs * sizeof(char *));
00175         if ( (config_fd = open(config_file, O_RDONLY)) < 0 )
00176         {
00177             fprintf(stderr, "Error getting file descriptor from stream of %s: %s\n",
00178                             config_file, strerror(errno));
00179             return LCAS_SETUP_ERROR_OPEN;
00180         }
00181         if ( (nread = read(config_fd, argbuf, LCAS_BUFSIZ)) < 0)
00182         {
00183             fprintf(stderr, "Error during read of lcas config file %s: %s\n",
00184                    config_file, strerror(errno));
00185             return LCAS_SETUP_ERROR_READ;
00186         }
00187         else if (nread >= LCAS_BUFSIZ)
00188         {
00189             fprintf(stderr, "Size of lcas config file should be less than %d bytes !\n",  LCAS_BUFSIZ);
00190             return LCAS_SETUP_ERROR_READ_SIZE;
00191         }
00192         argbuf[nread] = '\0';
00193         if (close(config_fd))
00194         {
00195             fprintf(stderr, "Unable to close file descriptor %d: %s\n", config_fd, strerror(errno));
00196             return LCAS_SETUP_ERROR_CLOSE;
00197         }
00198     
00199         /* Parse argument buffers */
00200         if ( (retval = lcas_tokenize(argbuf, arguments, &nargs, " \t\n")) != 0)
00201         {
00202             fprintf(stderr, "Error tokenizing lcas config file (retval = %d) from \"%s\".\n",
00203                    retval, arguments[i + 1]);
00204             switch (retval)
00205             {
00206                 case -1: fprintf(stderr, "    malloc error\n"); break;
00207                 case -2: fprintf(stderr, "    too many arguments, max = %d\n", LCAS_MAXARGS); break;
00208                 case -3: fprintf(stderr, "    cannot match quotes\n"); break;
00209             }
00210             return LCAS_SETUP_ERROR_PARSE;
00211         }
00212         for (i = 0; i < nargs; i++)
00213         {
00214             if ( (strcmp(arguments[i], "-lcas_db_file") == 0)
00215                  && (i + 1 < nargs) )
00216             {
00217                 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00218                 {
00219                     lcas_db_file = arguments[i + 1];
00220                 }
00221                 else
00222                 {
00223                     fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00224                     return 1;
00225                     return LCAS_SETUP_ERROR_ARGUMENT;
00226                 }
00227                 i++;
00228             }
00229             else if (  ( ((strcmp(arguments[i], "-lcas_etc_dir") == 0) || (strcmp(arguments[i], "-lcas_dir") == 0)) )
00230                  && (i + 1 < nargs)  )
00231             {
00232                 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00233                 {
00234                     lcas_etc_dir = arguments[i + 1];
00235                 }
00236                 else
00237                 {
00238                     fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00239                     return LCAS_SETUP_ERROR_ARGUMENT;
00240                 }
00241                 i++;
00242             }
00243             else if (  ( ((strcmp(arguments[i], "-lcas_mod_dir") == 0) || (strcmp(arguments[i], "-lcasmod_dir") == 0)) )
00244                  && (i + 1 < nargs)  )
00245             {
00246                 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00247                 {
00248                     lcas_mod_dir = arguments[i + 1];
00249                 }
00250                 else
00251                 {
00252                     fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00253                     return LCAS_SETUP_ERROR_ARGUMENT;
00254                 }
00255                 i++;
00256             }
00257             else if ( (strcmp(arguments[i], "-lcas_debug_level") == 0)
00258                  && (i + 1 < nargs) )
00259             {
00260                 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00261                 {
00262                     lcas_debug_level = arguments[i + 1];
00263                 }
00264                 else
00265                 {
00266                     fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00267                     return LCAS_SETUP_ERROR_ARGUMENT;
00268                 }
00269                 i++;
00270             }
00271             else if ( (strcmp(arguments[i], "-lcas_log_file") == 0)
00272                  && (i + 1 < nargs) )
00273             {
00274                 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00275                 {
00276                     lcas_log_file = arguments[i + 1];
00277                 }
00278                 else
00279                 {
00280                     fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00281                     return LCAS_SETUP_ERROR_ARGUMENT;
00282                 }
00283                 i++;
00284             }
00285             else if ( (strcmp(arguments[i], "-lcas_log_string") == 0)
00286                  && (i + 1 < nargs) )
00287             {
00288                 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00289                 {
00290                     lcas_log_string = arguments[i + 1];
00291                 }
00292                 else
00293                 {
00294                     fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00295                     return LCAS_SETUP_ERROR_ARGUMENT;
00296                 }
00297                 i++;
00298             }
00299             else if ( (strcmp(arguments[i], "-lcas_do_log") == 0)
00300                  && (i + 1 < nargs) )
00301             {
00302                 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00303                 {
00304                      if ( (strcmp(arguments[i+1],"yes") == 0) || (strcmp(arguments[i+1],"YES") == 0) ||
00305                         (strcmp(arguments[i+1],"y") == 0) || (strcmp(arguments[i+1],"Y") ==0) )
00306                      {
00307                          do_log = 1;
00308                      }
00309                      else if ( (strcmp(arguments[i+1],"no") == 0) || (strcmp(arguments[i+1],"NO") == 0) ||
00310                         (strcmp(arguments[i+1],"n") == 0) || (strcmp(arguments[i+1],"N") ==0) )
00311                      {
00312                          do_log = 0;
00313                      }
00314                      else
00315                      {
00316                          fprintf(stderr, "use \"yes\" or \"no\" for option %s\n", arguments[i]);
00317                          return LCAS_SETUP_ERROR_ARGUMENT;
00318                      }
00319     
00320                 }
00321                 else
00322                 {
00323                     fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00324                     return LCAS_SETUP_ERROR_ARGUMENT;
00325                 }
00326                 i++;
00327             }
00328             /*
00329              * If an option is found (argument starting with a dash) and it does not start with "-lcmaps": Failure
00330              * otherwise: ignore (could be arguments for lcmaps)
00331              */
00332             else if ((strncmp(arguments[i], "-", 1) == 0) && (strncmp(arguments[i], "-lcmaps", 7) != 0))
00333             {
00334                 fprintf(stderr, "Error: \"%s\" no such option\n", arguments[i]);
00335                 return LCAS_SETUP_ERROR_ARGUMENT;
00336             }
00337         }
00338     }
00339     else
00340     {
00341         /* No config file: take defaults */
00342 //        syslog(LOG_WARNING, "No configuration file found for LCAS (use \"-C\" option), taking default values instead\n");
00343     }
00344 
00345     if (lcas_debug_level)
00346     {
00347         setenv("LCAS_DEBUG_LEVEL", lcas_debug_level, 1);
00348     }
00349     else if (getenv("LCAS_DEBUG_LEVEL") == NULL)
00350     {
00351         setenv("LCAS_DEBUG_LEVEL", 0, 1);
00352     }
00353 
00354     if (lcas_etc_dir)
00355     {
00356         tmpname=lcas_genfilename(lcas_location, lcas_etc_dir, NULL);
00357         setenv("LCAS_DIR", tmpname, 1);
00358         setenv("LCAS_ETC_DIR", tmpname, 1);
00359         if (tmpname) { free(tmpname); tmpname=NULL; }
00360     }
00361     else if ((getenv("LCAS_DIR") == NULL) && (getenv("LCAS_ETC_DIR") == NULL))
00362     {
00363         tmpname=lcas_genfilename(lcas_location, LCAS_ETC_DIR, NULL);
00364         setenv("LCAS_DIR", tmpname, 1);
00365         setenv("LCAS_ETC_DIR", tmpname, 1);
00366         if (tmpname) { free(tmpname); tmpname=NULL; }
00367     }
00368 
00369     if (lcas_db_file)
00370     {
00371         tmpname=lcas_genfilename(lcas_etc_dir,lcas_db_file,NULL);
00372         setenv("LCAS_DB_FILE", tmpname, 1);
00373         if (tmpname) { free(tmpname); tmpname=NULL; }
00374     }
00375     else if (getenv("LCAS_DB_FILE") == NULL)
00376     {
00377         tmpname=lcas_genfilename(lcas_etc_dir,LCAS_DB_FILE,NULL);
00378         setenv("LCAS_DB_FILE", tmpname, 1);
00379         if (tmpname) { free(tmpname); tmpname=NULL; }
00380     }
00381 
00382     if (lcas_mod_dir)
00383     {
00384         tmpname=lcas_genfilename(lcas_location, lcas_mod_dir, NULL);
00385         setenv("LCAS_MOD_DIR", tmpname, 1);
00386         if (tmpname) { free(tmpname); tmpname=NULL; }
00387     }
00388     else if (getenv("LCAS_MOD_DIR") == NULL)
00389     {
00390         tmpname=lcas_genfilename(lcas_location, LCAS_MOD_DIR, NULL);
00391         setenv("LCAS_MOD_DIR", tmpname, 1);
00392         if (tmpname) { free(tmpname); tmpname=NULL; }
00393     }
00394 
00395     if (do_log)
00396     {
00397         if (lcas_log_file)
00398         {
00399             tmpname=lcas_genfilename(lcas_location, lcas_log_file, NULL);
00400             setenv("LCAS_LOG_FILE", tmpname, 1);
00401             if (tmpname) { free(tmpname); tmpname=NULL; }
00402         }
00403         else if (getenv("LCAS_LOG_FILE") == NULL)
00404         {
00405             tmpname=lcas_genfilename(NULL, LCAS_LOG_FILE, NULL);
00406             setenv("LCAS_LOG_FILE", tmpname, 1);
00407             if (tmpname) { free(tmpname); tmpname=NULL; }
00408         }
00409     }
00410     log_flag = (unsigned short) 0;
00411     if (do_log)
00412         log_flag |= DO_USRLOG;
00413     if (do_syslog)
00414         log_flag |= DO_SYSLOG;
00415     if (getenv("LCAS_LOG_TYPE") == NULL)
00416     {
00417         char tmpstr[6];
00418         snprintf(tmpstr, 6, "%u", log_flag);
00419         setenv("LCAS_LOG_FILE", tmpstr, 1);
00420     }
00421 
00422     /*
00423      * Set the log string. In this case do not override the existing value
00424      * because it may contain a more elaborate logging string including the
00425      * calling service and timestring
00426      */
00427     if (lcas_log_string)
00428     {
00429         setenv("LCAS_LOG_STRING", lcas_log_string, 0);
00430     }
00431 
00432     print_settings();
00433 
00434     return 0;
00435 }
00436 
00437 #endif /* LCAS_SETUP_C */
00438 
00439 /******************************************************************************
00440 CVS Information:
00441     $Source: /cvs/jra1mw/org.glite.security.lcas/src/lcas_setup.c,v $
00442     $Date: 2005/04/04 15:23:57 $
00443     $Revision: 2.1 $
00444     $Author: msteenba $
00445 ******************************************************************************/

Generated on Fri May 27 18:10:48 2005 for lcas by doxygen 1.3.5