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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. /*
  17. * Rspamd worker implementation
  18. */
  19. #include "config.h"
  20. #include "libutil/util.h"
  21. #include "libutil/map.h"
  22. #include "libutil/upstream.h"
  23. #include "libserver/protocol.h"
  24. #include "libserver/cfg_file.h"
  25. #include "libserver/url.h"
  26. #include "libserver/dns.h"
  27. #include "libmime/message.h"
  28. #include "rspamd.h"
  29. #include "keypairs_cache.h"
  30. #include "libstat/stat_api.h"
  31. #include "libserver/worker_util.h"
  32. #include "libserver/rspamd_control.h"
  33. #include "lua/lua_common.h"
  34. /* 60 seconds for worker's IO */
  35. #define DEFAULT_WORKER_IO_TIMEOUT 60000
  36. /* Timeout for task processing */
  37. #define DEFAULT_TASK_TIMEOUT 8.0
  38. gpointer init_worker (struct rspamd_config *cfg);
  39. void start_worker (struct rspamd_worker *worker);
  40. worker_t normal_worker = {
  41. "normal", /* Name */
  42. init_worker, /* Init function */
  43. start_worker, /* Start function */
  44. TRUE, /* Has socket */
  45. FALSE, /* Non unique */
  46. FALSE, /* Non threaded */
  47. TRUE, /* Killable */
  48. SOCK_STREAM, /* TCP socket */
  49. RSPAMD_WORKER_VER /* Version info */
  50. };
  51. #define msg_err_ctx(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \
  52. "controller", ctx->cfg->cfg_pool->tag.uid, \
  53. G_STRFUNC, \
  54. __VA_ARGS__)
  55. #define msg_warn_ctx(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
  56. "controller", ctx->cfg->cfg_pool->tag.uid, \
  57. G_STRFUNC, \
  58. __VA_ARGS__)
  59. #define msg_info_ctx(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \
  60. "controller", ctx->cfg->cfg_pool->tag.uid, \
  61. G_STRFUNC, \
  62. __VA_ARGS__)
  63. #define msg_debug_ctx(...) rspamd_default_log_function (G_LOG_LEVEL_DEBUG, \
  64. "controller", ctx->cfg->cfg_pool->tag.uid, \
  65. G_STRFUNC, \
  66. __VA_ARGS__)
  67. /*
  68. * Worker's context
  69. */
  70. struct rspamd_worker_ctx {
  71. guint32 timeout;
  72. struct timeval io_tv;
  73. /* Detect whether this worker is mime worker */
  74. gboolean is_mime;
  75. /* HTTP worker */
  76. gboolean is_http;
  77. /* JSON output */
  78. gboolean is_json;
  79. /* Allow learning throught worker */
  80. gboolean allow_learn;
  81. /* DNS resolver */
  82. struct rspamd_dns_resolver *resolver;
  83. /* Limit of tasks */
  84. guint32 max_tasks;
  85. /* Maximum time for task processing */
  86. gdouble task_timeout;
  87. /* Events base */
  88. struct event_base *ev_base;
  89. /* Encryption key */
  90. struct rspamd_cryptobox_keypair *key;
  91. /* Keys cache */
  92. struct rspamd_keypair_cache *keys_cache;
  93. /* Configuration */
  94. struct rspamd_config *cfg;
  95. };
  96. /*
  97. * Reduce number of tasks proceeded
  98. */
  99. static void
  100. reduce_tasks_count (gpointer arg)
  101. {
  102. guint *nconns = arg;
  103. (*nconns)--;
  104. }
  105. static void
  106. rspamd_task_timeout (gint fd, short what, gpointer ud)
  107. {
  108. struct rspamd_task *task = (struct rspamd_task *) ud;
  109. if (!(task->processed_stages & RSPAMD_TASK_STAGE_FILTERS)) {
  110. msg_info_task ("processing of task timed out, forced processing");
  111. task->processed_stages |= RSPAMD_TASK_STAGE_FILTERS;
  112. rspamd_session_cleanup (task->s);
  113. rspamd_task_process (task, RSPAMD_TASK_PROCESS_ALL);
  114. rspamd_session_pending (task->s);
  115. }
  116. }
  117. static gint
  118. rspamd_worker_body_handler (struct rspamd_http_connection *conn,
  119. struct rspamd_http_message *msg,
  120. const gchar *chunk, gsize len)
  121. {
  122. struct rspamd_task *task = (struct rspamd_task *) conn->ud;
  123. struct rspamd_worker_ctx *ctx;
  124. struct timeval task_tv;
  125. ctx = task->worker->ctx;
  126. if (!rspamd_protocol_handle_request (task, msg)) {
  127. msg_err_task ("cannot handle request: %e", task->err);
  128. task->flags |= RSPAMD_TASK_FLAG_SKIP;
  129. }
  130. else {
  131. if (task->cmd == CMD_PING) {
  132. task->flags |= RSPAMD_TASK_FLAG_SKIP;
  133. }
  134. else {
  135. if (!rspamd_task_load_message (task, msg, chunk, len)) {
  136. msg_err_task ("cannot load message: %e", task->err);
  137. task->flags |= RSPAMD_TASK_FLAG_SKIP;
  138. }
  139. }
  140. }
  141. /* Set global timeout for the task */
  142. if (ctx->task_timeout > 0.0) {
  143. event_set (&task->timeout_ev, -1, EV_TIMEOUT, rspamd_task_timeout,
  144. task);
  145. event_base_set (ctx->ev_base, &task->timeout_ev);
  146. double_to_tv (ctx->task_timeout, &task_tv);
  147. event_add (&task->timeout_ev, &task_tv);
  148. }
  149. rspamd_task_process (task, RSPAMD_TASK_PROCESS_ALL);
  150. return 0;
  151. }
  152. static void
  153. rspamd_worker_error_handler (struct rspamd_http_connection *conn, GError *err)
  154. {
  155. struct rspamd_task *task = (struct rspamd_task *) conn->ud;
  156. msg_info_task ("abnormally closing connection from: %s, error: %e",
  157. rspamd_inet_address_to_string (task->client_addr), err);
  158. /* Terminate session immediately */
  159. rspamd_session_destroy (task->s);
  160. }
  161. static gint
  162. rspamd_worker_finish_handler (struct rspamd_http_connection *conn,
  163. struct rspamd_http_message *msg)
  164. {
  165. struct rspamd_task *task = (struct rspamd_task *) conn->ud;
  166. if (task->processed_stages & RSPAMD_TASK_STAGE_REPLIED) {
  167. /* We are done here */
  168. msg_debug_task ("normally closing connection from: %s",
  169. rspamd_inet_address_to_string (task->client_addr));
  170. rspamd_session_destroy (task->s);
  171. }
  172. else if (task->processed_stages & RSPAMD_TASK_STAGE_DONE) {
  173. rspamd_session_pending (task->s);
  174. }
  175. return 0;
  176. }
  177. /*
  178. * Accept new connection and construct task
  179. */
  180. static void
  181. accept_socket (gint fd, short what, void *arg)
  182. {
  183. struct rspamd_worker *worker = (struct rspamd_worker *) arg;
  184. struct rspamd_worker_ctx *ctx;
  185. struct rspamd_task *task;
  186. rspamd_inet_addr_t *addr;
  187. gint nfd;
  188. ctx = worker->ctx;
  189. if (ctx->max_tasks != 0 && worker->nconns > ctx->max_tasks) {
  190. msg_info_ctx ("current tasks is now: %uD while maximum is: %uD",
  191. worker->nconns,
  192. ctx->max_tasks);
  193. return;
  194. }
  195. if ((nfd =
  196. rspamd_accept_from_socket (fd, &addr)) == -1) {
  197. msg_warn_ctx ("accept failed: %s", strerror (errno));
  198. return;
  199. }
  200. /* Check for EAGAIN */
  201. if (nfd == 0) {
  202. return;
  203. }
  204. task = rspamd_task_new (worker, ctx->cfg);
  205. msg_info_task ("accepted connection from %s port %d",
  206. rspamd_inet_address_to_string (addr),
  207. rspamd_inet_address_get_port (addr));
  208. /* Copy some variables */
  209. if (ctx->is_mime) {
  210. task->flags |= RSPAMD_TASK_FLAG_MIME;
  211. }
  212. else {
  213. task->flags &= ~RSPAMD_TASK_FLAG_MIME;
  214. }
  215. task->sock = nfd;
  216. task->client_addr = addr;
  217. worker->srv->stat->connections_count++;
  218. task->resolver = ctx->resolver;
  219. /* TODO: allow to disable autolearn in protocol */
  220. task->flags |= RSPAMD_TASK_FLAG_LEARN_AUTO;
  221. task->http_conn = rspamd_http_connection_new (
  222. rspamd_worker_body_handler,
  223. rspamd_worker_error_handler,
  224. rspamd_worker_finish_handler,
  225. 0,
  226. RSPAMD_HTTP_SERVER,
  227. ctx->keys_cache);
  228. task->ev_base = ctx->ev_base;
  229. worker->nconns++;
  230. rspamd_mempool_add_destructor (task->task_pool,
  231. (rspamd_mempool_destruct_t)reduce_tasks_count, &worker->nconns);
  232. /* Set up async session */
  233. task->s = rspamd_session_create (task->task_pool, rspamd_task_fin,
  234. rspamd_task_restore, (event_finalizer_t )rspamd_task_free, task);
  235. if (ctx->key) {
  236. rspamd_http_connection_set_key (task->http_conn, ctx->key);
  237. }
  238. rspamd_http_connection_read_message (task->http_conn,
  239. task,
  240. nfd,
  241. &ctx->io_tv,
  242. ctx->ev_base);
  243. }
  244. #ifdef WITH_HYPERSCAN
  245. static gboolean
  246. rspamd_worker_hyperscan_ready (struct rspamd_main *rspamd_main,
  247. struct rspamd_worker *worker, gint fd,
  248. struct rspamd_control_command *cmd,
  249. gpointer ud)
  250. {
  251. struct rspamd_control_reply rep;
  252. struct rspamd_re_cache *cache = worker->srv->cfg->re_cache;
  253. memset (&rep, 0, sizeof (rep));
  254. rep.type = RSPAMD_CONTROL_HYPERSCAN_LOADED;
  255. if (!rspamd_re_cache_is_hs_loaded (cache) || cmd->cmd.hs_loaded.forced) {
  256. msg_info ("loading hyperscan expressions after receiving compilation "
  257. "notice: %s",
  258. (!rspamd_re_cache_is_hs_loaded (cache)) ?
  259. "new db" : "forced update");
  260. rep.reply.hs_loaded.status = rspamd_re_cache_load_hyperscan (
  261. worker->srv->cfg->re_cache, cmd->cmd.hs_loaded.cache_dir);
  262. }
  263. if (write (fd, &rep, sizeof (rep)) != sizeof (rep)) {
  264. msg_err ("cannot write reply to the control socket: %s",
  265. strerror (errno));
  266. }
  267. return TRUE;
  268. }
  269. #endif
  270. gpointer
  271. init_worker (struct rspamd_config *cfg)
  272. {
  273. struct rspamd_worker_ctx *ctx;
  274. GQuark type;
  275. type = g_quark_try_string ("normal");
  276. ctx = g_malloc0 (sizeof (struct rspamd_worker_ctx));
  277. ctx->is_mime = TRUE;
  278. ctx->timeout = DEFAULT_WORKER_IO_TIMEOUT;
  279. ctx->cfg = cfg;
  280. ctx->task_timeout = DEFAULT_TASK_TIMEOUT;
  281. rspamd_rcl_register_worker_option (cfg,
  282. type,
  283. "mime",
  284. rspamd_rcl_parse_struct_boolean,
  285. ctx,
  286. G_STRUCT_OFFSET (struct rspamd_worker_ctx, is_mime),
  287. 0,
  288. "Set to `false` if this worker is intended to work with non-MIME messages");
  289. rspamd_rcl_register_worker_option (cfg,
  290. type,
  291. "http",
  292. rspamd_rcl_parse_struct_boolean,
  293. ctx,
  294. G_STRUCT_OFFSET (struct rspamd_worker_ctx, is_http),
  295. 0,
  296. "Deprecated: always true now");
  297. rspamd_rcl_register_worker_option (cfg,
  298. type,
  299. "json",
  300. rspamd_rcl_parse_struct_boolean,
  301. ctx,
  302. G_STRUCT_OFFSET (struct rspamd_worker_ctx, is_json),
  303. 0,
  304. "Deprecated: always true now");
  305. rspamd_rcl_register_worker_option (cfg,
  306. type,
  307. "allow_learn",
  308. rspamd_rcl_parse_struct_boolean,
  309. ctx,
  310. G_STRUCT_OFFSET (struct rspamd_worker_ctx, allow_learn),
  311. 0,
  312. "Deprecated: disabled and forgotten");
  313. rspamd_rcl_register_worker_option (cfg,
  314. type,
  315. "timeout",
  316. rspamd_rcl_parse_struct_time,
  317. ctx,
  318. G_STRUCT_OFFSET (struct rspamd_worker_ctx,
  319. timeout),
  320. RSPAMD_CL_FLAG_TIME_INTEGER,
  321. "Protocol IO timeout");
  322. rspamd_rcl_register_worker_option (cfg,
  323. type,
  324. "task_timeout",
  325. rspamd_rcl_parse_struct_time,
  326. ctx,
  327. G_STRUCT_OFFSET (struct rspamd_worker_ctx,
  328. task_timeout),
  329. RSPAMD_CL_FLAG_TIME_FLOAT,
  330. "Maximum task processing time, default: "
  331. G_STRINGIFY(DEFAULT_TASK_TIMEOUT)
  332. " seconds");
  333. rspamd_rcl_register_worker_option (cfg,
  334. type,
  335. "max_tasks",
  336. rspamd_rcl_parse_struct_integer,
  337. ctx,
  338. G_STRUCT_OFFSET (struct rspamd_worker_ctx,
  339. max_tasks),
  340. RSPAMD_CL_FLAG_INT_32,
  341. "Maximum count of parallel tasks processed by a single worker process");
  342. rspamd_rcl_register_worker_option (cfg,
  343. type,
  344. "keypair",
  345. rspamd_rcl_parse_struct_keypair,
  346. ctx,
  347. G_STRUCT_OFFSET (struct rspamd_worker_ctx,
  348. key),
  349. 0,
  350. "Encryption keypair");
  351. return ctx;
  352. }
  353. /*
  354. * Start worker process
  355. */
  356. void
  357. start_worker (struct rspamd_worker *worker)
  358. {
  359. struct rspamd_worker_ctx *ctx = worker->ctx;
  360. ctx->ev_base = rspamd_prepare_worker (worker, "normal", accept_socket);
  361. msec_to_tv (ctx->timeout, &ctx->io_tv);
  362. rspamd_map_watch (worker->srv->cfg, ctx->ev_base);
  363. rspamd_symbols_cache_start_refresh (worker->srv->cfg->cache, ctx->ev_base);
  364. ctx->resolver = dns_resolver_init (worker->srv->logger,
  365. ctx->ev_base,
  366. worker->srv->cfg);
  367. rspamd_upstreams_library_config (worker->srv->cfg, ctx->cfg->ups_ctx,
  368. ctx->ev_base, ctx->resolver->r);
  369. /* XXX: stupid default */
  370. ctx->keys_cache = rspamd_keypair_cache_new (256);
  371. rspamd_stat_init (worker->srv->cfg, ctx->ev_base);
  372. #ifdef WITH_HYPERSCAN
  373. rspamd_control_worker_add_cmd_handler (worker, RSPAMD_CONTROL_HYPERSCAN_LOADED,
  374. rspamd_worker_hyperscan_ready, ctx);
  375. #endif
  376. event_base_loop (ctx->ev_base, 0);
  377. rspamd_worker_block_signals ();
  378. g_mime_shutdown ();
  379. rspamd_stat_close ();
  380. rspamd_log_close (worker->srv->logger);
  381. if (ctx->key) {
  382. rspamd_keypair_unref (ctx->key);
  383. }
  384. rspamd_keypair_cache_destroy (ctx->keys_cache);
  385. exit (EXIT_SUCCESS);
  386. }