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

« back to all changes in this revision

Viewing changes to src/deliver.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/deliver.c,v 1.21 2005/06/28 10:23:35 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
/* The main code for delivering a message. */
22
24
  int transport_count;         /* returned transport count value */
23
25
  BOOL done;                   /* no more data needed */
24
26
  uschar *msg;                 /* error message */
 
27
  uschar *return_path;         /* return_path for these addresses */
25
28
} pardata;
26
29
 
27
30
/* Values for the process_recipients variable */
70
73
static pardata *parlist = NULL;
71
74
static int  return_count;
72
75
static uschar *frozen_info = US"";
 
76
static uschar *used_return_path = NULL;
73
77
 
74
78
static uschar spoolname[PATH_MAX];
75
79
 
152
156
deliver_domain = addr->domain;
153
157
self_hostname = addr->self_hostname;
154
158
 
 
159
#ifdef EXPERIMENTAL_BRIGHTMAIL
 
160
bmi_deliver = 1;    /* deliver by default */
 
161
bmi_alt_location = NULL;
 
162
bmi_base64_verdict = NULL;
 
163
bmi_base64_tracker_verdict = NULL;
 
164
#endif
 
165
 
155
166
/* If there's only one address we can set everything. */
156
167
 
157
168
if (addr->next == NULL)
201
212
      deliver_localpart_suffix = addr->parent->suffix;
202
213
      }
203
214
    }
 
215
 
 
216
#ifdef EXPERIMENTAL_BRIGHTMAIL
 
217
    /* Set expansion variables related to Brightmail AntiSpam */
 
218
    bmi_base64_verdict = bmi_get_base64_verdict(deliver_localpart_orig, deliver_domain_orig);
 
219
    bmi_base64_tracker_verdict = bmi_get_base64_tracker_verdict(bmi_base64_verdict);
 
220
    /* get message delivery status (0 - don't deliver | 1 - deliver) */
 
221
    bmi_deliver = bmi_get_delivery_status(bmi_base64_verdict);
 
222
    /* if message is to be delivered, get eventual alternate location */
 
223
    if (bmi_deliver == 1) {
 
224
      bmi_alt_location = bmi_get_alt_location(bmi_base64_verdict);
 
225
    };
 
226
#endif
 
227
 
204
228
  }
205
229
 
206
230
/* For multiple addresses, don't set local part, and leave the domain and
262
286
 
263
287
if (fd >= 0)
264
288
  {
265
 
  fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
 
289
  (void)fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
266
290
  if (fchown(fd, exim_uid, exim_gid) < 0)
267
291
    {
268
292
    *error = US"chown";
705
729
 
706
730
/* If there's an error message set, ensure that it contains only printing
707
731
characters - it should, but occasionally things slip in and this at least
708
 
stops the log format from getting wrecked. */
 
732
stops the log format from getting wrecked. We also scan the message for an LDAP
 
733
expansion item that has a password setting, and flatten the password. This is a
 
734
fudge, but I don't know a cleaner way of doing this. (If the item is badly
 
735
malformed, it won't ever have gone near LDAP.) */
709
736
 
710
 
if (addr->message != NULL) addr->message = string_printing(addr->message);
 
737
if (addr->message != NULL)
 
738
  {
 
739
  addr->message = string_printing(addr->message);
 
740
  if (Ustrstr(addr->message, "failed to expand") != NULL &&
 
741
      (Ustrstr(addr->message, "ldap:") != NULL ||
 
742
       Ustrstr(addr->message, "ldapdn:") != NULL ||
 
743
       Ustrstr(addr->message, "ldapm:") != NULL))
 
744
    {
 
745
    uschar *p = Ustrstr(addr->message, "pass=");
 
746
    if (p != NULL)
 
747
      {
 
748
      p += 5;
 
749
      while (*p != 0 && !isspace(*p)) *p++ = 'x';
 
750
      }
 
751
    }
 
752
  }
711
753
 
712
754
/* If we used a transport that has one of the "return_output" options set, and
713
755
if it did in fact generate some output, then for return_output we treat the
756
798
          log_write(0, LOG_MAIN, "<%s>: %s transport output: %s",
757
799
            addr->address, tb->name, s);
758
800
          }
759
 
        fclose(f);
 
801
        (void)fclose(f);
760
802
        }
761
803
      }
762
804
 
787
829
    addr->return_file = -1;
788
830
    }
789
831
 
790
 
  close(addr->return_file);
 
832
  (void)close(addr->return_file);
791
833
  }
792
834
 
793
835
/* Create the address string for logging. Must not do this earlier, because
836
878
  if ((log_extra_selector & LX_sender_on_delivery) != 0)
837
879
    s = string_append(s, &size, &ptr, 3, US" F=<", sender_address, US">");
838
880
 
839
 
  if ((log_extra_selector & LX_return_path_on_delivery) != 0)
840
 
    s = string_append(s, &size, &ptr, 3, US" P=<", return_path, US">");
 
881
  #ifdef EXPERIMENTAL_SRS
 
882
  if(addr->p.srs_sender)
 
883
    s = string_append(s, &size, &ptr, 3, US" SRS=<", addr->p.srs_sender, US">");
 
884
  #endif
 
885
 
 
886
  /* You might think that the return path must always be set for a successful
 
887
  delivery; indeed, I did for some time, until this statement crashed. The case
 
888
  when it is not set is for a delivery to /dev/null which is optimised by not
 
889
  being run at all. */
 
890
 
 
891
  if (used_return_path != NULL &&
 
892
        (log_extra_selector & LX_return_path_on_delivery) != 0)
 
893
    s = string_append(s, &size, &ptr, 3, US" P=<", used_return_path, US">");
841
894
 
842
895
  /* For a delivery from a system filter, there may not be a router */
843
896
 
1036
1089
    setflag(addr, af_ignore_error);
1037
1090
 
1038
1091
  /* Freeze the message if requested, or if this is a bounce message (or other
1039
 
  message with null sender). However, don't freeze if errors are being ignored.
1040
 
  The actual code to ignore occurs later, instead of sending a message. Logging
1041
 
  of freezing occurs later, just before writing the -H file. */
 
1092
  message with null sender) and this address does not have its own errors
 
1093
  address. However, don't freeze if errors are being ignored. The actual code
 
1094
  to ignore occurs later, instead of sending a message. Logging of freezing
 
1095
  occurs later, just before writing the -H file. */
1042
1096
 
1043
1097
  if (!testflag(addr, af_ignore_error) &&
1044
 
      (addr->special_action == SPECIAL_FREEZE || sender_address[0] == 0))
 
1098
      (addr->special_action == SPECIAL_FREEZE ||
 
1099
        (sender_address[0] == 0 && addr->p.errors_address == NULL)
 
1100
      ))
