]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Project] Fix setting of actions score
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 14 Jan 2019 11:04:33 +0000 (11:04 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 14 Jan 2019 11:04:33 +0000 (11:04 +0000)
src/libserver/cfg_file.h
src/libserver/cfg_rcl.c
src/libserver/cfg_utils.c
src/libserver/dynamic_cfg.c

index 1a30d3889a8c5106e2b3857d6d5d0efe6b3dba08..0557f44870b5907018c1f4679028a8eec21a5921 100644 (file)
@@ -641,6 +641,17 @@ gboolean rspamd_config_set_action_score (struct rspamd_config *cfg,
                const gchar *action_name,
                const ucl_object_t *obj);
 
+/**
+ * Check priority and maybe disable action completely
+ * @param cfg
+ * @param action_name
+ * @param priority
+ * @return
+ */
+gboolean rspamd_config_maybe_disable_action (struct rspamd_config *cfg,
+                                                                                        const gchar *action_name,
+                                                                                        guint priority);
+
 /**
  * Checks if a specified C or lua module is enabled or disabled in the config.
  * The logic of check is the following:
index c03af9c71cfe561d951d18fba2193755f08fbdff..fa1c07f6e908b695e0950a98531d566515050e31 100644 (file)
@@ -539,7 +539,8 @@ rspamd_rcl_actions_handler (rspamd_mempool_t *pool, const ucl_object_t *obj,
                }
                else {
                        if (ucl_object_type (cur) == UCL_NULL) {
-                               action_score = NAN;
+                               rspamd_config_maybe_disable_action (cfg, ucl_object_key (cur),
+                                               ucl_object_get_priority (cur));
                        }
                        else {
                                if (!ucl_object_todouble_safe (cur, &action_score)) {
@@ -556,8 +557,7 @@ rspamd_rcl_actions_handler (rspamd_mempool_t *pool, const ucl_object_t *obj,
 
                        rspamd_config_set_action_score (cfg,
                                        ucl_object_key (cur),
-                                       action_score,
-                                       ucl_object_get_priority (cur));
+                                       cur);
                }
        }
 
index eda78277dc23c8afa2d357eecea205c68abc03ae..1ab2ff05479337d035a6e208960497cb6afd904c 100644 (file)
@@ -1277,7 +1277,7 @@ gboolean
 rspamd_config_check_statfiles (struct rspamd_classifier_config *cf)
 {
        struct rspamd_statfile_config *st;
-       gboolean has_other = FALSE, res = FALSE, cur_class;
+       gboolean has_other = FALSE, res = FALSE, cur_class = FALSE;
        GList *cur;
 
        /* First check classes directly */
@@ -1893,39 +1893,45 @@ rspamd_config_action_from_ucl (struct rspamd_config *cfg,
 {
        const ucl_object_t *elt;
        gdouble threshold = NAN;
-       guint flags = 0, std_act;
+       guint flags = 0, std_act, obj_type;
 
-       elt = ucl_object_lookup_any (obj, "score", "threshold", NULL);
+       obj_type = ucl_object_type (obj);
 
-       if (elt) {
-               threshold = ucl_object_todouble (elt);
-       }
+       if (obj_type == UCL_OBJECT) {
+               obj_type = ucl_object_type (obj);
 
-       elt = ucl_object_lookup_any (obj, "flags");
+               elt = ucl_object_lookup_any (obj, "score", "threshold", NULL);
 
-       if (elt && ucl_object_type (elt) == UCL_ARRAY) {
-               const ucl_object_t *cur;
-               ucl_object_iter_t it = NULL;
+               if (elt) {
+                       threshold = ucl_object_todouble (elt);
+               }
 
-               while ((cur = ucl_object_iterate (elt, &it, true)) != NULL) {
-                       if (ucl_object_type (cur) == UCL_STRING) {
-                               const gchar *fl_str = ucl_object_tostring (cur);
+               elt = ucl_object_lookup_any (obj, "flags");
 
-                               if (g_ascii_strcasecmp (fl_str, "no_threshold") == 0) {
-                                       flags |= RSPAMD_ACTION_NO_THRESHOLD;
-                               }
-                               else if (g_ascii_strcasecmp (fl_str, "threshold_only") == 0) {
-                                       flags |= RSPAMD_ACTION_THRESHOLD_ONLY;
-                               }
-                               else if (g_ascii_strcasecmp (fl_str, "ham") == 0) {
-                                       flags |= RSPAMD_ACTION_HAM;
-                               }
-                               else {
-                                       msg_warn_config ("unknown action flag: %s", fl_str);
+               if (elt && ucl_object_type (elt) == UCL_ARRAY) {
+                       const ucl_object_t *cur;
+                       ucl_object_iter_t it = NULL;
+
+                       while ((cur = ucl_object_iterate (elt, &it, true)) != NULL) {
+                               if (ucl_object_type (cur) == UCL_STRING) {
+                                       const gchar *fl_str = ucl_object_tostring (cur);
+
+                                       if (g_ascii_strcasecmp (fl_str, "no_threshold") == 0) {
+                                               flags |= RSPAMD_ACTION_NO_THRESHOLD;
+                                       } else if (g_ascii_strcasecmp (fl_str, "threshold_only") == 0) {
+                                               flags |= RSPAMD_ACTION_THRESHOLD_ONLY;
+                                       } else if (g_ascii_strcasecmp (fl_str, "ham") == 0) {
+                                               flags |= RSPAMD_ACTION_HAM;
+                                       } else {
+                                               msg_warn_config ("unknown action flag: %s", fl_str);
+                                       }
                                }
                        }
                }
        }
+       else if (obj_type == UCL_FLOAT || obj_type == UCL_INT) {
+               threshold = ucl_object_todouble (obj);
+       }
 
        /* TODO: add lua references support */
 
@@ -1958,15 +1964,19 @@ rspamd_config_set_action_score (struct rspamd_config *cfg,
 {
        struct rspamd_action *act;
        const ucl_object_t *elt;
-       guint priority = ucl_object_get_priority (obj);
+       guint priority = ucl_object_get_priority (obj), obj_type;
 
        g_assert (cfg != NULL);
        g_assert (action_name != NULL);
 
-       elt = ucl_object_lookup (obj, "priority");
+       obj_type = ucl_object_type (obj);
 
-       if (elt) {
-               priority = ucl_object_toint (elt);
+       if (obj_type == UCL_OBJECT) {
+               elt = ucl_object_lookup (obj, "priority");
+
+               if (elt) {
+                       priority = ucl_object_toint (elt);
+               }
        }
 
        HASH_FIND_STR (cfg->actions, action_name, act);
@@ -1977,13 +1987,11 @@ rspamd_config_set_action_score (struct rspamd_config *cfg,
                        /* We can replace data */
                        msg_info_config ("action %s has been already registered with "
                                                         "priority %ud, override it with new priority: %ud, "
-                                                        "old score: %.2f, new score: %.2f",
+                                                        "old score: %.2f",
                                        action_name,
                                        act->priority,
                                        priority,
-                                       act->threshold,
-                                       ucl_object_todouble (
-                                                       ucl_object_lookup_any (obj, "score", "threshold", NULL)));
+                                       act->threshold);
                        return rspamd_config_action_from_ucl (cfg, act, obj, priority);
                }
                else {
@@ -2010,6 +2018,38 @@ rspamd_config_set_action_score (struct rspamd_config *cfg,
        return TRUE;
 }
 
+gboolean
+rspamd_config_maybe_disable_action (struct rspamd_config *cfg,
+                                                                                        const gchar *action_name,
+                                                                                        guint priority)
+{
+       struct rspamd_action *act;
+
+       HASH_FIND_STR (cfg->actions, action_name, act);
+
+       if (act) {
+               if (priority >= act->priority) {
+                       msg_info_config ("disable action %s; old priority: %ud, new priority: %ud",
+                                       action_name,
+                                       act->priority,
+                                       priority);
+
+                       HASH_DEL (cfg->actions, act);
+
+                       return TRUE;
+               }
+               else {
+                       msg_info_config ("action %s has been already registered with "
+                                                        "priority %ud, cannot disable it with new priority: %ud",
+                                       action_name,
+                                       act->priority,
+                                       priority);
+               }
+       }
+
+       return FALSE;
+}
+
 struct rspamd_action *
 rspamd_config_get_action (struct rspamd_config *cfg, const gchar *name)
 {
index 33231b46271adb3dda17970816ddd0b421202043..1e970a17b1e0d90d409e131691adce28c8bf40b7 100644 (file)
@@ -118,7 +118,13 @@ apply_dynamic_conf (const ucl_object_t *top, struct rspamd_config *cfg)
                                                nscore = ucl_object_todouble (v);
                                        }
 
-                                       rspamd_config_set_action_score (cfg, name, nscore, priority);
+                                       ucl_object_t *obj_tbl = ucl_object_typed_new (UCL_OBJECT);
+                                       ucl_object_insert_key (obj_tbl, ucl_object_fromdouble (nscore),
+                                                       "score", 0, false);
+                                       ucl_object_insert_key (obj_tbl, ucl_object_fromdouble (priority),
+                                                       "priority", 0, false);
+                                       rspamd_config_set_action_score (cfg, name, obj_tbl);
+                                       ucl_object_unref (obj_tbl);
                                }
                                else {
                                        msg_info (