From: Vsevolod Stakhov Date: Tue, 25 Oct 2016 08:07:49 +0000 (+0200) Subject: [Feature] Allow to update dynamic conf in Redis X-Git-Tag: 1.4.0~200 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0edaed34e4377c4cd06e2a28d14d9117499c1826;p=thirdparty%2Frspamd.git [Feature] Allow to update dynamic conf in Redis --- diff --git a/src/controller.c b/src/controller.c index 4fa2b137aa..65b40ca5ad 100644 --- a/src/controller.c +++ b/src/controller.c @@ -1854,15 +1854,6 @@ rspamd_controller_handle_saveactions ( return 0; } - /* Now check for dynamic config */ - if (!ctx->cfg->dynamic_conf) { - msg_err_session ("dynamic conf has not been defined"); - rspamd_controller_send_error (conn_ent, - 500, - "No dynamic_rules setting defined"); - return 0; - } - parser = ucl_parser_new (0); ucl_parser_add_chunk (parser, msg->body_buf.begin, msg->body_buf.len); @@ -1973,15 +1964,6 @@ rspamd_controller_handle_savesymbols ( return 0; } - /* Now check for dynamic config */ - if (!ctx->cfg->dynamic_conf) { - msg_err_session ("dynamic conf has not been defined"); - rspamd_controller_send_error (conn_ent, - 500, - "No dynamic_rules setting defined"); - return 0; - } - parser = ucl_parser_new (0); ucl_parser_add_chunk (parser, msg->body_buf.begin, msg->body_buf.len); diff --git a/src/libserver/dynamic_cfg.c b/src/libserver/dynamic_cfg.c index d31418588b..56b7b17f08 100644 --- a/src/libserver/dynamic_cfg.c +++ b/src/libserver/dynamic_cfg.c @@ -19,6 +19,7 @@ #include "filter.h" #include "dynamic_cfg.h" #include "unix-std.h" +#include "lua/lua_common.h" struct config_json_buf { GString *buf; @@ -393,6 +394,102 @@ new_dynamic_elt (ucl_object_t *arr, const gchar *name, gdouble value) return n; } +static gboolean +rspamd_maybe_add_lua_dynsym (struct rspamd_config *cfg, + const gchar *sym, + gdouble score) +{ + lua_State *L = cfg->lua_state; + gboolean ret = FALSE; + struct rspamd_config **pcfg; + + lua_getglobal (L, "rspamd_plugins"); + if (lua_type (L, -1) == LUA_TTABLE) { + lua_pushstring (L, "dynamic_conf"); + lua_gettable (L, -2); + + if (lua_type (L, -1) == LUA_TTABLE) { + lua_pushstring (L, "add_symbol"); + lua_gettable (L, -2); + + if (lua_type (L, -1) == LUA_TFUNCTION) { + pcfg = lua_newuserdata (L, sizeof (*pcfg)); + *pcfg = cfg; + rspamd_lua_setclass (L, "rspamd{config}", -1); + lua_pushstring (L, sym); + lua_pushnumber (L, score); + + if (lua_pcall (L, 3, 1, 0) != 0) { + msg_err_config ("cannot execute add_symbol script: %s", + lua_tostring (L, -1)); + } + else { + ret = lua_toboolean (L, -1); + } + + lua_pop (L, 1); + } + else { + lua_pop (L, 1); + } + } + + lua_pop (L, 1); + } + + lua_pop (L, 1); + + return ret; +} + +static gboolean +rspamd_maybe_add_lua_dynact (struct rspamd_config *cfg, + const gchar *action, + gdouble score) +{ + lua_State *L = cfg->lua_state; + gboolean ret = FALSE; + struct rspamd_config **pcfg; + + lua_getglobal (L, "rspamd_plugins"); + if (lua_type (L, -1) == LUA_TTABLE) { + lua_pushstring (L, "dynamic_conf"); + lua_gettable (L, -2); + + if (lua_type (L, -1) == LUA_TTABLE) { + lua_pushstring (L, "add_action"); + lua_gettable (L, -2); + + if (lua_type (L, -1) == LUA_TFUNCTION) { + pcfg = lua_newuserdata (L, sizeof (*pcfg)); + *pcfg = cfg; + rspamd_lua_setclass (L, "rspamd{config}", -1); + lua_pushstring (L, action); + lua_pushnumber (L, score); + + if (lua_pcall (L, 3, 1, 0) != 0) { + msg_err_config ("cannot execute add_action script: %s", + lua_tostring (L, -1)); + } + else { + ret = lua_toboolean (L, -1); + } + + lua_pop (L, 1); + } + else { + lua_pop (L, 1); + } + } + + lua_pop (L, 1); + } + + lua_pop (L, 1); + + return ret; +} + /** * Add symbol for specified metric * @param cfg config file object @@ -409,6 +506,10 @@ add_dynamic_symbol (struct rspamd_config *cfg, { ucl_object_t *metric, *syms; + if (rspamd_maybe_add_lua_dynsym (cfg, symbol, value)) { + return TRUE; + } + if (cfg->dynamic_conf == NULL) { msg_info ("dynamic conf is disabled"); return FALSE; @@ -497,6 +598,10 @@ add_dynamic_action (struct rspamd_config *cfg, ucl_object_t *metric, *acts; const gchar *action_name = rspamd_action_to_str (action); + if (rspamd_maybe_add_lua_dynact (cfg, action_name, value)) { + return TRUE; + } + if (cfg->dynamic_conf == NULL) { msg_info ("dynamic conf is disabled"); return FALSE; diff --git a/src/plugins/lua/dynamic_conf.lua b/src/plugins/lua/dynamic_conf.lua index e3cd975bea..34bce40dfb 100644 --- a/src/plugins/lua/dynamic_conf.lua +++ b/src/plugins/lua/dynamic_conf.lua @@ -332,11 +332,13 @@ local function add_dynamic_action(cfg, act, score) return add end -rspamd_plugins["dynamic_conf"] = { - add_symbol = function(cfg, sym, score) - return add_dynamic_symbol(cfg, sym, score) - end, - add_action = function(cfg, act, score) - return add_dynamic_action(cfg, act, score) - end, -} +if redis_params then + rspamd_plugins["dynamic_conf"] = { + add_symbol = function(cfg, sym, score) + return add_dynamic_symbol(cfg, sym, score) + end, + add_action = function(cfg, act, score) + return add_dynamic_action(cfg, act, score) + end, + } +end