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.c 67KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792
  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. /*
  17. * Implementation of map files handling
  18. */
  19. #include "config.h"
  20. #include "map.h"
  21. #include "map_private.h"
  22. #include "http_connection.h"
  23. #include "http_private.h"
  24. #include "rspamd.h"
  25. #include "contrib/zstd/zstd.h"
  26. #undef MAP_DEBUG_REFS
  27. #ifdef MAP_DEBUG_REFS
  28. #define MAP_RETAIN(x, t) do { \
  29. msg_err (G_GNUC_PRETTY_FUNCTION ": " t ": retain ref %p, refcount: %d -> %d", (x), (x)->ref.refcount, (x)->ref.refcount + 1); \
  30. REF_RETAIN(x); \
  31. } while (0)
  32. #define MAP_RELEASE(x, t) do { \
  33. msg_err (G_GNUC_PRETTY_FUNCTION ": " t ": release ref %p, refcount: %d -> %d", (x), (x)->ref.refcount, (x)->ref.refcount - 1); \
  34. REF_RELEASE(x); \
  35. } while (0)
  36. #else
  37. #define MAP_RETAIN(x, t) REF_RETAIN(x)
  38. #define MAP_RELEASE(x, t) REF_RELEASE(x)
  39. #endif
  40. static void free_http_cbdata_common (struct http_callback_data *cbd,
  41. gboolean plan_new);
  42. static void free_http_cbdata_dtor (gpointer p);
  43. static void free_http_cbdata (struct http_callback_data *cbd);
  44. static void rspamd_map_periodic_callback (gint fd, short what, void *ud);
  45. static void rspamd_map_schedule_periodic (struct rspamd_map *map, gboolean locked,
  46. gboolean initial, gboolean errored);
  47. static gboolean read_map_file_chunks (struct rspamd_map *map,
  48. struct map_cb_data *cbdata,
  49. const gchar *fname,
  50. gsize len,
  51. goffset off);
  52. static gboolean rspamd_map_save_http_cached_file (struct rspamd_map *map,
  53. struct rspamd_map_backend *bk,
  54. struct http_map_data *htdata,
  55. const guchar *data,
  56. gsize len);
  57. guint rspamd_map_log_id = (guint)-1;
  58. RSPAMD_CONSTRUCTOR(rspamd_map_log_init)
  59. {
  60. rspamd_map_log_id = rspamd_logger_add_debug_module("map");
  61. }
  62. /**
  63. * Write HTTP request
  64. */
  65. static void
  66. write_http_request (struct http_callback_data *cbd)
  67. {
  68. gchar datebuf[128];
  69. struct rspamd_http_message *msg;
  70. struct rspamd_map *map;
  71. map = cbd->map;
  72. if (cbd->fd != -1) {
  73. close (cbd->fd);
  74. }
  75. cbd->fd = rspamd_inet_address_connect (cbd->addr, SOCK_STREAM, TRUE);
  76. if (cbd->fd != -1) {
  77. msg = rspamd_http_new_message (HTTP_REQUEST);
  78. if (cbd->bk->protocol == MAP_PROTO_HTTPS) {
  79. msg->flags |= RSPAMD_HTTP_FLAG_SSL;
  80. }
  81. if (cbd->check) {
  82. msg->method = HTTP_HEAD;
  83. }
  84. if (cbd->stage == map_load_file) {
  85. msg->url = rspamd_fstring_append (msg->url,
  86. cbd->data->path, strlen (cbd->data->path));
  87. if (cbd->check && cbd->stage == map_load_file) {
  88. if (cbd->data->last_modified != 0) {
  89. rspamd_http_date_format (datebuf, sizeof (datebuf),
  90. cbd->data->last_modified);
  91. rspamd_http_message_add_header (msg, "If-Modified-Since",
  92. datebuf);
  93. }
  94. if (cbd->data->etag) {
  95. rspamd_http_message_add_header_len (msg, "If-None-Match",
  96. cbd->data->etag->str, cbd->data->etag->len);
  97. }
  98. }
  99. }
  100. else if (cbd->stage == map_load_pubkey) {
  101. msg->url = rspamd_fstring_append (msg->url,
  102. cbd->data->path, strlen (cbd->data->path));
  103. msg->url = rspamd_fstring_append (msg->url, ".pub", 4);
  104. }
  105. else if (cbd->stage == map_load_signature) {
  106. msg->url = rspamd_fstring_append (msg->url,
  107. cbd->data->path, strlen (cbd->data->path));
  108. msg->url = rspamd_fstring_append (msg->url, ".sig", 4);
  109. }
  110. else {
  111. g_assert_not_reached ();
  112. }
  113. msg->url = rspamd_fstring_append (msg->url, cbd->data->rest,
  114. strlen (cbd->data->rest));
  115. if (cbd->data->userinfo) {
  116. rspamd_http_message_add_header (msg, "Authorization",
  117. cbd->data->userinfo);
  118. }
  119. MAP_RETAIN (cbd, "http_callback_data");
  120. rspamd_http_connection_write_message (cbd->conn,
  121. msg,
  122. cbd->data->host,
  123. NULL,
  124. cbd,
  125. &cbd->tv);
  126. }
  127. else {
  128. msg_err_map ("cannot connect to %s: %s", cbd->data->host,
  129. strerror (errno));
  130. cbd->periodic->errored = TRUE;
  131. }
  132. }
  133. static gboolean
  134. rspamd_map_check_sig_pk_mem (const guchar *sig,
  135. gsize siglen,
  136. struct rspamd_map *map,
  137. const guchar *input,
  138. gsize inlen,
  139. struct rspamd_cryptobox_pubkey *pk)
  140. {
  141. GString *b32_key;
  142. gboolean ret = TRUE;
  143. if (siglen != rspamd_cryptobox_signature_bytes (RSPAMD_CRYPTOBOX_MODE_25519)) {
  144. msg_err_map ("can't open signature for %s: invalid size: %z", map->name, siglen);
  145. ret = FALSE;
  146. }
  147. if (ret && !rspamd_cryptobox_verify (sig, siglen, input, inlen,
  148. rspamd_pubkey_get_pk (pk, NULL), RSPAMD_CRYPTOBOX_MODE_25519)) {
  149. msg_err_map ("can't verify signature for %s: incorrect signature", map->name);
  150. ret = FALSE;
  151. }
  152. if (ret) {
  153. b32_key = rspamd_pubkey_print (pk,
  154. RSPAMD_KEYPAIR_BASE32|RSPAMD_KEYPAIR_PUBKEY);
  155. msg_info_map ("verified signature for %s using trusted key %v",
  156. map->name, b32_key);
  157. g_string_free (b32_key, TRUE);
  158. }
  159. return ret;
  160. }
  161. static gboolean
  162. rspamd_map_check_file_sig (const char *fname,
  163. struct rspamd_map *map,
  164. struct rspamd_map_backend *bk,
  165. const guchar *input,
  166. gsize inlen)
  167. {
  168. guchar *data;
  169. struct rspamd_cryptobox_pubkey *pk = NULL;
  170. GString *b32_key;
  171. gboolean ret = TRUE;
  172. gsize len = 0;
  173. gchar fpath[PATH_MAX];
  174. if (bk->trusted_pubkey == NULL) {
  175. /* Try to load and check pubkey */
  176. rspamd_snprintf (fpath, sizeof (fpath), "%s.pub", fname);
  177. data = rspamd_file_xmap (fpath, PROT_READ, &len, TRUE);
  178. if (data == NULL) {
  179. msg_err_map ("can't open pubkey %s: %s", fpath, strerror (errno));
  180. return FALSE;
  181. }
  182. pk = rspamd_pubkey_from_base32 (data, len, RSPAMD_KEYPAIR_SIGN,
  183. RSPAMD_CRYPTOBOX_MODE_25519);
  184. munmap (data, len);
  185. if (pk == NULL) {
  186. msg_err_map ("can't load pubkey %s", fpath);
  187. return FALSE;
  188. }
  189. /* We just check pk against the trusted db of keys */
  190. b32_key = rspamd_pubkey_print (pk,
  191. RSPAMD_KEYPAIR_BASE32|RSPAMD_KEYPAIR_PUBKEY);
  192. g_assert (b32_key != NULL);
  193. if (g_hash_table_lookup (map->cfg->trusted_keys, b32_key->str) == NULL) {
  194. msg_err_map ("pubkey loaded from %s is untrusted: %v", fpath,
  195. b32_key);
  196. g_string_free (b32_key, TRUE);
  197. rspamd_pubkey_unref (pk);
  198. return FALSE;
  199. }
  200. g_string_free (b32_key, TRUE);
  201. }
  202. else {
  203. pk = rspamd_pubkey_ref (bk->trusted_pubkey);
  204. }
  205. rspamd_snprintf (fpath, sizeof (fpath), "%s.sig", fname);
  206. data = rspamd_shmem_xmap (fpath, PROT_READ, &len);
  207. if (data == NULL) {
  208. msg_err_map ("can't open signature %s: %s", fpath, strerror (errno));
  209. ret = FALSE;
  210. }
  211. if (ret) {
  212. ret = rspamd_map_check_sig_pk_mem (data, len, map, input, inlen, pk);
  213. munmap (data, len);
  214. }
  215. rspamd_pubkey_unref (pk);
  216. return ret;
  217. }
  218. /**
  219. * Callback for destroying HTTP callback data
  220. */
  221. static void
  222. free_http_cbdata_common (struct http_callback_data *cbd, gboolean plan_new)
  223. {
  224. struct map_periodic_cbdata *periodic = cbd->periodic;
  225. if (cbd->shmem_sig) {
  226. rspamd_http_message_shmem_unref (cbd->shmem_sig);
  227. }
  228. if (cbd->shmem_pubkey) {
  229. rspamd_http_message_shmem_unref (cbd->shmem_pubkey);
  230. }
  231. if (cbd->shmem_data) {
  232. rspamd_http_message_shmem_unref (cbd->shmem_data);
  233. }
  234. if (cbd->pk) {
  235. rspamd_pubkey_unref (cbd->pk);
  236. }
  237. if (cbd->conn) {
  238. rspamd_http_connection_unref (cbd->conn);
  239. cbd->conn = NULL;
  240. }
  241. if (cbd->fd != -1) {
  242. close (cbd->fd);
  243. }
  244. if (cbd->addr) {
  245. rspamd_inet_address_free (cbd->addr);
  246. }
  247. MAP_RELEASE (cbd->bk, "rspamd_map_backend");
  248. MAP_RELEASE (periodic, "periodic");
  249. g_free (cbd);
  250. }
  251. static void
  252. free_http_cbdata (struct http_callback_data *cbd)
  253. {
  254. cbd->map->tmp_dtor = NULL;
  255. cbd->map->tmp_dtor_data = NULL;
  256. free_http_cbdata_common (cbd, TRUE);
  257. }
  258. static void
  259. free_http_cbdata_dtor (gpointer p)
  260. {
  261. struct http_callback_data *cbd = p;
  262. struct rspamd_map *map;
  263. map = cbd->map;
  264. if (cbd->stage >= map_load_file) {
  265. REF_RELEASE (cbd);
  266. }
  267. else {
  268. /* We cannot terminate DNS requests sent */
  269. cbd->stage = map_finished;
  270. }
  271. msg_warn_map ("%s: "
  272. "connection with http server is terminated: worker is stopping",
  273. map->name);
  274. }
  275. /*
  276. * HTTP callbacks
  277. */
  278. static void
  279. http_map_error (struct rspamd_http_connection *conn,
  280. GError *err)
  281. {
  282. struct http_callback_data *cbd = conn->ud;
  283. struct rspamd_map *map;
  284. map = cbd->map;
  285. cbd->periodic->errored = TRUE;
  286. msg_err_map ("error reading %s(%s): "
  287. "connection with http server terminated incorrectly: %e",
  288. cbd->bk->uri,
  289. cbd->addr ? rspamd_inet_address_to_string_pretty (cbd->addr) : "",
  290. err);
  291. rspamd_map_periodic_callback (-1, EV_TIMEOUT, cbd->periodic);
  292. MAP_RELEASE (cbd, "http_callback_data");
  293. }
  294. static void
  295. rspamd_map_cache_cb (gint fd, short what, gpointer ud)
  296. {
  297. struct rspamd_http_map_cached_cbdata *cache_cbd = ud;
  298. struct rspamd_map *map;
  299. struct http_map_data *data;
  300. struct timeval tv;
  301. map = cache_cbd->map;
  302. data = cache_cbd->data;
  303. if (cache_cbd->gen != cache_cbd->data->gen) {
  304. /* We have another update, so this cache element is obviously expired */
  305. /*
  306. * Important!: we do not set cache availability to zero here, as there
  307. * might be fresh cache
  308. */
  309. msg_info_map ("cached data is now expired (gen mismatch %L != %L) for %s",
  310. cache_cbd->gen, cache_cbd->data->gen, map->name);
  311. MAP_RELEASE (cache_cbd->shm, "rspamd_http_map_cached_cbdata");
  312. event_del (&cache_cbd->timeout);
  313. g_free (cache_cbd);
  314. }
  315. else if (cache_cbd->data->last_checked >= cache_cbd->last_checked) {
  316. /*
  317. * We checked map but we have not found anything more recent,
  318. * reschedule cache check
  319. */
  320. cache_cbd->last_checked = cache_cbd->data->last_checked;
  321. msg_debug_map ("cached data is up to date for %s", map->name);
  322. double_to_tv (map->poll_timeout * 2, &tv);
  323. event_add (&cache_cbd->timeout, &tv);
  324. }
  325. else {
  326. data->cur_cache_cbd = NULL;
  327. g_atomic_int_set (&data->cache->available, 0);
  328. MAP_RELEASE (cache_cbd->shm, "rspamd_http_map_cached_cbdata");
  329. msg_info_map ("cached data is now expired for %s", map->name);
  330. event_del (&cache_cbd->timeout);
  331. g_free (cache_cbd);
  332. }
  333. }
  334. static gboolean
  335. rspamd_http_check_pubkey (struct http_callback_data *cbd,
  336. struct rspamd_http_message *msg)
  337. {
  338. const rspamd_ftok_t *pubkey_hdr;
  339. pubkey_hdr = rspamd_http_message_find_header (msg, "Pubkey");
  340. if (pubkey_hdr) {
  341. cbd->pk = rspamd_pubkey_from_base32 (pubkey_hdr->begin,
  342. pubkey_hdr->len,
  343. RSPAMD_KEYPAIR_SIGN, RSPAMD_CRYPTOBOX_MODE_25519);
  344. return cbd->pk != NULL;
  345. }
  346. return FALSE;
  347. }
  348. static gboolean
  349. rspamd_http_check_signature (struct rspamd_map *map,
  350. struct http_callback_data *cbd,
  351. struct rspamd_http_message *msg)
  352. {
  353. const rspamd_ftok_t *sig_hdr;
  354. guchar *in;
  355. size_t dlen;
  356. sig_hdr = rspamd_http_message_find_header (msg, "Signature");
  357. if (sig_hdr && cbd->pk) {
  358. in = rspamd_shmem_xmap (cbd->shmem_data->shm_name, PROT_READ, &dlen);
  359. if (in == NULL) {
  360. msg_err_map ("cannot read tempfile %s: %s",
  361. cbd->shmem_data->shm_name,
  362. strerror (errno));
  363. return FALSE;
  364. }
  365. if (!rspamd_map_check_sig_pk_mem (sig_hdr->begin, sig_hdr->len,
  366. map, in,
  367. cbd->data_len, cbd->pk)) {
  368. munmap (in, dlen);
  369. return FALSE;
  370. }
  371. munmap (in, dlen);
  372. return TRUE;
  373. }
  374. return FALSE;
  375. }
  376. static int
  377. http_map_finish (struct rspamd_http_connection *conn,
  378. struct rspamd_http_message *msg)
  379. {
  380. struct http_callback_data *cbd = conn->ud;
  381. struct rspamd_map *map;
  382. struct rspamd_map_backend *bk;
  383. struct http_map_data *data;
  384. struct rspamd_http_map_cached_cbdata *cache_cbd;
  385. struct timeval tv;
  386. const rspamd_ftok_t *expires_hdr, *etag_hdr;
  387. char next_check_date[128];
  388. guchar *aux_data, *in = NULL;
  389. gsize inlen = 0, dlen = 0;
  390. map = cbd->map;
  391. bk = cbd->bk;
  392. data = bk->data.hd;
  393. if (msg->code == 200) {
  394. if (cbd->check) {
  395. cbd->periodic->need_modify = TRUE;
  396. /* Reset the whole chain */
  397. cbd->periodic->cur_backend = 0;
  398. /* Reset cache, old cached data will be cleaned on timeout */
  399. g_atomic_int_set (&data->cache->available, 0);
  400. data->cur_cache_cbd = NULL;
  401. rspamd_map_periodic_callback (-1, EV_TIMEOUT, cbd->periodic);
  402. MAP_RELEASE (cbd, "http_callback_data");
  403. return 0;
  404. }
  405. if (cbd->stage == map_load_file) {
  406. cbd->data->last_checked = msg->date;
  407. if (msg->last_modified) {
  408. cbd->data->last_modified = msg->last_modified;
  409. }
  410. else {
  411. cbd->data->last_modified = msg->date;
  412. }
  413. /* Maybe we need to check signature ? */
  414. if (bk->is_signed) {
  415. cbd->shmem_data = rspamd_http_message_shmem_ref (msg);
  416. cbd->data_len = msg->body_buf.len;
  417. if (bk->trusted_pubkey) {
  418. /* No need to load key */
  419. cbd->pk = rspamd_pubkey_ref (bk->trusted_pubkey);
  420. cbd->stage = map_load_signature;
  421. }
  422. else {
  423. if (!rspamd_http_check_pubkey (cbd, msg)) {
  424. cbd->stage = map_load_pubkey;
  425. }
  426. else {
  427. cbd->stage = map_load_signature;
  428. }
  429. }
  430. if (cbd->stage == map_load_signature) {
  431. /* Try HTTP header */
  432. if (rspamd_http_check_signature (map, cbd, msg)) {
  433. goto read_data;
  434. }
  435. }
  436. rspamd_http_connection_reset (cbd->conn);
  437. write_http_request (cbd);
  438. MAP_RELEASE (cbd, "http_callback_data");
  439. return 0;
  440. }
  441. else {
  442. /* Unsigned version - just open file */
  443. cbd->shmem_data = rspamd_http_message_shmem_ref (msg);
  444. cbd->data_len = msg->body_buf.len;
  445. goto read_data;
  446. }
  447. }
  448. else if (cbd->stage == map_load_pubkey) {
  449. /* We now can load pubkey */
  450. cbd->shmem_pubkey = rspamd_http_message_shmem_ref (msg);
  451. cbd->pubkey_len = msg->body_buf.len;
  452. aux_data = rspamd_shmem_xmap (cbd->shmem_pubkey->shm_name,
  453. PROT_READ, &inlen);
  454. if (aux_data == NULL) {
  455. msg_err_map ("cannot map pubkey file %s: %s",
  456. cbd->shmem_pubkey->shm_name, strerror (errno));
  457. goto err;
  458. }
  459. if (inlen < cbd->pubkey_len) {
  460. msg_err_map ("cannot map pubkey file %s: %s",
  461. cbd->shmem_pubkey->shm_name, strerror (errno));
  462. munmap (aux_data, inlen);
  463. goto err;
  464. }
  465. cbd->pk = rspamd_pubkey_from_base32 (aux_data, cbd->pubkey_len,
  466. RSPAMD_KEYPAIR_SIGN, RSPAMD_CRYPTOBOX_MODE_25519);
  467. munmap (aux_data, inlen);
  468. if (cbd->pk == NULL) {
  469. msg_err_map ("cannot load pubkey file %s: bad pubkey",
  470. cbd->shmem_pubkey->shm_name);
  471. goto err;
  472. }
  473. cbd->stage = map_load_signature;
  474. rspamd_http_connection_reset (cbd->conn);
  475. write_http_request (cbd);
  476. MAP_RELEASE (cbd, "http_callback_data");
  477. return 0;
  478. }
  479. else if (cbd->stage == map_load_signature) {
  480. /* We can now check signature */
  481. cbd->shmem_sig = rspamd_http_message_shmem_ref (msg);
  482. cbd->sig_len = msg->body_buf.len;
  483. aux_data = rspamd_shmem_xmap (cbd->shmem_sig->shm_name,
  484. PROT_READ, &inlen);
  485. if (aux_data == NULL) {
  486. msg_err_map ("cannot map signature file %s: %s",
  487. cbd->shmem_sig->shm_name, strerror (errno));
  488. goto err;
  489. }
  490. if (inlen < cbd->sig_len) {
  491. msg_err_map ("cannot map pubkey file %s: %s",
  492. cbd->shmem_pubkey->shm_name, strerror (errno));
  493. munmap (aux_data, inlen);
  494. goto err;
  495. }
  496. in = rspamd_shmem_xmap (cbd->shmem_data->shm_name, PROT_READ, &dlen);
  497. if (in == NULL) {
  498. msg_err_map ("cannot read tempfile %s: %s",
  499. cbd->shmem_data->shm_name,
  500. strerror (errno));
  501. munmap (aux_data, inlen);
  502. goto err;
  503. }
  504. if (!rspamd_map_check_sig_pk_mem (aux_data, cbd->sig_len, map, in,
  505. cbd->data_len, cbd->pk)) {
  506. munmap (aux_data, inlen);
  507. munmap (in, dlen);
  508. goto err;
  509. }
  510. munmap (in, dlen);
  511. }
  512. read_data:
  513. if (cbd->data_len == 0) {
  514. msg_err_map ("cannot read empty map");
  515. goto err;
  516. }
  517. g_assert (cbd->shmem_data != NULL);
  518. in = rspamd_shmem_xmap (cbd->shmem_data->shm_name, PROT_READ, &dlen);
  519. if (in == NULL) {
  520. msg_err_map ("cannot read tempfile %s: %s",
  521. cbd->shmem_data->shm_name,
  522. strerror (errno));
  523. goto err;
  524. }
  525. /* Check for expires */
  526. expires_hdr = rspamd_http_message_find_header (msg, "Expires");
  527. if (expires_hdr) {
  528. time_t hdate;
  529. hdate = rspamd_http_parse_date (expires_hdr->begin, expires_hdr->len);
  530. if (hdate != (time_t)-1 && hdate > msg->date) {
  531. if (map->next_check) {
  532. /* If we have multiple backends */
  533. hdate = MIN (map->next_check, hdate);
  534. }
  535. double cached_timeout = map->next_check - msg->date +
  536. map->poll_timeout * 2;
  537. map->next_check = hdate;
  538. double_to_tv (cached_timeout, &tv);
  539. }
  540. else {
  541. double_to_tv (map->poll_timeout * 2, &tv);
  542. }
  543. }
  544. else {
  545. double_to_tv (map->poll_timeout * 2, &tv);
  546. }
  547. /* Check for etag */
  548. etag_hdr = rspamd_http_message_find_header (msg, "ETag");
  549. if (etag_hdr) {
  550. if (cbd->data->etag) {
  551. /* Remove old etag */
  552. rspamd_fstring_free (cbd->data->etag);
  553. }
  554. cbd->data->etag = rspamd_fstring_new_init (etag_hdr->begin,
  555. etag_hdr->len);
  556. }
  557. else {
  558. if (cbd->data->etag) {
  559. /* Remove and clear old etag */
  560. rspamd_fstring_free (cbd->data->etag);
  561. cbd->data->etag = NULL;
  562. }
  563. }
  564. MAP_RETAIN (cbd->shmem_data, "shmem_data");
  565. cbd->data->gen ++;
  566. /*
  567. * We know that a map is in the locked state
  568. */
  569. g_atomic_int_set (&data->cache->available, 1);
  570. /* Store cached data */
  571. rspamd_strlcpy (data->cache->shmem_name, cbd->shmem_data->shm_name,
  572. sizeof (data->cache->shmem_name));
  573. data->cache->len = cbd->data_len;
  574. data->cache->last_modified = cbd->data->last_modified;
  575. cache_cbd = g_malloc0 (sizeof (*cache_cbd));
  576. cache_cbd->shm = cbd->shmem_data;
  577. cache_cbd->map = map;
  578. cache_cbd->data = cbd->data;
  579. cache_cbd->last_checked = cbd->data->last_checked;
  580. cache_cbd->gen = cbd->data->gen;
  581. MAP_RETAIN (cache_cbd->shm, "shmem_data");
  582. event_set (&cache_cbd->timeout, -1, EV_TIMEOUT, rspamd_map_cache_cb,
  583. cache_cbd);
  584. event_base_set (cbd->ev_base, &cache_cbd->timeout);
  585. event_add (&cache_cbd->timeout, &tv);
  586. data->cur_cache_cbd = cache_cbd;
  587. if (map->next_check) {
  588. rspamd_http_date_format (next_check_date, sizeof (next_check_date),
  589. map->next_check);
  590. }
  591. else {
  592. rspamd_http_date_format (next_check_date, sizeof (next_check_date),
  593. time (NULL) + map->poll_timeout);
  594. }
  595. if (cbd->bk->is_compressed) {
  596. ZSTD_DStream *zstream;
  597. ZSTD_inBuffer zin;
  598. ZSTD_outBuffer zout;
  599. guchar *out;
  600. gsize outlen, r;
  601. zstream = ZSTD_createDStream ();
  602. ZSTD_initDStream (zstream);
  603. zin.pos = 0;
  604. zin.src = in;
  605. zin.size = dlen;
  606. if ((outlen = ZSTD_getDecompressedSize (zin.src, zin.size)) == 0) {
  607. outlen = ZSTD_DStreamOutSize ();
  608. }
  609. out = g_malloc (outlen);
  610. zout.dst = out;
  611. zout.pos = 0;
  612. zout.size = outlen;
  613. while (zin.pos < zin.size) {
  614. r = ZSTD_decompressStream (zstream, &zout, &zin);
  615. if (ZSTD_isError (r)) {
  616. msg_err_map ("%s(%s): cannot decompress data: %s",
  617. cbd->bk->uri,
  618. rspamd_inet_address_to_string_pretty (cbd->addr),
  619. ZSTD_getErrorName (r));
  620. ZSTD_freeDStream (zstream);
  621. g_free (out);
  622. MAP_RELEASE (cbd->shmem_data, "shmem_data");
  623. goto err;
  624. }
  625. if (zout.pos == zout.size) {
  626. /* We need to extend output buffer */
  627. zout.size = zout.size * 1.5 + 1.0;
  628. out = g_realloc (zout.dst, zout.size);
  629. zout.dst = out;
  630. }
  631. }
  632. ZSTD_freeDStream (zstream);
  633. msg_info_map ("%s(%s): read map data %z bytes compressed, "
  634. "%z uncompressed, next check at %s",
  635. cbd->bk->uri,
  636. rspamd_inet_address_to_string_pretty (cbd->addr),
  637. dlen, zout.pos, next_check_date);
  638. map->read_callback (out, zout.pos, &cbd->periodic->cbdata, TRUE);
  639. rspamd_map_save_http_cached_file (map, bk, cbd->data, out, zout.pos);
  640. g_free (out);
  641. }
  642. else {
  643. msg_info_map ("%s(%s): read map data %z bytes, next check at %s",
  644. cbd->bk->uri,
  645. rspamd_inet_address_to_string_pretty (cbd->addr),
  646. dlen, next_check_date);
  647. rspamd_map_save_http_cached_file (map, bk, cbd->data, in, cbd->data_len);
  648. map->read_callback (in, cbd->data_len, &cbd->periodic->cbdata, TRUE);
  649. }
  650. MAP_RELEASE (cbd->shmem_data, "shmem_data");
  651. cbd->periodic->cur_backend ++;
  652. munmap (in, dlen);
  653. rspamd_map_periodic_callback (-1, EV_TIMEOUT, cbd->periodic);
  654. }
  655. else if (msg->code == 304 && (cbd->check && cbd->stage == map_load_file)) {
  656. cbd->data->last_checked = msg->date;
  657. if (msg->last_modified) {
  658. cbd->data->last_modified = msg->last_modified;
  659. }
  660. else {
  661. cbd->data->last_modified = msg->date;
  662. }
  663. expires_hdr = rspamd_http_message_find_header (msg, "Expires");
  664. if (expires_hdr) {
  665. time_t hdate;
  666. hdate = rspamd_http_parse_date (expires_hdr->begin, expires_hdr->len);
  667. if (hdate != (time_t)-1 && hdate > msg->date) {
  668. if (map->next_check) {
  669. /* If we have multiple backends */
  670. hdate = MIN (map->next_check, hdate);
  671. }
  672. map->next_check = hdate;
  673. }
  674. }
  675. etag_hdr = rspamd_http_message_find_header (msg, "ETag");
  676. if (etag_hdr) {
  677. if (cbd->data->etag) {
  678. /* Remove old etag */
  679. rspamd_fstring_free (cbd->data->etag);
  680. cbd->data->etag = rspamd_fstring_new_init (etag_hdr->begin,
  681. etag_hdr->len);
  682. }
  683. }
  684. if (map->next_check) {
  685. rspamd_http_date_format (next_check_date, sizeof (next_check_date),
  686. map->next_check);
  687. }
  688. else {
  689. rspamd_http_date_format (next_check_date, sizeof (next_check_date),
  690. time (NULL) + map->poll_timeout);
  691. }
  692. msg_info_map ("data is not modified for server %s, next check at %s",
  693. cbd->data->host, next_check_date);
  694. cbd->periodic->cur_backend ++;
  695. rspamd_map_periodic_callback (-1, EV_TIMEOUT, cbd->periodic);
  696. }
  697. else {
  698. msg_info_map ("cannot load map %s from %s: HTTP error %d",
  699. bk->uri, cbd->data->host, msg->code);
  700. goto err;
  701. }
  702. MAP_RELEASE (cbd, "http_callback_data");
  703. return 0;
  704. err:
  705. cbd->periodic->errored = 1;
  706. rspamd_map_periodic_callback (-1, EV_TIMEOUT, cbd->periodic);
  707. MAP_RELEASE (cbd, "http_callback_data");
  708. return 0;
  709. }
  710. static gboolean
  711. read_map_file_chunks (struct rspamd_map *map, struct map_cb_data *cbdata,
  712. const gchar *fname, gsize len, goffset off)
  713. {
  714. gint fd;
  715. gssize r, avail;
  716. gsize buflen = 1024 * 1024;
  717. gchar *pos, *bytes;
  718. fd = rspamd_file_xopen (fname, O_RDONLY, 0, TRUE);
  719. if (fd == -1) {
  720. msg_err_map ("can't open map for buffered reading %s: %s",
  721. fname, strerror (errno));
  722. return FALSE;
  723. }
  724. if (lseek (fd, off, SEEK_SET) == -1) {
  725. msg_err_map ("can't seek in map to pos %d for buffered reading %s: %s",
  726. (gint)off, fname, strerror (errno));
  727. return FALSE;
  728. }
  729. buflen = MIN (len, buflen);
  730. bytes = g_malloc (buflen);
  731. avail = buflen;
  732. pos = bytes;
  733. while ((r = read (fd, pos, avail)) > 0) {
  734. gchar *end = bytes + (pos - bytes) + r;
  735. msg_info_map ("%s: read map chunk, %z bytes", fname,
  736. r);
  737. pos = map->read_callback (bytes, end - bytes, cbdata, r == len);
  738. if (pos && pos > bytes && pos < end) {
  739. guint remain = end - pos;
  740. memmove (bytes, pos, remain);
  741. pos = bytes + remain;
  742. /* Need to preserve the remain */
  743. avail = ((gssize)buflen) - remain;
  744. if (avail <= 0) {
  745. /* Try realloc, too large element */
  746. g_assert (buflen >= remain);
  747. bytes = g_realloc (bytes, buflen * 2);
  748. pos = bytes + remain; /* Adjust */
  749. avail += buflen;
  750. buflen *= 2;
  751. }
  752. }
  753. else {
  754. avail = buflen;
  755. pos = bytes;
  756. }
  757. len -= r;
  758. }
  759. if (r == -1) {
  760. msg_err_map ("can't read from map %s: %s", fname, strerror (errno));
  761. close (fd);
  762. g_free (bytes);
  763. return FALSE;
  764. }
  765. close (fd);
  766. g_free (bytes);
  767. return TRUE;
  768. }
  769. /**
  770. * Callback for reading data from file
  771. */
  772. static gboolean
  773. read_map_file (struct rspamd_map *map, struct file_map_data *data,
  774. struct rspamd_map_backend *bk, struct map_periodic_cbdata *periodic)
  775. {
  776. gchar *bytes;
  777. gsize len;
  778. struct stat st;
  779. if (map->read_callback == NULL || map->fin_callback == NULL) {
  780. msg_err_map ("%s: bad callback for reading map file",
  781. data->filename);
  782. return FALSE;
  783. }
  784. if (stat (data->filename, &st) == -1) {
  785. /* File does not exist, skipping */
  786. if (errno != ENOENT) {
  787. msg_err_map ("%s: map file is unavailable for reading: %s",
  788. data->filename, strerror (errno));
  789. return FALSE;
  790. }
  791. else {
  792. msg_info_map ("%s: map file is not found; "
  793. "it will be read automatically if created",
  794. data->filename);
  795. return TRUE;
  796. }
  797. }
  798. len = st.st_size;
  799. if (bk->is_signed) {
  800. bytes = rspamd_file_xmap (data->filename, PROT_READ, &len, TRUE);
  801. if (bytes == NULL) {
  802. msg_err_map ("can't open map %s: %s", data->filename, strerror (errno));
  803. return FALSE;
  804. }
  805. if (!rspamd_map_check_file_sig (data->filename, map, bk, bytes, len)) {
  806. munmap (bytes, len);
  807. return FALSE;
  808. }
  809. munmap (bytes, len);
  810. }
  811. if (len > 0) {
  812. if (bk->is_compressed) {
  813. bytes = rspamd_file_xmap (data->filename, PROT_READ, &len, TRUE);
  814. if (bytes == NULL) {
  815. msg_err_map ("can't open map %s: %s", data->filename, strerror (errno));
  816. return FALSE;
  817. }
  818. ZSTD_DStream *zstream;
  819. ZSTD_inBuffer zin;
  820. ZSTD_outBuffer zout;
  821. guchar *out;
  822. gsize outlen, r;
  823. zstream = ZSTD_createDStream ();
  824. ZSTD_initDStream (zstream);
  825. zin.pos = 0;
  826. zin.src = bytes;
  827. zin.size = len;
  828. if ((outlen = ZSTD_getDecompressedSize (zin.src, zin.size)) == 0) {
  829. outlen = ZSTD_DStreamOutSize ();
  830. }
  831. out = g_malloc (outlen);
  832. zout.dst = out;
  833. zout.pos = 0;
  834. zout.size = outlen;
  835. while (zin.pos < zin.size) {
  836. r = ZSTD_decompressStream (zstream, &zout, &zin);
  837. if (ZSTD_isError (r)) {
  838. msg_err_map ("%s: cannot decompress data: %s",
  839. data->filename,
  840. ZSTD_getErrorName (r));
  841. ZSTD_freeDStream (zstream);
  842. g_free (out);
  843. munmap (bytes, len);
  844. return FALSE;
  845. }
  846. if (zout.pos == zout.size) {
  847. /* We need to extend output buffer */
  848. zout.size = zout.size * 1.5 + 1.0;
  849. out = g_realloc (zout.dst, zout.size);
  850. zout.dst = out;
  851. }
  852. }
  853. ZSTD_freeDStream (zstream);
  854. msg_info_map ("%s: read map data, %z bytes compressed, "
  855. "%z uncompressed)", data->filename,
  856. len, zout.pos);
  857. map->read_callback (out, zout.pos, &periodic->cbdata, TRUE);
  858. g_free (out);
  859. munmap (bytes, len);
  860. }
  861. else {
  862. /* Perform buffered read: fail-safe */
  863. if (!read_map_file_chunks (map, &periodic->cbdata, data->filename,
  864. len, 0)) {
  865. return FALSE;
  866. }
  867. }
  868. }
  869. else {
  870. /* Empty map */
  871. map->read_callback (NULL, 0, &periodic->cbdata, TRUE);
  872. }
  873. /* Also update at the read time */
  874. memcpy (&data->st, &st, sizeof (struct stat));
  875. return TRUE;
  876. }
  877. static gboolean
  878. read_map_static (struct rspamd_map *map, struct static_map_data *data,
  879. struct rspamd_map_backend *bk, struct map_periodic_cbdata *periodic)
  880. {
  881. guchar *bytes;
  882. gsize len;
  883. if (map->read_callback == NULL || map->fin_callback == NULL) {
  884. msg_err_map ("%s: bad callback for reading map file", map->name);
  885. data->processed = TRUE;
  886. return FALSE;
  887. }
  888. bytes = data->data;
  889. len = data->len;
  890. if (len > 0) {
  891. if (bk->is_compressed) {
  892. ZSTD_DStream *zstream;
  893. ZSTD_inBuffer zin;
  894. ZSTD_outBuffer zout;
  895. guchar *out;
  896. gsize outlen, r;
  897. zstream = ZSTD_createDStream ();
  898. ZSTD_initDStream (zstream);
  899. zin.pos = 0;
  900. zin.src = bytes;
  901. zin.size = len;
  902. if ((outlen = ZSTD_getDecompressedSize (zin.src, zin.size)) == 0) {
  903. outlen = ZSTD_DStreamOutSize ();
  904. }
  905. out = g_malloc (outlen);
  906. zout.dst = out;
  907. zout.pos = 0;
  908. zout.size = outlen;
  909. while (zin.pos < zin.size) {
  910. r = ZSTD_decompressStream (zstream, &zout, &zin);
  911. if (ZSTD_isError (r)) {
  912. msg_err_map ("%s: cannot decompress data: %s",
  913. map->name,
  914. ZSTD_getErrorName (r));
  915. ZSTD_freeDStream (zstream);
  916. g_free (out);
  917. return FALSE;
  918. }
  919. if (zout.pos == zout.size) {
  920. /* We need to extend output buffer */
  921. zout.size = zout.size * 1.5 + 1.0;
  922. out = g_realloc (zout.dst, zout.size);
  923. zout.dst = out;
  924. }
  925. }
  926. ZSTD_freeDStream (zstream);
  927. msg_info_map ("%s: read map data, %z bytes compressed, "
  928. "%z uncompressed)",
  929. map->name,
  930. len, zout.pos);
  931. map->read_callback (out, zout.pos, &periodic->cbdata, TRUE);
  932. g_free (out);
  933. }
  934. else {
  935. msg_info_map ("%s: read map data, %z bytes",
  936. map->name, len);
  937. map->read_callback (bytes, len, &periodic->cbdata, TRUE);
  938. }
  939. }
  940. else {
  941. map->read_callback (NULL, 0, &periodic->cbdata, TRUE);
  942. }
  943. data->processed = TRUE;
  944. return TRUE;
  945. }
  946. static void
  947. rspamd_map_periodic_dtor (struct map_periodic_cbdata *periodic)
  948. {
  949. struct rspamd_map *map;
  950. map = periodic->map;
  951. msg_debug_map ("periodic dtor %p", periodic);
  952. event_del (&periodic->ev);
  953. if (periodic->need_modify) {
  954. /* We are done */
  955. periodic->map->fin_callback (&periodic->cbdata, periodic->map->user_data);
  956. }
  957. else {
  958. /* Not modified */
  959. }
  960. if (periodic->locked) {
  961. rspamd_map_schedule_periodic (periodic->map, FALSE, FALSE, FALSE);
  962. g_atomic_int_set (periodic->map->locked, 0);
  963. msg_debug_map ("unlocked map");
  964. }
  965. g_free (periodic);
  966. }
  967. static void
  968. rspamd_map_schedule_periodic (struct rspamd_map *map,
  969. gboolean locked, gboolean initial, gboolean errored)
  970. {
  971. const gdouble error_mult = 20.0, lock_mult = 0.1;
  972. gdouble jittered_sec;
  973. gdouble timeout;
  974. struct map_periodic_cbdata *cbd;
  975. if (map->scheduled_check || (map->wrk && map->wrk->wanna_die)) {
  976. /* Do not schedule check if some check is already scheduled */
  977. return;
  978. }
  979. if (map->next_check != 0) {
  980. timeout = map->next_check - rspamd_get_calendar_ticks ();
  981. if (timeout < map->poll_timeout) {
  982. timeout = map->poll_timeout;
  983. if (errored) {
  984. timeout = map->poll_timeout * error_mult;
  985. }
  986. else if (locked) {
  987. timeout = map->poll_timeout * lock_mult;
  988. }
  989. jittered_sec = rspamd_time_jitter (timeout, 0);
  990. }
  991. else {
  992. jittered_sec = rspamd_time_jitter (timeout, map->poll_timeout);
  993. }
  994. /* Reset till the next usage */
  995. map->next_check = 0;
  996. }
  997. else {
  998. timeout = map->poll_timeout;
  999. if (initial) {
  1000. timeout = 0.0;
  1001. } else {
  1002. if (errored) {
  1003. timeout = map->poll_timeout * error_mult;
  1004. }
  1005. else if (locked) {
  1006. timeout = map->poll_timeout * lock_mult;
  1007. }
  1008. }
  1009. jittered_sec = rspamd_time_jitter (timeout, 0);
  1010. }
  1011. cbd = g_malloc0 (sizeof (*cbd));
  1012. cbd->cbdata.state = 0;
  1013. cbd->cbdata.prev_data = *map->user_data;
  1014. cbd->cbdata.cur_data = NULL;
  1015. cbd->cbdata.map = map;
  1016. cbd->map = map;
  1017. map->scheduled_check = TRUE;
  1018. REF_INIT_RETAIN (cbd, rspamd_map_periodic_dtor);
  1019. evtimer_set (&cbd->ev, rspamd_map_periodic_callback, cbd);
  1020. event_base_set (map->ev_base, &cbd->ev);
  1021. msg_debug_map ("schedule new periodic event %p in %.2f seconds",
  1022. cbd, jittered_sec);
  1023. double_to_tv (jittered_sec, &map->tv);
  1024. evtimer_add (&cbd->ev, &map->tv);
  1025. }
  1026. static void
  1027. rspamd_map_dns_callback (struct rdns_reply *reply, void *arg)
  1028. {
  1029. struct http_callback_data *cbd = arg;
  1030. struct rspamd_map *map;
  1031. guint flags = RSPAMD_HTTP_CLIENT_SIMPLE|RSPAMD_HTTP_CLIENT_SHARED;
  1032. map = cbd->map;
  1033. if (cbd->stage == map_finished) {
  1034. MAP_RELEASE (cbd, "http_callback_data");
  1035. return;
  1036. }
  1037. if (reply->code == RDNS_RC_NOERROR) {
  1038. /*
  1039. * We just get the first address hoping that a resolver performs
  1040. * round-robin rotation well
  1041. */
  1042. if (cbd->addr == NULL) {
  1043. cbd->addr = rspamd_inet_address_from_rnds (reply->entries);
  1044. if (cbd->addr != NULL) {
  1045. rspamd_inet_address_set_port (cbd->addr, cbd->data->port);
  1046. /* Try to open a socket */
  1047. cbd->fd = rspamd_inet_address_connect (cbd->addr, SOCK_STREAM,
  1048. TRUE);
  1049. if (cbd->fd != -1) {
  1050. cbd->stage = map_load_file;
  1051. cbd->conn = rspamd_http_connection_new (NULL,
  1052. cbd->fd,
  1053. NULL,
  1054. http_map_error,
  1055. http_map_finish,
  1056. flags,
  1057. RSPAMD_HTTP_CLIENT);
  1058. write_http_request (cbd);
  1059. }
  1060. else {
  1061. rspamd_inet_address_free (cbd->addr);
  1062. cbd->addr = NULL;
  1063. }
  1064. }
  1065. }
  1066. }
  1067. else if (cbd->stage < map_load_file) {
  1068. if (cbd->stage == map_resolve_host2) {
  1069. /* We have still one request pending */
  1070. cbd->stage = map_resolve_host1;
  1071. }
  1072. else {
  1073. /* We could not resolve host, so cowardly fail here */
  1074. msg_err_map ("cannot resolve %s: %s", cbd->data->host,
  1075. rdns_strerror (reply->code));
  1076. cbd->periodic->errored = 1;
  1077. rspamd_map_periodic_callback (-1, EV_TIMEOUT, cbd->periodic);
  1078. }
  1079. }
  1080. MAP_RELEASE (cbd, "http_callback_data");
  1081. }
  1082. static gboolean
  1083. rspamd_map_read_cached (struct rspamd_map *map, struct rspamd_map_backend *bk,
  1084. struct map_periodic_cbdata *periodic, const gchar *host)
  1085. {
  1086. gsize len;
  1087. gpointer in;
  1088. struct http_map_data *data;
  1089. data = bk->data.hd;
  1090. in = rspamd_shmem_xmap (data->cache->shmem_name, PROT_READ, &len);
  1091. if (in == NULL) {
  1092. msg_err ("cannot map cache from %s: %s", data->cache->shmem_name,
  1093. strerror (errno));
  1094. return FALSE;
  1095. }
  1096. if (len < data->cache->len) {
  1097. msg_err ("cannot map cache from %s: bad length %z, %z expected",
  1098. data->cache->shmem_name,
  1099. len, data->cache->len);
  1100. munmap (in, len);
  1101. return FALSE;
  1102. }
  1103. if (bk->is_compressed) {
  1104. ZSTD_DStream *zstream;
  1105. ZSTD_inBuffer zin;
  1106. ZSTD_outBuffer zout;
  1107. guchar *out;
  1108. gsize outlen, r;
  1109. zstream = ZSTD_createDStream ();
  1110. ZSTD_initDStream (zstream);
  1111. zin.pos = 0;
  1112. zin.src = in;
  1113. zin.size = len;
  1114. if ((outlen = ZSTD_getDecompressedSize (zin.src, zin.size)) == 0) {
  1115. outlen = ZSTD_DStreamOutSize ();
  1116. }
  1117. out = g_malloc (outlen);
  1118. zout.dst = out;
  1119. zout.pos = 0;
  1120. zout.size = outlen;
  1121. while (zin.pos < zin.size) {
  1122. r = ZSTD_decompressStream (zstream, &zout, &zin);
  1123. if (ZSTD_isError (r)) {
  1124. msg_err_map ("%s: cannot decompress data: %s",
  1125. bk->uri,
  1126. ZSTD_getErrorName (r));
  1127. ZSTD_freeDStream (zstream);
  1128. g_free (out);
  1129. munmap (in, len);
  1130. return FALSE;
  1131. }
  1132. if (zout.pos == zout.size) {
  1133. /* We need to extend output buffer */
  1134. zout.size = zout.size * 1.5 + 1.0;
  1135. out = g_realloc (zout.dst, zout.size);
  1136. zout.dst = out;
  1137. }
  1138. }
  1139. ZSTD_freeDStream (zstream);
  1140. msg_info_map ("%s: read map data cached %z bytes compressed, "
  1141. "%z uncompressed", bk->uri,
  1142. len, zout.pos);
  1143. map->read_callback (out, zout.pos, &periodic->cbdata, TRUE);
  1144. g_free (out);
  1145. }
  1146. else {
  1147. msg_info_map ("%s: read map data cached %z bytes", bk->uri,
  1148. len);
  1149. map->read_callback (in, len, &periodic->cbdata, TRUE);
  1150. }
  1151. munmap (in, len);
  1152. return TRUE;
  1153. }
  1154. static gboolean
  1155. rspamd_map_has_http_cached_file (struct rspamd_map *map,
  1156. struct rspamd_map_backend *bk)
  1157. {
  1158. gchar path[PATH_MAX];
  1159. guchar digest[rspamd_cryptobox_HASHBYTES];
  1160. struct rspamd_config *cfg = map->cfg;
  1161. struct stat st;
  1162. if (cfg->maps_cache_dir == NULL || cfg->maps_cache_dir[0] == '\0') {
  1163. return FALSE;
  1164. }
  1165. rspamd_cryptobox_hash (digest, bk->uri, strlen (bk->uri), NULL, 0);
  1166. rspamd_snprintf (path, sizeof (path), "%s%c%*xs.map", cfg->maps_cache_dir,
  1167. G_DIR_SEPARATOR, 20, digest);
  1168. if (stat (path, &st) != -1 && st.st_size >
  1169. sizeof (struct rspamd_http_file_data)) {
  1170. return TRUE;
  1171. }
  1172. return FALSE;
  1173. }
  1174. static gboolean
  1175. rspamd_map_save_http_cached_file (struct rspamd_map *map,
  1176. struct rspamd_map_backend *bk,
  1177. struct http_map_data *htdata,
  1178. const guchar *data,
  1179. gsize len)
  1180. {
  1181. gchar path[PATH_MAX];
  1182. guchar digest[rspamd_cryptobox_HASHBYTES];
  1183. struct rspamd_config *cfg = map->cfg;
  1184. gint fd;
  1185. struct rspamd_http_file_data header;
  1186. if (cfg->maps_cache_dir == NULL || cfg->maps_cache_dir[0] == '\0') {
  1187. return FALSE;
  1188. }
  1189. rspamd_cryptobox_hash (digest, bk->uri, strlen (bk->uri), NULL, 0);
  1190. rspamd_snprintf (path, sizeof (path), "%s%c%*xs.map", cfg->maps_cache_dir,
  1191. G_DIR_SEPARATOR, 20, digest);
  1192. fd = rspamd_file_xopen (path, O_WRONLY | O_TRUNC | O_CREAT,
  1193. 00600, FALSE);
  1194. if (fd == -1) {
  1195. return FALSE;
  1196. }
  1197. if (!rspamd_file_lock (fd, FALSE)) {
  1198. msg_err_map ("cannot lock file %s: %s", path, strerror (errno));
  1199. close (fd);
  1200. return FALSE;
  1201. }
  1202. memcpy (header.magic, rspamd_http_file_magic, sizeof (rspamd_http_file_magic));
  1203. header.mtime = htdata->last_modified;
  1204. header.next_check = map->next_check;
  1205. header.data_off = sizeof (header);
  1206. if (write (fd, &header, sizeof (header)) != sizeof (header)) {
  1207. msg_err_map ("cannot write file %s: %s", path, strerror (errno));
  1208. rspamd_file_unlock (fd, FALSE);
  1209. close (fd);
  1210. return FALSE;
  1211. }
  1212. /* Now write the rest */
  1213. if (write (fd, data, len) != len) {
  1214. msg_err_map ("cannot write file %s: %s", path, strerror (errno));
  1215. rspamd_file_unlock (fd, FALSE);
  1216. close (fd);
  1217. return FALSE;
  1218. }
  1219. rspamd_file_unlock (fd, FALSE);
  1220. close (fd);
  1221. msg_info_map ("saved data from %s in %s, %uz bytes", bk->uri, path, len);
  1222. return TRUE;
  1223. }
  1224. static gboolean
  1225. rspamd_map_read_http_cached_file (struct rspamd_map *map,
  1226. struct rspamd_map_backend *bk,
  1227. struct http_map_data *htdata,
  1228. struct map_cb_data *cbdata)
  1229. {
  1230. gchar path[PATH_MAX];
  1231. guchar digest[rspamd_cryptobox_HASHBYTES];
  1232. struct rspamd_config *cfg = map->cfg;
  1233. gint fd;
  1234. struct stat st;
  1235. struct rspamd_http_file_data header;
  1236. if (cfg->maps_cache_dir == NULL || cfg->maps_cache_dir[0] == '\0') {
  1237. return FALSE;
  1238. }
  1239. rspamd_cryptobox_hash (digest, bk->uri, strlen (bk->uri), NULL, 0);
  1240. rspamd_snprintf (path, sizeof (path), "%s%c%*xs.map", cfg->maps_cache_dir,
  1241. G_DIR_SEPARATOR, 20, digest);
  1242. fd = rspamd_file_xopen (path, O_RDONLY, 00600, FALSE);
  1243. if (fd == -1) {
  1244. return FALSE;
  1245. }
  1246. if (!rspamd_file_lock (fd, FALSE)) {
  1247. msg_err_map ("cannot lock file %s: %s", path, strerror (errno));
  1248. close (fd);
  1249. return FALSE;
  1250. }
  1251. (void)fstat (fd, &st);
  1252. if (read (fd, &header, sizeof (header)) != sizeof (header)) {
  1253. msg_err_map ("cannot read file %s: %s", path, strerror (errno));
  1254. rspamd_file_unlock (fd, FALSE);
  1255. close (fd);
  1256. return FALSE;
  1257. }
  1258. if (memcmp (header.magic, rspamd_http_file_magic,
  1259. sizeof (rspamd_http_file_magic)) != 0) {
  1260. msg_err_map ("invalid magic in file %s: %s", path, strerror (errno));
  1261. rspamd_file_unlock (fd, FALSE);
  1262. close (fd);
  1263. return FALSE;
  1264. }
  1265. rspamd_file_unlock (fd, FALSE);
  1266. close (fd);
  1267. map->next_check = header.next_check;
  1268. htdata->last_modified = header.mtime;
  1269. /* Now read file data */
  1270. /* Perform buffered read: fail-safe */
  1271. if (!read_map_file_chunks (map, cbdata, path,
  1272. st.st_size - header.data_off, header.data_off)) {
  1273. return FALSE;
  1274. }
  1275. msg_info_map ("read cached data for %s from %s, %uz bytes", bk->uri, path,
  1276. st.st_size - header.data_off);
  1277. return TRUE;
  1278. }
  1279. /**
  1280. * Async HTTP callback
  1281. */
  1282. static void
  1283. rspamd_map_common_http_callback (struct rspamd_map *map,
  1284. struct rspamd_map_backend *bk,
  1285. struct map_periodic_cbdata *periodic,
  1286. gboolean check)
  1287. {
  1288. struct http_map_data *data;
  1289. struct http_callback_data *cbd;
  1290. guint flags = RSPAMD_HTTP_CLIENT_SIMPLE|RSPAMD_HTTP_CLIENT_SHARED;
  1291. data = bk->data.hd;
  1292. if (g_atomic_int_get (&data->cache->available) == 1) {
  1293. /* Read cached data */
  1294. if (check) {
  1295. if (data->last_modified < data->cache->last_modified) {
  1296. periodic->need_modify = TRUE;
  1297. /* Reset the whole chain */
  1298. periodic->cur_backend = 0;
  1299. rspamd_map_periodic_callback (-1, EV_TIMEOUT, periodic);
  1300. }
  1301. else {
  1302. if (map->active_http) {
  1303. /* Check even if there is a cached version */
  1304. goto check;
  1305. }
  1306. else {
  1307. /* Switch to the next backend */
  1308. periodic->cur_backend++;
  1309. rspamd_map_periodic_callback (-1, EV_TIMEOUT, periodic);
  1310. }
  1311. }
  1312. return;
  1313. }
  1314. else {
  1315. if (map->active_http &&
  1316. data->last_modified > data->cache->last_modified) {
  1317. goto check;
  1318. }
  1319. else if (rspamd_map_read_cached (map, bk, periodic, data->host)) {
  1320. /* Switch to the next backend */
  1321. periodic->cur_backend++;
  1322. data->last_modified = data->cache->last_modified;
  1323. rspamd_map_periodic_callback (-1, EV_TIMEOUT, periodic);
  1324. return;
  1325. }
  1326. }
  1327. }
  1328. else if (!map->active_http) {
  1329. /* Switch to the next backend */
  1330. periodic->cur_backend ++;
  1331. rspamd_map_periodic_callback (-1, EV_TIMEOUT, periodic);
  1332. return;
  1333. }
  1334. check:
  1335. cbd = g_malloc0 (sizeof (struct http_callback_data));
  1336. cbd->ev_base = map->ev_base;
  1337. cbd->map = map;
  1338. cbd->data = data;
  1339. cbd->fd = -1;
  1340. cbd->check = check;
  1341. cbd->periodic = periodic;
  1342. MAP_RETAIN (periodic, "periodic");
  1343. cbd->bk = bk;
  1344. MAP_RETAIN (bk, "rspamd_map_backend");
  1345. cbd->stage = map_resolve_host2;
  1346. double_to_tv (map->cfg->map_timeout, &cbd->tv);
  1347. REF_INIT_RETAIN (cbd, free_http_cbdata);
  1348. msg_debug_map ("%s map data from %s", check ? "checking" : "reading",
  1349. data->host);
  1350. /* Send both A and AAAA requests */
  1351. if (rspamd_parse_inet_address (&cbd->addr, data->host, strlen (data->host))) {
  1352. rspamd_inet_address_set_port (cbd->addr, cbd->data->port);
  1353. cbd->fd = rspamd_inet_address_connect (cbd->addr, SOCK_STREAM,
  1354. TRUE);
  1355. if (cbd->fd != -1) {
  1356. cbd->stage = map_load_file;
  1357. cbd->conn = rspamd_http_connection_new (
  1358. NULL,
  1359. cbd->fd,
  1360. NULL,
  1361. http_map_error,
  1362. http_map_finish,
  1363. flags,
  1364. RSPAMD_HTTP_CLIENT);
  1365. write_http_request (cbd);
  1366. MAP_RELEASE (cbd, "http_callback_data");
  1367. }
  1368. else {
  1369. msg_warn_map ("cannot load map: cannot connect to %s: %s",
  1370. data->host, strerror (errno));
  1371. rspamd_inet_address_free (cbd->addr);
  1372. cbd->addr = NULL;
  1373. MAP_RELEASE (cbd, "http_callback_data");
  1374. }
  1375. return;
  1376. }
  1377. else if (map->r->r) {
  1378. if (rdns_make_request_full (map->r->r, rspamd_map_dns_callback, cbd,
  1379. map->cfg->dns_timeout, map->cfg->dns_retransmits, 1,
  1380. data->host, RDNS_REQUEST_A)) {
  1381. MAP_RETAIN (cbd, "http_callback_data");
  1382. }
  1383. if (rdns_make_request_full (map->r->r, rspamd_map_dns_callback, cbd,
  1384. map->cfg->dns_timeout, map->cfg->dns_retransmits, 1,
  1385. data->host, RDNS_REQUEST_AAAA)) {
  1386. MAP_RETAIN (cbd, "http_callback_data");
  1387. }
  1388. map->tmp_dtor = free_http_cbdata_dtor;
  1389. map->tmp_dtor_data = cbd;
  1390. }
  1391. else {
  1392. msg_warn_map ("cannot load map: DNS resolver is not initialized");
  1393. cbd->periodic->errored = TRUE;
  1394. }
  1395. MAP_RELEASE (cbd, "http_callback_data");
  1396. }
  1397. static void
  1398. rspamd_map_http_check_callback (gint fd, short what, void *ud)
  1399. {
  1400. struct map_periodic_cbdata *cbd = ud;
  1401. struct rspamd_map *map;
  1402. struct rspamd_map_backend *bk;
  1403. map = cbd->map;
  1404. bk = g_ptr_array_index (cbd->map->backends, cbd->cur_backend);
  1405. rspamd_map_common_http_callback (map, bk, cbd, TRUE);
  1406. }
  1407. static void
  1408. rspamd_map_http_read_callback (gint fd, short what, void *ud)
  1409. {
  1410. struct map_periodic_cbdata *cbd = ud;
  1411. struct rspamd_map *map;
  1412. struct rspamd_map_backend *bk;
  1413. map = cbd->map;
  1414. bk = g_ptr_array_index (cbd->map->backends, cbd->cur_backend);
  1415. rspamd_map_common_http_callback (map, bk, cbd, FALSE);
  1416. }
  1417. static void
  1418. rspamd_map_file_check_callback (gint fd, short what, void *ud)
  1419. {
  1420. struct rspamd_map *map;
  1421. struct map_periodic_cbdata *periodic = ud;
  1422. struct file_map_data *data;
  1423. struct rspamd_map_backend *bk;
  1424. struct stat st;
  1425. map = periodic->map;
  1426. bk = g_ptr_array_index (map->backends, periodic->cur_backend);
  1427. data = bk->data.fd;
  1428. if (stat (data->filename, &st) != -1 &&
  1429. (st.st_mtime > data->st.st_mtime || data->st.st_mtime == -1)) {
  1430. /* File was modified since last check */
  1431. msg_info_map ("old mtime is %t, new mtime is %t for map file %s",
  1432. data->st.st_mtime, st.st_mtime, data->filename);
  1433. memcpy (&data->st, &st, sizeof (struct stat));
  1434. periodic->need_modify = TRUE;
  1435. periodic->cur_backend = 0;
  1436. rspamd_map_periodic_callback (-1, EV_TIMEOUT, periodic);
  1437. return;
  1438. }
  1439. /* Switch to the next backend */
  1440. periodic->cur_backend ++;
  1441. rspamd_map_periodic_callback (-1, EV_TIMEOUT, periodic);
  1442. }
  1443. static void
  1444. rspamd_map_static_check_callback (gint fd, short what, void *ud)
  1445. {
  1446. struct rspamd_map *map;
  1447. struct map_periodic_cbdata *periodic = ud;
  1448. struct static_map_data *data;
  1449. struct rspamd_map_backend *bk;
  1450. map = periodic->map;
  1451. bk = g_ptr_array_index (map->backends, periodic->cur_backend);
  1452. data = bk->data.sd;
  1453. if (!data->processed) {
  1454. periodic->need_modify = TRUE;
  1455. periodic->cur_backend = 0;
  1456. rspamd_map_periodic_callback (-1, EV_TIMEOUT, periodic);
  1457. return;
  1458. }
  1459. /* Switch to the next backend */
  1460. periodic->cur_backend ++;
  1461. rspamd_map_periodic_callback (-1, EV_TIMEOUT, periodic);
  1462. }
  1463. static void
  1464. rspamd_map_file_read_callback (gint fd, short what, void *ud)
  1465. {
  1466. struct rspamd_map *map;
  1467. struct map_periodic_cbdata *periodic = ud;
  1468. struct file_map_data *data;
  1469. struct rspamd_map_backend *bk;
  1470. map = periodic->map;
  1471. bk = g_ptr_array_index (map->backends, periodic->cur_backend);
  1472. data = bk->data.fd;
  1473. msg_info_map ("rereading map file %s", data->filename);
  1474. if (!read_map_file (map, data, bk, periodic)) {
  1475. periodic->errored = TRUE;
  1476. }
  1477. /* Switch to the next backend */
  1478. periodic->cur_backend ++;
  1479. rspamd_map_periodic_callback (-1, EV_TIMEOUT, periodic);
  1480. }
  1481. static void
  1482. rspamd_map_static_read_callback (gint fd, short what, void *ud)
  1483. {
  1484. struct rspamd_map *map;
  1485. struct map_periodic_cbdata *periodic = ud;
  1486. struct static_map_data *data;
  1487. struct rspamd_map_backend *bk;
  1488. map = periodic->map;
  1489. bk = g_ptr_array_index (map->backends, periodic->cur_backend);
  1490. data = bk->data.sd;
  1491. msg_info_map ("rereading static map");
  1492. if (!read_map_static (map, data, bk, periodic)) {
  1493. periodic->errored = TRUE;
  1494. }
  1495. /* Switch to the next backend */
  1496. periodic->cur_backend ++;
  1497. rspamd_map_periodic_callback (-1, EV_TIMEOUT, periodic);
  1498. }
  1499. static void
  1500. rspamd_map_periodic_callback (gint fd, short what, void *ud)
  1501. {
  1502. struct rspamd_map_backend *bk;
  1503. struct map_periodic_cbdata *cbd = ud;
  1504. struct rspamd_map *map;
  1505. map = cbd->map;
  1506. map->scheduled_check = FALSE;
  1507. if (!cbd->locked) {
  1508. if (!g_atomic_int_compare_and_exchange (cbd->map->locked, 0, 1)) {
  1509. msg_debug_map (
  1510. "don't try to reread map as it is locked by other process, "
  1511. "will reread it later");
  1512. rspamd_map_schedule_periodic (map, TRUE, FALSE, FALSE);
  1513. MAP_RELEASE (cbd, "periodic");
  1514. return;
  1515. }
  1516. else {
  1517. msg_debug_map ("locked map");
  1518. cbd->locked = TRUE;
  1519. }
  1520. }
  1521. if (cbd->errored) {
  1522. /* We should not check other backends if some backend has failed */
  1523. rspamd_map_schedule_periodic (cbd->map, FALSE, FALSE, TRUE);
  1524. if (cbd->locked) {
  1525. g_atomic_int_set (cbd->map->locked, 0);
  1526. }
  1527. msg_debug_map ("unlocked map");
  1528. MAP_RELEASE (cbd, "periodic");
  1529. return;
  1530. }
  1531. /* For each backend we need to check for modifications */
  1532. if (cbd->cur_backend >= cbd->map->backends->len) {
  1533. /* Last backend */
  1534. msg_debug_map ("finished map: %d of %d", cbd->cur_backend,
  1535. cbd->map->backends->len);
  1536. MAP_RELEASE (cbd, "periodic");
  1537. return;
  1538. }
  1539. if (!(cbd->map->wrk && cbd->map->wrk->wanna_die)) {
  1540. bk = g_ptr_array_index (cbd->map->backends, cbd->cur_backend);
  1541. g_assert (bk != NULL);
  1542. if (cbd->need_modify) {
  1543. /* Load data from the next backend */
  1544. switch (bk->protocol) {
  1545. case MAP_PROTO_HTTP:
  1546. case MAP_PROTO_HTTPS:
  1547. rspamd_map_http_read_callback (fd, what, cbd);
  1548. break;
  1549. case MAP_PROTO_FILE:
  1550. rspamd_map_file_read_callback (fd, what, cbd);
  1551. break;
  1552. case MAP_PROTO_STATIC:
  1553. rspamd_map_static_read_callback (fd, what, cbd);
  1554. break;
  1555. }
  1556. } else {
  1557. /* Check the next backend */
  1558. switch (bk->protocol) {
  1559. case MAP_PROTO_HTTP:
  1560. case MAP_PROTO_HTTPS:
  1561. rspamd_map_http_check_callback (fd, what, cbd);
  1562. break;
  1563. case MAP_PROTO_FILE:
  1564. rspamd_map_file_check_callback (fd, what, cbd);
  1565. break;
  1566. case MAP_PROTO_STATIC:
  1567. rspamd_map_static_check_callback (fd, what, cbd);
  1568. break;
  1569. }
  1570. }
  1571. }
  1572. }
  1573. /* Start watching event for all maps */
  1574. void
  1575. rspamd_map_watch (struct rspamd_config *cfg,
  1576. struct event_base *ev_base,
  1577. struct rspamd_dns_resolver *resolver,
  1578. struct rspamd_worker *worker,
  1579. gboolean active_http)
  1580. {
  1581. GList *cur = cfg->maps;
  1582. struct rspamd_map *map;
  1583. /* First of all do synced read of data */
  1584. while (cur) {
  1585. map = cur->data;
  1586. map->ev_base = ev_base;
  1587. map->r = resolver;
  1588. map->wrk = worker;
  1589. if (active_http) {
  1590. map->active_http = active_http;
  1591. }
  1592. if (!map->active_http) {
  1593. /* Check cached version more frequently as it is cheap */
  1594. if (map->poll_timeout >= cfg->map_timeout &&
  1595. cfg->map_file_watch_multiplier < 1.0) {
  1596. map->poll_timeout =
  1597. map->poll_timeout * cfg->map_file_watch_multiplier;
  1598. }
  1599. }
  1600. rspamd_map_schedule_periodic (map, FALSE, TRUE, FALSE);
  1601. cur = g_list_next (cur);
  1602. }
  1603. }
  1604. void
  1605. rspamd_map_preload (struct rspamd_config *cfg)
  1606. {
  1607. GList *cur = cfg->maps;
  1608. struct rspamd_map *map;
  1609. struct rspamd_map_backend *bk;
  1610. guint i;
  1611. gboolean map_ok;
  1612. /* First of all do synced read of data */
  1613. while (cur) {
  1614. map = cur->data;
  1615. map_ok = TRUE;
  1616. PTR_ARRAY_FOREACH (map->backends, i, bk) {
  1617. if (!(bk->protocol == MAP_PROTO_FILE ||
  1618. bk->protocol == MAP_PROTO_STATIC)) {
  1619. if (bk->protocol == MAP_PROTO_HTTP ||
  1620. bk->protocol == MAP_PROTO_HTTPS) {
  1621. if (!rspamd_map_has_http_cached_file (map, bk)) {
  1622. if (!map->fallback_backend) {
  1623. map_ok = FALSE;
  1624. }
  1625. break;
  1626. }
  1627. else {
  1628. continue; /* We are yet fine */
  1629. }
  1630. }
  1631. map_ok = FALSE;
  1632. break;
  1633. }
  1634. }
  1635. if (map_ok) {
  1636. struct map_periodic_cbdata fake_cbd;
  1637. gboolean succeed = TRUE;
  1638. memset (&fake_cbd, 0, sizeof (fake_cbd));
  1639. fake_cbd.cbdata.state = 0;
  1640. fake_cbd.cbdata.prev_data = *map->user_data;
  1641. fake_cbd.cbdata.cur_data = NULL;
  1642. fake_cbd.cbdata.map = map;
  1643. fake_cbd.map = map;
  1644. PTR_ARRAY_FOREACH (map->backends, i, bk) {
  1645. fake_cbd.cur_backend = i;
  1646. if (bk->protocol == MAP_PROTO_FILE) {
  1647. if (!read_map_file (map, bk->data.fd, bk, &fake_cbd)) {
  1648. succeed = FALSE;
  1649. break;
  1650. }
  1651. }
  1652. else if (bk->protocol == MAP_PROTO_STATIC) {
  1653. if (!read_map_static (map, bk->data.sd, bk, &fake_cbd)) {
  1654. succeed = FALSE;
  1655. break;
  1656. }
  1657. }
  1658. else if (bk->protocol == MAP_PROTO_HTTP ||
  1659. bk->protocol == MAP_PROTO_HTTPS) {
  1660. if (!rspamd_map_read_http_cached_file (map, bk, bk->data.hd,
  1661. &fake_cbd.cbdata)) {
  1662. if (map->fallback_backend) {
  1663. /* Try fallback */
  1664. g_assert (map->fallback_backend->protocol ==
  1665. MAP_PROTO_FILE);
  1666. if (!read_map_file (map,
  1667. map->fallback_backend->data.fd,
  1668. map->fallback_backend, &fake_cbd)) {
  1669. succeed = FALSE;
  1670. break;
  1671. }
  1672. }
  1673. else {
  1674. succeed = FALSE;
  1675. break;
  1676. }
  1677. }
  1678. }
  1679. else {
  1680. g_assert_not_reached ();
  1681. }
  1682. }
  1683. if (succeed) {
  1684. map->fin_callback (&fake_cbd.cbdata, map->user_data);
  1685. }
  1686. else {
  1687. msg_info_map ("preload of %s failed", map->name);
  1688. }
  1689. }
  1690. cur = g_list_next (cur);
  1691. }
  1692. }
  1693. void
  1694. rspamd_map_remove_all (struct rspamd_config *cfg)
  1695. {
  1696. struct rspamd_map *map;
  1697. GList *cur;
  1698. struct rspamd_map_backend *bk;
  1699. struct map_cb_data cbdata;
  1700. guint i;
  1701. for (cur = cfg->maps; cur != NULL; cur = g_list_next (cur)) {
  1702. map = cur->data;
  1703. if (map->tmp_dtor) {
  1704. map->tmp_dtor (map->tmp_dtor_data);
  1705. }
  1706. if (map->dtor) {
  1707. cbdata.prev_data = NULL;
  1708. cbdata.map = map;
  1709. cbdata.cur_data = *map->user_data;
  1710. map->dtor (&cbdata);
  1711. *map->user_data = NULL;
  1712. }
  1713. for (i = 0; i < map->backends->len; i ++) {
  1714. bk = g_ptr_array_index (map->backends, i);
  1715. MAP_RELEASE (bk, "rspamd_map_backend");
  1716. }
  1717. if (map->fallback_backend) {
  1718. MAP_RELEASE (map->fallback_backend, "rspamd_map_backend");
  1719. }
  1720. }
  1721. g_list_free (cfg->maps);
  1722. cfg->maps = NULL;
  1723. }
  1724. static const gchar *
  1725. rspamd_map_check_proto (struct rspamd_config *cfg,
  1726. const gchar *map_line, struct rspamd_map_backend *bk)
  1727. {
  1728. const gchar *pos = map_line, *end, *end_key;
  1729. g_assert (bk != NULL);
  1730. g_assert (pos != NULL);
  1731. end = pos + strlen (pos);
  1732. /* Static check */
  1733. if (g_ascii_strcasecmp (pos, "static") == 0) {
  1734. bk->protocol = MAP_PROTO_STATIC;
  1735. bk->uri = g_strdup (pos);
  1736. return pos;
  1737. }
  1738. else if (g_ascii_strcasecmp (pos, "zst+static") == 0) {
  1739. bk->protocol = MAP_PROTO_STATIC;
  1740. bk->uri = g_strdup (pos + 4);
  1741. bk->is_compressed = TRUE;
  1742. return pos + 4;
  1743. }
  1744. for (;;) {
  1745. if (g_ascii_strncasecmp (pos, "sign+", sizeof ("sign+") - 1) == 0) {
  1746. bk->is_signed = TRUE;
  1747. pos += sizeof ("sign+") - 1;
  1748. }
  1749. else if (g_ascii_strncasecmp (pos, "fallback+", sizeof ("fallback+") - 1) == 0) {
  1750. bk->is_fallback = TRUE;
  1751. pos += sizeof ("fallback+") - 1;
  1752. }
  1753. else if (g_ascii_strncasecmp (pos, "key=", sizeof ("key=") - 1) == 0) {
  1754. pos += sizeof ("key=") - 1;
  1755. end_key = memchr (pos, '+', end - pos);
  1756. if (end_key != NULL) {
  1757. bk->trusted_pubkey = rspamd_pubkey_from_base32 (pos, end_key - pos,
  1758. RSPAMD_KEYPAIR_SIGN, RSPAMD_CRYPTOBOX_MODE_25519);
  1759. if (bk->trusted_pubkey == NULL) {
  1760. msg_err_config ("cannot read pubkey from map: %s",
  1761. map_line);
  1762. return NULL;
  1763. }
  1764. pos = end_key + 1;
  1765. } else if (end - pos > 64) {
  1766. /* Try hex encoding */
  1767. bk->trusted_pubkey = rspamd_pubkey_from_hex (pos, 64,
  1768. RSPAMD_KEYPAIR_SIGN, RSPAMD_CRYPTOBOX_MODE_25519);
  1769. if (bk->trusted_pubkey == NULL) {
  1770. msg_err_config ("cannot read pubkey from map: %s",
  1771. map_line);
  1772. return NULL;
  1773. }
  1774. pos += 64;
  1775. } else {
  1776. msg_err_config ("cannot read pubkey from map: %s",
  1777. map_line);
  1778. return NULL;
  1779. }
  1780. if (*pos == '+' || *pos == ':') {
  1781. pos++;
  1782. }
  1783. }
  1784. else {
  1785. /* No known flags */
  1786. break;
  1787. }
  1788. }
  1789. bk->protocol = MAP_PROTO_FILE;
  1790. if (g_ascii_strncasecmp (pos, "http://", sizeof ("http://") - 1) == 0) {
  1791. bk->protocol = MAP_PROTO_HTTP;
  1792. /* Include http:// */
  1793. bk->uri = g_strdup (pos);
  1794. pos += sizeof ("http://") - 1;
  1795. }
  1796. else if (g_ascii_strncasecmp (pos, "https://", sizeof ("https://") - 1) == 0) {
  1797. bk->protocol = MAP_PROTO_HTTPS;
  1798. /* Include https:// */
  1799. bk->uri = g_strdup (pos);
  1800. pos += sizeof ("https://") - 1;
  1801. }
  1802. else if (g_ascii_strncasecmp (pos, "file://", sizeof ("file://") - 1) == 0) {
  1803. pos += sizeof ("file://") - 1;
  1804. /* Exclude file:// */
  1805. bk->uri = g_strdup (pos);
  1806. }
  1807. else if (*pos == '/') {
  1808. /* Trivial file case */
  1809. bk->uri = g_strdup (pos);
  1810. }
  1811. else {
  1812. msg_err_config ("invalid map fetching protocol: %s", map_line);
  1813. return NULL;
  1814. }
  1815. return pos;
  1816. }
  1817. gboolean
  1818. rspamd_map_is_map (const gchar *map_line)
  1819. {
  1820. gboolean ret = FALSE;
  1821. g_assert (map_line != NULL);
  1822. if (map_line[0] == '/') {
  1823. ret = TRUE;
  1824. }
  1825. else if (g_ascii_strncasecmp (map_line, "sign+", sizeof ("sign+") - 1) == 0) {
  1826. ret = TRUE;
  1827. }
  1828. else if (g_ascii_strncasecmp (map_line, "fallback+", sizeof ("fallback+") - 1) == 0) {
  1829. ret = TRUE;
  1830. }
  1831. else if (g_ascii_strncasecmp (map_line, "file://", sizeof ("file://") - 1) == 0) {
  1832. ret = TRUE;
  1833. }
  1834. else if (g_ascii_strncasecmp (map_line, "http://", sizeof ("http://") - 1) == 0) {
  1835. ret = TRUE;
  1836. }
  1837. else if (g_ascii_strncasecmp (map_line, "https://", sizeof ("https://") - 1) == 0) {
  1838. ret = TRUE;
  1839. }
  1840. return ret;
  1841. }
  1842. static void
  1843. rspamd_map_backend_dtor (struct rspamd_map_backend *bk)
  1844. {
  1845. g_free (bk->uri);
  1846. switch (bk->protocol) {
  1847. case MAP_PROTO_FILE:
  1848. if (bk->data.fd) {
  1849. g_free (bk->data.fd->filename);
  1850. g_free (bk->data.fd);
  1851. }
  1852. break;
  1853. case MAP_PROTO_STATIC:
  1854. if (bk->data.sd) {
  1855. if (bk->data.sd->data) {
  1856. g_free (bk->data.sd->data);
  1857. }
  1858. g_free (bk->data.sd);
  1859. }
  1860. break;
  1861. case MAP_PROTO_HTTP:
  1862. case MAP_PROTO_HTTPS:
  1863. if (bk->data.hd) {
  1864. struct http_map_data *data = bk->data.hd;
  1865. g_free (data->host);
  1866. g_free (data->path);
  1867. g_free (data->rest);
  1868. if (data->userinfo) {
  1869. g_free (data->userinfo);
  1870. }
  1871. if (data->etag) {
  1872. rspamd_fstring_free (data->etag);
  1873. }
  1874. if (g_atomic_int_compare_and_exchange (&data->cache->available, 1, 0)) {
  1875. if (data->cur_cache_cbd) {
  1876. MAP_RELEASE (data->cur_cache_cbd->shm,
  1877. "rspamd_http_map_cached_cbdata");
  1878. event_del (&data->cur_cache_cbd->timeout);
  1879. g_free (data->cur_cache_cbd);
  1880. data->cur_cache_cbd = NULL;
  1881. }
  1882. unlink (data->cache->shmem_name);
  1883. }
  1884. g_free (bk->data.hd);
  1885. }
  1886. break;
  1887. }
  1888. if (bk->trusted_pubkey) {
  1889. rspamd_pubkey_unref (bk->trusted_pubkey);
  1890. }
  1891. g_free (bk);
  1892. }
  1893. static struct rspamd_map_backend *
  1894. rspamd_map_parse_backend (struct rspamd_config *cfg, const gchar *map_line)
  1895. {
  1896. struct rspamd_map_backend *bk;
  1897. struct file_map_data *fdata = NULL;
  1898. struct http_map_data *hdata = NULL;
  1899. struct static_map_data *sdata = NULL;
  1900. struct http_parser_url up;
  1901. const gchar *end, *p;
  1902. rspamd_ftok_t tok;
  1903. bk = g_malloc0 (sizeof (*bk));
  1904. REF_INIT_RETAIN (bk, rspamd_map_backend_dtor);
  1905. if (!rspamd_map_check_proto (cfg, map_line, bk)) {
  1906. goto err;
  1907. }
  1908. if (bk->is_fallback && bk->protocol != MAP_PROTO_FILE) {
  1909. msg_err_config ("fallback backend must be file for %s", bk->uri);
  1910. goto err;
  1911. }
  1912. end = map_line + strlen (map_line);
  1913. if (end - map_line > 5) {
  1914. p = end - 5;
  1915. if (g_ascii_strcasecmp (p, ".zstd") == 0) {
  1916. bk->is_compressed = TRUE;
  1917. }
  1918. p = end - 4;
  1919. if (g_ascii_strcasecmp (p, ".zst") == 0) {
  1920. bk->is_compressed = TRUE;
  1921. }
  1922. }
  1923. /* Now check for each proto separately */
  1924. if (bk->protocol == MAP_PROTO_FILE) {
  1925. fdata = g_malloc0 (sizeof (struct file_map_data));
  1926. fdata->st.st_mtime = -1;
  1927. if (access (bk->uri, R_OK) == -1) {
  1928. if (errno != ENOENT) {
  1929. msg_err_config ("cannot open file '%s': %s", bk->uri, strerror (errno));
  1930. goto err;
  1931. }
  1932. msg_info_config (
  1933. "map '%s' is not found, but it can be loaded automatically later",
  1934. bk->uri);
  1935. }
  1936. fdata->filename = g_strdup (bk->uri);
  1937. bk->data.fd = fdata;
  1938. }
  1939. else if (bk->protocol == MAP_PROTO_HTTP || bk->protocol == MAP_PROTO_HTTPS) {
  1940. hdata = g_malloc0 (sizeof (struct http_map_data));
  1941. memset (&up, 0, sizeof (up));
  1942. if (http_parser_parse_url (bk->uri, strlen (bk->uri), FALSE,
  1943. &up) != 0) {
  1944. msg_err_config ("cannot parse HTTP url: %s", bk->uri);
  1945. goto err;
  1946. }
  1947. else {
  1948. if (!(up.field_set & 1u << UF_HOST)) {
  1949. msg_err_config ("cannot parse HTTP url: %s: no host", bk->uri);
  1950. goto err;
  1951. }
  1952. tok.begin = bk->uri + up.field_data[UF_HOST].off;
  1953. tok.len = up.field_data[UF_HOST].len;
  1954. hdata->host = rspamd_ftokdup (&tok);
  1955. if (up.field_set & (1u << UF_PORT)) {
  1956. hdata->port = up.port;
  1957. }
  1958. else {
  1959. if (bk->protocol == MAP_PROTO_HTTP) {
  1960. hdata->port = 80;
  1961. }
  1962. else {
  1963. hdata->port = 443;
  1964. }
  1965. }
  1966. if (up.field_set & (1u << UF_PATH)) {
  1967. tok.begin = bk->uri + up.field_data[UF_PATH].off;
  1968. tok.len = up.field_data[UF_PATH].len;
  1969. hdata->path = rspamd_ftokdup (&tok);
  1970. /* We also need to check query + fragment */
  1971. if (up.field_set & ((1u << UF_QUERY) | (1u << UF_FRAGMENT))) {
  1972. tok.begin = bk->uri + up.field_data[UF_PATH].off +
  1973. up.field_data[UF_PATH].len;
  1974. tok.len = strlen (tok.begin);
  1975. hdata->rest = rspamd_ftokdup (&tok);
  1976. }
  1977. else {
  1978. hdata->rest = g_strdup ("");
  1979. }
  1980. }
  1981. if (up.field_set & (1u << UF_USERINFO)) {
  1982. /* Create authorisation header for basic auth */
  1983. guint len = sizeof ("Basic ") +
  1984. up.field_data[UF_USERINFO].len * 8 / 5 + 4;
  1985. hdata->userinfo = g_malloc (len);
  1986. rspamd_snprintf (hdata->userinfo, len, "Basic %*Bs",
  1987. (int)up.field_data[UF_USERINFO].len,
  1988. bk->uri + up.field_data[UF_USERINFO].off);
  1989. }
  1990. }
  1991. hdata->cache = rspamd_mempool_alloc0_shared (cfg->cfg_pool,
  1992. sizeof (*hdata->cache));
  1993. bk->data.hd = hdata;
  1994. }
  1995. else if (bk->protocol == MAP_PROTO_STATIC) {
  1996. sdata = g_malloc0 (sizeof (*sdata));
  1997. bk->data.sd = sdata;
  1998. }
  1999. bk->id = rspamd_cryptobox_fast_hash_specific (RSPAMD_CRYPTOBOX_T1HA,
  2000. bk->uri, strlen (bk->uri), 0xdeadbabe);
  2001. return bk;
  2002. err:
  2003. MAP_RELEASE (bk, "rspamd_map_backend");
  2004. if (hdata) {
  2005. g_free (hdata);
  2006. }
  2007. return NULL;
  2008. }
  2009. static void
  2010. rspamd_map_calculate_hash (struct rspamd_map *map)
  2011. {
  2012. struct rspamd_map_backend *bk;
  2013. guint i;
  2014. rspamd_cryptobox_hash_state_t st;
  2015. gchar *cksum_encoded, cksum[rspamd_cryptobox_HASHBYTES];
  2016. rspamd_cryptobox_hash_init (&st, NULL, 0);
  2017. for (i = 0; i < map->backends->len; i ++) {
  2018. bk = g_ptr_array_index (map->backends, i);
  2019. rspamd_cryptobox_hash_update (&st, bk->uri, strlen (bk->uri));
  2020. }
  2021. rspamd_cryptobox_hash_final (&st, cksum);
  2022. cksum_encoded = rspamd_encode_base32 (cksum, sizeof (cksum));
  2023. rspamd_strlcpy (map->tag, cksum_encoded, sizeof (map->tag));
  2024. g_free (cksum_encoded);
  2025. }
  2026. static gboolean
  2027. rspamd_map_add_static_string (struct rspamd_config *cfg,
  2028. const ucl_object_t *elt,
  2029. GString *target)
  2030. {
  2031. gsize sz;
  2032. const gchar *dline;
  2033. if (ucl_object_type (elt) != UCL_STRING) {
  2034. msg_err_config ("map has static backend but `data` is "
  2035. "not string like: %s",
  2036. ucl_object_type_to_string (elt->type));
  2037. return FALSE;
  2038. }
  2039. /* Otherwise, we copy data to the backend */
  2040. dline = ucl_object_tolstring (elt, &sz);
  2041. if (sz == 0) {
  2042. msg_err_config ("map has static backend but empty no data");
  2043. return FALSE;
  2044. }
  2045. g_string_append_len (target, dline, sz);
  2046. g_string_append_c (target, '\n');
  2047. return TRUE;
  2048. }
  2049. struct rspamd_map *
  2050. rspamd_map_add (struct rspamd_config *cfg,
  2051. const gchar *map_line,
  2052. const gchar *description,
  2053. map_cb_t read_callback,
  2054. map_fin_cb_t fin_callback,
  2055. map_dtor_t dtor,
  2056. void **user_data)
  2057. {
  2058. struct rspamd_map *map;
  2059. struct rspamd_map_backend *bk;
  2060. bk = rspamd_map_parse_backend (cfg, map_line);
  2061. if (bk == NULL) {
  2062. return NULL;
  2063. }
  2064. if (bk->is_fallback) {
  2065. msg_err_config ("cannot add map with fallback only backend: %s", bk->uri);
  2066. REF_RELEASE (bk);
  2067. return NULL;
  2068. }
  2069. map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (struct rspamd_map));
  2070. map->read_callback = read_callback;
  2071. map->fin_callback = fin_callback;
  2072. map->dtor = dtor;
  2073. map->user_data = user_data;
  2074. map->cfg = cfg;
  2075. map->id = rspamd_random_uint64_fast ();
  2076. map->locked =
  2077. rspamd_mempool_alloc0_shared (cfg->cfg_pool, sizeof (gint));
  2078. map->backends = g_ptr_array_sized_new (1);
  2079. rspamd_mempool_add_destructor (cfg->cfg_pool, rspamd_ptr_array_free_hard,
  2080. map->backends);
  2081. g_ptr_array_add (map->backends, bk);
  2082. map->name = rspamd_mempool_strdup (cfg->cfg_pool, map_line);
  2083. if (bk->protocol == MAP_PROTO_FILE) {
  2084. map->poll_timeout = (cfg->map_timeout * cfg->map_file_watch_multiplier);
  2085. } else {
  2086. map->poll_timeout = cfg->map_timeout;
  2087. }
  2088. if (description != NULL) {
  2089. map->description = rspamd_mempool_strdup (cfg->cfg_pool, description);
  2090. }
  2091. rspamd_map_calculate_hash (map);
  2092. msg_info_map ("added map %s", bk->uri);
  2093. cfg->maps = g_list_prepend (cfg->maps, map);
  2094. return map;
  2095. }
  2096. static inline void
  2097. rspamd_map_add_backend (struct rspamd_map *map, struct rspamd_map_backend *bk)
  2098. {
  2099. if (bk->is_fallback) {
  2100. if (map->fallback_backend) {
  2101. msg_warn_map ("redefining fallback backend from %s to %s",
  2102. map->fallback_backend->uri, bk->uri);
  2103. }
  2104. map->fallback_backend = bk;
  2105. }
  2106. else {
  2107. g_ptr_array_add (map->backends, bk);
  2108. }
  2109. }
  2110. struct rspamd_map*
  2111. rspamd_map_add_from_ucl (struct rspamd_config *cfg,
  2112. const ucl_object_t *obj,
  2113. const gchar *description,
  2114. map_cb_t read_callback,
  2115. map_fin_cb_t fin_callback,
  2116. map_dtor_t dtor,
  2117. void **user_data)
  2118. {
  2119. ucl_object_iter_t it = NULL;
  2120. const ucl_object_t *cur, *elt;
  2121. struct rspamd_map *map;
  2122. struct rspamd_map_backend *bk;
  2123. guint i;
  2124. g_assert (obj != NULL);
  2125. if (ucl_object_type (obj) == UCL_STRING) {
  2126. /* Just a plain string */
  2127. return rspamd_map_add (cfg, ucl_object_tostring (obj), description,
  2128. read_callback, fin_callback, dtor, user_data);
  2129. }
  2130. map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (struct rspamd_map));
  2131. map->read_callback = read_callback;
  2132. map->fin_callback = fin_callback;
  2133. map->dtor = dtor;
  2134. map->user_data = user_data;
  2135. map->cfg = cfg;
  2136. map->id = rspamd_random_uint64_fast ();
  2137. map->locked =
  2138. rspamd_mempool_alloc0_shared (cfg->cfg_pool, sizeof (gint));
  2139. map->backends = g_ptr_array_new ();
  2140. rspamd_mempool_add_destructor (cfg->cfg_pool, rspamd_ptr_array_free_hard,
  2141. map->backends);
  2142. map->poll_timeout = cfg->map_timeout;
  2143. if (description) {
  2144. map->description = rspamd_mempool_strdup (cfg->cfg_pool, description);
  2145. }
  2146. if (ucl_object_type (obj) == UCL_ARRAY) {
  2147. /* Add array of maps as multiple backends */
  2148. while ((cur = ucl_object_iterate (obj, &it, true)) != NULL) {
  2149. if (ucl_object_type (cur) == UCL_STRING) {
  2150. bk = rspamd_map_parse_backend (cfg, ucl_object_tostring (cur));
  2151. if (bk != NULL) {
  2152. rspamd_map_add_backend (map, bk);
  2153. if (!map->name) {
  2154. map->name = rspamd_mempool_strdup (cfg->cfg_pool,
  2155. ucl_object_tostring (cur));
  2156. }
  2157. }
  2158. }
  2159. else {
  2160. msg_err_config ("bad map element type: %s",
  2161. ucl_object_type_to_string (ucl_object_type (cur)));
  2162. }
  2163. }
  2164. if (map->backends->len == 0) {
  2165. msg_err_config ("map has no urls to be loaded: empty list");
  2166. goto err;
  2167. }
  2168. }
  2169. else if (ucl_object_type (obj) == UCL_OBJECT) {
  2170. elt = ucl_object_lookup (obj, "name");
  2171. if (elt && ucl_object_type (elt) == UCL_STRING) {
  2172. map->name = rspamd_mempool_strdup (cfg->cfg_pool,
  2173. ucl_object_tostring (elt));
  2174. }
  2175. elt = ucl_object_lookup (obj, "description");
  2176. if (elt && ucl_object_type (elt) == UCL_STRING) {
  2177. map->description = rspamd_mempool_strdup (cfg->cfg_pool,
  2178. ucl_object_tostring (elt));
  2179. }
  2180. elt = ucl_object_lookup_any (obj, "timeout", "poll", "poll_time",
  2181. "watch_interval", NULL);
  2182. if (elt) {
  2183. map->poll_timeout = ucl_object_todouble (elt);
  2184. }
  2185. elt = ucl_object_lookup_any (obj, "upstreams", "url", "urls", NULL);
  2186. if (elt == NULL) {
  2187. msg_err_config ("map has no urls to be loaded: no elt");
  2188. goto err;
  2189. }
  2190. if (ucl_object_type (elt) == UCL_ARRAY) {
  2191. /* Add array of maps as multiple backends */
  2192. it = ucl_object_iterate_new (elt);
  2193. while ((cur = ucl_object_iterate_safe (it, true)) != NULL) {
  2194. if (ucl_object_type (cur) == UCL_STRING) {
  2195. bk = rspamd_map_parse_backend (cfg, ucl_object_tostring (cur));
  2196. if (bk != NULL) {
  2197. rspamd_map_add_backend (map, bk);
  2198. if (!map->name) {
  2199. map->name = rspamd_mempool_strdup (cfg->cfg_pool,
  2200. ucl_object_tostring (cur));
  2201. }
  2202. }
  2203. }
  2204. else {
  2205. msg_err_config ("bad map element type: %s",
  2206. ucl_object_type_to_string (ucl_object_type (cur)));
  2207. ucl_object_iterate_free (it);
  2208. goto err;
  2209. }
  2210. }
  2211. ucl_object_iterate_free (it);
  2212. if (map->backends->len == 0) {
  2213. msg_err_config ("map has no urls to be loaded: empty object list");
  2214. goto err;
  2215. }
  2216. }
  2217. else if (ucl_object_type (elt) == UCL_STRING) {
  2218. bk = rspamd_map_parse_backend (cfg, ucl_object_tostring (elt));
  2219. if (bk != NULL) {
  2220. rspamd_map_add_backend (map, bk);
  2221. if (!map->name) {
  2222. map->name = rspamd_mempool_strdup (cfg->cfg_pool,
  2223. ucl_object_tostring (elt));
  2224. }
  2225. }
  2226. }
  2227. if (!map->backends || map->backends->len == 0) {
  2228. msg_err_config ("map has no urls to be loaded: no valid backends");
  2229. goto err;
  2230. }
  2231. }
  2232. else {
  2233. msg_err_config ("map has invalid type for value: %s",
  2234. ucl_object_type_to_string (ucl_object_type (obj)));
  2235. goto err;
  2236. }
  2237. gboolean all_local = TRUE;
  2238. PTR_ARRAY_FOREACH (map->backends, i, bk) {
  2239. if (bk->protocol == MAP_PROTO_STATIC) {
  2240. GString *map_data;
  2241. /* We need data field in ucl */
  2242. elt = ucl_object_lookup (obj, "data");
  2243. if (elt == NULL) {
  2244. msg_err_config ("map has static backend but no `data` field");
  2245. goto err;
  2246. }
  2247. if (ucl_object_type (elt) == UCL_STRING) {
  2248. map_data = g_string_sized_new (32);
  2249. if (rspamd_map_add_static_string (cfg, elt, map_data)) {
  2250. bk->data.sd->data = map_data->str;
  2251. bk->data.sd->len = map_data->len;
  2252. g_string_free (map_data, FALSE);
  2253. }
  2254. else {
  2255. g_string_free (map_data, TRUE);
  2256. msg_err_config ("map has static backend with invalid `data` field");
  2257. goto err;
  2258. }
  2259. }
  2260. else if (ucl_object_type (elt) == UCL_ARRAY) {
  2261. map_data = g_string_sized_new (32);
  2262. it = ucl_object_iterate_new (elt);
  2263. while ((cur = ucl_object_iterate_safe (it, true))) {
  2264. if (!rspamd_map_add_static_string (cfg, cur, map_data)) {
  2265. g_string_free (map_data, TRUE);
  2266. msg_err_config ("map has static backend with invalid "
  2267. "`data` field");
  2268. ucl_object_iterate_free (it);
  2269. goto err;
  2270. }
  2271. }
  2272. ucl_object_iterate_free (it);
  2273. bk->data.sd->data = map_data->str;
  2274. bk->data.sd->len = map_data->len;
  2275. g_string_free (map_data, FALSE);
  2276. }
  2277. }
  2278. else if (bk->protocol != MAP_PROTO_FILE) {
  2279. all_local = FALSE;
  2280. }
  2281. }
  2282. if (all_local) {
  2283. map->poll_timeout = (map->poll_timeout *
  2284. cfg->map_file_watch_multiplier);
  2285. }
  2286. rspamd_map_calculate_hash (map);
  2287. msg_debug_map ("added map from ucl");
  2288. cfg->maps = g_list_prepend (cfg->maps, map);
  2289. return map;
  2290. err:
  2291. if (map) {
  2292. PTR_ARRAY_FOREACH (map->backends, i, bk) {
  2293. MAP_RELEASE (bk, "rspamd_map_backend");
  2294. }
  2295. }
  2296. return NULL;
  2297. }
  2298. rspamd_map_traverse_function
  2299. rspamd_map_get_traverse_function (struct rspamd_map *map)
  2300. {
  2301. if (map) {
  2302. return map->traverse_function;
  2303. }
  2304. return NULL;
  2305. }
  2306. void
  2307. rspamd_map_traverse (struct rspamd_map *map, rspamd_map_traverse_cb cb,
  2308. gpointer cbdata, gboolean reset_hits)
  2309. {
  2310. if (*map->user_data && map->traverse_function) {
  2311. map->traverse_function (*map->user_data, cb, cbdata, reset_hits);
  2312. }
  2313. }