1045
1101
    {
1046
1102
    frozen_info = (addr->special_action == SPECIAL_FREEZE)? US"" :
1047
1103
      (sender_local && !local_error_message)?
1074
1130
  if ((log_extra_selector & LX_sender_on_delivery) != 0)
1075
1131
    s = string_append(s, &size, &ptr, 3, US" F=<", sender_address, US">");
1076
1132
 
1077
 
  /* Return patch may not be set if no delivery actually happened */
 
1133
  /* Return path may not be set if no delivery actually happened */
1078
1134
 
1079
 
  if (return_path != NULL &&
 
1135
  if (used_return_path != NULL &&
1080
1136
      (log_extra_selector & LX_return_path_on_delivery) != 0)
1081
1137
    {
1082
 
    s = string_append(s, &size, &ptr, 3, US" P=<", return_path, US">");
 
1138
    s = string_append(s, &size, &ptr, 3, US" P=<", used_return_path, US">");
1083
1139
    }
1084
1140
 
1085
1141
  if (addr->router != NULL)
1398
1454
*************************************************/
1399
1455
 
1400
1456
/* Check that this base address hasn't previously been delivered to its routed
1401
 
transport. The check is necessary at delivery time in order to handle homonymic
1402
 
addresses correctly in cases where the pattern of redirection changes between
1403
 
delivery attempts (so the unique fields change). Non-homonymic previous
1404
 
delivery is detected earlier, at routing time (which saves unnecessary
1405
 
routing).
1406
 
 
1407
 
Argument:   the address item
 
1457
transport. If it has been delivered, mark it done. The check is necessary at
 
1458
delivery time in order to handle homonymic addresses correctly in cases where
 
1459
the pattern of redirection changes between delivery attempts (so the unique
 
1460
fields change). Non-homonymic previous delivery is detected earlier, at routing
 
1461
time (which saves unnecessary routing).
 
1462
 
 
1463
Arguments:
 
1464
  addr      the address item
 
1465
  testing   TRUE if testing wanted only, without side effects
 
1466
 
1408
1467
Returns:    TRUE if previously delivered by the transport
1409
1468
*/
1410
1469
 
1411
1470
static BOOL
1412
 
previously_transported(address_item *addr)
 
1471
previously_transported(address_item *addr, BOOL testing)
1413
1472
{
1414
1473
(void)string_format(big_buffer, big_buffer_size, "%s/%s",
1415
1474
  addr->unique + (testflag(addr, af_homonym)? 3:0), addr->transport->name);
1419
1478
  DEBUG(D_deliver|D_route|D_transport)
1420
1479
    debug_printf("%s was previously delivered (%s transport): discarded\n",
1421
1480
    addr->address, addr->transport->name);
1422
 
  child_done(addr, tod_stamp(tod_log));
 
1481
  if (!testing) child_done(addr, tod_stamp(tod_log));
1423
1482
  return TRUE;
1424
1483
  }
1425
1484
 
1483
1542
/* Set up the return path from the errors or sender address. If the transport
1484
1543
has its own return path setting, expand it and replace the existing value. */
1485
1544
 
1486
 
return_path = (addr->p.errors_address != NULL)?
1487
 
  addr->p.errors_address : sender_address;
 
1545
if(addr->p.errors_address != NULL)
 
1546
  return_path = addr->p.errors_address;
 
1547
#ifdef EXPERIMENTAL_SRS
 
1548
else if(addr->p.srs_sender != NULL)
 
1549
  return_path = addr->p.srs_sender;
 
1550
#endif
 
1551
else
 
1552
  return_path = sender_address;
1488
1553
 
1489
1554
if (tp->return_path != NULL)
1490
1555
  {
1502
1567
  else return_path = new_return_path;
1503
1568
  }
1504
1569
 
 
1570
/* For local deliveries, one at a time, the value used for logging can just be
 
1571
set directly, once and for all. */
 
1572
 
 
1573
used_return_path = return_path;
 
1574
 
1505
1575
/* Sort out the uid, gid, and initgroups flag. If an error occurs, the message
1506
1576
gets put into the address(es), and the expansions are unset, so we can just
1507
1577
return. */
1616
1686
    #ifdef SETRLIMIT_NOT_SUPPORTED
1617
1687
    if (errno != ENOSYS && errno != ENOTSUP)
1618
1688
    #endif
1619
 
      log_write(0, LOG_MAIN|LOG_PANIC, "setrlimit failed: %s", strerror(errno));
 
1689
      log_write(0, LOG_MAIN|LOG_PANIC, "setrlimit(RLIMIT_CORE) failed: %s",
 
1690
        strerror(errno));
1620
1691
    }
1621
1692
  #endif
1622
1693
 
1657
1728
  half - for transports that exec things (e.g. pipe). Then set the required
1658
1729
  gid/uid. */
1659
1730
 
1660
 
  close(pfd[pipe_read]);
1661
 
  fcntl(pfd[pipe_write], F_SETFD, fcntl(pfd[pipe_write], F_GETFD) |
 
1731
  (void)close(pfd[pipe_read]);
 
1732
  (void)fcntl(pfd[pipe_write], F_SETFD, fcntl(pfd[pipe_write], F_GETFD) |
1662
1733
    FD_CLOEXEC);
1663
1734
  exim_setugid(uid, gid, use_initgroups,
1664
1735
    string_sprintf("local delivery to %s <%s> transport=%s", addr->local_part,
1723
1794
    int local_part_length = Ustrlen(addr2->local_part);
1724
1795
    uschar *s;
1725
1796
 
1726
 
    write(pfd[pipe_write], (void *)&(addr2->transport_return), sizeof(int));
1727
 
    write(pfd[pipe_write], (void *)&transport_count, sizeof(transport_count));
1728
 
    write(pfd[pipe_write], (void *)&(addr2->flags), sizeof(addr2->flags));
1729
 
    write(pfd[pipe_write], (void *)&(addr2->basic_errno), sizeof(int));
1730
 
    write(pfd[pipe_write], (void *)&(addr2->more_errno), sizeof(int));
1731
 
    write(pfd[pipe_write], (void *)&(addr2->special_action), sizeof(int));
1732
 
    write(pfd[pipe_write], (void *)&(addr2->transport),
 
1797
    (void)write(pfd[pipe_write], (void *)&(addr2->transport_return), sizeof(int));
 
1798
    (void)write(pfd[pipe_write], (void *)&transport_count, sizeof(transport_count));
 
1799
    (void)write(pfd[pipe_write], (void *)&(addr2->flags), sizeof(addr2->flags));
 
1800
    (void)write(pfd[pipe_write], (void *)&(addr2->basic_errno), sizeof(int));
 
1801
    (void)write(pfd[pipe_write], (void *)&(addr2->more_errno), sizeof(int));
 
1802
    (void)write(pfd[pipe_write], (void *)&(addr2->special_action), sizeof(int));
 
1803
    (void)write(pfd[pipe_write], (void *)&(addr2->transport),
1733
1804
      sizeof(transport_instance *));
1734
1805
 
1735
1806
    /* For a file delivery, pass back the local part, in case the original
1738
1809
 
1739
1810
    if (testflag(addr2, af_file))
1740
1811
      {
1741
 
      write(pfd[pipe_write], (void *)&local_part_length, sizeof(int));
1742
 
      write(pfd[pipe_write], addr2->local_part, local_part_length);
 
1812
      (void)write(pfd[pipe_write], (void *)&local_part_length, sizeof(int));
 
1813
      (void)write(pfd[pipe_write], addr2->local_part, local_part_length);
1743
1814
      }
1744
1815
 
1745
1816
    /* Now any messages */
1747
1818
    for (i = 0, s = addr2->message; i < 2; i++, s = addr2->user_message)
1748
1819
      {
1749
1820
      int message_length = (s == NULL)? 0 : Ustrlen(s) + 1;
1750
 
      write(pfd[pipe_write], (void *)&message_length, sizeof(int));
1751
 
      if (message_length > 0) write(pfd[pipe_write], s, message_length);
 
1821
      (void)write(pfd[pipe_write], (void *)&message_length, sizeof(int));
 
1822
      if (message_length > 0) (void)write(pfd[pipe_write], s, message_length);
1752
1823
      }
1753
1824
    }
1754
1825
 
1755
1826
  /* OK, this process is now done. Free any cached resources that it opened,
1756
1827
  and close the pipe we were writing down before exiting. */
1757
1828
 
1758
 
  close(pfd[pipe_write]);
 
1829
  (void)close(pfd[pipe_write]);
1759
1830
  search_tidyup();
1760
1831
  exit(EXIT_SUCCESS);
1761
1832
  }
1774
1845
overwriting the address structure. If data is missing, the default DEFER status
1775
1846
will remain. Afterwards, close the reading end. */
1776
1847
 
1777
 
close(pfd[pipe_write]);
 
1848
(void)close(pfd[pipe_write]);
1778
1849
 
1779
1850
for (addr2 = addr; addr2 != NULL; addr2 = addr2->next)
1780
1851
  {
1824
1895
    }
1825
1896
  }
1826
1897
 
1827
 
close(pfd[pipe_read]);
 
1898
(void)close(pfd[pipe_read]);
1828
1899
 
1829
1900
/* Unless shadowing, write all successful addresses immediately to the journal
1830
1901
file, to ensure they are recorded asap. For homonymic addresses, use the base
1927
1998
 
1928
1999
      /* Close and wait for child process to complete, without a timeout. */
1929
2000
 
1930
 
      fclose(f);
 
2001
      (void)fclose(f);
1931
2002
      (void)child_close(pid, 0);
1932
2003
      }
1933
2004
    }
2000
2071
  attempts. Non-homonymic previous delivery is detected earlier, at routing
2001
2072
  time. */
2002
2073
 
2003
 
  if (previously_transported(addr)) continue;
 
2074
  if (previously_transported(addr, FALSE)) continue;
2004
2075
 
2005
2076
  /* There are weird cases where logging is disabled */
2006
2077
 
2041
2112
    same characteristics. These are:
2042
2113
 
2043
2114
      same transport
 
2115
      not previously delivered (see comment about 50 lines above)
2044
2116
      same local part if the transport's configuration contains $local_part
2045
2117
      same domain if the transport's configuration contains $domain
2046
2118
      same errors address
