From: Frederic Marchal Date: Thu, 23 Jul 2015 19:32:34 +0000 (+0200) Subject: Include sub configuration files X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=da94b2c7104e30c8b6968e1fe620a36037fe65b6;p=thirdparty%2Fsarg.git Include sub configuration files Sarg.conf can include other configuration files with the "include" directive. It can be used to store common option in one file and create shorter sarg.conf dedicated to reporting tasks. --- diff --git a/getconf.c b/getconf.c index 188b466..99a8e1c 100644 --- a/getconf.c +++ b/getconf.c @@ -30,6 +30,12 @@ #define SET_LIST(list) list,sizeof(list)/sizeof(list[0]) +//! The configuration file from which the graph option was loaded +char GraphConfigFile[MAXLEN]=""; + +//! How many include directives were followed. +static int IncludeLevel=0; + extern numlist hours, weekdays; extern FileListObject AccessLog; extern int PerUserLimitsNumber; @@ -578,7 +584,7 @@ static void ccharset(char *CharSet) return; } -static void parmtest(char *buf) +static void parmtest(char *buf,const char *File) { char wbuf[2048]; struct getwordstruct gwarea; @@ -837,8 +843,6 @@ static void parmtest(char *buf) if (getparam_bool("user_authentication",buf,&UserAuthentication)>0) return; if (getparam_string("AuthUserTemplateFile",buf,wbuf,sizeof(wbuf))>0) { - char dir[MAXLEN]; - if (is_absolute(wbuf)) { if (strlen(wbuf)>=sizeof(AuthUserTemplateFile)) { debuga(__FILE__,__LINE__,_("Template file name is too long in parameter \"AuthUserTemplateFile\"\n")); @@ -846,7 +850,9 @@ static void parmtest(char *buf) } safe_strcpy(AuthUserTemplateFile,wbuf,sizeof(AuthUserTemplateFile)); } else { - safe_strcpy(dir,ConfigFile,sizeof(dir)); + char dir[MAXLEN]; + + safe_strcpy(dir,File,sizeof(dir)); if (snprintf(AuthUserTemplateFile,sizeof(AuthUserTemplateFile),"%s/%s",dirname(dir),wbuf)>=sizeof(AuthUserTemplateFile)) { debuga(__FILE__,__LINE__,_("Template file name is too long in parameter \"AuthUserTemplateFile\"\n")); exit(EXIT_FAILURE); @@ -867,8 +873,9 @@ static void parmtest(char *buf) #ifndef HAVE_GD if (Graphs) debugaz(__FILE__,__LINE__,_("No graphs available as sarg was not compiled with libgd. Set \"graphs\" to \"no\" in %s to disable this warning\n"), - ConfigFile); + File); #endif + safe_strcpy(GraphConfigFile,File,sizeof(GraphConfigFile)); return; } @@ -973,32 +980,48 @@ static void parmtest(char *buf) printf(_("SARG: Unknown option %s\n"),buf); } -void getconf(void) +void getconf(const char *File) { FILE *fp_in; char buf[MAXLEN]; + char IncludeFile[MAXLEN]; + + IncludeLevel++; + if (debug) { + if (IncludeLevel<=1) + debuga(__FILE__,__LINE__,_("Loading configuration file \"%s\"\n"),File); + else + debuga(__FILE__,__LINE__,_("Including configuration file \"%s\"\n"),File); + } - if(debug) - debuga(__FILE__,__LINE__,_("Loading configuration from \"%s\"\n"),ConfigFile); + // stop if include files are producing a loop + if (IncludeLevel>5) { + debuga(__FILE__,__LINE__,_("Too many nested configuration files included in \"%s\""),ConfigFile); + exit(EXIT_FAILURE); + } - if ((fp_in = fopen(ConfigFile, "r")) == NULL) { - debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),ConfigFile,strerror(errno)); + if ((fp_in = fopen(File, "r")) == NULL) { + debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),File,strerror(errno)); exit(EXIT_FAILURE); } while (fgets(buf, sizeof(buf), fp_in) != NULL) { fixendofline(buf); + if (getparam_string("include",buf,IncludeFile,sizeof(IncludeFile))>0) { + getconf(IncludeFile); + continue; + } + if (debugz>=LogLevel_Data) printf("SYSCONFDIR %s\n",buf); - parmtest(buf); - + parmtest(buf,File); } if (fclose(fp_in)==EOF) { - debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),ConfigFile,strerror(errno)); + debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),File,strerror(errno)); exit(EXIT_FAILURE); } - return; + IncludeLevel--; } diff --git a/grepday.c b/grepday.c index 99ba095..8b409f1 100644 --- a/grepday.c +++ b/grepday.c @@ -121,6 +121,8 @@ enum TextRefPos iconv_t localtoutf=(iconv_t)-1; #endif +extern char GraphConfigFile[MAXLEN]; + static void Sarg_gdImageStringFT (struct GraphDataStruct *gdata, int fg, char *fontlist, double ptsize, double angle, int x, int y, const char *string,enum TextRefPos RefPos) { @@ -635,12 +637,14 @@ void greport_prepare(void) #ifdef HAVE_GD if (!Graphs) { if (debugz>=LogLevel_Process) - debugaz(__FILE__,__LINE__,_("Graphs disabled as requested in %s\n"),ConfigFile); + debugaz(__FILE__,__LINE__,_("Graphs disabled as requested in %s\n"),GraphConfigFile); return; } if (GraphFont[0]=='\0') { - if (debugz>=LogLevel_Process) - debugaz(__FILE__,__LINE__,_("Graphs disabled as no font names were provided in %s\n"),ConfigFile); + if (debugz>=LogLevel_Process) { + const char *File=(GraphConfigFile[0]) ? GraphConfigFile : ConfigFile; + debugaz(__FILE__,__LINE__,_("Graphs disabled as no font names were provided in %s\n"),File); + } return; } diff --git a/include/defs.h b/include/defs.h index 101ce9a..73e20d9 100644 --- a/include/defs.h +++ b/include/defs.h @@ -231,7 +231,7 @@ int fnmatch(const char *pattern,const char *string,int flags); #endif // getconf.c -void getconf(void); +void getconf(const char *File); // grepday.c void greport_prepare(void); diff --git a/log.c b/log.c index 027a963..bd860e2 100644 --- a/log.c +++ b/log.c @@ -456,7 +456,7 @@ int main(int argc,char *argv[]) } if(access(ConfigFile, R_OK) == 0) - getconf(); + getconf(ConfigFile); if(userip) UserIp=true; diff --git a/sarg.conf b/sarg.conf index 0f7b3b2..18c8eb5 100644 --- a/sarg.conf +++ b/sarg.conf @@ -907,3 +907,18 @@ # Note that the max_successive_log_errors is still taken into account and # cannot be disabled. #max_total_log_errors 50 + +# TAG: include conffile +# Include the specified conffile. The full path must be provided to +# make sure the correct file is loaded. +# +# Use this option to store common options in one file and include it +# in multiple sarg.conf dedicated to various reporting tasks. +# +# Options declared last take precedence. Use it to include a file and +# then override some options after the include statement. Beware that +# some options are cumulative such as access_log, useragent_log or +# redirector_log. You can't override those options as explained here. +# Declaring them in the common file and the including file will merely +# add the latter to the list. +#include /etc/sarg/sarg-common.conf