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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 "libserver/url.h"
  15. #include "libserver/protocol.h"
  16. #include "libserver/buffer.h"
  17. #include "libserver/events.h"
  18. #include "libserver/roll_history.h"
  19. #include "libserver/task.h"
  20. #include <magic.h>
  21. /* Default values */
  22. #define FIXED_CONFIG_FILE RSPAMD_CONFDIR "/rspamd.conf"
  23. /* Time in seconds to exit for old worker */
  24. #define SOFT_SHUTDOWN_TIME 10
  25. /* Spam subject */
  26. #define SPAM_SUBJECT "*** SPAM *** "
  27. #ifdef CRLF
  28. #undef CRLF
  29. #undef CR
  30. #undef LF
  31. #endif
  32. #define CRLF "\r\n"
  33. #define CR '\r'
  34. #define LF '\n'
  35. /**
  36. * Worker process structure
  37. */
  38. struct rspamd_worker {
  39. pid_t pid; /**< pid of worker */
  40. guint index; /**< index number */
  41. guint nconns; /**< current connections count */
  42. gdouble start_time; /**< start time */
  43. struct rspamd_main *srv; /**< pointer to server structure */
  44. GQuark type; /**< process type */
  45. GHashTable *signal_events; /**< signal events */
  46. GList *accept_events; /**< socket events */
  47. struct rspamd_worker_conf *cf; /**< worker config data */
  48. gpointer ctx; /**< worker's specific data */
  49. gint control_pipe[2]; /**< control pipe. [0] is used by main process,
  50. [1] is used by a worker */
  51. gint srv_pipe[2]; /**< used by workers to request something from the
  52. main process. [0] - main, [1] - worker */
  53. struct event srv_ev; /**< used by main for read workers' requests */
  54. gpointer control_data; /**< used by control protocol to handle commands */
  55. };
  56. struct rspamd_worker_signal_handler;
  57. struct rspamd_worker_signal_cb {
  58. void (*handler) (struct rspamd_worker_signal_handler *, void *ud);
  59. void *handler_data;
  60. struct rspamd_worker_signal_cb *next, *prev;
  61. };
  62. struct rspamd_worker_signal_handler {
  63. gint signo;
  64. gboolean enabled;
  65. struct event ev;
  66. struct event_base *base;
  67. struct rspamd_worker *worker;
  68. struct rspamd_worker_signal_cb *cb;
  69. };
  70. struct rspamd_controller_pbkdf {
  71. gint id;
  72. guint rounds;
  73. gsize salt_len;
  74. gsize key_len;
  75. };
  76. /**
  77. * Common structure representing C module context
  78. */
  79. struct module_s;
  80. struct module_ctx {
  81. gint (*filter)(struct rspamd_task *task); /**< pointer to headers process function */
  82. struct module_s *mod; /**< module pointer */
  83. gboolean enabled; /**< true if module is enabled in configuration */
  84. };
  85. #ifndef WITH_HYPERSCAN
  86. #define RSPAMD_FEATURE_HYPERSCAN "0"
  87. #else
  88. #define RSPAMD_FEATURE_HYPERSCAN "1"
  89. #endif
  90. #ifndef WITH_PCRE2
  91. #define RSPAMD_FEATURE_PCRE2 "0"
  92. #else
  93. #define RSPAMD_FEATURE_PCRE2 "1"
  94. #endif
  95. #ifndef WITH_FANN
  96. #define RSPAMD_FEATURE_FANN "0"
  97. #else
  98. #define RSPAMD_FEATURE_FANN "1"
  99. #endif
  100. #ifndef WITH_SNOWBALL
  101. #define RSPAMD_FEATURE_SNOWBALL "0"
  102. #else
  103. #define RSPAMD_FEATURE_SNOWBALL "1"
  104. #endif
  105. #define RSPAMD_FEATURES \
  106. RSPAMD_FEATURE_HYPERSCAN RSPAMD_FEATURE_PCRE2 \
  107. RSPAMD_FEATURE_FANN RSPAMD_FEATURE_SNOWBALL
  108. #define RSPAMD_MODULE_VER \
  109. 0x1, /* Module version */ \
  110. RSPAMD_VERSION_NUM, /* Rspamd version */ \
  111. RSPAMD_FEATURES /* Compilation features */ \
  112. #define RSPAMD_WORKER_VER \
  113. 0x1, /* Worker version */ \
  114. RSPAMD_VERSION_NUM, /* Rspamd version */ \
  115. RSPAMD_FEATURES /* Compilation features */ \
  116. /**
  117. * Module
  118. */
  119. typedef struct module_s {
  120. const gchar *name;
  121. int (*module_init_func)(struct rspamd_config *cfg, struct module_ctx **ctx);
  122. int (*module_config_func)(struct rspamd_config *cfg);
  123. int (*module_reconfig_func)(struct rspamd_config *cfg);
  124. int (*module_attach_controller_func)(struct module_ctx *ctx,
  125. GHashTable *custom_commands);
  126. guint module_version;
  127. guint64 rspamd_version;
  128. const gchar *rspamd_features;
  129. } module_t;
  130. typedef struct worker_s {
  131. const gchar *name;
  132. gpointer (*worker_init_func)(struct rspamd_config *cfg);
  133. void (*worker_start_func)(struct rspamd_worker *worker);
  134. gboolean has_socket;
  135. gboolean unique;
  136. gboolean threaded;
  137. gboolean killable;
  138. gint listen_type;
  139. guint module_version;
  140. guint64 rspamd_version;
  141. const gchar *rspamd_features;
  142. } worker_t;
  143. struct pidfh;
  144. struct rspamd_config;
  145. struct tokenizer;
  146. struct rspamd_stat_classifier;
  147. struct rspamd_classifier_config;
  148. struct mime_part;
  149. struct rspamd_dns_resolver;
  150. struct rspamd_task;
  151. struct rspamd_cryptobox_library_ctx;
  152. /**
  153. * Server statistics
  154. */
  155. struct rspamd_stat {
  156. guint messages_scanned; /**< total number of messages scanned */
  157. guint actions_stat[METRIC_ACTION_NOACTION + 1]; /**< statistic for each action */
  158. guint connections_count; /**< total connections count */
  159. guint control_connections_count; /**< connections count to control interface */
  160. guint messages_learned; /**< messages learned */
  161. };
  162. /**
  163. * Struct that determine main server object (for logging purposes)
  164. */
  165. struct rspamd_main {
  166. struct rspamd_config *cfg; /**< pointer to config structure */
  167. pid_t pid; /**< main pid */
  168. /* Pid file structure */
  169. rspamd_pidfh_t *pfh; /**< struct pidfh for pidfile */
  170. GQuark type; /**< process type */
  171. struct rspamd_stat *stat; /**< pointer to statistics */
  172. rspamd_mempool_t *server_pool; /**< server's memory pool */
  173. GHashTable *workers; /**< workers pool indexed by pid */
  174. GHashTable *spairs; /**< socket pairs requested by workers */
  175. rspamd_logger_t *logger;
  176. uid_t workers_uid; /**< worker's uid running to */
  177. gid_t workers_gid; /**< worker's gid running to */
  178. gboolean is_privilleged; /**< true if run in privilleged mode */
  179. gboolean cores_throttling; /**< turn off cores when limits are exceeded */
  180. struct roll_history *history; /**< rolling history */
  181. struct event_base *ev_base;
  182. };
  183. /**
  184. * Structure to point exception in text from processing
  185. */
  186. struct process_exception {
  187. gsize pos;
  188. gsize len;
  189. };
  190. /**
  191. * Control session object
  192. */
  193. struct controller_command;
  194. struct controller_session;
  195. typedef gboolean (*controller_func_t)(gchar **args,
  196. struct controller_session *session);
  197. struct controller_session {
  198. struct rspamd_worker *worker; /**< pointer to worker structure (controller in fact) */
  199. gint sock; /**< socket descriptor */
  200. struct controller_command *cmd; /**< real command */
  201. struct rspamd_config *cfg; /**< pointer to config file */
  202. GList *parts; /**< extracted mime parts */
  203. struct rspamd_async_session * s; /**< async session object */
  204. struct rspamd_dns_resolver *resolver; /**< DNS resolver */
  205. struct event_base *ev_base; /**< Event base */
  206. };
  207. struct rspamd_external_libs_ctx {
  208. magic_t libmagic;
  209. void **local_addrs;
  210. struct rspamd_cryptobox_library_ctx *crypto_ctx;
  211. struct ottery_config *ottery_cfg;
  212. ref_entry_t ref;
  213. };
  214. /**
  215. * Register custom controller function
  216. */
  217. void register_custom_controller_command (const gchar *name,
  218. controller_func_t handler,
  219. gboolean privilleged,
  220. gboolean require_message);
  221. #define RSPAMD_PBKDF_ID_V1 1
  222. extern const struct rspamd_controller_pbkdf pbkdf_list[];
  223. #endif
  224. /*
  225. * vi:ts=4
  226. */