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

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