Skip to content

Commit

Permalink
[Rework] Some final fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vstakhov committed Aug 16, 2023
1 parent 9ea3d1a commit b9dd512
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 30 deletions.
42 changes: 28 additions & 14 deletions src/libserver/cfg_rcl.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
27 changes: 18 additions & 9 deletions src/libserver/cfg_utils.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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 */
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/libserver/symcache/symcache_impl.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions src/libutil/addr.c
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit b9dd512

Please sign in to comment.