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)
35 #include "fetchmail.h"
37 /* for W* macros after pclose() */
39 #include <sys/types.h>
40 #include <sys/resource.h>
47 /* BSD portability hack...I know, this is an ugly place to put it */
48 #if !defined(SIGCHLD) && defined(SIGCLD)
49 #define SIGCHLD SIGCLD
52 /* makes the open_sink()/close_sink() pair non-reentrant */
53 static int lmtp_responses;
55 void smtp_close(struct query *ctl, int sayquit)
56 /* close the socket to SMTP server */
58 if (ctl->smtp_socket != -1)
61 SMTP_quit(ctl->smtp_socket, ctl->smtphostmode);
62 SockClose(ctl->smtp_socket);
63 ctl->smtp_socket = -1;
68 static void smtp_rset(struct query *ctl)
69 /* reset the mail transaction */
71 if (SMTP_rset(ctl->smtp_socket, ctl->smtphostmode) == SM_UNRECOVERABLE)
73 /* close the bad connection. fetchmail will reconnect for the
79 int smtp_setup(struct query *ctl)
80 /* try to open a socket to the appropriate SMTP server for this query */
82 /* maybe it's time to close the socket in order to force delivery */
83 if (last_smtp_ok > 0 && time((time_t *)NULL) - last_smtp_ok > mytimeout)
88 if (NUM_NONZERO(ctl->batchlimit)) {
89 if (batchcount == ctl->batchlimit)
94 /* if no socket to any SMTP host is already set up, try to open one */
95 if (ctl->smtp_socket == -1)
98 * RFC 1123 requires that the domain name in HELO address is a
99 * "valid principal domain name" for the client host. If we're
100 * running in invisible mode, violate this with malice
101 * aforethought in order to make the Received headers and
102 * logging look right.
104 * In fact this code relies on the RFC1123 requirement that the
105 * SMTP listener must accept messages even if verification of the
106 * HELO name fails (RFC1123 section 5.2.5, paragraph 2).
108 * How we compute the true mailhost name to pass to the
109 * listener doesn't affect behavior on RFC1123-violating
110 * listeners that check for name match; we're going to lose
111 * on those anyway because we can never give them a name
112 * that matches the local machine fetchmail is running on.
113 * What it will affect is the listener's logging.
116 const char *id_me = run.invisible ? ctl->server.truename : fetchmailhost;
118 char *parsed_host = NULL;
123 * Run down the SMTP hunt list looking for a server that's up.
124 * Use both explicit hunt entries (value TRUE) and implicit
125 * (default) ones (value FALSE).
128 phase = LISTENER_WAIT;
130 set_timeout(ctl->server.timeout);
131 for (idp = ctl->smtphunt; idp; idp = idp->next)
134 const char *portnum = SMTP_PORT;
136 ctl->smtphost = idp->id; /* remember last host tried. */
137 if (ctl->smtphost[0]=='/')
139 ctl->smtphostmode = LMTP_MODE;
141 if ((ctl->smtp_socket = UnixOpen(ctl->smtphost))==-1)
146 ctl->smtphostmode = ctl->listener;
147 parsed_host = xstrdup(idp->id);
148 if ((cp = strrchr(parsed_host, '/')))
154 if ((ctl->smtp_socket = SockOpen(parsed_host,portnum,
155 ctl->server.plugout, &ai1)) == -1)
162 /* return immediately for ODMR */
163 if (ctl->server.protocol == P_ODMR)
168 return(ctl->smtp_socket); /* success */
171 /* first, probe for ESMTP */
172 if (SMTP_ok(ctl->smtp_socket, ctl->smtphostmode, TIMEOUT_STARTSMTP) == SM_OK &&
173 SMTP_ehlo(ctl->smtp_socket, ctl->smtphostmode, id_me,
174 ctl->server.esmtp_name, ctl->server.esmtp_password,
175 &ctl->server.esmtp_options) == SM_OK)
179 * RFC 1869 warns that some listeners hang up on a failed EHLO,
180 * so it's safest not to assume the socket will still be good.
184 /* if opening for ESMTP failed, try SMTP */
185 if (ctl->smtphost[0]=='/')
187 if ((ctl->smtp_socket = UnixOpen(ctl->smtphost))==-1)
192 if ((ctl->smtp_socket = SockOpen(parsed_host,portnum,
193 ctl->server.plugout, &ai1)) == -1)
200 if (SMTP_ok(ctl->smtp_socket, ctl->smtphostmode, TIMEOUT_STARTSMTP) == SM_OK &&
201 SMTP_helo(ctl->smtp_socket, ctl->smtphostmode, id_me) == SM_OK)
210 * RFC 1123 requires that the domain name part of the
211 * RCPT TO address be "canonicalized", that is a FQDN
212 * or MX but not a CNAME. Some listeners (like exim)
213 * enforce this. Now that we have the actual hostname,
214 * compute what we should canonicalize with.
216 xfree(ctl->destaddr);
217 if (ctl->smtpaddress)
218 ctl->destaddr = xstrdup(ctl->smtpaddress);
219 /* parsed_host is smtphost without the /port */
220 else if (parsed_host && parsed_host[0] != 0)
221 ctl->destaddr = xstrdup(parsed_host);
222 /* No smtphost is specified or it is a UNIX socket, then use
223 localhost as a domain part. */
225 ctl->destaddr = xstrdup("localhost");
228 /* end if (ctl->smtp_socket == -1) */
230 if (outlevel >= O_DEBUG && ctl->smtp_socket != -1)
231 report(stdout, GT_("forwarding to %s\n"), ctl->smtphost);
233 return(ctl->smtp_socket);
236 static void sanitize(char *s)
241 for (cp = s; (cp = strchr (cp, '\'')); cp++)
245 char *rcpt_address(struct query *ctl, const char *id,
248 static char addr[HOSTLEN+USERNAMELEN+1];
251 snprintf(addr, sizeof (addr), "%s", id);
253 else if (usesmtpname && ctl->smtpname)
255 snprintf(addr, sizeof (addr), "%s", ctl->smtpname);
259 snprintf(addr, sizeof (addr), "%s@%s", id, ctl->destaddr);
264 static int send_bouncemail(struct query *ctl, struct msgblk *msg,
265 int userclass, const char *message /* should have \r\n at the end */,
266 int nerrors, char *errors[])
267 /* bounce back an error report a la RFC 1892 */
269 char daemon_name[15 + HOSTLEN] = "MAILER-DAEMON@";
270 char boundary[BUFSIZ];
271 const char *bounce_to;
273 static char *fqdn_of_host = NULL;
274 const char *md1 = "MAILER-DAEMON", *md2 = "MAILER-DAEMON@";
276 /* don't bounce in reply to undeliverable bounces */
277 if (!msg || !msg->return_path[0] ||
278 strcmp(msg->return_path, "<>") == 0 ||
279 strcasecmp(msg->return_path, md1) == 0 ||
280 strncasecmp(msg->return_path, md2, strlen(md2)) == 0)
283 bounce_to = (run.bouncemail ? msg->return_path : run.postmaster);
285 /* can't just use fetchmailhost here, it might be localhost */
286 if (fqdn_of_host == NULL)
287 fqdn_of_host = host_fqdn(0); /* can't afford to bail out and
289 strlcat(daemon_name, fqdn_of_host, sizeof(daemon_name));
291 /* we need only SMTP for this purpose */
292 /* XXX FIXME: hardcoding localhost is nonsense if smtphost can be
294 if ((sock = SockOpen("localhost", SMTP_PORT, NULL, &ai1)) == -1)
297 if (SMTP_ok(sock, SMTP_MODE, TIMEOUT_STARTSMTP) != SM_OK)
303 if (SMTP_helo(sock, SMTP_MODE, fetchmailhost) != SM_OK
304 || SMTP_from(sock, SMTP_MODE, "<>", (char *)NULL) != SM_OK
305 || SMTP_rcpt(sock, SMTP_MODE, bounce_to) != SM_OK
306 || SMTP_data(sock, SMTP_MODE) != SM_OK)
308 SMTP_quit(sock, SMTP_MODE);
313 /* our first duty is to keep the sacred foo counters turning... */
314 snprintf(boundary, sizeof(boundary), "foo-mani-padme-hum-%ld-%ld-%ld",
315 (long)getpid(), (long)getppid(), (long)time(NULL));
317 if (outlevel >= O_VERBOSE)
318 report(stdout, GT_("SMTP: (bounce-message body)\n"));
320 /* this will usually go to sylog... */
321 report(stderr, GT_("mail from %s bounced to %s\n"),
322 daemon_name, bounce_to);
325 /* bouncemail headers */
326 SockPrintf(sock, "Subject: Mail delivery failed: returning message to sender\r\n");
327 SockPrintf(sock, "From: Mail Delivery System <%s>\r\n", daemon_name);
328 SockPrintf(sock, "To: %s\r\n", bounce_to);
329 SockPrintf(sock, "MIME-Version: 1.0\r\n");
330 SockPrintf(sock, "Content-Type: multipart/report; report-type=delivery-status;\r\n\tboundary=\"%s\"\r\n", boundary);
331 SockPrintf(sock, "\r\n");
333 /* RFC1892 part 1 -- human-readable message */
334 SockPrintf(sock, "--%s\r\n", boundary);
335 SockPrintf(sock,"Content-Type: text/plain\r\n");
336 SockPrintf(sock, "\r\n");
337 SockPrintf(sock, "This message was created automatically by mail delivery software.\r\n");
338 SockPrintf(sock, "\r\n");
339 SockPrintf(sock, "A message that you sent could not be delivered to one or more of its\r\n");
340 SockPrintf(sock, "recipients. This is a permanent error.\r\n");
341 SockPrintf(sock, "\r\n");
342 SockPrintf(sock, "Reason: %s", message);
343 SockPrintf(sock, "\r\n");
344 SockPrintf(sock, "The following address(es) failed:\r\n");
352 for (idp = msg->recipients; idp; idp = idp->next)
354 if (idp->val.status.mark == userclass)
357 SockPrintf(sock, "%s\r\n", rcpt_address (ctl, idp->id, 1));
359 if (nerrors == 1) error = errors[0];
360 else if (nerrors <= nusers)
362 SockPrintf(sock, "Internal error: SMTP error count doesn't match number of recipients.\r\n");
365 else error = errors[nusers++];
367 SockPrintf(sock, " SMTP error: %s\r\n\r\n", error);
371 /* RFC1892 part 2 -- machine-readable responses */
372 SockPrintf(sock, "--%s\r\n", boundary);
373 SockPrintf(sock,"Content-Type: message/delivery-status\r\n");
374 SockPrintf(sock, "\r\n");
375 SockPrintf(sock, "Reporting-MTA: dns; %s\r\n", fqdn_of_host);
378 for (idp = msg->recipients; idp; idp = idp->next)
379 if (idp->val.status.mark == userclass)
382 /* Minimum RFC1894 compliance + Diagnostic-Code field */
383 SockPrintf(sock, "\r\n");
384 SockPrintf(sock, "Final-Recipient: rfc822; %s\r\n",
385 rcpt_address (ctl, idp->id, 1));
386 SockPrintf(sock, "Last-Attempt-Date: %s\r\n", rfc822timestamp());
387 SockPrintf(sock, "Action: failed\r\n");
390 /* one error applies to all users */
392 else if (nerrors <= nusers)
394 SockPrintf(sock, "Internal error: SMTP error count doesn't match number of recipients.\r\n");
398 /* errors correspond 1-1 to selected users */
399 error = errors[nusers++];
401 if (strlen(error) > 9 && isdigit((unsigned char)error[4])
402 && error[5] == '.' && isdigit((unsigned char)error[6])
403 && error[7] == '.' && isdigit((unsigned char)error[8]))
404 /* Enhanced status code available, use it */
405 SockPrintf(sock, "Status: %5.5s\r\n", &(error[4]));
407 /* Enhanced status code not available, fake one */
408 SockPrintf(sock, "Status: %c.0.0\r\n", error[0]);
409 SockPrintf(sock, "Diagnostic-Code: %s\r\n", error);
411 SockPrintf(sock, "\r\n");
414 /* RFC1892 part 3 -- headers of undelivered message */
415 SockPrintf(sock, "--%s\r\n", boundary);
416 SockPrintf(sock, "Content-Type: text/rfc822-headers\r\n");
417 SockPrintf(sock, "\r\n");
420 SockWrite(sock, msg->headers, strlen(msg->headers));
421 SockPrintf(sock, "\r\n");
423 SockPrintf(sock, "--%s--\r\n", boundary);
425 if (SMTP_eom(sock, SMTP_MODE) != SM_OK
426 || SMTP_quit(sock, SMTP_MODE) != SM_OK)
437 static int handle_smtp_report(struct query *ctl, struct msgblk *msg)
438 /* handle SMTP errors based on the content of SMTP_response */
439 /* returns either PS_REFUSED (to delete message from the server),
440 * or PS_TRANSIENT (keeps the message on the server) */
442 int smtperr = atoi(smtp_response);
447 responses[0] = xstrdup(smtp_response);
451 * Don't do this! It can really mess you up if, for example, you're
452 * reporting an error with a single RCPT TO address among several;
453 * RSET discards the message body and it doesn't get sent to the
456 smtp_rset(ctl); /* stay on the safe side */
457 if (outlevel >= O_DEBUG)
458 report(stdout, GT_("Saved error is still %d\n"), smtperr);
459 #endif /* __UNUSED */
462 * Note: send_bouncemail message strings are not made subject
463 * to gettext translation because (a) they're going to be
464 * embedded in a text/plain 7bit part, and (b) they're
465 * going to be associated with listener error-response
466 * messages, which are probably in English (none of the
467 * MTAs I know about are internationalized).
469 for( walk = ctl->antispam; walk; walk = walk->next )
470 if ( walk->val.status.num == smtperr )
476 /* if (str_find(&ctl->antispam, smtperr)) */
480 * SMTP listener explicitly refuses to deliver mail
481 * coming from this address, probably due to an
482 * anti-spam domain exclusion. Respect this. Don't
483 * try to ship the message, and don't prevent it from
484 * being deleted. There's no point in bouncing the
485 * email either since most spammers don't put their
486 * real return email address anywhere in the headers
487 * (unless the user insists with the SET SPAMBOUNCE
492 * 571 = sendmail's "unsolicited email refused"
493 * 550 = exim's new antispam response (temporary)
494 * 501 = exim's old antispam response
495 * 554 = Postfix antispam response.
501 snprintf(rejmsg, sizeof(rejmsg),
502 "spam filter or virus scanner rejected message because:\r\n"
503 "%s\r\n", responses[0]);
505 send_bouncemail(ctl, msg, XMIT_ACCEPT,
506 rejmsg, 1, responses);
513 * Suppress error message only if the response specifically
514 * meant `excluded for policy reasons'. We *should* see
515 * an error when the return code is less specific.
518 report(stderr, GT_("%cMTP error: %s\n"),
524 case 552: /* message exceeds fixed maximum message size */
526 * Permanent no-go condition on the
527 * ESMTP server. Don't try to ship the message,
528 * and allow it to be deleted.
531 send_bouncemail(ctl, msg, XMIT_ACCEPT,
532 "This message was too large (SMTP error 552).\r\n",
537 case 553: /* invalid sending domain */
539 * These latter days 553 usually means a spammer is trying to
540 * cover his tracks. We never bouncemail on these, because
541 * (a) the return address is invalid by definition, and
542 * (b) we wouldn't want spammers to get confirmation that
543 * this address is live, anyway.
545 #ifdef __DONT_FEED_THE_SPAMMERS__
547 send_bouncemail(ctl, msg, XMIT_ACCEPT,
548 "Invalid address in MAIL FROM (SMTP error 553).\r\n",
550 #endif /* __DONT_FEED_THE_SPAMMERS__ */
554 case 530: /* must issue STARTTLS error */
556 * Some SMTP servers insist on encrypted communication
557 * Let's set PS_TRANSIENT, otherwise all messages to be sent
558 * over such server would be blackholed - see RFC 3207.
560 if (outlevel > O_SILENT)
561 report_complete(stdout,
562 GT_("SMTP server requires STARTTLS, keeping message.\n"));
564 return(PS_TRANSIENT);
567 /* bounce non-transient errors back to the sender */
568 if (smtperr >= 500 && smtperr <= 599)
571 send_bouncemail(ctl, msg, XMIT_ACCEPT,
572 "General SMTP/ESMTP error.\r\n",
578 * We're going to end up here on 4xx errors, like:
580 * 451: temporarily unable to identify sender (exim)
581 * 452: temporary out-of-queue-space condition on the ESMTP server.
583 * These are temporary errors. Don't try to ship the message,
584 * and suppress deletion so it can be retried on a future
587 * Bouncemail *might* be appropriate here as a delay
588 * notification (note; if we ever add this, we must make
589 * sure the RFC1894 Action field is "delayed" rather than
590 * "failed"). But it's not really necessary because
591 * these are not actual failures, we're very likely to be
592 * able to recover on the next cycle.
595 return(PS_TRANSIENT);
599 static int handle_smtp_report_without_bounce(struct query *ctl, struct msgblk *msg)
600 /* handle SMTP errors based on the content of SMTP_response */
601 /* atleast one PS_TRANSIENT: do not send the bounce mail, keep the mail;
602 * no PS_TRANSIENT, atleast one PS_SUCCESS: send the bounce mail, delete the mail;
603 * no PS_TRANSIENT, no PS_SUCCESS: do not send the bounce mail, delete the mail */
605 int smtperr = atoi(smtp_response);
609 if (str_find(&ctl->antispam, smtperr))
617 report(stderr, GT_("%cMTP error: %s\n"),
623 case 552: /* message exceeds fixed maximum message size */
628 case 553: /* invalid sending domain */
629 #ifdef __DONT_FEED_THE_SPAMMERS__
632 #endif /* __DONT_FEED_THE_SPAMMERS__ */
636 /* bounce non-transient errors back to the sender */
637 if (smtperr >= 500 && smtperr <= 599)
639 return(PS_TRANSIENT);
643 /* these are shared by open_sink and stuffline */
646 int stuffline(struct query *ctl, char *buf)
647 /* ship a line to the given control block's output sink (SMTP server or MDA) */
652 /* The line may contain NUL characters. Find the last char to use
653 * -- the real line termination is the sequence "\n\0".
655 last = buf + 1; /* last[-1] must be valid! */
656 while ((last += strlen(last)) && (last[-1] != '\n'))
659 /* fix message lines that have only \n termination (for qmail) */
662 if (last - 1 == buf || last[-2] != '\r')
671 phase = FORWARDING_WAIT;
674 * SMTP byte-stuffing. We only do this if the protocol does *not*
675 * use .<CR><LF> as EOM. If it does, the server will already have
676 * decorated any . lines it sends back up.
680 if (ctl->server.base_protocol->delimited) /* server has already byte-stuffed */
683 /* writing to MDA, undo byte-stuffing */
686 /* writing to SMTP, leave the byte-stuffing in place */;
689 else /* if (!protocol->delimited) -- not byte-stuffed already */
694 n = SockWrite(ctl->smtp_socket, buf, 1);
696 n = fwrite(buf, 1, 1, sinkfp);
697 if (ferror(sinkfp)) n = -1;
705 /* we may need to strip carriage returns */
710 for (sp = tp = buf; sp < last; sp++)
718 if (ctl->mda || ctl->bsmtp) {
719 n = fwrite(buf, 1, last - buf, sinkfp);
720 if (ferror(sinkfp)) n = -1;
721 } else if (ctl->smtp_socket != -1)
722 n = SockWrite(ctl->smtp_socket, buf, last - buf);
729 static int open_bsmtp_sink(struct query *ctl, struct msgblk *msg,
730 int *good_addresses, int *bad_addresses)
731 /* open a BSMTP stream */
736 if (strcmp(ctl->bsmtp, "-") == 0)
739 sinkfp = fopen(ctl->bsmtp, "a");
741 if (!sinkfp || ferror(sinkfp)) {
742 report(stderr, GT_("BSMTP file open failed: %s\n"),
747 /* see the ap computation under the SMTP branch */
748 need_anglebrs = (msg->return_path[0] != '<');
751 need_anglebrs ? "<" : "",
752 (msg->return_path[0]) ? msg->return_path : user,
753 need_anglebrs ? ">" : "");
755 if (ctl->pass8bits || (ctl->mimemsg & MSG_IS_8BIT))
756 fputs(" BODY=8BITMIME", sinkfp);
757 else if (ctl->mimemsg & MSG_IS_7BIT)
758 fputs(" BODY=7BIT", sinkfp);
760 /* exim's BSMTP processor does not handle SIZE */
761 /* fprintf(sinkfp, " SIZE=%d", msg->reallen); */
763 fprintf(sinkfp, "\r\n");
766 * RFC 1123 requires that the domain name part of the
767 * RCPT TO address be "canonicalized", that is a FQDN
768 * or MX but not a CNAME. Some listeners (like exim)
769 * enforce this. Now that we have the actual hostname,
770 * compute what we should canonicalize with.
772 xfree(ctl->destaddr);
773 ctl->destaddr = xstrdup(ctl->smtpaddress ? ctl->smtpaddress : "localhost");
776 for (idp = msg->recipients; idp; idp = idp->next)
777 if (idp->val.status.mark == XMIT_ACCEPT)
779 fprintf(sinkfp, "RCPT TO:<%s>\r\n",
780 rcpt_address (ctl, idp->id, 1));
784 fputs("DATA\r\n", sinkfp);
786 if (fflush(sinkfp) || ferror(sinkfp))
788 report(stderr, GT_("BSMTP preamble write failed: %s.\n"), strerror(errno));
795 /* this is experimental and will be removed if double bounces are reported */
796 #define EXPLICIT_BOUNCE_ON_BAD_ADDRESS
799 static const char *is_quad(const char *q)
800 /* Check if the string passed in points to what could be one quad of a
801 * dotted-quad IP address. Requirements are that the string is not a
802 * NULL pointer, begins with a period (which is skipped) or a digit
803 * and ends with a period or a NULL. If these requirements are met, a
804 * pointer to the last character (the period or the NULL character) is
805 * returned; otherwise NULL.
814 for(r=q;isdigit((unsigned char)*r);r++)
816 if ( ((*r) && (*r != '.')) || ((r-q) < 1) || ((r-q)>3) )
818 /* Make sure quad is < 255 */
827 else if (*(q+1) == '5')
837 static int is_dottedquad(const char *hostname)
838 /* Returns a true value if the passed in string looks like an IP
839 * address in dotted-quad form, and a false value otherwise.
843 return ((hostname=is_quad(is_quad(is_quad(is_quad(hostname))))) != NULL) &&
847 static int open_smtp_sink(struct query *ctl, struct msgblk *msg,
848 int *good_addresses, int *bad_addresses /* this must be signed, to prevent endless loop in from_addresses */)
849 /* open an SMTP stream */
853 char options[MSGBUFSIZE];
854 char addr[HOSTLEN+USERNAMELEN+1];
855 #ifdef EXPLICIT_BOUNCE_ON_BAD_ADDRESS
856 char **from_responses;
857 #endif /* EXPLICIT_BOUNCE_ON_BAD_ADDRESS */
859 int force_transient_error = 0;
863 * Compute ESMTP options.
866 if (ctl->server.esmtp_options & ESMTP_8BITMIME) {
867 if (ctl->pass8bits || (ctl->mimemsg & MSG_IS_8BIT))
868 strcpy(options, " BODY=8BITMIME");
869 else if (ctl->mimemsg & MSG_IS_7BIT)
870 strcpy(options, " BODY=7BIT");
873 if ((ctl->server.esmtp_options & ESMTP_SIZE) && msg->reallen > 0)
874 sprintf(options + strlen(options), " SIZE=%d", msg->reallen);
877 * Try to get the SMTP listener to take the Return-Path
878 * address as MAIL FROM. If it won't, fall back on the
879 * remotename and mailserver host. This won't affect replies,
880 * which use the header From address anyway; the MAIL FROM
881 * address is a place for the SMTP listener to send
882 * bouncemail. The point is to guarantee a FQDN in the MAIL
883 * FROM line -- some SMTP listeners, like smail, become
886 * RFC 1123 requires that the domain name part of the
887 * MAIL FROM address be "canonicalized", that is a
888 * FQDN or MX but not a CNAME. We'll assume the Return-Path
889 * header is already in this form here (it certainly
890 * is if rewrite is on). RFC 1123 is silent on whether
891 * a nonexistent hostname part is considered canonical.
893 * This is a potential problem if the MTAs further upstream
894 * didn't pass canonicalized From/Return-Path lines, *and* the
895 * local SMTP listener insists on them.
897 * Handle the case where an upstream MTA is setting a return
898 * path equal to "@". Ghod knows why anyone does this, but
899 * it's been reported to happen in mail from Amazon.com and
902 * Also, if the hostname is a dotted quad, wrap it in square brackets.
903 * Apparently this is required by RFC2821, section 4.1.3.
905 if (!msg->return_path[0] || (msg->return_path[0] == '@'))
907 if (strchr(ctl->remotename,'@') || strchr(ctl->remotename,'!'))
909 snprintf(addr, sizeof(addr), "%s", ctl->remotename);
911 else if (is_dottedquad(ctl->server.truename))
913 snprintf(addr, sizeof(addr), "%s@[%s]", ctl->remotename,
914 ctl->server.truename);
918 snprintf(addr, sizeof(addr),
919 "%s@%s", ctl->remotename, ctl->server.truename);
923 else if (strchr(msg->return_path,'@') || strchr(msg->return_path,'!'))
924 ap = msg->return_path;
925 /* in case Return-Path was "<>" we want to preserve that */
926 else if (strcmp(msg->return_path,"<>") == 0)
927 ap = msg->return_path;
928 else /* in case Return-Path existed but was local */
930 if (is_dottedquad(ctl->server.truename))
932 snprintf(addr, sizeof(addr), "%s@[%s]", msg->return_path,
933 ctl->server.truename);
937 snprintf(addr, sizeof(addr), "%s@%s",
938 msg->return_path, ctl->server.truename);
943 if ((smtp_err = SMTP_from(ctl->smtp_socket, ctl->smtphostmode,
944 ap, options)) == SM_UNRECOVERABLE)
947 return(PS_TRANSIENT);
949 if (smtp_err != SM_OK)
951 int err = handle_smtp_report(ctl, msg); /* map to PS_TRANSIENT or PS_REFUSED */
953 smtp_rset(ctl); /* stay on the safe side */
958 * Now list the recipient addressees
961 for (idp = msg->recipients; idp; idp = idp->next)
963 #ifdef EXPLICIT_BOUNCE_ON_BAD_ADDRESS
964 from_responses = (char **)xmalloc(sizeof(char *) * total_addresses);
965 #endif /* EXPLICIT_BOUNCE_ON_BAD_ADDRESS */
966 for (idp = msg->recipients; idp; idp = idp->next)
967 if (idp->val.status.mark == XMIT_ACCEPT)
970 address = rcpt_address (ctl, idp->id, 1);
971 if ((smtp_err = SMTP_rcpt(ctl->smtp_socket, ctl->smtphostmode,
972 address)) == SM_UNRECOVERABLE)
976 #ifdef EXPLICIT_BOUNCE_ON_BAD_ADDRESS
977 while (*bad_addresses)
978 free(from_responses[--*bad_addresses]);
979 free(from_responses);
980 #endif /* EXPLICIT_BOUNCE_ON_BAD_ADDRESS */
981 return(PS_TRANSIENT);
983 if (smtp_err == SM_OK)
987 switch (handle_smtp_report_without_bounce(ctl, msg))
990 force_transient_error = 1;
994 #ifdef EXPLICIT_BOUNCE_ON_BAD_ADDRESS
995 from_responses[*bad_addresses] = xstrdup(smtp_response);
996 #endif /* EXPLICIT_BOUNCE_ON_BAD_ADDRESS */
999 idp->val.status.mark = XMIT_RCPTBAD;
1000 if (outlevel >= O_VERBOSE)
1002 GT_("%cMTP listener doesn't like recipient address `%s'\n"),
1003 ctl->smtphostmode, address);
1007 if (outlevel >= O_VERBOSE)
1009 GT_("%cMTP listener doesn't really like recipient address `%s'\n"),
1010 ctl->smtphostmode, address);
1016 if (force_transient_error) {
1017 /* do not risk dataloss due to overengineered multidrop
1018 * crap. If one of the recipients returned PS_TRANSIENT,
1019 * we return exactly that.
1021 smtp_rset(ctl); /* required by RFC1870 */
1024 #ifdef EXPLICIT_BOUNCE_ON_BAD_ADDRESS
1026 * This should not be necessary, because the SMTP listener itself
1027 * should generate a bounce for the bad address.
1029 * XXX FIXME 2006-01-19: is this comment true? I don't think
1030 * it is, because the SMTP listener isn't required to accept bogus
1031 * messages. There appears to be general SMTP<->MDA and
1032 * responsibility confusion.
1035 send_bouncemail(ctl, msg, XMIT_RCPTBAD,
1036 "Some addresses were rejected by the MDA fetchmail forwards to.\r\n",
1037 *bad_addresses, from_responses);
1038 while (*bad_addresses)
1039 free(from_responses[--*bad_addresses]);
1040 free(from_responses);
1041 #endif /* EXPLICIT_BOUNCE_ON_BAD_ADDRESS */
1044 * It's tempting to do local notification only if bouncemail was
1045 * insufficient -- that is, to add && total_addresses > *bad_addresses
1046 * to the test here. The problem with this theory is that it would
1047 * make initial diagnosis of a broken multidrop configuration very
1048 * hard -- most single-recipient messages would just invisibly bounce.
1050 if (!(*good_addresses))
1052 if (!run.postmaster[0])
1054 if (outlevel >= O_VERBOSE)
1055 report(stderr, GT_("no address matches; no postmaster set.\n"));
1056 smtp_rset(ctl); /* required by RFC1870 */
1059 if ((smtp_err = SMTP_rcpt(ctl->smtp_socket, ctl->smtphostmode,
1060 rcpt_address (ctl, run.postmaster, 0))) == SM_UNRECOVERABLE)
1063 return(PS_TRANSIENT);
1065 if (smtp_err != SM_OK)
1067 report(stderr, GT_("can't even send to %s!\n"), run.postmaster);
1068 smtp_rset(ctl); /* required by RFC1870 */
1072 if (outlevel >= O_VERBOSE)
1073 report(stderr, GT_("no address matches; forwarding to %s.\n"), run.postmaster);
1077 * Tell the listener we're ready to send data.
1078 * Some listeners (like zmailer) may return antispam errors here.
1080 if ((smtp_err = SMTP_data(ctl->smtp_socket, ctl->smtphostmode))
1081 == SM_UNRECOVERABLE)
1084 return(PS_TRANSIENT);
1086 if (smtp_err != SM_OK)
1088 int err = handle_smtp_report(ctl, msg);
1089 smtp_rset(ctl); /* stay on the safe side */
1094 * We need to stash this away in order to know how many
1095 * response lines to expect after the LMTP end-of-message.
1097 lmtp_responses = *good_addresses;
1102 static int open_mda_sink(struct query *ctl, struct msgblk *msg,
1103 int *good_addresses, int *bad_addresses)
1104 /* open a stream to a local MDA */
1108 #endif /* HAVE_SETEUID */
1110 int length = 0, fromlen = 0, nameslen = 0;
1111 char *names = NULL, *before, *after, *from = NULL;
1113 (void)bad_addresses;
1114 xfree(ctl->destaddr);
1115 ctl->destaddr = xstrdup("localhost");
1117 for (idp = msg->recipients; idp; idp = idp->next)
1118 if (idp->val.status.mark == XMIT_ACCEPT)
1119 (*good_addresses)++;
1121 length = strlen(ctl->mda);
1122 before = xstrdup(ctl->mda);
1124 /* get user addresses for %T (or %s for backward compatibility) */
1125 if (strstr(before, "%s") || strstr(before, "%T"))
1128 * We go through this in order to be able to handle very
1129 * long lists of users and (re)implement %s.
1132 for (idp = msg->recipients; idp; idp = idp->next)
1133 if ((idp->val.status.mark == XMIT_ACCEPT))
1134 nameslen += (strlen(idp->id) + 1); /* string + ' ' */
1135 if ((*good_addresses == 0))
1136 nameslen = strlen(run.postmaster);
1138 names = (char *)xmalloc(nameslen + 1); /* account for '\0' */
1139 if (*good_addresses == 0)
1140 strcpy(names, run.postmaster);
1144 for (idp = msg->recipients; idp; idp = idp->next)
1145 if (idp->val.status.mark == XMIT_ACCEPT)
1147 strcat(names, idp->id);
1150 names[--nameslen] = '\0'; /* chop trailing space */
1156 /* get From address for %F */
1157 if (strstr(before, "%F"))
1159 from = xstrdup(msg->return_path);
1163 fromlen = strlen(from);
1166 /* do we have to build an mda string? */
1171 /* find length of resulting mda string */
1173 while ((sp = strstr(sp, "%s"))) {
1174 length += nameslen; /* subtract %s and add '' */
1178 while ((sp = strstr(sp, "%T"))) {
1179 length += nameslen; /* subtract %T and add '' */
1183 while ((sp = strstr(sp, "%F"))) {
1184 length += fromlen; /* subtract %F and add '' */
1188 after = (char *)xmalloc(length + 1);
1190 /* copy mda source string to after, while expanding %[sTF] */
1191 for (dp = after, sp = before; (*dp = *sp); dp++, sp++) {
1192 if (sp[0] != '%') continue;
1194 /* need to expand? BTW, no here overflow, because in
1195 ** the worst case (end of string) sp[1] == '\0' */
1196 if (sp[1] == 's' || sp[1] == 'T') {
1201 sp++; /* position sp over [sT] */
1202 dp--; /* adjust dp */
1203 } else if (sp[1] == 'F') {
1208 sp++; /* position sp over F */
1209 dp--; /* adjust dp */
1228 if (outlevel >= O_DEBUG)
1229 report(stdout, GT_("about to deliver with: %s\n"), before);
1233 * Arrange to run with user's permissions if we're root.
1234 * This will initialize the ownership of any files the
1235 * MDA creates properly. (The seteuid call is available
1236 * under all BSDs and Linux)
1238 orig_uid = getuid();
1239 if (seteuid(ctl->uid)) {
1240 report(stderr, GT_("Cannot switch effective user id to %ld: %s\n"), (long)ctl->uid, strerror(errno));
1243 #endif /* HAVE_SETEUID */
1245 sinkfp = popen(before, "w");
1250 /* this will fail quietly if we didn't start as root */
1251 if (seteuid(orig_uid)) {
1252 report(stderr, GT_("Cannot switch effective user id back to original %ld: %s\n"), (long)orig_uid, strerror(errno));
1255 #endif /* HAVE_SETEUID */
1259 report(stderr, GT_("MDA open failed\n"));
1264 * We need to disable the normal SIGCHLD handling here because
1265 * sigchld_handler() would reap away the error status, returning
1266 * error status instead of 0 for successful completion.
1268 set_signal_handler(SIGCHLD, SIG_DFL);
1273 int open_sink(struct query *ctl, struct msgblk *msg,
1274 int *good_addresses, int *bad_addresses)
1275 /* set up sinkfp to be an input sink we can ship a message to */
1277 *bad_addresses = *good_addresses = 0;
1279 if (want_progress() && outlevel >= O_VERBOSE && !ctl->mda && !ctl->bsmtp) puts("");
1281 if (ctl->bsmtp) /* dump to a BSMTP batch file */
1282 return(open_bsmtp_sink(ctl, msg, good_addresses, bad_addresses));
1284 * Try to forward to an SMTP or LMTP listener. If the attempt to
1285 * open a socket fails, fall through to attempt delivery via
1288 else if (!ctl->mda && smtp_setup(ctl) != -1)
1289 return(open_smtp_sink(ctl, msg, good_addresses, bad_addresses));
1292 * Awkward case. User didn't specify an MDA. Our attempt to get a
1293 * listener socket failed. Try to cope anyway -- initial configuration
1294 * may have found procmail.
1298 report(stderr, GT_("%cMTP connect to %s failed\n"),
1300 ctl->smtphost ? ctl->smtphost : "localhost");
1302 #ifndef FALLBACK_MDA
1303 /* No fallback MDA declared. Bail out. */
1307 * If user had things set up to forward offsite, no way
1308 * we want to deliver locally!
1310 if (ctl->smtphost && strcmp(ctl->smtphost, "localhost"))
1314 * User was delivering locally. We have a fallback MDA.
1315 * Latch it in place, logging the error, and fall through.
1316 * Set stripcr as we would if MDA had been the initial transport
1318 ctl->mda = FALLBACK_MDA;
1320 ctl->stripcr = TRUE;
1322 report(stderr, GT_("can't raise the listener; falling back to %s"),
1327 if (ctl->mda) /* must deliver through an MDA */
1328 return(open_mda_sink(ctl, msg, good_addresses, bad_addresses));
1333 void release_sink(struct query *ctl)
1334 /* release the per-message output sink, whether it's a pipe or SMTP socket */
1336 if (ctl->bsmtp && sinkfp)
1338 if (strcmp(ctl->bsmtp, "-"))
1341 sinkfp = (FILE *)NULL;
1349 sinkfp = (FILE *)NULL;
1351 deal_with_sigchld(); /* Restore SIGCHLD handling to reap zombies */
1355 int close_sink(struct query *ctl, struct msgblk *msg, flag forward)
1356 /* perform end-of-message actions on the current output sink */
1360 if (want_progress() && outlevel >= O_VERBOSE && !ctl->mda && !ctl->bsmtp) puts("");
1362 if (ctl->bsmtp && sinkfp) {
1365 /* implicit disk-full check here... */
1366 fputs(".\r\n", sinkfp);
1367 error = ferror(sinkfp);
1369 if (strcmp(ctl->bsmtp, "-"))
1371 if (fclose(sinkfp) == EOF) {
1375 sinkfp = (FILE *)NULL;
1380 GT_("Message termination or close of BSMTP file failed: %s\n"), strerror(oerrno));
1383 } else if (ctl->mda) {
1384 int rc = 0, e = 0, e2 = 0, err = 0;
1386 /* close the delivery pipe, we'll reopen before next message */
1390 err = 1, e2 = errno;
1391 if ((fflush(sinkfp)))
1392 err = 1, e2 = errno;
1395 rc = pclose(sinkfp);
1397 sinkfp = (FILE *)NULL;
1402 deal_with_sigchld(); /* Restore SIGCHLD handling to reap zombies */
1407 report(stderr, GT_("Error writing to MDA: %s\n"), strerror(e2));
1408 } else if (WIFSIGNALED(rc)) {
1410 GT_("MDA died of signal %d\n"), WTERMSIG(rc));
1411 } else if (WIFEXITED(rc)) {
1413 GT_("MDA returned nonzero status %d\n"), WEXITSTATUS(rc));
1416 GT_("Strange: MDA pclose returned %d and errno %d/%s, cannot handle at %s:%d\n"),
1417 rc, e, strerror(e), __FILE__, __LINE__);
1425 /* write message terminator */
1426 if ((smtp_err = SMTP_eom(ctl->smtp_socket, ctl->smtphostmode))
1427 == SM_UNRECOVERABLE)
1432 if (smtp_err != SM_OK)
1434 if (handle_smtp_report(ctl, msg) != PS_REFUSED)
1436 smtp_rset(ctl); /* stay on the safe side */
1441 report(stderr, GT_("SMTP listener refused delivery\n"));
1442 smtp_rset(ctl); /* stay on the safe side */
1448 * If this is an SMTP connection, SMTP_eom() ate the response.
1449 * But could be this is an LMTP connection, in which case we have to
1450 * interpret either (a) a single 503 response meaning there
1451 * were no successful RCPT TOs, or (b) a variable number of
1452 * responses, one for each successful RCPT TO. We need to send
1453 * bouncemail on each failed response and then return TRUE anyway,
1454 * otherwise the message will get left in the queue and resent
1455 * to people who got it the first time.
1457 if (ctl->smtphostmode == LMTP_MODE)
1459 if (lmtp_responses == 0)
1461 SMTP_ok(ctl->smtp_socket, ctl->smtphostmode, TIMEOUT_EOM);
1464 * According to RFC2033, 503 is the only legal response
1465 * if no RCPT TO commands succeeded. No error recovery
1466 * is really possible here, as we have no idea what
1467 * insane thing the listener might be doing if it doesn't
1470 if (atoi(smtp_response) == 503)
1471 report(stderr, GT_("LMTP delivery error on EOM\n"));
1474 GT_("Unexpected non-503 response to LMTP EOM: %s\n"),
1478 * It's not completely clear what to do here. We choose to
1479 * interpret delivery failure here as a transient error,
1480 * the same way SMTP delivery failure is handled. If we're
1481 * wrong, an undead message will get stuck in the queue.
1487 int i, errors, rc = FALSE;
1490 /* eat the RFC2033-required responses, saving errors */
1491 responses = (char **)xmalloc(sizeof(char *) * lmtp_responses);
1492 for (errors = i = 0; i < lmtp_responses; i++)
1494 if ((smtp_err = SMTP_ok(ctl->smtp_socket, ctl->smtphostmode, TIMEOUT_EOM))
1495 == SM_UNRECOVERABLE)
1500 if (smtp_err != SM_OK)
1502 responses[errors] = xstrdup(smtp_response);
1508 rc = TRUE; /* all deliveries succeeded */
1511 * One or more deliveries failed.
1512 * If we can bounce a failures list back to the
1513 * sender, and the postmaster does not want to
1514 * deal with the bounces return TRUE, deleting the
1515 * message from the server so it won't be
1516 * re-forwarded on subsequent poll cycles.
1518 rc = send_bouncemail(ctl, msg, XMIT_ACCEPT,
1519 "LMTP partial delivery failure.\r\n",
1523 for (i = 0; i < errors; i++)
1534 int open_warning_by_mail(struct query *ctl)
1535 /* set up output sink for a mailed warning to calling user */
1540 * Dispatching warning email is a little complicated. The problem is
1541 * that we have to deal with three distinct cases:
1543 * 1. Single-drop running from user account. Warning mail should
1544 * go to the local name for which we're collecting (coincides
1545 * with calling user).
1547 * 2. Single-drop running from root or other privileged ID, with rc
1548 * file generated on the fly (Ken Estes's weird setup...) Mail
1549 * should go to the local name for which we're collecting (does not
1550 * coincide with calling user).
1552 * 3. Multidrop. Mail must go to postmaster. We leave the recipients
1553 * member null so this message will fall through to run.postmaster.
1555 * The zero in the reallen element means we won't pass a SIZE
1556 * option to ESMTP; the message length would be more trouble than
1557 * it's worth to compute.
1559 struct msgblk reply = {NULL, NULL, "FETCHMAIL-DAEMON@", 0, 0};
1562 strlcat(reply.return_path, ctl->smtpaddress ? ctl->smtpaddress :
1563 fetchmailhost, sizeof(reply.return_path));
1565 if (!MULTIDROP(ctl)) /* send to calling user */
1567 save_str(&reply.recipients, ctl->localnames->id, XMIT_ACCEPT);
1568 status = open_sink(ctl, &reply, &good, &bad);
1569 free_str_list(&reply.recipients);
1571 else /* send to postmaster */
1572 status = open_sink(ctl, &reply, &good, &bad);
1574 stuff_warning(NULL, ctl, "From: FETCHMAIL-DAEMON@%s",
1575 ctl->smtpaddress ? ctl->smtpaddress : fetchmailhost);
1576 stuff_warning(NULL, ctl, "Date: %s", rfc822timestamp());
1577 stuff_warning(NULL, ctl, "MIME-Version: 1.0");
1578 stuff_warning(NULL, ctl, "Content-Transfer-Encoding: 8bit");
1579 stuff_warning(NULL, ctl, "Content-Type: text/plain; charset=\"%s\"", iana_charset);
1584 /* format and ship a warning message line by mail */
1585 /* if rfc2047charset is non-NULL, encode the line (that is assumed to be
1586 * a header line) as per RFC-2047 using rfc2047charset as the character
1588 #if defined(HAVE_STDARG_H)
1589 void stuff_warning(const char *rfc2047charset, struct query *ctl, const char *fmt, ... )
1591 void stuff_warning(rfc2047charset, ctl, fmt, va_alist)
1592 const char *charset;
1594 const char *fmt; /* printf-style format */
1598 /* make huge -- i18n can bulk up error messages a lot */
1599 char buf[2*MSGBUFSIZE+4];
1603 * stuffline() requires its input to be writeable (for CR stripping),
1604 * so we needed to copy the message to a writeable buffer anyway in
1605 * case it was a string constant. We make a virtue of that necessity
1606 * here by supporting stdargs/varargs.
1608 #if defined(HAVE_STDARG_H)
1613 vsnprintf(buf, sizeof(buf) - 2, fmt, ap);
1616 snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "\r\n");
1618 /* guard against very long lines */
1619 buf[MSGBUFSIZE+1] = '\r';
1620 buf[MSGBUFSIZE+2] = '\n';
1621 buf[MSGBUFSIZE+3] = '\0';
1623 stuffline(ctl, rfc2047charset != NULL ? rfc2047e(buf, rfc2047charset) : buf);
1626 void close_warning_by_mail(struct query *ctl, struct msgblk *msg)
1627 /* sign and send mailed warnings */
1629 stuff_warning(NULL, ctl, GT_("-- \nThe Fetchmail Daemon"));
1630 close_sink(ctl, msg, TRUE);
1633 /* sink.c ends here */