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

« back to all changes in this revision

Viewing changes to src/exim.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/exim.c,v 1.22 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
 
194
196
 
195
197
/* This function is called by millisleep() and exim_wait_tick() to wait for a
196
198
period of time that may include a fraction of a second. The coding is somewhat
197
 
tedious...
 
199
tedious. We do not expect setitimer() ever to fail, but if it does, the process
 
200
will wait for ever, so we panic in this instance. (There was a case of this
 
201
when a bug in a function that calls milliwait() caused it to pass invalid data.
 
202
That's when I added the check. :-)
198
203
 
199
204
Argument:  an itimerval structure containing the interval
200
205
Returns:   nothing
208
213
(void)sigemptyset(&sigmask);                           /* Empty mask */
209
214
(void)sigaddset(&sigmask, SIGALRM);                    /* Add SIGALRM */
210
215
(void)sigprocmask(SIG_BLOCK, &sigmask, &old_sigmask);  /* Block SIGALRM */
211
 
(void)setitimer(ITIMER_REAL, itval, NULL);             /* Start timer */
 
216
if (setitimer(ITIMER_REAL, itval, NULL) < 0)           /* Start timer */
 
217
  log_write(0, LOG_MAIN|LOG_PANIC_DIE,
 
218
    "setitimer() failed: %s", strerror(errno));
212
219
(void)sigfillset(&sigmask);                            /* All signals */
213
220
(void)sigdelset(&sigmask, SIGALRM);                    /* Remove SIGALRM */
214
221
(void)sigsuspend(&sigmask);                            /* Until SIGALRM */
309
316
  itval.it_interval.tv_usec = 0;
310
317
  itval.it_value.tv_sec = then_tv->tv_sec - now_tv.tv_sec;
311
318
  itval.it_value.tv_usec = then_tv->tv_usec + resolution - now_true_usec;
 
319
 
 
320
  /* We know that, overall, "now" is less than or equal to "then". Therefore, a
 
321
  negative value for the microseconds is possible only in the case when "now"
 
322
  is more than a second less than "then". That means that itval.it_value.tv_sec
 
323
  is greater than zero. The following correction is therefore safe. */
 
324
 
 
325
  if (itval.it_value.tv_usec < 0)
 
326
    {
 
327
    itval.it_value.tv_usec += 1000000;
 
328
    itval.it_value.tv_sec -= 1;
 
329
    }
 
330
 
312
331
  DEBUG(D_transport|D_receive)
313
332
    {
314
333
    if (!running_in_test_harness)
319
338
        itval.it_value.tv_usec);
320
339
      }
321
340
    }
 
341
 
322
342
  milliwait(&itval);
323
343
  }
324
344
}
366
386
This function ensures that fds 0, 1, and 2 are open if they do not already
367
387
exist, by connecting them to /dev/null.
368
388
 
 
389
This function is also used to ensure that std{in,out,err} exist at all times,
 
390
so that if any library that Exim calls tries to use them, it doesn't crash.
 
391
 
369
392
Arguments:  None
370
393
Returns:    Nothing
371
394
*/
383
406
    if (devnull < 0) devnull = open("/dev/null", O_RDWR);
384
407
    if (devnull < 0) log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s",
385
408
      string_open_failed(errno, "/dev/null"));
386
 
    if (devnull != i) dup2(devnull, i);
 
409
    if (devnull != i) (void)dup2(devnull, i);
387
410
    }
388
411
  }
389
 
if (devnull > 2) close(devnull);
 
412
if (devnull > 2) (void)close(devnull);
390
413
}
391
414
 
392
415
 
415
438
process to terminate. If they aren't closed, they hold up the calling process
416
439
until the initial delivery process finishes, which is not what we want.
417
440
 
418
 
However, if D_resolver is set, implying resolver debugging, leave stdout open,
419
 
because that's where the resolver writes its debugging output.
 
441
Exception: We do want it for synchronous delivery!
 
442
 
 
443
And notwithstanding all the above, if D_resolver is set, implying resolver
 
444
debugging, leave stdout open, because that's where the resolver writes its
 
445
debugging output.
420
446
 
421
447
When we close stderr (which implies we've also closed stdout), we also get rid
422
448
of any controlling terminal.
433
459
  #ifdef SUPPORT_TLS
434
460
  tls_close(FALSE);      /* Shut down the TLS library */
435
461
  #endif
436
 
  close(fileno(smtp_in));
437
 
  close(fileno(smtp_out));
 
462
  (void)close(fileno(smtp_in));
 
463
  (void)close(fileno(smtp_out));
438
464
  smtp_in = NULL;
439
465
  }
440
466
else
441
467
  {
442
 
  close(0);                                           /* stdin */
443
 
  if ((debug_selector & D_resolver) == 0)  close(1);  /* stdout */
444
 
  if (debug_selector == 0)                            /* stderr */
 
468
  (void)close(0);                                          /* stdin */
 
469
  if ((debug_selector & D_resolver) == 0) (void)close(1);  /* stdout */
 
470
  if (debug_selector == 0)                                 /* stderr */
445
471
    {
446
 
    close(2);
447
 
    log_stderr = NULL;
 
472
    if (!synchronous_delivery)
 
473
      {
 
474
      (void)close(2);
 
475
      log_stderr = NULL;
 
476
      }
448
477
    (void)setsid();
449
478
    }
450
479
  }
557
586
*************************************************/
558
587
 
559
588
/* Called to extract the port from the values given to -oMa and -oMi.
560
 
It also checks the syntax of the address.
 
589
It also checks the syntax of the address, and terminates it before the
 
590
port data when a port is extracted.
561
591
 
562
592
Argument:
563
593
  address   the address, with possible port on the end
570
600
check_port(uschar *address)
571
601
{
572
602
int port = host_extract_port(address);
573
 
if (!string_is_ip_address(address, NULL))
 
603
if (string_is_ip_address(address, NULL) == 0)
574
604
  {
575
605
  fprintf(stderr, "exim abandoned: \"%s\" is not an IP address\n", address);
576
606
  exit(EXIT_FAILURE);
593
623
  flags        flag bits for verify_address()
594
624
  exit_value   to be set for failures
595
625
 
596
 
Returns:       nothint
 
626
Returns:       nothing
597
627
*/
598
628
 
599
629
static void
611
641
else
612
642
  {
613
643
  int rc = verify_address(deliver_make_addr(address,TRUE), stdout, flags, -1,
614
 
    NULL);
 
644
    -1, -1, NULL, NULL, NULL);
615
645
  if (rc == FAIL) *exit_value = 2;
616
646
    else if (rc == DEFER && *exit_value == 0) *exit_value = 1;
617
647
  }
633
663
for one). The top bit in the table means "put in 2nd selector". This does not
634
664
yet apply to debug options, so the "=" facility sets only the first selector.
635
665
 
 
666
The "all" selector, which must be equal to 0xffffffff, is recognized specially.
 
667
It sets all the bits in both selectors. However, there is a facility for then
 
668
unsetting certain bits, because we want to turn off "memory" in the debug case.
 
669
 
