]> source.dussan.org Git - rspamd.git/commitdiff
[Rework] Propagate ucl variables from the command line
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 18 May 2017 15:32:26 +0000 (16:32 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 18 May 2017 15:40:48 +0000 (16:40 +0100)
13 files changed:
config.h.in
src/libserver/cfg_file.h
src/libserver/cfg_rcl.c
src/libserver/cfg_utils.c
src/lua/lua_common.c
src/lua/lua_common.h
src/lua/lua_util.c
src/rspamadm/configdump.c
src/rspamadm/confighelp.c
src/rspamadm/configtest.c
src/rspamadm/lua_repl.c
src/rspamd.c
test/rspamd_lua_test.c

index d9be4fedf19aeeb4ec8daec6b37c80500f00d222..9e2ccb9fd78ce3917ceeb2c8ad3c555f98b46e0f 100644 (file)
 #define RSPAMD_DBDIR "${DBDIR}"
 #define RSPAMD_EXAMPLESDIR "${EXAMPLESDIR}"
 #define RSPAMD_PLUGINSDIR "${PLUGINSDIR}"
+#define RSPAMD_LUALIBDIR "${LUALIBDIR}"
 #define RSPAMD_RULESDIR "${RULESDIR}"
 #define RSPAMD_WWWDIR "${WWWDIR}"
 #define RSPAMD_PREFIX "${CMAKE_INSTALL_PREFIX}"
index 2db607075f45fbb1a1ec58e7da6cc3299ba1d529..89f4f0fde03d6a43e892ef92507e4133e2e80be3 100644 (file)
@@ -581,7 +581,8 @@ void rspamd_ucl_add_conf_variables (struct ucl_parser *parser, GHashTable *vars)
  * @param reconfig
  * @return
  */
-gboolean rspamd_init_filters (struct rspamd_config *cfg, bool reconfig);
+gboolean rspamd_init_filters (struct rspamd_config *cfg, bool reconfig,
+               GHashTable *vars);
 
 /**
  * Add new symbol to the metric
index aa0282d5be59af421b081871632190b57d9417e1..d8fc1c764900b467ae58001163ac6a67e0d65f29 100644 (file)
@@ -705,6 +705,7 @@ rspamd_rcl_worker_handler (rspamd_mempool_t *pool, const ucl_object_t *obj,
 #define RSPAMD_LOGDIR_INDEX "LOGDIR"
 #define RSPAMD_PLUGINSDIR_INDEX "PLUGINSDIR"
 #define RSPAMD_RULESDIR_INDEX "RULESDIR"
+#define RSPAMD_LUALIBDIR_INDEX "LUALIBDIR"
 #define RSPAMD_WWWDIR_INDEX "WWWDIR"
 #define RSPAMD_PREFIX_INDEX "PREFIX"
 #define RSPAMD_VERSION_INDEX "VERSION"
@@ -902,7 +903,7 @@ rspamd_rcl_set_lua_globals (struct rspamd_config *cfg, lua_State *L,
        /* Clear stack from globals */
        lua_pop (L, 4);
 
-       rspamd_lua_set_path (L, cfg);
+       rspamd_lua_set_path (L, cfg, vars);
 
        /* Set known paths as rspamd_paths global */
        lua_getglobal (L, "rspamd_paths");
@@ -915,6 +916,7 @@ rspamd_rcl_set_lua_globals (struct rspamd_config *cfg, lua_State *L,
                rspamd_lua_table_set (L, RSPAMD_WWWDIR_INDEX, RSPAMD_WWWDIR);
                rspamd_lua_table_set (L, RSPAMD_PLUGINSDIR_INDEX, RSPAMD_PLUGINSDIR);
                rspamd_lua_table_set (L, RSPAMD_RULESDIR_INDEX, RSPAMD_RULESDIR);
+               rspamd_lua_table_set (L, RSPAMD_LUALIBDIR_INDEX, RSPAMD_LUALIBDIR);
                rspamd_lua_table_set (L, RSPAMD_PREFIX_INDEX, RSPAMD_PREFIX);
 
                /* Override from vars if needed */
index 354e9f082db82e5108de6d2a17060bacc17d62c4..982415fdbb53fd54b43e678a4a5cafe44b28cc2f 100644 (file)
@@ -1331,7 +1331,7 @@ rspamd_check_worker (struct rspamd_config *cfg, worker_t *wrk)
 }
 
 gboolean
-rspamd_init_filters (struct rspamd_config *cfg, bool reconfig)
+rspamd_init_filters (struct rspamd_config *cfg, bool reconfig, GHashTable *vars)
 {
        GList *cur;
        module_t *mod, **pmod;
@@ -1383,7 +1383,7 @@ rspamd_init_filters (struct rspamd_config *cfg, bool reconfig)
                cur = g_list_next (cur);
        }
 
-       return rspamd_init_lua_filters (cfg, 0);
+       return rspamd_init_lua_filters (cfg, 0, vars);
 }
 
 static void
