]> source.dussan.org Git - rspamd.git/commitdiff
Add add_header action to smtp proxy.
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Tue, 26 Oct 2010 17:38:03 +0000 (21:38 +0400)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Tue, 26 Oct 2010 17:38:03 +0000 (21:38 +0400)
In raw mode scan stripped html parts when regexp are not 'raw'.

src/cfg_file.h
src/expressions.c
src/filter.c
src/plugins/regexp.c
src/smtp.c
src/smtp_proto.c
src/smtp_utils.c
src/smtp_utils.h

index ddc78b15065338e85cebb428074227a4a7335c06..9045d3a7a8b29c83c75d3c27b66c2ecdef846258 100644 (file)
@@ -83,6 +83,7 @@ struct rspamd_regexp {
        GRegex *raw_regexp;                                                             /**< glib regexp structure for raw matching                             */
        gchar *header;                                                                  /**< header name for header regexps                                             */
        gboolean is_test;                                                               /**< true if this expression must be tested                             */
+       gboolean is_raw;                                                                /**< true if this regexp is done by raw matching                */
 };
 
 /**
index c4fa738b52f5ac81b81211b72e9e0eda88df3909..1f83c282716cb22014ae466688245dff308d78c3 100644 (file)
@@ -642,6 +642,7 @@ parse_regexp (memory_pool_t * pool, gchar *line, gboolean raw_mode)
                        break;
                case 'r':
                        regexp_flags |= G_REGEX_RAW;
+                       result->is_raw = TRUE;
                        p++;
                        break;
                        /* Type flags */
index 9cc6e542f834be93ef23aa3b8324026b3f67cd01..73f45211be9b23544ecce6c0c9a01501bb189bfb 100644 (file)
@@ -628,7 +628,6 @@ check_action_str (const gchar *data, gint *result)
                *result = METRIC_ACTION_REWRITE_SUBJECT;
        }
        else {
-               msg_err ("unknown action for metric: %s", data);
                return FALSE;
        }
        return TRUE;
index c70b5d2329063527d155e1e3ebabd07c462bf37f..8a33402f2c51698a05f0d18a7b65395cb5110232 100644 (file)
@@ -613,6 +613,8 @@ process_regexp (struct rspamd_regexp *re, struct worker_task *task, const gchar
                .re = re,
                .found = FALSE
        };
+       guint8                         *ct;
+       gsize                           clen;
        gint                            r;
 
 
