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 SMTP
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_ALLOCA_H)
35 #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
45 #define SMTP_PORT "smtp" /* standard SMTP service port */
47 #define SMTP_PORT 25 /* standard SMTP service port */
50 static int smtp_open(struct query *ctl)
51 /* try to open a socket to the appropriate SMTP server for this query */
53 /* maybe it's time to close the socket in order to force delivery */
54 if (NUM_NONZERO(ctl->batchlimit) && (ctl->smtp_socket != -1) && batchcount++ == ctl->batchlimit)
56 close(ctl->smtp_socket);
57 ctl->smtp_socket = -1;
61 /* if no socket to any SMTP host is already set up, try to open one */
62 if (ctl->smtp_socket == -1)
65 * RFC 1123 requires that the domain name in HELO address is a
66 * "valid principal domain name" for the client host. If we're
67 * running in invisible mode, violate this with malice
68 * aforethought in order to make the Received headers and
71 * In fact this code relies on the RFC1123 requirement that the
72 * SMTP listener must accept messages even if verification of the
73 * HELO name fails (RFC1123 section 5.2.5, paragraph 2).
75 * How we compute the true mailhost name to pass to the
76 * listener doesn't affect behavior on RFC1123- violating
77 * listeners that check for name match; we're going to lose
78 * on those anyway because we can never give them a name
79 * that matches the local machine fetchmail is running on.
80 * What it will affect is the listener's logging.
83 char *id_me = run.invisible ? ctl->server.truename : fetchmailhost;
89 * Run down the SMTP hunt list looking for a server that's up.
90 * Use both explicit hunt entries (value TRUE) and implicit
91 * (default) ones (value FALSE).
94 phase = LISTENER_WAIT;
96 set_timeout(ctl->server.timeout);
97 for (idp = ctl->smtphunt; idp; idp = idp->next)
99 char *cp, *parsed_host = alloca(strlen(idp->id) + 1);
101 char *portnum = SMTP_PORT;
103 int portnum = SMTP_PORT;
106 ctl->smtphost = idp->id; /* remember last host tried. */
108 strcpy(parsed_host, idp->id);
109 if ((cp = strrchr(parsed_host, '/')))
119 if ((ctl->smtp_socket = SockOpen(parsed_host,portnum,NULL)) == -1)
122 /* first, probe for ESMTP */
123 if (SMTP_ok(ctl->smtp_socket) == SM_OK &&
124 SMTP_ehlo(ctl->smtp_socket, id_me,
125 &ctl->server.esmtp_options) == SM_OK)
129 * RFC 1869 warns that some listeners hang up on a failed EHLO,
130 * so it's safest not to assume the socket will still be good.
132 SockClose(ctl->smtp_socket);
133 ctl->smtp_socket = -1;
135 /* if opening for ESMTP failed, try SMTP */
136 if ((ctl->smtp_socket = SockOpen(parsed_host,portnum,NULL)) == -1)
139 if (SMTP_ok(ctl->smtp_socket) == SM_OK &&
140 SMTP_helo(ctl->smtp_socket, id_me) == SM_OK)
143 SockClose(ctl->smtp_socket);
144 ctl->smtp_socket = -1;
151 * RFC 1123 requires that the domain name part of the
152 * RCPT TO address be "canonicalized", that is a FQDN
153 * or MX but not a CNAME. Some listeners (like exim)
154 * enforce this. Now that we have the actual hostname,
155 * compute what we should canonicalize with.
157 ctl->destaddr = ctl->smtpaddress ? ctl->smtpaddress : ( ctl->smtphost ? ctl->smtphost : "localhost");
159 if (outlevel >= O_DEBUG && ctl->smtp_socket != -1)
160 error(0, 0, "forwarding to %s", ctl->smtphost);
162 return(ctl->smtp_socket);
165 /* these are shared by open_sink and stuffline */
167 static RETSIGTYPE (*sigchld)();
169 int stuffline(struct query *ctl, char *buf)
170 /* ship a line to the given control block's output sink (SMTP server or MDA) */
175 /* The line may contain NUL characters. Find the last char to use
176 * -- the real line termination is the sequence "\n\0".
179 while ((last += strlen(last)) && (last[-1] != '\n'))
182 /* fix message lines that have only \n termination (for qmail) */
185 if (last - 1 == buf || last[-2] != '\r')
194 phase = FORWARDING_WAIT;
197 * SMTP byte-stuffing. We only do this if the protocol does *not*
198 * use .<CR><LF> as EOM. If it does, the server will already have
199 * decorated any . lines it sends back up.
202 if (ctl->server.base_protocol->delimited) /* server has already byte-stuffed */
207 /* writing to SMTP, leave the byte-stuffing in place */;
209 else /* if (!protocol->delimited) -- not byte-stuffed already */
212 SockWrite(ctl->smtp_socket, buf, 1); /* byte-stuff it */
214 /* leave it alone */;
217 /* we may need to strip carriage returns */
222 for (sp = tp = buf; sp < last; sp++)
231 n = fwrite(buf, 1, last - buf, sinkfp);
232 else if (ctl->smtp_socket != -1)
233 n = SockWrite(ctl->smtp_socket, buf, last - buf);
240 static void sanitize(char *s)
241 /* replace unsafe shellchars by an _ */
243 static char *ok_chars = " 1234567890!@%-_=+:,./abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
246 for (cp = s; *(cp += strspn(cp, ok_chars)); /* NO INCREMENT */)
250 int open_sink(struct query *ctl,
252 struct idlist *xmit_names,
254 int *good_addresses, int *bad_addresses)
255 /* set up sinkfp to be an input sink we can ship a message to */
259 *bad_addresses = *good_addresses = 0;
261 if (ctl->mda) /* we have a declared MDA */
263 int length = 0, fromlen = 0, nameslen = 0;
264 char *names = NULL, *before, *after, *from = NULL;
266 ctl->destaddr = "localhost";
268 for (idp = xmit_names; idp; idp = idp->next)
269 if (idp->val.status.mark == XMIT_ACCEPT)
272 length = strlen(ctl->mda);
273 before = xstrdup(ctl->mda);
275 /* get user addresses for %T (or %s for backward compatibility) */
276 if (strstr(before, "%s") || strstr(before, "%T"))
279 * We go through this in order to be able to handle very
280 * long lists of users and (re)implement %s.
283 for (idp = xmit_names; idp; idp = idp->next)
284 if (idp->val.status.mark == XMIT_ACCEPT)
285 nameslen += (strlen(idp->id) + 1); /* string + ' ' */
286 if (*good_addresses = 0)
287 nameslen = strlen(run.postmaster);
289 names = (char *)xmalloc(nameslen + 1); /* account for '\0' */
290 if (*good_addresses == 0)
291 strcpy(names, run.postmaster);
295 for (idp = xmit_names; idp; idp = idp->next)
296 if (idp->val.status.mark == XMIT_ACCEPT)
298 strcat(names, idp->id);
301 names[--nameslen] = '\0'; /* chop trailing space */
304 /* sanitize names in order to contain only harmless shell chars */
308 /* get From address for %F */
309 if (strstr(before, "%F"))
311 from = xstrdup(return_path);
313 /* sanitize from in order to contain *only* harmless shell chars */
316 fromlen = strlen(from);
319 /* do we have to build an mda string? */
324 /* find length of resulting mda string */
326 while (sp = strstr(sp, "%s")) {
327 length += nameslen - 2; /* subtract %s */
331 while (sp = strstr(sp, "%T")) {
332 length += nameslen - 2; /* subtract %T */
336 while (sp = strstr(sp, "%F")) {
337 length += fromlen - 2; /* subtract %F */
341 after = xmalloc(length + 1);
343 /* copy mda source string to after, while expanding %[sTF] */
344 for (dp = after, sp = before; *dp = *sp; dp++, sp++) {
345 if (sp[0] != '%') continue;
347 /* need to expand? BTW, no here overflow, because in
348 ** the worst case (end of string) sp[1] == '\0' */
349 if (sp[1] == 's' || sp[1] == 'T') {
352 sp++; /* position sp over [sT] */
353 dp--; /* adjust dp */
354 } else if (sp[1] == 'F') {
357 sp++; /* position sp over F */
358 dp--; /* adjust dp */
377 if (outlevel >= O_DEBUG)
378 error(0, 0, "about to deliver with: %s", before);
382 * Arrange to run with user's permissions if we're root.
383 * This will initialize the ownership of any files the
384 * MDA creates properly. (The seteuid call is available
385 * under all BSDs and Linux)
388 #endif /* HAVE_SETEUID */
390 sinkfp = popen(before, "w");
395 /* this will fail quietly if we didn't start as root */
397 #endif /* HAVE_SETEUID */
401 error(0, 0, "MDA open failed");
405 sigchld = signal(SIGCHLD, SIG_DFL);
409 char *ap, options[MSGBUFSIZE], addr[128];
411 /* build a connection to the SMTP listener */
412 if ((smtp_open(ctl) == -1))
414 error(0, errno, "SMTP connect to %s failed",
415 ctl->smtphost ? ctl->smtphost : "localhost");
420 * Compute ESMTP options.
423 if (ctl->server.esmtp_options & ESMTP_8BITMIME) {
424 if (ctl->pass8bits || (ctl->mimemsg & MSG_IS_8BIT))
425 strcpy(options, " BODY=8BITMIME");
426 else if (ctl->mimemsg & MSG_IS_7BIT)
427 strcpy(options, " BODY=7BIT");
430 if ((ctl->server.esmtp_options & ESMTP_SIZE) && reallen > 0)
431 sprintf(options + strlen(options), " SIZE=%ld", reallen);
434 * Try to get the SMTP listener to take the Return-Path
435 * address as MAIL FROM . If it won't, fall back on the
436 * calling-user ID. This won't affect replies, which use the
437 * header From address anyway.
439 * RFC 1123 requires that the domain name part of the
440 * MAIL FROM address be "canonicalized", that is a
441 * FQDN or MX but not a CNAME. We'll assume the From
442 * header is already in this form here (it certainly
443 * is if rewrite is on). RFC 1123 is silent on whether
444 * a nonexistent hostname part is considered canonical.
446 * This is a potential problem if the MTAs further upstream
447 * didn't pass canonicalized From/Return-Path lines, *and* the
448 * local SMTP listener insists on them.
450 * None of these error conditions generates bouncemail. Comments
451 * below explain for each case why this is so.
453 ap = (return_path[0]) ? return_path : user;
454 if (SMTP_from(ctl->smtp_socket, ap, options) != SM_OK)
456 int smtperr = atoi(smtp_response);
458 if (str_find(&ctl->antispam, smtperr))
461 * SMTP listener explicitly refuses to deliver mail
462 * coming from this address, probably due to an
463 * anti-spam domain exclusion. Respect this. Don't
464 * try to ship the message, and don't prevent it from
465 * being deleted. Typical values:
467 * 501 = exim's old antispam response
468 * 550 = exim's new antispam response (temporary)
469 * 553 = sendmail 8.8.7's generic REJECT
470 * 571 = sendmail's "unsolicited email refused"
472 * We don't send bouncemail on antispam failures because
473 * we don't want the scumbags to know the address is even
476 SMTP_rset(ctl->smtp_socket); /* required by RFC1870 */
481 * Suppress error message only if the response specifically
482 * meant `excluded for policy reasons'. We *should* see
483 * an error when the return code is less specific.
486 error(0, -1, "SMTP error: %s", smtp_response);
490 case 452: /* insufficient system storage */
492 * Temporary out-of-queue-space condition on the
493 * ESMTP server. Don't try to ship the message,
494 * and suppress deletion so it can be retried on
495 * a future retrieval cycle.
497 * Bouncemail *might* be appropriate here as a delay
498 * notification. But it's not really necessary because
499 * this is not an actual failure, we're very likely to be
500 * able to recover on the next cycle.
502 SMTP_rset(ctl->smtp_socket); /* required by RFC1870 */
503 return(PS_TRANSIENT);
505 case 552: /* message exceeds fixed maximum message size */
506 case 553: /* invalid sending domain */
508 * Permanent no-go condition on the
509 * ESMTP server. Don't try to ship the message,
510 * and allow it to be deleted.
512 * Bouncemail would be appropriate for 552, but in these
513 * latter days 553 usually means a spammer is trying to
514 * cover his tracks. We'd rather deny the scumbags any
515 * feedback that the address is valid.
517 SMTP_rset(ctl->smtp_socket); /* required by RFC1870 */
520 default: /* retry with postmaster's address */
521 if (SMTP_from(ctl->smtp_socket,run.postmaster,options)!=SM_OK)
523 error(0, -1, "SMTP error: %s", smtp_response);
524 return(PS_SMTP); /* should never happen */
530 * Now list the recipient addressees
532 for (idp = xmit_names; idp; idp = idp->next)
533 if (idp->val.status.mark == XMIT_ACCEPT)
535 if (strchr(idp->id, '@'))
536 strcpy(addr, idp->id);
539 snprintf(addr, sizeof(addr)-1, "%s@%s", idp->id, ctl->destaddr);
541 sprintf(addr, "%s@%s", idp->id, ctl->destaddr);
542 #endif /* HAVE_SNPRINTF */
544 if (SMTP_rcpt(ctl->smtp_socket, addr) == SM_OK)
549 idp->val.status.mark = XMIT_ANTISPAM;
551 "SMTP listener doesn't like recipient address `%s'",
555 if (!(*good_addresses))
558 snprintf(addr, sizeof(addr)-1, "%s@%s", run.postmaster, ctl->destaddr);
560 sprintf(addr, "%s@%s", run.postmaster, ctl->destaddr);
561 #endif /* HAVE_SNPRINTF */
563 if (SMTP_rcpt(ctl->smtp_socket, addr) != SM_OK)
565 error(0, 0, "can't even send to %s!", run.postmaster);
570 /* tell it we're ready to send data */
571 SMTP_data(ctl->smtp_socket);
577 void release_sink(struct query *ctl)
578 /* release the per-message output sink, whether it's a pipe or SMTP socket */
585 sinkfp = (FILE *)NULL;
587 signal(SIGCHLD, sigchld);
591 int close_sink(struct query *ctl, flag forward)
592 /* perform end-of-message actions on the current output sink */
598 /* close the delivery pipe, we'll reopen before next message */
602 sinkfp = (FILE *)NULL;
606 signal(SIGCHLD, sigchld);
609 error(0, -1, "MDA exited abnormally or returned nonzero status");
615 /* write message terminator */
616 if (SMTP_eom(ctl->smtp_socket) != SM_OK)
618 error(0, -1, "SMTP listener refused delivery");
626 /* sink.c ends here */