]> git.pld-linux.org Git - packages/exim.git/blame - localscan_dlopen_exim_4.20_or_better.patch
Up to 4.95; still using SRS_ALT implementation over new, native one (need migration...
[packages/exim.git] / localscan_dlopen_exim_4.20_or_better.patch
CommitLineData
b3e93248
AM
1Description: 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
6Author: David Woodhouse, Derrick 'dman' Hudson, Marc MERLIN
7Origin: other, http://marc.merlins.org/linux/exim/files/sa-exim-current/
8Forwarded: https://bugs.exim.org/show_bug.cgi?id=2671
9Last-Update: 2021-07-28
10
11--- a/src/EDITME
12+++ b/src/EDITME
13@@ -881,10 +881,25 @@
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
41@@ -33,10 +33,12 @@
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
56@@ -119,10 +119,14 @@
57 uschar *dsn_envid = NULL;
58 int dsn_ret = 0;
96e93d3d
AM
59 const pcre *regex_DSN = NULL;
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
73@@ -154,10 +154,13 @@
74 extern uschar *dsn_envid; /* DSN envid string */
75 extern int dsn_ret; /* DSN ret type*/
96e93d3d
AM
76 extern const pcre *regex_DSN; /* For recognizing DSN settings */
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
4d241ba7
AM
82 /* Input-reading functions for messages, so we can use special ones for
83 incoming TCP/IP. */
96e93d3d 84
b3e93248
AM
85 extern int (*lwr_receive_getc)(unsigned);
86 extern uschar * (*lwr_receive_getbuf)(unsigned *);
87--- a/src/local_scan.c
88+++ b/src/local_scan.c
89@@ -4,60 +4,135 @@
90
91 /* Copyright (c) University of Cambridge 1995 - 2009 */
4d241ba7
AM
92 /* See the file NOTICE for conditions of use and distribution. */
93
4d241ba7
AM
94
95-/******************************************************************************
96-This file contains a template local_scan() function that just returns ACCEPT.
97-If you want to implement your own version, you should copy this file to, say
98-Local/local_scan.c, and edit the copy. To use your version instead of the
99-default, you must set
100-
048ec4ef 101-HAVE_LOCAL_SCAN=yes
4d241ba7
AM
102-LOCAL_SCAN_SOURCE=Local/local_scan.c
103-
104-in your Local/Makefile. This makes it easy to copy your version for use with
105-subsequent Exim releases.
106-
107-For a full description of the API to this function, see the Exim specification.
108-******************************************************************************/
109-
110-
96e93d3d
AM
111 /* This is the only Exim header that you should include. The effect of
112 including any other Exim header is not defined, and may change from release to
113 release. Use only the documented interface! */
96e93d3d 114
b3e93248 115 #include "local_scan.h"
96e93d3d 116
b3e93248 117-
4d241ba7
AM
118-/* This is a "do-nothing" version of a local_scan() function. The arguments
119-are:
120-
121- fd The file descriptor of the open -D file, which contains the
122- body of the message. The file is open for reading and
123- writing, but modifying it is dangerous and not recommended.
124-
125- return_text A pointer to an unsigned char* variable which you can set in
126- order to return a text string. It is initialized to NULL.
127-
128-The return values of this function are:
129-
130- LOCAL_SCAN_ACCEPT
131- The message is to be accepted. The return_text argument is
132- saved in $local_scan_data.
133-
134- LOCAL_SCAN_REJECT
135- The message is to be rejected. The returned text is used
136- in the rejection message.
137-
138- LOCAL_SCAN_TEMPREJECT
139- This specifies a temporary rejection. The returned text
140- is used in the rejection message.
141-*/
142+#ifdef DLOPEN_LOCAL_SCAN
143+#include <dlfcn.h>
144+static int (*local_scan_fn)(int fd, uschar **return_text) = NULL;
145+static int load_local_scan_library(void);
146+#endif
147
148 int
149 local_scan(int fd, uschar **return_text)
150 {
4d241ba7 151-return LOCAL_SCAN_ACCEPT;
b3e93248 152+
4d241ba7
AM
153+#ifdef DLOPEN_LOCAL_SCAN
154+/* local_scan_path is defined AND not the empty string */
155+if (local_scan_path && *local_scan_path)
156+ {
157+ if (!local_scan_fn)
158+ {
159+ if (!load_local_scan_library())
160+ {
161+ char *base_msg , *error_msg , *final_msg ;
162+ int final_length = -1 ;
163+
164+ base_msg=US"Local configuration error - local_scan() library failure\n";
165+ error_msg = dlerror() ;
166+
167+ final_length = strlen(base_msg) + strlen(error_msg) + 1 ;
168+ final_msg = (char*)malloc( final_length*sizeof(char) ) ;
169+ *final_msg = '\0' ;
170+
171+ strcat( final_msg , base_msg ) ;
172+ strcat( final_msg , error_msg ) ;
173+
174+ *return_text = final_msg ;
175+ return LOCAL_SCAN_TEMPREJECT;
176+ }
177+ }
178+ return local_scan_fn(fd, return_text);
179+ }
180+else
181+#endif
182+ return LOCAL_SCAN_ACCEPT;
b3e93248
AM
183+}
184+
4d241ba7
AM
185+#ifdef DLOPEN_LOCAL_SCAN
186+
187+static int load_local_scan_library(void)
188+{
189+/* No point in keeping local_scan_lib since we'll never dlclose() anyway */
190+void *local_scan_lib = NULL;
191+int (*local_scan_version_fn)(void);
192+int vers_maj;
193+int vers_min;
194+
195+local_scan_lib = dlopen(local_scan_path, RTLD_NOW);
196+if (!local_scan_lib)
197+ {
198+ log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() library open failed - "
199+ "message temporarily rejected");
200+ return FALSE;
201+ }
202+
203+local_scan_version_fn = dlsym(local_scan_lib, "local_scan_version_major");
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_major() function - message temporarily rejected");
209+ return FALSE;
210+ }
211+
212+/* The major number is increased when the ABI is changed in a non
213+ backward compatible way. */
214+vers_maj = local_scan_version_fn();
215+
216+local_scan_version_fn = dlsym(local_scan_lib, "local_scan_version_minor");
217+if (!local_scan_version_fn)
218+ {
219+ dlclose(local_scan_lib);
220+ log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() library doesn't contain "
221+ "local_scan_version_minor() function - message temporarily rejected");
222+ return FALSE;
223+ }
224+
225+/* The minor number is increased each time a new feature is added (in a
226+ way that doesn't break backward compatibility) -- Marc */
227+vers_min = local_scan_version_fn();
228+
229+
230+if (vers_maj != LOCAL_SCAN_ABI_VERSION_MAJOR)
231+ {
232+ dlclose(local_scan_lib);
233+ local_scan_lib = NULL;
234+ log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() has an incompatible major"
235+ "version number, you need to recompile your module for this version"
236+ "of exim (The module was compiled for version %d.%d and this exim provides"
237+ "ABI version %d.%d)", vers_maj, vers_min, LOCAL_SCAN_ABI_VERSION_MAJOR,
238+ LOCAL_SCAN_ABI_VERSION_MINOR);
239+ return FALSE;
240+ }
241+else if (vers_min > LOCAL_SCAN_ABI_VERSION_MINOR)
242+ {
243+ dlclose(local_scan_lib);
244+ local_scan_lib = NULL;
245+ log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() has an incompatible minor"
246+ "version number, you need to recompile your module for this version"
247+ "of exim (The module was compiled for version %d.%d and this exim provides"
248+ "ABI version %d.%d)", vers_maj, vers_min, LOCAL_SCAN_ABI_VERSION_MAJOR,
249+ LOCAL_SCAN_ABI_VERSION_MINOR);
250+ return FALSE;
251+ }
252+
253+local_scan_fn = dlsym(local_scan_lib, "local_scan");
254+if (!local_scan_fn)
255+ {
256+ dlclose(local_scan_lib);
257+ log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() library doesn't contain "
96e93d3d 258+ "local_scan() function - message temporarily rejected");
4d241ba7
AM
259+ return FALSE;
260+ }
4d241ba7 261+return TRUE;
b3e93248
AM
262 }
263
4d241ba7 264+#endif /* DLOPEN_LOCAL_SCAN */
96e93d3d 265+
4d241ba7 266 /* End of local_scan.c */
b3e93248
AM
267--- a/src/local_scan.h
268+++ b/src/local_scan.h
269@@ -25,10 +25,11 @@
270 /* Some basic types that make some things easier, the Exim configuration
271 settings, and the store functions. */
96e93d3d
AM
272
273 #include <stdarg.h>
274 #include <sys/types.h>
275+#pragma GCC visibility push(default)
276 #include "config.h"
277 #include "mytypes.h"
278 #include "store.h"
b3e93248
AM
279
280
281@@ -164,10 +165,13 @@
282 extern header_line *header_last; /* Final header */
283 extern header_line *header_list; /* First header */
96e93d3d
AM
284 extern BOOL host_checking; /* Set when checking a host */
285 extern uschar *interface_address; /* Interface for incoming call */
286 extern int interface_port; /* Port number for incoming call */
287+#ifdef DLOPEN_LOCAL_SCAN
288+extern uschar *local_scan_path;
289+#endif
290 extern uschar *message_id; /* Internal id of message being handled */
291 extern uschar *received_protocol; /* Name of incoming protocol */
292 extern int recipients_count; /* Number of recipients */
b3e93248
AM
293 extern recipient_item *recipients_list;/* List of recipient addresses */
294 extern unsigned char *sender_address; /* Sender address */
295@@ -233,6 +237,8 @@
296 extern pid_t child_open_exim_function(int *, const uschar *);
297 extern pid_t child_open_exim2_function(int *, uschar *, uschar *, const uschar *);
0473fda3 298 extern pid_t child_open_function(uschar **, uschar **, int, int *, int *, BOOL, const uschar *);
96e93d3d
AM
299 #endif
300
301+#pragma GCC visibility pop
302+
303 /* End of local_scan.h */
b3e93248
AM
304--- a/src/readconf.c
305+++ b/src/readconf.c
306@@ -213,10 +213,13 @@
307 #endif
308 { "local_from_check", opt_bool, {&local_from_check} },
bc62d876
AM
309 { "local_from_prefix", opt_stringptr, {&local_from_prefix} },
310 { "local_from_suffix", opt_stringptr, {&local_from_suffix} },
311 { "local_interfaces", opt_stringptr, {&local_interfaces} },
4d241ba7 312+#ifdef DLOPEN_LOCAL_SCAN
b3e93248 313+ { "local_scan_path", opt_stringptr, &local_scan_path },
4d241ba7 314+#endif
906bd90b 315 #ifdef HAVE_LOCAL_SCAN
bc62d876 316 { "local_scan_timeout", opt_time, {&local_scan_timeout} },
906bd90b 317 #endif
b3e93248
AM
318 { "local_sender_retain", opt_bool, {&local_sender_retain} },
319 { "localhost_number", opt_stringptr, {&host_number_string} },
320--- a/src/string.c
321+++ b/src/string.c
322@@ -416,10 +416,11 @@
323
324
96e93d3d
AM
325
326 #if (defined(HAVE_LOCAL_SCAN) || defined(EXPAND_DLFUNC)) \
327 && !defined(MACRO_PREDEF) && !defined(COMPILE_UTILITY)
328+#pragma GCC visibility push(default)
329 /*************************************************
330 * Copy and save string *
331 *************************************************/
b3e93248
AM
332
333 /*
334@@ -468,10 +469,11 @@
335 uschar *ss = store_get(n + 1, is_tainted(s));
336 Ustrncpy(ss, s, n);
96e93d3d
AM
337 ss[n] = 0;
338 return ss;
339 }
340+#pragma GCC visibility pop
341 #endif
342
343
b3e93248
AM
344 /*************************************************
345 * Copy and save string in malloc'd store *
This page took 0.093663 seconds and 4 git commands to generate.