*
* Copyright 1998 by Eric S. Raymond
* For license terms, see the file COPYING in this directory.
- *
- * i18n by Arnaldo Carvalho de Melo <acme@conectiva.com.br> 7-Nov-1998
*/
#include "config.h"
#include <errno.h>
#include <string.h>
#include <signal.h>
+#include <time.h>
#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif /* HAVE_MEMORY_H */
#else
#include <varargs.h>
#endif
+#include <ctype.h>
#include "fetchmail.h"
#include "socket.h"
#define SIGCHLD SIGCLD
#endif
+/* makes the open_sink()/close_sink() pair non-reentrant */
+static int lmtp_responses;
+
static int smtp_open(struct query *ctl)
/* try to open a socket to the appropriate SMTP server for this query */
{
/* maybe it's time to close the socket in order to force delivery */
- if (NUM_NONZERO(ctl->batchlimit) && (ctl->smtp_socket != -1) && batchcount++ == ctl->batchlimit)
+ if (NUM_NONZERO(ctl->batchlimit) && (ctl->smtp_socket != -1) && ++batchcount == ctl->batchlimit)
{
close(ctl->smtp_socket);
ctl->smtp_socket = -1;
for (idp = ctl->smtphunt; idp; idp = idp->next)
{
char *cp, *parsed_host;
-#ifdef INET6
+#ifdef INET6_ENABLE
char *portnum = SMTP_PORT;
#else
int portnum = SMTP_PORT;
-#endif /* INET6 */
+#endif /* INET6_ENABLE */
xalloca(parsed_host, char *, strlen(idp->id) + 1);
if ((cp = strrchr(parsed_host, '/')))
{
*cp++ = 0;
-#ifdef INET6
+#ifdef INET6_ENABLE
portnum = cp;
#else
portnum = atoi(cp);
-#endif /* INET6 */
+#endif /* INET6_ENABLE */
}
if ((ctl->smtp_socket = SockOpen(parsed_host,portnum,NULL,
ctl->destaddr = ctl->smtpaddress ? ctl->smtpaddress : ( ctl->smtphost ? ctl->smtphost : "localhost");
if (outlevel >= O_DEBUG && ctl->smtp_socket != -1)
- error(0, 0, _("forwarding to %s"), ctl->smtphost);
+ report(stdout, _("forwarding to %s\n"), ctl->smtphost);
return(ctl->smtp_socket);
}
* decorated any . lines it sends back up.
*/
if (*buf == '.')
+ {
if (ctl->server.base_protocol->delimited) /* server has already byte-stuffed */
{
if (ctl->mda)
else
/* leave it alone */;
}
+ }
/* we may need to strip carriage returns */
if (ctl->stripcr)
*cp = '_';
}
-static int send_bouncemail(struct msgblk *msg,
- char *message, int nerrors, char *errors[])
+static int send_bouncemail(struct query *ctl, struct msgblk *msg,
+ int userclass, char *message,
+ int nerrors, char *errors[])
/* bounce back an error report a la RFC 1892 */
{
char daemon_name[18 + HOSTLEN] = "FETCHMAIL-DAEMON@";
- char boundary[BUFSIZ];
- int i, sock;
+ char boundary[BUFSIZ], *ts, *bounce_to;
+ int sock;
/* don't bounce in reply to undeliverable bounces */
if (!msg->return_path[0] || strcmp(msg->return_path, "<>") == 0)
return(FALSE);
+ bounce_to = (run.bouncemail ? msg->return_path : run.postmaster);
+
SMTP_setmode(SMTP_MODE);
strcat(daemon_name, fetchmailhost);
|| SMTP_ok(sock) != SM_OK
|| SMTP_helo(sock, "localhost") != SM_OK
|| SMTP_from(sock, daemon_name, (char *)NULL) != SM_OK
- || SMTP_rcpt(sock, msg->return_path) != SM_OK
+ || SMTP_rcpt(sock, bounce_to) != SM_OK
|| SMTP_data(sock) != SM_OK)
return(FALSE);
sprintf(boundary,
"om-mani-padme-hum-%d-%d-%ld",
- getpid(), getppid(), time((time_t *)NULL));
+ (int)getpid(), (int)getppid(), time((time_t *)NULL));
+
+ ts = rfc822timestamp();
if (outlevel >= O_VERBOSE)
- error(0, 0, "SMTP: (bounce-message body)");
+ report(stdout, "SMTP: (bounce-message body)\n");
/* bouncemail headers */
- SockPrintf(sock, "Return-Path: <>");
- SockPrintf(sock, "From: FETCHMAIL-DAEMON@%s\r\n", fetchmailhost);
- SockPrintf(sock, "To: %s\n", msg->return_path);
+ SockPrintf(sock, "Return-Path: <>\r\n");
+ SockPrintf(sock, "From: %s\r\n", daemon_name);
+ SockPrintf(sock, "To: %s\r\n", bounce_to);
SockPrintf(sock, "MIME-Version: 1.0\r\n");
- SockPrintf(sock, "Content-Type: multipart/report; report-type=delivery-status boundary=\"%s\"\r\n", boundary);
- SockPrintf(sock, "\r\n");
- SockPrintf(sock, "Content-Transfer-Encoding: 7bit\r\n");
+ SockPrintf(sock, "Content-Type: multipart/report; report-type=delivery-status;\r\n\tboundary=\"%s\"\r\n", boundary);
SockPrintf(sock, "\r\n");
/* RFC1892 part 1 -- human-readable message */
SockPrintf(sock, "\r\n");
SockWrite(sock, message, strlen(message));
SockPrintf(sock, "\r\n");
+ SockPrintf(sock, "\r\n");
if (nerrors)
{
+ struct idlist *idp;
+ int nusers;
+
/* RFC1892 part 2 -- machine-readable responses */
SockPrintf(sock, "--%s\r\n", boundary);
SockPrintf(sock,"Content-Type: message/delivery-status\r\n");
SockPrintf(sock, "\r\n");
- for (i = 0; i < nerrors; i++)
- SockPrintf(sock, errors[i]);
+ SockPrintf(sock, "Reporting-MTA: dns; %s\r\n", fetchmailhost);
+
+ nusers = 0;
+ for (idp = msg->recipients; idp; idp = idp->next)
+ if (idp->val.status.mark == userclass)
+ {
+ char *error;
+ /* Minimum RFC1894 compliance + Diagnostic-Code field */
+ SockPrintf(sock, "\r\n");
+ SockPrintf(sock, "Final-Recipient: rfc822; %s\r\n", idp->id);
+ SockPrintf(sock, "Last-Attempt-Date: %s\r\n", ts);
+ SockPrintf(sock, "Action: failed\r\n");
+
+ if (nerrors == 1)
+ /* one error applies to all users */
+ error = errors[0];
+ else if (nerrors > nusers)
+ {
+ SockPrintf(sock, "Internal error: SMTP error count doesn't match number of recipients.\r\n");
+ break;
+ }
+ else
+ /* errors correspond 1-1 to selected users */
+ error = errors[nusers++];
+
+ if (strlen(error) > 9 && isdigit(error[4])
+ && error[5] == '.' && isdigit(error[6])
+ && error[7] == '.' && isdigit(error[8]))
+ /* Enhanced status code available, use it */
+ SockPrintf(sock, "Status: %5.5s\r\n", &(error[4]));
+ else
+ /* Enhanced status code not available, fake one */
+ SockPrintf(sock, "Status: %c.0.0\r\n", error[0]);
+ SockPrintf(sock, "Diagnostic-Code: %s\r\n", error);
+ }
SockPrintf(sock, "\r\n");
}
return(TRUE);
}
+static int handle_smtp_report(struct query *ctl, struct msgblk *msg)
+/* handle SMTP errors based on the content of SMTP_response */
+/* Mail is deleted from the server if this function returns PS_REFUSED. */
+{
+ int smtperr = atoi(smtp_response);
+ char *responses[1];
+
+ xalloca(responses[0], char *, strlen(smtp_response)+1);
+ strcpy(responses[0], smtp_response);
+
+ /*
+ * Note: send_bouncemail message strings are not made subject
+ * to gettext translation because (a) they're going to be
+ * embedded in a text/plain 7bit part, and (b) they're
+ * going to be associated with listener error-response
+ * messages, which are probably in English (none of the
+ * MTAs I know about are internationalized).
+ */
+ if (str_find(&ctl->antispam, smtperr))
+ {
+ /*
+ * SMTP listener explicitly refuses to deliver mail
+ * coming from this address, probably due to an
+ * anti-spam domain exclusion. Respect this. Don't
+ * try to ship the message, and don't prevent it from
+ * being deleted. Default values:
+ *
+ * 571 = sendmail's "unsolicited email refused"
+ * 550 = exim's new antispam response (temporary)
+ * 501 = exim's old antispam response
+ * 554 = Postfix antispam response.
+ *
+ */
+ SMTP_rset(ctl->smtp_socket); /* stay on the safe site */
+ send_bouncemail(ctl, msg, XMIT_ACCEPT,
+ "Our spam filter rejected this transaction.\r\n",
+ 1, responses);
+ return(PS_REFUSED);
+ }
+
+ /*
+ * Suppress error message only if the response specifically
+ * meant `excluded for policy reasons'. We *should* see
+ * an error when the return code is less specific.
+ */
+ if (smtperr >= 400)
+ report(stderr, _("%cMTP error: %s\n"),
+ ctl->listener,
+ smtp_response);
+
+ switch (smtperr)
+ {
+ case 452: /* insufficient system storage */
+ /*
+ * Temporary out-of-queue-space condition on the
+ * ESMTP server. Don't try to ship the message,
+ * and suppress deletion so it can be retried on
+ * a future retrieval cycle.
+ *
+ * Bouncemail *might* be appropriate here as a delay
+ * notification (note; if we ever add this, we must make
+ * sure the RFC1894 Action field is "delayed" rather thwn
+ * "failed"). But it's not really necessary because
+ * this is not an actual failure, we're very likely to be
+ * able to recover on the next cycle.
+ */
+ SMTP_rset(ctl->smtp_socket); /* required by RFC1870 */
+ return(PS_TRANSIENT);
+
+ case 552: /* message exceeds fixed maximum message size */
+ /*
+ * Permanent no-go condition on the
+ * ESMTP server. Don't try to ship the message,
+ * and allow it to be deleted.
+ */
+ SMTP_rset(ctl->smtp_socket); /* required by RFC1870 */
+ send_bouncemail(ctl, msg, XMIT_ACCEPT,
+ "This message was too large.\r\n",
+ 1, responses);
+ return(run.bouncemail ? PS_REFUSED : PS_TRANSIENT);
+
+ case 553: /* invalid sending domain */
+ /*
+ * These latter days 553 usually means a spammer is trying to
+ * cover his tracks. We never bouncemail on these, because
+ * (a) the return address is invalid by definition, and
+ * (b) we wouldn't want spammers to get confirmation that
+ * this address is live, anyway.
+ */
+ SMTP_rset(ctl->smtp_socket); /* stay on the safe side */
+ send_bouncemail(ctl, msg, XMIT_ACCEPT,
+ "Invalid address.\r\n",
+ 1, responses);
+ return(PS_REFUSED);
+
+ default: /* bounce non-transient errors back to the sender */
+ SMTP_rset(ctl->smtp_socket); /* stay on the safe side */
+ if (smtperr >= 500 && smtperr <= 599)
+ if (send_bouncemail(ctl, msg, XMIT_ACCEPT,
+ "General SMTP/ESMTP error.\r\n",
+ 1, responses))
+ return(run.bouncemail ? PS_REFUSED : PS_TRANSIENT);
+ return(PS_TRANSIENT);
+ }
+}
+
int open_sink(struct query *ctl, struct msgblk *msg,
int *good_addresses, int *bad_addresses)
/* set up sinkfp to be an input sink we can ship a message to */
else if (ctl->mimemsg & MSG_IS_7BIT)
fputs(" BODY=7BIT", sinkfp);
- fprintf(sinkfp, " SIZE=%ld\r\n", msg->reallen);
+ fprintf(sinkfp, " SIZE=%d\r\n", msg->reallen);
/*
* RFC 1123 requires that the domain name part of the
if (ferror(sinkfp))
{
- error(0, -1, _("BSMTP file open or preamble write failed"));
+ report(stderr, _("BSMTP file open or preamble write failed\n"));
return(PS_BSMTP);
}
}
if (outlevel >= O_DEBUG)
- error(0, 0, _("about to deliver with: %s"), before);
+ report(stdout, _("about to deliver with: %s\n"), before);
#ifdef HAVE_SETEUID
/*
if (!sinkfp)
{
- error(0, 0, _("MDA open failed"));
+ report(stderr, _("MDA open failed\n"));
return(PS_IOERR);
}
else /* forward to an SMTP or LMTP listener */
{
const char *ap;
- char options[MSGBUFSIZE], addr[128];
+ char options[MSGBUFSIZE];
+ char addr[HOSTLEN+USERNAMELEN+1];
+ char **from_responses;
+ int total_addresses;
/* build a connection to the SMTP listener */
if ((smtp_open(ctl) == -1))
{
- error(0, errno, _("%cMTP connect to %s failed"),
+ report(stderr, _("%cMTP connect to %s failed\n"),
ctl->listener,
ctl->smtphost ? ctl->smtphost : "localhost");
return(PS_SMTP);
}
if ((ctl->server.esmtp_options & ESMTP_SIZE) && msg->reallen > 0)
- sprintf(options + strlen(options), " SIZE=%ld", msg->reallen);
+ sprintf(options + strlen(options), " SIZE=%d", msg->reallen);
/*
* Try to get the SMTP listener to take the Return-Path
* This is a potential problem if the MTAs further upstream
* didn't pass canonicalized From/Return-Path lines, *and* the
* local SMTP listener insists on them.
- *
- * Note: send_bouncemail message strings are not made subject
- * to gettext translation because (a) they're going to be
- * embedded in a text/plain 7bit part, and (b) they're
- * going to be associated with listener error-response
- * messages, which are probably in English (none of the
- * MTAs I know about are internationalized).
*/
ap = (msg->return_path[0]) ? msg->return_path : user;
if (SMTP_from(ctl->smtp_socket, ap, options) != SM_OK)
- {
- int smtperr = atoi(smtp_response);
- char *responses[1];
-
- responses[0] = smtp_response;
-
- /* required by RFC1870; sets us up to be able to send bouncemail */
- SMTP_rset(ctl->smtp_socket);
-
- if (str_find(&ctl->antispam, smtperr))
- {
- /*
- * SMTP listener explicitly refuses to deliver mail
- * coming from this address, probably due to an
- * anti-spam domain exclusion. Respect this. Don't
- * try to ship the message, and don't prevent it from
- * being deleted. Typical values:
- *
- * 501 = exim's old antispam response
- * 550 = exim's new antispam response (temporary)
- * 553 = sendmail 8.8.7's generic REJECT
- * 571 = sendmail's "unsolicited email refused"
- *
- */
- send_bouncemail(msg,
- "We do not accept mail from you.\r\n",
- 1, responses);
- return(PS_REFUSED);
- }
-
- /*
- * Suppress error message only if the response specifically
- * meant `excluded for policy reasons'. We *should* see
- * an error when the return code is less specific.
- */
- if (smtperr >= 400)
- error(0, -1, _("%cMTP error: %s"),
- ctl->listener,
- smtp_response);
-
- switch (smtperr)
- {
- case 452: /* insufficient system storage */
- /*
- * Temporary out-of-queue-space condition on the
- * ESMTP server. Don't try to ship the message,
- * and suppress deletion so it can be retried on
- * a future retrieval cycle.
- *
- * Bouncemail *might* be appropriate here as a delay
- * notification. But it's not really necessary because
- * this is not an actual failure, we're very likely to be
- * able to recover on the next cycle.
- */
- return(PS_TRANSIENT);
-
- case 552: /* message exceeds fixed maximum message size */
- /*
- * Permanent no-go condition on the
- * ESMTP server. Don't try to ship the message,
- * and allow it to be deleted.
- */
- send_bouncemail(msg,
- "This message was too large.\r\n",
- 1, responses);
- return(PS_REFUSED);
-
- case 553: /* invalid sending domain */
- /*
- * These latter days 553 usually means a spammer is trying to
- * cover his tracks.
- */
- send_bouncemail(msg,
- "Invalid address.\r\n",
- 1, responses);
- return(PS_REFUSED);
-
- default: /* bounce the error back to the sender */
- send_bouncemail(msg,
- "General SMTP/ESMTP error.\r\n",
- 1, responses);
- return(PS_REFUSED);
- }
- }
+ return(handle_smtp_report(ctl, msg));
/*
* Now list the recipient addressees
*/
+ total_addresses = 0;
+ for (idp = msg->recipients; idp; idp = idp->next)
+ total_addresses++;
+ xalloca(from_responses, char **, sizeof(char *) * total_addresses);
for (idp = msg->recipients; idp; idp = idp->next)
if (idp->val.status.mark == XMIT_ACCEPT)
{
(*good_addresses)++;
else
{
+ char errbuf[POPBUFSIZE];
+
+ strcpy(errbuf, idp->id);
+ strcat(errbuf, ": ");
+ strcat(errbuf, smtp_response);
+
+ xalloca(from_responses[*bad_addresses],
+ char *,
+ strlen(errbuf)+1);
+ strcpy(from_responses[*bad_addresses], errbuf);
+
(*bad_addresses)++;
- idp->val.status.mark = XMIT_ANTISPAM;
- error(0, 0,
- _("%cMTP listener doesn't like recipient address `%s'"),
- ctl->listener, addr);
+ idp->val.status.mark = XMIT_RCPTBAD;
+ if (outlevel >= O_VERBOSE)
+ report(stderr,
+ _("%cMTP listener doesn't like recipient address `%s'\n"),
+ ctl->listener, addr);
}
}
- if (!(*good_addresses))
+ if (*bad_addresses)
+ send_bouncemail(ctl, msg, XMIT_RCPTBAD,
+ "Some addresses were rejected by the MDA fetchmail forwards to.\r\n",
+ *bad_addresses, from_responses);
+ /*
+ * It's tempting to do local notification only if bouncemail was
+ * insufficient -- that is, to add && total_addresses > *bad_addresses
+ * to the test here. The problem with this theory is that it would
+ * make initial diagnosis of a broken multidrop configuration very
+ * hard -- most single-recipient messages would just invisibly bounce.
+ */
+ if (!(*good_addresses))
{
+ if (strchr(run.postmaster, '@'))
+ strcpy(addr, run.postmaster);
+ else
+ {
#ifdef HAVE_SNPRINTF
- snprintf(addr, sizeof(addr)-1, "%s@%s", run.postmaster, ctl->destaddr);
+ snprintf(addr, sizeof(addr)-1, "%s@%s", run.postmaster, ctl->destaddr);
#else
- sprintf(addr, "%s@%s", run.postmaster, ctl->destaddr);
+ sprintf(addr, "%s@%s", run.postmaster, ctl->destaddr);
#endif /* HAVE_SNPRINTF */
+ }
if (SMTP_rcpt(ctl->smtp_socket, addr) != SM_OK)
{
- error(0, 0, _("can't even send to %s!"), run.postmaster);
+ report(stderr, _("can't even send to %s!\n"), run.postmaster);
SMTP_rset(ctl->smtp_socket); /* required by RFC1870 */
return(PS_SMTP);
}
+
+ if (outlevel >= O_VERBOSE)
+ report(stderr, _("no address matches; forwarding to %s.\n"), run.postmaster);
}
- /* tell it we're ready to send data */
- SMTP_data(ctl->smtp_socket);
+ /*
+ * Tell the listener we're ready to send data.
+ * Some listeners (like zmailer) may return antispam errors here.
+ */
+ if (SMTP_data(ctl->smtp_socket) != SM_OK)
+ return(handle_smtp_report(ctl, msg));
}
/*
* We need to stash this away in order to know how many
* response lines to expect after the LMTP end-of-message.
*/
- msg->lmtp_responses = *good_addresses;
+ lmtp_responses = *good_addresses;
return(PS_SUCCESS);
}
signal(SIGCHLD, sigchld);
if (rc)
{
- error(0, -1, _("MDA exited abnormally or returned nonzero status"));
+ report(stderr,
+ _("MDA exited abnormally or returned nonzero status\n"));
return(FALSE);
}
}
fclose(sinkfp);
if (ferror(sinkfp))
{
- error(0, -1, _("Message termination or close of BSMTP file failed"));
+ report(stderr,
+ _("Message termination or close of BSMTP file failed\n"));
return(FALSE);
}
}
/* write message terminator */
if (SMTP_eom(ctl->smtp_socket) != SM_OK)
{
- error(0, -1, _("SMTP listener refused delivery"));
- return(FALSE);
+ if (handle_smtp_report(ctl, msg) != PS_REFUSED)
+ return(FALSE);
+ else
+ {
+ report(stderr, _("SMTP listener refused delivery\n"));
+ return(TRUE);
+ }
}
/*
* to people who got it the first time.
*/
if (ctl->listener == LMTP_MODE)
- if (msg->lmtp_responses == 0)
+ {
+ if (lmtp_responses == 0)
{
SMTP_ok(ctl->smtp_socket);
* comply.
*/
if (atoi(smtp_response) == 503)
- error(0, -1, _("LMTP delivery error on EOM"));
+ report(stderr, _("LMTP delivery error on EOM\n"));
else
- error(0, -1,
- _("Unexpected non-503 response to LMTP EOM: %s"),
+ report(stderr,
+ _("Unexpected non-503 response to LMTP EOM: %s\n"),
smtp_response);
/*
char **responses;
/* eat the RFC2033-required responses, saving errors */
- xalloca(responses, char **, sizeof(char *) * msg->lmtp_responses);
- for (errors = i = 0; i < msg->lmtp_responses; i++)
+ xalloca(responses, char **, sizeof(char *) * lmtp_responses);
+ for (errors = i = 0; i < lmtp_responses; i++)
{
if (SMTP_ok(ctl->smtp_socket) == SM_OK)
responses[i] = (char *)NULL;
else
/*
* One or more deliveries failed.
- * If we can bounce a failures list back to the sender,
- * return TRUE, deleting the message from the server so
- * it won't be re-forwarded on subsequent poll cycles.
+ * If we can bounce a failures list back to the
+ * sender, and the postmaster does not want to
+ * deal with the bounces return TRUE, deleting the
+ * message from the server so it won't be
+ * re-forwarded on subsequent poll cycles.
*/
- return(send_bouncemail(msg,
- "LSMTP partial delivery failure.\r\n",
- errors, responses));
+ return(send_bouncemail(ctl, msg, XMIT_ACCEPT,
+ "LSMTP partial delivery failure.\r\n",
+ errors, responses));
}
+ }
}
return(TRUE);
* option to ESMTP; the message length would be more trouble than
* it's worth to compute.
*/
- struct msgblk reply = {NULL, NULL, "FETCHMAIL-DAEMON", 0};
+ struct msgblk reply = {NULL, NULL, "FETCHMAIL-DAEMON@", 0};
+
+ strcat(reply.return_path, fetchmailhost);
if (!MULTIDROP(ctl)) /* send to calling user */
{