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.

spf.c 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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. /***MODULE:spf
  17. * rspamd module that checks spf records of incoming email
  18. *
  19. * Allowed options:
  20. * - symbol_allow (string): symbol to insert (default: 'R_SPF_ALLOW')
  21. * - symbol_fail (string): symbol to insert (default: 'R_SPF_FAIL')
  22. * - symbol_softfail (string): symbol to insert (default: 'R_SPF_SOFTFAIL')
  23. * - symbol_na (string): symbol to insert (default: 'R_SPF_NA')
  24. * - symbol_dnsfail (string): symbol to insert (default: 'R_SPF_DNSFAIL')
  25. * - symbol_permfail (string): symbol to insert (default: 'R_SPF_PERMFAIL')
  26. * - whitelist (map): map of whitelisted networks
  27. */
  28. #include "config.h"
  29. #include "libmime/message.h"
  30. #include "libserver/spf.h"
  31. #include "libutil/hash.h"
  32. #include "libutil/map.h"
  33. #include "libutil/map_helpers.h"
  34. #include "rspamd.h"
  35. #include "libserver/mempool_vars_internal.h"
  36. #define DEFAULT_SYMBOL_FAIL "R_SPF_FAIL"
  37. #define DEFAULT_SYMBOL_SOFTFAIL "R_SPF_SOFTFAIL"
  38. #define DEFAULT_SYMBOL_NEUTRAL "R_SPF_NEUTRAL"
  39. #define DEFAULT_SYMBOL_ALLOW "R_SPF_ALLOW"
  40. #define DEFAULT_SYMBOL_DNSFAIL "R_SPF_DNSFAIL"
  41. #define DEFAULT_SYMBOL_PERMFAIL "R_SPF_PERMFAIL"
  42. #define DEFAULT_SYMBOL_NA "R_SPF_NA"
  43. #define DEFAULT_CACHE_SIZE 2048
  44. static const gchar *M = "rspamd spf plugin";
  45. struct spf_ctx {
  46. struct module_ctx ctx;
  47. const gchar *symbol_fail;
  48. const gchar *symbol_softfail;
  49. const gchar *symbol_neutral;
  50. const gchar *symbol_allow;
  51. const gchar *symbol_dnsfail;
  52. const gchar *symbol_na;
  53. const gchar *symbol_permfail;
  54. struct rspamd_radix_map_helper *whitelist_ip;
  55. rspamd_lru_hash_t *spf_hash;
  56. gboolean check_local;
  57. gboolean check_authed;
  58. };
  59. static void spf_symbol_callback (struct rspamd_task *task,
  60. struct rspamd_symcache_item *item,
  61. void *unused);
  62. /* Initialization */
  63. gint spf_module_init (struct rspamd_config *cfg, struct module_ctx **ctx);
  64. gint spf_module_config (struct rspamd_config *cfg);
  65. gint spf_module_reconfig (struct rspamd_config *cfg);
  66. module_t spf_module = {
  67. "spf",
  68. spf_module_init,
  69. spf_module_config,
  70. spf_module_reconfig,
  71. NULL,
  72. RSPAMD_MODULE_VER,
  73. (guint)-1,
  74. };
  75. static inline struct spf_ctx *
  76. spf_get_context (struct rspamd_config *cfg)
  77. {
  78. return (struct spf_ctx *)g_ptr_array_index (cfg->c_modules,
  79. spf_module.ctx_offset);
  80. }
  81. gint
  82. spf_module_init (struct rspamd_config *cfg, struct module_ctx **ctx)
  83. {
  84. struct spf_ctx *spf_module_ctx;
  85. spf_module_ctx = rspamd_mempool_alloc0 (cfg->cfg_pool,
  86. sizeof (*spf_module_ctx));
  87. *ctx = (struct module_ctx *)spf_module_ctx;
  88. rspamd_rcl_add_doc_by_path (cfg,
  89. NULL,
  90. "SPF check plugin",
  91. "spf",
  92. UCL_OBJECT,
  93. NULL,
  94. 0,
  95. NULL,
  96. 0);
  97. rspamd_rcl_add_doc_by_path (cfg,
  98. "spf",
  99. "Map of IP addresses that should be excluded from SPF checks (in addition to `local_networks`)",
  100. "whitelist",
  101. UCL_STRING,
  102. NULL,
  103. 0,
  104. NULL,
  105. 0);
  106. rspamd_rcl_add_doc_by_path (cfg,
  107. "spf",
  108. "Symbol that is added if SPF check is successful",
  109. "symbol_allow",
  110. UCL_STRING,
  111. NULL,
  112. 0,
  113. NULL,
  114. 0);
  115. rspamd_rcl_add_doc_by_path (cfg,
  116. "spf",
  117. "Symbol that is added if SPF policy is set to 'deny'",
  118. "symbol_fail",
  119. UCL_STRING,
  120. NULL,
  121. 0,
  122. NULL,
  123. 0);
  124. rspamd_rcl_add_doc_by_path (cfg,
  125. "spf",
  126. "Symbol that is added if SPF policy is set to 'undefined'",
  127. "symbol_softfail",
  128. UCL_STRING,
  129. NULL,
  130. 0,
  131. NULL,
  132. 0);
  133. rspamd_rcl_add_doc_by_path (cfg,
  134. "spf",
  135. "Symbol that is added if SPF policy is set to 'neutral'",
  136. "symbol_neutral",
  137. UCL_STRING,
  138. NULL,
  139. 0,
  140. NULL,
  141. 0);
  142. rspamd_rcl_add_doc_by_path (cfg,
  143. "spf",
  144. "Symbol that is added if SPF policy is failed due to DNS failure",
  145. "symbol_dnsfail",
  146. UCL_STRING,
  147. NULL,
  148. 0,
  149. NULL,
  150. 0);
  151. rspamd_rcl_add_doc_by_path (cfg,
  152. "spf",
  153. "Symbol that is added if no SPF policy is found",
  154. "symbol_na",
  155. UCL_STRING,
  156. NULL,
  157. 0,
  158. NULL,
  159. 0);
  160. rspamd_rcl_add_doc_by_path (cfg,
  161. "spf",
  162. "Symbol that is added if SPF policy is invalid",
  163. "symbol_permfail",
  164. UCL_STRING,
  165. NULL,
  166. 0,
  167. NULL,
  168. 0);
  169. rspamd_rcl_add_doc_by_path (cfg,
  170. "spf",
  171. "Size of SPF parsed records cache",
  172. "spf_cache_size",
  173. UCL_INT,
  174. NULL,
  175. 0,
  176. NULL,
  177. 0);
  178. return 0;
  179. }
  180. gint
  181. spf_module_config (struct rspamd_config *cfg)
  182. {
  183. const ucl_object_t *value;
  184. gint res = TRUE, cb_id;
  185. guint cache_size;
  186. struct spf_ctx *spf_module_ctx = spf_get_context (cfg);
  187. if (!rspamd_config_is_module_enabled (cfg, "spf")) {
  188. return TRUE;
  189. }
  190. spf_module_ctx->whitelist_ip = NULL;
  191. value = rspamd_config_get_module_opt (cfg, "spf", "check_local");
  192. if (value == NULL) {
  193. rspamd_config_get_module_opt (cfg, "options", "check_local");
  194. }
  195. if (value != NULL) {
  196. spf_module_ctx->check_local = ucl_obj_toboolean (value);
  197. }
  198. else {
  199. spf_module_ctx->check_local = FALSE;
  200. }
  201. value = rspamd_config_get_module_opt (cfg, "spf", "check_authed");
  202. if (value == NULL) {
  203. rspamd_config_get_module_opt (cfg, "options", "check_authed");
  204. }
  205. if (value != NULL) {
  206. spf_module_ctx->check_authed = ucl_obj_toboolean (value);
  207. }
  208. else {
  209. spf_module_ctx->check_authed = FALSE;
  210. }
  211. if ((value =
  212. rspamd_config_get_module_opt (cfg, "spf", "symbol_fail")) != NULL) {
  213. spf_module_ctx->symbol_fail = ucl_obj_tostring (value);
  214. }
  215. else {
  216. spf_module_ctx->symbol_fail = DEFAULT_SYMBOL_FAIL;
  217. }
  218. if ((value =
  219. rspamd_config_get_module_opt (cfg, "spf", "symbol_softfail")) != NULL) {
  220. spf_module_ctx->symbol_softfail = ucl_obj_tostring (value);
  221. }
  222. else {
  223. spf_module_ctx->symbol_softfail = DEFAULT_SYMBOL_SOFTFAIL;
  224. }
  225. if ((value =
  226. rspamd_config_get_module_opt (cfg, "spf", "symbol_neutral")) != NULL) {
  227. spf_module_ctx->symbol_neutral = ucl_obj_tostring (value);
  228. }
  229. else {
  230. spf_module_ctx->symbol_neutral = DEFAULT_SYMBOL_NEUTRAL;
  231. }
  232. if ((value =
  233. rspamd_config_get_module_opt (cfg, "spf", "symbol_allow")) != NULL) {
  234. spf_module_ctx->symbol_allow = ucl_obj_tostring (value);
  235. }
  236. else {
  237. spf_module_ctx->symbol_allow = DEFAULT_SYMBOL_ALLOW;
  238. }
  239. if ((value =
  240. rspamd_config_get_module_opt (cfg, "spf", "symbol_dnsfail")) != NULL) {
  241. spf_module_ctx->symbol_dnsfail = ucl_obj_tostring (value);
  242. }
  243. else {
  244. spf_module_ctx->symbol_dnsfail = DEFAULT_SYMBOL_DNSFAIL;
  245. }
  246. if ((value =
  247. rspamd_config_get_module_opt (cfg, "spf", "symbol_na")) != NULL) {
  248. spf_module_ctx->symbol_na = ucl_obj_tostring (value);
  249. }
  250. else {
  251. spf_module_ctx->symbol_na = DEFAULT_SYMBOL_NA;
  252. }
  253. if ((value =
  254. rspamd_config_get_module_opt (cfg, "spf", "symbol_permfail")) != NULL) {
  255. spf_module_ctx->symbol_permfail = ucl_obj_tostring (value);
  256. }
  257. else {
  258. spf_module_ctx->symbol_permfail = DEFAULT_SYMBOL_PERMFAIL;
  259. }
  260. if ((value =
  261. rspamd_config_get_module_opt (cfg, "spf", "spf_cache_size")) != NULL) {
  262. cache_size = ucl_obj_toint (value);
  263. }
  264. else {
  265. cache_size = DEFAULT_CACHE_SIZE;
  266. }
  267. if ((value =
  268. rspamd_config_get_module_opt (cfg, "spf", "whitelist")) != NULL) {
  269. rspamd_config_radix_from_ucl (cfg, value, "SPF whitelist",
  270. &spf_module_ctx->whitelist_ip, NULL);
  271. }
  272. cb_id = rspamd_symcache_add_symbol (cfg->cache,
  273. "SPF_CHECK",
  274. 0,
  275. spf_symbol_callback,
  276. NULL,
  277. SYMBOL_TYPE_CALLBACK | SYMBOL_TYPE_FINE | SYMBOL_TYPE_EMPTY, -1);
  278. rspamd_config_add_symbol (cfg,
  279. "SPF_CHECK",
  280. 0.0,
  281. "SPF check callback",
  282. "policies",
  283. RSPAMD_SYMBOL_FLAG_IGNORE,
  284. 1,
  285. 1);
  286. rspamd_config_add_symbol_group (cfg, "SPF_CHECK", "spf");
  287. rspamd_symcache_add_symbol (cfg->cache,
  288. spf_module_ctx->symbol_fail, 0,
  289. NULL, NULL,
  290. SYMBOL_TYPE_VIRTUAL,
  291. cb_id);
  292. rspamd_symcache_add_symbol (cfg->cache,
  293. spf_module_ctx->symbol_softfail, 0,
  294. NULL, NULL,
  295. SYMBOL_TYPE_VIRTUAL,
  296. cb_id);
  297. rspamd_symcache_add_symbol (cfg->cache,
  298. spf_module_ctx->symbol_permfail, 0,
  299. NULL, NULL,
  300. SYMBOL_TYPE_VIRTUAL,
  301. cb_id);
  302. rspamd_symcache_add_symbol (cfg->cache,
  303. spf_module_ctx->symbol_na, 0,
  304. NULL, NULL,
  305. SYMBOL_TYPE_VIRTUAL,
  306. cb_id);
  307. rspamd_symcache_add_symbol (cfg->cache,
  308. spf_module_ctx->symbol_neutral, 0,
  309. NULL, NULL,
  310. SYMBOL_TYPE_VIRTUAL,
  311. cb_id);
  312. rspamd_symcache_add_symbol (cfg->cache,
  313. spf_module_ctx->symbol_allow, 0,
  314. NULL, NULL,
  315. SYMBOL_TYPE_VIRTUAL,
  316. cb_id);
  317. rspamd_symcache_add_symbol (cfg->cache,
  318. spf_module_ctx->symbol_dnsfail, 0,
  319. NULL, NULL,
  320. SYMBOL_TYPE_VIRTUAL,
  321. cb_id);
  322. if (cache_size > 0) {
  323. spf_module_ctx->spf_hash = rspamd_lru_hash_new (
  324. cache_size,
  325. NULL,
  326. (GDestroyNotify) spf_record_unref);
  327. rspamd_mempool_add_destructor (cfg->cfg_pool,
  328. (rspamd_mempool_destruct_t)rspamd_lru_hash_destroy,
  329. spf_module_ctx->spf_hash);
  330. }
  331. rspamd_mempool_add_destructor (cfg->cfg_pool,
  332. (rspamd_mempool_destruct_t)rspamd_map_helper_destroy_radix,
  333. spf_module_ctx->whitelist_ip);
  334. msg_info_config ("init internal spf module");
  335. return res;
  336. }
  337. gint
  338. spf_module_reconfig (struct rspamd_config *cfg)
  339. {
  340. return spf_module_config (cfg);
  341. }
  342. static gboolean
  343. spf_check_element (struct spf_resolved *rec, struct spf_addr *addr,
  344. struct rspamd_task *task, gboolean cached)
  345. {
  346. gboolean res = FALSE;
  347. const guint8 *s, *d;
  348. gchar *spf_result;
  349. guint af, mask, bmask, addrlen;
  350. const gchar *spf_message, *spf_symbol;
  351. struct spf_ctx *spf_module_ctx = spf_get_context (task->cfg);
  352. if (task->from_addr == NULL) {
  353. return FALSE;
  354. }
  355. if (addr->flags & RSPAMD_SPF_FLAG_TEMPFAIL) {
  356. /* Ignore failed addresses */
  357. return FALSE;
  358. }
  359. af = rspamd_inet_address_get_af (task->from_addr);
  360. /* Basic comparing algorithm */
  361. if (((addr->flags & RSPAMD_SPF_FLAG_IPV6) && af == AF_INET6) ||
  362. ((addr->flags & RSPAMD_SPF_FLAG_IPV4) && af == AF_INET)) {
  363. d = rspamd_inet_address_get_hash_key (task->from_addr, &addrlen);
  364. if (af == AF_INET6) {
  365. s = (const guint8 *)addr->addr6;
  366. mask = addr->m.dual.mask_v6;
  367. }
  368. else {
  369. s = (const guint8 *)addr->addr4;
  370. mask = addr->m.dual.mask_v4;
  371. }
  372. /* Compare the first bytes */
  373. bmask = mask / CHAR_BIT;
  374. if (mask > addrlen * CHAR_BIT) {
  375. msg_info_task ("bad mask length: %d", mask);
  376. }
  377. else if (memcmp (s, d, bmask) == 0) {
  378. if (bmask * CHAR_BIT < mask) {
  379. /* Compare the remaining bits */
  380. s += bmask;
  381. d += bmask;
  382. mask = (0xff << (CHAR_BIT - (mask - bmask * 8))) & 0xff;
  383. if ((*s & mask) == (*d & mask)) {
  384. res = TRUE;
  385. }
  386. }
  387. else {
  388. res = TRUE;
  389. }
  390. }
  391. }
  392. else {
  393. if (addr->flags & RSPAMD_SPF_FLAG_ANY) {
  394. res = TRUE;
  395. }
  396. else {
  397. res = FALSE;
  398. }
  399. }
  400. if (res) {
  401. spf_result = rspamd_mempool_alloc (task->task_pool,
  402. strlen (addr->spf_string) + 5);
  403. switch (addr->mech) {
  404. case SPF_FAIL:
  405. spf_symbol = spf_module_ctx->symbol_fail;
  406. spf_result[0] = '-';
  407. spf_message = "(SPF): spf fail";
  408. if (addr->flags & RSPAMD_SPF_FLAG_ANY) {
  409. if (rec->perm_failed) {
  410. msg_info_task ("do not apply SPF failed policy, as we have "
  411. "some addresses unresolved");
  412. spf_symbol = spf_module_ctx->symbol_permfail;
  413. }
  414. else if (rec->temp_failed) {
  415. msg_info_task ("do not apply SPF failed policy, as we have "
  416. "some addresses unresolved");
  417. spf_symbol = spf_module_ctx->symbol_dnsfail;
  418. spf_message = "(SPF): spf DNS fail";
  419. }
  420. }
  421. break;
  422. case SPF_SOFT_FAIL:
  423. spf_symbol = spf_module_ctx->symbol_softfail;
  424. spf_message = "(SPF): spf softfail";
  425. spf_result[0] = '~';
  426. if (addr->flags & RSPAMD_SPF_FLAG_ANY) {
  427. if (rec->perm_failed) {
  428. msg_info_task ("do not apply SPF failed policy, as we have "
  429. "some addresses unresolved");
  430. spf_symbol = spf_module_ctx->symbol_permfail;
  431. }
  432. else if (rec->temp_failed) {
  433. msg_info_task ("do not apply SPF failed policy, as we have "
  434. "some addresses unresolved");
  435. spf_symbol = spf_module_ctx->symbol_dnsfail;
  436. spf_message = "(SPF): spf DNS fail";
  437. }
  438. }
  439. break;
  440. case SPF_NEUTRAL:
  441. spf_symbol = spf_module_ctx->symbol_neutral;
  442. spf_message = "(SPF): spf neutral";
  443. spf_result[0] = '?';
  444. break;
  445. default:
  446. spf_symbol = spf_module_ctx->symbol_allow;
  447. spf_message = "(SPF): spf allow";
  448. spf_result[0] = '+';
  449. break;
  450. }
  451. gint r = rspamd_strlcpy (spf_result + 1, addr->spf_string,
  452. strlen (addr->spf_string) + 1);
  453. if (cached) {
  454. rspamd_strlcpy (spf_result + r + 1, ":c", 3);
  455. }
  456. rspamd_task_insert_result (task,
  457. spf_symbol,
  458. 1,
  459. spf_result);
  460. ucl_object_insert_key (task->messages,
  461. ucl_object_fromstring (spf_message), "spf", 0,
  462. false);
  463. return TRUE;
  464. }
  465. return FALSE;
  466. }
  467. static void
  468. spf_check_list (struct spf_resolved *rec, struct rspamd_task *task, gboolean cached)
  469. {
  470. guint i;
  471. struct spf_addr *addr;
  472. struct spf_ctx *spf_module_ctx = spf_get_context (task->cfg);
  473. if (cached) {
  474. msg_info_task ("use cached record for %s (0x%xuL) in LRU cache for %d seconds, "
  475. "%d/%d elements in the cache",
  476. rec->domain,
  477. rec->digest,
  478. rec->ttl,
  479. rspamd_lru_hash_size (spf_module_ctx->spf_hash),
  480. rspamd_lru_hash_capacity (spf_module_ctx->spf_hash));
  481. }
  482. for (i = 0; i < rec->elts->len; i ++) {
  483. addr = &g_array_index (rec->elts, struct spf_addr, i);
  484. if (spf_check_element (rec, addr, task, cached)) {
  485. break;
  486. }
  487. }
  488. }
  489. static void
  490. spf_plugin_callback (struct spf_resolved *record, struct rspamd_task *task,
  491. gpointer ud)
  492. {
  493. struct spf_resolved *l = NULL;
  494. struct rspamd_symcache_item *item = (struct rspamd_symcache_item *)ud;
  495. struct spf_ctx *spf_module_ctx = spf_get_context (task->cfg);
  496. if (record && record->na) {
  497. rspamd_task_insert_result (task,
  498. spf_module_ctx->symbol_na,
  499. 1,
  500. NULL);
  501. }
  502. else if (record && record->elts->len == 0 && record->temp_failed) {
  503. rspamd_task_insert_result (task,
  504. spf_module_ctx->symbol_dnsfail,
  505. 1,
  506. NULL);
  507. }
  508. else if (record && record->elts->len == 0 && record->perm_failed) {
  509. rspamd_task_insert_result (task,
  510. spf_module_ctx->symbol_permfail,
  511. 1,
  512. NULL);
  513. }
  514. else if (record && record->elts->len == 0) {
  515. rspamd_task_insert_result (task,
  516. spf_module_ctx->symbol_permfail,
  517. 1,
  518. NULL);
  519. }
  520. else if (record && record->domain) {
  521. spf_record_ref (record);
  522. if (!spf_module_ctx->spf_hash ||
  523. (l = rspamd_lru_hash_lookup (spf_module_ctx->spf_hash,
  524. record->domain, task->task_timestamp)) == NULL) {
  525. l = record;
  526. if (record->ttl > 0 &&
  527. !record->temp_failed &&
  528. !record->perm_failed &&
  529. !record->na) {
  530. if (spf_module_ctx->spf_hash) {
  531. rspamd_lru_hash_insert (spf_module_ctx->spf_hash,
  532. record->domain, spf_record_ref (l),
  533. task->task_timestamp, record->ttl);
  534. msg_info_task ("stored record for %s (0x%xuL) in LRU cache for %d seconds, "
  535. "%d/%d elements in the cache",
  536. record->domain,
  537. record->digest,
  538. record->ttl,
  539. rspamd_lru_hash_size (spf_module_ctx->spf_hash),
  540. rspamd_lru_hash_capacity (spf_module_ctx->spf_hash));
  541. }
  542. }
  543. }
  544. spf_record_ref (l);
  545. spf_check_list (l, task, FALSE);
  546. spf_record_unref (l);
  547. spf_record_unref (record);
  548. }
  549. rspamd_symcache_item_async_dec_check (task, item, M);
  550. }
  551. static void
  552. spf_symbol_callback (struct rspamd_task *task,
  553. struct rspamd_symcache_item *item,
  554. void *unused)
  555. {
  556. const gchar *domain;
  557. struct spf_resolved *l;
  558. gint *dmarc_checks;
  559. struct spf_ctx *spf_module_ctx = spf_get_context (task->cfg);
  560. /* Allow dmarc */
  561. dmarc_checks = rspamd_mempool_get_variable (task->task_pool,
  562. RSPAMD_MEMPOOL_DMARC_CHECKS);
  563. if (dmarc_checks) {
  564. (*dmarc_checks) ++;
  565. }
  566. else {
  567. dmarc_checks = rspamd_mempool_alloc (task->task_pool,
  568. sizeof (*dmarc_checks));
  569. *dmarc_checks = 1;
  570. rspamd_mempool_set_variable (task->task_pool,
  571. RSPAMD_MEMPOOL_DMARC_CHECKS,
  572. dmarc_checks, NULL);
  573. }
  574. if (rspamd_match_radix_map_addr (spf_module_ctx->whitelist_ip,
  575. task->from_addr) != NULL) {
  576. rspamd_symcache_finalize_item (task, item);
  577. return;
  578. }
  579. if ((!spf_module_ctx->check_authed && task->user != NULL)
  580. || (!spf_module_ctx->check_local &&
  581. rspamd_inet_address_is_local (task->from_addr, TRUE))) {
  582. msg_info_task ("skip SPF checks for local networks and authorized users");
  583. rspamd_symcache_finalize_item (task, item);
  584. return;
  585. }
  586. domain = rspamd_spf_get_domain (task);
  587. rspamd_symcache_item_async_inc (task, item, M);
  588. if (domain) {
  589. if (spf_module_ctx->spf_hash &&
  590. (l = rspamd_lru_hash_lookup (spf_module_ctx->spf_hash, domain,
  591. task->task_timestamp)) != NULL) {
  592. spf_record_ref (l);
  593. spf_check_list (l, task, TRUE);
  594. spf_record_unref (l);
  595. }
  596. else {
  597. if (!rspamd_spf_resolve (task, spf_plugin_callback, item)) {
  598. msg_info_task ("cannot make spf request for %s", domain);
  599. rspamd_task_insert_result (task,
  600. spf_module_ctx->symbol_dnsfail,
  601. 1,
  602. "(SPF): spf DNS fail");
  603. }
  604. else {
  605. rspamd_symcache_item_async_inc (task, item, M);
  606. }
  607. }
  608. }
  609. rspamd_symcache_item_async_dec_check (task, item, M);
  610. }