1f50e55
diff --git a/src/EDITME b/src/EDITME
1329eea
index 9372675..813dd41 100644
1f50e55
--- a/src/EDITME
1f50e55
+++ b/src/EDITME
1329eea
@@ -794,6 +794,20 @@ TLS_LIBS=-lssl -lcrypto
504eb7c
 
504eb7c
 
1f50e55
 #------------------------------------------------------------------------------
1f50e55
+# On systems which support dynamic loading of shared libraries, Exim can
1f50e55
+# load a local_scan function specified in its config file instead of having
1f50e55
+# to be recompiled with the desired local_scan function. For a full
1f50e55
+# description of the API to this function, see the Exim specification.
1f50e55
+
1f50e55
+DLOPEN_LOCAL_SCAN=yes
1f50e55
+
1f50e55
+# If you set DLOPEN_LOCAL_SCAN, then you need to include -rdynamic in the
1f50e55
+# linker flags.  Without it, the loaded .so won't be able to access any
1f50e55
+# functions from exim.
1f50e55
+
8999214
+LFLAGS=-rdynamic -ldl -pie
1f50e55
+
1f50e55
+#------------------------------------------------------------------------------
1f50e55
 # The default distribution of Exim contains only the plain text form of the
1f50e55
 # documentation. Other forms are available separately. If you want to install
1f50e55
 # the documentation in "info" format, first fetch the Texinfo documentation
1f50e55
diff --git a/src/config.h.defaults b/src/config.h.defaults
1329eea
index c33e098..6983a83 100644
1f50e55
--- a/src/config.h.defaults
1f50e55
+++ b/src/config.h.defaults
1329eea
@@ -28,6 +28,8 @@ it's a default value. */
b1c3dc2
 
b1c3dc2
 #define AUTH_VARS                     3
b1c3dc2
 
b1c3dc2
+#define DLOPEN_LOCAL_SCAN
b1c3dc2
+
b1c3dc2
 #define BIN_DIRECTORY
504eb7c
 
b1c3dc2
 #define CONFIGURE_FILE
1f50e55
diff --git a/src/globals.c b/src/globals.c
d7b60f9
index 1dbc015..10fb3e4 100644
1f50e55
--- a/src/globals.c
1f50e55
+++ b/src/globals.c
1329eea
@@ -169,6 +169,10 @@ uschar *tls_verify_certificates= US"system";
1f50e55
 uschar *tls_verify_hosts       = NULL;
1f50e55
 #endif
1f50e55
 
504eb7c
+#ifdef DLOPEN_LOCAL_SCAN
1f50e55
+uschar *local_scan_path        = NULL;
504eb7c
+#endif
1f50e55
+
84967cd
 #ifndef DISABLE_PRDR
1f50e55
 /* Per Recipient Data Response variables */
1f50e55
 BOOL    prdr_enable            = FALSE;
1f50e55
diff --git a/src/globals.h b/src/globals.h
d7b60f9
index f3e884b..7063d97 100644
1f50e55
--- a/src/globals.h
1f50e55
+++ b/src/globals.h
1329eea
@@ -131,6 +131,10 @@ extern uschar *tls_verify_certificates;/* Path for certificates to check */
1f50e55
 extern uschar *tls_verify_hosts;       /* Mandatory client verification */
1f50e55
 #endif
1f50e55
 
1f50e55
+#ifdef DLOPEN_LOCAL_SCAN
1f50e55
+extern uschar *local_scan_path;        /* Path to local_scan() library */
1f50e55
+#endif
84967cd
+
84967cd
 extern uschar  *dsn_envid;             /* DSN envid string */
84967cd
 extern int      dsn_ret;               /* DSN ret type*/
1329eea
 extern const pcre  *regex_DSN;         /* For recognizing DSN settings */
1f50e55
diff --git a/src/local_scan.c b/src/local_scan.c
1f50e55
index 3500047..8599172 100644
1f50e55
--- a/src/local_scan.c
1f50e55
+++ b/src/local_scan.c
1f50e55
@@ -5,60 +5,131 @@
b1c3dc2
 /* Copyright (c) University of Cambridge 1995 - 2009 */
504eb7c
 /* See the file NOTICE for conditions of use and distribution. */
504eb7c
 
504eb7c
+#include "exim.h"
504eb7c
 
