1 ## 50_localscan_dlopen.dpatch by Marc MERLIN
4 Description: Allow to use and switch between different local_scan functions
5 without recompiling exim.
6 http://marc.merlins.org/linux/exim/files/sa-exim-current/ Original patch from
7 David Woodhouse, modified first by Derrick 'dman' Hudson and then by Marc
8 MERLIN for SA-Exim and minor/major API version tracking
9 Author: David Woodhouse, Derrick 'dman' Hudson, Marc MERLIN
10 Origin: other, http://marc.merlins.org/linux/exim/files/sa-exim-current/
12 Last-Update: 2013-09-28
16 @@ -783,6 +783,21 @@ HEADERS_CHARSET="ISO-8859-1"
19 #------------------------------------------------------------------------------
20 +# On systems which support dynamic loading of shared libraries, Exim can
21 +# load a local_scan function specified in its config file instead of having
22 +# to be recompiled with the desired local_scan function. For a full
23 +# description of the API to this function, see the Exim specification.
25 +DLOPEN_LOCAL_SCAN=yes
27 +# If you set DLOPEN_LOCAL_SCAN, then you need to include -rdynamic in the
28 +# linker flags. Without it, the loaded .so won't be able to access any
29 +# functions from exim.
32 +CFLAGS += -fvisibility=hidden
34 +#------------------------------------------------------------------------------
35 # The default distribution of Exim contains only the plain text form of the
36 # documentation. Other forms are available separately. If you want to install
37 # the documentation in "info" format, first fetch the Texinfo documentation
38 --- a/src/config.h.defaults
39 +++ b/src/config.h.defaults
40 @@ -27,6 +27,8 @@ it's a default value. */
44 +#define DLOPEN_LOCAL_SCAN
48 #define CONFIGURE_FILE
51 @@ -134,6 +134,10 @@ BOOL smtp_use_dsn = FALSE;
52 uschar *dsn_advertise_hosts = NULL;
55 +#ifdef DLOPEN_LOCAL_SCAN
56 +uschar *local_scan_path = NULL;
60 BOOL gnutls_compat_mode = FALSE;
61 BOOL gnutls_allow_auto_pkcs11 = FALSE;
64 @@ -134,6 +134,9 @@ extern BOOL smtp_use_dsn; /
65 extern uschar *dsn_advertise_hosts; /* host for which TLS is advertised */
68 +#ifdef DLOPEN_LOCAL_SCAN
69 +extern uschar *local_scan_path; /* Path to local_scan() library */
71 /* Input-reading functions for messages, so we can use special ones for
74 --- a/src/local_scan.c
75 +++ b/src/local_scan.c
77 /* Copyright (c) University of Cambridge 1995 - 2009 */
78 /* See the file NOTICE for conditions of use and distribution. */
82 -/******************************************************************************
83 -This file contains a template local_scan() function that just returns ACCEPT.
84 -If you want to implement your own version, you should copy this file to, say
85 -Local/local_scan.c, and edit the copy. To use your version instead of the
86 -default, you must set
88 -LOCAL_SCAN_SOURCE=Local/local_scan.c
90 -in your Local/Makefile. This makes it easy to copy your version for use with
91 -subsequent Exim releases.
93 -For a full description of the API to this function, see the Exim specification.
94 -******************************************************************************/
97 -/* This is the only Exim header that you should include. The effect of
98 -including any other Exim header is not defined, and may change from release to
99 -release. Use only the documented interface! */
101 -#include "local_scan.h"
104 -/* This is a "do-nothing" version of a local_scan() function. The arguments
107 - fd The file descriptor of the open -D file, which contains the
108 - body of the message. The file is open for reading and
109 - writing, but modifying it is dangerous and not recommended.
111 - return_text A pointer to an unsigned char* variable which you can set in
112 - order to return a text string. It is initialized to NULL.
114 -The return values of this function are:
117 - The message is to be accepted. The return_text argument is
118 - saved in $local_scan_data.
121 - The message is to be rejected. The returned text is used
122 - in the rejection message.
124 - LOCAL_SCAN_TEMPREJECT
125 - This specifies a temporary rejection. The returned text
126 - is used in the rejection message.
128 +#ifdef DLOPEN_LOCAL_SCAN
130 +static int (*local_scan_fn)(int fd, uschar **return_text) = NULL;
131 +static int load_local_scan_library(void);
135 local_scan(int fd, uschar **return_text)
137 fd = fd; /* Keep picky compilers happy */
138 return_text = return_text;
139 -return LOCAL_SCAN_ACCEPT;
140 +#ifdef DLOPEN_LOCAL_SCAN
141 +/* local_scan_path is defined AND not the empty string */
142 +if (local_scan_path && *local_scan_path)
144 + if (!local_scan_fn)
146 + if (!load_local_scan_library())
148 + char *base_msg , *error_msg , *final_msg ;
149 + int final_length = -1 ;
151 + base_msg=US"Local configuration error - local_scan() library failure\n";
152 + error_msg = dlerror() ;
154 + final_length = strlen(base_msg) + strlen(error_msg) + 1 ;
155 + final_msg = (char*)malloc( final_length*sizeof(char) ) ;
156 + *final_msg = '\0' ;
158 + strcat( final_msg , base_msg ) ;
159 + strcat( final_msg , error_msg ) ;
161 + *return_text = final_msg ;
162 + return LOCAL_SCAN_TEMPREJECT;
165 + return local_scan_fn(fd, return_text);
169 + return LOCAL_SCAN_ACCEPT;
172 +#ifdef DLOPEN_LOCAL_SCAN
174 +static int load_local_scan_library(void)
176 +/* No point in keeping local_scan_lib since we'll never dlclose() anyway */
177 +void *local_scan_lib = NULL;
178 +int (*local_scan_version_fn)(void);
182 +local_scan_lib = dlopen(local_scan_path, RTLD_NOW);
183 +if (!local_scan_lib)
185 + log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() library open failed - "
186 + "message temporarily rejected");
190 +local_scan_version_fn = dlsym(local_scan_lib, "local_scan_version_major");
191 +if (!local_scan_version_fn)
193 + dlclose(local_scan_lib);
194 + log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() library doesn't contain "
195 + "local_scan_version_major() function - message temporarily rejected");
199 +/* The major number is increased when the ABI is changed in a non
200 + backward compatible way. */
201 +vers_maj = local_scan_version_fn();
203 +local_scan_version_fn = dlsym(local_scan_lib, "local_scan_version_minor");
204 +if (!local_scan_version_fn)
206 + dlclose(local_scan_lib);
207 + log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() library doesn't contain "
208 + "local_scan_version_minor() function - message temporarily rejected");
212 +/* The minor number is increased each time a new feature is added (in a
213 + way that doesn't break backward compatibility) -- Marc */
214 +vers_min = local_scan_version_fn();
217 +if (vers_maj != LOCAL_SCAN_ABI_VERSION_MAJOR)
219 + dlclose(local_scan_lib);
220 + local_scan_lib = NULL;
221 + log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() has an incompatible major"
222 + "version number, you need to recompile your module for this version"
223 + "of exim (The module was compiled for version %d.%d and this exim provides"
224 + "ABI version %d.%d)", vers_maj, vers_min, LOCAL_SCAN_ABI_VERSION_MAJOR,
225 + LOCAL_SCAN_ABI_VERSION_MINOR);
228 +else if (vers_min > LOCAL_SCAN_ABI_VERSION_MINOR)
230 + dlclose(local_scan_lib);
231 + local_scan_lib = NULL;
232 + log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() has an incompatible minor"
233 + "version number, you need to recompile your module for this version"
234 + "of exim (The module was compiled for version %d.%d and this exim provides"
235 + "ABI version %d.%d)", vers_maj, vers_min, LOCAL_SCAN_ABI_VERSION_MAJOR,
236 + LOCAL_SCAN_ABI_VERSION_MINOR);
240 +local_scan_fn = dlsym(local_scan_lib, "local_scan");
243 + dlclose(local_scan_lib);
244 + log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() library doesn't contain "
245 + "local_scan() function - message temporarily rejected");
252 +#endif /* DLOPEN_LOCAL_SCAN */
254 /* End of local_scan.c */
255 --- a/src/local_scan.h
256 +++ b/src/local_scan.h
257 @@ -17,6 +17,7 @@ settings, and the store functions. */
260 #include <sys/types.h>
261 +#pragma GCC visibility push(default)
265 @@ -194,4 +195,6 @@ extern uschar *string_copy(const uschar
266 extern uschar *string_copyn(uschar *, int);
267 extern uschar *string_sprintf(const char *, ...) ALMOST_PRINTF(1,2);
269 +#pragma GCC visibility pop
271 /* End of local_scan.h */
274 @@ -289,6 +289,9 @@ static optionlist optionlist_config[] =
275 { "local_from_prefix", opt_stringptr, &local_from_prefix },
276 { "local_from_suffix", opt_stringptr, &local_from_suffix },
277 { "local_interfaces", opt_stringptr, &local_interfaces },
278 +#ifdef DLOPEN_LOCAL_SCAN
279 + { "local_scan_path", opt_stringptr, &local_scan_path },
281 { "local_scan_timeout", opt_time, &local_scan_timeout },
282 { "local_sender_retain", opt_bool, &local_sender_retain },
283 { "localhost_number", opt_stringptr, &host_number_string },