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[] = {
47 static int type_values[] = {
56 T_CSA, /* Private type for "Client SMTP Authorization". */
58 T_MXH, /* Private type for "MX hostnames" */
64 T_ZNS /* Private type for "zone nameservers" */
68 /*************************************************
70 *************************************************/
72 /* See local README for interface description. */
75 dnsdb_open(uschar *filename, uschar **errmsg)
77 filename = filename; /* Keep picky compilers happy */
78 errmsg = errmsg; /* Ditto */
79 return (void *)(-1); /* Any non-0 value */
84 /*************************************************
85 * Find entry point for dnsdb *
86 *************************************************/
88 /* See local README for interface description. The query in the "keystring" may
89 consist of a number of parts.
91 (a) If the first significant character is '>' then the next character is the
92 separator character that is used when multiple records are found. The default
95 (b) If the next character is ',' then the next character is the separator
96 character used for multiple items of text in "TXT" records. Alternatively,
97 if the next character is ';' then these multiple items are concatenated with
98 no separator. With neither of these options specified, only the first item
99 is output. Similarly for "SPF" records, but the default for joining multiple
100 items in one SPF record is the empty string, for direct concatenation.
102 (c) If the next sequence of characters is 'defer_FOO' followed by a comma,
103 the defer behaviour is set to FOO. The possible behaviours are: 'strict', where
104 any defer causes the whole lookup to defer; 'lax', where a defer causes the
105 whole lookup to defer only if none of the DNS queries succeeds; and 'never',
106 where all defers are as if the lookup failed. The default is 'lax'.
108 (d) If the next sequence of characters is a sequence of letters and digits
109 followed by '=', it is interpreted as the name of the DNS record type. The
112 (e) Then there follows list of domain names. This is a generalized Exim list,
113 which may start with '<' in order to set a specific separator. The default
114 separator, as always, is colon. */
117 dnsdb_find(void *handle, uschar *filename, uschar *keystring, int length,
118 uschar **result, uschar **errmsg, BOOL *do_cache)
124 int defer_mode = PASS;
127 uschar *outsep = US"\n";
128 uschar *outsep2 = NULL;
129 uschar *equals, *domain, *found;
132 /* Because we're the working in the search pool, we try to reclaim as much
133 store as possible later, so we preallocate the result here */
135 uschar *yield = store_get(size);
141 handle = handle; /* Keep picky compilers happy */
146 /* If the string starts with '>' we change the output separator.
147 If it's followed by ';' or ',' we set the TXT output separator. */
149 while (isspace(*keystring)) keystring++;
150 if (*keystring == '>')
152 outsep = keystring + 1;
154 if (*keystring == ',')
156 outsep2 = keystring + 1;
159 else if (*keystring == ';')
164 while (isspace(*keystring)) keystring++;
167 /* Check for a defer behaviour keyword. */
169 if (strncmpic(keystring, US"defer_", 6) == 0)
172 if (strncmpic(keystring, US"strict", 6) == 0)
177 else if (strncmpic(keystring, US"lax", 3) == 0)
182 else if (strncmpic(keystring, US"never", 5) == 0)
189 *errmsg = US"unsupported dnsdb defer behaviour";
192 while (isspace(*keystring)) keystring++;
193 if (*keystring++ != ',')
195 *errmsg = US"dnsdb defer behaviour syntax error";
198 while (isspace(*keystring)) keystring++;
201 /* Figure out the "type" value if it is not T_TXT.
202 If the keystring contains an = this must be preceded by a valid type name. */
205 if ((equals = Ustrchr(keystring, '=')) != NULL)
208 uschar *tend = equals;
210 while (tend > keystring && isspace(tend[-1])) tend--;
211 len = tend - keystring;
213 for (i = 0; i < sizeof(type_names)/sizeof(uschar *); i++)
215 if (len == Ustrlen(type_names[i]) &&
216 strncmpic(keystring, US type_names[i], len) == 0)
218 type = type_values[i];
223 if (i >= sizeof(type_names)/sizeof(uschar *))
225 *errmsg = US"unsupported DNS record type";
229 keystring = equals + 1;
230 while (isspace(*keystring)) keystring++;
233 /* Initialize the resolver in case this is the first time it has been used. */
235 dns_init(FALSE, FALSE);
237 /* The remainder of the string must be a list of domains. As long as the lookup
238 for at least one of them succeeds, we return success. Failure means that none
241 The original implementation did not support a list of domains. Adding the list
242 feature is compatible, except in one case: when PTR records are being looked up
243 for a single IPv6 address. Fortunately, we can hack in a compatibility feature
244 here: If the type is PTR and no list separator is specified, and the entire
245 remaining string is valid as an IP address, set an impossible separator so that
246 it is treated as one item. */
248 if (type == T_PTR && keystring[0] != '<' &&
249 string_is_ip_address(keystring, NULL) != 0)
252 /* SPF strings should be concatenated without a separator, thus make
253 it the default if not defined (see RFC 4408 section 3.1.3).
254 Multiple SPF records are forbidden (section 3.1.2) but are currently
255 not handled specially, thus they are concatenated with \n by default. */
257 if (type == T_SPF && outsep2 == NULL)
260 /* Now scan the list and do a lookup for each item */
262 while ((domain = string_nextinlist(&keystring, &sep, buffer, sizeof(buffer)))
266 int searchtype = (type == T_CSA)? T_SRV : /* record type we want */
267 (type == T_MXH)? T_MX :
268 (type == T_ZNS)? T_NS : type;
270 /* If the type is PTR or CSA, we have to construct the relevant magic lookup
271 key if the original is an IP address (some experimental protocols are using
272 PTR records for different purposes where the key string is a host name, and
273 Exim's extended CSA can be keyed by domains or IP addresses). This code for
274 doing the reversal is now in a separate function. */
276 if ((type == T_PTR || type == T_CSA) &&
277 string_is_ip_address(domain, NULL) != 0)
279 dns_build_reverse(domain, rbuffer);
283 DEBUG(D_lookup) debug_printf("dnsdb key: %s\n", domain);
285 /* Do the lookup and sort out the result. There are three special types that
286 are handled specially: T_CSA, T_ZNS and T_MXH. The former two are handled in
287 a special lookup function so that the facility could be used from other
288 parts of the Exim code. The latter affects only what happens later on in
289 this function, but for tidiness it is handled in a similar way. If the
290 lookup fails, continue with the next domain. In the case of DEFER, adjust
291 the final "nothing found" result, but carry on to the next domain. */
294 rc = dns_special_lookup(&dnsa, domain, type, &found);
296 if (rc == DNS_NOMATCH || rc == DNS_NODATA) continue;
297 if (rc != DNS_SUCCEED)
299 if (defer_mode == DEFER) return DEFER; /* always defer */
300 else if (defer_mode == PASS) failrc = DEFER; /* defer only if all do */
301 continue; /* treat defer as fail */
304 /* Search the returned records */
306 for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
308 rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
310 if (rr->type != searchtype) continue;
312 /* There may be several addresses from an A6 record. Put the configured
313 separator between them, just as for between several records. However, A6
314 support is not normally configured these days. */
323 for (da = dns_address_from_rr(&dnsa, rr); da != NULL; da = da->next)
325 if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
326 yield = string_cat(yield, &size, &ptr, da->address,
327 Ustrlen(da->address));
332 /* Other kinds of record just have one piece of data each, but there may be
333 several of them, of course. */
335 if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
337 if (type == T_TXT || type == T_SPF)
341 /* output only the first item of data */
342 yield = string_cat(yield, &size, &ptr, (uschar *)(rr->data+1),
347 /* output all items */
349 while (data_offset < rr->size)
351 uschar chunk_len = (rr->data)[data_offset++];
352 if (outsep2[0] != '\0' && data_offset != 1)
353 yield = string_cat(yield, &size, &ptr, outsep2, 1);
354 yield = string_cat(yield, &size, &ptr,
355 (uschar *)((rr->data)+data_offset), chunk_len);
356 data_offset += chunk_len;
360 else /* T_CNAME, T_CSA, T_MX, T_MXH, T_NS, T_PTR, T_SRV */
362 int priority, weight, port;
364 uschar *p = (uschar *)(rr->data);
368 /* mxh ignores the priority number and includes only the hostnames */
369 GETSHORT(priority, p);
371 else if (type == T_MX)
373 GETSHORT(priority, p);
374 sprintf(CS s, "%d ", priority);
375 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
377 else if (type == T_SRV)
379 GETSHORT(priority, p);
382 sprintf(CS s, "%d %d %d ", priority, weight, port);
383 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
385 else if (type == T_CSA)
387 /* See acl_verify_csa() for more comments about CSA. */
389 GETSHORT(priority, p);
393 if (priority != 1) continue; /* CSA version must be 1 */
395 /* If the CSA record we found is not the one we asked for, analyse
396 the subdomain assertions in the port field, else analyse the direct
397 authorization status in the weight field. */
401 if (port & 1) *s = 'X'; /* explicit authorization required */
402 else *s = '?'; /* no subdomain assertions here */
406 if (weight < 2) *s = 'N'; /* not authorized */
407 else if (weight == 2) *s = 'Y'; /* authorized */
408 else if (weight == 3) *s = '?'; /* unauthorizable */
409 else continue; /* invalid */
413 yield = string_cat(yield, &size, &ptr, s, 2);
416 /* GETSHORT() has advanced the pointer to the target domain. */
418 rc = dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, p,
419 (DN_EXPAND_ARG4_TYPE)(s), sizeof(s));
421 /* If an overlong response was received, the data will have been
422 truncated and dn_expand may fail. */
426 log_write(0, LOG_MAIN, "host name alias list truncated: type=%s "
427 "domain=%s", dns_text_type(type), domain);
430 else yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
432 } /* Loop for list of returned records */
433 } /* Loop for list of domains */
435 /* Reclaim unused memory */
437 store_reset(yield + ptr + 1);
439 /* If ptr == 0 we have not found anything. Otherwise, insert the terminating
440 zero and return the result. */
442 if (ptr == 0) return failrc;
450 /*************************************************
451 * Version reporting entry point *
452 *************************************************/
454 /* See local README for interface description. */
456 #include "../version.h"
459 dnsdb_version_report(FILE *f)
462 fprintf(f, "Library version: DNSDB: Exim version %s\n", EXIM_VERSION_STR);
467 static lookup_info _lookup_info = {
468 US"dnsdb", /* lookup name */
469 lookup_querystyle, /* query style */
470 dnsdb_open, /* open function */
471 NULL, /* check function */
472 dnsdb_find, /* find function */
473 NULL, /* no close function */
474 NULL, /* no tidy function */
475 NULL, /* no quoting function */
476 dnsdb_version_report /* version reporting */
480 #define dnsdb_lookup_module_info _lookup_module_info
483 static lookup_info *_lookup_list[] = { &_lookup_info };
484 lookup_module_info dnsdb_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
486 /* End of lookups/dnsdb.c */