Manualroute: use same host for all RCPTs of a message, even under hosts_randomize...
[exim.git] / src / src / routers / rf_lookup_hostlist.c
index 5646315cbb8608ad2adb950de848d40991915d4a..78eda22fba3ce0d469c07917d2d2012be9be80ce 100644 (file)
@@ -1,10 +1,8 @@
-/* $Cambridge: exim/src/src/routers/rf_lookup_hostlist.c,v 1.3 2005/01/11 15:51:03 ph10 Exp $ */
-
 /*************************************************
 *     Exim - an Internet mail transport agent    *
 *************************************************/
 
-/* Copyright (c) University of Cambridge 1995 - 2005 */
+/* Copyright (c) University of Cambridge 1995 - 2015 */
 /* See the file NOTICE for conditions of use and distribution. */
 
 
@@ -27,6 +25,12 @@ case, MX records are looked up for the name, and the list of hosts obtained
 replaces the incoming "host". In other words, "x/MX" is shorthand for "those
 hosts pointed to by x's MX records".
 
+It is also possible for a port to be specified along with the host name or IP
+address. The syntax is to add ":port" on to the end. This doesn't work with
+IPv6 addresses, so we allow IP addresses to be enclosed in [] in order to make
+this work. The specification of the port must come last, that is, after "/MX"
+if that is present.
+
 Arguments:
   rblock               the router block
   addr                 the address being routed
@@ -57,20 +61,31 @@ 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; prev = h, h = next_h)
+for (prev = NULL, h = addr->host_list; h; h = next_h)
   {
-  uschar *canonical_name;
-  int rc, len;
+  const uschar *canonical_name;
+  int rc, len, port, mx, sort_key;
 
   next_h = h->next;
-  if (h->address != NULL) continue;
+  if (h->address) { prev = h; continue; }
 
   DEBUG(D_route|D_host_lookup)
     debug_printf("finding IP address for %s\n", h->name);
 
+  /* Handle any port setting that may be on the name; it will be removed
+  from the end of the name. */
+
+  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)
@@ -78,13 +93,15 @@ for (h = addr->host_list; h != NULL; prev = h, h = next_h)
     DEBUG(D_route|D_host_lookup)
       debug_printf("doing DNS MX lookup for %s\n", h->name);
 
-    h->name[len-3] = 0;
+    mx = MX_NONE;
+    h->name = string_copyn(h->name, len - 3);
     rc = host_find_bydns(h,
         ignore_target_hosts,
         HOST_FIND_BY_MX,                /* look only for MX records */
         NULL,                           /* SRV service not relevant */
         NULL,                           /* failing srv domains not relevant */
         NULL,                           /* no special mx failing domains */
+        &rblock->dnssec,               /* dnssec request/require */
         NULL,                           /* fully_qualified_name */
         NULL);                          /* indicate local host removed */
     }
@@ -92,10 +109,11 @@ for (h = addr->host_list; h != NULL; prev = h, h = next_h)
   /* If explicitly configured to look up by name, or if the "host name" is
   actually an IP address, do a byname lookup. */
 
-  else if (lookup_type == lk_byname || string_is_ip_address(h->name, NULL) > 0)
+  else if (lookup_type == lk_byname || string_is_ip_address(h->name, NULL) != 0)
     {
     DEBUG(D_route|D_host_lookup) debug_printf("calling host_find_byname\n");
-    rc = host_find_byname(h, ignore_target_hosts, &canonical_name, TRUE);
+    rc = host_find_byname(h, ignore_target_hosts, HOST_FIND_QUALIFY_SINGLE,
+      &canonical_name, TRUE);
     }
 
   /* Otherwise, do a DNS lookup. If that yields "host not found", and the
@@ -106,20 +124,23 @@ for (h = addr->host_list; h != NULL; prev = h, 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, &canonical_name, &removed);
-    if (rc == HOST_FOUND)
+    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 (removed) setflag(addr, af_local_host_removed);
-      }
-    else if (rc == 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, &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;
       }
     }
 
@@ -144,9 +165,16 @@ for (h = addr->host_list; h != NULL; prev = h, h = next_h)
 
   if (rc == HOST_FIND_FAILED)
     {
+    if (hff_code == hff_ignore)
+      {
+      if (prev == NULL) addr->host_list = next_h; else prev->next = next_h;
+      continue;   /* With the next host, leave prev unchanged */
+      }
+
     if (hff_code == hff_pass) return PASS;
     if (hff_code == hff_decline) return DECLINE;
 
+    addr->basic_errno = ERRNO_UNKNOWNHOST;
     addr->message =
       string_sprintf("lookup of host \"%s\" failed in %s router%s",
         h->name, rblock->name,
@@ -159,13 +187,32 @@ for (h = addr->host_list; h != NULL; prev = h, h = next_h)
     return DEFER;
     }
 
+  /* Deal with the settings that were previously cleared:
+  port, mx and sort_key. */
+
+  if (port != PORT_NONE)
+    {
+    host_item *hh;
+    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)
         {
@@ -185,6 +232,12 @@ for (h = addr->host_list; h != NULL; prev = h, h = next_h)
       }
     self_send = TRUE;
     }
+
+  /* Ensure that prev is the host before next_h; this will not be h if a lookup
+  found multiple addresses or multiple MX records. */
+
+  prev = h;
+  while (prev->next != next_h) prev = prev->next;
   }
 
 return OK;