1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2009 */
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
101 (c) If the next sequence of characters is 'defer_FOO' followed by a comma,
102 the defer behaviour is set to FOO. The possible behaviours are: 'strict', where
103 any defer causes the whole lookup to defer; 'lax', where a defer causes the
104 whole lookup to defer only if none of the DNS queries succeeds; and 'never',
105 where all defers are as if the lookup failed. The default is 'lax'.
107 (d) If the next sequence of characters is a sequence of letters and digits
108 followed by '=', it is interpreted as the name of the DNS record type. The
111 (e) Then there follows list of domain names. This is a generalized Exim list,
112 which may start with '<' in order to set a specific separator. The default
113 separator, as always, is colon. */
116 dnsdb_find(void *handle, uschar *filename, uschar *keystring, int length,
117 uschar **result, uschar **errmsg, BOOL *do_cache)
123 int defer_mode = PASS;
126 uschar *outsep = US"\n";
127 uschar *outsep2 = NULL;
128 uschar *equals, *domain, *found;
131 /* Because we're the working in the search pool, we try to reclaim as much
132 store as possible later, so we preallocate the result here */
134 uschar *yield = store_get(size);
140 handle = handle; /* Keep picky compilers happy */
145 /* If the string starts with '>' we change the output separator.
146 If it's followed by ';' or ',' we set the TXT output separator. */
148 while (isspace(*keystring)) keystring++;
149 if (*keystring == '>')
151 outsep = keystring + 1;
153 if (*keystring == ',')
155 outsep2 = keystring + 1;
158 else if (*keystring == ';')
163 while (isspace(*keystring)) keystring++;
166 /* Check for a defer behaviour keyword. */
168 if (strncmpic(keystring, US"defer_", 6) == 0)
171 if (strncmpic(keystring, US"strict", 6) == 0)
176 else if (strncmpic(keystring, US"lax", 3) == 0)
181 else if (strncmpic(keystring, US"never", 5) == 0)
188 *errmsg = US"unsupported dnsdb defer behaviour";
191 while (isspace(*keystring)) keystring++;
192 if (*keystring++ != ',')
194 *errmsg = US"dnsdb defer behaviour syntax error";
197 while (isspace(*keystring)) keystring++;
200 /* If the keystring contains an = this must be preceded by a valid type name. */
202 if ((equals = Ustrchr(keystring, '=')) != NULL)
205 uschar *tend = equals;
207 while (tend > keystring && isspace(tend[-1])) tend--;
208 len = tend - keystring;
210 for (i = 0; i < sizeof(type_names)/sizeof(uschar *); i++)
212 if (len == Ustrlen(type_names[i]) &&
213 strncmpic(keystring, US type_names[i], len) == 0)
215 type = type_values[i];
220 if (i >= sizeof(type_names)/sizeof(uschar *))
222 *errmsg = US"unsupported DNS record type";
226 keystring = equals + 1;
227 while (isspace(*keystring)) keystring++;
230 /* Initialize the resolver in case this is the first time it has been used. */
232 dns_init(FALSE, FALSE);
234 /* The remainder of the string must be a list of domains. As long as the lookup
235 for at least one of them succeeds, we return success. Failure means that none
238 The original implementation did not support a list of domains. Adding the list
239 feature is compatible, except in one case: when PTR records are being looked up
240 for a single IPv6 address. Fortunately, we can hack in a compatibility feature
241 here: If the type is PTR and no list separator is specified, and the entire
242 remaining string is valid as an IP address, set an impossible separator so that
243 it is treated as one item. */
245 if (type == T_PTR && keystring[0] != '<' &&
246 string_is_ip_address(keystring, NULL) != 0)
249 /* Now scan the list and do a lookup for each item */
251 while ((domain = string_nextinlist(&keystring, &sep, buffer, sizeof(buffer)))
255 int searchtype = (type == T_CSA)? T_SRV : /* record type we want */
256 (type == T_MXH)? T_MX :
257 (type == T_ZNS)? T_NS : type;
259 /* If the type is PTR or CSA, we have to construct the relevant magic lookup
260 key if the original is an IP address (some experimental protocols are using
261 PTR records for different purposes where the key string is a host name, and
262 Exim's extended CSA can be keyed by domains or IP addresses). This code for
263 doing the reversal is now in a separate function. */
265 if ((type == T_PTR || type == T_CSA) &&
266 string_is_ip_address(domain, NULL) != 0)
268 dns_build_reverse(domain, rbuffer);
272 DEBUG(D_lookup) debug_printf("dnsdb key: %s\n", domain);
274 /* Do the lookup and sort out the result. There are three special types that
275 are handled specially: T_CSA, T_ZNS and T_MXH. The former two are handled in
276 a special lookup function so that the facility could be used from other
277 parts of the Exim code. The latter affects only what happens later on in
278 this function, but for tidiness it is handled in a similar way. If the
279 lookup fails, continue with the next domain. In the case of DEFER, adjust
280 the final "nothing found" result, but carry on to the next domain. */
283 rc = dns_special_lookup(&dnsa, domain, type, &found);
285 if (rc == DNS_NOMATCH || rc == DNS_NODATA) continue;
286 if (rc != DNS_SUCCEED)
288 if (defer_mode == DEFER) return DEFER; /* always defer */
289 else if (defer_mode == PASS) failrc = DEFER; /* defer only if all do */
290 continue; /* treat defer as fail */
293 /* Search the returned records */
295 for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
297 rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
299 if (rr->type != searchtype) continue;
301 /* There may be several addresses from an A6 record. Put the configured
302 separator between them, just as for between several records. However, A6
303 support is not normally configured these days. */
312 for (da = dns_address_from_rr(&dnsa, rr); da != NULL; da = da->next)
314 if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
315 yield = string_cat(yield, &size, &ptr, da->address,
316 Ustrlen(da->address));
321 /* Other kinds of record just have one piece of data each, but there may be
322 several of them, of course. */
324 if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
326 if (type == T_TXT || type == T_SPF)
330 /* output only the first item of data */
331 yield = string_cat(yield, &size, &ptr, (uschar *)(rr->data+1),
336 /* output all items */
338 while (data_offset < rr->size)
340 uschar chunk_len = (rr->data)[data_offset++];
341 if (outsep2[0] != '\0' && data_offset != 1)
342 yield = string_cat(yield, &size, &ptr, outsep2, 1);
343 yield = string_cat(yield, &size, &ptr,
344 (uschar *)((rr->data)+data_offset), chunk_len);
345 data_offset += chunk_len;
349 else /* T_CNAME, T_CSA, T_MX, T_MXH, T_NS, T_PTR, T_SRV */
351 int priority, weight, port;
353 uschar *p = (uschar *)(rr->data);
357 /* mxh ignores the priority number and includes only the hostnames */
358 GETSHORT(priority, p);
360 else if (type == T_MX)
362 GETSHORT(priority, p);
363 sprintf(CS s, "%d ", priority);
364 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
366 else if (type == T_SRV)
368 GETSHORT(priority, p);
371 sprintf(CS s, "%d %d %d ", priority, weight, port);
372 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
374 else if (type == T_CSA)
376 /* See acl_verify_csa() for more comments about CSA. */
378 GETSHORT(priority, p);
382 if (priority != 1) continue; /* CSA version must be 1 */
384 /* If the CSA record we found is not the one we asked for, analyse
385 the subdomain assertions in the port field, else analyse the direct
386 authorization status in the weight field. */
390 if (port & 1) *s = 'X'; /* explicit authorization required */
391 else *s = '?'; /* no subdomain assertions here */
395 if (weight < 2) *s = 'N'; /* not authorized */
396 else if (weight == 2) *s = 'Y'; /* authorized */
397 else if (weight == 3) *s = '?'; /* unauthorizable */
398 else continue; /* invalid */
402 yield = string_cat(yield, &size, &ptr, s, 2);
405 /* GETSHORT() has advanced the pointer to the target domain. */
407 rc = dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, p,
408 (DN_EXPAND_ARG4_TYPE)(s), sizeof(s));
410 /* If an overlong response was received, the data will have been
411 truncated and dn_expand may fail. */
415 log_write(0, LOG_MAIN, "host name alias list truncated: type=%s "
416 "domain=%s", dns_text_type(type), domain);
419 else yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
421 } /* Loop for list of returned records */
422 } /* Loop for list of domains */
424 /* Reclaim unused memory */
426 store_reset(yield + ptr + 1);
428 /* If ptr == 0 we have not found anything. Otherwise, insert the terminating
429 zero and return the result. */
431 if (ptr == 0) return failrc;
439 /*************************************************
440 * Version reporting entry point *
441 *************************************************/
443 /* See local README for interface description. */
445 #include "../version.h"
448 dnsdb_version_report(FILE *f)
451 fprintf(f, "Library version: DNSDB: Exim version %s\n", EXIM_VERSION_STR);
456 static lookup_info _lookup_info = {
457 US"dnsdb", /* lookup name */
458 lookup_querystyle, /* query style */
459 dnsdb_open, /* open function */
460 NULL, /* check function */
461 dnsdb_find, /* find function */
462 NULL, /* no close function */
463 NULL, /* no tidy function */
464 NULL, /* no quoting function */
465 dnsdb_version_report /* version reporting */
469 #define dnsdb_lookup_module_info _lookup_module_info
472 static lookup_info *_lookup_list[] = { &_lookup_info };
473 lookup_module_info dnsdb_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
475 /* End of lookups/dnsdb.c */