1 /* $Cambridge: exim/src/src/lookups/dnsdb.c,v 1.17 2007/01/08 10:50:19 ph10 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2007 */
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 sequence of characters is 'defer_FOO' followed by a comma,
92 the defer behaviour is set to FOO. The possible behaviours are: 'strict', where
93 any defer causes the whole lookup to defer; 'lax', where a defer causes the
94 whole lookup to defer only if none of the DNS queries succeeds; and 'never',
95 where all defers are as if the lookup failed. The default is 'lax'.
97 (c) If the next sequence of characters is a sequence of letters and digits
98 followed by '=', it is interpreted as the name of the DNS record type. The
101 (d) Then there follows list of domain names. This is a generalized Exim list,
102 which may start with '<' in order to set a specific separator. The default
103 separator, as always, is colon. */
106 dnsdb_find(void *handle, uschar *filename, uschar *keystring, int length,
107 uschar **result, uschar **errmsg, BOOL *do_cache)
113 int defer_mode = PASS;
116 uschar *outsep = US"\n";
117 uschar *equals, *domain, *found;
120 /* Because we're the working in the search pool, we try to reclaim as much
121 store as possible later, so we preallocate the result here */
123 uschar *yield = store_get(size);
129 handle = handle; /* Keep picky compilers happy */
134 /* If the string starts with '>' we change the output separator */
136 while (isspace(*keystring)) keystring++;
137 if (*keystring == '>')
139 outsep = keystring + 1;
141 while (isspace(*keystring)) keystring++;
144 /* Check for a defer behaviour keyword. */
146 if (strncmpic(keystring, US"defer_", 6) == 0)
149 if (strncmpic(keystring, US"strict", 6) == 0)
154 else if (strncmpic(keystring, US"lax", 3) == 0)
159 else if (strncmpic(keystring, US"never", 5) == 0)
166 *errmsg = US"unsupported dnsdb defer behaviour";
169 while (isspace(*keystring)) keystring++;
170 if (*keystring++ != ',')
172 *errmsg = US"dnsdb defer behaviour syntax error";
175 while (isspace(*keystring)) keystring++;
178 /* If the keystring contains an = this must be preceded by a valid type name. */
180 if ((equals = Ustrchr(keystring, '=')) != NULL)
183 uschar *tend = equals;
185 while (tend > keystring && isspace(tend[-1])) tend--;
186 len = tend - keystring;
188 for (i = 0; i < sizeof(type_names)/sizeof(uschar *); i++)
190 if (len == Ustrlen(type_names[i]) &&
191 strncmpic(keystring, US type_names[i], len) == 0)
193 type = type_values[i];
198 if (i >= sizeof(type_names)/sizeof(uschar *))
200 *errmsg = US"unsupported DNS record type";
204 keystring = equals + 1;
205 while (isspace(*keystring)) keystring++;
208 /* Initialize the resolver in case this is the first time it has been used. */
210 dns_init(FALSE, FALSE);
212 /* The remainder of the string must be a list of domains. As long as the lookup
213 for at least one of them succeeds, we return success. Failure means that none
216 The original implementation did not support a list of domains. Adding the list
217 feature is compatible, except in one case: when PTR records are being looked up
218 for a single IPv6 address. Fortunately, we can hack in a compatibility feature
219 here: If the type is PTR and no list separator is specified, and the entire
220 remaining string is valid as an IP address, set an impossible separator so that
221 it is treated as one item. */
223 if (type == T_PTR && keystring[0] != '<' &&
224 string_is_ip_address(keystring, NULL) != 0)
227 /* Now scan the list and do a lookup for each item */
229 while ((domain = string_nextinlist(&keystring, &sep, buffer, sizeof(buffer)))
233 int searchtype = (type == T_CSA)? T_SRV : /* record type we want */
234 (type == T_MXH)? T_MX :
235 (type == T_ZNS)? T_NS : type;
237 /* If the type is PTR or CSA, we have to construct the relevant magic lookup
238 key if the original is an IP address (some experimental protocols are using
239 PTR records for different purposes where the key string is a host name, and
240 Exim's extended CSA can be keyed by domains or IP addresses). This code for
241 doing the reversal is now in a separate function. */
243 if ((type == T_PTR || type == T_CSA) &&
244 string_is_ip_address(domain, NULL) != 0)
246 dns_build_reverse(domain, rbuffer);
250 DEBUG(D_lookup) debug_printf("dnsdb key: %s\n", domain);
252 /* Do the lookup and sort out the result. There are three special types that
253 are handled specially: T_CSA, T_ZNS and T_MXH. The former two are handled in
254 a special lookup function so that the facility could be used from other
255 parts of the Exim code. The latter affects only what happens later on in
256 this function, but for tidiness it is handled in a similar way. If the
257 lookup fails, continue with the next domain. In the case of DEFER, adjust
258 the final "nothing found" result, but carry on to the next domain. */
261 rc = dns_special_lookup(&dnsa, domain, type, &found);
263 if (rc == DNS_NOMATCH || rc == DNS_NODATA) continue;
264 if (rc != DNS_SUCCEED)
266 if (defer_mode == DEFER) return DEFER; /* always defer */
267 else if (defer_mode == PASS) failrc = DEFER; /* defer only if all do */
268 continue; /* treat defer as fail */
271 /* Search the returned records */
273 for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
275 rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
277 if (rr->type != searchtype) continue;
279 /* There may be several addresses from an A6 record. Put the configured
280 separator between them, just as for between several records. However, A6
281 support is not normally configured these days. */
290 for (da = dns_address_from_rr(&dnsa, rr); da != NULL; da = da->next)
292 if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
293 yield = string_cat(yield, &size, &ptr, da->address,
294 Ustrlen(da->address));
299 /* Other kinds of record just have one piece of data each, but there may be
300 several of them, of course. */
302 if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
306 yield = string_cat(yield, &size, &ptr, (uschar *)(rr->data+1),
309 else /* T_CNAME, T_CSA, T_MX, T_MXH, T_NS, T_PTR, T_SRV */
311 int priority, weight, port;
313 uschar *p = (uschar *)(rr->data);
317 /* mxh ignores the priority number and includes only the hostnames */
318 GETSHORT(priority, p);
320 else if (type == T_MX)
322 GETSHORT(priority, p);
323 sprintf(CS s, "%d ", priority);
324 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
326 else if (type == T_SRV)
328 GETSHORT(priority, p);
331 sprintf(CS s, "%d %d %d ", priority, weight, port);
332 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
334 else if (type == T_CSA)
336 /* See acl_verify_csa() for more comments about CSA. */
338 GETSHORT(priority, p);
342 if (priority != 1) continue; /* CSA version must be 1 */
344 /* If the CSA record we found is not the one we asked for, analyse
345 the subdomain assertions in the port field, else analyse the direct
346 authorization status in the weight field. */
350 if (port & 1) *s = 'X'; /* explicit authorization required */
351 else *s = '?'; /* no subdomain assertions here */
355 if (weight < 2) *s = 'N'; /* not authorized */
356 else if (weight == 2) *s = 'Y'; /* authorized */
357 else if (weight == 3) *s = '?'; /* unauthorizable */
358 else continue; /* invalid */
362 yield = string_cat(yield, &size, &ptr, s, 2);
365 /* GETSHORT() has advanced the pointer to the target domain. */
367 rc = dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, p,
368 (DN_EXPAND_ARG4_TYPE)(s), sizeof(s));
370 /* If an overlong response was received, the data will have been
371 truncated and dn_expand may fail. */
375 log_write(0, LOG_MAIN, "host name alias list truncated: type=%s "
376 "domain=%s", dns_text_type(type), domain);
379 else yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
381 } /* Loop for list of returned records */
382 } /* Loop for list of domains */
384 /* Reclaim unused memory */
386 store_reset(yield + ptr + 1);
388 /* If ptr == 0 we have not found anything. Otherwise, insert the terminating
389 zero and return the result. */
391 if (ptr == 0) return failrc;
397 /* End of lookups/dnsdb.c */