aboutsummaryrefslogtreecommitdiffstats
path: root/src/libserver
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2019-01-14 16:48:52 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2019-01-14 16:48:52 +0000
commitaa07f4bc9eff2a252743e20faa334f2aa7d589d5 (patch)
tree177051f9fe263c7b1523aff6f83e8c1675d7d8b0 /src/libserver
parent137f4b1e53572d5f43a5a6368482f82149652091 (diff)
downloadrspamd-aa07f4bc9eff2a252743e20faa334f2aa7d589d5.tar.gz
rspamd-aa07f4bc9eff2a252743e20faa334f2aa7d589d5.zip
[Project] More work towards flexible actions
Diffstat (limited to 'src/libserver')
-rw-r--r--src/libserver/cfg_file.h3
-rw-r--r--src/libserver/cfg_file_private.h2
-rw-r--r--src/libserver/cfg_utils.c21
-rw-r--r--src/libserver/events.c1
-rw-r--r--src/libserver/html.c2
-rw-r--r--src/libserver/protocol.c34
-rw-r--r--src/libserver/roll_history.c5
-rw-r--r--src/libserver/task.c30
8 files changed, 68 insertions, 30 deletions
diff --git a/src/libserver/cfg_file.h b/src/libserver/cfg_file.h
index 0557f4487..5c245a31b 100644
--- a/src/libserver/cfg_file.h
+++ b/src/libserver/cfg_file.h
@@ -708,6 +708,9 @@ gboolean rspamd_config_radix_from_ucl (struct rspamd_config *cfg,
struct rspamd_action * rspamd_config_get_action (struct rspamd_config *cfg,
const gchar *name);
+struct rspamd_action * rspamd_config_get_action_by_type (struct rspamd_config *cfg,
+ enum rspamd_action_type type);
+
#define msg_err_config(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \
cfg->cfg_pool->tag.tagname, cfg->checksum, \
G_STRFUNC, \
diff --git a/src/libserver/cfg_file_private.h b/src/libserver/cfg_file_private.h
index 03f735efe..94cb9aa08 100644
--- a/src/libserver/cfg_file_private.h
+++ b/src/libserver/cfg_file_private.h
@@ -26,7 +26,7 @@
* Action config definition
*/
struct rspamd_action {
- enum rspamd_action_type action;
+ enum rspamd_action_type action_type;
enum rspamd_action_flags flags;
guint priority;
gint lua_handler_ref; /* If special handling is needed */
diff --git a/src/libserver/cfg_utils.c b/src/libserver/cfg_utils.c
index 1ab2ff054..26bf4e4b0 100644
--- a/src/libserver/cfg_utils.c
+++ b/src/libserver/cfg_utils.c
@@ -141,7 +141,7 @@ rspamd_config_new (enum rspamd_config_init_flags flags)
action->threshold = NAN;
action->name = rspamd_mempool_strdup (cfg->cfg_pool,
rspamd_action_to_str (i));
- action->action = i;
+ action->action_type = i;
if (i == METRIC_ACTION_SOFT_REJECT) {
action->flags |= RSPAMD_ACTION_NO_THRESHOLD;
@@ -1946,10 +1946,10 @@ rspamd_config_action_from_ucl (struct rspamd_config *cfg,
act->flags = flags;
if (rspamd_action_from_str (act->name, &std_act)) {
- act->action = std_act;
+ act->action_type = std_act;
}
else {
- act->action = METRIC_ACTION_CUSTOM;
+ act->action_type = METRIC_ACTION_CUSTOM;
}
rspamd_actions_sort (cfg);
@@ -2060,6 +2060,21 @@ rspamd_config_get_action (struct rspamd_config *cfg, const gchar *name)
return res;
}
+struct rspamd_action *
+rspamd_config_get_action_by_type (struct rspamd_config *cfg,
+ enum rspamd_action_type type)
+{
+ struct rspamd_action *cur, *tmp;
+
+ HASH_ITER (hh, cfg->actions, cur, tmp) {
+ if (cur->action_type == type) {
+ return cur;
+ }
+ }
+
+ return NULL;
+}
+
gboolean
rspamd_config_radix_from_ucl (struct rspamd_config *cfg,
const ucl_object_t *obj,
diff --git a/src/libserver/events.c b/src/libserver/events.c
index 2fd0c55ff..3f6d47112 100644
--- a/src/libserver/events.c
+++ b/src/libserver/events.c
@@ -16,6 +16,7 @@
#include "config.h"
#include "rspamd.h"
#include "contrib/uthash/utlist.h"
+#include "contrib/libucl/khash.h"
#include "events.h"
#include "cryptobox.h"
diff --git a/src/libserver/html.c b/src/libserver/html.c
index 2568d4c2a..8ebb3862e 100644
--- a/src/libserver/html.c
+++ b/src/libserver/html.c
@@ -22,6 +22,8 @@
#include "html_colors.h"
#include "html_entities.h"
#include "url.h"
+#include "contrib/libucl/khash.h"
+
#include <unicode/uversion.h>
#include <unicode/ucnv.h>
#if U_ICU_VERSION_MAJOR_NUM >= 46
diff --git a/src/libserver/protocol.c b/src/libserver/protocol.c
index 35463be52..713c54499 100644
--- a/src/libserver/protocol.c
+++ b/src/libserver/protocol.c
@@ -19,6 +19,8 @@
#include "utlist.h"
#include "http_private.h"
#include "worker_private.h"
+#include "libserver/cfg_file_private.h"
+#include "libmime/filter_private.h"
#include "contrib/zstd/zstd.h"
#include "lua/lua_common.h"
#include "unix-std.h"
@@ -922,12 +924,12 @@ rspamd_metric_result_ucl (struct rspamd_task *task,
{
struct rspamd_symbol_result *sym;
gboolean is_spam;
- enum rspamd_action_type action = METRIC_ACTION_NOACTION;
+ struct rspamd_action *action;
ucl_object_t *obj = NULL, *sobj;
const gchar *subject;
- action = rspamd_check_action_metric (task, mres);
- is_spam = (action < METRIC_ACTION_GREYLIST);
+ action = rspamd_check_action_metric (task);
+ is_spam = !(action->flags & RSPAMD_ACTION_HAM);
if (task->cmd != CMD_CHECK_V2) {
obj = ucl_object_typed_new (UCL_OBJECT);
@@ -955,10 +957,10 @@ rspamd_metric_result_ucl (struct rspamd_task *task,
ucl_object_fromdouble (rspamd_task_get_required_score (task, mres)),
"required_score", 0, false);
ucl_object_insert_key (obj,
- ucl_object_fromstring (rspamd_action_to_str (action)),
+ ucl_object_fromstring (action->name),
"action", 0, false);
- if (action == METRIC_ACTION_REWRITE_SUBJECT) {
+ if (action->action_type == METRIC_ACTION_REWRITE_SUBJECT) {
subject = rspamd_protocol_rewrite_subject (task);
if (subject) {
@@ -1253,7 +1255,8 @@ rspamd_protocol_http_reply (struct rspamd_http_message *msg,
gpointer h, v;
ucl_object_t *top = NULL;
rspamd_fstring_t *reply;
- gint action, flags = RSPAMD_PROTOCOL_DEFAULT;
+ gint flags = RSPAMD_PROTOCOL_DEFAULT;
+ struct rspamd_action *action;
/* Write custom headers */
g_hash_table_iter_init (&hiter, task->reply_headers);
@@ -1376,19 +1379,24 @@ end:
if (metric_res != NULL) {
- action = rspamd_check_action_metric (task, metric_res);
+ action = rspamd_check_action_metric (task);
- if (action == METRIC_ACTION_SOFT_REJECT &&
+ /* TODO: handle custom actions in stats */
+ if (action->action_type == METRIC_ACTION_SOFT_REJECT &&
(task->flags & RSPAMD_TASK_FLAG_GREYLISTED)) {
/* Set stat action to greylist to display greylisted messages */
- action = METRIC_ACTION_GREYLIST;
+#ifndef HAVE_ATOMIC_BUILTINS
+ task->worker->srv->stat->actions_stat[METRIC_ACTION_GREYLIST]++;
+#else
+ __atomic_add_fetch (&task->worker->srv->stat->actions_stat[METRIC_ACTION_GREYLIST],
+ 1, __ATOMIC_RELEASE);
+#endif
}
-
- if (action < METRIC_ACTION_MAX) {
+ else if (action->action_type < METRIC_ACTION_MAX) {
#ifndef HAVE_ATOMIC_BUILTINS
- task->worker->srv->stat->actions_stat[action]++;
+ task->worker->srv->stat->actions_stat[action->action_type]++;
#else
- __atomic_add_fetch (&task->worker->srv->stat->actions_stat[action],
+ __atomic_add_fetch (&task->worker->srv->stat->actions_stat[action->action_type],
1, __ATOMIC_RELEASE);
#endif
}
diff --git a/src/libserver/roll_history.c b/src/libserver/roll_history.c
index 3df597816..c9367409d 100644
--- a/src/libserver/roll_history.c
+++ b/src/libserver/roll_history.c
@@ -17,6 +17,7 @@
#include "rspamd.h"
#include "lua/lua_common.h"
#include "unix-std.h"
+#include "cfg_file_private.h"
static const gchar rspamd_history_magic_old[] = {'r', 's', 'h', '1'};
@@ -101,6 +102,7 @@ rspamd_roll_history_update (struct roll_history *history,
struct roll_history_row *row;
struct rspamd_metric_result *metric_res;
struct history_metric_callback_data cbdata;
+ struct rspamd_action *action;
if (history->disabled) {
return;
@@ -155,7 +157,8 @@ rspamd_roll_history_update (struct roll_history *history,
}
else {
row->score = metric_res->score;
- row->action = rspamd_check_action_metric (task, metric_res);
+ action = rspamd_check_action_metric (task);
+ row->action = action->action_type;
row->required_score = rspamd_task_get_required_score (task, metric_res);
cbdata.pos = row->symbols;
cbdata.remain = sizeof (row->symbols);
diff --git a/src/libserver/task.c b/src/libserver/task.c
index eaa379361..df1a561cb 100644
--- a/src/libserver/task.c
+++ b/src/libserver/task.c
@@ -26,7 +26,10 @@
#include "utlist.h"
#include "contrib/zstd/zstd.h"
#include "libserver/mempool_vars_internal.h"
+#include "libserver/cfg_file_private.h"
#include "libmime/lang_detection.h"
+#include "libmime/filter_private.h"
+
#include <math.h>
/*
@@ -1072,11 +1075,11 @@ rspamd_task_log_metric_res (struct rspamd_task *task,
rspamd_fstring_t *symbuf;
struct rspamd_symbol_result *sym;
GPtrArray *sorted_symbols;
- enum rspamd_action_type act;
+ struct rspamd_action *act;
guint i, j;
mres = task->result;
- act = rspamd_check_action_metric (task, mres);
+ act = rspamd_check_action_metric (task);
if (mres != NULL) {
switch (lf->type) {
@@ -1084,7 +1087,7 @@ rspamd_task_log_metric_res (struct rspamd_task *task,
if (RSPAMD_TASK_IS_SKIPPED (task)) {
res.begin = "S";
}
- else if (act == METRIC_ACTION_REJECT) {
+ else if (!(act->flags & RSPAMD_ACTION_HAM)) {
res.begin = "T";
}
else {
@@ -1094,7 +1097,7 @@ rspamd_task_log_metric_res (struct rspamd_task *task,
res.len = 1;
break;
case RSPAMD_LOG_ACTION:
- res.begin = rspamd_action_to_str (act);
+ res.begin = act->name;
res.len = strlen (res.begin);
break;
case RSPAMD_LOG_SCORES:
@@ -1441,14 +1444,17 @@ rspamd_task_log_variable (struct rspamd_task *task,
if (!isnan (pr->target_score)) {
var.len = rspamd_snprintf (numbuf, sizeof (numbuf),
"%s \"%s\"; score=%.2f (set by %s)",
- rspamd_action_to_str (pr->action),
- pr->message, pr->target_score, pr->module);
+ pr->action->name,
+ pr->message,
+ pr->target_score,
+ pr->module);
}
else {
var.len = rspamd_snprintf (numbuf, sizeof (numbuf),
"%s \"%s\"; score=nan (set by %s)",
- rspamd_action_to_str (pr->action),
- pr->message, pr->module);
+ pr->action->name,
+ pr->message,
+ pr->module);
}
var.begin = numbuf;
}
@@ -1536,7 +1542,7 @@ rspamd_task_write_log (struct rspamd_task *task)
gdouble
rspamd_task_get_required_score (struct rspamd_task *task, struct rspamd_metric_result *m)
{
- guint i;
+ gint i;
if (m == NULL) {
m = task->result;
@@ -1546,9 +1552,9 @@ rspamd_task_get_required_score (struct rspamd_task *task, struct rspamd_metric_r
}
}
- for (i = METRIC_ACTION_REJECT; i < METRIC_ACTION_NOACTION; i ++) {
- if (!isnan (m->actions_limits[i])) {
- return m->actions_limits[i];
+ for (i = m->nactions - 1; i >= 0; i --) {
+ if (!isnan (m->actions_limits[i].cur_limit)) {
+ return m->actions_limits[i].cur_limit;
}
}