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"
16 /* Ancient systems (e.g. SunOS4) don't appear to have T_TXT defined in their
23 /* Table of recognized DNS record types and their integer values. */
25 static char *type_names[] = {
44 static int type_values[] = {
53 T_CSA, /* Private type for "Client SMTP Authorization". */
55 T_MXH, /* Private type for "MX hostnames" */
60 T_ZNS /* Private type for "zone nameservers" */
64 /*************************************************
66 *************************************************/
68 /* See local README for interface description. */
71 dnsdb_open(uschar *filename, uschar **errmsg)
73 filename = filename; /* Keep picky compilers happy */
74 errmsg = errmsg; /* Ditto */
75 return (void *)(-1); /* Any non-0 value */
80 /*************************************************
81 * Find entry point for dnsdb *
82 *************************************************/
84 /* See local README for interface description. The query in the "keystring" may
85 consist of a number of parts.
87 (a) If the first significant character is '>' then the next character is the
88 separator character that is used when multiple records are found. The default
91 (b) If the next character is ',' then the next character is the separator
92 character used for multiple items of text in "TXT" records. Alternatively,
93 if the next character is ';' then these multiple items are concatenated with
94 no separator. With neither of these options specified, only the first item
97 (c) If the next sequence of characters is 'defer_FOO' followed by a comma,
98 the defer behaviour is set to FOO. The possible behaviours are: 'strict', where
99 any defer causes the whole lookup to defer; 'lax', where a defer causes the
100 whole lookup to defer only if none of the DNS queries succeeds; and 'never',
101 where all defers are as if the lookup failed. The default is 'lax'.
103 (d) If the next sequence of characters is a sequence of letters and digits
104 followed by '=', it is interpreted as the name of the DNS record type. The
107 (e) Then there follows list of domain names. This is a generalized Exim list,
108 which may start with '<' in order to set a specific separator. The default
109 separator, as always, is colon. */
112 dnsdb_find(void *handle, uschar *filename, uschar *keystring, int length,
113 uschar **result, uschar **errmsg, BOOL *do_cache)
119 int defer_mode = PASS;
122 uschar *outsep = US"\n";
123 uschar *outsep2 = NULL;
124 uschar *equals, *domain, *found;
127 /* Because we're the working in the search pool, we try to reclaim as much
128 store as possible later, so we preallocate the result here */
130 uschar *yield = store_get(size);
136 handle = handle; /* Keep picky compilers happy */
141 /* If the string starts with '>' we change the output separator.
142 If it's followed by ';' or ',' we set the TXT output separator. */
144 while (isspace(*keystring)) keystring++;
145 if (*keystring == '>')
147 outsep = keystring + 1;
149 if (*keystring == ',')
151 outsep2 = keystring + 1;
154 else if (*keystring == ';')
159 while (isspace(*keystring)) keystring++;
162 /* Check for a defer behaviour keyword. */
164 if (strncmpic(keystring, US"defer_", 6) == 0)
167 if (strncmpic(keystring, US"strict", 6) == 0)
172 else if (strncmpic(keystring, US"lax", 3) == 0)
177 else if (strncmpic(keystring, US"never", 5) == 0)
184 *errmsg = US"unsupported dnsdb defer behaviour";
187 while (isspace(*keystring)) keystring++;
188 if (*keystring++ != ',')
190 *errmsg = US"dnsdb defer behaviour syntax error";
193 while (isspace(*keystring)) keystring++;
196 /* If the keystring contains an = this must be preceded by a valid type name. */
198 if ((equals = Ustrchr(keystring, '=')) != NULL)
201 uschar *tend = equals;
203 while (tend > keystring && isspace(tend[-1])) tend--;
204 len = tend - keystring;
206 for (i = 0; i < sizeof(type_names)/sizeof(uschar *); i++)
208 if (len == Ustrlen(type_names[i]) &&
209 strncmpic(keystring, US type_names[i], len) == 0)
211 type = type_values[i];
216 if (i >= sizeof(type_names)/sizeof(uschar *))
218 *errmsg = US"unsupported DNS record type";
222 keystring = equals + 1;
223 while (isspace(*keystring)) keystring++;
226 /* Initialize the resolver in case this is the first time it has been used. */
228 dns_init(FALSE, FALSE);
230 /* The remainder of the string must be a list of domains. As long as the lookup
231 for at least one of them succeeds, we return success. Failure means that none
234 The original implementation did not support a list of domains. Adding the list
235 feature is compatible, except in one case: when PTR records are being looked up
236 for a single IPv6 address. Fortunately, we can hack in a compatibility feature
237 here: If the type is PTR and no list separator is specified, and the entire
238 remaining string is valid as an IP address, set an impossible separator so that
239 it is treated as one item. */
241 if (type == T_PTR && keystring[0] != '<' &&
242 string_is_ip_address(keystring, NULL) != 0)
245 /* Now scan the list and do a lookup for each item */
247 while ((domain = string_nextinlist(&keystring, &sep, buffer, sizeof(buffer)))
251 int searchtype = (type == T_CSA)? T_SRV : /* record type we want */
252 (type == T_MXH)? T_MX :
253 (type == T_ZNS)? T_NS : type;
255 /* If the type is PTR or CSA, we have to construct the relevant magic lookup
256 key if the original is an IP address (some experimental protocols are using
257 PTR records for different purposes where the key string is a host name, and
258 Exim's extended CSA can be keyed by domains or IP addresses). This code for
259 doing the reversal is now in a separate function. */
261 if ((type == T_PTR || type == T_CSA) &&
262 string_is_ip_address(domain, NULL) != 0)
264 dns_build_reverse(domain, rbuffer);
268 DEBUG(D_lookup) debug_printf("dnsdb key: %s\n", domain);
270 /* Do the lookup and sort out the result. There are three special types that
271 are handled specially: T_CSA, T_ZNS and T_MXH. The former two are handled in
272 a special lookup function so that the facility could be used from other
273 parts of the Exim code. The latter affects only what happens later on in
274 this function, but for tidiness it is handled in a similar way. If the
275 lookup fails, continue with the next domain. In the case of DEFER, adjust
276 the final "nothing found" result, but carry on to the next domain. */
279 rc = dns_special_lookup(&dnsa, domain, type, &found);
281 if (rc == DNS_NOMATCH || rc == DNS_NODATA) continue;
282 if (rc != DNS_SUCCEED)
284 if (defer_mode == DEFER) return DEFER; /* always defer */
285 else if (defer_mode == PASS) failrc = DEFER; /* defer only if all do */
286 continue; /* treat defer as fail */
289 /* Search the returned records */
291 for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
293 rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
295 if (rr->type != searchtype) continue;
297 /* There may be several addresses from an A6 record. Put the configured
298 separator between them, just as for between several records. However, A6
299 support is not normally configured these days. */
308 for (da = dns_address_from_rr(&dnsa, rr); da != NULL; da = da->next)
310 if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
311 yield = string_cat(yield, &size, &ptr, da->address,
312 Ustrlen(da->address));
317 /* Other kinds of record just have one piece of data each, but there may be
318 several of them, of course. */
320 if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
326 /* output only the first item of data */
327 yield = string_cat(yield, &size, &ptr, (uschar *)(rr->data+1),
332 /* output all items */
334 while (data_offset < rr->size)
336 uschar chunk_len = (rr->data)[data_offset++];
337 if (outsep2[0] != '\0' && data_offset != 1)
338 yield = string_cat(yield, &size, &ptr, outsep2, 1);
339 yield = string_cat(yield, &size, &ptr,
340 (uschar *)((rr->data)+data_offset), chunk_len);
341 data_offset += chunk_len;
345 else /* T_CNAME, T_CSA, T_MX, T_MXH, T_NS, T_PTR, T_SRV */
347 int priority, weight, port;
349 uschar *p = (uschar *)(rr->data);
353 /* mxh ignores the priority number and includes only the hostnames */
354 GETSHORT(priority, p);
356 else if (type == T_MX)
358 GETSHORT(priority, p);
359 sprintf(CS s, "%d ", priority);
360 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
362 else if (type == T_SRV)
364 GETSHORT(priority, p);
367 sprintf(CS s, "%d %d %d ", priority, weight, port);
368 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
370 else if (type == T_CSA)
372 /* See acl_verify_csa() for more comments about CSA. */
374 GETSHORT(priority, p);
378 if (priority != 1) continue; /* CSA version must be 1 */
380 /* If the CSA record we found is not the one we asked for, analyse
381 the subdomain assertions in the port field, else analyse the direct
382 authorization status in the weight field. */
386 if (port & 1) *s = 'X'; /* explicit authorization required */
387 else *s = '?'; /* no subdomain assertions here */
391 if (weight < 2) *s = 'N'; /* not authorized */
392 else if (weight == 2) *s = 'Y'; /* authorized */
393 else if (weight == 3) *s = '?'; /* unauthorizable */
394 else continue; /* invalid */
398 yield = string_cat(yield, &size, &ptr, s, 2);
401 /* GETSHORT() has advanced the pointer to the target domain. */
403 rc = dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, p,
404 (DN_EXPAND_ARG4_TYPE)(s), sizeof(s));
406 /* If an overlong response was received, the data will have been
407 truncated and dn_expand may fail. */
411 log_write(0, LOG_MAIN, "host name alias list truncated: type=%s "
412 "domain=%s", dns_text_type(type), domain);
415 else yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
417 } /* Loop for list of returned records */
418 } /* Loop for list of domains */
420 /* Reclaim unused memory */
422 store_reset(yield + ptr + 1);
424 /* If ptr == 0 we have not found anything. Otherwise, insert the terminating
425 zero and return the result. */
427 if (ptr == 0) return failrc;
433 /* End of lookups/dnsdb.c */