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