Bugzilla #2: If the last fallback host listed was multihomed, only its
[exim.git] / src / src / transports / smtp.c
index 1a7f84271325a7e92e10c1c5e41537f3bd6e2fee..edcdc409d799fe0482ae8059c572d3e23fdf0aec 100644 (file)
@@ -1,10 +1,10 @@
-/* $Cambridge: exim/src/src/transports/smtp.c,v 1.2 2004/10/14 14:52:45 ph10 Exp $ */
+/* $Cambridge: exim/src/src/transports/smtp.c,v 1.8 2005/03/22 15:45:35 ph10 Exp $ */
 
 /*************************************************
 *     Exim - an Internet mail transport agent    *
 *************************************************/
 
-/* Copyright (c) University of Cambridge 1995 - 2004 */
+/* Copyright (c) University of Cambridge 1995 - 2005 */
 /* See the file NOTICE for conditions of use and distribution. */
 
 #include "../exim.h"
@@ -35,6 +35,20 @@ optionlist smtp_transport_options[] = {
       (void *)offsetof(smtp_transport_options_block, data_timeout) },
   { "delay_after_cutoff", opt_bool,
       (void *)offsetof(smtp_transport_options_block, delay_after_cutoff) },
+#ifdef EXPERIMENTAL_DOMAINKEYS
+  { "dk_canon", opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, dk_canon) },
+  { "dk_domain", opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, dk_domain) },
+  { "dk_headers", opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, dk_headers) },
+  { "dk_private_key", opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, dk_private_key) },
+  { "dk_selector", opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, dk_selector) },
+  { "dk_strict", opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, dk_strict) },
+#endif
   { "dns_qualify_single",   opt_bool,
       (void *)offsetof(smtp_transport_options_block, dns_qualify_single) },
   { "dns_search_parents",   opt_bool,
@@ -57,6 +71,8 @@ optionlist smtp_transport_options[] = {
   #endif
   { "hosts_max_try",        opt_int,
       (void *)offsetof(smtp_transport_options_block, hosts_max_try) },
+  { "hosts_max_try_hardlimit", opt_int,
+      (void *)offsetof(smtp_transport_options_block, hosts_max_try_hardlimit) },
   #ifdef SUPPORT_TLS
   { "hosts_nopass_tls",     opt_stringptr,
       (void *)offsetof(smtp_transport_options_block, hosts_nopass_tls) },
@@ -138,6 +154,7 @@ smtp_transport_options_block smtp_transport_option_defaults = {
   10*60,               /* final timeout */
   1024,                /* size_addition */
   5,                   /* hosts_max_try */
+  50,                  /* hosts_max_try_hardlimit */
   FALSE,               /* allow_localhost */
   FALSE,               /* gethostbyname */
   TRUE,                /* dns_qualify_single */
@@ -155,6 +172,14 @@ smtp_transport_options_block smtp_transport_option_defaults = {
   NULL,                /* tls_verify_certificates */
   TRUE                 /* tls_tempfail_tryclear */
   #endif
+  #ifdef EXPERIMENTAL_DOMAINKEYS
+ ,NULL,                /* dk_canon */
+  NULL,                /* dk_domain */
+  NULL,                /* dk_headers */
+  NULL,                /* dk_private_key */
+  NULL,                /* dk_selector */
+  NULL                 /* dk_strict */
+  #endif
 };
 
 
@@ -390,7 +415,7 @@ end the DATA. */
 if (*errno_value == ERRNO_FILTER_FAIL)
   {
   *message = US string_sprintf("transport filter process failed (%d)%s",
-    more_errno, 
+    more_errno,
     (more_errno == EX_EXECFAILED)? ": unable to execute command" : "");
   return FALSE;
   }
@@ -1391,6 +1416,23 @@ if (!ok) ok = TRUE; else
   DEBUG(D_transport|D_v)
     debug_printf("  SMTP>> writing message and terminating \".\"\n");
   transport_count = 0;
+#ifdef EXPERIMENTAL_DOMAINKEYS
+  if ( (ob->dk_private_key != NULL) && (ob->dk_selector != NULL) )
+    ok = dk_transport_write_message(addrlist, inblock.sock,
+      topt_use_crlf | topt_end_dot | topt_escape_headers |
+        (tblock->body_only? topt_no_headers : 0) |
+        (tblock->headers_only? topt_no_body : 0) |
+        (tblock->return_path_add? topt_add_return_path : 0) |
+        (tblock->delivery_date_add? topt_add_delivery_date : 0) |
+        (tblock->envelope_to_add? topt_add_envelope_to : 0),
+      0,            /* No size limit */
+      tblock->add_headers, tblock->remove_headers,
+      US".", US"..",    /* Escaping strings */
+      tblock->rewrite_rules, tblock->rewrite_existflags,
+      ob->dk_private_key, ob->dk_domain, ob->dk_selector,
+      ob->dk_canon, ob->dk_headers, ob->dk_strict);
+  else
+#endif
   ok = transport_write_message(addrlist, inblock.sock,
     topt_use_crlf | topt_end_dot | topt_escape_headers |
       (tblock->body_only? topt_no_headers : 0) |
@@ -1926,6 +1968,7 @@ int hosts_looked_up = 0;
 int hosts_retry = 0;
 int hosts_serial = 0;
 int hosts_total = 0;
+int total_hosts_tried = 0;
 address_item *addr;
 BOOL expired = TRUE;
 BOOL continuing = continue_hostname != NULL;
@@ -2091,7 +2134,9 @@ if (Ustrcmp(pistring, ":25") == 0) pistring = US"";
 
 .  If there are any addresses whose status is still DEFER, carry on to the
    next host/IPaddress, unless we have tried the number of hosts given
-   by hosts_max_try; otherwise return.
+   by hosts_max_try or hosts_max_try_hardlimit; otherwise return. Note that
+   there is some fancy logic for hosts_max_try that means its limit can be
+   overstepped in some circumstances.
 
 If we get to the end of the list, all hosts have deferred at least one address,
 or not reached their retry times. If delay_after_cutoff is unset, it requests a
@@ -2108,7 +2153,9 @@ for (cutoff_retry = 0; expired &&
   int unexpired_hosts_tried = 0;
 
   for (host = hostlist;
-       host != NULL && unexpired_hosts_tried < ob->hosts_max_try;
+       host != NULL &&
+         unexpired_hosts_tried < ob->hosts_max_try &&
+         total_hosts_tried < ob->hosts_max_try_hardlimit;
        host = nexthost)
     {
     int rc;
@@ -2125,11 +2172,6 @@ for (cutoff_retry = 0; expired &&
     uschar *retry_message_key = NULL;
     uschar *serialize_key = NULL;
 
-    /* Default next host is next host. :-) But this can vary if the
-    hosts_max_try limit is hit (see below). */
-
-    nexthost = host->next;
-
     /* Set the flag requesting that this host be added to the waiting
     database if the delivery fails temporarily or if we are running with
     queue_smtp or a 2-stage queue run. This gets unset for certain
@@ -2169,7 +2211,7 @@ for (cutoff_retry = 0; expired &&
       /* Find by name if so configured, or if it's an IP address. We don't
       just copy the IP address, because we need the test-for-local to happen. */
 
-      if (ob->gethostbyname || string_is_ip_address(host->name, NULL))
+      if (ob->gethostbyname || string_is_ip_address(host->name, NULL) > 0)
         rc = host_find_byname(host, NULL, &canonical_name, TRUE);
       else
         {
@@ -2236,6 +2278,13 @@ for (cutoff_retry = 0; expired &&
       continue;      /* With next host */
       }
 
+    /* The default next host is the next host. :-) But this can vary if the
+    hosts_max_try limit is hit (see below). NOTE: we cannot put this setting
+    earlier than this, because a multihomed host whose addresses are not looked
+    up till just above will add to the host list. */
+
+    nexthost = host->next;
+
     /* If queue_smtp is set (-odqs or the first part of a 2-stage run), or the
     domain is in queue_smtp_domains, we don't actually want to attempt any
     deliveries. When doing a queue run, queue_smtp_domains is always unset. If
@@ -2413,13 +2462,16 @@ for (cutoff_retry = 0; expired &&
 
     /* This is for real. If the host is expired, we don't count it for
     hosts_max_retry. This ensures that all hosts must expire before an address
-    is timed out. Otherwise, if we are about to hit the hosts_max_retry limit,
-    check to see if there is a subsequent hosts with a different MX value. If
-    so, make that the next host, and don't count this one. This is a heuristic
-    to make sure that different MXs do get tried. With a normal kind of retry
-    rule, they would get tried anyway when the earlier hosts were delayed, but
-    if the domain has a "retry every time" type of rule - as is often used for
-    the the very large ISPs, that won't happen. */
+    is timed out, unless hosts_max_try_hardlimit (which protects against
+    lunatic DNS configurations) is reached.
+
+    If the host is not expired and we are about to hit the hosts_max_retry
+    limit, check to see if there is a subsequent hosts with a different MX
+    value. If so, make that the next host, and don't count this one. This is a
+    heuristic to make sure that different MXs do get tried. With a normal kind
+    of retry rule, they would get tried anyway when the earlier hosts were
+    delayed, but if the domain has a "retry every time" type of rule - as is
+    often used for the the very large ISPs, that won't happen. */
 
     else
       {
@@ -2441,6 +2493,7 @@ for (cutoff_retry = 0; expired &&
 
       /* Attempt the delivery. */
 
+      total_hosts_tried++;
       rc = smtp_deliver(addrlist, host, host_af, port, interface, tblock,
         expanded_hosts != NULL, &message_defer, FALSE);
 
@@ -2606,7 +2659,7 @@ for (cutoff_retry = 0; expired &&
     maximum retry time for this host. This means we may try try all hosts,
     ignoring the limit, when messages have been around for some time. This is
     important because if we don't try all hosts, the address will never time
-    out. */
+    out. NOTE: this does not apply to hosts_max_try_hardlimit. */
 
     if ((rc == DEFER || some_deferred) && nexthost != NULL)
       {
@@ -2671,15 +2724,26 @@ found, we end up here, but can detect these cases and handle them specially. */
 for (addr = addrlist; addr != NULL; addr = addr->next)
   {
   /* If host is not NULL, it means that we stopped processing the host list
-  because of hosts_max_try. This means we need to behave as if some hosts were
-  skipped because their retry time had not come. Specifically, this prevents
-  the address from timing out. */
+  because of hosts_max_try or hosts_max_try_hardlimit. In the former case, this
+  means we need to behave as if some hosts were skipped because their retry
+  time had not come. Specifically, this prevents the address from timing out.
+  However, if we have hit hosts_max_try_hardlimit, we want to behave as if all
+  hosts were tried. */
 
   if (host != NULL)
     {
-    DEBUG(D_transport)
-      debug_printf("hosts_max_try limit caused some hosts to be skipped\n");
-    setflag(addr, af_retry_skipped);
+    if (total_hosts_tried >= ob->hosts_max_try_hardlimit)
+      {
+      DEBUG(D_transport)
+        debug_printf("hosts_max_try_hardlimit reached: behave as if all "
+          "hosts were tried\n");
+      }
+    else
+      {
+      DEBUG(D_transport)
+        debug_printf("hosts_max_try limit caused some hosts to be skipped\n");
+      setflag(addr, af_retry_skipped);
+      }
     }
 
   if (queue_smtp)    /* no deliveries attempted */