]> source.dussan.org Git - rspamd.git/commitdiff
Expand path in file maps.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 13 Nov 2013 18:17:00 +0000 (18:17 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 13 Nov 2013 18:17:00 +0000 (18:17 +0000)
src/cfg_file.h
src/cfg_rcl.c
src/cfg_utils.c
src/map.c
src/util.c
src/util.h

index b7deb1ca698567f2bf4b67025d7a723a5cf2b46e..9dec61bc3c7418f061abc7fa76d1fadb9648b9c3 100644 (file)
@@ -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 
index 6fc92ed19fbeab75b50a6c8ffc0d755ab2b1a702..557c532e88162bc5bdbb2f15ab07e881fc367f29 100644 (file)
@@ -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);
index 6a9477e00c1ad342800407cbab4eb8a12efb56cf..dd6b00dcd92e3c148292b6ff98b15fea05b866b2 100644 (file)
@@ -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
  */
index 7c61dcc9eb1e268e618e90fd98b300e276c29317..25541a7e69432af5fb5c35be8e5735812e826c42 100644 (file)
--- 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);
        }
index 88d62f974dce6e2abca9e5ef4eb1245f1b23ea1d..1e0378d2ae9ae3e635963e7c6715559482d25c08 100644 (file)
@@ -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
  */
index 2526a981a49ff73f1e12298a2860258f02a83648..9278f41a327ffed8d7b71de2a95406598efcb97d 100644 (file)
@@ -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