@@ -710,8 +712,15 @@ process_regexp (struct rspamd_regexp *re, struct worker_task *task, const gchar
                        else {
                                regexp = re->regexp;
                        }
-
-                       if (g_regex_match_full (regexp, part->orig->data, part->orig->len, 0, 0, NULL, &err) == TRUE) {
+                       if (re->is_raw) {
+                               ct = part->orig->data;
+                               clen = part->orig->len;
+                       }
+                       else {
+                               ct = part->content->data;
+                               clen = part->content->len;
+                       }
+                       if (g_regex_match_full (regexp, ct, clen, 0, 0, NULL, &err) == TRUE) {
                                if (G_UNLIKELY (re->is_test)) {
                                        msg_info ("process test regexp %s for mime part returned TRUE", re->regexp_text);
                                }
index 1bda83423b6aee4b576ba03a89d5a0369169fb9e..6cecb52e65c4f25e0cf6096f33fed4caeb018eb7 100644 (file)
@@ -342,6 +342,7 @@ process_smtp_data (struct smtp_session *session)
                }
        }
        else {
+               msg_info ("not scan message as it is %z bytes and maximum is %z", st.st_size, session->ctx->max_size);
                session->task = NULL;
                return smtp_send_upstream_message (session);
        }
index ee9084f284af666aeca94a886577e1ef0bfe50e6..6df3c18c6405f826e5c008f6ba5f07d6f54bafe6 100644 (file)
@@ -582,17 +582,7 @@ smtp_upstream_read_socket (f_str_t * in, void *arg)
                                return FALSE;
                        }
                        else if (r == 1) {
-                               r = strlen (session->cfg->temp_dir) + sizeof ("/rspamd-XXXXXX");
-                               session->temp_name = memory_pool_alloc (session->pool, r);
-                               rspamd_snprintf (session->temp_name, r, "%s%crspamd-XXXXXX", session->cfg->temp_dir, G_DIR_SEPARATOR);
-#ifdef HAVE_MKSTEMP
-                               /* Umask is set before */
-                               session->temp_fd = mkstemp (session->temp_name);
-#else
-                               session->temp_fd = g_mkstemp_full (session->temp_name, O_RDWR, S_IWUSR | S_IRUSR);
-#endif
-                               if (session->temp_fd == -1) {
-                                       msg_err ("mkstemp error: %s", strerror (errno));
+                               if (! make_smtp_tempfile (session)) {
                                        session->error = SMTP_ERROR_FILE;
                                        session->state = SMTP_STATE_CRITICAL_ERROR;
                                        rspamd_dispatcher_restore (session->dispatcher);
index 1a30112eea129253aa3eee9c573d92e6b982f94f..c15bc063ef11588ee713324239e38b210142d6d0 100644 (file)
@@ -185,11 +185,36 @@ smtp_metric_callback (gpointer key, gpointer value, gpointer ud)
 #endif
 }
 
+gboolean
+make_smtp_tempfile (struct smtp_session *session)
+{
+       gsize                            r;
+
+       r = strlen (session->cfg->temp_dir) + sizeof ("/rspamd-XXXXXX");
+       session->temp_name = memory_pool_alloc (session->pool, r);
+       rspamd_snprintf (session->temp_name, r, "%s%crspamd-XXXXXX", session->cfg->temp_dir, G_DIR_SEPARATOR);
+#ifdef HAVE_MKSTEMP
+       /* Umask is set before */
+       session->temp_fd = mkstemp (session->temp_name);
+#else
+       session->temp_fd = g_mkstemp_full (session->temp_name, O_RDWR, S_IWUSR | S_IRUSR);
+#endif
+       if (session->temp_fd == -1) {
+               msg_err ("mkstemp error: %s", strerror (errno));
+
+               return FALSE;
+       }
+
+       return TRUE;
+}
+
 gboolean
 write_smtp_reply (struct smtp_session *session)
 {
        gchar                           logbuf[1024];
        struct smtp_metric_callback_data cd;
+       GMimeStream                    *stream;
+       int                             old_fd;
 
        /* Check metrics */
        cd.session = session;
@@ -218,6 +243,53 @@ write_smtp_reply (struct smtp_session *session)
                destroy_session (session->s);
                return FALSE;
        }
+       else if (cd.action <= METRIC_ACTION_ADD_HEADER || cd.action <= METRIC_ACTION_REWRITE_SUBJECT) {
+               old_fd = session->temp_fd;
+               if (! make_smtp_tempfile (session)) {
+                       session->error = SMTP_ERROR_FILE;
+                       session->state = SMTP_STATE_CRITICAL_ERROR;
+                       rspamd_dispatcher_restore (session->dispatcher);
+                       if (! rspamd_dispatcher_write (session->dispatcher, session->error, 0, FALSE, TRUE)) {
+                               goto err;
+                       }
+                       destroy_session (session->s);
+                       return FALSE;
+               }
+               if (cd.action <= METRIC_ACTION_ADD_HEADER) {
+#ifndef GMIME24
+                       g_mime_message_add_header (session->task->message, "X-Spam", "true");
+#else
+                       g_mime_object_append_header (GMIME_OBJECT (session->task->message), "X-Spam", "true");
+#endif
+               }
+               else if (cd.action <= METRIC_ACTION_REWRITE_SUBJECT) {
+                       /* XXX: add this action */
+               }
+               stream = g_mime_stream_fs_new (session->temp_fd);
+               g_mime_stream_fs_set_owner (GMIME_STREAM_FS (stream), FALSE);
+               close (old_fd);
+
+               if (g_mime_object_write_to_stream (GMIME_OBJECT (session->task->message), stream) == -1) {
+                       msg_err ("cannot write MIME object to stream: %s", strerror (errno));
+                       session->error = SMTP_ERROR_FILE;
+                       session->state = SMTP_STATE_CRITICAL_ERROR;
+                       rspamd_dispatcher_restore (session->dispatcher);
+                       if (! rspamd_dispatcher_write (session->dispatcher, session->error, 0, FALSE, TRUE)) {
+                               goto err;
+                       }
+                       destroy_session (session->s);
+                       return FALSE;
+               }
+               g_object_unref (stream);
+       }
        /* XXX: Add other actions */
        return smtp_send_upstream_message (session);
+err:
+       session->error = SMTP_ERROR_FILE;
+       session->state = SMTP_STATE_CRITICAL_ERROR;
+       if (! rspamd_dispatcher_write (session->dispatcher, session->error, 0, FALSE, TRUE)) {
+               return FALSE;
+       }
+       destroy_session (session->s);
+       return FALSE;
 }
index 0374a715e0919db5e21a66adf37d8323dabecccc..e61a85ae21875083b0491a18f2ec578b4bd25597 100644 (file)
@@ -23,6 +23,10 @@ gboolean smtp_send_upstream_message (struct smtp_session *session);
  */
 gboolean create_smtp_upstream_connection (struct smtp_session *session);
 
+/**
+ * Create temporary file for smtp session
+ */
+gboolean make_smtp_tempfile (struct smtp_session *session);
 
 /**
  * Write reply to upstream