]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Feature] Allow to update dynamic conf in Redis
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 25 Oct 2016 08:07:49 +0000 (10:07 +0200)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 25 Oct 2016 08:08:06 +0000 (10:08 +0200)
src/controller.c
src/libserver/dynamic_cfg.c
src/plugins/lua/dynamic_conf.lua

index 4fa2b137aa45961b95133ac35b7bae2f5cc02536..65b40ca5ad51dfd17a0589893894cd9342265a56 100644 (file)
@@ -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);
 
index d31418588b67947c2fcafd884be193d058449ca6..56b7b17f081b15a2749cb2a4f765bbab40b9b42d 100644 (file)
@@ -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;
index e3cd975bea5d95b31caace16a8cd1772a821ccd7..34bce40dfba3649b28d3f2256611b7edabbf0f05 100644 (file)
@@ -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