aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/dkim_check.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2014-07-23 12:53:08 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2014-07-23 12:53:08 +0100
commitfe79d8c5a39f2b717f78cc3f3ef21b3cfc46500b (patch)
treec84e6a5d4c5cd78a7a2cc3c7adbc7af5d0541682 /src/plugins/dkim_check.c
parente0483657ff6cf1adc828ccce457814d61fe90a0d (diff)
downloadrspamd-fe79d8c5a39f2b717f78cc3f3ef21b3cfc46500b.tar.gz
rspamd-fe79d8c5a39f2b717f78cc3f3ef21b3cfc46500b.zip
Revert "Unify code style."
This reverts commit e0483657ff6cf1adc828ccce457814d61fe90a0d.
Diffstat (limited to 'src/plugins/dkim_check.c')
-rw-r--r--src/plugins/dkim_check.c237
1 files changed, 84 insertions, 153 deletions
diff --git a/src/plugins/dkim_check.c b/src/plugins/dkim_check.c
index 5ebe336f0..78743ebf1 100644
--- a/src/plugins/dkim_check.c
+++ b/src/plugins/dkim_check.c
@@ -37,15 +37,15 @@
* - skip_mutli (flag): skip messages with multiply dkim signatures
*/
-#include "cfg_file.h"
#include "config.h"
-#include "dkim.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 "dkim.h"
+#include "hash.h"
#define DEFAULT_SYMBOL_REJECT "R_DKIM_REJECT"
#define DEFAULT_SYMBOL_TEMPFAIL "R_DKIM_TEMPFAIL"
@@ -55,24 +55,24 @@
#define DEFAULT_TIME_JITTER 60
struct dkim_ctx {
- gint (*filter) (struct rspamd_task * task);
- const gchar *symbol_reject;
- const gchar *symbol_tempfail;
- const gchar *symbol_allow;
-
- rspamd_mempool_t *dkim_pool;
- radix_tree_t *whitelist_ip;
- GHashTable *dkim_domains;
- guint strict_multiplier;
- guint time_jitter;
- rspamd_lru_hash_t *dkim_hash;
- gboolean trusted_only;
- gboolean skip_multi;
+ gint (*filter) (struct rspamd_task * task);
+ const gchar *symbol_reject;
+ const gchar *symbol_tempfail;
+ const gchar *symbol_allow;
+
+ rspamd_mempool_t *dkim_pool;
+ radix_tree_t *whitelist_ip;
+ GHashTable *dkim_domains;
+ guint strict_multiplier;
+ guint time_jitter;
+ rspamd_lru_hash_t *dkim_hash;
+ gboolean trusted_only;
+ gboolean skip_multi;
};
-static struct dkim_ctx *dkim_module_ctx = NULL;
+static struct dkim_ctx *dkim_module_ctx = NULL;
-static void dkim_symbol_callback (struct rspamd_task *task, void *unused);
+static void dkim_symbol_callback (struct rspamd_task *task, void *unused);
/* Initialization */
gint dkim_module_init (struct rspamd_config *cfg, struct module_ctx **ctx);
@@ -92,8 +92,7 @@ dkim_module_init (struct rspamd_config *cfg, struct module_ctx **ctx)
{
dkim_module_ctx = g_malloc0 (sizeof (struct dkim_ctx));
- dkim_module_ctx->dkim_pool = rspamd_mempool_new (
- rspamd_mempool_suggest_size ());
+ dkim_module_ctx->dkim_pool = rspamd_mempool_new (rspamd_mempool_suggest_size ());
*ctx = (struct module_ctx *)dkim_module_ctx;
@@ -103,96 +102,79 @@ dkim_module_init (struct rspamd_config *cfg, struct module_ctx **ctx)
gint
dkim_module_config (struct rspamd_config *cfg)
{
- const ucl_object_t *value;
- gint res = TRUE;
- guint cache_size, cache_expire;
- gboolean got_trusted = FALSE;
+ const ucl_object_t *value;
+ gint res = TRUE;
+ guint cache_size, cache_expire;
+ gboolean got_trusted = FALSE;
dkim_module_ctx->whitelist_ip = radix_tree_create ();
- if ((value =
- rspamd_config_get_module_opt (cfg, "dkim", "symbol_reject")) != NULL) {
+ if ((value = rspamd_config_get_module_opt (cfg, "dkim", "symbol_reject")) != NULL) {
dkim_module_ctx->symbol_reject = ucl_obj_tostring (value);
}
else {
dkim_module_ctx->symbol_reject = DEFAULT_SYMBOL_REJECT;
}
- if ((value =
- rspamd_config_get_module_opt (cfg, "dkim",
- "symbol_tempfail")) != NULL) {
+ if ((value = rspamd_config_get_module_opt (cfg, "dkim", "symbol_tempfail")) != NULL) {
dkim_module_ctx->symbol_tempfail = ucl_obj_tostring (value);
}
else {
dkim_module_ctx->symbol_tempfail = DEFAULT_SYMBOL_TEMPFAIL;
}
- if ((value =
- rspamd_config_get_module_opt (cfg, "dkim", "symbol_allow")) != NULL) {
+ if ((value = rspamd_config_get_module_opt (cfg, "dkim", "symbol_allow")) != NULL) {
dkim_module_ctx->symbol_allow = ucl_obj_tostring (value);
}
else {
dkim_module_ctx->symbol_allow = DEFAULT_SYMBOL_ALLOW;
}
- if ((value =
- rspamd_config_get_module_opt (cfg, "dkim",
- "dkim_cache_size")) != NULL) {
+ if ((value = rspamd_config_get_module_opt (cfg, "dkim", "dkim_cache_size")) != NULL) {
cache_size = ucl_obj_toint (value);
}
else {
cache_size = DEFAULT_CACHE_SIZE;
}
- if ((value =
- rspamd_config_get_module_opt (cfg, "dkim",
- "dkim_cache_expire")) != NULL) {
+ if ((value = rspamd_config_get_module_opt (cfg, "dkim", "dkim_cache_expire")) != NULL) {
cache_expire = ucl_obj_todouble (value);
}
else {
cache_expire = DEFAULT_CACHE_MAXAGE;
}
- if ((value =
- rspamd_config_get_module_opt (cfg, "dkim", "time_jitter")) != NULL) {
+ if ((value = rspamd_config_get_module_opt (cfg, "dkim", "time_jitter")) != NULL) {
dkim_module_ctx->time_jitter = ucl_obj_todouble (value);
}
else {
dkim_module_ctx->time_jitter = DEFAULT_TIME_JITTER;
}
- if ((value =
- rspamd_config_get_module_opt (cfg, "dkim", "whitelist")) != NULL) {
- if (!add_map (cfg, ucl_obj_tostring (value),
- "DKIM whitelist", read_radix_list, fin_radix_list,
- (void **)&dkim_module_ctx->whitelist_ip)) {
- msg_warn ("cannot load whitelist from %s",
- ucl_obj_tostring (value));
+ if ((value = rspamd_config_get_module_opt (cfg, "dkim", "whitelist")) != NULL) {
+ if (! add_map (cfg, ucl_obj_tostring (value),
+ "DKIM whitelist", read_radix_list, fin_radix_list,
+ (void **)&dkim_module_ctx->whitelist_ip)) {
+ msg_warn ("cannot load whitelist from %s", ucl_obj_tostring (value));
}
}
- if ((value =
- rspamd_config_get_module_opt (cfg, "dkim", "domains")) != NULL) {
- if (!add_map (cfg, ucl_obj_tostring (value),
- "DKIM domains", read_kv_list, fin_kv_list,
- (void **)&dkim_module_ctx->dkim_domains)) {
- msg_warn ("cannot load dkim domains list from %s",
- ucl_obj_tostring (value));
+ if ((value = rspamd_config_get_module_opt (cfg, "dkim", "domains")) != NULL) {
+ if (! add_map (cfg, ucl_obj_tostring (value),
+ "DKIM domains", read_kv_list, fin_kv_list,
+ (void **)&dkim_module_ctx->dkim_domains)) {
+ msg_warn ("cannot load dkim domains list from %s", ucl_obj_tostring (value));
}
else {
got_trusted = TRUE;
}
}
- if ((value =
- rspamd_config_get_module_opt (cfg, "dkim",
- "strict_multiplier")) != NULL) {
+ if ((value = rspamd_config_get_module_opt (cfg, "dkim", "strict_multiplier")) != NULL) {
dkim_module_ctx->strict_multiplier = ucl_obj_toint (value);
}
else {
dkim_module_ctx->strict_multiplier = 1;
}
- if ((value =
- rspamd_config_get_module_opt (cfg, "dkim", "trusted_only")) != NULL) {
+ if ((value = rspamd_config_get_module_opt (cfg, "dkim", "trusted_only")) != NULL) {
dkim_module_ctx->trusted_only = ucl_obj_toboolean (value);
}
else {
dkim_module_ctx->trusted_only = FALSE;
}
- if ((value =
- rspamd_config_get_module_opt (cfg, "dkim", "skip_multi")) != NULL) {
+ if ((value = rspamd_config_get_module_opt (cfg, "dkim", "skip_multi")) != NULL) {
dkim_module_ctx->skip_multi = ucl_obj_toboolean (value);
}
else {
@@ -200,33 +182,19 @@ dkim_module_config (struct rspamd_config *cfg)
}
if (dkim_module_ctx->trusted_only && !got_trusted) {
- msg_err (
- "trusted_only option is set and no trusted domains are defined; disabling dkim module completely as it is useless in this case");
+ msg_err ("trusted_only option is set and no trusted domains are defined; disabling dkim module completely as it is useless in this case");
}
else {
- register_symbol (&cfg->cache,
- dkim_module_ctx->symbol_reject,
- 1,
- dkim_symbol_callback,
- NULL);
- register_virtual_symbol (&cfg->cache,
- dkim_module_ctx->symbol_tempfail,
- 1);
- register_virtual_symbol (&cfg->cache,
- dkim_module_ctx->symbol_allow,
- 1);
-
- dkim_module_ctx->dkim_hash = rspamd_lru_hash_new (rspamd_strcase_hash,
- rspamd_strcase_equal,
- cache_size,
- cache_expire,
- g_free,
- (GDestroyNotify)rspamd_dkim_key_free);
+ register_symbol (&cfg->cache, dkim_module_ctx->symbol_reject, 1, dkim_symbol_callback, NULL);
+ register_virtual_symbol (&cfg->cache, dkim_module_ctx->symbol_tempfail, 1);
+ register_virtual_symbol (&cfg->cache, dkim_module_ctx->symbol_allow, 1);
+
+ dkim_module_ctx->dkim_hash = rspamd_lru_hash_new (rspamd_strcase_hash, rspamd_strcase_equal,
+ cache_size, cache_expire, g_free, (GDestroyNotify)rspamd_dkim_key_free);
#ifndef HAVE_OPENSSL
- msg_warn (
- "openssl is not found so dkim rsa check is disabled, only check body hash, it is NOT safe to trust these results");
+ msg_warn ("openssl is not found so dkim rsa check is disabled, only check body hash, it is NOT safe to trust these results");
#endif
}
@@ -241,8 +209,7 @@ dkim_module_reconfig (struct rspamd_config *cfg)
if (dkim_module_ctx->dkim_domains) {
g_hash_table_destroy (dkim_module_ctx->dkim_domains);
}
- dkim_module_ctx->dkim_pool = rspamd_mempool_new (
- rspamd_mempool_suggest_size ());
+ dkim_module_ctx->dkim_pool = rspamd_mempool_new (rspamd_mempool_suggest_size ());
return dkim_module_config (cfg);
}
@@ -253,14 +220,14 @@ dkim_module_reconfig (struct rspamd_config *cfg)
static gboolean
dkim_module_parse_strict (const gchar *value, gint *allow, gint *deny)
{
- const gchar *colon;
- gulong val;
+ const gchar *colon;
+ gulong val;
colon = strchr (value, ':');
if (colon) {
if (rspamd_strtoul (value, colon - value, &val)) {
*deny = val;
- colon++;
+ colon ++;
if (rspamd_strtoul (colon, strlen (colon), &val)) {
*allow = val;
return TRUE;
@@ -271,38 +238,25 @@ dkim_module_parse_strict (const gchar *value, gint *allow, gint *deny)
}
static void
-dkim_module_check (struct rspamd_task *task,
- rspamd_dkim_context_t *ctx,
- rspamd_dkim_key_t *key)
+dkim_module_check (struct rspamd_task *task, rspamd_dkim_context_t *ctx, rspamd_dkim_key_t *key)
{
- gint res, score_allow = 1, score_deny = 1;
- const gchar *strict_value;
+ gint res, score_allow = 1, score_deny = 1;
+ const gchar *strict_value;
- msg_debug ("check dkim signature for %s domain from %s",
- ctx->domain,
- ctx->dns_key);
+ msg_debug ("check dkim signature for %s domain from %s", ctx->domain, ctx->dns_key);
res = rspamd_dkim_check (ctx, key, task);
if (dkim_module_ctx->dkim_domains != NULL) {
/* Perform strict check */
- if ((strict_value =
- g_hash_table_lookup (dkim_module_ctx->dkim_domains,
- ctx->domain)) != NULL) {
- if (!dkim_module_parse_strict (strict_value, &score_allow,
- &score_deny)) {
+ if ((strict_value = g_hash_table_lookup (dkim_module_ctx->dkim_domains, ctx->domain)) != NULL) {
+ if (!dkim_module_parse_strict (strict_value, &score_allow, &score_deny)) {
score_allow = dkim_module_ctx->strict_multiplier;
score_deny = dkim_module_ctx->strict_multiplier;
- msg_debug (
- "no specific score found for %s domain, using %d for it",
- ctx->domain,
- score_deny);
+ msg_debug ("no specific score found for %s domain, using %d for it", ctx->domain, score_deny);
}
else {
- msg_debug (
- "specific score found for %s domain: using %d for deny and %d for allow",
- ctx->dns_key,
- score_deny,
- score_allow);
+ msg_debug ("specific score found for %s domain: using %d for deny and %d for allow",
+ ctx->dns_key, score_deny, score_allow);
}
}
}
@@ -319,20 +273,15 @@ dkim_module_check (struct rspamd_task *task,
}
static void
-dkim_module_key_handler (rspamd_dkim_key_t *key,
- gsize keylen,
- rspamd_dkim_context_t *ctx,
- gpointer ud,
- GError *err)
+dkim_module_key_handler (rspamd_dkim_key_t *key, gsize keylen, rspamd_dkim_context_t *ctx, gpointer ud, GError *err)
{
- struct rspamd_task *task = ud;
+ struct rspamd_task *task = ud;
if (key != NULL) {
/* Add new key to the lru cache */
- rspamd_lru_hash_insert (dkim_module_ctx->dkim_hash,
- g_strdup (ctx->dns_key),
- key, task->tv.tv_sec, key->ttl);
+ rspamd_lru_hash_insert (dkim_module_ctx->dkim_hash, g_strdup (ctx->dns_key),
+ key, task->tv.tv_sec, key->ttl);
dkim_module_check (task, ctx, key);
}
else {
@@ -340,8 +289,7 @@ dkim_module_key_handler (rspamd_dkim_key_t *key,
msg_info ("cannot get key for domain %s", ctx->dns_key);
if (err != NULL) {
insert_result (task, dkim_module_ctx->symbol_tempfail, 1,
- g_list_prepend (NULL,
- rspamd_mempool_strdup (task->task_pool, err->message)));
+ g_list_prepend (NULL, rspamd_mempool_strdup (task->task_pool, err->message)));
}
else {
@@ -357,23 +305,19 @@ dkim_module_key_handler (rspamd_dkim_key_t *key,
static void
dkim_symbol_callback (struct rspamd_task *task, void *unused)
{
- GList *hlist;
- rspamd_dkim_context_t *ctx;
- rspamd_dkim_key_t *key;
- GError *err = NULL;
+ GList *hlist;
+ rspamd_dkim_context_t *ctx;
+ rspamd_dkim_key_t *key;
+ GError *err = NULL;
/* First check if a message has its signature */
- hlist = message_get_header (task->task_pool,
- task->message,
- DKIM_SIGNHEADER,
- FALSE);
+ hlist = message_get_header (task->task_pool, task->message, DKIM_SIGNHEADER, FALSE);
if (hlist != NULL) {
/* Check whitelist */
msg_debug ("dkim signature found");
if (task->from_addr.af == AF_INET ||
- radix32tree_find (dkim_module_ctx->whitelist_ip,
- ntohl (task->from_addr.addr.s4.sin_addr.s_addr)) ==
- RADIX_NO_VALUE) {
+ radix32tree_find (dkim_module_ctx->whitelist_ip,
+ ntohl (task->from_addr.addr.s4.sin_addr.s_addr)) == RADIX_NO_VALUE) {
/* Parse signature */
msg_debug ("create dkim signature");
/*
@@ -382,45 +326,32 @@ dkim_symbol_callback (struct rspamd_task *task, void *unused)
*/
if (dkim_module_ctx->skip_multi) {
if (hlist->next != NULL) {
- msg_info (
- "<%s> skip dkim check as it has several dkim signatures",
- task->message_id);
+ msg_info ("<%s> skip dkim check as it has several dkim signatures", task->message_id);
return;
}
}
hlist = g_list_last (hlist);
- ctx = rspamd_create_dkim_context (hlist->data,
- task->task_pool,
- dkim_module_ctx->time_jitter,
- &err);
+ ctx = rspamd_create_dkim_context (hlist->data, task->task_pool, dkim_module_ctx->time_jitter, &err);
if (ctx == NULL) {
msg_info ("cannot parse DKIM context: %s", err->message);
g_error_free (err);
}
else {
/* Get key */
- if (dkim_module_ctx->trusted_only &&
- (dkim_module_ctx->dkim_domains == NULL ||
- g_hash_table_lookup (dkim_module_ctx->dkim_domains,
- ctx->domain) == NULL)) {
+ if (dkim_module_ctx->trusted_only && (dkim_module_ctx->dkim_domains == NULL ||
+ g_hash_table_lookup (dkim_module_ctx->dkim_domains, ctx->domain) == NULL)) {
msg_debug ("skip dkim check for %s domain", ctx->domain);
return;
}
- key = rspamd_lru_hash_lookup (dkim_module_ctx->dkim_hash,
- ctx->dns_key,
- task->tv.tv_sec);
+ key = rspamd_lru_hash_lookup (dkim_module_ctx->dkim_hash, ctx->dns_key, task->tv.tv_sec);
if (key != NULL) {
debug_task ("found key for %s in cache", ctx->dns_key);
dkim_module_check (task, ctx, key);
}
else {
debug_task ("request key for %s from DNS", ctx->dns_key);
- task->dns_requests++;
- rspamd_get_dkim_key (ctx,
- task->resolver,
- task->s,
- dkim_module_key_handler,
- task);
+ task->dns_requests ++;
+ rspamd_get_dkim_key (ctx, task->resolver, task->s, dkim_module_key_handler, task);
}
}
}