SPDX: license tags (mostly by guesswork)
[exim.git] / src / src / lookups / spf.c
index db2c33631f9e83484b77119f068f7f8956e48f17..a1052d7fc4c752efffac3295fc6981c6c3ed9f22 100644 (file)
@@ -2,19 +2,18 @@
 *     Exim - an Internet mail transport agent    *
 *************************************************/
 
-/*
- * Exim - SPF lookup module using libspf2
- * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- *
- * Copyright (c) 2005 Chris Webb, Arachsys Internet Services Ltd
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * Copyright (c) The Exim Maintainers 2016
- */
+/* Exim - SPF lookup module using libspf2
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Copyright (c) The Exim Maintainers 2020 - 2022
+Copyright (c) 2005 Chris Webb, Arachsys Internet Services Ltd
+SPDX-License-Identifier: GPL-2.0-or-later
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+*/
 
 #include "../exim.h"
 
@@ -32,16 +31,31 @@ static void dummy(int x) { dummy2(x-1); }
 #include <spf2/spf_dns_resolv.h>
 #include <spf2/spf_dns_cache.h>
 
+extern SPF_dns_server_t * SPF_dns_exim_new(int);
+
+
 static void *
-spf_open(uschar *filename, uschar **errmsg)
+spf_open(const uschar * filename, uschar ** errmsg)
 {
-SPF_server_t *spf_server;
-if ((spf_server = SPF_server_new(SPF_DNS_CACHE, 0)))
-  return (void *) spf_server;
-*errmsg = US"SPF_server_new() failed";
-return NULL;
+SPF_dns_server_t * dc;
+SPF_server_t *spf_server = NULL;
+int debug = 0;
+
+DEBUG(D_lookup) debug = 1;
+
+if ((dc = SPF_dns_exim_new(debug)))
+  if ((dc = SPF_dns_cache_new(dc, NULL, debug, 8)))
+    spf_server = SPF_server_new_dns(dc, debug);
+
+if (!spf_server)
+  {
+  *errmsg = US"SPF_dns_exim_nnew() failed";
+  return NULL;
+  }
+return (void *) spf_server;
 }
 
+
 static void
 spf_close(void *handle)
 {
@@ -50,8 +64,9 @@ if (spf_server) SPF_server_free(spf_server);
 }
 
 static int
-spf_find(void *handle, uschar *filename, const uschar *keystring, int key_len,
-             uschar **result, uschar **errmsg, uint *do_cache)
+spf_find(void * handle, const uschar * filename, const uschar * keystring,
+  int key_len, uschar ** result, uschar ** errmsg, uint * do_cache,
+  const uschar * opts)
 {
 SPF_server_t *spf_server = handle;
 SPF_request_t *spf_request;
@@ -96,6 +111,9 @@ if (SPF_request_set_env_from(spf_request, CS keystring))
 
 SPF_request_query_mailfrom(spf_request, &spf_response);
 *result = string_copy(US SPF_strresult(SPF_response_result(spf_response)));
+
+DEBUG(D_lookup) spf_response_debug(spf_response);
+
 SPF_response_free(spf_response);
 SPF_request_free(spf_request);
 return OK;
@@ -110,25 +128,26 @@ return OK;
 
 #include "../version.h"
 
-void
-spf_version_report(FILE *f)
+gstring *
+spf_version_report(gstring * g)
 {
 #ifdef DYNLOOKUP
-fprintf(f, "Library version: SPF: Exim version %s\n", EXIM_VERSION_STR);
+g = string_fmt_append(g, "Library version: SPF: Exim version %s\n", EXIM_VERSION_STR));
 #endif
+return g;
 }
 
 
 static lookup_info _lookup_info = {
-  US"spf",                       /* lookup name */
-  0,                             /* not absfile, not query style */
-  spf_open,                      /* open function */
-  NULL,                          /* no check function */
-  spf_find,                      /* find function */
-  spf_close,                     /* close function */
-  NULL,                          /* no tidy function */
-  NULL,                          /* no quoting function */
-  spf_version_report             /* version reporting */
+  .name = US"spf",                     /* lookup name */
+  .type = 0,                           /* not absfile, not query style */
+  .open = spf_open,                    /* open function */
+  .check = NULL,                       /* no check function */
+  .find = spf_find,                    /* find function */
+  .close = spf_close,                  /* close function */
+  .tidy = NULL,                                /* no tidy function */
+  .quote = NULL,                       /* no quoting function */
+  .version_report = spf_version_report             /* version reporting */
 };
 
 #ifdef DYNLOOKUP