You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rspamadm.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "config.h"
  17. #include "rspamadm.h"
  18. #include "rspamd.h"
  19. #include "ottery.h"
  20. #include "lua/lua_common.h"
  21. #include "lua/lua_thread_pool.h"
  22. #include "lua_ucl.h"
  23. #include "unix-std.h"
  24. #ifdef HAVE_LIBUTIL_H
  25. #include <libutil.h>
  26. #endif
  27. static gboolean verbose = FALSE;
  28. static gboolean list_commands = FALSE;
  29. static gboolean show_help = FALSE;
  30. static gboolean show_version = FALSE;
  31. GHashTable *ucl_vars = NULL;
  32. struct rspamd_main *rspamd_main = NULL;
  33. struct rspamd_async_session *rspamadm_session = NULL;
  34. lua_State *L = NULL;
  35. /* Defined in modules.c */
  36. extern module_t *modules[];
  37. extern worker_t *workers[];
  38. static void rspamadm_help (gint argc, gchar **argv, const struct rspamadm_command *);
  39. static const char* rspamadm_help_help (gboolean full_help, const struct rspamadm_command *);
  40. struct rspamadm_command help_command = {
  41. .name = "help",
  42. .flags = RSPAMADM_FLAG_NOHELP,
  43. .help = rspamadm_help_help,
  44. .run = rspamadm_help
  45. };
  46. static gboolean rspamadm_parse_ucl_var (const gchar *option_name,
  47. const gchar *value, gpointer data,
  48. GError **error);
  49. static GOptionEntry entries[] = {
  50. {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
  51. "Enable verbose logging", NULL},
  52. {"list-commands", 'l', 0, G_OPTION_ARG_NONE, &list_commands,
  53. "List available commands", NULL},
  54. {"var", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer)&rspamadm_parse_ucl_var,
  55. "Redefine UCL variable", NULL},
  56. {"help", 'h', 0, G_OPTION_ARG_NONE, &show_help,
  57. "Show help", NULL},
  58. {"version", 'V', 0, G_OPTION_ARG_NONE, &show_version,
  59. "Show version", NULL},
  60. {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}
  61. };
  62. GQuark
  63. rspamadm_error (void)
  64. {
  65. return g_quark_from_static_string ("rspamadm");
  66. }
  67. static void
  68. rspamadm_version (void)
  69. {
  70. rspamd_printf ("Rspamadm %s\n", RVERSION);
  71. }
  72. static void
  73. rspamadm_usage (GOptionContext *context)
  74. {
  75. gchar *help_str;
  76. help_str = g_option_context_get_help (context, TRUE, NULL);
  77. rspamd_printf ("%s", help_str);
  78. }
  79. static void
  80. rspamadm_commands (GPtrArray *all_commands)
  81. {
  82. const struct rspamadm_command *cmd;
  83. guint i;
  84. rspamd_printf ("Rspamadm %s\n", RVERSION);
  85. rspamd_printf ("Usage: rspamadm [global_options] command [command_options]\n");
  86. rspamd_printf ("\nAvailable commands:\n");
  87. PTR_ARRAY_FOREACH (all_commands, i, cmd) {
  88. if (!(cmd->flags & RSPAMADM_FLAG_NOHELP)) {
  89. if (cmd->flags & RSPAMADM_FLAG_LUA) {
  90. (void)cmd->help (FALSE, cmd);
  91. }
  92. else {
  93. printf (" %-18s %-60s\n", cmd->name, cmd->help (FALSE, cmd));
  94. }
  95. }
  96. }
  97. }
  98. static const char *
  99. rspamadm_help_help (gboolean full_help, const struct rspamadm_command *cmd)
  100. {
  101. const char *help_str;
  102. if (full_help) {
  103. help_str = "Shows help for a specified command\n"
  104. "Usage: rspamadm help <command>";
  105. }
  106. else {
  107. help_str = "Shows help for a specified command";
  108. }
  109. return help_str;
  110. }
  111. static void
  112. rspamadm_help (gint argc, gchar **argv, const struct rspamadm_command *command)
  113. {
  114. const gchar *cmd_name;
  115. const struct rspamadm_command *cmd;
  116. GPtrArray *all_commands = (GPtrArray *)command->command_data;
  117. rspamd_printf ("Rspamadm %s\n", RVERSION);
  118. rspamd_printf ("Usage: rspamadm [global_options] command [command_options]\n\n");
  119. if (argc <= 1) {
  120. cmd_name = "help";
  121. }
  122. else {
  123. cmd_name = argv[1];
  124. rspamd_printf ("Showing help for %s command\n\n", cmd_name);
  125. }
  126. cmd = rspamadm_search_command (cmd_name, all_commands);
  127. if (cmd == NULL) {
  128. fprintf (stderr, "Invalid command name: %s\n", cmd_name);
  129. exit (EXIT_FAILURE);
  130. }
  131. if (strcmp (cmd_name, "help") == 0) {
  132. guint i;
  133. rspamd_printf ("Available commands:\n");
  134. PTR_ARRAY_FOREACH (all_commands, i, cmd) {
  135. if (!(cmd->flags & RSPAMADM_FLAG_NOHELP)) {
  136. if (!(cmd->flags & RSPAMADM_FLAG_LUA)) {
  137. printf (" %-18s %-60s\n", cmd->name,
  138. cmd->help (FALSE, cmd));
  139. }
  140. else {
  141. /* Just call lua subr */
  142. (void)cmd->help (FALSE, cmd);
  143. }
  144. }
  145. }
  146. }
  147. else {
  148. if (!(cmd->flags & RSPAMADM_FLAG_LUA)) {
  149. rspamd_printf ("%s\n", cmd->help (TRUE, cmd));
  150. }
  151. else {
  152. /* Just call lua subr */
  153. (void)cmd->help (TRUE, cmd);
  154. }
  155. }
  156. }
  157. static gboolean
  158. rspamadm_parse_ucl_var (const gchar *option_name,
  159. const gchar *value, gpointer data,
  160. GError **error)
  161. {
  162. gchar *k, *v, *t;
  163. t = strchr (value, '=');
  164. if (t != NULL) {
  165. k = g_strdup (value);
  166. t = k + (t - value);
  167. v = g_strdup (t + 1);
  168. *t = '\0';
  169. g_hash_table_insert (ucl_vars, k, v);
  170. }
  171. else {
  172. g_set_error (error, rspamadm_error (), EINVAL,
  173. "Bad variable format: %s", value);
  174. return FALSE;
  175. }
  176. return TRUE;
  177. }
  178. static void
  179. lua_thread_str_error_cb (struct thread_entry *thread, int ret, const char *msg)
  180. {
  181. struct lua_call_data *cd = thread->cd;
  182. msg_err ("call to rspamadm lua script failed (%d): %s", ret, msg);
  183. cd->ret = ret;
  184. }
  185. gboolean
  186. rspamadm_execute_lua_ucl_subr (gint argc, gchar **argv,
  187. const ucl_object_t *res,
  188. const gchar *script_name,
  189. gboolean rspamadm_subcommand)
  190. {
  191. struct thread_entry *thread = lua_thread_pool_get_for_config (rspamd_main->cfg);
  192. lua_State *L = thread->lua_state;
  193. gint i;
  194. gchar str[PATH_MAX];
  195. g_assert (script_name != NULL);
  196. g_assert (res != NULL);
  197. g_assert (L != NULL);
  198. /* Init internal rspamadm routines */
  199. if (rspamadm_subcommand) {
  200. rspamd_snprintf (str, sizeof (str), "return require \"%s.%s\"", "rspamadm",
  201. script_name);
  202. }
  203. else {
  204. rspamd_snprintf (str, sizeof (str), "return require \"%s\"",
  205. script_name);
  206. }
  207. if (luaL_dostring (L, str) != 0) {
  208. msg_err ("cannot execute lua script %s: %s",
  209. str, lua_tostring (L, -1));
  210. return FALSE;
  211. }
  212. else {
  213. if (lua_type (L, -1) == LUA_TTABLE) {
  214. lua_pushstring (L, "handler");
  215. lua_gettable (L, -2);
  216. }
  217. if (lua_type (L, -1) != LUA_TFUNCTION) {
  218. msg_err ("lua script must return "
  219. "function and not %s",
  220. lua_typename (L, lua_type (L, -1)));
  221. return FALSE;
  222. }
  223. }
  224. /* Push function */
  225. lua_pushvalue (L, -1);
  226. /* Push argv */
  227. lua_newtable (L);
  228. for (i = 1; i < argc; i ++) {
  229. lua_pushstring (L, argv[i]);
  230. lua_rawseti (L, -2, i);
  231. }
  232. /* Push results */
  233. ucl_object_push_lua (L, res, TRUE);
  234. if (lua_repl_thread_call (thread, 2, NULL, lua_thread_str_error_cb) != 0) {
  235. return FALSE;
  236. }
  237. /* error function */
  238. lua_settop (L, 0);
  239. return TRUE;
  240. }
  241. static gint
  242. rspamdadm_commands_sort_func (gconstpointer a, gconstpointer b)
  243. {
  244. const struct rspamadm_command *cmda = *((struct rspamadm_command const **)a),
  245. *cmdb = *((struct rspamadm_command const **)b);
  246. return strcmp (cmda->name, cmdb->name);
  247. }
  248. static gboolean
  249. rspamadm_command_maybe_match_name (const gchar *cmd, const gchar *input)
  250. {
  251. gsize clen, inplen;
  252. clen = strlen (cmd);
  253. inplen = strlen (input);
  254. if (rspamd_strings_levenshtein_distance (cmd, clen,
  255. input, inplen, 1) == 1) {
  256. return TRUE;
  257. }
  258. else if ((clen > inplen &&
  259. rspamd_substring_search (cmd, clen, input, inplen) != -1) ||
  260. (inplen > clen &&
  261. rspamd_substring_search (input, inplen, cmd, clen) != -1)) {
  262. return TRUE;
  263. }
  264. return FALSE;
  265. }
  266. static void
  267. rspamadm_add_lua_globals (void)
  268. {
  269. struct rspamd_async_session **psession;
  270. struct event_base **pev_base;
  271. rspamadm_session = rspamd_session_create (rspamd_main->cfg->cfg_pool, NULL,
  272. NULL, (event_finalizer_t )NULL, NULL);
  273. psession = lua_newuserdata (L, sizeof (struct rspamd_async_session*));
  274. rspamd_lua_setclass (L, "rspamd{session}", -1);
  275. *psession = rspamadm_session;
  276. lua_setglobal (L, "rspamadm_session");
  277. pev_base = lua_newuserdata (L, sizeof (struct event_base *));
  278. rspamd_lua_setclass (L, "rspamd{ev_base}", -1);
  279. *pev_base = rspamd_main->ev_base;
  280. lua_setglobal (L, "rspamadm_ev_base");
  281. }
  282. gint
  283. main (gint argc, gchar **argv, gchar **env)
  284. {
  285. GError *error = NULL;
  286. GOptionContext *context;
  287. GOptionGroup *og;
  288. struct rspamd_config *cfg;
  289. GQuark process_quark;
  290. gchar **nargv, **targv;
  291. const gchar *cmd_name;
  292. const struct rspamadm_command *cmd;
  293. GPtrArray *all_commands = g_ptr_array_new (); /* Discovered during check */
  294. gint i, nargc, targc;
  295. worker_t **pworker;
  296. ucl_vars = g_hash_table_new_full (rspamd_strcase_hash,
  297. rspamd_strcase_equal, g_free, g_free);
  298. process_quark = g_quark_from_static_string ("rspamadm");
  299. cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_DEFAULT);
  300. cfg->libs_ctx = rspamd_init_libs ();
  301. rspamd_main = g_malloc0 (sizeof (*rspamd_main));
  302. rspamd_main->cfg = cfg;
  303. rspamd_main->pid = getpid ();
  304. rspamd_main->type = process_quark;
  305. rspamd_main->server_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (),
  306. "rspamadm");
  307. #ifdef HAVE_EVENT_NO_CACHE_TIME_FLAG
  308. struct event_config *ev_cfg;
  309. ev_cfg = event_config_new ();
  310. event_config_set_flag (ev_cfg, EVENT_BASE_FLAG_NO_CACHE_TIME);
  311. rspamd_main->ev_base = event_base_new_with_config (ev_cfg);
  312. #else
  313. rspamd_main->ev_base = event_init ();
  314. #endif
  315. rspamadm_fill_internal_commands (all_commands);
  316. help_command.command_data = all_commands;
  317. /* Now read options and store everything till the first non-dash argument */
  318. nargv = g_malloc0 (sizeof (gchar *) * (argc + 1));
  319. nargv[0] = g_strdup (argv[0]);
  320. for (i = 1, nargc = 1; i < argc; i ++) {
  321. if (argv[i] && argv[i][0] == '-') {
  322. /* Copy to nargv */
  323. nargv[nargc] = g_strdup (argv[i]);
  324. nargc ++;
  325. }
  326. else {
  327. break;
  328. }
  329. }
  330. context = g_option_context_new ("command - rspamd administration utility");
  331. og = g_option_group_new ("global", "global options", "global options",
  332. NULL, NULL);
  333. g_option_context_set_help_enabled (context, FALSE);
  334. g_option_group_add_entries (og, entries);
  335. g_option_context_set_summary (context,
  336. "Summary:\n Rspamd administration utility version "
  337. RVERSION
  338. "\n Release id: "
  339. RID);
  340. g_option_context_set_main_group (context, og);
  341. targv = nargv;
  342. targc = nargc;
  343. if (!g_option_context_parse (context, &targc, &targv, &error)) {
  344. fprintf (stderr, "option parsing failed: %s\n", error->message);
  345. g_error_free (error);
  346. exit (1);
  347. }
  348. /* Setup logger */
  349. if (verbose) {
  350. cfg->log_level = G_LOG_LEVEL_DEBUG;
  351. cfg->log_flags |= RSPAMD_LOG_FLAG_USEC;
  352. }
  353. else {
  354. cfg->log_level = G_LOG_LEVEL_MESSAGE;
  355. }
  356. cfg->log_type = RSPAMD_LOG_CONSOLE;
  357. /* Avoid timestamps printing */
  358. cfg->log_flags |= RSPAMD_LOG_FLAG_RSPAMADM;
  359. rspamd_set_logger (cfg, process_quark, &rspamd_main->logger,
  360. rspamd_main->server_pool);
  361. (void) rspamd_log_open (rspamd_main->logger);
  362. (void) dns_resolver_init (rspamd_main->logger,
  363. rspamd_main->ev_base,
  364. cfg);
  365. g_log_set_default_handler (rspamd_glib_log_function, rspamd_main->logger);
  366. g_set_printerr_handler (rspamd_glib_printerr_function);
  367. rspamd_config_post_load (cfg,
  368. RSPAMD_CONFIG_INIT_LIBS|RSPAMD_CONFIG_INIT_URL|RSPAMD_CONFIG_INIT_NO_TLD);
  369. pworker = &workers[0];
  370. while (*pworker) {
  371. /* Init string quarks */
  372. (void) g_quark_from_static_string ((*pworker)->name);
  373. pworker++;
  374. }
  375. cfg->compiled_modules = modules;
  376. cfg->compiled_workers = workers;
  377. gperf_profiler_init (cfg, "rspamadm");
  378. setproctitle ("rspamdadm");
  379. L = cfg->lua_state;
  380. rspamd_lua_set_path (L, NULL, ucl_vars);
  381. rspamd_lua_set_globals (cfg, L, ucl_vars);
  382. rspamadm_add_lua_globals ();
  383. #ifdef WITH_HIREDIS
  384. rspamd_redis_pool_config (cfg->redis_pool, cfg, rspamd_main->ev_base);
  385. #endif
  386. /* Init rspamadm global */
  387. lua_newtable (L);
  388. PTR_ARRAY_FOREACH (all_commands, i, cmd) {
  389. if (cmd->lua_subrs != NULL) {
  390. cmd->lua_subrs (L);
  391. }
  392. cmd ++;
  393. }
  394. lua_setglobal (L, "rspamadm");
  395. rspamadm_fill_lua_commands (L, all_commands);
  396. g_ptr_array_sort (all_commands, rspamdadm_commands_sort_func);
  397. g_strfreev (nargv);
  398. if (show_version) {
  399. rspamadm_version ();
  400. exit (EXIT_SUCCESS);
  401. }
  402. if (show_help) {
  403. rspamadm_usage (context);
  404. exit (EXIT_SUCCESS);
  405. }
  406. if (list_commands) {
  407. rspamadm_commands (all_commands);
  408. exit (EXIT_SUCCESS);
  409. }
  410. cmd_name = argv[nargc];
  411. if (cmd_name == NULL) {
  412. cmd_name = "help";
  413. }
  414. cmd = rspamadm_search_command (cmd_name, all_commands);
  415. if (cmd == NULL) {
  416. rspamd_fprintf (stderr, "Invalid command name: %s\n", cmd_name);
  417. /* Try fuzz search */
  418. rspamd_fprintf (stderr, "Suggested commands:\n");
  419. PTR_ARRAY_FOREACH (all_commands, i, cmd) {
  420. guint j;
  421. const gchar *alias;
  422. if (rspamadm_command_maybe_match_name (cmd->name, cmd_name)) {
  423. rspamd_fprintf (stderr, "%s\n", cmd->name);
  424. }
  425. else {
  426. PTR_ARRAY_FOREACH (cmd->aliases, j, alias) {
  427. if (rspamadm_command_maybe_match_name (alias, cmd_name)) {
  428. rspamd_fprintf (stderr, "%s\n", alias);
  429. }
  430. }
  431. }
  432. }
  433. exit (EXIT_FAILURE);
  434. }
  435. if (nargc < argc) {
  436. nargv = g_malloc0 (sizeof (gchar *) * (argc - nargc + 1));
  437. nargv[0] = g_strdup_printf ("%s %s", argv[0], cmd_name);
  438. for (i = 1; i < argc - nargc; i ++) {
  439. nargv[i] = g_strdup (argv[i + nargc]);
  440. }
  441. targc = argc - nargc;
  442. targv = nargv;
  443. cmd->run (targc, targv, cmd);
  444. g_strfreev (nargv);
  445. }
  446. else {
  447. cmd->run (0, NULL, cmd);
  448. }
  449. event_base_loopexit (rspamd_main->ev_base, NULL);
  450. #ifdef HAVE_EVENT_NO_CACHE_TIME_FLAG
  451. event_config_free (ev_cfg);
  452. #endif
  453. REF_RELEASE (rspamd_main->cfg);
  454. rspamd_log_close (rspamd_main->logger, TRUE);
  455. g_free (rspamd_main);
  456. g_ptr_array_free (all_commands, TRUE);
  457. return 0;
  458. }