~ubuntu-branches/ubuntu/hardy/exim4/hardy-proposed

« back to all changes in this revision

Viewing changes to src/transports/smtp.c

  • Committer: Bazaar Package Importer
  • Author(s): Marc Haber
  • Date: 2005-07-02 06:08:34 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050702060834-qk17pd52kb9nt3bj
Tags: 4.52-1
* new upstream version 4.51. (mh)
  * adapt 70_remove_exim-users_references
  * remove 37_gnutlsparams
  * adapt 36_pcre
  * adapt 31_eximmanpage
* fix package priorities to have them in sync with override again. (mh)
* Fix error in nb (Norwegian) translation.
  Thanks to Helge Hafting. (mh). Closes: #315775
* Standards-Version: 3.6.2, no changes needed. (mh)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Cambridge: exim/exim-src/src/transports/smtp.c,v 1.13 2005/06/29 14:17:01 ph10 Exp $ */
 
2
 
1
3
/*************************************************
2
4
*     Exim - an Internet mail transport agent    *
3
5
*************************************************/
4
6
 
5
 
/* Copyright (c) University of Cambridge 1995 - 2004 */
 
7
/* Copyright (c) University of Cambridge 1995 - 2005 */
6
8
/* See the file NOTICE for conditions of use and distribution. */
7
9
 
8
10
#include "../exim.h"
33
35
      (void *)offsetof(smtp_transport_options_block, data_timeout) },
34
36
  { "delay_after_cutoff", opt_bool,
35
37
      (void *)offsetof(smtp_transport_options_block, delay_after_cutoff) },
 
38
#ifdef EXPERIMENTAL_DOMAINKEYS
 
39
  { "dk_canon", opt_stringptr,
 
40
      (void *)offsetof(smtp_transport_options_block, dk_canon) },
 
41
  { "dk_domain", opt_stringptr,
 
42
      (void *)offsetof(smtp_transport_options_block, dk_domain) },
 
43
  { "dk_headers", opt_stringptr,
 
44
      (void *)offsetof(smtp_transport_options_block, dk_headers) },
 
45
  { "dk_private_key", opt_stringptr,
 
46
      (void *)offsetof(smtp_transport_options_block, dk_private_key) },
 
47
  { "dk_selector", opt_stringptr,
 
48
      (void *)offsetof(smtp_transport_options_block, dk_selector) },
 
49
  { "dk_strict", opt_stringptr,
 
50
      (void *)offsetof(smtp_transport_options_block, dk_strict) },
 
51
#endif
36
52
  { "dns_qualify_single",   opt_bool,
37
53
      (void *)offsetof(smtp_transport_options_block, dns_qualify_single) },
38
54
  { "dns_search_parents",   opt_bool,
55
71
  #endif
56
72
  { "hosts_max_try",        opt_int,
57
73
      (void *)offsetof(smtp_transport_options_block, hosts_max_try) },
 
74
  { "hosts_max_try_hardlimit", opt_int,
 
75
      (void *)offsetof(smtp_transport_options_block, hosts_max_try_hardlimit) },
58
76
  #ifdef SUPPORT_TLS
59
77
  { "hosts_nopass_tls",     opt_stringptr,
60
78
      (void *)offsetof(smtp_transport_options_block, hosts_nopass_tls) },
136
154
  10*60,               /* final timeout */
137
155
  1024,                /* size_addition */
138
156
  5,                   /* hosts_max_try */
 
157
  50,                  /* hosts_max_try_hardlimit */
139
158
  FALSE,               /* allow_localhost */
140
159
  FALSE,               /* gethostbyname */
141
160
  TRUE,                /* dns_qualify_single */
153
172
  NULL,                /* tls_verify_certificates */
154
173
  TRUE                 /* tls_tempfail_tryclear */
155
174
  #endif
 
175
  #ifdef EXPERIMENTAL_DOMAINKEYS
 
176
 ,NULL,                /* dk_canon */
 
177
  NULL,                /* dk_domain */
 
178
  NULL,                /* dk_headers */
 
179
  NULL,                /* dk_private_key */
 
180
  NULL,                /* dk_selector */
 
181
  NULL                 /* dk_strict */
 
182
  #endif
156
183
};
157
184
 
158
185
 
286
313
status means that an address is not currently being processed.
287
314
 
288
315
Arguments:
289
 
  addrlist     points to a chain of addresses
290
 
  errno_value  to put in each address's errno field
291
 
  msg          to put in each address's message field
292
 
  rc           to put in each address's transport_return field
 
316
  addrlist       points to a chain of addresses
 
317
  errno_value    to put in each address's errno field
 
318
  msg            to put in each address's message field
 
319
  rc             to put in each address's transport_return field
 
320
  pass_message   if TRUE, set the "pass message" flag in the address
293
321
 