2054
2126
      {
2055
2127
      BOOL ok =
2056
2128
        tp == next->transport &&
 
2129
        !previously_transported(next, TRUE) &&
2057
2130
        (!uses_lp  || Ustrcmp(next->local_part, addr->local_part) == 0) &&
2058
2131
        (!uses_dom || Ustrcmp(next->domain, addr->domain) == 0) &&
2059
2132
        same_strings(next->p.errors_address, addr->p.errors_address) &&
2517
2590
 
2518
2591
Read in large chunks into the big buffer and then scan through, interpreting
2519
2592
the data therein. In most cases, only a single read will be necessary. No
2520
 
individual item will ever be anywhere near 500 bytes in length, so by ensuring
2521
 
that we read the next chunk when there is less than 500 bytes left in the
2522
 
non-final chunk, we can assume each item is complete in store before handling
2523
 
it. Actually, each item is written using a single write(), which is atomic for
2524
 
small items (less than PIPE_BUF, which seems to be at least 512 in any Unix) so
2525
 
even if we are reading while the subprocess is still going, we should never
2526
 
have only a partial item in the buffer.
 
2593
individual item will ever be anywhere near 2500 bytes in length, so by ensuring
 
2594
that we read the next chunk when there is less than 2500 bytes left in the
 
2595
non-final chunk, we can assume each item is complete in the buffer before
 
2596
handling it. Each item is written using a single write(), which is atomic for
 
2597
small items (less than PIPE_BUF, which seems to be at least 512 in any Unix and
 
2598
often bigger) so even if we are reading while the subprocess is still going, we
 
2599
should never have only a partial item in the buffer.
2527
2600
 
2528
2601
Argument:
2529
2602
  poffset     the offset of the parlist item
2559
2632
 
2560
2633
Each separate item is written to the pipe in a single write(), and as they are
2561
2634
all short items, the writes will all be atomic and we should never find
2562
 
ourselves in the position of having read an incomplete item. */
 
2635
ourselves in the position of having read an incomplete item. "Short" in this
 
2636
case can mean up to about 1K in the case when there is a long error message
 
2637
associated with an address. */
2563
2638
 
2564
2639
DEBUG(D_deliver) debug_printf("reading pipe for subprocess %d (%s)\n",
2565
2640
  (int)p->pid, eop? "ended" : "not ended");
2573
2648
  There will be only one read if we get all the available data (i.e. don't
2574
2649
  fill the buffer completely). */
2575
2650
 
2576
 
  if (remaining < 500 && unfinished)
 
2651
  if (remaining < 2500 && unfinished)
2577
2652
    {
2578
2653
    int len;
2579
2654
    int available = big_buffer_size - remaining;
2810
2885
/* Close our end of the pipe, to prevent deadlock if the far end is still
2811
2886
pushing stuff into it. */
2812
2887
 
2813
 
close(fd);
 
2888
(void)close(fd);
2814
2889
p->fd = -1;
2815
2890
 
2816
2891
/* If we have finished without error, but haven't had data for every address,
3181
3256
 
3182
3257
else if (!parlist[poffset].done) (void)par_read_pipe(poffset, TRUE);
3183
3258
 
3184
 
/* Put the data count into a global, mark the data slot unused, decrement the
3185
 
count of subprocesses, and return the address chain. */
 
3259
/* Put the data count and return path into globals, mark the data slot unused,
 
3260
decrement the count of subprocesses, and return the address chain. */
3186
3261
 
3187
3262
transport_count = parlist[poffset].transport_count;
 
3263
used_return_path = parlist[poffset].return_path;
3188
3264
parlist[poffset].pid = 0;
3189
3265
parcount--;
3190
3266
return addrlist;
3239
3315
to an existing delivery channel, skip all but those addresses that can go to
3240
3316
that channel. The skipped addresses just get deferred.
3241
3317
 
 
3318
If mua_wrapper is set, all addresses must be able to be sent in a single
 
3319
transaction. If not, this function yields FALSE.
 
3320
 
3242
3321
In Exim 4, remote deliveries are always done in separate processes, even
3243
3322
if remote_max_parallel = 1 or if there's only one delivery to do. The reason
3244
3323
is so that the base process can retain privilege. This makes the
3251
3330
Arguments:
3252
3331
  fallback  TRUE if processing fallback hosts
3253
3332
 
3254
 
Returns:    Nothing
 
3333
Returns:    TRUE normally
 
3334
            FALSE if mua_wrapper is set and the addresses cannot all be sent
 
3335
              in one transaction
3255
3336
*/
3256
3337
 
3257
 
static void
 
3338
static BOOL
3258
3339
do_remote_deliveries(BOOL fallback)
3259
3340
{
3260
3341
int parmax;
3323
3404
  attempts. Non-homonymic previous delivery is detected earlier, at routing
3324
3405
  time. */
3325
3406
 
3326
 
  if (previously_transported(addr)) continue;
 
3407
  if (previously_transported(addr, FALSE)) continue;
3327
3408
 
3328
3409
  /* Force failure if the message is too big. */
3329
3410
 
3344
3425
  multi_domain = tp->multi_domain;
3345
3426
 
3346
3427
  /* Get the maximum it can handle in one envelope, with zero meaning
3347
 
  unlimited. */
 
3428
  unlimited, which is forced for the MUA wrapper case. */
3348
3429
 
3349
3430
  address_count_max = tp->max_addresses;
3350
 
  if (address_count_max == 0) address_count_max = 9999;
 
3431
  if (address_count_max == 0 || mua_wrapper) address_count_max = 999999;
3351
3432
 
3352
3433
 
3353
3434
  /************************************************************************/
3441
3522
    else anchor = &(next->next);
3442
3523
    }
3443
3524
 
 
3525
  /* If we are acting as an MUA wrapper, all addresses must go in a single
 
3526
  transaction. If not, put them back on the chain and yield FALSE. */
 
3527
 
 
3528
  if (mua_wrapper && addr_remote != NULL)
 
3529
    {
 
3530
    last->next = addr_remote;
 
3531
    addr_remote = addr;
 
3532
    return FALSE;
 
3533
    }
 
3534
 
3444
3535
  /* Set up the expansion variables for this set of addresses */
3445
3536
 
3446
3537
  deliver_set_expansions(addr);
3448
3539
  /* Compute the return path, expanding a new one if required. The old one
3449
3540
  must be set first, as it might be referred to in the expansion. */
3450
3541
 
3451
 
  return_path = (addr->p.errors_address != NULL)?
3452
 
    addr->p.errors_address : sender_address;
 
3542
  if(addr->p.errors_address != NULL)
 
3543
    return_path = addr->p.errors_address;
 
3544
#ifdef EXPERIMENTAL_SRS
 
3545
  else if(addr->p.srs_sender != NULL)
 
3546
    return_path = addr->p.srs_sender;
 
3547
#endif
 
3548
  else
 
3549
    return_path = sender_address;
3453
3550
 
3454
3551
  if (tp->return_path != NULL)
3455
3552
    {
3576
3673
    distinguishes between EOF and no-more-data. */
3577
3674
 
3578
3675
    #ifdef O_NONBLOCK
3579
 
    fcntl(pfd[pipe_read], F_SETFL, O_NONBLOCK);
 
3676
    (void)fcntl(pfd[pipe_read], F_SETFL, O_NONBLOCK);
3580
3677
    #else
3581
 
    fcntl(pfd[pipe_read], F_SETFL, O_NDELAY);
 
3678
    (void)fcntl(pfd[pipe_read], F_SETFL, O_NDELAY);
3582
3679
    #endif
3583
3680
 
3584
3681
    /* If the maximum number of subprocesses already exist, wait for a process
3610
3707
 
3611
3708
  if (poffset >= remote_max_parallel)
3612
3709
    {
3613
 
    close(pfd[pipe_write]);
3614
 
    close(pfd[pipe_read]);
 
3710
    (void)close(pfd[pipe_write]);
 
3711
    (void)close(pfd[pipe_read]);
3615
3712
    remote_post_process(addr, LOG_MAIN|LOG_PANIC,
3616
3713
      US"Unexpectedly no free subprocess slot", fallback);
3617
3714
    continue;
3651
3748
    a new process that may be forked to do another delivery down the same
3652
3749
    SMTP connection. */
3653
3750
 
3654
 
    fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
 
3751
    (void)fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
3655
3752
 
3656
3753
    /* Close open file descriptors for the pipes of other processes
3657
3754
    that are running in parallel. */
3658
3755
 
3659
3756
    for (poffset = 0; poffset < remote_max_parallel; poffset++)
3660
 
      if (parlist[poffset].pid != 0) close(parlist[poffset].fd);
 
3757
      if (parlist[poffset].pid != 0) (void)close(parlist[poffset].fd);
3661
3758
 
3662
3759
    /* This process has inherited a copy of the file descriptor
3663
3760
    for the data file, but its file pointer is shared with all the
3667
3764
    the parent process. There doesn't seem to be any way of doing
3668
3765
    a dup-with-new-file-pointer. */
3669
3766
 
3670
 
    close(deliver_datafile);
 
3767
    (void)close(deliver_datafile);
3671
3768
    sprintf(CS spoolname, "%s/input/%s/%s-D", spool_directory, message_subdir,
3672
3769
      message_id);
3673
3770
    deliver_datafile = Uopen(spoolname, O_RDWR | O_APPEND, 0);
3678
3775
 
3679
3776
    /* Set the close-on-exec flag */
3680
3777
 
3681
 
    fcntl(deliver_datafile, F_SETFD, fcntl(deliver_datafile, F_GETFD) |
 
3778
    (void)fcntl(deliver_datafile, F_SETFD, fcntl(deliver_datafile, F_GETFD) |
3682
3779
      FD_CLOEXEC);
3683
3780
 
3684
3781
    /* Set the uid/gid of this process; bombs out on failure. */
3691
3788
    and run the transport. Afterwards, transport_count will contain the number
3692
3789
    of bytes written. */
3693
3790
 
3694
 
    close(pfd[pipe_read]);
 
3791
    (void)close(pfd[pipe_read]);
3695
3792
    set_process_info("delivering %s using %s", message_id, tp->name);
3696
3793
    debug_print_string(tp->debug_string);
3697
3794
    if (!(tp->info->code)(addr->transport, addr)) replicate_status(addr);
3720
3817
      {
3721
3818
      if (h->address == NULL || h->status < hstatus_unusable) continue;
3722
3819
      sprintf(CS big_buffer, "H%c%c%s", h->status, h->why, h->address);
3723
 
      write(fd, big_buffer, Ustrlen(big_buffer+3) + 4);
 
3820
      (void)write(fd, big_buffer, Ustrlen(big_buffer+3) + 4);
3724
3821
      }
3725
3822
 
3726
3823
    /* The number of bytes written. This is the same for each address. Even
3730
3827
 
3731
3828
    big_buffer[0] = 'S';
3732
3829
    memcpy(big_buffer+1, &transport_count, sizeof(transport_count));
3733
 
    write(fd, big_buffer, sizeof(transport_count) + 1);
 
3830
    (void)write(fd, big_buffer, sizeof(transport_count) + 1);
3734
3831
 
3735
3832
    /* Information about what happened to each address. Three item types are
3736
3833
    used: an optional 'X' item first, for TLS information, followed by 'R'
3760
3857
          sprintf(CS ptr, "%.512s", addr->peerdn);
3761
3858
          while(*ptr++);
3762
3859
          }
3763
 
        write(fd, big_buffer, ptr - big_buffer);
 
3860
        (void)write(fd, big_buffer, ptr - big_buffer);
3764
3861
        }
3765
3862
      #endif
3766
3863
 
3780
3877
          sprintf(CS ptr, "%.512s", r->message);
3781
3878
          while(*ptr++);
3782
3879
          }
3783
 
        write(fd, big_buffer, ptr - big_buffer);
 
3880
        (void)write(fd, big_buffer, ptr - big_buffer);
3784
3881
        }
3785
3882
 
3786
3883
      /* The rest of the information goes in an 'A' item. */
3797
3894
 
3798
3895
      if (addr->message == NULL) *ptr++ = 0; else
3799
3896
        {
3800
 
        sprintf(CS ptr, "%.512s", addr->message);
 
3897
        sprintf(CS ptr, "%.1024s", addr->message);
3801
3898
        while(*ptr++);
3802
3899
        }
3803
3900
 
3804
3901
      if (addr->user_message == NULL) *ptr++ = 0; else
3805
3902
        {
3806
 
        sprintf(CS ptr, "%.512s", addr->user_message);
 
3903
        sprintf(CS ptr, "%.1024s", addr->user_message);
3807
3904
        while(*ptr++);
3808
3905
        }
3809
3906
 
3816
3913
        memcpy(ptr, &(addr->host_used->port), sizeof(addr->host_used->port));
3817
3914
        ptr += sizeof(addr->host_used->port);
3818
3915
        }
3819
 
      write(fd, big_buffer, ptr - big_buffer);
 
3916
      (void)write(fd, big_buffer, ptr - big_buffer);
3820
3917
      }
3821
3918
 
3822
3919
    /* Add termination flag, close the pipe, and that's it. The character
3826
3923
 
3827
3924
    big_buffer[0] = 'Z';
3828
3925
    big_buffer[1] = (continue_transport == NULL)? '0' : '1';
3829
 
    write(fd, big_buffer, 2);
3830
 
    close(fd);
 
3926
    (void)write(fd, big_buffer, 2);
 
3927
    (void)close(fd);
3831
3928
    exit(EXIT_SUCCESS);
3832
3929
    }
3833
3930
 
3834
3931
  /* Back in the mainline: close the unwanted half of the pipe. */
3835
3932
 
3836
 
  close(pfd[pipe_write]);
 
3933
  (void)close(pfd[pipe_write]);
3837
3934
 
3838
3935
  /* Fork failed; defer with error message */
3839
3936
 
3840
3937
  if (pid < 0)
3841
3938
    {
3842
 
    close(pfd[pipe_read]);
 
3939
    (void)close(pfd[pipe_read]);
3843
3940
    remote_post_process(addr, LOG_MAIN|LOG_PANIC,
3844
3941
      string_sprintf("fork failed for remote delivery to %s: %s",
3845
3942
        addr->domain, strerror(errno)), fallback);
3855
3952
  parlist[poffset].fd = pfd[pipe_read];
3856
3953
  parlist[poffset].done = FALSE;
3857
3954
  parlist[poffset].msg = NULL;
 
3955
  parlist[poffset].return_path = return_path;
3858
3956
 
3859
3957
  /* If the process we've just started is sending a message down an existing
3860
3958
  channel, wait for it now. This ensures that only one such process runs at
3880
3978
are still running and post-process their addresses. */
3881
3979
 
3882
3980
par_reduce(0, fallback);
 
3981
return TRUE;
3883
3982
}
3884
3983
 
3885
3984
 
4036
4135
*************************************************/
4037
4136
 
4038
4137
/* This function is called when a passed transport channel cannot be used.
4039
 
It attempts to close it down tidily. The yield is always FAIL so that the
4040
 
function call can be the argument of a "return" statement.
 
4138
It attempts to close it down tidily. The yield is always DELIVER_NOT_ATTEMPTED
 
4139
so that the function call can be the argument of a "return" statement.
4041
4140
 
4042
4141
Arguments:  None
4043
 
Returns:    FAIL
 
4142
Returns:    DELIVER_NOT_ATTEMPTED
4044
4143
*/
4045
4144
 
4046
4145
static int
4058
4157
      }
4059
4158
    }
