From: Vsevolod Stakhov Date: Fri, 10 Oct 2025 12:37:32 +0000 (+0100) Subject: [Feature] Improve body rewriting support in rspamc and proxy X-Git-Tag: 3.14.0~82 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b13da2ca5e11e6fff32e31cce68aba4ffdc190f5;p=thirdparty%2Frspamd.git [Feature] Improve body rewriting support in rspamc and proxy - Add --output-body option to rspamc for saving rewritten message body to file instead of printing to stdout - Enable body_block protocol flag in proxy for non-milter mode to ensure message body is always available for rewriting operations - This ensures consistent body rewriting capability across all protocol modes (rspamc, milter, and proxy) --- diff --git a/src/client/rspamc.cxx b/src/client/rspamc.cxx index c42d301429..7d20ac759a 100644 --- a/src/client/rspamc.cxx +++ b/src/client/rspamc.cxx @@ -94,6 +94,7 @@ static const char *user_agent = "rspamc"; static const char *files_list = nullptr; static const char *queue_id = nullptr; static const char *log_tag = nullptr; +static const char *output_body = nullptr; static std::string settings; static std::string neural_train; static std::string neural_rule; @@ -200,6 +201,8 @@ static GOptionEntry entries[] = "Set Queue-ID header for the request", nullptr}, {"log-tag", '\0', 0, G_OPTION_ARG_STRING, &log_tag, "Set Log-Tag header for the request", nullptr}, + {"output-body", '\0', 0, G_OPTION_ARG_FILENAME, &output_body, + "Save rewritten message body to file", nullptr}, {"settings", '\0', 0, G_OPTION_ARG_CALLBACK, (void *) &rspamc_settings_callback, "Set Settings header as JSON/UCL for the request", nullptr}, {nullptr, 0, 0, G_OPTION_ARG_NONE, nullptr, nullptr, nullptr}}; @@ -2102,8 +2105,29 @@ rspamc_client_cb(struct rspamd_client_connection *conn, } if (body) { - rspamc_print(out, "\nNew body:\n{}\n", - std::string_view{body, bodylen}); + if (output_body) { + /* Save body to file */ + FILE *body_file = fopen(output_body, "wb"); + if (body_file) { + if (fwrite(body, 1, bodylen, body_file) != bodylen) { + rspamc_print(stderr, "Failed to write body to file {}: {}\n", + output_body, strerror(errno)); + } + else { + rspamc_print(out, "\nNew body saved to: {}\n", output_body); + } + fclose(body_file); + } + else { + rspamc_print(stderr, "Failed to open output file {}: {}\n", + output_body, strerror(errno)); + } + } + else { + /* Print to stdout */ + rspamc_print(out, "\nNew body:\n{}\n", + std::string_view{body, bodylen}); + } } ucl_object_unref(result); diff --git a/src/rspamd_proxy.c b/src/rspamd_proxy.c index 82a43d3d27..5321c86772 100644 --- a/src/rspamd_proxy.c +++ b/src/rspamd_proxy.c @@ -2470,6 +2470,10 @@ rspamd_proxy_self_scan(struct rspamd_proxy_session *session) rspamd_task_set_rcpt_esmtp_args(task, session->rcpt_esmtp_args); } } + else { + /* Always request body block for rewriting support */ + task->protocol_flags |= RSPAMD_TASK_PROTOCOL_FLAG_BODY_BLOCK; + } task->sock = -1;