]> source.dussan.org Git - rspamd.git/commitdiff
[Project] Ressurect empty prefilters as connection filters
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 19 Oct 2020 14:14:58 +0000 (15:14 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 19 Oct 2020 14:14:58 +0000 (15:14 +0100)
src/libserver/rspamd_symcache.c
src/libserver/rspamd_symcache.h
src/libserver/task.c
src/libserver/task.h

index 3236a5e5e7c3682116c5e3379daf1b398aeabd3e..831b98a3db008bd086437491ef29153c36f61f1f 100644 (file)
@@ -151,8 +151,9 @@ struct rspamd_symcache {
        GHashTable *items_by_symbol;
        GPtrArray *items_by_id;
        struct symcache_order *items_by_order;
-       GPtrArray *filters;
+       GPtrArray *connfilters;
        GPtrArray *prefilters;
+       GPtrArray *filters;
        GPtrArray *postfilters;
        GPtrArray *composites;
        GPtrArray *idempotent;
@@ -744,6 +745,7 @@ rspamd_symcache_post_init (struct rspamd_symcache *cache)
                }
        }
 
+       g_ptr_array_sort_with_data (cache->connfilters, prefilters_cmp, cache);
        g_ptr_array_sort_with_data (cache->prefilters, prefilters_cmp, cache);
        g_ptr_array_sort_with_data (cache->postfilters, postfilters_cmp, cache);
        g_ptr_array_sort_with_data (cache->idempotent, postfilters_cmp, cache);
@@ -1116,6 +1118,11 @@ rspamd_symcache_add_symbol (struct rspamd_symcache *cache,
                        g_ptr_array_add (cache->postfilters, item);
                        item->container = cache->postfilters;
                }
