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)
28 #include "fetchmail.h"
32 /* BSD portability hack...I know, this is an ugly place to put it */
33 #if !defined(SIGCHLD) && defined(SIGCLD)
34 #define SIGCHLD SIGCLD
38 #define SMTP_PORT "smtp" /* standard SMTP service port */
40 #define SMTP_PORT 25 /* standard SMTP service port */
43 static int smtp_open(struct query *ctl)
44 /* try to open a socket to the appropriate SMTP server for this query */
46 /* maybe it's time to close the socket in order to force delivery */
47 if (NUM_NONZERO(ctl->batchlimit) && (ctl->smtp_socket != -1) && batchcount++ == ctl->batchlimit)
49 close(ctl->smtp_socket);
50 ctl->smtp_socket = -1;
54 /* if no socket to any SMTP host is already set up, try to open one */
55 if (ctl->smtp_socket == -1)
58 * RFC 1123 requires that the domain name in HELO address is a
59 * "valid principal domain name" for the client host. If we're
60 * running in invisible mode, violate this with malice
61 * aforethought in order to make the Received headers and
64 * In fact this code relies on the RFC1123 requirement that the
65 * SMTP listener must accept messages even if verification of the
66 * HELO name fails (RFC1123 section 5.2.5, paragraph 2).
68 * How we compute the true mailhost name to pass to the
69 * listener doesn't affect behavior on RFC1123- violating
70 * listeners that check for name match; we're going to lose
71 * on those anyway because we can never give them a name
72 * that matches the local machine fetchmail is running on.
73 * What it will affect is the listener's logging.
76 char *id_me = run.invisible ? ctl->server.truename : fetchmailhost;
82 * Run down the SMTP hunt list looking for a server that's up.
83 * Use both explicit hunt entries (value TRUE) and implicit
84 * (default) ones (value FALSE).
87 phase = LISTENER_WAIT;
89 set_timeout(ctl->server.timeout);
90 for (idp = ctl->smtphunt; idp; idp = idp->next)
92 char *cp, *parsed_host;
94 char *portnum = SMTP_PORT;
96 int portnum = SMTP_PORT;
99 xalloca(parsed_host, char *, strlen(idp->id) + 1);
101 ctl->smtphost = idp->id; /* remember last host tried. */
103 strcpy(parsed_host, idp->id);
104 if ((cp = strrchr(parsed_host, '/')))
114 if ((ctl->smtp_socket = SockOpen(parsed_host,portnum,NULL)) == -1)
117 /* first, probe for ESMTP */
118 if (SMTP_ok(ctl->smtp_socket) == SM_OK &&
119 SMTP_ehlo(ctl->smtp_socket, id_me,
120 &ctl->server.esmtp_options) == SM_OK)
124 * RFC 1869 warns that some listeners hang up on a failed EHLO,
125 * so it's safest not to assume the socket will still be good.
127 SockClose(ctl->smtp_socket);
128 ctl->smtp_socket = -1;
130 /* if opening for ESMTP failed, try SMTP */
131 if ((ctl->smtp_socket = SockOpen(parsed_host,portnum,NULL)) == -1)
134 if (SMTP_ok(ctl->smtp_socket) == SM_OK &&
135 SMTP_helo(ctl->smtp_socket, id_me) == SM_OK)
138 SockClose(ctl->smtp_socket);
139 ctl->smtp_socket = -1;
146 * RFC 1123 requires that the domain name part of the
147 * RCPT TO address be "canonicalized", that is a FQDN
148 * or MX but not a CNAME. Some listeners (like exim)
149 * enforce this. Now that we have the actual hostname,
150 * compute what we should canonicalize with.
152 ctl->destaddr = ctl->smtpaddress ? ctl->smtpaddress : ( ctl->smtphost ? ctl->smtphost : "localhost");
154 if (outlevel >= O_DEBUG && ctl->smtp_socket != -1)
155 error(0, 0, "forwarding to %s", ctl->smtphost);
157 return(ctl->smtp_socket);
160 /* these are shared by open_sink and stuffline */
162 static RETSIGTYPE (*sigchld)();
164 int stuffline(struct query *ctl, char *buf)
165 /* ship a line to the given control block's output sink (SMTP server or MDA) */
170 /* The line may contain NUL characters. Find the last char to use
171 * -- the real line termination is the sequence "\n\0".
174 while ((last += strlen(last)) && (last[-1] != '\n'))
177 /* fix message lines that have only \n termination (for qmail) */
180 if (last - 1 == buf || last[-2] != '\r')
189 phase = FORWARDING_WAIT;
192 * SMTP byte-stuffing. We only do this if the protocol does *not*
193 * use .<CR><LF> as EOM. If it does, the server will already have
194 * decorated any . lines it sends back up.
197 if (ctl->server.base_protocol->delimited) /* server has already byte-stuffed */
202 /* writing to SMTP, leave the byte-stuffing in place */;
204 else /* if (!protocol->delimited) -- not byte-stuffed already */
207 SockWrite(ctl->smtp_socket, buf, 1); /* byte-stuff it */
209 /* leave it alone */;
212 /* we may need to strip carriage returns */
217 for (sp = tp = buf; sp < last; sp++)
226 n = fwrite(buf, 1, last - buf, sinkfp);
227 else if (ctl->smtp_socket != -1)
228 n = SockWrite(ctl->smtp_socket, buf, last - buf);
235 static void sanitize(char *s)
236 /* replace unsafe shellchars by an _ */
238 static char *ok_chars = " 1234567890!@%-_=+:,./abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
241 for (cp = s; *(cp += strspn(cp, ok_chars)); /* NO INCREMENT */)
245 int open_sink(struct query *ctl,
247 struct idlist *xmit_names,
249 int *good_addresses, int *bad_addresses)
250 /* set up sinkfp to be an input sink we can ship a message to */
254 *bad_addresses = *good_addresses = 0;
256 if (ctl->mda) /* we have a declared MDA */
258 int length = 0, fromlen = 0, nameslen = 0;
259 char *names = NULL, *before, *after, *from = NULL;
261 ctl->destaddr = "localhost";
263 for (idp = xmit_names; idp; idp = idp->next)
264 if (idp->val.status.mark == XMIT_ACCEPT)
267 length = strlen(ctl->mda);
268 before = xstrdup(ctl->mda);
270 /* get user addresses for %T (or %s for backward compatibility) */
271 if (strstr(before, "%s") || strstr(before, "%T"))
274 * We go through this in order to be able to handle very
275 * long lists of users and (re)implement %s.
278 for (idp = xmit_names; idp; idp = idp->next)
279 if ((idp->val.status.mark == XMIT_ACCEPT))
280 nameslen += (strlen(idp->id) + 1); /* string + ' ' */
281 if ((*good_addresses = 0))
282 nameslen = strlen(run.postmaster);
284 names = (char *)xmalloc(nameslen + 1); /* account for '\0' */
285 if (*good_addresses == 0)
286 strcpy(names, run.postmaster);
290 for (idp = xmit_names; idp; idp = idp->next)
291 if (idp->val.status.mark == XMIT_ACCEPT)
293 strcat(names, idp->id);
296 names[--nameslen] = '\0'; /* chop trailing space */
299 /* sanitize names in order to contain only harmless shell chars */
303 /* get From address for %F */
304 if (strstr(before, "%F"))
306 from = xstrdup(return_path);
308 /* sanitize from in order to contain *only* harmless shell chars */
311 fromlen = strlen(from);
314 /* do we have to build an mda string? */
319 /* find length of resulting mda string */
321 while ((sp = strstr(sp, "%s"))) {
322 length += nameslen - 2; /* subtract %s */
326 while ((sp = strstr(sp, "%T"))) {
327 length += nameslen - 2; /* subtract %T */
331 while ((sp = strstr(sp, "%F"))) {
332 length += fromlen - 2; /* subtract %F */
336 after = xmalloc(length + 1);
338 /* copy mda source string to after, while expanding %[sTF] */
339 for (dp = after, sp = before; (*dp = *sp); dp++, sp++) {
340 if (sp[0] != '%') continue;
342 /* need to expand? BTW, no here overflow, because in
343 ** the worst case (end of string) sp[1] == '\0' */
344 if (sp[1] == 's' || sp[1] == 'T') {
347 sp++; /* position sp over [sT] */
348 dp--; /* adjust dp */
349 } else if (sp[1] == 'F') {
352 sp++; /* position sp over F */
353 dp--; /* adjust dp */
372 if (outlevel >= O_DEBUG)
373 error(0, 0, "about to deliver with: %s", before);
377 * Arrange to run with user's permissions if we're root.
378 * This will initialize the ownership of any files the
379 * MDA creates properly. (The seteuid call is available
380 * under all BSDs and Linux)
383 #endif /* HAVE_SETEUID */
385 sinkfp = popen(before, "w");
390 /* this will fail quietly if we didn't start as root */
392 #endif /* HAVE_SETEUID */
396 error(0, 0, "MDA open failed");
400 sigchld = signal(SIGCHLD, SIG_DFL);
404 char *ap, options[MSGBUFSIZE], addr[128];
406 /* build a connection to the SMTP listener */
407 if ((smtp_open(ctl) == -1))
409 error(0, errno, "SMTP connect to %s failed",
410 ctl->smtphost ? ctl->smtphost : "localhost");
415 * Compute ESMTP options.
418 if (ctl->server.esmtp_options & ESMTP_8BITMIME) {
419 if (ctl->pass8bits || (ctl->mimemsg & MSG_IS_8BIT))
420 strcpy(options, " BODY=8BITMIME");
421 else if (ctl->mimemsg & MSG_IS_7BIT)
422 strcpy(options, " BODY=7BIT");
425 if ((ctl->server.esmtp_options & ESMTP_SIZE) && reallen > 0)
426 sprintf(options + strlen(options), " SIZE=%ld", reallen);
429 * Try to get the SMTP listener to take the Return-Path
430 * address as MAIL FROM . If it won't, fall back on the
431 * calling-user ID. This won't affect replies, which use the
432 * header From address anyway.
434 * RFC 1123 requires that the domain name part of the
435 * MAIL FROM address be "canonicalized", that is a
436 * FQDN or MX but not a CNAME. We'll assume the From
437 * header is already in this form here (it certainly
438 * is if rewrite is on). RFC 1123 is silent on whether
439 * a nonexistent hostname part is considered canonical.
441 * This is a potential problem if the MTAs further upstream
442 * didn't pass canonicalized From/Return-Path lines, *and* the
443 * local SMTP listener insists on them.
445 * None of these error conditions generates bouncemail. Comments
446 * below explain for each case why this is so.
448 ap = (return_path[0]) ? return_path : user;
449 if (SMTP_from(ctl->smtp_socket, ap, options) != SM_OK)
451 int smtperr = atoi(smtp_response);
453 if (str_find(&ctl->antispam, smtperr))
456 * SMTP listener explicitly refuses to deliver mail
457 * coming from this address, probably due to an
458 * anti-spam domain exclusion. Respect this. Don't
459 * try to ship the message, and don't prevent it from
460 * being deleted. Typical values:
462 * 501 = exim's old antispam response
463 * 550 = exim's new antispam response (temporary)
464 * 553 = sendmail 8.8.7's generic REJECT
465 * 571 = sendmail's "unsolicited email refused"
467 * We don't send bouncemail on antispam failures because
468 * we don't want the scumbags to know the address is even
471 SMTP_rset(ctl->smtp_socket); /* required by RFC1870 */
476 * Suppress error message only if the response specifically
477 * meant `excluded for policy reasons'. We *should* see
478 * an error when the return code is less specific.
481 error(0, -1, "SMTP error: %s", smtp_response);
485 case 452: /* insufficient system storage */
487 * Temporary out-of-queue-space condition on the
488 * ESMTP server. Don't try to ship the message,
489 * and suppress deletion so it can be retried on
490 * a future retrieval cycle.
492 * Bouncemail *might* be appropriate here as a delay
493 * notification. But it's not really necessary because
494 * this is not an actual failure, we're very likely to be
495 * able to recover on the next cycle.
497 SMTP_rset(ctl->smtp_socket); /* required by RFC1870 */
498 return(PS_TRANSIENT);
500 case 552: /* message exceeds fixed maximum message size */
501 case 553: /* invalid sending domain */
503 * Permanent no-go condition on the
504 * ESMTP server. Don't try to ship the message,
505 * and allow it to be deleted.
507 * Bouncemail would be appropriate for 552, but in these
508 * latter days 553 usually means a spammer is trying to
509 * cover his tracks. We'd rather deny the scumbags any
510 * feedback that the address is valid.
512 SMTP_rset(ctl->smtp_socket); /* required by RFC1870 */
515 default: /* retry with postmaster's address */
516 if (SMTP_from(ctl->smtp_socket,run.postmaster,options)!=SM_OK)
518 error(0, -1, "SMTP error: %s", smtp_response);
519 return(PS_SMTP); /* should never happen */
525 * Now list the recipient addressees
527 for (idp = xmit_names; idp; idp = idp->next)
528 if (idp->val.status.mark == XMIT_ACCEPT)
530 if (strchr(idp->id, '@'))
531 strcpy(addr, idp->id);
534 snprintf(addr, sizeof(addr)-1, "%s@%s", idp->id, ctl->destaddr);
536 sprintf(addr, "%s@%s", idp->id, ctl->destaddr);
537 #endif /* HAVE_SNPRINTF */
539 if (SMTP_rcpt(ctl->smtp_socket, addr) == SM_OK)
544 idp->val.status.mark = XMIT_ANTISPAM;
546 "SMTP listener doesn't like recipient address `%s'",
550 if (!(*good_addresses))
553 snprintf(addr, sizeof(addr)-1, "%s@%s", run.postmaster, ctl->destaddr);
555 sprintf(addr, "%s@%s", run.postmaster, ctl->destaddr);
556 #endif /* HAVE_SNPRINTF */
558 if (SMTP_rcpt(ctl->smtp_socket, addr) != SM_OK)
560 error(0, 0, "can't even send to %s!", run.postmaster);
561 SMTP_rset(ctl->smtp_socket); /* required by RFC1870 */
566 /* tell it we're ready to send data */
567 SMTP_data(ctl->smtp_socket);
573 void release_sink(struct query *ctl)
574 /* release the per-message output sink, whether it's a pipe or SMTP socket */
581 sinkfp = (FILE *)NULL;
583 signal(SIGCHLD, sigchld);
587 int close_sink(struct query *ctl, flag forward)
588 /* perform end-of-message actions on the current output sink */
594 /* close the delivery pipe, we'll reopen before next message */
598 sinkfp = (FILE *)NULL;
602 signal(SIGCHLD, sigchld);
605 error(0, -1, "MDA exited abnormally or returned nonzero status");
611 /* write message terminator */
612 if (SMTP_eom(ctl->smtp_socket) != SM_OK)
614 error(0, -1, "SMTP listener refused delivery");
622 /* sink.c ends here */