diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2014-07-23 12:45:28 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2014-07-23 12:45:28 +0100 |
commit | e0483657ff6cf1adc828ccce457814d61fe90a0d (patch) | |
tree | 5183e4163f40b81b3e7d5f51488d360883782154 /src/plugins/surbl.c | |
parent | 7962087e808fb824aa3af6d41d02abc92916ba1e (diff) | |
download | rspamd-e0483657ff6cf1adc828ccce457814d61fe90a0d.tar.gz rspamd-e0483657ff6cf1adc828ccce457814d61fe90a0d.zip |
Unify code style.
Diffstat (limited to 'src/plugins/surbl.c')
-rw-r--r-- | src/plugins/surbl.c | 805 |
1 files changed, 521 insertions, 284 deletions
diff --git a/src/plugins/surbl.c b/src/plugins/surbl.c index ef25c1d78..bd4037228 100644 --- a/src/plugins/surbl.c +++ b/src/plugins/surbl.c @@ -41,27 +41,27 @@ * - bit (string): describes a prefix for a single bit */ +#include "cfg_file.h" #include "config.h" +#include "dns.h" +#include "expressions.h" +#include "hash.h" #include "main.h" +#include "map.h" #include "message.h" -#include "cfg_file.h" -#include "expressions.h" #include "util.h" -#include "map.h" -#include "dns.h" -#include "hash.h" #include "surbl.h" -static struct surbl_ctx *surbl_module_ctx = NULL; +static struct surbl_ctx *surbl_module_ctx = NULL; static void surbl_test_url (struct rspamd_task *task, void *user_data); static void dns_callback (struct rdns_reply *reply, gpointer arg); static void process_dns_results (struct rspamd_task *task, - struct suffix_item *suffix, gchar *url, guint32 addr); + struct suffix_item *suffix, gchar *url, guint32 addr); -#define NO_REGEXP (gpointer)-1 +#define NO_REGEXP (gpointer) - 1 #define SURBL_ERROR surbl_error_quark () #define WHITELIST_ERROR 0 @@ -89,50 +89,63 @@ module_t surbl_module = { static void exception_insert (gpointer st, gconstpointer key, gpointer value) { - GHashTable **t = st; - gint level = 0; - const gchar *p = key; - f_str_t *val; - + GHashTable **t = st; + gint level = 0; + const gchar *p = key; + f_str_t *val; + while (*p) { if (*p == '.') { - level ++; + level++; } - p ++; + p++; } if (level >= MAX_LEVELS) { - msg_err ("invalid domain in exceptions list: %s, levels: %d", (gchar *)key, level); + msg_err ("invalid domain in exceptions list: %s, levels: %d", + (gchar *)key, + level); return; } - + val = g_malloc (sizeof (f_str_t)); val->begin = (gchar *)key; val->len = strlen (key); if (t[level] == NULL) { - t[level] = g_hash_table_new_full (fstr_strcase_hash, fstr_strcase_equal, g_free, NULL); + t[level] = g_hash_table_new_full (fstr_strcase_hash, + fstr_strcase_equal, + g_free, + NULL); } g_hash_table_insert (t[level], val, value); } static gchar * -read_exceptions_list (rspamd_mempool_t * pool, gchar * chunk, gint len, struct map_cb_data *data) +read_exceptions_list (rspamd_mempool_t * pool, + gchar * chunk, + gint len, + struct map_cb_data *data) { if (data->cur_data == NULL) { - data->cur_data = rspamd_mempool_alloc0 (pool, sizeof (GHashTable *) * MAX_LEVELS); + data->cur_data = rspamd_mempool_alloc0 (pool, + sizeof (GHashTable *) * MAX_LEVELS); } - return abstract_parse_list (pool, chunk, len, data, (insert_func) exception_insert); + return abstract_parse_list (pool, + chunk, + len, + data, + (insert_func) exception_insert); } static void fin_exceptions_list (rspamd_mempool_t * pool, struct map_cb_data *data) { - GHashTable **t; - gint i; + GHashTable **t; + gint i; if (data->prev_data) { t = data->prev_data; - for (i = 0; i < MAX_LEVELS; i ++) { + for (i = 0; i < MAX_LEVELS; i++) { if (t[i] != NULL) { g_hash_table_destroy (t[i]); } @@ -143,16 +156,16 @@ fin_exceptions_list (rspamd_mempool_t * pool, struct map_cb_data *data) static void redirector_insert (gpointer st, gconstpointer key, gpointer value) { - GHashTable *t = st; - const gchar *p = key, *begin = key; - gchar *new; - gsize len; - GRegex *re = NO_REGEXP; - GError *err = NULL; - guint idx; + GHashTable *t = st; + const gchar *p = key, *begin = key; + gchar *new; + gsize len; + GRegex *re = NO_REGEXP; + GError *err = NULL; + guint idx; while (*p && !g_ascii_isspace (*p)) { - p ++; + p++; } len = p - begin; @@ -165,13 +178,17 @@ redirector_insert (gpointer st, gconstpointer key, gpointer value) if (g_ascii_isspace (*p)) { while (g_ascii_isspace (*p) && *p) { - p ++; + p++; } if (*p) { - re = g_regex_new (p, G_REGEX_RAW | G_REGEX_OPTIMIZE | G_REGEX_NO_AUTO_CAPTURE | G_REGEX_CASELESS, - 0, &err); + re = g_regex_new (p, + G_REGEX_RAW | G_REGEX_OPTIMIZE | G_REGEX_NO_AUTO_CAPTURE | G_REGEX_CASELESS, + 0, + &err); if (re == NULL) { - msg_warn ("could not read regexp: %s while reading regexp %s", err->message, p); + msg_warn ("could not read regexp: %s while reading regexp %s", + err->message, + p); re = NO_REGEXP; } } @@ -182,21 +199,31 @@ redirector_insert (gpointer st, gconstpointer key, gpointer value) static void redirector_item_free (gpointer p) { - GRegex *re; + GRegex *re; if (p != NULL && p != NO_REGEXP) { re = (GRegex *)p; g_regex_unref (re); } } -static gchar * -read_redirectors_list (rspamd_mempool_t * pool, gchar * chunk, gint len, struct map_cb_data *data) +static gchar * +read_redirectors_list (rspamd_mempool_t * pool, + gchar * chunk, + gint len, + struct map_cb_data *data) { if (data->cur_data == NULL) { - data->cur_data = g_hash_table_new_full (rspamd_strcase_hash, rspamd_strcase_equal, g_free, redirector_item_free); + data->cur_data = g_hash_table_new_full (rspamd_strcase_hash, + rspamd_strcase_equal, + g_free, + redirector_item_free); } - return abstract_parse_list (pool, chunk, len, data, (insert_func) redirector_insert); + return abstract_parse_list (pool, + chunk, + len, + data, + (insert_func) redirector_insert); } void @@ -214,7 +241,8 @@ surbl_module_init (struct rspamd_config *cfg, struct module_ctx **ctx) surbl_module_ctx->use_redirector = 0; surbl_module_ctx->suffixes = NULL; - surbl_module_ctx->surbl_pool = rspamd_mempool_new (rspamd_mempool_suggest_size ()); + surbl_module_ctx->surbl_pool = rspamd_mempool_new ( + rspamd_mempool_suggest_size ()); surbl_module_ctx->tld2_file = NULL; surbl_module_ctx->whitelist_file = NULL; @@ -222,16 +250,28 @@ surbl_module_init (struct rspamd_config *cfg, struct module_ctx **ctx) surbl_module_ctx->redirector_trie = rspamd_trie_create (TRUE); surbl_module_ctx->redirector_ptrs = g_ptr_array_new (); - surbl_module_ctx->redirector_hosts = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal); - surbl_module_ctx->whitelist = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal); + surbl_module_ctx->redirector_hosts = g_hash_table_new (rspamd_strcase_hash, + rspamd_strcase_equal); + surbl_module_ctx->whitelist = g_hash_table_new (rspamd_strcase_hash, + rspamd_strcase_equal); /* Zero exceptions hashes */ - surbl_module_ctx->exceptions = rspamd_mempool_alloc0 (surbl_module_ctx->surbl_pool, MAX_LEVELS * sizeof (GHashTable *)); + surbl_module_ctx->exceptions = rspamd_mempool_alloc0 ( + surbl_module_ctx->surbl_pool, + MAX_LEVELS * sizeof (GHashTable *)); /* Register destructors */ - rspamd_mempool_add_destructor (surbl_module_ctx->surbl_pool, (rspamd_mempool_destruct_t) g_hash_table_destroy, surbl_module_ctx->whitelist); - rspamd_mempool_add_destructor (surbl_module_ctx->surbl_pool, (rspamd_mempool_destruct_t) g_hash_table_destroy, surbl_module_ctx->redirector_hosts); - - rspamd_mempool_add_destructor (surbl_module_ctx->surbl_pool, (rspamd_mempool_destruct_t) rspamd_trie_free, surbl_module_ctx->redirector_trie); - rspamd_mempool_add_destructor (surbl_module_ctx->surbl_pool, (rspamd_mempool_destruct_t) g_ptr_array_unref, surbl_module_ctx->redirector_ptrs); + rspamd_mempool_add_destructor (surbl_module_ctx->surbl_pool, + (rspamd_mempool_destruct_t) g_hash_table_destroy, + surbl_module_ctx->whitelist); + rspamd_mempool_add_destructor (surbl_module_ctx->surbl_pool, + (rspamd_mempool_destruct_t) g_hash_table_destroy, + surbl_module_ctx->redirector_hosts); + + rspamd_mempool_add_destructor (surbl_module_ctx->surbl_pool, + (rspamd_mempool_destruct_t) rspamd_trie_free, + surbl_module_ctx->redirector_trie); + rspamd_mempool_add_destructor (surbl_module_ctx->surbl_pool, + (rspamd_mempool_destruct_t) g_ptr_array_unref, + surbl_module_ctx->redirector_ptrs); *ctx = (struct module_ctx *)surbl_module_ctx; @@ -244,8 +284,8 @@ surbl_module_init (struct rspamd_config *cfg, struct module_ctx **ctx) static void register_bit_symbols (struct rspamd_config *cfg, struct suffix_item *suffix) { - GList *cur; - struct surbl_bit_item *bit; + GList *cur; + struct surbl_bit_item *bit; if (suffix->bits != NULL) { /* Prepend bit to symbol */ @@ -264,131 +304,163 @@ register_bit_symbols (struct rspamd_config *cfg, struct suffix_item *suffix) gint surbl_module_config (struct rspamd_config *cfg) { - GList *cur_opt; - struct suffix_item *new_suffix, *cur_suffix = NULL; - struct surbl_bit_item *new_bit; + GList *cur_opt; + struct suffix_item *new_suffix, *cur_suffix = NULL; + struct surbl_bit_item *new_bit; - const ucl_object_t *value, *cur, *cur_rule, *cur_bit; - ucl_object_iter_t it = NULL; - const gchar *redir_val; - guint32 bit; - gint i, idx; + const ucl_object_t *value, *cur, *cur_rule, *cur_bit; + ucl_object_iter_t it = NULL; + const gchar *redir_val; + guint32 bit; + gint i, idx; - if ((value = rspamd_config_get_module_opt (cfg, "surbl", "redirector")) != NULL) { + if ((value = + rspamd_config_get_module_opt (cfg, "surbl", "redirector")) != NULL) { i = 0; - LL_FOREACH (value, cur) { - i ++; + LL_FOREACH (value, cur) + { + i++; } - surbl_module_ctx->redirectors = rspamd_mempool_alloc0 (surbl_module_ctx->surbl_pool, - i * sizeof (struct redirector_upstream)); + surbl_module_ctx->redirectors = rspamd_mempool_alloc0 ( + surbl_module_ctx->surbl_pool, + i * sizeof (struct redirector_upstream)); idx = 0; - LL_FOREACH (value, cur) { + LL_FOREACH (value, cur) + { redir_val = ucl_obj_tostring (cur); surbl_module_ctx->redirectors[idx].up.priority = 100; - if (! rspamd_parse_host_port_priority (surbl_module_ctx->surbl_pool, - redir_val, &surbl_module_ctx->redirectors[idx].addr, - &surbl_module_ctx->redirectors[idx].port, - &surbl_module_ctx->redirectors[idx].up.priority)) { + if (!rspamd_parse_host_port_priority (surbl_module_ctx->surbl_pool, + redir_val, &surbl_module_ctx->redirectors[idx].addr, + &surbl_module_ctx->redirectors[idx].port, + &surbl_module_ctx->redirectors[idx].up.priority)) { msg_warn ("invalid redirector definition: %s", redir_val); } else { if (surbl_module_ctx->redirectors[idx].port != 0) { - surbl_module_ctx->redirectors[idx].name = rspamd_mempool_strdup (surbl_module_ctx->surbl_pool, + surbl_module_ctx->redirectors[idx].name = + rspamd_mempool_strdup (surbl_module_ctx->surbl_pool, redir_val); - msg_info ("add redirector %s", surbl_module_ctx->redirectors[idx].name); - idx ++; + msg_info ("add redirector %s", + surbl_module_ctx->redirectors[idx].name); + idx++; } } - i ++; + i++; } surbl_module_ctx->redirectors_number = idx; - surbl_module_ctx->use_redirector = (surbl_module_ctx->redirectors_number != 0); + surbl_module_ctx->use_redirector = + (surbl_module_ctx->redirectors_number != 0); } - if ((value = rspamd_config_get_module_opt (cfg, "surbl", "redirector_symbol")) != NULL) { + if ((value = + rspamd_config_get_module_opt (cfg, "surbl", + "redirector_symbol")) != NULL) { surbl_module_ctx->redirector_symbol = ucl_obj_tostring (value); - register_virtual_symbol (&cfg->cache, surbl_module_ctx->redirector_symbol, 1.0); + register_virtual_symbol (&cfg->cache, + surbl_module_ctx->redirector_symbol, + 1.0); } else { surbl_module_ctx->redirector_symbol = NULL; } - if ((value = rspamd_config_get_module_opt (cfg, "surbl", "weight")) != NULL) { + if ((value = + rspamd_config_get_module_opt (cfg, "surbl", "weight")) != NULL) { surbl_module_ctx->weight = ucl_obj_toint (value); } else { surbl_module_ctx->weight = DEFAULT_SURBL_WEIGHT; } - if ((value = rspamd_config_get_module_opt (cfg, "surbl", "url_expire")) != NULL) { + if ((value = + rspamd_config_get_module_opt (cfg, "surbl", "url_expire")) != NULL) { surbl_module_ctx->url_expire = ucl_obj_todouble (value); } else { surbl_module_ctx->url_expire = DEFAULT_SURBL_URL_EXPIRE; } - if ((value = rspamd_config_get_module_opt (cfg, "surbl", "redirector_connect_timeout")) != NULL) { + if ((value = + rspamd_config_get_module_opt (cfg, "surbl", + "redirector_connect_timeout")) != NULL) { surbl_module_ctx->connect_timeout = ucl_obj_todouble (value); } else { surbl_module_ctx->connect_timeout = DEFAULT_REDIRECTOR_CONNECT_TIMEOUT; } - if ((value = rspamd_config_get_module_opt (cfg, "surbl", "redirector_read_timeout")) != NULL) { + if ((value = + rspamd_config_get_module_opt (cfg, "surbl", + "redirector_read_timeout")) != NULL) { surbl_module_ctx->read_timeout = ucl_obj_todouble (value); } else { surbl_module_ctx->read_timeout = DEFAULT_REDIRECTOR_READ_TIMEOUT; } - if ((value = rspamd_config_get_module_opt (cfg, "surbl", "redirector_hosts_map")) != NULL) { - add_map (cfg, ucl_obj_tostring (value), - "SURBL redirectors list", read_redirectors_list, fin_redirectors_list, - (void **)&surbl_module_ctx->redirector_hosts); + if ((value = + rspamd_config_get_module_opt (cfg, "surbl", + "redirector_hosts_map")) != NULL) { + add_map (cfg, ucl_obj_tostring ( + value), + "SURBL redirectors list", read_redirectors_list, fin_redirectors_list, + (void **)&surbl_module_ctx->redirector_hosts); } - if ((value = rspamd_config_get_module_opt (cfg, "surbl", "max_urls")) != NULL) { + if ((value = + rspamd_config_get_module_opt (cfg, "surbl", "max_urls")) != NULL) { surbl_module_ctx->max_urls = ucl_obj_toint (value); } else { surbl_module_ctx->max_urls = DEFAULT_SURBL_MAX_URLS; } - if ((value = rspamd_config_get_module_opt (cfg, "surbl", "exceptions")) != NULL) { + if ((value = + rspamd_config_get_module_opt (cfg, "surbl", "exceptions")) != NULL) { if (add_map (cfg, ucl_obj_tostring (value), - "SURBL exceptions list", read_exceptions_list, fin_exceptions_list, - (void **)&surbl_module_ctx->exceptions)) { - surbl_module_ctx->tld2_file = rspamd_mempool_strdup (surbl_module_ctx->surbl_pool, - ucl_obj_tostring (value) + sizeof ("file://") - 1); + "SURBL exceptions list", read_exceptions_list, fin_exceptions_list, + (void **)&surbl_module_ctx->exceptions)) { + surbl_module_ctx->tld2_file = rspamd_mempool_strdup ( + surbl_module_ctx->surbl_pool, + ucl_obj_tostring (value) + sizeof ("file://") - 1); } } - if ((value = rspamd_config_get_module_opt (cfg, "surbl", "whitelist")) != NULL) { + if ((value = + rspamd_config_get_module_opt (cfg, "surbl", "whitelist")) != NULL) { if (add_map (cfg, ucl_obj_tostring (value), - "SURBL whitelist", read_host_list, fin_host_list, - (void **)&surbl_module_ctx->whitelist)) { - surbl_module_ctx->whitelist_file = rspamd_mempool_strdup (surbl_module_ctx->surbl_pool, - ucl_obj_tostring (value) + sizeof ("file://") - 1); + "SURBL whitelist", read_host_list, fin_host_list, + (void **)&surbl_module_ctx->whitelist)) { + surbl_module_ctx->whitelist_file = rspamd_mempool_strdup ( + surbl_module_ctx->surbl_pool, + ucl_obj_tostring (value) + sizeof ("file://") - 1); } } value = rspamd_config_get_module_opt (cfg, "surbl", "rule"); if (value != NULL && value->type == UCL_OBJECT) { - LL_FOREACH (value, cur_rule) { + LL_FOREACH (value, cur_rule) + { cur = ucl_obj_get_key (cur_rule, "suffix"); if (cur == NULL) { msg_err ("surbl rule must have explicit symbol definition"); continue; } - new_suffix = rspamd_mempool_alloc (surbl_module_ctx->surbl_pool, sizeof (struct suffix_item)); - new_suffix->suffix = rspamd_mempool_strdup (surbl_module_ctx->surbl_pool, - ucl_obj_tostring (cur)); + new_suffix = rspamd_mempool_alloc (surbl_module_ctx->surbl_pool, + sizeof (struct suffix_item)); + new_suffix->suffix = rspamd_mempool_strdup ( + surbl_module_ctx->surbl_pool, + ucl_obj_tostring (cur)); new_suffix->options = 0; new_suffix->bits = NULL; cur = ucl_obj_get_key (cur_rule, "symbol"); if (cur == NULL) { - msg_warn ("surbl rule for suffix %s lacks symbol, using %s as symbol", new_suffix->suffix, - DEFAULT_SURBL_SYMBOL); - new_suffix->symbol = rspamd_mempool_strdup (surbl_module_ctx->surbl_pool, - DEFAULT_SURBL_SYMBOL); + msg_warn ( + "surbl rule for suffix %s lacks symbol, using %s as symbol", + new_suffix->suffix, + DEFAULT_SURBL_SYMBOL); + new_suffix->symbol = rspamd_mempool_strdup ( + surbl_module_ctx->surbl_pool, + DEFAULT_SURBL_SYMBOL); } else { - new_suffix->symbol = rspamd_mempool_strdup (surbl_module_ctx->surbl_pool, - ucl_obj_tostring (cur)); + new_suffix->symbol = rspamd_mempool_strdup ( + surbl_module_ctx->surbl_pool, + ucl_obj_tostring (cur)); } cur = ucl_obj_get_key (cur_rule, "options"); if (cur != NULL && cur->type == UCL_STRING) { @@ -399,19 +471,33 @@ surbl_module_config (struct rspamd_config *cfg) cur = ucl_obj_get_key (cur_rule, "bits"); if (cur != NULL && cur->type == UCL_OBJECT) { it = NULL; - while ((cur_bit = ucl_iterate_object (cur, &it, true)) != NULL) { - if (ucl_object_key (cur_bit) != NULL && cur_bit->type == UCL_INT) { + while ((cur_bit = + ucl_iterate_object (cur, &it, true)) != NULL) { + if (ucl_object_key (cur_bit) != NULL && cur_bit->type == + UCL_INT) { bit = ucl_obj_toint (cur_bit); - new_bit = rspamd_mempool_alloc (surbl_module_ctx->surbl_pool, sizeof (struct surbl_bit_item)); + new_bit = rspamd_mempool_alloc ( + surbl_module_ctx->surbl_pool, + sizeof (struct surbl_bit_item)); new_bit->bit = bit; - new_bit->symbol = rspamd_mempool_strdup (surbl_module_ctx->surbl_pool, ucl_object_key (cur_bit)); - msg_debug ("add new bit suffix: %d with symbol: %s", (gint)new_bit->bit, new_bit->symbol); - new_suffix->bits = g_list_prepend (new_suffix->bits, new_bit); + new_bit->symbol = rspamd_mempool_strdup ( + surbl_module_ctx->surbl_pool, + ucl_object_key (cur_bit)); + msg_debug ("add new bit suffix: %d with symbol: %s", + (gint)new_bit->bit, new_bit->symbol); + new_suffix->bits = g_list_prepend (new_suffix->bits, + new_bit); } } } - surbl_module_ctx->suffixes = g_list_prepend (surbl_module_ctx->suffixes, new_suffix); - register_callback_symbol (&cfg->cache, new_suffix->symbol, 1, surbl_test_url, new_suffix); + surbl_module_ctx->suffixes = g_list_prepend ( + surbl_module_ctx->suffixes, + new_suffix); + register_callback_symbol (&cfg->cache, + new_suffix->symbol, + 1, + surbl_test_url, + new_suffix); } } /* Add default suffix */ @@ -421,8 +507,9 @@ surbl_module_config (struct rspamd_config *cfg) } if (surbl_module_ctx->suffixes != NULL) { - rspamd_mempool_add_destructor (surbl_module_ctx->surbl_pool, (rspamd_mempool_destruct_t) g_list_free, - surbl_module_ctx->suffixes); + rspamd_mempool_add_destructor (surbl_module_ctx->surbl_pool, + (rspamd_mempool_destruct_t) g_list_free, + surbl_module_ctx->suffixes); } cur_opt = surbl_module_ctx->suffixes; @@ -430,8 +517,9 @@ surbl_module_config (struct rspamd_config *cfg) cur_suffix = cur_opt->data; if (cur_suffix->bits != NULL) { register_bit_symbols (cfg, cur_suffix); - rspamd_mempool_add_destructor (surbl_module_ctx->surbl_pool, (rspamd_mempool_destruct_t) g_list_free, - cur_suffix->bits); + rspamd_mempool_add_destructor (surbl_module_ctx->surbl_pool, + (rspamd_mempool_destruct_t) g_list_free, + cur_suffix->bits); } cur_opt = g_list_next (cur_opt); } @@ -447,25 +535,40 @@ surbl_module_reconfig (struct rspamd_config *cfg) /* Reinit module */ surbl_module_ctx->use_redirector = 0; surbl_module_ctx->suffixes = NULL; - surbl_module_ctx->surbl_pool = rspamd_mempool_new (rspamd_mempool_suggest_size ()); + surbl_module_ctx->surbl_pool = rspamd_mempool_new ( + rspamd_mempool_suggest_size ()); surbl_module_ctx->tld2_file = NULL; surbl_module_ctx->whitelist_file = NULL; surbl_module_ctx->redirectors_number = 0; surbl_module_ctx->redirector_trie = rspamd_trie_create (TRUE); - surbl_module_ctx->redirector_hosts = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal); - surbl_module_ctx->whitelist = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal); + surbl_module_ctx->redirector_hosts = g_hash_table_new (rspamd_strcase_hash, + rspamd_strcase_equal); + surbl_module_ctx->whitelist = g_hash_table_new (rspamd_strcase_hash, + rspamd_strcase_equal); /* Zero exceptions hashes */ - surbl_module_ctx->exceptions = rspamd_mempool_alloc0 (surbl_module_ctx->surbl_pool, MAX_LEVELS * sizeof (GHashTable *)); + surbl_module_ctx->exceptions = rspamd_mempool_alloc0 ( + surbl_module_ctx->surbl_pool, + MAX_LEVELS * sizeof (GHashTable *)); /* Register destructors */ - rspamd_mempool_add_destructor (surbl_module_ctx->surbl_pool, (rspamd_mempool_destruct_t) g_hash_table_destroy, surbl_module_ctx->whitelist); - rspamd_mempool_add_destructor (surbl_module_ctx->surbl_pool, (rspamd_mempool_destruct_t) g_hash_table_destroy, surbl_module_ctx->redirector_hosts); - - rspamd_mempool_add_destructor (surbl_module_ctx->surbl_pool, (rspamd_mempool_destruct_t) g_list_free, surbl_module_ctx->suffixes); - - rspamd_mempool_add_destructor (surbl_module_ctx->surbl_pool, (rspamd_mempool_destruct_t) rspamd_trie_free, surbl_module_ctx->redirector_trie); - rspamd_mempool_add_destructor (surbl_module_ctx->surbl_pool, (rspamd_mempool_destruct_t) g_ptr_array_unref, surbl_module_ctx->redirector_ptrs); + rspamd_mempool_add_destructor (surbl_module_ctx->surbl_pool, + (rspamd_mempool_destruct_t) g_hash_table_destroy, + surbl_module_ctx->whitelist); + rspamd_mempool_add_destructor (surbl_module_ctx->surbl_pool, + (rspamd_mempool_destruct_t) g_hash_table_destroy, + surbl_module_ctx->redirector_hosts); + + rspamd_mempool_add_destructor (surbl_module_ctx->surbl_pool, + (rspamd_mempool_destruct_t) g_list_free, + surbl_module_ctx->suffixes); + + rspamd_mempool_add_destructor (surbl_module_ctx->surbl_pool, + (rspamd_mempool_destruct_t) rspamd_trie_free, + surbl_module_ctx->redirector_trie); + rspamd_mempool_add_destructor (surbl_module_ctx->surbl_pool, + (rspamd_mempool_destruct_t) g_ptr_array_unref, + surbl_module_ctx->redirector_ptrs); /* Perform configure */ return surbl_module_config (cfg); @@ -473,16 +576,22 @@ surbl_module_reconfig (struct rspamd_config *cfg) -static gchar * -format_surbl_request (rspamd_mempool_t * pool, f_str_t * hostname, struct suffix_item *suffix, - gboolean append_suffix, GError ** err, gboolean forced, GTree *tree) +static gchar * +format_surbl_request (rspamd_mempool_t * pool, + f_str_t * hostname, + struct suffix_item *suffix, + gboolean append_suffix, + GError ** err, + gboolean forced, + GTree *tree) { - GHashTable *t; - gchar *result = NULL, *dots[MAX_LEVELS], num_buf[sizeof("18446744073709551616")], *p; - gint len, slen, r, i, dots_num = 0, level = MAX_LEVELS; - gboolean is_numeric = TRUE; - guint64 ip_num; - f_str_t f; + GHashTable *t; + gchar *result = NULL, *dots[MAX_LEVELS], + num_buf[sizeof("18446744073709551616")], *p; + gint len, slen, r, i, dots_num = 0, level = MAX_LEVELS; + gboolean is_numeric = TRUE; + guint64 ip_num; + f_str_t f; if (G_LIKELY (suffix != NULL)) { slen = strlen (suffix->suffix); @@ -494,19 +603,19 @@ format_surbl_request (rspamd_mempool_t * pool, f_str_t * hostname, struct suffix g_assert_not_reached (); } len = hostname->len + slen + 2; - + p = hostname->begin; while (p - hostname->begin < (gint)hostname->len && dots_num < MAX_LEVELS) { if (*p == '.') { dots[dots_num] = p; - dots_num ++; + dots_num++; } - else if (! g_ascii_isdigit (*p)) { + else if (!g_ascii_isdigit (*p)) { is_numeric = FALSE; } - p ++; + p++; } - + /* Check for numeric expressions */ if (is_numeric && dots_num == 3) { /* This is ip address */ @@ -516,7 +625,7 @@ format_surbl_request (rspamd_mempool_t * pool, f_str_t * hostname, struct suffix return NULL; } result = rspamd_mempool_alloc (pool, len); - r = rspamd_snprintf (result, len, "%*s.%*s.%*s.%*s", + r = rspamd_snprintf (result, len, "%*s.%*s.%*s.%*s", (gint)(hostname->len - (dots[2] - hostname->begin + 1)), dots[2] + 1, (gint)(dots[2] - dots[1] - 1), @@ -533,13 +642,16 @@ format_surbl_request (rspamd_mempool_t * pool, f_str_t * hostname, struct suffix msg_info ("ignore request of ip url for list %s", suffix->symbol); return NULL; } - rspamd_strlcpy (num_buf, hostname->begin, MIN (hostname->len + 1, sizeof (num_buf))); + rspamd_strlcpy (num_buf, hostname->begin, + MIN (hostname->len + 1, sizeof (num_buf))); errno = 0; ip_num = strtoull (num_buf, NULL, 10); if (errno != 0) { - msg_info ("cannot convert ip to number '%s': %s", num_buf, strerror (errno)); - g_set_error (err, SURBL_ERROR, /* error domain */ - CONVERSION_ERROR, /* error code */ + msg_info ("cannot convert ip to number '%s': %s", + num_buf, + strerror (errno)); + g_set_error (err, SURBL_ERROR, /* error domain */ + CONVERSION_ERROR, /* error code */ "URL cannot be decoded"); return NULL; } @@ -549,19 +661,25 @@ format_surbl_request (rspamd_mempool_t * pool, f_str_t * hostname, struct suffix /* Hack for bugged windows resolver */ ip_num &= 0xFFFFFFFF; /* Get octets */ - r = rspamd_snprintf (result, len, "%ud.%ud.%ud.%ud", - (guint32) ip_num & 0x000000FF, (guint32) (ip_num & 0x0000FF00) >> 8, (guint32) (ip_num & 0x00FF0000) >> 16, (guint32) (ip_num & 0xFF000000) >> 24); + r = rspamd_snprintf (result, + len, + "%ud.%ud.%ud.%ud", + (guint32) ip_num & 0x000000FF, + (guint32) (ip_num & 0x0000FF00) >> 8, + (guint32) (ip_num & 0x00FF0000) >> 16, + (guint32) (ip_num & 0xFF000000) >> 24); } else { /* Not a numeric url */ result = rspamd_mempool_alloc (pool, len); /* Now we should try to check for exceptions */ - if (! forced) { - for (i = MAX_LEVELS - 1; i >= 0; i --) { + if (!forced) { + for (i = MAX_LEVELS - 1; i >= 0; i--) { t = surbl_module_ctx->exceptions[i]; if (t != NULL && dots_num >= i + 1) { f.begin = dots[dots_num - i - 1] + 1; - f.len = hostname->len - (dots[dots_num - i - 1] - hostname->begin + 1); + f.len = hostname->len - + (dots[dots_num - i - 1] - hostname->begin + 1); if (g_hash_table_lookup (t, &f) != NULL) { level = dots_num - i - 1; break; @@ -571,31 +689,41 @@ format_surbl_request (rspamd_mempool_t * pool, f_str_t * hostname, struct suffix } if (level != MAX_LEVELS) { if (level == 0) { - r = rspamd_snprintf (result, len, "%*s", (gint)hostname->len, hostname->begin); + r = rspamd_snprintf (result, + len, + "%*s", + (gint)hostname->len, + hostname->begin); } else { - r = rspamd_snprintf (result, len, "%*s", - (gint)(hostname->len - (dots[level - 1] - hostname->begin + 1)), - dots[level - 1] + 1); + r = rspamd_snprintf (result, len, "%*s", + (gint)(hostname->len - + (dots[level - 1] - hostname->begin + 1)), + dots[level - 1] + 1); } } else if (dots_num >= 2) { r = rspamd_snprintf (result, len, "%*s", - (gint)(hostname->len - (dots[dots_num - 2] - hostname->begin + 1)), + (gint)(hostname->len - + (dots[dots_num - 2] - hostname->begin + 1)), dots[dots_num - 2] + 1); } else { - r = rspamd_snprintf (result, len, "%*s", (gint)hostname->len, hostname->begin); + r = rspamd_snprintf (result, + len, + "%*s", + (gint)hostname->len, + hostname->begin); } } if (tree != NULL) { if (g_tree_lookup (tree, result) != NULL) { msg_debug ("url %s is already registered", result); - g_set_error (err, SURBL_ERROR, /* error domain */ - DUPLICATE_ERROR, /* error code */ - "URL is duplicated: %s", /* error message format string */ - result); + g_set_error (err, SURBL_ERROR, /* error domain */ + DUPLICATE_ERROR, /* error code */ + "URL is duplicated: %s", /* error message format string */ + result); return NULL; } else { @@ -603,12 +731,13 @@ format_surbl_request (rspamd_mempool_t * pool, f_str_t * hostname, struct suffix } } - if (!forced && g_hash_table_lookup (surbl_module_ctx->whitelist, result) != NULL) { + if (!forced && + g_hash_table_lookup (surbl_module_ctx->whitelist, result) != NULL) { msg_debug ("url %s is whitelisted", result); - g_set_error (err, SURBL_ERROR, /* error domain */ - WHITELIST_ERROR, /* error code */ - "URL is whitelisted: %s", /* error message format string */ - result); + g_set_error (err, SURBL_ERROR, /* error domain */ + WHITELIST_ERROR, /* error code */ + "URL is whitelisted: %s", /* error message format string */ + result); return NULL; } @@ -617,38 +746,48 @@ format_surbl_request (rspamd_mempool_t * pool, f_str_t * hostname, struct suffix rspamd_snprintf (result + r, len - r, ".%s", suffix->suffix); } - msg_debug ("request: %s, dots: %d, level: %d, orig: %*s", result, dots_num, level, (gint)hostname->len, hostname->begin); + msg_debug ("request: %s, dots: %d, level: %d, orig: %*s", + result, + dots_num, + level, + (gint)hostname->len, + hostname->begin); return result; } static void make_surbl_requests (struct uri *url, struct rspamd_task *task, - struct suffix_item *suffix, gboolean forced, GTree *tree) + struct suffix_item *suffix, gboolean forced, GTree *tree) { - gchar *surbl_req; - f_str_t f; - GError *err = NULL; - struct dns_param *param; + gchar *surbl_req; + f_str_t f; + GError *err = NULL; + struct dns_param *param; f.begin = url->host; f.len = url->hostlen; if ((surbl_req = format_surbl_request (task->task_pool, &f, suffix, TRUE, - &err, forced, tree)) != NULL) { - param = rspamd_mempool_alloc (task->task_pool, sizeof (struct dns_param)); + &err, forced, tree)) != NULL) { + param = + rspamd_mempool_alloc (task->task_pool, sizeof (struct dns_param)); param->url = url; param->task = task; param->suffix = suffix; - param->host_resolve = rspamd_mempool_strdup (task->task_pool, surbl_req); + param->host_resolve = + rspamd_mempool_strdup (task->task_pool, surbl_req); debug_task ("send surbl dns request %s", surbl_req); - if (make_dns_request (task->resolver, task->s, task->task_pool, dns_callback, - (void *)param, RDNS_REQUEST_A, surbl_req)) { - task->dns_requests ++; + if (make_dns_request (task->resolver, task->s, task->task_pool, + dns_callback, + (void *)param, RDNS_REQUEST_A, surbl_req)) { + task->dns_requests++; } } - else if (err != NULL && err->code != WHITELIST_ERROR && err->code != DUPLICATE_ERROR) { - msg_info ("cannot format url string for surbl %s, %s", struri (url), err->message); + else if (err != NULL && err->code != WHITELIST_ERROR && err->code != + DUPLICATE_ERROR) { + msg_info ("cannot format url string for surbl %s, %s", struri ( + url), err->message); g_error_free (err); return; } @@ -658,58 +797,67 @@ make_surbl_requests (struct uri *url, struct rspamd_task *task, } static void -process_dns_results (struct rspamd_task *task, struct suffix_item *suffix, gchar *url, guint32 addr) +process_dns_results (struct rspamd_task *task, + struct suffix_item *suffix, + gchar *url, + guint32 addr) { - GList *cur; - struct surbl_bit_item *bit; + GList *cur; + struct surbl_bit_item *bit; if (suffix->bits != NULL) { cur = g_list_first (suffix->bits); while (cur) { bit = (struct surbl_bit_item *)cur->data; - debug_task ("got result(%d) AND bit(%d): %d", (gint)addr, (gint)ntohl (bit->bit), - (gint)bit->bit & (gint)ntohl (addr)); + debug_task ("got result(%d) AND bit(%d): %d", + (gint)addr, + (gint)ntohl (bit->bit), + (gint)bit->bit & (gint)ntohl (addr)); if (((gint)bit->bit & (gint)ntohl (addr)) != 0) { insert_result (task, bit->symbol, 1, - g_list_prepend (NULL, rspamd_mempool_strdup (task->task_pool, url))); + g_list_prepend (NULL, + rspamd_mempool_strdup (task->task_pool, url))); } cur = g_list_next (cur); } } else { - insert_result (task, suffix->symbol, 1, g_list_prepend (NULL, rspamd_mempool_strdup (task->task_pool, url))); + insert_result (task, suffix->symbol, 1, + g_list_prepend (NULL, + rspamd_mempool_strdup (task->task_pool, url))); } } static void dns_callback (struct rdns_reply *reply, gpointer arg) { - struct dns_param *param = (struct dns_param *)arg; - struct rspamd_task *task = param->task; - struct rdns_reply_entry *elt; + struct dns_param *param = (struct dns_param *)arg; + struct rspamd_task *task = param->task; + struct rdns_reply_entry *elt; debug_task ("in surbl request callback"); /* If we have result from DNS server, this url exists in SURBL, so increase score */ if (reply->code == RDNS_RC_NOERROR && reply->entries) { msg_info ("<%s> domain [%s] is in surbl %s", param->task->message_id, - param->host_resolve, param->suffix->suffix); + param->host_resolve, param->suffix->suffix); elt = reply->entries; if (elt->type == RDNS_REQUEST_A) { process_dns_results (param->task, param->suffix, - param->host_resolve, (guint32)elt->content.a.addr.s_addr); + param->host_resolve, (guint32)elt->content.a.addr.s_addr); } } else { debug_task ("<%s> domain [%s] is not in surbl %s", - param->task->message_id, param->host_resolve, param->suffix->suffix); + param->task->message_id, param->host_resolve, + param->suffix->suffix); } } static void free_redirector_session (void *ud) { - struct redirector_param *param = (struct redirector_param *)ud; + struct redirector_param *param = (struct redirector_param *)ud; event_del (¶m->ev); g_string_free (param->buf, TRUE); @@ -719,38 +867,55 @@ free_redirector_session (void *ud) static void redirector_callback (gint fd, short what, void *arg) { - struct redirector_param *param = (struct redirector_param *)arg; - struct rspamd_task *task = param->task; - gchar url_buf[512]; - gint r; - struct timeval *timeout; - gchar *p, *c; - gboolean found = FALSE; + struct redirector_param *param = (struct redirector_param *)arg; + struct rspamd_task *task = param->task; + gchar url_buf[512]; + gint r; + struct timeval *timeout; + gchar *p, *c; + gboolean found = FALSE; switch (param->state) { case STATE_CONNECT: /* We have write readiness after connect call, so reinit event */ if (what == EV_WRITE) { - timeout = rspamd_mempool_alloc (param->task->task_pool, sizeof (struct timeval)); + timeout = + rspamd_mempool_alloc (param->task->task_pool, + sizeof (struct timeval)); timeout->tv_sec = surbl_module_ctx->read_timeout / 1000; - timeout->tv_usec = (surbl_module_ctx->read_timeout - timeout->tv_sec * 1000) * 1000; + timeout->tv_usec = + (surbl_module_ctx->read_timeout - timeout->tv_sec * + 1000) * 1000; event_del (¶m->ev); - event_set (¶m->ev, param->sock, EV_READ | EV_PERSIST, redirector_callback, (void *)param); + event_set (¶m->ev, + param->sock, + EV_READ | EV_PERSIST, + redirector_callback, + (void *)param); event_add (¶m->ev, timeout); - r = rspamd_snprintf (url_buf, sizeof (url_buf), "GET %s HTTP/1.0\r\n\r\n", struri (param->url)); + r = rspamd_snprintf (url_buf, + sizeof (url_buf), + "GET %s HTTP/1.0\r\n\r\n", + struri (param->url)); if (write (param->sock, url_buf, r) == -1) { - msg_err ("write failed %s to %s", strerror (errno), param->redirector->name); + msg_err ("write failed %s to %s", strerror ( + errno), param->redirector->name); upstream_fail (¶m->redirector->up, param->task->tv.tv_sec); - remove_normal_event (param->task->s, free_redirector_session, param); + remove_normal_event (param->task->s, + free_redirector_session, + param); return; } param->state = STATE_READ; } else { - msg_info ("<%s> connection to redirector %s timed out while waiting for write", - param->task->message_id, param->redirector->name); + msg_info ( + "<%s> connection to redirector %s timed out while waiting for write", + param->task->message_id, + param->redirector->name); upstream_fail (¶m->redirector->up, param->task->tv.tv_sec); - remove_normal_event (param->task->s, free_redirector_session, param); + remove_normal_event (param->task->s, free_redirector_session, + param); return; } @@ -759,10 +924,17 @@ redirector_callback (gint fd, short what, void *arg) if (what == EV_READ) { r = read (param->sock, url_buf, sizeof (url_buf) - 1); if (r <= 0) { - msg_err ("read failed: %s from %s", strerror (errno), param->redirector->name); + msg_err ("read failed: %s from %s", strerror ( + errno), param->redirector->name); upstream_fail (¶m->redirector->up, param->task->tv.tv_sec); - make_surbl_requests (param->url, param->task, param->suffix, FALSE, param->tree); - remove_normal_event (param->task->s, free_redirector_session, param); + make_surbl_requests (param->url, + param->task, + param->suffix, + FALSE, + param->tree); + remove_normal_event (param->task->s, + free_redirector_session, + param); return; } @@ -779,21 +951,36 @@ redirector_callback (gint fd, short what, void *arg) } } if (found) { - debug_task ("<%s> got reply from redirector: '%s' -> '%s'", param->task->message_id, struri (param->url), c); - r = parse_uri (param->url, rspamd_mempool_strdup (param->task->task_pool, c), param->task->task_pool); - if (r == URI_ERRNO_OK || r == URI_ERRNO_NO_SLASHES || r == URI_ERRNO_NO_HOST_SLASH) { - make_surbl_requests (param->url, param->task, param->suffix, FALSE, param->tree); + debug_task ("<%s> got reply from redirector: '%s' -> '%s'", + param->task->message_id, + struri (param->url), + c); + r = + parse_uri (param->url, + rspamd_mempool_strdup (param->task->task_pool, + c), param->task->task_pool); + if (r == URI_ERRNO_OK || r == URI_ERRNO_NO_SLASHES || r == + URI_ERRNO_NO_HOST_SLASH) { + make_surbl_requests (param->url, + param->task, + param->suffix, + FALSE, + param->tree); } } } upstream_ok (¶m->redirector->up, param->task->tv.tv_sec); - remove_normal_event (param->task->s, free_redirector_session, param); + remove_normal_event (param->task->s, free_redirector_session, + param); } else { - msg_info ("<%s> reading redirector %s timed out, while waiting for read", - param->redirector->name, param->task->message_id); + msg_info ( + "<%s> reading redirector %s timed out, while waiting for read", + param->redirector->name, + param->task->message_id); upstream_fail (¶m->redirector->up, param->task->tv.tv_sec); - remove_normal_event (param->task->s, free_redirector_session, param); + remove_normal_event (param->task->s, free_redirector_session, + param); } break; } @@ -802,30 +989,42 @@ redirector_callback (gint fd, short what, void *arg) static void register_redirector_call (struct uri *url, struct rspamd_task *task, - struct suffix_item *suffix, const gchar *rule, GTree *tree) + struct suffix_item *suffix, const gchar *rule, GTree *tree) { - gint s = -1; - struct redirector_param *param; - struct timeval *timeout; - struct redirector_upstream *selected; - - selected = (struct redirector_upstream *)get_upstream_round_robin (surbl_module_ctx->redirectors, - surbl_module_ctx->redirectors_number, - sizeof (struct redirector_upstream), - task->tv.tv_sec, DEFAULT_UPSTREAM_ERROR_TIME, - DEFAULT_UPSTREAM_DEAD_TIME, DEFAULT_UPSTREAM_MAXERRORS); + gint s = -1; + struct redirector_param *param; + struct timeval *timeout; + struct redirector_upstream *selected; + + selected = (struct redirector_upstream *)get_upstream_round_robin ( + surbl_module_ctx->redirectors, + surbl_module_ctx->redirectors_number, + sizeof (struct redirector_upstream), + task->tv.tv_sec, + DEFAULT_UPSTREAM_ERROR_TIME, + DEFAULT_UPSTREAM_DEAD_TIME, + DEFAULT_UPSTREAM_MAXERRORS); if (selected) { - s = make_universal_socket (selected->addr, selected->port, SOCK_STREAM, TRUE, FALSE, FALSE); + s = make_universal_socket (selected->addr, + selected->port, + SOCK_STREAM, + TRUE, + FALSE, + FALSE); } if (s == -1) { - msg_info ("<%s> cannot create tcp socket failed: %s", task->message_id, strerror (errno)); + msg_info ("<%s> cannot create tcp socket failed: %s", + task->message_id, + strerror (errno)); make_surbl_requests (url, task, suffix, FALSE, tree); return; } - param = rspamd_mempool_alloc (task->task_pool, sizeof (struct redirector_param)); + param = + rspamd_mempool_alloc (task->task_pool, + sizeof (struct redirector_param)); param->url = url; param->task = task; param->state = STATE_CONNECT; @@ -836,25 +1035,33 @@ register_redirector_call (struct uri *url, struct rspamd_task *task, param->tree = tree; timeout = rspamd_mempool_alloc (task->task_pool, sizeof (struct timeval)); timeout->tv_sec = surbl_module_ctx->connect_timeout / 1000; - timeout->tv_usec = (surbl_module_ctx->connect_timeout - timeout->tv_sec * 1000) * 1000; + timeout->tv_usec = + (surbl_module_ctx->connect_timeout - timeout->tv_sec * 1000) * 1000; event_set (¶m->ev, s, EV_WRITE, redirector_callback, (void *)param); event_add (¶m->ev, timeout); - register_async_event (task->s, free_redirector_session, param, g_quark_from_static_string ("surbl")); - - msg_info ("<%s> registered redirector call for %s to %s, according to rule: %s", - task->message_id, struri (url), selected->name, rule); + register_async_event (task->s, + free_redirector_session, + param, + g_quark_from_static_string ("surbl")); + + msg_info ( + "<%s> registered redirector call for %s to %s, according to rule: %s", + task->message_id, + struri (url), + selected->name, + rule); } -static gboolean +static gboolean surbl_tree_url_callback (gpointer key, gpointer value, void *data) { - struct redirector_param *param = data; - struct rspamd_task *task; - struct uri *url = value; - gchar *red_domain; - const gchar *pos; - GRegex *re; - guint idx, len; + struct redirector_param *param = data; + struct rspamd_task *task; + struct uri *url = value; + gchar *red_domain; + const gchar *pos; + GRegex *re; + guint idx, len; task = param->task; debug_task ("check url %s", struri (url)); @@ -866,32 +1073,48 @@ surbl_tree_url_callback (gpointer key, gpointer value, void *data) if (surbl_module_ctx->use_redirector) { /* Search in trie */ if (surbl_module_ctx->redirector_trie && - (pos = rspamd_trie_lookup (surbl_module_ctx->redirector_trie, url->host, url->hostlen, &idx)) != NULL && - idx < surbl_module_ctx->redirector_ptrs->len) { + (pos = + rspamd_trie_lookup (surbl_module_ctx->redirector_trie, url->host, + url->hostlen, &idx)) != NULL && + idx < surbl_module_ctx->redirector_ptrs->len) { /* Get corresponding prefix */ - red_domain = g_ptr_array_index (surbl_module_ctx->redirector_ptrs, idx); + red_domain = g_ptr_array_index (surbl_module_ctx->redirector_ptrs, + idx); if (red_domain != NULL) { len = strlen (red_domain); /* First check that we have found domain at the end of host */ if (pos + len == url->host + url->hostlen && (pos == url->host || *(pos - 1) == '.')) { /* Try to find corresponding regexp */ - re = g_hash_table_lookup (surbl_module_ctx->redirector_hosts, red_domain); - if (re != NULL && (re == NO_REGEXP || g_regex_match (re, url->string, 0, NULL))) { + re = g_hash_table_lookup ( + surbl_module_ctx->redirector_hosts, + red_domain); + if (re != NULL && + (re == NO_REGEXP || + g_regex_match (re, url->string, 0, NULL))) { /* If no regexp found or founded regexp matches url string register redirector's call */ if (surbl_module_ctx->redirector_symbol != NULL) { - insert_result (param->task, surbl_module_ctx->redirector_symbol, 1, g_list_prepend (NULL, red_domain)); + insert_result (param->task, + surbl_module_ctx->redirector_symbol, + 1, + g_list_prepend (NULL, red_domain)); } - register_redirector_call (url, param->task, param->suffix, red_domain, param->tree); + register_redirector_call (url, + param->task, + param->suffix, + red_domain, + param->tree); return FALSE; } } } } - make_surbl_requests (url, param->task, param->suffix, FALSE, param->tree); + make_surbl_requests (url, param->task, param->suffix, FALSE, + param->tree); } else { - make_surbl_requests (url, param->task, param->suffix, FALSE, param->tree); + make_surbl_requests (url, param->task, param->suffix, FALSE, + param->tree); } return FALSE; @@ -900,13 +1123,15 @@ surbl_tree_url_callback (gpointer key, gpointer value, void *data) static void surbl_test_url (struct rspamd_task *task, void *user_data) { - struct redirector_param param; - struct suffix_item *suffix = user_data; + struct redirector_param param; + struct suffix_item *suffix = user_data; param.task = task; param.suffix = suffix; param.tree = g_tree_new ((GCompareFunc)strcmp); - rspamd_mempool_add_destructor (task->task_pool, (rspamd_mempool_destruct_t)g_tree_destroy, param.tree); + rspamd_mempool_add_destructor (task->task_pool, + (rspamd_mempool_destruct_t)g_tree_destroy, + param.tree); g_tree_foreach (task->urls, surbl_tree_url_callback, ¶m); } /* @@ -914,17 +1139,17 @@ surbl_test_url (struct rspamd_task *task, void *user_data) */ #if 0 struct urls_tree_cb_data { - gchar *buf; - gsize len; - gsize off; - struct rspamd_task *task; + gchar *buf; + gsize len; + gsize off; + struct rspamd_task *task; }; static gboolean calculate_buflen_cb (gpointer key, gpointer value, gpointer cbdata) { - struct urls_tree_cb_data *cb = cbdata; - struct uri *url = value; + struct urls_tree_cb_data *cb = cbdata; + struct uri *url = value; cb->len += strlen (struri (url)) + url->hostlen + sizeof (" <\"\">, ") - 1; @@ -934,23 +1159,30 @@ calculate_buflen_cb (gpointer key, gpointer value, gpointer cbdata) static gboolean write_urls_buffer (gpointer key, gpointer value, gpointer cbdata) { - struct urls_tree_cb_data *cb = cbdata; - struct uri *url = value; - f_str_t f; - gchar *urlstr; - gsize len; + struct urls_tree_cb_data *cb = cbdata; + struct uri *url = value; + f_str_t f; + gchar *urlstr; + gsize len; f.begin = url->host; f.len = url->hostlen; - if ((urlstr = format_surbl_request (cb->task->task_pool, &f, NULL, FALSE, NULL, FALSE)) != NULL) { + if ((urlstr = + format_surbl_request (cb->task->task_pool, &f, NULL, FALSE, NULL, + FALSE)) != NULL) { len = strlen (urlstr); if (cb->off + len >= cb->len) { - msg_info ("cannot write urls header completely, stripped reply at: %z", cb->off); + msg_info ( + "cannot write urls header completely, stripped reply at: %z", + cb->off); return TRUE; } else { - cb->off += rspamd_snprintf (cb->buf + cb->off, cb->len - cb->off, " %s <\"%s\">,", - urlstr, struri (url)); + cb->off += rspamd_snprintf (cb->buf + cb->off, + cb->len - cb->off, + " %s <\"%s\">,", + urlstr, + struri (url)); } } @@ -961,7 +1193,7 @@ write_urls_buffer (gpointer key, gpointer value, gpointer cbdata) static gboolean urls_command_handler (struct rspamd_task *task) { - struct urls_tree_cb_data cb; + struct urls_tree_cb_data cb; /* First calculate buffer length */ cb.len = sizeof (RSPAMD_REPLY_BANNER "/1.0 0 " SPAMD_OK CRLF "Urls: " CRLF); @@ -969,9 +1201,12 @@ urls_command_handler (struct rspamd_task *task) g_tree_foreach (task->urls, calculate_buflen_cb, &cb); cb.buf = rspamd_mempool_alloc (task->task_pool, cb.len * sizeof (gchar)); - cb.off += rspamd_snprintf (cb.buf + cb.off, cb.len - cb.off, "%s/%s 0 %s" CRLF "Urls:", - (task->proto == SPAMC_PROTO) ? SPAMD_REPLY_BANNER : RSPAMD_REPLY_BANNER, - "1.3", SPAMD_OK); + cb.off += rspamd_snprintf (cb.buf + cb.off, + cb.len - cb.off, + "%s/%s 0 %s" CRLF "Urls:", + (task->proto == SPAMC_PROTO) ? SPAMD_REPLY_BANNER : RSPAMD_REPLY_BANNER, + "1.3", + SPAMD_OK); cb.task = task; /* Write urls to buffer */ @@ -982,10 +1217,12 @@ urls_command_handler (struct rspamd_task *task) cb.buf[--cb.off] = '\0'; } /* Write result */ - if (! rspamd_dispatcher_write (task->dispatcher, cb.buf, cb.off, FALSE, TRUE)) { + if (!rspamd_dispatcher_write (task->dispatcher, cb.buf, cb.off, FALSE, + TRUE)) { return FALSE; } - if (!rspamd_dispatcher_write (task->dispatcher, CRLF, sizeof (CRLF) - 1, FALSE, TRUE)) { + if (!rspamd_dispatcher_write (task->dispatcher, CRLF, sizeof (CRLF) - 1, + FALSE, TRUE)) { return FALSE; } task->state = STATE_REPLY; @@ -994,5 +1231,5 @@ urls_command_handler (struct rspamd_task *task) } #endif /* - * vi:ts=4 + * vi:ts=4 */ |