294
322
If errno_value has the special value ERRNO_CONNECTTIMEOUT, ETIMEDOUT is put in
295
323
the errno field, and RTEF_CTOUT is ORed into the more_errno field, to indicate
299
327
*/
300
328
 
301
329
static
302
 
void set_errno(address_item *addrlist, int errno_value, uschar *msg, int rc)
 
330
void set_errno(address_item *addrlist, int errno_value, uschar *msg, int rc,
 
331
  BOOL pass_message)
303
332
{
304
333
address_item *addr;
305
334
int orvalue = 0;
313
342
  if (addr->transport_return < PENDING) continue;
314
343
  addr->basic_errno = errno_value;
315
344
  addr->more_errno |= orvalue;
316
 
  if (msg != NULL) addr->message = msg;
 
345
  if (msg != NULL)
 
346
    {
 
347
    addr->message = msg;
 
348
    if (pass_message) setflag(addr, af_pass_message);
 
349
    }
317
350
  addr->transport_return = rc;
318
351
  }
319
352
}
331
364
chosen.
332
365
 
333
366
Arguments:
334
 
  host         the current host, to get its name for messages
335
 
  errno_value  pointer to the errno value
336
 
  more_errno   from the top address for use with ERRNO_FILTER_FAIL
337
 
  buffer       the SMTP response buffer
338
 
  yield        where to put a one-digit SMTP response code
339
 
  message      where to put an errror message
 
367
  host           the current host, to get its name for messages
 
368
  errno_value    pointer to the errno value
 
369
  more_errno     from the top address for use with ERRNO_FILTER_FAIL
 
370
  buffer         the SMTP response buffer
 
371
  yield          where to put a one-digit SMTP response code
 
372
  message        where to put an errror message
 
373
  pass_message   set TRUE if message is an SMTP response
340
374
 
341
 
Returns:       TRUE if an SMTP "QUIT" command should be sent, else FALSE
 
375
Returns:         TRUE if an SMTP "QUIT" command should be sent, else FALSE
342
376
*/
343
377
 
344
378
static BOOL check_response(host_item *host, int *errno_value, int more_errno,
345
 
  uschar *buffer, int *yield, uschar **message)
 
379
  uschar *buffer, int *yield, uschar **message, BOOL *pass_message)