4060
4159
  }
4061
 
return FAIL;
 
4160
return DELIVER_NOT_ATTEMPTED;
4062
4161
}
4063
4162
 
4064
4163
 
4098
4197
  printed = US"an undisclosed address";
4099
4198
  yield = FALSE;
4100
4199
  }
4101
 
 
4102
4200
else if (!testflag(addr, af_pfr) || addr->parent == NULL)
4103
4201
  printed = addr->address;
4104
4202
 
4135
4233
 
4136
4234
 
4137
4235
 
4138
 
 
4139
4236
/*************************************************
4140
4237
*         Print error for an address             *
4141
4238
*************************************************/
4145
4242
introducing newlines. All lines are indented by 4; the initial printing
4146
4243
position must be set before calling.
4147
4244
 
 
4245
This function used always to print the error. Nowadays we want to restrict it
 
4246
to cases such as SMTP errors from a remote host, and errors from :fail: and
 
4247
filter "fail". We no longer pass other information willy-nilly in bounce and
 
4248
warning messages. Text in user_message is always output; text in message only
 
4249
if the af_pass_message flag is set.
 
4250
 
4148
4251
Arguments:
4149
 
  addr         points to the address
 
4252
  addr         the address
4150
4253
  f            the FILE to print on
 
4254
  s            some leading text
4151
4255
 
4152
4256
Returns:       nothing
4153
4257
*/
4154
4258
 
4155
4259
static void
4156
 
