diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-01-14 11:04:33 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-01-14 11:04:33 +0000 |
commit | 137f4b1e53572d5f43a5a6368482f82149652091 (patch) | |
tree | 47702d200012e7b68419ecbc71c7c2805471a073 | |
parent | d43bc62e0a733b8666aad028628173c71c3fc2f3 (diff) | |
download | rspamd-137f4b1e53572d5f43a5a6368482f82149652091.tar.gz rspamd-137f4b1e53572d5f43a5a6368482f82149652091.zip |
[Project] Fix setting of actions score
-rw-r--r-- | src/libserver/cfg_file.h | 11 | ||||
-rw-r--r-- | src/libserver/cfg_rcl.c | 6 | ||||
-rw-r--r-- | src/libserver/cfg_utils.c | 104 | ||||
-rw-r--r-- | src/libserver/dynamic_cfg.c | 8 |
4 files changed, 93 insertions, 36 deletions
diff --git a/src/libserver/cfg_file.h b/src/libserver/cfg_file.h index 1a30d3889..0557f4487 100644 --- a/src/libserver/cfg_file.h +++ b/src/libserver/cfg_file.h @@ -642,6 +642,17 @@ gboolean rspamd_config_set_action_score (struct rspamd_config *cfg, 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: * diff --git a/src/libserver/cfg_rcl.c b/src/libserver/cfg_rcl.c index c03af9c71..fa1c07f6e 100644 --- a/src/libserver/cfg_rcl.c +++ b/src/libserver/cfg_rcl.c @@ -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); } } diff --git a/src/libserver/cfg_utils.c b/src/libserver/cfg_utils.c index eda78277d..1ab2ff054 100644 --- a/src/libserver/cfg_utils.c +++ b/src/libserver/cfg_utils.c @@ -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) { diff --git a/src/libserver/dynamic_cfg.c b/src/libserver/dynamic_cfg.c index 33231b462..1e970a17b 100644 --- a/src/libserver/dynamic_cfg.c +++ b/src/libserver/dynamic_cfg.c @@ -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 ( |