Commit | Line | Data |
---|---|---|
059ec3d9 PH |
1 | /************************************************* |
2 | * Exim - an Internet mail transport agent * | |
3 | *************************************************/ | |
4 | ||
f9ba5e22 | 5 | /* Copyright (c) University of Cambridge 1995 - 2018 */ |
059ec3d9 PH |
6 | /* See the file NOTICE for conditions of use and distribution. */ |
7 | ||
8 | /* Functions concerned with retrying unsuccessful deliveries. */ | |
9 | ||
10 | ||
11 | #include "exim.h" | |
12 | ||
13 | ||
14 | ||
15 | /************************************************* | |
16 | * Check the ultimate address timeout * | |
17 | *************************************************/ | |
18 | ||
19 | /* This function tests whether a message has been on the queue longer than | |
ba9af0af | 20 | the maximum retry time for a particular host or address. |
059ec3d9 PH |
21 | |
22 | Arguments: | |
ba9af0af | 23 | retry_key the key to look up a retry rule |
059ec3d9 | 24 | domain the domain to look up a domain retry rule |
ba9af0af | 25 | retry_record contains error information for finding rule |
059ec3d9 PH |
26 | now the time |
27 | ||
28 | Returns: TRUE if the ultimate timeout has been reached | |
29 | */ | |
30 | ||
ba9af0af | 31 | BOOL |
55414b25 | 32 | retry_ultimate_address_timeout(uschar *retry_key, const uschar *domain, |
ba9af0af | 33 | dbdata_retry *retry_record, time_t now) |
059ec3d9 | 34 | { |
ba9af0af TF |
35 | BOOL address_timeout; |
36 | ||
37 | DEBUG(D_retry) | |
38 | { | |
39 | debug_printf("retry time not reached: checking ultimate address timeout\n"); | |
40 | debug_printf(" now=%d first_failed=%d next_try=%d expired=%d\n", | |
41 | (int)now, (int)retry_record->first_failed, | |
42 | (int)retry_record->next_try, retry_record->expired); | |
43 | } | |
059ec3d9 PH |
44 | |
45 | retry_config *retry = | |
ba9af0af TF |
46 | retry_find_config(retry_key+2, domain, |
47 | retry_record->basic_errno, retry_record->more_errno); | |
059ec3d9 PH |
48 | |
49 | if (retry != NULL && retry->rules != NULL) | |
50 | { | |
51 | retry_rule *last_rule; | |
52 | for (last_rule = retry->rules; | |
53 | last_rule->next != NULL; | |
54 | last_rule = last_rule->next); | |
ba9af0af | 55 | DEBUG(D_retry) |
c816d124 | 56 | debug_printf(" received_time=%d diff=%d timeout=%d\n", |
cab0c277 | 57 | (int)received_time.tv_sec, (int)(now - received_time.tv_sec), last_rule->timeout); |
32dfdf8b | 58 | address_timeout = (now - received_time.tv_sec > last_rule->timeout); |
059ec3d9 | 59 | } |
ea49d0e1 PH |
60 | else |
61 | { | |
ba9af0af | 62 | DEBUG(D_retry) |
ea49d0e1 | 63 | debug_printf("no retry rule found: assume timed out\n"); |
ba9af0af | 64 | address_timeout = TRUE; |
ea49d0e1 | 65 | } |
059ec3d9 | 66 | |
ba9af0af TF |
67 | DEBUG(D_retry) |
68 | if (address_timeout) | |
69 | debug_printf("on queue longer than maximum retry for address - " | |
70 | "allowing delivery\n"); | |
71 | ||
059ec3d9 PH |
72 | return address_timeout; |
73 | } | |
74 | ||
75 | ||
76 | ||
77 | /************************************************* | |
78 | * Set status of a host+address item * | |
79 | *************************************************/ | |
80 | ||
81 | /* This function is passed a host_item which contains a host name and an | |
82 | IP address string. Its job is to set the status of the address if it is not | |
83 | already set (indicated by hstatus_unknown). The possible values are: | |
84 | ||
85 | hstatus_usable the address is not listed in the unusable tree, and does | |
86 | not have a retry record, OR the time is past the next | |
87 | try time, OR the message has been on the queue for more | |
88 | than the maximum retry time for a failing host | |
89 | ||
90 | hstatus_unusable the address is listed in the unusable tree, or does have | |
91 | a retry record, and the time is not yet at the next retry | |
92 | time. | |
93 | ||
94 | hstatus_unusable_expired as above, but also the retry time has expired | |
95 | for this address. | |
96 | ||
97 | The reason a delivery is permitted when a message has been around for a very | |
98 | long time is to allow the ultimate address timeout to operate after a delivery | |
99 | failure. Otherwise some messages may stick around without being tried for too | |
100 | long. | |
101 | ||
102 | If a host retry record is retrieved from the hints database, the time of last | |
103 | trying is filled into the last_try field of the host block. If a host is | |
104 | generally usable, a check is made to see if there is a retry delay on this | |
105 | specific message at this host. | |
106 | ||
107 | If a non-standard port is being used, it is added to the retry key. | |
108 | ||
109 | Arguments: | |
110 | domain the address domain | |
111 | host pointer to a host item | |
112 | portstring "" for standard port, ":xxxx" for a non-standard port | |
113 | include_ip_address TRUE to include the address in the key - this is | |
114 | usual, but sometimes is not wanted | |
115 | retry_host_key where to put a pointer to the key for the host-specific | |
116 | retry record, if one is read and the host is usable | |
117 | retry_message_key where to put a pointer to the key for the message+host | |
118 | retry record, if one is read and the host is usable | |
119 | ||
120 | Returns: TRUE if the host has expired but is usable because | |
121 | its retry time has come | |
122 | */ | |
123 | ||
124 | BOOL | |
55414b25 | 125 | retry_check_address(const uschar *domain, host_item *host, uschar *portstring, |
059ec3d9 PH |
126 | BOOL include_ip_address, uschar **retry_host_key, uschar **retry_message_key) |
127 | { | |
128 | BOOL yield = FALSE; | |
129 | time_t now = time(NULL); | |
130 | uschar *host_key, *message_key; | |
131 | open_db dbblock; | |
132 | open_db *dbm_file; | |
133 | tree_node *node; | |
134 | dbdata_retry *host_retry_record, *message_retry_record; | |
135 | ||
136 | *retry_host_key = *retry_message_key = NULL; | |
137 | ||
138 | DEBUG(D_transport|D_retry) debug_printf("checking status of %s\n", host->name); | |
139 | ||
140 | /* Do nothing if status already set; otherwise initialize status as usable. */ | |
141 | ||
142 | if (host->status != hstatus_unknown) return FALSE; | |
143 | host->status = hstatus_usable; | |
144 | ||
145 | /* Generate the host key for the unusable tree and the retry database. Ensure | |
146 | host names are lower cased (that's what %S does). */ | |
147 | ||
148 | host_key = include_ip_address? | |
149 | string_sprintf("T:%S:%s%s", host->name, host->address, portstring) : | |
150 | string_sprintf("T:%S%s", host->name, portstring); | |
151 | ||
152 | /* Generate the message-specific key */ | |
153 | ||
154 | message_key = string_sprintf("%s:%s", host_key, message_id); | |
155 | ||
156 | /* Search the tree of unusable IP addresses. This is filled in when deliveries | |
157 | fail, because the retry database itself is not updated until the end of all | |
158 | deliveries (so as to do it all in one go). The tree records addresses that have | |
159 | become unusable during this delivery process (i.e. those that will get put into | |
160 | the retry database when it is updated). */ | |
161 | ||
36d295f1 | 162 | if ((node = tree_search(tree_unusable, host_key))) |
059ec3d9 PH |
163 | { |
164 | DEBUG(D_transport|D_retry) debug_printf("found in tree of unusables\n"); | |
165 | host->status = (node->data.val > 255)? | |
166 | hstatus_unusable_expired : hstatus_unusable; | |
167 | host->why = node->data.val & 255; | |
168 | return FALSE; | |
169 | } | |
170 | ||
171 | /* Open the retry database, giving up if there isn't one. Otherwise, search for | |
172 | the retry records, and then close the database again. */ | |
173 | ||
36d295f1 | 174 | if (!(dbm_file = dbfn_open(US"retry", O_RDONLY, &dbblock, FALSE))) |
059ec3d9 PH |
175 | { |
176 | DEBUG(D_deliver|D_retry|D_hints_lookup) | |
177 | debug_printf("no retry data available\n"); | |
178 | return FALSE; | |
179 | } | |
180 | host_retry_record = dbfn_read(dbm_file, host_key); | |
181 | message_retry_record = dbfn_read(dbm_file, message_key); | |
182 | dbfn_close(dbm_file); | |
183 | ||
184 | /* Ignore the data if it is too old - too long since it was written */ | |
185 | ||
36d295f1 | 186 | if (!host_retry_record) |
059ec3d9 PH |
187 | { |
188 | DEBUG(D_transport|D_retry) debug_printf("no host retry record\n"); | |
189 | } | |
190 | else if (now - host_retry_record->time_stamp > retry_data_expire) | |
191 | { | |
192 | host_retry_record = NULL; | |
193 | DEBUG(D_transport|D_retry) debug_printf("host retry record too old\n"); | |
194 | } | |
195 | ||
36d295f1 | 196 | if (!message_retry_record) |
059ec3d9 PH |
197 | { |
198 | DEBUG(D_transport|D_retry) debug_printf("no message retry record\n"); | |
199 | } | |
200 | else if (now - message_retry_record->time_stamp > retry_data_expire) | |
201 | { | |
202 | message_retry_record = NULL; | |
203 | DEBUG(D_transport|D_retry) debug_printf("message retry record too old\n"); | |
204 | } | |
205 | ||
206 | /* If there's a host-specific retry record, check for reaching the retry | |
207 | time (or forcing). If not, and the host is not expired, check for the message | |
208 | having been around for longer than the maximum retry time for this host or | |
209 | address. Allow the delivery if it has. Otherwise set the appropriate unusable | |
210 | flag and return FALSE. Otherwise arrange to return TRUE if this is an expired | |
211 | host. */ | |
212 | ||
36d295f1 | 213 | if (host_retry_record) |
059ec3d9 PH |
214 | { |
215 | *retry_host_key = host_key; | |
216 | ||
217 | /* We have not reached the next try time. Check for the ultimate address | |
218 | timeout if the host has not expired. */ | |
219 | ||
220 | if (now < host_retry_record->next_try && !deliver_force) | |
221 | { | |
059ec3d9 | 222 | if (!host_retry_record->expired && |
ba9af0af TF |
223 | retry_ultimate_address_timeout(host_key, domain, |
224 | host_retry_record, now)) | |
059ec3d9 | 225 | return FALSE; |
059ec3d9 PH |
226 | |
227 | /* We have not hit the ultimate address timeout; host is unusable. */ | |
228 | ||
229 | host->status = (host_retry_record->expired)? | |
230 | hstatus_unusable_expired : hstatus_unusable; | |
231 | host->why = hwhy_retry; | |
232 | host->last_try = host_retry_record->last_try; | |
233 | return FALSE; | |
234 | } | |
235 | ||
236 | /* Host is usable; set return TRUE if expired. */ | |
237 | ||
238 | yield = host_retry_record->expired; | |
239 | } | |
240 | ||
241 | /* It's OK to try the host. If there's a message-specific retry record, check | |
242 | for reaching its retry time (or forcing). If not, mark the host unusable, | |
243 | unless the ultimate address timeout has been reached. */ | |
244 | ||
36d295f1 | 245 | if (message_retry_record) |
059ec3d9 PH |
246 | { |
247 | *retry_message_key = message_key; | |
248 | if (now < message_retry_record->next_try && !deliver_force) | |
249 | { | |
ba9af0af TF |
250 | if (!retry_ultimate_address_timeout(host_key, domain, |
251 | message_retry_record, now)) | |
059ec3d9 PH |
252 | { |
253 | host->status = hstatus_unusable; | |
254 | host->why = hwhy_retry; | |
255 | } | |
059ec3d9 PH |
256 | return FALSE; |
257 | } | |
258 | } | |
259 | ||
260 | return yield; | |
261 | } | |
262 | ||
263 | ||
264 | ||
265 | ||
266 | /************************************************* | |
267 | * Add a retry item to an address * | |
268 | *************************************************/ | |
269 | ||
270 | /* Retry items are chained onto an address when it is deferred either by router | |
271 | or by a transport, or if it succeeds or fails and there was a previous retry | |
272 | item that now needs to be deleted. Sometimes there can be both kinds of item: | |
273 | for example, if routing was deferred but then succeeded, and delivery then | |
274 | deferred. In that case there is a delete item for the routing retry, and an | |
275 | updating item for the delivery. | |
276 | ||
277 | (But note that that is only visible at the outer level, because in remote | |
278 | delivery subprocesses, the address starts "clean", with no retry items carried | |
279 | in.) | |
280 | ||
281 | These items are used at the end of a delivery attempt to update the retry | |
282 | database. The keys start R: for routing delays and T: for transport delays. | |
283 | ||
284 | Arguments: | |
285 | addr the address block onto which to hang the item | |
286 | key the retry key | |
287 | flags delete, host, and message flags, copied into the block | |
288 | ||
289 | Returns: nothing | |
290 | */ | |
291 | ||
292 | void | |
293 | retry_add_item(address_item *addr, uschar *key, int flags) | |
294 | { | |
295 | retry_item *rti = store_get(sizeof(retry_item)); | |
c562fd30 | 296 | host_item * host = addr->host_used; |
36d295f1 | 297 | |
059ec3d9 PH |
298 | rti->next = addr->retries; |
299 | addr->retries = rti; | |
300 | rti->key = key; | |
301 | rti->basic_errno = addr->basic_errno; | |
302 | rti->more_errno = addr->more_errno; | |
c562fd30 JH |
303 | rti->message = host |
304 | ? string_sprintf("H=%s [%s]: %s", host->name, host->address, addr->message) | |
305 | : addr->message; | |
059ec3d9 PH |
306 | rti->flags = flags; |
307 | ||
308 | DEBUG(D_transport|D_retry) | |
309 | { | |
310 | int letter = rti->more_errno & 255; | |
311 | debug_printf("added retry item for %s: errno=%d more_errno=", rti->key, | |
312 | rti->basic_errno); | |
313 | if (letter == 'A' || letter == 'M') | |
314 | debug_printf("%d,%c", (rti->more_errno >> 8) & 255, letter); | |
315 | else | |
316 | debug_printf("%d", rti->more_errno); | |
317 | debug_printf(" flags=%d\n", flags); | |
318 | } | |
319 | } | |
320 | ||
321 | ||
322 | ||
323 | /************************************************* | |
324 | * Find retry configuration data * | |
325 | *************************************************/ | |
326 | ||
327 | /* Search the in-store retry information for the first retry item that applies | |
328 | to a given destination. If the key contains an @ we are probably handling a | |
329 | local delivery and have a complete address to search for; this happens when | |
330 | retry_use_local_part is set on a router. Otherwise, the key is likely to be a | |
331 | host name for a remote delivery, or a domain name for a local delivery. We | |
332 | prepend *@ on the front of it so that it will match a retry item whose address | |
333 | item pattern is independent of the local part. The alternate key, if set, is | |
334 | always just a domain, so we treat it likewise. | |
335 | ||
336 | Arguments: | |
337 | key key for which retry info is wanted | |
338 | alternate alternative key, always just a domain | |
339 | basic_errno specific error predicate on the retry rule, or zero | |
340 | more_errno additional data for errno predicate | |
341 | ||
342 | Returns: pointer to retry rule, or NULL | |
343 | */ | |
344 | ||
345 | retry_config * | |
55414b25 | 346 | retry_find_config(const uschar *key, const uschar *alternate, int basic_errno, |
059ec3d9 PH |
347 | int more_errno) |
348 | { | |
55414b25 | 349 | const uschar *colon = Ustrchr(key, ':'); |
059ec3d9 PH |
350 | retry_config *yield; |
351 | ||
ea49d0e1 PH |
352 | /* If there's a colon in the key, there are two possibilities: |
353 | ||
354 | (1) This is a key for a host, ip address, and possibly port, in the format | |
355 | ||
356 | hostname:ip+port | |
357 | ||
55414b25 | 358 | In this case, we copy the host name. |
ea49d0e1 PH |
359 | |
360 | (2) This is a key for a pipe, file, or autoreply delivery, in the format | |
361 | ||
362 | pipe-or-file-or-auto:x@y | |
363 | ||
364 | where x@y is the original address that provoked the delivery. The pipe or | |
365 | file or auto will start with | or / or >, whereas a host name will start | |
366 | with a letter or a digit. In this case we want to use the original address | |
367 | to search for a retry rule. */ | |
059ec3d9 | 368 | |
55414b25 JH |
369 | if (colon) |
370 | key = isalnum(*key) | |
371 | ? string_copyn(key, colon-key) /* the hostname */ | |
372 | : Ustrrchr(key, ':') + 1; /* Take from the last colon */ | |
059ec3d9 PH |
373 | |
374 | /* Sort out the keys */ | |
375 | ||
55414b25 JH |
376 | if (!Ustrchr(key, '@')) key = string_sprintf("*@%s", key); |
377 | if (alternate) alternate = string_sprintf("*@%s", alternate); | |
059ec3d9 PH |
378 | |
379 | /* Scan the configured retry items. */ | |
380 | ||
36d295f1 | 381 | for (yield = retries; yield; yield = yield->next) |
059ec3d9 | 382 | { |
55414b25 JH |
383 | const uschar *plist = yield->pattern; |
384 | const uschar *slist = yield->senders; | |
059ec3d9 PH |
385 | |
386 | /* If a specific error is set for this item, check that we are handling that | |
387 | specific error, and if so, check any additional error information if | |
388 | required. */ | |
389 | ||
390 | if (yield->basic_errno != 0) | |
391 | { | |
392 | /* Special code is required for quota errors, as these can either be system | |
393 | quota errors, or Exim's own quota imposition, which has a different error | |
394 | number. Full partitions are also treated in the same way as quota errors. | |
395 | */ | |
396 | ||
397 | if (yield->basic_errno == ERRNO_EXIMQUOTA) | |
398 | { | |
399 | if ((basic_errno != ERRNO_EXIMQUOTA && basic_errno != errno_quota && | |
400 | basic_errno != ENOSPC) || | |
401 | (yield->more_errno != 0 && yield->more_errno > more_errno)) | |
402 | continue; | |
403 | } | |
404 | ||
e97957bc PH |
405 | /* The TLSREQUIRED error also covers TLSFAILURE. These are subtly different |
406 | errors, but not worth separating at this level. */ | |
407 | ||
408 | else if (yield->basic_errno == ERRNO_TLSREQUIRED) | |
409 | { | |
410 | if (basic_errno != ERRNO_TLSREQUIRED && basic_errno != ERRNO_TLSFAILURE) | |
411 | continue; | |
412 | } | |
413 | ||
414 | /* Handle 4xx responses to MAIL, RCPT, or DATA. The code that was received | |
415 | is in the 2nd least significant byte of more_errno (with 400 subtracted). | |
416 | The required value is coded in the 2nd least significant byte of the | |
417 | yield->more_errno field as follows: | |
059ec3d9 PH |
418 | |
419 | 255 => any 4xx code | |
420 | >= 100 => the decade must match the value less 100 | |
421 | < 100 => the exact value must match | |
422 | */ | |
423 | ||
e97957bc PH |
424 | else if (yield->basic_errno == ERRNO_MAIL4XX || |
425 | yield->basic_errno == ERRNO_RCPT4XX || | |
426 | yield->basic_errno == ERRNO_DATA4XX) | |
059ec3d9 PH |
427 | { |
428 | int wanted; | |
e97957bc | 429 | if (basic_errno != yield->basic_errno) continue; |
059ec3d9 PH |
430 | wanted = (yield->more_errno >> 8) & 255; |
431 | if (wanted != 255) | |
432 | { | |
433 | int evalue = (more_errno >> 8) & 255; | |
434 | if (wanted >= 100) | |
435 | { | |
436 | if ((evalue/10)*10 != wanted - 100) continue; | |
437 | } | |
438 | else if (evalue != wanted) continue; | |
439 | } | |
440 | } | |
441 | ||
442 | /* There are some special cases for timeouts */ | |
443 | ||
444 | else if (yield->basic_errno == ETIMEDOUT) | |
445 | { | |
446 | if (basic_errno != ETIMEDOUT) continue; | |
447 | ||
448 | /* Just RTEF_CTOUT in the rule => don't care about 'A'/'M' addresses */ | |
449 | if (yield->more_errno == RTEF_CTOUT) | |
450 | { | |
451 | if ((more_errno & RTEF_CTOUT) == 0) continue; | |
452 | } | |
453 | ||
454 | else if (yield->more_errno != 0) | |
455 | { | |
456 | int cf_errno = more_errno; | |
457 | if ((yield->more_errno & RTEF_CTOUT) == 0) cf_errno &= ~RTEF_CTOUT; | |
458 | if (yield->more_errno != cf_errno) continue; | |
459 | } | |
460 | } | |
461 | ||
462 | /* Default checks for exact match */ | |
463 | ||
464 | else | |
465 | { | |
466 | if (yield->basic_errno != basic_errno || | |
467 | (yield->more_errno != 0 && yield->more_errno != more_errno)) | |
468 | continue; | |
469 | } | |
470 | } | |
471 | ||
472 | /* If the "senders" condition is set, check it. Note that sender_address may | |
473 | be null during -brt checking, in which case we do not use this rule. */ | |
474 | ||
36d295f1 JH |
475 | if ( slist |
476 | && ( !sender_address | |
477 | || match_address_list_basic(sender_address, &slist, 0) != OK | |
478 | ) ) | |
059ec3d9 PH |
479 | continue; |
480 | ||
481 | /* Check for a match between the address list item at the start of this retry | |
482 | rule and either the main or alternate keys. */ | |
483 | ||
36d295f1 JH |
484 | if ( match_address_list_basic(key, &plist, UCHAR_MAX+1) == OK |
485 | || ( alternate | |
486 | && match_address_list_basic(alternate, &plist, UCHAR_MAX+1) == OK | |
487 | ) ) | |
059ec3d9 PH |
488 | break; |
489 | } | |
490 | ||
059ec3d9 PH |
491 | return yield; |
492 | } | |
493 | ||
494 | ||
495 | ||
496 | ||
497 | /************************************************* | |
498 | * Update retry database * | |
499 | *************************************************/ | |
500 | ||
501 | /* Update the retry data for any directing/routing/transporting that was | |
502 | deferred, or delete it for those that succeeded after a previous defer. This is | |
503 | done all in one go to minimize opening/closing/locking of the database file. | |
504 | ||
505 | Note that, because SMTP delivery involves a list of destinations to try, there | |
506 | may be defer-type retry information for some of them even when the message was | |
507 | successfully delivered. Likewise if it eventually failed. | |
508 | ||
509 | This function may move addresses from the defer to the failed queue if the | |
510 | ultimate retry time has expired. | |
511 | ||
512 | Arguments: | |
513 | addr_defer queue of deferred addresses | |
514 | addr_failed queue of failed addresses | |
515 | addr_succeed queue of successful addresses | |
516 | ||
517 | Returns: nothing | |
518 | */ | |
519 | ||
520 | void | |
521 | retry_update(address_item **addr_defer, address_item **addr_failed, | |
522 | address_item **addr_succeed) | |
523 | { | |
524 | open_db dbblock; | |
525 | open_db *dbm_file = NULL; | |
526 | time_t now = time(NULL); | |
527 | int i; | |
528 | ||
529 | DEBUG(D_retry) debug_printf("Processing retry items\n"); | |
530 | ||
531 | /* Three-times loop to handle succeeded, failed, and deferred addresses. | |
532 | Deferred addresses must be handled after failed ones, because some may be moved | |
533 | to the failed chain if they have timed out. */ | |
534 | ||
535 | for (i = 0; i < 3; i++) | |
536 | { | |
537 | address_item *endaddr, *addr; | |
538 | address_item *last_first = NULL; | |
7b4c8c1f JH |
539 | address_item **paddr = i==0 ? addr_succeed : |
540 | i==1 ? addr_failed : addr_defer; | |
059ec3d9 PH |
541 | address_item **saved_paddr = NULL; |
542 | ||
7b4c8c1f JH |
543 | DEBUG(D_retry) debug_printf("%s addresses:\n", |
544 | i == 0 ? "Succeeded" : i == 1 ? "Failed" : "Deferred"); | |
059ec3d9 PH |
545 | |
546 | /* Loop for each address on the chain. For deferred addresses, the whole | |
547 | address times out unless one of its retry addresses has a retry rule that | |
548 | hasn't yet timed out. Deferred addresses should not be requesting deletion | |
549 | of retry items, but just in case they do by accident, treat that case | |
550 | as "not timed out". | |
551 | ||
552 | As well as handling the addresses themselves, we must also process any | |
553 | retry items for any parent addresses - these are typically "delete" items, | |
554 | because the parent must have succeeded in order to generate the child. */ | |
555 | ||
7b4c8c1f | 556 | while ((endaddr = *paddr)) |
059ec3d9 PH |
557 | { |
558 | BOOL timed_out = FALSE; | |
559 | retry_item *rti; | |
560 | ||
7b4c8c1f | 561 | for (addr = endaddr; addr; addr = addr->parent) |
059ec3d9 PH |
562 | { |
563 | int update_count = 0; | |
564 | int timedout_count = 0; | |
565 | ||
7b4c8c1f JH |
566 | DEBUG(D_retry) debug_printf(" %s%s\n", addr->address, |
567 | addr->retries ? "" : ": no retry items"); | |
059ec3d9 PH |
568 | |
569 | /* Loop for each retry item. */ | |
570 | ||
7b4c8c1f | 571 | for (rti = addr->retries; rti; rti = rti->next) |
059ec3d9 PH |
572 | { |
573 | uschar *message; | |
574 | int message_length, message_space, failing_interval, next_try; | |
575 | retry_rule *rule, *final_rule; | |
576 | retry_config *retry; | |
577 | dbdata_retry *retry_record; | |
578 | ||
579 | /* Open the retry database if it is not already open; failure to open | |
580 | the file is logged, but otherwise ignored - deferred addresses will | |
581 | get retried at the next opportunity. Not opening earlier than this saves | |
582 | opening if no addresses have retry items - common when none have yet | |
583 | reached their retry next try time. */ | |
584 | ||
7b4c8c1f | 585 | if (!dbm_file) |
059ec3d9 PH |
586 | dbm_file = dbfn_open(US"retry", O_RDWR, &dbblock, TRUE); |
587 | ||
7b4c8c1f | 588 | if (!dbm_file) |
059ec3d9 PH |
589 | { |
590 | DEBUG(D_deliver|D_retry|D_hints_lookup) | |
591 | debug_printf("retry database not available for updating\n"); | |
592 | return; | |
593 | } | |
594 | ||
595 | /* If there are no deferred addresses, that is, if this message is | |
596 | completing, and the retry item is for a message-specific SMTP error, | |
597 | force it to be deleted, because there's no point in keeping data for | |
598 | no-longer-existing messages. This situation can occur when a domain has | |
599 | two hosts and a message-specific error occurs for the first of them, | |
600 | but the address gets delivered to the second one. This optimization | |
601 | doesn't succeed in cleaning out all the dead entries, but it helps. */ | |
602 | ||
7b4c8c1f | 603 | if (!*addr_defer && rti->flags & rf_message) |
059ec3d9 PH |
604 | rti->flags |= rf_delete; |
605 | ||
606 | /* Handle the case of a request to delete the retry info for this | |
607 | destination. */ | |
608 | ||
7b4c8c1f | 609 | if (rti->flags & rf_delete) |
059ec3d9 PH |
610 | { |
611 | (void)dbfn_delete(dbm_file, rti->key); | |
612 | DEBUG(D_retry) | |
613 | debug_printf("deleted retry information for %s\n", rti->key); | |
614 | continue; | |
615 | } | |
616 | ||
617 | /* Count the number of non-delete retry items. This is so that we | |
618 | can compare it to the count of timed_out ones, to check whether | |
619 | all are timed out. */ | |
620 | ||
621 | update_count++; | |
622 | ||
623 | /* Get the retry information for this destination and error code, if | |
624 | any. If this item is for a remote host with ip address, then pass | |
625 | the domain name as an alternative to search for. If no retry | |
626 | information is found, we can't generate a retry time, so there is | |
627 | no point updating the database. This retry item is timed out. */ | |
628 | ||
7b4c8c1f JH |
629 | if (!(retry = retry_find_config(rti->key + 2, |
630 | rti->flags & rf_host ? addr->domain : NULL, | |
631 | rti->basic_errno, rti->more_errno))) | |
059ec3d9 PH |
632 | { |
633 | DEBUG(D_retry) debug_printf("No configured retry item for %s%s%s\n", | |
634 | rti->key, | |
7b4c8c1f JH |
635 | rti->flags & rf_host ? US" or " : US"", |
636 | rti->flags & rf_host ? addr->domain : US""); | |
059ec3d9 PH |
637 | if (addr == endaddr) timedout_count++; |
638 | continue; | |
639 | } | |
640 | ||
641 | DEBUG(D_retry) | |
7b4c8c1f | 642 | if (rti->flags & rf_host) |
ea49d0e1 PH |
643 | debug_printf("retry for %s (%s) = %s %d %d\n", rti->key, |
644 | addr->domain, retry->pattern, retry->basic_errno, | |
645 | retry->more_errno); | |
059ec3d9 | 646 | else |
ea49d0e1 PH |
647 | debug_printf("retry for %s = %s %d %d\n", rti->key, retry->pattern, |
648 | retry->basic_errno, retry->more_errno); | |
059ec3d9 PH |
649 | |
650 | /* Set up the message for the database retry record. Because DBM | |
651 | records have a maximum data length, we enforce a limit. There isn't | |
652 | much point in keeping a huge message here, anyway. */ | |
653 | ||
7b4c8c1f JH |
654 | message = rti->basic_errno > 0 |
655 | ? US strerror(rti->basic_errno) | |
656 | : rti->message | |
657 | ? US string_printing(rti->message) | |
658 | : US"unknown error"; | |
059ec3d9 PH |
659 | message_length = Ustrlen(message); |
660 | if (message_length > 150) message_length = 150; | |
661 | ||
662 | /* Read a retry record from the database or construct a new one. | |
663 | Ignore an old one if it is too old since it was last updated. */ | |
664 | ||
665 | retry_record = dbfn_read(dbm_file, rti->key); | |
7b4c8c1f JH |
666 | if ( retry_record |
667 | && now - retry_record->time_stamp > retry_data_expire) | |
059ec3d9 PH |
668 | retry_record = NULL; |
669 | ||
7b4c8c1f | 670 | if (!retry_record) |
059ec3d9 PH |
671 | { |
672 | retry_record = store_get(sizeof(dbdata_retry) + message_length); | |
673 | message_space = message_length; | |
674 | retry_record->first_failed = now; | |
675 | retry_record->last_try = now; | |
676 | retry_record->next_try = now; | |
677 | retry_record->expired = FALSE; | |
678 | retry_record->text[0] = 0; /* just in case */ | |
679 | } | |
680 | else message_space = Ustrlen(retry_record->text); | |
681 | ||
682 | /* Compute how long this destination has been failing */ | |
683 | ||
684 | failing_interval = now - retry_record->first_failed; | |
727071f8 PH |
685 | DEBUG(D_retry) debug_printf("failing_interval=%d message_age=%d\n", |
686 | failing_interval, message_age); | |
687 | ||
dd16e114 PH |
688 | /* For a non-host error, if the message has been on the queue longer |
689 | than the recorded time of failure, use the message's age instead. This | |
690 | can happen when some messages can be delivered and others cannot; a | |
691 | successful delivery will reset the first_failed time, and this can lead | |
692 | to a failing message being retried too often. */ | |
727071f8 | 693 | |
7b4c8c1f | 694 | if (!(rti->flags & rf_host) && message_age > failing_interval) |
dd16e114 | 695 | failing_interval = message_age; |
059ec3d9 PH |
696 | |
697 | /* Search for the current retry rule. The cutoff time of the | |
698 | last rule is handled differently to the others. The rule continues | |
699 | to operate for ever (the global maximum interval will eventually | |
700 | limit the gaps) but its cutoff time determines when an individual | |
701 | destination times out. If there are no retry rules, the destination | |
702 | always times out, but we can't compute a retry time. */ | |
703 | ||
704 | final_rule = NULL; | |
7b4c8c1f | 705 | for (rule = retry->rules; rule; rule = rule->next) |
059ec3d9 PH |
706 | { |
707 | if (failing_interval <= rule->timeout) break; | |
708 | final_rule = rule; | |
709 | } | |
710 | ||
711 | /* If there's an un-timed out rule, the destination has not | |
712 | yet timed out, so the address as a whole has not timed out (but we are | |
713 | interested in this only for the end address). Make sure the expired | |
714 | flag is false (can be forced via fixdb from outside, but ensure it is | |
715 | consistent with the rules whenever we go through here). */ | |
716 | ||
7b4c8c1f | 717 | if (rule) |
059ec3d9 | 718 | retry_record->expired = FALSE; |
059ec3d9 PH |
719 | |
720 | /* Otherwise, set the retry timeout expired, and set the final rule | |
721 | as the one from which to compute the next retry time. Subsequent | |
722 | messages will fail immediately until the retry time is reached (unless | |
723 | there are other, still active, retries). */ | |
724 | ||
725 | else | |
726 | { | |
727 | rule = final_rule; | |
728 | retry_record->expired = TRUE; | |
729 | if (addr == endaddr) timedout_count++; | |
730 | } | |
731 | ||
732 | /* There is a special case to consider when some messages get through | |
733 | to a destination and others don't. This can happen locally when a | |
734 | large message pushes a user over quota, and it can happen remotely | |
735 | when a machine is on a dodgy Internet connection. The messages that | |
736 | get through wipe the retry information, causing those that don't to | |
737 | stay on the queue longer than the final retry time. In order to | |
738 | avoid this, we check, using the time of arrival of the message, to | |
739 | see if it has been on the queue for more than the final cutoff time, | |
740 | and if so, cause this retry item to time out, and the retry time to | |
741 | be set to "now" so that any subsequent messages in the same condition | |
742 | also get tried. We search for the last rule onwards from the one that | |
743 | is in use. If there are no retry rules for the item, rule will be null | |
744 | and timedout_count will already have been updated. | |
745 | ||
746 | This implements "timeout this rule if EITHER the host (or routing or | |
747 | directing) has been failing for more than the maximum time, OR if the | |
727071f8 PH |
748 | message has been on the queue for more than the maximum time." |
749 | ||
750 | February 2006: It is possible that this code is no longer needed | |
751 | following the change to the retry calculation to use the message age if | |
752 | it is larger than the time since first failure. It may be that the | |
753 | expired flag is always set when the other conditions are met. However, | |
754 | this is a small bit of code, and it does no harm to leave it in place, | |
755 | just in case. */ | |
059ec3d9 | 756 | |
32dfdf8b | 757 | if ( received_time.tv_sec <= retry_record->first_failed |
7b4c8c1f JH |
758 | && addr == endaddr |
759 | && !retry_record->expired | |
760 | && rule) | |
059ec3d9 PH |
761 | { |
762 | retry_rule *last_rule; | |
7b4c8c1f JH |
763 | for (last_rule = rule; last_rule->next; last_rule = last_rule->next) |
764 | ; | |
32dfdf8b | 765 | if (now - received_time.tv_sec > last_rule->timeout) |
059ec3d9 PH |
766 | { |
767 | DEBUG(D_retry) debug_printf("on queue longer than maximum retry\n"); | |
768 | timedout_count++; | |
769 | rule = NULL; | |
770 | } | |
771 | } | |
772 | ||
773 | /* Compute the next try time from the rule, subject to the global | |
774 | maximum, and update the retry database. If rule == NULL it means | |
775 | there were no rules at all (and the timeout will be set expired), | |
776 | or we have a message that is older than the final timeout. In this | |
777 | case set the next retry time to now, so that one delivery attempt | |
778 | happens for subsequent messages. */ | |
779 | ||
7b4c8c1f JH |
780 | if (!rule) |
781 | next_try = now; | |
782 | else | |
059ec3d9 | 783 | { |
7b4c8c1f JH |
784 | if (rule->rule == 'F') |
785 | next_try = now + rule->p1; | |
6af56900 | 786 | else /* rule = 'G' or 'H' */ |
059ec3d9 PH |
787 | { |
788 | int last_predicted_gap = | |
789 | retry_record->next_try - retry_record->last_try; | |
790 | int last_actual_gap = now - retry_record->last_try; | |
791 | int lastgap = (last_predicted_gap < last_actual_gap)? | |
792 | last_predicted_gap : last_actual_gap; | |
6af56900 PH |
793 | int next_gap = (lastgap * rule->p2)/1000; |
794 | if (rule->rule == 'G') | |
6af56900 | 795 | next_try = now + ((lastgap < rule->p1)? rule->p1 : next_gap); |
6af56900 PH |
796 | else /* The 'H' rule */ |
797 | { | |
798 | next_try = now + rule->p1; | |
799 | if (next_gap > rule->p1) | |
3cd34f13 PH |
800 | next_try += random_number(next_gap - rule->p1)/2 + |
801 | (next_gap - rule->p1)/2; | |
6af56900 | 802 | } |
059ec3d9 PH |
803 | } |
804 | } | |
805 | ||
806 | /* Impose a global retry max */ | |
807 | ||
808 | if (next_try - now > retry_interval_max) | |
809 | next_try = now + retry_interval_max; | |
810 | ||
811 | /* If the new message length is greater than the previous one, we | |
812 | have to copy the record first. */ | |
813 | ||
814 | if (message_length > message_space) | |
815 | { | |
816 | dbdata_retry *newr = store_get(sizeof(dbdata_retry) + message_length); | |
817 | memcpy(newr, retry_record, sizeof(dbdata_retry)); | |
818 | retry_record = newr; | |
819 | } | |
820 | ||
821 | /* Set up the retry record; message_length may be less than the string | |
822 | length for very long error strings. */ | |
823 | ||
824 | retry_record->last_try = now; | |
825 | retry_record->next_try = next_try; | |
826 | retry_record->basic_errno = rti->basic_errno; | |
827 | retry_record->more_errno = rti->more_errno; | |
828 | Ustrncpy(retry_record->text, message, message_length); | |
829 | retry_record->text[message_length] = 0; | |
830 | ||
831 | DEBUG(D_retry) | |
832 | { | |
833 | int letter = retry_record->more_errno & 255; | |
834 | debug_printf("Writing retry data for %s\n", rti->key); | |
835 | debug_printf(" first failed=%d last try=%d next try=%d expired=%d\n", | |
836 | (int)retry_record->first_failed, (int)retry_record->last_try, | |
837 | (int)retry_record->next_try, retry_record->expired); | |
838 | debug_printf(" errno=%d more_errno=", retry_record->basic_errno); | |
839 | if (letter == 'A' || letter == 'M') | |
840 | debug_printf("%d,%c", (retry_record->more_errno >> 8) & 255, | |
841 | letter); | |
842 | else | |
843 | debug_printf("%d", retry_record->more_errno); | |
844 | debug_printf(" %s\n", retry_record->text); | |
845 | } | |
846 | ||
847 | (void)dbfn_write(dbm_file, rti->key, retry_record, | |
848 | sizeof(dbdata_retry) + message_length); | |
849 | } /* Loop for each retry item */ | |
850 | ||
851 | /* If all the non-delete retry items are timed out, the address is | |
852 | timed out, provided that we didn't skip any hosts because their retry | |
853 | time was not reached (or because of hosts_max_try). */ | |
854 | ||
855 | if (update_count > 0 && update_count == timedout_count) | |
059ec3d9 PH |
856 | if (!testflag(endaddr, af_retry_skipped)) |
857 | { | |
858 | DEBUG(D_retry) debug_printf("timed out: all retries expired\n"); | |
859 | timed_out = TRUE; | |
860 | } | |
861 | else | |
059ec3d9 PH |
862 | DEBUG(D_retry) |
863 | debug_printf("timed out but some hosts were skipped\n"); | |
059ec3d9 PH |
864 | } /* Loop for an address and its parents */ |
865 | ||
866 | /* If this is a deferred address, and retry processing was requested by | |
867 | means of one or more retry items, and they all timed out, move the address | |
868 | to the failed queue, and restart this loop without updating paddr. | |
869 | ||
870 | If there were several addresses batched in the same remote delivery, only | |
871 | the original top one will have host retry items attached to it, but we want | |
872 | to handle all the same. Each will have a pointer back to its "top" address, | |
873 | and they will now precede the item with the retries because addresses are | |
874 | inverted when added to these final queues. We have saved information about | |
875 | them in passing (below) so they can all be cut out at once. */ | |
876 | ||
877 | if (i == 2) /* Handling defers */ | |
878 | { | |
7b4c8c1f | 879 | if (endaddr->retries && timed_out) |
059ec3d9 PH |
880 | { |
881 | if (last_first == endaddr) paddr = saved_paddr; | |
882 | addr = *paddr; | |
883 | *paddr = endaddr->next; | |
884 | ||
885 | endaddr->next = *addr_failed; | |
886 | *addr_failed = addr; | |
887 | ||
888 | for (;; addr = addr->next) | |
889 | { | |
890 | setflag(addr, af_retry_timedout); | |
891 | addr->message = (addr->message == NULL)? US"retry timeout exceeded" : | |
892 | string_sprintf("%s: retry timeout exceeded", addr->message); | |
fffffe4c PH |
893 | addr->user_message = (addr->user_message == NULL)? |
894 | US"retry timeout exceeded" : | |
895 | string_sprintf("%s: retry timeout exceeded", addr->user_message); | |
059ec3d9 PH |
896 | log_write(0, LOG_MAIN, "** %s%s%s%s: retry timeout exceeded", |
897 | addr->address, | |
898 | (addr->parent == NULL)? US"" : US" <", | |
899 | (addr->parent == NULL)? US"" : addr->parent->address, | |
900 | (addr->parent == NULL)? US"" : US">"); | |
901 | ||
902 | if (addr == endaddr) break; | |
903 | } | |
904 | ||
905 | continue; /* Restart from changed *paddr */ | |
906 | } | |
907 | ||
908 | /* This address is to remain on the defer chain. If it has a "first" | |
909 | pointer, save the pointer to it in case we want to fail the set of | |
910 | addresses when we get to the first one. */ | |
911 | ||
912 | if (endaddr->first != last_first) | |
913 | { | |
914 | last_first = endaddr->first; | |
915 | saved_paddr = paddr; | |
916 | } | |
917 | } | |
918 | ||
919 | /* All cases (succeed, fail, defer left on queue) */ | |
920 | ||
921 | paddr = &(endaddr->next); /* Advance to next address */ | |
922 | } /* Loop for all addresses */ | |
923 | } /* Loop for succeed, fail, defer */ | |
924 | ||
925 | /* Close and unlock the database */ | |
926 | ||
7b4c8c1f | 927 | if (dbm_file) dbfn_close(dbm_file); |
059ec3d9 PH |
928 | |
929 | DEBUG(D_retry) debug_printf("end of retry processing\n"); | |
930 | } | |
931 | ||
932 | /* End of retry.c */ |