2 * sink.c -- forwarding/delivery support for fetchmail
4 * The interface of this module (open_sink(), stuff_line(), close_sink(),
5 * release_sink()) seals off the delivery logic from the protocol machine,
6 * so the latter won't have to care whether it's shipping to an [SL]MTP
7 * listener daemon or an MDA pipe.
9 * Copyright 1998 by Eric S. Raymond
10 * For license terms, see the file COPYING in this directory.
20 #endif /* HAVE_MEMORY_H */
21 #if defined(STDC_HEADERS)
24 #if defined(HAVE_UNISTD_H)
27 #if defined(HAVE_STDARG_H)
33 #include "fetchmail.h"
37 /* BSD portability hack...I know, this is an ugly place to put it */
38 #if !defined(SIGCHLD) && defined(SIGCLD)
39 #define SIGCHLD SIGCLD
43 #define SMTP_PORT "smtp" /* standard SMTP service port */
45 #define SMTP_PORT 25 /* standard SMTP service port */
48 static int lmtp_responses; /* how many should we expect? */
49 static struct msgblk msgcopy; /* copies of various message internals */
51 static int smtp_open(struct query *ctl)
52 /* try to open a socket to the appropriate SMTP server for this query */
54 /* maybe it's time to close the socket in order to force delivery */
55 if (NUM_NONZERO(ctl->batchlimit) && (ctl->smtp_socket != -1) && batchcount++ == ctl->batchlimit)
57 close(ctl->smtp_socket);
58 ctl->smtp_socket = -1;
62 /* if no socket to any SMTP host is already set up, try to open one */
63 if (ctl->smtp_socket == -1)
66 * RFC 1123 requires that the domain name in HELO address is a
67 * "valid principal domain name" for the client host. If we're
68 * running in invisible mode, violate this with malice
69 * aforethought in order to make the Received headers and
72 * In fact this code relies on the RFC1123 requirement that the
73 * SMTP listener must accept messages even if verification of the
74 * HELO name fails (RFC1123 section 5.2.5, paragraph 2).
76 * How we compute the true mailhost name to pass to the
77 * listener doesn't affect behavior on RFC1123- violating
78 * listeners that check for name match; we're going to lose
79 * on those anyway because we can never give them a name
80 * that matches the local machine fetchmail is running on.
81 * What it will affect is the listener's logging.
84 const char *id_me = run.invisible ? ctl->server.truename : fetchmailhost;
90 * Run down the SMTP hunt list looking for a server that's up.
91 * Use both explicit hunt entries (value TRUE) and implicit
92 * (default) ones (value FALSE).
95 phase = LISTENER_WAIT;
97 set_timeout(ctl->server.timeout);
98 for (idp = ctl->smtphunt; idp; idp = idp->next)
100 char *cp, *parsed_host;
102 char *portnum = SMTP_PORT;
104 int portnum = SMTP_PORT;
107 xalloca(parsed_host, char *, strlen(idp->id) + 1);
109 ctl->smtphost = idp->id; /* remember last host tried. */
111 strcpy(parsed_host, idp->id);
112 if ((cp = strrchr(parsed_host, '/')))
122 if ((ctl->smtp_socket = SockOpen(parsed_host,portnum,NULL,
123 ctl->server.plugout)) == -1)
126 /* are we doing SMTP or LMTP? */
127 SMTP_setmode(ctl->listener);
129 /* first, probe for ESMTP */
130 if (SMTP_ok(ctl->smtp_socket) == SM_OK &&
131 SMTP_ehlo(ctl->smtp_socket, id_me,
132 &ctl->server.esmtp_options) == SM_OK)
136 * RFC 1869 warns that some listeners hang up on a failed EHLO,
137 * so it's safest not to assume the socket will still be good.
139 SockClose(ctl->smtp_socket);
140 ctl->smtp_socket = -1;
142 /* if opening for ESMTP failed, try SMTP */
143 if ((ctl->smtp_socket = SockOpen(parsed_host,portnum,NULL,
144 ctl->server.plugout)) == -1)
147 if (SMTP_ok(ctl->smtp_socket) == SM_OK &&
148 SMTP_helo(ctl->smtp_socket, id_me) == SM_OK)
151 SockClose(ctl->smtp_socket);
152 ctl->smtp_socket = -1;
159 * RFC 1123 requires that the domain name part of the
160 * RCPT TO address be "canonicalized", that is a FQDN
161 * or MX but not a CNAME. Some listeners (like exim)
162 * enforce this. Now that we have the actual hostname,
163 * compute what we should canonicalize with.
165 ctl->destaddr = ctl->smtpaddress ? ctl->smtpaddress : ( ctl->smtphost ? ctl->smtphost : "localhost");
167 if (outlevel >= O_DEBUG && ctl->smtp_socket != -1)
168 error(0, 0, "forwarding to %s", ctl->smtphost);
170 return(ctl->smtp_socket);
173 /* these are shared by open_sink and stuffline */
175 static RETSIGTYPE (*sigchld)(int);
177 int stuffline(struct query *ctl, char *buf)
178 /* ship a line to the given control block's output sink (SMTP server or MDA) */
183 /* The line may contain NUL characters. Find the last char to use
184 * -- the real line termination is the sequence "\n\0".
187 while ((last += strlen(last)) && (last[-1] != '\n'))
190 /* fix message lines that have only \n termination (for qmail) */
193 if (last - 1 == buf || last[-2] != '\r')
202 phase = FORWARDING_WAIT;
205 * SMTP byte-stuffing. We only do this if the protocol does *not*
206 * use .<CR><LF> as EOM. If it does, the server will already have
207 * decorated any . lines it sends back up.
210 if (ctl->server.base_protocol->delimited) /* server has already byte-stuffed */
215 /* writing to SMTP, leave the byte-stuffing in place */;
217 else /* if (!protocol->delimited) -- not byte-stuffed already */
220 SockWrite(ctl->smtp_socket, buf, 1); /* byte-stuff it */
222 /* leave it alone */;
225 /* we may need to strip carriage returns */
230 for (sp = tp = buf; sp < last; sp++)
238 if (ctl->mda || ctl->bsmtp)
239 n = fwrite(buf, 1, last - buf, sinkfp);
240 else if (ctl->smtp_socket != -1)
241 n = SockWrite(ctl->smtp_socket, buf, last - buf);
248 static void sanitize(char *s)
249 /* replace unsafe shellchars by an _ */
251 const static char *ok_chars = " 1234567890!@%-_=+:,./abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
254 for (cp = s; *(cp += strspn(cp, ok_chars)); /* NO INCREMENT */)
258 int open_sink(struct query *ctl, struct msgblk *msg,
259 int *good_addresses, int *bad_addresses)
260 /* set up sinkfp to be an input sink we can ship a message to */
264 *bad_addresses = *good_addresses = 0;
266 if (ctl->bsmtp) /* dump to a BSMTP batch file */
268 if (strcmp(ctl->bsmtp, "-") == 0)
271 sinkfp = fopen(ctl->bsmtp, "a");
273 /* see the ap computation under the SMTP branch */
275 "MAIL FROM: %s", (msg->return_path[0]) ? msg->return_path : user);
277 if (ctl->pass8bits || (ctl->mimemsg & MSG_IS_8BIT))
278 fputs(" BODY=8BITMIME", sinkfp);
279 else if (ctl->mimemsg & MSG_IS_7BIT)
280 fputs(" BODY=7BIT", sinkfp);
282 fprintf(sinkfp, " SIZE=%ld\r\n", msg->reallen);
285 * RFC 1123 requires that the domain name part of the
286 * RCPT TO address be "canonicalized", that is a FQDN
287 * or MX but not a CNAME. Some listeners (like exim)
288 * enforce this. Now that we have the actual hostname,
289 * compute what we should canonicalize with.
291 ctl->destaddr = ctl->smtpaddress ? ctl->smtpaddress : "localhost";
294 for (idp = msg->xmit_names; idp; idp = idp->next)
295 if (idp->val.status.mark == XMIT_ACCEPT)
297 if (strchr(idp->id, '@'))
299 "RCPT TO: %s\r\n", idp->id);
302 "RCPT TO: %s@%s\r\n", idp->id, ctl->destaddr);
306 fputs("DATA\r\n", sinkfp);
310 error(0, -1, "BSMTP file open or preamble write failed");
314 else if (ctl->mda) /* we have a declared MDA */
316 int length = 0, fromlen = 0, nameslen = 0;
317 char *names = NULL, *before, *after, *from = NULL;
319 ctl->destaddr = "localhost";
321 for (idp = msg->xmit_names; idp; idp = idp->next)
322 if (idp->val.status.mark == XMIT_ACCEPT)
325 length = strlen(ctl->mda);
326 before = xstrdup(ctl->mda);
328 /* get user addresses for %T (or %s for backward compatibility) */
329 if (strstr(before, "%s") || strstr(before, "%T"))
332 * We go through this in order to be able to handle very
333 * long lists of users and (re)implement %s.
336 for (idp = msg->xmit_names; idp; idp = idp->next)
337 if ((idp->val.status.mark == XMIT_ACCEPT))
338 nameslen += (strlen(idp->id) + 1); /* string + ' ' */
339 if ((*good_addresses == 0))
340 nameslen = strlen(run.postmaster);
342 names = (char *)xmalloc(nameslen + 1); /* account for '\0' */
343 if (*good_addresses == 0)
344 strcpy(names, run.postmaster);
348 for (idp = msg->xmit_names; idp; idp = idp->next)
349 if (idp->val.status.mark == XMIT_ACCEPT)
351 strcat(names, idp->id);
354 names[--nameslen] = '\0'; /* chop trailing space */
357 /* sanitize names in order to contain only harmless shell chars */
361 /* get From address for %F */
362 if (strstr(before, "%F"))
364 from = xstrdup(msg->return_path);
366 /* sanitize from in order to contain *only* harmless shell chars */
369 fromlen = strlen(from);
372 /* do we have to build an mda string? */
377 /* find length of resulting mda string */
379 while ((sp = strstr(sp, "%s"))) {
380 length += nameslen - 2; /* subtract %s */
384 while ((sp = strstr(sp, "%T"))) {
385 length += nameslen - 2; /* subtract %T */
389 while ((sp = strstr(sp, "%F"))) {
390 length += fromlen - 2; /* subtract %F */
394 after = xmalloc(length + 1);
396 /* copy mda source string to after, while expanding %[sTF] */
397 for (dp = after, sp = before; (*dp = *sp); dp++, sp++) {
398 if (sp[0] != '%') continue;
400 /* need to expand? BTW, no here overflow, because in
401 ** the worst case (end of string) sp[1] == '\0' */
402 if (sp[1] == 's' || sp[1] == 'T') {
405 sp++; /* position sp over [sT] */
406 dp--; /* adjust dp */
407 } else if (sp[1] == 'F') {
410 sp++; /* position sp over F */
411 dp--; /* adjust dp */
430 if (outlevel >= O_DEBUG)
431 error(0, 0, "about to deliver with: %s", before);
435 * Arrange to run with user's permissions if we're root.
436 * This will initialize the ownership of any files the
437 * MDA creates properly. (The seteuid call is available
438 * under all BSDs and Linux)
441 #endif /* HAVE_SETEUID */
443 sinkfp = popen(before, "w");
448 /* this will fail quietly if we didn't start as root */
450 #endif /* HAVE_SETEUID */
454 error(0, 0, "MDA open failed");
458 sigchld = signal(SIGCHLD, SIG_DFL);
460 else /* forward to an SMTP listener */
463 char options[MSGBUFSIZE], addr[128];
465 /* build a connection to the SMTP listener */
466 if ((smtp_open(ctl) == -1))
468 error(0, errno, "%cMTP connect to %s failed",
470 ctl->smtphost ? ctl->smtphost : "localhost");
475 * Stash a copy of the parsed message block
476 * for use by close_sink().
478 memcpy(&msgcopy, msg, sizeof(struct msgblk));
481 * Compute ESMTP options.
484 if (ctl->server.esmtp_options & ESMTP_8BITMIME) {
485 if (ctl->pass8bits || (ctl->mimemsg & MSG_IS_8BIT))
486 strcpy(options, " BODY=8BITMIME");
487 else if (ctl->mimemsg & MSG_IS_7BIT)
488 strcpy(options, " BODY=7BIT");
491 if ((ctl->server.esmtp_options & ESMTP_SIZE) && msg->reallen > 0)
492 sprintf(options + strlen(options), " SIZE=%ld", msg->reallen);
495 * Try to get the SMTP listener to take the Return-Path
496 * address as MAIL FROM . If it won't, fall back on the
497 * calling-user ID. This won't affect replies, which use the
498 * header From address anyway.
500 * RFC 1123 requires that the domain name part of the
501 * MAIL FROM address be "canonicalized", that is a
502 * FQDN or MX but not a CNAME. We'll assume the From
503 * header is already in this form here (it certainly
504 * is if rewrite is on). RFC 1123 is silent on whether
505 * a nonexistent hostname part is considered canonical.
507 * This is a potential problem if the MTAs further upstream
508 * didn't pass canonicalized From/Return-Path lines, *and* the
509 * local SMTP listener insists on them.
511 * None of these error conditions generates bouncemail. Comments
512 * below explain for each case why this is so.
514 ap = (msg->return_path[0]) ? msg->return_path : user;
515 if (SMTP_from(ctl->smtp_socket, ap, options) != SM_OK)
517 int smtperr = atoi(smtp_response);
519 if (str_find(&ctl->antispam, smtperr))
522 * SMTP listener explicitly refuses to deliver mail
523 * coming from this address, probably due to an
524 * anti-spam domain exclusion. Respect this. Don't
525 * try to ship the message, and don't prevent it from
526 * being deleted. Typical values:
528 * 501 = exim's old antispam response
529 * 550 = exim's new antispam response (temporary)
530 * 553 = sendmail 8.8.7's generic REJECT
531 * 571 = sendmail's "unsolicited email refused"
533 * We don't send bouncemail on antispam failures because
534 * we don't want the scumbags to know the address is even
537 SMTP_rset(ctl->smtp_socket); /* required by RFC1870 */
542 * Suppress error message only if the response specifically
543 * meant `excluded for policy reasons'. We *should* see
544 * an error when the return code is less specific.
547 error(0, -1, "%cMTP error: %s",
553 case 452: /* insufficient system storage */
555 * Temporary out-of-queue-space condition on the
556 * ESMTP server. Don't try to ship the message,
557 * and suppress deletion so it can be retried on
558 * a future retrieval cycle.
560 * Bouncemail *might* be appropriate here as a delay
561 * notification. But it's not really necessary because
562 * this is not an actual failure, we're very likely to be
563 * able to recover on the next cycle.
565 SMTP_rset(ctl->smtp_socket); /* required by RFC1870 */
566 return(PS_TRANSIENT);
568 case 552: /* message exceeds fixed maximum message size */
569 case 553: /* invalid sending domain */
571 * Permanent no-go condition on the
572 * ESMTP server. Don't try to ship the message,
573 * and allow it to be deleted.
575 * Bouncemail would be appropriate for 552, but in these
576 * latter days 553 usually means a spammer is trying to
577 * cover his tracks. We'd rather deny the scumbags any
578 * feedback that the address is valid.
580 SMTP_rset(ctl->smtp_socket); /* required by RFC1870 */
583 default: /* retry with postmaster's address */
584 if (SMTP_from(ctl->smtp_socket,run.postmaster,options)!=SM_OK)
586 error(0, -1, "%cMTP error: %s",
589 return(PS_SMTP); /* should never happen */
595 * Now list the recipient addressees
597 for (idp = msg->xmit_names; idp; idp = idp->next)
598 if (idp->val.status.mark == XMIT_ACCEPT)
600 if (strchr(idp->id, '@'))
601 strcpy(addr, idp->id);
604 snprintf(addr, sizeof(addr)-1, "%s@%s", idp->id, ctl->destaddr);
606 sprintf(addr, "%s@%s", idp->id, ctl->destaddr);
607 #endif /* HAVE_SNPRINTF */
609 if (SMTP_rcpt(ctl->smtp_socket, addr) == SM_OK)
614 idp->val.status.mark = XMIT_ANTISPAM;
616 "%cMTP listener doesn't like recipient address `%s'",
617 ctl->listener, addr);
620 if (!(*good_addresses))
623 snprintf(addr, sizeof(addr)-1, "%s@%s", run.postmaster, ctl->destaddr);
625 sprintf(addr, "%s@%s", run.postmaster, ctl->destaddr);
626 #endif /* HAVE_SNPRINTF */
628 if (SMTP_rcpt(ctl->smtp_socket, addr) != SM_OK)
630 error(0, 0, "can't even send to %s!", run.postmaster);
631 SMTP_rset(ctl->smtp_socket); /* required by RFC1870 */
636 /* tell it we're ready to send data */
637 SMTP_data(ctl->smtp_socket);
641 * We need to stash this away in order to know how many
642 * response lines to expect after the LMTP end-of-message.
644 lmtp_responses = *good_addresses;
649 void release_sink(struct query *ctl)
650 /* release the per-message output sink, whether it's a pipe or SMTP socket */
659 sinkfp = (FILE *)NULL;
661 signal(SIGCHLD, sigchld);
665 int close_sink(struct query *ctl, flag forward)
666 /* perform end-of-message actions on the current output sink */
672 /* close the delivery pipe, we'll reopen before next message */
676 sinkfp = (FILE *)NULL;
680 signal(SIGCHLD, sigchld);
683 error(0, -1, "MDA exited abnormally or returned nonzero status");
689 /* implicit disk-full check here... */
690 fputs("..\r\n", sinkfp);
691 if (strcmp(ctl->bsmtp, "-"))
695 error(0, -1, "Message termination or close of BSMTP file failed");
701 /* write message terminator */
702 if (SMTP_eom(ctl->smtp_socket) != SM_OK)
704 error(0, -1, "SMTP listener refused delivery");
709 * If this is an SMTP connection, SMTP_eom() ate the response.
710 * But could be this is an LMTP connection, in which case we have to
711 * interpret either (a) a single 503 response meaning there
712 * were no successful RCPT TOs, or (b) a variable number of
713 * responses, one for each successful RCPT TO. We need to send
714 * bouncemail on each failed response and then return TRUE anyway,
715 * otherwise the message will get left in the queue and resent
716 * to people who got it the first time.
718 if (ctl->listener == LMTP_MODE)
719 if (lmtp_responses == 0)
721 SMTP_ok(ctl->smtp_socket);
724 * According to RFC2033, 503 is the only legal response
725 * if no RCPT TO commands succeeded. No error recovery
726 * is really possible here, as we have no idea what
727 * insane thing the listener might be doing if it doesn't
730 if (atoi(smtp_response) == 503)
731 error(0, -1, "LMTP delivery error on EOM");
734 "Unexpected non-503 response to LMTP EOM: %s",
738 * It's not completely clear what to do here. We choose to
739 * interpret delivery failure here as a transient error,
740 * the same way SMTP delivery failure is handled. If we're
741 * wrong, an undead message will get stuck in the queue.
750 /* eat the RFC2033-required responses, saving errors */
751 xalloca(responses, char **, sizeof(char *) * lmtp_responses);
752 for (errors = i = 0; i < lmtp_responses; i++)
754 if (SMTP_ok(ctl->smtp_socket) == SM_OK)
755 responses[i] = (char *)NULL;
758 xalloca(responses[errors],
760 strlen(smtp_response)+1);
761 strcpy(responses[errors], smtp_response);
767 return(TRUE); /* all deliveries succeeded */
768 else if (errors == lmtp_responses)
769 return(FALSE); /* all deliveries failed */
773 * Now life gets messy. There are multiple recipients,
774 * and one or more (but not all) deliveries failed.
776 * What we'd really like to do is bounce a
777 * failures list back to the sender and return
778 * TRUE, deleting the message from the server so
779 * it won't be re-forwarded on subsequent poll
782 * We can't do that yet, so instead we report
783 * failures to the calling-user/postmaster. If we can't,
784 * there's a transient error in local delivery; leave
785 * the message on the server.
787 char buf[MSGBUFSIZE];
789 if (open_warning_by_mail(ctl))
792 /* generate an error report a la RFC 1892 */
793 stuff_warning(ctl, "MIME-Version: 1.0");
794 stuff_warning(ctl, "Content-Type: multipart/report report-type=text/plain boundary=\"om-mani-padme-hum\"");
795 stuff_warning(ctl, "Content-Transfer-Encoding: 7bit");
796 stuff_warning(ctl, "");
799 /* RFC1892 part 1 -- human-readable message */
800 stuff_warning(ctl, "-- om-mani-padme-hum");
801 stuff_warning(ctl, "");
803 /* RFC1892 part 2 -- machine-readable responses */
804 stuff_warning(ctl, "-- om-mani-padme-hum");
805 stuff_warning(ctl,"Content-Type: message/delivery-status");
806 stuff_warning(ctl, "");
807 for (i = 0; i < errors; i++)
808 stuff_warning(ctl, responses[i]);
810 /* RFC1892 part 3 -- headers of undelivered message */
811 stuff_warning(ctl, "-- om-mani-padme-hum");
812 stuff_warning(ctl, "Content-Type: text/rfc822-headers");
813 stuff_warning(ctl, "");
814 stuffline(ctl, msgcopy.headers);
816 stuff_warning(ctl, "-- om-mani-padme-hum --");
818 close_warning_by_mail(ctl);
821 * It's not completely clear what to do here,
822 * either. We choose to interpret delivery
823 * failure here as a permanent error, so the
824 * people who got successful deliveries won't see
825 * endless repetitions of the same message on
826 * subsequent poll messages. If we're wrong, a
827 * transient error will cause someone to lose
828 * mail. This could only happen with
829 * multi-recipient messages coming from a remote
830 * mailserver to two or more local users...
840 int open_warning_by_mail(struct query *ctl)
841 /* set up output sink for a mailed warning to calling user */
844 static struct msgblk msg = {NULL, NULL, "FETCHMAIL-DAEMON", 0};
847 * We give a null address list as arg 4 because we actually *want*
848 * this message to go to run.postmaster. The zero length arg 5 means
849 * we won't pass a SIZE option to ESMTP; the message length would
850 * be more trouble than it's worth to compute.
852 return(open_sink(ctl, &msg, &good, &bad));
855 #if defined(HAVE_STDARG_H)
856 void stuff_warning(struct query *ctl, const char *fmt, ... )
858 void stuff_warning(struct query *ctl, fmt, va_alist)
860 const char *fmt; /* printf-style format */
863 /* format and ship a warning message line by mail */
865 char buf[POPBUFSIZE];
869 * stuffline() requires its input to be writeable (for CR stripping),
870 * so we needed to copy the message to a writeable buffer anyway in
871 * case it was a string constant. We make a virtue of that necessity
872 * here by supporting stdargs/varargs.
874 #if defined(HAVE_STDARG_H)
879 #ifdef HAVE_VSNPRINTF
880 vsnprintf(buf, sizeof(buf), fmt, ap);
882 vsprintf(buf, fmt, ap);
891 void close_warning_by_mail(struct query *ctl)
892 /* sign and send mailed warnings */
894 stuff_warning(ctl, "--\r\n\t\t\t\tThe Fetchmail Daemon\r\n");
895 close_sink(ctl, TRUE);
898 /* sink.c ends here */