Various SASL fixes.
[exim.git] / src / src / child.c
CommitLineData
059ec3d9
PH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
0a49a7a4 5/* Copyright (c) University of Cambridge 1995 - 2009 */
059ec3d9
PH
6/* See the file NOTICE for conditions of use and distribution. */
7
8
9#include "exim.h"
10
11static void (*oldsignal)(int);
12
13
14/*************************************************
15* Ensure an fd has a given value *
16*************************************************/
17
18/* This function is called when we want to ensure that a certain fd has a
19specific value (one of 0, 1, 2). If it hasn't got it already, close the value
20we want, duplicate the fd, then close the old one.
21
22Arguments:
23 oldfd original fd
24 newfd the fd we want
25
26Returns: nothing
27*/
28
29static void
30force_fd(int oldfd, int newfd)
31{
32if (oldfd == newfd) return;
f1e894f3
PH
33(void)close(newfd);
34(void)dup2(oldfd, newfd);
35(void)close(oldfd);
059ec3d9
PH
36}
37
38
e7726cbf 39#ifndef STAND_ALONE
059ec3d9
PH
40/*************************************************
41* Build argv list and optionally re-exec Exim *
42*************************************************/
43
44/* This function is called when Exim wants to re-exec (overlay) itself in the
45current process. This is different to child_open_exim(), which runs another
46Exim process in parallel (but it then calls this function). The function's
47basic job is to build the argv list according to the values of current options
48settings. There is a basic list that all calls require, and an additional list
49that some do not require. Further additions can be given as additional
50arguments. An option specifies whether the exec() is actually to happen, and if
51so, what is to be done if it fails.
52
53Arguments:
54 exec_type CEE_RETURN_ARGV => don't exec; return the argv list
55 CEE_EXEC_EXIT => just exit() on exec failure
56 CEE_EXEC_PANIC => panic-die on exec failure
57 kill_v if TRUE, don't pass on the D_v flag
58 pcount if not NULL, points to extra size of argv required, and if
59 CEE_RETURN_ARGV is specified, it is updated to give the
60 number of slots used
61 minimal TRUE if only minimal argv is required
62 acount number of additional arguments
63 ... further values to add to argv
64
65Returns: if CEE_RETURN_ARGV is given, returns a pointer to argv;
66 otherwise, does not return
67*/
68
69uschar **
70child_exec_exim(int exec_type, BOOL kill_v, int *pcount, BOOL minimal,
71 int acount, ...)
72{
73int first_special = -1;
74int n = 0;
75int extra = (pcount != NULL)? *pcount : 0;
76uschar **argv =
77 store_get((extra + acount + MAX_CLMACROS + 16) * sizeof(char *));
78
79/* In all case, the list starts out with the path, any macros, and a changed
80config file. */
81
82argv[n++] = exim_path;
83if (clmacro_count > 0)
84 {
85 memcpy(argv + n, clmacros, clmacro_count * sizeof(uschar *));
86 n += clmacro_count;
87 }
88if (config_changed)
89 {
90 argv[n++] = US"-C";
91 argv[n++] = config_main_filename;
92 }
93
94/* These values are added only for non-minimal cases. If debug_selector is
95precisely D_v, we have to assume this was started by a non-admin user, and
96we suppress the flag when requested. (This happens when passing on an SMTP
97connection, and after ETRN.) If there's more debugging going on, an admin user
98was involved, so we do pass it on. */
99
100if (!minimal)
101 {
102 if (debug_selector == D_v)
103 {
104 if (!kill_v) argv[n++] = US"-v";
105 }
106 else
107 {
108 if (debug_selector != 0)
109 argv[n++] = string_sprintf("-d=0x%x", debug_selector);
110 }
111 if (dont_deliver) argv[n++] = US"-N";
112 if (queue_smtp) argv[n++] = US"-odqs";
113 if (synchronous_delivery) argv[n++] = US"-odi";
114 if (connection_max_messages >= 0)
115 argv[n++] = string_sprintf("-oB%d", connection_max_messages);
116 }
117
118/* Now add in any others that are in the call. Remember which they were,
119for more helpful diagnosis on failure. */
120
121if (acount > 0)
122 {
123 va_list ap;
124 va_start(ap, acount);
125 first_special = n;
126 while (acount-- > 0)
127 argv[n++] = va_arg(ap, uschar *);
128 va_end(ap);
129 }
130
131/* Terminate the list, and return it, if that is what is wanted. */
132
133argv[n] = NULL;
134if (exec_type == CEE_RETURN_ARGV)
135 {
136 if (pcount != NULL) *pcount = n;
137 return argv;
138 }
139
140/* Otherwise, do the exec() here, and handle the consequences of an unexpected
141failure. We know that there will always be at least one extra option in the
142call when exec() is done here, so it can be used to add to the panic data. */
143
144DEBUG(D_exec) debug_print_argv(argv);
145exim_nullstd(); /* Make sure std{in,out,err} exist */
146execv(CS argv[0], (char *const *)argv);
147
148log_write(0,
149 LOG_MAIN | ((exec_type == CEE_EXEC_EXIT)? LOG_PANIC : LOG_PANIC_DIE),
150 "re-exec of exim (%s) with %s failed: %s", exim_path, argv[first_special],
151 strerror(errno));
152
153/* Get here if exec_type == CEE_EXEC_EXIT.
154Note: this must be _exit(), not exit(). */
155
156_exit(EX_EXECFAILED);
157
158return NULL; /* To keep compilers happy */
159}
160
161
162
163
164/*************************************************
165* Create a child Exim process *
166*************************************************/
167
168/* This function is called when Exim wants to run a parallel instance of itself
169in order to inject a message via the standard input. The function creates a
170child process and runs Exim in it. It sets up a pipe to the standard input of
171the new process, and returns that to the caller via fdptr. The function returns
172the pid of the new process, or -1 if things go wrong. If debug_fd is
173non-negative, it is passed as stderr.
174
5977a0b3
PH
175This interface is now a just wrapper for the more complicated function
176child_open_exim2(), which has additional arguments. The wrapper must continue
177to exist, even if all calls from within Exim are changed, because it is
178documented for use from local_scan().
179
059ec3d9
PH
180Argument: fdptr pointer to int for the stdin fd
181Returns: pid of the created process or -1 if anything has gone wrong
182*/
183
184pid_t
185child_open_exim(int *fdptr)
186{
5977a0b3
PH
187return child_open_exim2(fdptr, US"<>", bounce_sender_authentication);
188}
189
190
191/* This is a more complicated function for creating a child Exim process, with
192more arguments.
193
194Arguments:
195 fdptr pointer to int for the stdin fd
196 sender for a sender address (data for -f)
197 sender_authentication authenticated sender address or NULL
198
199Returns: pid of the created process or -1 if anything has gone wrong
200*/
201
202pid_t
203child_open_exim2(int *fdptr, uschar *sender, uschar *sender_authentication)
204{
059ec3d9
PH
205int pfd[2];
206int save_errno;
207pid_t pid;
208
209/* Create the pipe and fork the process. Ensure that SIGCHLD is set to
210SIG_DFL before forking, so that the child process can be waited for. We
211sometimes get here with it set otherwise. Save the old state for resetting
212on the wait. */
213
214if (pipe(pfd) != 0) return (pid_t)(-1);
215oldsignal = signal(SIGCHLD, SIG_DFL);
216pid = fork();
217
218/* Child process: make the reading end of the pipe into the standard input and
219close the writing end. If debugging, pass debug_fd as stderr. Then re-exec
75e0e026
PH
220Exim with appropriat options. In the test harness, use -odi unless queue_only
221is set, so that the bounce is fully delivered before returning. Failure is
222signalled with EX_EXECFAILED (specified by CEE_EXEC_EXIT), but this shouldn't
223occur. */
059ec3d9
PH
224
225if (pid == 0)
226 {
227 force_fd(pfd[pipe_read], 0);
f1e894f3 228 (void)close(pfd[pipe_write]);
059ec3d9 229 if (debug_fd > 0) force_fd(debug_fd, 2);
75e0e026
PH
230 if (running_in_test_harness && !queue_only)
231 {
232 if (sender_authentication != NULL)
233 child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 9,
234 US "-odi", US"-t", US"-oem", US"-oi", US"-f", sender, US"-oMas",
235 sender_authentication, message_id_option);
236 else
237 child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 7,
238 US "-odi", US"-t", US"-oem", US"-oi", US"-f", sender,
239 message_id_option);
240 /* Control does not return here. */
241 }
242 else /* Not test harness */
243 {
244 if (sender_authentication != NULL)
245 child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 8,
246 US"-t", US"-oem", US"-oi", US"-f", sender, US"-oMas",
247 sender_authentication, message_id_option);
248 else
249 child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 6,
250 US"-t", US"-oem", US"-oi", US"-f", sender, message_id_option);
251 /* Control does not return here. */
252 }
059ec3d9
PH
253 }
254
255/* Parent process. Save fork() errno and close the reading end of the stdin
256pipe. */
257
258save_errno = errno;
f1e894f3 259(void)close(pfd[pipe_read]);
059ec3d9
PH
260
261/* Fork succeeded */
262
263if (pid > 0)
264 {
265 *fdptr = pfd[pipe_write]; /* return writing end of stdin pipe */
266 return pid; /* and pid of new process */
267 }
268
269/* Fork failed */
270
f1e894f3 271(void)close(pfd[pipe_write]);
059ec3d9
PH
272errno = save_errno;
273return (pid_t)(-1);
274}
75e0e026 275#endif /* STAND_ALONE */
059ec3d9
PH
276
277
278
279/*************************************************
280* Create a non-Exim child process *
281*************************************************/
282
283/* This function creates a child process and runs the given command in it. It
284sets up pipes to the standard input and output of the new process, and returns
285them to the caller. The standard error is cloned to the output. If there are
286any file descriptors "in the way" in the new process, they are closed. A new
287umask is supplied for the process, and an optional new uid and gid are also
288available. These are used by the queryprogram router to set an unprivileged id.
b668c215
PH
289SIGUSR1 is always disabled in the new process, as it is not going to be running
290Exim (the function child_open_exim() is provided for that). This function
291returns the pid of the new process, or -1 if things go wrong.
059ec3d9
PH
292
293Arguments:
294 argv the argv for exec in the new process
295 envp the envp for exec in the new process
296 newumask umask to set in the new process
297 newuid point to uid for the new process or NULL for no change
298 newgid point to gid for the new process or NULL for no change
299 infdptr pointer to int into which the fd of the stdin of the new process
300 is placed
301 outfdptr pointer to int into which the fd of the stdout/stderr of the new
302 process is placed
303 wd if not NULL, a path to be handed to chdir() in the new process
304 make_leader if TRUE, make the new process a process group leader
8e669ac1 305
059ec3d9
PH
306Returns: the pid of the created process or -1 if anything has gone wrong
307*/
308
309pid_t
310child_open_uid(uschar **argv, uschar **envp, int newumask, uid_t *newuid,
311 gid_t *newgid, int *infdptr, int *outfdptr, uschar *wd, BOOL make_leader)
312{
313int save_errno;
314int inpfd[2], outpfd[2];
315pid_t pid;
316
317/* Create the pipes. */
318
319if (pipe(inpfd) != 0) return (pid_t)(-1);
320if (pipe(outpfd) != 0)
321 {
f1e894f3
PH
322 (void)close(inpfd[pipe_read]);
323 (void)close(inpfd[pipe_write]);
059ec3d9
PH
324 return (pid_t)(-1);
325 }
326
327/* Fork the process. Ensure that SIGCHLD is set to SIG_DFL before forking, so
328that the child process can be waited for. We sometimes get here with it set
329otherwise. Save the old state for resetting on the wait. */
330
331oldsignal = signal(SIGCHLD, SIG_DFL);
332pid = fork();
333
59e82a2a
PH
334/* Handle the child process. First, set the required environment. We must do
335this before messing with the pipes, in order to be able to write debugging
336output when things go wrong. */
059ec3d9
PH
337
338if (pid == 0)
339 {
59e82a2a 340 signal(SIGUSR1, SIG_IGN);
605021fc 341 signal(SIGPIPE, SIG_DFL);
59e82a2a
PH
342
343 if (newgid != NULL && setgid(*newgid) < 0)
344 {
345 DEBUG(D_any) debug_printf("failed to set gid=%ld in subprocess: %s\n",
346 (long int)(*newgid), strerror(errno));
347 goto CHILD_FAILED;
348 }
349
350 if (newuid != NULL && setuid(*newuid) < 0)
351 {
352 DEBUG(D_any) debug_printf("failed to set uid=%ld in subprocess: %s\n",
353 (long int)(*newuid), strerror(errno));
354 goto CHILD_FAILED;
355 }
356
357 (void)umask(newumask);
358
359 if (wd != NULL && Uchdir(wd) < 0)
360 {
361 DEBUG(D_any) debug_printf("failed to chdir to %s: %s\n", wd,
362 strerror(errno));
363 goto CHILD_FAILED;
364 }
365
366 /* Becomes a process group leader if requested, and then organize the pipes.
367 Any unexpected failure is signalled with EX_EXECFAILED; these are all "should
368 never occur" failures, except for exec failing because the command doesn't
369 exist. */
370
371 if (make_leader && setpgid(0,0) < 0)
372 {
373 DEBUG(D_any) debug_printf("failed to set group leader in subprocess: %s\n",
374 strerror(errno));
375 goto CHILD_FAILED;
376 }
059ec3d9 377
f1e894f3 378 (void)close(inpfd[pipe_write]);
059ec3d9
PH
379 force_fd(inpfd[pipe_read], 0);
380
f1e894f3 381 (void)close(outpfd[pipe_read]);
059ec3d9
PH
382 force_fd(outpfd[pipe_write], 1);
383
f1e894f3
PH
384 (void)close(2);
385 (void)dup2(1, 2);
059ec3d9 386
059ec3d9
PH
387 /* Now do the exec */
388
389 if (envp == NULL) execv(CS argv[0], (char *const *)argv);
390 else execve(CS argv[0], (char *const *)argv, (char *const *)envp);
391
392 /* Failed to execv. Signal this failure using EX_EXECFAILED. We are
393 losing the actual errno we got back, because there is no way to return
59e82a2a 394 this information. */
059ec3d9
PH
395
396 CHILD_FAILED:
397 _exit(EX_EXECFAILED); /* Note: must be _exit(), NOT exit() */
398 }
399
400/* Parent process. Save any fork failure code, and close the reading end of the
401stdin pipe, and the writing end of the stdout pipe. */
402
403save_errno = errno;
f1e894f3
PH
404(void)close(inpfd[pipe_read]);
405(void)close(outpfd[pipe_write]);
059ec3d9
PH
406
407/* Fork succeeded; return the input/output pipes and the pid */
408
409if (pid > 0)
410 {
411 *infdptr = inpfd[pipe_write];
412 *outfdptr = outpfd[pipe_read];
413 return pid;
414 }
415
416/* Fork failed; reset fork errno before returning */
417
f1e894f3
PH
418(void)close(inpfd[pipe_write]);
419(void)close(outpfd[pipe_read]);
059ec3d9
PH
420errno = save_errno;
421return (pid_t)(-1);
422}
423
424
425
426
427/*************************************************
428* Create child process without uid change *
429*************************************************/
430
431/* This function is a wrapper for child_open_uid() that doesn't have the uid,
b668c215
PH
432gid and working directory changing arguments. The function is provided so as to
433have a clean interface for use from local_scan(), but also saves writing NULL
434arguments several calls that would otherwise use child_open_uid().
059ec3d9
PH
435
436Arguments:
437 argv the argv for exec in the new process
438 envp the envp for exec in the new process
439 newumask umask to set in the new process
440 infdptr pointer to int into which the fd of the stdin of the new process
441 is placed
442 outfdptr pointer to int into which the fd of the stdout/stderr of the new
443 process is placed
444 make_leader if TRUE, make the new process a process group leader
445
446Returns: the pid of the created process or -1 if anything has gone wrong
447*/
448
449pid_t
450child_open(uschar **argv, uschar **envp, int newumask, int *infdptr,
451 int *outfdptr, BOOL make_leader)
452{
453return child_open_uid(argv, envp, newumask, NULL, NULL, infdptr, outfdptr,
454 NULL, make_leader);
455}
456
457
458
459
460/*************************************************
461* Close down child process *
462*************************************************/
463
464/* Wait for the given process to finish, with optional timeout.
465
466Arguments
467 pid: the pid to wait for
468 timeout: maximum time to wait; 0 means for as long as it takes
469
470Returns: >= 0 process terminated by exiting; value is process
471 ending status; if an execve() failed, the value
472 is typically 127 (defined as EX_EXECFAILED)
473 < 0 & > -256 process was terminated by a signal; value is the
474 negation of the signal number
475 -256 timed out
476 -257 other error in wait(); errno still set
477*/
478
479int
480child_close(pid_t pid, int timeout)
481{
482int yield;
483
484if (timeout > 0)
485 {
486 sigalrm_seen = FALSE;
487 alarm(timeout);
488 }
489
490for(;;)
491 {
492 int status;
493 pid_t rc = waitpid(pid, &status, 0);
494 if (rc == pid)
495 {
496 int lowbyte = status & 255;
497 if (lowbyte == 0) yield = (status >> 8) & 255;
498 else yield = -lowbyte;
499 break;
500 }
501 if (rc < 0)
502 {
503 yield = (errno == EINTR && sigalrm_seen)? -256 : -257;
504 break;
505 }
506 }
507
508if (timeout > 0) alarm(0);
509
510signal(SIGCHLD, oldsignal); /* restore */
511return yield;
512}
513
514/* End of child.c */