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.

rspamd.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*-
  2. * Copyright 2016-2017 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. #ifndef RSPAMD_MAIN_H
  17. #define RSPAMD_MAIN_H
  18. #include "config.h"
  19. #include "libutil/fstring.h"
  20. #include "libutil/mem_pool.h"
  21. #include "libutil/util.h"
  22. #include "libserver/logger.h"
  23. #include "libserver/http/http_connection.h"
  24. #include "libutil/upstream.h"
  25. #include "libutil/radix.h"
  26. #include "libserver/cfg_file.h"
  27. #include "libserver/url.h"
  28. #include "libserver/protocol.h"
  29. #include "libserver/async_session.h"
  30. #include "libserver/roll_history.h"
  31. #include "libserver/task.h"
  32. #include <openssl/ssl.h>
  33. /* Default values */
  34. #define FIXED_CONFIG_FILE RSPAMD_CONFDIR "/rspamd.conf"
  35. /* Time in seconds to exit for old worker */
  36. #define SOFT_SHUTDOWN_TIME 10
  37. /* Spam subject */
  38. #define SPAM_SUBJECT "*** SPAM *** %s"
  39. #ifdef CRLF
  40. #undef CRLF
  41. #undef CR
  42. #undef LF
  43. #endif
  44. #define CRLF "\r\n"
  45. #define CR '\r'
  46. #define LF '\n'
  47. #ifdef __cplusplus
  48. extern "C" {
  49. #endif
  50. struct rspamd_main;
  51. enum rspamd_worker_flags {
  52. RSPAMD_WORKER_HAS_SOCKET = (1 << 0),
  53. RSPAMD_WORKER_UNIQUE = (1 << 1),
  54. RSPAMD_WORKER_THREADED = (1 << 2),
  55. RSPAMD_WORKER_KILLABLE = (1 << 3),
  56. RSPAMD_WORKER_ALWAYS_START = (1 << 4),
  57. RSPAMD_WORKER_SCANNER = (1 << 5),
  58. RSPAMD_WORKER_CONTROLLER = (1 << 6),
  59. RSPAMD_WORKER_NO_TERMINATE_DELAY = (1 << 7),
  60. };
  61. struct rspamd_worker_accept_event {
  62. ev_io accept_ev;
  63. ev_timer throttling_ev;
  64. struct ev_loop *event_loop;
  65. struct rspamd_worker_accept_event *prev, *next;
  66. };
  67. typedef void (*rspamd_worker_term_cb) (EV_P_ ev_child *, struct rspamd_main *,
  68. struct rspamd_worker *);
  69. struct rspamd_worker_heartbeat {
  70. ev_timer heartbeat_ev; /**< used by main for checking heartbeats and by workers to send heartbeats */
  71. ev_tstamp last_event; /**< last heartbeat received timestamp */
  72. gint64 nbeats; /**< positive for beats received, negative for beats missed */
  73. };
  74. enum rspamd_worker_state {
  75. rspamd_worker_state_running = 0,
  76. rspamd_worker_state_wanna_die,
  77. rspamd_worker_state_terminating,
  78. rspamd_worker_wait_connections,
  79. rspamd_worker_wait_final_scripts,
  80. rspamd_worker_wanna_die
  81. };
  82. /**
  83. * Worker process structure
  84. */
  85. struct rspamd_worker {
  86. pid_t pid; /**< pid of worker */
  87. pid_t ppid; /**< pid of parent */
  88. guint index; /**< index number */
  89. guint nconns; /**< current connections count */
  90. enum rspamd_worker_state state; /**< current worker state */
  91. gboolean cores_throttled; /**< set to true if cores throttling took place */
  92. gdouble start_time; /**< start time */
  93. struct rspamd_main *srv; /**< pointer to server structure */
  94. GQuark type; /**< process type */
  95. GHashTable *signal_events; /**< signal events */
  96. struct rspamd_worker_accept_event *accept_events; /**< socket events */
  97. struct rspamd_worker_conf *cf; /**< worker config data */
  98. gpointer ctx; /**< worker's specific data */
  99. gint flags; /**< worker's flags (enum rspamd_worker_flags) */
  100. gint control_pipe[2]; /**< control pipe. [0] is used by main process,
  101. [1] is used by a worker */
  102. gint srv_pipe[2]; /**< used by workers to request something from the
  103. main process. [0] - main, [1] - worker */
  104. ev_io srv_ev; /**< used by main for read workers' requests */
  105. struct rspamd_worker_heartbeat hb; /**< heartbeat data */
  106. gpointer control_data; /**< used by control protocol to handle commands */
  107. gpointer tmp_data; /**< used to avoid race condition to deal with control messages */
  108. ev_child cld_ev; /**< to allow reaping */
  109. rspamd_worker_term_cb term_handler; /**< custom term handler */
  110. GHashTable *control_events_pending; /**< control events pending indexed by ptr */
  111. };
  112. struct rspamd_abstract_worker_ctx {
  113. guint64 magic;
  114. /* Events base */
  115. struct ev_loop *event_loop;
  116. /* DNS resolver */
  117. struct rspamd_dns_resolver *resolver;
  118. /* Config */
  119. struct rspamd_config *cfg;
  120. char data[];
  121. };
  122. struct rspamd_worker_signal_handler;
  123. typedef gboolean (*rspamd_worker_signal_cb_t) (
  124. struct rspamd_worker_signal_handler *, void *ud);
  125. struct rspamd_worker_signal_handler_elt {
  126. rspamd_worker_signal_cb_t handler;
  127. void *handler_data;
  128. struct rspamd_worker_signal_handler_elt *next, *prev;
  129. };
  130. struct rspamd_worker_signal_handler {
  131. gint signo;
  132. gboolean enabled;
  133. ev_signal ev_sig;
  134. struct ev_loop *event_loop;
  135. struct rspamd_worker *worker;
  136. struct rspamd_worker_signal_handler_elt *cb;
  137. };
  138. /**
  139. * Common structure representing C module context
  140. */
  141. struct module_s;
  142. struct module_ctx {
  143. gint (*filter) (struct rspamd_task *task); /**< pointer to headers process function */
  144. struct module_s *mod; /**< module pointer */
  145. gboolean enabled; /**< true if module is enabled in configuration */
  146. };
  147. #ifndef WITH_HYPERSCAN
  148. #define RSPAMD_FEATURE_HYPERSCAN "0"
  149. #else
  150. #define RSPAMD_FEATURE_HYPERSCAN "1"
  151. #endif
  152. #ifndef WITH_PCRE2
  153. #define RSPAMD_FEATURE_PCRE2 "0"
  154. #else
  155. #define RSPAMD_FEATURE_PCRE2 "1"
  156. #endif
  157. #ifndef WITH_FANN
  158. #define RSPAMD_FEATURE_FANN "0"
  159. #else
  160. #define RSPAMD_FEATURE_FANN "1"
  161. #endif
  162. #ifndef WITH_SNOWBALL
  163. #define RSPAMD_FEATURE_SNOWBALL "0"
  164. #else
  165. #define RSPAMD_FEATURE_SNOWBALL "1"
  166. #endif
  167. #define RSPAMD_CUR_MODULE_VERSION 0x1
  168. #define RSPAMD_CUR_WORKER_VERSION 0x2
  169. #define RSPAMD_FEATURES \
  170. RSPAMD_FEATURE_HYPERSCAN RSPAMD_FEATURE_PCRE2 \
  171. RSPAMD_FEATURE_FANN RSPAMD_FEATURE_SNOWBALL
  172. #define RSPAMD_MODULE_VER \
  173. RSPAMD_CUR_MODULE_VERSION, /* Module version */ \
  174. RSPAMD_VERSION_NUM, /* Rspamd version */ \
  175. RSPAMD_FEATURES /* Compilation features */ \
  176. #define RSPAMD_WORKER_VER \
  177. RSPAMD_CUR_WORKER_VERSION, /* Worker version */ \
  178. RSPAMD_VERSION_NUM, /* Rspamd version */ \
  179. RSPAMD_FEATURES /* Compilation features */ \
  180. /**
  181. * Module
  182. */
  183. typedef struct module_s {
  184. const gchar *name;
  185. int (*module_init_func) (struct rspamd_config *cfg, struct module_ctx **ctx);
  186. int (*module_config_func) (struct rspamd_config *cfg);
  187. int (*module_reconfig_func) (struct rspamd_config *cfg);
  188. int (*module_attach_controller_func) (struct module_ctx *ctx,
  189. GHashTable *custom_commands);
  190. guint module_version;
  191. guint64 rspamd_version;
  192. const gchar *rspamd_features;
  193. guint ctx_offset;
  194. } module_t;
  195. enum rspamd_worker_socket_type {
  196. RSPAMD_WORKER_SOCKET_NONE = 0,
  197. RSPAMD_WORKER_SOCKET_TCP = (1 << 0),
  198. RSPAMD_WORKER_SOCKET_UDP = (1 << 1),
  199. };
  200. struct rspamd_worker_listen_socket {
  201. const rspamd_inet_addr_t *addr;
  202. gint fd;
  203. enum rspamd_worker_socket_type type;
  204. bool is_systemd;
  205. };
  206. typedef struct worker_s {
  207. const gchar *name;
  208. gpointer (*worker_init_func) (struct rspamd_config *cfg);
  209. void (*worker_start_func) (struct rspamd_worker *worker);
  210. int flags;
  211. int listen_type;
  212. guint worker_version;
  213. guint64 rspamd_version;
  214. const gchar *rspamd_features;
  215. } worker_t;
  216. /**
  217. * Check if loaded worker is compatible with rspamd
  218. * @param cfg
  219. * @param wrk
  220. * @return
  221. */
  222. gboolean rspamd_check_worker (struct rspamd_config *cfg, worker_t *wrk);
  223. /**
  224. * Check if loaded module is compatible with rspamd
  225. * @param cfg
  226. * @param wrk
  227. * @return
  228. */
  229. gboolean rspamd_check_module (struct rspamd_config *cfg, module_t *wrk);
  230. struct pidfh;
  231. struct rspamd_config;
  232. struct tokenizer;
  233. struct rspamd_stat_classifier;
  234. struct rspamd_classifier_config;
  235. struct rspamd_mime_part;
  236. struct rspamd_dns_resolver;
  237. struct rspamd_task;
  238. struct rspamd_cryptobox_library_ctx;
  239. /**
  240. * Server statistics
  241. */
  242. struct rspamd_stat {
  243. guint messages_scanned; /**< total number of messages scanned */
  244. guint actions_stat[METRIC_ACTION_MAX]; /**< statistic for each action */
  245. guint connections_count; /**< total connections count */
  246. guint control_connections_count; /**< connections count to control interface */
  247. guint messages_learned; /**< messages learned */
  248. };
  249. /**
  250. * Struct that determine main server object (for logging purposes)
  251. */
  252. struct rspamd_main {
  253. struct rspamd_config *cfg; /**< pointer to config structure */
  254. pid_t pid; /**< main pid */
  255. /* Pid file structure */
  256. rspamd_pidfh_t *pfh; /**< struct pidfh for pidfile */
  257. GQuark type; /**< process type */
  258. struct rspamd_stat *stat; /**< pointer to statistics */
  259. rspamd_mempool_t *server_pool; /**< server's memory pool */
  260. rspamd_mempool_mutex_t *start_mtx; /**< server is starting up */
  261. GHashTable *workers; /**< workers pool indexed by pid */
  262. GHashTable *spairs; /**< socket pairs requested by workers */
  263. rspamd_logger_t *logger;
  264. uid_t workers_uid; /**< worker's uid running to */
  265. gid_t workers_gid; /**< worker's gid running to */
  266. gboolean is_privilleged; /**< true if run in privilleged mode */
  267. gboolean wanna_die; /**< no respawn of processes */
  268. gboolean cores_throttling; /**< turn off cores when limits are exceeded */
  269. struct roll_history *history; /**< rolling history */
  270. struct ev_loop *event_loop;
  271. ev_signal term_ev, int_ev, hup_ev, usr1_ev; /**< signals */
  272. struct rspamd_http_context *http_ctx;
  273. };
  274. enum rspamd_exception_type {
  275. RSPAMD_EXCEPTION_NEWLINE = 0,
  276. RSPAMD_EXCEPTION_URL,
  277. RSPAMD_EXCEPTION_GENERIC,
  278. };
  279. /**
  280. * Structure to point exception in text from processing
  281. */
  282. struct rspamd_process_exception {
  283. goffset pos;
  284. guint len;
  285. gpointer ptr;
  286. enum rspamd_exception_type type;
  287. };
  288. /**
  289. * Control session object
  290. */
  291. struct controller_command;
  292. struct controller_session;
  293. typedef gboolean (*controller_func_t) (gchar **args,
  294. struct controller_session *session);
  295. struct controller_session {
  296. struct rspamd_worker *worker; /**< pointer to worker structure (controller in fact) */
  297. gint sock; /**< socket descriptor */
  298. struct controller_command *cmd; /**< real command */
  299. struct rspamd_config *cfg; /**< pointer to config file */
  300. GList *parts; /**< extracted mime parts */
  301. struct rspamd_async_session *s; /**< async session object */
  302. struct rspamd_dns_resolver *resolver; /**< DNS resolver */
  303. struct ev_loop *ev_base; /**< Event base */
  304. };
  305. struct zstd_dictionary {
  306. void *dict;
  307. gsize size;
  308. guint id;
  309. };
  310. struct rspamd_external_libs_ctx {
  311. void **local_addrs;
  312. struct rspamd_cryptobox_library_ctx *crypto_ctx;
  313. struct ottery_config *ottery_cfg;
  314. SSL_CTX *ssl_ctx;
  315. SSL_CTX *ssl_ctx_noverify;
  316. struct zstd_dictionary *in_dict;
  317. struct zstd_dictionary *out_dict;
  318. void *out_zstream;
  319. void *in_zstream;
  320. ref_entry_t ref;
  321. };
  322. /**
  323. * Register custom controller function
  324. */
  325. void register_custom_controller_command (const gchar *name,
  326. controller_func_t handler,
  327. gboolean privilleged,
  328. gboolean require_message);
  329. #ifdef __cplusplus
  330. }
  331. #endif
  332. #endif