+               else if (item->type & SYMBOL_TYPE_CONNFILTER) {
+                       type_str = "connfilter";
+                       g_ptr_array_add (cache->connfilters, item);
+                       item->container = cache->connfilters;
+               }
                else {
                        item->is_filter = TRUE;
                        g_ptr_array_add (cache->filters, item);
@@ -1301,8 +1308,9 @@ rspamd_symcache_destroy (struct rspamd_symcache *cache)
                g_hash_table_destroy (cache->items_by_symbol);
                g_ptr_array_free (cache->items_by_id, TRUE);
                rspamd_mempool_delete (cache->static_pool);
-               g_ptr_array_free (cache->filters, TRUE);
+               g_ptr_array_free (cache->connfilters, TRUE);
                g_ptr_array_free (cache->prefilters, TRUE);
+               g_ptr_array_free (cache->filters, TRUE);
                g_ptr_array_free (cache->postfilters, TRUE);
                g_ptr_array_free (cache->idempotent, TRUE);
                g_ptr_array_free (cache->composites, TRUE);
@@ -1328,8 +1336,9 @@ rspamd_symcache_new (struct rspamd_config *cfg)
        cache->items_by_symbol = g_hash_table_new (rspamd_str_hash,
                        rspamd_str_equal);
        cache->items_by_id = g_ptr_array_new ();
-       cache->filters = g_ptr_array_new ();
+       cache->connfilters = g_ptr_array_new ();
        cache->prefilters = g_ptr_array_new ();
+       cache->filters = g_ptr_array_new ();
        cache->postfilters = g_ptr_array_new ();
        cache->idempotent = g_ptr_array_new ();
        cache->composites = g_ptr_array_new ();
@@ -2069,6 +2078,50 @@ rspamd_symcache_process_symbols (struct rspamd_task *task,
        start_events_pending = rspamd_session_events_pending (task->s);
 
        switch (stage) {
+       case RSPAMD_TASK_STAGE_CONNFILTERS:
+               /* Check for connection filters */
+               saved_priority = G_MININT;
+               all_done = TRUE;
+
+               for (i = 0; i < (gint) cache->connfilters->len; i++) {
+                       item = g_ptr_array_index (cache->connfilters, i);
+                       dyn_item = rspamd_symcache_get_dynamic (checkpoint, item);
+
+                       if (RSPAMD_TASK_IS_SKIPPED (task)) {
+                               return TRUE;
+                       }
+
+                       if (!CHECK_START_BIT (checkpoint, dyn_item) &&
+                               !CHECK_FINISH_BIT (checkpoint, dyn_item)) {
+
+                               if (checkpoint->has_slow) {
+                                       /* Delay */
+                                       checkpoint->has_slow = FALSE;
+
+                                       return FALSE;
+                               }
+                               /* Check priorities */
+                               if (saved_priority == G_MININT) {
+                                       saved_priority = item->priority;
+                               }
+                               else {
+                                       if (item->priority < saved_priority &&
+                                               rspamd_session_events_pending (task->s) > start_events_pending) {
+                                               /*
+                                                * Delay further checks as we have higher
+                                                * priority filters to be processed
+                                                */
+                                               return FALSE;
+                                       }
+                               }
+
+                               rspamd_symcache_check_symbol (task, cache, item,
+                                               checkpoint);
+                               all_done = FALSE;
+                       }
+               }
+               break;
+
        case RSPAMD_TASK_STAGE_PRE_FILTERS:
                /* Check for prefilters */
                saved_priority = G_MININT;
index c220b1cccff7b26d4a3328478ec289c15d1ce0e6..67c753e393ecb90c5cd7cf45e0df863f4301904e 100644 (file)
@@ -48,10 +48,11 @@ enum rspamd_symbol_type {
        SYMBOL_TYPE_CLASSIFIER = (1u << 6u),
        SYMBOL_TYPE_FINE = (1u << 7u),
        SYMBOL_TYPE_EMPTY = (1u << 8u), /* Allow execution on empty tasks */
-       SYMBOL_TYPE_PREFILTER = (1u << 9u),
-       SYMBOL_TYPE_POSTFILTER = (1u << 10u),
-       SYMBOL_TYPE_NOSTAT = (1u << 11u), /* Skip as statistical symbol */
-       SYMBOL_TYPE_IDEMPOTENT = (1u << 12u), /* Symbol cannot change metric */
+       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 */
index 6b93ac8101969568ce31db2101eab96370f158fc..e7a83a60389afef4fc7512efbb8b9e77c38cc26f 100644 (file)
@@ -727,6 +727,7 @@ rspamd_task_process (struct rspamd_task *task, guint stages)
                }
                break;
 
+       case RSPAMD_TASK_STAGE_CONNFILTERS:
        case RSPAMD_TASK_STAGE_PRE_FILTERS:
        case RSPAMD_TASK_STAGE_FILTERS:
                all_done = rspamd_symcache_process_symbols (task, task->cfg->cache, st);
@@ -1806,8 +1807,8 @@ rspamd_task_stage_name (enum rspamd_task_stage stg)
        case RSPAMD_TASK_STAGE_CONNECT:
                ret = "connect";
                break;
-       case RSPAMD_TASK_STAGE_ENVELOPE:
-               ret = "envelope";
+       case RSPAMD_TASK_STAGE_CONNFILTERS:
+               ret = "connection_filter";
                break;
        case RSPAMD_TASK_STAGE_READ_MESSAGE:
                ret = "read_message";
index 0e23ea55dea615e47f591d8909df7f99019e2421..ca49ed1e09f7c27e595000afb578dccd9c2e6c51 100644 (file)
@@ -40,7 +40,7 @@ enum rspamd_command {
 
 enum rspamd_task_stage {
        RSPAMD_TASK_STAGE_CONNECT = (1u << 0u),
-       RSPAMD_TASK_STAGE_ENVELOPE = (1u << 1u),
+       RSPAMD_TASK_STAGE_CONNFILTERS = (1u << 1u),
        RSPAMD_TASK_STAGE_READ_MESSAGE = (1u << 2u),
        RSPAMD_TASK_STAGE_PROCESS_MESSAGE = (1u << 3u),
        RSPAMD_TASK_STAGE_PRE_FILTERS = (1u << 4u),
@@ -60,7 +60,7 @@ enum rspamd_task_stage {
 };
 
 #define RSPAMD_TASK_PROCESS_ALL (RSPAMD_TASK_STAGE_CONNECT | \
-        RSPAMD_TASK_STAGE_ENVELOPE | \
+        RSPAMD_TASK_STAGE_CONNFILTERS | \
         RSPAMD_TASK_STAGE_READ_MESSAGE | \
         RSPAMD_TASK_STAGE_PRE_FILTERS | \
         RSPAMD_TASK_STAGE_PROCESS_MESSAGE | \
@@ -77,7 +77,6 @@ enum rspamd_task_stage {
         RSPAMD_TASK_STAGE_IDEMPOTENT | \
         RSPAMD_TASK_STAGE_DONE)
 #define RSPAMD_TASK_PROCESS_LEARN (RSPAMD_TASK_STAGE_CONNECT | \
-        RSPAMD_TASK_STAGE_ENVELOPE | \
         RSPAMD_TASK_STAGE_READ_MESSAGE | \
         RSPAMD_TASK_STAGE_PROCESS_MESSAGE | \
         RSPAMD_TASK_STAGE_CLASSIFIERS_PRE | \