]>
Commit | Line | Data |
---|---|---|
b3e93248 AM |
1 | Description: Allow one to use and switch between different local_scan functions |
2 | without recompiling exim. | |
3 | http://marc.merlins.org/linux/exim/files/sa-exim-current/ Original patch from | |
4 | David Woodhouse, modified first by Derrick 'dman' Hudson and then by Marc | |
5 | MERLIN for SA-Exim and minor/major API version tracking | |
6 | Author: David Woodhouse, Derrick 'dman' Hudson, Marc MERLIN | |
7 | Origin: other, http://marc.merlins.org/linux/exim/files/sa-exim-current/ | |
8 | Forwarded: https://bugs.exim.org/show_bug.cgi?id=2671 | |
661899b6 | 9 | Last-Update: 2022-12-19 |
b3e93248 AM |
10 | |
11 | --- a/src/EDITME | |
12 | +++ b/src/EDITME | |
661899b6 | 13 | @@ -874,10 +874,25 @@ HEADERS_CHARSET="ISO-8859-1" |
b3e93248 AM |
14 | # as the traditional crypt() function. |
15 | # *** WARNING *** WARNING *** WARNING *** WARNING *** WARNING *** | |
4d241ba7 AM |
16 | |
17 | ||
18 | #------------------------------------------------------------------------------ | |
19 | +# On systems which support dynamic loading of shared libraries, Exim can | |
20 | +# load a local_scan function specified in its config file instead of having | |
96e93d3d | 21 | +# to be recompiled with the desired local_scan function. For a full |
4d241ba7 AM |
22 | +# description of the API to this function, see the Exim specification. |
23 | + | |
24 | +DLOPEN_LOCAL_SCAN=yes | |
25 | + | |
26 | +# If you set DLOPEN_LOCAL_SCAN, then you need to include -rdynamic in the | |
27 | +# linker flags. Without it, the loaded .so won't be able to access any | |
28 | +# functions from exim. | |
29 | + | |
96e93d3d AM |
30 | +LDFLAGS += -rdynamic |
31 | +CFLAGS += -fvisibility=hidden | |
4d241ba7 AM |
32 | + |
33 | +#------------------------------------------------------------------------------ | |
34 | # The default distribution of Exim contains only the plain text form of the | |
35 | # documentation. Other forms are available separately. If you want to install | |
36 | # the documentation in "info" format, first fetch the Texinfo documentation | |
b3e93248 AM |
37 | # sources from the ftp directory and unpack them, which should create files |
38 | # with the extension "texinfo" in the doc directory. You may find that the | |
39 | --- a/src/config.h.defaults | |
40 | +++ b/src/config.h.defaults | |
661899b6 | 41 | @@ -31,10 +31,12 @@ Do not put spaces between # and the 'def |
b3e93248 AM |
42 | #define AUTH_SPA |
43 | #define AUTH_TLS | |
44 | ||
45 | #define AUTH_VARS 4 | |
46 | ||
47 | +#define DLOPEN_LOCAL_SCAN | |
48 | + | |
49 | #define BIN_DIRECTORY | |
50 | ||
51 | #define CONFIGURE_FILE | |
52 | #define CONFIGURE_FILE_USE_EUID | |
53 | #define CONFIGURE_FILE_USE_NODE | |
54 | --- a/src/globals.c | |
55 | +++ b/src/globals.c | |
661899b6 | 56 | @@ -115,10 +115,14 @@ tls_support tls_out = { |
b3e93248 AM |
57 | uschar *dsn_envid = NULL; |
58 | int dsn_ret = 0; | |
bb1028e5 | 59 | const pcre2_code *regex_DSN = NULL; |
96e93d3d | 60 | uschar *dsn_advertise_hosts = NULL; |
4d241ba7 AM |
61 | |
62 | +#ifdef DLOPEN_LOCAL_SCAN | |
63 | +uschar *local_scan_path = NULL; | |
64 | +#endif | |
96e93d3d AM |
65 | + |
66 | #ifndef DISABLE_TLS | |
67 | BOOL gnutls_compat_mode = FALSE; | |
68 | BOOL gnutls_allow_auto_pkcs11 = FALSE; | |
b3e93248 AM |
69 | uschar *hosts_require_alpn = NULL; |
70 | uschar *openssl_options = NULL; | |
71 | --- a/src/globals.h | |
72 | +++ b/src/globals.h | |
661899b6 | 73 | @@ -153,10 +153,14 @@ extern uschar *tls_advertise_hosts; / |
b3e93248 AM |
74 | extern uschar *dsn_envid; /* DSN envid string */ |
75 | extern int dsn_ret; /* DSN ret type*/ | |
bb1028e5 | 76 | extern const pcre2_code *regex_DSN; /* For recognizing DSN settings */ |
96e93d3d | 77 | extern uschar *dsn_advertise_hosts; /* host for which TLS is advertised */ |
4d241ba7 AM |
78 | |
79 | +#ifdef DLOPEN_LOCAL_SCAN | |
80 | +extern uschar *local_scan_path; /* Path to local_scan() library */ | |
81 | +#endif | |
bb1028e5 | 82 | + |
4d241ba7 AM |
83 | /* Input-reading functions for messages, so we can use special ones for |
84 | incoming TCP/IP. */ | |
96e93d3d | 85 | |
b3e93248 AM |
86 | extern int (*lwr_receive_getc)(unsigned); |
87 | extern uschar * (*lwr_receive_getbuf)(unsigned *); | |
88 | --- a/src/local_scan.c | |
89 | +++ b/src/local_scan.c | |
661899b6 | 90 | @@ -5,60 +5,136 @@ |
bb1028e5 | 91 | /* Copyright (c) The Exim Maintainers 2021 */ |
4d241ba7 | 92 | /* See the file NOTICE for conditions of use and distribution. */ |
661899b6 | 93 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
4d241ba7 | 94 | |
4d241ba7 AM |
95 | |
96 | -/****************************************************************************** | |
97 | -This file contains a template local_scan() function that just returns ACCEPT. | |
98 | -If you want to implement your own version, you should copy this file to, say | |
99 | -Local/local_scan.c, and edit the copy. To use your version instead of the | |
100 | -default, you must set | |
101 | - | |
048ec4ef | 102 | -HAVE_LOCAL_SCAN=yes |
4d241ba7 AM |
103 | -LOCAL_SCAN_SOURCE=Local/local_scan.c |
104 | - | |
105 | -in your Local/Makefile. This makes it easy to copy your version for use with | |
106 | -subsequent Exim releases. | |
107 | - | |
108 | -For a full description of the API to this function, see the Exim specification. | |
109 | -******************************************************************************/ | |
110 | - | |
111 | - | |
96e93d3d AM |
112 | /* This is the only Exim header that you should include. The effect of |
113 | including any other Exim header is not defined, and may change from release to | |
114 | release. Use only the documented interface! */ | |
96e93d3d | 115 | |
b3e93248 | 116 | #include "local_scan.h" |
96e93d3d | 117 | |
b3e93248 | 118 | - |
4d241ba7 AM |
119 | -/* This is a "do-nothing" version of a local_scan() function. The arguments |
120 | -are: | |
121 | - | |
122 | - fd The file descriptor of the open -D file, which contains the | |
123 | - body of the message. The file is open for reading and | |
124 | - writing, but modifying it is dangerous and not recommended. | |
125 | - | |
126 | - return_text A pointer to an unsigned char* variable which you can set in | |
127 | - order to return a text string. It is initialized to NULL. | |
128 | - | |
129 | -The return values of this function are: | |
130 | - | |
131 | - LOCAL_SCAN_ACCEPT | |
132 | - The message is to be accepted. The return_text argument is | |
133 | - saved in $local_scan_data. | |
134 | - | |
135 | - LOCAL_SCAN_REJECT | |
136 | - The message is to be rejected. The returned text is used | |
137 | - in the rejection message. | |
138 | - | |
139 | - LOCAL_SCAN_TEMPREJECT | |
140 | - This specifies a temporary rejection. The returned text | |
141 | - is used in the rejection message. | |
142 | -*/ | |
143 | +#ifdef DLOPEN_LOCAL_SCAN | |
144 | +#include <dlfcn.h> | |
661899b6 | 145 | +#include <stdlib.h> |
4d241ba7 AM |
146 | +static int (*local_scan_fn)(int fd, uschar **return_text) = NULL; |
147 | +static int load_local_scan_library(void); | |
148 | +#endif | |
149 | ||
150 | int | |
151 | local_scan(int fd, uschar **return_text) | |
152 | { | |
4d241ba7 | 153 | -return LOCAL_SCAN_ACCEPT; |
b3e93248 | 154 | + |
4d241ba7 AM |
155 | +#ifdef DLOPEN_LOCAL_SCAN |
156 | +/* local_scan_path is defined AND not the empty string */ | |
157 | +if (local_scan_path && *local_scan_path) | |
158 | + { | |
159 | + if (!local_scan_fn) | |
160 | + { | |
161 | + if (!load_local_scan_library()) | |
162 | + { | |
163 | + char *base_msg , *error_msg , *final_msg ; | |
164 | + int final_length = -1 ; | |
165 | + | |
166 | + base_msg=US"Local configuration error - local_scan() library failure\n"; | |
167 | + error_msg = dlerror() ; | |
168 | + | |
169 | + final_length = strlen(base_msg) + strlen(error_msg) + 1 ; | |
170 | + final_msg = (char*)malloc( final_length*sizeof(char) ) ; | |
171 | + *final_msg = '\0' ; | |
172 | + | |
173 | + strcat( final_msg , base_msg ) ; | |
174 | + strcat( final_msg , error_msg ) ; | |
175 | + | |
176 | + *return_text = final_msg ; | |
177 | + return LOCAL_SCAN_TEMPREJECT; | |
178 | + } | |
179 | + } | |
180 | + return local_scan_fn(fd, return_text); | |
181 | + } | |
182 | +else | |
183 | +#endif | |
184 | + return LOCAL_SCAN_ACCEPT; | |
b3e93248 AM |
185 | +} |
186 | + | |
4d241ba7 AM |
187 | +#ifdef DLOPEN_LOCAL_SCAN |
188 | + | |
189 | +static int load_local_scan_library(void) | |
190 | +{ | |
191 | +/* No point in keeping local_scan_lib since we'll never dlclose() anyway */ | |
192 | +void *local_scan_lib = NULL; | |
193 | +int (*local_scan_version_fn)(void); | |
194 | +int vers_maj; | |
195 | +int vers_min; | |
196 | + | |
197 | +local_scan_lib = dlopen(local_scan_path, RTLD_NOW); | |
198 | +if (!local_scan_lib) | |
199 | + { | |
200 | + log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() library open failed - " | |
201 | + "message temporarily rejected"); | |
202 | + return FALSE; | |
203 | + } | |
204 | + | |
205 | +local_scan_version_fn = dlsym(local_scan_lib, "local_scan_version_major"); | |
206 | +if (!local_scan_version_fn) | |
207 | + { | |
208 | + dlclose(local_scan_lib); | |
209 | + log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() library doesn't contain " | |
210 | + "local_scan_version_major() function - message temporarily rejected"); | |
211 | + return FALSE; | |
212 | + } | |
213 | + | |
214 | +/* The major number is increased when the ABI is changed in a non | |
215 | + backward compatible way. */ | |
216 | +vers_maj = local_scan_version_fn(); | |
217 | + | |
218 | +local_scan_version_fn = dlsym(local_scan_lib, "local_scan_version_minor"); | |
219 | +if (!local_scan_version_fn) | |
220 | + { | |
221 | + dlclose(local_scan_lib); | |
222 | + log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() library doesn't contain " | |
223 | + "local_scan_version_minor() function - message temporarily rejected"); | |
224 | + return FALSE; | |
225 | + } | |
226 | + | |
227 | +/* The minor number is increased each time a new feature is added (in a | |
228 | + way that doesn't break backward compatibility) -- Marc */ | |
229 | +vers_min = local_scan_version_fn(); | |
230 | + | |
231 | + | |
232 | +if (vers_maj != LOCAL_SCAN_ABI_VERSION_MAJOR) | |
233 | + { | |
234 | + dlclose(local_scan_lib); | |
235 | + local_scan_lib = NULL; | |
236 | + log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() has an incompatible major" | |
237 | + "version number, you need to recompile your module for this version" | |
238 | + "of exim (The module was compiled for version %d.%d and this exim provides" | |
239 | + "ABI version %d.%d)", vers_maj, vers_min, LOCAL_SCAN_ABI_VERSION_MAJOR, | |
240 | + LOCAL_SCAN_ABI_VERSION_MINOR); | |
241 | + return FALSE; | |
242 | + } | |
243 | +else if (vers_min > LOCAL_SCAN_ABI_VERSION_MINOR) | |
244 | + { | |
245 | + dlclose(local_scan_lib); | |
246 | + local_scan_lib = NULL; | |
247 | + log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() has an incompatible minor" | |
248 | + "version number, you need to recompile your module for this version" | |
249 | + "of exim (The module was compiled for version %d.%d and this exim provides" | |
250 | + "ABI version %d.%d)", vers_maj, vers_min, LOCAL_SCAN_ABI_VERSION_MAJOR, | |
251 | + LOCAL_SCAN_ABI_VERSION_MINOR); | |
252 | + return FALSE; | |
253 | + } | |
254 | + | |
255 | +local_scan_fn = dlsym(local_scan_lib, "local_scan"); | |
256 | +if (!local_scan_fn) | |
257 | + { | |
258 | + dlclose(local_scan_lib); | |
259 | + log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() library doesn't contain " | |
96e93d3d | 260 | + "local_scan() function - message temporarily rejected"); |
4d241ba7 AM |
261 | + return FALSE; |
262 | + } | |
4d241ba7 | 263 | +return TRUE; |
b3e93248 AM |
264 | } |
265 | ||
4d241ba7 | 266 | +#endif /* DLOPEN_LOCAL_SCAN */ |
96e93d3d | 267 | + |
4d241ba7 | 268 | /* End of local_scan.c */ |
b3e93248 AM |
269 | --- a/src/local_scan.h |
270 | +++ b/src/local_scan.h | |
661899b6 | 271 | @@ -25,10 +25,11 @@ store.c |
b3e93248 AM |
272 | /* Some basic types that make some things easier, the Exim configuration |
273 | settings, and the store functions. */ | |
96e93d3d AM |
274 | |
275 | #include <stdarg.h> | |
276 | #include <sys/types.h> | |
277 | +#pragma GCC visibility push(default) | |
278 | #include "config.h" | |
279 | #include "mytypes.h" | |
280 | #include "store.h" | |
b3e93248 AM |
281 | |
282 | ||
661899b6 | 283 | @@ -164,10 +165,13 @@ extern const uschar *headers_charset; / |
b3e93248 AM |
284 | extern header_line *header_last; /* Final header */ |
285 | extern header_line *header_list; /* First header */ | |
96e93d3d AM |
286 | extern BOOL host_checking; /* Set when checking a host */ |
287 | extern uschar *interface_address; /* Interface for incoming call */ | |
288 | extern int interface_port; /* Port number for incoming call */ | |
289 | +#ifdef DLOPEN_LOCAL_SCAN | |
290 | +extern uschar *local_scan_path; | |
291 | +#endif | |
292 | extern uschar *message_id; /* Internal id of message being handled */ | |
293 | extern uschar *received_protocol; /* Name of incoming protocol */ | |
294 | extern int recipients_count; /* Number of recipients */ | |
b3e93248 AM |
295 | extern recipient_item *recipients_list;/* List of recipient addresses */ |
296 | extern unsigned char *sender_address; /* Sender address */ | |
661899b6 | 297 | @@ -234,6 +238,8 @@ extern uschar * string_copy_taint_functi |
b3e93248 AM |
298 | extern pid_t child_open_exim_function(int *, const uschar *); |
299 | extern pid_t child_open_exim2_function(int *, uschar *, uschar *, const uschar *); | |
0473fda3 | 300 | extern pid_t child_open_function(uschar **, uschar **, int, int *, int *, BOOL, const uschar *); |
96e93d3d AM |
301 | #endif |
302 | ||
303 | +#pragma GCC visibility pop | |
304 | + | |
305 | /* End of local_scan.h */ | |
b3e93248 AM |
306 | --- a/src/readconf.c |
307 | +++ b/src/readconf.c | |
661899b6 | 308 | @@ -210,10 +210,13 @@ static optionlist optionlist_config[] = |
b3e93248 AM |
309 | #endif |
310 | { "local_from_check", opt_bool, {&local_from_check} }, | |
bc62d876 AM |
311 | { "local_from_prefix", opt_stringptr, {&local_from_prefix} }, |
312 | { "local_from_suffix", opt_stringptr, {&local_from_suffix} }, | |
313 | { "local_interfaces", opt_stringptr, {&local_interfaces} }, | |
4d241ba7 | 314 | +#ifdef DLOPEN_LOCAL_SCAN |
b3e93248 | 315 | + { "local_scan_path", opt_stringptr, &local_scan_path }, |
4d241ba7 | 316 | +#endif |
906bd90b | 317 | #ifdef HAVE_LOCAL_SCAN |
bc62d876 | 318 | { "local_scan_timeout", opt_time, {&local_scan_timeout} }, |
906bd90b | 319 | #endif |
b3e93248 AM |
320 | { "local_sender_retain", opt_bool, {&local_sender_retain} }, |
321 | { "localhost_number", opt_stringptr, {&host_number_string} }, | |
322 | --- a/src/string.c | |
323 | +++ b/src/string.c | |
661899b6 | 324 | @@ -416,10 +416,11 @@ return ss; |
b3e93248 AM |
325 | |
326 | ||
96e93d3d AM |
327 | |
328 | #if (defined(HAVE_LOCAL_SCAN) || defined(EXPAND_DLFUNC)) \ | |
329 | && !defined(MACRO_PREDEF) && !defined(COMPILE_UTILITY) | |
330 | +#pragma GCC visibility push(default) | |
331 | /************************************************* | |
332 | * Copy and save string * | |
333 | *************************************************/ | |
b3e93248 AM |
334 | |
335 | /* | |
661899b6 | 336 | @@ -461,10 +462,11 @@ Returns: copy of string in new store |
bb1028e5 AM |
337 | uschar * |
338 | string_copyn_function(const uschar * s, int n) | |
339 | { | |
340 | return string_copyn(s, n); | |
96e93d3d AM |
341 | } |
342 | +#pragma GCC visibility pop | |
343 | #endif | |
344 | ||
345 | ||
b3e93248 AM |
346 | /************************************************* |
347 | * Copy and save string in malloc'd store * |