504eb7c
-/******************************************************************************
504eb7c
-This file contains a template local_scan() function that just returns ACCEPT.
504eb7c
-If you want to implement your own version, you should copy this file to, say
504eb7c
-Local/local_scan.c, and edit the copy. To use your version instead of the
504eb7c
-default, you must set
504eb7c
-
504eb7c
-LOCAL_SCAN_SOURCE=Local/local_scan.c
504eb7c
-
504eb7c
-in your Local/Makefile. This makes it easy to copy your version for use with
504eb7c
-subsequent Exim releases.
504eb7c
-
504eb7c
-For a full description of the API to this function, see the Exim specification.
504eb7c
-******************************************************************************/
504eb7c
-
504eb7c
-
504eb7c
-/* This is the only Exim header that you should include. The effect of
504eb7c
-including any other Exim header is not defined, and may change from release to
504eb7c
-release. Use only the documented interface! */
504eb7c
-
504eb7c
-#include "local_scan.h"
504eb7c
-
504eb7c
-
504eb7c
-/* This is a "do-nothing" version of a local_scan() function. The arguments
504eb7c
-are:
504eb7c
-
504eb7c
-  fd             The file descriptor of the open -D file, which contains the
504eb7c
-                   body of the message. The file is open for reading and
504eb7c
-                   writing, but modifying it is dangerous and not recommended.
504eb7c
-
504eb7c
-  return_text    A pointer to an unsigned char* variable which you can set in
504eb7c
-                   order to return a text string. It is initialized to NULL.
504eb7c
-
504eb7c
-The return values of this function are:
504eb7c
-
504eb7c
-  LOCAL_SCAN_ACCEPT
504eb7c
-                 The message is to be accepted. The return_text argument is
504eb7c
-                   saved in $local_scan_data.
504eb7c
-
504eb7c
-  LOCAL_SCAN_REJECT
504eb7c
-                 The message is to be rejected. The returned text is used
504eb7c
-                   in the rejection message.
504eb7c
-
504eb7c
-  LOCAL_SCAN_TEMPREJECT
504eb7c
-                 This specifies a temporary rejection. The returned text
504eb7c
-                   is used in the rejection message.
504eb7c
-*/
504eb7c
+#ifdef DLOPEN_LOCAL_SCAN
504eb7c
+#include <dlfcn.h>
504eb7c
+static int (*local_scan_fn)(int fd, uschar **return_text) = NULL;
504eb7c
+static int load_local_scan_library(void);
504eb7c
+#endif
504eb7c
 
504eb7c
 int
504eb7c
 local_scan(int fd, uschar **return_text)
504eb7c
 {
504eb7c
 fd = fd;                      /* Keep picky compilers happy */
504eb7c
 return_text = return_text;
504eb7c
-return LOCAL_SCAN_ACCEPT;
504eb7c
+#ifdef DLOPEN_LOCAL_SCAN
504eb7c
+/* local_scan_path is defined AND not the empty string */
504eb7c
+if (local_scan_path && *local_scan_path)
504eb7c
+  {
504eb7c
+  if (!local_scan_fn)
504eb7c
+    {
504eb7c
+    if (!load_local_scan_library())
504eb7c
+      {
504eb7c
+        char *base_msg , *error_msg , *final_msg ;
504eb7c
+        int final_length = -1 ;
504eb7c
+
504eb7c
+        base_msg=US"Local configuration error - local_scan() library failure\n";
504eb7c
+        error_msg = dlerror() ;
504eb7c
+
504eb7c
+        final_length = strlen(base_msg) + strlen(error_msg) + 1 ;
504eb7c
+        final_msg = (char*)malloc( final_length*sizeof(char) ) ;
504eb7c
+        *final_msg = '\0' ;
504eb7c
+
504eb7c
+        strcat( final_msg , base_msg ) ;
504eb7c
+        strcat( final_msg , error_msg ) ;
504eb7c
+
504eb7c
+        *return_text = final_msg ;
504eb7c
+      return LOCAL_SCAN_TEMPREJECT;
504eb7c
+      }
504eb7c
+    }
504eb7c
+    return local_scan_fn(fd, return_text);
504eb7c
+  }
504eb7c
+else
504eb7c
+#endif
504eb7c
+  return LOCAL_SCAN_ACCEPT;
1f50e55
 }
1f50e55
 
