Don't defer for lists of domains (in dnsdb and dnslists sublists) if any
[exim.git] / src / src / lookups / dnsdb.c
1 /* $Cambridge: exim/src/src/lookups/dnsdb.c,v 1.5 2004/11/25 14:31:28 ph10 Exp $ */
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
17 header 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
25 static 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",
35 "mxh",
36 "ns",
37 "ptr",
38 "srv",
39 "txt",
40 "zns"
41 };
42
43 static 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,
53 T_MXH, /* Private type for "MX hostnames" */
54 T_NS,
55 T_PTR,
56 T_SRV,
57 T_TXT,
58 T_ZNS /* Private type for "zone nameservers" */
59 };
60
61
62 /*************************************************
63 * Open entry point *
64 *************************************************/
65
66 /* See local README for interface description. */
67
68 void *
69 dnsdb_open(uschar *filename, uschar **errmsg)
70 {
71 filename = filename; /* Keep picky compilers happy */
72 errmsg = errmsg; /* Ditto */
73 return (void *)(-1); /* Any non-0 value */
74 }
75
76
77
78 /*************************************************
79 * Find entry point for dnsdb *
80 *************************************************/
81
82 /* See local README for interface description. The query in the "keystring" may
83 consist of a number of parts.
84
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
87 separator is newline.
88
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
91 default is "A".
92
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. */
96
97 int
98 dnsdb_find(void *handle, uschar *filename, uschar *keystring, int length,
99 uschar **result, uschar **errmsg, BOOL *do_cache)
100 {
101 int rc;
102 int size = 256;
103 int ptr = 0;
104 int sep = 0;
105 int type = T_TXT;
106 int failrc = FAIL;
107 uschar *outsep = US"\n";
108 uschar *equals, *domain;
109 uschar buffer[256];
110
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 */
113
114 uschar *yield = store_get(size);
115
116 dns_record *rr;
117 dns_answer dnsa;
118 dns_scan dnss;
119
120 handle = handle; /* Keep picky compilers happy */
121 filename = filename;
122 length = length;
123 do_cache = do_cache;
124
125 /* If the string starts with '>' we change the output separator */
126
127 while (isspace(*keystring)) keystring++;
128 if (*keystring == '>')
129 {
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
137 if ((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
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 }
154
155 if (i >= sizeof(type_names)/sizeof(uschar *))
156 {
157 *errmsg = US"unsupported DNS record type";
158 return DEFER;
159 }
160
161 keystring = equals + 1;
162 while (isspace(*keystring)) keystring++;
163 }
164
165 /* Initialize the resolver in case this is the first time it has been used. */
166
167 dns_init(FALSE, FALSE);
168
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
171 of them were found.
172
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. */
179
180 if (type == T_PTR && keystring[0] != '<' &&
181 string_is_ip_address(keystring, NULL) > 0)
182 sep = -1;
183
184 /* Now scan the list and do a lookup for each item */
185
186 while ((domain = string_nextinlist(&keystring, &sep, buffer, sizeof(buffer)))
187 != NULL)
188 {
189 uschar rbuffer[256];
190 int searchtype = (type == T_ZNS)? T_NS : /* record type we want */
191 (type == T_MXH)? T_MX : type;
192
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)
197 {
198 dns_build_reverse(domain, rbuffer);
199 domain = rbuffer;
200 }
201
202 DEBUG(D_lookup) debug_printf("dnsdb key: %s\n", domain);
203
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. */
211
212 rc = dns_special_lookup(&dnsa, domain, type, NULL);
213
214 if (rc == DNS_NOMATCH || rc == DNS_NODATA) continue;
215 if (rc != DNS_SUCCEED)
216 {
217 failrc = DEFER;
218 continue;
219 }
220
221 /* Search the returned records */
222
223 for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
224 rr != NULL;
225 rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
226 {
227 if (rr->type != searchtype) continue;
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)
238 {
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;
247 }
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)
255 {
256 yield = string_cat(yield, &size, &ptr, (uschar *)(rr->data+1),
257 (rr->data)[0]);
258 }
259 else /* T_CNAME, T_MX, T_MXH, T_NS, T_SRV, T_PTR */
260 {
261 int num;
262 uschar s[264];
263 uschar *p = (uschar *)(rr->data);
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)
271 {
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 {
278 int weight, port;
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 }
285
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));
299 }
300 } /* Loop for list of returned records */
301 } /* Loop for list of domains */
302
303 /* Reclaim unused memory */
304
305 store_reset(yield + ptr + 1);
306
307 /* If ptr == 0 we have not found anything. Otherwise, insert the terminating
308 zero and return the result. */
309
310 if (ptr == 0) return failrc;
311 yield[ptr] = 0;
312 *result = yield;
313 return OK;
314 }
315
316 /* End of lookups/dnsdb.c */