index 634f7c56916d16a0782afa5323507e3a8aaadeaf..2af506f13c727b166643ca560fe663311fe66622 100644 (file)
@@ -202,10 +202,14 @@ lua_add_actions_global (lua_State *L)
 }
 
 void
-rspamd_lua_set_path (lua_State *L, struct rspamd_config *cfg)
+rspamd_lua_set_path (lua_State *L, struct rspamd_config *cfg, GHashTable *vars)
 {
        const gchar *old_path, *additional_path = NULL;
        const ucl_object_t *opts;
+       const gchar *pluginsdir = RSPAMD_PLUGINSDIR,
+                       *rulesdir = RSPAMD_RULESDIR,
+                       *lualibdir = RSPAMD_LUALIBDIR;
+
        gchar path_buf[PATH_MAX];
 
        lua_getglobal (L, "package");
@@ -228,16 +232,37 @@ rspamd_lua_set_path (lua_State *L, struct rspamd_config *cfg)
                }
        }
 
+       if (vars) {
+               gchar *t;
+
+               t = g_hash_table_lookup (vars, "PLUGINSDIR");
+               if (t) {
+                       pluginsdir = t;
+               }
+
+               t = g_hash_table_lookup (vars, "RULESDIR");
+               if (t) {
+                       rulesdir = t;
+               }
+
+               t = g_hash_table_lookup (vars, "LUALIBDIR");
+               if (t) {
+                       lualibdir = t;
+               }
+       }
+
        if (additional_path) {
                rspamd_snprintf (path_buf, sizeof (path_buf),
-                               "%s/lua/?.lua;%s/lua/?.lua;%s/?.lua;%s;%s",
-                               RSPAMD_PLUGINSDIR, RSPAMD_CONFDIR, RSPAMD_RULESDIR,
+                               "%s/lua/?.lua;%s/lua/?.lua;%s/?.lua;%s/?.lua;%s/?.so;%s;%s",
+                               pluginsdir, RSPAMD_CONFDIR, rulesdir,
+                               lualibdir, lualibdir,
                                additional_path, old_path);
        }
        else {
                rspamd_snprintf (path_buf, sizeof (path_buf),
-                               "%s/lua/?.lua;%s/lua/?.lua;%s/?.lua;%s",
-                               RSPAMD_PLUGINSDIR, RSPAMD_CONFDIR, RSPAMD_RULESDIR,
+                               "%s/lua/?.lua;%s/lua/?.lua;%s/?.lua;%s/?.lua;%s/?.so;%s",
+                               pluginsdir, RSPAMD_CONFDIR, rulesdir,
+                               lualibdir, lualibdir,
                                old_path);
        }
 
@@ -340,7 +365,8 @@ rspamd_free_lua_locked (struct lua_locked_state *st)
 }
 
 gboolean
