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

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