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

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