1 /* $Cambridge: exim/src/src/lookups/dnsdb.c,v 1.7 2004/12/21 12:21:46 ph10 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2004 */
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
[] = {
43 static int type_values
[] = {
53 T_MXH
, /* Private type for "MX hostnames" */
58 T_ZNS
/* Private type for "zone nameservers" */
62 /*************************************************
64 *************************************************/
66 /* See local README for interface description. */
69 dnsdb_open(uschar
*filename
, uschar
**errmsg
)
71 filename
= filename
; /* Keep picky compilers happy */
72 errmsg
= errmsg
; /* Ditto */
73 return (void *)(-1); /* Any non-0 value */
78 /*************************************************
79 * Find entry point for dnsdb *
80 *************************************************/
82 /* See local README for interface description. The query in the "keystring" may
83 consist of a number of parts.
85 (a) If the first significant character is '>' then the next character is the
86 separator character that is used when multiple records are found. The default
89 (b) If the next sequence of characters is 'defer_FOO' followed by a comma,
90 the defer behaviour is set to FOO. The possible behaviours are: 'strict', where
91 any defer causes the whole lookup to defer; 'lax', where a defer causes the
92 whole lookup to defer only if none of the DNS queries succeeds; and 'never',
93 where all defers are as if the lookup failed. The default is 'lax'.
95 (c) If the next sequence of characters is a sequence of letters and digits
96 followed by '=', it is interpreted as the name of the DNS record type. The
99 (d) Then there follows list of domain names. This is a generalized Exim list,
100 which may start with '<' in order to set a specific separator. The default
101 separator, as always, is colon. */
104 dnsdb_find(void *handle
, uschar
*filename
, uschar
*keystring
, int length
,
105 uschar
**result
, uschar
**errmsg
, BOOL
*do_cache
)
111 int defer_mode
= PASS
;
114 uschar
*outsep
= US
"\n";
115 uschar
*equals
, *domain
;
118 /* Because we're the working in the search pool, we try to reclaim as much
119 store as possible later, so we preallocate the result here */
121 uschar
*yield
= store_get(size
);
127 handle
= handle
; /* Keep picky compilers happy */
132 /* If the string starts with '>' we change the output separator */
134 while (isspace(*keystring
)) keystring
++;
135 if (*keystring
== '>')
137 outsep
= keystring
+ 1;
139 while (isspace(*keystring
)) keystring
++;
142 /* Check for a defer behaviour keyword. */
144 if (strncmpic(keystring
, US
"defer_", 6) == 0)
147 if (strncmpic(keystring
, US
"strict", 6) == 0)
152 else if (strncmpic(keystring
, US
"lax", 3) == 0)
157 else if (strncmpic(keystring
, US
"never", 5) == 0)
164 *errmsg
= US
"unsupported dnsdb defer behaviour";
167 while (isspace(*keystring
)) keystring
++;
168 if (*keystring
++ != ',')
170 *errmsg
= US
"dnsdb defer behaviour syntax error";
173 while (isspace(*keystring
)) keystring
++;
176 /* If the keystring contains an = this must be preceded by a valid type name. */
178 if ((equals
= Ustrchr(keystring
, '=')) != NULL
)
181 uschar
*tend
= equals
;
183 while (tend
> keystring
&& isspace(tend
[-1])) tend
--;
184 len
= tend
- keystring
;
186 for (i
= 0; i
< sizeof(type_names
)/sizeof(uschar
*); i
++)
188 if (len
== Ustrlen(type_names
[i
]) &&
189 strncmpic(keystring
, US type_names
[i
], len
) == 0)
191 type
= type_values
[i
];
196 if (i
>= sizeof(type_names
)/sizeof(uschar
*))
198 *errmsg
= US
"unsupported DNS record type";
202 keystring
= equals
+ 1;
203 while (isspace(*keystring
)) keystring
++;
206 /* Initialize the resolver in case this is the first time it has been used. */
208 dns_init(FALSE
, FALSE
);
210 /* The remainder of the string must be a list of domains. As long as the lookup
211 for at least one of them succeeds, we return success. Failure means that none
214 The original implementation did not support a list of domains. Adding the list
215 feature is compatible, except in one case: when PTR records are being looked up
216 for a single IPv6 address. Fortunately, we can hack in a compatibility feature
217 here: If the type is PTR and no list separator is specified, and the entire
218 remaining string is valid as an IP address, set an impossible separator so that
219 it is treated as one item. */
221 if (type
== T_PTR
&& keystring
[0] != '<' &&
222 string_is_ip_address(keystring
, NULL
) > 0)
225 /* Now scan the list and do a lookup for each item */
227 while ((domain
= string_nextinlist(&keystring
, &sep
, buffer
, sizeof(buffer
)))
231 int searchtype
= (type
== T_ZNS
)? T_NS
: /* record type we want */
232 (type
== T_MXH
)? T_MX
: type
;
234 /* If the type is PTR, we have to construct the relevant magic lookup key if
235 the original is an IP address (some experimental protocols are using PTR
236 records for different purposes where the key string is a host name). This
237 code for doing the reversal is now in a separate function. */
239 if (type
== T_PTR
&& string_is_ip_address(domain
, NULL
))
241 dns_build_reverse(domain
, rbuffer
);
245 DEBUG(D_lookup
) debug_printf("dnsdb key: %s\n", domain
);
247 /* Do the lookup and sort out the result. There are two special types that
248 are handled specially: T_ZNS and T_MXH. The former is handled in a special
249 lookup function so that the facility could be used from other parts of the
250 Exim code. The latter affects only what happens later on in this function,
251 but for tidiness it is handled in a similar way. If the lookup fails,
252 continue with the next domain. In the case of DEFER, adjust the final
253 "nothing found" result, but carry on to the next domain. */
255 rc
= dns_special_lookup(&dnsa
, domain
, type
, NULL
);
257 if (rc
== DNS_NOMATCH
|| rc
== DNS_NODATA
) continue;
258 if (rc
!= DNS_SUCCEED
)
260 if (defer_mode
== DEFER
) return DEFER
; /* always defer */
261 else if (defer_mode
== PASS
) failrc
= DEFER
; /* defer only if all do */
262 continue; /* treat defer as fail */
265 /* Search the returned records */
267 for (rr
= dns_next_rr(&dnsa
, &dnss
, RESET_ANSWERS
);
269 rr
= dns_next_rr(&dnsa
, &dnss
, RESET_NEXT
))
271 if (rr
->type
!= searchtype
) continue;
273 /* There may be several addresses from an A6 record. Put the configured
274 separator between them, just as for between several records. However, A6
275 support is not normally configured these days. */
284 for (da
= dns_address_from_rr(&dnsa
, rr
); da
!= NULL
; da
= da
->next
)
286 if (ptr
!= 0) yield
= string_cat(yield
, &size
, &ptr
, outsep
, 1);
287 yield
= string_cat(yield
, &size
, &ptr
, da
->address
,
288 Ustrlen(da
->address
));
293 /* Other kinds of record just have one piece of data each, but there may be
294 several of them, of course. */
296 if (ptr
!= 0) yield
= string_cat(yield
, &size
, &ptr
, outsep
, 1);
300 yield
= string_cat(yield
, &size
, &ptr
, (uschar
*)(rr
->data
+1),
303 else /* T_CNAME, T_MX, T_MXH, T_NS, T_SRV, T_PTR */
307 uschar
*p
= (uschar
*)(rr
->data
);
311 /* mxh ignores the priority number and includes only the hostnames */
312 GETSHORT(num
, p
); /* pointer is advanced */
314 else if (type
== T_MX
)
316 GETSHORT(num
, p
); /* pointer is advanced */
317 sprintf(CS s
, "%d ", num
);
318 yield
= string_cat(yield
, &size
, &ptr
, s
, Ustrlen(s
));
320 else if (type
== T_SRV
)
323 GETSHORT(num
, p
); /* pointer is advanced */
326 sprintf(CS s
, "%d %d %d ", num
, weight
, port
);
327 yield
= string_cat(yield
, &size
, &ptr
, s
, Ustrlen(s
));
330 rc
= dn_expand(dnsa
.answer
, dnsa
.answer
+ dnsa
.answerlen
, p
,
331 (DN_EXPAND_ARG4_TYPE
)(s
), sizeof(s
));
333 /* If an overlong response was received, the data will have been
334 truncated and dn_expand may fail. */
338 log_write(0, LOG_MAIN
, "host name alias list truncated: type=%s "
339 "domain=%s", dns_text_type(type
), domain
);
342 else yield
= string_cat(yield
, &size
, &ptr
, s
, Ustrlen(s
));
344 } /* Loop for list of returned records */
345 } /* Loop for list of domains */
347 /* Reclaim unused memory */
349 store_reset(yield
+ ptr
+ 1);
351 /* If ptr == 0 we have not found anything. Otherwise, insert the terminating
352 zero and return the result. */
354 if (ptr
== 0) return failrc
;
360 /* End of lookups/dnsdb.c */