From 7d7440b2114b4c81816882433294f9c951bc39e8 Mon Sep 17 00:00:00 2001 From: "cebka@lenovo-laptop" Date: Wed, 30 Dec 2009 15:54:48 +0300 Subject: [PATCH] * Add support of whitelists in settings (by Anton Nekhoroshin) --- src/settings.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++-- src/settings.h | 1 + 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/settings.c b/src/settings.c index 6de04a7eb..cd7167edd 100644 --- a/src/settings.c +++ b/src/settings.c @@ -53,6 +53,9 @@ settings_free (gpointer data) if (s->reject_scores) { g_hash_table_destroy (s->reject_scores); } + if (s->whitelist) { + g_hash_table_destroy (s->whitelist); + } g_free (s); } @@ -101,7 +104,7 @@ void json_fin_cb (memory_pool_t * pool, struct map_cb_data *data) { struct json_buf *jb; - int nelts, i; + int nelts, i, n, a; json_t *js, *cur_elt, *cur_nm, *it_val; json_error_t je; struct rspamd_settings *cur_settings; @@ -154,6 +157,7 @@ json_fin_cb (memory_pool_t * pool, struct map_cb_data *data) cur_settings->metric_scores = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); cur_settings->reject_scores = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); cur_settings->factors = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + cur_settings->whitelist = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); cur_settings->statfile_alias = NULL; cur_settings->want_spam = FALSE; @@ -217,6 +221,18 @@ json_fin_cb (memory_pool_t * pool, struct map_cb_data *data) json_it = json_object_iter_next(cur_nm, json_it); } } + /* Whitelist object */ + cur_nm = json_object_get (cur_elt, "whitelist"); + if (cur_nm != NULL && json_is_array (cur_nm)) { + n = json_array_size(cur_nm); + for(a = 0; a < n; a++) { + it_val = json_array_get(cur_nm, a); + if (it_val && json_is_string (it_val)) { + g_hash_table_insert (cur_settings->whitelist, g_strdup (json_string_value (it_val)), g_strdup (json_string_value (it_val))); + } + + } + } /* Want spam */ cur_nm = json_object_get (cur_elt, "want_spam"); if (cur_nm != NULL) { @@ -302,6 +318,29 @@ check_setting (struct worker_task *task, struct rspamd_settings **user_settings, return FALSE; } +static gboolean +check_whitelist(struct worker_task *task, struct rspamd_settings *s) +{ + char *src_email = NULL, *src_domain = NULL; + + if (task->from != NULL) { + src_email = task->from; + } else { + return FALSE; + } + + src_domain = strchr (src_email, '@'); + if(src_domain != NULL) { + src_domain++; + } + + if (((g_hash_table_lookup (s->whitelist, src_email) != NULL) || + ( (src_domain != NULL) && (g_hash_table_lookup (s->whitelist, src_domain) != NULL)) )) { + return TRUE; + } + return FALSE; +} + gboolean check_metric_settings (struct worker_task * task, struct metric * metric, double *score, double *rscore) { @@ -312,7 +351,11 @@ check_metric_settings (struct worker_task * task, struct metric * metric, double if (check_setting (task, &us, &ds)) { if (us != NULL) { - /* First search in user's settings */ + /* First look in user white list */ + if (check_whitelist(task, us)) { + *score = DEFAULT_REJECT_SCORE; + return TRUE; + } if ((rs = g_hash_table_lookup (us->reject_scores, metric->name)) != NULL) { *rscore = *rs; } @@ -330,6 +373,10 @@ check_metric_settings (struct worker_task * task, struct metric * metric, double } } else if (ds != NULL) { + if (check_whitelist(task, ds)) { + *score = DEFAULT_REJECT_SCORE; + return TRUE; + } if ((rs = g_hash_table_lookup (ds->reject_scores, metric->name)) != NULL) { *rscore = *rs; } diff --git a/src/settings.h b/src/settings.h index 58e877570..1f7ee743f 100644 --- a/src/settings.h +++ b/src/settings.h @@ -8,6 +8,7 @@ struct rspamd_settings { GHashTable *metric_scores; /**< hash table of metric require scores for this setting */ GHashTable *reject_scores; /**< hash table of metric reject scores for this setting */ GHashTable *factors; /**< hash table of new factors for this setting */ + GHashTable *whitelist; /**< hash table of whitelist for this setting */ char *statfile_alias; /**< alias for statfile used */ gboolean want_spam; /**< if true disable rspamd checks */ }; -- 2.39.5