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.

map_private.h 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright 2024 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef SRC_LIBUTIL_MAP_PRIVATE_H_
  17. #define SRC_LIBUTIL_MAP_PRIVATE_H_
  18. #include "config.h"
  19. #include "mem_pool.h"
  20. #include "keypair.h"
  21. #include "unix-std.h"
  22. #include "map.h"
  23. #include "ref.h"
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. typedef void (*rspamd_map_tmp_dtor)(gpointer p);
  28. extern unsigned int rspamd_map_log_id;
  29. #define msg_err_map(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \
  30. "map", map->tag, \
  31. RSPAMD_LOG_FUNC, \
  32. __VA_ARGS__)
  33. #define msg_warn_map(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \
  34. "map", map->tag, \
  35. RSPAMD_LOG_FUNC, \
  36. __VA_ARGS__)
  37. #define msg_info_map(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \
  38. "map", map->tag, \
  39. RSPAMD_LOG_FUNC, \
  40. __VA_ARGS__)
  41. #define msg_debug_map(...) rspamd_conditional_debug_fast(NULL, NULL, \
  42. rspamd_map_log_id, "map", map->tag, \
  43. RSPAMD_LOG_FUNC, \
  44. __VA_ARGS__)
  45. enum fetch_proto {
  46. MAP_PROTO_FILE,
  47. MAP_PROTO_HTTP,
  48. MAP_PROTO_HTTPS,
  49. MAP_PROTO_STATIC
  50. };
  51. /**
  52. * Data specific to file maps
  53. */
  54. struct file_map_data {
  55. char *filename;
  56. gboolean need_modify;
  57. ev_stat st_ev;
  58. };
  59. struct http_map_data;
  60. struct rspamd_http_map_cached_cbdata {
  61. ev_timer timeout;
  62. struct ev_loop *event_loop;
  63. struct rspamd_storage_shmem *shm;
  64. struct rspamd_map *map;
  65. struct http_map_data *data;
  66. uint64_t gen;
  67. time_t last_checked;
  68. };
  69. struct rspamd_map_cachepoint {
  70. int available;
  71. gsize len;
  72. time_t last_modified;
  73. char shmem_name[256];
  74. };
  75. /**
  76. * Data specific to HTTP maps
  77. */
  78. struct http_map_data {
  79. /* Shared cache data */
  80. struct rspamd_map_cachepoint *cache;
  81. /* Non-shared for cache owner, used to cleanup cache */
  82. struct rspamd_http_map_cached_cbdata *cur_cache_cbd;
  83. char *userinfo;
  84. char *path;
  85. char *host;
  86. char *rest;
  87. rspamd_fstring_t *etag;
  88. time_t last_modified;
  89. time_t last_checked;
  90. gboolean request_sent;
  91. uint64_t gen;
  92. uint16_t port;
  93. };
  94. struct static_map_data {
  95. unsigned char *data;
  96. gsize len;
  97. gboolean processed;
  98. };
  99. union rspamd_map_backend_data {
  100. struct file_map_data *fd;
  101. struct http_map_data *hd;
  102. struct static_map_data *sd;
  103. };
  104. struct rspamd_map;
  105. struct rspamd_map_backend {
  106. enum fetch_proto protocol;
  107. gboolean is_signed;
  108. gboolean is_compressed;
  109. gboolean is_fallback;
  110. struct rspamd_map *map;
  111. struct ev_loop *event_loop;
  112. uint32_t id;
  113. struct rspamd_cryptobox_pubkey *trusted_pubkey;
  114. union rspamd_map_backend_data data;
  115. char *uri;
  116. ref_entry_t ref;
  117. };
  118. struct map_periodic_cbdata;
  119. struct rspamd_map {
  120. struct rspamd_dns_resolver *r;
  121. struct rspamd_config *cfg;
  122. GPtrArray *backends;
  123. struct rspamd_map_backend *fallback_backend;
  124. map_cb_t read_callback;
  125. map_fin_cb_t fin_callback;
  126. map_dtor_t dtor;
  127. void **user_data;
  128. struct ev_loop *event_loop;
  129. struct rspamd_worker *wrk;
  130. char *description;
  131. char *name;
  132. uint32_t id;
  133. struct map_periodic_cbdata *scheduled_check;
  134. rspamd_map_tmp_dtor tmp_dtor;
  135. gpointer tmp_dtor_data;
  136. rspamd_map_traverse_function traverse_function;
  137. rspamd_map_on_load_function on_load_function;
  138. gpointer on_load_ud;
  139. GDestroyNotify on_load_ud_dtor;
  140. gpointer lua_map;
  141. gsize nelts;
  142. uint64_t digest;
  143. /* Should we check HTTP or just load cached data */
  144. ev_tstamp timeout;
  145. double poll_timeout;
  146. time_t next_check;
  147. bool active_http;
  148. bool non_trivial; /* E.g. has http backends in active mode */
  149. bool file_only; /* No HTTP backends found */
  150. bool static_only; /* No need to check */
  151. bool no_file_read; /* Do not read files */
  152. bool seen; /* This map has already been watched or pre-loaded */
  153. /* Shared lock for temporary disabling of map reading (e.g. when this map is written by UI) */
  154. int *locked;
  155. char tag[MEMPOOL_UID_LEN];
  156. };
  157. enum rspamd_map_http_stage {
  158. http_map_resolve_host2 = 0, /* 2 requests sent */
  159. http_map_resolve_host1, /* 1 requests sent */
  160. http_map_http_conn, /* http connection */
  161. http_map_terminated /* terminated when doing resolving */
  162. };
  163. struct map_periodic_cbdata {
  164. struct rspamd_map *map;
  165. struct map_cb_data cbdata;
  166. ev_timer ev;
  167. gboolean need_modify;
  168. gboolean errored;
  169. gboolean locked;
  170. unsigned int cur_backend;
  171. ref_entry_t ref;
  172. };
  173. static const char rspamd_http_file_magic[] =
  174. {'r', 'm', 'c', 'd', '2', '0', '0', '0'};
  175. struct rspamd_http_file_data {
  176. unsigned char magic[sizeof(rspamd_http_file_magic)];
  177. goffset data_off;
  178. gulong mtime;
  179. gulong next_check;
  180. gulong etag_len;
  181. };
  182. struct http_callback_data {
  183. struct ev_loop *event_loop;
  184. struct rspamd_http_connection *conn;
  185. GPtrArray *addrs;
  186. rspamd_inet_addr_t *addr;
  187. struct rspamd_map *map;
  188. struct rspamd_map_backend *bk;
  189. struct http_map_data *data;
  190. struct map_periodic_cbdata *periodic;
  191. struct rspamd_cryptobox_pubkey *pk;
  192. struct rspamd_storage_shmem *shmem_data;
  193. gsize data_len;
  194. gboolean check;
  195. enum rspamd_map_http_stage stage;
  196. ev_tstamp timeout;
  197. ref_entry_t ref;
  198. };
  199. #ifdef __cplusplus
  200. }
  201. #endif
  202. #endif /* SRC_LIBUTIL_MAP_PRIVATE_H_ */