636
670
A bad value for a debug setting is treated as an unknown option - error message
637
671
to stderr and die. For log settings, which come from the configuration file,
638
672
we write to the log on the way out...
640
674
Arguments:
641
675
  selector1      address of the first bit string
642
676
  selector2      address of the second bit string, or NULL
 
677
  notall1        bits to exclude from "all" for selector1
 
678
  notall2        bits to exclude from "all" for selector2
643
679
  string         the configured string
644
680
  options        the table of option names
645
681
  count          size of table
649
685
*/
650
686
 
651
687
static void
652
 
decode_bits(unsigned int *selector1, unsigned int *selector2, uschar *string,
653
 
  bit_table *options, int count, uschar *which)
 
688
decode_bits(unsigned int *selector1, unsigned int *selector2, int notall1,
 
689
  int notall2, uschar *string, bit_table *options, int count, uschar *which)
654
690
{
655
691
uschar *errmsg;
656
692
if (string == NULL) return;
703
739
        unsigned int bit = middle->bit;
704
740
        unsigned int *selector;
705
741
 
706
 
        /* The value with all bits set means "set all bits in both selectors"
 
742
        /* The value with all bits set means "force all bits in both selectors"
707
743
        in the case where two are being handled. However, the top bit in the
708
 
        second selector is never set. */
 
744
        second selector is never set. When setting, some bits can be excluded.
 
745
        */
709
746
 
710
747
        if (bit == 0xffffffff)
711
748
          {
712
 
          *selector1 = adding? bit : 0;
713
 
          if (selector2 != NULL) *selector2 = adding? 0x7fffffff : 0;
 
749
          if (adding)
 
750
            {
 
751
            *selector1 = 0xffffffff ^ notall1;
 
752
            if (selector2 != NULL) *selector2 = 0x7fffffff ^ notall2;
 
753
            }
 
754
          else
 
755
            {
 
756
            *selector1 = 0;
 
757
            if (selector2 != NULL) *selector2 = 0;
 
758
            }
714
759
          }
715
760
 
716
761
        /* Otherwise, the 0x80000000 bit means "this value, without the top
800
845
#ifdef EXIM_PERL
801
846
  fprintf(f, " Perl");
802
847
#endif
 
848
#ifdef EXPAND_DLFUNC
 
849
  fprintf(f, " Expand_dlfunc");
 
850
#endif
803
851
#ifdef USE_TCP_WRAPPERS
804
852
  fprintf(f, " TCPwrappers");
805
853
#endif
810
858
  fprintf(f, " OpenSSL");
811
859
  #endif
812
860
#endif
 
861
#ifdef WITH_CONTENT_SCAN
 
862
  fprintf(f, " Content_Scanning");
 
863
#endif
 
864
#ifdef WITH_OLD_DEMIME
 
865
  fprintf(f, " Old_Demime");
 
866
#endif
 
867
#ifdef EXPERIMENTAL_SPF
 
868
  fprintf(f, " Experimental_SPF");
 
869
#endif
 
870
#ifdef EXPERIMENTAL_SRS
 
871
  fprintf(f, " Experimental_SRS");
 
872
#endif
 
873
#ifdef EXPERIMENTAL_BRIGHTMAIL
 
874
  fprintf(f, " Experimental_Brightmail");
 
875
#endif
 
876
#ifdef EXPERIMENTAL_DOMAINKEYS
 
877
  fprintf(f, " Experimental_DomainKeys");
 
878
#endif
813
879
fprintf(f, "\n");
814
880
 
815
881
fprintf(f, "Lookups:");
864
930
#ifdef AUTH_CRAM_MD5
865
931
  fprintf(f, " cram_md5");
866
932
#endif
 
933
#ifdef AUTH_CYRUS_SASL
 
934
  fprintf(f, " cyrus_sasl");
 
935
#endif
867
936
#ifdef AUTH_PLAINTEXT
868
937
  fprintf(f, " plaintext");
869
938
#endif
937
1006
 
938
1007
 
939
1008
/*************************************************
 
1009
*               Quote a local part               *
 
1010
*************************************************/
 
1011
 
 
1012
/* This function is used when a sender address or a From: or Sender: header
 
1013
line is being created from the caller's login, or from an authenticated_id. It
 
1014
applies appropriate quoting rules for a local part.
 
1015
 
 
1016
Argument:    the local part
 
1017
Returns:     the local part, quoted if necessary
 
1018
*/
 
1019
 
 
1020
uschar *
 
1021
local_part_quote(uschar *lpart)
 
1022
{
 
1023
BOOL needs_quote = FALSE;
 
1024
int size, ptr;
 
1025
uschar *yield;
 
1026
uschar *t;
 
1027
 
 
1028
for (t = lpart; !needs_quote && *t != 0; t++)
 
1029
  {
 
1030
  needs_quote = !isalnum(*t) && strchr("!#$%&'*+-/=?^_`{|}~", *t) == NULL &&
 
1031
    (*t != '.' || t == lpart || t[1] == 0);
 
1032
  }
 
1033
 
 
1034
if (!needs_quote) return lpart;
 
1035
 
 
1036
size = ptr = 0;
 
1037
yield = string_cat(NULL, &size, &ptr, US"\"", 1);
 
1038
 
 
1039
for (;;)
 
1040
  {
 
1041
  uschar *nq = US Ustrpbrk(lpart, "\\\"");
 
1042
  if (nq == NULL)
 
1043
    {
 
1044
    yield = string_cat(yield, &size, &ptr, lpart, Ustrlen(lpart));
 
1045
    break;
 
1046
    }
 
1047
  yield = string_cat(yield, &size, &ptr, lpart, nq - lpart);
 
1048
  yield = string_cat(yield, &size, &ptr, US"\\", 1);
 
1049
  yield = string_cat(yield, &size, &ptr, nq, 1);
 
1050
  lpart = nq + 1;
 
1051
  }
 
1052
 
 
1053
yield = string_cat(yield, &size, &ptr, US"\"", 1);
 
1054
yield[ptr] = 0;
 
1055
return yield;
 
1056
}
 
1057
 
 
1058
 
 
1059
 
 
1060
#ifdef USE_READLINE
 
1061
/*************************************************
 
1062
*         Load readline() functions              *
 
1063
*************************************************/
 
1064
 
 
1065
/* This function is called from testing executions that read data from stdin,
 
1066
but only when running as the calling user. Currently, only -be does this. The
 
1067
function loads the readline() function library and passes back the functions.
 
1068
On some systems, it needs the curses library, so load that too, but try without
 
1069
it if loading fails. All this functionality has to be requested at build time.
 
1070
 
 
1071
Arguments:
 
1072
  fn_readline_ptr   pointer to where to put the readline pointer
 
1073
  fn_addhist_ptr    pointer to where to put the addhistory function
 
1074
 
 
1075
Returns:            the dlopen handle or NULL on failure
 
1076
*/
 
1077
 
 
1078
static void *
 
1079
set_readline(char * (**fn_readline_ptr)(char *),
 
1080
             char * (**fn_addhist_ptr)(char *))
 
1081
{
 
1082
void *dlhandle;
 
1083
void *dlhandle_curses = dlopen("libcurses.so", RTLD_GLOBAL|RTLD_LAZY);
 
1084
 
 
1085
dlhandle = dlopen("libreadline.so", RTLD_GLOBAL|RTLD_NOW);
 
1086
if (dlhandle_curses != NULL) dlclose(dlhandle_curses);
 
1087
 
 
1088
if (dlhandle != NULL)
 
1089
  {
 
1090
  *fn_readline_ptr = (char *(*)(char*))dlsym(dlhandle, "readline");
 
1091
  *fn_addhist_ptr = (char *(*)(char*))dlsym(dlhandle, "add_history");
 
1092
  }
 
1093
else
 
1094
  {
 
1095
  DEBUG(D_any) debug_printf("failed to load readline: %s\n", dlerror());
 
1096
  }
 
1097
 
 
1098
return dlhandle;
 
1099
}
 
1100
#endif
 
1101
 
 
1102
 
 
1103
 
 
1104
/*************************************************
 
1105
*    Get a line from stdin for testing things    *
 
1106
*************************************************/
 
1107
 
 
1108
/* This function is called when running tests that can take a number of lines
 
1109
of input (for example, -be and -bt). It handles continuations and trailing
 
1110
spaces. And prompting and a blank line output on eof. If readline() is in use,
 
1111
the arguments are non-NULL and provide the relevant functions.
 
1112
 
 
1113
Arguments:
 
1114
  fn_readline   readline function or NULL
 
1115
  fn_addhist    addhist function or NULL
 
1116
 
 
1117
Returns:        pointer to dynamic memory, or NULL at end of file
 
1118
*/
 
1119
 
 
1120
static uschar *
 
1121
get_stdinput(char *(*fn_readline)(char *), char *(*fn_addhist)(char *))
 
1122
{
 
1123
int i;
 
1124
int size = 0;
 
1125
int ptr = 0;
 
1126
uschar *yield = NULL;
 
1127
 
 
1128
if (fn_readline == NULL) printf("> ");
 
1129
 
 
1130
for (i = 0;; i++)
 
1131
  {
 
1132
  uschar buffer[1024];
 
1133
  uschar *p, *ss;
 
1134
 
 
1135
  #ifdef USE_READLINE
 
1136
  char *readline_line = NULL;
 
1137
  if (fn_readline != NULL)
 
1138
    {
 
1139
    if ((readline_line = fn_readline((i > 0)? "":"> ")) == NULL) break;
 
1140
    if (*readline_line != 0 && fn_addhist != NULL) fn_addhist(readline_line);
 
1141
    p = US readline_line;
 
1142
    }
 
1143
  else
 
1144
  #endif
 
1145
 
 
1146
  /* readline() not in use */
 
1147
 
 
1148
    {
 
1149
    if (Ufgets(buffer, sizeof(buffer), stdin) == NULL) break;
 
1150
    p = buffer;
 
1151
    }
 
1152
 
 
1153
  /* Handle the line */
 
1154
 
 
1155
  ss = p + (int)Ustrlen(p);
 
1156
  while (ss > p && isspace(ss[-1])) ss--;
 
1157
 
 
1158
  if (i > 0)
 
1159
    {
 
1160
    while (p < ss && isspace(*p)) p++;   /* leading space after cont */
 
1161
    }
 
1162
 
 
1163
  yield = string_cat(yield, &size, &ptr, p, ss - p);
 
1164
 
 
1165
  #ifdef USE_READLINE
 
1166
  if (fn_readline != NULL) free(readline_line);
 
1167
  #endif
 
1168
 
 
1169
  if (ss == p || yield[ptr-1] != '\\')
 
1170
    {
 
1171
    yield[ptr] = 0;
 
1172
    break;
 
1173
    }
 
1174
  yield[--ptr] = 0;
 
1175
  }
 
1176
 
 
1177
if (yield == NULL) printf("\n");
 
1178
return yield;
 
1179
}
 
1180
 
 
1181
 
 
1182
 
 
1183
/*************************************************
940
1184
*          Entry point and high-level code       *
941
1185
*************************************************/
942
1186
 
962
1206
int  arg_receive_timeout = -1;
963
1207
int  arg_smtp_receive_timeout = -1;
964
1208
int  arg_error_handling = error_handling;
965
 
int  filter_fd = -1;
 
1209
int  filter_sfd = -1;
 
1210
int  filter_ufd = -1;
966
1211
int  group_count;
967
1212
int  i;
968
1213
int  list_queue_option = 0;
1008
1253
uschar *ftest_suffix = NULL;
1009
1254
uschar *real_sender_address;
1010
1255
uschar *originator_home = US"/";
1011
 
BOOL ftest_system = FALSE;
1012
1256
void *reset_point;
1013
1257
 
1014
1258
struct passwd *pw;
1027
1271
 
1028
1272
extern char **environ;
1029
1273
 
1030
 
/* If the Exim user and/or group were defined by ref:name at build time, we
1031
 
must now find the actual uid/gid values. This is a feature to make the lives of
1032
 
binary distributors easier. */
 
1274
/* If the Exim user and/or group and/or the configuration file owner/group were
 
1275
defined by ref:name at build time, we must now find the actual uid/gid values.
 
1276
This is a feature to make the lives of binary distributors easier. */
1033
1277
 
1034
1278
#ifdef EXIM_USERNAME
1035
1279
if (route_finduser(US EXIM_USERNAME, &pw, &exim_uid))
1053
1297
  }