print_address_error(address_item *addr, FILE *f)
 
4260
print_address_error(address_item *addr, FILE *f, uschar *t)
4157
4261
{
 
4262
int count = Ustrlen(t);
4158
4263
uschar *s = (addr->user_message != NULL)? addr->user_message : addr->message;
4159
 
if (addr->basic_errno > 0)
4160
 
  {
4161
 
  fprintf(f, "%s%s", strerror(addr->basic_errno),
4162
 
    (s == NULL)? "" : ":\n    ");
4163
 
  }
4164
 
if (s == NULL)
4165
 
  {
4166
 
  if (addr->basic_errno <= 0) fprintf(f, "unknown error");
4167
 
  }
 
4264
 
 
4265
if (addr->user_message != NULL)
 
4266
  s = addr->user_message;
4168
4267
else
4169
4268
  {
4170
 
  int count = 0;
4171
 
  while (*s != 0)
4172
 
    {
4173
 
    if (*s == '\\' && s[1] == 'n')
 
4269
  if (!testflag(addr, af_pass_message) || addr->message == NULL) return;
 
4270
  s = addr->message;
 
4271
  }
 
4272
 
 
4273
fprintf(f, "\n    %s", t);
 
4274
 
 
4275
while (*s != 0)
 
4276
  {
 
4277
  if (*s == '\\' && s[1] == 'n')
 
4278
    {
 
4279
    fprintf(f, "\n    ");
 
4280
    s += 2;
 
4281
    count = 0;
 
4282
    }
 
4283
  else
 
4284
    {
 
4285
    fputc(*s, f);
 
4286
    count++;
 
4287
    if (*s++ == ':' && isspace(*s) && count > 45)
4174
4288
      {
4175
 
      fprintf(f, "\n    ");
4176
 
      s += 2;
 
4289
      fprintf(f, "\n   ");  /* sic (because space follows) */
4177
4290
      count = 0;
4178
4291
      }
4179
 
    else
4180
 
      {
4181
 
      fputc(*s, f);
4182
 
      count++;
4183
 
      if (*s++ == ':' && isspace(*s) && count > 45)
4184
 
        {
4185
 
        fprintf(f, "\n   ");  /* sic (because space follows) */
4186
 
        count = 0;
4187
 
        }
4188
 
      }
 
4292
    }
 
4293
  }
 
4294
}
 
4295
 
 
4296
 
 
4297
 
 
4298
 
 
4299
 
 
4300
 
 
4301
/*************************************************
 
4302
*     Check list of addresses for duplication    *
 
4303
*************************************************/
 
4304
 
 
4305
/* This function was introduced when the test for duplicate addresses that are
 
4306
not pipes, files, or autoreplies was moved from the middle of routing to when
 
4307
routing was complete. That was to fix obscure cases when the routing history
 
4308
affects the subsequent routing of identical addresses. If that change has to be
 
4309
reversed, this function is no longer needed. For a while, the old code that was
 
4310
affected by this change is commented with !!!OLD-DE-DUP!!! so it can be found
 
4311
easily.
 
4312
 
 
4313
This function is called after routing, to check that the final routed addresses
 
4314
are not duplicates. If we detect a duplicate, we remember what it is a
 
4315
duplicate of. Note that pipe, file, and autoreply de-duplication is handled
 
4316
during routing, so we must leave such "addresses" alone here, as otherwise they
 
4317
will incorrectly be discarded.
 
4318
 
 
4319
Argument:     address of list anchor
 
4320
Returns:      nothing
 
4321
*/
 
4322
 
 
4323
static void
 
4324
do_duplicate_check(address_item **anchor)
 
4325
{
 
4326
address_item *addr;
 
4327
while ((addr = *anchor) != NULL)
 
4328
  {
 
4329
  tree_node *tnode;
 
4330
  if (testflag(addr, af_pfr))
 
4331
    {
 
4332
    anchor = &(addr->next);
 
4333
    }
 
4334
  else if ((tnode = tree_search(tree_duplicates, addr->unique)) != NULL)
 
4335
    {
 
4336
    DEBUG(D_deliver|D_route)
 
4337
      debug_printf("%s is a duplicate address: discarded\n", addr->unique);
 
4338
    *anchor = addr->next;
 
4339
    addr->dupof = tnode->data.ptr;
 
4340
    addr->next = addr_duplicate;
 
4341
    addr_duplicate = addr;
 
4342
    }
 
4343
  else
 
4344
    {
 
4345
    tree_add_duplicate(addr->unique, addr);
 
4346
    anchor = &(addr->next);
4189
4347
    }
4190
4348
  }
4191
4349
}
4204
4362
will be locked.
4205
4363
 
4206
4364
If no delivery is attempted for any of the above reasons, the function returns
4207
 
FALSE, otherwise TRUE.
 
4365
DELIVER_NOT_ATTEMPTED.
4208
4366
 
4209
4367
If the give_up flag is set true, do not attempt any deliveries, but instead
4210
4368
fail all outstanding addresses and return the message to the sender (or
4211
4369
whoever).
4212
4370
 
4213
4371
A delivery operation has a process all to itself; we never deliver more than
4214
 
one message in the same process. Therefore we needn't worry about store
4215
 
leakage.
 
4372
one message in the same process. Therefore we needn't worry too much about
 
4373
store leakage.
4216
4374
 
4217
4375
Arguments:
4218
4376
  id          the id of the message to be delivered
4221
4379
  give_up     TRUE if an administrator has requested that delivery attempts
4222
4380
              be abandoned
4223
4381
 
4224
 
Returns:      OK if an attempt was made at delivering the message
4225
 
              FAIL otherwise
 
4382
Returns:      When the global variable mua_wrapper is FALSE:
 
4383
                DELIVER_ATTEMPTED_NORMAL   if a delivery attempt was made
 
4384
                DELIVER_NOT_ATTEMPTED      otherwise (see comment above)
 
4385
              When the global variable mua_wrapper is TRUE:
 
4386
                DELIVER_MUA_SUCCEEDED      if delivery succeeded
 
4387
                DELIVER_MUA_FAILED         if delivery failed
 
4388
                DELIVER_NOT_ATTEMPTED      if not attempted (should not occur)
4226
4389
*/
4227
4390
 
4228
4391
int
4229
4392
deliver_message(uschar *id, BOOL forced, BOOL give_up)
4230
4393
{
4231
4394
int i, rc;
 
4395
int final_yield = DELIVER_ATTEMPTED_NORMAL;
4232
4396
time_t now = time(NULL);
4233
4397
address_item *addr_last = NULL;
4234
4398
uschar *filter_message = NULL;
4242
4406
  string_sprintf("delivering %s (queue run pid %d)", id, queue_run_pid);
4243
4407
 
4244
4408
/* If the D_process_info bit is on, set_process_info() will output debugging
4245
 
information. If not, we want to show this inifial information if D_deliver or
4246
 
D_queue_run is set. */
 
4409
information. If not, we want to show this initial information if D_deliver or
 
4410
D_queue_run is set or in verbose mode. */
4247
4411
 
4248
4412
set_process_info("%s", info);
4249
4413
 
4250
4414
if ((debug_selector & D_process_info) == 0 &&
4251
 
    (debug_selector & (D_deliver|D_queue_run)) != 0)
 
4415
    (debug_selector & (D_deliver|D_queue_run|D_v)) != 0)
4252
4416
  debug_printf("%s\n", info);
4253
4417
 
4254
4418
/* Ensure that we catch any subprocesses that are created. Although Exim
4298
4462
while queue running - another process probably completed delivery. As part of
4299
4463
opening the data file, message_subdir gets set. */
4300
4464
 
4301
 
if (!spool_open_datafile(id)) return continue_closedown();
 
4465
if (!spool_open_datafile(id))
 
4466
  return continue_closedown();  /* yields DELIVER_NOT_ATTEMPTED */
4302
4467
 
4303
4468
/* The value of message_size at this point has been set to the data length,
4304
4469
plus one for the blank line that notionally precedes the data. */
4317
4482
    sprintf(CS big_buffer, "%s/input/%s/%s", spool_directory, message_subdir,
4318
4483
      spoolname);
4319
4484
    if (Ustat(big_buffer, &statbuf) == 0)
4320
 
      {
4321
 
      int size = statbuf.st_size;   /* Because might be a long */
4322
 
      log_write(0, LOG_MAIN, "Format error in spool file %s: size=%d",
4323
 
        spoolname, size);
4324
 
      }
 
4485
      log_write(0, LOG_MAIN, "Format error in spool file %s: "
 
4486
        "size=" OFF_T_FMT, spoolname, statbuf.st_size);
4325
4487
    else log_write(0, LOG_MAIN, "Format error in spool file %s", spoolname);
4326
4488
    }
