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