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.

main.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 "fstring.h"
  9. #include "mem_pool.h"
  10. #include "statfile.h"
  11. #include "url.h"
  12. #include "memcached.h"
  13. #include "protocol.h"
  14. #include "filter.h"
  15. #include "buffer.h"
  16. #include "hash.h"
  17. #include "events.h"
  18. #include "util.h"
  19. #include "logger.h"
  20. /* Default values */
  21. #define FIXED_CONFIG_FILE ETC_PREFIX "/rspamd.xml"
  22. /* Time in seconds to exit for old worker */
  23. #define SOFT_SHUTDOWN_TIME 10
  24. /* Default metric name */
  25. #define DEFAULT_METRIC "default"
  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. gboolean is_initialized; /**< is initialized */
  42. gboolean is_dying; /**< if worker is going to shutdown */
  43. gboolean pending; /**< if worker is pending to run */
  44. struct rspamd_main *srv; /**< pointer to server structure */
  45. GQuark type; /**< process type */
  46. struct event sig_ev_usr1; /**< signals event */
  47. struct event sig_ev_usr2; /**< signals event */
  48. struct event bind_ev; /**< socket events */
  49. struct worker_conf *cf; /**< worker config data */
  50. gpointer ctx; /**< worker's specific data */
  51. };
  52. /**
  53. * Module
  54. */
  55. struct pidfh;
  56. struct config_file;
  57. struct tokenizer;
  58. struct classifier;
  59. struct classifier_config;
  60. struct mime_part;
  61. struct rspamd_view;
  62. struct rspamd_dns_resolver;
  63. struct worker_task;
  64. /**
  65. * Server statistics
  66. */
  67. struct rspamd_stat {
  68. guint messages_scanned; /**< total number of messages scanned */
  69. guint messages_spam; /**< messages treated as spam */
  70. guint messages_ham; /**< messages treated as ham */
  71. guint connections_count; /**< total connections count */
  72. guint control_connections_count; /**< connections count to control interface */
  73. guint messages_learned; /**< messages learned */
  74. guint fuzzy_hashes; /**< number of fuzzy hashes stored */
  75. guint fuzzy_hashes_expired; /**< number of fuzzy hashes expired */
  76. };
  77. /**
  78. * Struct that determine main server object (for logging purposes)
  79. */
  80. struct rspamd_main {
  81. struct config_file *cfg; /**< pointer to config structure */
  82. pid_t pid; /**< main pid */
  83. /* Pid file structure */
  84. rspamd_pidfh_t *pfh; /**< struct pidfh for pidfile */
  85. GQuark type; /**< process type */
  86. guint ev_initialized; /**< is event system is initialized */
  87. struct rspamd_stat *stat; /**< pointer to statistics */
  88. memory_pool_t *server_pool; /**< server's memory pool */
  89. statfile_pool_t *statfile_pool; /**< shared statfiles pool */
  90. GHashTable *workers; /**< workers pool indexed by pid */
  91. rspamd_logger_t *logger;
  92. uid_t workers_uid; /**< worker's uid running to */
  93. gid_t workers_gid; /**< worker's gid running to */
  94. gboolean is_privilleged; /**< true if run in privilleged mode */
  95. };
  96. struct counter_data {
  97. guint64 value;
  98. gint number;
  99. };
  100. /**
  101. * Structure to point exception in text from processing
  102. */
  103. struct process_exception {
  104. gsize pos;
  105. gsize len;
  106. };
  107. /**
  108. * Union that would be used for storing sockaddrs
  109. */
  110. union sa_union {
  111. struct sockaddr_storage ss;
  112. struct sockaddr_in s4;
  113. struct sockaddr_in6 s6;
  114. };
  115. /**
  116. * Control session object
  117. */
  118. struct controller_session {
  119. struct rspamd_worker *worker; /**< pointer to worker structure (controller in fact) */
  120. enum {
  121. STATE_COMMAND,
  122. STATE_LEARN,
  123. STATE_LEARN_SPAM_PRE,
  124. STATE_LEARN_SPAM,
  125. STATE_REPLY,
  126. STATE_QUIT,
  127. STATE_OTHER,
  128. STATE_WAIT,
  129. STATE_WEIGHTS
  130. } state; /**< current session state */
  131. gint sock; /**< socket descriptor */
  132. /* Access to authorized commands */
  133. gint authorized; /**< whether this session is authorized */
  134. memory_pool_t *session_pool; /**< memory pool for session */
  135. struct config_file *cfg; /**< pointer to config file */
  136. gchar *learn_rcpt; /**< recipient for learning */
  137. gchar *learn_from; /**< from address for learning */
  138. struct classifier_config *learn_classifier;
  139. gchar *learn_symbol; /**< symbol to train */
  140. double learn_multiplier; /**< multiplier for learning */
  141. rspamd_io_dispatcher_t *dispatcher; /**< IO dispatcher object */
  142. f_str_t *learn_buf; /**< learn input */
  143. GList *parts; /**< extracted mime parts */
  144. gint in_class; /**< positive or negative learn */
  145. void (*other_handler)(struct controller_session *session,
  146. f_str_t *in); /**< other command handler to execute at the end of processing */
  147. void *other_data; /**< and its data */
  148. struct rspamd_async_session* s; /**< async session object */
  149. struct worker_task *learn_task;
  150. struct rspamd_dns_resolver *resolver; /**< DNS resolver */
  151. struct event_base *ev_base; /**< Event base */
  152. };
  153. typedef void (*controller_func_t)(gchar **args, struct controller_session *session);
  154. /**
  155. * Worker task structure
  156. */
  157. struct worker_task {
  158. struct rspamd_worker *worker; /**< pointer to worker object */
  159. enum {
  160. READ_COMMAND,
  161. READ_HEADER,
  162. READ_MESSAGE,
  163. WRITE_REPLY,
  164. WRITE_ERROR,
  165. WAIT_FILTER,
  166. WAIT_POST_FILTER,
  167. CLOSING_CONNECTION,
  168. WRITING_REPLY
  169. } state; /**< current session state */
  170. size_t content_length; /**< length of user's input */
  171. enum rspamd_protocol proto; /**< protocol (rspamc or spamc) */
  172. guint proto_ver; /**< protocol version */
  173. enum rspamd_command cmd; /**< command */
  174. struct custom_command *custom_cmd; /**< custom command if any */
  175. gint sock; /**< socket descriptor */
  176. gboolean is_mime; /**< if this task is mime task */
  177. gboolean is_json; /**< output is JSON */
  178. gboolean is_http; /**< output is HTTP */
  179. gboolean allow_learn; /**< allow learning */
  180. gboolean is_skipped; /**< whether message was skipped by configuration */
  181. gchar *helo; /**< helo header value */
  182. gchar *from; /**< from header value */
  183. gchar *queue_id; /**< queue id if specified */
  184. const gchar *message_id; /**< message id */
  185. GList *rcpt; /**< recipients list */
  186. guint nrcpt; /**< number of recipients */
  187. struct in_addr from_addr; /**< client addr in numeric form */
  188. struct in_addr client_addr; /**< client addr in numeric form */
  189. gchar *deliver_to; /**< address to deliver */
  190. gchar *user; /**< user to deliver */
  191. gchar *subject; /**< subject (for non-mime) */
  192. gchar *statfile; /**< statfile for learning */
  193. f_str_t *msg; /**< message buffer */
  194. rspamd_io_dispatcher_t *dispatcher; /**< IO dispatcher object */
  195. struct rspamd_async_session* s; /**< async session object */
  196. gint parts_count; /**< mime parts count */
  197. GMimeMessage *message; /**< message, parsed with GMime */
  198. GMimeObject *parser_parent_part; /**< current parent part */
  199. InternetAddressList *rcpts; /**< list of all recipients */
  200. GList *parts; /**< list of parsed parts */
  201. GList *text_parts; /**< list of text parts */
  202. gchar *raw_headers_str; /**< list of raw headers */
  203. GList *received; /**< list of received headers */
  204. GTree *urls; /**< list of parsed urls */
  205. GTree *emails; /**< list of parsed emails */
  206. GList *images; /**< list of images */
  207. GHashTable *raw_headers; /**< list of raw headers */
  208. GHashTable *results; /**< hash table of metric_result indexed by
  209. * metric's name */
  210. GHashTable *tokens; /**< hash table of tokens indexed by tokenizer
  211. * pointer */
  212. GList *messages; /**< list of messages that would be reported */
  213. GHashTable *re_cache; /**< cache for matched or not matched regexps */
  214. struct config_file *cfg; /**< pointer to config object */
  215. gchar *last_error; /**< last error */
  216. gint error_code; /**< code of last error */
  217. memory_pool_t *task_pool; /**< memory pool for task */
  218. #ifdef HAVE_CLOCK_GETTIME
  219. struct timespec ts; /**< time of connection */
  220. #endif
  221. struct timeval tv; /**< time of connection */
  222. struct rspamd_view *view; /**< matching view */
  223. gboolean view_checked;
  224. gboolean pass_all_filters; /**< pass task throught every rule */
  225. guint32 parser_recursion; /**< for avoiding recursion stack overflow */
  226. gboolean (*fin_callback)(void *arg); /**< calback for filters finalizing */
  227. void *fin_arg; /**< argument for fin callback */
  228. guint32 dns_requests; /**< number of DNS requests per this task */
  229. struct rspamd_dns_resolver *resolver; /**< DNS resolver */
  230. struct event_base *ev_base; /**< Event base */
  231. };
  232. /**
  233. * Common structure representing C module context
  234. */
  235. struct module_ctx {
  236. gint (*filter)(struct worker_task *task); /**< pointer to headers process function */
  237. };
  238. /**
  239. * Common structure for C module
  240. */
  241. struct c_module {
  242. const gchar *name; /**< name */
  243. struct module_ctx *ctx; /**< pointer to context */
  244. };
  245. /**
  246. * Register custom controller function
  247. */
  248. void register_custom_controller_command (const gchar *name, controller_func_t handler, gboolean privilleged, gboolean require_message);
  249. /**
  250. * If set, reopen log file on next write
  251. */
  252. extern struct rspamd_main *rspamd_main;
  253. /* Worker task manipulations */
  254. /**
  255. * Construct new task for worker
  256. */
  257. struct worker_task* construct_task (struct rspamd_worker *worker);
  258. /**
  259. * Destroy task object and remove its IO dispatcher if it exists
  260. */
  261. void free_task (struct worker_task *task, gboolean is_soft);
  262. void free_task_hard (gpointer ud);
  263. void free_task_soft (gpointer ud);
  264. /**
  265. * Set counter for a symbol
  266. */
  267. double set_counter (const gchar *name, guint32 value);
  268. #endif
  269. /*
  270. * vi:ts=4
  271. */