From: Vsevolod Stakhov Date: Fri, 30 Jan 2015 15:19:04 +0000 (+0000) Subject: Rework message store to allow encryption. X-Git-Tag: 0.9.0~798 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=39c853952b97d56726839e6e5681cd7e1849c364;p=rspamd.git Rework message store to allow encryption. --- diff --git a/src/controller.c b/src/controller.c index 715f71dcc..16651ea0b 100644 --- a/src/controller.c +++ b/src/controller.c @@ -798,7 +798,6 @@ rspamd_controller_handle_learn_common ( } task = rspamd_task_new (session->ctx->worker); - task->msg = msg->body; task->resolver = ctx->resolver; task->ev_base = ctx->ev_base; @@ -815,7 +814,8 @@ rspamd_controller_handle_learn_common ( task->sock = conn_ent->conn->fd; - if (!rspamd_task_process (task, msg, NULL, FALSE)) { + /* XXX: Handle encrypted messages */ + if (!rspamd_task_process (task, msg, msg->body->str, msg->body->len, NULL, FALSE)) { msg_warn ("filters cannot be processed for %s", task->message_id); rspamd_controller_send_error (conn_ent, 500, task->last_error); destroy_session (task->s); @@ -890,7 +890,6 @@ rspamd_controller_handle_scan (struct rspamd_http_connection_entry *conn_ent, task = rspamd_task_new (session->ctx->worker); task->ev_base = session->ctx->ev_base; - task->msg = msg->body; task->resolver = ctx->resolver; task->ev_base = ctx->ev_base; @@ -905,7 +904,8 @@ rspamd_controller_handle_scan (struct rspamd_http_connection_entry *conn_ent, task->http_conn = rspamd_http_connection_ref (conn_ent->conn); task->sock = conn_ent->conn->fd; - if (!rspamd_task_process (task, msg, NULL, FALSE)) { + /* XXX: handle encrypted messages */ + if (!rspamd_task_process (task, msg, msg->body->str, msg->body->len, NULL, FALSE)) { msg_warn ("filters cannot be processed for %s", task->message_id); rspamd_controller_send_error (conn_ent, 500, task->last_error); destroy_session (task->s); diff --git a/src/libmime/message.c b/src/libmime/message.c index 94137af15..869d0a06e 100644 --- a/src/libmime/message.c +++ b/src/libmime/message.c @@ -1465,8 +1465,8 @@ process_message (struct rspamd_task *task) gint rc; tmp = rspamd_mempool_alloc (task->task_pool, sizeof (GByteArray)); - tmp->data = task->msg->str; - tmp->len = task->msg->len; + tmp->data = (guint8 *)task->msg.start; + tmp->len = task->msg.len; stream = g_mime_stream_mem_new_with_byte_array (tmp); /* @@ -1478,7 +1478,7 @@ process_message (struct rspamd_task *task) if (task->is_mime) { debug_task ("construct mime parser from string length %d", - (gint)task->msg->len); + (gint)task->msg.len); /* create a new parser object to parse the stream */ parser = g_mime_parser_new_with_stream (stream); g_object_unref (stream); diff --git a/src/libmime/smtp_utils.c b/src/libmime/smtp_utils.c index b29a9d717..a5a2f26ef 100644 --- a/src/libmime/smtp_utils.c +++ b/src/libmime/smtp_utils.c @@ -35,8 +35,8 @@ free_smtp_session (gpointer arg) if (session) { if (session->task) { rspamd_task_free (session->task, FALSE); - if (session->task->msg->str) { - munmap (session->task->msg->str, session->task->msg->len); + if (session->task->msg.start) { + munmap (session->task->msg.start, session->task->msg.len); } } if (session->rcpt) { @@ -212,14 +212,14 @@ smtp_metric_callback (gpointer key, gpointer value, gpointer ud) cd->log_offset += rspamd_snprintf (cd->log_buf + cd->log_offset, cd->log_size - cd->log_offset, "]), len: %z, time: %s,", - task->msg->len, + task->msg.len, calculate_check_time (&task->tv, &task->ts, task->cfg->clock_res, &task->scan_milliseconds)); #else cd->log_offset += rspamd_snprintf (cd->log_buf + cd->log_offset, cd->log_size - cd->log_offset, "]), len: %z, time: %s,", - task->msg->len, + task->msg.len, calculate_check_time (&task->tv, task->cfg->clock_res, &task->scan_milliseconds)); #endif diff --git a/src/libserver/dkim.c b/src/libserver/dkim.c index dc0b73166..e178cfea4 100644 --- a/src/libserver/dkim.c +++ b/src/libserver/dkim.c @@ -1591,12 +1591,12 @@ rspamd_dkim_check (rspamd_dkim_context_t *ctx, g_return_val_if_fail (ctx != NULL, DKIM_ERROR); g_return_val_if_fail (key != NULL, DKIM_ERROR); - g_return_val_if_fail (task->msg != NULL, DKIM_ERROR); + g_return_val_if_fail (task->msg.len > 0, DKIM_ERROR); /* First of all find place of body */ - p = task->msg->str; + p = task->msg.start; - end = task->msg->str + task->msg->len; + end = task->msg.start + task->msg.len; while (p <= end) { /* Search for \r\n\r\n at the end of headers */ diff --git a/src/libserver/protocol.c b/src/libserver/protocol.c index 9632418c4..25dcee1c0 100644 --- a/src/libserver/protocol.c +++ b/src/libserver/protocol.c @@ -735,7 +735,7 @@ rspamd_metric_result_ucl (struct rspamd_task *task, #ifdef HAVE_CLOCK_GETTIME rspamd_printf_gstring (logbuf, "]), len: %z, time: %s, dns req: %d,", - task->msg->len, calculate_check_time (&task->tv, &task->ts, + task->msg.len, calculate_check_time (&task->tv, &task->ts, task->cfg->clock_res, &task->scan_milliseconds), task->dns_requests); #else rspamd_printf_gstring (logbuf, "]), len: %z, time: %s, dns req: %d,", diff --git a/src/libserver/roll_history.c b/src/libserver/roll_history.c index 4e6ee01a8..5edf5bea0 100644 --- a/src/libserver/roll_history.c +++ b/src/libserver/roll_history.c @@ -146,7 +146,7 @@ rspamd_roll_history_update (struct roll_history *history, } row->scan_time = task->scan_milliseconds; - row->len = (task->msg == NULL ? 0 : task->msg->len); + row->len = task->msg.len; row->completed = TRUE; } diff --git a/src/libserver/task.c b/src/libserver/task.c index b8d042dc6..699e129ab 100644 --- a/src/libserver/task.c +++ b/src/libserver/task.c @@ -293,29 +293,16 @@ rspamd_task_free_soft (gpointer ud) gboolean rspamd_task_process (struct rspamd_task *task, - struct rspamd_http_message *msg, GThreadPool *classify_pool, + struct rspamd_http_message *msg, const gchar *start, gsize len, + GThreadPool *classify_pool, gboolean process_extra_filters) { gint r; GError *err = NULL; - if (msg->body->len == 0) { - msg_err ("got zero length body"); - task->last_error = "message's body is empty"; - return FALSE; - } - - /* XXX: awful hack */ - if (msg->peer_key != NULL) { - task->msg = rspamd_mempool_alloc (task->task_pool, sizeof (GString)); - task->msg->len = msg->body->len - 16; - task->msg->allocated_len = 0; - task->msg->str = msg->body->str + 16; - } - else { - task->msg = msg->body; - } - debug_task ("got string of length %z", task->msg->len); + task->msg.start = start; + task->msg.len = len; + debug_task ("got string of length %z", task->msg.len); /* We got body, set wanna_die flag */ task->s->wanna_die = TRUE; diff --git a/src/libserver/task.h b/src/libserver/task.h index 9702befa5..43349ffa9 100644 --- a/src/libserver/task.h +++ b/src/libserver/task.h @@ -93,7 +93,10 @@ struct rspamd_task { gchar *hostname; /**< hostname reported by MTA */ GHashTable *request_headers; /**< HTTP headers in a request */ GHashTable *reply_headers; /**< Custom reply headers */ - GString *msg; /**< message buffer */ + struct { + const gchar *start; + gsize len; + } msg; /**< message buffer */ struct rspamd_http_connection *http_conn; /**< HTTP server connection */ struct rspamd_async_session * s; /**< async session object */ gint parts_count; /**< mime parts count */ @@ -181,7 +184,8 @@ gboolean rspamd_task_fin (void *arg); * @return task has been successfully parsed and processed */ gboolean rspamd_task_process (struct rspamd_task *task, - struct rspamd_http_message *msg, GThreadPool *classify_pool, + struct rspamd_http_message *msg, const gchar *start, gsize len, + GThreadPool *classify_pool, gboolean process_extra_filters); /** diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index 98aba7f15..ef52b4544 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -716,7 +716,9 @@ lua_task_create_from_buffer (lua_State *L) ptask = lua_newuserdata (L, sizeof (gpointer)); rspamd_lua_setclass (L, "rspamd{task}", -1); *ptask = task; - task->msg = g_string_new_len (data, len); + task->msg.start = rspamd_mempool_alloc (task->task_pool, len + 1); + memcpy ((gpointer)task->msg.start, data, len); + task->msg.len = len; } return 1; } @@ -726,7 +728,7 @@ lua_task_process_message (lua_State *L) { struct rspamd_task *task = lua_check_task (L); - if (task != NULL && task->msg != NULL && task->msg->len > 0) { + if (task != NULL && task->msg.len > 0) { if (process_message (task) == 0) { lua_pushboolean (L, TRUE); } diff --git a/src/plugins/fuzzy_check.c b/src/plugins/fuzzy_check.c index 573ab0868..2dd05a109 100644 --- a/src/plugins/fuzzy_check.c +++ b/src/plugins/fuzzy_check.c @@ -1260,7 +1260,9 @@ fuzzy_process_handler (struct rspamd_http_connection_entry *conn_ent, task->cfg = ctx->cfg; /* Allocate message from string */ - task->msg = msg->body; + /* XXX: what about encrypted messsages ? */ + task->msg.start = msg->body->str; + task->msg.len = msg->body->len; saved = rspamd_mempool_alloc0 (task->task_pool, sizeof (gint)); err = rspamd_mempool_alloc0 (task->task_pool, sizeof (GError *)); diff --git a/src/plugins/regexp.c b/src/plugins/regexp.c index 39b8af1ec..74ab46ab5 100644 --- a/src/plugins/regexp.c +++ b/src/plugins/regexp.c @@ -666,8 +666,8 @@ process_regexp (struct rspamd_regexp *re, case REGEXP_MESSAGE: debug_task ("checking message regexp: %s", re->regexp_text); regexp = re->raw_regexp; - ct = task->msg->str; - clen = task->msg->len; + ct = (guint8 *)task->msg.start; + clen = task->msg.len; if (regexp_module_ctx->max_size != 0 && clen > regexp_module_ctx->max_size) { diff --git a/src/worker.c b/src/worker.c index 079fa8fae..e55483daf 100644 --- a/src/worker.c +++ b/src/worker.c @@ -133,7 +133,7 @@ rspamd_worker_body_handler (struct rspamd_http_connection *conn, task->peer_key = rspamd_http_connection_key_ref (msg->peer_key); } - if (!rspamd_task_process (task, msg, ctx->classify_pool, TRUE)) { + if (!rspamd_task_process (task, msg, chunk, len, ctx->classify_pool, TRUE)) { task->state = WRITE_REPLY; }