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 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * Copyright (c) 2015, Vsevolod Stakhov
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY AUTHOR ''AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
  17. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  20. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include "config.h"
  25. #include "rspamadm.h"
  26. #include "rspamd.h"
  27. #include "ottery.h"
  28. #include "lua/lua_common.h"
  29. #include "lua_ucl.h"
  30. #include "unix-std.h"
  31. #ifdef HAVE_LIBUTIL_H
  32. #include <libutil.h>
  33. #endif
  34. static gboolean verbose = FALSE;
  35. static gboolean list_commands = FALSE;
  36. static gboolean show_help = FALSE;
  37. static gboolean show_version = FALSE;
  38. GHashTable *ucl_vars = NULL;
  39. struct rspamd_main *rspamd_main = NULL;
  40. static void rspamadm_help (gint argc, gchar **argv);
  41. static const char* rspamadm_help_help (gboolean full_help);
  42. struct rspamadm_command help_command = {
  43. .name = "help",
  44. .flags = RSPAMADM_FLAG_NOHELP,
  45. .help = rspamadm_help_help,
  46. .run = rspamadm_help
  47. };
  48. static gboolean rspamadm_parse_ucl_var (const gchar *option_name,
  49. const gchar *value, gpointer data,
  50. GError **error);
  51. static union {
  52. gboolean (*func)(const gchar *option_name,
  53. const gchar *value, gpointer data,
  54. GError **error);
  55. const gpointer ptr;
  56. } rspamadm_parse_ucl_var_un = {
  57. .func = &rspamadm_parse_ucl_var
  58. };
  59. static GOptionEntry entries[] = {
  60. {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
  61. "Enable verbose logging", NULL},
  62. {"list-commands", 'l', 0, G_OPTION_ARG_NONE, &list_commands,
  63. "List available commands", NULL},
  64. {"var", 0, 0, G_OPTION_ARG_CALLBACK, &rspamadm_parse_ucl_var_un,
  65. "Redefine UCL variable", NULL},
  66. {"help", 'h', 0, G_OPTION_ARG_NONE, &show_help,
  67. "Show help", NULL},
  68. {"version", 'h', 0, G_OPTION_ARG_NONE, &show_version,
  69. "Show version", NULL},
  70. {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}
  71. };
  72. GQuark
  73. rspamadm_error (void)
  74. {
  75. return g_quark_from_static_string ("rspamadm");
  76. }
  77. static void
  78. rspamadm_version (void)
  79. {
  80. printf ("Rspamadm %s\n", RVERSION);
  81. }
  82. static void
  83. rspamadm_usage (GOptionContext *context)
  84. {
  85. gchar *help_str;
  86. help_str = g_option_context_get_help (context, TRUE, NULL);
  87. printf ("%s", help_str);
  88. }
  89. static void
  90. rspamadm_commands (void)
  91. {
  92. const struct rspamadm_command **cmd;
  93. printf ("Rspamadm %s\n", RVERSION);
  94. printf ("Usage: rspamadm [global_options] command [command_options]\n");
  95. printf ("\nAvailable commands:\n");
  96. cmd = commands;
  97. while (*cmd) {
  98. if (!((*cmd)->flags & RSPAMADM_FLAG_NOHELP)) {
  99. printf (" %-18s %-60s\n", (*cmd)->name, (*cmd)->help (FALSE));
  100. }
  101. cmd ++;
  102. }
  103. }
  104. static const char *
  105. rspamadm_help_help (gboolean full_help)
  106. {
  107. const char *help_str;
  108. if (full_help) {
  109. help_str = "Shows help for a specified command\n"
  110. "Usage: rspamadm help <command>";
  111. }
  112. else {
  113. help_str = "Shows help for a specified command";
  114. }
  115. return help_str;
  116. }
  117. static void
  118. rspamadm_help (gint argc, gchar **argv)
  119. {
  120. const gchar *cmd_name;
  121. const struct rspamadm_command *cmd, **cmd_list;
  122. printf ("Rspamadm %s\n", RVERSION);
  123. printf ("Usage: rspamadm [global_options] command [command_options]\n\n");
  124. if (argc <= 1) {
  125. cmd_name = "help";
  126. }
  127. else {
  128. cmd_name = argv[1];
  129. printf ("Showing help for %s command\n\n", cmd_name);
  130. }
  131. cmd = rspamadm_search_command (cmd_name);
  132. if (cmd == NULL) {
  133. fprintf (stderr, "Invalid command name: %s\n", cmd_name);
  134. exit (EXIT_FAILURE);
  135. }
  136. if (strcmp (cmd_name, "help") == 0) {
  137. printf ("Available commands:\n");
  138. cmd_list = commands;
  139. while (*cmd_list) {
  140. if (!((*cmd_list)->flags & RSPAMADM_FLAG_NOHELP)) {
  141. printf (" %-18s %-60s\n", (*cmd_list)->name,
  142. (*cmd_list)->help (FALSE));
  143. }
  144. cmd_list++;
  145. }
  146. }
  147. else {
  148. printf ("%s\n", cmd->help (TRUE));
  149. }
  150. }
  151. static gboolean
  152. rspamadm_parse_ucl_var (const gchar *option_name,
  153. const gchar *value, gpointer data,
  154. GError **error)
  155. {
  156. gchar *k, *v, *t;
  157. t = strchr (value, '=');
  158. if (t != NULL) {
  159. k = g_strdup (value);
  160. t = k + (t - value);
  161. v = g_strdup (t + 1);
  162. *t = '\0';
  163. g_hash_table_insert (ucl_vars, k, v);
  164. }
  165. else {
  166. g_set_error (error, rspamadm_error (), EINVAL,
  167. "Bad variable format: %s", value);
  168. return FALSE;
  169. }
  170. return TRUE;
  171. }
  172. gboolean
  173. rspamadm_execute_lua_ucl_subr (gpointer pL, gint argc, gchar **argv,
  174. const ucl_object_t *res, const gchar *script)
  175. {
  176. lua_State *L = pL;
  177. gint err_idx, cb_idx, i, ret;
  178. GString *tb;
  179. g_assert (script != NULL);
  180. g_assert (res != NULL);
  181. g_assert (L != NULL);
  182. if (luaL_dostring (L, script) != 0) {
  183. msg_err ("cannot execute lua script: %s",
  184. lua_tostring (L, -1));
  185. return FALSE;
  186. }
  187. else {
  188. if (lua_type (L, -1) == LUA_TFUNCTION) {
  189. cb_idx = luaL_ref (L, LUA_REGISTRYINDEX);
  190. }
  191. else {
  192. msg_err ("lua script must return "
  193. "function and not %s",
  194. lua_typename (L,
  195. lua_type (L, -1)));
  196. return FALSE;
  197. }
  198. }
  199. lua_pushcfunction (L, &rspamd_lua_traceback);
  200. err_idx = lua_gettop (L);
  201. /* Push function */
  202. lua_rawgeti (L, LUA_REGISTRYINDEX, cb_idx);
  203. /* Push argv */
  204. lua_newtable (L);
  205. for (i = 0; i < argc; i ++) {
  206. lua_pushstring (L, argv[i]);
  207. lua_rawseti (L, -2, i + 1);
  208. }
  209. /* Push results */
  210. ucl_object_push_lua (L, res, TRUE);
  211. if ((ret = lua_pcall (L, 2, 0, err_idx)) != 0) {
  212. tb = lua_touserdata (L, -1);
  213. msg_err ("call to adm lua script failed (%d): %v", ret, tb);
  214. if (tb) {
  215. g_string_free (tb, TRUE);
  216. }
  217. lua_pop (L, 1);
  218. return FALSE;
  219. }
  220. /* error function */
  221. lua_pop (L, 1);
  222. luaL_unref (L, LUA_REGISTRYINDEX, cb_idx);
  223. return TRUE;
  224. }
  225. gint
  226. main (gint argc, gchar **argv, gchar **env)
  227. {
  228. GError *error = NULL;
  229. GOptionContext *context;
  230. GOptionGroup *og;
  231. struct rspamd_config *cfg;
  232. GQuark process_quark;
  233. gchar **nargv, **targv;
  234. const gchar *cmd_name;
  235. const struct rspamadm_command *cmd;
  236. gint i, nargc, targc;
  237. ucl_vars = g_hash_table_new_full (rspamd_strcase_hash,
  238. rspamd_strcase_equal, g_free, g_free);
  239. process_quark = g_quark_from_static_string ("rspamadm");
  240. cfg = rspamd_config_new ();
  241. cfg->libs_ctx = rspamd_init_libs ();
  242. rspamd_main = g_malloc0 (sizeof (*rspamd_main));
  243. rspamd_main->cfg = cfg;
  244. rspamd_main->pid = getpid ();
  245. rspamd_main->type = process_quark;
  246. rspamd_main->server_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (),
  247. "rspamadm");
  248. /* Setup logger */
  249. if (verbose) {
  250. cfg->log_level = G_LOG_LEVEL_DEBUG;
  251. }
  252. else {
  253. cfg->log_level = G_LOG_LEVEL_INFO;
  254. }
  255. cfg->log_type = RSPAMD_LOG_CONSOLE;
  256. rspamd_set_logger (cfg, process_quark, rspamd_main);
  257. (void) rspamd_log_open (rspamd_main->logger);
  258. g_log_set_default_handler (rspamd_glib_log_function, rspamd_main->logger);
  259. g_set_printerr_handler (rspamd_glib_printerr_function);
  260. gperf_profiler_init (cfg, "rspamadm");
  261. setproctitle ("rspamdadm");
  262. /* Now read options and store everything till the first non-dash argument */
  263. nargv = g_malloc0 (sizeof (gchar *) * (argc + 1));
  264. nargv[0] = g_strdup (argv[0]);
  265. for (i = 1, nargc = 1; i < argc; i ++) {
  266. if (argv[i] && argv[i][0] == '-') {
  267. /* Copy to nargv */
  268. nargv[nargc] = g_strdup (argv[i]);
  269. nargc ++;
  270. }
  271. else {
  272. break;
  273. }
  274. }
  275. context = g_option_context_new ("command - rspamd administration utility");
  276. og = g_option_group_new ("global", "global options", "global options",
  277. NULL, NULL);
  278. g_option_context_set_help_enabled (context, FALSE);
  279. g_option_group_add_entries (og, entries);
  280. g_option_context_set_summary (context,
  281. "Summary:\n Rspamd administration utility version "
  282. RVERSION
  283. "\n Release id: "
  284. RID);
  285. g_option_context_set_main_group (context, og);
  286. targv = nargv;
  287. targc = nargc;
  288. if (!g_option_context_parse (context, &targc, &targv, &error)) {
  289. fprintf (stderr, "option parsing failed: %s\n", error->message);
  290. g_error_free (error);
  291. exit (1);
  292. }
  293. g_strfreev (nargv);
  294. if (show_version) {
  295. rspamadm_version ();
  296. exit (EXIT_SUCCESS);
  297. }
  298. if (show_help) {
  299. rspamadm_usage (context);
  300. exit (EXIT_SUCCESS);
  301. }
  302. if (list_commands) {
  303. rspamadm_commands ();
  304. exit (EXIT_SUCCESS);
  305. }
  306. cmd_name = argv[nargc];
  307. if (cmd_name == NULL) {
  308. cmd_name = "help";
  309. }
  310. cmd = rspamadm_search_command (cmd_name);
  311. if (cmd == NULL) {
  312. fprintf (stderr, "Invalid command name: %s\n", cmd_name);
  313. exit (EXIT_FAILURE);
  314. }
  315. if (nargc < argc) {
  316. nargv = g_malloc0 (sizeof (gchar *) * (argc - nargc + 1));
  317. nargv[0] = g_strdup_printf ("%s %s", argv[0], cmd_name);
  318. for (i = 1; i < argc - nargc; i ++) {
  319. nargv[i] = g_strdup (argv[i + nargc]);
  320. }
  321. targc = argc - nargc;
  322. targv = nargv;
  323. cmd->run (targc, targv);
  324. g_strfreev (nargv);
  325. }
  326. else {
  327. cmd->run (0, NULL);
  328. }
  329. rspamd_log_close (rspamd_main->logger);
  330. REF_RELEASE (rspamd_main->cfg);
  331. g_free (rspamd_main);
  332. return 0;
  333. }