1054
1298
#endif
1055
1299
 
 
1300
#ifdef CONFIGURE_OWNERNAME
 
1301
if (!route_finduser(US CONFIGURE_OWNERNAME, NULL, &config_uid))
 
1302
  {
 
1303
  fprintf(stderr, "exim: failed to find uid for user name \"%s\"\n",
 
1304
    CONFIGURE_OWNERNAME);
 
1305
  exit(EXIT_FAILURE);
 
1306
  }
 
1307
#endif
 
1308
 
 
1309
#ifdef CONFIGURE_GROUPNAME
 
1310
if (!route_findgroup(US CONFIGURE_GROUPNAME, &config_gid))
 
1311
  {
 
1312
  fprintf(stderr, "exim: failed to find gid for group name \"%s\"\n",
 
1313
    CONFIGURE_GROUPNAME);
 
1314
  exit(EXIT_FAILURE);
 
1315
  }
 
1316
#endif
 
1317
 
1056
1318
/* In the Cygwin environment, some initialization needs doing. It is fudged
1057
1319
in by means of this macro. */
1058
1320
 
1356
1618
    else if (*argrest == 'e')
1357
1619
      expansion_test = checking = TRUE;
1358
1620
 
1359
 
    /* -bf:  Run in mail filter testing mode
1360
 
       -bF:  Ditto, but for system filters
 
1621
    /* -bF:  Run system filter test */
 
1622
 
 
1623
    else if (*argrest == 'F')
 
