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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*-
  2. * Copyright 2016 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 guint rspamd_map_log_id;
  29. #define msg_err_map(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \
  30. "map", map->tag, \
  31. G_STRFUNC, \
  32. __VA_ARGS__)
  33. #define msg_warn_map(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
  34. "map", map->tag, \
  35. G_STRFUNC, \
  36. __VA_ARGS__)
  37. #define msg_info_map(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \
  38. "map", map->tag, \
  39. G_STRFUNC, \
  40. __VA_ARGS__)
  41. #define msg_debug_map(...) rspamd_conditional_debug_fast (NULL, NULL, \
  42. rspamd_map_log_id, "map", map->tag, \
  43. G_STRFUNC, \
  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. gchar *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. guint64 gen;
  67. time_t last_checked;
  68. };
  69. struct rspamd_map_cachepoint {
  70. gint available;
  71. gsize len;
  72. time_t last_modified;
  73. gchar 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. gchar *userinfo;
  84. gchar *path;
  85. gchar *host;
  86. gchar *rest;
  87. rspamd_fstring_t *etag;
  88. time_t last_modified;
  89. time_t last_checked;
  90. gboolean request_sent;
  91. guint64 gen;
  92. guint16 port;
  93. };
  94. struct static_map_data {
  95. guchar *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_backend {
  105. enum fetch_proto protocol;
  106. gboolean is_signed;
  107. gboolean is_compressed;
  108. gboolean is_fallback;
  109. struct ev_loop *event_loop;
  110. guint32 id;
  111. struct rspamd_cryptobox_pubkey *trusted_pubkey;
  112. union rspamd_map_backend_data data;
  113. gchar *uri;
  114. ref_entry_t ref;
  115. };
  116. struct map_periodic_cbdata;
  117. struct rspamd_map {
  118. struct rspamd_dns_resolver *r;
  119. struct rspamd_config *cfg;
  120. GPtrArray *backends;
  121. struct rspamd_map_backend *fallback_backend;
  122. map_cb_t read_callback;
  123. map_fin_cb_t fin_callback;
  124. map_dtor_t dtor;
  125. void **user_data;
  126. struct ev_loop *event_loop;
  127. struct rspamd_worker *wrk;
  128. gchar *description;
  129. gchar *name;
  130. guint32 id;
  131. struct map_periodic_cbdata *scheduled_check;
  132. rspamd_map_tmp_dtor tmp_dtor;
  133. gpointer tmp_dtor_data;
  134. rspamd_map_traverse_function traverse_function;
  135. gpointer lua_map;
  136. gsize nelts;
  137. guint64 digest;
  138. /* Should we check HTTP or just load cached data */
  139. ev_tstamp timeout;
  140. gdouble poll_timeout;
  141. time_t next_check;
  142. gboolean active_http;
  143. gboolean non_trivial; /* E.g. has http backends in active mode */
  144. gboolean file_only; /* No HTTP backends found */
  145. gboolean static_only; /* No need to check */
  146. /* Shared lock for temporary disabling of map reading (e.g. when this map is written by UI) */
  147. gint *locked;
  148. gchar tag[MEMPOOL_UID_LEN];
  149. };
  150. enum rspamd_map_http_stage {
  151. http_map_resolve_host2 = 0, /* 2 requests sent */
  152. http_map_resolve_host1, /* 1 requests sent */
  153. http_map_http_conn, /* http connection */
  154. http_map_terminated /* terminated when doing resolving */
  155. };
  156. struct map_periodic_cbdata {
  157. struct rspamd_map *map;
  158. struct map_cb_data cbdata;
  159. ev_timer ev;
  160. gboolean need_modify;
  161. gboolean errored;
  162. gboolean locked;
  163. guint cur_backend;
  164. ref_entry_t ref;
  165. };
  166. static const gchar rspamd_http_file_magic[] =
  167. {'r', 'm', 'c', 'd', '2', '0', '0', '0'};
  168. struct rspamd_http_file_data {
  169. guchar magic[sizeof (rspamd_http_file_magic)];
  170. goffset data_off;
  171. gulong mtime;
  172. gulong next_check;
  173. gulong etag_len;
  174. };
  175. struct http_callback_data {
  176. struct ev_loop *event_loop;
  177. struct rspamd_http_connection *conn;
  178. GPtrArray *addrs;
  179. rspamd_inet_addr_t *addr;
  180. struct rspamd_map *map;
  181. struct rspamd_map_backend *bk;
  182. struct http_map_data *data;
  183. struct map_periodic_cbdata *periodic;
  184. struct rspamd_cryptobox_pubkey *pk;
  185. struct rspamd_storage_shmem *shmem_data;
  186. gsize data_len;
  187. gboolean check;
  188. enum rspamd_map_http_stage stage;
  189. ev_tstamp timeout;
  190. ref_entry_t ref;
  191. };
  192. #ifdef __cplusplus
  193. }
  194. #endif
  195. #endif /* SRC_LIBUTIL_MAP_PRIVATE_H_ */