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