Revert "Revert "oran-shell-release: release image for F""
[pti/rtp.git] / meta-starlingx / meta-stx-distro / recipes-security / gssproxy / files / Fix-silent-crash-with-duplicate-config-sections.patch
1 From caec174b203206185b6075c0e822c6f45070dd87 Mon Sep 17 00:00:00 2001
2 From: Alexander Scheel <ascheel@redhat.com>
3 Date: Wed, 9 Aug 2017 15:00:26 -0400
4 Subject: [PATCH] Fix silent crash with duplicate config sections
5
6 Signed-off-by: Alexander Scheel <ascheel@redhat.com>
7 Reviewed-by: Simo Sorce <simo@redhat.com>
8 Resolves: #194
9 Merges: #202
10 (cherry picked from commit c0d85387fc38f9554d601ec2ddb111031a694387)
11 ---
12  proxy/configure.ac    | 125 ++++++++++++++++++++++++++++++++++++++++++
13  proxy/src/gp_config.c |  27 ++++-----
14  2 files changed, 137 insertions(+), 15 deletions(-)
15
16 diff --git a/proxy/configure.ac b/proxy/configure.ac
17 index c52dbb6..9e01f7d 100644
18 --- a/proxy/configure.ac
19 +++ b/proxy/configure.ac
20 @@ -107,6 +107,131 @@ fi
21  AC_SUBST(INI_LIBS)
22  AC_SUBST(INI_CFLAGS)
23  
24 +AC_CHECK_LIB(ref_array, ref_array_destroy, [],
25 +             [AC_MSG_WARN([ref_array library must support ref_array_destroy])],
26 +             [$INI_CONFIG_LIBS])
27 +
28 +AC_RUN_IFELSE([AC_LANG_SOURCE([[
29 +/* See: https://pagure.io/SSSD/ding-libs/pull-request/3172 */
30 +#include <linux/limits.h>
31 +#include <string.h>
32 +#include <errno.h>
33 +#include <stdio.h>
34 +#include <stdlib.h>
35 +#include <stdint.h>
36 +#include <ini_configobj.h>
37 +#include <ini_config.h>
38 +
39 +static int write_to_file(char *path, char *text)
40 +{
41 +    FILE *f = fopen(path, "w");
42 +    int bytes = 0;
43 +    if (f == NULL)
44 +        return 1;
45 +
46 +    bytes = fprintf(f, "%s", text);
47 +    if (bytes != strlen(text))
48 +        return 1;
49 +
50 +    return fclose(f);
51 +}
52 +
53 +int main(void)
54 +{
55 +    char base_path[PATH_MAX];
56 +    char augment_path[PATH_MAX];
57 +
58 +    char config_base[] =
59 +        "[section]\n"
60 +        "key1 = first\n"
61 +        "key2 = exists\n";
62 +
63 +    char config_augment[] =
64 +        "[section]\n"
65 +        "key1 = augment\n"
66 +        "key3 = exists\n";
67 +
68 +    char *builddir;
69 +
70 +    struct ini_cfgobj *in_cfg, *result_cfg;
71 +    struct ini_cfgfile *file_ctx;
72 +
73 +    uint32_t merge_flags = INI_MS_DETECT | INI_MS_PRESERVE;
74 +
75 +    int ret;
76 +
77 +    builddir = getenv("builddir");
78 +    if (builddir == NULL) {
79 +        builddir = strdup(".");
80 +    }
81 +
82 +    snprintf(base_path, PATH_MAX, "%s/tmp_augment_base.conf", builddir);
83 +    snprintf(augment_path, PATH_MAX, "%s/tmp_augment_augment.conf", builddir);
84 +
85 +    ret = write_to_file(base_path, config_base);
86 +    if (ret != 0) {
87 +        ret = 1;
88 +        goto cleanup;
89 +    }
90 +
91 +    ret = write_to_file(augment_path, config_augment);
92 +    if (ret != 0) {
93 +        goto cleanup;
94 +    }
95 +
96 +    /* Match only augment.conf */
97 +    const char *m_patterns[] = { "^tmp_augment_augment.conf$", NULL };
98 +
99 +     /* Match all sections */
100 +    const char *m_sections[] = { ".*", NULL };
101 +
102 +    /* Create config collection */
103 +    ret = ini_config_create(&in_cfg);
104 +    if (ret != EOK)
105 +        goto cleanup;
106 +
107 +    /* Open base.conf */
108 +    ret = ini_config_file_open(base_path, 0, &file_ctx);
109 +    if (ret != EOK)
110 +        goto cleanup;
111 +
112 +    /* Seed in_cfg with base.conf */
113 +    ret = ini_config_parse(file_ctx, 1, 0, 0, in_cfg);
114 +    if (ret != EOK)
115 +        goto cleanup;
116 +
117 +    /* Update base.conf with augment.conf */
118 +    ret = ini_config_augment(in_cfg,
119 +                             builddir,
120 +                             m_patterns,
121 +                             m_sections,
122 +                             NULL,
123 +                             INI_STOP_ON_NONE,
124 +                             0,
125 +                             INI_PARSE_NOSPACE|INI_PARSE_NOTAB,
126 +                             merge_flags,
127 +                             &result_cfg,
128 +                             NULL,
129 +                             NULL);
130 +    /* We always expect EEXIST due to DETECT being set. */
131 +    if (ret != EEXIST)
132 +        goto cleanup;
133 +
134 +    ret = 0;
135 +
136 +cleanup:
137 +    remove(base_path);
138 +    remove(augment_path);
139 +
140 +    /* Per autoconf guidelines */
141 +    if (ret != 0)
142 +        ret = 1;
143 +
144 +    return ret;
145 +}
146 +]])]
147 +,, [AC_MSG_ERROR(["ini_config library must support extended INI_MS_DETECT. See: https://pagure.io/SSSD/ding-libs/pull-request/3172"])])
148 +
149  AX_PTHREAD(,[AC_MSG_ERROR([Could not find Pthreads support])])
150  
151  LIBS="$PTHREAD_LIBS $LIBS"
152 diff --git a/proxy/src/gp_config.c b/proxy/src/gp_config.c
153 index 07f7c8d..cd057a0 100644
154 --- a/proxy/src/gp_config.c
155 +++ b/proxy/src/gp_config.c
156 @@ -728,7 +728,7 @@ static int gp_config_from_file(const char *config_file,
157                                 0, /* metadata_flags, FIXME */
158                                 &file_ctx);
159      if (ret) {
160 -        GPDEBUG("Failed to open config file: %d (%s)\n",
161 +        GPERROR("Failed to open config file: %d (%s)\n",
162                  ret, gp_strerror(ret));
163          ini_config_destroy(ini_config);
164          return ret;
165 @@ -742,7 +742,7 @@ static int gp_config_from_file(const char *config_file,
166      if (ret) {
167          char **errors = NULL;
168          /* we had a parsing failure */
169 -        GPDEBUG("Failed to parse config file: %d (%s)\n",
170 +        GPERROR("Failed to parse config file: %d (%s)\n",
171                  ret, gp_strerror(ret));
172          if (ini_config_error_count(ini_config)) {
173              ini_config_get_errors(ini_config, &errors);
174 @@ -791,26 +791,25 @@ static int gp_config_from_dir(const char *config_dir,
175                               INI_STOP_ON_ANY, /* error_level */
176                               collision_flags,
177                               INI_PARSE_NOWRAP,
178 -                             /* do not allow colliding sections with the same
179 -                              * name in different files */
180 -                             INI_MS_ERROR,
181 +                             /* allow sections with the same name in
182 +                              * different files, but log warnings */
183 +                             INI_MS_DETECT | INI_MS_PRESERVE,
184                               &result_cfg,
185                               &error_list,
186                               NULL);
187 -    if (ret) {
188 +    if (error_list) {
189          uint32_t len;
190 -
191 -        if (!error_list) {
192 -            GPAUDIT("Error when reading config directory number: %d\n", ret);
193 -            return ret;
194 -        }
195 -
196          len = ref_array_len(error_list);
197          for (uint32_t i = 0; i < len; i++) {
198              /* libini has an unfixable bug where error strings are (char **) */
199              GPAUDIT("Error when reading config directory: %s\n",
200                      *(char **)ref_array_get(error_list, i, NULL));
201          }
202 +        ref_array_destroy(error_list);
203 +    }
204 +
205 +    if (ret && ret != EEXIST) {
206 +        GPERROR("Error when reading config directory number: %d\n", ret);
207  
208          ref_array_destroy(error_list);
209          return ret;
210 @@ -821,9 +820,7 @@ static int gp_config_from_dir(const char *config_dir,
211          ini_config_destroy(*ini_config);
212          *ini_config = result_cfg;
213      }
214 -    if (error_list) {
215 -        ref_array_destroy(error_list);
216 -    }
217 +
218      return 0;
219  }
220