]> Pileus Git - ~andy/fetchmail/blob - sink.c
3abbd6052e24f8a77f4c292bc0a04fd018fabe78
[~andy/fetchmail] / sink.c
1 /*
2  * sink.c -- forwarding/delivery support for fetchmail
3  *
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.
8  *
9  * Copyright 1998 by Eric S. Raymond
10  * For license terms, see the file COPYING in this directory.
11  */
12
13 #include  "config.h"
14 #include  <stdio.h>
15 #include  <errno.h>
16 #include  <string.h>
17 #include  <signal.h>
18 #ifdef HAVE_MEMORY_H
19 #include  <memory.h>
20 #endif /* HAVE_MEMORY_H */
21 #if defined(STDC_HEADERS)
22 #include  <stdlib.h>
23 #endif
24 #if defined(HAVE_UNISTD_H)
25 #include  <unistd.h>
26 #endif
27 #if defined(HAVE_STDARG_H)
28 #include  <stdarg.h>
29 #else
30 #include  <varargs.h>
31 #endif
32 #include  <ctype.h>
33 #include  <langinfo.h>
34
35 #include  "fetchmail.h"
36
37 /* for W* macros after pclose() */
38 #define _USE_BSD
39 #include <sys/types.h>
40 #include <sys/resource.h>
41 #include <sys/wait.h>
42
43 #include  "socket.h"
44 #include  "smtp.h"
45 #include  "i18n.h"
46
47 /* BSD portability hack...I know, this is an ugly place to put it */
48 #if !defined(SIGCHLD) && defined(SIGCLD)
49 #define SIGCHLD SIGCLD
50 #endif
51
52 /* makes the open_sink()/close_sink() pair non-reentrant */
53 static int lmtp_responses;
54
55 void smtp_close(struct query *ctl, int sayquit)
56 /* close the socket to SMTP server */
57 {
58     if (ctl->smtp_socket != -1)
59     {
60         if (sayquit)
61             SMTP_quit(ctl->smtp_socket, ctl->smtphostmode);
62         SockClose(ctl->smtp_socket);
63         ctl->smtp_socket = -1;
64     }
65     batchcount = 0;
66 }
67
68 static void smtp_rset(struct query *ctl)
69 /* reset the mail transaction */
70 {
71     if (SMTP_rset(ctl->smtp_socket, ctl->smtphostmode) == SM_UNRECOVERABLE)
72     {
73         /* close the bad connection. fetchmail will reconnect for the
74          * next mail */
75         smtp_close(ctl, 0);
76     }
77 }
78
79 int smtp_setup(struct query *ctl)
80 /* try to open a socket to the appropriate SMTP server for this query */ 
81 {
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)
84     {
85         smtp_close(ctl, 1);
86         last_smtp_ok = 0;
87     }
88     if (NUM_NONZERO(ctl->batchlimit)) {
89         if (batchcount == ctl->batchlimit)
90             smtp_close(ctl, 1);
91         batchcount++;
92     }
93
94     /* if no socket to any SMTP host is already set up, try to open one */
95     if (ctl->smtp_socket == -1) 
96     {
97         /* 
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.
103          *
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).
107          *
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.
114          */
115         struct idlist   *idp;
116         const char *id_me = run.invisible ? ctl->server.truename : fetchmailhost;
117         int oldphase;
118         char *parsed_host = NULL;
119
120         errno = 0;
121
122         /*
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).
126          */
127         oldphase = phase;
128         phase = LISTENER_WAIT;
129
130         set_timeout(ctl->server.timeout);
131         for (idp = ctl->smtphunt; idp; idp = idp->next)
132         {
133             char        *cp;
134             const char  *portnum = SMTP_PORT;
135
136             ctl->smtphost = idp->id;  /* remember last host tried. */
137             if (ctl->smtphost[0]=='/')
138             {
139                 ctl->smtphostmode = LMTP_MODE;
140                 xfree(parsed_host);
141                 if ((ctl->smtp_socket = UnixOpen(ctl->smtphost))==-1)
142                     continue;
143             }
144             else
145             {
146                 ctl->smtphostmode = ctl->listener;
147                 parsed_host = xstrdup(idp->id);
148                 if ((cp = strrchr(parsed_host, '/')))
149                 {
150                     *cp++ = 0;
151                     if (cp[0])
152                         portnum = cp;
153                 }
154                 if ((ctl->smtp_socket = SockOpen(parsed_host,portnum,
155                                 ctl->server.plugout, &ai1)) == -1)
156                 {
157                     xfree(parsed_host);
158                     continue;
159                 }
160             }
161
162             /* return immediately for ODMR */
163             if (ctl->server.protocol == P_ODMR)
164             {
165                 set_timeout(0);
166                 phase = oldphase;
167                 xfree(parsed_host);
168                 return(ctl->smtp_socket); /* success */
169             }
170
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)
176                 break;  /* success */
177
178             /*
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.
181              */
182             smtp_close(ctl, 0);
183
184             /* if opening for ESMTP failed, try SMTP */
185             if (ctl->smtphost[0]=='/')
186             {
187                 if ((ctl->smtp_socket = UnixOpen(ctl->smtphost))==-1)
188                     continue;
189             }
190             else
191             {
192                 if ((ctl->smtp_socket = SockOpen(parsed_host,portnum,
193                                 ctl->server.plugout, &ai1)) == -1)
194                 {
195                     xfree(parsed_host);
196                     continue;
197                 }
198             }
199
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)
202                 break;  /* success */
203
204             smtp_close(ctl, 0);
205         }
206         set_timeout(0);
207         phase = oldphase;
208
209         /*
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.
215          */
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. */
224         else
225             ctl->destaddr = xstrdup("localhost");
226         xfree(parsed_host);
227     }
228     /* end if (ctl->smtp_socket == -1) */
229
230     if (outlevel >= O_DEBUG && ctl->smtp_socket != -1)
231         report(stdout, GT_("forwarding to %s\n"), ctl->smtphost);
232
233     return(ctl->smtp_socket);
234 }
235
236 static void sanitize(char *s)
237 /* replace ' by _ */
238 {
239     char *cp;
240
241     for (cp = s; (cp = strchr (cp, '\'')); cp++)
242         *cp = '_';
243 }
244
245 char *rcpt_address(struct query *ctl, const char *id,
246                           int usesmtpname)
247 {
248     static char addr[HOSTLEN+USERNAMELEN+1];
249     if (strchr(id, '@'))
250     {
251         snprintf(addr, sizeof (addr), "%s", id);
252     }
253     else if (usesmtpname && ctl->smtpname)
254     {
255         snprintf(addr, sizeof (addr), "%s", ctl->smtpname);
256     }
257     else
258     {
259         snprintf(addr, sizeof (addr), "%s@%s", id, ctl->destaddr);
260     }
261     return addr;
262 }
263
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 */
268 {
269     char daemon_name[15 + HOSTLEN] = "MAILER-DAEMON@";
270     char boundary[BUFSIZ];
271     const char *bounce_to;
272     int sock;
273     static char *fqdn_of_host = NULL;
274     const char *md1 = "MAILER-DAEMON", *md2 = "MAILER-DAEMON@";
275
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)
281         return(TRUE);
282
283     bounce_to = (run.bouncemail ? msg->return_path : run.postmaster);
284
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
288                                         lose the NDN here */
289     strlcat(daemon_name, fqdn_of_host, sizeof(daemon_name));
290
291     /* we need only SMTP for this purpose */
292     /* XXX FIXME: hardcoding localhost is nonsense if smtphost can be
293      * configured */
294     if ((sock = SockOpen("localhost", SMTP_PORT, NULL, &ai1)) == -1)
295         return(FALSE);
296
297     if (SMTP_ok(sock, SMTP_MODE, TIMEOUT_STARTSMTP) != SM_OK)
298     {
299         SockClose(sock);
300         return FALSE;
301     }
302
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)
307     {
308         SMTP_quit(sock, SMTP_MODE);
309         SockClose(sock);
310         return(FALSE);
311     }
312
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));
316
317     if (outlevel >= O_VERBOSE)
318         report(stdout, GT_("SMTP: (bounce-message body)\n"));
319     else
320         /* this will usually go to sylog... */
321         report(stderr, GT_("mail from %s bounced to %s\n"),
322                daemon_name, bounce_to);
323
324
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");
332
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");
345
346     if (nerrors)
347     {
348         struct idlist   *idp;
349         int             nusers;
350         
351         nusers = 0;
352         for (idp = msg->recipients; idp; idp = idp->next)
353         {
354             if (idp->val.status.mark == userclass)
355             {
356                 char    *error;
357                 SockPrintf(sock, "%s\r\n", rcpt_address (ctl, idp->id, 1));
358                 
359                 if (nerrors == 1) error = errors[0];
360                 else if (nerrors <= nusers)
361                 {
362                     SockPrintf(sock, "Internal error: SMTP error count doesn't match number of recipients.\r\n");
363                     break;
364                 }
365                 else error = errors[nusers++];
366                         
367                 SockPrintf(sock, "   SMTP error: %s\r\n\r\n", error);
368             }
369         }
370     
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);
376
377         nusers = 0;
378         for (idp = msg->recipients; idp; idp = idp->next)
379             if (idp->val.status.mark == userclass)
380             {
381                 char    *error;
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");
388
389                 if (nerrors == 1)
390                     /* one error applies to all users */
391                     error = errors[0];
392                 else if (nerrors <= nusers)
393                 {
394                     SockPrintf(sock, "Internal error: SMTP error count doesn't match number of recipients.\r\n");
395                     break;
396                 }
397                 else
398                     /* errors correspond 1-1 to selected users */
399                     error = errors[nusers++];
400                 
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]));
406                 else
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);
410             }
411         SockPrintf(sock, "\r\n");
412     }
413
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");
418     if (msg->headers)
419     {
420         SockWrite(sock, msg->headers, strlen(msg->headers));
421         SockPrintf(sock, "\r\n");
422     }
423     SockPrintf(sock, "--%s--\r\n", boundary); 
424
425     if (SMTP_eom(sock, SMTP_MODE) != SM_OK
426             || SMTP_quit(sock, SMTP_MODE) != SM_OK)
427     {
428         SockClose(sock);
429         return(FALSE);
430     }
431
432     SockClose(sock);
433
434     return(TRUE);
435 }
436
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) */
441 {
442     int smtperr = atoi(smtp_response);
443     char *responses[1];
444     struct idlist *walk;
445     int found = 0;
446
447     responses[0] = xstrdup(smtp_response);
448
449 #ifdef __UNUSED__
450     /*
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
454      * valid recipients.
455      */
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 */
460
461     /*
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).
468      */
469     for( walk = ctl->antispam; walk; walk = walk->next )
470         if ( walk->val.status.num == smtperr ) 
471         { 
472                 found=1;
473                 break;
474         }
475
476     /* if (str_find(&ctl->antispam, smtperr)) */
477     if ( found )
478     {
479         /*
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
488          * config option).
489          *
490          * Default values:
491          *
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.
496          *
497          */
498         if (run.spambounce)
499         {
500             char rejmsg[160];
501             snprintf(rejmsg, sizeof(rejmsg),
502                     "spam filter or virus scanner rejected message because:\r\n"
503                     "%s\r\n", responses[0]);
504
505             send_bouncemail(ctl, msg, XMIT_ACCEPT,
506                     rejmsg, 1, responses);
507         }
508         free(responses[0]);
509         return(PS_REFUSED);
510     }
511
512     /*
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.
516      */
517     if (smtperr >= 400)
518         report(stderr, GT_("%cMTP error: %s\n"), 
519               ctl->smtphostmode,
520               responses[0]);
521
522     switch (smtperr)
523     {
524     case 552: /* message exceeds fixed maximum message size */
525         /*
526          * Permanent no-go condition on the
527          * ESMTP server.  Don't try to ship the message, 
528          * and allow it to be deleted.
529          */
530         if (run.bouncemail)
531             send_bouncemail(ctl, msg, XMIT_ACCEPT,
532                         "This message was too large (SMTP error 552).\r\n", 
533                         1, responses);
534         free(responses[0]);
535         return(PS_REFUSED);
536   
537     case 553: /* invalid sending domain */
538         /*
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.
544          */
545 #ifdef __DONT_FEED_THE_SPAMMERS__
546         if (run.bouncemail)
547             send_bouncemail(ctl, msg, XMIT_ACCEPT,
548                         "Invalid address in MAIL FROM (SMTP error 553).\r\n", 
549                         1, responses);
550 #endif /* __DONT_FEED_THE_SPAMMERS__ */
551         free(responses[0]);
552         return(PS_REFUSED);
553
554     case 530: /* must issue STARTTLS error */
555         /*
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.
559          */
560         if (outlevel > O_SILENT)
561                 report_complete(stdout,
562                                 GT_("SMTP server requires STARTTLS, keeping message.\n"));
563         free(responses[0]);
564         return(PS_TRANSIENT);
565
566     default:
567         /* bounce non-transient errors back to the sender */
568         if (smtperr >= 500 && smtperr <= 599)
569         {
570             if (run.bouncemail)
571                 send_bouncemail(ctl, msg, XMIT_ACCEPT,
572                                 "General SMTP/ESMTP error.\r\n", 
573                                 1, responses);
574             free(responses[0]);
575             return(PS_REFUSED);
576         }
577         /*
578          * We're going to end up here on 4xx errors, like:
579          *
580          * 451: temporarily unable to identify sender (exim)
581          * 452: temporary out-of-queue-space condition on the ESMTP server.
582          *
583          * These are temporary errors.  Don't try to ship the message,
584          * and suppress deletion so it can be retried on a future
585          * retrieval cycle.
586          *
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.
593          */
594         free(responses[0]);
595         return(PS_TRANSIENT);
596     }
597 }
598
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 */
604 {
605     int smtperr = atoi(smtp_response);
606
607     (void)msg;
608
609     if (str_find(&ctl->antispam, smtperr))
610     {
611         if (run.spambounce)
612          return(PS_SUCCESS);
613         return(PS_REFUSED);
614     }
615
616     if (smtperr >= 400)
617         report(stderr, GT_("%cMTP error: %s\n"), 
618               ctl->smtphostmode,
619               smtp_response);
620
621     switch (smtperr)
622     {
623     case 552: /* message exceeds fixed maximum message size */
624         if (run.bouncemail)
625             return(PS_SUCCESS);
626         return(PS_REFUSED);
627
628     case 553: /* invalid sending domain */
629 #ifdef __DONT_FEED_THE_SPAMMERS__
630         if (run.bouncemail)
631             return(PS_SUCCESS);
632 #endif /* __DONT_FEED_THE_SPAMMERS__ */
633         return(PS_REFUSED);
634
635     default:
636         /* bounce non-transient errors back to the sender */
637         if (smtperr >= 500 && smtperr <= 599)
638             return(PS_SUCCESS);
639         return(PS_TRANSIENT);
640     }
641 }
642
643 /* these are shared by open_sink and stuffline */
644 static FILE *sinkfp;
645
646 int stuffline(struct query *ctl, char *buf)
647 /* ship a line to the given control block's output sink (SMTP server or MDA) */
648 {
649     int n, oldphase;
650     char *last;
651
652     if (!buf)
653         return -1;
654
655     /* The line may contain NUL characters. Find the last char to use
656      * -- the real line termination is the sequence "\n\0".
657      */
658     last = buf + 1; /* last[-1] must be valid! */
659     while ((last += strlen(last)) && (last[-1] != '\n'))
660         last++;
661
662     /* fix message lines that have only \n termination (for qmail) */
663     if (ctl->forcecr)
664     {
665         if (last - 1 == buf || last[-2] != '\r')
666         {
667             last[-1] = '\r';
668             *last++  = '\n';
669             *last    = '\0';
670         }
671     }
672
673     oldphase = phase;
674     phase = FORWARDING_WAIT;
675
676     /*
677      * SMTP byte-stuffing.  We only do this if the protocol does *not*
678      * use .<CR><LF> as EOM.  If it does, the server will already have
679      * decorated any . lines it sends back up.
680      */
681     if (*buf == '.')
682     {
683         if (ctl->server.base_protocol->delimited)       /* server has already byte-stuffed */
684         {
685             if (ctl->mda) {
686                 /* writing to MDA, undo byte-stuffing */
687                 ++buf;
688             } else {
689                 /* writing to SMTP, leave the byte-stuffing in place */;
690             }
691         }
692         else /* if (!protocol->delimited)       -- not byte-stuffed already */
693         {
694             /* byte-stuff it */
695             if (!ctl->mda)  {
696                 if (!ctl->bsmtp) {
697                     n = SockWrite(ctl->smtp_socket, buf, 1);
698                 } else {
699                     n = fwrite(buf, 1, 1, sinkfp);
700                     if (ferror(sinkfp)) n = -1;
701                 }
702                 if (n < 0)
703                     return n;
704             }
705         }
706     }
707
708     /* we may need to strip carriage returns */
709     if (ctl->stripcr)
710     {
711         char    *sp, *tp;
712
713         for (sp = tp = buf; sp < last; sp++)
714             if (*sp != '\r')
715                 *tp++ =  *sp;
716         *tp = '\0';
717         last = tp;
718     }
719
720     n = 0;
721     if (ctl->mda || ctl->bsmtp) {
722         n = fwrite(buf, 1, last - buf, sinkfp);
723         if (ferror(sinkfp)) n = -1;
724     } else if (ctl->smtp_socket != -1)
725         n = SockWrite(ctl->smtp_socket, buf, last - buf);
726
727     phase = oldphase;
728
729     return(n);
730 }
731
732 static int open_bsmtp_sink(struct query *ctl, struct msgblk *msg,
733               int *good_addresses, int *bad_addresses)
734 /* open a BSMTP stream */
735 {
736     struct      idlist *idp;
737     int         need_anglebrs;
738
739     if (strcmp(ctl->bsmtp, "-") == 0)
740         sinkfp = stdout;
741     else
742         sinkfp = fopen(ctl->bsmtp, "a");
743
744     if (!sinkfp || ferror(sinkfp)) {
745         report(stderr, GT_("BSMTP file open failed: %s\n"), 
746                 strerror(errno));
747         return(PS_BSMTP);
748     }
749
750     /* see the ap computation under the SMTP branch */
751     need_anglebrs = (msg->return_path[0] != '<');
752     fprintf(sinkfp,
753             "MAIL FROM:%s%s%s",
754             need_anglebrs ? "<" : "",
755             (msg->return_path[0]) ? msg->return_path : user,
756             need_anglebrs ? ">" : "");
757
758     if (ctl->pass8bits || (ctl->mimemsg & MSG_IS_8BIT))
759         fputs(" BODY=8BITMIME", sinkfp);
760     else if (ctl->mimemsg & MSG_IS_7BIT)
761         fputs(" BODY=7BIT", sinkfp);
762
763     /* exim's BSMTP processor does not handle SIZE */
764     /* fprintf(sinkfp, " SIZE=%d", msg->reallen); */
765
766     fprintf(sinkfp, "\r\n");
767
768     /*
769      * RFC 1123 requires that the domain name part of the
770      * RCPT TO address be "canonicalized", that is a FQDN
771      * or MX but not a CNAME.  Some listeners (like exim)
772      * enforce this.  Now that we have the actual hostname,
773      * compute what we should canonicalize with.
774      */
775     xfree(ctl->destaddr);
776     ctl->destaddr = xstrdup(ctl->smtpaddress ? ctl->smtpaddress : "localhost");
777
778     *bad_addresses = 0;
779     for (idp = msg->recipients; idp; idp = idp->next)
780         if (idp->val.status.mark == XMIT_ACCEPT)
781         {
782             fprintf(sinkfp, "RCPT TO:<%s>\r\n",
783                 rcpt_address (ctl, idp->id, 1));
784             (*good_addresses)++;
785         }
786
787     fputs("DATA\r\n", sinkfp);
788
789     if (fflush(sinkfp) || ferror(sinkfp))
790     {
791         report(stderr, GT_("BSMTP preamble write failed: %s.\n"), strerror(errno));
792         return(PS_BSMTP);
793     }
794
795     return(PS_SUCCESS);
796 }
797
798 /* this is experimental and will be removed if double bounces are reported */
799 #define EXPLICIT_BOUNCE_ON_BAD_ADDRESS
800
801
802 static const char *is_quad(const char *q)
803 /* Check if the string passed in points to what could be one quad of a
804  * dotted-quad IP address.  Requirements are that the string is not a
805  * NULL pointer, begins with a period (which is skipped) or a digit
806  * and ends with a period or a NULL.  If these requirements are met, a
807  * pointer to the last character (the period or the NULL character) is
808  * returned; otherwise NULL.
809  */
810 {
811   const char *r;
812   
813   if (!q || !*q)
814     return NULL;
815   if (*q == '.')
816     q++;
817   for(r=q;isdigit((unsigned char)*r);r++)
818     ;
819   if ( ((*r) && (*r != '.')) || ((r-q) < 1) || ((r-q)>3) )
820     return NULL;
821   /* Make sure quad is < 255 */
822   if ( (r-q) == 3)
823   {
824     if (*q > '2')
825       return NULL;
826     else if (*q == '2')
827     {
828       if (*(q+1) > '5')
829         return NULL;
830       else if (*(q+1) == '5')
831       {
832         if (*(q+2) > '5')
833           return NULL;
834       }
835     }
836   }
837   return r;
838 }
839
840 static int is_dottedquad(const char *hostname)
841 /* Returns a true value if the passed in string looks like an IP
842  *  address in dotted-quad form, and a false value otherwise.
843  */
844
845 {
846   return ((hostname=is_quad(is_quad(is_quad(is_quad(hostname))))) != NULL) &&
847     (*hostname == '\0');
848 }
849
850 static int open_smtp_sink(struct query *ctl, struct msgblk *msg,
851               int *good_addresses, int *bad_addresses /* this must be signed, to prevent endless loop in from_addresses */)
852 /* open an SMTP stream */
853 {
854     const char  *ap;
855     struct      idlist *idp;
856     char                options[MSGBUFSIZE]; 
857     char                addr[HOSTLEN+USERNAMELEN+1];
858 #ifdef EXPLICIT_BOUNCE_ON_BAD_ADDRESS
859     char                **from_responses;
860 #endif /* EXPLICIT_BOUNCE_ON_BAD_ADDRESS */
861     int         total_addresses;
862     int         force_transient_error = 0;
863     int         smtp_err;
864
865     /*
866      * Compute ESMTP options.
867      */
868     options[0] = '\0';
869     if (ctl->server.esmtp_options & ESMTP_8BITMIME) {
870          if (ctl->pass8bits || (ctl->mimemsg & MSG_IS_8BIT))
871             strcpy(options, " BODY=8BITMIME");
872          else if (ctl->mimemsg & MSG_IS_7BIT)
873             strcpy(options, " BODY=7BIT");
874     }
875
876     if ((ctl->server.esmtp_options & ESMTP_SIZE) && msg->reallen > 0)
877         sprintf(options + strlen(options), " SIZE=%d", msg->reallen);
878
879     /*
880      * Try to get the SMTP listener to take the Return-Path
881      * address as MAIL FROM.  If it won't, fall back on the
882      * remotename and mailserver host.  This won't affect replies,
883      * which use the header From address anyway; the MAIL FROM
884      * address is a place for the SMTP listener to send
885      * bouncemail.  The point is to guarantee a FQDN in the MAIL
886      * FROM line -- some SMTP listeners, like smail, become
887      * unhappy otherwise.
888      *
889      * RFC 1123 requires that the domain name part of the
890      * MAIL FROM address be "canonicalized", that is a
891      * FQDN or MX but not a CNAME.  We'll assume the Return-Path
892      * header is already in this form here (it certainly
893      * is if rewrite is on).  RFC 1123 is silent on whether
894      * a nonexistent hostname part is considered canonical.
895      *
896      * This is a potential problem if the MTAs further upstream
897      * didn't pass canonicalized From/Return-Path lines, *and* the
898      * local SMTP listener insists on them. 
899      *
900      * Handle the case where an upstream MTA is setting a return
901      * path equal to "@".  Ghod knows why anyone does this, but 
902      * it's been reported to happen in mail from Amazon.com and
903      * Motorola.
904      *
905      * Also, if the hostname is a dotted quad, wrap it in square brackets.
906      * Apparently this is required by RFC2821, section 4.1.3.
907      */
908     if (!msg->return_path[0] || (msg->return_path[0] == '@'))
909     {
910       if (strchr(ctl->remotename,'@') || strchr(ctl->remotename,'!'))
911       {
912         snprintf(addr, sizeof(addr), "%s", ctl->remotename);
913       }
914       else if (is_dottedquad(ctl->server.truename))
915       {
916         snprintf(addr, sizeof(addr), "%s@[%s]", ctl->remotename,
917                 ctl->server.truename);
918       }
919       else
920       {
921         snprintf(addr, sizeof(addr),
922               "%s@%s", ctl->remotename, ctl->server.truename);
923       }
924         ap = addr;
925     }
926     else if (strchr(msg->return_path,'@') || strchr(msg->return_path,'!'))
927         ap = msg->return_path;
928     /* in case Return-Path was "<>" we want to preserve that */
929     else if (strcmp(msg->return_path,"<>") == 0)
930         ap = msg->return_path;
931     else                /* in case Return-Path existed but was local */
932     {
933       if (is_dottedquad(ctl->server.truename))
934       {
935         snprintf(addr, sizeof(addr), "%s@[%s]", msg->return_path,
936                 ctl->server.truename);
937       }
938       else
939       {
940         snprintf(addr, sizeof(addr), "%s@%s",
941                 msg->return_path, ctl->server.truename);
942       }
943         ap = addr;
944     }
945
946     if ((smtp_err = SMTP_from(ctl->smtp_socket, ctl->smtphostmode,
947                     ap, options)) == SM_UNRECOVERABLE)
948     {
949         smtp_close(ctl, 0);
950         return(PS_TRANSIENT);
951     }
952     if (smtp_err != SM_OK)
953     {
954         int err = handle_smtp_report(ctl, msg); /* map to PS_TRANSIENT or PS_REFUSED */
955
956         smtp_rset(ctl);    /* stay on the safe side */
957         return(err);
958     }
959
960     /*
961      * Now list the recipient addressees
962      */
963     total_addresses = 0;
964     for (idp = msg->recipients; idp; idp = idp->next)
965         total_addresses++;
966 #ifdef EXPLICIT_BOUNCE_ON_BAD_ADDRESS
967     from_responses = (char **)xmalloc(sizeof(char *) * total_addresses);
968 #endif /* EXPLICIT_BOUNCE_ON_BAD_ADDRESS */
969     for (idp = msg->recipients; idp; idp = idp->next)
970         if (idp->val.status.mark == XMIT_ACCEPT)
971         {
972             const char *address;
973             address = rcpt_address (ctl, idp->id, 1);
974             if ((smtp_err = SMTP_rcpt(ctl->smtp_socket, ctl->smtphostmode,
975                             address)) == SM_UNRECOVERABLE)
976             {
977                 smtp_close(ctl, 0);
978 transient:
979 #ifdef EXPLICIT_BOUNCE_ON_BAD_ADDRESS
980                 while (*bad_addresses)
981                     free(from_responses[--*bad_addresses]);
982                 free(from_responses);
983 #endif /* EXPLICIT_BOUNCE_ON_BAD_ADDRESS */
984                 return(PS_TRANSIENT);
985             }
986             if (smtp_err == SM_OK)
987                 (*good_addresses)++;
988             else
989             {
990                 switch (handle_smtp_report_without_bounce(ctl, msg))
991                 {
992                     case PS_TRANSIENT:
993                     force_transient_error = 1;
994                     break;
995
996                     case PS_SUCCESS:
997 #ifdef EXPLICIT_BOUNCE_ON_BAD_ADDRESS
998                     from_responses[*bad_addresses] = xstrdup(smtp_response);
999 #endif /* EXPLICIT_BOUNCE_ON_BAD_ADDRESS */
1000
1001                     (*bad_addresses)++;
1002                     idp->val.status.mark = XMIT_RCPTBAD;
1003                     if (outlevel >= O_VERBOSE)
1004                         report(stderr,
1005                               GT_("%cMTP listener doesn't like recipient address `%s'\n"),
1006                               ctl->smtphostmode, address);
1007                     break;
1008
1009                     case PS_REFUSED:
1010                     if (outlevel >= O_VERBOSE)
1011                         report(stderr,
1012                               GT_("%cMTP listener doesn't really like recipient address `%s'\n"),
1013                               ctl->smtphostmode, address);
1014                     break;
1015                 }
1016             }
1017         }
1018
1019     if (force_transient_error) {
1020             /* do not risk dataloss due to overengineered multidrop
1021              * crap. If one of the recipients returned PS_TRANSIENT,
1022              * we return exactly that.
1023              */
1024             smtp_rset(ctl);        /* required by RFC1870 */
1025             goto transient;
1026     }
1027 #ifdef EXPLICIT_BOUNCE_ON_BAD_ADDRESS
1028     /*
1029      * This should not be necessary, because the SMTP listener itself
1030      * should generate a bounce for the bad address.
1031      *
1032      * XXX FIXME 2006-01-19: is this comment true? I don't think
1033      * it is, because the SMTP listener isn't required to accept bogus
1034      * messages. There appears to be general SMTP<->MDA and
1035      * responsibility confusion.
1036      */
1037     if (*bad_addresses)
1038         send_bouncemail(ctl, msg, XMIT_RCPTBAD,
1039                         "Some addresses were rejected by the MDA fetchmail forwards to.\r\n",
1040                         *bad_addresses, from_responses);
1041     while (*bad_addresses)
1042         free(from_responses[--*bad_addresses]);
1043     free(from_responses);
1044 #endif /* EXPLICIT_BOUNCE_ON_BAD_ADDRESS */
1045
1046     /*
1047      * It's tempting to do local notification only if bouncemail was
1048      * insufficient -- that is, to add && total_addresses > *bad_addresses
1049      * to the test here.  The problem with this theory is that it would
1050      * make initial diagnosis of a broken multidrop configuration very
1051      * hard -- most single-recipient messages would just invisibly bounce.
1052      */
1053     if (!(*good_addresses)) 
1054     {
1055         if (!run.postmaster[0])
1056         {
1057             if (outlevel >= O_VERBOSE)
1058                 report(stderr, GT_("no address matches; no postmaster set.\n"));
1059             smtp_rset(ctl);     /* required by RFC1870 */
1060             return(PS_REFUSED);
1061         }
1062         if ((smtp_err = SMTP_rcpt(ctl->smtp_socket, ctl->smtphostmode,
1063                 rcpt_address (ctl, run.postmaster, 0))) == SM_UNRECOVERABLE)
1064         {
1065             smtp_close(ctl, 0);
1066             return(PS_TRANSIENT);
1067         }
1068         if (smtp_err != SM_OK)
1069         {
1070             report(stderr, GT_("can't even send to %s!\n"), run.postmaster);
1071             smtp_rset(ctl);     /* required by RFC1870 */
1072             return(PS_REFUSED);
1073         }
1074
1075         if (outlevel >= O_VERBOSE)
1076             report(stderr, GT_("no address matches; forwarding to %s.\n"), run.postmaster);
1077     }
1078
1079     /* 
1080      * Tell the listener we're ready to send data.
1081      * Some listeners (like zmailer) may return antispam errors here.
1082      */
1083     if ((smtp_err = SMTP_data(ctl->smtp_socket, ctl->smtphostmode))
1084             == SM_UNRECOVERABLE)
1085     {
1086         smtp_close(ctl, 0);
1087         return(PS_TRANSIENT);
1088     }
1089     if (smtp_err != SM_OK)
1090     {
1091         int err = handle_smtp_report(ctl, msg);
1092         smtp_rset(ctl);    /* stay on the safe side */
1093         return(err);
1094     }
1095
1096     /*
1097      * We need to stash this away in order to know how many
1098      * response lines to expect after the LMTP end-of-message.
1099      */
1100     lmtp_responses = *good_addresses;
1101
1102     return(PS_SUCCESS);
1103 }
1104
1105 static int open_mda_sink(struct query *ctl, struct msgblk *msg,
1106               int *good_addresses, int *bad_addresses)
1107 /* open a stream to a local MDA */
1108 {
1109 #ifdef HAVE_SETEUID
1110     uid_t orig_uid;
1111 #endif /* HAVE_SETEUID */
1112     struct      idlist *idp;
1113     int length = 0, fromlen = 0, nameslen = 0;
1114     char        *names = NULL, *before, *after, *from = NULL;
1115
1116     (void)bad_addresses;
1117     xfree(ctl->destaddr);
1118     ctl->destaddr = xstrdup("localhost");
1119
1120     for (idp = msg->recipients; idp; idp = idp->next)
1121         if (idp->val.status.mark == XMIT_ACCEPT)
1122             (*good_addresses)++;
1123
1124     length = strlen(ctl->mda);
1125     before = xstrdup(ctl->mda);
1126
1127     /* get user addresses for %T (or %s for backward compatibility) */
1128     if (strstr(before, "%s") || strstr(before, "%T"))
1129     {
1130         /*
1131          * We go through this in order to be able to handle very
1132          * long lists of users and (re)implement %s.
1133          */
1134         nameslen = 0;
1135         for (idp = msg->recipients; idp; idp = idp->next)
1136             if ((idp->val.status.mark == XMIT_ACCEPT))
1137                 nameslen += (strlen(idp->id) + 1);      /* string + ' ' */
1138         if ((*good_addresses == 0))
1139             nameslen = strlen(run.postmaster);
1140
1141         names = (char *)xmalloc(nameslen + 1);  /* account for '\0' */
1142         if (*good_addresses == 0)
1143             strcpy(names, run.postmaster);
1144         else
1145         {
1146             names[0] = '\0';
1147             for (idp = msg->recipients; idp; idp = idp->next)
1148                 if (idp->val.status.mark == XMIT_ACCEPT)
1149                 {
1150                     strcat(names, idp->id);
1151                     strcat(names, " ");
1152                 }
1153             names[--nameslen] = '\0';   /* chop trailing space */
1154         }
1155
1156         sanitize(names);
1157     }
1158
1159     /* get From address for %F */
1160     if (strstr(before, "%F"))
1161     {
1162         from = xstrdup(msg->return_path);
1163
1164         sanitize(from);
1165
1166         fromlen = strlen(from);
1167     }
1168
1169     /* do we have to build an mda string? */
1170     if (names || from) 
1171     {           
1172         char    *sp, *dp;
1173
1174         /* find length of resulting mda string */
1175         sp = before;
1176         while ((sp = strstr(sp, "%s"))) {
1177             length += nameslen; /* subtract %s and add '' */
1178             sp += 2;
1179         }
1180         sp = before;
1181         while ((sp = strstr(sp, "%T"))) {
1182             length += nameslen; /* subtract %T and add '' */
1183             sp += 2;
1184         }
1185         sp = before;
1186         while ((sp = strstr(sp, "%F"))) {
1187             length += fromlen;  /* subtract %F and add '' */
1188             sp += 2;
1189         }
1190
1191         after = (char *)xmalloc(length + 1);
1192
1193         /* copy mda source string to after, while expanding %[sTF] */
1194         for (dp = after, sp = before; (*dp = *sp); dp++, sp++) {
1195             if (sp[0] != '%')   continue;
1196
1197             /* need to expand? BTW, no here overflow, because in
1198             ** the worst case (end of string) sp[1] == '\0' */
1199             if (sp[1] == 's' || sp[1] == 'T') {
1200                 *dp++ = '\'';
1201                 strcpy(dp, names);
1202                 dp += nameslen;
1203                 *dp++ = '\'';
1204                 sp++;   /* position sp over [sT] */
1205                 dp--;   /* adjust dp */
1206             } else if (sp[1] == 'F') {
1207                 *dp++ = '\'';
1208                 strcpy(dp, from);
1209                 dp += fromlen;
1210                 *dp++ = '\'';
1211                 sp++;   /* position sp over F */
1212                 dp--;   /* adjust dp */
1213             }
1214         }
1215
1216         if (names) {
1217             free(names);
1218             names = NULL;
1219         }
1220         if (from) {
1221             free(from);
1222             from = NULL;
1223         }
1224
1225         free(before);
1226
1227         before = after;
1228     }
1229
1230
1231     if (outlevel >= O_DEBUG)
1232         report(stdout, GT_("about to deliver with: %s\n"), before);
1233
1234 #ifdef HAVE_SETEUID
1235     /*
1236      * Arrange to run with user's permissions if we're root.
1237      * This will initialize the ownership of any files the
1238      * MDA creates properly.  (The seteuid call is available
1239      * under all BSDs and Linux)
1240      */
1241     orig_uid = getuid();
1242     if (seteuid(ctl->uid)) {
1243         report(stderr, GT_("Cannot switch effective user id to %ld: %s\n"), (long)ctl->uid, strerror(errno));
1244         return PS_IOERR;
1245     }
1246 #endif /* HAVE_SETEUID */
1247
1248     sinkfp = popen(before, "w");
1249     free(before);
1250     before = NULL;
1251
1252 #ifdef HAVE_SETEUID
1253     /* this will fail quietly if we didn't start as root */
1254     if (seteuid(orig_uid)) {
1255         report(stderr, GT_("Cannot switch effective user id back to original %ld: %s\n"), (long)orig_uid, strerror(errno));
1256         return PS_IOERR;
1257     }
1258 #endif /* HAVE_SETEUID */
1259
1260     if (!sinkfp)
1261     {
1262         report(stderr, GT_("MDA open failed\n"));
1263         return(PS_IOERR);
1264     }
1265
1266     /*
1267      * We need to disable the normal SIGCHLD handling here because 
1268      * sigchld_handler() would reap away the error status, returning
1269      * error status instead of 0 for successful completion.
1270      */
1271     set_signal_handler(SIGCHLD, SIG_DFL);
1272
1273     return(PS_SUCCESS);
1274 }
1275
1276 int open_sink(struct query *ctl, struct msgblk *msg,
1277               int *good_addresses, int *bad_addresses)
1278 /* set up sinkfp to be an input sink we can ship a message to */
1279 {
1280     *bad_addresses = *good_addresses = 0;
1281
1282     if (want_progress() && outlevel >= O_VERBOSE && !ctl->mda && !ctl->bsmtp) puts("");
1283
1284     if (ctl->bsmtp)             /* dump to a BSMTP batch file */
1285         return(open_bsmtp_sink(ctl, msg, good_addresses, bad_addresses));
1286     /* 
1287      * Try to forward to an SMTP or LMTP listener.  If the attempt to 
1288      * open a socket fails, fall through to attempt delivery via
1289      * local MDA.
1290      */
1291     else if (!ctl->mda && smtp_setup(ctl) != -1)
1292         return(open_smtp_sink(ctl, msg, good_addresses, bad_addresses));
1293
1294     /*
1295      * Awkward case.  User didn't specify an MDA.  Our attempt to get a
1296      * listener socket failed.  Try to cope anyway -- initial configuration
1297      * may have found procmail.
1298      */
1299     else if (!ctl->mda)
1300     {
1301         report(stderr, GT_("%cMTP connect to %s failed\n"),
1302                ctl->smtphostmode,
1303                ctl->smtphost ? ctl->smtphost : "localhost");
1304
1305 #ifndef FALLBACK_MDA
1306         /* No fallback MDA declared.  Bail out. */
1307         return(PS_SMTP);
1308 #else
1309         /*
1310          * If user had things set up to forward offsite, no way
1311          * we want to deliver locally!
1312          */
1313         if (ctl->smtphost && strcmp(ctl->smtphost, "localhost"))
1314             return(PS_SMTP);
1315
1316         /* 
1317          * User was delivering locally.  We have a fallback MDA.
1318          * Latch it in place, logging the error, and fall through.
1319          * Set stripcr as we would if MDA had been the initial transport
1320          */
1321         ctl->mda = FALLBACK_MDA;
1322         if (!ctl->forcecr)
1323             ctl->stripcr = TRUE;
1324
1325         report(stderr, GT_("can't raise the listener; falling back to %s"),
1326                          FALLBACK_MDA);
1327 #endif
1328     }
1329
1330     if (ctl->mda)               /* must deliver through an MDA */
1331         return(open_mda_sink(ctl, msg, good_addresses, bad_addresses));
1332
1333     return(PS_SUCCESS);
1334 }
1335
1336 void release_sink(struct query *ctl)
1337 /* release the per-message output sink, whether it's a pipe or SMTP socket */
1338 {
1339     if (ctl->bsmtp && sinkfp)
1340     {
1341         if (strcmp(ctl->bsmtp, "-"))
1342         {
1343             fclose(sinkfp);
1344             sinkfp = (FILE *)NULL;
1345         }
1346     }
1347     else if (ctl->mda)
1348     {
1349         if (sinkfp)
1350         {
1351             pclose(sinkfp);
1352             sinkfp = (FILE *)NULL;
1353         }
1354         deal_with_sigchld(); /* Restore SIGCHLD handling to reap zombies */
1355     }
1356 }
1357
1358 int close_sink(struct query *ctl, struct msgblk *msg, flag forward)
1359 /* perform end-of-message actions on the current output sink */
1360 {
1361     int smtp_err;
1362
1363     if (want_progress() && outlevel >= O_VERBOSE && !ctl->mda && !ctl->bsmtp) puts("");
1364
1365     if (ctl->bsmtp && sinkfp) {
1366         int error, oerrno;
1367
1368         /* implicit disk-full check here... */
1369         fputs(".\r\n", sinkfp);
1370         error = ferror(sinkfp);
1371         oerrno = errno;
1372         if (strcmp(ctl->bsmtp, "-"))
1373         {
1374             if (fclose(sinkfp) == EOF) {
1375                 error = 1;
1376                 oerrno = errno;
1377             }
1378             sinkfp = (FILE *)NULL;
1379         }
1380         if (error)
1381         {
1382             report(stderr, 
1383                    GT_("Message termination or close of BSMTP file failed: %s\n"), strerror(oerrno));
1384             return(FALSE);
1385         }
1386     } else if (ctl->mda) {
1387         int rc = 0, e = 0, e2 = 0, err = 0;
1388
1389         /* close the delivery pipe, we'll reopen before next message */
1390         if (sinkfp)
1391         {
1392             if (ferror(sinkfp))
1393                 err = 1, e2 = errno;
1394             if ((fflush(sinkfp)))
1395                 err = 1, e2 = errno;
1396
1397             errno = 0;
1398             rc = pclose(sinkfp);
1399             e = errno;
1400             sinkfp = (FILE *)NULL;
1401         }
1402
1403         deal_with_sigchld(); /* Restore SIGCHLD handling to reap zombies */
1404
1405         if (rc || err)
1406         {
1407             if (err) {
1408                 report(stderr, GT_("Error writing to MDA: %s\n"), strerror(e2));
1409             } else if (WIFSIGNALED(rc)) {
1410                 report(stderr, 
1411                         GT_("MDA died of signal %d\n"), WTERMSIG(rc));
1412             } else if (WIFEXITED(rc)) {
1413                 report(stderr, 
1414                         GT_("MDA returned nonzero status %d\n"), WEXITSTATUS(rc));
1415             } else {
1416                 report(stderr,
1417                         GT_("Strange: MDA pclose returned %d and errno %d/%s, cannot handle at %s:%d\n"),
1418                         rc, e, strerror(e), __FILE__, __LINE__);
1419             }
1420
1421             return(FALSE);
1422         }
1423     }
1424     else if (forward)
1425     {
1426         /* write message terminator */
1427         if ((smtp_err = SMTP_eom(ctl->smtp_socket, ctl->smtphostmode))
1428                 == SM_UNRECOVERABLE)
1429         {
1430             smtp_close(ctl, 0);
1431             return(FALSE);
1432         }
1433         if (smtp_err != SM_OK)
1434         {
1435             if (handle_smtp_report(ctl, msg) != PS_REFUSED)
1436             {
1437                 smtp_rset(ctl);    /* stay on the safe side */
1438                 return(FALSE);
1439             }
1440             else
1441             {
1442                 report(stderr, GT_("SMTP listener refused delivery\n"));
1443                 smtp_rset(ctl);    /* stay on the safe side */
1444                 return(TRUE);
1445             }
1446         }
1447
1448         /*
1449          * If this is an SMTP connection, SMTP_eom() ate the response.
1450          * But could be this is an LMTP connection, in which case we have to
1451          * interpret either (a) a single 503 response meaning there
1452          * were no successful RCPT TOs, or (b) a variable number of
1453          * responses, one for each successful RCPT TO.  We need to send
1454          * bouncemail on each failed response and then return TRUE anyway,
1455          * otherwise the message will get left in the queue and resent
1456          * to people who got it the first time.
1457          */
1458         if (ctl->smtphostmode == LMTP_MODE)
1459         {
1460             if (lmtp_responses == 0)
1461             {
1462                 SMTP_ok(ctl->smtp_socket, ctl->smtphostmode, TIMEOUT_EOM);
1463
1464                 /*
1465                  * According to RFC2033, 503 is the only legal response
1466                  * if no RCPT TO commands succeeded.  No error recovery
1467                  * is really possible here, as we have no idea what
1468                  * insane thing the listener might be doing if it doesn't
1469                  * comply.
1470                  */
1471                 if (atoi(smtp_response) == 503)
1472                     report(stderr, GT_("LMTP delivery error on EOM\n"));
1473                 else
1474                     report(stderr,
1475                           GT_("Unexpected non-503 response to LMTP EOM: %s\n"),
1476                           smtp_response);
1477
1478                 /*
1479                  * It's not completely clear what to do here.  We choose to
1480                  * interpret delivery failure here as a transient error, 
1481                  * the same way SMTP delivery failure is handled.  If we're
1482                  * wrong, an undead message will get stuck in the queue.
1483                  */
1484                 return(FALSE);
1485             }
1486             else
1487             {
1488                 int     i, errors, rc = FALSE;
1489                 char    **responses;
1490
1491                 /* eat the RFC2033-required responses, saving errors */ 
1492                 responses = (char **)xmalloc(sizeof(char *) * lmtp_responses);
1493                 for (errors = i = 0; i < lmtp_responses; i++)
1494                 {
1495                     if ((smtp_err = SMTP_ok(ctl->smtp_socket, ctl->smtphostmode, TIMEOUT_EOM))
1496                             == SM_UNRECOVERABLE)
1497                     {
1498                         smtp_close(ctl, 0);
1499                         goto unrecov;
1500                     }
1501                     if (smtp_err != SM_OK)
1502                     {
1503                         responses[errors] = xstrdup(smtp_response);
1504                         errors++;
1505                     }
1506                 }
1507
1508                 if (errors == 0)
1509                     rc = TRUE;  /* all deliveries succeeded */
1510                 else
1511                     /*
1512                      * One or more deliveries failed.
1513                      * If we can bounce a failures list back to the
1514                      * sender, and the postmaster does not want to
1515                      * deal with the bounces return TRUE, deleting the
1516                      * message from the server so it won't be
1517                      * re-forwarded on subsequent poll cycles.
1518                      */
1519                     rc = send_bouncemail(ctl, msg, XMIT_ACCEPT,
1520                             "LMTP partial delivery failure.\r\n",
1521                             errors, responses);
1522
1523 unrecov:
1524                 for (i = 0; i < errors; i++)
1525                     free(responses[i]);
1526                 free(responses);
1527                 return rc;
1528             }
1529         }
1530     }
1531
1532     return(TRUE);
1533 }
1534
1535 int open_warning_by_mail(struct query *ctl)
1536 /* set up output sink for a mailed warning to calling user */
1537 {
1538     int good, bad;
1539
1540     /*
1541      * Dispatching warning email is a little complicated.  The problem is
1542      * that we have to deal with three distinct cases:
1543      *
1544      * 1. Single-drop running from user account.  Warning mail should
1545      * go to the local name for which we're collecting (coincides
1546      * with calling user).
1547      *
1548      * 2. Single-drop running from root or other privileged ID, with rc
1549      * file generated on the fly (Ken Estes's weird setup...)  Mail
1550      * should go to the local name for which we're collecting (does not 
1551      * coincide with calling user).
1552      * 
1553      * 3. Multidrop.  Mail must go to postmaster.  We leave the recipients
1554      * member null so this message will fall through to run.postmaster.
1555      *
1556      * The zero in the reallen element means we won't pass a SIZE
1557      * option to ESMTP; the message length would be more trouble than
1558      * it's worth to compute.
1559      */
1560     struct msgblk reply = {NULL, NULL, "FETCHMAIL-DAEMON@", 0, 0};
1561     int status;
1562
1563     strlcat(reply.return_path, ctl->smtpaddress ? ctl->smtpaddress :
1564             fetchmailhost, sizeof(reply.return_path));
1565
1566     if (!MULTIDROP(ctl))                /* send to calling user */
1567     {
1568         save_str(&reply.recipients, ctl->localnames->id, XMIT_ACCEPT);
1569         status = open_sink(ctl, &reply, &good, &bad);
1570         free_str_list(&reply.recipients);
1571     }
1572     else                                /* send to postmaster  */
1573         status = open_sink(ctl, &reply, &good, &bad);
1574     if (status == 0) {
1575         stuff_warning(NULL, ctl, "From: FETCHMAIL-DAEMON@%s",
1576                 ctl->smtpaddress ? ctl->smtpaddress : fetchmailhost);
1577         stuff_warning(NULL, ctl, "Date: %s", rfc822timestamp());
1578         stuff_warning(NULL, ctl, "MIME-Version: 1.0");
1579         stuff_warning(NULL, ctl, "Content-Transfer-Encoding: 8bit");
1580         stuff_warning(NULL, ctl, "Content-Type: text/plain; charset=\"%s\"", iana_charset);
1581     }
1582     return(status);
1583 }
1584
1585 /* format and ship a warning message line by mail */
1586 /* if rfc2047charset is non-NULL, encode the line (that is assumed to be
1587  * a header line) as per RFC-2047 using rfc2047charset as the character
1588  * set field */
1589 #if defined(HAVE_STDARG_H)
1590 void stuff_warning(const char *rfc2047charset, struct query *ctl, const char *fmt, ... )
1591 #else
1592 void stuff_warning(rfc2047charset, ctl, fmt, va_alist)
1593 const char *charset;
1594 struct query *ctl;
1595 const char *fmt;        /* printf-style format */
1596 va_dcl
1597 #endif
1598 {
1599     /* make huge -- i18n can bulk up error messages a lot */
1600     char        buf[2*MSGBUFSIZE+4];
1601     va_list ap;
1602
1603     /*
1604      * stuffline() requires its input to be writeable (for CR stripping),
1605      * so we needed to copy the message to a writeable buffer anyway in
1606      * case it was a string constant.  We make a virtue of that necessity
1607      * here by supporting stdargs/varargs.
1608      */
1609 #if defined(HAVE_STDARG_H)
1610     va_start(ap, fmt) ;
1611 #else
1612     va_start(ap);
1613 #endif
1614     vsnprintf(buf, sizeof(buf) - 2, fmt, ap);
1615     va_end(ap);
1616
1617     snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "\r\n");
1618
1619     /* guard against very long lines */
1620     buf[MSGBUFSIZE+1] = '\r';
1621     buf[MSGBUFSIZE+2] = '\n';
1622     buf[MSGBUFSIZE+3] = '\0';
1623
1624     stuffline(ctl, rfc2047charset != NULL ? rfc2047e(buf, rfc2047charset) : buf);
1625 }
1626
1627 void close_warning_by_mail(struct query *ctl, struct msgblk *msg)
1628 /* sign and send mailed warnings */
1629 {
1630     stuff_warning(NULL, ctl, GT_("-- \nThe Fetchmail Daemon"));
1631     close_sink(ctl, msg, TRUE);
1632 }
1633
1634 /* sink.c ends here */