A forced expansion failure in the SPA authenticator is now treated the
[exim.git] / src / src / lookups / dnsdb.c
CommitLineData
c38d6da9 1/* $Cambridge: exim/src/src/lookups/dnsdb.c,v 1.5 2004/11/25 14:31:28 ph10 Exp $ */
0756eb3c
PH
2
3/*************************************************
4* Exim - an Internet mail transport agent *
5*************************************************/
6
7/* Copyright (c) University of Cambridge 1995 - 2004 */
8/* See the file NOTICE for conditions of use and distribution. */
9
10#include "../exim.h"
11#include "lf_functions.h"
12#include "dnsdb.h"
13
14
15
16/* Ancient systems (e.g. SunOS4) don't appear to have T_TXT defined in their
17header files. */
18
19#ifndef T_TXT
20#define T_TXT 16
21#endif
22
23/* Table of recognized DNS record types and their integer values. */
24
25static char *type_names[] = {
26 "a",
27#if HAVE_IPV6
28 "aaaa",
29 #ifdef SUPPORT_A6
30 "a6",
31 #endif
32#endif
33 "cname",
34 "mx",
ea3bc19b 35 "mxh",
0756eb3c
PH
36 "ns",
37 "ptr",
38 "srv",
33397d19
PH
39 "txt",
40 "zns"
41};
0756eb3c
PH
42
43static int type_values[] = {
44 T_A,
45#if HAVE_IPV6
46 T_AAAA,
47 #ifdef SUPPORT_A6
48 T_A6,
49 #endif
50#endif
51 T_CNAME,
52 T_MX,
ea3bc19b 53 T_MXH, /* Private type for "MX hostnames" */
0756eb3c
PH
54 T_NS,
55 T_PTR,
56 T_SRV,
33397d19
PH
57 T_TXT,
58 T_ZNS /* Private type for "zone nameservers" */
59};
0756eb3c
PH
60
61
62/*************************************************
63* Open entry point *
64*************************************************/
65
66/* See local README for interface description. */
67
68void *
69dnsdb_open(uschar *filename, uschar **errmsg)
70{
71filename = filename; /* Keep picky compilers happy */
72errmsg = errmsg; /* Ditto */
73return (void *)(-1); /* Any non-0 value */
74}
75
76
77
78/*************************************************
79* Find entry point for dnsdb *
80*************************************************/
81
7bb56e1f
PH
82/* See local README for interface description. The query in the "keystring" may
83consist of a number of parts.
84
85(a) If the first significant character is '>' then the next character is the
86separator character that is used when multiple records are found. The default
87separator is newline.
88
89(b) If the next sequence of characters is a sequence of letters and digits
90followed by '=', it is interpreted as the name of the DNS record type. The
91default is "A".
92
93(c) Then there follows list of domain names. This is a generalized Exim list,
94which may start with '<' in order to set a specific separator. The default
95separator, as always, is colon. */
0756eb3c
PH
96
97int
98dnsdb_find(void *handle, uschar *filename, uschar *keystring, int length,
99 uschar **result, uschar **errmsg, BOOL *do_cache)
100{
101int rc;
102int size = 256;
103int ptr = 0;
7bb56e1f 104int sep = 0;
0756eb3c 105int type = T_TXT;
c38d6da9 106int failrc = FAIL;
7bb56e1f
PH
107uschar *outsep = US"\n";
108uschar *equals, *domain;
0756eb3c
PH
109uschar buffer[256];
110
111/* Because we're the working in the search pool, we try to reclaim as much
112store as possible later, so we preallocate the result here */
113
114uschar *yield = store_get(size);
115
116dns_record *rr;
117dns_answer dnsa;
118dns_scan dnss;
119
120handle = handle; /* Keep picky compilers happy */
121filename = filename;
122length = length;
123do_cache = do_cache;
124
7bb56e1f 125/* If the string starts with '>' we change the output separator */
0756eb3c 126
7bb56e1f
PH
127while (isspace(*keystring)) keystring++;
128if (*keystring == '>')
0756eb3c 129 {
7bb56e1f
PH
130 outsep = keystring + 1;
131 keystring += 2;
132 while (isspace(*keystring)) keystring++;
133 }
134
135/* If the keystring contains an = this must be preceded by a valid type name. */
136
137if ((equals = Ustrchr(keystring, '=')) != NULL)
138 {
139 int i, len;
140 uschar *tend = equals;
141
142 while (tend > keystring && isspace(tend[-1])) tend--;
143 len = tend - keystring;
144
0756eb3c
PH
145 for (i = 0; i < sizeof(type_names)/sizeof(uschar *); i++)
146 {
147 if (len == Ustrlen(type_names[i]) &&
148 strncmpic(keystring, US type_names[i], len) == 0)
149 {
150 type = type_values[i];
151 break;
152 }
153 }
7bb56e1f 154
0756eb3c
PH
155 if (i >= sizeof(type_names)/sizeof(uschar *))
156 {
157 *errmsg = US"unsupported DNS record type";
158 return DEFER;
159 }
7bb56e1f
PH
160
161 keystring = equals + 1;
162 while (isspace(*keystring)) keystring++;
0756eb3c 163 }
7bb56e1f
PH
164
165/* Initialize the resolver in case this is the first time it has been used. */
0756eb3c
PH
166
167dns_init(FALSE, FALSE);
0756eb3c 168
7bb56e1f
PH
169/* The remainder of the string must be a list of domains. As long as the lookup
170for at least one of them succeeds, we return success. Failure means that none
171of them were found.
0756eb3c 172
7bb56e1f
PH
173The original implementation did not support a list of domains. Adding the list
174feature is compatible, except in one case: when PTR records are being looked up
175for a single IPv6 address. Fortunately, we can hack in a compatibility feature
176here: If the type is PTR and no list separator is specified, and the entire
177remaining string is valid as an IP address, set an impossible separator so that
178it is treated as one item. */
33397d19 179
7bb56e1f
PH
180if (type == T_PTR && keystring[0] != '<' &&
181 string_is_ip_address(keystring, NULL) > 0)
182 sep = -1;
0756eb3c 183
7bb56e1f 184/* Now scan the list and do a lookup for each item */
0756eb3c 185
7bb56e1f
PH
186while ((domain = string_nextinlist(&keystring, &sep, buffer, sizeof(buffer)))
187 != NULL)
188 {
189 uschar rbuffer[256];
ea3bc19b
PH
190 int searchtype = (type == T_ZNS)? T_NS : /* record type we want */
191 (type == T_MXH)? T_MX : type;
0756eb3c 192
7bb56e1f
PH
193 /* If the type is PTR, we have to construct the relevant magic lookup
194 key. This code is now in a separate function. */
195
196 if (type == T_PTR)
0756eb3c 197 {
7bb56e1f
PH
198 dns_build_reverse(domain, rbuffer);
199 domain = rbuffer;
0756eb3c 200 }
7bb56e1f
PH
201
202 DEBUG(D_lookup) debug_printf("dnsdb key: %s\n", domain);
203
ea3bc19b
PH
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,
c38d6da9
PH
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. */
7bb56e1f
PH
211
212 rc = dns_special_lookup(&dnsa, domain, type, NULL);
213
214 if (rc == DNS_NOMATCH || rc == DNS_NODATA) continue;
c38d6da9
PH
215 if (rc != DNS_SUCCEED)
216 {
217 failrc = DEFER;
218 continue;
219 }
7bb56e1f 220
ea3bc19b
PH
221 /* Search the returned records */
222
7bb56e1f
PH
223 for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
224 rr != NULL;
225 rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
0756eb3c 226 {
ea3bc19b 227 if (rr->type != searchtype) continue;
7bb56e1f
PH
228
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. */
232
233 if (type == T_A ||
234 #ifdef SUPPORT_A6
235 type == T_A6 ||
236 #endif
237 type == T_AAAA)
0756eb3c 238 {
7bb56e1f
PH
239 dns_address *da;
240 for (da = dns_address_from_rr(&dnsa, rr); da != NULL; da = da->next)
241 {
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));
245 }
246 continue;
0756eb3c 247 }
7bb56e1f
PH
248
249 /* Other kinds of record just have one piece of data each, but there may be
250 several of them, of course. */
251
252 if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
253
254 if (type == T_TXT)
0756eb3c 255 {
7bb56e1f
PH
256 yield = string_cat(yield, &size, &ptr, (uschar *)(rr->data+1),
257 (rr->data)[0]);
0756eb3c 258 }
ea3bc19b 259 else /* T_CNAME, T_MX, T_MXH, T_NS, T_SRV, T_PTR */
0756eb3c 260 {
ea3bc19b 261 int num;
7bb56e1f
PH
262 uschar s[264];
263 uschar *p = (uschar *)(rr->data);
ea3bc19b
PH
264
265 if (type == T_MXH)
266 {
267 /* mxh ignores the priority number and includes only the hostnames */
268 GETSHORT(num, p); /* pointer is advanced */
269 }
270 else if (type == T_MX)
7bb56e1f 271 {
7bb56e1f
PH
272 GETSHORT(num, p); /* pointer is advanced */
273 sprintf(CS s, "%d ", num);
274 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
275 }
276 else if (type == T_SRV)
277 {
ea3bc19b 278 int weight, port;
7bb56e1f
PH
279 GETSHORT(num, p); /* pointer is advanced */
280 GETSHORT(weight, p);
281 GETSHORT(port, p);
282 sprintf(CS s, "%d %d %d ", num, weight, port);
283 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
284 }
ea3bc19b 285
7bb56e1f
PH
286 rc = dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, p,
287 (DN_EXPAND_ARG4_TYPE)(s), sizeof(s));
288
289 /* If an overlong response was received, the data will have been
290 truncated and dn_expand may fail. */
291
292 if (rc < 0)
293 {
294 log_write(0, LOG_MAIN, "host name alias list truncated: type=%s "
295 "domain=%s", dns_text_type(type), domain);
296 break;
297 }
298 else yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
0756eb3c 299 }
7bb56e1f
PH
300 } /* Loop for list of returned records */
301 } /* Loop for list of domains */
302
303/* Reclaim unused memory */
0756eb3c 304
7bb56e1f
PH
305store_reset(yield + ptr + 1);
306
307/* If ptr == 0 we have not found anything. Otherwise, insert the terminating
308zero and return the result. */
309
c38d6da9 310if (ptr == 0) return failrc;
0756eb3c 311yield[ptr] = 0;
0756eb3c 312*result = yield;
0756eb3c
PH
313return OK;
314}
315
316/* End of lookups/dnsdb.c */