From 5bd3e828708d3f1d0e576ebb70a0fb5397528dde Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sun, 24 Apr 2022 12:42:13 +0100 Subject: [PATCH] [Project] Few more methods --- src/libserver/symcache/symcache_c.cxx | 71 +++++++++++++-------- src/libserver/symcache/symcache_item.cxx | 2 +- src/libserver/symcache/symcache_item.hxx | 4 +- src/libserver/symcache/symcache_runtime.cxx | 42 ++++++++++-- src/libserver/symcache/symcache_runtime.hxx | 9 +++ 5 files changed, 96 insertions(+), 32 deletions(-) diff --git a/src/libserver/symcache/symcache_c.cxx b/src/libserver/symcache/symcache_c.cxx index cb8a8af14..a559c3564 100644 --- a/src/libserver/symcache/symcache_c.cxx +++ b/src/libserver/symcache/symcache_c.cxx @@ -165,15 +165,15 @@ rspamd_symcache_validate(struct rspamd_symcache *cache, } ucl_object_t * -rspamd_symcache_counters (struct rspamd_symcache *cache) +rspamd_symcache_counters(struct rspamd_symcache *cache) { auto *real_cache = C_API_SYMCACHE(cache); return real_cache->counters(); } void * -rspamd_symcache_start_refresh (struct rspamd_symcache *cache, - struct ev_loop *ev_base, struct rspamd_worker *w) +rspamd_symcache_start_refresh(struct rspamd_symcache *cache, + struct ev_loop *ev_base, struct rspamd_worker *w) { auto *real_cache = C_API_SYMCACHE(cache); return new rspamd::symcache::cache_refresh_cbdata{real_cache, ev_base, w}; @@ -187,16 +187,16 @@ rspamd_symcache_inc_frequency(struct rspamd_symcache *_cache, struct rspamd_symc } void -rspamd_symcache_add_delayed_dependency (struct rspamd_symcache *cache, - const gchar *from, const gchar *to) +rspamd_symcache_add_delayed_dependency(struct rspamd_symcache *cache, + const gchar *from, const gchar *to) { auto *real_cache = C_API_SYMCACHE(cache); real_cache->add_delayed_dependency(from, to); } const gchar * -rspamd_symcache_get_parent (struct rspamd_symcache *cache, - const gchar *symbol) +rspamd_symcache_get_parent(struct rspamd_symcache *cache, + const gchar *symbol) { auto *real_cache = C_API_SYMCACHE(cache); @@ -213,8 +213,8 @@ rspamd_symcache_get_parent (struct rspamd_symcache *cache, return nullptr; } -const gchar* -rspamd_symcache_item_name (struct rspamd_symcache_item *item) +const gchar * +rspamd_symcache_item_name(struct rspamd_symcache_item *item) { auto *real_item = C_API_SYMCACHE_ITEM(item); return real_item->get_name().c_str(); @@ -259,7 +259,7 @@ rspamd_symcache_get_symbol_details(struct rspamd_symcache *cache, auto *sym = real_cache->get_item_by_name(symbol, false); if (sym) { - ucl_object_insert_key (this_sym_ucl, + ucl_object_insert_key(this_sym_ucl, ucl_object_fromstring(sym->get_type_str()), "type", strlen("type"), false); } @@ -267,13 +267,13 @@ rspamd_symcache_get_symbol_details(struct rspamd_symcache *cache, void rspamd_symcache_foreach(struct rspamd_symcache *cache, - void (*func) (struct rspamd_symcache_item *item, gpointer /* userdata */), - gpointer ud) + void (*func)(struct rspamd_symcache_item *item, gpointer /* userdata */), + gpointer ud) { auto *real_cache = C_API_SYMCACHE(cache); - real_cache->symbols_foreach([&](const rspamd::symcache::cache_item* item) { - func((struct rspamd_symcache_item *)item, ud); + real_cache->symbols_foreach([&](const rspamd::symcache::cache_item *item) { + func((struct rspamd_symcache_item *) item, ud); }); } @@ -288,9 +288,9 @@ rspamd_symcache_disable_all_symbols(struct rspamd_task *task, } gboolean -rspamd_symcache_disable_symbol (struct rspamd_task *task, - struct rspamd_symcache *cache, - const gchar *symbol) +rspamd_symcache_disable_symbol(struct rspamd_task *task, + struct rspamd_symcache *cache, + const gchar *symbol) { auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime); auto *real_cache = C_API_SYMCACHE(cache); @@ -299,9 +299,9 @@ rspamd_symcache_disable_symbol (struct rspamd_task *task, } gboolean -rspamd_symcache_enable_symbol (struct rspamd_task *task, - struct rspamd_symcache *cache, - const gchar *symbol) +rspamd_symcache_enable_symbol(struct rspamd_task *task, + struct rspamd_symcache *cache, + const gchar *symbol) { auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime); auto *real_cache = C_API_SYMCACHE(cache); @@ -310,9 +310,9 @@ rspamd_symcache_enable_symbol (struct rspamd_task *task, } gboolean -rspamd_symcache_is_checked (struct rspamd_task *task, - struct rspamd_symcache *cache, - const gchar *symbol) +rspamd_symcache_is_checked(struct rspamd_task *task, + struct rspamd_symcache *cache, + const gchar *symbol) { auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime); auto *real_cache = C_API_SYMCACHE(cache); @@ -321,11 +321,32 @@ rspamd_symcache_is_checked (struct rspamd_task *task, } gboolean -rspamd_symcache_process_settings (struct rspamd_task *task, - struct rspamd_symcache *cache) +rspamd_symcache_process_settings(struct rspamd_task *task, + struct rspamd_symcache *cache) { auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime); auto *real_cache = C_API_SYMCACHE(cache); return cache_runtime->process_settings(task, *real_cache); +} + +gboolean +rspamd_symcache_is_item_allowed(struct rspamd_task *task, + struct rspamd_symcache_item *item, + gboolean exec_only) +{ + auto *real_item = C_API_SYMCACHE_ITEM(item); + + return real_item->is_allowed(task, exec_only); +} + +gboolean +rspamd_symcache_is_symbol_enabled(struct rspamd_task *task, + struct rspamd_symcache *cache, + const gchar *symbol) +{ + auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime); + auto *real_cache = C_API_SYMCACHE(cache); + + return cache_runtime->is_symbol_enabled(task, *real_cache, symbol); } \ No newline at end of file diff --git a/src/libserver/symcache/symcache_item.cxx b/src/libserver/symcache/symcache_item.cxx index 99e3cfb5d..4eb2cc008 100644 --- a/src/libserver/symcache/symcache_item.cxx +++ b/src/libserver/symcache/symcache_item.cxx @@ -229,7 +229,7 @@ auto cache_item::get_type_str() const -> const char * RSPAMD_UNREACHABLE; } -auto cache_item::is_item_allowed(struct rspamd_task *task, bool exec_only) -> bool +auto cache_item::is_allowed(struct rspamd_task *task, bool exec_only) const -> bool { const auto *what = "execution"; diff --git a/src/libserver/symcache/symcache_item.hxx b/src/libserver/symcache/symcache_item.hxx index 484065cc2..f4ed75eb1 100644 --- a/src/libserver/symcache/symcache_item.hxx +++ b/src/libserver/symcache/symcache_item.hxx @@ -93,7 +93,7 @@ public: // TODO } - auto check_conditions(std::string_view sym_name, struct rspamd_task *task) -> bool { + auto check_conditions(std::string_view sym_name, struct rspamd_task *task) const -> bool { return std::all_of(std::begin(conditions), std::end(conditions), [&](const auto &cond) { return cond.check(sym_name, task); }); } @@ -300,7 +300,7 @@ public: * @param exec_only * @return */ - auto is_item_allowed(struct rspamd_task *task, bool exec_only) -> bool; + auto is_allowed(struct rspamd_task *task, bool exec_only) const -> bool; private: /** diff --git a/src/libserver/symcache/symcache_runtime.cxx b/src/libserver/symcache/symcache_runtime.cxx index 74dd4a183..a231bda31 100644 --- a/src/libserver/symcache/symcache_runtime.cxx +++ b/src/libserver/symcache/symcache_runtime.cxx @@ -96,7 +96,7 @@ symcache_runtime::process_settings(struct rspamd_task *task, const symcache &cac g_hash_table_iter_init(&gr_it, gr->symbols); while (g_hash_table_iter_next(&gr_it, &k, &v)) { - functor((const char*)k); + functor((const char *) k); } } } @@ -116,7 +116,7 @@ symcache_runtime::process_settings(struct rspamd_task *task, const symcache &cac it = nullptr; while ((cur = ucl_iterate_object(enabled, &it, true)) != nullptr) { - enable_symbol(task, cache,ucl_object_tostring(cur)); + enable_symbol(task, cache, ucl_object_tostring(cur)); } } @@ -137,7 +137,7 @@ symcache_runtime::process_settings(struct rspamd_task *task, const symcache &cac it = nullptr; while ((cur = ucl_iterate_object (disabled, &it, true)) != nullptr) { - disable_symbol(task, cache,ucl_object_tostring(cur)); + disable_symbol(task, cache, ucl_object_tostring(cur)); } } @@ -152,7 +152,7 @@ symcache_runtime::process_settings(struct rspamd_task *task, const symcache &cac auto symcache_runtime::disable_all_symbols(int skip_mask) -> void { - for (auto i = 0; i < order->size(); i ++) { + for (auto i = 0; i < order->size(); i++) { auto *dyn_item = &dynamic_items[i]; const auto &item = order->d[i]; @@ -237,5 +237,39 @@ symcache_runtime::is_symbol_checked(const symcache &cache, std::string_view name return false; } +auto +symcache_runtime::is_symbol_enabled(struct rspamd_task *task, const symcache &cache, std::string_view name) -> bool +{ + + const auto *item = cache.get_item_by_name(name, true); + if (item) { + + if (!item->is_allowed(task, true)) { + return false; + } + else { + auto our_id_maybe = rspamd::find_map(order->by_cache_id, item->id); + + if (our_id_maybe) { + auto *dyn_item = &dynamic_items[our_id_maybe.value()]; + if (dyn_item->started) { + /* Already started */ + return false; + } + + if (!item->is_virtual()) { + return std::get(item->specific).check_conditions(item->symbol, task); + } + } + else { + /* Unknown item */ + msg_debug_cache_task("cannot enable %s: symbol not found", name.data()); + } + } + } + + return true; +} + } diff --git a/src/libserver/symcache/symcache_runtime.hxx b/src/libserver/symcache/symcache_runtime.hxx index 63eda08e2..8a0da7bc0 100644 --- a/src/libserver/symcache/symcache_runtime.hxx +++ b/src/libserver/symcache/symcache_runtime.hxx @@ -109,6 +109,15 @@ public: * @return */ auto is_symbol_checked(const symcache &cache, std::string_view name) -> bool; + + /** + * Checks if a symbol is enabled for execution, checking all pending conditions + * @param task + * @param cache + * @param name + * @return + */ + auto is_symbol_enabled(struct rspamd_task *task, const symcache &cache, std::string_view name) -> bool; }; -- 2.39.5