Use compressed form of ipv6 in $sender_host_address under -bh. Bug 3027
[exim.git] / src / src / transports / lmtp.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) The Exim Maintainers 2020 - 2023 */
6 /* Copyright (c) University of Cambridge 1995 - 2018 */
7 /* See the file NOTICE for conditions of use and distribution. */
8 /* SPDX-License-Identifier: GPL-2.0-or-later */
9
10
11 #include "../exim.h"
12 #ifdef TRANSPORT_LMTP           /* Remainder of file */
13
14 #include "lmtp.h"
15
16 #define PENDING_OK 256
17
18
19 /* Options specific to the lmtp transport. They must be in alphabetic
20 order (note that "_" comes before the lower case letters). Those starting
21 with "*" are not settable by the user but are used by the option-reading
22 software for alternative value types. Some options are stored in the transport
23 instance block so as to be publicly visible; these are flagged with opt_public.
24 */
25
26 optionlist lmtp_transport_options[] = {
27   { "batch_id",          opt_stringptr | opt_public,
28       OPT_OFF(transport_instance, batch_id) },
29   { "batch_max",         opt_int | opt_public,
30       OPT_OFF(transport_instance, batch_max) },
31   { "command",           opt_stringptr,
32       OPT_OFF(lmtp_transport_options_block, cmd) },
33   { "ignore_quota",      opt_bool,
34       OPT_OFF(lmtp_transport_options_block, ignore_quota) },
35   { "socket",            opt_stringptr,
36       OPT_OFF(lmtp_transport_options_block, skt) },
37   { "timeout",           opt_time,
38       OPT_OFF(lmtp_transport_options_block, timeout) }
39 };
40
41 /* Size of the options list. An extern variable has to be used so that its
42 address can appear in the tables drtables.c. */
43
44 int lmtp_transport_options_count =
45   sizeof(lmtp_transport_options)/sizeof(optionlist);
46
47
48 #ifdef MACRO_PREDEF
49
50 /* Dummy values */
51 lmtp_transport_options_block lmtp_transport_option_defaults = {0};
52 void lmtp_transport_init(transport_instance *tblock) {}
53 BOOL lmtp_transport_entry(transport_instance *tblock, address_item *addr) {return FALSE;}
54
55 #else   /*!MACRO_PREDEF*/
56
57
58 /* Default private options block for the lmtp transport. */
59
60 lmtp_transport_options_block lmtp_transport_option_defaults = {
61   NULL,           /* cmd */
62   NULL,           /* skt */
63   5*60,           /* timeout */
64   0,              /* options */
65   FALSE           /* ignore_quota */
66 };
67
68
69
70 /*************************************************
71 *          Initialization entry point            *
72 *************************************************/
73
74 /* Called for each instance, after its options have been read, to
75 enable consistency checks to be done, or anything else that needs
76 to be set up. */
77
78 void
79 lmtp_transport_init(transport_instance *tblock)
80 {
81 lmtp_transport_options_block *ob =
82   (lmtp_transport_options_block *)(tblock->options_block);
83
84 /* Either the command field or the socket field must be set */
85
86 if ((ob->cmd == NULL) == (ob->skt == NULL))
87   log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
88     "one (and only one) of command or socket must be set for the %s transport",
89     tblock->name);
90
91 /* If a fixed uid field is set, then a gid field must also be set. */
92
93 if (tblock->uid_set && !tblock->gid_set && tblock->expand_gid == NULL)
94   log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
95     "user set without group for the %s transport", tblock->name);
96
97 /* Set up the bitwise options for transport_write_message from the various
98 driver options. Only one of body_only and headers_only can be set. */
99
100 ob->options |=
101   (tblock->body_only? topt_no_headers : 0) |
102   (tblock->headers_only? topt_no_body : 0) |
103   (tblock->return_path_add? topt_add_return_path : 0) |
104   (tblock->delivery_date_add? topt_add_delivery_date : 0) |
105   (tblock->envelope_to_add? topt_add_envelope_to : 0) |
106   topt_use_crlf | topt_end_dot;
107 }
108
109
110 /*************************************************
111 *          Check an LMTP response                *
112 *************************************************/
113
114 /* This function is given an errno code and the LMTP response buffer to
115 analyse. It sets an appropriate message and puts the first digit of the
116 response code into the yield variable. If no response was actually read, a
117 suitable digit is chosen.
118
119 Arguments:
120   errno_value  pointer to the errno value
121   more_errno   from the top address for use with ERRNO_FILTER_FAIL
122   buffer       the LMTP response buffer
123   yield        where to put a one-digit LMTP response code
124   message      where to put an error message
125
126 Returns:       TRUE if a "QUIT" command should be sent, else FALSE
127 */
128
129 static BOOL
130 check_response(int *errno_value, int more_errno, uschar *buffer,
131   int *yield, uschar **message)
132 {
133 *yield = '4';    /* Default setting is to give a temporary error */
134
135 /* Handle response timeout */
136
137 if (*errno_value == ETIMEDOUT)
138   {
139   *message = string_sprintf("LMTP timeout after %s", big_buffer);
140   if (transport_count > 0)
141     *message = string_sprintf("%s (%d bytes written)", *message,
142       transport_count);
143   *errno_value = 0;
144   return FALSE;
145   }
146
147 /* Handle malformed LMTP response */
148
149 if (*errno_value == ERRNO_SMTPFORMAT)
150   {
151   *message = string_sprintf("Malformed LMTP response after %s: %s",
152     big_buffer, string_printing(buffer));
153   return FALSE;
154   }
155
156 /* Handle a failed filter process error; can't send QUIT as we mustn't
157 end the DATA. */
158
159 if (*errno_value == ERRNO_FILTER_FAIL)
160   {
161   *message = string_sprintf("transport filter process failed (%d)%s",
162     more_errno,
163     (more_errno == EX_EXECFAILED)? ": unable to execute command" : "");
164   return FALSE;
165   }
166
167 /* Handle a failed add_headers expansion; can't send QUIT as we mustn't
168 end the DATA. */
169
170 if (*errno_value == ERRNO_CHHEADER_FAIL)
171   {
172   *message =
173     string_sprintf("failed to expand headers_add or headers_remove: %s",
174       expand_string_message);
175   return FALSE;
176   }
177
178 /* Handle failure to write a complete data block */
179
180 if (*errno_value == ERRNO_WRITEINCOMPLETE)
181   {
182   *message = US"failed to write a data block";
183   return FALSE;
184   }
185
186 /* Handle error responses from the remote process. */
187
188 if (buffer[0] != 0)
189   {
190   const uschar *s = string_printing(buffer);
191   *message = string_sprintf("LMTP error after %s: %s", big_buffer, s);
192   *yield = buffer[0];
193   return TRUE;
194   }
195
196 /* No data was read. If there is no errno, this must be the EOF (i.e.
197 connection closed) case, which causes deferral. Otherwise, leave the errno
198 value to be interpreted. In all cases, we have to assume the connection is now
199 dead. */
200
201 if (*errno_value == 0)
202   {
203   *errno_value = ERRNO_SMTPCLOSED;
204   *message = string_sprintf("LMTP connection closed after %s", big_buffer);
205   }
206
207 return FALSE;
208 }
209
210
211
212 /*************************************************
213 *             Write LMTP command                 *
214 *************************************************/
215
216 /* The formatted command is left in big_buffer so that it can be reflected in
217 any error message.
218
219 Arguments:
220   fd         the fd to write to
221   format     a format, starting with one of
222              of HELO, MAIL FROM, RCPT TO, DATA, ".", or QUIT.
223   ...        data for the format
224
225 Returns:     TRUE if successful, FALSE if not, with errno set
226 */
227
228 static BOOL
229 lmtp_write_command(int fd, const char *format, ...)
230 {
231 gstring gs = { .size = big_buffer_size, .ptr = 0, .s = big_buffer };
232 int rc;
233 va_list ap;
234
235 /*XXX see comment in smtp_write_command() regarding leaving stuff in
236 big_buffer */
237
238 va_start(ap, format);
239 if (!string_vformat(&gs, SVFMT_TAINT_NOCHK, CS format, ap))
240   {
241   va_end(ap);
242   errno = ERRNO_SMTPFORMAT;
243   return FALSE;
244   }
245 va_end(ap);
246 DEBUG(D_transport|D_v) debug_printf("  LMTP>> %Y", &gs);
247 rc = write(fd, gs.s, gs.ptr);
248 gs.ptr -= 2; string_from_gstring(&gs); /* remove \r\n for debug and error message */
249 if (rc > 0) return TRUE;
250 DEBUG(D_transport) debug_printf("write failed: %s\n", strerror(errno));
251 return FALSE;
252 }
253
254
255
256
257 /*************************************************
258 *              Read LMTP response                *
259 *************************************************/
260
261 /* This function reads an LMTP response with a timeout, and returns the
262 response in the given buffer. It also analyzes the first digit of the reply
263 code and returns FALSE if it is not acceptable.
264
265 FALSE is also returned after a reading error. In this case buffer[0] will be
266 zero, and the error code will be in errno.
267
268 Arguments:
269   f         a file to read from
270   buffer    where to put the response
271   size      the size of the buffer
272   okdigit   the expected first digit of the response
273   timeout   the timeout to use
274
275 Returns:    TRUE if a valid, non-error response was received; else FALSE
276 */
277
278 static BOOL
279 lmtp_read_response(FILE *f, uschar *buffer, int size, int okdigit, int timeout)
280 {
281 int count;
282 uschar *ptr = buffer;
283 uschar *readptr = buffer;
284
285 /* Ensure errno starts out zero */
286
287 errno = 0;
288
289 /* Loop for handling LMTP responses that do not all come in one line. */
290
291 for (;;)
292   {
293   /* If buffer is too full, something has gone wrong. */
294
295   if (size < 10)
296     {
297     *readptr = 0;
298     errno = ERRNO_SMTPFORMAT;
299     return FALSE;
300     }
301
302   /* Loop to cover the read getting interrupted. */
303
304   for (;;)
305     {
306     char *rc;
307     int save_errno;
308
309     *readptr = 0;           /* In case nothing gets read */
310     sigalrm_seen = FALSE;
311     ALARM(timeout);
312     rc = Ufgets(readptr, size-1, f);
313     save_errno = errno;
314     ALARM_CLR(0);
315     errno = save_errno;
316
317     if (rc != NULL) break;  /* A line has been read */
318
319     /* Handle timeout; must do this first because it uses EINTR */
320
321     if (sigalrm_seen) errno = ETIMEDOUT;
322
323     /* If some other interrupt arrived, just retry. We presume this to be rare,
324     but it can happen (e.g. the SIGUSR1 signal sent by exiwhat causes
325     read() to exit). */
326
327     else if (errno == EINTR)
328       {
329       DEBUG(D_transport) debug_printf("EINTR while reading LMTP response\n");
330       continue;
331       }
332
333     /* Handle other errors, including EOF; ensure buffer is completely empty. */
334
335     buffer[0] = 0;
336     return FALSE;
337     }
338
339   /* Adjust size in case we have to read another line, and adjust the
340   count to be the length of the line we are about to inspect. */
341
342   count = Ustrlen(readptr);
343   size -= count;
344   count += readptr - ptr;
345
346   /* See if the final two characters in the buffer are \r\n. If not, we
347   have to read some more. At least, that is what we should do on a strict
348   interpretation of the RFC. But accept LF as well, as we do for SMTP. */
349
350   if (ptr[count-1] != '\n')
351     {
352     DEBUG(D_transport)
353       {
354       debug_printf("LMTP input line incomplete in one buffer:\n  ");
355       for (int i = 0; i < count; i++)
356         {
357         int c = (ptr[i]);
358         if (mac_isprint(c)) debug_printf("%c", c); else debug_printf("<%d>", c);
359         }
360       debug_printf("\n");
361       }
362     readptr = ptr + count;
363     continue;
364     }
365
366   /* Remove any whitespace at the end of the buffer. This gets rid of CR, LF
367   etc. at the end. Show it, if debugging, formatting multi-line responses. */
368
369   while (count > 0 && isspace(ptr[count-1])) count--;
370   ptr[count] = 0;
371
372   DEBUG(D_transport|D_v)
373     {
374     uschar *s = ptr;
375     uschar *t = ptr;
376     while (*t != 0)
377       {
378       while (*t != 0 && *t != '\n') t++;
379       debug_printf("  %s %*s\n", (s == ptr)? "LMTP<<" : "      ",
380         (int)(t-s), s);
381       if (*t == 0) break;
382       s = t = t + 1;
383       }
384     }
385
386   /* Check the format of the response: it must start with three digits; if
387   these are followed by a space or end of line, the response is complete. If
388   they are followed by '-' this is a multi-line response and we must look for
389   another line until the final line is reached. The only use made of multi-line
390   responses is to pass them back as error messages. We therefore just
391   concatenate them all within the buffer, which should be large enough to
392   accept any reasonable number of lines. A multiline response may already
393   have been read in one go - hence the loop here. */
394
395   for(;;)
396     {
397     uschar *p;
398     if (count < 3 ||
399        !isdigit(ptr[0]) ||
400        !isdigit(ptr[1]) ||
401        !isdigit(ptr[2]) ||
402        (ptr[3] != '-' && ptr[3] != ' ' && ptr[3] != 0))
403       {
404       errno = ERRNO_SMTPFORMAT;    /* format error */
405       return FALSE;
406       }
407
408     /* If a single-line response, exit the loop */
409
410     if (ptr[3] != '-') break;
411
412     /* For a multi-line response see if the next line is already read, and if
413     so, stay in this loop to check it. */
414
415     p = ptr + 3;
416     while (*(++p) != 0)
417       {
418       if (*p == '\n')
419         {
420         ptr = ++p;
421         break;
422         }
423       }
424     if (*p == 0) break;   /* No more lines to check */
425     }
426
427   /* End of response. If the last of the lines we are looking at is the final
428   line, we are done. Otherwise more data has to be read. */
429
430   if (ptr[3] != '-') break;
431
432   /* Move the reading pointer upwards in the buffer and insert \n in case this
433   is an error message that subsequently gets printed. Set the scanning pointer
434   to the reading pointer position. */
435
436   ptr += count;
437   *ptr++ = '\n';
438   size--;
439   readptr = ptr;
440   }
441
442 /* Return a value that depends on the LMTP return code. Ensure that errno is
443 zero, because the caller of this function looks at errno when FALSE is
444 returned, to distinguish between an unexpected return code and other errors
445 such as timeouts, lost connections, etc. */
446
447 errno = 0;
448 return buffer[0] == okdigit;
449 }
450
451
452
453
454
455
456 /*************************************************
457 *              Main entry point                  *
458 *************************************************/
459
460 /* See local README for interface details. For setup-errors, this transport
461 returns FALSE, indicating that the first address has the status for all; in
462 normal cases it returns TRUE, indicating that each address has its own status
463 set. */
464
465 BOOL
466 lmtp_transport_entry(
467   transport_instance *tblock,      /* data for this instantiation */
468   address_item *addrlist)          /* address(es) we are working on */
469 {
470 pid_t pid = 0;
471 FILE *out;
472 lmtp_transport_options_block *ob =
473   (lmtp_transport_options_block *)(tblock->options_block);
474 struct sockaddr_un sockun;         /* don't call this "sun" ! */
475 int timeout = ob->timeout;
476 int fd_in = -1, fd_out = -1;
477 int code, save_errno;
478 BOOL send_data;
479 BOOL yield = FALSE;
480 uschar *igquotstr = US"";
481 uschar *sockname = NULL;
482 const uschar **argv;
483 uschar buffer[256];
484
485 DEBUG(D_transport) debug_printf("%s transport entered\n", tblock->name);
486
487 /* Initialization ensures that either a command or a socket is specified, but
488 not both. When a command is specified, call the common function for creating an
489 argument list and expanding the items. */
490
491 if (ob->cmd)
492   {
493   DEBUG(D_transport) debug_printf("using command %s\n", ob->cmd);
494   sprintf(CS buffer, "%.50s transport", tblock->name);
495   if (!transport_set_up_command(&argv, ob->cmd, TSUC_EXPAND_ARGS, PANIC,
496         addrlist, buffer, NULL))
497     return FALSE;
498
499   /* If the -N option is set, can't do any more. Presume all has gone well. */
500   if (f.dont_deliver)
501     goto MINUS_N;
502
503 /* As this is a local transport, we are already running with the required
504 uid/gid and current directory. Request that the new process be a process group
505 leader, so we can kill it and all its children on an error. */
506
507   if ((pid = child_open(USS argv, NULL, 0, &fd_in, &fd_out, TRUE,
508                         US"lmtp-tpt-cmd")) < 0)
509     {
510     addrlist->message = string_sprintf(
511       "Failed to create child process for %s transport: %s", tblock->name,
512         strerror(errno));
513     return FALSE;
514     }
515   }
516
517 /* When a socket is specified, expand the string and create a socket. */
518
519 else
520   {
521   DEBUG(D_transport) debug_printf("using socket %s\n", ob->skt);
522   if (!(sockname = expand_string(ob->skt)))
523     {
524     addrlist->message = string_sprintf("Expansion of \"%s\" (socket setting "
525       "for %s transport) failed: %s", ob->skt, tblock->name,
526       expand_string_message);
527     return FALSE;
528     }
529   if ((fd_in = fd_out = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
530     {
531     addrlist->message = string_sprintf(
532       "Failed to create socket %s for %s transport: %s",
533         ob->skt, tblock->name, strerror(errno));
534     return FALSE;
535     }
536
537   /* If the -N option is set, can't do any more. Presume all has gone well. */
538   if (f.dont_deliver)
539     goto MINUS_N;
540
541   sockun.sun_family = AF_UNIX;
542   sprintf(sockun.sun_path, "%.*s", (int)(sizeof(sockun.sun_path)-1), sockname);
543   if(connect(fd_out, (struct sockaddr *)(&sockun), sizeof(sockun)) == -1)
544     {
545     addrlist->message = string_sprintf(
546       "Failed to connect to socket %s for %s transport: %s",
547         sockun.sun_path, tblock->name, strerror(errno));
548     return FALSE;
549     }
550   }
551
552
553 /* Make the output we are going to read into a file. */
554
555 out = fdopen(fd_out, "rb");
556
557 /* Now we must implement the LMTP protocol. It is like SMTP, except that after
558 the end of the message, a return code for every accepted RCPT TO is sent. This
559 allows for message+recipient checks after the message has been received. */
560
561 /* First thing is to wait for an initial greeting. */
562
563 Ustrcpy(big_buffer, US"initial connection");
564 if (!lmtp_read_response(out, buffer, sizeof(buffer), '2', timeout))
565   goto RESPONSE_FAILED;
566
567 /* Next, we send a LHLO command, and expect a positive response */
568
569 if (!lmtp_write_command(fd_in, "%s %s\r\n", "LHLO", primary_hostname))
570   goto WRITE_FAILED;
571
572 if (!lmtp_read_response(out, buffer, sizeof(buffer), '2', timeout))
573   goto RESPONSE_FAILED;
574
575 /* If the ignore_quota option is set, note whether the server supports the
576 IGNOREQUOTA option, and if so, set an appropriate addition for RCPT. */
577
578 if (ob->ignore_quota)
579   igquotstr = regex_match(regex_IGNOREQUOTA, buffer, -1, NULL)
580     ? US" IGNOREQUOTA" : US"";
581
582 /* Now the envelope sender */
583
584 if (!lmtp_write_command(fd_in, "MAIL FROM:<%s>\r\n", return_path))
585   goto WRITE_FAILED;
586
587 if (!lmtp_read_response(out, buffer, sizeof(buffer), '2', timeout))
588   {
589   if (errno == 0 && buffer[0] == '4')
590     {
591     errno = ERRNO_MAIL4XX;
592     addrlist->more_errno |= ((buffer[1] - '0')*10 + buffer[2] - '0') << 8;
593     }
594   goto RESPONSE_FAILED;
595   }
596
597 /* Next, we hand over all the recipients. Some may be permanently or
598 temporarily rejected; others may be accepted, for now. */
599
600 send_data = FALSE;
601 for (address_item * addr = addrlist; addr; addr = addr->next)
602   {
603   if (!lmtp_write_command(fd_in, "RCPT TO:<%s>%s\r\n",
604        transport_rcpt_address(addr, tblock->rcpt_include_affixes), igquotstr))
605     goto WRITE_FAILED;
606   if (lmtp_read_response(out, buffer, sizeof(buffer), '2', timeout))
607     {
608     send_data = TRUE;
609     addr->transport_return = PENDING_OK;
610     }
611   else
612     {
613     if (errno != 0 || buffer[0] == 0) goto RESPONSE_FAILED;
614     addr->message = string_sprintf("LMTP error after %s: %s", big_buffer,
615       string_printing(buffer));
616     setflag(addr, af_pass_message);   /* Allow message to go to user */
617     if (buffer[0] == '5') addr->transport_return = FAIL; else
618       {
619       addr->basic_errno = ERRNO_RCPT4XX;
620       addr->more_errno |= ((buffer[1] - '0')*10 + buffer[2] - '0') << 8;
621       }
622     }
623   }
624
625 /* Now send the text of the message if there were any good recipients. */
626
627 if (send_data)
628   {
629   BOOL ok;
630   transport_ctx tctx = {
631     {fd_in},
632     tblock,
633     addrlist,
634     US".", US"..",
635     ob->options
636   };
637
638   if (!lmtp_write_command(fd_in, "DATA\r\n")) goto WRITE_FAILED;
639   if (!lmtp_read_response(out, buffer, sizeof(buffer), '3', timeout))
640     {
641     if (errno == 0 && buffer[0] == '4')
642       {
643       errno = ERRNO_DATA4XX;
644       addrlist->more_errno |= ((buffer[1] - '0')*10 + buffer[2] - '0') << 8;
645       }
646     goto RESPONSE_FAILED;
647     }
648
649   sigalrm_seen = FALSE;
650   transport_write_timeout = timeout;
651   Ustrcpy(big_buffer, US"sending data block");   /* For error messages */
652   DEBUG(D_transport|D_v)
653     debug_printf("  LMTP>> writing message and terminating \".\"\n");
654
655   transport_count = 0;
656   ok = transport_write_message(&tctx, 0);
657
658   /* Failure can either be some kind of I/O disaster (including timeout),
659   or the failure of a transport filter or the expansion of added headers. */
660
661   if (!ok)
662     {
663     buffer[0] = 0;              /* There hasn't been a response */
664     goto RESPONSE_FAILED;
665     }
666
667   Ustrcpy(big_buffer, US"end of data");   /* For error messages */
668
669   /* We now expect a response for every address that was accepted above,
670   in the same order. For those that get a response, their status is fixed;
671   any that are accepted have been handed over, even if later responses crash -
672   at least, that's how I read RFC 2033. */
673
674   for (address_item * addr = addrlist; addr; addr = addr->next)
675     {
676     if (addr->transport_return != PENDING_OK) continue;
677
678     if (lmtp_read_response(out, buffer, sizeof(buffer), '2', timeout))
679       {
680       addr->transport_return = OK;
681       if (LOGGING(smtp_confirmation))
682         {
683         const uschar *s = string_printing(buffer);
684         /* de-const safe here as string_printing known to have alloc'n'copied */
685         addr->message = (s == buffer)? US string_copy(s) : US s;
686         }
687       }
688     /* If the response has failed badly, use it for all the remaining pending
689     addresses and give up. */
690
691     else if (errno != 0 || buffer[0] == 0)
692       {
693       save_errno = errno;
694       check_response(&save_errno, addr->more_errno, buffer, &code,
695         &(addr->message));
696       addr->transport_return = (code == '5')? FAIL : DEFER;
697       for (address_item * a = addr->next; a; a = a->next)
698         {
699         if (a->transport_return != PENDING_OK) continue;
700         a->basic_errno = addr->basic_errno;
701         a->message = addr->message;
702         a->transport_return = addr->transport_return;
703         }
704       break;
705       }
706
707     /* Otherwise, it's an LMTP error code return for one address */
708
709     else
710       {
711       if (buffer[0] == '4')
712         {
713         addr->basic_errno = ERRNO_DATA4XX;
714         addr->more_errno |= ((buffer[1] - '0')*10 + buffer[2] - '0') << 8;
715         }
716       addr->message = string_sprintf("LMTP error after %s: %s", big_buffer,
717         string_printing(buffer));
718       addr->transport_return = (buffer[0] == '5')? FAIL : DEFER;
719       setflag(addr, af_pass_message);   /* Allow message to go to user */
720       }
721     }
722   }
723
724 /* The message transaction has completed successfully - this doesn't mean that
725 all the addresses have necessarily been transferred, but each has its status
726 set, so we change the yield to TRUE. */
727
728 yield = TRUE;
729 (void) lmtp_write_command(fd_in, "QUIT\r\n");
730 (void) lmtp_read_response(out, buffer, sizeof(buffer), '2', 1);
731
732 goto RETURN;
733
734
735 /* Come here if any call to read_response, other than a response after the data
736 phase, failed. Put the error in the top address - this will be replicated
737 because the yield is still FALSE. (But omit ETIMEDOUT, as there will already be
738 a suitable message.) Analyse the error, and if if isn't too bad, send a QUIT
739 command. Wait for the response with a short timeout, so we don't wind up this
740 process before the far end has had time to read the QUIT. */
741
742 RESPONSE_FAILED:
743
744 save_errno = errno;
745 if (errno != ETIMEDOUT && errno != 0) addrlist->basic_errno = errno;
746 addrlist->message = NULL;
747
748 if (check_response(&save_errno, addrlist->more_errno,
749     buffer, &code, &(addrlist->message)))
750   {
751   (void) lmtp_write_command(fd_in, "QUIT\r\n");
752   (void) lmtp_read_response(out, buffer, sizeof(buffer), '2', 1);
753   }
754
755 addrlist->transport_return = (code == '5')? FAIL : DEFER;
756 if (code == '4' && save_errno > 0)
757   addrlist->message = string_sprintf("%s: %s", addrlist->message,
758     strerror(save_errno));
759 goto KILL_AND_RETURN;
760
761 /* Come here if there are errors during writing of a command or the message
762 itself. This error will be applied to all the addresses. */
763
764 WRITE_FAILED:
765
766 addrlist->transport_return = PANIC;
767 addrlist->basic_errno = errno;
768 if (errno == ERRNO_CHHEADER_FAIL)
769   addrlist->message =
770     string_sprintf("Failed to expand headers_add or headers_remove: %s",
771       expand_string_message);
772 else if (errno == ERRNO_FILTER_FAIL)
773   addrlist->message = US"Filter process failure";
774 else if (errno == ERRNO_WRITEINCOMPLETE)
775   addrlist->message = US"Failed repeatedly to write data";
776 else if (errno == ERRNO_SMTPFORMAT)
777   addrlist->message = US"overlong LMTP command generated";
778 else
779   addrlist->message = string_sprintf("Error %d", errno);
780
781 /* Come here after errors. Kill off the process. */
782
783 KILL_AND_RETURN:
784
785 if (pid > 0) killpg(pid, SIGKILL);
786
787 /* Come here from all paths after the subprocess is created. Wait for the
788 process, but with a timeout. */
789
790 RETURN:
791
792 (void)child_close(pid, timeout);
793
794 if (fd_in >= 0) (void)close(fd_in);
795 if (fd_out >= 0) (void)fclose(out);
796
797 DEBUG(D_transport)
798   debug_printf("%s transport yields %d\n", tblock->name, yield);
799
800 return yield;
801
802
803 MINUS_N:
804   DEBUG(D_transport)
805     debug_printf("*** delivery by %s transport bypassed by -N option",
806       tblock->name);
807   addrlist->transport_return = OK;
808   return FALSE;
809 }
810
811 #endif  /*!MACRO_PREDEF*/
812 #endif  /*TRANSPORT_LMTP*/
813 /* End of transport/lmtp.c */