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