4327
4489
  else
4355
4517
      readconf_printtime(keep_malformed));
4356
4518
    }
4357
4519
 
4358
 
  close(deliver_datafile);
 
4520
  (void)close(deliver_datafile);
4359
4521
  deliver_datafile = -1;
4360
 
  return continue_closedown();
 
4522
  return continue_closedown();   /* yields DELIVER_NOT_ATTEMPTED */
4361
4523
  }
4362
4524
 
4363
4525
/* The spool header file has been read. Look to see if there is an existing
4381
4543
    DEBUG(D_deliver) debug_printf("Previously delivered address %s taken from "
4382
4544
      "journal file\n", big_buffer);
4383
4545
    }
4384
 
  fclose(jread);
 
4546
  (void)fclose(jread);
4385
4547
  /* Panic-dies on error */
4386
4548
  (void)spool_write_header(message_id, SW_DELIVERING, NULL);
4387
4549
  }
4389
4551
  {
4390
4552
  log_write(0, LOG_MAIN|LOG_PANIC, "attempt to open journal for reading gave: "
4391
4553
    "%s", strerror(errno));
4392
 
  return continue_closedown();
 
4554
  return continue_closedown();   /* yields DELIVER_NOT_ATTEMPTED */
4393
4555
  }
4394
4556
 
4395
4557
/* A null recipients list indicates some kind of disaster. */
4396
4558
 
4397
4559
if (recipients_list == NULL)
4398
4560
  {
4399
 
  close(deliver_datafile);
 
4561
  (void)close(deliver_datafile);
4400
4562
  deliver_datafile = -1;
4401
4563
  log_write(0, LOG_MAIN, "Spool error: no recipients for %s", spoolname);
4402
 
  return continue_closedown();
 
4564
  return continue_closedown();   /* yields DELIVER_NOT_ATTEMPTED */
4403
4565
  }
4404
4566
 
4405
4567
 
4416
4578
 
4417
4579
  if (move_frozen_messages &&
4418
4580
      spool_move_message(id, message_subdir, US"", US"F"))
4419
 
    return continue_closedown();
 
4581
    return continue_closedown();   /* yields DELIVER_NOT_ATTEMPTED */
4420
4582
  #endif
4421
4583
 
4422
4584
  /* For all frozen messages (bounces or not), timeout_frozen_after sets the
4439
4601
    log_write(0, LOG_MAIN, "Unfrozen by errmsg timer");
4440
4602
    }
4441
4603
 
4442
 
  /* If there's no auto thaw, or we haven't reached the auto thaw time yet, and
4443
 
  this delivery is not forced by an admin user, do not attempt delivery of this
4444
 
  message. Note that forced is set for continuing messages down the same
4445
 
  channel, in order to skip load checking and ignore hold domains, but we
4446
 
  don't want unfreezing in that case. */
 
4604
  /* If this is a bounce message, or there's no auto thaw, or we haven't
 
4605
  reached the auto thaw time yet, and this delivery is not forced by an admin
 
4606
  user, do not attempt delivery of this message. Note that forced is set for
 
4607
  continuing messages down the same channel, in order to skip load checking and
 
4608
  ignore hold domains, but we don't want unfreezing in that case. */
4447
4609
 
4448
4610
  else
4449
4611
    {
4450
 
    if ((auto_thaw <= 0 || now <= deliver_frozen_at + auto_thaw) &&
4451
 
      (!forced || !deliver_force_thaw || !admin_user ||
4452
 
        continue_hostname != NULL))
 
4612
    if ((sender_address[0] == 0 ||
 
4613
         auto_thaw <= 0 ||
 
4614
         now <= deliver_frozen_at + auto_thaw
 
4615
        )
 
4616
        &&
 
4617
        (!forced || !deliver_force_thaw || !admin_user ||
 
4618
          continue_hostname != NULL
 
4619
        ))
4453
4620
      {
4454
 
      close(deliver_datafile);
 
4621
      (void)close(deliver_datafile);
4455
4622
      deliver_datafile = -1;
4456
4623
      log_write(L_skip_delivery, LOG_MAIN, "Message is frozen");
4457
 
      return continue_closedown();
 
4624
      return continue_closedown();   /* yields DELIVER_NOT_ATTEMPTED */
4458
4625
      }
4459
4626
 
4460
4627
    /* If delivery was forced (by an admin user), assume a manual thaw.
4492
4659
    {
4493
4660
    log_write(0, LOG_MAIN|LOG_PANIC, "Couldn't %s message log %s: %s", error,
4494
4661
      spoolname, strerror(errno));
4495
 
    return continue_closedown();
 
4662
    return continue_closedown();   /* yields DELIVER_NOT_ATTEMPTED */
4496
4663
    }
4497
4664
 
4498
4665
  /* Make a C stream out of it. */
4502
4669
    {
4503
4670
    log_write(0, LOG_MAIN|LOG_PANIC, "Couldn't fdopen message log %s: %s",
4504
4671
      spoolname, strerror(errno));
4505
 
    return continue_closedown();
 
4672
    return continue_closedown();   /* yields DELIVER_NOT_ATTEMPTED */
4506
4673
    }
4507
4674
  }
4508
4675
 
4572
4739
      RDO_REALLOG |
4573
4740
      RDO_REWRITE,
4574
4741
    NULL,                   /* No :include: restriction (not used in filter) */
 
4742
    NULL,                   /* No sieve vacation directory (not sieve!) */
 
4743
    NULL,                   /* No sieve user address (not sieve!) */
 
4744
    NULL,                   /* No sieve subaddress (not sieve!) */
4575
4745
    &ugid,                  /* uid/gid data */
4576
4746
    &addr_new,              /* Where to hang generated addresses */
4577
4747
    &filter_message,        /* Where to put error message */
4583
4753
 
4584
4754
  if (rc == FF_ERROR || rc == FF_NONEXIST)
4585
4755
    {
4586
 
    close(deliver_datafile);
 
4756
    (void)close(deliver_datafile);
4587
4757
    deliver_datafile = -1;
4588
4758
    log_write(0, LOG_MAIN|LOG_PANIC, "Error in system filter: %s",
4589
4759
      string_printing(filter_message));
4590
 
    return continue_closedown();
 
4760
    return continue_closedown();   /* yields DELIVER_NOT_ATTEMPTED */
4591
4761
    }
4592
4762
 
4593
4763
  /* Reset things. If the filter message is an empty string, which can happen
4850
5020
        case RECIP_FAIL_FILTER:
4851
5021
        new->message =
4852
5022
          (filter_message == NULL)? US"delivery cancelled" : filter_message;
 
5023
        setflag(new, af_pass_message);
4853
5024
        goto RECIP_QUEUE_FAILED;   /* below */
4854
5025
 
4855
5026
 
5198
5369
      continue;
5199
5370
      }
5200
5371
 
 
5372
 
 
5373
    /* !!!OLD-DE-DUP!!!  We used to test for duplicates at this point, in order
 
5374
    to save effort on routing duplicate addresses. However, facilities have
 
5375
    been added to Exim so that now two identical addresses that are children of
 
5376
    other addresses may be routed differently as a result of their previous
 
5377
    routing history. For example, different redirect routers may have given
 
5378
    them different redirect_router values, but there are other cases too.
 
5379
    Therefore, tests for duplicates now take place when routing is complete.
 
5380
    This is the old code, kept for a while for the record, and in case this
 
5381
    radical change has to be backed out for some reason. */
 
5382
 
 
5383
    #ifdef NEVER
5201
5384
    /* If it's a duplicate, remember what it's a duplicate of */
5202
5385
 
5203
5386
    if ((tnode = tree_search(tree_duplicates, addr->unique)) != NULL)
5213
5396
    /* Record this address, so subsequent duplicates get picked up. */
5214
5397
 
5215
5398
    tree_add_duplicate(addr->unique, addr);
 
5399
    #endif
 
5400
 
 
5401
 
5216
5402
 
5217
5403
    /* Get the routing retry status, saving the two retry keys (with and
5218
5404
    without the local part) for subsequent use. Ignore retry records that
5525
5711
local_user_gid = (gid_t)(-1);
5526
5712
local_user_uid = (uid_t)(-1);
5527
5713
 
 
5714
 
 
5715
/* !!!OLD-DE-DUP!!! The next two statement were introduced when checking for
 
5716
duplicates was moved from within routing to afterwards. If that change has to
 
5717
be backed out, they should be removed. */
 
