From 03bcabb80535bb9a3bc29d92c8eb26ed43262a98 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 12 May 2015 15:11:42 +0100 Subject: [PATCH] Fix finding headers in HTTP messages. --- src/controller.c | 36 +++++++++++++++++------------------- src/http_proxy.c | 12 +++++++----- src/libutil/http.c | 6 +++--- src/libutil/http.h | 2 +- src/plugins/fuzzy_check.c | 14 +++++++------- src/plugins/surbl.c | 11 ++++++----- 6 files changed, 41 insertions(+), 40 deletions(-) diff --git a/src/controller.c b/src/controller.c index af5ff2f90..061f3ce02 100644 --- a/src/controller.c +++ b/src/controller.c @@ -231,7 +231,7 @@ rspamd_encrypted_password_get_str (const gchar * password, gsize skip, return str; } -static gboolean rspamd_check_encrypted_password (const gchar * password, +static gboolean rspamd_check_encrypted_password (const GString * password, const gchar * check, const struct rspamd_controller_pbkdf *pbkdf) { const gchar *salt, *hash; @@ -268,7 +268,7 @@ static gboolean rspamd_check_encrypted_password (const gchar * password, } local_key = g_alloca (pbkdf->key_len); - rspamd_cryptobox_pbkdf (password, strlen (password), + rspamd_cryptobox_pbkdf (password->str, password->len, salt_decoded, salt_len, local_key, pbkdf->key_len, pbkdf->rounds); @@ -290,7 +290,8 @@ static gboolean rspamd_controller_check_password( struct rspamd_controller_session *session, struct rspamd_http_message *msg, gboolean is_enable) { - const gchar *password, *check; + const gchar *check; + const GString *password; struct rspamd_controller_worker_ctx *ctx = session->ctx; gboolean check_normal = TRUE, check_enable = TRUE, ret = TRUE; const struct rspamd_controller_pbkdf *pbkdf = NULL; @@ -338,7 +339,7 @@ static gboolean rspamd_controller_check_password( } if (check != NULL) { if (!rspamd_is_encrypted_password (check, &pbkdf)) { - ret = rspamd_constant_memcmp (password, check, 0); + ret = rspamd_constant_memcmp (password->str, check, password->len); } else { ret = rspamd_check_encrypted_password (password, check, @@ -360,7 +361,8 @@ static gboolean rspamd_controller_check_password( if (ctx->password != NULL) { check = ctx->password; if (!rspamd_is_encrypted_password (check, &pbkdf)) { - check_normal = rspamd_constant_memcmp (password, check, 0); + check_normal = rspamd_constant_memcmp (password->str, check, + password->len); } else { check_normal = rspamd_check_encrypted_password (password, @@ -374,7 +376,8 @@ static gboolean rspamd_controller_check_password( if (ctx->enable_password != NULL) { check = ctx->enable_password; if (!rspamd_is_encrypted_password (check, &pbkdf)) { - check_enable = rspamd_constant_memcmp (password, check, 0); + check_enable = rspamd_constant_memcmp (password->str, check, + password->len); } else { check_enable = rspamd_check_encrypted_password (password, @@ -655,7 +658,7 @@ rspamd_controller_handle_get_map (struct rspamd_http_connection_entry *conn_ent, struct rspamd_controller_session *session = conn_ent->ud; GList *cur; struct rspamd_map *map; - const gchar *idstr; + const GString *idstr; gchar *errstr; struct stat st; gint fd; @@ -676,8 +679,8 @@ rspamd_controller_handle_get_map (struct rspamd_http_connection_entry *conn_ent, return 0; } - id = strtoul (idstr, &errstr, 10); - if (*errstr != '\0') { + id = strtoul (idstr->str, &errstr, 10); + if (*errstr != '\0' && *errstr != '\n') { msg_info ("invalid map id"); rspamd_controller_send_error (conn_ent, 400, "400 invalid map id"); return 0; @@ -961,7 +964,6 @@ rspamd_controller_handle_learn_common ( struct rspamd_controller_worker_ctx *ctx; struct rspamd_classifier_config *cl; struct rspamd_task *task; - const gchar *classifier; ctx = session->ctx; @@ -977,12 +979,8 @@ rspamd_controller_handle_learn_common ( return 0; } - if ((classifier = - rspamd_http_message_find_header (msg, "Classifier")) == NULL) { - classifier = "bayes"; - } - - cl = rspamd_config_find_classifier (ctx->cfg, classifier); + /* XXX: now work with only bayes */ + cl = rspamd_config_find_classifier (ctx->cfg, "bayes"); if (cl == NULL) { rspamd_controller_send_error (conn_ent, 400, "Classifier not found"); return 0; @@ -1343,7 +1341,7 @@ rspamd_controller_handle_savemap (struct rspamd_http_connection_entry *conn_ent, GList *cur; struct rspamd_map *map; struct rspamd_controller_worker_ctx *ctx; - const gchar *idstr; + const GString *idstr; gchar *errstr; guint32 id; gboolean found = FALSE; @@ -1371,8 +1369,8 @@ rspamd_controller_handle_savemap (struct rspamd_http_connection_entry *conn_ent, return 0; } - id = strtoul (idstr, &errstr, 10); - if (*errstr != '\0') { + id = strtoul (idstr->str, &errstr, 10); + if (*errstr != '\0' && *errstr != '\r') { msg_info ("invalid map id"); rspamd_controller_send_error (conn_ent, 400, "Map id is invalid"); return 0; diff --git a/src/http_proxy.c b/src/http_proxy.c index 5bae04029..b24057cc8 100644 --- a/src/http_proxy.c +++ b/src/http_proxy.c @@ -294,7 +294,8 @@ proxy_client_finish_handler (struct rspamd_http_connection *conn, { struct http_proxy_session *session = conn->ud; struct rspamd_http_upstream *backend = NULL; - const gchar *host; + const GString *host; + gchar hostbuf[512]; if (!session->replied) { host = rspamd_http_message_find_header (msg, "Host"); @@ -303,7 +304,8 @@ proxy_client_finish_handler (struct rspamd_http_connection *conn, backend = session->ctx->default_upstream; } else { - backend = g_hash_table_lookup (session->ctx->upstreams, host); + rspamd_strlcpy (hostbuf, host->str, sizeof (hostbuf)); + backend = g_hash_table_lookup (session->ctx->upstreams, hostbuf); if (backend == NULL) { backend = session->ctx->default_upstream; @@ -312,14 +314,14 @@ proxy_client_finish_handler (struct rspamd_http_connection *conn, if (backend == NULL) { /* No backend */ - msg_err ("cannot find upstream for %s", host ? host : "default"); + msg_err ("cannot find upstream for %s", host ? hostbuf : "default"); goto err; } else { session->up = rspamd_upstream_get (backend->u, RSPAMD_UPSTREAM_ROUND_ROBIN); if (session->up == NULL) { - msg_err ("cannot select upstream for %s", host ? host : "default"); + msg_err ("cannot select upstream for %s", host ? hostbuf : "default"); goto err; } @@ -327,7 +329,7 @@ proxy_client_finish_handler (struct rspamd_http_connection *conn, rspamd_upstream_addr (session->up), SOCK_STREAM, TRUE); if (session->backend_sock == -1) { - msg_err ("cannot connect upstream for %s", host ? host : "default"); + msg_err ("cannot connect upstream for %s", host ? hostbuf : "default"); rspamd_upstream_fail (session->up); goto err; } diff --git a/src/libutil/http.c b/src/libutil/http.c index 3a996f6e7..ff43ebd11 100644 --- a/src/libutil/http.c +++ b/src/libutil/http.c @@ -1740,12 +1740,12 @@ rspamd_http_message_add_header (struct rspamd_http_message *msg, } } -const gchar * +const GString * rspamd_http_message_find_header (struct rspamd_http_message *msg, const gchar *name) { struct rspamd_http_header *hdr; - const gchar *res = NULL; + const GString *res = NULL; guint slen = strlen (name); if (msg != NULL) { @@ -1753,7 +1753,7 @@ rspamd_http_message_find_header (struct rspamd_http_message *msg, { if (hdr->name->len == slen) { if (g_ascii_strncasecmp (hdr->name->str, name, slen) == 0) { - res = hdr->value->str; + res = hdr->value; break; } } diff --git a/src/libutil/http.h b/src/libutil/http.h index e709e1e09..46c79ebb4 100644 --- a/src/libutil/http.h +++ b/src/libutil/http.h @@ -325,7 +325,7 @@ void rspamd_http_message_add_header (struct rspamd_http_message *msg, * @param msg message * @param name name of header */ -const gchar * rspamd_http_message_find_header (struct rspamd_http_message *msg, +const GString * rspamd_http_message_find_header (struct rspamd_http_message *msg, const gchar *name); /** diff --git a/src/plugins/fuzzy_check.c b/src/plugins/fuzzy_check.c index af0aab420..f7a59522c 100644 --- a/src/plugins/fuzzy_check.c +++ b/src/plugins/fuzzy_check.c @@ -1292,7 +1292,7 @@ static gboolean fuzzy_controller_handler (struct rspamd_http_connection_entry *conn_ent, struct rspamd_http_message *msg, struct module_ctx *ctx, gint cmd) { - const gchar *arg; + const GString *arg; gchar *err_str; gint value = 1, flag = 0; @@ -1300,18 +1300,18 @@ fuzzy_controller_handler (struct rspamd_http_connection_entry *conn_ent, arg = rspamd_http_message_find_header (msg, "Weight"); if (arg) { errno = 0; - value = strtol (arg, &err_str, 10); - if (errno != 0 || *err_str != '\0') { - msg_info ("error converting numeric argument %s", arg); + value = strtol (arg->str, &err_str, 10); + if (*err_str != '\0' && *err_str != '\r') { + msg_info ("error converting numeric argument %v", arg); value = 0; } } arg = rspamd_http_message_find_header (msg, "Flag"); if (arg) { errno = 0; - flag = strtol (arg, &err_str, 10); - if (errno != 0 || *err_str != '\0') { - msg_info ("error converting numeric argument %s", arg); + flag = strtol (arg->str, &err_str, 10); + if (*err_str != '\0' && *err_str != '\r') { + msg_info ("error converting numeric argument %v", arg); flag = 0; } } diff --git a/src/plugins/surbl.c b/src/plugins/surbl.c index e896f8749..531a8f3ba 100644 --- a/src/plugins/surbl.c +++ b/src/plugins/surbl.c @@ -947,20 +947,21 @@ surbl_redirector_finish (struct rspamd_http_connection *conn, { struct redirector_param *param = (struct redirector_param *)conn->ud; gint r, urllen; - const gchar *hdr; + const GString *hdr; gchar *urlstr; if (msg->code == 200) { hdr = rspamd_http_message_find_header (msg, "Uri"); if (hdr != NULL) { - msg_info ("<%s> got reply from redirector: '%s' -> '%s'", + msg_info ("<%s> got reply from redirector: '%s' -> '%v'", param->task->message_id, struri (param->url), hdr); - urllen = strlen (hdr); - urlstr = rspamd_mempool_strdup (param->task->task_pool, - hdr); + urllen = hdr->len; + urlstr = rspamd_mempool_alloc (param->task->task_pool, + urllen + 1); + rspamd_strlcpy (urlstr, hdr->str, urllen + 1); r = rspamd_url_parse (param->url, urlstr, urllen, param->task->task_pool); -- 2.39.5