1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2012 */
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 /* Table of recognized DNS record types and their integer values. */
27 static const char *type_names[] = {
48 static int type_values[] = {
51 T_APL, /* Private type for AAAA + A */
58 T_CSA, /* Private type for "Client SMTP Authorization". */
60 T_MXH, /* Private type for "MX hostnames" */
66 T_ZNS /* Private type for "zone nameservers" */
70 /*************************************************
72 *************************************************/
74 /* See local README for interface description. */
77 dnsdb_open(uschar *filename, uschar **errmsg)
79 filename = filename; /* Keep picky compilers happy */
80 errmsg = errmsg; /* Ditto */
81 return (void *)(-1); /* Any non-0 value */
86 /*************************************************
87 * Find entry point for dnsdb *
88 *************************************************/
90 /* See local README for interface description. The query in the "keystring" may
91 consist of a number of parts.
93 (a) If the first significant character is '>' then the next character is the
94 separator character that is used when multiple records are found. The default
97 (b) If the next character is ',' then the next character is the separator
98 character used for multiple items of text in "TXT" records. Alternatively,
99 if the next character is ';' then these multiple items are concatenated with
100 no separator. With neither of these options specified, only the first item
101 is output. Similarly for "SPF" records, but the default for joining multiple
102 items in one SPF record is the empty string, for direct concatenation.
104 (c) If the next sequence of characters is 'defer_FOO' followed by a comma,
105 the defer behaviour is set to FOO. The possible behaviours are: 'strict', where
106 any defer causes the whole lookup to defer; 'lax', where a defer causes the
107 whole lookup to defer only if none of the DNS queries succeeds; and 'never',
108 where all defers are as if the lookup failed. The default is 'lax'.
110 (d) If the next sequence of characters is a sequence of letters and digits
111 followed by '=', it is interpreted as the name of the DNS record type. The
114 (e) Then there follows list of domain names. This is a generalized Exim list,
115 which may start with '<' in order to set a specific separator. The default
116 separator, as always, is colon. */
119 dnsdb_find(void *handle, uschar *filename, uschar *keystring, int length,
120 uschar **result, uschar **errmsg, BOOL *do_cache)
126 int defer_mode = PASS;
129 uschar *outsep = US"\n";
130 uschar *outsep2 = NULL;
131 uschar *equals, *domain, *found;
134 /* Because we're the working in the search pool, we try to reclaim as much
135 store as possible later, so we preallocate the result here */
137 uschar *yield = store_get(size);
143 handle = handle; /* Keep picky compilers happy */
148 /* If the string starts with '>' we change the output separator.
149 If it's followed by ';' or ',' we set the TXT output separator. */
151 while (isspace(*keystring)) keystring++;
152 if (*keystring == '>')
154 outsep = keystring + 1;
156 if (*keystring == ',')
158 outsep2 = keystring + 1;
161 else if (*keystring == ';')
166 while (isspace(*keystring)) keystring++;
169 /* Check for a defer behaviour keyword. */
171 if (strncmpic(keystring, US"defer_", 6) == 0)
174 if (strncmpic(keystring, US"strict", 6) == 0)
179 else if (strncmpic(keystring, US"lax", 3) == 0)
184 else if (strncmpic(keystring, US"never", 5) == 0)
191 *errmsg = US"unsupported dnsdb defer behaviour";
194 while (isspace(*keystring)) keystring++;
195 if (*keystring++ != ',')
197 *errmsg = US"dnsdb defer behaviour syntax error";
200 while (isspace(*keystring)) keystring++;
203 /* Figure out the "type" value if it is not T_TXT.
204 If the keystring contains an = this must be preceded by a valid type name. */
207 if ((equals = Ustrchr(keystring, '=')) != NULL)
210 uschar *tend = equals;
212 while (tend > keystring && isspace(tend[-1])) tend--;
213 len = tend - keystring;
215 for (i = 0; i < sizeof(type_names)/sizeof(uschar *); i++)
217 if (len == Ustrlen(type_names[i]) &&
218 strncmpic(keystring, US type_names[i], len) == 0)
220 type = type_values[i];
225 if (i >= sizeof(type_names)/sizeof(uschar *))
227 *errmsg = US"unsupported DNS record type";
231 keystring = equals + 1;
232 while (isspace(*keystring)) keystring++;
235 /* Initialize the resolver in case this is the first time it has been used. */
237 dns_init(FALSE, FALSE);
239 /* The remainder of the string must be a list of domains. As long as the lookup
240 for at least one of them succeeds, we return success. Failure means that none
243 The original implementation did not support a list of domains. Adding the list
244 feature is compatible, except in one case: when PTR records are being looked up
245 for a single IPv6 address. Fortunately, we can hack in a compatibility feature
246 here: If the type is PTR and no list separator is specified, and the entire
247 remaining string is valid as an IP address, set an impossible separator so that
248 it is treated as one item. */
250 if (type == T_PTR && keystring[0] != '<' &&
251 string_is_ip_address(keystring, NULL) != 0)
254 /* SPF strings should be concatenated without a separator, thus make
255 it the default if not defined (see RFC 4408 section 3.1.3).
256 Multiple SPF records are forbidden (section 3.1.2) but are currently
257 not handled specially, thus they are concatenated with \n by default. */
259 if (type == T_SPF && outsep2 == NULL)
262 /* Now scan the list and do a lookup for each item */
264 while ((domain = string_nextinlist(&keystring, &sep, buffer, sizeof(buffer)))
268 int searchtype = (type == T_CSA)? T_SRV : /* record type we want */
269 (type == T_MXH)? T_MX :
270 (type == T_ZNS)? T_NS : type;
272 /* If the type is PTR or CSA, we have to construct the relevant magic lookup
273 key if the original is an IP address (some experimental protocols are using
274 PTR records for different purposes where the key string is a host name, and
275 Exim's extended CSA can be keyed by domains or IP addresses). This code for
276 doing the reversal is now in a separate function. */
278 if ((type == T_PTR || type == T_CSA) &&
279 string_is_ip_address(domain, NULL) != 0)
281 dns_build_reverse(domain, rbuffer);
287 DEBUG(D_lookup) debug_printf("dnsdb key: %s\n", domain);
289 /* Do the lookup and sort out the result. There are four special types that
290 are handled specially: T_CSA, T_ZNS, T_APL and T_MXH.
291 The first two are handled in a special lookup function so that the facility
292 could be used from other parts of the Exim code. T_APL is handled by looping
293 over the types of A lookup. T_MXH affects only what happens later on in
294 this function, but for tidiness it is handled by the "special". If the
295 lookup fails, continue with the next domain. In the case of DEFER, adjust
296 the final "nothing found" result, but carry on to the next domain. */
300 if (type == T_APL) /* NB cannot happen unless HAVE_IPV6 */
302 if (searchtype == T_APL)
303 # if defined(SUPPORT_A6)
308 else if (searchtype == T_A6) searchtype = T_AAAA;
309 else if (searchtype == T_AAAA) searchtype = T_A;
310 rc = dns_special_lookup(&dnsa, domain, searchtype, &found);
314 rc = dns_special_lookup(&dnsa, domain, type, &found);
316 if (rc == DNS_NOMATCH || rc == DNS_NODATA) continue;
317 if (rc != DNS_SUCCEED)
319 if (defer_mode == DEFER) return DEFER; /* always defer */
320 if (defer_mode == PASS) failrc = DEFER; /* defer only if all do */
321 continue; /* treat defer as fail */
324 /* Search the returned records */
326 for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
328 rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
330 if (rr->type != searchtype) continue;
332 /* There may be several addresses from an A6 record. Put the configured
333 separator between them, just as for between several records. However, A6
334 support is not normally configured these days. */
344 for (da = dns_address_from_rr(&dnsa, rr); da != NULL; da = da->next)
346 if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
347 yield = string_cat(yield, &size, &ptr, da->address,
348 Ustrlen(da->address));
353 /* Other kinds of record just have one piece of data each, but there may be
354 several of them, of course. */
356 if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
358 if (type == T_TXT || type == T_SPF)
362 /* output only the first item of data */
363 yield = string_cat(yield, &size, &ptr, (uschar *)(rr->data+1),
368 /* output all items */
370 while (data_offset < rr->size)
372 uschar chunk_len = (rr->data)[data_offset++];
373 if (outsep2[0] != '\0' && data_offset != 1)
374 yield = string_cat(yield, &size, &ptr, outsep2, 1);
375 yield = string_cat(yield, &size, &ptr,
376 (uschar *)((rr->data)+data_offset), chunk_len);
377 data_offset += chunk_len;
381 else /* T_CNAME, T_CSA, T_MX, T_MXH, T_NS, T_PTR, T_SRV */
383 int priority, weight, port;
385 uschar *p = (uschar *)(rr->data);
389 /* mxh ignores the priority number and includes only the hostnames */
390 GETSHORT(priority, p);
392 else if (type == T_MX)
394 GETSHORT(priority, p);
395 sprintf(CS s, "%d ", priority);
396 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
398 else if (type == T_SRV)
400 GETSHORT(priority, p);
403 sprintf(CS s, "%d %d %d ", priority, weight, port);
404 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
406 else if (type == T_CSA)
408 /* See acl_verify_csa() for more comments about CSA. */
410 GETSHORT(priority, p);
414 if (priority != 1) continue; /* CSA version must be 1 */
416 /* If the CSA record we found is not the one we asked for, analyse
417 the subdomain assertions in the port field, else analyse the direct
418 authorization status in the weight field. */
422 if (port & 1) *s = 'X'; /* explicit authorization required */
423 else *s = '?'; /* no subdomain assertions here */
427 if (weight < 2) *s = 'N'; /* not authorized */
428 else if (weight == 2) *s = 'Y'; /* authorized */
429 else if (weight == 3) *s = '?'; /* unauthorizable */
430 else continue; /* invalid */
434 yield = string_cat(yield, &size, &ptr, s, 2);
437 /* GETSHORT() has advanced the pointer to the target domain. */
439 rc = dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, p,
440 (DN_EXPAND_ARG4_TYPE)(s), sizeof(s));
442 /* If an overlong response was received, the data will have been
443 truncated and dn_expand may fail. */
447 log_write(0, LOG_MAIN, "host name alias list truncated: type=%s "
448 "domain=%s", dns_text_type(type), domain);
451 else yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
453 } /* Loop for list of returned records */
455 /* Loop for set of A-lookupu types */
456 } while (type == T_APL && searchtype != T_A);
458 } /* Loop for list of domains */
460 /* Reclaim unused memory */
462 store_reset(yield + ptr + 1);
464 /* If ptr == 0 we have not found anything. Otherwise, insert the terminating
465 zero and return the result. */
467 if (ptr == 0) return failrc;
475 /*************************************************
476 * Version reporting entry point *
477 *************************************************/
479 /* See local README for interface description. */
481 #include "../version.h"
484 dnsdb_version_report(FILE *f)
487 fprintf(f, "Library version: DNSDB: Exim version %s\n", EXIM_VERSION_STR);
492 static lookup_info _lookup_info = {
493 US"dnsdb", /* lookup name */
494 lookup_querystyle, /* query style */
495 dnsdb_open, /* open function */
496 NULL, /* check function */
497 dnsdb_find, /* find function */
498 NULL, /* no close function */
499 NULL, /* no tidy function */
500 NULL, /* no quoting function */
501 dnsdb_version_report /* version reporting */
505 #define dnsdb_lookup_module_info _lookup_module_info
508 static lookup_info *_lookup_list[] = { &_lookup_info };
509 lookup_module_info dnsdb_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
511 /* End of lookups/dnsdb.c */