1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Exim - SPF lookup module using libspf2
6 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 Copyright (c) The Exim Maintainers 2020 - 2022
9 Copyright (c) 2005 Chris Webb, Arachsys Internet Services Ltd
10 SPDX-License-Identifier: GPL-2.0-or-later
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License
14 as published by the Free Software Foundation; either version 2
15 of the License, or (at your option) any later version.
21 static void dummy(int x);
22 static void dummy2(int x) { dummy(x-1); }
23 static void dummy(int x) { dummy2(x-1); }
26 #include "lf_functions.h"
27 #if !defined(HAVE_NS_TYPE) && defined(NS_INADDRSZ)
31 #include <spf2/spf_dns_resolv.h>
32 #include <spf2/spf_dns_cache.h>
34 extern SPF_dns_server_t * SPF_dns_exim_new(int);
38 spf_open(const uschar * filename, uschar ** errmsg)
40 SPF_dns_server_t * dc;
41 SPF_server_t *spf_server = NULL;
44 DEBUG(D_lookup) debug = 1;
46 if ((dc = SPF_dns_exim_new(debug)))
47 if ((dc = SPF_dns_cache_new(dc, NULL, debug, 8)))
48 spf_server = SPF_server_new_dns(dc, debug);
52 *errmsg = US"SPF_dns_exim_nnew() failed";
55 return (void *) spf_server;
60 spf_close(void *handle)
62 SPF_server_t *spf_server = handle;
63 if (spf_server) SPF_server_free(spf_server);
67 spf_find(void * handle, const uschar * filename, const uschar * keystring,
68 int key_len, uschar ** result, uschar ** errmsg, uint * do_cache,
71 SPF_server_t *spf_server = handle;
72 SPF_request_t *spf_request;
73 SPF_response_t *spf_response = NULL;
75 if (!(spf_request = SPF_request_new(spf_server)))
77 *errmsg = US"SPF_request_new() failed";
82 switch (string_is_ip_address(filename, NULL))
88 if (!SPF_request_set_ipv4_str(spf_request, CS filename))
90 *errmsg = string_sprintf("invalid IPv4 address '%s'", filename);
95 if (!SPF_request_set_ipv6_str(spf_request, CS filename))
97 *errmsg = string_sprintf("invalid IPv6 address '%s'", filename);
101 *errmsg = string_sprintf("invalid IP address '%s'", filename);
106 if (SPF_request_set_env_from(spf_request, CS keystring))
108 *errmsg = string_sprintf("invalid envelope from address '%s'", keystring);
112 SPF_request_query_mailfrom(spf_request, &spf_response);
113 *result = string_copy(US SPF_strresult(SPF_response_result(spf_response)));
115 DEBUG(D_lookup) spf_response_debug(spf_response);
117 SPF_response_free(spf_response);
118 SPF_request_free(spf_request);
123 /*************************************************
124 * Version reporting entry point *
125 *************************************************/
127 /* See local README for interface description. */
129 #include "../version.h"
132 spf_version_report(gstring * g)
135 g = string_fmt_append(g, "Library version: SPF: Exim version %s\n", EXIM_VERSION_STR));
141 static lookup_info _lookup_info = {
142 .name = US"spf", /* lookup name */
143 .type = 0, /* not absfile, not query style */
144 .open = spf_open, /* open function */
145 .check = NULL, /* no check function */
146 .find = spf_find, /* find function */
147 .close = spf_close, /* close function */
148 .tidy = NULL, /* no tidy function */
149 .quote = NULL, /* no quoting function */
150 .version_report = spf_version_report /* version reporting */
154 #define spf_lookup_module_info _lookup_module_info
157 static lookup_info *_lookup_list[] = { &_lookup_info };
158 lookup_module_info spf_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
160 #endif /* SUPPORT_SPF */