]> git.ipfire.org Git - thirdparty/sarg.git/commitdiff
Include sub configuration files
authorFrederic Marchal <fmarchal@users.sourceforge.net>
Thu, 23 Jul 2015 19:32:34 +0000 (21:32 +0200)
committerFrederic Marchal <fmarchal@users.sourceforge.net>
Thu, 23 Jul 2015 19:32:34 +0000 (21:32 +0200)
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.

getconf.c
grepday.c
include/defs.h
log.c
sarg.conf

index 188b4663725ca87b121e8e8c0cf2baf725a7c18a..99a8e1c75140e043a822bf9ff90b807ef82c3cfb 100644 (file)
--- a/getconf.c
+++ b/getconf.c
 
 #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--;
 }
index 99ba095192dafd57253463c6737f695a8f3ec293..8b409f1010dc1df0a3adbe164491cba075aeaf5d 100644 (file)
--- 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;
        }
 
index 101ce9a34c15a80753563ae80e517474bd8b9977..73e20d959d837eac7e5167286b8a670b4ec5ff90 100644 (file)
@@ -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 027a9635bde2ee6d80c762f2243ab109858d0f54..bd860e2492093a49feb5087a5b16bd6e0cd300c1 100644 (file)
--- 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;
 
index 0f7b3b21e7f46ce5f6e36fb6ccf519f4d636d400..18c8eb58a0965bae018be473b030a74b71c93114 100644 (file)
--- a/sarg.conf
+++ b/sarg.conf
 #      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