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.
21 #endif /* HAVE_MEMORY_H */
22 #if defined(STDC_HEADERS)
25 #if defined(HAVE_UNISTD_H)
28 #if defined(HAVE_STDARG_H)
34 #include "fetchmail.h"
39 /* BSD portability hack...I know, this is an ugly place to put it */
40 #if !defined(SIGCHLD) && defined(SIGCLD)
41 #define SIGCHLD SIGCLD
44 /* makes the open_sink()/close_sink() pair non-reentrant */
45 static lmtp_responses;
47 static int smtp_open(struct query *ctl)
48 /* try to open a socket to the appropriate SMTP server for this query */
50 /* maybe it's time to close the socket in order to force delivery */
51 if (NUM_NONZERO(ctl->batchlimit) && (ctl->smtp_socket != -1) && batchcount++ == ctl->batchlimit)
53 close(ctl->smtp_socket);
54 ctl->smtp_socket = -1;
58 /* if no socket to any SMTP host is already set up, try to open one */
59 if (ctl->smtp_socket == -1)
62 * RFC 1123 requires that the domain name in HELO address is a
63 * "valid principal domain name" for the client host. If we're
64 * running in invisible mode, violate this with malice
65 * aforethought in order to make the Received headers and
68 * In fact this code relies on the RFC1123 requirement that the
69 * SMTP listener must accept messages even if verification of the
70 * HELO name fails (RFC1123 section 5.2.5, paragraph 2).
72 * How we compute the true mailhost name to pass to the
73 * listener doesn't affect behavior on RFC1123- violating
74 * listeners that check for name match; we're going to lose
75 * on those anyway because we can never give them a name
76 * that matches the local machine fetchmail is running on.
77 * What it will affect is the listener's logging.
80 const char *id_me = run.invisible ? ctl->server.truename : fetchmailhost;
86 * Run down the SMTP hunt list looking for a server that's up.
87 * Use both explicit hunt entries (value TRUE) and implicit
88 * (default) ones (value FALSE).
91 phase = LISTENER_WAIT;
93 set_timeout(ctl->server.timeout);
94 for (idp = ctl->smtphunt; idp; idp = idp->next)
96 char *cp, *parsed_host;
98 char *portnum = SMTP_PORT;
100 int portnum = SMTP_PORT;
103 xalloca(parsed_host, char *, strlen(idp->id) + 1);
105 ctl->smtphost = idp->id; /* remember last host tried. */
107 strcpy(parsed_host, idp->id);
108 if ((cp = strrchr(parsed_host, '/')))
118 if ((ctl->smtp_socket = SockOpen(parsed_host,portnum,NULL,
119 ctl->server.plugout)) == -1)
122 /* are we doing SMTP or LMTP? */
123 SMTP_setmode(ctl->listener);
125 /* first, probe for ESMTP */
126 if (SMTP_ok(ctl->smtp_socket) == SM_OK &&
127 SMTP_ehlo(ctl->smtp_socket, id_me,
128 &ctl->server.esmtp_options) == SM_OK)
132 * RFC 1869 warns that some listeners hang up on a failed EHLO,
133 * so it's safest not to assume the socket will still be good.
135 SockClose(ctl->smtp_socket);
136 ctl->smtp_socket = -1;
138 /* if opening for ESMTP failed, try SMTP */
139 if ((ctl->smtp_socket = SockOpen(parsed_host,portnum,NULL,
140 ctl->server.plugout)) == -1)
143 if (SMTP_ok(ctl->smtp_socket) == SM_OK &&
144 SMTP_helo(ctl->smtp_socket, id_me) == SM_OK)
147 SockClose(ctl->smtp_socket);
148 ctl->smtp_socket = -1;
155 * RFC 1123 requires that the domain name part of the
156 * RCPT TO address be "canonicalized", that is a FQDN
157 * or MX but not a CNAME. Some listeners (like exim)
158 * enforce this. Now that we have the actual hostname,
159 * compute what we should canonicalize with.
161 ctl->destaddr = ctl->smtpaddress ? ctl->smtpaddress : ( ctl->smtphost ? ctl->smtphost : "localhost");
163 if (outlevel >= O_DEBUG && ctl->smtp_socket != -1)
164 error(0, 0, _("forwarding to %s"), ctl->smtphost);
166 return(ctl->smtp_socket);
169 /* these are shared by open_sink and stuffline */
171 static RETSIGTYPE (*sigchld)(int);
173 int stuffline(struct query *ctl, char *buf)
174 /* ship a line to the given control block's output sink (SMTP server or MDA) */
179 /* The line may contain NUL characters. Find the last char to use
180 * -- the real line termination is the sequence "\n\0".
183 while ((last += strlen(last)) && (last[-1] != '\n'))
186 /* fix message lines that have only \n termination (for qmail) */
189 if (last - 1 == buf || last[-2] != '\r')
198 phase = FORWARDING_WAIT;
201 * SMTP byte-stuffing. We only do this if the protocol does *not*
202 * use .<CR><LF> as EOM. If it does, the server will already have
203 * decorated any . lines it sends back up.
206 if (ctl->server.base_protocol->delimited) /* server has already byte-stuffed */
211 /* writing to SMTP, leave the byte-stuffing in place */;
213 else /* if (!protocol->delimited) -- not byte-stuffed already */
216 SockWrite(ctl->smtp_socket, buf, 1); /* byte-stuff it */
218 /* leave it alone */;
221 /* we may need to strip carriage returns */
226 for (sp = tp = buf; sp < last; sp++)
234 if (ctl->mda || ctl->bsmtp)
235 n = fwrite(buf, 1, last - buf, sinkfp);
236 else if (ctl->smtp_socket != -1)
237 n = SockWrite(ctl->smtp_socket, buf, last - buf);
244 static void sanitize(char *s)
245 /* replace unsafe shellchars by an _ */
247 const static char *ok_chars = " 1234567890!@%-_=+:,./abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
250 for (cp = s; *(cp += strspn(cp, ok_chars)); /* NO INCREMENT */)
254 static int send_bouncemail(struct msgblk *msg, int userclass,
255 char *message, int nerrors, char *errors[])
256 /* bounce back an error report a la RFC 1892 */
258 char daemon_name[18 + HOSTLEN] = "FETCHMAIL-DAEMON@";
259 char boundary[BUFSIZ];
262 /* don't bounce in reply to undeliverable bounces */
263 if (!msg->return_path[0] || strcmp(msg->return_path, "<>") == 0)
266 SMTP_setmode(SMTP_MODE);
268 strcat(daemon_name, fetchmailhost);
270 /* we need only SMTP for this purpose */
271 if ((sock = SockOpen("localhost", SMTP_PORT, NULL, NULL)) == -1
272 || SMTP_ok(sock) != SM_OK
273 || SMTP_helo(sock, "localhost") != SM_OK
274 || SMTP_from(sock, daemon_name, (char *)NULL) != SM_OK
275 || SMTP_rcpt(sock, msg->return_path) != SM_OK
276 || SMTP_data(sock) != SM_OK)
280 "om-mani-padme-hum-%d-%d-%ld",
281 getpid(), getppid(), time((time_t *)NULL));
283 if (outlevel >= O_VERBOSE)
284 error(0, 0, "SMTP: (bounce-message body)");
286 /* bouncemail headers */
287 SockPrintf(sock, "Return-Path: <>");
288 SockPrintf(sock, "From: FETCHMAIL-DAEMON@%s\r\n", fetchmailhost);
289 SockPrintf(sock, "To: %s\n", msg->return_path);
290 SockPrintf(sock, "MIME-Version: 1.0\r\n");
291 SockPrintf(sock, "Content-Type: multipart/report; report-type=delivery-status; boundary=\"%s\"\r\n", boundary);
292 SockPrintf(sock, "\r\n");
293 SockPrintf(sock, "Content-Transfer-Encoding: 7bit\r\n");
294 SockPrintf(sock, "\r\n");
296 /* RFC1892 part 1 -- human-readable message */
297 SockPrintf(sock, "--%s\r\n", boundary);
298 SockPrintf(sock,"Content-Type: text/plain\r\n");
299 SockPrintf(sock, "\r\n");
300 SockWrite(sock, message, strlen(message));
301 SockPrintf(sock, "\r\n");
302 SockPrintf(sock, "\r\n");
309 /* RFC1892 part 2 -- machine-readable responses */
310 SockPrintf(sock, "--%s\r\n", boundary);
311 SockPrintf(sock,"Content-Type: message/delivery-status\r\n");
312 SockPrintf(sock, "\r\n");
313 SockPrintf(sock, "Reporting-MTA: dns; %s\r\n", fetchmailhost);
316 for (idp = msg->recipients; idp; idp = idp->next)
317 if (idp->val.status.mark == userclass)
320 /* Minimum RFC1894 compliance + Diagnostic-Code field */
321 SockPrintf(sock, "\r\n");
322 SockPrintf(sock, "Final-Recipient: rfc822; %s\r\n", idp->id);
323 SockPrintf(sock, "Action: failed\r\n");
326 /* one error applies to all users */
328 else if (nerrors > nusers)
330 SockPrintf(sock, "Internal error: SMTP error count doesn't match number of recipients.\r\n");
334 /* errors correspond 1-1 to selected users */
335 error = errors[nusers++];
337 if (strlen(error) > 9 && isdigit(error[4])
338 && error[5] == '.' && isdigit(error[6])
339 && error[7] == '.' && isdigit(error[8]))
340 /* Enhanced status code available, use it */
341 SockPrintf(sock, "Status: %5.5s\r\n", &(error[4]));
343 /* Enhanced status code not available, fake one */
344 SockPrintf(sock, "Status: %c.0.0\r\n", error[0]);
345 SockPrintf(sock, "Diagnostic-Code: %s\r\n", error);
347 SockPrintf(sock, "\r\n");
350 /* RFC1892 part 3 -- headers of undelivered message */
351 SockPrintf(sock, "--%s\r\n", boundary);
352 SockPrintf(sock, "Content-Type: text/rfc822-headers\r\n");
353 SockPrintf(sock, "\r\n");
354 SockWrite(sock, msg->headers, strlen(msg->headers));
355 SockPrintf(sock, "\r\n");
356 SockPrintf(sock, "--%s--\r\n", boundary);
358 if (SMTP_eom(sock) != SM_OK || SMTP_quit(sock))
366 static int handle_smtp_error(struct query *ctl, struct msgblk *msg)
367 /* handle SMTP errors based on the content of SMTP_response */
369 int smtperr = atoi(smtp_response);
372 responses[0] = smtp_response;
374 /* required by RFC1870; sets us up to be able to send bouncemail */
375 SMTP_rset(ctl->smtp_socket);
378 * Note: send_bouncemail message strings are not made subject
379 * to gettext translation because (a) they're going to be
380 * embedded in a text/plain 7bit part, and (b) they're
381 * going to be associated with listener error-response
382 * messages, which are probably in English (none of the
383 * MTAs I know about are internationalized).
385 if (str_find(&ctl->antispam, smtperr))
388 * SMTP listener explicitly refuses to deliver mail
389 * coming from this address, probably due to an
390 * anti-spam domain exclusion. Respect this. Don't
391 * try to ship the message, and don't prevent it from
392 * being deleted. Typical values:
394 * 501 = exim's old antispam response
395 * 550 = exim's new antispam response (temporary)
396 * 553 = sendmail 8.8.7's generic REJECT
397 * 571 = sendmail's "unsolicited email refused"
400 send_bouncemail(msg, XMIT_ACCEPT,
401 "Our spam filter rejected this transaction.\r\n",
407 * Suppress error message only if the response specifically
408 * meant `excluded for policy reasons'. We *should* see
409 * an error when the return code is less specific.
412 error(0, -1, _("%cMTP error: %s"),
418 case 452: /* insufficient system storage */
420 * Temporary out-of-queue-space condition on the
421 * ESMTP server. Don't try to ship the message,
422 * and suppress deletion so it can be retried on
423 * a future retrieval cycle.
425 * Bouncemail *might* be appropriate here as a delay
426 * notification. But it's not really necessary because
427 * this is not an actual failure, we're very likely to be
428 * able to recover on the next cycle.
430 return(PS_TRANSIENT);
432 case 552: /* message exceeds fixed maximum message size */
434 * Permanent no-go condition on the
435 * ESMTP server. Don't try to ship the message,
436 * and allow it to be deleted.
438 send_bouncemail(msg, XMIT_ACCEPT,
439 "This message was too large.\r\n",
443 case 553: /* invalid sending domain */
445 * These latter days 553 usually means a spammer is trying to
448 send_bouncemail(msg, XMIT_ACCEPT,
449 "Invalid address.\r\n",
453 default: /* bounce the error back to the sender */
454 send_bouncemail(msg, XMIT_ACCEPT,
455 "General SMTP/ESMTP error.\r\n",
461 int open_sink(struct query *ctl, struct msgblk *msg,
462 int *good_addresses, int *bad_addresses)
463 /* set up sinkfp to be an input sink we can ship a message to */
467 *bad_addresses = *good_addresses = 0;
469 if (ctl->bsmtp) /* dump to a BSMTP batch file */
471 if (strcmp(ctl->bsmtp, "-") == 0)
474 sinkfp = fopen(ctl->bsmtp, "a");
476 /* see the ap computation under the SMTP branch */
478 "MAIL FROM: %s", (msg->return_path[0]) ? msg->return_path : user);
480 if (ctl->pass8bits || (ctl->mimemsg & MSG_IS_8BIT))
481 fputs(" BODY=8BITMIME", sinkfp);
482 else if (ctl->mimemsg & MSG_IS_7BIT)
483 fputs(" BODY=7BIT", sinkfp);
485 fprintf(sinkfp, " SIZE=%d\r\n", msg->reallen);
488 * RFC 1123 requires that the domain name part of the
489 * RCPT TO address be "canonicalized", that is a FQDN
490 * or MX but not a CNAME. Some listeners (like exim)
491 * enforce this. Now that we have the actual hostname,
492 * compute what we should canonicalize with.
494 ctl->destaddr = ctl->smtpaddress ? ctl->smtpaddress : "localhost";
497 for (idp = msg->recipients; idp; idp = idp->next)
498 if (idp->val.status.mark == XMIT_ACCEPT)
500 if (strchr(idp->id, '@'))
502 "RCPT TO: %s\r\n", idp->id);
505 "RCPT TO: %s@%s\r\n", idp->id, ctl->destaddr);
509 fputs("DATA\r\n", sinkfp);
513 error(0, -1, _("BSMTP file open or preamble write failed"));
517 else if (ctl->mda) /* we have a declared MDA */
519 int length = 0, fromlen = 0, nameslen = 0;
520 char *names = NULL, *before, *after, *from = NULL;
522 ctl->destaddr = "localhost";
524 for (idp = msg->recipients; idp; idp = idp->next)
525 if (idp->val.status.mark == XMIT_ACCEPT)
528 length = strlen(ctl->mda);
529 before = xstrdup(ctl->mda);
531 /* get user addresses for %T (or %s for backward compatibility) */
532 if (strstr(before, "%s") || strstr(before, "%T"))
535 * We go through this in order to be able to handle very
536 * long lists of users and (re)implement %s.
539 for (idp = msg->recipients; idp; idp = idp->next)
540 if ((idp->val.status.mark == XMIT_ACCEPT))
541 nameslen += (strlen(idp->id) + 1); /* string + ' ' */
542 if ((*good_addresses == 0))
543 nameslen = strlen(run.postmaster);
545 names = (char *)xmalloc(nameslen + 1); /* account for '\0' */
546 if (*good_addresses == 0)
547 strcpy(names, run.postmaster);
551 for (idp = msg->recipients; idp; idp = idp->next)
552 if (idp->val.status.mark == XMIT_ACCEPT)
554 strcat(names, idp->id);
557 names[--nameslen] = '\0'; /* chop trailing space */
560 /* sanitize names in order to contain only harmless shell chars */
564 /* get From address for %F */
565 if (strstr(before, "%F"))
567 from = xstrdup(msg->return_path);
569 /* sanitize from in order to contain *only* harmless shell chars */
572 fromlen = strlen(from);
575 /* do we have to build an mda string? */
580 /* find length of resulting mda string */
582 while ((sp = strstr(sp, "%s"))) {
583 length += nameslen - 2; /* subtract %s */
587 while ((sp = strstr(sp, "%T"))) {
588 length += nameslen - 2; /* subtract %T */
592 while ((sp = strstr(sp, "%F"))) {
593 length += fromlen - 2; /* subtract %F */
597 after = xmalloc(length + 1);
599 /* copy mda source string to after, while expanding %[sTF] */
600 for (dp = after, sp = before; (*dp = *sp); dp++, sp++) {
601 if (sp[0] != '%') continue;
603 /* need to expand? BTW, no here overflow, because in
604 ** the worst case (end of string) sp[1] == '\0' */
605 if (sp[1] == 's' || sp[1] == 'T') {
608 sp++; /* position sp over [sT] */
609 dp--; /* adjust dp */
610 } else if (sp[1] == 'F') {
613 sp++; /* position sp over F */
614 dp--; /* adjust dp */
633 if (outlevel >= O_DEBUG)
634 error(0, 0, _("about to deliver with: %s"), before);
638 * Arrange to run with user's permissions if we're root.
639 * This will initialize the ownership of any files the
640 * MDA creates properly. (The seteuid call is available
641 * under all BSDs and Linux)
644 #endif /* HAVE_SETEUID */
646 sinkfp = popen(before, "w");
651 /* this will fail quietly if we didn't start as root */
653 #endif /* HAVE_SETEUID */
657 error(0, 0, _("MDA open failed"));
661 sigchld = signal(SIGCHLD, SIG_DFL);
663 else /* forward to an SMTP or LMTP listener */
666 char options[MSGBUFSIZE];
667 char addr[HOSTLEN+USERNAMELEN+1];
668 char **from_responses;
671 /* build a connection to the SMTP listener */
672 if ((smtp_open(ctl) == -1))
674 error(0, errno, _("%cMTP connect to %s failed"),
676 ctl->smtphost ? ctl->smtphost : "localhost");
681 * Compute ESMTP options.
684 if (ctl->server.esmtp_options & ESMTP_8BITMIME) {
685 if (ctl->pass8bits || (ctl->mimemsg & MSG_IS_8BIT))
686 strcpy(options, " BODY=8BITMIME");
687 else if (ctl->mimemsg & MSG_IS_7BIT)
688 strcpy(options, " BODY=7BIT");
691 if ((ctl->server.esmtp_options & ESMTP_SIZE) && msg->reallen > 0)
692 sprintf(options + strlen(options), " SIZE=%d", msg->reallen);
695 * Try to get the SMTP listener to take the Return-Path
696 * address as MAIL FROM . If it won't, fall back on the
697 * calling-user ID. This won't affect replies, which use the
698 * header From address anyway.
700 * RFC 1123 requires that the domain name part of the
701 * MAIL FROM address be "canonicalized", that is a
702 * FQDN or MX but not a CNAME. We'll assume the From
703 * header is already in this form here (it certainly
704 * is if rewrite is on). RFC 1123 is silent on whether
705 * a nonexistent hostname part is considered canonical.
707 * This is a potential problem if the MTAs further upstream
708 * didn't pass canonicalized From/Return-Path lines, *and* the
709 * local SMTP listener insists on them.
711 ap = (msg->return_path[0]) ? msg->return_path : user;
712 if (SMTP_from(ctl->smtp_socket, ap, options) != SM_OK)
713 return(handle_smtp_error(ctl, msg));
716 * Now list the recipient addressees
719 for (idp = msg->recipients; idp; idp = idp->next)
721 xalloca(from_responses, char **, sizeof(char *) * total_addresses);
722 for (idp = msg->recipients; idp; idp = idp->next)
723 if (idp->val.status.mark == XMIT_ACCEPT)
725 if (strchr(idp->id, '@'))
726 strcpy(addr, idp->id);
729 snprintf(addr, sizeof(addr)-1, "%s@%s", idp->id, ctl->destaddr);
731 sprintf(addr, "%s@%s", idp->id, ctl->destaddr);
732 #endif /* HAVE_SNPRINTF */
734 if (SMTP_rcpt(ctl->smtp_socket, addr) == SM_OK)
738 char errbuf[POPBUFSIZE];
740 strcpy(errbuf, idp->id);
741 strcat(errbuf, ": ");
742 strcat(errbuf, smtp_response);
744 xalloca(from_responses[*bad_addresses],
747 strcpy(from_responses[*bad_addresses], errbuf);
750 idp->val.status.mark = XMIT_RCPTBAD;
751 if (outlevel >= O_VERBOSE)
753 _("%cMTP listener doesn't like recipient address `%s'"),
754 ctl->listener, addr);
758 send_bouncemail(msg, XMIT_RCPTBAD,
759 "Some addresses were rejected by the MDA fetchmail forwards to.\r\n",
760 *bad_addresses, from_responses);
762 * It's tempting to do local notification only if bouncemail was
763 * insufficient -- that is, to add && total_addresses > *bad_addresses
764 * to the test here. The problem with this theory is that it would
765 * make initial diagnosis of a broken multidrop configuration very
766 * hard -- most single-recipient messages would just invisibly bounce.
768 if (!(*good_addresses))
770 if (strchr(run.postmaster, '@'))
771 strcpy(addr, run.postmaster);
775 snprintf(addr, sizeof(addr)-1, "%s@%s", run.postmaster, ctl->destaddr);
777 sprintf(addr, "%s@%s", run.postmaster, ctl->destaddr);
778 #endif /* HAVE_SNPRINTF */
781 if (SMTP_rcpt(ctl->smtp_socket, addr) != SM_OK)
783 error(0, 0, _("can't even send to %s!"), run.postmaster);
784 SMTP_rset(ctl->smtp_socket); /* required by RFC1870 */
788 if (outlevel >= O_VERBOSE)
789 error(0, 0, _("no address matches; forwarding to %s."), run.postmaster);
793 * Tell the listener we're ready to send data.
794 * Some listeners (like zmailer) may return antispam errors here.
796 if (SMTP_data(ctl->smtp_socket) != SM_OK)
797 return(handle_smtp_error(ctl, msg));
801 * We need to stash this away in order to know how many
802 * response lines to expect after the LMTP end-of-message.
804 lmtp_responses = *good_addresses;
809 void release_sink(struct query *ctl)
810 /* release the per-message output sink, whether it's a pipe or SMTP socket */
819 sinkfp = (FILE *)NULL;
821 signal(SIGCHLD, sigchld);
825 int close_sink(struct query *ctl, struct msgblk *msg, flag forward)
826 /* perform end-of-message actions on the current output sink */
832 /* close the delivery pipe, we'll reopen before next message */
836 sinkfp = (FILE *)NULL;
840 signal(SIGCHLD, sigchld);
843 error(0, -1, _("MDA exited abnormally or returned nonzero status"));
849 /* implicit disk-full check here... */
850 fputs("..\r\n", sinkfp);
851 if (strcmp(ctl->bsmtp, "-"))
855 error(0, -1, _("Message termination or close of BSMTP file failed"));
861 /* write message terminator */
862 if (SMTP_eom(ctl->smtp_socket) != SM_OK)
864 if (handle_smtp_error(ctl, msg) != PS_REFUSED)
868 error(0, -1, _("SMTP listener refused delivery"));
874 * If this is an SMTP connection, SMTP_eom() ate the response.
875 * But could be this is an LMTP connection, in which case we have to
876 * interpret either (a) a single 503 response meaning there
877 * were no successful RCPT TOs, or (b) a variable number of
878 * responses, one for each successful RCPT TO. We need to send
879 * bouncemail on each failed response and then return TRUE anyway,
880 * otherwise the message will get left in the queue and resent
881 * to people who got it the first time.
883 if (ctl->listener == LMTP_MODE)
884 if (lmtp_responses == 0)
886 SMTP_ok(ctl->smtp_socket);
889 * According to RFC2033, 503 is the only legal response
890 * if no RCPT TO commands succeeded. No error recovery
891 * is really possible here, as we have no idea what
892 * insane thing the listener might be doing if it doesn't
895 if (atoi(smtp_response) == 503)
896 error(0, -1, _("LMTP delivery error on EOM"));
899 _("Unexpected non-503 response to LMTP EOM: %s"),
903 * It's not completely clear what to do here. We choose to
904 * interpret delivery failure here as a transient error,
905 * the same way SMTP delivery failure is handled. If we're
906 * wrong, an undead message will get stuck in the queue.
915 /* eat the RFC2033-required responses, saving errors */
916 xalloca(responses, char **, sizeof(char *) * lmtp_responses);
917 for (errors = i = 0; i < lmtp_responses; i++)
919 if (SMTP_ok(ctl->smtp_socket) == SM_OK)
920 responses[i] = (char *)NULL;
923 xalloca(responses[errors],
925 strlen(smtp_response)+1);
926 strcpy(responses[errors], smtp_response);
932 return(TRUE); /* all deliveries succeeded */
935 * One or more deliveries failed.
936 * If we can bounce a failures list back to the sender,
937 * return TRUE, deleting the message from the server so
938 * it won't be re-forwarded on subsequent poll cycles.
940 return(send_bouncemail(msg, XMIT_ACCEPT,
941 "LSMTP partial delivery failure.\r\n",
949 int open_warning_by_mail(struct query *ctl, struct msgblk *msg)
950 /* set up output sink for a mailed warning to calling user */
955 * Dispatching warning email is a little complicated. The problem is
956 * that we have to deal with three distinct cases:
958 * 1. Single-drop running from user account. Warning mail should
959 * go to the local name for which we're collecting (coincides
960 * with calling user).
962 * 2. Single-drop running from root or other privileged ID, with rc
963 * file generated on the fly (Ken Estes's weird setup...) Mail
964 * should go to the local name for which we're collecting (does not
965 * coincide with calling user).
967 * 3. Multidrop. Mail must go to postmaster. We leave the recipients
968 * member null so this message will fall through to run.postmaster.
970 * The zero in the reallen element means we won't pass a SIZE
971 * option to ESMTP; the message length would be more trouble than
972 * it's worth to compute.
974 struct msgblk reply = {NULL, NULL, "FETCHMAIL-DAEMON", 0};
976 if (!MULTIDROP(ctl)) /* send to calling user */
980 save_str(&reply.recipients, ctl->localnames->id, XMIT_ACCEPT);
981 status = open_sink(ctl, &reply, &good, &bad);
982 free_str_list(&reply.recipients);
985 else /* send to postmaster */
986 return(open_sink(ctl, &reply, &good, &bad));
989 #if defined(HAVE_STDARG_H)
990 void stuff_warning(struct query *ctl, const char *fmt, ... )
992 void stuff_warning(struct query *ctl, fmt, va_alist)
994 const char *fmt; /* printf-style format */
997 /* format and ship a warning message line by mail */
999 char buf[POPBUFSIZE];
1003 * stuffline() requires its input to be writeable (for CR stripping),
1004 * so we needed to copy the message to a writeable buffer anyway in
1005 * case it was a string constant. We make a virtue of that necessity
1006 * here by supporting stdargs/varargs.
1008 #if defined(HAVE_STDARG_H)
1013 #ifdef HAVE_VSNPRINTF
1014 vsnprintf(buf, sizeof(buf), fmt, ap);
1016 vsprintf(buf, fmt, ap);
1020 strcat(buf, "\r\n");
1022 stuffline(ctl, buf);
1025 void close_warning_by_mail(struct query *ctl, struct msgblk *msg)
1026 /* sign and send mailed warnings */
1028 stuff_warning(ctl, "--\r\n\t\t\t\tThe Fetchmail Daemon\r\n");
1029 close_sink(ctl, msg, TRUE);
1032 /* sink.c ends here */