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