Manualroute: use same host for all RCPTs of a message, even under hosts_randomize...
authorEric Andresen <eandres@google.com>
Tue, 27 Oct 2015 14:10:32 +0000 (14:10 +0000)
committerJeremy Harris <jgh146exb@wizmail.org>
Tue, 27 Oct 2015 14:15:25 +0000 (14:15 +0000)
Patch from author adjusted by JH to retain MX priorities when /MX indirection is used.

src/src/routers/rf_lookup_hostlist.c
test/confs/0613 [new file with mode: 0644]
test/log/0613 [new file with mode: 0644]
test/scripts/0000-Basic/0613 [new file with mode: 0644]

index fefc1a4..78eda22 100644 (file)
@@ -61,14 +61,13 @@ host, omit it and any subsequent hosts - i.e. treat the list like an ordered
 list of MX hosts. If the first host is the local host, act according to the
 "self" option in the configuration. */
 
-prev = NULL;
-for (h = addr->host_list; h != NULL; h = next_h)
+for (prev = NULL, h = addr->host_list; h; h = next_h)
   {
   const uschar *canonical_name;
-  int rc, len, port;
+  int rc, len, port, mx, sort_key;
 
   next_h = h->next;
-  if (h->address != NULL) { prev = h; continue; }
+  if (h->address) { prev = h; continue; }
 
   DEBUG(D_route|D_host_lookup)
     debug_printf("finding IP address for %s\n", h->name);
@@ -78,8 +77,15 @@ for (h = addr->host_list; h != NULL; h = next_h)
 
   port = host_item_get_port(h);
 
+  /* Store the previous mx and sort_key values, which were assigned in
+  host_build_hostlist and will be overwritten by host_find_bydns. */
+
+  mx = h->mx;
+  sort_key = h->sort_key;
+
   /* If the name ends with "/MX", we interpret it to mean "the list of hosts
-  pointed to by MX records with this name". */
+  pointed to by MX records with this name", and the MX record values override
+  the ordering from host_build_hostlist. */
 
   len = Ustrlen(h->name);
   if (len > 3 && strcmpic(h->name + len - 3, US"/mx") == 0)
@@ -87,6 +93,7 @@ for (h = addr->host_list; h != NULL; h = next_h)
     DEBUG(D_route|D_host_lookup)
       debug_printf("doing DNS MX lookup for %s\n", h->name);
 
+    mx = MX_NONE;
     h->name = string_copyn(h->name, len - 3);
     rc = host_find_bydns(h,
         ignore_target_hosts,
@@ -117,23 +124,23 @@ for (h = addr->host_list; h != NULL; h = next_h)
     {
     BOOL removed;
     DEBUG(D_route|D_host_lookup) debug_printf("doing DNS lookup\n");
-    rc = host_find_bydns(h, ignore_target_hosts, HOST_FIND_BY_A, NULL, NULL,
-      NULL,
-      &rblock->dnssec,                 /* domains for request/require */
-      &canonical_name, &removed);
-    if (rc == HOST_FOUND)
-      {
-      if (removed) setflag(addr, af_local_host_removed);
-      }
-    else if (rc == HOST_FIND_FAILED)
+    switch (rc = host_find_bydns(h, ignore_target_hosts, HOST_FIND_BY_A, NULL,
+       NULL, NULL,
+       &rblock->dnssec,                        /* domains for request/require */
+       &canonical_name, &removed))
       {
-      if (lookup_type == lk_default)
-        {
-        DEBUG(D_route|D_host_lookup)
-          debug_printf("DNS lookup failed: trying getipnodebyname\n");
-        rc = host_find_byname(h, ignore_target_hosts, HOST_FIND_QUALIFY_SINGLE,
-          &canonical_name, TRUE);
-        }
+      case HOST_FOUND:
+        if (removed) setflag(addr, af_local_host_removed);
+       break;
+      case HOST_FIND_FAILED:
+       if (lookup_type == lk_default)
+         {
+         DEBUG(D_route|D_host_lookup)
+           debug_printf("DNS lookup failed: trying getipnodebyname\n");
+         rc = host_find_byname(h, ignore_target_hosts, HOST_FIND_QUALIFY_SINGLE,
+           &canonical_name, TRUE);
+         }
+       break;
       }
     }
 
@@ -180,7 +187,8 @@ for (h = addr->host_list; h != NULL; h = next_h)
     return DEFER;
     }
 
-  /* Deal with a port setting */
+  /* Deal with the settings that were previously cleared:
+  port, mx and sort_key. */
 
   if (port != PORT_NONE)
     {
@@ -188,13 +196,23 @@ for (h = addr->host_list; h != NULL; h = next_h)
     for (hh = h; hh != next_h; hh = hh->next) hh->port = port;
     }
 
+  if (mx != MX_NONE)
+    {
+    host_item *hh;
+    for (hh = h; hh != next_h; hh = hh->next)
+      {
+      hh->mx = mx;
+      hh->sort_key = sort_key;
+      }
+    }
+
   /* A local host gets chopped, with its successors, if there are previous
   hosts. Otherwise the self option is used. If it is set to "send", any
   subsequent hosts that are also the local host do NOT get chopped. */
 
   if (rc == HOST_FOUND_LOCAL && !self_send)
     {
-    if (prev != NULL)
+    if (prev)
       {
       DEBUG(D_route)
         {
diff --git a/test/confs/0613 b/test/confs/0613
new file mode 100644 (file)
index 0000000..a42a8e9
--- /dev/null
@@ -0,0 +1,35 @@
+# Exim test configuration 0613
+# manualroute, hosts_randomize and multiple recipients
+
+exim_path = EXIM_PATH
+host_lookup_order = bydns
+primary_hostname = myhost.test.ex
+spool_directory = DIR/spool
+log_file_path = DIR/spool/log/%slog
+gecos_pattern = ""
+gecos_name = CALLER_NAME
+
+# ----- Main settings -----
+
+mua_wrapper = true
+log_selector = +received_recipients
+
+# ----- Routers -----
+
+begin routers
+
+smarthost:
+  driver = manualroute
+  hosts_randomize = true
+  route_list = * "</ ten-2.test.ex:15600 / ten-3.test.ex:15600"
+  transport = remote_smtp
+  no_more
+
+# ----- Transports -----
+
+begin transports
+
+remote_smtp:
+  driver = smtp
+
+# End
diff --git a/test/log/0613 b/test/log/0613
new file mode 100644 (file)
index 0000000..6bbab64
--- /dev/null
@@ -0,0 +1,9 @@
+1999-03-02 09:44:33 "Network error" here is expected. We must not get an mua_wrapper fault.
+1999-03-02 09:44:33 10HmaX-0005vi-00 <= CALLER@myhost.test.ex U=CALLER P=local S=sss for a b c d
+1999-03-02 09:44:33 10HmaX-0005vi-00 H=ten-2.test.ex [V4NET.0.0.2] Network Error
+1999-03-02 09:44:33 10HmaX-0005vi-00 H=ten-3.test.ex [V4NET.0.0.3] Network Error
+1999-03-02 09:44:33 10HmaX-0005vi-00 ** a@myhost.test.ex R=smarthost T=remote_smtp: Network Error
+1999-03-02 09:44:33 10HmaX-0005vi-00 ** b@myhost.test.ex R=smarthost T=remote_smtp: Network Error
+1999-03-02 09:44:33 10HmaX-0005vi-00 ** c@myhost.test.ex R=smarthost T=remote_smtp: Network Error
+1999-03-02 09:44:33 10HmaX-0005vi-00 ** d@myhost.test.ex R=smarthost T=remote_smtp: Network Error
+1999-03-02 09:44:33 10HmaX-0005vi-00 Completed
diff --git a/test/scripts/0000-Basic/0613 b/test/scripts/0000-Basic/0613
new file mode 100644 (file)
index 0000000..07616ba
--- /dev/null
@@ -0,0 +1,14 @@
+# manualroute, hosts_randomize and multiple recipients
+#
+#
+exim -z '"Network error" here is expected. We must not get an mua_wrapper fault.'
+****
+#
+1
+exim -bm a b c d
+Subject: test
+
+data
+.
+****
+no_msglog_check