346
380
{
347
381
uschar *pl = US"";
348
382
 
387
421
 
388
422
if (*errno_value == ERRNO_FILTER_FAIL)
389
423
  {
390
 
  *message = US string_sprintf("transport filter process failed (%d)", more_errno);
 
424
  *message = US string_sprintf("transport filter process failed (%d)%s",
 
425
    more_errno,
 
426
    (more_errno == EX_EXECFAILED)? ": unable to execute command" : "");
391
427
  return FALSE;
392
428
  }
393
429
 
415
451
if (buffer[0] != 0)
416
452
  {
417
453
  uschar *s = string_printing(buffer);
418
 
  *message = US string_sprintf("SMTP error from remote mailer after %s%s: "
 
454
  *message = US string_sprintf("SMTP error from remote mail server after %s%s: "
419
455
    "host %s [%s]: %s", pl, smtp_command, host->name, host->address, s);
 
456
  *pass_message = TRUE;
420
457
  *yield = buffer[0];
421
458
  return TRUE;
422
459
  }
594
631
    uschar *message = string_sprintf("SMTP timeout while connected to %s [%s] "
595
632
      "after RCPT TO:<%s>", host->name, host->address,
596
633
      transport_rcpt_address(addr, include_affixes));
597
 
    set_errno(addrlist, save_errno, message, DEFER);
 
634
    set_errno(addrlist, save_errno, message, DEFER, FALSE);
598
635
    retry_add_item(addr, addr->address_retry_key, 0);
599
636
    host->update_waiting = FALSE;
600
637
    return -1;
617
654
  else
618
655
    {
619
656
    addr->message =
620
 
      string_sprintf("SMTP error from remote mailer after RCPT TO:<%s>: "
 
657
      string_sprintf("SMTP error from remote mail server after RCPT TO:<%s>: "
621
658
        "host %s [%s]: %s", transport_rcpt_address(addr, include_affixes),
622
659
        host->name, host->address, string_printing(buffer));
 
660
    setflag(addr, af_pass_message);
623
661
    deliver_msglog("%s %s\n", tod_stamp(tod_log), addr->message);
624
662
 
625
663
    /* The response was 5xx */
634
672
 
635
673
    else
636
674
      {
 
675
      int bincode = (buffer[1] - '0')*10 + buffer[2] - '0';
 
676
 
 
677
      addr->transport_return = DEFER;
 
678
      addr->basic_errno = ERRNO_RCPT4XX;
 
679
      addr->more_errno |= bincode << 8;
 
680
 
637
681
      /* Log temporary errors if there are more hosts to be tried. */
 
682
 
638
683
      if (host->next != NULL) log_write(0, LOG_MAIN, "%s", addr->message);
639
 
      addr->transport_return = DEFER;
640
684
 
641
685
      /* Do not put this message on the list of those waiting for this host,
642
686
      as otherwise it is likely to be tried too often. */
664
708
  {
665
709
  int code;
666
710
  uschar *msg;
 
711
  BOOL pass_message;
667
712
  if (pending_DATA > 0 || (yield & 1) != 0) return -3;
668
 
  (void)check_response(host, &errno, 0, buffer, &code, &msg);
 
713
  (void)check_response(host, &errno, 0, buffer, &code, &msg, &pass_message);
669
714
  DEBUG(D_transport) debug_printf("%s\nerror for DATA ignored: pipelining "
670
715
    "is in use and there were no good recipients\n", msg);
671
716
  }
690
735
Otherwise, we have to make a connection to the remote host, and do the
691
736
initial protocol exchange.
692
737
 
 
738
When running as an MUA wrapper, if the sender or any recipient is rejected,
 
739
temporarily or permanently, we force failure for all recipients.
 
740
 
693
741
Arguments:
694
742
  addrlist        chain of potential addresses to deliver; only those whose
695
743
                  transport_return field is set to PENDING_DEFER are currently
744
792
BOOL completed_address = FALSE;
745
793
BOOL esmtp = TRUE;
746
794
BOOL pending_MAIL;
 
795
BOOL pass_message = FALSE;
747
796
smtp_inblock inblock;
748
797
smtp_outblock outblock;
749
798
int max_rcpt = tblock->max_addresses;
784
833
  {
785
834
  uschar *message = string_sprintf("failed to expand helo_data: %s",
786
835
    expand_string_message);
787
 
  set_errno(addrlist, 0, message, DEFER);
 
836
  set_errno(addrlist, 0, message, DEFER, FALSE);
788
837
  return ERROR;
789
838
  }
790
839
 
804
853
      {
805
854
      uschar *message = string_sprintf("failed to expand "
806
855
        "authenticated_sender: %s", expand_string_message);
807
 
      set_errno(addrlist, 0, message, DEFER);
 
856
      set_errno(addrlist, 0, message, DEFER, FALSE);
808
857
      return ERROR;
809
858
      }
810
859
    }
823
872
  if (inblock.sock < 0)
824
873
    {
825
874
    set_errno(addrlist, (errno == ETIMEDOUT)? ERRNO_CONNECTTIMEOUT : errno,
826
 
      NULL, DEFER);
 
875
      NULL, DEFER, FALSE);
827
876
    return DEFER;
828
877
    }
829
878
 
874
923
 
875
924
  if (esmtp)
876
925
    {
877
 
    if (!smtp_write_command(&outblock, FALSE, "%s %s\r\n",
878
 
         lmtp? "LHLO" : "EHLO", helo_data))
 
926
    if (smtp_write_command(&outblock, FALSE, "%s %s\r\n",
 
927
         lmtp? "LHLO" : "EHLO", helo_data) < 0)
879
928
      goto SEND_FAILED;
880
929
    if (!smtp_read_response(&inblock, buffer, sizeof(buffer), '2',
881
930
           ob->command_timeout))
892
941
 
893
942
  if (!esmtp)
894
943
    {
895
 
    if (!smtp_write_command(&outblock, FALSE, "HELO %s\r\n", helo_data))
 
944
    if (smtp_write_command(&outblock, FALSE, "HELO %s\r\n", helo_data) < 0)
896
945
      goto SEND_FAILED;
897
946
    if (!smtp_read_response(&inblock, buffer, sizeof(buffer), '2',
898
947
      ob->command_timeout)) goto RESPONSE_FAILED;
934
983
        host->address, NULL) != OK)
935
984
  {
936
985
  uschar buffer2[4096];
937
 
  if (!smtp_write_command(&outblock, FALSE, "STARTTLS\r\n")) goto SEND_FAILED;
 
986
  if (smtp_write_command(&outblock, FALSE, "STARTTLS\r\n") < 0)
 
987
    goto SEND_FAILED;
938
988
 
939
989
  /* If there is an I/O error, transmission of this message is deferred. If
940
990
  there is a temporary rejection of STARRTLS and tls_tempfail_tryclear is
981
1031
 
982
1032
    for (addr = addrlist; addr != NULL; addr = addr->next)
983
1033
      {
984
 
      addr->cipher = tls_cipher;
985
 
      addr->peerdn = tls_peerdn;
 
1034
      if (addr->transport_return == PENDING_DEFER)
 
1035
        {
 
1036
        addr->cipher = tls_cipher;
 
1037
        addr->peerdn = tls_peerdn;
 
1038
        }
986
1039
      }
987
1040
    }
988
1041
  }
991
1044
 
992
1045
if (tls_active >= 0)
993
1046
  {
994
 
  if (!smtp_write_command(&outblock, FALSE, "%s %s\r\n", lmtp? "LHLO" : "EHLO",
995
 
        helo_data))
 
1047
  if (smtp_write_command(&outblock, FALSE, "%s %s\r\n", lmtp? "LHLO" : "EHLO",
 
1048
        helo_data) < 0)
996
1049
    goto SEND_FAILED;
997
1050
  if (!smtp_read_response(&inblock, buffer, sizeof(buffer), '2',
998
1051
       ob->command_timeout))
1144
1197
 
1145
1198
            case ERROR:
1146
1199
            yield = ERROR;
1147
 
            set_errno(addrlist, 0, string_copy(buffer), DEFER);
 
1200
            set_errno(addrlist, 0, string_copy(buffer), DEFER, FALSE);
1148
1201
            goto SEND_QUIT;
1149
1202
            }
1150
1203
 
1160
1213
    {
1161
1214
    yield = DEFER;
1162
1215
    set_errno(addrlist, ERRNO_AUTHFAIL,
1163
 
      string_sprintf("authentication required but %s", fail_reason), DEFER);
 
1216
      string_sprintf("authentication required but %s", fail_reason), DEFER,
 
1217
      FALSE);
1164
1218
    goto SEND_QUIT;
1165
1219
    }
1166
1220
  }
1186
1240
 
1187
1241
  if (!rc)
1188
1242
    {
1189
 
    set_errno(addrlist->next, addrlist->basic_errno, addrlist->message, DEFER);
 
1243
    set_errno(addrlist->next, addrlist->basic_errno, addrlist->message, DEFER,
 
1244
      FALSE);
1190
1245
    yield = ERROR;
1191
1246
    goto SEND_QUIT;
1192
1247
    }
1263
1318
several before we have to read the responses for those seen so far. This
1264
1319
checking is done by a subroutine because it also needs to be done at the end.
1265
1320
Send only up to max_rcpt addresses at a time, leaving first_addr pointing to
1266
 
the next one if not all are sent. */
 
1321
the next one if not all are sent.
 
1322
 
 
1323
In the MUA wrapper situation, we want to flush the PIPELINING buffer for the
 
1324
last address because we want to abort if any recipients have any kind of
 
1325
problem, temporary or permanent. We know that all recipient addresses will have
 
1326
the PENDING_DEFER status, because only one attempt is ever made, and we know
 
1327
that max_rcpt will be large, so all addresses will be done at once. */
1267
1328
 
1268
1329
for (addr = first_addr;
1269
1330
     address_count < max_rcpt && addr != NULL;
1270
1331
     addr = addr->next)
1271
1332
  {
1272
1333
  int count;
 
1334
  BOOL no_flush;
1273
1335
 
1274
1336
  if (addr->transport_return != PENDING_DEFER) continue;
1275
1337
 
1276
1338
  address_count++;
 
1339
  no_flush = smtp_use_pipelining && (!mua_wrapper || addr->next != NULL);
1277
1340
 
1278
1341
  /* Now send the RCPT command, and process outstanding responses when
1279
1342
  necessary. After a timeout on RCPT, we just end the function, leaving the
1280
1343
  yield as OK, because this error can often mean that there is a problem with
1281
1344
  just one address, so we don't want to delay the host. */
1282
1345
 
1283
 
  count = smtp_write_command(&outblock, smtp_use_pipelining,
1284
 
    "RCPT TO:<%s>\r\n",
 
1346
  count = smtp_write_command(&outblock, no_flush, "RCPT TO:<%s>\r\n",
1285
1347
    transport_rcpt_address(addr, tblock->rcpt_include_affixes));
1286
1348
  if (count < 0) goto SEND_FAILED;
1287
1349
  if (count > 0)
1306
1368
    }
1307
1369
  }      /* Loop for next address */
1308
1370
 
 
1371
/* If we are an MUA wrapper, abort if any RCPTs were rejected, either
 
1372
permanently or temporarily. We should have flushed and synced after the last
 
1373
RCPT. */
 
1374
 
 
1375
if (mua_wrapper)
 
1376
  {
 
1377
  address_item *badaddr;
 
1378
  for (badaddr = first_addr; badaddr != NULL; badaddr = badaddr->next)
 
1379
    {
 
1380
    if (badaddr->transport_return != PENDING_OK) break;
 
1381
    }
 
1382
  if (badaddr != NULL)
 
1383
    {
 
1384
    set_errno(addrlist, 0, badaddr->message, FAIL,
 
1385
      testflag(badaddr, af_pass_message));
 
1386
    ok = FALSE;
 
1387
    }
 
1388
  }
 
1389
 
1309
1390
/* If ok is TRUE, we know we have got at least one good recipient, and must now
1310
 
send DATA, but if it is FALSE, we may still have a good recipient buffered up
1311
 
if we are pipelining. We don't want to waste time sending DATA needlessly, so
1312
 
we only send it if either ok is TRUE or if we are pipelining. The responses are
1313
 
all handled by sync_responses(). */
 
1391
send DATA, but if it is FALSE (in the normal, non-wrapper case), we may still
 
1392
have a good recipient buffered up if we are pipelining. We don't want to waste
 
1393
time sending DATA needlessly, so we only send it if either ok is TRUE or if we
 
1394
are pipelining. The responses are all handled by sync_responses(). */
1314
1395
 
1315
 
if (ok || smtp_use_pipelining)
 
1396
if (ok || (smtp_use_pipelining && !mua_wrapper))
1316
1397
  {
1317
1398
  int count = smtp_write_command(&outblock, FALSE, "DATA\r\n");
1318
1399
  if (count < 0) goto SEND_FAILED;
1352
1433
  DEBUG(D_transport|D_v)
1353
1434
    debug_printf("  SMTP>> writing message and terminating \".\"\n");
1354
1435
  transport_count = 0;
 
1436
#ifdef EXPERIMENTAL_DOMAINKEYS
 
1437
  if ( (ob->dk_private_key != NULL) && (ob->dk_selector != NULL) )
 
1438
    ok = dk_transport_write_message(addrlist, inblock.sock,
 
1439
      topt_use_crlf | topt_end_dot | topt_escape_headers |
 
1440
        (tblock->body_only? topt_no_headers : 0) |
 
1441
        (tblock->headers_only? topt_no_body : 0) |
 
1442
        (tblock->return_path_add? topt_add_return_path : 0) |
 
1443
        (tblock->delivery_date_add? topt_add_delivery_date : 0) |
 
1444
        (tblock->envelope_to_add? topt_add_envelope_to : 0),
 
1445
      0,            /* No size limit */
 
1446
      tblock->add_headers, tblock->remove_headers,
 
1447
      US".", US"..",    /* Escaping strings */
 
1448
      tblock->rewrite_rules, tblock->rewrite_existflags,
 
1449
      ob->dk_private_key, ob->dk_domain, ob->dk_selector,
 
1450
      ob->dk_canon, ob->dk_headers, ob->dk_strict);
 
1451
  else
 
1452
#endif
1355
1453
  ok = transport_write_message(addrlist, inblock.sock,
1356
1454
    topt_use_crlf | topt_end_dot | topt_escape_headers |
1357
1455
      (tblock->body_only? topt_no_headers : 0) |
1412
1510
    int delivery_time = (int)(time(NULL) - start_delivery_time);
1413
1511
    int len;
1414
1512
    host_item *thost;
1415
 
 
1416
 
 
1417
1513
    uschar *conf = NULL;
1418
1514
    send_rset = FALSE;
1419
1515
 
1515
1611
  save_errno = errno;
1516
1612
  message = NULL;
1517
1613
  send_quit = check_response(host, &save_errno, addrlist->more_errno,
1518
 
    buffer, &code, &message);
 
1614
    buffer, &code, &message, &pass_message);
1519
1615
  goto FAILED;
1520
1616
 
1521
1617
  SEND_FAILED:
1522
1618
  save_errno = errno;
1523
1619
  code = '4';
1524
 
  message = US string_sprintf("send() to %s [%s] failed",
 
1620
  message = US string_sprintf("send() to %s [%s] failed: %s",
1525
1621
    host->name, host->address, strerror(save_errno));
1526
1622
  send_quit = FALSE;
1527
1623
  goto FAILED;
1550
1646
    {
1551
1647
    if (code == '5')
1552
1648
      {
1553
 
      set_errno(addrlist, save_errno, message, FAIL);
 
1649
      set_errno(addrlist, save_errno, message, FAIL, pass_message);
1554
1650
      }
1555
1651
    else
1556
1652
      {
1557
 
      set_errno(addrlist, save_errno, message, DEFER);
 
1653
      set_errno(addrlist, save_errno, message, DEFER, pass_message);
1558
1654
      yield = DEFER;
1559
1655
      }
1560
1656
    }
1580
1676
    {
1581
1677
    yield = (save_errno == ERRNO_CHHEADER_FAIL ||
1582
1678
             save_errno == ERRNO_FILTER_FAIL)? ERROR : DEFER;
1583
 
    set_errno(addrlist, save_errno, message, DEFER);
 
1679
    set_errno(addrlist, save_errno, message, DEFER, pass_message);
1584
1680
    }
1585
1681
 
1586
1682
  /* Otherwise we have a message-specific error response from the remote
1599
1695
 
1600
1696
  else
1601
1697
    {
1602
 
    set_errno(addrlist, save_errno, message, (code == '5')? FAIL : DEFER);
 
1698
    if (mua_wrapper) code = '5';  /* Force hard failure in wrapper mode */
 
1699
 
 
1700
    set_errno(addrlist, save_errno, message, (code == '5')? FAIL : DEFER,
 
1701
      pass_message);
1603
1702
 
1604
1703
    /* If there's an errno, the message contains just the identity of
1605
1704
    the host. */
1646
1745
 
1647
1746
DEBUG(D_transport)
1648
1747
  debug_printf("ok=%d send_quit=%d send_rset=%d continue_more=%d "
1649
 
    "yield=%d first_address=%d\n", ok, send_quit, send_rset, continue_more,
1650
 
    yield, (int)first_addr);
 
1748
    "yield=%d first_address is %sNULL\n", ok, send_quit, send_rset,
 
1749
    continue_more, yield, (first_addr == NULL)? "":"not ");
1651
1750
 
1652
1751
if (completed_address && ok && send_quit)
1653
1752
  {
1663
1762
        ))
1664
1763
    {
1665
1764
    uschar *msg;
 
1765
    BOOL pass_message;
1666
1766
 
1667
1767
    if (send_rset)
1668
1768
      {
1669
 
      if (! (ok = smtp_write_command(&outblock, FALSE, "RSET\r\n")))
 
1769
      if (! (ok = smtp_write_command(&outblock, FALSE, "RSET\r\n") >= 0))
1670
1770
        {
1671
 
        msg = US string_sprintf("send() to %s [%s] failed", host->name,
 
1771
        msg = US string_sprintf("send() to %s [%s] failed: %s", host->name,
1672
1772
          host->address, strerror(save_errno));
1673
1773
        send_quit = FALSE;
1674
1774
        }
1676
1776
                  ob->command_timeout)))
1677
1777
        {
1678
1778
        int code;
1679
 
        send_quit = check_response(host, &errno, 0, buffer, &code, &msg);
 
1779
        send_quit = check_response(host, &errno, 0, buffer, &code, &msg,
 
1780
          &pass_message);
1680
1781
        if (!send_quit)
1681
1782
          {
1682
1783
          DEBUG(D_transport) debug_printf("%s\n", msg);
1704
1805
      if (tls_active >= 0)
1705
1806
        {
1706
1807
        tls_close(TRUE);
1707
 
        ok = smtp_write_command(&outblock, FALSE, "EHLO %s\r\n", helo_data) &&
 
1808
        ok = smtp_write_command(&outblock,FALSE,"EHLO %s\r\n",helo_data) >= 0 &&
1708
1809
             smtp_read_response(&inblock, buffer, sizeof(buffer), '2',
1709
1810
               ob->command_timeout);
1710
1811
        }
1722
1823
 
1723
1824
    /* If RSET failed and there are addresses left, they get deferred. */
1724
1825
 
1725
 
    else set_errno(first_addr, errno, msg, DEFER);
 
1826
    else set_errno(first_addr, errno, msg, DEFER, FALSE);
1726
1827
    }
1727
1828
  }
1728
1829
 
1745
1846
operation, the old commented-out code was removed on 17-Sep-99. */
1746
1847
 
1747
1848
SEND_QUIT:
1748
 
if (send_quit) (void) smtp_write_command(&outblock, FALSE, "QUIT\r\n");
 
1849
if (send_quit) (void)smtp_write_command(&outblock, FALSE, "QUIT\r\n");
1749
1850
 
1750
1851
END_OFF:
1751
1852
 
1765
1866
specified in the transports, and therefore not visible at top level, in which
1766
1867
case continue_more won't get set. */
1767
1868
 
1768
 
close(inblock.sock);
 
1869
(void)close(inblock.sock);
1769
1870
continue_transport = NULL;
1770
1871
continue_hostname = NULL;
1771
1872
return yield;
1815
1916
outblock.cmd_count = 0;
1816
1917
outblock.authenticating = FALSE;
1817
1918
 
1818
 
smtp_write_command(&outblock, FALSE, "QUIT\r\n");
1819
 
(void) smtp_read_response(&inblock, buffer, sizeof(buffer), '2',
 
1919
(void)smtp_write_command(&outblock, FALSE, "QUIT\r\n");
 
1920
(void)smtp_read_response(&inblock, buffer, sizeof(buffer), '2',
1820
1921
  ob->command_timeout);
1821
 
close(inblock.sock);
 
1922
(void)close(inblock.sock);
1822
1923
}
1823
1924
 
1824
1925
 
1887
1988
int hosts_retry = 0;
1888
1989
int hosts_serial = 0;
1889
1990
int hosts_total = 0;
 
1991
int total_hosts_tried = 0;
1890
1992
address_item *addr;
1891
1993
BOOL expired = TRUE;
1892
1994
BOOL continuing = continue_hostname != NULL;
2052
2154
 
2053
2155
.  If there are any addresses whose status is still DEFER, carry on to the
2054
2156
   next host/IPaddress, unless we have tried the number of hosts given
2055
 
   by hosts_max_try; otherwise return.
 
2157
   by hosts_max_try or hosts_max_try_hardlimit; otherwise return. Note that
 
2158
   there is some fancy logic for hosts_max_try that means its limit can be
 
2159
   overstepped in some circumstances.
2056
2160
 
2057
2161
If we get to the end of the list, all hosts have deferred at least one address,
2058
2162
or not reached their retry times. If delay_after_cutoff is unset, it requests a
2069
2173
  int unexpired_hosts_tried = 0;
2070
2174
 
2071
2175
  for (host = hostlist;
2072
 
       host != NULL && unexpired_hosts_tried < ob->hosts_max_try;
 
2176
       host != NULL &&
 
2177
         unexpired_hosts_tried < ob->hosts_max_try &&
 
2178
         total_hosts_tried < ob->hosts_max_try_hardlimit;
2073
2179
       host = nexthost)
2074
2180
    {
2075
2181
    int rc;
2087
2193
    uschar *serialize_key = NULL;
2088
2194
 
2089
2195
    /* Default next host is next host. :-) But this can vary if the
2090
 
    hosts_max_try limit is hit (see below). */
 
2196
    hosts_max_try limit is hit (see below). It may also be reset if a host
 
2197
    address is looked up here (in case the host was multihomed). */
2091
2198
 
2092
2199
    nexthost = host->next;
2093
2200
 
2130
2237
      /* Find by name if so configured, or if it's an IP address. We don't
2131
2238
      just copy the IP address, because we need the test-for-local to happen. */
2132
2239
 
2133
 
      if (ob->gethostbyname || string_is_ip_address(host->name, NULL))
 
2240
      if (ob->gethostbyname || string_is_ip_address(host->name, NULL) > 0)
2134
2241
        rc = host_find_byname(host, NULL, &canonical_name, TRUE);
2135
2242
      else
2136
 
        rc = host_find_bydns(host, NULL, HOST_FIND_BY_A, NULL,
2137
 
          ob->dns_qualify_single, ob->dns_search_parents,
 
2243
        {
 
2244
        int flags = HOST_FIND_BY_A;
 
2245
        if (ob->dns_qualify_single) flags |= HOST_FIND_QUALIFY_SINGLE;
 
2246
        if (ob->dns_search_parents) flags |= HOST_FIND_SEARCH_PARENTS;
 
2247
        rc = host_find_bydns(host, NULL, flags, NULL, NULL, NULL,
2138
2248
          &canonical_name, NULL);
 
2249
        }
2139
2250
 
2140
2251
      /* Failure to find the host at this time (usually DNS temporary failure)
2141
2252
      is really a kind of routing failure rather than a transport failure.
2193
2304
      continue;      /* With next host */
2194
2305
      }
2195
2306
 
 
2307
    /* Reset the default next host in case a multihomed host whose addresses
 
2308
    are not looked up till just above added to the host list. */
 
2309
 
 
2310
    nexthost = host->next;
 
2311
 
2196
2312
    /* If queue_smtp is set (-odqs or the first part of a 2-stage run), or the
2197
2313
    domain is in queue_smtp_domains, we don't actually want to attempt any
2198
2314
    deliveries. When doing a queue run, queue_smtp_domains is always unset. If
2350
2466
    if (dont_deliver)
2351
2467
      {
2352
2468
      host_item *host2;
2353
 
      set_errno(addrlist, 0, NULL, OK);
 
2469
      set_errno(addrlist, 0, NULL, OK, FALSE);
2354
2470
      for (addr = addrlist; addr != NULL; addr = addr->next)
2355
2471
        {
2356
2472
        addr->host_used = host;
2370
2486
 
2371
2487
    /* This is for real. If the host is expired, we don't count it for
2372
2488
    hosts_max_retry. This ensures that all hosts must expire before an address
2373
 
    is timed out. Otherwise, if we are about to hit the hosts_max_retry limit,
2374
 
    check to see if there is a subsequent hosts with a different MX value. If
2375
 
    so, make that the next host, and don't count this one. This is a heuristic
2376
 
    to make sure that different MXs do get tried. With a normal kind of retry
2377
 
    rule, they would get tried anyway when the earlier hosts were delayed, but
2378
 
    if the domain has a "retry every time" type of rule - as is often used for
2379
 
    the the very large ISPs, that won't happen. */
 
2489
    is timed out, unless hosts_max_try_hardlimit (which protects against
 
2490
    lunatic DNS configurations) is reached.
 
2491
 
 
2492
    If the host is not expired and we are about to hit the hosts_max_retry
 
2493
    limit, check to see if there is a subsequent hosts with a different MX
 
2494
    value. If so, make that the next host, and don't count this one. This is a
 
2495
    heuristic to make sure that different MXs do get tried. With a normal kind
 
2496
    of retry rule, they would get tried anyway when the earlier hosts were
 
2497
    delayed, but if the domain has a "retry every time" type of rule - as is
 
2498
    often used for the the very large ISPs, that won't happen. */
2380
2499
 
2381
2500
    else
2382
2501
      {
2398
2517
 
2399
2518
      /* Attempt the delivery. */
2400
2519
 
 
2520
      total_hosts_tried++;
2401
2521
      rc = smtp_deliver(addrlist, host, host_af, port, interface, tblock,
2402
2522
        expanded_hosts != NULL, &message_defer, FALSE);
2403
2523
 
2563
2683
    maximum retry time for this host. This means we may try try all hosts,
2564
2684
    ignoring the limit, when messages have been around for some time. This is
2565
2685
    important because if we don't try all hosts, the address will never time
2566
 
    out. */
 
2686
    out. NOTE: this does not apply to hosts_max_try_hardlimit. */
2567
2687
 
2568
2688
    if ((rc == DEFER || some_deferred) && nexthost != NULL)
2569
2689
      {
2602
2722
  }
2603
2723
 
2604
2724
 
2605
 
/* Get here if all IP addresses are skipped or defer at least one address. Add
2606
 
a standard message to each deferred address if there hasn't been an error, that
2607
 
is, if it hasn't actually been tried this time. The variable "expired" will be
2608
 
FALSE if any deliveries were actually tried, or if there was at least one host
2609
 
that was not expired. That is, it is TRUE only if no deliveries were tried and
2610
 
all hosts were expired. If a delivery has been tried, an error code will be
2611
 
set, and the failing of the message is handled by the retry code later.
 
2725
/* Get here if all IP addresses are skipped or defer at least one address. In
 
2726
MUA wrapper mode, this will happen only for connection or other non-message-
 
2727
specific failures. Force the delivery status for all addresses to FAIL. */
 
2728
 
 
2729
if (mua_wrapper)
 
2730
  {
 
2731
  for (addr = addrlist; addr != NULL; addr = addr->next)
 
2732
    addr->transport_return = FAIL;
 
2733
  goto END_TRANSPORT;
 
2734
  }
 
2735
 
 
2736
/* In the normal, non-wrapper case, add a standard message to each deferred
 
2737
address if there hasn't been an error, that is, if it hasn't actually been
 
2738
tried this time. The variable "expired" will be FALSE if any deliveries were
 
2739
actually tried, or if there was at least one host that was not expired. That
 
2740
is, it is TRUE only if no deliveries were tried and all hosts were expired. If
 
2741
a delivery has been tried, an error code will be set, and the failing of the
 
2742
message is handled by the retry code later.
2612
2743
 
2613
2744
If queue_smtp is set, or this transport was called to send a subsequent message
2614
2745
down an existing TCP/IP connection, and something caused the host not to be
2617
2748
for (addr = addrlist; addr != NULL; addr = addr->next)
2618
2749
  {
2619
2750
  /* If host is not NULL, it means that we stopped processing the host list
2620
 
  because of hosts_max_try. This means we need to behave as if some hosts were
2621
 
  skipped because their retry time had not come. Specifically, this prevents
2622
 
  the address from timing out. */
 
2751
  because of hosts_max_try or hosts_max_try_hardlimit. In the former case, this
 
2752
  means we need to behave as if some hosts were skipped because their retry
 
2753
  time had not come. Specifically, this prevents the address from timing out.
 
2754
  However, if we have hit hosts_max_try_hardlimit, we want to behave as if all
 
2755
  hosts were tried. */
2623
2756
 
2624
2757
  if (host != NULL)
2625
2758
    {
2626
 
    DEBUG(D_transport)
2627
 
      debug_printf("hosts_max_try limit caused some hosts to be skipped\n");
2628
 
    setflag(addr, af_retry_skipped);
 
2759
    if (total_hosts_tried >= ob->hosts_max_try_hardlimit)
 
2760
      {
 
2761
      DEBUG(D_transport)
 
2762
        debug_printf("hosts_max_try_hardlimit reached: behave as if all "
 
2763
          "hosts were tried\n");
 
2764
      }
 
2765
    else
 
2766
      {
 
2767
      DEBUG(D_transport)
 
2768
        debug_printf("hosts_max_try limit caused some hosts to be skipped\n");
 
2769
      setflag(addr, af_retry_skipped);
 
2770
      }
2629
2771
    }
2630
2772
 
2631
2773
  if (queue_smtp)    /* no deliveries attempted */
2646
2788
      }
2647
2789
    else if (expired)
2648
2790
      {
 
2791
      setflag(addr, af_pass_message);   /* This is not a security risk */
2649
2792
      addr->message = (ob->delay_after_cutoff)?
2650
2793
        US"retry time not reached for any host after a long failure period" :
2651
2794
        US"all hosts have been failing for a long time and were last tried "