readsocket expansion: response caching
[exim.git] / src / src / drtables.c
index 1d81557983dd7dc5d8ff4769b0792f85e3a532cb..7ecca505629bfba639dd18cd5eb191260e52f00e 100644 (file)
@@ -2,7 +2,7 @@
 *     Exim - an Internet mail transport agent    *
 *************************************************/
 
-/* Copyright (c) University of Cambridge 1995 - 2017 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
 /* See the file NOTICE for conditions of use and distribution. */
 
 
@@ -35,6 +35,10 @@ set to NULL for those that are not compiled into the binary. */
 #include "auths/dovecot.h"
 #endif
 
+#ifdef AUTH_EXTERNAL
+#include "auths/external.h"
+#endif
+
 #ifdef AUTH_GSASL
 #include "auths/gsasl_exim.h"
 #endif
@@ -69,7 +73,8 @@ auth_info auths_available[] = {
   .init =              auth_cram_md5_init,
   .servercode =                auth_cram_md5_server,
   .clientcode =                auth_cram_md5_client,
-  .version_report =    NULL
+  .version_report =    NULL,
+  .macros_create =     NULL,
   },
 #endif
 
@@ -83,7 +88,8 @@ auth_info auths_available[] = {
   .init =              auth_cyrus_sasl_init,
   .servercode =                auth_cyrus_sasl_server,
   .clientcode =                NULL,
-  .version_report =    auth_cyrus_sasl_version_report
+  .version_report =    auth_cyrus_sasl_version_report,
+  .macros_create =     NULL,
   },
 #endif
 
@@ -97,7 +103,23 @@ auth_info auths_available[] = {
   .init =              auth_dovecot_init,
   .servercode =                auth_dovecot_server,
   .clientcode =                NULL,
-  .version_report =    NULL
+  .version_report =    NULL,
+  .macros_create =     NULL,
+  },
+#endif
+
+#ifdef AUTH_EXTERNAL
+  {
+  .driver_name =       US"external",
+  .options =           auth_external_options,
+  .options_count =     &auth_external_options_count,
+  .options_block =     &auth_external_option_defaults,
+  .options_len =       sizeof(auth_external_options_block),
+  .init =              auth_external_init,
+  .servercode =                auth_external_server,
+  .clientcode =                auth_external_client,
+  .version_report =    NULL,
+  .macros_create =     NULL,
   },
 #endif
 
@@ -110,8 +132,9 @@ auth_info auths_available[] = {
   .options_len =       sizeof(auth_gsasl_options_block),
   .init =              auth_gsasl_init,
   .servercode =                auth_gsasl_server,
-  .clientcode =                NULL,
-  .version_report =    auth_gsasl_version_report
+  .clientcode =                auth_gsasl_client,
+  .version_report =    auth_gsasl_version_report,
+  .macros_create =     auth_gsasl_macros,
   },
 #endif
 
@@ -119,13 +142,14 @@ auth_info auths_available[] = {
   {
   .driver_name =       US"heimdal_gssapi",
   .options =           auth_heimdal_gssapi_options,
-  .options_count        &auth_heimdal_gssapi_options_count,
+  .options_count =     &auth_heimdal_gssapi_options_count,
   .options_block =     &auth_heimdal_gssapi_option_defaults,
   .options_len =       sizeof(auth_heimdal_gssapi_options_block),
   .init =              auth_heimdal_gssapi_init,
   .servercode =                auth_heimdal_gssapi_server,
   .clientcode =                NULL,
-  .version_report =    auth_heimdal_gssapi_version_report
+  .version_report =    auth_heimdal_gssapi_version_report,
+  .macros_create =     NULL,
   },
 #endif
 
@@ -139,7 +163,8 @@ auth_info auths_available[] = {
   .init =              auth_plaintext_init,
   .servercode =                auth_plaintext_server,
   .clientcode =                auth_plaintext_client,
-  .version_report =    NULL
+  .version_report =    NULL,
+  .macros_create =     NULL,
   },
 #endif
 
@@ -153,7 +178,8 @@ auth_info auths_available[] = {
   .init =              auth_spa_init,
   .servercode =                auth_spa_server,
   .clientcode =                auth_spa_client,
-  .version_report =    NULL
+  .version_report =    NULL,
+  .macros_create =     NULL,
   },
 #endif
 
@@ -167,11 +193,12 @@ auth_info auths_available[] = {
   .init =              auth_tls_init,
   .servercode =                auth_tls_server,
   .clientcode =                NULL,
-  .version_report =    NULL
+  .version_report =    NULL,
+  .macros_create =     NULL,
   },
 #endif
 
-{ .driver_name = US"" }                /* end marker */
+  { .driver_name = US"" }              /* end marker */
 };
 
 
