1 /* $Cambridge: exim/src/src/lookups/dnsdb.c,v 1.5 2004/11/25 14:31:28 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 a sequence of letters and digits
90 followed by '=', it is interpreted as the name of the DNS record type. The
93 (c) Then there follows list of domain names. This is a generalized Exim list,
94 which may start with '<' in order to set a specific separator. The default
95 separator, as always, is colon. */
98 dnsdb_find(void *handle, uschar *filename, uschar *keystring, int length,
99 uschar **result, uschar **errmsg, BOOL *do_cache)
107 uschar *outsep = US"\n";
108 uschar *equals, *domain;
111 /* Because we're the working in the search pool, we try to reclaim as much
112 store as possible later, so we preallocate the result here */
114 uschar *yield = store_get(size);
120 handle = handle; /* Keep picky compilers happy */
125 /* If the string starts with '>' we change the output separator */
127 while (isspace(*keystring)) keystring++;
128 if (*keystring == '>')
130 outsep = keystring + 1;
132 while (isspace(*keystring)) keystring++;
135 /* If the keystring contains an = this must be preceded by a valid type name. */
137 if ((equals = Ustrchr(keystring, '=')) != NULL)
140 uschar *tend = equals;
142 while (tend > keystring && isspace(tend[-1])) tend--;
143 len = tend - keystring;
145 for (i = 0; i < sizeof(type_names)/sizeof(uschar *); i++)
147 if (len == Ustrlen(type_names[i]) &&
148 strncmpic(keystring, US type_names[i], len) == 0)
150 type = type_values[i];
155 if (i >= sizeof(type_names)/sizeof(uschar *))
157 *errmsg = US"unsupported DNS record type";
161 keystring = equals + 1;
162 while (isspace(*keystring)) keystring++;
165 /* Initialize the resolver in case this is the first time it has been used. */
167 dns_init(FALSE, FALSE);
169 /* The remainder of the string must be a list of domains. As long as the lookup
170 for at least one of them succeeds, we return success. Failure means that none
173 The original implementation did not support a list of domains. Adding the list
174 feature is compatible, except in one case: when PTR records are being looked up
175 for a single IPv6 address. Fortunately, we can hack in a compatibility feature
176 here: If the type is PTR and no list separator is specified, and the entire
177 remaining string is valid as an IP address, set an impossible separator so that
178 it is treated as one item. */
180 if (type == T_PTR && keystring[0] != '<' &&
181 string_is_ip_address(keystring, NULL) > 0)
184 /* Now scan the list and do a lookup for each item */
186 while ((domain = string_nextinlist(&keystring, &sep, buffer, sizeof(buffer)))
190 int searchtype = (type == T_ZNS)? T_NS : /* record type we want */
191 (type == T_MXH)? T_MX : type;
193 /* If the type is PTR, we have to construct the relevant magic lookup
194 key. This code is now in a separate function. */
198 dns_build_reverse(domain, rbuffer);
202 DEBUG(D_lookup) debug_printf("dnsdb key: %s\n", domain);
204 /* Do the lookup and sort out the result. There are two special types that
205 are handled specially: T_ZNS and T_MXH. The former is handled in a special
206 lookup function so that the facility could be used from other parts of the
207 Exim code. The latter affects only what happens later on in this function,
208 but for tidiness it is handled in a similar way. If the lookup fails,
209 continue with the next domain. In the case of DEFER, adjust the final
210 "nothing found" result, but carry on to the next domain. */
212 rc = dns_special_lookup(&dnsa, domain, type, NULL);
214 if (rc == DNS_NOMATCH || rc == DNS_NODATA) continue;
215 if (rc != DNS_SUCCEED)
221 /* Search the returned records */
223 for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
225 rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
227 if (rr->type != searchtype) continue;
229 /* There may be several addresses from an A6 record. Put the configured
230 separator between them, just as for between several records. However, A6
231 support is not normally configured these days. */
240 for (da = dns_address_from_rr(&dnsa, rr); da != NULL; da = da->next)
242 if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
243 yield = string_cat(yield, &size, &ptr, da->address,
244 Ustrlen(da->address));
249 /* Other kinds of record just have one piece of data each, but there may be
250 several of them, of course. */
252 if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
256 yield = string_cat(yield, &size, &ptr, (uschar *)(rr->data+1),
259 else /* T_CNAME, T_MX, T_MXH, T_NS, T_SRV, T_PTR */
263 uschar *p = (uschar *)(rr->data);
267 /* mxh ignores the priority number and includes only the hostnames */
268 GETSHORT(num, p); /* pointer is advanced */
270 else if (type == T_MX)
272 GETSHORT(num, p); /* pointer is advanced */
273 sprintf(CS s, "%d ", num);
274 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
276 else if (type == T_SRV)
279 GETSHORT(num, p); /* pointer is advanced */
282 sprintf(CS s, "%d %d %d ", num, weight, port);
283 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
286 rc = dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, p,
287 (DN_EXPAND_ARG4_TYPE)(s), sizeof(s));
289 /* If an overlong response was received, the data will have been
290 truncated and dn_expand may fail. */
294 log_write(0, LOG_MAIN, "host name alias list truncated: type=%s "
295 "domain=%s", dns_text_type(type), domain);
298 else yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
300 } /* Loop for list of returned records */
301 } /* Loop for list of domains */
303 /* Reclaim unused memory */
305 store_reset(yield + ptr + 1);
307 /* If ptr == 0 we have not found anything. Otherwise, insert the terminating
308 zero and return the result. */
310 if (ptr == 0) return failrc;
316 /* End of lookups/dnsdb.c */