Commit | Line | Data |
---|---|---|
059ec3d9 PH |
1 | /* $Cambridge: exim/src/src/dns.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ |
2 | ||
3 | /************************************************* | |
4 | * Exim - an Internet mail transport agent * | |
5 | *************************************************/ | |
6 | ||
7 | /* Copyright (c) University of Cambridge 1995 - 2004 */ | |
8 | /* See the file NOTICE for conditions of use and distribution. */ | |
9 | ||
10 | /* Functions for interfacing with the DNS. */ | |
11 | ||
12 | #include "exim.h" | |
13 | ||
14 | ||
15 | /* Function declaration needed for mutual recursion when A6 records | |
16 | are supported. */ | |
17 | ||
18 | #if HAVE_IPV6 | |
19 | #ifdef SUPPORT_A6 | |
20 | static void dns_complete_a6(dns_address ***, dns_answer *, dns_record *, | |
21 | int, uschar *); | |
22 | #endif | |
23 | #endif | |
24 | ||
25 | ||
26 | ||
27 | /************************************************* | |
28 | * Initialize and configure resolver * | |
29 | *************************************************/ | |
30 | ||
31 | /* Initialize the resolver and the storage for holding DNS answers if this is | |
32 | the first time we have been here, and set the resolver options. | |
33 | ||
34 | Arguments: | |
35 | qualify_single TRUE to set the RES_DEFNAMES option | |
36 | search_parents TRUE to set the RES_DNSRCH option | |
37 | ||
38 | Returns: nothing | |
39 | */ | |
40 | ||
41 | void | |
42 | dns_init(BOOL qualify_single, BOOL search_parents) | |
43 | { | |
44 | if ((_res.options & RES_INIT) == 0) | |
45 | { | |
46 | DEBUG(D_resolver) _res.options |= RES_DEBUG; /* For Cygwin */ | |
47 | res_init(); | |
48 | DEBUG(D_resolver) _res.options |= RES_DEBUG; | |
49 | } | |
50 | ||
51 | _res.options &= ~(RES_DNSRCH | RES_DEFNAMES); | |
52 | _res.options |= (qualify_single? RES_DEFNAMES : 0) | | |
53 | (search_parents? RES_DNSRCH : 0); | |
54 | if (dns_retrans > 0) _res.retrans = dns_retrans; | |
55 | if (dns_retry > 0) _res.retry = dns_retry; | |
56 | } | |
57 | ||
58 | ||
59 | ||
60 | /************************************************* | |
61 | * Build key name for PTR records * | |
62 | *************************************************/ | |
63 | ||
64 | /* This function inverts an IP address and adds the relevant domain, to produce | |
65 | a name that can be used to look up PTR records. | |
66 | ||
67 | Arguments: | |
68 | string the IP address as a string | |
69 | buffer a suitable buffer, long enough to hold the result | |
70 | ||
71 | Returns: nothing | |
72 | */ | |
73 | ||
74 | void | |
75 | dns_build_reverse(uschar *string, uschar *buffer) | |
76 | { | |
77 | uschar *p = string + Ustrlen(string); | |
78 | uschar *pp = buffer; | |
79 | ||
80 | /* Handle IPv4 address */ | |
81 | ||
82 | #if HAVE_IPV6 | |
83 | if (Ustrchr(string, ':') == NULL) | |
84 | #endif | |
85 | { | |
86 | int i; | |
87 | for (i = 0; i < 4; i++) | |
88 | { | |
89 | uschar *ppp = p; | |
90 | while (ppp > string && ppp[-1] != '.') ppp--; | |
91 | Ustrncpy(pp, ppp, p - ppp); | |
92 | pp += p - ppp; | |
93 | *pp++ = '.'; | |
94 | p = ppp - 1; | |
95 | } | |
96 | Ustrcpy(pp, "in-addr.arpa"); | |
97 | } | |
98 | ||
99 | /* Handle IPv6 address; convert to binary so as to fill out any | |
100 | abbreviation in the textual form. */ | |
101 | ||
102 | #if HAVE_IPV6 | |
103 | else | |
104 | { | |
105 | int i; | |
106 | int v6[4]; | |
107 | (void)host_aton(string, v6); | |
108 | ||
109 | /* The original specification for IPv6 reverse lookup was to invert each | |
110 | nibble, and look in the ip6.int domain. The domain was subsequently | |
111 | changed to ip6.arpa. */ | |
112 | ||
113 | for (i = 3; i >= 0; i--) | |
114 | { | |
115 | int j; | |
116 | for (j = 0; j < 32; j += 4) | |
117 | { | |
118 | sprintf(CS pp, "%x.", (v6[i] >> j) & 15); | |
119 | pp += 2; | |
120 | } | |
121 | } | |
122 | Ustrcpy(pp, "ip6.arpa."); | |
123 | ||
124 | /* Another way of doing IPv6 reverse lookups was proposed in conjunction | |
125 | with A6 records. However, it fell out of favour when they did. The | |
126 | alternative was to construct a binary key, and look in ip6.arpa. I tried | |
127 | to make this code do that, but I could not make it work on Solaris 8. The | |
128 | resolver seems to lose the initial backslash somehow. However, now that | |
129 | this style of reverse lookup has been dropped, it doesn't matter. These | |
130 | lines are left here purely for historical interest. */ | |
131 | ||
132 | /************************************************** | |
133 | Ustrcpy(pp, "\\[x"); | |
134 | pp += 3; | |
135 | ||
136 | for (i = 0; i < 4; i++) | |
137 | { | |
138 | sprintf(pp, "%08X", v6[i]); | |
139 | pp += 8; | |
140 | } | |
141 | Ustrcpy(pp, "].ip6.arpa."); | |
142 | **************************************************/ | |
143 | ||
144 | } | |
145 | #endif | |
146 | } | |
147 | ||
148 | ||
149 | ||
150 | ||
151 | /************************************************* | |
152 | * Get next DNS record from answer block * | |
153 | *************************************************/ | |
154 | ||
155 | /* Call this with reset == RESET_ANSWERS to scan the answer block, reset == | |
156 | RESET_ADDITIONAL to scan the additional records, and reset == RESET_NEXT to | |
157 | get the next record. The result is in static storage which must be copied if | |
158 | it is to be preserved. | |
159 | ||
160 | Arguments: | |
161 | dnsa pointer to dns answer block | |
162 | dnss pointer to dns scan block | |
163 | reset option specifing what portion to scan, as described above | |
164 | ||
165 | Returns: next dns record, or NULL when no more | |
166 | */ | |
167 | ||
168 | dns_record * | |
169 | dns_next_rr(dns_answer *dnsa, dns_scan *dnss, int reset) | |
170 | { | |
171 | HEADER *h = (HEADER *)dnsa->answer; | |
172 | int namelen; | |
173 | ||
174 | /* Reset the saved data when requested to, and skip to the first required RR */ | |
175 | ||
176 | if (reset != RESET_NEXT) | |
177 | { | |
178 | dnss->rrcount = ntohs(h->qdcount); | |
179 | dnss->aptr = dnsa->answer + sizeof(HEADER); | |
180 | ||
181 | /* Skip over questions; failure to expand the name just gives up */ | |
182 | ||
183 | while (dnss->rrcount-- > 0) | |
184 | { | |
185 | namelen = dn_expand(dnsa->answer, dnsa->answer + dnsa->answerlen, | |
186 | dnss->aptr, (DN_EXPAND_ARG4_TYPE) &(dnss->srr.name), DNS_MAXNAME); | |
187 | if (namelen < 0) { dnss->rrcount = 0; return NULL; } | |
188 | dnss->aptr += namelen + 4; /* skip name & type & class */ | |
189 | } | |
190 | ||
191 | /* Get the number of answer records. */ | |
192 | ||
193 | dnss->rrcount = ntohs(h->ancount); | |
194 | ||
195 | /* Skip over answers and NS records if wanting to look at the additional | |
196 | records. */ | |
197 | ||
198 | if (reset == RESET_ADDITIONAL) | |
199 | { | |
200 | dnss->rrcount += ntohs(h->nscount); | |
201 | while (dnss->rrcount-- > 0) | |
202 | { | |
203 | namelen = dn_expand(dnsa->answer, dnsa->answer + dnsa->answerlen, | |
204 | dnss->aptr, (DN_EXPAND_ARG4_TYPE) &(dnss->srr.name), DNS_MAXNAME); | |
205 | if (namelen < 0) { dnss->rrcount = 0; return NULL; } | |
206 | dnss->aptr += namelen + 8; /* skip name, type, class & TTL */ | |
207 | GETSHORT(dnss->srr.size, dnss->aptr); /* size of data portion */ | |
208 | dnss->aptr += dnss->srr.size; /* skip over it */ | |
209 | } | |
210 | dnss->rrcount = ntohs(h->arcount); | |
211 | } | |
212 | } | |
213 | ||
214 | ||
215 | /* The variable dnss->aptr is now pointing at the next RR, and dnss->rrcount | |
216 | contains the number of RR records left. */ | |
217 | ||
218 | if (dnss->rrcount-- <= 0) return NULL; | |
219 | ||
220 | /* If expanding the RR domain name fails, behave as if no more records | |
221 | (something safe). */ | |
222 | ||
223 | namelen = dn_expand(dnsa->answer, dnsa->answer + dnsa->answerlen, dnss->aptr, | |
224 | (DN_EXPAND_ARG4_TYPE) &(dnss->srr.name), DNS_MAXNAME); | |
225 | if (namelen < 0) { dnss->rrcount = 0; return NULL; } | |
226 | ||
227 | /* Move the pointer past the name and fill in the rest of the data structure | |
228 | from the following bytes. */ | |
229 | ||
230 | dnss->aptr += namelen; | |
231 | GETSHORT(dnss->srr.type, dnss->aptr); /* Record type */ | |
232 | dnss->aptr += 6; /* Don't want class or TTL */ | |
233 | GETSHORT(dnss->srr.size, dnss->aptr); /* Size of data portion */ | |
234 | dnss->srr.data = dnss->aptr; /* The record's data follows */ | |
235 | dnss->aptr += dnss->srr.size; /* Advance to next RR */ | |
236 | ||
237 | /* Return a pointer to the dns_record structure within the dns_answer. This is | |
238 | for convenience so that the scans can use nice-looking for loops. */ | |
239 | ||
240 | return &(dnss->srr); | |
241 | } | |
242 | ||
243 | ||
244 | ||
245 | ||
246 | /************************************************* | |
247 | * Turn DNS type into text * | |
248 | *************************************************/ | |
249 | ||
250 | /* Turn the coded record type into a string for printing. | |
251 | ||
252 | Argument: record type | |
253 | Returns: pointer to string | |
254 | */ | |
255 | ||
256 | uschar * | |
257 | dns_text_type(int t) | |
258 | { | |
259 | switch(t) | |
260 | { | |
261 | case T_A: return US"A"; | |
262 | case T_MX: return US"MX"; | |
263 | case T_AAAA: return US"AAAA"; | |
264 | case T_A6: return US"A6"; | |
265 | case T_TXT: return US"TXT"; | |
266 | case T_PTR: return US"PTR"; | |
267 | case T_SRV: return US"SRV"; | |
268 | default: return US"?"; | |
269 | } | |
270 | } | |
271 | ||
272 | ||
273 | ||
274 | /************************************************* | |
275 | * Cache a failed DNS lookup result * | |
276 | *************************************************/ | |
277 | ||
278 | /* We cache failed lookup results so as not to experience timeouts many | |
279 | times for the same domain. We need to retain the resolver options because they | |
280 | may change. For successful lookups, we rely on resolver and/or name server | |
281 | caching. | |
282 | ||
283 | Arguments: | |
284 | name the domain name | |
285 | type the lookup type | |
286 | rc the return code | |
287 | ||
288 | Returns: the return code | |
289 | */ | |
290 | ||
291 | static int | |
292 | dns_return(uschar *name, int type, int rc) | |
293 | { | |
294 | tree_node *node = store_get_perm(sizeof(tree_node) + 290); | |
295 | sprintf(CS node->name, "%.255s-%s-%lx", name, dns_text_type(type), | |
296 | _res.options); | |
297 | node->data.val = rc; | |
298 | (void)tree_insertnode(&tree_dns_fails, node); | |
299 | return rc; | |
300 | } | |
301 | ||
302 | ||
303 | ||
304 | /************************************************* | |
305 | * Do basic DNS lookup * | |
306 | *************************************************/ | |
307 | ||
308 | /* Call the resolver to look up the given domain name, using the given type, | |
309 | and check the result. The error code TRY_AGAIN is documented as meaning "non- | |
310 | Authoritive Host not found, or SERVERFAIL". Sometimes there are badly set | |
311 | up nameservers that produce this error continually, so there is the option of | |
312 | providing a list of domains for which this is treated as a non-existent | |
313 | host. | |
314 | ||
315 | Arguments: | |
316 | dnsa pointer to dns_answer structure | |
317 | name name to look up | |
318 | type type of DNS record required (T_A, T_MX, etc) | |
319 | ||
320 | Returns: DNS_SUCCEED successful lookup | |
321 | DNS_NOMATCH name not found (NXDOMAIN) | |
322 | or name contains illegal characters (if checking) | |
323 | DNS_NODATA domain exists, but no data for this type (NODATA) | |
324 | DNS_AGAIN soft failure, try again later | |
325 | DNS_FAIL DNS failure | |
326 | */ | |
327 | ||
328 | int | |
329 | dns_basic_lookup(dns_answer *dnsa, uschar *name, int type) | |
330 | { | |
331 | #ifndef STAND_ALONE | |
332 | int rc; | |
333 | uschar *save; | |
334 | #endif | |
335 | ||
336 | tree_node *previous; | |
337 | uschar node_name[290]; | |
338 | ||
339 | /* DNS lookup failures of any kind are cached in a tree. This is mainly so that | |
340 | a timeout on one domain doesn't happen time and time again for messages that | |
341 | have many addresses in the same domain. We rely on the resolver and name server | |
342 | caching for successful lookups. */ | |
343 | ||
344 | sprintf(CS node_name, "%.255s-%s-%lx", name, dns_text_type(type), | |
345 | _res.options); | |
346 | previous = tree_search(tree_dns_fails, node_name); | |
347 | if (previous != NULL) | |
348 | { | |
349 | DEBUG(D_dns) debug_printf("DNS lookup of %.255s-%s: using cached value %s\n", | |
350 | name, dns_text_type(type), | |
351 | (previous->data.val == DNS_NOMATCH)? "DNS_NOMATCH" : | |
352 | (previous->data.val == DNS_NODATA)? "DNS_NODATA" : | |
353 | (previous->data.val == DNS_AGAIN)? "DNS_AGAIN" : | |
354 | (previous->data.val == DNS_FAIL)? "DNS_FAIL" : "??"); | |
355 | return previous->data.val; | |
356 | } | |
357 | ||
358 | /* If we are running in the test harness, recognize a couple of special | |
359 | names that always give error returns. This makes it straightforward to | |
360 | test the handling of DNS errors. */ | |
361 | ||
362 | if (running_in_test_harness) | |
363 | { | |
364 | uschar *endname = name + Ustrlen(name); | |
365 | if (Ustrcmp(endname - 14, "test.again.dns") == 0) | |
366 | { | |
367 | int delay = Uatoi(name); /* digits at the start of the name */ | |
368 | DEBUG(D_dns) debug_printf("Real DNS lookup of %s (%s) bypassed for testing\n", | |
369 | name, dns_text_type(type)); | |
370 | if (delay > 0) | |
371 | { | |
372 | DEBUG(D_dns) debug_printf("delaying %d seconds\n", delay); | |
373 | sleep(delay); | |
374 | } | |
375 | DEBUG(D_dns) debug_printf("returning DNS_AGAIN\n"); | |
376 | return dns_return(name, type, DNS_AGAIN); | |
377 | } | |
378 | if (Ustrcmp(endname - 13, "test.fail.dns") == 0) | |
379 | { | |
380 | DEBUG(D_dns) debug_printf("Real DNS lookup of %s (%s) bypassed for testing\n", | |
381 | name, dns_text_type(type)); | |
382 | DEBUG(D_dns) debug_printf("returning DNS_FAIL\n"); | |
383 | return dns_return(name, type, DNS_FAIL); | |
384 | } | |
385 | } | |
386 | ||
387 | /* If configured, check the hygene of the name passed to lookup. Otherwise, | |
388 | although DNS lookups may give REFUSED at the lower level, some resolvers | |
389 | turn this into TRY_AGAIN, which is silly. Give a NOMATCH return, since such | |
390 | domains cannot be in the DNS. The check is now done by a regular expression; | |
391 | give it space for substring storage to save it having to get its own if the | |
392 | regex has substrings that are used - the default uses a conditional. | |
393 | ||
394 | This test is omitted for PTR records. These occur only in calls from the dnsdb | |
395 | lookup, which constructs the names itself, so they should be OK. Besides, | |
396 | bitstring labels don't conform to normal name syntax. (But the aren't used any | |
397 | more.) | |
398 | ||
399 | For SRV records, we omit the initial _smtp._tcp. components at the start. */ | |
400 | ||
401 | #ifndef STAND_ALONE /* Omit this for stand-alone tests */ | |
402 | ||
403 | if (check_dns_names_pattern[0] != 0 && type != T_PTR) | |
404 | { | |
405 | uschar *checkname = name; | |
406 | int ovector[3*(EXPAND_MAXN+1)]; | |
407 | ||
408 | if (regex_check_dns_names == NULL) | |
409 | regex_check_dns_names = | |
410 | regex_must_compile(check_dns_names_pattern, FALSE, TRUE); | |
411 | ||
412 | /* For an SRV lookup, skip over the first two components (the service and | |
413 | protocol names, which both start with an underscore). */ | |
414 | ||
415 | if (type == T_SRV) | |
416 | { | |
417 | while (*checkname++ != '.'); | |
418 | while (*checkname++ != '.'); | |
419 | } | |
420 | ||
421 | if (pcre_exec(regex_check_dns_names, NULL, CS checkname, Ustrlen(checkname), | |
422 | 0, PCRE_EOPT, ovector, sizeof(ovector)/sizeof(int)) < 0) | |
423 | { | |
424 | DEBUG(D_dns) | |
425 | debug_printf("DNS name syntax check failed: %s (%s)\n", name, | |
426 | dns_text_type(type)); | |
427 | host_find_failed_syntax = TRUE; | |
428 | return DNS_NOMATCH; | |
429 | } | |
430 | } | |
431 | ||
432 | #endif /* STAND_ALONE */ | |
433 | ||
434 | /* Call the resolver; for an overlong response, res_search() will return the | |
435 | number of bytes the message would need, so we need to check for this case. | |
436 | The effect is to truncate overlong data. */ | |
437 | ||
438 | dnsa->answerlen = res_search(CS name, C_IN, type, dnsa->answer, MAXPACKET); | |
439 | if (dnsa->answerlen > MAXPACKET) dnsa->answerlen = MAXPACKET; | |
440 | ||
441 | if (dnsa->answerlen < 0) switch (h_errno) | |
442 | { | |
443 | case HOST_NOT_FOUND: | |
444 | DEBUG(D_dns) debug_printf("DNS lookup of %s (%s) gave HOST_NOT_FOUND\n" | |
445 | "returning DNS_NOMATCH\n", name, dns_text_type(type)); | |
446 | return dns_return(name, type, DNS_NOMATCH); | |
447 | ||
448 | case TRY_AGAIN: | |
449 | DEBUG(D_dns) debug_printf("DNS lookup of %s (%s) gave TRY_AGAIN\n", | |
450 | name, dns_text_type(type)); | |
451 | ||
452 | /* Cut this out for various test programs */ | |
453 | #ifndef STAND_ALONE | |
454 | save = deliver_domain; | |
455 | deliver_domain = name; /* set $domain */ | |
456 | rc = match_isinlist(name, &dns_again_means_nonexist, 0, NULL, NULL, | |
457 | MCL_DOMAIN, TRUE, NULL); | |
458 | deliver_domain = save; | |
459 | if (rc != OK) | |
460 | { | |
461 | DEBUG(D_dns) debug_printf("returning DNS_AGAIN\n"); | |
462 | return dns_return(name, type, DNS_AGAIN); | |
463 | } | |
464 | DEBUG(D_dns) debug_printf("%s is in dns_again_means_nonexist: returning " | |
465 | "DNS_NOMATCH\n", name); | |
466 | return dns_return(name, type, DNS_NOMATCH); | |
467 | ||
468 | #else /* For stand-alone tests */ | |
469 | return dns_return(name, type, DNS_AGAIN); | |
470 | #endif | |
471 | ||
472 | case NO_RECOVERY: | |
473 | DEBUG(D_dns) debug_printf("DNS lookup of %s (%s) gave NO_RECOVERY\n" | |
474 | "returning DNS_FAIL\n", name, dns_text_type(type)); | |
475 | return dns_return(name, type, DNS_FAIL); | |
476 | ||
477 | case NO_DATA: | |
478 | DEBUG(D_dns) debug_printf("DNS lookup of %s (%s) gave NO_DATA\n" | |
479 | "returning DNS_NODATA\n", name, dns_text_type(type)); | |
480 | return dns_return(name, type, DNS_NODATA); | |
481 | ||
482 | default: | |
483 | DEBUG(D_dns) debug_printf("DNS lookup of %s (%s) gave unknown DNS error %d\n" | |
484 | "returning DNS_FAIL\n", name, dns_text_type(type), h_errno); | |
485 | return dns_return(name, type, DNS_FAIL); | |
486 | } | |
487 | ||
488 | DEBUG(D_dns) debug_printf("DNS lookup of %s (%s) succeeded\n", | |
489 | name, dns_text_type(type)); | |
490 | ||
491 | return DNS_SUCCEED; | |
492 | } | |
493 | ||
494 | ||
495 | ||
496 | ||
497 | /************************************************ | |
498 | * Do a DNS lookup and handle CNAMES * | |
499 | ************************************************/ | |
500 | ||
501 | /* Look up the given domain name, using the given type. Follow CNAMEs if | |
502 | necessary, but only so many times. There aren't supposed to be CNAME chains in | |
503 | the DNS, but you are supposed to cope with them if you find them. | |
504 | ||
505 | The assumption is made that if the resolver gives back records of the | |
506 | requested type *and* a CNAME, we don't need to make another call to look up | |
507 | the CNAME. I can't see how it could return only some of the right records. If | |
508 | it's done a CNAME lookup in the past, it will have all of them; if not, it | |
509 | won't return any. | |
510 | ||
511 | If fully_qualified_name is not NULL, set it to point to the full name | |
512 | returned by the resolver, if this is different to what it is given, unless | |
513 | the returned name starts with "*" as some nameservers seem to be returning | |
514 | wildcards in this form. | |
515 | ||
516 | Arguments: | |
517 | dnsa pointer to dns_answer structure | |
518 | name domain name to look up | |
519 | type DNS record type (T_A, T_MX, etc) | |
520 | fully_qualified_name if not NULL, return the returned name here if its | |
521 | contents are different (i.e. it must be preset) | |
522 | ||
523 | Returns: DNS_SUCCEED successful lookup | |
524 | DNS_NOMATCH name not found | |
525 | DNS_NODATA no data found | |
526 | DNS_AGAIN soft failure, try again later | |
527 | DNS_FAIL DNS failure | |
528 | */ | |
529 | ||
530 | int | |
531 | dns_lookup(dns_answer *dnsa, uschar *name, int type, uschar **fully_qualified_name) | |
532 | { | |
533 | int i; | |
534 | uschar *orig_name = name; | |
535 | ||
536 | /* Loop to follow CNAME chains so far, but no further... */ | |
537 | ||
538 | for (i = 0; i < 10; i++) | |
539 | { | |
540 | uschar data[256]; | |
541 | dns_record *rr, cname_rr, type_rr; | |
542 | dns_scan dnss; | |
543 | int datalen, rc; | |
544 | ||
545 | /* DNS lookup failures get passed straight back. */ | |
546 | ||
547 | if ((rc = dns_basic_lookup(dnsa, name, type)) != DNS_SUCCEED) return rc; | |
548 | ||
549 | /* We should have either records of the required type, or a CNAME record, | |
550 | or both. We need to know whether both exist for getting the fully qualified | |
551 | name, but avoid scanning more than necessary. Note that we must copy the | |
552 | contents of any rr blocks returned by dns_next_rr() as they use the same | |
553 | area in the dnsa block. */ | |
554 | ||
555 | cname_rr.data = type_rr.data = NULL; | |
556 | for (rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS); | |
557 | rr != NULL; | |
558 | rr = dns_next_rr(dnsa, &dnss, RESET_NEXT)) | |
559 | { | |
560 | if (rr->type == type) | |
561 | { | |
562 | if (type_rr.data == NULL) type_rr = *rr; | |
563 | if (cname_rr.data != NULL) break; | |
564 | } | |
565 | else if (rr->type == T_CNAME) cname_rr = *rr; | |
566 | } | |
567 | ||
568 | /* If a CNAME was found, take the fully qualified name from it; otherwise | |
569 | from the first data record, if present. For testing, there is a magic name | |
570 | that gets its casing adjusted, because my resolver doesn't seem to pass back | |
571 | upper case letters in domain names. */ | |
572 | ||
573 | if (fully_qualified_name != NULL) | |
574 | { | |
575 | if (cname_rr.data != NULL) | |
576 | { | |
577 | if (Ustrcmp(cname_rr.name, *fully_qualified_name) != 0 && | |
578 | cname_rr.name[0] != '*') | |
579 | *fully_qualified_name = string_copy_dnsdomain(cname_rr.name); | |
580 | } | |
581 | else if (type_rr.data != NULL) | |
582 | { | |
583 | if (running_in_test_harness && | |
584 | Ustrcmp(type_rr.name, "uppercase.test.ex") == 0) | |
585 | *fully_qualified_name = US"UpperCase.test.ex"; | |
586 | else | |
587 | { | |
588 | if (Ustrcmp(type_rr.name, *fully_qualified_name) != 0 && | |
589 | type_rr.name[0] != '*') | |
590 | *fully_qualified_name = string_copy_dnsdomain(type_rr.name); | |
591 | } | |
592 | } | |
593 | } | |
594 | ||
595 | /* If any data records of the correct type were found, we are done. */ | |
596 | ||
597 | if (type_rr.data != NULL) return DNS_SUCCEED; | |
598 | ||
599 | /* If there are no data records, we need to re-scan the DNS using the | |
600 | domain given in the CNAME record, which should exist (otherwise we should | |
601 | have had a failure from dns_lookup). However code against the possibility of | |
602 | its not existing. */ | |
603 | ||
604 | if (cname_rr.data == NULL) return DNS_FAIL; | |
605 | datalen = dn_expand(dnsa->answer, dnsa->answer + dnsa->answerlen, | |
606 | cname_rr.data, (DN_EXPAND_ARG4_TYPE)data, 256); | |
607 | if (datalen < 0) return DNS_FAIL; | |
608 | name = data; | |
609 | } /* Loop back to do another lookup */ | |
610 | ||
611 | /*Control reaches here after 10 times round the CNAME loop. Something isn't | |
612 | right... */ | |
613 | ||
614 | log_write(0, LOG_MAIN, "CNAME loop for %s encountered", orig_name); | |
615 | return DNS_FAIL; | |
616 | } | |
617 | ||
618 | ||
619 | ||
620 | /* Support for A6 records has been commented out since they were demoted to | |
621 | experimental status at IETF 51. */ | |
622 | ||
623 | #if HAVE_IPV6 && defined(SUPPORT_A6) | |
624 | ||
625 | /************************************************* | |
626 | * Search DNS block for prefix RRs * | |
627 | *************************************************/ | |
628 | ||
629 | /* Called from dns_complete_a6() to search an additional section or a main | |
630 | answer section for required prefix records to complete an IPv6 address obtained | |
631 | from an A6 record. For each prefix record, a recursive call to dns_complete_a6 | |
632 | is made, with a new copy of the address so far. | |
633 | ||
634 | Arguments: | |
635 | dnsa the DNS answer block | |
636 | which RESET_ADDITIONAL or RESET_ANSWERS | |
637 | name name of prefix record | |
638 | yptrptr pointer to the pointer that points to where to hang the next | |
639 | dns_address structure | |
640 | bits number of bits we have already got | |
641 | bitvec the bits we have already got | |
642 | ||
643 | Returns: TRUE if any records were found | |
644 | */ | |
645 | ||
646 | static BOOL | |
647 | dns_find_prefix(dns_answer *dnsa, int which, uschar *name, dns_address | |
648 | ***yptrptr, int bits, uschar *bitvec) | |
649 | { | |
650 | BOOL yield = FALSE; | |
651 | dns_record *rr; | |
652 | dns_scan dnss; | |
653 | ||
654 | for (rr = dns_next_rr(dnsa, &dnss, which); | |
655 | rr != NULL; | |
656 | rr = dns_next_rr(dnsa, &dnss, RESET_NEXT)) | |
657 | { | |
658 | uschar cbitvec[16]; | |
659 | if (rr->type != T_A6 || strcmpic(rr->name, name) != 0) continue; | |
660 | yield = TRUE; | |
661 | memcpy(cbitvec, bitvec, sizeof(cbitvec)); | |
662 | dns_complete_a6(yptrptr, dnsa, rr, bits, cbitvec); | |
663 | } | |
664 | ||
665 | return yield; | |
666 | } | |
667 | ||
668 | ||
669 | ||
670 | /************************************************* | |
671 | * Follow chains of A6 records * | |
672 | *************************************************/ | |
673 | ||
674 | /* A6 records may be incomplete, with pointers to other records containing more | |
675 | bits of the address. There can be a tree structure, leading to a number of | |
676 | addresses originating from a single initial A6 record. | |
677 | ||
678 | Arguments: | |
679 | yptrptr pointer to the pointer that points to where to hang the next | |
680 | dns_address structure | |
681 | dnsa the current DNS answer block | |
682 | rr the RR we have at present | |
683 | bits number of bits we have already got | |
684 | bitvec the bits we have already got | |
685 | ||
686 | Returns: nothing | |
687 | */ | |
688 | ||
689 | static void | |
690 | dns_complete_a6(dns_address ***yptrptr, dns_answer *dnsa, dns_record *rr, | |
691 | int bits, uschar *bitvec) | |
692 | { | |
693 | static uschar bitmask[] = { 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80 }; | |
694 | uschar *p = (uschar *)(rr->data); | |
695 | int prefix_len, suffix_len; | |
696 | int i, j, k; | |
697 | uschar *chainptr; | |
698 | uschar chain[264]; | |
699 | dns_answer cdnsa; | |
700 | ||
701 | /* The prefix length is the first byte. It defines the prefix which is missing | |
702 | from the data in this record as a number of bits. Zero means this is the end of | |
703 | a chain. The suffix is the data in this record; only sufficient bytes to hold | |
704 | it are supplied. There may be zero bytes. We have to ignore trailing bits that | |
705 | we have already obtained from earlier RRs in the chain. */ | |
706 | ||
707 | prefix_len = *p++; /* bits */ | |
708 | suffix_len = (128 - prefix_len + 7)/8; /* bytes */ | |
709 | ||
710 | /* If the prefix in this record is greater than the prefix in the previous | |
711 | record in the chain, we have to ignore the record (RFC 2874). */ | |
712 | ||
713 | if (prefix_len > 128 - bits) return; | |
714 | ||
715 | /* In this little loop, the number of bits up to and including the current byte | |
716 | is held in k. If we have none of the bits in this byte, we can just or it into | |
717 | the current data. If we have all of the bits in this byte, we skip it. | |
718 | Otherwise, some masking has to be done. */ | |
719 | ||
720 | for (i = suffix_len - 1, j = 15, k = 8; i >= 0; i--) | |
721 | { | |
722 | int required = k - bits; | |
723 | if (required >= 8) bitvec[j] |= p[i]; | |
724 | else if (required > 0) bitvec[j] |= p[i] & bitmask[required]; | |
725 | j--; /* I tried putting these in the "for" statement, but gcc muttered */ | |
726 | k += 8; /* about computed values not being used. */ | |
727 | } | |
728 | ||
729 | /* If the prefix_length is zero, we are at the end of a chain. Build a | |
730 | dns_address item with the current data, hang it onto the end of the chain, | |
731 | adjust the hanging pointer, and we are done. */ | |
732 | ||
733 | if (prefix_len == 0) | |
734 | { | |
735 | dns_address *new = store_get(sizeof(dns_address) + 50); | |
736 | inet_ntop(AF_INET6, bitvec, CS new->address, 50); | |
737 | new->next = NULL; | |
738 | **yptrptr = new; | |
739 | *yptrptr = &(new->next); | |
740 | return; | |
741 | } | |
742 | ||
743 | /* Prefix length is not zero. Reset the number of bits that we have collected | |
744 | so far, and extract the chain name. */ | |
745 | ||
746 | bits = 128 - prefix_len; | |
747 | p += suffix_len; | |
748 | ||
749 | chainptr = chain; | |
750 | while ((i = *p++) != 0) | |
751 | { | |
752 | if (chainptr != chain) *chainptr++ = '.'; | |
753 | memcpy(chainptr, p, i); | |
754 | chainptr += i; | |
755 | p += i; | |
756 | } | |
757 | *chainptr = 0; | |
758 | chainptr = chain; | |
759 | ||
760 | /* Now scan the current DNS response record to see if the additional section | |
761 | contains the records we want. This processing can be cut out for testing | |
762 | purposes. */ | |
763 | ||
764 | if (dns_find_prefix(dnsa, RESET_ADDITIONAL, chainptr, yptrptr, bits, bitvec)) | |
765 | return; | |
766 | ||
767 | /* No chain records were found in the current DNS response block. Do a new DNS | |
768 | lookup to try to find these records. This opens up the possibility of DNS | |
769 | failures. We ignore them at this point; if all branches of the tree fail, there | |
770 | will be no addresses at the end. */ | |
771 | ||
772 | if (dns_lookup(&cdnsa, chainptr, T_A6, NULL) == DNS_SUCCEED) | |
773 | (void)dns_find_prefix(&cdnsa, RESET_ANSWERS, chainptr, yptrptr, bits, bitvec); | |
774 | } | |
775 | #endif /* HAVE_IPV6 && defined(SUPPORT_A6) */ | |
776 | ||
777 | ||
778 | ||
779 | ||
780 | /************************************************* | |
781 | * Get address(es) from DNS record * | |
782 | *************************************************/ | |
783 | ||
784 | /* The record type is either T_A for an IPv4 address or T_AAAA (or T_A6 when | |
785 | supported) for an IPv6 address. In the A6 case, there may be several addresses, | |
786 | generated by following chains. A recursive function does all the hard work. A6 | |
787 | records now look like passing into history, so the code is only included when | |
788 | explicitly asked for. | |
789 | ||
790 | Argument: | |
791 | dnsa the DNS answer block | |
792 | rr the RR | |
793 | ||
794 | Returns: pointer a chain of dns_address items | |
795 | */ | |
796 | ||
797 | dns_address * | |
798 | dns_address_from_rr(dns_answer *dnsa, dns_record *rr) | |
799 | { | |
800 | dns_address *yield = NULL; | |
801 | ||
802 | #if HAVE_IPV6 && defined(SUPPORT_A6) | |
803 | dns_address **yieldptr = &yield; | |
804 | uschar bitvec[16]; | |
805 | #else | |
806 | dnsa = dnsa; /* Stop picky compilers warning */ | |
807 | #endif | |
808 | ||
809 | if (rr->type == T_A) | |
810 | { | |
811 | uschar *p = (uschar *)(rr->data); | |
812 | yield = store_get(sizeof(dns_address) + 20); | |
813 | (void)sprintf(CS yield->address, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); | |
814 | yield->next = NULL; | |
815 | } | |
816 | ||
817 | #if HAVE_IPV6 | |
818 | ||
819 | #ifdef SUPPORT_A6 | |
820 | else if (rr->type == T_A6) | |
821 | { | |
822 | memset(bitvec, 0, sizeof(bitvec)); | |
823 | dns_complete_a6(&yieldptr, dnsa, rr, 0, bitvec); | |
824 | } | |
825 | #endif /* SUPPORT_A6 */ | |
826 | ||
827 | else | |
828 | { | |
829 | yield = store_get(sizeof(dns_address) + 50); | |
830 | inet_ntop(AF_INET6, (uschar *)(rr->data), CS yield->address, 50); | |
831 | yield->next = NULL; | |
832 | } | |
833 | #endif /* HAVE_IPV6 */ | |
834 | ||
835 | return yield; | |
836 | } | |
837 | ||
838 | /* End of dns.c */ |