]> source.dussan.org Git - rspamd.git/commitdiff
[Project] Rework rspamadm and Lua init path
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 24 May 2018 18:55:37 +0000 (19:55 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 24 May 2018 18:56:05 +0000 (19:56 +0100)
26 files changed:
src/libserver/cfg_file.h
src/libserver/cfg_utils.c
src/lua/lua_common.c
src/lua/lua_util.c
src/rspamadm/commands.c
src/rspamadm/configdump.c
src/rspamadm/confighelp.c
src/rspamadm/configtest.c
src/rspamadm/configwizard.c
src/rspamadm/control.c
src/rspamadm/corpus_test.c
src/rspamadm/dkim_keygen.c
src/rspamadm/fuzzy_convert.c
src/rspamadm/fuzzy_merge.c
src/rspamadm/grep.c
src/rspamadm/keypair.c
src/rspamadm/lua_repl.c
src/rspamadm/pw.c
src/rspamadm/rescore.c
src/rspamadm/rspamadm.c
src/rspamadm/rspamadm.h
src/rspamadm/signtool.c
src/rspamadm/stat_convert.c
src/rspamd.c
test/rspamd_test_suite.c
test/rspamd_upstream_test.c

index 520c847f336d714ee38929238fb4055b0b03e871..777340e16b4eb24a7bc2d53acb1799bbbd95c698 100644 (file)
@@ -313,6 +313,7 @@ struct rspamd_config {
        gboolean enable_experimental;                   /**< Enable experimental plugins                                                */
        gboolean disable_pcre_jit;                      /**< Disable pcre JIT                                                                   */
        gboolean disable_lua_squeeze;                   /**< Disable lua rules squeezing                                                */
+       gboolean own_lua_state;                         /**< True if we have created lua_state internally               */
 
        gsize max_diff;                                 /**< maximum diff size for text parts                                   */
        gsize max_cores_size;                           /**< maximum size occupied by rspamd core files                 */
@@ -445,11 +446,16 @@ struct rspamd_config {
 gboolean rspamd_parse_bind_line (struct rspamd_config *cfg,
        struct rspamd_worker_conf *cf, const gchar *str);
 
+
+enum rspamd_config_init_flags {
+       RSPAMD_CONFIG_INIT_DEFAULT = 0,
+       RSPAMD_CONFIG_INIT_SKIP_LUA = (1 << 0)
+};
 /**
  * Init default values
  * @param cfg config file
  */
-struct rspamd_config *rspamd_config_new (void);
+struct rspamd_config *rspamd_config_new (enum rspamd_config_init_flags flags);
 
 /**
  * Free memory used by config structure
index bf63b2188aeebfcc0d66f92fadbb8599fc2873e1..e606139cf10c23271bfa6802d589a85ef36b7698 100644 (file)
@@ -114,7 +114,7 @@ rspamd_parse_bind_line (struct rspamd_config *cfg,
 }
 
 struct rspamd_config *
-rspamd_config_new (void)
+rspamd_config_new (enum rspamd_config_init_flags flags)
 {
        struct rspamd_config *cfg;
 
@@ -172,7 +172,11 @@ rspamd_config_new (void)
        cfg->min_word_len = DEFAULT_MIN_WORD;
        cfg->max_word_len = DEFAULT_MAX_WORD;
 
-       cfg->lua_state = rspamd_lua_init ();
+       if (!(flags & RSPAMD_CONFIG_INIT_SKIP_LUA)) {
+               cfg->lua_state = rspamd_lua_init ();
+               cfg->own_lua_state = TRUE;
+       }
+
        cfg->cache = rspamd_symbols_cache_new (cfg);
        cfg->ups_ctx = rspamd_upstreams_library_init ();
        cfg->re_cache = rspamd_re_cache_new ();
@@ -251,7 +255,10 @@ rspamd_config_free (struct rspamd_config *cfg)
        rspamd_re_cache_unref (cfg->re_cache);
        rspamd_upstreams_library_unref (cfg->ups_ctx);
        rspamd_mempool_delete (cfg->cfg_pool);
-       lua_close (cfg->lua_state);
+
+       if (cfg->lua_state && cfg->own_lua_state) {
+               lua_close (cfg->lua_state);
+       }
        REF_RELEASE (cfg->libs_ctx);
 
        DL_FOREACH_SAFE (cfg->log_pipes, lp, ltmp) {
index fe715f393ea5b722fb50d22c541d95c1e4bbd5a7..f33bc3daafc047c096ae84d474ce8799f7ebb973 100644 (file)
@@ -587,9 +587,6 @@ rspamd_init_lua_filters (struct rspamd_config *cfg, gboolean force_load)
                cur = g_list_next (cur);
        }
 
-       /* Assign state */
-       cfg->lua_state = L;
-
        return TRUE;
 }
 
index 6c200d1be8c0c82a9de733bf571633ef4ff3715d..47b7106b6d7fdafb491ca1ce5b764e65b8766f97 100644 (file)
@@ -626,7 +626,8 @@ lua_util_load_rspamd_config (lua_State *L)
        cfg_name = luaL_checkstring (L, 1);
 
        if (cfg_name) {
-               cfg = rspamd_config_new ();
+               cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_SKIP_LUA);
+               cfg->lua_state = L;
 
                if (rspamd_config_read (cfg, cfg_name, NULL, NULL, NULL, NULL)) {
                        msg_err_config ("cannot load config from %s", cfg_name);
@@ -655,7 +656,8 @@ lua_util_config_from_ucl (lua_State *L)
 
        if (obj) {
                cfg = g_malloc0 (sizeof (struct rspamd_config));
-               cfg = rspamd_config_new ();
+               cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_SKIP_LUA);
+               cfg->lua_state = L;
 
                cfg->rcl_obj = obj;
                cfg->cache = rspamd_symbols_cache_new (cfg);
index 410306fe3f360861db2f8e50114f2373cc8dd6dd..87a3d961ef1736b6fa2dcb4bfb4e0262ba89b7ea 100644 (file)
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 #include "rspamadm.h"
+#include "libutil/util.h"
 
 extern struct rspamadm_command pw_command;
 extern struct rspamadm_command keypair_command;
@@ -55,23 +56,33 @@ const struct rspamadm_command *commands[] = {
 
 
 const struct rspamadm_command *
-rspamadm_search_command (const gchar *name)
+rspamadm_search_command (const gchar *name, GPtrArray *all_commands)
 {
-       const struct rspamadm_command *ret = NULL;
+       const struct rspamadm_command *ret = NULL, *cmd;
        guint i;
 
        if (name == NULL) {
                name = "help";
        }
 
-       for (i = 0; i < G_N_ELEMENTS (commands); i ++) {
-               if (commands[i] != NULL) {
-                       if (strcmp (name, commands[i]->name) == 0) {
-                               ret = commands[i];
+       PTR_ARRAY_FOREACH (all_commands, i, cmd) {
+                       if (strcmp (name, cmd->name) == 0) {
+                               ret = cmd;
                                break;
                        }
-               }
        }
 
        return ret;
 }
+
+void
+rspamadm_fill_internal_commands (GPtrArray *dest)
+{
+       guint i;
+
+       for (i = 0; i < G_N_ELEMENTS (commands); i ++) {
+               if (commands[i]) {
+                       g_ptr_array_add (dest, (gpointer)commands[i]);
+               }
+       }
+}
index d5380bc9df0d0b1ec3af7081f7b583d9572ffdb9..486a74cef3ea62899af8f1d3f5181b7e6221e08c 100644 (file)
@@ -33,8 +33,8 @@ extern struct rspamd_main *rspamd_main;
 extern module_t *modules[];
 extern worker_t *workers[];
 
-static void rspamadm_configdump (gint argc, gchar **argv);
-static const char *rspamadm_configdump_help (gboolean full_help);
+static void rspamadm_configdump (gint argc, gchar **argv, const struct rspamadm_command *);
+static const char *rspamadm_configdump_help (gboolean full_help, const struct rspamadm_command *);
 
 struct rspamadm_command configdump_command = {
                .name = "configdump",
@@ -61,7 +61,7 @@ static GOptionEntry entries[] = {
 };
 
 static const char *
-rspamadm_configdump_help (gboolean full_help)
+rspamadm_configdump_help (gboolean full_help, const struct rspamadm_command *cmd)
 {
        const char *help_str;
 
@@ -237,7 +237,7 @@ rspamadm_dump_section_obj (struct rspamd_config *cfg,
 }
 
 static void
-rspamadm_configdump (gint argc, gchar **argv)
+rspamadm_configdump (gint argc, gchar **argv, const struct rspamadm_command *cmd)
 {
        GOptionContext *context;
        GError *error = NULL;
index 8f208b80583ea473486f8c36de924f9469401151..7ce72dea98803de10b60578fa0da3dc07d472edb 100644 (file)
@@ -30,9 +30,11 @@ extern struct rspamd_main *rspamd_main;
 extern module_t *modules[];
 extern worker_t *workers[];
 
-static void rspamadm_confighelp (gint argc, gchar **argv);
+static void rspamadm_confighelp (gint argc, gchar **argv,
+                                                                const struct rspamadm_command *cmd);
 
-static const char *rspamadm_confighelp_help (gboolean full_help);
+static const char *rspamadm_confighelp_help (gboolean full_help,
+                                                                                        const struct rspamadm_command *cmd);
 
 struct rspamadm_command confighelp_command = {
                .name = "confighelp",
@@ -55,7 +57,7 @@ static GOptionEntry entries[] = {
 };
 
 static const char *
-rspamadm_confighelp_help (gboolean full_help)
+rspamadm_confighelp_help (gboolean full_help, const struct rspamadm_command *cmd)
 {
        const char *help_str;
 
@@ -189,7 +191,7 @@ rspamadm_confighelp_search_word (const ucl_object_t *obj, const gchar *str)
 }
 
 static void
-rspamadm_confighelp (gint argc, gchar **argv)
+rspamadm_confighelp (gint argc, gchar **argv, const struct rspamadm_command *cmd)
 {
        struct rspamd_config *cfg;
        ucl_object_t *doc_obj;
@@ -224,7 +226,8 @@ rspamadm_confighelp (gint argc, gchar **argv)
                pworker++;
        }
 
-       cfg = rspamd_config_new ();
+       cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_SKIP_LUA);
+       cfg->lua_state = L;
        cfg->compiled_modules = modules;
        cfg->compiled_workers = workers;
 
index 18104c1092936de9c8c2889818780e966f81a39a..94914e817ef430c7ea6575d6849bbcb0fd75fbed 100644 (file)
@@ -28,8 +28,10 @@ extern struct rspamd_main *rspamd_main;
 extern module_t *modules[];
 extern worker_t *workers[];
 
-static void rspamadm_configtest (gint argc, gchar **argv);
-static const char *rspamadm_configtest_help (gboolean full_help);
+static void rspamadm_configtest (gint argc, gchar **argv,
+                                                                const struct rspamadm_command *cmd);
+static const char *rspamadm_configtest_help (gboolean full_help,
+                                                                                        const struct rspamadm_command *cmd);
 
 struct rspamadm_command configtest_command = {
                .name = "configtest",
@@ -50,7 +52,7 @@ static GOptionEntry entries[] = {
 };
 
 static const char *
-rspamadm_configtest_help (gboolean full_help)
+rspamadm_configtest_help (gboolean full_help, const struct rspamadm_command *cmd)
 {
        const char *help_str;
 
@@ -94,7 +96,7 @@ config_logger (rspamd_mempool_t *pool, gpointer ud)
 }
 
 static void
-rspamadm_configtest (gint argc, gchar **argv)
+rspamadm_configtest (gint argc, gchar **argv, const struct rspamadm_command *cmd)
 {
        GOptionContext *context;
        GError *error = NULL;
index 12c7b8cc1801bee321f29e5f5910aeb4c0059385..5aaa94914fb1836ceae9e65642653ff239d46bbc 100644 (file)
@@ -28,8 +28,10 @@ extern struct rspamd_main *rspamd_main;
 extern module_t *modules[];
 extern worker_t *workers[];
 
-static void rspamadm_configwizard (gint argc, gchar **argv);
-static const char *rspamadm_configwizard_help (gboolean full_help);
+static void rspamadm_configwizard (gint argc, gchar **argv,
+                                                                  const struct rspamadm_command *cmd);
+static const char *rspamadm_configwizard_help (gboolean full_help,
+                                                                                          const struct rspamadm_command *cmd);
 
 struct rspamadm_command configwizard_command = {
                .name = "configwizard",
@@ -46,7 +48,7 @@ static GOptionEntry entries[] = {
 };
 
 static const char *
-rspamadm_configwizard_help (gboolean full_help)
+rspamadm_configwizard_help (gboolean full_help, const struct rspamadm_command *cmd)
 {
        const char *help_str;
 
@@ -83,7 +85,8 @@ config_logger (rspamd_mempool_t *pool, gpointer ud)
 }
 
 static void
-rspamadm_configwizard (gint argc, gchar **argv)
+rspamadm_configwizard (gint argc, gchar **argv,
+                                          const struct rspamadm_command *cmd)
 {
        GOptionContext *context;
        GError *error = NULL;
index 35cecbc69e55077c4de7f347dfba1666c2d7de6d..35b5415e7aa2a7335b1c349aedea1a6d74d824c5 100644 (file)
@@ -31,8 +31,10 @@ static gboolean ucl = TRUE;
 static gboolean compact = FALSE;
 static gdouble timeout = 1.0;
 
-static void rspamadm_control (gint argc, gchar **argv);
-static const char *rspamadm_control_help (gboolean full_help);
+static void rspamadm_control (gint argc, gchar **argv,
+                                                         const struct rspamadm_command *cmd);
+static const char *rspamadm_control_help (gboolean full_help,
+                                                                                 const struct rspamadm_command *cmd);
 
 struct rspamadm_command control_command = {
                .name = "control",
@@ -64,7 +66,7 @@ static GOptionEntry entries[] = {
 };
 
 static const char *
-rspamadm_control_help (gboolean full_help)
+rspamadm_control_help (gboolean full_help, const struct rspamadm_command *cmd)
 {
        const char *help_str;
 
@@ -158,7 +160,7 @@ rspamd_control_finish_handler (struct rspamd_http_connection *conn,
 }
 
 static void
-rspamadm_control (gint argc, gchar **argv)
+rspamadm_control (gint argc, gchar **argv, const struct rspamadm_command *_cmd)
 {
        GOptionContext *context;
        GError *error = NULL;
index d72788d21d013bdd73d3b16657bb485b9575af01..305c3a8adc16adb69e74a2475e939c87a13937ec 100644 (file)
@@ -24,8 +24,10 @@ static gchar *output_location = "results.log";
 static gint connections = 10;
 static gdouble timeout = 60.0;
 
-static void rspamadm_corpus_test (gint argc, gchar **argv);
-static const char *rspamadm_corpus_test_help (gboolean full_help);
+static void rspamadm_corpus_test (gint argc, gchar **argv,
+                                                                 const struct rspamadm_command *cmd);
+static const char *rspamadm_corpus_test_help (gboolean full_help,
+                                                                                         const struct rspamadm_command *cmd);
 
 struct rspamadm_command corpus_test_command = {
        .name = "corpus_test",
@@ -49,7 +51,7 @@ static GOptionEntry entries[] = {
 };
 
 static const char *
-rspamadm_corpus_test_help (gboolean full_help)
+rspamadm_corpus_test_help (gboolean full_help, const struct rspamadm_command *cmd)
 {
        const char *help_str;
 
@@ -74,7 +76,7 @@ rspamadm_corpus_test_help (gboolean full_help)
 }
 
 static void
-rspamadm_corpus_test (gint argc, gchar **argv)
+rspamadm_corpus_test (gint argc, gchar **argv, const struct rspamadm_command *cmd)
 {
        GOptionContext *context;
        GError *error = NULL;
index e32f7bf050902fe6cdfe017c8ae479a62068f327..86b228d018c3e0d0554ed5997d9c5682a0197ec4 100644 (file)
@@ -27,8 +27,10 @@ static gchar *selector = NULL;
 static gchar *domain = NULL;
 static guint bits = 1024;
 
-static void rspamadm_dkim_keygen (gint argc, gchar **argv);
-static const char *rspamadm_dkim_keygen_help (gboolean full_help);
+static void rspamadm_dkim_keygen (gint argc, gchar **argv,
+                                                                 const struct rspamadm_command *cmd);
+static const char *rspamadm_dkim_keygen_help (gboolean full_help,
+                                                                                         const struct rspamadm_command *cmd);
 static void rspamadm_dkim_keygen_lua_subrs (gpointer pL);
 
 struct rspamadm_command dkim_keygen_command = {
@@ -52,7 +54,7 @@ static GOptionEntry entries[] = {
 };
 
 static const char *
-rspamadm_dkim_keygen_help (gboolean full_help)
+rspamadm_dkim_keygen_help (gboolean full_help, const struct rspamadm_command *cmd)
 {
        const char *help_str;
 
@@ -221,7 +223,7 @@ rspamadm_dkim_keygen_lua_subrs (gpointer pL)
 }
 
 static void
-rspamadm_dkim_keygen (gint argc, gchar **argv)
+rspamadm_dkim_keygen (gint argc, gchar **argv, const struct rspamadm_command *cmd)
 {
        GOptionContext *context;
        GError *error = NULL;
index 40757d2e4de87e651c2ee840e585454d5d298fe2..ebf60bd248908e036515bedbefb17312a11f7507 100644 (file)
@@ -24,8 +24,10 @@ static gchar *redis_db = NULL;
 static gchar *redis_password = NULL;
 static int64_t fuzzy_expiry = 0;
 
-static void rspamadm_fuzzyconvert (gint argc, gchar **argv);
-static const char *rspamadm_fuzzyconvert_help (gboolean full_help);
+static void rspamadm_fuzzyconvert (gint argc, gchar **argv,
+                                                                  const struct rspamadm_command *cmd);
+static const char *rspamadm_fuzzyconvert_help (gboolean full_help,
+                                                                                          const struct rspamadm_command *cmd);
 
 struct rspamadm_command fuzzyconvert_command = {
                .name = "fuzzyconvert",
@@ -51,7 +53,7 @@ static GOptionEntry entries[] = {
 
 
 static const char *
-rspamadm_fuzzyconvert_help (gboolean full_help)
+rspamadm_fuzzyconvert_help (gboolean full_help, const struct rspamadm_command *cmd)
 {
        const char *help_str;
 
@@ -72,7 +74,7 @@ rspamadm_fuzzyconvert_help (gboolean full_help)
 }
 
 static void
-rspamadm_fuzzyconvert (gint argc, gchar **argv)
+rspamadm_fuzzyconvert (gint argc, gchar **argv, const struct rspamadm_command *cmd)
 {
        GOptionContext *context;
        GError *error = NULL;
index 94631137c70e328773d2d51e8f832c38df93206f..f5e6847fad5eb734e789220a5c7bf08c02623d6a 100644 (file)
@@ -22,8 +22,10 @@ static gchar *target = NULL;
 static gchar **sources = NULL;
 static gboolean quiet;
 
-static void rspamadm_fuzzy_merge (gint argc, gchar **argv);
-static const char *rspamadm_fuzzy_merge_help (gboolean full_help);
+static void rspamadm_fuzzy_merge (gint argc, gchar **argv,
+                                                                 const struct rspamadm_command *cmd);
+static const char *rspamadm_fuzzy_merge_help (gboolean full_help,
+                                                                                         const struct rspamadm_command *cmd);
 
 struct rspamadm_command fuzzy_merge_command = {
                .name = "fuzzy_merge",
@@ -157,7 +159,7 @@ static struct rspamd_sqlite3_prstmt prepared_stmts[STMAX] = {
 };
 
 static const char *
-rspamadm_fuzzy_merge_help (gboolean full_help)
+rspamadm_fuzzy_merge_help (gboolean full_help, const struct rspamadm_command *cmd)
 {
        const char *help_str;
 
@@ -218,7 +220,7 @@ rspamadm_op_equal (gconstpointer a, gconstpointer b)
 }
 
 static void
-rspamadm_fuzzy_merge (gint argc, gchar **argv)
+rspamadm_fuzzy_merge (gint argc, gchar **argv, const struct rspamadm_command *cmd)
 {
        GOptionContext *context;
        GError *error = NULL;
index 5474ecf46573ccc3976c196d9db4116d59d9b894..60189f39c4227c74e33e504306122cad4e5abde0 100644 (file)
@@ -26,8 +26,10 @@ static gboolean orphans = FALSE;
 static gboolean partial = FALSE;
 static gboolean luapat = FALSE;
 
-static void rspamadm_grep (gint argc, gchar **argv);
-static const char *rspamadm_grep_help (gboolean full_help);
+static void rspamadm_grep (gint argc, gchar **argv,
+                                                  const struct rspamadm_command *cmd);
+static const char *rspamadm_grep_help (gboolean full_help,
+                                                                          const struct rspamadm_command *cmd);
 
 struct rspamadm_command grep_command = {
                .name = "grep",
@@ -57,7 +59,7 @@ static GOptionEntry entries[] = {
 
 
 static const char *
-rspamadm_grep_help (gboolean full_help)
+rspamadm_grep_help (gboolean full_help, const struct rspamadm_command *cmd)
 {
        const char *help_str;
 
@@ -81,7 +83,7 @@ rspamadm_grep_help (gboolean full_help)
 }
 
 static void
-rspamadm_grep (gint argc, gchar **argv)
+rspamadm_grep (gint argc, gchar **argv, const struct rspamadm_command *cmd)
 {
        GOptionContext *context;
        GError *error = NULL;
index 4214e005258d9d79000a3f8619c37983c8446404..a1d9e5d6bf8fb78db30cbd965dee2b8d594802fd 100644 (file)
@@ -25,8 +25,10 @@ static gboolean openssl = FALSE;
 static gboolean ucl = FALSE;
 static gboolean sign = FALSE;
 
-static void rspamadm_keypair (gint argc, gchar **argv);
-static const char *rspamadm_keypair_help (gboolean full_help);
+static void rspamadm_keypair (gint argc, gchar **argv,
+                                                         const struct rspamadm_command *cmd);
+static const char *rspamadm_keypair_help (gboolean full_help,
+                                                                                 const struct rspamadm_command *cmd);
 
 struct rspamadm_command keypair_command = {
                .name = "keypair",
@@ -51,7 +53,7 @@ static GOptionEntry entries[] = {
 };
 
 static const char *
-rspamadm_keypair_help (gboolean full_help)
+rspamadm_keypair_help (gboolean full_help, const struct rspamadm_command *cmd)
 {
        const char *help_str;
 
@@ -74,7 +76,7 @@ rspamadm_keypair_help (gboolean full_help)
 }
 
 static void
-rspamadm_keypair (gint argc, gchar **argv)
+rspamadm_keypair (gint argc, gchar **argv, const struct rspamadm_command *cmd)
 {
        GOptionContext *context;
        GError *error = NULL;
index e0ee7d9f178b03c60fc85cc8811d4f0789d66ae2..4ec934b912b4b3128c8bd8b025fde7f46bdc67f6 100644 (file)
@@ -47,8 +47,10 @@ static const char *default_history_file = ".rspamd_repl.hist";
 #endif
 #define MULTILINE_PROMPT "... "
 
-static void rspamadm_lua (gint argc, gchar **argv);
-static const char *rspamadm_lua_help (gboolean full_help);
+static void rspamadm_lua (gint argc, gchar **argv,
+               const struct rspamadm_command *cmd);
+static const char *rspamadm_lua_help (gboolean full_help,
+                                                                         const struct rspamadm_command *cmd);
 
 struct rspamadm_command lua_command = {
                .name = "lua",
@@ -121,7 +123,7 @@ static GOptionEntry entries[] = {
 };
 
 static const char *
-rspamadm_lua_help (gboolean full_help)
+rspamadm_lua_help (gboolean full_help, const struct rspamadm_command *cmd)
 {
        const char *help_str;
 
@@ -658,7 +660,7 @@ rspamadm_lua_handle_exec (struct rspamd_http_connection_entry *conn_ent,
 }
 
 static void
-rspamadm_lua (gint argc, gchar **argv)
+rspamadm_lua (gint argc, gchar **argv, const struct rspamadm_command *cmd)
 {
        GOptionContext *context;
        GError *error = NULL;
@@ -689,9 +691,6 @@ rspamadm_lua (gint argc, gchar **argv)
                }
        }
 
-       L = rspamd_lua_init ();
-       rspamd_lua_set_path (L, NULL, ucl_vars);
-
        if (paths) {
                for (elt = paths; *elt != NULL; elt ++) {
                        rspamadm_lua_add_path (L, *elt);
index 3299c479d86707680c1b938ccd6c9bbb4817c8ee..b00aac22367446ffda2d898eb7fda71550842501 100644 (file)
 #include "rspamadm.h"
 #include "unix-std.h"
 
-static void rspamadm_pw (gint argc, gchar **argv);
-static const char *rspamadm_pw_help (gboolean full_help);
+static void rspamadm_pw (gint argc, gchar **argv,
+                                                const struct rspamadm_command *cmd);
+static const char *rspamadm_pw_help (gboolean full_help,
+                                                                        const struct rspamadm_command *cmd);
 static void rspamadm_pw_lua_subrs (gpointer pL);
 
 static gboolean do_encrypt = FALSE;
@@ -57,7 +59,7 @@ static GOptionEntry entries[] = {
 };
 
 static const char *
-rspamadm_pw_help (gboolean full_help)
+rspamadm_pw_help (gboolean full_help, const struct rspamadm_command *cmd)
 {
        const char *help_str;
 
@@ -354,7 +356,7 @@ rspamadm_alg_list (void)
 }
 
 static void
-rspamadm_pw (gint argc, gchar **argv)
+rspamadm_pw (gint argc, gchar **argv, const struct rspamadm_command *cmd)
 {
        GOptionContext *context;
        GError *error = NULL;
index 9267400233a7bfd1b34cd05f7cd44a063c3d4b38..e30c6613a2c816f092621cdb71979f0306f0b476 100644 (file)
 #include "rspamadm.h"
 #include "lua/lua_common.h"
 
-#if !defined(WITH_TORCH) || !defined(WITH_LUAJIT)
-#define HAS_TORCH false
-#else
-#define HAS_TORCH true
-#endif
-
 static gchar *logdir = NULL;
 static gchar *output = "new.scores";
 static gboolean score_diff = false;  /* Print score diff flag */
@@ -33,9 +27,11 @@ extern struct rspamd_main *rspamd_main;
 extern module_t *modules[];
 extern worker_t *workers[];
 
-static void rspamadm_rescore (gint argc, gchar **argv);
+static void rspamadm_rescore (gint argc, gchar **argv,
+                                                         const struct rspamadm_command *cmd);
 
-static const char *rspamadm_rescore_help (gboolean full_help);
+static const char *rspamadm_rescore_help (gboolean full_help,
+                                                                                 const struct rspamadm_command *cmd);
 
 struct rspamadm_command rescore_command = {
                .name = "rescore",
@@ -75,8 +71,8 @@ config_logger (rspamd_mempool_t *pool, gpointer ud)
 }
 
 static const char *
-rspamadm_rescore_help (gboolean full_help) {
-
+rspamadm_rescore_help (gboolean full_help, const struct rspamadm_command *cmd)
+{
        const char *help_str;
 
        if (full_help) {
@@ -95,16 +91,21 @@ rspamadm_rescore_help (gboolean full_help) {
 }
 
 static void
-rspamadm_rescore (gint argc, gchar **argv) {
-
+rspamadm_rescore (gint argc, gchar **argv, const struct rspamadm_command *cmd)
+{
        GOptionContext *context;
        GError *error = NULL;
-       lua_State *L;
        struct rspamd_config *cfg = rspamd_main->cfg, **pcfg;
        gboolean ret = TRUE;
        worker_t **pworker;
        const gchar *confdir;
 
+#ifndef WITH_TORCH
+       rspamd_fprintf (stderr, "Torch is not enabled. "
+                               "Use -DENABLE_TORCH=ON option while running cmake.\n");
+       exit (EXIT_FAILURE);
+#endif
+
        context = g_option_context_new (
                        "rescore - estimate optimal symbol weights from log files");
 
@@ -123,12 +124,6 @@ rspamadm_rescore (gint argc, gchar **argv) {
                exit (EXIT_FAILURE);
        }
 
-       if (!HAS_TORCH) {
-               rspamd_fprintf (stderr, "Torch is not enabled. "
-                               "Use -DENABLE_TORCH=ON option while running cmake.\n");
-               exit (EXIT_FAILURE);
-       }
-
        if (logdir == NULL) {
                rspamd_fprintf (stderr, "Please specify log directory.\n");
                exit (EXIT_FAILURE);
@@ -176,8 +171,6 @@ rspamadm_rescore (gint argc, gchar **argv) {
        }
 
        if (ret) {
-               L = cfg->lua_state;
-               rspamd_lua_set_path (L, cfg->rcl_obj, ucl_vars);
                ucl_object_insert_key (cfg->rcl_obj, ucl_object_fromstring (cfg->cfg_name),
                                "config_path", 0, false);
                ucl_object_insert_key (cfg->rcl_obj, ucl_object_fromstring (logdir),
index b39c758975efd1d0f6816a2ed9b75d7644d77eab..0ec4ba40355ab16b032449b8ad39711ed181f329 100644 (file)
@@ -31,9 +31,10 @@ static gboolean show_help = FALSE;
 static gboolean show_version = FALSE;
 GHashTable *ucl_vars = NULL;
 struct rspamd_main *rspamd_main = NULL;
+lua_State *L = NULL;
 
-static void rspamadm_help (gint argc, gchar **argv);
-static const char* rspamadm_help_help (gboolean full_help);
+static void rspamadm_help (gint argc, gchar **argv, const struct rspamadm_command *);
+static const char* rspamadm_help_help (gboolean full_help, const struct rspamadm_command *);
 
 struct rspamadm_command help_command = {
        .name = "help",
@@ -83,26 +84,26 @@ rspamadm_usage (GOptionContext *context)
 }
 
 static void
-rspamadm_commands (void)
+rspamadm_commands (GPtrArray *all_commands)
 {
-       const struct rspamadm_command **cmd;
+       const struct rspamadm_command *cmd;
+       guint i;
 
        printf ("Rspamadm %s\n", RVERSION);
        printf ("Usage: rspamadm [global_options] command [command_options]\n");
        printf ("\nAvailable commands:\n");
 
-       cmd = commands;
-
-       while (*cmd) {
-               if (!((*cmd)->flags & RSPAMADM_FLAG_NOHELP)) {
-                       printf ("  %-18s %-60s\n", (*cmd)->name, (*cmd)->help (FALSE));
+       PTR_ARRAY_FOREACH (all_commands, i, cmd) {
+               if (!(cmd->flags & RSPAMADM_FLAG_NOHELP)) {
+                       printf ("  %-18s %-60s\n", cmd->name, cmd->help (FALSE, cmd));
                }
+
                cmd ++;
        }
 }
 
 static const char *
-rspamadm_help_help (gboolean full_help)
+rspamadm_help_help (gboolean full_help, const struct rspamadm_command *cmd)
 {
        const char *help_str;
 
@@ -118,10 +119,11 @@ rspamadm_help_help (gboolean full_help)
 }
 
 static void
-rspamadm_help (gint argc, gchar **argv)
+rspamadm_help (gint argc, gchar **argv, const struct rspamadm_command *command)
 {
        const gchar *cmd_name;
-       const struct rspamadm_command *cmd, **cmd_list;
+       const struct rspamadm_command *cmd;
+       GPtrArray *all_commands = (GPtrArray *)command->command_data;
 
        printf ("Rspamadm %s\n", RVERSION);
        printf ("Usage: rspamadm [global_options] command [command_options]\n\n");
@@ -134,7 +136,7 @@ rspamadm_help (gint argc, gchar **argv)
                printf ("Showing help for %s command\n\n", cmd_name);
        }
 
-       cmd = rspamadm_search_command (cmd_name);
+       cmd = rspamadm_search_command (cmd_name, all_commands);
 
        if (cmd == NULL) {
                fprintf (stderr, "Invalid command name: %s\n", cmd_name);
@@ -142,20 +144,18 @@ rspamadm_help (gint argc, gchar **argv)
        }
 
        if (strcmp (cmd_name, "help") == 0) {
+               guint i;
                printf ("Available commands:\n");
 
-               cmd_list = commands;
-
-               while (*cmd_list) {
-                       if (!((*cmd_list)->flags & RSPAMADM_FLAG_NOHELP)) {
-                               printf ("  %-18s %-60s\n", (*cmd_list)->name,
-                                               (*cmd_list)->help (FALSE));
+               PTR_ARRAY_FOREACH (all_commands, i, cmd) {
+                       if (!(cmd->flags & RSPAMADM_FLAG_NOHELP)) {
+                               printf ("  %-18s %-60s\n", cmd->name,
+                                               cmd->help (FALSE, cmd));
                        }
-                       cmd_list++;
                }
        }
        else {
-               printf ("%s\n", cmd->help (TRUE));
+               printf ("%s\n", cmd->help (TRUE, cmd));
        }
 }
 
@@ -222,6 +222,11 @@ rspamadm_execute_lua_ucl_subr (gpointer pL, gint argc, gchar **argv,
                return FALSE;
        }
        else {
+               if (lua_type (L, -1) == LUA_TTABLE) {
+                       lua_pushstring (L, "handler");
+                       lua_gettable (L, -2);
+               }
+
                if (lua_type (L, -1) != LUA_TFUNCTION) {
                        msg_err ("lua script must return "
                                        "function and not %s",
@@ -278,12 +283,13 @@ main (gint argc, gchar **argv, gchar **env)
        gchar **nargv, **targv;
        const gchar *cmd_name;
        const struct rspamadm_command *cmd;
+       GPtrArray *all_commands = g_ptr_array_new (); /* Discovered during check */
        gint i, nargc, targc;
 
        ucl_vars = g_hash_table_new_full (rspamd_strcase_hash,
                rspamd_strcase_equal, g_free, g_free);
        process_quark = g_quark_from_static_string ("rspamadm");
-       cfg = rspamd_config_new ();
+       cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_DEFAULT);
        cfg->libs_ctx = rspamd_init_libs ();
        rspamd_main = g_malloc0 (sizeof (*rspamd_main));
        rspamd_main->cfg = cfg;
@@ -291,6 +297,8 @@ main (gint argc, gchar **argv, gchar **env)
        rspamd_main->type = process_quark;
        rspamd_main->server_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (),
                        "rspamadm");
+       rspamadm_fill_internal_commands (all_commands);
+       help_command.command_data = all_commands;
 
        /* Setup logger */
        if (verbose) {
@@ -349,6 +357,9 @@ main (gint argc, gchar **argv, gchar **env)
                exit (1);
        }
 
+       L = cfg->lua_state;
+       rspamd_lua_set_path (L, NULL, ucl_vars);
+
        g_strfreev (nargv);
 
        if (show_version) {
@@ -360,7 +371,7 @@ main (gint argc, gchar **argv, gchar **env)
                exit (EXIT_SUCCESS);
        }
        if (list_commands) {
-               rspamadm_commands ();
+               rspamadm_commands (all_commands);
                exit (EXIT_SUCCESS);
        }
 
@@ -370,7 +381,7 @@ main (gint argc, gchar **argv, gchar **env)
                cmd_name = "help";
        }
 
-       cmd = rspamadm_search_command (cmd_name);
+       cmd = rspamadm_search_command (cmd_name, all_commands);
 
        if (cmd == NULL) {
                fprintf (stderr, "Invalid command name: %s\n", cmd_name);
@@ -387,16 +398,18 @@ main (gint argc, gchar **argv, gchar **env)
 
                targc = argc - nargc;
                targv = nargv;
-               cmd->run (targc, targv);
+               cmd->run (targc, targv, cmd);
                g_strfreev (nargv);
        }
        else {
-               cmd->run (0, NULL);
+               cmd->run (0, NULL, cmd);
        }
 
        rspamd_log_close (rspamd_main->logger);
        REF_RELEASE (rspamd_main->cfg);
+       lua_close (L);
        g_free (rspamd_main);
+       g_ptr_array_free (all_commands, TRUE);
 
        return 0;
 }
index 9a209fd91c82da322ad3b9e47d32e6758c972bb0..3f619e52c6aebeba35f74d381de5d26c961aadc5 100644 (file)
 
 #include "config.h"
 #include "ucl.h"
+#include <lua.h>
+#include <lauxlib.h>
+#include <lualib.h>
 
 extern GHashTable *ucl_vars;
+extern lua_State *L;
 
 GQuark rspamadm_error (void);
 
-typedef const gchar* (*rspamadm_help_func) (gboolean full_help);
-typedef void (*rspamadm_run_func) (gint argc, gchar **argv);
+struct rspamadm_command;
+typedef const gchar* (*rspamadm_help_func) (gboolean full_help,
+                                                                                       const struct rspamadm_command *cmd);
+typedef void (*rspamadm_run_func) (gint argc, gchar **argv,
+                                                                  const struct rspamadm_command *cmd);
 typedef void (*rspamadm_lua_exports_func) (gpointer lua_state);
 
 #define RSPAMADM_FLAG_NOHELP (1 << 0)
+#define RSPAMADM_FLAG_LUA (1 << 0)
 
 struct rspamadm_command {
        const gchar *name;
@@ -35,14 +43,19 @@ struct rspamadm_command {
        rspamadm_help_func help;
        rspamadm_run_func run;
        rspamadm_lua_exports_func lua_subrs;
+       gint cbref;
+       gpointer command_data; /* Opaque data */
 };
 
 extern const struct rspamadm_command *commands[];
 extern struct rspamadm_command help_command;
 
-const struct rspamadm_command *rspamadm_search_command (const gchar *name);
+const struct rspamadm_command *rspamadm_search_command (const gchar *name,
+               GPtrArray *all_commands);
+void rspamadm_fill_internal_commands (GPtrArray *dest);
 
 gboolean rspamadm_execute_lua_ucl_subr (gpointer L, gint argc, gchar **argv,
-               const ucl_object_t *res, const gchar *script_name);
+                                                                               const ucl_object_t *res,
+                                                                               const gchar *script_name);
 
 #endif
index 0ae7e22c57fbd08e69fdaf1f85a9368cfdc08885..af8d05c961e1df71ea28d541e44d3ce90fed0cde 100644 (file)
@@ -39,8 +39,10 @@ static gchar *editor = NULL;
 static gboolean edit = FALSE;
 enum rspamd_cryptobox_mode mode = RSPAMD_CRYPTOBOX_MODE_25519;
 
-static void rspamadm_signtool (gint argc, gchar **argv);
-static const char *rspamadm_signtool_help (gboolean full_help);
+static void rspamadm_signtool (gint argc, gchar **argv,
+                                                          const struct rspamadm_command *cmd);
+static const char *rspamadm_signtool_help (gboolean full_help,
+                                                                                  const struct rspamadm_command *cmd);
 
 struct rspamadm_command signtool_command = {
                .name = "signtool",
@@ -75,7 +77,8 @@ static GOptionEntry entries[] = {
 };
 
 static const char *
-rspamadm_signtool_help (gboolean full_help)
+rspamadm_signtool_help (gboolean full_help,
+                                               const struct rspamadm_command *cmd)
 {
        const char *help_str;
 
@@ -462,7 +465,7 @@ rspamadm_verify_file (const gchar *fname, const guchar *pk)
 
 
 static void
-rspamadm_signtool (gint argc, gchar **argv)
+rspamadm_signtool (gint argc, gchar **argv, const struct rspamadm_command *cmd)
 {
        GOptionContext *context;
        GError *error = NULL;
index e4cf24629a19af8e510e83c6c0b836d6bbffa5c3..68723c40f721037608259678b5811589a52b0172 100644 (file)
@@ -37,8 +37,10 @@ static gchar *redis_db = NULL;
 static gchar *redis_password = NULL;
 static gboolean reset_previous = FALSE;
 
-static void rspamadm_statconvert (gint argc, gchar **argv);
-static const char *rspamadm_statconvert_help (gboolean full_help);
+static void rspamadm_statconvert (gint argc, gchar **argv,
+                                                                 const struct rspamadm_command *cmd);
+static const char *rspamadm_statconvert_help (gboolean full_help,
+                                                                                         const struct rspamadm_command *cmd);
 
 struct rspamadm_command statconvert_command = {
                .name = "statconvert",
@@ -77,7 +79,7 @@ static GOptionEntry entries[] = {
 
 
 static const char *
-rspamadm_statconvert_help (gboolean full_help)
+rspamadm_statconvert_help (gboolean full_help, const struct rspamadm_command *cmd)
 {
        const char *help_str;
 
@@ -107,7 +109,7 @@ rspamadm_statconvert_help (gboolean full_help)
 }
 
 static void
-rspamadm_statconvert (gint argc, gchar **argv)
+rspamadm_statconvert (gint argc, gchar **argv, const struct rspamadm_command *cmd)
 {
        GOptionContext *context;
        GError *error = NULL;
index cc3be33572381a9d948c07e060f36998cb6e3dcf..d6490d6aa53aadc668bef473fd68d5c07757ee2c 100644 (file)
@@ -276,7 +276,7 @@ reread_config (struct rspamd_main *rspamd_main)
        gchar *cfg_file;
 
        rspamd_symbols_cache_save (rspamd_main->cfg->cache);
-       tmp_cfg = rspamd_config_new ();
+       tmp_cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_DEFAULT);
        g_hash_table_unref (tmp_cfg->c_modules);
        tmp_cfg->c_modules = g_hash_table_ref (rspamd_main->cfg->c_modules);
        tmp_cfg->libs_ctx = rspamd_main->cfg->libs_ctx;
@@ -1180,7 +1180,7 @@ main (gint argc, gchar **argv, gchar **env)
                        "main");
        rspamd_main->stat = rspamd_mempool_alloc0_shared (rspamd_main->server_pool,
                        sizeof (struct rspamd_stat));
-       rspamd_main->cfg = rspamd_config_new ();
+       rspamd_main->cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_DEFAULT);
        rspamd_main->spairs = g_hash_table_new_full (rspamd_spair_hash,
                        rspamd_spair_equal, g_free, rspamd_spair_close);
        rspamd_main->start_mtx = rspamd_mempool_get_mutex (rspamd_main->server_pool);
index d6fa237a364a0464faf46a2d8b1d60aee7da9ae5..2f8e893deb18cf197dd072957450555764ba3627 100644 (file)
@@ -15,7 +15,7 @@ main (int argc, char **argv)
        rspamd_main = (struct rspamd_main *)g_malloc (sizeof (struct rspamd_main));
        memset (rspamd_main, 0, sizeof (struct rspamd_main));
        rspamd_main->server_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL);
-       cfg = rspamd_config_new ();
+       cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_DEFAULT);
        rspamd_main->cfg = cfg;
        cfg->cfg_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL);
        cfg->log_type = RSPAMD_LOG_CONSOLE;
index 47094398f71c19e162d9b2f26f57bd228dcce225..cce0008a927d757a5b85f25258b7b4d295559e2a 100644 (file)
@@ -64,7 +64,7 @@ rspamd_upstream_test_func (void)
        struct timeval tv;
        rspamd_inet_addr_t *addr, *next_addr, *paddr;
 
-       cfg = rspamd_config_new ();
+       cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_SKIP_LUA);
        cfg->dns_retransmits = 2;
        cfg->dns_timeout = 0.5;
        cfg->upstream_max_errors = 1;