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

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