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