]> source.dussan.org Git - rspamd.git/commitdiff
[Rework] Some final fixes
authorVsevolod Stakhov <vsevolod@rspamd.com>
Wed, 16 Aug 2023 15:39:20 +0000 (16:39 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Wed, 16 Aug 2023 15:39:20 +0000 (16:39 +0100)
src/libserver/cfg_rcl.cxx
src/libserver/cfg_utils.cxx
src/libserver/symcache/symcache_impl.cxx
src/libutil/addr.c

index 1ca9e5b693208ae4cc5636e123401f58f39b3e2c..3f596c44c26aab7ae397569e0d64dbc519a695ef 100644 (file)
@@ -36,6 +36,8 @@
 #include "fmt/core.h"
 #include "libutil/cxx/util.hxx"
 #include "libutil/cxx/file_util.hxx"
+#include "frozen/unordered_set.h"
+#include "frozen/string.h"
 
 #ifdef HAVE_SYSLOG_H
 #include <syslog.h>
@@ -701,7 +703,15 @@ rspamd_rcl_actions_handler(rspamd_mempool_t *pool, const ucl_object_t *obj,
 
        return rspamd_rcl_section_parse_defaults(cfg, *section, pool, obj, cfg, err);
 }
-
+constexpr const auto known_worker_attributes = frozen::make_unordered_set<frozen::string>({
+       "bind_socket",
+       "listen",
+       "bind",
+       "count",
+       "max_files",
+       "max_core",
+       "enabled",
+});
 static gboolean
 rspamd_rcl_worker_handler(rspamd_mempool_t *pool, const ucl_object_t *obj,
                                                  const gchar *key, gpointer ud,
@@ -800,7 +810,7 @@ rspamd_rcl_worker_handler(rspamd_mempool_t *pool, const ucl_object_t *obj,
                                        }
                                }
                        }
-                       else {
+                       else if (known_worker_attributes.find(std::string_view{ucl_object_key(cur)}) == known_worker_attributes.end()) {
                                msg_warn_config("unknown worker attribute: %s; worker type: %s", ucl_object_key(cur), worker_type);
                        }
                }
