Blame SOURCES/Fix-silent-crash-with-duplicate-config-sections.patch

472fdf
From caec174b203206185b6075c0e822c6f45070dd87 Mon Sep 17 00:00:00 2001
472fdf
From: Alexander Scheel <ascheel@redhat.com>
472fdf
Date: Wed, 9 Aug 2017 15:00:26 -0400
472fdf
Subject: [PATCH] Fix silent crash with duplicate config sections
472fdf
472fdf
Signed-off-by: Alexander Scheel <ascheel@redhat.com>
472fdf
Reviewed-by: Simo Sorce <simo@redhat.com>
472fdf
Resolves: #194
472fdf
Merges: #202
472fdf
(cherry picked from commit c0d85387fc38f9554d601ec2ddb111031a694387)
472fdf
---
472fdf
 proxy/configure.ac    | 125 ++++++++++++++++++++++++++++++++++++++++++
472fdf
 proxy/src/gp_config.c |  27 ++++-----
472fdf
 2 files changed, 137 insertions(+), 15 deletions(-)
472fdf
472fdf
diff --git a/proxy/configure.ac b/proxy/configure.ac
472fdf
index c52dbb6..9e01f7d 100644
472fdf
--- a/proxy/configure.ac
472fdf
+++ b/proxy/configure.ac
472fdf
@@ -107,6 +107,131 @@ fi
472fdf
 AC_SUBST(INI_LIBS)
472fdf
 AC_SUBST(INI_CFLAGS)
472fdf
 
472fdf
+AC_CHECK_LIB(ref_array, ref_array_destroy, [],
472fdf
+             [AC_MSG_WARN([ref_array library must support ref_array_destroy])],
472fdf
+             [$INI_CONFIG_LIBS])
472fdf
+
472fdf
+AC_RUN_IFELSE([AC_LANG_SOURCE([[
472fdf
+/* See: https://pagure.io/SSSD/ding-libs/pull-request/3172 */
472fdf
+#include <linux/limits.h>
472fdf
+#include <string.h>
472fdf
+#include <errno.h>
472fdf
+#include <stdio.h>
472fdf
+#include <stdlib.h>
472fdf
+#include <stdint.h>
472fdf
+#include <ini_configobj.h>
472fdf
+#include <ini_config.h>
472fdf
+
472fdf
+static int write_to_file(char *path, char *text)
472fdf
+{
472fdf
+    FILE *f = fopen(path, "w");
472fdf
+    int bytes = 0;
472fdf
+    if (f == NULL)
472fdf
+        return 1;
472fdf
+
472fdf
+    bytes = fprintf(f, "%s", text);
472fdf
+    if (bytes != strlen(text))
472fdf
+        return 1;
472fdf
+
472fdf
+    return fclose(f);
472fdf
+}
472fdf
+
472fdf
+int main(void)
472fdf
+{
472fdf
+    char base_path[PATH_MAX];
472fdf
+    char augment_path[PATH_MAX];
472fdf
+
472fdf
+    char config_base[] =
472fdf
+        "[section]\n"
472fdf
+        "key1 = first\n"
472fdf
+        "key2 = exists\n";
472fdf
+
472fdf
+    char config_augment[] =
472fdf
+        "[section]\n"
472fdf
+        "key1 = augment\n"
472fdf
+        "key3 = exists\n";
472fdf
+
472fdf
+    char *builddir;
472fdf
+
472fdf
+    struct ini_cfgobj *in_cfg, *result_cfg;
472fdf
+    struct ini_cfgfile *file_ctx;
472fdf
+
472fdf
+    uint32_t merge_flags = INI_MS_DETECT | INI_MS_PRESERVE;
472fdf
+
472fdf
+    int ret;
472fdf
+
472fdf
+    builddir = getenv("builddir");
472fdf
+    if (builddir == NULL) {
472fdf
+        builddir = strdup(".");
472fdf
+    }
472fdf
+
472fdf
+    snprintf(base_path, PATH_MAX, "%s/tmp_augment_base.conf", builddir);
472fdf
+    snprintf(augment_path, PATH_MAX, "%s/tmp_augment_augment.conf", builddir);
472fdf
+
472fdf
+    ret = write_to_file(base_path, config_base);
472fdf
+    if (ret != 0) {
472fdf
+        ret = 1;
472fdf
+        goto cleanup;
472fdf
+    }
472fdf
+
472fdf
+    ret = write_to_file(augment_path, config_augment);
472fdf
+    if (ret != 0) {
472fdf
+        goto cleanup;
472fdf
+    }
472fdf
+
472fdf
+    /* Match only augment.conf */
472fdf
+    const char *m_patterns[] = { "^tmp_augment_augment.conf$", NULL };
472fdf
+
472fdf
+     /* Match all sections */
472fdf
+    const char *m_sections[] = { ".*", NULL };
472fdf
+
472fdf
+    /* Create config collection */
472fdf
+    ret = ini_config_create(&in_cfg);
472fdf
+    if (ret != EOK)
472fdf
+        goto cleanup;
472fdf
+
472fdf
+    /* Open base.conf */
472fdf
+    ret = ini_config_file_open(base_path, 0, &file_ctx);
472fdf
+    if (ret != EOK)
472fdf
+        goto cleanup;
472fdf
+
472fdf
+    /* Seed in_cfg with base.conf */
472fdf
+    ret = ini_config_parse(file_ctx, 1, 0, 0, in_cfg);
472fdf
+    if (ret != EOK)
472fdf
+        goto cleanup;
472fdf
+
472fdf
+    /* Update base.conf with augment.conf */
472fdf
+    ret = ini_config_augment(in_cfg,
472fdf
+                             builddir,
472fdf
+                             m_patterns,
472fdf
+                             m_sections,
472fdf
+                             NULL,
472fdf
+                             INI_STOP_ON_NONE,
472fdf
+                             0,
472fdf
+                             INI_PARSE_NOSPACE|INI_PARSE_NOTAB,
472fdf
+                             merge_flags,
472fdf
+                             &result_cfg,
472fdf
+                             NULL,
472fdf
+                             NULL);
472fdf
+    /* We always expect EEXIST due to DETECT being set. */
472fdf
+    if (ret != EEXIST)
472fdf
+        goto cleanup;
472fdf
+
472fdf
+    ret = 0;
472fdf
+
472fdf
+cleanup:
472fdf
+    remove(base_path);
472fdf
+    remove(augment_path);
472fdf
+
472fdf
+    /* Per autoconf guidelines */
472fdf
+    if (ret != 0)
472fdf
+        ret = 1;
472fdf
+
472fdf
+    return ret;
472fdf
+}
472fdf
+]])]
472fdf
+,, [AC_MSG_ERROR(["ini_config library must support extended INI_MS_DETECT. See: https://pagure.io/SSSD/ding-libs/pull-request/3172"])])
472fdf
+
472fdf
 AX_PTHREAD(,[AC_MSG_ERROR([Could not find Pthreads support])])
