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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. /* Default values */
  21. #define FIXED_CONFIG_FILE RSPAMD_CONFDIR "/rspamd.conf"
  22. /* Time in seconds to exit for old worker */
  23. #define SOFT_SHUTDOWN_TIME 10
  24. /* Spam subject */
  25. #define SPAM_SUBJECT "*** SPAM *** "
  26. #ifdef CRLF
  27. #undef CRLF
  28. #undef CR
  29. #undef LF
  30. #endif
  31. #define CRLF "\r\n"
  32. #define CR '\r'
  33. #define LF '\n'
  34. /**
  35. * Worker process structure
  36. */
  37. struct rspamd_worker {
  38. pid_t pid; /**< pid of worker */
  39. guint index; /**< index number */
  40. guint nconns; /**< current connections count */
  41. gdouble start_time; /**< start time */
  42. struct rspamd_main *srv; /**< pointer to server structure */
  43. GQuark type; /**< process type */
  44. GHashTable *signal_events; /**< signal events */
  45. GList *accept_events; /**< socket events */
  46. struct rspamd_worker_conf *cf; /**< worker config data */
  47. gpointer ctx; /**< worker's specific data */
  48. gint control_pipe[2]; /**< control pipe. [0] is used by main process,
  49. [1] is used by a worker */
  50. gpointer control_data; /**< used by control protocol to handle commands */
  51. };
  52. struct rspamd_worker_signal_handler;
  53. struct rspamd_worker_signal_cb {
  54. void (*handler) (struct rspamd_worker_signal_handler *, void *ud);
  55. void *handler_data;
  56. struct rspamd_worker_signal_cb *next, *prev;
  57. };
  58. struct rspamd_worker_signal_handler {
  59. gint signo;
  60. gboolean enabled;
  61. struct event ev;
  62. struct event_base *base;
  63. struct rspamd_worker *worker;
  64. struct rspamd_worker_signal_cb *cb;
  65. };
  66. struct rspamd_controller_pbkdf {
  67. gint id;
  68. guint rounds;
  69. gsize salt_len;
  70. gsize key_len;
  71. };
  72. /**
  73. * Common structure representing C module context
  74. */
  75. struct module_s;
  76. struct module_ctx {
  77. gint (*filter)(struct rspamd_task *task); /**< pointer to headers process function */
  78. struct module_s *mod; /**< module pointer */
  79. gboolean enabled; /**< true if module is enabled in configuration */
  80. };
  81. /**
  82. * Module
  83. */
  84. typedef struct module_s {
  85. const gchar *name;
  86. int (*module_init_func)(struct rspamd_config *cfg, struct module_ctx **ctx);
  87. int (*module_config_func)(struct rspamd_config *cfg);
  88. int (*module_reconfig_func)(struct rspamd_config *cfg);
  89. int (*module_attach_controller_func)(struct module_ctx *ctx,
  90. GHashTable *custom_commands);
  91. } module_t;
  92. typedef struct worker_s {
  93. const gchar *name;
  94. gpointer (*worker_init_func)(struct rspamd_config *cfg);
  95. void (*worker_start_func)(struct rspamd_worker *worker);
  96. gboolean has_socket;
  97. gboolean unique;
  98. gboolean threaded;
  99. gboolean killable;
  100. gint listen_type;
  101. } worker_t;
  102. struct pidfh;
  103. struct rspamd_config;
  104. struct tokenizer;
  105. struct rspamd_stat_classifier;
  106. struct rspamd_classifier_config;
  107. struct mime_part;
  108. struct rspamd_dns_resolver;
  109. struct rspamd_task;
  110. /**
  111. * The epoch of the fuzzy client
  112. */
  113. enum rspamd_fuzzy_epoch {
  114. RSPAMD_FUZZY_EPOCH6 = 0, /**< pre 0.6.x */
  115. RSPAMD_FUZZY_EPOCH8, /**< 0.8 till 0.9 */
  116. RSPAMD_FUZZY_EPOCH9, /**< 0.9 + */
  117. RSPAMD_FUZZY_EPOCH10, /**< 1.0 + encryption */
  118. RSPAMD_FUZZY_EPOCH_MAX
  119. };
  120. /**
  121. * Server statistics
  122. */
  123. struct rspamd_stat {
  124. guint messages_scanned; /**< total number of messages scanned */
  125. guint actions_stat[METRIC_ACTION_NOACTION + 1]; /**< statistic for each action */
  126. guint connections_count; /**< total connections count */
  127. guint control_connections_count; /**< connections count to control interface */
  128. guint messages_learned; /**< messages learned */
  129. guint fuzzy_hashes; /**< number of fuzzy hashes stored */
  130. guint fuzzy_hashes_expired; /**< number of fuzzy hashes expired */
  131. guint64 fuzzy_hashes_checked[RSPAMD_FUZZY_EPOCH_MAX]; /**< ammount of check requests for each epoch */
  132. guint64 fuzzy_hashes_found[RSPAMD_FUZZY_EPOCH_MAX]; /**< amount of hashes found by epoch */
  133. };
  134. /**
  135. * Struct that determine main server object (for logging purposes)
  136. */
  137. struct rspamd_main {
  138. struct rspamd_config *cfg; /**< pointer to config structure */
  139. pid_t pid; /**< main pid */
  140. /* Pid file structure */
  141. rspamd_pidfh_t *pfh; /**< struct pidfh for pidfile */
  142. GQuark type; /**< process type */
  143. guint ev_initialized; /**< is event system is initialized */
  144. struct rspamd_stat *stat; /**< pointer to statistics */
  145. rspamd_mempool_t *server_pool; /**< server's memory pool */
  146. GHashTable *workers; /**< workers pool indexed by pid */
  147. rspamd_logger_t *logger;
  148. uid_t workers_uid; /**< worker's uid running to */
  149. gid_t workers_gid; /**< worker's gid running to */
  150. gboolean is_privilleged; /**< true if run in privilleged mode */
  151. struct roll_history *history; /**< rolling history */
  152. struct event_base *ev_base;
  153. };
  154. /**
  155. * Structure to point exception in text from processing
  156. */
  157. struct process_exception {
  158. gsize pos;
  159. gsize len;
  160. };
  161. /**
  162. * Control session object
  163. */
  164. struct controller_command;
  165. struct controller_session;
  166. typedef gboolean (*controller_func_t)(gchar **args,
  167. struct controller_session *session);
  168. struct controller_session {
  169. struct rspamd_worker *worker; /**< pointer to worker structure (controller in fact) */
  170. enum {
  171. STATE_COMMAND,
  172. STATE_HEADER,
  173. STATE_LEARN,
  174. STATE_LEARN_SPAM_PRE,
  175. STATE_LEARN_SPAM,
  176. STATE_REPLY,
  177. STATE_QUIT,
  178. STATE_OTHER,
  179. STATE_WAIT,
  180. STATE_WEIGHTS
  181. } state; /**< current session state */
  182. gint sock; /**< socket descriptor */
  183. /* Access to authorized commands */
  184. gboolean authorized; /**< whether this session is authorized */
  185. gboolean restful; /**< whether this session is a restful session */
  186. GHashTable *kwargs; /**< keyword arguments for restful command */
  187. struct controller_command *cmd; /**< real command */
  188. rspamd_mempool_t *session_pool; /**< memory pool for session */
  189. struct rspamd_config *cfg; /**< pointer to config file */
  190. gchar *learn_rcpt; /**< recipient for learning */
  191. gchar *learn_from; /**< from address for learning */
  192. struct rspamd_classifier_config *learn_classifier;
  193. gchar *learn_symbol; /**< symbol to train */
  194. double learn_multiplier; /**< multiplier for learning */
  195. rspamd_io_dispatcher_t *dispatcher; /**< IO dispatcher object */
  196. rspamd_fstring_t *learn_buf; /**< learn input */
  197. GList *parts; /**< extracted mime parts */
  198. gint in_class; /**< positive or negative learn */
  199. gboolean (*other_handler)(struct controller_session *session,
  200. rspamd_fstring_t *in); /**< other command handler to execute at the end of processing */
  201. void *other_data; /**< and its data */
  202. controller_func_t custom_handler; /**< custom command handler */
  203. struct rspamd_async_session * s; /**< async session object */
  204. struct rspamd_task *learn_task;
  205. struct rspamd_dns_resolver *resolver; /**< DNS resolver */
  206. struct event_base *ev_base; /**< Event base */
  207. };
  208. /**
  209. * Register custom controller function
  210. */
  211. void register_custom_controller_command (const gchar *name,
  212. controller_func_t handler,
  213. gboolean privilleged,
  214. gboolean require_message);
  215. #define RSPAMD_PBKDF_ID_V1 1
  216. extern const struct rspamd_controller_pbkdf pbkdf_list[];
  217. #endif
  218. /*
  219. * vi:ts=4
  220. */