From 58fe582b5d21d10d365954759fcb4c436b557810 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 7 Oct 2015 15:58:57 +0100 Subject: [PATCH] Fix the code for updated strings in HTTP library. --- src/controller.c | 57 +++++++++++++++++-------------------- src/http_proxy.c | 4 +-- src/libserver/protocol.c | 28 +++++++++--------- src/libserver/task.c | 29 ++++++++++++------- src/libserver/worker_util.c | 14 ++++----- src/plugins/fuzzy_check.c | 17 +++++------ src/plugins/surbl.c | 6 ++-- 7 files changed, 78 insertions(+), 77 deletions(-) diff --git a/src/controller.c b/src/controller.c index c68cd1bdc..6daf0bc57 100644 --- a/src/controller.c +++ b/src/controller.c @@ -252,7 +252,7 @@ rspamd_encrypted_password_get_str (const gchar * password, gsize skip, static gboolean rspamd_check_encrypted_password (struct rspamd_controller_worker_ctx *ctx, - const GString * password, const gchar * check, + const rspamd_ftok_t * password, const gchar * check, const struct rspamd_controller_pbkdf *pbkdf, gboolean is_enable) { @@ -267,7 +267,7 @@ rspamd_check_encrypted_password (struct rspamd_controller_worker_ctx *ctx, /* First of all check cached versions to save resources */ if (is_enable && ctx->cached_enable_password.len != 0) { if (password->len != ctx->cached_enable_password.len || - !rspamd_constant_memcmp (password->str, + !rspamd_constant_memcmp (password->begin, ctx->cached_enable_password.begin, password->len)) { msg_info_ctx ("incorrect or absent enable password has been specified"); return FALSE; @@ -277,7 +277,7 @@ rspamd_check_encrypted_password (struct rspamd_controller_worker_ctx *ctx, } else if (!is_enable && ctx->cached_password.len != 0) { if (password->len != ctx->cached_password.len || - !rspamd_constant_memcmp (password->str, + !rspamd_constant_memcmp (password->begin, ctx->cached_password.begin, password->len)) { msg_info_ctx ("incorrect or absent password has been specified"); return FALSE; @@ -314,7 +314,7 @@ rspamd_check_encrypted_password (struct rspamd_controller_worker_ctx *ctx, } local_key = g_alloca (pbkdf->key_len); - rspamd_cryptobox_pbkdf (password->str, password->len, + rspamd_cryptobox_pbkdf (password->begin, password->len, salt_decoded, salt_len, local_key, pbkdf->key_len, pbkdf->rounds); @@ -335,7 +335,7 @@ rspamd_check_encrypted_password (struct rspamd_controller_worker_ctx *ctx, /* Mmap region */ m = mmap (NULL, password->len, PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); - memcpy (m, password->str, password->len); + memcpy (m, password->begin, password->len); (void)mprotect (m, password->len, PROT_READ); (void)mlock (m, password->len); cache->begin = m; @@ -353,8 +353,8 @@ static gboolean rspamd_controller_check_password( struct rspamd_http_message *msg, gboolean is_enable) { const gchar *check; - const GString *password; - GString lookup; + const rspamd_ftok_t *password; + rspamd_ftok_t lookup; GHashTable *query_args = NULL; struct rspamd_controller_worker_ctx *ctx = session->ctx; gboolean check_normal = TRUE, check_enable = TRUE, ret = TRUE, @@ -381,7 +381,7 @@ static gboolean rspamd_controller_check_password( /* Try to get password from query args */ query_args = rspamd_http_message_parse_query (msg); - lookup.str = (gchar *)"password"; + lookup.begin = (gchar *)"password"; lookup.len = sizeof ("password") - 1; password = g_hash_table_lookup (query_args, &lookup); @@ -415,7 +415,7 @@ static gboolean rspamd_controller_check_password( } if (check != NULL) { if (!rspamd_is_encrypted_password (check, &pbkdf)) { - ret = rspamd_constant_memcmp (password->str, check, password->len); + ret = rspamd_constant_memcmp (password->begin, check, password->len); } else { ret = rspamd_check_encrypted_password (ctx, password, check, @@ -437,7 +437,7 @@ 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->str, check, + check_normal = rspamd_constant_memcmp (password->begin, check, password->len); } else { @@ -453,7 +453,7 @@ 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->str, check, + check_enable = rspamd_constant_memcmp (password->begin, check, password->len); } else { @@ -748,11 +748,10 @@ 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 GString *idstr; - gchar *errstr; + const rspamd_ftok_t *idstr; struct stat st; gint fd; - guint32 id; + gulong id; gboolean found = FALSE; struct rspamd_http_message *reply; @@ -769,8 +768,7 @@ rspamd_controller_handle_get_map (struct rspamd_http_connection_entry *conn_ent, return 0; } - id = strtoul (idstr->str, &errstr, 10); - if (*errstr != '\0' && !g_ascii_isspace (*errstr)) { + if (!rspamd_strtoul (idstr->begin, idstr->len, &id)) { msg_info_session ("invalid map id"); rspamd_controller_send_error (conn_ent, 400, "400 invalid map id"); return 0; @@ -802,7 +800,7 @@ rspamd_controller_handle_get_map (struct rspamd_http_connection_entry *conn_ent, reply = rspamd_http_new_message (HTTP_RESPONSE); reply->date = time (NULL); reply->code = 200; - reply->body = g_string_sized_new (st.st_size); + reply->body = rspamd_fstring_sized_new (st.st_size); /* Read the whole buffer */ if (read (fd, reply->body->str, st.st_size) == -1) { @@ -814,7 +812,6 @@ rspamd_controller_handle_get_map (struct rspamd_http_connection_entry *conn_ent, } reply->body->len = st.st_size; - reply->body->str[reply->body->len] = '\0'; close (fd); @@ -925,7 +922,7 @@ rspamd_controller_handle_graph ( GHashTable *query; struct rspamd_controller_session *session = conn_ent->ud; struct rspamd_controller_worker_ctx *ctx; - GString srch, *value; + rspamd_ftok_t srch, *value; struct rspamd_rrd_query_result *rrd_result; gulong i, j, ts, start_row, cnt, t; ucl_object_t *res, *elt[4], *data_elt; @@ -951,7 +948,7 @@ rspamd_controller_handle_graph ( } query = rspamd_http_message_parse_query (msg); - srch.str = (gchar *)"type"; + srch.begin = (gchar *)"type"; srch.len = 4; if (query == NULL || (value = g_hash_table_lookup (query, &srch)) == NULL) { @@ -965,16 +962,16 @@ rspamd_controller_handle_graph ( return 0; } - if (strncmp (value->str, "hourly", value->len) == 0) { + if (strncmp (value->begin, "hourly", value->len) == 0) { rra_num = rra_hourly; } - else if (strncmp (value->str, "daily", value->len) == 0) { + else if (strncmp (value->begin, "daily", value->len) == 0) { rra_num = rra_hourly; } - else if (strncmp (value->str, "weekly", value->len) == 0) { + else if (strncmp (value->begin, "weekly", value->len) == 0) { rra_num = rra_hourly; } - else if (strncmp (value->str, "monthly", value->len) == 0) { + else if (strncmp (value->begin, "monthly", value->len) == 0) { rra_num = rra_monthly; } @@ -1640,9 +1637,8 @@ rspamd_controller_handle_savemap (struct rspamd_http_connection_entry *conn_ent, GList *cur; struct rspamd_map *map; struct rspamd_controller_worker_ctx *ctx; - const GString *idstr; - gchar *errstr; - guint32 id; + const rspamd_ftok_t *idstr; + gulong id; gboolean found = FALSE; gint fd; @@ -1668,9 +1664,8 @@ rspamd_controller_handle_savemap (struct rspamd_http_connection_entry *conn_ent, return 0; } - id = strtoul (idstr->str, &errstr, 10); - if (*errstr != '\0' && !g_ascii_isspace (*errstr)) { - msg_info_session ("invalid map id: %v", idstr); + if (!rspamd_strtoul (idstr->begin, idstr->len, &id)) { + msg_info_session ("invalid map id: %T", idstr); rspamd_controller_send_error (conn_ent, 400, "Map id is invalid"); return 0; } @@ -1983,7 +1978,7 @@ rspamd_controller_handle_custom (struct rspamd_http_connection_entry *conn_ent, cmd = g_hash_table_lookup (session->ctx->custom_commands, msg->url->str); if (cmd == NULL || cmd->handler == NULL) { - msg_err_session ("custom command %v has not been found", msg->url); + msg_err_session ("custom command %V has not been found", msg->url); rspamd_controller_send_error (conn_ent, 404, "No command associated"); return 0; } diff --git a/src/http_proxy.c b/src/http_proxy.c index f71adc3f6..6570e01cc 100644 --- a/src/http_proxy.c +++ b/src/http_proxy.c @@ -295,7 +295,7 @@ proxy_client_finish_handler (struct rspamd_http_connection *conn, { struct http_proxy_session *session = conn->ud; struct rspamd_http_upstream *backend = NULL; - const GString *host; + const rspamd_ftok_t *host; gchar hostbuf[512]; if (!session->replied) { @@ -305,7 +305,7 @@ proxy_client_finish_handler (struct rspamd_http_connection *conn, backend = session->ctx->default_upstream; } else { - rspamd_strlcpy (hostbuf, host->str, sizeof (hostbuf)); + rspamd_strlcpy (hostbuf, host->begin, MIN(host->len + 1, sizeof (hostbuf))); backend = g_hash_table_lookup (session->ctx->upstreams, hostbuf); if (backend == NULL) { diff --git a/src/libserver/protocol.c b/src/libserver/protocol.c index 74b229c83..ba2599f5f 100644 --- a/src/libserver/protocol.c +++ b/src/libserver/protocol.c @@ -313,7 +313,7 @@ rspamd_protocol_handle_headers (struct rspamd_task *task, task->deliver_to); } else { - debug_task ("wrong header: %v", hn); + debug_task ("wrong header: %V", hn); validh = FALSE; } break; @@ -328,7 +328,7 @@ rspamd_protocol_handle_headers (struct rspamd_task *task, debug_task ("read hostname header, value: %s", task->hostname); } else { - debug_task ("wrong header: %v", hn); + debug_task ("wrong header: %V", hn); validh = FALSE; } break; @@ -336,12 +336,12 @@ rspamd_protocol_handle_headers (struct rspamd_task *task, case 'F': if (g_ascii_strncasecmp (headern, FROM_HEADER, hlen) == 0) { if (!rspamd_task_add_sender (task, hv->str)) { - msg_err_task ("bad from header: '%v'", hv); + msg_err_task ("bad from header: '%V'", hv); validh = FALSE; } } else { - debug_task ("wrong header: %v", hn); + debug_task ("wrong header: %V", hn); validh = FALSE; } break; @@ -357,7 +357,7 @@ rspamd_protocol_handle_headers (struct rspamd_task *task, } } else { - debug_task ("wrong header: %v", hn); + debug_task ("wrong header: %V", hn); validh = FALSE; } break; @@ -368,7 +368,7 @@ rspamd_protocol_handle_headers (struct rspamd_task *task, debug_task ("read queue_id header, value: %s", task->queue_id); } else { - debug_task ("wrong header: %v", hn); + debug_task ("wrong header: %V", hn); validh = FALSE; } break; @@ -376,13 +376,13 @@ rspamd_protocol_handle_headers (struct rspamd_task *task, case 'R': if (g_ascii_strncasecmp (headern, RCPT_HEADER, hlen) == 0) { if (!rspamd_task_add_recipient (task, hv->str)) { - msg_err_task ("bad from header: '%v'", h->value); + msg_err_task ("bad from header: '%V'", h->value); validh = FALSE; } - debug_task ("read rcpt header, value: %v", hv); + debug_task ("read rcpt header, value: %V", hv); } else { - debug_task ("wrong header: %v", hn); + debug_task ("wrong header: %V", hn); validh = FALSE; } break; @@ -390,14 +390,14 @@ rspamd_protocol_handle_headers (struct rspamd_task *task, case 'I': if (g_ascii_strncasecmp (headern, IP_ADDR_HEADER, hlen) == 0) { if (!rspamd_parse_inet_address (&task->from_addr, hv->str)) { - msg_err_task ("bad ip header: '%v'", hv); + msg_err_task ("bad ip header: '%V'", hv); return FALSE; } - debug_task ("read IP header, value: %v", hv); + debug_task ("read IP header, value: %V", hv); has_ip = TRUE; } else { - debug_task ("wrong header: %v", hn); + debug_task ("wrong header: %V", hn); validh = FALSE; } break; @@ -463,7 +463,7 @@ rspamd_protocol_handle_headers (struct rspamd_task *task, task->message_len = strtoul (hv->str, NULL, 10); if (task->message_len == 0) { - msg_err_task ("Invalid message length header: %v", hv); + msg_err_task ("Invalid message length header: %V", hv); validh = FALSE; } else { @@ -475,7 +475,7 @@ rspamd_protocol_handle_headers (struct rspamd_task *task, } break; default: - debug_task ("unknown header: %v", hn); + debug_task ("unknown header: %V", hn); validh = FALSE; break; } diff --git a/src/libserver/task.c b/src/libserver/task.c index 6d0eca2a2..907260af2 100644 --- a/src/libserver/task.c +++ b/src/libserver/task.c @@ -71,15 +71,15 @@ rspamd_task_new (struct rspamd_worker *worker) new_task->re_cache); new_task->raw_headers = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal); - new_task->request_headers = g_hash_table_new_full (rspamd_gstring_icase_hash, - rspamd_gstring_icase_equal, rspamd_gstring_free_hard, - rspamd_gstring_free_hard); + new_task->request_headers = g_hash_table_new_full (rspamd_ftok_icase_hash, + rspamd_ftok_icase_equal, rspamd_fstring_mapped_ftok_free, + rspamd_fstring_mapped_ftok_free); rspamd_mempool_add_destructor (new_task->task_pool, (rspamd_mempool_destruct_t) g_hash_table_unref, new_task->request_headers); - new_task->reply_headers = g_hash_table_new_full (rspamd_gstring_icase_hash, - rspamd_gstring_icase_equal, rspamd_gstring_free_hard, - rspamd_gstring_free_hard); + new_task->reply_headers = g_hash_table_new_full (rspamd_ftok_icase_hash, + rspamd_ftok_icase_equal, rspamd_fstring_mapped_ftok_free, + rspamd_fstring_mapped_ftok_free); rspamd_mempool_add_destructor (new_task->task_pool, (rspamd_mempool_destruct_t) g_hash_table_unref, new_task->reply_headers); @@ -261,6 +261,7 @@ rspamd_task_load_message (struct rspamd_task *task, ucl_object_t *control_obj; gchar filepath[PATH_MAX], *fp; gint fd, flen; + rspamd_ftok_t srch, *tok; gpointer map; struct stat st; @@ -268,11 +269,19 @@ rspamd_task_load_message (struct rspamd_task *task, rspamd_protocol_handle_headers (task, msg); } - if (task->flags & RSPAMD_TASK_FLAG_FILE) { - g_assert (task->msg.len > 0); + srch.begin = "file"; + srch.len = 4; + tok = g_hash_table_lookup (task->request_headers, &srch); - r = rspamd_strlcpy (filepath, task->msg.begin, - MIN (sizeof (filepath), task->msg.len + 1)); + if (tok == NULL) { + srch.begin = "path"; + srch.len = 4; + tok = g_hash_table_lookup (task->request_headers, &srch); + } + + if (tok) { + r = rspamd_strlcpy (filepath, tok->begin, + MIN (sizeof (filepath), tok->len + 1)); rspamd_decode_url (filepath, filepath, r + 1); flen = strlen (filepath); diff --git a/src/libserver/worker_util.c b/src/libserver/worker_util.c index eee200a41..ddd72acb9 100644 --- a/src/libserver/worker_util.c +++ b/src/libserver/worker_util.c @@ -278,14 +278,14 @@ rspamd_controller_send_error (struct rspamd_http_connection_entry *entry, msg = rspamd_http_new_message (HTTP_RESPONSE); va_start (args, error_msg); - msg->status = g_string_sized_new (128); - rspamd_vprintf_gstring (msg->status, error_msg, args); + msg->status = rspamd_fstring_sized_new (128); + rspamd_vprintf_fstring (&msg->status, error_msg, args); va_end (args); msg->date = time (NULL); msg->code = code; - msg->body = g_string_sized_new (128); - rspamd_printf_gstring (msg->body, "{\"error\":\"%v\"}", msg->status); + msg->body = rspamd_fstring_sized_new (128); + rspamd_printf_fstring (&msg->body, "{\"error\":\"%V\"}", msg->status); rspamd_http_connection_reset (entry->conn); rspamd_http_connection_write_message (entry->conn, msg, @@ -307,7 +307,7 @@ rspamd_controller_send_string (struct rspamd_http_connection_entry *entry, msg = rspamd_http_new_message (HTTP_RESPONSE); msg->date = time (NULL); msg->code = 200; - msg->body = g_string_new (str); + msg->body = rspamd_fstring_new_init (str, strlen (str)); rspamd_http_connection_reset (entry->conn); rspamd_http_connection_write_message (entry->conn, msg, @@ -329,8 +329,8 @@ rspamd_controller_send_ucl (struct rspamd_http_connection_entry *entry, msg = rspamd_http_new_message (HTTP_RESPONSE); msg->date = time (NULL); msg->code = 200; - msg->body = g_string_sized_new (BUFSIZ); - rspamd_ucl_emit_gstring (obj, UCL_EMIT_JSON_COMPACT, msg->body); + msg->body = rspamd_fstring_sized_new (BUFSIZ); + rspamd_ucl_emit_fstring (obj, UCL_EMIT_JSON_COMPACT, &msg->body); rspamd_http_connection_reset (entry->conn); rspamd_http_connection_write_message (entry->conn, msg, diff --git a/src/plugins/fuzzy_check.c b/src/plugins/fuzzy_check.c index bc4822b17..6ca18aaf8 100644 --- a/src/plugins/fuzzy_check.c +++ b/src/plugins/fuzzy_check.c @@ -1549,26 +1549,23 @@ static gboolean fuzzy_controller_handler (struct rspamd_http_connection_entry *conn_ent, struct rspamd_http_message *msg, struct module_ctx *ctx, gint cmd) { - const GString *arg; - gchar *err_str; - gint value = 1, flag = 0; + const rspamd_ftok_t *arg; + glong value = 1, flag = 0; /* Get size */ arg = rspamd_http_message_find_header (msg, "Weight"); if (arg) { errno = 0; - value = strtol (arg->str, &err_str, 10); - if (*err_str != '\0' && *err_str != '\r') { - msg_info ("error converting numeric argument %v", arg); - value = 0; + + if (!rspamd_strtol (arg->begin, arg->len, &value)) { + msg_info ("error converting numeric argument %T", arg); } } arg = rspamd_http_message_find_header (msg, "Flag"); if (arg) { errno = 0; - flag = strtol (arg->str, &err_str, 10); - if (*err_str != '\0' && *err_str != '\r') { - msg_info ("error converting numeric argument %v", arg); + if (!rspamd_strtol (arg->begin, arg->len, &flag)) { + msg_info ("error converting numeric argument %T", arg); flag = 0; } } diff --git a/src/plugins/surbl.c b/src/plugins/surbl.c index 0c1d6887f..7ce58ab1d 100644 --- a/src/plugins/surbl.c +++ b/src/plugins/surbl.c @@ -988,7 +988,7 @@ surbl_redirector_finish (struct rspamd_http_connection *conn, struct redirector_param *param = (struct redirector_param *)conn->ud; struct rspamd_task *task; gint r, urllen; - const GString *hdr; + const rspamd_ftok_t *hdr; gchar *urlstr; task = param->task; @@ -1003,7 +1003,7 @@ surbl_redirector_finish (struct rspamd_http_connection *conn, urllen = hdr->len; urlstr = rspamd_mempool_alloc (param->task->task_pool, urllen + 1); - rspamd_strlcpy (urlstr, hdr->str, urllen + 1); + rspamd_strlcpy (urlstr, hdr->begin, urllen + 1); r = rspamd_url_parse (param->url, urlstr, urllen, param->task->task_pool); @@ -1066,7 +1066,7 @@ register_redirector_call (struct rspamd_url *url, struct rspamd_task *task, RSPAMD_HTTP_CLIENT_SIMPLE, RSPAMD_HTTP_CLIENT, NULL); msg = rspamd_http_new_message (HTTP_REQUEST); - g_string_assign (msg->url, struri (url)); + msg->url = rspamd_fstring_assign (msg->url, struri (url), strlen (struri (url))); param->sock = s; param->suffix = suffix; param->redirector = selected; -- 2.39.5