static gint rspamd_control_finish_handler (struct rspamd_http_connection *conn, struct rspamd_http_message *msg) { struct ucl_parser *parser; ucl_object_t *obj; rspamd_fstring_t *out; const gchar *body; gsize body_len; struct rspamadm_control_cbdata *cbdata = conn->ud; body = rspamd_http_message_get_body (msg, &body_len); parser = ucl_parser_new (0); if (!body || !ucl_parser_add_chunk (parser, body, body_len)) { rspamd_fprintf (stderr, "cannot parse server's reply: %s\n", ucl_parser_get_error (parser)); ucl_parser_free (parser); } else { obj = ucl_parser_get_object (parser); out = rspamd_fstring_new (); if (json) { rspamd_ucl_emit_fstring (obj, UCL_EMIT_JSON, &out); } else if (compact) { rspamd_ucl_emit_fstring (obj, UCL_EMIT_JSON_COMPACT, &out); } else { if (strcmp (cbdata->path, "/fuzzystat") == 0) { rspamadm_execute_lua_ucl_subr (cbdata->L, cbdata->argc, cbdata->argv, obj, rspamadm_script_fuzzy_stat); rspamd_fstring_free (out); ucl_object_unref (obj); ucl_parser_free (parser); return 0; } else { rspamd_ucl_emit_fstring (obj, UCL_EMIT_CONFIG, &out); } } rspamd_fprintf (stdout, "%V", out); rspamd_fstring_free (out); ucl_object_unref (obj); ucl_parser_free (parser); } return 0; }
static void rspamadm_confighelp_show (struct rspamd_config *cfg, gint argc, gchar **argv, const char *key, const ucl_object_t *obj) { rspamd_fstring_t *out; rspamd_lua_set_path (cfg->lua_state, NULL, ucl_vars); out = rspamd_fstring_new (); if (json) { rspamd_ucl_emit_fstring (obj, UCL_EMIT_JSON, &out); } else if (compact) { rspamd_ucl_emit_fstring (obj, UCL_EMIT_JSON_COMPACT, &out); } else { /* TODO: add lua helper for output */ if (key) { rspamd_fprintf (stdout, "Showing help for %s%s:\n", keyword ? "keyword " : "", key); } else { rspamd_fprintf (stdout, "Showing help for all options:\n"); } rspamadm_execute_lua_ucl_subr (argc, argv, obj, "confighelp", TRUE); rspamd_fstring_free (out); return; } rspamd_fprintf (stdout, "%V", out); rspamd_fprintf (stdout, "\n"); rspamd_fstring_free (out); }
static void rspamadm_rescore (gint argc, gchar **argv) { GOptionContext *context; GError *error = NULL; lua_State *L; ucl_object_t *obj; context = g_option_context_new ( "rescore - Estimate optimal symbol weights from log files"); g_option_context_set_summary (context, "Summary:\n Rspamd administration utility version " RVERSION "\n Release id: " RID); g_option_context_add_main_entries (context, entries, NULL); g_option_context_set_ignore_unknown_options (context, TRUE); if (!g_option_context_parse (context, &argc, &argv, &error)) { rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message); g_error_free (error); 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); } L = rspamd_lua_init (); rspamd_lua_set_path (L, NULL, NULL); obj = ucl_object_typed_new (UCL_OBJECT); ucl_object_insert_key (obj, ucl_object_fromstring (logdir), "logdir", 0, false); ucl_object_insert_key (obj, ucl_object_fromstring (output), "output", 0, false); ucl_object_insert_key (obj, ucl_object_fromdouble (threshold), "threshold", 0, false); ucl_object_insert_key (obj, ucl_object_fromint (iters), "iters", 0, false); ucl_object_insert_key (obj, ucl_object_frombool (score_diff), "diff", 0, false); rspamadm_execute_lua_ucl_subr (L, argc, argv, obj, "rescore"); lua_close (L); ucl_object_unref (obj); }
static void rspamadm_statconvert (gint argc, gchar **argv) { GOptionContext *context; GError *error = NULL; lua_State *L; ucl_object_t *obj; context = g_option_context_new ( "statconvert - converts statistics from sqlite3 to redis"); g_option_context_set_summary (context, "Summary:\n Rspamd administration utility version " RVERSION "\n Release id: " RID); g_option_context_add_main_entries (context, entries, NULL); g_option_context_set_ignore_unknown_options (context, TRUE); if (!g_option_context_parse (context, &argc, &argv, &error)) { rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message); g_error_free (error); exit (1); } if (!source_db) { rspamd_fprintf (stderr, "source db is missing\n"); exit (1); } if (!redis_host) { rspamd_fprintf (stderr, "redis host is missing\n"); exit (1); } if (!symbol) { rspamd_fprintf (stderr, "symbol is missing\n"); exit (1); } L = rspamd_lua_init (); obj = ucl_object_typed_new (UCL_OBJECT); ucl_object_insert_key (obj, ucl_object_fromstring (source_db), "source_db", 0, false); ucl_object_insert_key (obj, ucl_object_fromstring (redis_host), "redis_host", 0, false); ucl_object_insert_key (obj, ucl_object_fromstring (symbol), "symbol", 0, false); if (cache_db != NULL) { ucl_object_insert_key (obj, ucl_object_fromstring (cache_db), "cache_db", 0, false); } rspamadm_execute_lua_ucl_subr (L, argc, argv, obj, rspamadm_script_stat_convert); lua_close (L); ucl_object_unref (obj); }
static void rspamadm_statconvert (gint argc, gchar **argv, const struct rspamadm_command *cmd) { GOptionContext *context; GError *error = NULL; ucl_object_t *obj; context = g_option_context_new ( "statconvert - converts statistics from sqlite3 to redis"); g_option_context_set_summary (context, "Summary:\n Rspamd administration utility version " RVERSION "\n Release id: " RID); g_option_context_add_main_entries (context, entries, NULL); g_option_context_set_ignore_unknown_options (context, TRUE); if (!g_option_context_parse (context, &argc, &argv, &error)) { rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message); g_error_free (error); exit (1); } if (config_file) { /* Load config file, assuming that it has all information required */ struct ucl_parser *parser; parser = ucl_parser_new (0); rspamd_ucl_add_conf_variables (parser, ucl_vars); if (!ucl_parser_add_file (parser, config_file)) { msg_err ("ucl parser error: %s", ucl_parser_get_error (parser)); ucl_parser_free (parser); exit (EXIT_FAILURE); } obj = ucl_parser_get_object (parser); ucl_parser_free (parser); } else { /* We need to get all information from the command line */ ucl_object_t *classifier, *statfile_ham, *statfile_spam, *tmp, *redis; /* Check arguments sanity */ if (spam_db == NULL) { msg_err ("No spam-db specified"); exit (EXIT_FAILURE); } if (ham_db == NULL) { msg_err ("No ham-db specified"); exit (EXIT_FAILURE); } if (redis_host == NULL) { msg_err ("No redis-host specified"); exit (EXIT_FAILURE); } if (symbol_ham == NULL) { msg_err ("No symbol-ham specified"); exit (EXIT_FAILURE); } if (symbol_spam == NULL) { msg_err ("No symbol-spam specified"); exit (EXIT_FAILURE); } obj = ucl_object_typed_new (UCL_OBJECT); classifier = ucl_object_typed_new (UCL_OBJECT); ucl_object_insert_key (obj, classifier, "classifier", 0, false); /* Now we need to create "bayes" key in it */ tmp = ucl_object_typed_new (UCL_OBJECT); ucl_object_insert_key (classifier, tmp, "bayes", 0, false); classifier = tmp; ucl_object_insert_key (classifier, ucl_object_fromstring ("sqlite3"), "backend", 0, false); if (cache_db != NULL) { ucl_object_t *cache; cache = ucl_object_typed_new (UCL_OBJECT); ucl_object_insert_key (cache, ucl_object_fromstring ("sqlite3"), "type", 0, false); ucl_object_insert_key (cache, ucl_object_fromstring (cache_db), "file", 0, false); ucl_object_insert_key (classifier, cache, "cache", 0, false); } statfile_ham = ucl_object_typed_new (UCL_OBJECT); ucl_object_insert_key (statfile_ham, ucl_object_fromstring (symbol_ham), "symbol", 0, false); ucl_object_insert_key (statfile_ham, ucl_object_frombool (false), "spam", 0, false); ucl_object_insert_key (statfile_ham, ucl_object_fromstring (ham_db), "db", 0, false); statfile_spam = ucl_object_typed_new (UCL_OBJECT); ucl_object_insert_key (statfile_spam, ucl_object_fromstring (symbol_spam), "symbol", 0, false); ucl_object_insert_key (statfile_spam, ucl_object_frombool (true), "spam", 0, false); ucl_object_insert_key (statfile_spam, ucl_object_fromstring (spam_db), "db", 0, false); DL_APPEND (statfile_ham, statfile_spam); ucl_object_insert_key (classifier, statfile_ham, "statfile", 0, false); /* Deal with redis */ redis = ucl_object_typed_new (UCL_OBJECT); ucl_object_insert_key (obj, redis, "redis", 0, false); ucl_object_insert_key (redis, ucl_object_fromstring (redis_host), "servers", 0, false); if (redis_db) { ucl_object_insert_key (redis, ucl_object_fromstring (redis_db), "dbname", 0, false); } if (redis_password) { ucl_object_insert_key (redis, ucl_object_fromstring (redis_password), "password", 0, false); } } ucl_object_insert_key (obj, ucl_object_frombool (reset_previous), "reset_previous", 0, false); if (expire != 0) { ucl_object_insert_key (obj, ucl_object_fromdouble (expire), "expire", 0, false); } rspamadm_execute_lua_ucl_subr (argc, argv, obj, "stat_convert", TRUE); ucl_object_unref (obj); }