From: Vsevolod Stakhov Date: Wed, 13 Nov 2013 18:17:00 +0000 (+0000) Subject: Expand path in file maps. X-Git-Tag: 0.6.0~70 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2a0a59ee52a424d10bc7d1d5d77c451b842d8e00;p=rspamd.git Expand path in file maps. --- diff --git a/src/cfg_file.h b/src/cfg_file.h index b7deb1ca6..9dec61bc3 100644 --- a/src/cfg_file.h +++ b/src/cfg_file.h @@ -551,15 +551,6 @@ gboolean check_classifier_statfiles (struct classifier_config *cf); */ struct classifier_config* find_classifier_conf (struct config_file *cfg, const gchar *name); -/* - * Expand path that may contain configuration variables: - * $CONFDIR - configuration directory - * $LOCALSTATESDIR - local states directory - * $INSTALLPREFIX - installation prefix - * $VERSION - rspamd version - */ -const gchar* rspamd_expand_path (memory_pool_t *pool, const gchar *path); - #endif /* ifdef CFG_FILE_H */ /* * vi:ts=4 diff --git a/src/cfg_rcl.c b/src/cfg_rcl.c index 6fc92ed19..557c532e8 100644 --- a/src/cfg_rcl.c +++ b/src/cfg_rcl.c @@ -1151,8 +1151,12 @@ rspamd_rcl_parse_struct_string (struct config_file *cfg, ucl_object_t *obj, target = (gchar **)(((gchar *)pd->user_struct) + pd->offset); switch (obj->type) { case UCL_STRING: - /* Direct assigning is safe, as object is likely linked to the cfg mem_pool */ - *target = ucl_copy_value_trash (obj); + if (pd->flags & RSPAMD_CL_FLAG_STRING_PATH) { + *target = rspamd_expand_path (cfg->cfg_pool, ucl_copy_value_trash (obj)); + } + else { + *target = memory_pool_strdup (cfg->cfg_pool, ucl_copy_value_trash (obj)); + } break; case UCL_INT: *target = memory_pool_alloc (cfg->cfg_pool, num_str_len); @@ -1171,10 +1175,6 @@ rspamd_rcl_parse_struct_string (struct config_file *cfg, ucl_object_t *obj, return FALSE; } - if (pd->flags & RSPAMD_CL_FLAG_STRING_PATH) { - *target = (gchar *)rspamd_expand_path (cfg->cfg_pool, *target); - } - return TRUE; } @@ -1322,8 +1322,12 @@ rspamd_rcl_parse_struct_string_list (struct config_file *cfg, ucl_object_t *obj, for (cur = obj; cur != NULL; cur = cur->next) { switch (cur->type) { case UCL_STRING: - /* Direct assigning is safe, as curect is likely linked to the cfg mem_pool */ - val = ucl_copy_value_trash (obj); + if (pd->flags & RSPAMD_CL_FLAG_STRING_PATH) { + val = rspamd_expand_path (cfg->cfg_pool, ucl_copy_value_trash (obj)); + } + else { + val = memory_pool_strdup (cfg->cfg_pool, ucl_copy_value_trash (obj)); + } break; case UCL_INT: val = memory_pool_alloc (cfg->cfg_pool, num_str_len); diff --git a/src/cfg_utils.c b/src/cfg_utils.c index 6a9477e00..dd6b00dcd 100644 --- a/src/cfg_utils.c +++ b/src/cfg_utils.c @@ -1150,139 +1150,6 @@ check_classifier_statfiles (struct classifier_config *cf) return res; } -/* - * Variables: - * $CONFDIR - configuration directory - * $LOCALSTATESDIR - local states directory - * $INSTALLPREFIX - installation prefix - * $VERSION - rspamd version - */ - -#define RSPAMD_CONFDIR_MACRO "CONFDIR" -#define RSPAMD_LOCALSTATESDIR_MACRO "LOCALSTATESDIR" -#define RSPAMD_INSTALLPREFIX_MACRO "INSTALLPREFIX" -#define RSPAMD_VERSION_MACRO "VERSION" - -static const gchar * -rspamd_check_path_variable (const gchar *in, gsize *len) -{ - switch (*in) { - case 'C': - if (strncmp (in, RSPAMD_CONFDIR_MACRO, sizeof (RSPAMD_CONFDIR_MACRO) - 1) == 0) { - *len += sizeof (ETC_PREFIX) - 1; - in += sizeof (RSPAMD_CONFDIR_MACRO) - 1; - } - break; - case 'L': - if (strncmp (in, RSPAMD_LOCALSTATESDIR_MACRO, sizeof (RSPAMD_LOCALSTATESDIR_MACRO) - 1) == 0) { - *len += sizeof (LOCALSTATES_PREFIX) - 1; - in += sizeof (RSPAMD_LOCALSTATESDIR_MACRO) - 1; - } - break; - case 'I': - if (strncmp (in, RSPAMD_INSTALLPREFIX_MACRO, sizeof (RSPAMD_INSTALLPREFIX_MACRO) - 1) == 0) { - *len += sizeof (CMAKE_PREFIX) - 1; - in += sizeof (RSPAMD_INSTALLPREFIX_MACRO) - 1; - } - break; - case 'V': - if (strncmp (in, RSPAMD_VERSION_MACRO, sizeof (RSPAMD_VERSION_MACRO) - 1) == 0) { - *len += sizeof (RVERSION) - 1; - in += sizeof (RSPAMD_VERSION_MACRO) - 1; - } - break; - } - - return in; -} - -static const gchar * -rspamd_expand_path_variable (const gchar *in, gchar **dest) -{ - gchar *d = *dest; - const gchar *v = in + 1; - - *d = *v; - in ++; - - switch (*v) { - case 'C': - if (strncmp (v, RSPAMD_CONFDIR_MACRO, sizeof (RSPAMD_CONFDIR_MACRO) - 1) == 0) { - memcpy (d, ETC_PREFIX, sizeof (ETC_PREFIX) - 1); - d += sizeof (ETC_PREFIX) - 1; - in += sizeof (RSPAMD_CONFDIR_MACRO) - 1; - } - break; - case 'L': - if (strncmp (v, RSPAMD_LOCALSTATESDIR_MACRO, sizeof (RSPAMD_LOCALSTATESDIR_MACRO) - 1) == 0) { - memcpy (d, LOCALSTATES_PREFIX, sizeof (LOCALSTATES_PREFIX) - 1); - d += sizeof (LOCALSTATES_PREFIX) - 1; - in += sizeof (RSPAMD_LOCALSTATESDIR_MACRO) - 1; - } - break; - case 'I': - if (strncmp (v, RSPAMD_INSTALLPREFIX_MACRO, sizeof (RSPAMD_INSTALLPREFIX_MACRO) - 1) == 0) { - memcpy (d, CMAKE_PREFIX, sizeof (CMAKE_PREFIX) - 1); - d += sizeof (CMAKE_PREFIX) - 1; - in += sizeof (RSPAMD_INSTALLPREFIX_MACRO) - 1; - } - break; - case 'V': - if (strncmp (v, RSPAMD_VERSION_MACRO, sizeof (RSPAMD_VERSION_MACRO) - 1) == 0) { - memcpy (d, RVERSION, sizeof (RVERSION) - 1); - d += sizeof (RVERSION) - 1; - in += sizeof (RSPAMD_VERSION_MACRO) - 1; - } - break; - } - - *dest = d; - return in; -} - -const gchar* -rspamd_expand_path (memory_pool_t *pool, const gchar *path) -{ - const gchar *p = path; - gchar *dest, *d; - gsize len = 0, orig_len = 0; - - while (*p != '\0') { - if (*p == '$') { - len ++; - p = rspamd_check_path_variable (p + 1, &len); - } - else { - len ++; - } - orig_len ++; - p ++; - } - - if (len == orig_len) { - return path; - } - - dest = memory_pool_alloc (pool, len + 1); - p = path; - d = dest; - - while (*p != '\0') { - if (*p == '$') { - p = rspamd_expand_path_variable (p, &d); - } - else { - *d++ = *p++; - } - } - - *d = '\0'; - - msg_debug ("expanded %s to %s", path, dest); - - return dest; -} - /* * vi:ts=4 */ diff --git a/src/map.c b/src/map.c index 7c61dcc9e..25541a7e6 100644 --- a/src/map.c +++ b/src/map.c @@ -1044,9 +1044,16 @@ add_map (struct config_file *cfg, const gchar *map_line, const gchar *descriptio new_map->user_data = user_data; new_map->protocol = proto; new_map->cfg = cfg; - new_map->uri = memory_pool_strdup (cfg->cfg_pool, proto == MAP_PROTO_FILE ? def : map_line); new_map->id = g_random_int (); new_map->locked = memory_pool_alloc0_shared (cfg->cfg_pool, sizeof (gint)); + + if (proto == MAP_PROTO_FILE) { + new_map->uri = rspamd_expand_path (cfg->cfg_pool, def); + def = new_map->uri; + } + else { + new_map->uri = memory_pool_strdup (cfg->cfg_pool, map_line); + } if (description != NULL) { new_map->description = memory_pool_strdup (cfg->cfg_pool, description); } diff --git a/src/util.c b/src/util.c index 88d62f974..1e0378d2a 100644 --- a/src/util.c +++ b/src/util.c @@ -2384,6 +2384,139 @@ restart: #endif } +/* + * Variables: + * $CONFDIR - configuration directory + * $LOCALSTATESDIR - local states directory + * $INSTALLPREFIX - installation prefix + * $VERSION - rspamd version + */ + +#define RSPAMD_CONFDIR_MACRO "CONFDIR" +#define RSPAMD_LOCALSTATESDIR_MACRO "LOCALSTATESDIR" +#define RSPAMD_INSTALLPREFIX_MACRO "INSTALLPREFIX" +#define RSPAMD_VERSION_MACRO "VERSION" + +static const gchar * +rspamd_check_path_variable (const gchar *in, gsize *len) +{ + switch (*in) { + case 'C': + if (strncmp (in, RSPAMD_CONFDIR_MACRO, sizeof (RSPAMD_CONFDIR_MACRO) - 1) == 0) { + *len += sizeof (ETC_PREFIX) - 1; + in += sizeof (RSPAMD_CONFDIR_MACRO) - 1; + } + break; + case 'L': + if (strncmp (in, RSPAMD_LOCALSTATESDIR_MACRO, sizeof (RSPAMD_LOCALSTATESDIR_MACRO) - 1) == 0) { + *len += sizeof (LOCALSTATES_PREFIX) - 1; + in += sizeof (RSPAMD_LOCALSTATESDIR_MACRO) - 1; + } + break; + case 'I': + if (strncmp (in, RSPAMD_INSTALLPREFIX_MACRO, sizeof (RSPAMD_INSTALLPREFIX_MACRO) - 1) == 0) { + *len += sizeof (CMAKE_PREFIX) - 1; + in += sizeof (RSPAMD_INSTALLPREFIX_MACRO) - 1; + } + break; + case 'V': + if (strncmp (in, RSPAMD_VERSION_MACRO, sizeof (RSPAMD_VERSION_MACRO) - 1) == 0) { + *len += sizeof (RVERSION) - 1; + in += sizeof (RSPAMD_VERSION_MACRO) - 1; + } + break; + } + + return in; +} + +static const gchar * +rspamd_expand_path_variable (const gchar *in, gchar **dest) +{ + gchar *d = *dest; + const gchar *v = in + 1; + + *d = *v; + in ++; + + switch (*v) { + case 'C': + if (strncmp (v, RSPAMD_CONFDIR_MACRO, sizeof (RSPAMD_CONFDIR_MACRO) - 1) == 0) { + memcpy (d, ETC_PREFIX, sizeof (ETC_PREFIX) - 1); + d += sizeof (ETC_PREFIX) - 1; + in += sizeof (RSPAMD_CONFDIR_MACRO) - 1; + } + break; + case 'L': + if (strncmp (v, RSPAMD_LOCALSTATESDIR_MACRO, sizeof (RSPAMD_LOCALSTATESDIR_MACRO) - 1) == 0) { + memcpy (d, LOCALSTATES_PREFIX, sizeof (LOCALSTATES_PREFIX) - 1); + d += sizeof (LOCALSTATES_PREFIX) - 1; + in += sizeof (RSPAMD_LOCALSTATESDIR_MACRO) - 1; + } + break; + case 'I': + if (strncmp (v, RSPAMD_INSTALLPREFIX_MACRO, sizeof (RSPAMD_INSTALLPREFIX_MACRO) - 1) == 0) { + memcpy (d, CMAKE_PREFIX, sizeof (CMAKE_PREFIX) - 1); + d += sizeof (CMAKE_PREFIX) - 1; + in += sizeof (RSPAMD_INSTALLPREFIX_MACRO) - 1; + } + break; + case 'V': + if (strncmp (v, RSPAMD_VERSION_MACRO, sizeof (RSPAMD_VERSION_MACRO) - 1) == 0) { + memcpy (d, RVERSION, sizeof (RVERSION) - 1); + d += sizeof (RVERSION) - 1; + in += sizeof (RSPAMD_VERSION_MACRO) - 1; + } + break; + } + + *dest = d; + return in; +} + +gchar* +rspamd_expand_path (memory_pool_t *pool, const gchar *path) +{ + const gchar *p = path; + gchar *dest, *d; + gsize len = 0, orig_len = 0; + + while (*p != '\0') { + if (*p == '$') { + len ++; + p = rspamd_check_path_variable (p + 1, &len); + } + else { + len ++; + } + orig_len ++; + p ++; + } + + if (len == orig_len) { + return memory_pool_strdup (pool, path); + } + + dest = memory_pool_alloc (pool, len + 1); + p = path; + d = dest; + + while (*p != '\0') { + if (*p == '$') { + p = rspamd_expand_path_variable (p, &d); + } + else { + *d++ = *p++; + } + } + + *d = '\0'; + + msg_debug ("expanded %s to %s", path, dest); + + return dest; +} + /* * vi:ts=4 */ diff --git a/src/util.h b/src/util.h index 2526a981a..9278f41a3 100644 --- a/src/util.h +++ b/src/util.h @@ -439,4 +439,15 @@ time_t parse_http_date (const gchar *header, gsize len); */ gint rspamd_read_passphrase (gchar *buf, gint size, gint rwflag, gpointer key); +/** + * Expand path that may contain configuration variables: + * $CONFDIR - configuration directory + * $LOCALSTATESDIR - local states directory + * $INSTALLPREFIX - installation prefix + * $VERSION - rspamd version + * @param pool to use + * @param path path to expand + */ +gchar* rspamd_expand_path (memory_pool_t *pool, const gchar *path); + #endif