Blame SOURCES/0165-lib-introduce-global-configuration-option-for-exclud.patch

4b6aa8
From 2b8b92486ac28aff4a7d99fc18084db3d6f9617c Mon Sep 17 00:00:00 2001
4b6aa8
From: Matej Habrnal <mhabrnal@redhat.com>
4b6aa8
Date: Fri, 12 Feb 2016 14:20:19 +0100
4b6aa8
Subject: [PATCH] lib: introduce global configuration + option for excluded
4b6aa8
 elements
4b6aa8
4b6aa8
Related to rhbz#1168494
4b6aa8
4b6aa8
Signed-off-by: Matej Habrnal <mhabrnal@redhat.com>
4b6aa8
Signed-off-by: Jakub Filak <jfilak@redhat.com>
4b6aa8
---
4b6aa8
 src/include/Makefile.am            |   2 +-
4b6aa8
 src/include/global_configuration.h |  45 ++++++++++++
4b6aa8
 src/include/internal_libreport.h   |   2 +-
4b6aa8
 src/include/libreport_types.h      |   2 +
4b6aa8
 src/lib/Makefile.am                |   3 +-
4b6aa8
 src/lib/global_configuration.c     | 143 +++++++++++++++++++++++++++++++++++++
4b6aa8
 src/plugins/reporter-upload.c      |   4 +-
4b6aa8
 7 files changed, 196 insertions(+), 5 deletions(-)
4b6aa8
 create mode 100644 src/include/global_configuration.h
4b6aa8
 create mode 100644 src/lib/global_configuration.c
4b6aa8
4b6aa8
diff --git a/src/include/Makefile.am b/src/include/Makefile.am
4b6aa8
index de44cda..062bffb 100644
4b6aa8
--- a/src/include/Makefile.am
4b6aa8
+++ b/src/include/Makefile.am
4b6aa8
@@ -9,7 +9,7 @@ libreport_include_HEADERS = \
4b6aa8
     run_event.h \
4b6aa8
     libreport_curl.h \
4b6aa8
     workflow.h \
4b6aa8
-    \
4b6aa8
+    global_configuration.h \
4b6aa8
     config_item_info.h \
4b6aa8
     file_obj.h \
4b6aa8
     internal_libreport.h \
4b6aa8
diff --git a/src/include/global_configuration.h b/src/include/global_configuration.h
4b6aa8
new file mode 100644
4b6aa8
index 0000000..9666796
4b6aa8
--- /dev/null
4b6aa8
+++ b/src/include/global_configuration.h
4b6aa8
@@ -0,0 +1,45 @@
4b6aa8
+/*
4b6aa8
+    Copyright (C) 2015  ABRT team
4b6aa8
+    Copyright (C) 2015  RedHat Inc
4b6aa8
+
4b6aa8
+    This program is free software; you can redistribute it and/or modify
4b6aa8
+    it under the terms of the GNU General Public License as published by
4b6aa8
+    the Free Software Foundation; either version 2 of the License, or
4b6aa8
+    (at your option) any later version.
4b6aa8
+
4b6aa8
+    This program is distributed in the hope that it will be useful,
4b6aa8
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
4b6aa8
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4b6aa8
+    GNU General Public License for more details.
4b6aa8
+
4b6aa8
+    You should have received a copy of the GNU General Public License along
4b6aa8
+    with this program; if not, write to the Free Software Foundation, Inc.,
4b6aa8
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
4b6aa8
+*/
4b6aa8
+
4b6aa8
+#ifndef LIBREPORT_GLOBAL_CONFIGURATION_H
4b6aa8
+#define LIBREPORT_GLOBAL_CONFIGURATION_H
4b6aa8
+
4b6aa8
+#include "libreport_types.h"
4b6aa8
+
4b6aa8
+#ifdef __cplusplus
4b6aa8
+extern "C" {
4b6aa8
+#endif
4b6aa8
+
4b6aa8
+#define load_global_configuration libreport_load_global_configuration
4b6aa8
+bool load_global_configuration(void);
4b6aa8
+
4b6aa8
+#define load_global_configuration_from_dirs libreport_load_global_configuration_from_dirs
4b6aa8
+bool load_global_configuration_from_dirs(const char *dirs[], int dir_flags[]);
4b6aa8
+
4b6aa8
+#define free_global_configuration libreport_free_global_configuration
4b6aa8
+void free_global_configuration(void);
4b6aa8
+
4b6aa8
+#define get_global_always_excluded_elements libreport_get_global_always_excluded_elements
4b6aa8
+string_vector_ptr_t get_global_always_excluded_elements(void);
4b6aa8
+
4b6aa8
+#ifdef __cplusplus
4b6aa8
+}
4b6aa8
+#endif
4b6aa8
+
4b6aa8
+#endif /* LIBREPORT_GLOBAL_CONFIGURATION_H */
4b6aa8
diff --git a/src/include/internal_libreport.h b/src/include/internal_libreport.h
4b6aa8
index 2046e69..b632803 100644
4b6aa8
--- a/src/include/internal_libreport.h
4b6aa8
+++ b/src/include/internal_libreport.h
4b6aa8
@@ -45,7 +45,6 @@
4b6aa8
 #include <termios.h>
