Przeglądaj źródła

[Project] Rework rspamadm and Lua init path

tags/1.7.6
Vsevolod Stakhov 6 lat temu
rodzic
commit
25ef6b9821

+ 7
- 1
src/libserver/cfg_file.h Wyświetl plik

@@ -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

+ 10
- 3
src/libserver/cfg_utils.c Wyświetl plik

@@ -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) {

+ 0
- 3
src/lua/lua_common.c Wyświetl plik

@@ -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;
}


+ 4
- 2
src/lua/lua_util.c Wyświetl plik

@@ -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);

+ 18
- 7
src/rspamadm/commands.c Wyświetl plik

@@ -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]);
}
}
}

+ 4
- 4
src/rspamadm/configdump.c Wyświetl plik

@@ -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;

+ 8
- 5
src/rspamadm/confighelp.c Wyświetl plik

@@ -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;


+ 6
- 4
src/rspamadm/configtest.c Wyświetl plik

@@ -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;

+ 7
- 4
src/rspamadm/configwizard.c Wyświetl plik

@@ -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;

+ 6
- 4
src/rspamadm/control.c Wyświetl plik

@@ -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;

+ 6
- 4
src/rspamadm/corpus_test.c Wyświetl plik

@@ -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;

+ 6
- 4
src/rspamadm/dkim_keygen.c Wyświetl plik

@@ -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;

+ 6
- 4
src/rspamadm/fuzzy_convert.c Wyświetl plik

@@ -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;

+ 6
- 4
src/rspamadm/fuzzy_merge.c Wyświetl plik

@@ -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;

+ 6
- 4
src/rspamadm/grep.c Wyświetl plik

@@ -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;

+ 6
- 4
src/rspamadm/keypair.c Wyświetl plik

@@ -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;

+ 6
- 7
src/rspamadm/lua_repl.c Wyświetl plik

@@ -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);

+ 6
- 4
src/rspamadm/pw.c Wyświetl plik

@@ -21,8 +21,10 @@
#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;

+ 14
- 21
src/rspamadm/rescore.c Wyświetl plik

@@ -18,12 +18,6 @@
#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),

+ 39
- 26
src/rspamadm/rspamadm.c Wyświetl plik

@@ -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;
}

+ 17
- 4
src/rspamadm/rspamadm.h Wyświetl plik

@@ -18,16 +18,24 @@

#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

+ 7
- 4
src/rspamadm/signtool.c Wyświetl plik

@@ -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;

+ 6
- 4
src/rspamadm/stat_convert.c Wyświetl plik

@@ -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;

+ 2
- 2
src/rspamd.c Wyświetl plik

@@ -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);

+ 1
- 1
test/rspamd_test_suite.c Wyświetl plik

@@ -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;

+ 1
- 1
test/rspamd_upstream_test.c Wyświetl plik

@@ -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;

Ładowanie…
Anuluj
Zapisz