From 48b8a405a13ef92007e16ec3aa84d2ae505d2717 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Fri, 4 May 2018 17:58:17 +0100 Subject: [PATCH] [Fix] Fix importing static maps from UCL Issue: #2216 --- src/libutil/map.c | 73 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 11 deletions(-) diff --git a/src/libutil/map.c b/src/libutil/map.c index 698ccf3fd..1e8f70f7e 100644 --- a/src/libutil/map.c +++ b/src/libutil/map.c @@ -1977,6 +1977,35 @@ rspamd_map_calculate_hash (struct rspamd_map *map) g_free (cksum_encoded); } +static gboolean +rspamd_map_add_static_string (struct rspamd_config *cfg, + const ucl_object_t *elt, + GString *target) +{ + gsize sz; + const gchar *dline; + + if (ucl_object_type (elt) != UCL_STRING) { + msg_err_config ("map has static backend but `data` is " + "not string like: %s", + ucl_object_type_to_string (elt->type)); + return FALSE; + } + + /* Otherwise, we copy data to the backend */ + dline = ucl_object_tolstring (elt, &sz); + + if (sz == 0) { + msg_err_config ("map has static backend but empty no data"); + return FALSE; + } + + g_string_append_len (target, dline, sz); + g_string_append_c (target, '\n'); + + return TRUE; +} + struct rspamd_map * rspamd_map_add (struct rspamd_config *cfg, const gchar *map_line, @@ -2039,8 +2068,6 @@ rspamd_map_add_from_ucl (struct rspamd_config *cfg, const ucl_object_t *cur, *elt; struct rspamd_map *map; struct rspamd_map_backend *bk; - gsize sz; - const gchar *dline; guint i; g_assert (obj != NULL); @@ -2183,25 +2210,49 @@ rspamd_map_add_from_ucl (struct rspamd_config *cfg, PTR_ARRAY_FOREACH (map->backends, i, bk) { if (bk->protocol == MAP_PROTO_STATIC) { + GString *map_data; /* We need data field in ucl */ elt = ucl_object_lookup (obj, "data"); - if (elt == NULL || ucl_object_type (elt) != UCL_STRING) { + if (elt == NULL) { msg_err_config ("map has static backend but no `data` field"); goto err; } - /* Otherwise, we copy data to the backend */ - dline = ucl_object_tolstring (elt, &sz); - if (sz == 0) { - msg_err_config ("map has static backend but empty `data` field"); - goto err; + if (ucl_object_type (elt) == UCL_STRING) { + map_data = g_string_sized_new (32); + + if (rspamd_map_add_static_string (cfg, elt, map_data)) { + bk->data.sd->data = map_data->str; + bk->data.sd->len = map_data->len; + g_string_free (map_data, FALSE); + } + else { + g_string_free (map_data, TRUE); + msg_err_config ("map has static backend with invalid `data` field"); + goto err; + } } + else if (ucl_object_type (elt) == UCL_ARRAY) { + map_data = g_string_sized_new (32); + it = ucl_object_iterate_new (elt); + + while ((cur = ucl_object_iterate_safe (it, true))) { + if (!rspamd_map_add_static_string (cfg, cur, map_data)) { + g_string_free (map_data, TRUE); + msg_err_config ("map has static backend with invalid " + "`data` field"); + ucl_object_iterate_free (it); + goto err; + } + } - bk->data.sd->data = g_malloc (sz); - bk->data.sd->len = sz; - memcpy (bk->data.sd->data, dline, sz); + ucl_object_iterate_free (it); + bk->data.sd->data = map_data->str; + bk->data.sd->len = map_data->len; + g_string_free (map_data, FALSE); + } } } } -- 2.39.5