1624
      {
 
1625
      filter_test |= FTEST_SYSTEM;
 
1626
      if (*(++argrest) != 0) { badarg = TRUE; break; }
 
1627
      if (++i < argc) filter_test_sfile = argv[i]; else
 
1628
        {
 
1629
        fprintf(stderr, "exim: file name expected after %s\n", argv[i-1]);
 
1630
        exit(EXIT_FAILURE);
 
1631
        }
 
1632
      }
 
1633
 
 
1634
    /* -bf:  Run user filter test
1361
1635
       -bfd: Set domain for filter testing
1362
1636
       -bfl: Set local part for filter testing
1363
1637
       -bfp: Set prefix for filter testing
1364
1638
       -bfs: Set suffix for filter testing
1365
1639
    */
1366
1640
 
1367
 
    else if (*argrest == 'f' || *argrest == 'F')
 
1641
    else if (*argrest == 'f')
1368
1642
      {
1369
 
      ftest_system = *argrest++ == 'F';
1370
 
      if (*argrest == 0)
 
1643
      if (*(++argrest) == 0)
1371
1644
        {
1372
 
        if(++i < argc) filter_test = argv[i]; else
 
1645
        filter_test |= FTEST_USER;
 
1646
        if (++i < argc) filter_test_ufile = argv[i]; else
1373
1647
          {
1374
1648
          fprintf(stderr, "exim: file name expected after %s\n", argv[i-1]);
1375
1649
          exit(EXIT_FAILURE);
1638
1912
    break;
1639
1913
 
1640
1914
    /* -d: Set debug level (see also -v below) or set the drop_cr option.
1641
 
    The latter is now a no-opt, retained for compatibility only. */
 
1915
    The latter is now a no-op, retained for compatibility only. If -dd is used,
 
1916
    debugging subprocesses of the daemon is disabled. */
1642
1917
 
1643
1918
    case 'd':
1644
1919
    if (Ustrcmp(argrest, "ropcr") == 0)
1654
1929
      unsigned int selector = D_default;
1655
1930
      debug_selector = 0;
1656
1931
      debug_file = NULL;
 
1932
      if (*argrest == 'd')
 
1933
        {
 
1934
        debug_daemon = TRUE;
 
1935
        argrest++;
 
1936
        }
1657
1937
      if (*argrest != 0)
1658
 
        decode_bits(&selector, NULL, argrest, debug_options,
 
1938
        decode_bits(&selector, NULL, D_memory, 0, argrest, debug_options,
1659
1939
          debug_options_count, US"debug");
1660
1940
      debug_selector = selector;
1661
1941
      }
2530
2810
    (smtp_input || extract_recipients || recipients_arg < argc) &&
2531
2811
    (daemon_listen || queue_interval >= 0 || bi_option ||
2532
2812
      test_retry_arg >= 0 || test_rewrite_arg >= 0 ||
2533
 
      filter_test != NULL || (msg_action_arg > 0 && !one_msg_action))
 
2813
      filter_test != FTEST_NONE || (msg_action_arg > 0 && !one_msg_action))
2534
2814
    ) ||
2535
2815
    (
2536
2816
    msg_action_arg > 0 &&
2548
2828
    (
2549
2829
    list_options &&
2550
2830
    (checking || smtp_input || extract_recipients ||
2551
 
      filter_test != NULL || bi_option)
 
2831
      filter_test != FTEST_NONE || bi_option)
2552
2832
    ) ||
2553
2833
    (
2554
2834
    verify_address_mode &&
2555
2835
    (address_test_mode || smtp_input || extract_recipients ||
2556
 
      filter_test != NULL || bi_option)
 
2836
      filter_test != FTEST_NONE || bi_option)
2557
2837
    ) ||
2558
2838
    (
2559
2839
    address_test_mode && (smtp_input || extract_recipients ||
2560
 
      filter_test != NULL || bi_option)
 
2840
      filter_test != FTEST_NONE || bi_option)
2561
2841
    ) ||
2562
2842
    (
2563
 
    smtp_input && (sender_address != NULL || filter_test != NULL ||
 
2843
    smtp_input && (sender_address != NULL || filter_test != FTEST_NONE ||
2564
2844
      extract_recipients)
2565
2845
    ) ||
2566
2846
    (
2604
2884
else
2605
2885
  {
2606
2886
  struct rlimit rlp;
 
2887
 
2607
2888
  #ifdef RLIMIT_NOFILE
2608
2889
  if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
 
2890
    {
 
2891
    log_write(0, LOG_MAIN|LOG_PANIC, "getrlimit(RLIMIT_NOFILE) failed: %s",
 
2892
      strerror(errno));
2609
2893
    rlp.rlim_cur = rlp.rlim_max = 0;
 
2894
    }
 
2895
 
 
2896
  /* I originally chose 1000 as a nice big number that was unlikely to
 
2897
  be exceeded. It turns out that some older OS have a fixed upper limit of
 
2898
  256. */
 
2899
 
2610
2900
  if (rlp.rlim_cur < 1000)
2611
2901
    {
2612
2902
    rlp.rlim_cur = rlp.rlim_max = 1000;
2613
 
    (void)setrlimit(RLIMIT_NOFILE, &rlp);
 
2903
    if (setrlimit(RLIMIT_NOFILE, &rlp) < 0)
 
2904
      {
 
2905
      rlp.rlim_cur = rlp.rlim_max = 256;
 
2906
      if (setrlimit(RLIMIT_NOFILE, &rlp) < 0)
 
2907
        log_write(0, LOG_MAIN|LOG_PANIC, "setrlimit(RLIMIT_NOFILE) failed: %s",
 
2908
          strerror(errno));
 
2909
      }
2614
2910
    }
2615
2911
  #endif
 
2912
 
2616
2913
  #ifdef RLIMIT_NPROC
2617
 
    #ifdef RLIM_INFINITY
 
2914
  if (getrlimit(RLIMIT_NPROC, &rlp) < 0)
 
2915
    {
 
2916
    log_write(0, LOG_MAIN|LOG_PANIC, "getrlimit(RLIMIT_NPROC) failed: %s",
 
2917
      strerror(errno));
 
2918
    rlp.rlim_cur = rlp.rlim_max = 0;
 
2919
    }
 
2920
 
 
2921
  #ifdef RLIM_INFINITY
 
2922
  if (rlp.rlim_cur != RLIM_INFINITY && rlp.rlim_cur < 1000)
 
2923
    {
2618
2924
    rlp.rlim_cur = rlp.rlim_max = RLIM_INFINITY;
2619
 
    #else
 
2925
  #else
 
2926
  if (rlp.rlim_cur < 1000)
 
2927
    {
2620
2928
    rlp.rlim_cur = rlp.rlim_max = 1000;
2621
 
    #endif
2622
 
  (void)setrlimit(RLIMIT_NPROC, &rlp);
 
2929
  #endif
 
2930
    if (setrlimit(RLIMIT_NPROC, &rlp) < 0)
 
2931
      log_write(0, LOG_MAIN|LOG_PANIC, "setrlimit(RLIMIT_NPROC) failed: %s",
 
2932
        strerror(errno));
 
2933
    }
2623
2934
  #endif
2624
2935
  }
2625
2936
 
2689
3000
    ) ||                                         /*   OR   */
2690
3001
    expansion_test                               /* expansion testing */
2691
3002
    ||                                           /*   OR   */
2692
 
    filter_test != NULL)                         /* Filter testing */
 
3003
    filter_test != FTEST_NONE)                   /* Filter testing */
