]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Milter: Add support of quarantine and discard actions
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 26 Jan 2019 14:22:37 +0000 (14:22 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 26 Jan 2019 14:22:37 +0000 (14:22 +0000)
src/libserver/milter.c
src/libserver/milter.h
src/libserver/protocol.c
src/rspamd_proxy.c

index 16d6252e97bab059bf4ccaa60bd5cadb4c04b346..b871c23475037f013b4ed5e638941ac4f6373c56 100644 (file)
@@ -25,6 +25,7 @@
 #include "libutil/http.h"
 #include "libutil/http_private.h"
 #include "libserver/protocol_internal.h"
+#include "libserver/cfg_file_private.h"
 #include "libmime/filter.h"
 #include "libserver/worker_util.h"
 #include "utlist.h"
@@ -1557,7 +1558,7 @@ rspamd_milter_remove_header_safe (struct rspamd_milter_session *session,
  */
 static gboolean
 rspamd_milter_process_milter_block (struct rspamd_milter_session *session,
-               const ucl_object_t *obj, gint action)
+               const ucl_object_t *obj, struct rspamd_action *action)
 {
        const ucl_object_t *elt, *cur, *cur_elt;
        ucl_object_iter_t it;
@@ -1731,7 +1732,7 @@ rspamd_milter_process_milter_block (struct rspamd_milter_session *session,
                }
        }
 
-       if (action == METRIC_ACTION_ADD_HEADER) {
+       if (action->action_type == METRIC_ACTION_ADD_HEADER) {
                elt = ucl_object_lookup (obj, "spam_header");
 
                if (elt) {
@@ -1783,7 +1784,7 @@ rspamd_milter_send_task_results (struct rspamd_milter_session *session,
        const ucl_object_t *elt;
        struct rspamd_milter_private *priv = session->priv;
        const gchar *str_action;
-       gint action = METRIC_ACTION_REJECT;
+       struct rspamd_action *action;
        rspamd_fstring_t *xcode = NULL, *rcode = NULL, *reply = NULL;
        GString *hname, *hvalue;
        gboolean processed = FALSE;
@@ -1805,7 +1806,14 @@ rspamd_milter_send_task_results (struct rspamd_milter_session *session,
        }
 
        str_action = ucl_object_tostring (elt);
-       rspamd_action_from_str (str_action, &action);
+       action = rspamd_config_get_action (milter_ctx->cfg, str_action);
+
+       if (action == NULL) {
+               msg_err_milter ("action %s has not been registered", str_action);
+               rspamd_milter_send_action (session, RSPAMD_MILTER_TEMPFAIL);
+
+               goto cleanup;
+       }
 
        elt = ucl_object_lookup (results, "messages");
        if (elt) {
@@ -1860,7 +1868,7 @@ rspamd_milter_send_task_results (struct rspamd_milter_session *session,
                goto cleanup;
        }
 
-       switch (action) {
+       switch (action->action_type) {
        case METRIC_ACTION_REJECT:
                if (priv->discard_on_reject) {
                        rspamd_milter_send_action (session, RSPAMD_MILTER_DISCARD);
@@ -1939,6 +1947,17 @@ rspamd_milter_send_task_results (struct rspamd_milter_session *session,
                rspamd_milter_send_action (session, RSPAMD_MILTER_ACCEPT);
                break;
 
+       case METRIC_ACTION_MILTER_QUARANTINE:
+               /* TODO: be more flexible about SMTP messages */
+               rspamd_milter_send_action (session, RSPAMD_MILTER_QUARANTINE,
+                               RSPAMD_MILTER_QUARANTINE_MESSAGE);
+
+               /* Quarantine also requires accept action, all hail Sendmail */
+               rspamd_milter_send_action (session, RSPAMD_MILTER_ACCEPT);
+               break;
+       case METRIC_ACTION_MILTER_DISCARD:
+               rspamd_milter_send_action (session, RSPAMD_MILTER_DISCARD);
+               break;
        case METRIC_ACTION_GREYLIST:
        case METRIC_ACTION_NOACTION:
        default:
index 35dc8f9087a12ce5b6986acb6745d2ff06568789..7f375b018e675bff411f81ccb84a968f193d71e2 100644 (file)
@@ -43,12 +43,14 @@ enum rspamd_milter_reply {
 struct rspamd_email_address;
 struct event_base;
 struct rspamd_http_message;
+struct rspamd_config;
 
 struct rspamd_milter_context {
        const gchar *spam_header;
        const gchar *client_ca_name;
        const gchar *reject_message;
        void *sessions_cache;
+       struct rspamd_config *cfg;
        gboolean discard_on_reject;
        gboolean quarantine_on_reject;
 };
index 713c544996b4e00c54a009875e0c034f3fa0a4fd..d8c790e8f0bce718521617f520170ee189ce4958 100644 (file)
@@ -968,6 +968,17 @@ rspamd_metric_result_ucl (struct rspamd_task *task,
                                "subject", 0, false);
                }
        }
+       if (action->flags & RSPAMD_ACTION_MILTER) {
+               /* Treat milter action specially */
+               if (action->action_type == METRIC_ACTION_MILTER_DISCARD) {
+                       ucl_object_insert_key (obj, ucl_object_fromstring ("discard"),
+                                       "reject", 0, false);
+               }
+               else if (action->action_type == METRIC_ACTION_MILTER_QUARANTINE) {
+                       ucl_object_insert_key (obj, ucl_object_fromstring ("quarantine"),
+                                       "reject", 0, false);
+               }
+       }
 
        /* Now handle symbols */
        if (task->cmd == CMD_CHECK_V2) {
index aff866bb4ac75928a9628a7d45aea9b27485f7b9..9ea2b0d74f5be9ea378b1c76f4778e8722873382 100644 (file)
@@ -2245,6 +2245,7 @@ start_rspamd_proxy (struct rspamd_worker *worker) {
        ctx->milter_ctx.sessions_cache = ctx->sessions_cache;
        ctx->milter_ctx.client_ca_name = ctx->client_ca_name;
        ctx->milter_ctx.reject_message = ctx->reject_message;
+       ctx->milter_ctx.cfg = ctx->cfg;
        rspamd_milter_init_library (&ctx->milter_ctx);
 
        rspamd_lua_run_postloads (ctx->cfg->lua_state, ctx->cfg, ctx->ev_base,