C99 initialisers
[exim.git] / src / src / lookups / spf.c
CommitLineData
92f1b170
TK
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
53ef3d84
JH
5/* Exim - SPF lookup module using libspf2
6 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7
8Copyright (c) 2005 Chris Webb, Arachsys Internet Services Ltd
9
10This program is free software; you can redistribute it and/or
11modify it under the terms of the GNU General Public License
12as published by the Free Software Foundation; either version 2
13of the License, or (at your option) any later version.
14
15Copyright (c) The Exim Maintainers 2020
16*/
92f1b170
TK
17
18#include "../exim.h"
19
7952eef9 20#ifndef SUPPORT_SPF
e1d15f5e
JH
21static void dummy(int x);
22static void dummy2(int x) { dummy(x-1); }
23static void dummy(int x) { dummy2(x-1); }
92f1b170
TK
24#else
25
26#include "lf_functions.h"
bda76da8
JH
27#if !defined(HAVE_NS_TYPE) && defined(NS_INADDRSZ)
28# define HAVE_NS_TYPE
56dda33c 29#endif
92f1b170
TK
30#include <spf2/spf.h>
31#include <spf2/spf_dns_resolv.h>
32#include <spf2/spf_dns_cache.h>
33
b8e97684
JH
34extern SPF_dns_server_t * SPF_dns_exim_new(int);
35
36
14b3c5bc 37static void *
d447dbd1 38spf_open(const uschar * filename, uschar ** errmsg)
14b3c5bc 39{
b8e97684
JH
40SPF_dns_server_t * dc;
41SPF_server_t *spf_server = NULL;
42int debug = 0;
43
44DEBUG(D_lookup) debug = 1;
45
46if ((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);
49
50if (!spf_server)
51 {
52 *errmsg = US"SPF_dns_exim_nnew() failed";
53 return NULL;
54 }
55return (void *) spf_server;
92f1b170
TK
56}
57
b8e97684 58
14b3c5bc
JH
59static void
60spf_close(void *handle)
61{
de2e5b3d
JH
62SPF_server_t *spf_server = handle;
63if (spf_server) SPF_server_free(spf_server);
92f1b170
TK
64}
65
14b3c5bc 66static int
d447dbd1 67spf_find(void * handle, const uschar * filename, const uschar * keystring,
67a57a5a
JH
68 int key_len, uschar ** result, uschar ** errmsg, uint * do_cache,
69 const uschar * opts)
14b3c5bc 70{
de2e5b3d
JH
71SPF_server_t *spf_server = handle;
72SPF_request_t *spf_request;
73SPF_response_t *spf_response = NULL;
92f1b170 74
de2e5b3d
JH
75if (!(spf_request = SPF_request_new(spf_server)))
76 {
77 *errmsg = US"SPF_request_new() failed";
78 return FAIL;
92f1b170
TK
79 }
80
de2e5b3d
JH
81#if HAVE_IPV6
82switch (string_is_ip_address(filename, NULL))
30afa09e
JH
83#else
84switch (4)
85#endif
de2e5b3d
JH
86 {
87 case 4:
de2e5b3d
JH
88 if (!SPF_request_set_ipv4_str(spf_request, CS filename))
89 break;
90 *errmsg = string_sprintf("invalid IPv4 address '%s'", filename);
92f1b170 91 return FAIL;
de2e5b3d
JH
92#if HAVE_IPV6
93
94 case 6:
95 if (!SPF_request_set_ipv6_str(spf_request, CS filename))
96 break;
97 *errmsg = string_sprintf("invalid IPv6 address '%s'", filename);
98 return FAIL;
99
100 default:
101 *errmsg = string_sprintf("invalid IP address '%s'", filename);
92f1b170 102 return FAIL;
de2e5b3d 103#endif
30afa09e 104 }
de2e5b3d
JH
105
106if (SPF_request_set_env_from(spf_request, CS keystring))
107 {
108 *errmsg = string_sprintf("invalid envelope from address '%s'", keystring);
109 return FAIL;
110}
92f1b170 111
de2e5b3d
JH
112SPF_request_query_mailfrom(spf_request, &spf_response);
113*result = string_copy(US SPF_strresult(SPF_response_result(spf_response)));
53ef3d84
JH
114
115DEBUG(D_lookup) spf_response_debug(spf_response);
116
de2e5b3d
JH
117SPF_response_free(spf_response);
118SPF_request_free(spf_request);
119return OK;
92f1b170
TK
120}
121
6545de78
PP
122
123/*************************************************
124* Version reporting entry point *
125*************************************************/
126
127/* See local README for interface description. */
128
129#include "../version.h"
130
131void
132spf_version_report(FILE *f)
133{
134#ifdef DYNLOOKUP
135fprintf(f, "Library version: SPF: Exim version %s\n", EXIM_VERSION_STR);
136#endif
137}
138
139
e6d225ae 140static lookup_info _lookup_info = {
9f400174
JH
141 .name = US"spf", /* lookup name */
142 .type = 0, /* not absfile, not query style */
143 .open = spf_open, /* open function */
144 .check = NULL, /* no check function */
145 .find = spf_find, /* find function */
146 .close = spf_close, /* close function */
147 .tidy = NULL, /* no tidy function */
148 .quote = NULL, /* no quoting function */
149 .version_report = spf_version_report /* version reporting */
e6d225ae
DW
150};
151
152#ifdef DYNLOOKUP
153#define spf_lookup_module_info _lookup_module_info
154#endif
155
156static lookup_info *_lookup_list[] = { &_lookup_info };
157lookup_module_info spf_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
158
7952eef9 159#endif /* SUPPORT_SPF */