2693
3004
  {
2694
3005
  setgroups(group_count, group_list);
2695
3006
  exim_setugid(real_uid, real_gid, FALSE,
2712
3023
 
2713
3024
else exim_setugid(geteuid(), getegid(), FALSE, US"forcing real = effective");
2714
3025
 
2715
 
/* If testing a filter, open the file now, before wasting time doing other
 
3026
/* If testing a filter, open the file(s) now, before wasting time doing other
2716
3027
setups and reading the message. */
2717
3028
 
2718
 
if (filter_test != NULL)
2719
 
  {
2720
 
  filter_fd = Uopen(filter_test, O_RDONLY,0);
2721
 
  if (filter_fd < 0)
2722
 
    {
2723
 
    fprintf(stderr, "exim: failed to open %s: %s\n", filter_test,
 
3029
if ((filter_test & FTEST_SYSTEM) != 0)
 
3030
  {
 
3031
  filter_sfd = Uopen(filter_test_sfile, O_RDONLY, 0);
 
3032
  if (filter_sfd < 0)
 
3033
    {
 
3034
    fprintf(stderr, "exim: failed to open %s: %s\n", filter_test_sfile,
 
3035
      strerror(errno));
 
3036
    return EXIT_FAILURE;
 
3037
    }
 
3038
  }
 
3039
 
 
3040
if ((filter_test & FTEST_USER) != 0)
 
3041
  {
 
3042
  filter_ufd = Uopen(filter_test_ufile, O_RDONLY, 0);
 
3043
  if (filter_ufd < 0)
 
3044
    {
 
3045
    fprintf(stderr, "exim: failed to open %s: %s\n", filter_test_ufile,
2724
3046
      strerror(errno));
2725
3047
    return EXIT_FAILURE;
2726
3048
    }
2734
3056
 
2735
3057
/* Handle the decoding of logging options. */
2736
3058
 
2737
 
decode_bits(&log_write_selector, &log_extra_selector, log_selector_string,
 
3059
decode_bits(&log_write_selector, &log_extra_selector, 0, 0, log_selector_string,
2738
3060
  log_options, log_options_count, US"log");
2739
3061
 
2740
3062
DEBUG(D_any)
2931
3253
Don't attempt it if logging is disabled, or if listing variables or if
2932
3254
verifying/testing addresses or expansions. */
2933
3255
 
2934
 
if ((log_extra_selector & LX_arguments) != 0 && really_exim
2935
 
     && !list_options && !checking)
 
3256
if (((debug_selector & D_any) != 0 || (log_extra_selector & LX_arguments) != 0)
 
3257
      && really_exim && !list_options && !checking)
2936
3258
  {
2937
3259
  int i;
2938
3260
  uschar *p = big_buffer;
2960
3282
      quote = US"";
2961
3283
      while (*pp != 0) if (isspace(*pp++)) { quote = US"\""; break; }
2962
3284
      }
2963
 
    sprintf(CS p, " %s%.*s%s", quote, big_buffer_size - (p - big_buffer) - 4,
2964
 
      printing, quote);
 
3285
    sprintf(CS p, " %s%.*s%s", quote, (int)(big_buffer_size -
 
3286
      (p - big_buffer) - 4), printing, quote);
2965
3287
    while (*p) p++;
2966
3288
    }
2967
 
  log_write(0, LOG_MAIN, "%s", big_buffer);
 
3289
 
 
3290
  if ((log_extra_selector & LX_arguments) != 0)
 
3291
    log_write(0, LOG_MAIN, "%s", big_buffer);
 
3292
  else
 
3293
    debug_printf("%s\n", big_buffer);
2968
3294
  }
2969
3295
 
2970
3296
/* Set the working directory to be the top-level spool directory. We don't rely
2987
3313
 
2988
3314
if (bi_option)
2989
3315
  {
2990
 
  fclose(config_file);
 
3316
  (void)fclose(config_file);
2991
3317
  if (bi_command != NULL)
2992
3318
    {
2993
3319
    int i = 0;
3115
3441
  }
3116
3442
 
3117
3443
/* If the caller is not trusted, certain arguments are ignored when running for
3118
 
real, but are permitted when checking things (-be, -bv, -bt, -bh, -bf). Note
3119
 
that authority for performing certain actions on messages is tested in the
 
3444
real, but are permitted when checking things (-be, -bv, -bt, -bh, -bf, -bF).
 
3445
Note that authority for performing certain actions on messages is tested in the
3120
3446
queue_action() function. */
3121
3447
 
3122
 
if (!trusted_caller && !checking && filter_test == NULL)
 
3448
if (!trusted_caller && !checking && filter_test == FTEST_NONE)
3123
3449
  {
3124
3450
  sender_host_name = sender_host_address = interface_address =
3125
3451
    sender_ident = received_protocol = NULL;
3147
3473
if (smtp_input)
3148
3474
  {
3149
3475
  union sockaddr_46 inetd_sock;
3150
 
  SOCKLEN_T size = sizeof(inetd_sock);
 
3476
  EXIM_SOCKLEN_T size = sizeof(inetd_sock);
3151
3477
  if (getpeername(0, (struct sockaddr *)(&inetd_sock), &size) == 0)
3152
3478
    {
3153
3479
    int family = ((struct sockaddr *)(&inetd_sock))->sa_family;
3160
3486
        interface_address = host_ntoa(-1, &interface_sock, NULL,
3161
3487
          &interface_port);
3162
3488
 
 
3489
      if (host_is_tls_on_connect_port(interface_port)) tls_on_connect = TRUE;
 
3490
 
3163
3491
      if (real_uid == root_uid || real_uid == exim_uid || interface_port < 1024)
3164
3492
        {
3165
3493
        is_inetd = TRUE;
3166
3494
        sender_host_address = host_ntoa(-1, (struct sockaddr *)(&inetd_sock),
3167
3495
          NULL, &sender_host_port);
 
3496
        if (mua_wrapper) log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Input from "
 
3497
          "inetd is not supported when mua_wrapper is set");
3168
3498
        }
3169
3499
      else
3170
3500
        {
3280
3610
 
3281
3611
readconf_rest(msg_action_arg > 0 || (queue_interval == 0 && !daemon_listen));
3282
3612
 
3283
 
/* The configuration data will have been read into POOL_PERM because we won'd
 
3613
/* The configuration data will have been read into POOL_PERM because we won't
3284
3614
ever want to reset back past it. Change the current pool to POOL_MAIN. In fact,
3285
3615
this is just a bit of pedantic tidiness. It wouldn't really matter if the
3286
3616
configuration were read into POOL_MAIN, because we don't do any resets till
3336
3666
      printf("%s\n", CS error);
3337
3667
      return EXIT_FAILURE;
3338
3668
      }
 
3669
 
 
3670
    /* For the rcpt_4xx errors, a value of 255 means "any", and a code > 100 as
 
3671
    an error is for matching codes to the decade. Turn them into a real error
 
3672
    code, off the decade. */
 
3673
 
 
3674
    if (basic_errno == ERRNO_RCPT4XX)
 
3675
      {
 
3676
      int code = (more_errno >> 8) & 255;
 
3677
      if (code == 255)
 
3678
        more_errno = (more_errno & 0xffff00ff) | (21 << 8);
 
3679
      else if (code > 100)
 
3680
        more_errno = (more_errno & 0xffff00ff) | ((code - 96) << 8);
 
3681
      }
3339
3682
    }
3340
3683
 
3341
3684
  yield = retry_find_config(s1, s2, basic_errno, more_errno);
3482
3825
may also be used to construct a from: or a sender: header, and in this case we
3483
3826
need the user's full name as well, so save a copy of it, checked for RFC822
3484
3827
syntax and munged if necessary, if it hasn't previously been set by the -F
3485
 
argument. We try to get the passwd entry more than once, in case NIS or other
3486
 
delays are in evidence. Save the home directory for use in filter testing
 
3828
argument. We may try to get the passwd entry more than once, in case NIS or
 
3829
other delays are in evidence. Save the home directory for use in filter testing
3487
3830
(only). */
3488
3831
 
3489
 
for (i = 1; i <= 10; i++)
 
3832
for (i = 0;;)
3490
3833
  {
3491
3834
  if ((pw = getpwuid(real_uid)) != NULL)
3492
3835
    {
3499
3842
    if (originator_name == NULL)
3500
3843
      {
3501
3844
      if (sender_address == NULL ||
3502
 
           (!trusted_caller && filter_test == NULL))
 
3845
           (!trusted_caller && filter_test == FTEST_NONE))
3503
3846
        {
3504
3847
        uschar *name = US pw->pw_gecos;
3505
3848
        uschar *amp = Ustrchr(name, '&');
3555
3898
 
3556
3899
    break;
3557
3900
    }
 
3901
 
 
3902
  if (++i > finduser_retries) break;
3558
3903
  sleep(1);
3559
3904
  }
3560
3905
 
3561
3906
/* If we cannot get a user login, log the incident and give up, unless the
3562
 
configuration specifies something to use. */
 
3907
configuration specifies something to use. When running in the test harness,
 
3908
any setting of unknown_login overrides the actual login name. */
3563
3909
 
3564
 
if (originator_login == NULL)
 
3910
if (originator_login == NULL || running_in_test_harness)
3565
3911
  {
3566
3912
  if (unknown_login != NULL)
3567
3913
    {
3593
3939
 
3594
3940
/* Run in daemon and/or queue-running mode. The function daemon_go() never
3595
3941
returns. We leave this till here so that the originator_ fields are available
3596
 
for incoming messages via the daemon. */
 
3942
for incoming messages via the daemon. The daemon cannot be run in mua_wrapper
 
3943
mode. */
3597
3944
 
3598
 
if (daemon_listen || queue_interval > 0) daemon_go();
 
3945
if (daemon_listen || queue_interval > 0)
 
3946
  {
 
3947
  if (mua_wrapper)
 
3948
    {
 
3949
    fprintf(stderr, "Daemon cannot be run when mua_wrapper is set\n");
 
3950
    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Daemon cannot be run when "
 
3951
      "mua_wrapper is set");
 
3952
    }
 
3953
  daemon_go();
 
3954
  }
3599
3955
 
3600
3956
/* If the sender ident has not been set (by a trusted caller) set it to
3601
3957
the caller. This will get overwritten below for an inetd call. If a trusted
3625
3981
message via SMTP (inetd invocation or otherwise). */
3626
3982
 
3627
3983
if ((sender_address == NULL && !smtp_input) ||
3628
 
    (!trusted_caller && filter_test == NULL))
 
3984
    (!trusted_caller && filter_test == FTEST_NONE))
3629
3985
  {
3630
3986
  sender_local = TRUE;
3631
3987
 
3656
4012
       ||                                /*         OR            */
3657
4013
       (sender_address[0] != 0 &&        /* Non-empty sender address, AND */
3658
4014
       !checking &&                      /* Not running tests, AND */
3659
 
       filter_test == NULL))             /* Not testing a filter */
 
4015
       filter_test == FTEST_NONE))       /* Not testing a filter */
3660
4016
    {
3661
4017
    sender_address = originator_login;
3662
4018
    sender_address_forced = FALSE;
3674
4030
 
3675
4031
if (sender_address != NULL && sender_address[0] != 0 &&
3676
4032
    sender_address_domain == 0)
3677
 
  sender_address = string_sprintf("%s@%s", sender_address,
 
4033
  sender_address = string_sprintf("%s@%s", local_part_quote(sender_address),
3678
4034
    qualify_domain_sender);
3679
4035
 
3680
4036
DEBUG(D_receive) debug_printf("sender address = %s\n", sender_address);
3723
4079
      }
3724
4080
    }
3725
4081
 
3726
 
  else
 
4082
  else for (;;)
3727
4083
    {
3728
 
    for (;;)
3729
 
      {
3730
 
      int len;
3731
 
      uschar buffer[256];
3732
 
      printf("> ");
3733
 
      if (Ufgets(buffer, 256, stdin) == NULL) break;
3734
 
      len = Ustrlen(buffer);
3735
 
      while (len > 0 && isspace(buffer[len-1])) len--;
3736
 
      buffer[len] = 0;
3737
 
      test_address(buffer, flags, &exit_value);
3738
 
      }
3739
 
    printf("\n");
 
4084
    uschar *s = get_stdinput(NULL, NULL);
 
4085
    if (s == NULL) break;
 
4086
    test_address(s, flags, &exit_value);
3740
4087
    }
 
4088
 
3741
4089
  route_tidyup();
3742
4090
  exim_exit(exit_value);
3743
4091
  }
3758
4106
      }
3759
4107
    }
3760
4108
 
3761
 
  /* Read stdin, allowing for continuations */
 
4109
  /* Read stdin */
3762
4110
 
3763
4111
  else
3764
4112
    {
3765
 
    uschar buffer[1024];
 
4113
    char *(*fn_readline)(char *) = NULL;
 
4114
    char *(*fn_addhist)(char *) = NULL;
 
4115
 
 
4116
    #ifdef USE_READLINE
 
4117
    void *dlhandle = set_readline(&fn_readline, &fn_addhist);
 
4118
    #endif
3766
4119
 
3767
4120
    for (;;)
3768
4121
      {
3769
 
      int i;
3770
 
      int size = 0;
3771
 
      int ptr = 0;
3772
 
      uschar *source = NULL;
3773
4122
      uschar *ss;
3774
 
 
3775
 
      printf("> ");
3776
 
      for (i = 0;; i++)
3777
 
        {
3778
 
        uschar *p = buffer;
3779
 
        if (Ufgets(buffer, sizeof(buffer), stdin) == NULL) break;
3780
 
        ss = buffer + (int)Ustrlen(buffer);
3781
 
        while (ss > buffer && isspace(ss[-1])) ss--;
3782
 
        if (i > 0)
3783
 
          {
3784
 
          while (p < ss && isspace(*p)) p++;   /* leading space after cont */
3785
 
          }
3786
 
        source = string_cat(source, &size, &ptr, p, ss - p);
3787
 
        if (ss == buffer || ss[-1] != '\\')
3788
 
          {
3789
 
          source[ptr] = 0;
3790
 
          break;
3791
 
          }
3792
 
        source[--ptr] = 0;
3793
 
        }
3794
 
      if (source == NULL)
3795
 
        {
3796
 
        printf("\n");
3797
 
        break;
3798
 
        }
3799
 
 
 
4123
      uschar *source = get_stdinput(fn_readline, fn_addhist);
 
4124
      if (source == NULL) break;
3800
4125
      ss = expand_string(source);
3801
4126
      if (ss == NULL)
3802
4127
        printf ("Failed: %s\n", expand_string_message);
3803
4128
      else printf("%s\n", CS ss);
3804
4129
      }
 
4130
 
 
4131
    #ifdef USE_READLINE
 
4132
    if (dlhandle != NULL) dlclose(dlhandle);
 
4133
    #endif
3805
4134
    }
3806
4135
 
3807
4136
  exim_exit(EXIT_SUCCESS);
3834
4163
 
3835
4164
if (host_checking)
3836
4165
  {
 
4166
  int x[4];
 
4167
  int size;
 
4168
 
3837
4169
  sender_ident = NULL;
3838
4170
  if (running_in_test_harness && sender_host_port != 0 &&
3839
4171
      interface_address != NULL && interface_port != 0)
3840
4172
    verify_get_ident(1413);
3841
4173
 
 
4174
  /* In case the given address is a non-canonical IPv6 address, canonicize
 
4175
  it. The code works for both IPv4 and IPv6, as it happens. */
 
4176
 
 
4177
  size = host_aton(sender_host_address, x);
 
4178
  sender_host_address = store_get(48);  /* large enough for full IPv6 */
 
4179
  (void)host_nmtoa(size, x, -1, sender_host_address, ':');
 
4180
 
 
4181
  /* Now set up for testing */
 
4182
 
3842
4183
  host_build_sender_fullhost();
3843
4184
  smtp_input = TRUE;
3844
4185
  smtp_in = stdin;
3852
4193
    "**** This is not for real!\n\n",
3853
4194
      sender_host_address);
3854
4195
 
 
4196
  if (verify_check_host(&hosts_connection_nolog) == OK)
 
4197
    log_write_selector &= ~L_smtp_connection;
3855
4198
  log_write(L_smtp_connection, LOG_MAIN, "%s", smtp_get_connection_info());
 
4199
 
3856
4200
  if (smtp_start_session())
3857
4201
    {
3858
4202
    reset_point = store_get(0);
3878
4222
    printf("Configuration file is %s\n", config_main_filename);
3879
4223
    return EXIT_SUCCESS;
3880
4224
    }
3881
 
  if (filter_test == NULL)
 
4225
  if (filter_test == FTEST_NONE)
3882
4226
    {
3883
4227
    fprintf(stderr,
3884
4228
"Exim is a Mail Transfer Agent. It is normally called by Mail User Agents,\n"
3889
4233
  }
3890
4234
 
3891
4235
 
 
4236
/* If mua_wrapper is set, Exim is being used to turn an MUA that submits on the
 
4237
standard input into an MUA that submits to a smarthost over TCP/IP. We know
 
4238
that we are not called from inetd, because that is rejected above. The
 
4239
following configuration settings are forced here:
 
4240
 
 
4241
  (1) Synchronous delivery (-odi)
 
4242
  (2) Errors to stderr (-oep == -oeq)
 
4243
  (3) No parallel remote delivery
 
4244
  (4) Unprivileged delivery
 
4245
 
 
4246
We don't force overall queueing options because there are several of them;
 
4247
instead, queueing is avoided below when mua_wrapper is set. However, we do need
 
4248
to override any SMTP queueing. */
 
4249
 
 
4250
if (mua_wrapper)
 
4251
  {
 
4252
  synchronous_delivery = TRUE;
 
4253
  arg_error_handling = ERRORS_STDERR;
 
4254
  remote_max_parallel = 1;
 
4255
  deliver_drop_privilege = TRUE;
 
4256
  queue_smtp = FALSE;
 
4257
  queue_smtp_domains = NULL;
 
4258
  }
 
4259
 
 
4260
 
3892
4261
/* Prepare to accept one or more new messages on the standard input. When a
3893
4262
message has been read, its id is returned in message_id[]. If doing immediate
3894
4263
delivery, we fork a delivery process for each received message, except for the
3906
4275
 
3907
4276
else if (is_inetd)
3908
4277
  {
3909
 
  fclose(stderr);
 
4278
  (void)fclose(stderr);
 
4279
  exim_nullstd();                       /* Re-open to /dev/null */
3910
4280
  verify_get_ident(IDENT_PORT);
3911
4281
  host_build_sender_fullhost();
3912
4282
  set_process_info("handling incoming connection from %s via inetd",
3915
4285
 
3916
4286
/* If the sender host address has been set, build sender_fullhost if it hasn't
3917
4287
already been done (which it will have been for inetd). This caters for the
3918
 
case when it is forced by -oMa. However, we must flag that is isn't a socket,
 
4288
case when it is forced by -oMa. However, we must flag that it isn't a socket,
3919
4289
so that the test for IP options is skipped for -bs input. */
3920
4290
 
3921
4291
if (sender_host_address != NULL && sender_fullhost == NULL)
3935
4305
if exim is started from inetd. In this case fd 0 will be set to the socket,
3936
4306
but fd 1 will not be set. This also happens for passed SMTP channels. */
3937
4307
 
3938
 
if (fstat(1, &statbuf) < 0) dup2(0, 1);
 
4308
if (fstat(1, &statbuf) < 0) (void)dup2(0, 1);
3939
4309
 
3940
4310
/* Set up the incoming protocol name and the state of the program. Root
3941
4311
is allowed to force received protocol via the -oMr option above, and if we are
3942
4312
in a non-local SMTP state it means we have come via inetd and the process info
3943
4313
has already been set up. We don't set received_protocol here for smtp input,
3944
 
as it varies according to batch/HELO/EHLO. */
 
4314
as it varies according to batch/HELO/EHLO/AUTH/TLS. */
3945
4315
 
3946
4316
if (smtp_input)
3947
4317
  {
3956
4326
    sender_address);
3957
4327
  }
3958
4328
 
3959
 
/* Initialize the local_queue-only flag */
 
4329
/* Initialize the local_queue-only flag (this will be ignored if mua_wrapper is
 
4330
set) */
3960
4331
 
3961
4332
queue_check_only();
3962
4333
local_queue_only = queue_only;
3979
4350
  {
3980
4351
  smtp_in = stdin;
3981
4352
  smtp_out = stdout;
 
4353
  if (verify_check_host(&hosts_connection_nolog) == OK)
 
4354
    log_write_selector &= ~L_smtp_connection;
3982
4355
  log_write(L_smtp_connection, LOG_MAIN, "%s", smtp_get_connection_info());
3983
4356
  if (!smtp_start_session())
3984
4357
    {
4108
4481
    int count = argc - recipients_arg;
4109
4482
    uschar **list = argv + recipients_arg;
4110
4483
 
 
4484
    /* These options cannot be changed dynamically for non-SMTP messages */
 
4485
 
 
4486
    active_local_sender_retain = local_sender_retain;
 
4487
    active_local_from_check = local_from_check;
 
4488
 
4111
4489
    /* Save before any rewriting */
4112
4490
 
4113
4491
    raw_sender = string_copy(sender_address);
4198
4576
        }
4199
4577
      }
4200
4578
 
4201
 
    /* Read the data for the message. If filter_test is true, this will
4202
 
    just read the headers for the message, and not write anything onto
4203
 
    the spool. */
 
4579
    /* Read the data for the message. If filter_test is not FTEST_NONE, this
 
4580
    will just read the headers for the message, and not write anything onto the
 
4581
    spool. */
4204
4582
 
4205
4583
    message_ended = END_NOTENDED;
4206
4584
    more = receive_msg(extract_recipients);
4219
4597
  unless specified. The the return path is set to to the sender unless it has
4220
4598
  already been set from a return-path header in the message. */
4221
4599
 
4222
 
  if (filter_test != NULL)
 
4600
  if (filter_test != FTEST_NONE)
4223
4601
    {
4224
4602
    deliver_domain = (ftest_domain != NULL)?
4225
4603
      ftest_domain : qualify_domain_recipient;
4253
4631
    if (ftest_prefix != NULL) printf("Prefix    = %s\n", ftest_prefix);
4254
4632
    if (ftest_suffix != NULL) printf("Suffix    = %s\n", ftest_suffix);
4255
4633
 
4256
 
    chdir("/");   /* Get away from wherever the user is running this from */
4257
 
    exim_exit(filter_runtest(filter_fd, ftest_system, more)?
4258
 
      EXIT_SUCCESS : EXIT_FAILURE);
 
4634
    (void)chdir("/");   /* Get away from wherever the user is running this from */
 
4635
 
 
4636
    /* Now we run either a system filter test, or a user filter test, or both.
 
4637
    In the latter case, headers added by the system filter will persist and be
 
4638
    available to the user filter. We need to copy the filter variables
 
4639
    explicitly. */
 
4640
 
 
4641
    if ((filter_test & FTEST_SYSTEM) != 0)
 
4642
      {
 
4643
      if (!filter_runtest(filter_sfd, filter_test_sfile, TRUE, more))
 
4644
        exim_exit(EXIT_FAILURE);
 
4645
      }
 
4646
 
 
4647
    memcpy(filter_sn, filter_n, sizeof(filter_sn));
 
4648
 
 
4649
    if ((filter_test & FTEST_USER) != 0)
 
4650
      {
 
4651
      if (!filter_runtest(filter_ufd, filter_test_ufile, FALSE, more))
 
4652
        exim_exit(EXIT_FAILURE);
 
4653
      }
 
4654
 
 
4655
    exim_exit(EXIT_SUCCESS);
4259
4656
    }
4260
4657
 
4261
4658
  /* Else act on the result of message reception. We should not get here unless
4283
4680
      }
4284
4681
    }
4285
4682
 
 
4683
  /* If running as an MUA wrapper, all queueing options and freezing options
 
4684
  are ignored. */
 
4685
 
 
4686
  if (mua_wrapper)
 
4687
    local_queue_only = queue_only_policy = deliver_freeze = FALSE;
 
4688
 
4286
4689
  /* Log the queueing here, when it will get a message id attached, but
4287
4690
  not if queue_only is set (case 0). Case 1 doesn't happen here (too many
4288
4691
  connections). */
4305
4708
  /* Else do the delivery unless the ACL or local_scan() called for queue only
4306
4709
  or froze the message. Always deliver in a separate process. A fork failure is
4307
4710
  not a disaster, as the delivery will eventually happen on a subsequent queue
4308
 
  run. */
 
4711
  run. The search cache must be tidied before the fork, as the parent will
 
4712
  do it before exiting. The child will trigger a lookup failure and
 
4713
  thereby defer the delivery if it tries to use (for example) a cached ldap
 
4714
  connection that the parent has called unbind on. */
4309
4715
 
4310
4716
  else if (!queue_only_policy && !deliver_freeze)
4311
4717
    {
4312
4718
    pid_t pid;
 
4719
    search_tidyup();
 
4720
 
4313
4721
    if ((pid = fork()) == 0)
4314
4722
      {
 
4723
      int rc;
4315
4724
      close_unwanted();      /* Close unwanted file descriptors and TLS */
4316
 
 
4317
 
      /* Re-exec Exim if we need to regain privilege */
 
4725
      exim_nullstd();        /* Ensure std{in,out,err} exist */
 
4726
 
 
4727
      /* Occasionally in the test harness we don't have synchronous delivery
 
4728
      set (can happen with bounces). In that case, let the old process finish
 
4729
      before continuing, to keep the debug output the same. */
 
4730
 
 
4731
      if (running_in_test_harness && !synchronous_delivery) millisleep(100);
 
4732
 
 
4733
      /* Re-exec Exim if we need to regain privilege (note: in mua_wrapper
 
4734
      mode, deliver_drop_privilege is forced TRUE). */
4318
4735
 
4319
4736
      if (geteuid() != root_uid && !deliver_drop_privilege && !unprivileged)
4320
4737
        {
4325
4742
 
4326
4743
      /* No need to re-exec */
4327
4744
 
4328
 
      (void)deliver_message(message_id, FALSE, FALSE);
 
4745
      rc = deliver_message(message_id, FALSE, FALSE);
4329
4746
      search_tidyup();
4330
 
      _exit(EXIT_SUCCESS);
 
4747
      _exit((!mua_wrapper || rc == DELIVER_MUA_SUCCEEDED)?
 
4748
        EXIT_SUCCESS : EXIT_FAILURE);
4331
4749
      }
4332
4750
 
4333
4751
    if (pid < 0)
4336
4754
        "process: %s", strerror(errno));
4337
4755
      }
4338
4756
 
4339
 
    /* In the parent, wait if synchronous delivery is required. */
 
4757
    /* In the parent, wait if synchronous delivery is required. This will
 
4758
    always be the case in MUA wrapper mode. */
4340
4759
 
4341
4760
    else if (synchronous_delivery)
4342
4761
      {
4343
4762
      int status;
4344
4763
      while (wait(&status) != pid);
 
4764
      if ((status & 0x00ff) != 0)
 
4765
        log_write(0, LOG_MAIN|LOG_PANIC,
 
4766
          "process %d crashed with signal %d while delivering %s",
 
4767
          (int)pid, status & 0x00ff, message_id);
 
4768
      if (mua_wrapper && (status & 0xffff) != 0) exim_exit(EXIT_FAILURE);
4345
4769
      }
4346
4770
    }
4347
4771