1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2009 */
6 /* See the file NOTICE for conditions of use and distribution. */
8 /* This module contains code for extracting addresses from a forwarding list
9 (from an alias or forward file) or by running the filter interpreter. It may do
10 this in a sub-process if a uid/gid are supplied. */
15 enum { FILE_EXIST, FILE_NOT_EXIST, FILE_EXIST_UNCLEAR };
17 #define REPLY_EXISTS 0x01
18 #define REPLY_EXPAND 0x02
19 #define REPLY_RETURN 0x04
22 /*************************************************
23 * Check string for filter program *
24 *************************************************/
26 /* This function checks whether a string is actually a filter program. The rule
27 is that it must start with "# Exim filter ..." (any capitalization, spaces
28 optional). It is envisaged that in future, other kinds of filter may be
29 implemented. That's why it is implemented the way it is. The function is global
30 because it is also called from filter.c when checking filters.
34 Returns: FILTER_EXIM if it starts with "# Exim filter"
35 FILTER_SIEVE if it starts with "# Sieve filter"
36 FILTER_FORWARD otherwise
39 /* This is an auxiliary function for matching a tag. */
42 match_tag(const uschar *s, const uschar *tag)
44 for (; *tag != 0; s++, tag++)
48 while (*s == ' ' || *s == '\t') s++;
51 else if (tolower(*s) != tolower(*tag)) break;
56 /* This is the real function. It should be easy to add checking different
57 tags for other types of filter. */
60 rda_is_filter(const uschar *s)
62 while (isspace(*s)) s++; /* Skips initial blank lines */
63 if (match_tag(s, CUS"# exim filter")) return FILTER_EXIM;
64 else if (match_tag(s, CUS"# sieve filter")) return FILTER_SIEVE;
65 else return FILTER_FORWARD;
71 /*************************************************
72 * Check for existence of file *
73 *************************************************/
75 /* First of all, we stat the file. If this fails, we try to stat the enclosing
76 directory, because a file in an unmounted NFS directory will look the same as a
77 non-existent file. It seems that in Solaris 2.6, statting an entry in an
78 indirect map that is currently unmounted does not cause the mount to happen.
79 Instead, dummy data is returned, which defeats the whole point of this test.
80 However, if a stat() is done on some object inside the directory, such as the
81 "." back reference to itself, then the mount does occur. If an NFS host is
82 taken offline, it is possible for the stat() to get stuck until it comes back.
83 To guard against this, stick a timer round it. If we can't access the "."
84 inside the directory, try the plain directory, just in case that helps.
87 filename the file name
88 error for message on error
90 Returns: FILE_EXIST the file exists
91 FILE_NOT_EXIST the file does not exist
92 FILE_EXIST_UNCLEAR cannot determine existence
96 rda_exists(uschar *filename, uschar **error)
102 if ((rc = Ustat(filename, &statbuf)) >= 0) return FILE_EXIST;
105 Ustrncpy(big_buffer, filename, big_buffer_size - 3);
106 sigalrm_seen = FALSE;
108 if (saved_errno == ENOENT)
110 slash = Ustrrchr(big_buffer, '/');
111 Ustrcpy(slash+1, ".");
114 rc = Ustat(big_buffer, &statbuf);
115 if (rc != 0 && errno == EACCES && !sigalrm_seen)
118 rc = Ustat(big_buffer, &statbuf);
123 DEBUG(D_route) debug_printf("stat(%s)=%d\n", big_buffer, rc);
126 if (sigalrm_seen || rc != 0)
128 *error = string_sprintf("failed to stat %s (%s)", big_buffer,
129 sigalrm_seen? "timeout" : strerror(saved_errno));
130 return FILE_EXIST_UNCLEAR;
133 *error = string_sprintf("%s does not exist", filename);
134 DEBUG(D_route) debug_printf("%s\n", *error);
135 return FILE_NOT_EXIST;
140 /*************************************************
141 * Get forwarding list from a file *
142 *************************************************/
144 /* Open a file and read its entire contents into a block of memory. Certain
145 opening errors are optionally treated the same as "file does not exist".
147 ENOTDIR means that something along the line is not a directory: there are
148 installations that set home directories to be /dev/null for non-login accounts
149 but in normal circumstances this indicates some kind of configuration error.
151 EACCES means there's a permissions failure. Some users turn off read permission
152 on a .forward file to suspend forwarding, but this is probably an error in any
153 kind of mailing list processing.
155 The redirect block that contains the file name also contains constraints such
156 as who may own the file, and mode bits that must not be set. This function is
159 rdata rdirect block, containing file name and constraints
160 options for the RDO_ENOTDIR and RDO_EACCES options
161 error where to put an error message
162 yield what to return from rda_interpret on error
164 Returns: pointer to string in store; NULL on error
168 rda_get_file_contents(redirect_block *rdata, int options, uschar **error,
173 uschar *filename = rdata->string;
174 BOOL uid_ok = !rdata->check_owner;
175 BOOL gid_ok = !rdata->check_group;
178 /* Attempt to open the file. If it appears not to exist, check up on the
179 containing directory by statting it. If the directory does not exist, we treat
180 this situation as an error (which will cause delivery to defer); otherwise we
181 pass back FF_NONEXIST, which causes the redirect router to decline.
183 However, if the ignore_enotdir option is set (to ignore "something on the
184 path is not a directory" errors), the right behaviour seems to be not to do the
187 fwd = Ufopen(filename, "rb");
192 case ENOENT: /* File does not exist */
193 DEBUG(D_route) debug_printf("%s does not exist\n%schecking parent directory\n",
195 ((options & RDO_ENOTDIR) != 0)? "ignore_enotdir set => skip " : "");
196 *yield = (((options & RDO_ENOTDIR) != 0) ||
197 rda_exists(filename, error) == FILE_NOT_EXIST)?
198 FF_NONEXIST : FF_ERROR;
201 case ENOTDIR: /* Something on the path isn't a directory */
202 if ((options & RDO_ENOTDIR) == 0) goto DEFAULT_ERROR;
203 DEBUG(D_route) debug_printf("non-directory on path %s: file assumed not to "
204 "exist\n", filename);
205 *yield = FF_NONEXIST;
208 case EACCES: /* Permission denied */
209 if ((options & RDO_EACCES) == 0) goto DEFAULT_ERROR;
210 DEBUG(D_route) debug_printf("permission denied for %s: file assumed not to "
211 "exist\n", filename);
212 *yield = FF_NONEXIST;
217 *error = string_open_failed(errno, "%s", filename);
223 /* Check that we have a regular file. */
225 if (fstat(fileno(fwd), &statbuf) != 0)
227 *error = string_sprintf("failed to stat %s: %s", filename, strerror(errno));
231 if ((statbuf.st_mode & S_IFMT) != S_IFREG)
233 *error = string_sprintf("%s is not a regular file", filename);
237 /* Check for unwanted mode bits */
239 if ((statbuf.st_mode & rdata->modemask) != 0)
241 *error = string_sprintf("bad mode (0%o) for %s: 0%o bit(s) unexpected",
242 statbuf.st_mode, filename, statbuf.st_mode & rdata->modemask);
246 /* Check the file owner and file group if required to do so. */
250 if (rdata->pw != NULL && statbuf.st_uid == rdata->pw->pw_uid)
252 else if (rdata->owners != NULL)
255 for (i = 1; i <= (int)(rdata->owners[0]); i++)
256 if (rdata->owners[i] == statbuf.st_uid) { uid_ok = TRUE; break; }
262 if (rdata->pw != NULL && statbuf.st_gid == rdata->pw->pw_gid)
264 else if (rdata->owngroups != NULL)
267 for (i = 1; i <= (int)(rdata->owngroups[0]); i++)
268 if (rdata->owngroups[i] == statbuf.st_gid) { gid_ok = TRUE; break; }
272 if (!uid_ok || !gid_ok)
274 *error = string_sprintf("bad %s for %s", uid_ok? "group" : "owner", filename);
278 /* Put an upper limit on the size of the file, just to stop silly people
279 feeding in ridiculously large files, which can easily be created by making
280 files that have holes in them. */
282 if (statbuf.st_size > MAX_FILTER_SIZE)
284 *error = string_sprintf("%s is too big (max %d)", filename, MAX_FILTER_SIZE);
288 /* Read the file in one go in order to minimize the time we have it open. */
290 filebuf = store_get(statbuf.st_size + 1);
292 if (fread(filebuf, 1, statbuf.st_size, fwd) != statbuf.st_size)
294 *error = string_sprintf("error while reading %s: %s",
295 filename, strerror(errno));
298 filebuf[statbuf.st_size] = 0;
301 debug_printf(OFF_T_FMT " bytes read from %s\n", statbuf.st_size, filename);
306 /* Return an error: the string is already set up. */
316 /*************************************************
317 * Extract info from list or filter *
318 *************************************************/
320 /* This function calls the appropriate function to extract addresses from a
321 forwarding list, or to run a filter file and get addresses from there.
324 rdata the redirection block
325 options the options bits
326 include_directory restrain to this directory
327 sieve_vacation_directory passed to sieve_interpret
328 sieve_enotify_mailto_owner passed to sieve_interpret
329 sieve_useraddress passed to sieve_interpret
330 sieve_subaddress passed to sieve_interpret
331 generated where to hang generated addresses
332 error for error messages
333 eblockp for details of skipped syntax errors
335 filtertype set to the filter type:
336 FILTER_FORWARD => a traditional .forward file
337 FILTER_EXIM => an Exim filter file
338 FILTER_SIEVE => a Sieve filter file
339 a system filter is always forced to be FILTER_EXIM
341 Returns: a suitable return for rda_interpret()
345 rda_extract(redirect_block *rdata, int options, uschar *include_directory,
346 uschar *sieve_vacation_directory, uschar *sieve_enotify_mailto_owner,
347 uschar *sieve_useraddress, uschar *sieve_subaddress,
348 address_item **generated, uschar **error, error_block **eblockp,
356 data = rda_get_file_contents(rdata, options, error, &yield);
357 if (data == NULL) return yield;
359 else data = rdata->string;
361 *filtertype = system_filtering? FILTER_EXIM : rda_is_filter(data);
363 /* Filter interpretation is done by a general function that is also called from
364 the filter testing option (-bf). There are two versions: one for Exim filtering
365 and one for Sieve filtering. Several features of string expansion may be locked
366 out at sites that don't trust users. This is done by setting flags in
367 expand_forbid that the expander inspects. */
369 if (*filtertype != FILTER_FORWARD)
372 int old_expand_forbid = expand_forbid;
374 DEBUG(D_route) debug_printf("data is %s filter program\n",
375 (*filtertype == FILTER_EXIM)? "an Exim" : "a Sieve");
377 /* RDO_FILTER is an "allow" bit */
379 if ((options & RDO_FILTER) == 0)
381 *error = US"filtering not enabled";
386 (expand_forbid & ~RDO_FILTER_EXPANSIONS) |
387 (options & RDO_FILTER_EXPANSIONS);
389 /* RDO_{EXIM,SIEVE}_FILTER are forbid bits */
391 if (*filtertype == FILTER_EXIM)
393 if ((options & RDO_EXIM_FILTER) != 0)
395 *error = US"Exim filtering not enabled";
398 frc = filter_interpret(data, options, generated, error);
402 if ((options & RDO_SIEVE_FILTER) != 0)
404 *error = US"Sieve filtering not enabled";
407 frc = sieve_interpret(data, options, sieve_vacation_directory,
408 sieve_enotify_mailto_owner, sieve_useraddress, sieve_subaddress,
412 expand_forbid = old_expand_forbid;
416 /* Not a filter script */
418 DEBUG(D_route) debug_printf("file is not a filter file\n");
420 return parse_forward_list(data,
421 options, /* specials that are allowed */
422 generated, /* where to hang them */
423 error, /* for errors */
424 deliver_domain, /* to qualify \name */
425 include_directory, /* restrain to directory */
426 eblockp); /* for skipped syntax errors */
432 /*************************************************
433 * Write string down pipe *
434 *************************************************/
436 /* This function is used for tranferring a string down a pipe between
437 processes. If the pointer is NULL, a length of zero is written.
447 rda_write_string(int fd, uschar *s)
449 int len = (s == NULL)? 0 : Ustrlen(s) + 1;
450 (void)write(fd, &len, sizeof(int));
451 if (s != NULL) (void)write(fd, s, len);
456 /*************************************************
457 * Read string from pipe *
458 *************************************************/
460 /* This function is used for receiving a string from a pipe.
464 sp where to put the string
466 Returns: FALSE if data missing
470 rda_read_string(int fd, uschar **sp)
474 if (read(fd, &len, sizeof(int)) != sizeof(int)) return FALSE;
475 if (len == 0) *sp = NULL; else
477 *sp = store_get(len);
478 if (read(fd, *sp, len) != len) return FALSE;
485 /*************************************************
486 * Interpret forward list or filter *
487 *************************************************/
489 /* This function is passed a forward list string (unexpanded) or the name of a
490 file (unexpanded) whose contents are the forwarding list. The list may in fact
491 be a filter program if it starts with "#Exim filter" or "#Sieve filter". Other
492 types of filter, with different inital tag strings, may be introduced in due
495 The job of the function is to process the forwarding list or filter. It is
496 pulled out into this separate function, because it is used for system filter
497 files as well as from the redirect router.
499 If the function is given a uid/gid, it runs a subprocess that passes the
500 results back via a pipe. This provides security for things like :include:s in
501 users' .forward files, and "logwrite" calls in users' filter files. A
502 sub-process is NOT used when:
504 . No uid/gid is provided
505 . The input is a string which is not a filter string, and does not contain
507 . The input is a file whose non-existence can be detected in the main
508 process (which is usually running as root).
511 rdata redirect data (file + constraints, or data string)
512 options options to pass to the extraction functions,
513 plus ENOTDIR and EACCES handling bits
514 include_directory restrain :include: to this directory
515 sieve_vacation_directory directory passed to sieve_interpret
516 sieve_enotify_mailto_owner passed to sieve_interpret
517 sieve_useraddress passed to sieve_interpret
518 sieve_subaddress passed to sieve_interpret
519 ugid uid/gid to run under - if NULL, no change
520 generated where to hang generated addresses, initially NULL
521 error pointer for error message
522 eblockp for skipped syntax errors; NULL if no skipping
523 filtertype set to the type of file:
524 FILTER_FORWARD => traditional .forward file
525 FILTER_EXIM => an Exim filter file
526 FILTER_SIEVE => a Sieve filter file
527 a system filter is always forced to be FILTER_EXIM
528 rname router name for error messages in the format
529 "xxx router" or "system filter"
531 Returns: values from extraction function, or FF_NONEXIST:
532 FF_DELIVERED success, a significant action was taken
533 FF_NOTDELIVERED success, no significant action
534 FF_BLACKHOLE :blackhole:
535 FF_DEFER defer requested
536 FF_FAIL fail requested
537 FF_INCLUDEFAIL some problem with :include:
538 FF_FREEZE freeze requested
539 FF_ERROR there was a problem
540 FF_NONEXIST the file does not exist
544 rda_interpret(redirect_block *rdata, int options, uschar *include_directory,
545 uschar *sieve_vacation_directory, uschar *sieve_enotify_mailto_owner,
546 uschar *sieve_useraddress, uschar *sieve_subaddress, ugid_block *ugid,
547 address_item **generated, uschar **error, error_block **eblockp,
548 int *filtertype, uschar *rname)
552 BOOL had_disaster = FALSE;
555 uschar *readerror = US"";
556 void (*oldsignal)(int);
558 DEBUG(D_route) debug_printf("rda_interpret (%s): %s\n",
559 (rdata->isfile)? "file" : "string", rdata->string);
561 /* Do the expansions of the file name or data first, while still privileged. */
563 data = expand_string(rdata->string);
566 if (expand_string_forcedfail) return FF_NOTDELIVERED;
567 *error = string_sprintf("failed to expand \"%s\": %s", rdata->string,
568 expand_string_message);
571 rdata->string = data;
573 DEBUG(D_route) debug_printf("expanded: %s\n", data);
575 if (rdata->isfile && data[0] != '/')
577 *error = string_sprintf("\"%s\" is not an absolute path", data);
581 /* If no uid/gid are supplied, or if we have a data string which does not start
582 with #Exim filter or #Sieve filter, and does not contain :include:, do all the
583 work in this process. Note that for a system filter, we always have a file, so
584 the work is done in this process only if no user is supplied. */
586 if (!ugid->uid_set || /* Either there's no uid, or */
587 (!rdata->isfile && /* We've got the data, and */
588 rda_is_filter(data) == FILTER_FORWARD && /* It's not a filter script, */
589 Ustrstr(data, ":include:") == NULL)) /* and there's no :include: */
591 return rda_extract(rdata, options, include_directory,
592 sieve_vacation_directory, sieve_enotify_mailto_owner, sieve_useraddress,
593 sieve_subaddress, generated, error, eblockp, filtertype);
596 /* We need to run the processing code in a sub-process. However, if we can
597 determine the non-existence of a file first, we can decline without having to
598 create the sub-process. */
600 if (rdata->isfile && rda_exists(data, error) == FILE_NOT_EXIST)
603 /* If the file does exist, or we can't tell (non-root mounted NFS directory)
604 we have to create the subprocess to do everything as the given user. The
605 results of processing are passed back via a pipe. */
608 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "creation of pipe for filter or "
609 ":include: failed for %s: %s", rname, strerror(errno));
611 /* Ensure that SIGCHLD is set to SIG_DFL before forking, so that the child
612 process can be waited for. We sometimes get here with it set otherwise. Save
613 the old state for resetting on the wait. Ensure that all cached resources are
614 freed so that the subprocess starts with a clean slate and doesn't interfere
615 with the parent process. */
617 oldsignal = signal(SIGCHLD, SIG_DFL);
620 if ((pid = fork()) == 0)
622 header_line *waslast = header_last; /* Save last header */
624 fd = pfd[pipe_write];
625 (void)close(pfd[pipe_read]);
626 exim_setugid(ugid->uid, ugid->gid, FALSE, rname);
628 /* Addresses can get rewritten in filters; if we are not root or the exim
629 user (and we probably are not), turn off rewrite logging, because we cannot
630 write to the log now. */
632 if (ugid->uid != root_uid && ugid->uid != exim_uid)
634 DEBUG(D_rewrite) debug_printf("turned off address rewrite logging (not "
635 "root or exim in this process)\n");
636 log_write_selector &= ~L_address_rewrite;
639 /* Now do the business */
641 yield = rda_extract(rdata, options, include_directory,
642 sieve_vacation_directory, sieve_enotify_mailto_owner, sieve_useraddress,
643 sieve_subaddress, generated, error, eblockp, filtertype);
645 /* Pass back whether it was a filter, and the return code and any overall
646 error text via the pipe. */
648 (void)write(fd, filtertype, sizeof(int));
649 (void)write(fd, &yield, sizeof(int));
650 rda_write_string(fd, *error);
652 /* Pass back the contents of any syntax error blocks if we have a pointer */
657 for (ep = *eblockp; ep != NULL; ep = ep->next)
659 rda_write_string(fd, ep->text1);
660 rda_write_string(fd, ep->text2);
662 rda_write_string(fd, NULL); /* Indicates end of eblocks */
665 /* If this is a system filter, we have to pass back the numbers of any
666 original header lines that were removed, and then any header lines that were
667 added but not subsequently removed. */
669 if (system_filtering)
673 for (h = header_list; h != waslast->next; i++, h = h->next)
675 if (h->type == htype_old) (void)write(fd, &i, sizeof(i));
678 (void)write(fd, &i, sizeof(i));
680 while (waslast != header_last)
682 waslast = waslast->next;
683 if (waslast->type != htype_old)
685 rda_write_string(fd, waslast->text);
686 (void)write(fd, &(waslast->type), sizeof(waslast->type));
689 rda_write_string(fd, NULL); /* Indicates end of added headers */
692 /* Write the contents of the $n variables */
694 (void)write(fd, filter_n, sizeof(filter_n));
696 /* If the result was DELIVERED or NOTDELIVERED, we pass back the generated
697 addresses, and their associated information, through the pipe. This is
698 just tedious, but it seems to be the only safe way. We do this also for
699 FAIL and FREEZE, because a filter is allowed to set up deliveries that
700 are honoured before freezing or failing. */
702 if (yield == FF_DELIVERED || yield == FF_NOTDELIVERED ||
703 yield == FF_FAIL || yield == FF_FREEZE)
706 for (addr = *generated; addr != NULL; addr = addr->next)
708 int reply_options = 0;
710 rda_write_string(fd, addr->address);
711 (void)write(fd, &(addr->mode), sizeof(addr->mode));
712 (void)write(fd, &(addr->flags), sizeof(addr->flags));
713 rda_write_string(fd, addr->p.errors_address);
715 if (addr->pipe_expandn != NULL)
718 for (pp = addr->pipe_expandn; *pp != NULL; pp++)
719 rda_write_string(fd, *pp);
721 rda_write_string(fd, NULL);
723 if (addr->reply == NULL)
724 (void)write(fd, &reply_options, sizeof(int)); /* 0 means no reply */
727 reply_options |= REPLY_EXISTS;
728 if (addr->reply->file_expand) reply_options |= REPLY_EXPAND;
729 if (addr->reply->return_message) reply_options |= REPLY_RETURN;
730 (void)write(fd, &reply_options, sizeof(int));
731 (void)write(fd, &(addr->reply->expand_forbid), sizeof(int));
732 (void)write(fd, &(addr->reply->once_repeat), sizeof(time_t));
733 rda_write_string(fd, addr->reply->to);
734 rda_write_string(fd, addr->reply->cc);
735 rda_write_string(fd, addr->reply->bcc);
736 rda_write_string(fd, addr->reply->from);
737 rda_write_string(fd, addr->reply->reply_to);
738 rda_write_string(fd, addr->reply->subject);
739 rda_write_string(fd, addr->reply->headers);
740 rda_write_string(fd, addr->reply->text);
741 rda_write_string(fd, addr->reply->file);
742 rda_write_string(fd, addr->reply->logfile);
743 rda_write_string(fd, addr->reply->oncelog);
747 rda_write_string(fd, NULL); /* Marks end of addresses */
750 /* OK, this process is now done. Free any cached resources. Must use _exit()
758 /* Back in the main process: panic if the fork did not succeed. */
761 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "fork failed for %s", rname);
763 /* Read the pipe to get the data from the filter/forward. Our copy of the
764 writing end must be closed first, as otherwise read() won't return zero on an
765 empty pipe. Afterwards, close the reading end. */
767 (void)close(pfd[pipe_write]);
769 /* Read initial data, including yield and contents of *error */
772 if (read(fd, filtertype, sizeof(int)) != sizeof(int) ||
773 read(fd, &yield, sizeof(int)) != sizeof(int) ||
774 !rda_read_string(fd, error)) goto DISASTER;
776 /* Read the contents of any syntax error blocks if we have a pointer */
782 error_block **p = eblockp;
785 if (!rda_read_string(fd, &s)) goto DISASTER;
786 if (s == NULL) break;
787 e = store_get(sizeof(error_block));
790 if (!rda_read_string(fd, &s)) goto DISASTER;
797 /* If this is a system filter, read the identify of any original header lines
798 that were removed, and then read data for any new ones that were added. */
800 if (system_filtering)
803 header_line *h = header_list;
808 if (read(fd, &n, sizeof(int)) != sizeof(int)) goto DISASTER;
814 if (h == NULL) goto DISASTER_NO_HEADER;
823 if (!rda_read_string(fd, &s)) goto DISASTER;
824 if (s == NULL) break;
825 if (read(fd, &type, sizeof(type)) != sizeof(type)) goto DISASTER;
826 header_add(type, "%s", s);
830 /* Read the values of the $n variables */
832 if (read(fd, filter_n, sizeof(filter_n)) != sizeof(filter_n)) goto DISASTER;
834 /* If the yield is DELIVERED, NOTDELIVERED, FAIL, or FREEZE there may follow
835 addresses and data to go with them. Keep them in the same order in the
838 if (yield == FF_DELIVERED || yield == FF_NOTDELIVERED ||
839 yield == FF_FAIL || yield == FF_FREEZE)
841 address_item **nextp = generated;
845 int i, reply_options;
848 uschar *expandn[EXPAND_MAXN + 2];
850 /* First string is the address; NULL => end of addresses */
852 if (!rda_read_string(fd, &recipient)) goto DISASTER;
853 if (recipient == NULL) break;
855 /* Hang on the end of the chain */
857 addr = deliver_make_addr(recipient, FALSE);
859 nextp = &(addr->next);
861 /* Next comes the mode and the flags fields */
863 if (read(fd, &(addr->mode), sizeof(addr->mode)) != sizeof(addr->mode) ||
864 read(fd, &(addr->flags), sizeof(addr->flags)) != sizeof(addr->flags) ||
865 !rda_read_string(fd, &(addr->p.errors_address))) goto DISASTER;
867 /* Next comes a possible setting for $thisaddress and any numerical
868 variables for pipe expansion, terminated by a NULL string. The maximum
869 number of numericals is EXPAND_MAXN. Note that we put filter_thisaddress
870 into the zeroth item in the vector - this is sorted out inside the pipe
873 for (i = 0; i < EXPAND_MAXN + 1; i++)
876 if (!rda_read_string(fd, &temp)) goto DISASTER;
877 if (i == 0) filter_thisaddress = temp; /* Just in case */
879 if (temp == NULL) break;
884 addr->pipe_expandn = store_get((i+1) * sizeof(uschar **));
885 addr->pipe_expandn[i] = NULL;
886 while (--i >= 0) addr->pipe_expandn[i] = expandn[i];
889 /* Then an int containing reply options; zero => no reply data. */
891 if (read(fd, &reply_options, sizeof(int)) != sizeof(int)) goto DISASTER;
892 if ((reply_options & REPLY_EXISTS) != 0)
894 addr->reply = store_get(sizeof(reply_item));
896 addr->reply->file_expand = (reply_options & REPLY_EXPAND) != 0;
897 addr->reply->return_message = (reply_options & REPLY_RETURN) != 0;
899 if (read(fd,&(addr->reply->expand_forbid),sizeof(int)) !=
901 read(fd,&(addr->reply->once_repeat),sizeof(time_t)) !=
903 !rda_read_string(fd, &(addr->reply->to)) ||
904 !rda_read_string(fd, &(addr->reply->cc)) ||
905 !rda_read_string(fd, &(addr->reply->bcc)) ||
906 !rda_read_string(fd, &(addr->reply->from)) ||
907 !rda_read_string(fd, &(addr->reply->reply_to)) ||
908 !rda_read_string(fd, &(addr->reply->subject)) ||
909 !rda_read_string(fd, &(addr->reply->headers)) ||
910 !rda_read_string(fd, &(addr->reply->text)) ||
911 !rda_read_string(fd, &(addr->reply->file)) ||
912 !rda_read_string(fd, &(addr->reply->logfile)) ||
913 !rda_read_string(fd, &(addr->reply->oncelog)))
919 /* All data has been transferred from the sub-process. Reap it, close the
920 reading end of the pipe, and we are done. */
923 while ((rc = wait(&status)) != pid)
925 if (rc < 0 && errno == ECHILD) /* Process has vanished */
927 log_write(0, LOG_MAIN, "redirection process %d vanished unexpectedly", pid);
933 debug_printf("rda_interpret: subprocess yield=%d error=%s\n", yield, *error);
937 *error = string_sprintf("internal problem in %s: failure to transfer "
938 "data from subprocess: status=%04x%s%s%s", rname,
940 (*error == NULL)? US"" : US": error=",
941 (*error == NULL)? US"" : *error);
942 log_write(0, LOG_MAIN|LOG_PANIC, "%s", *error);
944 else if (status != 0)
946 log_write(0, LOG_MAIN|LOG_PANIC, "internal problem in %s: unexpected status "
947 "%04x from redirect subprocess (but data correctly received)", rname,
953 signal(SIGCHLD, oldsignal); /* restore */
957 /* Come here if the data indicates removal of a header that we can't find */
960 readerror = US" readerror=bad header identifier";
965 /* Come here is there's a shambles in transferring the data over the pipe. The
966 value of errno should still be set. */
969 readerror = string_sprintf(" readerror='%s'", strerror(errno));