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