1 /* $Cambridge: exim/src/src/lookups/dnsdb.c,v 1.20 2010/05/29 19:23:26 nm4 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2009 */
8 /* See the file NOTICE for conditions of use and distribution. */
11 #include "lf_functions.h"
15 /* Ancient systems (e.g. SunOS4) don't appear to have T_TXT defined in their
22 /* Table of recognized DNS record types and their integer values. */
24 static const char *type_names[] = {
43 static int type_values[] = {
52 T_CSA, /* Private type for "Client SMTP Authorization". */
54 T_MXH, /* Private type for "MX hostnames" */
59 T_ZNS /* Private type for "zone nameservers" */
63 /*************************************************
65 *************************************************/
67 /* See local README for interface description. */
70 dnsdb_open(uschar *filename, uschar **errmsg)
72 filename = filename; /* Keep picky compilers happy */
73 errmsg = errmsg; /* Ditto */
74 return (void *)(-1); /* Any non-0 value */
79 /*************************************************
80 * Find entry point for dnsdb *
81 *************************************************/
83 /* See local README for interface description. The query in the "keystring" may
84 consist of a number of parts.
86 (a) If the first significant character is '>' then the next character is the
87 separator character that is used when multiple records are found. The default
90 (b) If the next character is ',' then the next character is the separator
91 character used for multiple items of text in "TXT" records. Alternatively,
92 if the next character is ';' then these multiple items are concatenated with
93 no separator. With neither of these options specified, only the first item
96 (c) If the next sequence of characters is 'defer_FOO' followed by a comma,
97 the defer behaviour is set to FOO. The possible behaviours are: 'strict', where
98 any defer causes the whole lookup to defer; 'lax', where a defer causes the
99 whole lookup to defer only if none of the DNS queries succeeds; and 'never',
100 where all defers are as if the lookup failed. The default is 'lax'.
102 (d) If the next sequence of characters is a sequence of letters and digits
103 followed by '=', it is interpreted as the name of the DNS record type. The
106 (e) Then there follows list of domain names. This is a generalized Exim list,
107 which may start with '<' in order to set a specific separator. The default
108 separator, as always, is colon. */
111 dnsdb_find(void *handle, uschar *filename, uschar *keystring, int length,
112 uschar **result, uschar **errmsg, BOOL *do_cache)
118 int defer_mode = PASS;
121 uschar *outsep = US"\n";
122 uschar *outsep2 = NULL;
123 uschar *equals, *domain, *found;
126 /* Because we're the working in the search pool, we try to reclaim as much
127 store as possible later, so we preallocate the result here */
129 uschar *yield = store_get(size);
135 handle = handle; /* Keep picky compilers happy */
140 /* If the string starts with '>' we change the output separator.
141 If it's followed by ';' or ',' we set the TXT output separator. */
143 while (isspace(*keystring)) keystring++;
144 if (*keystring == '>')
146 outsep = keystring + 1;
148 if (*keystring == ',')
150 outsep2 = keystring + 1;
153 else if (*keystring == ';')
158 while (isspace(*keystring)) keystring++;
161 /* Check for a defer behaviour keyword. */
163 if (strncmpic(keystring, US"defer_", 6) == 0)
166 if (strncmpic(keystring, US"strict", 6) == 0)
171 else if (strncmpic(keystring, US"lax", 3) == 0)
176 else if (strncmpic(keystring, US"never", 5) == 0)
183 *errmsg = US"unsupported dnsdb defer behaviour";
186 while (isspace(*keystring)) keystring++;
187 if (*keystring++ != ',')
189 *errmsg = US"dnsdb defer behaviour syntax error";
192 while (isspace(*keystring)) keystring++;
195 /* If the keystring contains an = this must be preceded by a valid type name. */
197 if ((equals = Ustrchr(keystring, '=')) != NULL)
200 uschar *tend = equals;
202 while (tend > keystring && isspace(tend[-1])) tend--;
203 len = tend - keystring;
205 for (i = 0; i < sizeof(type_names)/sizeof(uschar *); i++)
207 if (len == Ustrlen(type_names[i]) &&
208 strncmpic(keystring, US type_names[i], len) == 0)
210 type = type_values[i];
215 if (i >= sizeof(type_names)/sizeof(uschar *))
217 *errmsg = US"unsupported DNS record type";
221 keystring = equals + 1;
222 while (isspace(*keystring)) keystring++;
225 /* Initialize the resolver in case this is the first time it has been used. */
227 dns_init(FALSE, FALSE);
229 /* The remainder of the string must be a list of domains. As long as the lookup
230 for at least one of them succeeds, we return success. Failure means that none
233 The original implementation did not support a list of domains. Adding the list
234 feature is compatible, except in one case: when PTR records are being looked up
235 for a single IPv6 address. Fortunately, we can hack in a compatibility feature
236 here: If the type is PTR and no list separator is specified, and the entire
237 remaining string is valid as an IP address, set an impossible separator so that
238 it is treated as one item. */
240 if (type == T_PTR && keystring[0] != '<' &&
241 string_is_ip_address(keystring, NULL) != 0)
244 /* Now scan the list and do a lookup for each item */
246 while ((domain = string_nextinlist(&keystring, &sep, buffer, sizeof(buffer)))
250 int searchtype = (type == T_CSA)? T_SRV : /* record type we want */
251 (type == T_MXH)? T_MX :
252 (type == T_ZNS)? T_NS : type;
254 /* If the type is PTR or CSA, we have to construct the relevant magic lookup
255 key if the original is an IP address (some experimental protocols are using
256 PTR records for different purposes where the key string is a host name, and
257 Exim's extended CSA can be keyed by domains or IP addresses). This code for
258 doing the reversal is now in a separate function. */
260 if ((type == T_PTR || type == T_CSA) &&
261 string_is_ip_address(domain, NULL) != 0)
263 dns_build_reverse(domain, rbuffer);
267 DEBUG(D_lookup) debug_printf("dnsdb key: %s\n", domain);
269 /* Do the lookup and sort out the result. There are three special types that
270 are handled specially: T_CSA, T_ZNS and T_MXH. The former two are handled in
271 a special lookup function so that the facility could be used from other
272 parts of the Exim code. The latter affects only what happens later on in
273 this function, but for tidiness it is handled in a similar way. If the
274 lookup fails, continue with the next domain. In the case of DEFER, adjust
275 the final "nothing found" result, but carry on to the next domain. */
278 rc = dns_special_lookup(&dnsa, domain, type, &found);
280 if (rc == DNS_NOMATCH || rc == DNS_NODATA) continue;
281 if (rc != DNS_SUCCEED)
283 if (defer_mode == DEFER) return DEFER; /* always defer */
284 else if (defer_mode == PASS) failrc = DEFER; /* defer only if all do */
285 continue; /* treat defer as fail */
288 /* Search the returned records */
290 for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
292 rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
294 if (rr->type != searchtype) continue;
296 /* There may be several addresses from an A6 record. Put the configured
297 separator between them, just as for between several records. However, A6
298 support is not normally configured these days. */
307 for (da = dns_address_from_rr(&dnsa, rr); da != NULL; da = da->next)
309 if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
310 yield = string_cat(yield, &size, &ptr, da->address,
311 Ustrlen(da->address));
316 /* Other kinds of record just have one piece of data each, but there may be
317 several of them, of course. */
319 if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
325 /* output only the first item of data */
326 yield = string_cat(yield, &size, &ptr, (uschar *)(rr->data+1),
331 /* output all items */
333 while (data_offset < rr->size)
335 uschar chunk_len = (rr->data)[data_offset++];
336 if (outsep2[0] != '\0' && data_offset != 1)
337 yield = string_cat(yield, &size, &ptr, outsep2, 1);
338 yield = string_cat(yield, &size, &ptr,
339 (uschar *)((rr->data)+data_offset), chunk_len);
340 data_offset += chunk_len;
344 else /* T_CNAME, T_CSA, T_MX, T_MXH, T_NS, T_PTR, T_SRV */
346 int priority, weight, port;
348 uschar *p = (uschar *)(rr->data);
352 /* mxh ignores the priority number and includes only the hostnames */
353 GETSHORT(priority, p);
355 else if (type == T_MX)
357 GETSHORT(priority, p);
358 sprintf(CS s, "%d ", priority);
359 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
361 else if (type == T_SRV)
363 GETSHORT(priority, p);
366 sprintf(CS s, "%d %d %d ", priority, weight, port);
367 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
369 else if (type == T_CSA)
371 /* See acl_verify_csa() for more comments about CSA. */
373 GETSHORT(priority, p);
377 if (priority != 1) continue; /* CSA version must be 1 */
379 /* If the CSA record we found is not the one we asked for, analyse
380 the subdomain assertions in the port field, else analyse the direct
381 authorization status in the weight field. */
385 if (port & 1) *s = 'X'; /* explicit authorization required */
386 else *s = '?'; /* no subdomain assertions here */
390 if (weight < 2) *s = 'N'; /* not authorized */
391 else if (weight == 2) *s = 'Y'; /* authorized */
392 else if (weight == 3) *s = '?'; /* unauthorizable */
393 else continue; /* invalid */
397 yield = string_cat(yield, &size, &ptr, s, 2);
400 /* GETSHORT() has advanced the pointer to the target domain. */
402 rc = dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, p,
403 (DN_EXPAND_ARG4_TYPE)(s), sizeof(s));
405 /* If an overlong response was received, the data will have been
406 truncated and dn_expand may fail. */
410 log_write(0, LOG_MAIN, "host name alias list truncated: type=%s "
411 "domain=%s", dns_text_type(type), domain);
414 else yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
416 } /* Loop for list of returned records */
417 } /* Loop for list of domains */
419 /* Reclaim unused memory */
421 store_reset(yield + ptr + 1);
423 /* If ptr == 0 we have not found anything. Otherwise, insert the terminating
424 zero and return the result. */
426 if (ptr == 0) return failrc;
434 /*************************************************
435 * Version reporting entry point *
436 *************************************************/
438 /* See local README for interface description. */
440 #include "../version.h"
443 dnsdb_version_report(FILE *f)
446 fprintf(f, "Library version: DNSDB: Exim version %s\n", EXIM_VERSION_STR);
451 static lookup_info _lookup_info = {
452 US"dnsdb", /* lookup name */
453 lookup_querystyle, /* query style */
454 dnsdb_open, /* open function */
455 NULL, /* check function */
456 dnsdb_find, /* find function */
457 NULL, /* no close function */
458 NULL, /* no tidy function */
459 NULL, /* no quoting function */
460 dnsdb_version_report /* version reporting */
464 #define dnsdb_lookup_module_info _lookup_module_info
467 static lookup_info *_lookup_list[] = { &_lookup_info };
468 lookup_module_info dnsdb_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
470 /* End of lookups/dnsdb.c */