-rspamd_init_lua_filters (struct rspamd_config *cfg, gboolean force_load)
+rspamd_init_lua_filters (struct rspamd_config *cfg, gboolean force_load,
+               GHashTable *vars)
 {
        struct rspamd_config **pcfg;
        GList *cur;
@@ -349,7 +375,7 @@ rspamd_init_lua_filters (struct rspamd_config *cfg, gboolean force_load)
        GString *tb;
        gint err_idx;
 
-       rspamd_lua_set_path (L, cfg);
+       rspamd_lua_set_path (L, cfg, vars);
        cur = g_list_first (cfg->script_modules);
 
        while (cur) {
index a4d212fc5aa3d828f4d132c674c6b2edbd63687e..82957d3a1191834da7e813fcc529334a83d89781 100644 (file)
@@ -163,7 +163,8 @@ lua_State *rspamd_lua_init (void);
  * Load and initialize lua plugins
  */
 gboolean
-rspamd_init_lua_filters (struct rspamd_config *cfg, gboolean force_load);
+rspamd_init_lua_filters (struct rspamd_config *cfg, gboolean force_load,
+               GHashTable *vars);
 
 /**
  * Initialize new locked lua_State structure
@@ -279,7 +280,8 @@ gboolean rspamd_lua_check_condition (struct rspamd_config *cfg,
 void rspamd_lua_dumpstack (lua_State *L);
 
 /* Set lua path according to the configuration */
-void rspamd_lua_set_path (lua_State *L, struct rspamd_config *cfg);
+void rspamd_lua_set_path (lua_State *L, struct rspamd_config *cfg,
+               GHashTable *vars);
 
 struct memory_pool_s * rspamd_lua_check_mempool (lua_State * L, gint pos);
 struct rspamd_config * lua_check_config (lua_State * L, gint pos);
index afa5da3715999f5d689b0d6bfa794ffd376eb6f8..fc051814b86f6036c21637d328f959147365bc0d 100644 (file)
@@ -648,7 +648,7 @@ lua_util_process_message (lua_State *L)
 
        if (cfg != NULL && message != NULL) {
                base = event_init ();
-               rspamd_init_filters (cfg, FALSE);
+               rspamd_init_filters (cfg, FALSE, NULL);
                task = rspamd_task_new (NULL, cfg);
                task->ev_base = base;
                task->msg.begin = rspamd_mempool_alloc (task->task_pool, mlen);
index 355437b9ca37147f3caefa76bc7878e6313e2182..720d6bf51c24f9c509969e7fb825020eedbc1803 100644 (file)
@@ -286,7 +286,7 @@ rspamadm_configdump (gint argc, gchar **argv)
                /* Do post-load actions */
                rspamd_lua_post_load_config (cfg);
 
-               if (!rspamd_init_filters (rspamd_main->cfg, FALSE)) {
+               if (!rspamd_init_filters (rspamd_main->cfg, FALSE, ucl_vars)) {
                        ret = FALSE;
                }
 
index 591f686529ea2820a6408af5bf14ec4979f25b05..26251937e40fa91759bb8f5b0b7828c7bf278d62 100644 (file)
@@ -250,7 +250,7 @@ rspamadm_confighelp (gint argc, gchar **argv)
        }
 
        /* Init lua modules */
-       rspamd_init_lua_filters (cfg, TRUE);
+       rspamd_init_lua_filters (cfg, TRUE, ucl_vars);
 
        if (argc > 1) {
                for (i = 1; i < argc; i ++) {
index 59f466de2ca332a139f02735823945cbb0ebdbce..3a15b976b21c33e962763b723f9bd4baa685d771 100644 (file)
@@ -146,7 +146,7 @@ rspamadm_configtest (gint argc, gchar **argv)
                /* Do post-load actions */
                rspamd_lua_post_load_config (cfg);
 
-               if (!rspamd_init_filters (rspamd_main->cfg, FALSE)) {
+               if (!rspamd_init_filters (rspamd_main->cfg, FALSE, ucl_vars)) {
                        ret = FALSE;
                }
 
index e3656e8c9166215b6e73145e4af5202f1683c17c..d7d7721a8126c490c914711766c11cf2d973dbfb 100644 (file)
@@ -676,7 +676,7 @@ rspamadm_lua (gint argc, gchar **argv)
        }
 
        L = rspamd_lua_init ();
-       rspamd_lua_set_path (L, NULL);
+       rspamd_lua_set_path (L, NULL, ucl_vars);
 
        if (paths) {
                for (elt = paths; *elt != NULL; elt ++) {
index 1a08c938f2a706043916825f79fd55285650b32f..828720cae8811664a71086943533295ea8709007 100644 (file)
@@ -68,7 +68,8 @@ static gboolean load_rspamd_config (struct rspamd_main *rspamd_main,
                struct rspamd_config *cfg,
                gboolean init_modules,
                enum rspamd_post_load_options opts,
-               gboolean reload);
+               gboolean reload,
+               GHashTable *vars);
 
 /* Control socket */
 static gint control_fd;
@@ -287,7 +288,8 @@ reread_config (struct rspamd_main *rspamd_main)
        rspamd_main->cfg = tmp_cfg;
 
        if (!load_rspamd_config (rspamd_main, tmp_cfg, TRUE,
-                       RSPAMD_CONFIG_INIT_VALIDATE|RSPAMD_CONFIG_INIT_SYMCACHE, TRUE)) {
+                       RSPAMD_CONFIG_INIT_VALIDATE|RSPAMD_CONFIG_INIT_SYMCACHE,
+                       TRUE, ucl_vars)) {
                rspamd_main->cfg = old_cfg;
                rspamd_log_close_priv (rspamd_main->logger,
                                        rspamd_main->workers_uid,
@@ -825,7 +827,8 @@ static gboolean
 load_rspamd_config (struct rspamd_main *rspamd_main,
                struct rspamd_config *cfg, gboolean init_modules,
                enum rspamd_post_load_options opts,
-               gboolean reload)
+               gboolean reload,
+               GHashTable *vars)
 {
        cfg->compiled_modules = modules;
        cfg->compiled_workers = workers;
@@ -854,7 +857,7 @@ load_rspamd_config (struct rspamd_main *rspamd_main,
        rspamd_lua_post_load_config (cfg);
 
        if (init_modules) {
-               rspamd_init_filters (cfg, reload);
+               rspamd_init_filters (cfg, reload, vars);
        }
 
        /* Do post-load actions */
@@ -1220,7 +1223,8 @@ main (gint argc, gchar **argv, gchar **env)
                        rspamd_main->workers_gid);
 
        if (config_test || dump_cache) {
-               if (!load_rspamd_config (rspamd_main, rspamd_main->cfg, FALSE, 0, FALSE)) {
+               if (!load_rspamd_config (rspamd_main, rspamd_main->cfg, FALSE, 0,
+                               FALSE, ucl_vars)) {
                        exit (EXIT_FAILURE);
                }
 
@@ -1243,7 +1247,7 @@ main (gint argc, gchar **argv, gchar **env)
 
        /* Load config */
        if (!load_rspamd_config (rspamd_main, rspamd_main->cfg, TRUE,
-                       RSPAMD_CONFIG_LOAD_ALL, FALSE)) {
+                       RSPAMD_CONFIG_LOAD_ALL, FALSE, ucl_vars)) {
                exit (EXIT_FAILURE);
        }
 
index baad40ed7389d330fc7e2e2a223fa1753d43ba2a..0c785e9e649fbbee12ad16b65f5c83de12ad84c1 100644 (file)
@@ -106,7 +106,7 @@ rspamd_lua_test_func (void)
        }
 
        lua_setglobal (L, "tests_list");
-       rspamd_lua_set_path (L, NULL);
+       rspamd_lua_set_path (L, NULL, NULL);
 
        lua_pushcfunction (L, traceback);
        luaL_loadfile (L, rp);