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.

control.c 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 "cryptobox.h"
  19. #include "printf.h"
  20. #include "libserver/http/http_connection.h"
  21. #include "libserver/http/http_private.h"
  22. #include "addr.h"
  23. #include "unix-std.h"
  24. #include "contrib/libev/ev.h"
  25. #include "libutil/util.h"
  26. #include "lua/lua_common.h"
  27. static gchar *control_path = RSPAMD_DBDIR "/rspamd.sock";
  28. static gboolean json = FALSE;
  29. static gboolean ucl = TRUE;
  30. static gboolean compact = FALSE;
  31. static gdouble timeout = 1.0;
  32. static void rspamadm_control (gint argc, gchar **argv,
  33. const struct rspamadm_command *cmd);
  34. static const char *rspamadm_control_help (gboolean full_help,
  35. const struct rspamadm_command *cmd);
  36. struct rspamadm_command control_command = {
  37. .name = "control",
  38. .flags = 0,
  39. .help = rspamadm_control_help,
  40. .run = rspamadm_control,
  41. .lua_subrs = NULL,
  42. };
  43. struct rspamadm_control_cbdata {
  44. const gchar *path;
  45. gint argc;
  46. gchar **argv;
  47. };
  48. static GOptionEntry entries[] = {
  49. {"json", 'j', 0, G_OPTION_ARG_NONE, &json,
  50. "Output json", NULL},
  51. {"compact", 'c', 0, G_OPTION_ARG_NONE, &compact,
  52. "Output compacted", NULL},
  53. {"ucl", 'u', 0, G_OPTION_ARG_NONE, &ucl,
  54. "Output ucl (default)", NULL},
  55. {"socket", 's', 0, G_OPTION_ARG_STRING, &control_path,
  56. "Use the following socket path", NULL},
  57. {"timeout", 't', 0, G_OPTION_ARG_DOUBLE, &timeout,
  58. "Set IO timeout (1s by default)", NULL},
  59. {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}
  60. };
  61. static const char *
  62. rspamadm_control_help (gboolean full_help, const struct rspamadm_command *cmd)
  63. {
  64. const char *help_str;
  65. if (full_help) {
  66. help_str = "Manage rspamd main control interface\n\n"
  67. "Usage: rspamadm control [-c] [-j] [-u] [-s path] command\n"
  68. "Where options are:\n\n"
  69. "-c: output compacted json\n"
  70. "-j: output linted json\n"
  71. "-u: output ucl (default)\n"
  72. "-s: use the following socket instead of " RSPAMD_DBDIR "/rspamd.sock\n"
  73. "-t: set IO timeout (1.0 seconds default)\n"
  74. "--help: shows available options and commands\n\n"
  75. "Supported commands:\n"
  76. "stat - show statistics\n"
  77. "reload - reload workers dynamic data\n"
  78. "reresolve - resolve upstreams addresses\n"
  79. "recompile - recompile hyperscan regexes\n"
  80. "fuzzystat - show fuzzy statistics\n"
  81. "fuzzysync - immediately sync fuzzy database to storage\n";
  82. }
  83. else {
  84. help_str = "Manage rspamd main control interface";
  85. }
  86. return help_str;
  87. }
  88. static void
  89. rspamd_control_error_handler (struct rspamd_http_connection *conn, GError *err)
  90. {
  91. rspamd_fprintf (stderr, "Cannot make HTTP request: %e\n", err);
  92. ev_break (rspamd_main->event_loop, EVBREAK_ALL);
  93. }
  94. static gint
  95. rspamd_control_finish_handler (struct rspamd_http_connection *conn,
  96. struct rspamd_http_message *msg)
  97. {
  98. struct ucl_parser *parser;
  99. ucl_object_t *obj;
  100. rspamd_fstring_t *out;
  101. const gchar *body;
  102. gsize body_len;
  103. struct rspamadm_control_cbdata *cbdata = conn->ud;
  104. body = rspamd_http_message_get_body (msg, &body_len);
  105. parser = ucl_parser_new (0);
  106. if (!body || !ucl_parser_add_chunk (parser, body, body_len)) {
  107. rspamd_fprintf (stderr, "cannot parse server's reply: %s\n",
  108. ucl_parser_get_error (parser));
  109. ucl_parser_free (parser);
  110. }
  111. else {
  112. obj = ucl_parser_get_object (parser);
  113. out = rspamd_fstring_new ();
  114. if (json) {
  115. rspamd_ucl_emit_fstring (obj, UCL_EMIT_JSON, &out);
  116. }
  117. else if (compact) {
  118. rspamd_ucl_emit_fstring (obj, UCL_EMIT_JSON_COMPACT, &out);
  119. }
  120. else {
  121. if (strcmp (cbdata->path, "/fuzzystat") == 0) {
  122. rspamadm_execute_lua_ucl_subr (cbdata->argc - 1,
  123. &cbdata->argv[1],
  124. obj,
  125. "fuzzy_stat",
  126. TRUE);
  127. rspamd_fstring_free (out);
  128. ucl_object_unref (obj);
  129. ucl_parser_free (parser);
  130. goto end;
  131. }
  132. else {
  133. rspamd_ucl_emit_fstring (obj, UCL_EMIT_CONFIG, &out);
  134. }
  135. }
  136. rspamd_fprintf (stdout, "%V", out);
  137. rspamd_fstring_free (out);
  138. ucl_object_unref (obj);
  139. ucl_parser_free (parser);
  140. }
  141. end:
  142. ev_break (rspamd_main->event_loop, EVBREAK_ALL);
  143. return 0;
  144. }
  145. static void
  146. rspamadm_control (gint argc, gchar **argv, const struct rspamadm_command *_cmd)
  147. {
  148. GOptionContext *context;
  149. GError *error = NULL;
  150. const gchar *cmd, *path = NULL;
  151. struct rspamd_http_connection *conn;
  152. struct rspamd_http_message *msg;
  153. rspamd_inet_addr_t *addr;
  154. static struct rspamadm_control_cbdata cbdata;
  155. context = g_option_context_new (
  156. "control - manage rspamd main control interface");
  157. g_option_context_set_summary (context,
  158. "Summary:\n Rspamd administration utility version "
  159. RVERSION
  160. "\n Release id: "
  161. RID);
  162. g_option_context_add_main_entries (context, entries, NULL);
  163. g_option_context_set_ignore_unknown_options (context, TRUE);
  164. if (!g_option_context_parse (context, &argc, &argv, &error)) {
  165. rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message);
  166. g_error_free (error);
  167. g_option_context_free (context);
  168. exit (EXIT_FAILURE);
  169. }
  170. g_option_context_free (context);
  171. if (argc <= 1) {
  172. rspamd_fprintf (stderr, "command required\n");
  173. exit (EXIT_FAILURE);
  174. }
  175. cmd = argv[1];
  176. if (g_ascii_strcasecmp (cmd, "stat") == 0) {
  177. path = "/stat";
  178. }
  179. else if (g_ascii_strcasecmp (cmd, "reload") == 0) {
  180. path = "/reload";
  181. }
  182. else if (g_ascii_strcasecmp (cmd, "reresolve") == 0) {
  183. path = "/reresolve";
  184. }
  185. else if (g_ascii_strcasecmp (cmd, "recompile") == 0) {
  186. path = "/recompile";
  187. }
  188. else if (g_ascii_strcasecmp (cmd, "fuzzystat") == 0 ||
  189. g_ascii_strcasecmp (cmd, "fuzzy_stat") == 0) {
  190. path = "/fuzzystat";
  191. }
  192. else if (g_ascii_strcasecmp (cmd, "fuzzysync") == 0 ||
  193. g_ascii_strcasecmp (cmd, "fuzzy_sync") == 0) {
  194. path = "/fuzzysync";
  195. }
  196. else {
  197. rspamd_fprintf (stderr, "unknown command: %s\n", cmd);
  198. exit (EXIT_FAILURE);
  199. }
  200. if (!rspamd_parse_inet_address (&addr,
  201. control_path, strlen (control_path), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) {
  202. rspamd_fprintf (stderr, "bad control path: %s\n", control_path);
  203. exit (EXIT_FAILURE);
  204. }
  205. conn = rspamd_http_connection_new_client (
  206. rspamd_main->http_ctx, /* Default context */
  207. NULL,
  208. rspamd_control_error_handler,
  209. rspamd_control_finish_handler,
  210. RSPAMD_HTTP_CLIENT_SIMPLE,
  211. addr);
  212. if (!conn) {
  213. rspamd_fprintf (stderr, "cannot open connection to %s: %s\n",
  214. control_path, strerror (errno));
  215. exit (-errno);
  216. }
  217. msg = rspamd_http_new_message (HTTP_REQUEST);
  218. msg->url = rspamd_fstring_new_init (path, strlen (path));
  219. cbdata.argc = argc;
  220. cbdata.argv = argv;
  221. cbdata.path = path;
  222. rspamd_http_connection_write_message (conn, msg, NULL, NULL, &cbdata,
  223. timeout);
  224. ev_loop (rspamd_main->event_loop, 0);
  225. rspamd_http_connection_unref (conn);
  226. rspamd_inet_address_free (addr);
  227. }