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

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