summaryrefslogtreecommitdiffstats
path: root/src/libserver/rspamd_symcache.h
blob: 2c67cba0df1cba0cee7be438ecd9e896e3c35877 (plain)
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
 * Copyright 2023 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.
 */
#ifndef RSPAMD_SYMBOLS_CACHE_H
#define RSPAMD_SYMBOLS_CACHE_H

#include "config.h"
#include "ucl.h"
#include "cfg_file.h"
#include "contrib/libev/ev.h"

#include <lua.h>

#ifdef __cplusplus
extern "C" {
#endif

struct rspamd_task;
struct rspamd_config;
struct rspamd_symcache;
struct rspamd_worker;
struct rspamd_symcache_dynamic_item;
struct rspamd_symcache_item;
struct rspamd_config_settings_elt;

typedef void (*symbol_func_t)(struct rspamd_task *task,
							  struct rspamd_symcache_dynamic_item *item,
							  gpointer user_data);

enum rspamd_symbol_type {
	SYMBOL_TYPE_NORMAL = (1u << 0u),
	SYMBOL_TYPE_VIRTUAL = (1u << 1u),
	SYMBOL_TYPE_CALLBACK = (1u << 2u),
	SYMBOL_TYPE_GHOST = (1u << 3u),
	SYMBOL_TYPE_SKIPPED = (1u << 4u),
	SYMBOL_TYPE_COMPOSITE = (1u << 5u),
	SYMBOL_TYPE_CLASSIFIER = (1u << 6u),
	SYMBOL_TYPE_FINE = (1u << 7u),
	SYMBOL_TYPE_EMPTY = (1u << 8u),      /* Allow execution on empty tasks */
	SYMBOL_TYPE_CONNFILTER = (1u << 9u), /* Connection stage filter */
	SYMBOL_TYPE_PREFILTER = (1u << 10u),
	SYMBOL_TYPE_POSTFILTER = (1u << 11u),
	SYMBOL_TYPE_NOSTAT = (1u << 12u),             /* Skip as statistical symbol */
	SYMBOL_TYPE_IDEMPOTENT = (1u << 13u),         /* Symbol cannot change metric */
	SYMBOL_TYPE_TRIVIAL = (1u << 14u),            /* Symbol is trivial */
	SYMBOL_TYPE_MIME_ONLY = (1u << 15u),          /* Symbol is mime only */
	SYMBOL_TYPE_EXPLICIT_DISABLE = (1u << 16u),   /* Symbol should be disabled explicitly only */
	SYMBOL_TYPE_IGNORE_PASSTHROUGH = (1u << 17u), /* Symbol ignores passthrough result */
	SYMBOL_TYPE_EXPLICIT_ENABLE = (1u << 18u),    /* Symbol should be enabled explicitly only */
	SYMBOL_TYPE_USE_CORO = (1u << 19u),           /* Symbol uses lua coroutines */
};

/**
 * Abstract structure for saving callback data for symbols
 */
struct rspamd_abstract_callback_data {
	guint64 magic;
	char data[];
};

/**
 * Shared memory block specific for each symbol
 */
struct rspamd_symcache_item_stat {
	struct rspamd_counter_data time_counter;
	gdouble avg_time;
	gdouble weight;
	guint hits;
	guint64 total_hits;
	struct rspamd_counter_data frequency_counter;
	gdouble avg_frequency;
	gdouble stddev_frequency;
};

/**
 * Creates new cache structure
 * @return
 */
struct rspamd_symcache *rspamd_symcache_new(struct rspamd_config *cfg);

/**
 * Remove the cache structure syncing data if needed
 * @param cache
 */
void rspamd_symcache_destroy(struct rspamd_symcache *cache);

/**
 * Saves symbols cache to disk if possible
 * @param cache
 */
void rspamd_symcache_save(struct rspamd_symcache *cache);

/**
 * Load symbols cache from file, must be called _after_ init_symbols_cache
 */
gboolean rspamd_symcache_init(struct rspamd_symcache *cache);

/**
 * Generic function to register a symbol
 * @param cache
 * @param name
 * @param weight
 * @param priority
 * @param func
 * @param user_data
 * @param type
 * @param parent
 */
gint rspamd_symcache_add_symbol(struct rspamd_symcache *cache,
								const gchar *name,
								gint priority,
								symbol_func_t func,
								gpointer user_data,
								int type,
								gint parent);

/**
 * Adds augmentation to the symbol
 * @param cache
 * @param sym_id
 * @param augmentation
 * @return
 */
bool rspamd_symcache_add_symbol_augmentation(struct rspamd_symcache *cache,
											 int sym_id,
											 const char *augmentation,
											 const char *value);

/**
 * Add callback to be executed whenever symbol has peak value
 * @param cache
 * @param cbref
 */
void rspamd_symcache_set_peak_callback(struct rspamd_symcache *cache,
									   gint cbref);

/**
 * Add delayed condition to the specific symbol in cache. So symbol can be absent
 * to the moment of addition
 * @param cache
 * @param id id of symbol
 * @param L lua state pointer
 * @param cbref callback reference (returned by luaL_ref)
 * @return TRUE if condition has been added
 */
gboolean rspamd_symcache_add_condition_delayed(struct rspamd_symcache *cache,
											   const gchar *sym,
											   lua_State *L, gint cbref);

/**
 * Find symbol in cache by id and returns its id resolving virtual symbols if
 * applicable
 * @param cache
 * @param name
 * @return id of symbol or (-1) if a symbol has not been found
 */
gint rspamd_symcache_find_symbol(struct rspamd_symcache *cache,
								 const gchar *name);

/**
 * Get statistics for a specific symbol
 * @param cache
 * @param name
 * @param frequency
 * @param tm
 * @return
 */
gboolean rspamd_symcache_stat_symbol(struct rspamd_symcache *cache,
									 const gchar *name,
									 gdouble *frequency,
									 gdouble *freq_stddev,
									 gdouble *tm,
									 guint *nhits);

/**
 * Returns number of symbols registered in symbols cache
 * @param cache
 * @return number of symbols in the cache
 */
guint rspamd_symcache_stats_symbols_count(struct rspamd_symcache *cache);

/**
 * Validate cache items against theirs weights defined in metrics
 * @param cache symbols cache
 * @param cfg configuration
 * @param strict do strict checks - symbols MUST be described in metrics
 */
gboolean rspamd_symcache_validate(struct rspamd_symcache *cache,
								  struct rspamd_config *cfg,
								  gboolean strict);

/**
 * Call function for cached symbol using saved callback
 * @param task task object
 * @param cache symbols cache
 * @param saved_item pointer to currently saved item
 */
gboolean rspamd_symcache_process_symbols(struct rspamd_task *task,
										 struct rspamd_symcache *cache,
										 guint stage);

/**
 * Return statistics about the cache as ucl object (array of objects one per item)
 * @param cache
 * @return
 */
ucl_object_t *rspamd_symcache_counters(struct rspamd_symcache *cache);

/**
 * Start cache reloading
 * @param cache
 * @param ev_base
 */
void *rspamd_symcache_start_refresh(struct rspamd_symcache *cache,
									struct ev_loop *ev_base,
									struct rspamd_worker *w);

/**
 * Increases counter for a specific symbol
 * @param cache
 * @param symbol
 */
void rspamd_symcache_inc_frequency(struct rspamd_symcache *_cache,
								   struct rspamd_symcache_item *item,
								   const gchar *sym_name);

/**
 * Add delayed dependency that is resolved on cache post-load routine
 * @param cache
 * @param from
 * @param to
 */
void rspamd_symcache_add_delayed_dependency(struct rspamd_symcache *cache,
											const gchar *from, const gchar *to);

/**
 * Get abstract callback data for a symbol (or its parent symbol)
 * @param cache cache object
 * @param symbol symbol name
 * @return abstract callback data or NULL if symbol is absent or has no data attached
 */
struct rspamd_abstract_callback_data *rspamd_symcache_get_cbdata(
	struct rspamd_symcache *cache, const gchar *symbol);

/**
 * Returns symbol's parent name (or symbol name itself)
 * @param cache
 * @param symbol
 * @return
 */
const gchar *rspamd_symcache_get_parent(struct rspamd_symcache *cache,
										const gchar *symbol);

guint rspamd_symcache_get_symbol_flags(struct rspamd_symcache *cache,
									   const gchar *symbol);

void rspamd_symcache_get_symbol_details(struct rspamd_symcache *cache,
										const gchar *symbol,
										ucl_object_t *this_sym_ucl);


/**
 * Process settings for task
 * @param task
 * @param cache
 * @return
 */
gboolean rspamd_symcache_process_settings(struct rspamd_task *task,
										  struct rspamd_symcache *cache);


/**
 * Checks if a symbol specified has been checked (or disabled)
 * @param task
 * @param cache
 * @param symbol
 * @return
 */
gboolean rspamd_symcache_is_checked(struct rspamd_task *task,
									struct rspamd_symcache *cache,
									const gchar *symbol);

/**
 * Returns checksum for all cache items
 * @param cache
 * @return
 */
guint64 rspamd_symcache_get_cksum(struct rspamd_symcache *cache);

/**
 * Checks if a symbols is enabled (not checked and conditions return true if present)
 * @param task
 * @param cache
 * @param symbol
 * @return
 */
gboolean rspamd_symcache_is_symbol_enabled(struct rspamd_task *task,
										   struct rspamd_symcache *cache,
										   const gchar *symbol);

/**
 * Enable this symbol for task
 * @param task
 * @param cache
 * @param symbol
 * @return TRUE if a symbol has been enabled (not executed before)
 */
gboolean rspamd_symcache_enable_symbol(struct rspamd_task *task,
									   struct rspamd_symcache *cache,
									   const gchar *symbol);

/**
 * Enable this symbol for task
 * @param task
 * @param cache
 * @param symbol
 * @return TRUE if a symbol has been disabled (not executed before)
 */
gboolean rspamd_symcache_disable_symbol(struct rspamd_task *task,
										struct rspamd_symcache *cache,
										const gchar *symbol);

/**
 * Disable execution of a symbol or a pattern (a string enclosed in `//`) permanently
 * @param task
 * @param cache
 * @param symbol
 * @return
 */
void rspamd_symcache_disable_symbol_static(struct rspamd_symcache *cache,
										   const gchar *symbol);
/**
 * Add a symbol or a pattern to the list of explicitly and statically enabled symbols
 * @param cache
 * @param symbol
 * @return
 */
void rspamd_symcache_enable_symbol_static(struct rspamd_symcache *cache,
										  const gchar *symbol);

/**
 * Process specific function for each cache element (in order they are added)
 * @param cache
 * @param func
 * @param ud
 */
void rspamd_symcache_foreach(struct rspamd_symcache *cache,
							 void (*func)(struct rspamd_symcache_item *item, gpointer /* userdata */),
							 gpointer ud);

/**
 * Returns the current item being processed (if any)
 * @param task
 * @return
 */
struct rspamd_symcache_dynamic_item *rspamd_symcache_get_cur_item(struct rspamd_task *task);

/**
 * Replaces the current item being processed.
 * Returns the current item being processed (if any)
 * @param task
 * @param item
 * @return
 */
struct rspamd_symcache_dynamic_item *rspamd_symcache_set_cur_item(struct rspamd_task *task,
																  struct rspamd_symcache_dynamic_item *item);


/**
 * Finalize the current async element potentially calling its deps
 */
void rspamd_symcache_finalize_item(struct rspamd_task *task,
								   struct rspamd_symcache_dynamic_item *item);

/*
 * Increase number of async events pending for an item
 */
guint rspamd_symcache_item_async_inc_full(struct rspamd_task *task,
										  struct rspamd_symcache_dynamic_item *item,
										  const gchar *subsystem,
										  const gchar *loc);

#define rspamd_symcache_item_async_inc(task, item, subsystem) \
	rspamd_symcache_item_async_inc_full(task, item, subsystem, G_STRLOC)

/*
 * Decrease number of async events pending for an item, asserts if no events pending
 */
guint rspamd_symcache_item_async_dec_full(struct rspamd_task *task,
										  struct rspamd_symcache_dynamic_item *item,
										  const gchar *subsystem,
										  const gchar *loc);

#define rspamd_symcache_item_async_dec(task, item, subsystem) \
	rspamd_symcache_item_async_dec_full(task, item, subsystem, G_STRLOC)

/**
 * Decrease number of async events pending for an item, asserts if no events pending
 * If no events are left, this function calls `rspamd_symbols_cache_finalize_item` and returns TRUE
 * @param task
 * @param item
 * @return
 */
gboolean rspamd_symcache_item_async_dec_check_full(struct rspamd_task *task,
												   struct rspamd_symcache_dynamic_item *item,
												   const gchar *subsystem,
												   const gchar *loc);

#define rspamd_symcache_item_async_dec_check(task, item, subsystem) \
	rspamd_symcache_item_async_dec_check_full(task, item, subsystem, G_STRLOC)

/**
 * Disables execution of all symbols, excluding those specified in `skip_mask`
 * @param task
 * @param cache
 * @param skip_mask
 */
void rspamd_symcache_disable_all_symbols(struct rspamd_task *task,
										 struct rspamd_symcache *cache,
										 guint skip_mask);

/**
 * Iterates over the list of the enabled composites calling specified function
 * @param task
 * @param cache
 * @param func
 * @param fd
 */
void rspamd_symcache_composites_foreach(struct rspamd_task *task,
										struct rspamd_symcache *cache,
										GHFunc func,
										gpointer fd);

/**
 * Sets allowed settings ids for a symbol
 * @param cache
 * @param symbol
 * @param ids
 * @param nids
 */
bool rspamd_symcache_set_allowed_settings_ids(struct rspamd_symcache *cache,
											  const gchar *symbol,
											  const guint32 *ids,
											  guint nids);
/**
 * Sets denied settings ids for a symbol
 * @param cache
 * @param symbol
 * @param ids
 * @param nids
 */
bool rspamd_symcache_set_forbidden_settings_ids(struct rspamd_symcache *cache,
												const gchar *symbol,
												const guint32 *ids,
												guint nids);

/**
 * Returns allowed ids for a symbol as a constant array
 * @param cache
 * @param symbol
 * @param nids
 * @return
 */
const guint32 *rspamd_symcache_get_allowed_settings_ids(struct rspamd_symcache *cache,
														const gchar *symbol,
														guint *nids);

/**
 * Returns denied ids for a symbol as a constant array
 * @param cache
 * @param symbol
 * @param nids
 * @return
 */
const guint32 *rspamd_symcache_get_forbidden_settings_ids(struct rspamd_symcache *cache,
														  const gchar *symbol,
														  guint *nids);


/**
 * Processes settings_elt in cache and converts it to a set of
 * adjustments for forbidden/allowed settings_ids for each symbol
 * @param cache
 * @param elt
 */
void rspamd_symcache_process_settings_elt(struct rspamd_symcache *cache,
										  struct rspamd_config_settings_elt *elt);

/**
 * Check if a symbol is allowed for execution/insertion, this does not involve
 * condition scripts to be checked (so it is intended to be fast).
 * @param task
 * @param item
 * @param exec_only
 * @return
 */
gboolean rspamd_symcache_is_item_allowed(struct rspamd_task *task,
										 struct rspamd_symcache_item *item,
										 gboolean exec_only);

/**
 * Returns symcache item flags
 * @param item
 * @return
 */
gint rspamd_symcache_dyn_item_flags(struct rspamd_task *task,
									struct rspamd_symcache_dynamic_item *dyn_item);
gint rspamd_symcache_item_flags(struct rspamd_symcache_item *item);

/**
 * Returns cache item name
 * @param item
 * @return
 */
const gchar *rspamd_symcache_dyn_item_name(struct rspamd_task *task,
										   struct rspamd_symcache_dynamic_item *dyn_item);
const gchar *rspamd_symcache_item_name(struct rspamd_symcache_item *item);

/**
 * Returns the current item stat
 * @param item
 * @return
 */
const struct rspamd_symcache_item_stat *
rspamd_symcache_item_stat(struct rspamd_symcache_item *item);

/**
 * Enable profiling for task (e.g. when a slow rule has been found)
 * @param task
 */
void rspamd_symcache_enable_profile(struct rspamd_task *task);

struct rspamd_symcache_timeout_item {
	double timeout;
	const struct rspamd_symcache_item *item;
};

struct rspamd_symcache_timeout_result {
	double max_timeout;
	struct rspamd_symcache_timeout_item *items;
	size_t nitems;
};
/**
 * Gets maximum timeout announced by symbols cache
 * @param cache
 * @return new symcache timeout_result structure, that should be freed by call
 * `rspamd_symcache_timeout_result_free`
 */
struct rspamd_symcache_timeout_result *rspamd_symcache_get_max_timeout(struct rspamd_symcache *cache);

/**
 * Frees results obtained from the previous function
 * @param res
 */
void rspamd_symcache_timeout_result_free(struct rspamd_symcache_timeout_result *res);

/**
 * Destroy internal state of the symcache runtime
 * @param task
 */
void rspamd_symcache_runtime_destroy(struct rspamd_task *task);
#ifdef __cplusplus
}
#endif

#endif