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

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