1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2014 */
6 /* See the file NOTICE for conditions of use and distribution. */
9 #include "lf_functions.h"
13 /* Ancient systems (e.g. SunOS4) don't appear to have T_TXT defined in their
20 /* Many systems do not have T_SPF. */
25 /* New TLSA record for DANE */
30 /* Table of recognized DNS record types and their integer values. */
32 static const char *type_names[] = {
54 static int type_values[] = {
57 T_APL, /* Private type for AAAA + A */
64 T_CSA, /* Private type for "Client SMTP Authorization". */
66 T_MXH, /* Private type for "MX hostnames" */
73 T_ZNS /* Private type for "zone nameservers" */
77 /*************************************************
79 *************************************************/
81 /* See local README for interface description. */
84 dnsdb_open(uschar *filename, uschar **errmsg)
86 filename = filename; /* Keep picky compilers happy */
87 errmsg = errmsg; /* Ditto */
88 return (void *)(-1); /* Any non-0 value */
93 /*************************************************
94 * Find entry point for dnsdb *
95 *************************************************/
97 /* See local README for interface description. The query in the "keystring" may
98 consist of a number of parts.
100 (a) If the first significant character is '>' then the next character is the
101 separator character that is used when multiple records are found. The default
102 separator is newline.
104 (b) If the next character is ',' then the next character is the separator
105 character used for multiple items of text in "TXT" records. Alternatively,
106 if the next character is ';' then these multiple items are concatenated with
107 no separator. With neither of these options specified, only the first item
108 is output. Similarly for "SPF" records, but the default for joining multiple
109 items in one SPF record is the empty string, for direct concatenation.
111 (c) If the next sequence of characters is 'defer_FOO' followed by a comma,
112 the defer behaviour is set to FOO. The possible behaviours are: 'strict', where
113 any defer causes the whole lookup to defer; 'lax', where a defer causes the
114 whole lookup to defer only if none of the DNS queries succeeds; and 'never',
115 where all defers are as if the lookup failed. The default is 'lax'.
117 (d) Another optional comma-sep field: 'dnssec_FOO', with 'strict', 'lax'
118 and 'never' (default); can appear before or after (c). The meanings are
119 require, try and don't-try dnssec respectively.
121 (e) If the next sequence of characters is a sequence of letters and digits
122 followed by '=', it is interpreted as the name of the DNS record type. The
125 (f) Then there follows list of domain names. This is a generalized Exim list,
126 which may start with '<' in order to set a specific separator. The default
127 separator, as always, is colon. */
130 dnsdb_find(void *handle, uschar *filename, uschar *keystring, int length,
131 uschar **result, uschar **errmsg, BOOL *do_cache)
137 int defer_mode = PASS;
138 int dnssec_mode = OK;
141 uschar *outsep = US"\n";
142 uschar *outsep2 = NULL;
143 uschar *equals, *domain, *found;
146 /* Because we're the working in the search pool, we try to reclaim as much
147 store as possible later, so we preallocate the result here */
149 uschar *yield = store_get(size);
155 handle = handle; /* Keep picky compilers happy */
160 /* If the string starts with '>' we change the output separator.
161 If it's followed by ';' or ',' we set the TXT output separator. */
163 while (isspace(*keystring)) keystring++;
164 if (*keystring == '>')
166 outsep = keystring + 1;
168 if (*keystring == ',')
170 outsep2 = keystring + 1;
173 else if (*keystring == ';')
178 while (isspace(*keystring)) keystring++;
181 /* Check for a modifier keyword. */
183 while ( strncmpic(keystring, US"defer_", 6) == 0
184 || strncmpic(keystring, US"dnssec_", 7) == 0
187 if (strncmpic(keystring, US"defer_", 6) == 0)
190 if (strncmpic(keystring, US"strict", 6) == 0)
195 else if (strncmpic(keystring, US"lax", 3) == 0)
200 else if (strncmpic(keystring, US"never", 5) == 0)
207 *errmsg = US"unsupported dnsdb defer behaviour";
214 if (strncmpic(keystring, US"strict", 6) == 0)
219 else if (strncmpic(keystring, US"lax", 3) == 0)
224 else if (strncmpic(keystring, US"never", 5) == 0)
231 *errmsg = US"unsupported dnsdb dnssec behaviour";
235 while (isspace(*keystring)) keystring++;
236 if (*keystring++ != ',')
238 *errmsg = US"dnsdb modifier syntax error";
241 while (isspace(*keystring)) keystring++;
244 /* Figure out the "type" value if it is not T_TXT.
245 If the keystring contains an = this must be preceded by a valid type name. */
248 if ((equals = Ustrchr(keystring, '=')) != NULL)
251 uschar *tend = equals;
253 while (tend > keystring && isspace(tend[-1])) tend--;
254 len = tend - keystring;
256 for (i = 0; i < sizeof(type_names)/sizeof(uschar *); i++)
258 if (len == Ustrlen(type_names[i]) &&
259 strncmpic(keystring, US type_names[i], len) == 0)
261 type = type_values[i];
266 if (i >= sizeof(type_names)/sizeof(uschar *))
268 *errmsg = US"unsupported DNS record type";
272 keystring = equals + 1;
273 while (isspace(*keystring)) keystring++;
276 /* Initialize the resolver in case this is the first time it has been used. */
278 dns_init(FALSE, FALSE, dnssec_mode != OK);
280 /* The remainder of the string must be a list of domains. As long as the lookup
281 for at least one of them succeeds, we return success. Failure means that none
284 The original implementation did not support a list of domains. Adding the list
285 feature is compatible, except in one case: when PTR records are being looked up
286 for a single IPv6 address. Fortunately, we can hack in a compatibility feature
287 here: If the type is PTR and no list separator is specified, and the entire
288 remaining string is valid as an IP address, set an impossible separator so that
289 it is treated as one item. */
291 if (type == T_PTR && keystring[0] != '<' &&
292 string_is_ip_address(keystring, NULL) != 0)
295 /* SPF strings should be concatenated without a separator, thus make
296 it the default if not defined (see RFC 4408 section 3.1.3).
297 Multiple SPF records are forbidden (section 3.1.2) but are currently
298 not handled specially, thus they are concatenated with \n by default. */
300 if (type == T_SPF && outsep2 == NULL)
303 /* Now scan the list and do a lookup for each item */
305 while ((domain = string_nextinlist(&keystring, &sep, buffer, sizeof(buffer)))
309 int searchtype = (type == T_CSA)? T_SRV : /* record type we want */
310 (type == T_MXH)? T_MX :
311 (type == T_ZNS)? T_NS : type;
313 /* If the type is PTR or CSA, we have to construct the relevant magic lookup
314 key if the original is an IP address (some experimental protocols are using
315 PTR records for different purposes where the key string is a host name, and
316 Exim's extended CSA can be keyed by domains or IP addresses). This code for
317 doing the reversal is now in a separate function. */
319 if ((type == T_PTR || type == T_CSA) &&
320 string_is_ip_address(domain, NULL) != 0)
322 dns_build_reverse(domain, rbuffer);
328 DEBUG(D_lookup) debug_printf("dnsdb key: %s\n", domain);
330 /* Do the lookup and sort out the result. There are four special types that
331 are handled specially: T_CSA, T_ZNS, T_APL and T_MXH.
332 The first two are handled in a special lookup function so that the facility
333 could be used from other parts of the Exim code. T_APL is handled by looping
334 over the types of A lookup. T_MXH affects only what happens later on in
335 this function, but for tidiness it is handled by the "special". If the
336 lookup fails, continue with the next domain. In the case of DEFER, adjust
337 the final "nothing found" result, but carry on to the next domain. */
341 if (type == T_APL) /* NB cannot happen unless HAVE_IPV6 */
343 if (searchtype == T_APL)
344 # if defined(SUPPORT_A6)
349 else if (searchtype == T_A6) searchtype = T_AAAA;
350 else if (searchtype == T_AAAA) searchtype = T_A;
351 rc = dns_special_lookup(&dnsa, domain, searchtype, &found);
355 rc = dns_special_lookup(&dnsa, domain, type, &found);
357 lookup_dnssec_authenticated = dnssec_mode==OK ? NULL
358 : dns_is_secure(&dnsa) ? US"yes" : US"no";
360 if (rc == DNS_NOMATCH || rc == DNS_NODATA) continue;
361 if (rc != DNS_SUCCEED)
363 if (defer_mode == DEFER)
365 dns_init(FALSE, FALSE, FALSE); /* clr dnssec bit */
366 return DEFER; /* always defer */
368 if (defer_mode == PASS) failrc = DEFER; /* defer only if all do */
369 continue; /* treat defer as fail */
371 if (dnssec_mode == DEFER && !dns_is_secure(&dnsa))
378 /* Search the returned records */
380 for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
382 rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
384 if (rr->type != searchtype) continue;
386 /* There may be several addresses from an A6 record. Put the configured
387 separator between them, just as for between several records. However, A6
388 support is not normally configured these days. */
398 for (da = dns_address_from_rr(&dnsa, rr); da != NULL; da = da->next)
400 if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
401 yield = string_cat(yield, &size, &ptr, da->address,
402 Ustrlen(da->address));
407 /* Other kinds of record just have one piece of data each, but there may be
408 several of them, of course. */
410 if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
412 if (type == T_TXT || type == T_SPF)
416 /* output only the first item of data */
417 yield = string_cat(yield, &size, &ptr, (uschar *)(rr->data+1),
422 /* output all items */
424 while (data_offset < rr->size)
426 uschar chunk_len = (rr->data)[data_offset++];
427 if (outsep2[0] != '\0' && data_offset != 1)
428 yield = string_cat(yield, &size, &ptr, outsep2, 1);
429 yield = string_cat(yield, &size, &ptr,
430 (uschar *)((rr->data)+data_offset), chunk_len);
431 data_offset += chunk_len;
435 else if (type == T_TLSA)
437 uint8_t usage, selector, matching_type;
438 uint16_t i, payload_length;
439 uschar s[MAX_TLSA_EXPANDED_SIZE];
441 uschar *p = (uschar *)(rr->data);
445 matching_type = *p++;
446 /* What's left after removing the first 3 bytes above */
447 payload_length = rr->size - 3;
448 sp += sprintf(CS s, "%d %d %d ", usage, selector, matching_type);
449 /* Now append the cert/identifier, one hex char at a time */
451 i < payload_length && sp-s < (MAX_TLSA_EXPANDED_SIZE - 4);
454 sp += sprintf(CS sp, "%02x", (unsigned char)p[i]);
456 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
458 else /* T_CNAME, T_CSA, T_MX, T_MXH, T_NS, T_PTR, T_SRV */
460 int priority, weight, port;
462 uschar *p = (uschar *)(rr->data);
466 /* mxh ignores the priority number and includes only the hostnames */
467 GETSHORT(priority, p);
469 else if (type == T_MX)
471 GETSHORT(priority, p);
472 sprintf(CS s, "%d ", priority);
473 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
475 else if (type == T_SRV)
477 GETSHORT(priority, p);
480 sprintf(CS s, "%d %d %d ", priority, weight, port);
481 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
483 else if (type == T_CSA)
485 /* See acl_verify_csa() for more comments about CSA. */
487 GETSHORT(priority, p);
491 if (priority != 1) continue; /* CSA version must be 1 */
493 /* If the CSA record we found is not the one we asked for, analyse
494 the subdomain assertions in the port field, else analyse the direct
495 authorization status in the weight field. */
499 if (port & 1) *s = 'X'; /* explicit authorization required */
500 else *s = '?'; /* no subdomain assertions here */
504 if (weight < 2) *s = 'N'; /* not authorized */
505 else if (weight == 2) *s = 'Y'; /* authorized */
506 else if (weight == 3) *s = '?'; /* unauthorizable */
507 else continue; /* invalid */
511 yield = string_cat(yield, &size, &ptr, s, 2);
514 /* GETSHORT() has advanced the pointer to the target domain. */
516 rc = dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, p,
517 (DN_EXPAND_ARG4_TYPE)(s), sizeof(s));
519 /* If an overlong response was received, the data will have been
520 truncated and dn_expand may fail. */
524 log_write(0, LOG_MAIN, "host name alias list truncated: type=%s "
525 "domain=%s", dns_text_type(type), domain);
528 else yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
530 } /* Loop for list of returned records */
532 /* Loop for set of A-lookupu types */
533 } while (type == T_APL && searchtype != T_A);
535 } /* Loop for list of domains */
537 /* Reclaim unused memory */
539 store_reset(yield + ptr + 1);
541 /* If ptr == 0 we have not found anything. Otherwise, insert the terminating
542 zero and return the result. */
544 dns_init(FALSE, FALSE, FALSE); /* clear the dnssec bit for getaddrbyname */
546 if (ptr == 0) return failrc;
554 /*************************************************
555 * Version reporting entry point *
556 *************************************************/
558 /* See local README for interface description. */
560 #include "../version.h"
563 dnsdb_version_report(FILE *f)
566 fprintf(f, "Library version: DNSDB: Exim version %s\n", EXIM_VERSION_STR);
571 static lookup_info _lookup_info = {
572 US"dnsdb", /* lookup name */
573 lookup_querystyle, /* query style */
574 dnsdb_open, /* open function */
575 NULL, /* check function */
576 dnsdb_find, /* find function */
577 NULL, /* no close function */
578 NULL, /* no tidy function */
579 NULL, /* no quoting function */
580 dnsdb_version_report /* version reporting */
584 #define dnsdb_lookup_module_info _lookup_module_info
587 static lookup_info *_lookup_list[] = { &_lookup_info };
588 lookup_module_info dnsdb_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
592 /* End of lookups/dnsdb.c */