472fdf
 
472fdf
 LIBS="$PTHREAD_LIBS $LIBS"
472fdf
diff --git a/proxy/src/gp_config.c b/proxy/src/gp_config.c
472fdf
index 07f7c8d..cd057a0 100644
472fdf
--- a/proxy/src/gp_config.c
472fdf
+++ b/proxy/src/gp_config.c
472fdf
@@ -728,7 +728,7 @@ static int gp_config_from_file(const char *config_file,
472fdf
                                0, /* metadata_flags, FIXME */
472fdf
                                &file_ctx);
472fdf
     if (ret) {
472fdf
-        GPDEBUG("Failed to open config file: %d (%s)\n",
472fdf
+        GPERROR("Failed to open config file: %d (%s)\n",
472fdf
                 ret, gp_strerror(ret));
472fdf
         ini_config_destroy(ini_config);
472fdf
         return ret;
472fdf
@@ -742,7 +742,7 @@ static int gp_config_from_file(const char *config_file,
472fdf
     if (ret) {
472fdf
         char **errors = NULL;
472fdf
         /* we had a parsing failure */
472fdf
-        GPDEBUG("Failed to parse config file: %d (%s)\n",
472fdf
+        GPERROR("Failed to parse config file: %d (%s)\n",
472fdf
                 ret, gp_strerror(ret));
472fdf
         if (ini_config_error_count(ini_config)) {
472fdf
             ini_config_get_errors(ini_config, &errors);
472fdf
@@ -791,26 +791,25 @@ static int gp_config_from_dir(const char *config_dir,
472fdf
                              INI_STOP_ON_ANY, /* error_level */
472fdf
                              collision_flags,
472fdf
                              INI_PARSE_NOWRAP,
472fdf
-                             /* do not allow colliding sections with the same
472fdf
-                              * name in different files */
472fdf
-                             INI_MS_ERROR,
472fdf
+                             /* allow sections with the same name in
472fdf
+                              * different files, but log warnings */
472fdf
+                             INI_MS_DETECT | INI_MS_PRESERVE,
472fdf
                              &result_cfg,
472fdf
                              &error_list,
472fdf
                              NULL);
472fdf
-    if (ret) {
472fdf
+    if (error_list) {
472fdf
         uint32_t len;
472fdf
-
472fdf
-        if (!error_list) {
472fdf
-            GPAUDIT("Error when reading config directory number: %d\n", ret);
472fdf
-            return ret;
472fdf
-        }
472fdf
-
472fdf
         len = ref_array_len(error_list);
472fdf
         for (uint32_t i = 0; i < len; i++) {
472fdf
             /* libini has an unfixable bug where error strings are (char **) */
472fdf
             GPAUDIT("Error when reading config directory: %s\n",
472fdf
                     *(char **)ref_array_get(error_list, i, NULL));
472fdf
         }
472fdf
+        ref_array_destroy(error_list);
472fdf
+    }
472fdf
+
472fdf
+    if (ret && ret != EEXIST) {
472fdf
+        GPERROR("Error when reading config directory number: %d\n", ret);
472fdf
 
472fdf
         ref_array_destroy(error_list);
472fdf
         return ret;
472fdf
@@ -821,9 +820,7 @@ static int gp_config_from_dir(const char *config_dir,
472fdf
         ini_config_destroy(*ini_config);
472fdf
         *ini_config = result_cfg;
472fdf
     }
472fdf
-    if (error_list) {
472fdf
-        ref_array_destroy(error_list);
472fdf
-    }
472fdf
+
472fdf
     return 0;
472fdf
 }
472fdf