1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
/*-
* Copyright 2016 Vsevolod Stakhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "config.h"
#include "mem_pool.h"
#include "filter.h"
#include "rspamd.h"
#include "message.h"
#include "lua/lua_common.h"
#include "cryptobox.h"
#include <math.h>
#define COMMON_PART_FACTOR 95
struct metric_result *
rspamd_create_metric_result (struct rspamd_task *task, const gchar *name)
{
struct metric_result *metric_res;
struct metric *metric;
guint i;
metric_res = g_hash_table_lookup (task->results, name);
if (metric_res != NULL) {
return metric_res;
}
metric = g_hash_table_lookup (task->cfg->metrics, name);
if (metric == NULL) {
return NULL;
}
metric_res =
rspamd_mempool_alloc (task->task_pool,
sizeof (struct metric_result));
metric_res->symbols = g_hash_table_new (rspamd_str_hash,
rspamd_str_equal);
rspamd_mempool_add_destructor (task->task_pool,
(rspamd_mempool_destruct_t) g_hash_table_unref,
metric_res->symbols);
metric_res->sym_groups = g_hash_table_new (g_direct_hash, g_direct_equal);
rspamd_mempool_add_destructor (task->task_pool,
(rspamd_mempool_destruct_t) g_hash_table_unref,
metric_res->sym_groups);
metric_res->metric = metric;
metric_res->grow_factor = 0;
metric_res->score = 0;
for (i = 0; i < METRIC_ACTION_MAX; i++) {
metric_res->actions_limits[i] = metric->actions[i].score;
}
metric_res->action = METRIC_ACTION_MAX;
g_hash_table_insert (task->results, (gpointer) metric->name,
metric_res);
return metric_res;
}
static void
insert_metric_result (struct rspamd_task *task,
struct metric *metric,
const gchar *symbol,
double flag,
const gchar *opt,
gboolean single)
{
struct metric_result *metric_res;
struct symbol *s;
char *opt_cpy;
gdouble w, *gr_score = NULL;
struct rspamd_symbol_def *sdef;
struct rspamd_symbols_group *gr = NULL;
const ucl_object_t *mobj, *sobj;
metric_res = rspamd_create_metric_result (task, metric->name);
sdef = g_hash_table_lookup (metric->symbols, symbol);
if (sdef == NULL) {
w = 0.0;
}
else {
w = (*sdef->weight_ptr) * flag;
gr = sdef->gr;
if (gr != NULL) {
gr_score = g_hash_table_lookup (metric_res->sym_groups, gr);
if (gr_score == NULL) {
gr_score = rspamd_mempool_alloc (task->task_pool, sizeof (gdouble));
*gr_score = 0;
g_hash_table_insert (metric_res->sym_groups, gr, gr_score);
}
}
}
if (task->settings) {
mobj = task->settings;
gdouble corr;
sobj = ucl_object_lookup (mobj, symbol);
if (sobj != NULL && ucl_object_todouble_safe (sobj, &corr)) {
msg_debug ("settings: changed weight of symbol %s from %.2f to %.2f",
symbol, w, corr);
w = corr * flag;
}
}
/* XXX: does not take grow factor into account */
if (gr != NULL && gr_score != NULL && gr->max_score > 0.0) {
if (*gr_score >= gr->max_score) {
msg_info_task ("maximum group score %.2f for group %s has been reached,"
" ignoring symbol %s with weight %.2f", gr->max_score,
gr->name, symbol, w);
return;
}
else if (*gr_score + w > gr->max_score) {
w = gr->max_score - *gr_score;
}
*gr_score += w;
}
/* Add metric score */
if ((s = g_hash_table_lookup (metric_res->symbols, symbol)) != NULL) {
if (sdef && (sdef->flags & RSPAMD_SYMBOL_FLAG_ONESHOT)) {
/*
* For one shot symbols we do not need to add them again, so
* we just force single behaviour here
*/
single = TRUE;
}
if (opt) {
if (s->options && !(sdef &&
(sdef->flags & RSPAMD_SYMBOL_FLAG_ONEPARAM))) {
/* Append new options */
if (!g_hash_table_lookup (s->options, opt)) {
opt_cpy = rspamd_mempool_strdup (task->task_pool, opt);
g_hash_table_insert (s->options, opt_cpy, opt_cpy);
}
}
else {
s->options = g_hash_table_new (rspamd_strcase_hash,
rspamd_strcase_equal);
rspamd_mempool_add_destructor (task->task_pool,
(rspamd_mempool_destruct_t)g_hash_table_unref,
s->options);
opt_cpy = rspamd_mempool_strdup (task->task_pool, opt);
g_hash_table_insert (s->options, opt_cpy, opt_cpy);
}
}
if (!single) {
/* Handle grow factor */
if (metric_res->grow_factor && w > 0) {
w *= metric_res->grow_factor;
metric_res->grow_factor *= metric->grow_factor;
}
s->score += w;
metric_res->score += w;
}
else {
if (fabs (s->score) < fabs (w)) {
/* Replace less weight with a bigger one */
metric_res->score = metric_res->score - s->score + w;
s->score = w;
}
}
}
else {
s = rspamd_mempool_alloc (task->task_pool, sizeof (struct symbol));
/* Handle grow factor */
if (metric_res->grow_factor && w > 0) {
w *= metric_res->grow_factor;
metric_res->grow_factor *= metric->grow_factor;
}
else if (w > 0) {
metric_res->grow_factor = metric->grow_factor;
}
s->score = w;
s->name = symbol;
s->def = sdef;
metric_res->score += w;
if (opt) {
s->options = g_hash_table_new (rspamd_strcase_hash,
rspamd_strcase_equal);
rspamd_mempool_add_destructor (task->task_pool,
(rspamd_mempool_destruct_t)g_hash_table_unref,
s->options);
opt_cpy = rspamd_mempool_strdup (task->task_pool, opt);
g_hash_table_insert (s->options, opt_cpy, opt_cpy);
}
else {
s->options = NULL;
}
g_hash_table_insert (metric_res->symbols, (gpointer) symbol, s);
}
msg_debug ("symbol %s, score %.2f, metric %s, factor: %f",
symbol,
s->score,
metric->name,
w);
}
static void
insert_result_common (struct rspamd_task *task,
const gchar *symbol,
double flag,
const gchar *opt,
gboolean single)
{
struct metric *metric;
GList *cur, *metric_list;
metric_list = g_hash_table_lookup (task->cfg->metrics_symbols, symbol);
if (metric_list) {
cur = metric_list;
while (cur) {
metric = cur->data;
insert_metric_result (task, metric, symbol, flag, opt, single);
cur = g_list_next (cur);
}
}
else {
/* Insert symbol to default metric */
insert_metric_result (task,
task->cfg->default_metric,
symbol,
flag,
opt,
single);
}
/* Process cache item */
if (task->cfg->cache) {
rspamd_symbols_cache_inc_frequency (task->cfg->cache, symbol);
}
}
/* Insert result that may be increased on next insertions */
void
rspamd_task_insert_result (struct rspamd_task *task,
const gchar *symbol,
double flag,
const gchar *opt)
{
insert_result_common (task, symbol, flag, opt, task->cfg->one_shot_mode);
}
/* Insert result as a single option */
void
rspamd_task_insert_result_single (struct rspamd_task *task,
const gchar *symbol,
double flag,
const gchar *opt)
{
insert_result_common (task, symbol, flag, opt, TRUE);
}
gboolean
rspamd_action_from_str (const gchar *data, gint *result)
{
guint64 h;
h = rspamd_cryptobox_fast_hash_specific (RSPAMD_CRYPTOBOX_XXHASH64,
data, strlen (data), 0xdeadbabe);
switch (h) {
case 0x9917BFDB46332B8CULL: /* reject */
*result = METRIC_ACTION_REJECT;
break;
case 0x7130EE37D07B3715ULL: /* greylist */
*result = METRIC_ACTION_GREYLIST;
break;
case 0xCA6087E05480C60CULL: /* add_header */
case 0x87A3D27783B16241ULL: /* add header */
*result = METRIC_ACTION_ADD_HEADER;
break;
case 0x4963374ED8B90449ULL: /* rewrite_subject */
case 0x5C9FC4679C025948ULL: /* rewrite subject */
*result = METRIC_ACTION_REWRITE_SUBJECT;
break;
case 0xFC7D6502EE71FDD9ULL: /* soft reject */
case 0x73576567C262A82DULL: /* soft_reject */
*result = METRIC_ACTION_SOFT_REJECT;
break;
case 0x207091B927D1EC0DULL: /* no action */
case 0xB7D92D002CD46325ULL: /* no_action */
case 0x167C0DF4BAA9BCECULL: /* accept */
*result = METRIC_ACTION_NOACTION;
break;
default:
return FALSE;
}
return TRUE;
}
const gchar *
rspamd_action_to_str (enum rspamd_metric_action action)
{
switch (action) {
case METRIC_ACTION_REJECT:
return "reject";
case METRIC_ACTION_SOFT_REJECT:
return "soft reject";
case METRIC_ACTION_REWRITE_SUBJECT:
return "rewrite subject";
case METRIC_ACTION_ADD_HEADER:
return "add header";
case METRIC_ACTION_GREYLIST:
return "greylist";
case METRIC_ACTION_NOACTION:
return "no action";
case METRIC_ACTION_MAX:
return "invalid max action";
}
return "unknown action";
}
const gchar *
rspamd_action_to_str_alt (enum rspamd_metric_action action)
{
switch (action) {
case METRIC_ACTION_REJECT:
return "reject";
case METRIC_ACTION_SOFT_REJECT:
return "soft_reject";
case METRIC_ACTION_REWRITE_SUBJECT:
return "rewrite_subject";
case METRIC_ACTION_ADD_HEADER:
return "add_header";
case METRIC_ACTION_GREYLIST:
return "greylist";
case METRIC_ACTION_NOACTION:
return "no action";
case METRIC_ACTION_MAX:
return "invalid max action";
}
return "unknown action";
}
enum rspamd_metric_action
rspamd_check_action_metric (struct rspamd_task *task, struct metric_result *mres)
{
struct metric_action *action, *selected_action = NULL;
double max_score = 0, sc;
int i;
if (task->pre_result.action == METRIC_ACTION_MAX) {
for (i = METRIC_ACTION_REJECT; i < METRIC_ACTION_MAX; i++) {
action = &mres->metric->actions[i];
sc = mres->actions_limits[i];
if (isnan (sc)) {
continue;
}
if (mres->score >= sc && sc > max_score) {
selected_action = action;
max_score = sc;
}
}
}
else {
i = task->pre_result.action;
selected_action = &mres->metric->actions[i];
sc = mres->actions_limits[i];
while (isnan (sc)) {
i = (i + 1) % METRIC_ACTION_MAX;
sc = mres->actions_limits[i];
if (i == task->pre_result.action) {
/* No scores defined, just avoid NaN */
sc = 0;
break;
}
}
mres->score = sc;
}
if (selected_action) {
return selected_action->action;
}
return METRIC_ACTION_NOACTION;
}
|