4b6aa8
 #include <time.h>
4b6aa8
 #include <unistd.h>
4b6aa8
-#include <stdbool.h>
4b6aa8
 /* Try to pull in PATH_MAX */
4b6aa8
 #include <limits.h>
4b6aa8
 #include <sys/param.h>
4b6aa8
@@ -91,6 +90,7 @@ int vdprintf(int d, const char *format, va_list ap);
4b6aa8
 
4b6aa8
 /* Pull in entire public libreport API */
4b6aa8
 #include "dump_dir.h"
4b6aa8
+#include "global_configuration.h"
4b6aa8
 #include "event_config.h"
4b6aa8
 #include "problem_data.h"
4b6aa8
 #include "report.h"
4b6aa8
diff --git a/src/include/libreport_types.h b/src/include/libreport_types.h
4b6aa8
index 2c60972..eb70fca 100644
4b6aa8
--- a/src/include/libreport_types.h
4b6aa8
+++ b/src/include/libreport_types.h
4b6aa8
@@ -19,9 +19,11 @@
4b6aa8
 #ifndef LIBREPORT_TYPES_H_
4b6aa8
 #define LIBREPORT_TYPES_H_
4b6aa8
 
4b6aa8
+#include <stdbool.h>
4b6aa8
 #include <glib.h>
4b6aa8
 
4b6aa8
 typedef gchar **string_vector_ptr_t;
4b6aa8
+typedef const gchar *const *const_string_vector_const_ptr_t;
4b6aa8
 
4b6aa8
 #define string_vector_new_from_string libreport_string_vector_new_from_string
4b6aa8
 string_vector_ptr_t string_vector_new_from_string(const char *vector);
4b6aa8
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am
4b6aa8
index 7d9722a..f9ea602 100644
4b6aa8
--- a/src/lib/Makefile.am
4b6aa8
+++ b/src/lib/Makefile.am
4b6aa8
@@ -55,7 +55,8 @@ libreport_la_SOURCES = \
4b6aa8
     workflow_xml_parser.c \
4b6aa8
     config_item_info.c \
4b6aa8
     xml_parser.c \
4b6aa8
-    libreport_init.c
4b6aa8
+    libreport_init.c \
4b6aa8
+    global_configuration.c
4b6aa8
 
4b6aa8
 libreport_la_CPPFLAGS = \
4b6aa8
     -I$(srcdir)/../include \
