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