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
9 Last-Update: 2022-04-24
14 # as the traditional crypt() function.
15 # *** WARNING *** WARNING *** WARNING *** WARNING *** WARNING ***
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
21 +# to be recompiled with the desired local_scan function. For a full
22 +# description of the API to this function, see the Exim specification.
24 +DLOPEN_LOCAL_SCAN=yes
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.
31 +CFLAGS += -fvisibility=hidden
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
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
47 +#define DLOPEN_LOCAL_SCAN
51 #define CONFIGURE_FILE
52 #define CONFIGURE_FILE_USE_EUID
53 #define CONFIGURE_FILE_USE_NODE
57 uschar *dsn_envid = NULL;
59 const pcre2_code *regex_DSN = NULL;
60 uschar *dsn_advertise_hosts = NULL;
62 +#ifdef DLOPEN_LOCAL_SCAN
63 +uschar *local_scan_path = NULL;
67 BOOL gnutls_compat_mode = FALSE;
68 BOOL gnutls_allow_auto_pkcs11 = FALSE;
69 uschar *hosts_require_alpn = NULL;
70 uschar *openssl_options = NULL;
74 extern uschar *dsn_envid; /* DSN envid string */
75 extern int dsn_ret; /* DSN ret type*/
76 extern const pcre2_code *regex_DSN; /* For recognizing DSN settings */
77 extern uschar *dsn_advertise_hosts; /* host for which TLS is advertised */
79 +#ifdef DLOPEN_LOCAL_SCAN
80 +extern uschar *local_scan_path; /* Path to local_scan() library */
83 /* Input-reading functions for messages, so we can use special ones for
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
91 /* Copyright (c) University of Cambridge 1995 - 2009 */
92 /* Copyright (c) The Exim Maintainers 2021 */
93 /* See the file NOTICE for conditions of use and distribution. */
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
103 -LOCAL_SCAN_SOURCE=Local/local_scan.c
105 -in your Local/Makefile. This makes it easy to copy your version for use with
106 -subsequent Exim releases.
108 -For a full description of the API to this function, see the Exim specification.
109 -******************************************************************************/
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! */
116 #include "local_scan.h"
119 -/* This is a "do-nothing" version of a local_scan() function. The arguments
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.
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.
129 -The return values of this function are:
132 - The message is to be accepted. The return_text argument is
133 - saved in $local_scan_data.
136 - The message is to be rejected. The returned text is used
137 - in the rejection message.
139 - LOCAL_SCAN_TEMPREJECT
140 - This specifies a temporary rejection. The returned text
141 - is used in the rejection message.
143 +#ifdef DLOPEN_LOCAL_SCAN
145 +static int (*local_scan_fn)(int fd, uschar **return_text) = NULL;
146 +static int load_local_scan_library(void);
150 local_scan(int fd, uschar **return_text)
152 -return LOCAL_SCAN_ACCEPT;
154 +#ifdef DLOPEN_LOCAL_SCAN
155 +/* local_scan_path is defined AND not the empty string */
156 +if (local_scan_path && *local_scan_path)
158 + if (!local_scan_fn)
160 + if (!load_local_scan_library())
162 + char *base_msg , *error_msg , *final_msg ;
163 + int final_length = -1 ;
165 + base_msg=US"Local configuration error - local_scan() library failure\n";
166 + error_msg = dlerror() ;
168 + final_length = strlen(base_msg) + strlen(error_msg) + 1 ;
169 + final_msg = (char*)malloc( final_length*sizeof(char) ) ;
170 + *final_msg = '\0' ;
172 + strcat( final_msg , base_msg ) ;
173 + strcat( final_msg , error_msg ) ;
175 + *return_text = final_msg ;
176 + return LOCAL_SCAN_TEMPREJECT;
179 + return local_scan_fn(fd, return_text);
183 + return LOCAL_SCAN_ACCEPT;
186 +#ifdef DLOPEN_LOCAL_SCAN
188 +static int load_local_scan_library(void)
190 +/* No point in keeping local_scan_lib since we'll never dlclose() anyway */
191 +void *local_scan_lib = NULL;
192 +int (*local_scan_version_fn)(void);
196 +local_scan_lib = dlopen(local_scan_path, RTLD_NOW);
197 +if (!local_scan_lib)
199 + log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() library open failed - "
200 + "message temporarily rejected");
204 +local_scan_version_fn = dlsym(local_scan_lib, "local_scan_version_major");
205 +if (!local_scan_version_fn)
207 + dlclose(local_scan_lib);
208 + log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() library doesn't contain "
209 + "local_scan_version_major() function - message temporarily rejected");
213 +/* The major number is increased when the ABI is changed in a non
214 + backward compatible way. */
215 +vers_maj = local_scan_version_fn();
217 +local_scan_version_fn = dlsym(local_scan_lib, "local_scan_version_minor");
218 +if (!local_scan_version_fn)
220 + dlclose(local_scan_lib);
221 + log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() library doesn't contain "
222 + "local_scan_version_minor() function - message temporarily rejected");
226 +/* The minor number is increased each time a new feature is added (in a
227 + way that doesn't break backward compatibility) -- Marc */
228 +vers_min = local_scan_version_fn();
231 +if (vers_maj != LOCAL_SCAN_ABI_VERSION_MAJOR)
233 + dlclose(local_scan_lib);
234 + local_scan_lib = NULL;
235 + log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() has an incompatible major"
236 + "version number, you need to recompile your module for this version"
237 + "of exim (The module was compiled for version %d.%d and this exim provides"
238 + "ABI version %d.%d)", vers_maj, vers_min, LOCAL_SCAN_ABI_VERSION_MAJOR,
239 + LOCAL_SCAN_ABI_VERSION_MINOR);
242 +else if (vers_min > LOCAL_SCAN_ABI_VERSION_MINOR)
244 + dlclose(local_scan_lib);
245 + local_scan_lib = NULL;
246 + log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() has an incompatible minor"
247 + "version number, you need to recompile your module for this version"
248 + "of exim (The module was compiled for version %d.%d and this exim provides"
249 + "ABI version %d.%d)", vers_maj, vers_min, LOCAL_SCAN_ABI_VERSION_MAJOR,
250 + LOCAL_SCAN_ABI_VERSION_MINOR);
254 +local_scan_fn = dlsym(local_scan_lib, "local_scan");
257 + dlclose(local_scan_lib);
258 + log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() library doesn't contain "
259 + "local_scan() function - message temporarily rejected");
265 +#endif /* DLOPEN_LOCAL_SCAN */
267 /* End of local_scan.c */
268 --- a/src/local_scan.h
269 +++ b/src/local_scan.h
271 /* Some basic types that make some things easier, the Exim configuration
272 settings, and the store functions. */
275 #include <sys/types.h>
276 +#pragma GCC visibility push(default)
282 @@ -164,10 +165,13 @@
283 extern header_line *header_last; /* Final header */
284 extern header_line *header_list; /* First header */
285 extern BOOL host_checking; /* Set when checking a host */
286 extern uschar *interface_address; /* Interface for incoming call */
287 extern int interface_port; /* Port number for incoming call */
288 +#ifdef DLOPEN_LOCAL_SCAN
289 +extern uschar *local_scan_path;
291 extern uschar *message_id; /* Internal id of message being handled */
292 extern uschar *received_protocol; /* Name of incoming protocol */
293 extern int recipients_count; /* Number of recipients */
294 extern recipient_item *recipients_list;/* List of recipient addresses */
295 extern unsigned char *sender_address; /* Sender address */
297 extern pid_t child_open_exim_function(int *, const uschar *);
298 extern pid_t child_open_exim2_function(int *, uschar *, uschar *, const uschar *);
299 extern pid_t child_open_function(uschar **, uschar **, int, int *, int *, BOOL, const uschar *);
302 +#pragma GCC visibility pop
304 /* End of local_scan.h */
307 @@ -210,10 +210,13 @@
309 { "local_from_check", opt_bool, {&local_from_check} },
310 { "local_from_prefix", opt_stringptr, {&local_from_prefix} },
311 { "local_from_suffix", opt_stringptr, {&local_from_suffix} },
312 { "local_interfaces", opt_stringptr, {&local_interfaces} },
313 +#ifdef DLOPEN_LOCAL_SCAN
314 + { "local_scan_path", opt_stringptr, &local_scan_path },
316 #ifdef HAVE_LOCAL_SCAN
317 { "local_scan_timeout", opt_time, {&local_scan_timeout} },
319 { "local_sender_retain", opt_bool, {&local_sender_retain} },
320 { "localhost_number", opt_stringptr, {&host_number_string} },
323 @@ -416,10 +416,11 @@
327 #if (defined(HAVE_LOCAL_SCAN) || defined(EXPAND_DLFUNC)) \
328 && !defined(MACRO_PREDEF) && !defined(COMPILE_UTILITY)
329 +#pragma GCC visibility push(default)
330 /*************************************************
331 * Copy and save string *
332 *************************************************/
335 @@ -461,10 +462,11 @@
337 string_copyn_function(const uschar * s, int n)
339 return string_copyn(s, n);
341 +#pragma GCC visibility pop
345 /*************************************************
346 * Copy and save string in malloc'd store *