@@ -834,14 +844,14 @@ rspamd_rcl_lua_handler(rspamd_mempool_t *pool, const ucl_object_t *obj,
        auto *cfg = static_cast<rspamd_config *>(ud);
        auto lua_src = fs::path{ucl_object_tostring(obj)};
        auto *L = RSPAMD_LUA_CFG_STATE(cfg);
-       std::error_code ec1, ec2;
+       std::error_code ec1;
 
        auto lua_dir = fs::weakly_canonical(lua_src.parent_path(), ec1);
-       auto lua_file = fs::weakly_canonical(lua_src.filename(), ec2);
+       auto lua_file = lua_src.filename();
 
-       if (ec1 && ec2 && !lua_dir.empty() && !lua_file.empty()) {
+       if (!ec1 && !lua_dir.empty() && !lua_file.empty()) {
                auto cur_dir = fs::current_path(ec1);
-               if (ec1 && !cur_dir.empty() && ::chdir(lua_dir.c_str()) != -1) {
+               if (!ec1 && !cur_dir.empty() && ::chdir(lua_dir.c_str()) != -1) {
                        /* Push traceback function */
                        lua_pushcfunction(L, &rspamd_lua_traceback);
                        auto err_idx = lua_gettop(L);
@@ -886,13 +896,13 @@ rspamd_rcl_lua_handler(rspamd_mempool_t *pool, const ucl_object_t *obj,
                        g_set_error(err, CFG_RCL_ERROR, ENOENT, "cannot chdir to %s: %s",
                                                lua_dir.c_str(), strerror(errno));
                        if (::chdir(cur_dir.c_str()) == -1) {
-                               msg_err_config("cannot chdir to %s: %s", cur_dir.c_str(), strerror(errno));
+                               msg_err_config("cannot chdir back to %s: %s", cur_dir.c_str(), strerror(errno));
                        }
 
                        return FALSE;
                }
                if (::chdir(cur_dir.c_str()) == -1) {
-                       msg_err_config("cannot chdir to %s: %s", cur_dir.c_str(), strerror(errno));
+                       msg_err_config("cannot chdir back to %s: %s", cur_dir.c_str(), strerror(errno));
                }
        }
        else {
@@ -918,12 +928,17 @@ rspamd_rcl_add_lua_plugins_path(struct rspamd_rcl_sections_map *sections,
 
        auto add_single_file = [&](const fs::path &fpath) -> bool {
                auto fname = fpath.filename();
+               auto modname = fname.string();
+
+               if (fname.has_extension()) {
+                       modname = modname.substr(0, modname.size() - fname.extension().native().size());
+               }
                auto *cur_mod = rspamd_mempool_alloc_type(cfg->cfg_pool,
                                                                                                  struct script_module);
                cur_mod->path = rspamd_mempool_strdup(cfg->cfg_pool, fpath.c_str());
-               cur_mod->name = rspamd_mempool_strdup(cfg->cfg_pool, fname.c_str());
+               cur_mod->name = rspamd_mempool_strdup(cfg->cfg_pool, modname.c_str());
 
-               if (sections->lua_modules_seen.contains(fname.string())) {
+               if (sections->lua_modules_seen.contains(modname)) {
                        msg_info_config("already seen module %s, skip %s",
                                                        cur_mod->name, cur_mod->path);
                        return false;
@@ -967,7 +982,7 @@ rspamd_rcl_add_lua_plugins_path(struct rspamd_rcl_sections_map *sections,
        else {
                /* Handle directory */
                for (const auto &p: fs::recursive_directory_iterator(dir, ec)) {
-                       auto fpath = std::string_view{p.path().string()};
+                       auto fpath = p.path().string();
                        if (p.is_regular_file() && fpath.ends_with(".lua")) {
                                add_single_file(p.path());
                        }
@@ -2462,10 +2477,9 @@ rspamd_rcl_process_section(struct rspamd_config *cfg,
                /* Section has been already processed */
                return TRUE;
        }
+
        g_assert(obj != nullptr);
        g_assert(sec.handler != nullptr);
-       /* Set processed flag */
-       sec.processed = true;
 
        if (sec.key_attr) {
                it = ucl_object_iterate_new(obj);
@@ -3278,7 +3292,7 @@ void rspamd_rcl_register_worker_option(struct rspamd_config *cfg,
                return;
        }
 
-       auto nhandler = handler_it.first->second;
+       auto &nhandler = handler_it.first->second;
        nhandler.parser.flags = flags;
        nhandler.parser.offset = offset;
        nhandler.parser.user_struct = target;
index a088559f1b5b472cdbac8a4dd820549d0831e2ee..fc3564954da05fd7a7360697c469b0ed1ea1591e 100644 (file)
@@ -538,6 +538,7 @@ constexpr const auto config_vars = frozen::make_unordered_map<frozen::string, st
        {"symbols_scores_params", {RSPAMD_LOG_SYMBOLS, RSPAMD_LOG_FMT_FLAG_SYMBOLS_PARAMS | RSPAMD_LOG_FMT_FLAG_SYMBOLS_SCORES}},
        {"groups", {RSPAMD_LOG_GROUPS, 0}},
        {"public_groups", {RSPAMD_LOG_PUBLIC_GROUPS, 0}},
+       {"is_spam", {RSPAMD_LOG_ISSPAM, 0}},
 });
 
 static gboolean
@@ -913,7 +914,7 @@ rspamd_config_post_load(struct rspamd_config *cfg,
 
        if (opts & RSPAMD_CONFIG_INIT_SYMCACHE) {
                /* Init config cache */
-               ret &= rspamd_symcache_init(cfg->cache);
+               ret = rspamd_symcache_init(cfg->cache) && ret;
 
                /* Init re cache */
                rspamd_re_cache_init(cfg->re_cache, cfg);
@@ -930,7 +931,12 @@ 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 = rspamd_config_libs(cfg->libs_ctx, cfg) && ret;
+
+               if (!ret) {
+                       msg_err_config("cannot configure libraries, fatal error");
+                       return FALSE;
+               }
        }
 
        /* Validate cache */
@@ -956,7 +962,7 @@ rspamd_config_post_load(struct rspamd_config *cfg,
                                                        " Rspamd features will be broken");
                }
 
-               ret &= rspamd_symcache_validate(cfg->cache, cfg, FALSE);
+               ret = rspamd_symcache_validate(cfg->cache, cfg, FALSE) && ret;
        }
 
        if (opts & RSPAMD_CONFIG_INIT_POST_LOAD_LUA) {
@@ -1049,11 +1055,6 @@ rspamd_worker_conf_dtor(struct rspamd_worker_conf *wcf)
        if (wcf) {
                struct rspamd_worker_bind_conf *cnf, *tmp;
 
-               LL_FOREACH_SAFE(wcf->bind_conf, cnf, tmp)
-               {
-                       g_ptr_array_free(cnf->addrs, TRUE);
-               }
-
                ucl_object_unref(wcf->options);
                g_queue_free(wcf->active_workers);
                g_hash_table_unref(wcf->params);
@@ -2733,11 +2734,19 @@ rspamd_config_libs(struct rspamd_external_libs_ctx *ctx,
 
        if (ctx != nullptr) {
                if (cfg->local_addrs) {
+                       GError *err = nullptr;
                        ret = rspamd_config_radix_from_ucl(cfg, cfg->local_addrs,
                                                                                           "Local addresses",
                                                                                           (struct rspamd_radix_map_helper **) ctx->local_addrs,
-                                                                                          nullptr,
+                                                                                          &err,
                                                                                           nullptr, "local addresses");
+
+                       if (!ret) {
+                               msg_err_config("cannot load local addresses: %e", err);
+                               g_error_free(err);
+
+                               return ret;
+                       }
                }
 
                rspamd_free_zstd_dictionary(ctx->in_dict);
index 5e0612587cf2b95ea2b57316ce22ccabac389411..6fd705be156001b676ce5d75242785661d2365e8 100644 (file)
@@ -42,7 +42,7 @@ auto symcache::init() -> bool
 
        if (cfg->cache_filename != nullptr) {
                msg_debug_cache("loading symcache saved data from %s", cfg->cache_filename);
-               res = load_items();
+               load_items();
        }
 
        ankerl::unordered_dense::set<int> disabled_ids;
index b54bd553949058e7c31248e7283fbc055326f8ec..e011c991a42adb892c9e1460c978e613a7d4a853 100644 (file)
@@ -1,11 +1,11 @@
-/*-
- * Copyright 2016 Vsevolod Stakhov
+/*
+ * Copyright 2023 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ *    http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -1477,7 +1477,7 @@ rspamd_parse_host_port_priority(const gchar *str,
 
                if (*addrs == NULL) {
                        *addrs = g_ptr_array_new_full(1,
-                                                                                 (GDestroyNotify) rspamd_inet_address_free);
+                                                                                 pool == NULL ? NULL : (GDestroyNotify) rspamd_inet_address_free);
 
                        if (pool != NULL) {
                                rspamd_mempool_add_destructor(pool,
@@ -1486,7 +1486,7 @@ rspamd_parse_host_port_priority(const gchar *str,
                }
 
                if (v4_any) {
-                       cur_addr = rspamd_inet_addr_create(AF_INET, pool);
+                       cur_addr = rspamd_inet_addr_create(AF_INET, NULL);
                        rspamd_parse_inet_address_ip4("0.0.0.0",
                                                                                  sizeof("0.0.0.0") - 1, &su.s4.sin_addr);
                        memcpy(&cur_addr->u.in.addr.s4.sin_addr, &su.s4.sin_addr,
@@ -1496,7 +1496,7 @@ rspamd_parse_host_port_priority(const gchar *str,
                        g_ptr_array_add(*addrs, cur_addr);
                }
                if (v6_any) {
-                       cur_addr = rspamd_inet_addr_create(AF_INET6, pool);
+                       cur_addr = rspamd_inet_addr_create(AF_INET6, NULL);
                        rspamd_parse_inet_address_ip6("::",
                                                                                  sizeof("::") - 1, &su.s6.sin6_addr);
                        memcpy(&cur_addr->u.in.addr.s6.sin6_addr, &su.s6.sin6_addr,