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 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. typedef void (*rspamd_map_tmp_dtor) (gpointer p);
  25. extern guint rspamd_map_log_id;
  26. #define msg_err_map(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \
  27. "map", map->tag, \
  28. G_STRFUNC, \
  29. __VA_ARGS__)
  30. #define msg_warn_map(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
  31. "map", map->tag, \
  32. G_STRFUNC, \
  33. __VA_ARGS__)
  34. #define msg_info_map(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \
  35. "map", map->tag, \
  36. G_STRFUNC, \
  37. __VA_ARGS__)
  38. #define msg_debug_map(...) rspamd_conditional_debug_fast (NULL, NULL, \
  39. rspamd_map_log_id, "map", map->tag, \
  40. G_STRFUNC, \
  41. __VA_ARGS__)
  42. enum fetch_proto {
  43. MAP_PROTO_FILE,
  44. MAP_PROTO_HTTP,
  45. MAP_PROTO_HTTPS,
  46. MAP_PROTO_STATIC
  47. };
  48. /**
  49. * Data specific to file maps
  50. */
  51. struct file_map_data {
  52. gchar *filename;
  53. struct stat st;
  54. };
  55. struct http_map_data;
  56. struct rspamd_http_map_cached_cbdata {
  57. struct event timeout;
  58. struct rspamd_storage_shmem *shm;
  59. struct rspamd_map *map;
  60. struct http_map_data *data;
  61. guint64 gen;
  62. time_t last_checked;
  63. };
  64. struct rspamd_map_cachepoint {
  65. gint available;
  66. gsize len;
  67. time_t last_modified;
  68. gchar shmem_name[256];
  69. };
  70. /**
  71. * Data specific to HTTP maps
  72. */
  73. struct http_map_data {
  74. /* Shared cache data */
  75. struct rspamd_map_cachepoint *cache;
  76. /* Non-shared for cache owner, used to cleanup cache */
  77. struct rspamd_http_map_cached_cbdata *cur_cache_cbd;
  78. gchar *userinfo;
  79. gchar *path;
  80. gchar *host;
  81. gchar *rest;
  82. gchar *last_signature;
  83. rspamd_fstring_t *etag;
  84. time_t last_modified;
  85. time_t last_checked;
  86. gboolean request_sent;
  87. guint64 gen;
  88. guint16 port;
  89. };
  90. struct static_map_data {
  91. guchar *data;
  92. gsize len;
  93. gboolean processed;
  94. };
  95. union rspamd_map_backend_data {
  96. struct file_map_data *fd;
  97. struct http_map_data *hd;
  98. struct static_map_data *sd;
  99. };
  100. struct rspamd_map_backend {
  101. enum fetch_proto protocol;
  102. gboolean is_signed;
  103. gboolean is_compressed;
  104. gboolean is_fallback;
  105. guint32 id;
  106. struct rspamd_cryptobox_pubkey *trusted_pubkey;
  107. union rspamd_map_backend_data data;
  108. gchar *uri;
  109. ref_entry_t ref;
  110. };
  111. struct rspamd_map {
  112. struct rspamd_dns_resolver *r;
  113. struct rspamd_config *cfg;
  114. GPtrArray *backends;
  115. struct rspamd_map_backend *fallback_backend;
  116. map_cb_t read_callback;
  117. map_fin_cb_t fin_callback;
  118. map_dtor_t dtor;
  119. void **user_data;
  120. struct event_base *ev_base;
  121. struct rspamd_worker *wrk;
  122. gchar *description;
  123. gchar *name;
  124. guint32 id;
  125. gboolean scheduled_check;
  126. rspamd_map_tmp_dtor tmp_dtor;
  127. gpointer tmp_dtor_data;
  128. rspamd_map_traverse_function traverse_function;
  129. gpointer lua_map;
  130. gsize nelts;
  131. guint64 digest;
  132. /* Should we check HTTP or just load cached data */
  133. struct timeval tv;
  134. gdouble poll_timeout;
  135. time_t next_check;
  136. gboolean active_http;
  137. /* Shared lock for temporary disabling of map reading (e.g. when this map is written by UI) */
  138. gint *locked;
  139. gchar tag[MEMPOOL_UID_LEN];
  140. };
  141. enum rspamd_map_http_stage {
  142. map_resolve_host2 = 0, /* 2 requests sent */
  143. map_resolve_host1, /* 1 requests sent */
  144. map_load_file,
  145. map_load_pubkey,
  146. map_load_signature,
  147. map_finished
  148. };
  149. struct map_periodic_cbdata {
  150. struct rspamd_map *map;
  151. struct map_cb_data cbdata;
  152. struct event ev;
  153. gboolean need_modify;
  154. gboolean errored;
  155. gboolean locked;
  156. guint cur_backend;
  157. ref_entry_t ref;
  158. };
  159. static const gchar rspamd_http_file_magic[] =
  160. {'r', 'm', 'c', 'd', '1', '0', '0', '0'};
  161. struct rspamd_http_file_data {
  162. guchar magic[sizeof (rspamd_http_file_magic)];
  163. goffset data_off;
  164. gulong mtime;
  165. gulong next_check;
  166. };
  167. struct http_callback_data {
  168. struct event_base *ev_base;
  169. struct rspamd_http_connection *conn;
  170. rspamd_inet_addr_t *addr;
  171. struct rspamd_map *map;
  172. struct rspamd_map_backend *bk;
  173. struct http_map_data *data;
  174. struct map_periodic_cbdata *periodic;
  175. struct rspamd_cryptobox_pubkey *pk;
  176. gboolean check;
  177. struct rspamd_storage_shmem *shmem_data;
  178. struct rspamd_storage_shmem *shmem_sig;
  179. struct rspamd_storage_shmem *shmem_pubkey;
  180. gsize data_len;
  181. gsize sig_len;
  182. gsize pubkey_len;
  183. enum rspamd_map_http_stage stage;
  184. gint fd;
  185. struct timeval tv;
  186. ref_entry_t ref;
  187. };
  188. #endif /* SRC_LIBUTIL_MAP_PRIVATE_H_ */