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[] = {
51 static int type_values[] = {
54 T_ADDRESSES, /* Private type for AAAA + A */
58 T_CSA, /* Private type for "Client SMTP Authorization". */
60 T_MXH, /* Private type for "MX hostnames" */
67 T_ZNS /* Private type for "zone nameservers" */
71 /*************************************************
73 *************************************************/
75 /* See local README for interface description. */
78 dnsdb_open(uschar *filename, uschar **errmsg)
80 filename = filename; /* Keep picky compilers happy */
81 errmsg = errmsg; /* Ditto */
82 return (void *)(-1); /* Any non-0 value */
87 /*************************************************
88 * Find entry point for dnsdb *
89 *************************************************/
91 /* See local README for interface description. The query in the "keystring" may
92 consist of a number of parts.
94 (a) If the first significant character is '>' then the next character is the
95 separator character that is used when multiple records are found. The default
98 (b) If the next character is ',' then the next character is the separator
99 character used for multiple items of text in "TXT" records. Alternatively,
100 if the next character is ';' then these multiple items are concatenated with
101 no separator. With neither of these options specified, only the first item
102 is output. Similarly for "SPF" records, but the default for joining multiple
103 items in one SPF record is the empty string, for direct concatenation.
105 (c) Options, all comma-terminated, in any order. Any unrecognised option
106 terminates option processing. Recognised options are:
108 - 'defer_FOO': set the defer behaviour to FOO. The possible behaviours are:
109 'strict', where any defer causes the whole lookup to defer; 'lax', where a defer
110 causes the whole lookup to defer only if none of the DNS queries succeeds; and
111 'never', where all defers are as if the lookup failed. The default is 'lax'.
113 - 'dnssec_FOO', with 'strict', 'lax' and 'never' (default). The meanings are
114 require, try and don't-try dnssec respectively.
116 - 'retrans_VAL', set the timeout value. VAL is an Exim time specification
117 (eg "5s"). The default is set by the main configuration option 'dns_retrans'.
119 - 'retry_VAL', set the retry count on timeouts. VAL is an integer. The
120 default is set by the main configuration option "dns_retry".
122 (d) If the next sequence of characters is a sequence of letters and digits
123 followed by '=', it is interpreted as the name of the DNS record type. The
126 (e) Then there follows list of domain names. This is a generalized Exim list,
127 which may start with '<' in order to set a specific separator. The default
128 separator, as always, is colon. */
131 dnsdb_find(void *handle, uschar *filename, const uschar *keystring, int length,
132 uschar **result, uschar **errmsg, BOOL *do_cache)
138 int defer_mode = PASS;
139 int dnssec_mode = OK;
140 int save_retrans = dns_retrans;
141 int save_retry = dns_retry;
144 const uschar *outsep = CUS"\n";
145 const uschar *outsep2 = NULL;
146 uschar *equals, *domain, *found;
148 /* Because we're the working in the search pool, we try to reclaim as much
149 store as possible later, so we preallocate the result here */
151 uschar *yield = store_get(size);
157 handle = handle; /* Keep picky compilers happy */
162 /* If the string starts with '>' we change the output separator.
163 If it's followed by ';' or ',' we set the TXT output separator. */
165 while (isspace(*keystring)) keystring++;
166 if (*keystring == '>')
168 outsep = keystring + 1;
170 if (*keystring == ',')
172 outsep2 = keystring + 1;
175 else if (*keystring == ';')
180 while (isspace(*keystring)) keystring++;
183 /* Check for a modifier keyword. */
187 if (strncmpic(keystring, US"defer_", 6) == 0)
190 if (strncmpic(keystring, US"strict", 6) == 0)
191 { defer_mode = DEFER; keystring += 6; }
192 else if (strncmpic(keystring, US"lax", 3) == 0)
193 { defer_mode = PASS; keystring += 3; }
194 else if (strncmpic(keystring, US"never", 5) == 0)
195 { defer_mode = OK; keystring += 5; }
198 *errmsg = US"unsupported dnsdb defer behaviour";
202 else if (strncmpic(keystring, US"dnssec_", 7) == 0)
205 if (strncmpic(keystring, US"strict", 6) == 0)
206 { dnssec_mode = DEFER; keystring += 6; }
207 else if (strncmpic(keystring, US"lax", 3) == 0)
208 { dnssec_mode = PASS; keystring += 3; }
209 else if (strncmpic(keystring, US"never", 5) == 0)
210 { dnssec_mode = OK; keystring += 5; }
213 *errmsg = US"unsupported dnsdb dnssec behaviour";
217 else if (strncmpic(keystring, US"retrans_", 8) == 0)
220 if ((timeout_sec = readconf_readtime(keystring += 8, ',', FALSE)) <= 0)
222 *errmsg = US"unsupported dnsdb timeout value";
225 dns_retrans = timeout_sec;
226 while (*keystring != ',') keystring++;
228 else if (strncmpic(keystring, US"retry_", 6) == 0)
231 if ((retries = (int)strtol(keystring + 6, CSS &keystring, 0)) < 0)
233 *errmsg = US"unsupported dnsdb retry count";
241 while (isspace(*keystring)) keystring++;
242 if (*keystring++ != ',')
244 *errmsg = US"dnsdb modifier syntax error";
247 while (isspace(*keystring)) keystring++;
250 /* Figure out the "type" value if it is not T_TXT.
251 If the keystring contains an = this must be preceded by a valid type name. */
254 if ((equals = Ustrchr(keystring, '=')) != NULL)
257 uschar *tend = equals;
259 while (tend > keystring && isspace(tend[-1])) tend--;
260 len = tend - keystring;
262 for (i = 0; i < sizeof(type_names)/sizeof(uschar *); i++)
264 if (len == Ustrlen(type_names[i]) &&
265 strncmpic(keystring, US type_names[i], len) == 0)
267 type = type_values[i];
272 if (i >= sizeof(type_names)/sizeof(uschar *))
274 *errmsg = US"unsupported DNS record type";
278 keystring = equals + 1;
279 while (isspace(*keystring)) keystring++;
282 /* Initialize the resolver in case this is the first time it has been used. */
284 dns_init(FALSE, FALSE, dnssec_mode != OK);
286 /* The remainder of the string must be a list of domains. As long as the lookup
287 for at least one of them succeeds, we return success. Failure means that none
290 The original implementation did not support a list of domains. Adding the list
291 feature is compatible, except in one case: when PTR records are being looked up
292 for a single IPv6 address. Fortunately, we can hack in a compatibility feature
293 here: If the type is PTR and no list separator is specified, and the entire
294 remaining string is valid as an IP address, set an impossible separator so that
295 it is treated as one item. */
297 if (type == T_PTR && keystring[0] != '<' &&
298 string_is_ip_address(keystring, NULL) != 0)
301 /* SPF strings should be concatenated without a separator, thus make
302 it the default if not defined (see RFC 4408 section 3.1.3).
303 Multiple SPF records are forbidden (section 3.1.2) but are currently
304 not handled specially, thus they are concatenated with \n by default.
305 MX priority and value are space-separated by default.
306 SRV and TLSA record parts are space-separated by default. */
308 if (!outsep2) switch(type)
310 case T_SPF: outsep2 = US""; break;
311 case T_SRV: case T_MX: case T_TLSA: outsep2 = US" "; break;
314 /* Now scan the list and do a lookup for each item */
316 while ((domain = string_nextinlist(&keystring, &sep, NULL, 0)))
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;
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. */
329 if ((type == T_PTR || type == T_CSA) &&
330 string_is_ip_address(domain, NULL) != 0)
332 dns_build_reverse(domain, rbuffer);
338 DEBUG(D_lookup) debug_printf("dnsdb key: %s\n", domain);
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. */
351 if (type == T_ADDRESSES) /* NB cannot happen unless HAVE_IPV6 */
353 if (searchtype == T_ADDRESSES) searchtype = T_AAAA;
354 else if (searchtype == T_AAAA) searchtype = T_A;
355 rc = dns_special_lookup(&dnsa, domain, searchtype, CUSS &found);
359 rc = dns_special_lookup(&dnsa, domain, type, CUSS &found);
361 lookup_dnssec_authenticated = dnssec_mode==OK ? NULL
362 : dns_is_secure(&dnsa) ? US"yes" : US"no";
364 if (rc == DNS_NOMATCH || rc == DNS_NODATA) continue;
365 if ( rc != DNS_SUCCEED
366 || (dnssec_mode == DEFER && !dns_is_secure(&dnsa))
369 if (defer_mode == DEFER)
371 dns_retrans = save_retrans;
372 dns_retry = save_retry;
373 dns_init(FALSE, FALSE, FALSE); /* clr dnssec bit */
374 return DEFER; /* always defer */
376 if (defer_mode == PASS) failrc = DEFER; /* defer only if all do */
377 continue; /* treat defer as fail */
381 /* Search the returned records */
383 for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
385 rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
387 if (rr->type != searchtype) continue;
389 /* There may be several addresses from an A6 record. Put the configured
390 separator between them, just as for between several records. However, A6
391 support is not normally configured these days. */
393 if (type == T_A || type == T_AAAA || type == T_ADDRESSES)
396 for (da = dns_address_from_rr(&dnsa, rr); da != NULL; da = da->next)
398 if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
399 yield = string_cat(yield, &size, &ptr, da->address,
400 Ustrlen(da->address));
405 /* Other kinds of record just have one piece of data each, but there may be
406 several of them, of course. */
408 if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
410 if (type == T_TXT || type == T_SPF)
414 /* output only the first item of data */
415 yield = string_cat(yield, &size, &ptr, (uschar *)(rr->data+1),
420 /* output all items */
422 while (data_offset < rr->size)
424 uschar chunk_len = (rr->data)[data_offset++];
425 if (outsep2[0] != '\0' && data_offset != 1)
426 yield = string_cat(yield, &size, &ptr, outsep2, 1);
427 yield = string_cat(yield, &size, &ptr,
428 (uschar *)((rr->data)+data_offset), chunk_len);
429 data_offset += chunk_len;
433 else if (type == T_TLSA)
435 uint8_t usage, selector, matching_type;
436 uint16_t i, payload_length;
437 uschar s[MAX_TLSA_EXPANDED_SIZE];
439 uschar *p = (uschar *)(rr->data);
443 matching_type = *p++;
444 /* What's left after removing the first 3 bytes above */
445 payload_length = rr->size - 3;
446 sp += sprintf(CS s, "%d%c%d%c%d%c", usage, *outsep2,
447 selector, *outsep2, matching_type, *outsep2);
448 /* Now append the cert/identifier, one hex char at a time */
450 i < payload_length && sp-s < (MAX_TLSA_EXPANDED_SIZE - 4);
453 sp += sprintf(CS sp, "%02x", (unsigned char)p[i]);
455 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
457 else /* T_CNAME, T_CSA, T_MX, T_MXH, T_NS, T_PTR, T_SRV */
459 int priority, weight, port;
461 uschar *p = (uschar *)(rr->data);
465 /* mxh ignores the priority number and includes only the hostnames */
466 GETSHORT(priority, p);
468 else if (type == T_MX)
470 GETSHORT(priority, p);
471 sprintf(CS s, "%d%c", priority, *outsep2);
472 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
474 else if (type == T_SRV)
476 GETSHORT(priority, p);
479 sprintf(CS s, "%d%c%d%c%d%c", priority, *outsep2,
480 weight, *outsep2, port, *outsep2);
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. */
497 if (Ustrcmp(found, domain) != 0)
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_ADDRESSES && 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_retrans = save_retrans;
545 dns_retry = save_retry;
546 dns_init(FALSE, FALSE, FALSE); /* clear the dnssec bit for getaddrbyname */
548 if (ptr == 0) return failrc;
556 /*************************************************
557 * Version reporting entry point *
558 *************************************************/
560 /* See local README for interface description. */
562 #include "../version.h"
565 dnsdb_version_report(FILE *f)
568 fprintf(f, "Library version: DNSDB: Exim version %s\n", EXIM_VERSION_STR);
573 static lookup_info _lookup_info = {
574 US"dnsdb", /* lookup name */
575 lookup_querystyle, /* query style */
576 dnsdb_open, /* open function */
577 NULL, /* check function */
578 dnsdb_find, /* find function */
579 NULL, /* no close function */
580 NULL, /* no tidy function */
581 NULL, /* no quoting function */
582 dnsdb_version_report /* version reporting */
586 #define dnsdb_lookup_module_info _lookup_module_info
589 static lookup_info *_lookup_list[] = { &_lookup_info };
590 lookup_module_info dnsdb_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
594 /* End of lookups/dnsdb.c */