504eb7c
+#ifdef DLOPEN_LOCAL_SCAN
504eb7c
+
504eb7c
+static int load_local_scan_library(void)
504eb7c
+{
504eb7c
+/* No point in keeping local_scan_lib since we'll never dlclose() anyway */
504eb7c
+void *local_scan_lib = NULL;
504eb7c
+int (*local_scan_version_fn)(void);
504eb7c
+int vers_maj;
504eb7c
+int vers_min;
504eb7c
+
504eb7c
+local_scan_lib = dlopen(local_scan_path, RTLD_NOW);
504eb7c
+if (!local_scan_lib)
504eb7c
+  {
504eb7c
+  log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() library open failed - "
504eb7c
+    "message temporarily rejected");
504eb7c
+  return FALSE;
504eb7c
+  }
504eb7c
+
504eb7c
+local_scan_version_fn = dlsym(local_scan_lib, "local_scan_version_major");
504eb7c
+if (!local_scan_version_fn)
504eb7c
+  {
504eb7c
+  dlclose(local_scan_lib);
504eb7c
+  log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() library doesn't contain "
504eb7c
+    "local_scan_version_major() function - message temporarily rejected");
504eb7c
+  return FALSE;
504eb7c
+  }
504eb7c
+
504eb7c
+/* The major number is increased when the ABI is changed in a non
504eb7c
+   backward compatible way. */
504eb7c
+vers_maj = local_scan_version_fn();
504eb7c
+
504eb7c
+local_scan_version_fn = dlsym(local_scan_lib, "local_scan_version_minor");
504eb7c
+if (!local_scan_version_fn)
504eb7c
+  {
504eb7c
+  dlclose(local_scan_lib);
504eb7c
+  log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() library doesn't contain "
504eb7c
+    "local_scan_version_minor() function - message temporarily rejected");
504eb7c
+  return FALSE;
504eb7c
+  }
504eb7c
+
504eb7c
+/* The minor number is increased each time a new feature is added (in a
504eb7c
+   way that doesn't break backward compatibility) -- Marc */
504eb7c
+vers_min = local_scan_version_fn();
504eb7c
+
504eb7c
+
504eb7c
+if (vers_maj != LOCAL_SCAN_ABI_VERSION_MAJOR)
504eb7c
+  {
504eb7c
+  dlclose(local_scan_lib);
504eb7c
+  local_scan_lib = NULL;
504eb7c
+  log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() has an incompatible major"
504eb7c
+    "version number, you need to recompile your module for this version"
504eb7c
+    "of exim (The module was compiled for version %d.%d and this exim provides"
504eb7c
+    "ABI version %d.%d)", vers_maj, vers_min, LOCAL_SCAN_ABI_VERSION_MAJOR,
504eb7c
+    LOCAL_SCAN_ABI_VERSION_MINOR);
504eb7c
+  return FALSE;
504eb7c
+  }
504eb7c
+else if (vers_min > LOCAL_SCAN_ABI_VERSION_MINOR)
504eb7c
+  {
504eb7c
+  dlclose(local_scan_lib);
504eb7c
+  local_scan_lib = NULL;
504eb7c
+  log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() has an incompatible minor"
504eb7c
+    "version number, you need to recompile your module for this version"
504eb7c
+    "of exim (The module was compiled for version %d.%d and this exim provides"
504eb7c
+    "ABI version %d.%d)", vers_maj, vers_min, LOCAL_SCAN_ABI_VERSION_MAJOR,
504eb7c
+    LOCAL_SCAN_ABI_VERSION_MINOR);
504eb7c
+  return FALSE;
504eb7c
+  }
504eb7c
+
504eb7c
+local_scan_fn = dlsym(local_scan_lib, "local_scan");
504eb7c
+if (!local_scan_fn)
504eb7c
+  {
504eb7c
+  dlclose(local_scan_lib);
504eb7c
+  log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() library doesn't contain "
1f50e55
+    "local_scan() function - message temporarily rejected");
504eb7c
+  return FALSE;
504eb7c
+  }
504eb7c
+
504eb7c
+return TRUE;
1f50e55
+}
1f50e55
+
be4f17c
+#endif /* DLOPEN_LOCAL_SCAN */
be4f17c
+
504eb7c
 /* End of local_scan.c */
1f50e55
diff --git a/src/readconf.c b/src/readconf.c
d7b60f9
index 1de6bd7..d1e5142 100644
1f50e55
--- a/src/readconf.c
1f50e55
+++ b/src/readconf.c
d7b60f9
@@ -300,6 +300,9 @@ static optionlist optionlist_config[] = {
1f50e55
   { "local_from_prefix",        opt_stringptr,   &local_from_prefix },
1f50e55
   { "local_from_suffix",        opt_stringptr,   &local_from_suffix },
1f50e55
   { "local_interfaces",         opt_stringptr,   &local_interfaces },
504eb7c
+#ifdef DLOPEN_LOCAL_SCAN
1f50e55
+  { "local_scan_path",          opt_stringptr,   &local_scan_path },
504eb7c
+#endif
1f50e55
   { "local_scan_timeout",       opt_time,        &local_scan_timeout },
1f50e55
   { "local_sender_retain",      opt_bool,        &local_sender_retain },
1f50e55
   { "localhost_number",         opt_stringptr,   &host_number_string },