[syslog-ng] [PATCH 2/2] cfg-lexer: Implement glob-based @include.

Gergely Nagy algernon at balabit.hu
Fri Mar 30 11:46:13 CEST 2012


Both for distributions and otherwise, the ability to limit what kind
of files are included, especially when doing a directory include is
very important.

This patch implements just that, by extending cfg_lexer_include_file()
to try and resolve globs, if it can't find a file otherwise. Files
included this way still respect the global include-path setting, so
@include "foo.d/*.conf" will still work.

Signed-off-by: Gergely Nagy <algernon at balabit.hu>
---
 lib/cfg-lexer.c |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 49 insertions(+), 0 deletions(-)

diff --git a/lib/cfg-lexer.c b/lib/cfg-lexer.c
index cfe229f..9469cbf 100644
--- a/lib/cfg-lexer.c
+++ b/lib/cfg-lexer.c
@@ -31,6 +31,7 @@
 #include "misc.h"
 
 #include <string.h>
+#include <glob.h>
 #include <sys/stat.h>
 
 struct _CfgArgs
@@ -478,6 +479,51 @@ cfg_lexer_include_file_simple(CfgLexer *self, gchar *filename)
   return FALSE;
 }
 
+static gboolean
+cfg_lexer_include_file_glob_at(CfgLexer *self, const gchar *pattern)
+{
+  glob_t globbuf;
+  size_t i;
+  gboolean status = FALSE;
+
+  if (glob(pattern, 0, NULL, &globbuf) != 0)
+    return FALSE;
+
+  for (i = 0; i < globbuf.gl_pathc; i++)
+    status |= cfg_lexer_include_file(self, globbuf.gl_pathv[i]);
+
+  globfree(&globbuf);
+
+  return status;
+}
+
+static gboolean
+cfg_lexer_include_file_glob(CfgLexer *self, const gchar *filename_)
+{
+  const gchar *path = cfg_args_get(self->globals, "include-path");
+
+  if (filename_[0] == '/' || !path)
+    return cfg_lexer_include_file_glob_at(self, filename_);
+  else
+    {
+      gchar **dirs;
+      gchar *cf;
+      gint i = 0;
+      gboolean status = FALSE;
+
+      dirs = g_strsplit(path, G_SEARCHPATH_SEPARATOR_S, 0);
+      while (dirs && dirs[i])
+        {
+          cf = g_build_filename(dirs[i], filename_, NULL);
+          status |= cfg_lexer_include_file_glob_at(self, cf);
+          g_free(cf);
+          i++;
+        }
+      g_strfreev(dirs);
+      return status;
+    }
+}
+
 gboolean
 cfg_lexer_include_file(CfgLexer *self, const gchar *filename_)
 {
@@ -497,6 +543,9 @@ cfg_lexer_include_file(CfgLexer *self, const gchar *filename_)
   filename = find_file_in_path(cfg_args_get(self->globals, "include-path"), filename_, G_FILE_TEST_EXISTS);
   if (!filename || stat(filename, &st) < 0)
     {
+      if (cfg_lexer_include_file_glob(self, filename_))
+        return TRUE;
+
       msg_error("Include file/directory not found",
                 evt_tag_str("filename", filename_),
                 evt_tag_str("include-path", cfg_args_get(self->globals, "include-path")),
-- 
1.7.9




More information about the syslog-ng mailing list