From 1cc29aa2341dd04ce71912c824c53cc096e9bdba Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 11 Jul 2024 13:47:05 +0100 Subject: [Minor] Fix logic --- src/libserver/cfg_utils.cxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/libserver') diff --git a/src/libserver/cfg_utils.cxx b/src/libserver/cfg_utils.cxx index dd85eddfb..bcd26d59c 100644 --- a/src/libserver/cfg_utils.cxx +++ b/src/libserver/cfg_utils.cxx @@ -928,9 +928,9 @@ rspamd_config_post_load(struct rspamd_config *cfg, if (opts & RSPAMD_CONFIG_INIT_LIBS) { /* Config other libraries */ - ret = rspamd_config_libs(cfg->libs_ctx, cfg) && ret; + auto libs_ret = rspamd_config_libs(cfg->libs_ctx, cfg); - if (!ret) { + if (!libs_ret) { msg_err_config("cannot configure libraries, fatal error"); return FALSE; } -- cgit v1.2.3 From 77d78f6ab7cf5590930c46d6d71c7345f6422fad Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 11 Jul 2024 14:03:40 +0100 Subject: [Minor] Specify failure reason clearly --- src/libserver/cfg_utils.cxx | 34 +++++++++++++++++++++++++--------- src/rspamd.c | 4 ++++ 2 files changed, 29 insertions(+), 9 deletions(-) (limited to 'src/libserver') diff --git a/src/libserver/cfg_utils.cxx b/src/libserver/cfg_utils.cxx index bcd26d59c..afa4009cc 100644 --- a/src/libserver/cfg_utils.cxx +++ b/src/libserver/cfg_utils.cxx @@ -57,7 +57,7 @@ #ifdef HAVE_SYS_RESOURCE_H #include #endif -#include +#include #include "libserver/composites/composites.h" #include "blas-config.h" @@ -69,6 +69,7 @@ #include "cxx/util.hxx" #include "frozen/unordered_map.h" #include "frozen/string.h" +#include "contrib/expected/expected.hpp" #include "contrib/ankerl/unordered_dense.h" #define DEFAULT_SCORE 10.0 @@ -826,8 +827,7 @@ gboolean rspamd_config_post_load(struct rspamd_config *cfg, enum rspamd_post_load_options opts) { - - auto ret = TRUE; + auto ret = tl::expected{}; rspamd_adjust_clocks_resolution(cfg); rspamd_logger_configure_modules(cfg->debug_modules); @@ -867,14 +867,15 @@ rspamd_config_post_load(struct rspamd_config *cfg, else { if (opts & RSPAMD_CONFIG_INIT_VALIDATE) { msg_err_config("no url_tld option has been specified"); - ret = FALSE; + ret = tl::make_unexpected(std::string{"no url_tld option has been specified"}); } } } else { if (access(cfg->tld_file, R_OK) == -1) { if (opts & RSPAMD_CONFIG_INIT_VALIDATE) { - ret = FALSE; + ret = tl::make_unexpected(fmt::format("cannot access tld file {}: {}", + cfg->tld_file, strerror(errno))); msg_err_config("cannot access tld file %s: %s", cfg->tld_file, strerror(errno)); } @@ -905,13 +906,17 @@ rspamd_config_post_load(struct rspamd_config *cfg, if (!rspamd_config_parse_log_format(cfg)) { msg_err_config("cannot parse log format, task logging will not be available"); if (opts & RSPAMD_CONFIG_INIT_VALIDATE) { - ret = FALSE; + ret = tl::make_unexpected(std::string{"cannot parse log format"}); } } if (opts & RSPAMD_CONFIG_INIT_SYMCACHE) { /* Init config cache */ - ret = rspamd_symcache_init(cfg->cache) && ret; + auto symcache_ret = rspamd_symcache_init(cfg->cache); + + if (!symcache_ret) { + ret = tl::make_unexpected(std::string{"symcache init failed"}); + } /* Init re cache */ rspamd_re_cache_init(cfg->re_cache, cfg); @@ -959,7 +964,11 @@ rspamd_config_post_load(struct rspamd_config *cfg, " Rspamd features will be broken"); } - ret = rspamd_symcache_validate(cfg->cache, cfg, FALSE) && ret; + auto val_ret = rspamd_symcache_validate(cfg->cache, cfg, FALSE); + + if (!val_ret) { + ret = tl::make_unexpected(std::string{"symcache validation failed"}); + } } if (opts & RSPAMD_CONFIG_INIT_POST_LOAD_LUA) { @@ -970,7 +979,14 @@ rspamd_config_post_load(struct rspamd_config *cfg, rspamd_map_preload(cfg); } - return ret; + if (ret) { + return true; + } + else { + msg_err_config("error on post-init stage: %s", ret.error().c_str()); + + return false; + } } struct rspamd_classifier_config * diff --git a/src/rspamd.c b/src/rspamd.c index 117f3b995..b6c361cb2 100644 --- a/src/rspamd.c +++ b/src/rspamd.c @@ -979,12 +979,16 @@ load_rspamd_config(struct rspamd_main *rspamd_main, if (init_modules) { if (!rspamd_init_filters(cfg, reload, false)) { + msg_err_main("init filters failed"); + return FALSE; } } /* Do post-load actions */ if (!rspamd_config_post_load(cfg, opts)) { + msg_err_main("post load failed"); + return FALSE; } } -- cgit v1.2.3 From 47c7cf97134d9c828c2dc859e55ce5594a48e319 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 11 Jul 2024 14:13:26 +0100 Subject: [Minor] cmath is a bit more strict --- src/libserver/cfg_utils.cxx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/libserver') diff --git a/src/libserver/cfg_utils.cxx b/src/libserver/cfg_utils.cxx index afa4009cc..1344bc4f9 100644 --- a/src/libserver/cfg_utils.cxx +++ b/src/libserver/cfg_utils.cxx @@ -136,14 +136,14 @@ struct rspamd_actions_list { void sort() { std::sort(actions.begin(), actions.end(), [](const action_ptr &a1, const action_ptr &a2) -> bool { - if (!isnan(a1->threshold) && !isnan(a2->threshold)) { + if (!std::isnan(a1->threshold) && !std::isnan(a2->threshold)) { return a1->threshold < a2->threshold; } - if (isnan(a1->threshold) && isnan(a2->threshold)) { + if (std::isnan(a1->threshold) && std::isnan(a2->threshold)) { return false; } - else if (isnan(a1->threshold)) { + else if (std::isnan(a1->threshold)) { return true; } @@ -1541,7 +1541,7 @@ rspamd_config_new_symbol(struct rspamd_config *cfg, const char *symbol, rspamd_mempool_alloc0_type(cfg->cfg_pool, struct rspamd_symbol); score_ptr = rspamd_mempool_alloc_type(cfg->cfg_pool, double); - if (isnan(score)) { + if (std::isnan(score)) { /* In fact, it could be defined later */ msg_debug_config("score is not defined for symbol %s, set it to zero", symbol); @@ -1652,7 +1652,7 @@ rspamd_config_add_symbol(struct rspamd_config *cfg, } if (sym_def->priority > priority && - (isnan(score) || !(sym_def->flags & RSPAMD_SYMBOL_FLAG_UNSCORED))) { + (std::isnan(score) || !(sym_def->flags & RSPAMD_SYMBOL_FLAG_UNSCORED))) { msg_debug_config("symbol %s has been already registered with " "priority %ud, do not override (new priority: %ud)", symbol, @@ -1673,7 +1673,7 @@ rspamd_config_add_symbol(struct rspamd_config *cfg, } else { - if (!isnan(score)) { + if (!std::isnan(score)) { msg_debug_config("symbol %s has been already registered with " "priority %ud, override it with new priority: %ud, " "old score: %.2f, new score: %.2f", @@ -2013,7 +2013,7 @@ rspamd_config_action_from_ucl(struct rspamd_config *cfg, /* TODO: add lua references support */ - if (isnan(threshold) && !(flags & RSPAMD_ACTION_NO_THRESHOLD)) { + if (std::isnan(threshold) && !(flags & RSPAMD_ACTION_NO_THRESHOLD)) { msg_err_config("action %s has no threshold being set and it is not" " a no threshold action", act->name); -- cgit v1.2.3 From f8b0fd41a3b6335d86e9fa3ee25e1e5b12df1c7b Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 17 Jul 2024 15:35:18 +0100 Subject: [Feature] Treat SPF +all in a special way Issue: #4996 --- src/libserver/spf.c | 2 +- src/libserver/spf.h | 1 + src/lua/lua_spf.c | 2 ++ src/plugins/lua/spf.lua | 1 + 4 files changed, 5 insertions(+), 1 deletion(-) (limited to 'src/libserver') diff --git a/src/libserver/spf.c b/src/libserver/spf.c index 32c020bf3..afd77294b 100644 --- a/src/libserver/spf.c +++ b/src/libserver/spf.c @@ -1418,7 +1418,7 @@ parse_spf_all(struct spf_record *rec, struct spf_addr *addr) /* Disallow +all */ if (addr->mech == SPF_PASS) { - addr->flags |= RSPAMD_SPF_FLAG_INVALID; + addr->flags |= RSPAMD_SPF_FLAG_INVALID | RSPAMD_SPF_FLAG_PLUSALL; msg_notice_spf("domain %s allows any SPF (+all), ignore SPF record completely", rec->sender_domain); } diff --git a/src/libserver/spf.h b/src/libserver/spf.h index cc0ee4c05..2487b6d57 100644 --- a/src/libserver/spf.h +++ b/src/libserver/spf.h @@ -77,6 +77,7 @@ typedef enum spf_action_e { #define RSPAMD_SPF_FLAG_PERMFAIL (1u << 10u) #define RSPAMD_SPF_FLAG_RESOLVED (1u << 11u) #define RSPAMD_SPF_FLAG_CACHED (1u << 12u) +#define RSPAMD_SPF_FLAG_PLUSALL (1u << 13u) /** Default SPF limits for avoiding abuse **/ #define SPF_MAX_NESTING 10 diff --git a/src/lua/lua_spf.c b/src/lua/lua_spf.c index 46e72202f..a9bcfc80c 100644 --- a/src/lua/lua_spf.c +++ b/src/lua/lua_spf.c @@ -89,6 +89,8 @@ lua_load_spf(lua_State *L) lua_setfield(L, -2, "perm_fail"); lua_pushinteger(L, RSPAMD_SPF_FLAG_CACHED); lua_setfield(L, -2, "cached"); + lua_pushinteger(L, RSPAMD_SPF_FLAG_PLUSALL); + lua_setfield(L, -2, "plusall"); lua_setfield(L, -2, "flags"); diff --git a/src/plugins/lua/spf.lua b/src/plugins/lua/spf.lua index 48f3c17be..b9add61a6 100644 --- a/src/plugins/lua/spf.lua +++ b/src/plugins/lua/spf.lua @@ -56,6 +56,7 @@ local symbols = { dnsfail = "R_SPF_DNSFAIL", permfail = "R_SPF_PERMFAIL", na = "R_SPF_NA", + plusall = "R_SPF_PLUSALL", } local default_config = { -- cgit v1.2.3 From 02e433ff1f9fa327387a7d0453e3e851776fd160 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 18 Jul 2024 13:26:26 +0100 Subject: [Minor] Fix several issues with flag propagation --- src/libserver/spf.c | 5 ++++- src/libserver/spf.h | 1 + src/lua/lua_spf.c | 7 ++++++- src/plugins/lua/spf.lua | 2 ++ 4 files changed, 13 insertions(+), 2 deletions(-) (limited to 'src/libserver') diff --git a/src/libserver/spf.c b/src/libserver/spf.c index afd77294b..562222042 100644 --- a/src/libserver/spf.c +++ b/src/libserver/spf.c @@ -451,6 +451,9 @@ rspamd_spf_process_reference(struct spf_resolved *target, target->flags |= RSPAMD_SPF_RESOLVED_NA; continue; } + if (cur->flags & RSPAMD_SPF_FLAG_PLUSALL) { + target->flags |= RSPAMD_SPF_RESOLVED_PLUSALL; + } if (cur->flags & RSPAMD_SPF_FLAG_INVALID) { /* Ignore invalid elements */ continue; @@ -1418,7 +1421,7 @@ parse_spf_all(struct spf_record *rec, struct spf_addr *addr) /* Disallow +all */ if (addr->mech == SPF_PASS) { - addr->flags |= RSPAMD_SPF_FLAG_INVALID | RSPAMD_SPF_FLAG_PLUSALL; + addr->flags |= RSPAMD_SPF_FLAG_PLUSALL; msg_notice_spf("domain %s allows any SPF (+all), ignore SPF record completely", rec->sender_domain); } diff --git a/src/libserver/spf.h b/src/libserver/spf.h index 2487b6d57..b89dc4d0e 100644 --- a/src/libserver/spf.h +++ b/src/libserver/spf.h @@ -105,6 +105,7 @@ enum rspamd_spf_resolved_flags { RSPAMD_SPF_RESOLVED_TEMP_FAILED = (1u << 0u), RSPAMD_SPF_RESOLVED_PERM_FAILED = (1u << 1u), RSPAMD_SPF_RESOLVED_NA = (1u << 2u), + RSPAMD_SPF_RESOLVED_PLUSALL = (1u << 3u), }; struct spf_resolved { diff --git a/src/lua/lua_spf.c b/src/lua/lua_spf.c index a9bcfc80c..850ce2120 100644 --- a/src/lua/lua_spf.c +++ b/src/lua/lua_spf.c @@ -89,7 +89,7 @@ lua_load_spf(lua_State *L) lua_setfield(L, -2, "perm_fail"); lua_pushinteger(L, RSPAMD_SPF_FLAG_CACHED); lua_setfield(L, -2, "cached"); - lua_pushinteger(L, RSPAMD_SPF_FLAG_PLUSALL); + lua_pushinteger(L, RSPAMD_SPF_RESOLVED_PLUSALL); lua_setfield(L, -2, "plusall"); lua_setfield(L, -2, "flags"); @@ -370,6 +370,11 @@ spf_check_element(lua_State *L, struct spf_resolved *rec, struct spf_addr *addr, lua_pushinteger(L, RSPAMD_SPF_RESOLVED_TEMP_FAILED); lua_pushfstring(L, "%cany", spf_mech_char(addr->mech)); } + else if (rec->flags & RSPAMD_SPF_RESOLVED_PLUSALL) { + lua_pushboolean(L, false); + lua_pushinteger(L, RSPAMD_SPF_RESOLVED_PLUSALL); + lua_pushfstring(L, "%cany", spf_mech_char(addr->mech)); + } else { lua_pushboolean(L, true); lua_pushinteger(L, addr->mech); diff --git a/src/plugins/lua/spf.lua b/src/plugins/lua/spf.lua index b9add61a6..356507250 100644 --- a/src/plugins/lua/spf.lua +++ b/src/plugins/lua/spf.lua @@ -119,6 +119,8 @@ local function spf_check_callback(task) local function flag_to_symbol(fl) if bit.band(fl, rspamd_spf.flags.temp_fail) ~= 0 then return local_config.symbols.dnsfail + elseif bit.band(fl, rspamd_spf.flags.plusall) ~= 0 then + return local_config.symbols.plusall elseif bit.band(fl, rspamd_spf.flags.perm_fail) ~= 0 then return local_config.symbols.permfail elseif bit.band(fl, rspamd_spf.flags.na) ~= 0 then -- cgit v1.2.3 From 5a2dedd00eb68a5d4c7acda93fd3cc4f3bb2d358 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sun, 28 Jul 2024 08:45:26 +0100 Subject: [Minor] Try to fix ambigious rvalue --- src/libserver/cfg_rcl.cxx | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/libserver') diff --git a/src/libserver/cfg_rcl.cxx b/src/libserver/cfg_rcl.cxx index ce3df4010..8a479fa6d 100644 --- a/src/libserver/cfg_rcl.cxx +++ b/src/libserver/cfg_rcl.cxx @@ -3623,7 +3623,8 @@ rspamd_config_parse_ucl(struct rspamd_config *cfg, auto &cfg_file = cfg_file_maybe.value(); /* Try to load keyfile if available */ - rspamd::util::raii_file::open(fmt::format("{}.key", filename), O_RDONLY).map([&](const auto &keyfile) { + auto keyfile_name = fmt::format("{}.key", filename); + rspamd::util::raii_file::open(keyfile_name, O_RDONLY).map([&](const auto &keyfile) { auto *kp_parser = ucl_parser_new(0); if (ucl_parser_add_fd(kp_parser, keyfile.get_fd())) { auto *kp_obj = ucl_parser_get_object(kp_parser); @@ -3632,8 +3633,8 @@ rspamd_config_parse_ucl(struct rspamd_config *cfg, decrypt_keypair = rspamd_keypair_from_ucl(kp_obj); if (decrypt_keypair == nullptr) { - msg_err_config_forced("cannot load keypair from %s.key: invalid keypair", - filename); + msg_err_config_forced("cannot load keypair from %s: invalid keypair", + keyfile_name.c_str()); } else { /* Add decryption support to UCL */ @@ -3645,8 +3646,8 @@ rspamd_config_parse_ucl(struct rspamd_config *cfg, ucl_object_unref(kp_obj); } else { - msg_err_config_forced("cannot load keypair from %s.key: %s", - filename, ucl_parser_get_error(kp_parser)); + msg_err_config_forced("cannot load keypair from %s: %s", + keyfile_name.c_str(), ucl_parser_get_error(kp_parser)); } ucl_parser_free(kp_parser); }); -- cgit v1.2.3