5718
 
 
5719
/* Check for any duplicate addresses. This check is delayed until after
 
5720
routing, because the flexibility of the routing configuration means that
 
5721
identical addresses with different parentage may end up being redirected to
 
5722
different addresses. Checking for duplicates too early (as we previously used
 
5723
to) makes this kind of thing not work. */
 
5724
 
 
5725
do_duplicate_check(&addr_local);
 
5726
do_duplicate_check(&addr_remote);
 
5727
 
 
5728
 
 
5729
/* When acting as an MUA wrapper, we proceed only if all addresses route to a
 
5730
remote transport. The check that they all end up in one transaction happens in
 
5731
the do_remote_deliveries() function. */
 
5732
 
 
5733
if (mua_wrapper && (addr_local != NULL || addr_failed != NULL ||
 
5734
                    addr_defer != NULL))
 
5735
  {
 
5736
  address_item *addr;
 
5737
  uschar *which, *colon, *msg;
 
5738
 
 
5739
  if (addr_local != NULL)
 
5740
    {
 
5741
    addr = addr_local;
 
5742
    which = US"local";
 
5743
    }
 
5744
  else if (addr_defer != NULL)
 
5745
    {
 
5746
    addr = addr_defer;
 
5747
    which = US"deferred";
 
5748
    }
 
5749
  else
 
5750
    {
 
5751
    addr = addr_failed;
 
5752
    which = US"failed";
 
5753
    }
 
5754
 
 
5755
  while (addr->parent != NULL) addr = addr->parent;
 
5756
 
 
5757
  if (addr->message != NULL)
 
5758
    {
 
5759
    colon = US": ";
 
5760
    msg = addr->message;
 
5761
    }
 
5762
  else colon = msg = US"";
 
5763
 
 
5764
  /* We don't need to log here for a forced failure as it will already
 
5765
  have been logged. Defer will also have been logged, but as a defer, so we do
 
5766
  need to do the failure logging. */
 
5767
 
 
5768
  if (addr != addr_failed)
 
5769
    log_write(0, LOG_MAIN, "** %s routing yielded a %s delivery",
 
5770
      addr->address, which);
 
5771
 
 
5772
  /* Always write an error to the caller */
 
5773
 
 
5774
  fprintf(stderr, "routing %s yielded a %s delivery%s%s\n", addr->address,
 
5775
    which, colon, msg);
 
5776
 
 
5777
  final_yield = DELIVER_MUA_FAILED;
 
5778
  addr_failed = addr_defer = NULL;   /* So that we remove the message */
 
5779
  goto DELIVERY_TIDYUP;
 
5780
  }
 
5781
 
 
5782
 
5528
5783
/* If this is a run to continue deliveries to an external channel that is
5529
5784
already set up, defer any local deliveries. */
5530
5785
 
5583
5838
    {
5584
5839
    log_write(0, LOG_MAIN|LOG_PANIC, "Couldn't open journal file %s: %s",
5585
5840
      spoolname, strerror(errno));
5586
 
    return FAIL;
 
5841
    return DELIVER_NOT_ATTEMPTED;
5587
5842
    }
5588
5843
 
5589
5844
  /* Set the close-on-exec flag, make the file owned by Exim, and ensure
5590
5845
  that the mode is correct - the group setting doesn't always seem to get
5591
5846
  set automatically. */
5592
5847
 
5593
 
  fcntl(journal_fd, F_SETFD, fcntl(journal_fd, F_GETFD) | FD_CLOEXEC);
5594
 
  fchown(journal_fd, exim_uid, exim_gid);
5595
 
  fchmod(journal_fd, SPOOL_MODE);
 
5848
  (void)fcntl(journal_fd, F_SETFD, fcntl(journal_fd, F_GETFD) | FD_CLOEXEC);
 
5849
  (void)fchown(journal_fd, exim_uid, exim_gid);
 
5850
  (void)fchmod(journal_fd, SPOOL_MODE);
5596
5851
  }
5597
5852
 
5598
5853
 
5650
5905
    regex_must_compile(US"\\n250[\\s\\-]STARTTLS(\\s|\\n|$)", FALSE, TRUE);
5651
5906
  #endif
5652
5907
 
5653
 
  /* Now sort the addresses if required, and do the deliveries. */
 
5908
  /* Now sort the addresses if required, and do the deliveries. The yield of
 
5909
  do_remote_deliveries is FALSE when mua_wrapper is set and all addresses
 
5910
  cannot be delivered in one transaction. */
5654
5911
 
5655
5912
  if (remote_sort_domains != NULL) sort_remote_deliveries();
5656
 
  do_remote_deliveries(FALSE);
 
5913
  if (!do_remote_deliveries(FALSE))
 
5914
    {
 
5915
    log_write(0, LOG_MAIN, "** mua_wrapper is set but recipients cannot all "
 
5916
      "be delivered in one transaction");
 
5917
    fprintf(stderr, "delivery to smarthost failed (configuration problem)\n");
 
5918
 
 
5919
    final_yield = DELIVER_MUA_FAILED;
 
5920
    addr_failed = addr_defer = NULL;   /* So that we remove the message */
 
5921
    goto DELIVERY_TIDYUP;
 
5922
    }
5657
5923
 
5658
5924
  /* See if any of the addresses that failed got put on the queue for delivery
5659
5925
  to their fallback hosts. We do it this way because often the same fallback
5660
5926
  host is used for many domains, so all can be sent in a single transaction
5661
5927
  (if appropriately configured). */
5662
5928
 
5663
 
  if (addr_fallback != NULL)
 
5929
  if (addr_fallback != NULL && !mua_wrapper)
5664
5930
    {
5665
5931
    DEBUG(D_deliver) debug_printf("Delivering to fallback hosts\n");
5666
5932
    addr_remote = addr_fallback;
5685
5951
set_process_info("tidying up after delivering %s", message_id);
5686
5952
signal(SIGTERM, SIG_IGN);
5687
5953
 
5688
 
/* First we update the retry database. This is done in one fell swoop at the
5689
 
end in order not to keep opening and closing (and locking) the database. The
5690
 
code for handling retries is hived off into a separate module for convenience.
5691
 
We pass it the addresses of the various chains, because deferred addresses can
5692
 
get moved onto the failed chain if the retry cutoff time has expired for all
5693
 
alternative destinations. Bypass the updating of the database if the -N flag is
5694
 
set, which is a debugging thing that prevents actual delivery. */
5695
 
 
5696
 
if (!dont_deliver) retry_update(&addr_defer, &addr_failed, &addr_succeed);
 
5954
/* When we are acting as an MUA wrapper, the smtp transport will either have
 
5955
succeeded for all addresses, or failed them all in normal cases. However, there
 
5956
are some setup situations (e.g. when a named port does not exist) that cause an
 
5957
immediate exit with deferral of all addresses. Convert those into failures. We
 
5958
do not ever want to retry, nor do we want to send a bounce message. */
 
5959
 
 
5960
if (mua_wrapper)
 
5961
  {
 
5962
  if (addr_defer != NULL)
 
5963
    {
 
5964
    address_item *addr, *nextaddr;
 
5965
    for (addr = addr_defer; addr != NULL; addr = nextaddr)
 
5966
      {
 
5967
      log_write(0, LOG_MAIN, "** %s mua_wrapper forced failure for deferred "
 
5968
        "delivery", addr->address);
 
5969
      nextaddr = addr->next;
 
5970
      addr->next = addr_failed;
 
5971
      addr_failed = addr;
 
5972
      }
 
5973
    addr_defer = NULL;
 
5974
    }
 
5975
 
 
5976
  /* Now all should either have succeeded or failed. */
 
5977
 
 
5978
  if (addr_failed == NULL) final_yield = DELIVER_MUA_SUCCEEDED; else
 
5979
    {
 
5980
    uschar *s = (addr_failed->user_message != NULL)?
 
5981
      addr_failed->user_message : addr_failed->message;
 
5982
 
 
5983
    fprintf(stderr, "Delivery failed: ");
 
5984
    if (addr_failed->basic_errno > 0)
 
5985
      {
 
5986
      fprintf(stderr, "%s", strerror(addr_failed->basic_errno));
 
5987
      if (s != NULL) fprintf(stderr, ": ");
 
5988
      }
 
5989
    if (s == NULL)
 
5990
      {
 
5991
      if (addr_failed->basic_errno <= 0) fprintf(stderr, "unknown error");
 
5992
      }
 
5993
    else fprintf(stderr, "%s", CS s);
 
5994
    fprintf(stderr, "\n");
 
5995
 
 
5996
    final_yield = DELIVER_MUA_FAILED;
 
5997
    addr_failed = NULL;
 
5998
    }
 
5999
  }
 
6000
 
 
6001
/* In a normal configuration, we now update the retry database. This is done in
 
6002
one fell swoop at the end in order not to keep opening and closing (and
 
6003
locking) the database. The code for handling retries is hived off into a
 
6004
separate module for convenience. We pass it the addresses of the various
 
6005
chains, because deferred addresses can get moved onto the failed chain if the
 
6006
retry cutoff time has expired for all alternative destinations. Bypass the
 
6007
updating of the database if the -N flag is set, which is a debugging thing that
 
6008
prevents actual delivery. */
 
6009
 
 
6010
else if (!dont_deliver) retry_update(&addr_defer, &addr_failed, &addr_succeed);
5697
6011
 
5698
6012
/* If any addresses failed, we must send a message to somebody, unless
5699
6013
af_ignore_error is set, in which case no action is taken. It is possible for
5913
6227
 
5914
6228
      /* Process the addresses, leaving them on the msgchain if they have a
5915
6229
      file name for a return message. (There has already been a check in
5916
 
      post_process_one() for the existence of data in the message file.) */
 
6230
      post_process_one() for the existence of data in the message file.) A TRUE
 