4b6aa8
diff --git a/src/lib/global_configuration.c b/src/lib/global_configuration.c
4b6aa8
new file mode 100644
4b6aa8
index 0000000..903a2fb
4b6aa8
--- /dev/null
4b6aa8
+++ b/src/lib/global_configuration.c
4b6aa8
@@ -0,0 +1,143 @@
4b6aa8
+/*
4b6aa8
+    Copyright (C) 2015  ABRT team
4b6aa8
+    Copyright (C) 2015  RedHat Inc
4b6aa8
+
4b6aa8
+    This program is free software; you can redistribute it and/or modify
4b6aa8
+    it under the terms of the GNU General Public License as published by
4b6aa8
+    the Free Software Foundation; either version 2 of the License, or
4b6aa8
+    (at your option) any later version.
4b6aa8
+
4b6aa8
+    This program is distributed in the hope that it will be useful,
4b6aa8
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
4b6aa8
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4b6aa8
+    GNU General Public License for more details.
4b6aa8
+
4b6aa8
+    You should have received a copy of the GNU General Public License along
4b6aa8
+    with this program; if not, write to the Free Software Foundation, Inc.,
4b6aa8
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
4b6aa8
+*/
4b6aa8
+
4b6aa8
+#include "global_configuration.h"
4b6aa8
+#include "internal_libreport.h"
4b6aa8
+
4b6aa8
+#define OPT_NAME_SCRUBBED_VARIABLES "ScrubbedENVVariables"
4b6aa8
+#define OPT_NAME_EXCLUDED_ELEMENTS "AlwaysExcludedElements"
4b6aa8
+
4b6aa8
+static const char *const s_recognized_options[] = {
4b6aa8
+    OPT_NAME_SCRUBBED_VARIABLES,
4b6aa8
+    OPT_NAME_EXCLUDED_ELEMENTS,
4b6aa8
+    NULL,
4b6aa8
+};
4b6aa8
+
4b6aa8
+static map_string_t *s_global_settings;
4b6aa8
+
4b6aa8
+bool load_global_configuration(void)
4b6aa8
+{
4b6aa8
+    static const char *dirs[] = {
4b6aa8
+        CONF_DIR,
4b6aa8
+        NULL,
4b6aa8
+        NULL,
4b6aa8
+    };
4b6aa8
+
4b6aa8
+    static int dir_flags[] = {
4b6aa8
+#if 0
4b6aa8
+        CONF_DIR_FLAG_NONE,
4b6aa8
+#else
4b6aa8
+        /* jfilak: RHEL-7 do not use global configuration file */
4b6aa8
+        CONF_DIR_FLAG_OPTIONAL,
4b6aa8
+#endif
4b6aa8
+        CONF_DIR_FLAG_OPTIONAL,
4b6aa8
+        -1,
4b6aa8
+    };
4b6aa8
+
4b6aa8
+    if (dirs[1] == NULL)
4b6aa8
+        dirs[1] = get_user_conf_base_dir();
4b6aa8
+
4b6aa8
+    return load_global_configuration_from_dirs(dirs, dir_flags);
4b6aa8
+}
4b6aa8
+
4b6aa8
+bool load_global_configuration_from_dirs(const char *dirs[], int dir_flags[])
4b6aa8
+{
4b6aa8
+    if (s_global_settings == NULL)
4b6aa8
+    {
4b6aa8
+        s_global_settings = new_map_string();
4b6aa8
+
4b6aa8
+        bool ret = load_conf_file_from_dirs_ext("libreport.conf", dirs, dir_flags, s_global_settings,
4b6aa8
+                                               /*don't skip without value*/ false);
4b6aa8
+        if (!ret)
4b6aa8
+        {
4b6aa8
+            error_msg("Failed to load libreport global configuration");
4b6aa8
+            free_global_configuration();
4b6aa8
+            return false;
4b6aa8
+        }
4b6aa8
+
4b6aa8
+        map_string_iter_t iter;
4b6aa8
+        init_map_string_iter(&iter, s_global_settings);
4b6aa8
+        const char *key, *value;
4b6aa8
+        while(next_map_string_iter(&iter, &key, &value))
4b6aa8
+        {
4b6aa8
+            /* Die to avoid security leaks in case where someone made a typo in a option name */
4b6aa8
+            if (!is_in_string_list(key, s_recognized_options))
4b6aa8
+            {
4b6aa8
+                error_msg("libreport global configuration contains unrecognized option : '%s'", key);
4b6aa8
+                free_global_configuration();
4b6aa8
+                return false;
4b6aa8
+            }
4b6aa8
+        }
4b6aa8
+    }
4b6aa8
+    else
4b6aa8
+        log_notice("libreport global configuration already loaded");
4b6aa8
+
4b6aa8
+    return true;
4b6aa8
+}
4b6aa8
+
4b6aa8
+void free_global_configuration(void)
4b6aa8
+{
4b6aa8
+    if (s_global_settings != NULL)
4b6aa8
+    {
4b6aa8
+        free_map_string(s_global_settings);
4b6aa8
+        s_global_settings = NULL;
4b6aa8
+    }
4b6aa8
+}
4b6aa8
+
4b6aa8
+static void assert_global_configuration_initialized(void)
4b6aa8
+{
4b6aa8
+    if (NULL == s_global_settings)
4b6aa8
+    {
4b6aa8
+        error_msg("libreport global configuration is not initialized");
4b6aa8
+        abort();
4b6aa8
+    }
4b6aa8
+}
4b6aa8
+
4b6aa8
+#define get_helper(type, getter, name) \
4b6aa8
+    ({ \
4b6aa8
+    assert_global_configuration_initialized(); \
4b6aa8
+    type opt; \
4b6aa8
+    if (getter(s_global_settings, name, &opt)) \
4b6aa8
+        /* Die to avoid security leaks in case where someone made a error */ \
4b6aa8
+        error_msg_and_die("libreport global settings contains invalid data: '"name"'"); \
4b6aa8
+    opt;\
4b6aa8
+    })
4b6aa8
+
4b6aa8
+string_vector_ptr_t get_global_always_excluded_elements(void)
4b6aa8
+{
4b6aa8
+    assert_global_configuration_initialized();
4b6aa8
+
4b6aa8
+    char *env_exclude = getenv("EXCLUDE_FROM_REPORT");
4b6aa8
+    const char *gc_exclude = get_map_string_item_or_NULL(s_global_settings, OPT_NAME_EXCLUDED_ELEMENTS);
4b6aa8
+
4b6aa8
+    if (env_exclude != NULL && gc_exclude == NULL)
4b6aa8
+        return string_vector_new_from_string(env_exclude);
4b6aa8
+
4b6aa8
+    if (env_exclude == NULL && gc_exclude != NULL)
4b6aa8
+        return string_vector_new_from_string(gc_exclude);
4b6aa8
+
4b6aa8
+    if (env_exclude == NULL && gc_exclude == NULL)
4b6aa8
+        return string_vector_new_from_string(NULL);
4b6aa8
+
4b6aa8
+    char *joined_exclude = xasprintf("%s, %s", env_exclude, gc_exclude);
4b6aa8
+    string_vector_ptr_t ret = string_vector_new_from_string(joined_exclude);
4b6aa8
+    free(joined_exclude);
4b6aa8
+
4b6aa8
+    return ret;
4b6aa8
+}
4b6aa8
diff --git a/src/plugins/reporter-upload.c b/src/plugins/reporter-upload.c
4b6aa8
index 84c827b..6d83d2f 100644
4b6aa8
--- a/src/plugins/reporter-upload.c
4b6aa8
+++ b/src/plugins/reporter-upload.c
4b6aa8
@@ -96,12 +96,12 @@ static int create_and_upload_archive(
4b6aa8
 
4b6aa8
     /* Write data to the tarball */
4b6aa8
     {
4b6aa8
-        char *exclude_from_report = getenv("EXCLUDE_FROM_REPORT");
4b6aa8
+        string_vector_ptr_t exclude_from_report = get_global_always_excluded_elements();
4b6aa8
         dd_init_next_file(dd);
4b6aa8
         char *short_name, *full_name;
4b6aa8
         while (dd_get_next_file(dd, &short_name, &full_name))
4b6aa8
         {
4b6aa8
-            if (exclude_from_report && is_in_comma_separated_list(short_name, exclude_from_report))
4b6aa8
+            if (exclude_from_report && is_in_string_list(short_name, (const_string_vector_const_ptr_t)exclude_from_report))
4b6aa8
                 goto next;
4b6aa8
 
4b6aa8
             // dd_get_next_file guarantees that it's a REG:
4b6aa8
-- 
4b6aa8
1.8.3.1
4b6aa8