@@ -329,7 +356,7 @@ router_info routers_available[] = {
   .ri_flags =          ri_notransport
   },
 #endif
-{ US"" }
+  { US"" }
 };
 
 
@@ -419,12 +446,65 @@ transport_info transports_available[] = {
   .local =             FALSE
   },
 #endif
-{ US"" }
+  { US"" }
 };
 
-
 #ifndef MACRO_PREDEF
 
+gstring *
+auth_show_supported(gstring * g)
+{
+g = string_cat(g, US"Authenticators:");
+for (auth_info * ai = auths_available; ai->driver_name[0]; ai++)
+               g = string_fmt_append(g, " %s", ai->driver_name);
+return string_cat(g, US"\n");
+}
+
+gstring *
+route_show_supported(gstring * g)
+{
+g = string_cat(g, US"Routers:");
+for (router_info * rr = routers_available; rr->driver_name[0]; rr++)
+               g = string_fmt_append(g, " %s", rr->driver_name);
+return string_cat(g, US"\n");
+}
+
+gstring *
+transport_show_supported(gstring * g)
+{
+g = string_cat(g, US"Transports:");
+#ifdef TRANSPORT_APPENDFILE
+  g = string_cat(g, US" appendfile");
+  #ifdef SUPPORT_MAILDIR
+    g = string_cat(g, US"/maildir");   /* damn these subclasses */
+  #endif
+  #ifdef SUPPORT_MAILSTORE
+    g = string_cat(g, US"/mailstore");
+  #endif
+  #ifdef SUPPORT_MBX
+    g = string_cat(g, US"/mbx");
+  #endif
+#endif
+#ifdef TRANSPORT_AUTOREPLY
+  g = string_cat(g, US" autoreply");
+#endif
+#ifdef TRANSPORT_LMTP
+  g = string_cat(g, US" lmtp");
+#endif
+#ifdef TRANSPORT_PIPE
+  g = string_cat(g, US" pipe");
+#endif
+#ifdef EXPERIMENTAL_QUEUEFILE
+  g = string_cat(g, US" queuefile");
+#endif
+#ifdef TRANSPORT_SMTP
+  g = string_cat(g, US" smtp");
+#endif
+return string_cat(g, US"\n");
+}
+
+
+
 struct lookupmodulestr
 {
   void *dl;
@@ -437,7 +517,7 @@ static struct lookupmodulestr *lookupmodules = NULL;
 static void
 addlookupmodule(void *dl, struct lookup_module_info *info)
 {
-struct lookupmodulestr *p = store_malloc(sizeof(struct lookupmodulestr));
+struct lookupmodulestr *p = store_get(sizeof(struct lookupmodulestr), FALSE);
 
 p->dl = dl;
 p->info = info;
@@ -465,8 +545,7 @@ if (lookup_list[pos])
   /* need to insert it, so move all the other items up
   (last slot is still empty, of course) */
 
-  memmove(&lookup_list[pos+1],
-         &lookup_list[pos],
+  memmove(&lookup_list[pos+1], &lookup_list[pos],
          sizeof(lookup_info *) * (lookup_list_count-pos-1));
   }
 lookup_list[pos] = info;
@@ -492,6 +571,9 @@ extern lookup_module_info dsearch_lookup_module_info;
 #if defined(LOOKUP_IBASE) && LOOKUP_IBASE!=2
 extern lookup_module_info ibase_lookup_module_info;
 #endif
+#if defined(LOOKUP_JSON)
+extern lookup_module_info json_lookup_module_info;
+#endif
 #if defined(LOOKUP_LDAP)
 extern lookup_module_info ldap_lookup_module_info;
 #endif
@@ -522,7 +604,7 @@ extern lookup_module_info redis_lookup_module_info;
 #if defined(EXPERIMENTAL_LMDB)
 extern lookup_module_info lmdb_lookup_module_info;
 #endif
-#if defined(EXPERIMENTAL_SPF)
+#if defined(SUPPORT_SPF)
 extern lookup_module_info spf_lookup_module_info;
 #endif
 #if defined(LOOKUP_SQLITE) && LOOKUP_SQLITE!=2
@@ -535,193 +617,200 @@ extern lookup_module_info testdb_lookup_module_info;
 extern lookup_module_info whoson_lookup_module_info;
 #endif
 
+extern lookup_module_info readsock_lookup_module_info;
+
 
 void
 init_lookup_list(void)
 {
 #ifdef LOOKUP_MODULE_DIR
-  DIR *dd;
-  struct dirent *ent;
-  int countmodules = 0;
-  int moduleerrors = 0;
+DIR *dd;
+struct dirent *ent;
+int countmodules = 0;
+int moduleerrors = 0;
 #endif
-  struct lookupmodulestr *p;
-  static BOOL lookup_list_init_done = FALSE;
-
+static BOOL lookup_list_init_done = FALSE;
+rmark reset_point;
 
-  if (lookup_list_init_done)
-    return;
-  lookup_list_init_done = TRUE;
+if (lookup_list_init_done)
+  return;
+reset_point = store_mark();
+lookup_list_init_done = TRUE;
 
 #if defined(LOOKUP_CDB) && LOOKUP_CDB!=2
-  addlookupmodule(NULL, &cdb_lookup_module_info);
+addlookupmodule(NULL, &cdb_lookup_module_info);
 #endif
 
 #if defined(LOOKUP_DBM) && LOOKUP_DBM!=2
-  addlookupmodule(NULL, &dbmdb_lookup_module_info);
+addlookupmodule(NULL, &dbmdb_lookup_module_info);
 #endif
 
 #if defined(LOOKUP_DNSDB) && LOOKUP_DNSDB!=2
-  addlookupmodule(NULL, &dnsdb_lookup_module_info);
+addlookupmodule(NULL, &dnsdb_lookup_module_info);
 #endif
 
 #if defined(LOOKUP_DSEARCH) && LOOKUP_DSEARCH!=2
-  addlookupmodule(NULL, &dsearch_lookup_module_info);
+addlookupmodule(NULL, &dsearch_lookup_module_info);
 #endif
 
 #if defined(LOOKUP_IBASE) && LOOKUP_IBASE!=2
-  addlookupmodule(NULL, &ibase_lookup_module_info);
+addlookupmodule(NULL, &ibase_lookup_module_info);
 #endif
 
 #ifdef LOOKUP_LDAP
-  addlookupmodule(NULL, &ldap_lookup_module_info);
+addlookupmodule(NULL, &ldap_lookup_module_info);
+#endif
+
+#ifdef LOOKUP_JSON
+addlookupmodule(NULL, &json_lookup_module_info);
 #endif
 
 #if defined(LOOKUP_LSEARCH) && LOOKUP_LSEARCH!=2
-  addlookupmodule(NULL, &lsearch_lookup_module_info);
+addlookupmodule(NULL, &lsearch_lookup_module_info);
 #endif
 
 #if defined(LOOKUP_MYSQL) && LOOKUP_MYSQL!=2
-  addlookupmodule(NULL, &mysql_lookup_module_info);
+addlookupmodule(NULL, &mysql_lookup_module_info);
 #endif
 
 #if defined(LOOKUP_NIS) && LOOKUP_NIS!=2
-  addlookupmodule(NULL, &nis_lookup_module_info);
+addlookupmodule(NULL, &nis_lookup_module_info);
 #endif
 
 #if defined(LOOKUP_NISPLUS) && LOOKUP_NISPLUS!=2
-  addlookupmodule(NULL, &nisplus_lookup_module_info);
+addlookupmodule(NULL, &nisplus_lookup_module_info);
 #endif
 
 #if defined(LOOKUP_ORACLE) && LOOKUP_ORACLE!=2
-  addlookupmodule(NULL, &oracle_lookup_module_info);
+addlookupmodule(NULL, &oracle_lookup_module_info);
 #endif
 
 #if defined(LOOKUP_PASSWD) && LOOKUP_PASSWD!=2
-  addlookupmodule(NULL, &passwd_lookup_module_info);
+addlookupmodule(NULL, &passwd_lookup_module_info);
 #endif
 
 #if defined(LOOKUP_PGSQL) && LOOKUP_PGSQL!=2
-  addlookupmodule(NULL, &pgsql_lookup_module_info);
+addlookupmodule(NULL, &pgsql_lookup_module_info);
 #endif
 
 #if defined(LOOKUP_REDIS) && LOOKUP_REDIS!=2
-  addlookupmodule(NULL, &redis_lookup_module_info);
+addlookupmodule(NULL, &redis_lookup_module_info);
 #endif
 
 #ifdef EXPERIMENTAL_LMDB
-  addlookupmodule(NULL, &lmdb_lookup_module_info);
+addlookupmodule(NULL, &lmdb_lookup_module_info);
 #endif
 
-#ifdef EXPERIMENTAL_SPF
-  addlookupmodule(NULL, &spf_lookup_module_info);
+#ifdef SUPPORT_SPF
+addlookupmodule(NULL, &spf_lookup_module_info);
 #endif
 
 #if defined(LOOKUP_SQLITE) && LOOKUP_SQLITE!=2
-  addlookupmodule(NULL, &sqlite_lookup_module_info);
+addlookupmodule(NULL, &sqlite_lookup_module_info);
 #endif
 
 #if defined(LOOKUP_TESTDB) && LOOKUP_TESTDB!=2
-  addlookupmodule(NULL, &testdb_lookup_module_info);
+addlookupmodule(NULL, &testdb_lookup_module_info);
 #endif
 
 #if defined(LOOKUP_WHOSON) && LOOKUP_WHOSON!=2
-  addlookupmodule(NULL, &whoson_lookup_module_info);
+addlookupmodule(NULL, &whoson_lookup_module_info);
 #endif
 
+addlookupmodule(NULL, &readsock_lookup_module_info);
+
 #ifdef LOOKUP_MODULE_DIR
-  dd = opendir(LOOKUP_MODULE_DIR);
-  if (dd == NULL) {
-    DEBUG(D_lookup) debug_printf("Couldn't open %s: not loading lookup modules\n", LOOKUP_MODULE_DIR);
-    log_write(0, LOG_MAIN, "Couldn't open %s: not loading lookup modules\n", LOOKUP_MODULE_DIR);
+if (!(dd = exim_opendir(LOOKUP_MODULE_DIR)))
+  {
+  DEBUG(D_lookup) debug_printf("Couldn't open %s: not loading lookup modules\n", LOOKUP_MODULE_DIR);
+  log_write(0, LOG_MAIN, "Couldn't open %s: not loading lookup modules\n", LOOKUP_MODULE_DIR);
   }
-  else {
-    const pcre *regex_islookupmod = regex_must_compile(
-      US"\\." DYNLIB_FN_EXT "$", FALSE, TRUE);
-
-    DEBUG(D_lookup) debug_printf("Loading lookup modules from %s\n", LOOKUP_MODULE_DIR);
-    while ((ent = readdir(dd)) != NULL) {
-      char *name = ent->d_name;
-      int len = (int)strlen(name);
-      if (pcre_exec(regex_islookupmod, NULL, name, len, 0, PCRE_EOPT, NULL, 0) >= 0) {
-        int pathnamelen = len + (int)strlen(LOOKUP_MODULE_DIR) + 2;
-        void *dl;
-        struct lookup_module_info *info;
-        const char *errormsg;
-
-        /* SRH: am I being paranoid here or what? */
-        if (pathnamelen > big_buffer_size) {
-          fprintf(stderr, "Loading lookup modules: %s/%s: name too long\n", LOOKUP_MODULE_DIR, name);
-          log_write(0, LOG_MAIN|LOG_PANIC, "%s/%s: name too long\n", LOOKUP_MODULE_DIR, name);
-          continue;
-        }
-
-        /* SRH: snprintf here? */
-        sprintf(CS big_buffer, "%s/%s", LOOKUP_MODULE_DIR, name);
-
-        dl = dlopen(CS big_buffer, RTLD_NOW);// TJ was LAZY
-        if (dl == NULL) {
-          fprintf(stderr, "Error loading %s: %s\n", name, dlerror());
-          moduleerrors++;
-          log_write(0, LOG_MAIN|LOG_PANIC, "Error loading lookup module %s: %s\n", name, dlerror());
-          continue;
-        }
-
-        /* FreeBSD nsdispatch() can trigger dlerror() errors about
-         * _nss_cache_cycle_prevention_function; we need to clear the dlerror()
-         * state before calling dlsym(), so that any error afterwards only
-         * comes from dlsym().
-         */
-        errormsg = dlerror();
-
-        info = (struct lookup_module_info*) dlsym(dl, "_lookup_module_info");
-        if ((errormsg = dlerror()) != NULL) {
-          fprintf(stderr, "%s does not appear to be a lookup module (%s)\n", name, errormsg);
-          dlclose(dl);
-          moduleerrors++;
-          log_write(0, LOG_MAIN|LOG_PANIC, "%s does not appear to be a lookup module (%s)\n", name, errormsg);
-          continue;
-        }
-        if (info->magic != LOOKUP_MODULE_INFO_MAGIC) {
-          fprintf(stderr, "Lookup module %s is not compatible with this version of Exim\n", name);
-          dlclose(dl);
-          moduleerrors++;
-          log_write(0, LOG_MAIN|LOG_PANIC, "Lookup module %s is not compatible with this version of Exim\n", name);
-          continue;
-        }
-
-        addlookupmodule(dl, info);
-        DEBUG(D_lookup) debug_printf("Loaded \"%s\" (%d lookup types)\n", name, info->lookupcount);
-        countmodules++;
+else
+  {
+  const pcre *regex_islookupmod = regex_must_compile(
+    US"\\." DYNLIB_FN_EXT "$", FALSE, TRUE);
+
+  DEBUG(D_lookup) debug_printf("Loading lookup modules from %s\n", LOOKUP_MODULE_DIR);
+  while ((ent = readdir(dd)))
+    {
+    char *name = ent->d_name;
+    int len = (int)strlen(name);
+    if (pcre_exec(regex_islookupmod, NULL, name, len, 0, PCRE_EOPT, NULL, 0) >= 0)
+      {
+      int pathnamelen = len + (int)strlen(LOOKUP_MODULE_DIR) + 2;
+      void *dl;
+      struct lookup_module_info *info;
+      const char *errormsg;
+
+      /* SRH: am I being paranoid here or what? */
+      if (pathnamelen > big_buffer_size)
+       {
+       fprintf(stderr, "Loading lookup modules: %s/%s: name too long\n", LOOKUP_MODULE_DIR, name);
+       log_write(0, LOG_MAIN|LOG_PANIC, "%s/%s: name too long\n", LOOKUP_MODULE_DIR, name);
+       continue;
+       }
+
+      /* SRH: snprintf here? */
+      sprintf(CS big_buffer, "%s/%s", LOOKUP_MODULE_DIR, name);
+
+      if (!(dl = dlopen(CS big_buffer, RTLD_NOW)))
+       {
+       errormsg = dlerror();
+       fprintf(stderr, "Error loading %s: %s\n", name, errormsg);
+       log_write(0, LOG_MAIN|LOG_PANIC, "Error loading lookup module %s: %s\n", name, errormsg);
+       moduleerrors++;
+       continue;
+       }
+
+      /* FreeBSD nsdispatch() can trigger dlerror() errors about
+       * _nss_cache_cycle_prevention_function; we need to clear the dlerror()
+       * state before calling dlsym(), so that any error afterwards only
+       * comes from dlsym().
+       */
+      errormsg = dlerror();
+
+      info = (struct lookup_module_info*) dlsym(dl, "_lookup_module_info");
+      if ((errormsg = dlerror()))
+       {
+       fprintf(stderr, "%s does not appear to be a lookup module (%s)\n", name, errormsg);
+       log_write(0, LOG_MAIN|LOG_PANIC, "%s does not appear to be a lookup module (%s)\n", name, errormsg);
+       dlclose(dl);
+       moduleerrors++;
+       continue;
+       }
+      if (info->magic != LOOKUP_MODULE_INFO_MAGIC)
+       {
+       fprintf(stderr, "Lookup module %s is not compatible with this version of Exim\n", name);
+       log_write(0, LOG_MAIN|LOG_PANIC, "Lookup module %s is not compatible with this version of Exim\n", name);
+       dlclose(dl);
+       moduleerrors++;
+       continue;
+       }
+
+      addlookupmodule(dl, info);
+      DEBUG(D_lookup) debug_printf("Loaded \"%s\" (%d lookup types)\n", name, info->lookupcount);
+      countmodules++;
       }
     }
-    store_free((void*)regex_islookupmod);
-    closedir(dd);
+  store_free((void*)regex_islookupmod);
+  closedir(dd);
   }
 
-  DEBUG(D_lookup) debug_printf("Loaded %d lookup modules\n", countmodules);
+DEBUG(D_lookup) debug_printf("Loaded %d lookup modules\n", countmodules);
 #endif
 
-  DEBUG(D_lookup) debug_printf("Total %d lookups\n", lookup_list_count);
-
-  lookup_list = store_malloc(sizeof(lookup_info *) * lookup_list_count);
-  memset(lookup_list, 0, sizeof(lookup_info *) * lookup_list_count);
+DEBUG(D_lookup) debug_printf("Total %d lookups\n", lookup_list_count);
 
-  /* now add all lookups to the real list */
-  p = lookupmodules;
-  while (p) {
-    int j;
-    struct lookupmodulestr *pnext;
+lookup_list = store_malloc(sizeof(lookup_info *) * lookup_list_count);
+memset(lookup_list, 0, sizeof(lookup_info *) * lookup_list_count);
 
-    for (j = 0; j < p->info->lookupcount; j++)
-      add_lookup_to_list(p->info->lookups[j]);
-
-    pnext = p->next;
-    store_free(p);
-    p = pnext;
-  }
-  /* just to be sure */
-  lookupmodules = NULL;
+/* now add all lookups to the real list */
+for (struct lookupmodulestr * p = lookupmodules; p; p = p->next)
+  for (int j = 0; j < p->info->lookupcount; j++)
+    add_lookup_to_list(p->info->lookups[j]);
+store_reset(reset_point);
+/* just to be sure */
+lookupmodules = NULL;
 }
 
 #endif /*!MACRO_PREDEF*/