DMARC: add ARC info to history records
[exim.git] / src / src / spf.c
index 3a1912a918573522b9608251056a107205467e87..3d83f07ba43a6d43482ce83203f4a9eaf991c0ec 100644 (file)
@@ -3,9 +3,10 @@
 *************************************************/
 
 /* SPF support.
+   Copyright (c) The Exim Maintainers 2015 - 2022
    Copyright (c) Tom Kistner <tom@duncanthrax.net> 2004 - 2014
    License: GPL
-   Copyright (c) The Exim Maintainers 2015 - 2020
+   SPDX-License-Identifier: GPL-2.0-or-later
 */
 
 /* Code for calling spf checks via libspf-alt. Called from acl.c. */
@@ -34,15 +35,17 @@ SPF_response_t  *spf_response_2mx = NULL;
 SPF_dns_rr_t  * spf_nxdomain = NULL;
 
 
-void
-spf_lib_version_report(FILE * fp)
+gstring *
+spf_lib_version_report(gstring * g)
 {
 int maj, min, patch;
+
 SPF_get_lib_version(&maj, &min, &patch);
-fprintf(fp, "Library version: spf2: Compile: %d.%d.%d\n",
+g = string_fmt_append(g, "Library version: spf2: Compile: %d.%d.%d\n",
        SPF_LIB_VERSION_MAJOR, SPF_LIB_VERSION_MINOR, SPF_LIB_VERSION_PATCH);
-fprintf(fp, "                       Runtime: %d.%d.%d\n",
+g = string_fmt_append(g,    "                       Runtime: %d.%d.%d\n",
         maj, min, patch);
+return g;
 }
 
 
@@ -80,6 +83,7 @@ if (rr_type == T_SPF)
   HDEBUG(D_host_lookup) debug_printf("faking NO_DATA for SPF RR(99) lookup\n");
   srr.herrno = NO_DATA;
   SPF_dns_rr_dup(&spfrr, &srr);
+  store_free_dns_answer(dnsa);
   return spfrr;
   }
 
@@ -100,6 +104,7 @@ for (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS); rr;
 if (found == 0)
   {
   SPF_dns_rr_dup(&spfrr, &srr);
+  store_free_dns_answer(dnsa);
   return spfrr;
   }
 
@@ -171,6 +176,7 @@ if (!(srr.num_rr = found))
 
 /* spfrr->rr must have been malloc()d for this */
 SPF_dns_rr_dup(&spfrr, &srr);
+store_free_dns_answer(dnsa);
 return spfrr;
 }
 
@@ -199,7 +205,7 @@ spf_nxdomain = SPF_dns_rr_new_init(spf_dns_server,
   "", ns_t_any, 24 * 60 * 60, HOST_NOT_FOUND);
 if (!spf_nxdomain)
   {
-  free(spf_dns_server);
+  store_free(spf_dns_server);
   return NULL;
   }
 
@@ -395,20 +401,31 @@ gstring *
 authres_spf(gstring * g)
 {
 uschar * s;
-if (!spf_result) return g;
-
-g = string_append(g, 2, US";\n\tspf=", spf_result);
-if (spf_result_guessed)
-  g = string_cat(g, US" (best guess record for domain)");
+if (spf_result)
+  {
+  int start = 0;               /* Compiler quietening */
+  DEBUG(D_acl) start = gstring_length(g);
 
-s = expand_string(US"$sender_address_domain");
-if (s && *s)
-  return string_append(g, 2, US" smtp.mailfrom=", s);
+  g = string_append(g, 2, US";\n\tspf=", spf_result);
+  if (spf_result_guessed)
+    g = string_cat(g, US" (best guess record for domain)");
 
-s = sender_helo_name;
-return s && *s
-  ? string_append(g, 2, US" smtp.helo=", s)
-  : string_cat(g, US" smtp.mailfrom=<>");
+  s = expand_string(US"$sender_address_domain");
+  if (s && *s)
+    g = string_append(g, 2, US" smtp.mailfrom=", s);
+  else
+    {
+    s = sender_helo_name;
+    g = s && *s
+      ? string_append(g, 2, US" smtp.helo=", s)
+      : string_cat(g, US" smtp.mailfrom=<>");
+    }
+  DEBUG(D_acl) debug_printf("SPF:\tauthres '%.*s'\n",
+                 gstring_length(g) - start - 3, g->s + start + 3);
+  }
+else
+  DEBUG(D_acl) debug_printf("SPF:\tno authres\n");
+return g;
 }