6231
      return from print_address_information() means that the address is not
 
6232
      hidden. */
5917
6233
 
5918
6234
      paddr = &msgchain;
5919
6235
      for (addr = msgchain; addr != NULL; addr = *paddr)
5920
6236
        {
5921
6237
        if (print_address_information(addr, f, US"  ", US"\n    ", US""))
5922
 
          {
5923
 
          /* A TRUE return from print_address_information() means that the
5924
 
          address is not hidden. If there is a return file, it has already
5925
 
          been checked to ensure it is not empty. Omit the bland "return
5926
 
          message generated" error, but otherwise include error information. */
5927
 
 
5928
 
          if (addr->return_file < 0 ||
5929
 
              addr->message == NULL ||
5930
 
              Ustrcmp(addr->message, "return message generated") != 0)
5931
 
            {
5932
 
            fprintf(f, "\n    ");
5933
 
            print_address_error(addr, f);
5934
 
            }
5935
 
          }
 
6238
          print_address_error(addr, f, US"");
5936
6239
 
5937
6240
        /* End the final line for the address */
5938
6241
 
6007
6310
          else
6008
6311
            {
6009
6312
            while ((ch = fgetc(fm)) != EOF) fputc(ch, f);
6010
 
            fclose(fm);
 
6313
            (void)fclose(fm);
6011
6314
            }
6012
6315
          Uunlink(addr->return_filename);
6013
6316
 
6060
6363
            if (emf_text != NULL) fprintf(f, "%s", CS emf_text); else
6061
6364
              {
6062
6365
              fprintf(f,
6063
 
"------ The body of the message is %d characters long; only the first\n"
6064
 
"------ %d or so are included here.\n", (int)statbuf.st_size, max);
 
6366
"------ The body of the message is " OFF_T_FMT " characters long; only the first\n"
 
6367
"------ %d or so are included here.\n", statbuf.st_size, max);
6065
6368
              }
6066
6369
            }
6067
6370
          }
6080
6383
        {
6081
6384
        emf_text = next_emf(emf, US"final");
6082
6385
        if (emf_text != NULL) fprintf(f, "%s", CS emf_text);
6083
 
        fclose(emf);
 
6386
        (void)fclose(emf);
6084
6387
        }
6085
6388
 
6086
6389
      /* Close the file, which should send an EOF to the child process
6087
6390
      that is receiving the message. Wait for it to finish. */
6088
6391
 
6089
 
      fclose(f);
 
6392
      (void)fclose(f);
6090
6393
      rc = child_close(pid, 0);     /* Waits for child to close, no timeout */
6091
6394
 
6092
6395
      /* In the test harness, let the child do it's thing first. */
6139
6442
 
6140
6443
disable_logging = FALSE;  /* In case left set */
6141
6444
 
 
6445
/* Come here from the mua_wrapper case if routing goes wrong */
 
6446
 
 
6447
DELIVERY_TIDYUP:
 
6448
 
6142
6449
/* If there are now no deferred addresses, we are done. Preserve the
6143
6450
message log if so configured, and we are using them. Otherwise, sling it.
6144
6451
Then delete the message itself. */
6178
6485
  sprintf(CS spoolname, "%s/input/%s/%s-H", spool_directory, message_subdir, id);
6179
6486
  if (Uunlink(spoolname) < 0)
6180
6487
    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to unlink %s", spoolname);
6181
 
  log_write(0, LOG_MAIN, "Completed");
 
6488
 
 
6489
  /* Log the end of this message, with queue time if requested. */
 
6490
 
 
6491
  if ((log_extra_selector & LX_queue_time_overall) != 0)
 
6492
    log_write(0, LOG_MAIN, "Completed QT=%s",
 
6493
      readconf_printtime(time(NULL) - received_time));
 
6494
  else
 
6495
    log_write(0, LOG_MAIN, "Completed");
6182
6496
  }
6183
6497
 
6184
6498
/* If there are deferred addresses, we are keeping this message because it is
6423
6737
            (addr_defer->next == NULL)? "is": "are");
6424
6738
          }
6425
6739
 
6426
 
        /* List the addresses. For any that are hidden, don't give the delay
6427
 
        reason, because it might expose that which is hidden. Also, do not give
6428
 
        "retry time not reached" because that isn't helpful. */
 
6740
        /* List the addresses, with error information if allowed */
6429
6741
 
6430
6742
        fprintf(f, "\n");
6431
6743
        while (addr_defer != NULL)
6432
6744
          {
6433
6745
          address_item *addr = addr_defer;
6434
6746
          addr_defer = addr->next;
6435
 
          if (print_address_information(addr, f, US"  ", US"\n    ", US"") &&
6436
 
              addr->basic_errno > ERRNO_RETRY_BASE)
6437
 
            {
6438
 
            fprintf(f, "\n    Delay reason: ");
6439
 
            print_address_error(addr, f);
6440
 
            }
 
6747
          if (print_address_information(addr, f, US"  ", US"\n    ", US""))
 
6748
            print_address_error(addr, f, US"Delay reason: ");
6441
6749
          fprintf(f, "\n");
6442
6750
          }
6443
6751
        fprintf(f, "\n");
6448
6756
          {
6449
6757
          wmf_text = next_emf(wmf, US"final");
6450
6758
          if (wmf_text != NULL) fprintf(f, "%s", CS wmf_text);
6451
 
          fclose(wmf);
 
6759
          (void)fclose(wmf);
6452
6760
          }
6453
6761
        else
6454
6762
          {
6462
6770
        /* Close and wait for child process to complete, without a timeout.
6463
6771
        If there's an error, don't update the count. */
6464
6772
 
6465
 
        fclose(f);
 
6773
        (void)fclose(f);
6466
6774
        if (child_close(pid, 0) == 0)
6467
6775
          {
6468
6776
          warning_count = count;
6545
6853
/* Finished with the message log. If the message is complete, it will have
6546
6854
been unlinked or renamed above. */
6547
6855
 
6548
 
if (message_logs) fclose(message_log);
 
6856
if (message_logs) (void)fclose(message_log);
6549
6857
 
6550
6858
/* Now we can close and remove the journal file. Its only purpose is to record
6551
6859
successfully completed deliveries asap so that this information doesn't get
6559
6867
message off the main spool if frozen and the option is set. It should get moved
6560
6868
at the next attempt, after the journal has been inspected. */
6561
6869
 
6562
 
if (journal_fd >= 0) close(journal_fd);
 
6870
if (journal_fd >= 0) (void)close(journal_fd);
6563
6871
 
6564
6872
if (remove_journal)
6565
6873
  {
6580
6888
will go away. Otherwise the message becomes available for another process
6581
6889
to try delivery. */
6582
6890
 
6583
 
close(deliver_datafile);
 
6891
(void)close(deliver_datafile);
6584
6892
deliver_datafile = -1;
6585
6893
DEBUG(D_deliver) debug_printf("end delivery of %s\n", id);
6586
6894
 
6591
6899
released. */
6592
6900
 
6593
6901
search_tidyup();
6594
 
return OK;
 
6902
return final_yield;
6595
6903
}
6596
6904
 
6597
6905
/* End of deliver.c */