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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. #include "roll_history.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. /* Default metric name */
  26. #define DEFAULT_METRIC "default"
  27. /* Spam subject */
  28. #define SPAM_SUBJECT "*** SPAM *** "
  29. #ifdef CRLF
  30. #undef CRLF
  31. #undef CR
  32. #undef LF
  33. #endif
  34. #define CRLF "\r\n"
  35. #define CR '\r'
  36. #define LF '\n'
  37. /**
  38. * Worker process structure
  39. */
  40. struct rspamd_worker {
  41. pid_t pid; /**< pid of worker */
  42. gboolean is_initialized; /**< is initialized */
  43. gboolean is_dying; /**< if worker is going to shutdown */
  44. gboolean pending; /**< if worker is pending to run */
  45. struct rspamd_main *srv; /**< pointer to server structure */
  46. GQuark type; /**< process type */
  47. struct event sig_ev_usr1; /**< signals event */
  48. struct event sig_ev_usr2; /**< signals event */
  49. GList *accept_events; /**< socket events */
  50. struct worker_conf *cf; /**< worker config data */
  51. gpointer ctx; /**< worker's specific data */
  52. };
  53. /**
  54. * Module
  55. */
  56. struct pidfh;
  57. struct config_file;
  58. struct tokenizer;
  59. struct classifier;
  60. struct classifier_config;
  61. struct mime_part;
  62. struct rspamd_view;
  63. struct rspamd_dns_resolver;
  64. struct worker_task;
  65. /**
  66. * Server statistics
  67. */
  68. struct rspamd_stat {
  69. guint messages_scanned; /**< total number of messages scanned */
  70. guint actions_stat[METRIC_ACTION_NOACTION + 1]; /**< statistic for each action */
  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_hash_t *counters; /**< symbol cache counters */
  92. rspamd_logger_t *logger;
  93. uid_t workers_uid; /**< worker's uid running to */
  94. gid_t workers_gid; /**< worker's gid running to */
  95. gboolean is_privilleged; /**< true if run in privilleged mode */
  96. struct roll_history *history; /**< rolling history */
  97. };
  98. struct counter_data {
  99. guint64 value;
  100. gint number;
  101. };
  102. /**
  103. * Structure to point exception in text from processing
  104. */
  105. struct process_exception {
  106. gsize pos;
  107. gsize len;
  108. };
  109. /**
  110. * Union that would be used for storing sockaddrs
  111. */
  112. union sa_union {
  113. struct sockaddr_storage ss;
  114. struct sockaddr_in s4;
  115. struct sockaddr_in6 s6;
  116. };
  117. /**
  118. * Control session object
  119. */
  120. struct controller_command;
  121. struct controller_session;
  122. typedef void (*controller_func_t)(gchar **args, struct controller_session *session);
  123. struct controller_session {
  124. struct rspamd_worker *worker; /**< pointer to worker structure (controller in fact) */
  125. enum {
  126. STATE_COMMAND,
  127. STATE_HEADER,
  128. STATE_LEARN,
  129. STATE_LEARN_SPAM_PRE,
  130. STATE_LEARN_SPAM,
  131. STATE_REPLY,
  132. STATE_QUIT,
  133. STATE_OTHER,
  134. STATE_WAIT,
  135. STATE_WEIGHTS
  136. } state; /**< current session state */
  137. gint sock; /**< socket descriptor */
  138. /* Access to authorized commands */
  139. gboolean authorized; /**< whether this session is authorized */
  140. gboolean restful; /**< whether this session is a restful session */
  141. GHashTable *kwargs; /**< keyword arguments for restful command */
  142. struct controller_command *cmd; /**< real command */
  143. memory_pool_t *session_pool; /**< memory pool for session */
  144. struct config_file *cfg; /**< pointer to config file */
  145. gchar *learn_rcpt; /**< recipient for learning */
  146. gchar *learn_from; /**< from address for learning */
  147. struct classifier_config *learn_classifier;
  148. gchar *learn_symbol; /**< symbol to train */
  149. double learn_multiplier; /**< multiplier for learning */
  150. rspamd_io_dispatcher_t *dispatcher; /**< IO dispatcher object */
  151. f_str_t *learn_buf; /**< learn input */
  152. GList *parts; /**< extracted mime parts */
  153. gint in_class; /**< positive or negative learn */
  154. void (*other_handler)(struct controller_session *session,
  155. f_str_t *in); /**< other command handler to execute at the end of processing */
  156. void *other_data; /**< and its data */
  157. controller_func_t custom_handler; /**< custom command handler */
  158. struct rspamd_async_session* s; /**< async session object */
  159. struct worker_task *learn_task;
  160. struct rspamd_dns_resolver *resolver; /**< DNS resolver */
  161. struct event_base *ev_base; /**< Event base */
  162. };
  163. /**
  164. * Worker task structure
  165. */
  166. struct worker_task {
  167. struct rspamd_worker *worker; /**< pointer to worker object */
  168. enum {
  169. READ_COMMAND,
  170. READ_HEADER,
  171. READ_MESSAGE,
  172. WRITE_REPLY,
  173. WRITE_ERROR,
  174. WAIT_PRE_FILTER,
  175. WAIT_FILTER,
  176. WAIT_POST_FILTER,
  177. CLOSING_CONNECTION,
  178. WRITING_REPLY
  179. } state; /**< current session state */
  180. size_t content_length; /**< length of user's input */
  181. enum rspamd_protocol proto; /**< protocol (rspamc or spamc) */
  182. guint proto_ver; /**< protocol version */
  183. enum rspamd_command cmd; /**< command */
  184. struct custom_command *custom_cmd; /**< custom command if any */
  185. gint sock; /**< socket descriptor */
  186. gboolean is_mime; /**< if this task is mime task */
  187. gboolean is_json; /**< output is JSON */
  188. gboolean is_http; /**< output is HTTP */
  189. gboolean allow_learn; /**< allow learning */
  190. gboolean is_skipped; /**< whether message was skipped by configuration */
  191. gchar *helo; /**< helo header value */
  192. gchar *from; /**< from header value */
  193. gchar *queue_id; /**< queue id if specified */
  194. const gchar *message_id; /**< message id */
  195. GList *rcpt; /**< recipients list */
  196. guint nrcpt; /**< number of recipients */
  197. #ifdef HAVE_INET_PTON
  198. struct {
  199. union {
  200. struct in_addr in4;
  201. struct in6_addr in6;
  202. } d;
  203. gboolean ipv6;
  204. gboolean has_addr;
  205. } from_addr;
  206. #else
  207. struct in_addr from_addr; /**< client addr in numeric form */
  208. #endif
  209. struct in_addr client_addr; /**< client addr in numeric form */
  210. gchar *deliver_to; /**< address to deliver */
  211. gchar *user; /**< user to deliver */
  212. gchar *subject; /**< subject (for non-mime) */
  213. gchar *statfile; /**< statfile for learning */
  214. f_str_t *msg; /**< message buffer */
  215. rspamd_io_dispatcher_t *dispatcher; /**< IO dispatcher object */
  216. struct rspamd_async_session* s; /**< async session object */
  217. gint parts_count; /**< mime parts count */
  218. GMimeMessage *message; /**< message, parsed with GMime */
  219. GMimeObject *parser_parent_part; /**< current parent part */
  220. InternetAddressList *rcpts; /**< list of all recipients */
  221. GList *parts; /**< list of parsed parts */
  222. GList *text_parts; /**< list of text parts */
  223. gchar *raw_headers_str; /**< list of raw headers */
  224. GList *received; /**< list of received headers */
  225. GTree *urls; /**< list of parsed urls */
  226. GTree *emails; /**< list of parsed emails */
  227. GList *images; /**< list of images */
  228. GHashTable *raw_headers; /**< list of raw headers */
  229. GHashTable *results; /**< hash table of metric_result indexed by
  230. * metric's name */
  231. GHashTable *tokens; /**< hash table of tokens indexed by tokenizer
  232. * pointer */
  233. GList *messages; /**< list of messages that would be reported */
  234. GHashTable *re_cache; /**< cache for matched or not matched regexps */
  235. struct config_file *cfg; /**< pointer to config object */
  236. gchar *last_error; /**< last error */
  237. gint error_code; /**< code of last error */
  238. memory_pool_t *task_pool; /**< memory pool for task */
  239. #ifdef HAVE_CLOCK_GETTIME
  240. struct timespec ts; /**< time of connection */
  241. #endif
  242. struct timeval tv; /**< time of connection */
  243. struct rspamd_view *view; /**< matching view */
  244. guint32 scan_milliseconds; /**< how much milliseconds passed */
  245. gboolean view_checked;
  246. gboolean pass_all_filters; /**< pass task throught every rule */
  247. guint32 parser_recursion; /**< for avoiding recursion stack overflow */
  248. gboolean (*fin_callback)(void *arg); /**< calback for filters finalizing */
  249. void *fin_arg; /**< argument for fin callback */
  250. guint32 dns_requests; /**< number of DNS requests per this task */
  251. struct rspamd_dns_resolver *resolver; /**< DNS resolver */
  252. struct event_base *ev_base; /**< Event base */
  253. struct {
  254. enum rspamd_metric_action action; /**< Action of pre filters */
  255. gchar *str; /**< String describing action */
  256. } pre_result; /**< Result of pre-filters */
  257. };
  258. /**
  259. * Common structure representing C module context
  260. */
  261. struct module_ctx {
  262. gint (*filter)(struct worker_task *task); /**< pointer to headers process function */
  263. };
  264. /**
  265. * Common structure for C module
  266. */
  267. struct c_module {
  268. const gchar *name; /**< name */
  269. struct module_ctx *ctx; /**< pointer to context */
  270. };
  271. /**
  272. * Register custom controller function
  273. */
  274. void register_custom_controller_command (const gchar *name, controller_func_t handler, gboolean privilleged, gboolean require_message);
  275. /**
  276. * If set, reopen log file on next write
  277. */
  278. extern struct rspamd_main *rspamd_main;
  279. /* Worker task manipulations */
  280. /**
  281. * Construct new task for worker
  282. */
  283. struct worker_task* construct_task (struct rspamd_worker *worker);
  284. /**
  285. * Destroy task object and remove its IO dispatcher if it exists
  286. */
  287. void free_task (struct worker_task *task, gboolean is_soft);
  288. void free_task_hard (gpointer ud);
  289. void free_task_soft (gpointer ud);
  290. /**
  291. * Set counter for a symbol
  292. */
  293. double set_counter (const gchar *name, guint32 value);
  294. #ifndef HAVE_SA_SIGINFO
  295. typedef void (*rspamd_sig_handler_t) (gint);
  296. #else
  297. typedef void (*rspamd_sig_handler_t) (gint, siginfo_t *, void *);
  298. #endif
  299. /**
  300. * Prepare worker's startup
  301. * @param worker worker structure
  302. * @param name name of the worker
  303. * @param sig_handler handler of main signals
  304. * @param accept_handler handler of accept event for listen sockets
  305. * @return event base suitable for a worker
  306. */
  307. struct event_base *
  308. prepare_worker (struct rspamd_worker *worker, const char *name,
  309. rspamd_sig_handler_t sig_handler,
  310. void (*accept_handler)(int, short, void *));
  311. /**
  312. * Stop accepting new connections for a worker
  313. * @param worker
  314. */
  315. void worker_stop_accept (struct rspamd_worker *worker);
  316. #endif
  317. /*
  318. * vi:ts=4
  319. */