Import Debian changes 4.89-2+deb9u4
[hcoop/debian/exim4.git] / debian / patches / 50_localscan_dlopen.dpatch
CommitLineData
de45f55a
AM
1## 50_localscan_dlopen.dpatch by Marc MERLIN
2
3
4Description: 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
9Author: David Woodhouse, Derrick 'dman' Hudson, Marc MERLIN
10Origin: other, http://marc.merlins.org/linux/exim/files/sa-exim-current/
11Forwarded: no
0baa7b9d 12Last-Update: 2014-12-01
de45f55a
AM
13
14--- a/src/EDITME
15+++ b/src/EDITME
0baa7b9d 16@@ -785,6 +785,21 @@ HEADERS_CHARSET="ISO-8859-1"
de45f55a
AM
17
18
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.
24+
25+DLOPEN_LOCAL_SCAN=yes
26+
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.
30+
31+LDFLAGS += -rdynamic
32+CFLAGS += -fvisibility=hidden
33+
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
0baa7b9d 40@@ -28,6 +28,8 @@ it's a default value. */
de45f55a
AM
41
42 #define AUTH_VARS 3
43
44+#define DLOPEN_LOCAL_SCAN
45+
46 #define BIN_DIRECTORY
47
48 #define CONFIGURE_FILE
49--- a/src/globals.c
50+++ b/src/globals.c
0baa7b9d
SB
51@@ -140,6 +140,10 @@ int dsn_ret = 0;
52 const pcre *regex_DSN = NULL;
de45f55a 53 uschar *dsn_advertise_hosts = NULL;
de45f55a
AM
54
55+#ifdef DLOPEN_LOCAL_SCAN
56+uschar *local_scan_path = NULL;
57+#endif
58+
59 #ifdef SUPPORT_TLS
60 BOOL gnutls_compat_mode = FALSE;
61 BOOL gnutls_allow_auto_pkcs11 = FALSE;
62--- a/src/globals.h
63+++ b/src/globals.h
0baa7b9d
SB
64@@ -133,6 +133,9 @@ extern int dsn_ret; /
65 extern const pcre *regex_DSN; /* For recognizing DSN settings */
de45f55a 66 extern uschar *dsn_advertise_hosts; /* host for which TLS is advertised */
de45f55a
AM
67
68+#ifdef DLOPEN_LOCAL_SCAN
69+extern uschar *local_scan_path; /* Path to local_scan() library */
70+#endif
71 /* Input-reading functions for messages, so we can use special ones for
72 incoming TCP/IP. */
73
74--- a/src/local_scan.c
75+++ b/src/local_scan.c
76@@ -5,60 +5,131 @@
77 /* Copyright (c) University of Cambridge 1995 - 2009 */
78 /* See the file NOTICE for conditions of use and distribution. */
79
80+#include "exim.h"
81
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
87-
88-LOCAL_SCAN_SOURCE=Local/local_scan.c
89-
90-in your Local/Makefile. This makes it easy to copy your version for use with
91-subsequent Exim releases.
92-
93-For a full description of the API to this function, see the Exim specification.
94-******************************************************************************/
95-
96-
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! */
100-
101-#include "local_scan.h"
102-
103-
104-/* This is a "do-nothing" version of a local_scan() function. The arguments
105-are:
106-
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.
110-
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.
113-
114-The return values of this function are:
115-
116- LOCAL_SCAN_ACCEPT
117- The message is to be accepted. The return_text argument is
118- saved in $local_scan_data.
119-
120- LOCAL_SCAN_REJECT
121- The message is to be rejected. The returned text is used
122- in the rejection message.
123-
124- LOCAL_SCAN_TEMPREJECT
125- This specifies a temporary rejection. The returned text
126- is used in the rejection message.
127-*/
128+#ifdef DLOPEN_LOCAL_SCAN
129+#include <dlfcn.h>
130+static int (*local_scan_fn)(int fd, uschar **return_text) = NULL;
131+static int load_local_scan_library(void);
132+#endif
133
134 int
135 local_scan(int fd, uschar **return_text)
136 {
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)
143+ {
144+ if (!local_scan_fn)
145+ {
146+ if (!load_local_scan_library())
147+ {
148+ char *base_msg , *error_msg , *final_msg ;
149+ int final_length = -1 ;
150+
151+ base_msg=US"Local configuration error - local_scan() library failure\n";
152+ error_msg = dlerror() ;
153+
154+ final_length = strlen(base_msg) + strlen(error_msg) + 1 ;
155+ final_msg = (char*)malloc( final_length*sizeof(char) ) ;
156+ *final_msg = '\0' ;
157+
158+ strcat( final_msg , base_msg ) ;
159+ strcat( final_msg , error_msg ) ;
160+
161+ *return_text = final_msg ;
162+ return LOCAL_SCAN_TEMPREJECT;
163+ }
164+ }
165+ return local_scan_fn(fd, return_text);
166+ }
167+else
168+#endif
169+ return LOCAL_SCAN_ACCEPT;
170+}
171+
172+#ifdef DLOPEN_LOCAL_SCAN
173+
174+static int load_local_scan_library(void)
175+{
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);
179+int vers_maj;
180+int vers_min;
181+
182+local_scan_lib = dlopen(local_scan_path, RTLD_NOW);
183+if (!local_scan_lib)
184+ {
185+ log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() library open failed - "
186+ "message temporarily rejected");
187+ return FALSE;
188+ }
189+
190+local_scan_version_fn = dlsym(local_scan_lib, "local_scan_version_major");
191+if (!local_scan_version_fn)
192+ {
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");
196+ return FALSE;
197+ }
198+
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();
202+
203+local_scan_version_fn = dlsym(local_scan_lib, "local_scan_version_minor");
204+if (!local_scan_version_fn)
205+ {
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");
209+ return FALSE;
210+ }
211+
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();
215+
216+
217+if (vers_maj != LOCAL_SCAN_ABI_VERSION_MAJOR)
218+ {
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);
226+ return FALSE;
227+ }
228+else if (vers_min > LOCAL_SCAN_ABI_VERSION_MINOR)
229+ {
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);
237+ return FALSE;
238+ }
239+
240+local_scan_fn = dlsym(local_scan_lib, "local_scan");
241+if (!local_scan_fn)
242+ {
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");
246+ return FALSE;
247+ }
248+
249+return TRUE;
250 }
251
252+#endif /* DLOPEN_LOCAL_SCAN */
253+
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. */
258
259 #include <stdarg.h>
260 #include <sys/types.h>
261+#pragma GCC visibility push(default)
262 #include "config.h"
263 #include "mytypes.h"
264 #include "store.h"
0baa7b9d
SB
265@@ -192,4 +193,6 @@ extern uschar *string_copy(const uschar
266 extern uschar *string_copyn(const uschar *, int);
de45f55a
AM
267 extern uschar *string_sprintf(const char *, ...) ALMOST_PRINTF(1,2);
268
269+#pragma GCC visibility pop
270+
271 /* End of local_scan.h */
272--- a/src/readconf.c
273+++ b/src/readconf.c
0baa7b9d 274@@ -313,6 +313,9 @@ static optionlist optionlist_config[] =
de45f55a
AM
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 },
280+#endif
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 },