Import Debian changes 4.89-2+deb9u4
[hcoop/debian/exim4.git] / src / lookups / dnsdb.c
CommitLineData
420a0d19
CE
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
2813c06e 5/* Copyright (c) University of Cambridge 1995 - 2015 */
420a0d19
CE
6/* See the file NOTICE for conditions of use and distribution. */
7
8#include "../exim.h"
9#include "lf_functions.h"
10
11
12
13/* Ancient systems (e.g. SunOS4) don't appear to have T_TXT defined in their
14header files. */
15
16#ifndef T_TXT
17#define T_TXT 16
18#endif
19
20/* Many systems do not have T_SPF. */
21#ifndef T_SPF
22#define T_SPF 99
23#endif
24
25/* New TLSA record for DANE */
26#ifndef T_TLSA
27#define T_TLSA 52
28#endif
29
30/* Table of recognized DNS record types and their integer values. */
31
32static const char *type_names[] = {
33 "a",
34#if HAVE_IPV6
35 "a+",
36 "aaaa",
420a0d19
CE
37#endif
38 "cname",
39 "csa",
40 "mx",
41 "mxh",
42 "ns",
43 "ptr",
2813c06e 44 "soa",
420a0d19
CE
45 "spf",
46 "srv",
47 "tlsa",
48 "txt",
49 "zns"
50};
51
52static int type_values[] = {
53 T_A,
54#if HAVE_IPV6
55 T_ADDRESSES, /* Private type for AAAA + A */
56 T_AAAA,
420a0d19
CE
57#endif
58 T_CNAME,
59 T_CSA, /* Private type for "Client SMTP Authorization". */
60 T_MX,
61 T_MXH, /* Private type for "MX hostnames" */
62 T_NS,
63 T_PTR,
2813c06e 64 T_SOA,
420a0d19
CE
65 T_SPF,
66 T_SRV,
67 T_TLSA,
68 T_TXT,
69 T_ZNS /* Private type for "zone nameservers" */
70};
71
72
73/*************************************************
74* Open entry point *
75*************************************************/
76
77/* See local README for interface description. */
78
79static void *
80dnsdb_open(uschar *filename, uschar **errmsg)
81{
82filename = filename; /* Keep picky compilers happy */
83errmsg = errmsg; /* Ditto */
84return (void *)(-1); /* Any non-0 value */
85}
86
87
88
89/*************************************************
90* Find entry point for dnsdb *
91*************************************************/
92
93/* See local README for interface description. The query in the "keystring" may
94consist of a number of parts.
95
96(a) If the first significant character is '>' then the next character is the
97separator character that is used when multiple records are found. The default
98separator is newline.
99
100(b) If the next character is ',' then the next character is the separator
101character used for multiple items of text in "TXT" records. Alternatively,
102if the next character is ';' then these multiple items are concatenated with
103no separator. With neither of these options specified, only the first item
104is output. Similarly for "SPF" records, but the default for joining multiple
105items in one SPF record is the empty string, for direct concatenation.
106
2813c06e
CE
107(c) Options, all comma-terminated, in any order. Any unrecognised option
108terminates option processing. Recognised options are:
420a0d19 109
2813c06e
CE
110- 'defer_FOO': set the defer behaviour to FOO. The possible behaviours are:
111'strict', where any defer causes the whole lookup to defer; 'lax', where a defer
112causes the whole lookup to defer only if none of the DNS queries succeeds; and
113'never', where all defers are as if the lookup failed. The default is 'lax'.
114
115- 'dnssec_FOO', with 'strict', 'lax' and 'never' (default). The meanings are
420a0d19
CE
116require, try and don't-try dnssec respectively.
117
2813c06e
CE
118- 'retrans_VAL', set the timeout value. VAL is an Exim time specification
119(eg "5s"). The default is set by the main configuration option 'dns_retrans'.
120
121- 'retry_VAL', set the retry count on timeouts. VAL is an integer. The
122default is set by the main configuration option "dns_retry".
123
124(d) If the next sequence of characters is a sequence of letters and digits
420a0d19
CE
125followed by '=', it is interpreted as the name of the DNS record type. The
126default is "TXT".
127
2813c06e 128(e) Then there follows list of domain names. This is a generalized Exim list,
420a0d19
CE
129which may start with '<' in order to set a specific separator. The default
130separator, as always, is colon. */
131
132static int
2813c06e
CE
133dnsdb_find(void *handle, uschar *filename, const uschar *keystring, int length,
134 uschar **result, uschar **errmsg, uint *do_cache)
420a0d19
CE
135{
136int rc;
137int size = 256;
138int ptr = 0;
139int sep = 0;
140int defer_mode = PASS;
141int dnssec_mode = OK;
2813c06e
CE
142int save_retrans = dns_retrans;
143int save_retry = dns_retry;
420a0d19
CE
144int type;
145int failrc = FAIL;
2813c06e
CE
146const uschar *outsep = CUS"\n";
147const uschar *outsep2 = NULL;
420a0d19 148uschar *equals, *domain, *found;
420a0d19
CE
149
150/* Because we're the working in the search pool, we try to reclaim as much
151store as possible later, so we preallocate the result here */
152
153uschar *yield = store_get(size);
154
155dns_record *rr;
156dns_answer dnsa;
157dns_scan dnss;
158
159handle = handle; /* Keep picky compilers happy */
160filename = filename;
161length = length;
162do_cache = do_cache;
163
164/* If the string starts with '>' we change the output separator.
165If it's followed by ';' or ',' we set the TXT output separator. */
166
167while (isspace(*keystring)) keystring++;
168if (*keystring == '>')
169 {
170 outsep = keystring + 1;
171 keystring += 2;
172 if (*keystring == ',')
173 {
174 outsep2 = keystring + 1;
175 keystring += 2;
176 }
177 else if (*keystring == ';')
178 {
179 outsep2 = US"";
180 keystring++;
181 }
182 while (isspace(*keystring)) keystring++;
183 }
184
185/* Check for a modifier keyword. */
186
2813c06e 187for (;;)
420a0d19
CE
188 {
189 if (strncmpic(keystring, US"defer_", 6) == 0)
190 {
191 keystring += 6;
192 if (strncmpic(keystring, US"strict", 6) == 0)
2813c06e 193 { defer_mode = DEFER; keystring += 6; }
420a0d19 194 else if (strncmpic(keystring, US"lax", 3) == 0)
2813c06e 195 { defer_mode = PASS; keystring += 3; }
420a0d19 196 else if (strncmpic(keystring, US"never", 5) == 0)
2813c06e 197 { defer_mode = OK; keystring += 5; }
420a0d19
CE
198 else
199 {
200 *errmsg = US"unsupported dnsdb defer behaviour";
201 return DEFER;
202 }
203 }
2813c06e 204 else if (strncmpic(keystring, US"dnssec_", 7) == 0)
420a0d19
CE
205 {
206 keystring += 7;
207 if (strncmpic(keystring, US"strict", 6) == 0)
2813c06e 208 { dnssec_mode = DEFER; keystring += 6; }
420a0d19 209 else if (strncmpic(keystring, US"lax", 3) == 0)
2813c06e
CE
210 { dnssec_mode = PASS; keystring += 3; }
211 else if (strncmpic(keystring, US"never", 5) == 0)
212 { dnssec_mode = OK; keystring += 5; }
213 else
420a0d19 214 {
2813c06e
CE
215 *errmsg = US"unsupported dnsdb dnssec behaviour";
216 return DEFER;
420a0d19 217 }
2813c06e
CE
218 }
219 else if (strncmpic(keystring, US"retrans_", 8) == 0)
220 {
221 int timeout_sec;
222 if ((timeout_sec = readconf_readtime(keystring += 8, ',', FALSE)) <= 0)
420a0d19 223 {
2813c06e
CE
224 *errmsg = US"unsupported dnsdb timeout value";
225 return DEFER;
420a0d19 226 }
2813c06e
CE
227 dns_retrans = timeout_sec;
228 while (*keystring != ',') keystring++;
229 }
230 else if (strncmpic(keystring, US"retry_", 6) == 0)
231 {
232 int retries;
233 if ((retries = (int)strtol(CCS keystring + 6, CSS &keystring, 0)) < 0)
420a0d19 234 {
2813c06e 235 *errmsg = US"unsupported dnsdb retry count";
420a0d19
CE
236 return DEFER;
237 }
2813c06e 238 dns_retry = retries;
420a0d19 239 }
2813c06e
CE
240 else
241 break;
242
420a0d19
CE
243 while (isspace(*keystring)) keystring++;
244 if (*keystring++ != ',')
245 {
246 *errmsg = US"dnsdb modifier syntax error";
247 return DEFER;
248 }
249 while (isspace(*keystring)) keystring++;
250 }
251
252/* Figure out the "type" value if it is not T_TXT.
253If the keystring contains an = this must be preceded by a valid type name. */
254
255type = T_TXT;
256if ((equals = Ustrchr(keystring, '=')) != NULL)
257 {
258 int i, len;
259 uschar *tend = equals;
260
261 while (tend > keystring && isspace(tend[-1])) tend--;
262 len = tend - keystring;
263
2813c06e 264 for (i = 0; i < nelem(type_names); i++)
420a0d19
CE
265 if (len == Ustrlen(type_names[i]) &&
266 strncmpic(keystring, US type_names[i], len) == 0)
267 {
268 type = type_values[i];
269 break;
270 }
420a0d19 271
2813c06e 272 if (i >= nelem(type_names))
420a0d19
CE
273 {
274 *errmsg = US"unsupported DNS record type";
275 return DEFER;
276 }
277
278 keystring = equals + 1;
279 while (isspace(*keystring)) keystring++;
280 }
281
282/* Initialize the resolver in case this is the first time it has been used. */
283
284dns_init(FALSE, FALSE, dnssec_mode != OK);
285
286/* The remainder of the string must be a list of domains. As long as the lookup
287for at least one of them succeeds, we return success. Failure means that none
288of them were found.
289
290The original implementation did not support a list of domains. Adding the list
291feature is compatible, except in one case: when PTR records are being looked up
292for a single IPv6 address. Fortunately, we can hack in a compatibility feature
293here: If the type is PTR and no list separator is specified, and the entire
294remaining string is valid as an IP address, set an impossible separator so that
295it is treated as one item. */
296
297if (type == T_PTR && keystring[0] != '<' &&
298 string_is_ip_address(keystring, NULL) != 0)
299 sep = -1;
300
301/* SPF strings should be concatenated without a separator, thus make
302it the default if not defined (see RFC 4408 section 3.1.3).
303Multiple SPF records are forbidden (section 3.1.2) but are currently
2813c06e
CE
304not handled specially, thus they are concatenated with \n by default.
305MX priority and value are space-separated by default.
306SRV and TLSA record parts are space-separated by default. */
420a0d19 307
2813c06e
CE
308if (!outsep2) switch(type)
309 {
310 case T_SPF: outsep2 = US""; break;
311 case T_SRV: case T_MX: case T_TLSA: outsep2 = US" "; break;
312 }
420a0d19
CE
313
314/* Now scan the list and do a lookup for each item */
315
2813c06e 316while ((domain = string_nextinlist(&keystring, &sep, NULL, 0)))
420a0d19
CE
317 {
318 uschar rbuffer[256];
319 int searchtype = (type == T_CSA)? T_SRV : /* record type we want */
320 (type == T_MXH)? T_MX :
321 (type == T_ZNS)? T_NS : type;
322
323 /* If the type is PTR or CSA, we have to construct the relevant magic lookup
324 key if the original is an IP address (some experimental protocols are using
325 PTR records for different purposes where the key string is a host name, and
326 Exim's extended CSA can be keyed by domains or IP addresses). This code for
327 doing the reversal is now in a separate function. */
328
329 if ((type == T_PTR || type == T_CSA) &&
330 string_is_ip_address(domain, NULL) != 0)
331 {
332 dns_build_reverse(domain, rbuffer);
333 domain = rbuffer;
334 }
335
336 do
337 {
338 DEBUG(D_lookup) debug_printf("dnsdb key: %s\n", domain);
339
340 /* Do the lookup and sort out the result. There are four special types that
341 are handled specially: T_CSA, T_ZNS, T_ADDRESSES and T_MXH.
342 The first two are handled in a special lookup function so that the facility
343 could be used from other parts of the Exim code. T_ADDRESSES is handled by looping
344 over the types of A lookup. T_MXH affects only what happens later on in
345 this function, but for tidiness it is handled by the "special". If the
346 lookup fails, continue with the next domain. In the case of DEFER, adjust
347 the final "nothing found" result, but carry on to the next domain. */
348
349 found = domain;
350#if HAVE_IPV6
351 if (type == T_ADDRESSES) /* NB cannot happen unless HAVE_IPV6 */
352 {
2813c06e 353 if (searchtype == T_ADDRESSES) searchtype = T_AAAA;
420a0d19 354 else if (searchtype == T_AAAA) searchtype = T_A;
2813c06e 355 rc = dns_special_lookup(&dnsa, domain, searchtype, CUSS &found);
420a0d19
CE
356 }
357 else
358#endif
2813c06e 359 rc = dns_special_lookup(&dnsa, domain, type, CUSS &found);
420a0d19
CE
360
361 lookup_dnssec_authenticated = dnssec_mode==OK ? NULL
362 : dns_is_secure(&dnsa) ? US"yes" : US"no";
363
364 if (rc == DNS_NOMATCH || rc == DNS_NODATA) continue;
365 if ( rc != DNS_SUCCEED
366 || (dnssec_mode == DEFER && !dns_is_secure(&dnsa))
367 )
368 {
369 if (defer_mode == DEFER)
370 {
2813c06e
CE
371 dns_retrans = save_retrans;
372 dns_retry = save_retry;
420a0d19
CE
373 dns_init(FALSE, FALSE, FALSE); /* clr dnssec bit */
374 return DEFER; /* always defer */
375 }
376 if (defer_mode == PASS) failrc = DEFER; /* defer only if all do */
377 continue; /* treat defer as fail */
378 }
379
380
381 /* Search the returned records */
382
383 for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
384 rr != NULL;
385 rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
386 {
387 if (rr->type != searchtype) continue;
388
2813c06e
CE
389 if (*do_cache > rr->ttl)
390 *do_cache = rr->ttl;
420a0d19 391
2813c06e 392 if (type == T_A || type == T_AAAA || type == T_ADDRESSES)
420a0d19
CE
393 {
394 dns_address *da;
2813c06e 395 for (da = dns_address_from_rr(&dnsa, rr); da; da = da->next)
420a0d19 396 {
2813c06e
CE
397 if (ptr != 0) yield = string_catn(yield, &size, &ptr, outsep, 1);
398 yield = string_cat(yield, &size, &ptr, da->address);
420a0d19
CE
399 }
400 continue;
401 }
402
403 /* Other kinds of record just have one piece of data each, but there may be
404 several of them, of course. */
405
2813c06e 406 if (ptr != 0) yield = string_catn(yield, &size, &ptr, outsep, 1);
420a0d19
CE
407
408 if (type == T_TXT || type == T_SPF)
409 {
410 if (outsep2 == NULL)
411 {
412 /* output only the first item of data */
2813c06e 413 yield = string_catn(yield, &size, &ptr, (uschar *)(rr->data+1),
420a0d19
CE
414 (rr->data)[0]);
415 }
416 else
417 {
418 /* output all items */
419 int data_offset = 0;
420 while (data_offset < rr->size)
421 {
422 uschar chunk_len = (rr->data)[data_offset++];
423 if (outsep2[0] != '\0' && data_offset != 1)
2813c06e
CE
424 yield = string_catn(yield, &size, &ptr, outsep2, 1);
425 yield = string_catn(yield, &size, &ptr,
426 US ((rr->data)+data_offset), chunk_len);
420a0d19
CE
427 data_offset += chunk_len;
428 }
429 }
430 }
431 else if (type == T_TLSA)
432 {
433 uint8_t usage, selector, matching_type;
434 uint16_t i, payload_length;
435 uschar s[MAX_TLSA_EXPANDED_SIZE];
436 uschar * sp = s;
2813c06e 437 uschar * p = US rr->data;
420a0d19
CE
438
439 usage = *p++;
440 selector = *p++;
441 matching_type = *p++;
442 /* What's left after removing the first 3 bytes above */
443 payload_length = rr->size - 3;
2813c06e
CE
444 sp += sprintf(CS s, "%d%c%d%c%d%c", usage, *outsep2,
445 selector, *outsep2, matching_type, *outsep2);
420a0d19
CE
446 /* Now append the cert/identifier, one hex char at a time */
447 for (i=0;
448 i < payload_length && sp-s < (MAX_TLSA_EXPANDED_SIZE - 4);
449 i++)
420a0d19 450 sp += sprintf(CS sp, "%02x", (unsigned char)p[i]);
2813c06e
CE
451
452 yield = string_cat(yield, &size, &ptr, s);
420a0d19 453 }
2813c06e 454 else /* T_CNAME, T_CSA, T_MX, T_MXH, T_NS, T_PTR, T_SOA, T_SRV */
420a0d19
CE
455 {
456 int priority, weight, port;
457 uschar s[264];
2813c06e
CE
458 uschar * p = US rr->data;
459
460 switch (type)
461 {
462 case T_MXH:
463 /* mxh ignores the priority number and includes only the hostnames */
464 GETSHORT(priority, p);
465 break;
466
467 case T_MX:
468 GETSHORT(priority, p);
469 sprintf(CS s, "%d%c", priority, *outsep2);
470 yield = string_cat(yield, &size, &ptr, s);
471 break;
472
473 case T_SRV:
474 GETSHORT(priority, p);
475 GETSHORT(weight, p);
476 GETSHORT(port, p);
477 sprintf(CS s, "%d%c%d%c%d%c", priority, *outsep2,
478 weight, *outsep2, port, *outsep2);
479 yield = string_cat(yield, &size, &ptr, s);
480 break;
481
482 case T_CSA:
483 /* See acl_verify_csa() for more comments about CSA. */
484 GETSHORT(priority, p);
485 GETSHORT(weight, p);
486 GETSHORT(port, p);
487
488 if (priority != 1) continue; /* CSA version must be 1 */
489
490 /* If the CSA record we found is not the one we asked for, analyse
491 the subdomain assertions in the port field, else analyse the direct
492 authorization status in the weight field. */
493
494 if (Ustrcmp(found, domain) != 0)
495 {
496 if (port & 1) *s = 'X'; /* explicit authorization required */
497 else *s = '?'; /* no subdomain assertions here */
498 }
499 else
500 {
501 if (weight < 2) *s = 'N'; /* not authorized */
502 else if (weight == 2) *s = 'Y'; /* authorized */
503 else if (weight == 3) *s = '?'; /* unauthorizable */
504 else continue; /* invalid */
505 }
506
507 s[1] = ' ';
508 yield = string_catn(yield, &size, &ptr, s, 2);
509 break;
510
511 default:
512 break;
513 }
420a0d19
CE
514
515 /* GETSHORT() has advanced the pointer to the target domain. */
516
517 rc = dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, p,
2813c06e 518 (DN_EXPAND_ARG4_TYPE)s, sizeof(s));
420a0d19
CE
519
520 /* If an overlong response was received, the data will have been
521 truncated and dn_expand may fail. */
522
523 if (rc < 0)
524 {
525 log_write(0, LOG_MAIN, "host name alias list truncated: type=%s "
526 "domain=%s", dns_text_type(type), domain);
527 break;
528 }
2813c06e
CE
529 else yield = string_cat(yield, &size, &ptr, s);
530
531 if (type == T_SOA && outsep2 != NULL)
532 {
533 unsigned long serial, refresh, retry, expire, minimum;
534
535 p += rc;
536 yield = string_catn(yield, &size, &ptr, outsep2, 1);
537
538 rc = dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, p,
539 (DN_EXPAND_ARG4_TYPE)s, sizeof(s));
540 if (rc < 0)
541 {
542 log_write(0, LOG_MAIN, "responsible-mailbox truncated: type=%s "
543 "domain=%s", dns_text_type(type), domain);
544 break;
545 }
546 else yield = string_cat(yield, &size, &ptr, s);
547
548 p += rc;
549 GETLONG(serial, p); GETLONG(refresh, p);
550 GETLONG(retry, p); GETLONG(expire, p); GETLONG(minimum, p);
551 sprintf(CS s, "%c%lu%c%lu%c%lu%c%lu%c%lu",
552 *outsep2, serial, *outsep2, refresh,
553 *outsep2, retry, *outsep2, expire, *outsep2, minimum);
554 yield = string_cat(yield, &size, &ptr, s);
555 }
420a0d19
CE
556 }
557 } /* Loop for list of returned records */
558
2813c06e 559 /* Loop for set of A-lookup types */
420a0d19
CE
560 } while (type == T_ADDRESSES && searchtype != T_A);
561
562 } /* Loop for list of domains */
563
564/* Reclaim unused memory */
565
566store_reset(yield + ptr + 1);
567
568/* If ptr == 0 we have not found anything. Otherwise, insert the terminating
569zero and return the result. */
570
2813c06e
CE
571dns_retrans = save_retrans;
572dns_retry = save_retry;
420a0d19
CE
573dns_init(FALSE, FALSE, FALSE); /* clear the dnssec bit for getaddrbyname */
574
575if (ptr == 0) return failrc;
576yield[ptr] = 0;
577*result = yield;
578return OK;
579}
580
581
582
583/*************************************************
584* Version reporting entry point *
585*************************************************/
586
587/* See local README for interface description. */
588
589#include "../version.h"
590
591void
592dnsdb_version_report(FILE *f)
593{
594#ifdef DYNLOOKUP
595fprintf(f, "Library version: DNSDB: Exim version %s\n", EXIM_VERSION_STR);
596#endif
597}
598
599
600static lookup_info _lookup_info = {
601 US"dnsdb", /* lookup name */
602 lookup_querystyle, /* query style */
603 dnsdb_open, /* open function */
604 NULL, /* check function */
605 dnsdb_find, /* find function */
606 NULL, /* no close function */
607 NULL, /* no tidy function */
608 NULL, /* no quoting function */
609 dnsdb_version_report /* version reporting */
610};
611
612#ifdef DYNLOOKUP
613#define dnsdb_lookup_module_info _lookup_module_info
614#endif
615
616static lookup_info *_lookup_list[] = { &_lookup_info };
617lookup_module_info dnsdb_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
618
619/* vi: aw ai sw=2
620*/
621/* End of lookups/dnsdb.c */