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 */
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.
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!
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
421
447
When we close stderr (which implies we've also closed stdout), we also get rid
422
448
of any controlling terminal.
939
1008
/*************************************************
1009
* Quote a local part *
1010
*************************************************/
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.
1016
Argument: the local part
1017
Returns: the local part, quoted if necessary
1021
local_part_quote(uschar *lpart)
1023
BOOL needs_quote = FALSE;
1028
for (t = lpart; !needs_quote && *t != 0; t++)
1030
needs_quote = !isalnum(*t) && strchr("!#$%&'*+-/=?^_`{|}~", *t) == NULL &&
1031
(*t != '.' || t == lpart || t[1] == 0);
1034
if (!needs_quote) return lpart;
1037
yield = string_cat(NULL, &size, &ptr, US"\"", 1);
1041
uschar *nq = US Ustrpbrk(lpart, "\\\"");
1044
yield = string_cat(yield, &size, &ptr, lpart, Ustrlen(lpart));
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);
1053
yield = string_cat(yield, &size, &ptr, US"\"", 1);
1061
/*************************************************
1062
* Load readline() functions *
1063
*************************************************/
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.
1072
fn_readline_ptr pointer to where to put the readline pointer
1073
fn_addhist_ptr pointer to where to put the addhistory function
1075
Returns: the dlopen handle or NULL on failure
1079
set_readline(char * (**fn_readline_ptr)(char *),
1080
char * (**fn_addhist_ptr)(char *))
1083
void *dlhandle_curses = dlopen("libcurses.so", RTLD_GLOBAL|RTLD_LAZY);
1085
dlhandle = dlopen("libreadline.so", RTLD_GLOBAL|RTLD_NOW);
1086
if (dlhandle_curses != NULL) dlclose(dlhandle_curses);
1088
if (dlhandle != NULL)
1090
*fn_readline_ptr = (char *(*)(char*))dlsym(dlhandle, "readline");
1091
*fn_addhist_ptr = (char *(*)(char*))dlsym(dlhandle, "add_history");
1095
DEBUG(D_any) debug_printf("failed to load readline: %s\n", dlerror());
1104
/*************************************************
1105
* Get a line from stdin for testing things *
1106
*************************************************/
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.
1114
fn_readline readline function or NULL
1115
fn_addhist addhist function or NULL
1117
Returns: pointer to dynamic memory, or NULL at end of file
1121
get_stdinput(char *(*fn_readline)(char *), char *(*fn_addhist)(char *))
1126
uschar *yield = NULL;
1128
if (fn_readline == NULL) printf("> ");
1132
uschar buffer[1024];
1136
char *readline_line = NULL;
1137
if (fn_readline != NULL)
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;
1146
/* readline() not in use */
1149
if (Ufgets(buffer, sizeof(buffer), stdin) == NULL) break;
1153
/* Handle the line */
1155
ss = p + (int)Ustrlen(p);
1156
while (ss > p && isspace(ss[-1])) ss--;
1160
while (p < ss && isspace(*p)) p++; /* leading space after cont */
1163
yield = string_cat(yield, &size, &ptr, p, ss - p);
1166
if (fn_readline != NULL) free(readline_line);
1169
if (ss == p || yield[ptr-1] != '\\')
1177
if (yield == NULL) printf("\n");
1183
/*************************************************
940
1184
* Entry point and high-level code *
941
1185
*************************************************/
1356
1618
else if (*argrest == 'e')
1357
1619
expansion_test = checking = TRUE;
1359
/* -bf: Run in mail filter testing mode
1360
-bF: Ditto, but for system filters
1621
/* -bF: Run system filter test */
1623
else if (*argrest == 'F')
1625
filter_test |= FTEST_SYSTEM;
1626
if (*(++argrest) != 0) { badarg = TRUE; break; }
1627
if (++i < argc) filter_test_sfile = argv[i]; else
1629
fprintf(stderr, "exim: file name expected after %s\n", argv[i-1]);
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
1367
else if (*argrest == 'f' || *argrest == 'F')
1641
else if (*argrest == 'f')
1369
ftest_system = *argrest++ == 'F';
1643
if (*(++argrest) == 0)
1372
if(++i < argc) filter_test = argv[i]; else
1645
filter_test |= FTEST_USER;
1646
if (++i < argc) filter_test_ufile = argv[i]; else
1374
1648
fprintf(stderr, "exim: file name expected after %s\n", argv[i-1]);
1375
1649
exit(EXIT_FAILURE);
2606
2886
struct rlimit rlp;
2607
2888
#ifdef RLIMIT_NOFILE
2608
2889
if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
2891
log_write(0, LOG_MAIN|LOG_PANIC, "getrlimit(RLIMIT_NOFILE) failed: %s",
2609
2893
rlp.rlim_cur = rlp.rlim_max = 0;
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
2610
2900
if (rlp.rlim_cur < 1000)
2612
2902
rlp.rlim_cur = rlp.rlim_max = 1000;
2613
(void)setrlimit(RLIMIT_NOFILE, &rlp);
2903
if (setrlimit(RLIMIT_NOFILE, &rlp) < 0)
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",
2616
2913
#ifdef RLIMIT_NPROC
2617
#ifdef RLIM_INFINITY
2914
if (getrlimit(RLIMIT_NPROC, &rlp) < 0)
2916
log_write(0, LOG_MAIN|LOG_PANIC, "getrlimit(RLIMIT_NPROC) failed: %s",
2918
rlp.rlim_cur = rlp.rlim_max = 0;
2921
#ifdef RLIM_INFINITY
2922
if (rlp.rlim_cur != RLIM_INFINITY && rlp.rlim_cur < 1000)
2618
2924
rlp.rlim_cur = rlp.rlim_max = RLIM_INFINITY;
2926
if (rlp.rlim_cur < 1000)
2620
2928
rlp.rlim_cur = rlp.rlim_max = 1000;
2622
(void)setrlimit(RLIMIT_NPROC, &rlp);
2930
if (setrlimit(RLIMIT_NPROC, &rlp) < 0)
2931
log_write(0, LOG_MAIN|LOG_PANIC, "setrlimit(RLIMIT_NPROC) failed: %s",
2713
3024
else exim_setugid(geteuid(), getegid(), FALSE, US"forcing real = effective");
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. */
2718
if (filter_test != NULL)
2720
filter_fd = Uopen(filter_test, O_RDONLY,0);
2723
fprintf(stderr, "exim: failed to open %s: %s\n", filter_test,
3029
if ((filter_test & FTEST_SYSTEM) != 0)
3031
filter_sfd = Uopen(filter_test_sfile, O_RDONLY, 0);
3034
fprintf(stderr, "exim: failed to open %s: %s\n", filter_test_sfile,
3036
return EXIT_FAILURE;
3040
if ((filter_test & FTEST_USER) != 0)
3042
filter_ufd = Uopen(filter_test_ufile, O_RDONLY, 0);
3045
fprintf(stderr, "exim: failed to open %s: %s\n", filter_test_ufile,
2724
3046
strerror(errno));
2725
3047
return EXIT_FAILURE;
2961
3283
while (*pp != 0) if (isspace(*pp++)) { quote = US"\""; break; }
2963
sprintf(CS p, " %s%.*s%s", quote, big_buffer_size - (p - big_buffer) - 4,
3285
sprintf(CS p, " %s%.*s%s", quote, (int)(big_buffer_size -
3286
(p - big_buffer) - 4), printing, quote);
2965
3287
while (*p) p++;
2967
log_write(0, LOG_MAIN, "%s", big_buffer);
3290
if ((log_extra_selector & LX_arguments) != 0)
3291
log_write(0, LOG_MAIN, "%s", big_buffer);
3293
debug_printf("%s\n", big_buffer);
2970
3296
/* Set the working directory to be the top-level spool directory. We don't rely
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. */
3122
if (!trusted_caller && !checking && filter_test == NULL)
3448
if (!trusted_caller && !checking && filter_test == FTEST_NONE)
3124
3450
sender_host_name = sender_host_address = interface_address =
3125
3451
sender_ident = received_protocol = NULL;
3761
/* Read stdin, allowing for continuations */
3765
uschar buffer[1024];
4113
char *(*fn_readline)(char *) = NULL;
4114
char *(*fn_addhist)(char *) = NULL;
4117
void *dlhandle = set_readline(&fn_readline, &fn_addhist);
3772
uschar *source = NULL;
3779
if (Ufgets(buffer, sizeof(buffer), stdin) == NULL) break;
3780
ss = buffer + (int)Ustrlen(buffer);
3781
while (ss > buffer && isspace(ss[-1])) ss--;
3784
while (p < ss && isspace(*p)) p++; /* leading space after cont */
3786
source = string_cat(source, &size, &ptr, p, ss - p);
3787
if (ss == buffer || ss[-1] != '\\')
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);
4132
if (dlhandle != NULL) dlclose(dlhandle);
3807
4136
exim_exit(EXIT_SUCCESS);
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:
4241
(1) Synchronous delivery (-odi)
4242
(2) Errors to stderr (-oep == -oeq)
4243
(3) No parallel remote delivery
4244
(4) Unprivileged delivery
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. */
4252
synchronous_delivery = TRUE;
4253
arg_error_handling = ERRORS_STDERR;
4254
remote_max_parallel = 1;
4255
deliver_drop_privilege = TRUE;
4257
queue_smtp_domains = NULL;
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
4253
4631
if (ftest_prefix != NULL) printf("Prefix = %s\n", ftest_prefix);
4254
4632
if (ftest_suffix != NULL) printf("Suffix = %s\n", ftest_suffix);
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 */
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
4641
if ((filter_test & FTEST_SYSTEM) != 0)
4643
if (!filter_runtest(filter_sfd, filter_test_sfile, TRUE, more))
4644
exim_exit(EXIT_FAILURE);
4647
memcpy(filter_sn, filter_n, sizeof(filter_sn));
4649
if ((filter_test & FTEST_USER) != 0)
4651
if (!filter_runtest(filter_ufd, filter_test_ufile, FALSE, more))
4652
exim_exit(EXIT_FAILURE);
4655
exim_exit(EXIT_SUCCESS);
4261
4658
/* Else act on the result of message reception. We should not get here unless
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
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. */
4310
4716
else if (!queue_only_policy && !deliver_freeze)
4313
4721
if ((pid = fork()) == 0)
4315
4724
close_unwanted(); /* Close unwanted file descriptors and TLS */
4317
/* Re-exec Exim if we need to regain privilege */
4725
exim_nullstd(); /* Ensure std{in,out,err} exist */
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. */
4731
if (running_in_test_harness && !synchronous_delivery) millisleep(100);
4733
/* Re-exec Exim if we need to regain privilege (note: in mua_wrapper
4734
mode, deliver_drop_privilege is forced TRUE). */
4319
4736
if (geteuid() != root_uid && !deliver_drop_privilege && !unprivileged)
4336
4754
"process: %s", strerror(errno));
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. */
4341
4760
else if (synchronous_delivery)
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);