(1) Last-minute sieve patch (updates to latest spec).
[exim.git] / src / src / lookups / dnsdb.c
CommitLineData
8e669ac1 1/* $Cambridge: exim/src/src/lookups/dnsdb.c,v 1.10 2005/02/17 11:58:27 ph10 Exp $ */
0756eb3c
PH
2
3/*************************************************
4* Exim - an Internet mail transport agent *
5*************************************************/
6
c988f1f4 7/* Copyright (c) University of Cambridge 1995 - 2005 */
0756eb3c
PH
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 39 "txt",
8e669ac1 40 "zns"
33397d19 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
8e669ac1
PH
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
7bb56e1f
PH
87separator is newline.
88
ff4dbb19
PH
89(b) If the next sequence of characters is 'defer_FOO' followed by a comma,
90the defer behaviour is set to FOO. The possible behaviours are: 'strict', where
91any defer causes the whole lookup to defer; 'lax', where a defer causes the
92whole lookup to defer only if none of the DNS queries succeeds; and 'never',
93where all defers are as if the lookup failed. The default is 'lax'.
94
8e669ac1
PH
95(c) If the next sequence of characters is a sequence of letters and digits
96followed by '=', it is interpreted as the name of the DNS record type. The
ff4dbb19 97default is "TXT".
7bb56e1f 98
8e669ac1
PH
99(d) Then there follows list of domain names. This is a generalized Exim list,
100which may start with '<' in order to set a specific separator. The default
7bb56e1f 101separator, as always, is colon. */
0756eb3c
PH
102
103int
104dnsdb_find(void *handle, uschar *filename, uschar *keystring, int length,
105 uschar **result, uschar **errmsg, BOOL *do_cache)
106{
107int rc;
108int size = 256;
109int ptr = 0;
7bb56e1f 110int sep = 0;
ff4dbb19 111int defer_mode = PASS;
0756eb3c 112int type = T_TXT;
c38d6da9 113int failrc = FAIL;
7bb56e1f
PH
114uschar *outsep = US"\n";
115uschar *equals, *domain;
0756eb3c
PH
116uschar buffer[256];
117
118/* Because we're the working in the search pool, we try to reclaim as much
119store as possible later, so we preallocate the result here */
120
121uschar *yield = store_get(size);
122
123dns_record *rr;
124dns_answer dnsa;
125dns_scan dnss;
126
127handle = handle; /* Keep picky compilers happy */
128filename = filename;
129length = length;
130do_cache = do_cache;
131
7bb56e1f 132/* If the string starts with '>' we change the output separator */
0756eb3c 133
7bb56e1f
PH
134while (isspace(*keystring)) keystring++;
135if (*keystring == '>')
0756eb3c 136 {
7bb56e1f 137 outsep = keystring + 1;
8e669ac1 138 keystring += 2;
7bb56e1f 139 while (isspace(*keystring)) keystring++;
8e669ac1 140 }
7bb56e1f 141
ff4dbb19
PH
142/* Check for a defer behaviour keyword. */
143
144if (strncmpic(keystring, US"defer_", 6) == 0)
145 {
146 keystring += 6;
147 if (strncmpic(keystring, US"strict", 6) == 0)
148 {
149 defer_mode = DEFER;
150 keystring += 6;
151 }
152 else if (strncmpic(keystring, US"lax", 3) == 0)
153 {
154 defer_mode = PASS;
155 keystring += 3;
156 }
157 else if (strncmpic(keystring, US"never", 5) == 0)
158 {
159 defer_mode = OK;
160 keystring += 5;
161 }
162 else
163 {
164 *errmsg = US"unsupported dnsdb defer behaviour";
165 return DEFER;
166 }
167 while (isspace(*keystring)) keystring++;
168 if (*keystring++ != ',')
169 {
170 *errmsg = US"dnsdb defer behaviour syntax error";
171 return DEFER;
172 }
173 while (isspace(*keystring)) keystring++;
174 }
175
7bb56e1f
PH
176/* If the keystring contains an = this must be preceded by a valid type name. */
177
178if ((equals = Ustrchr(keystring, '=')) != NULL)
179 {
180 int i, len;
181 uschar *tend = equals;
8e669ac1
PH
182
183 while (tend > keystring && isspace(tend[-1])) tend--;
184 len = tend - keystring;
185
0756eb3c
PH
186 for (i = 0; i < sizeof(type_names)/sizeof(uschar *); i++)
187 {
188 if (len == Ustrlen(type_names[i]) &&
189 strncmpic(keystring, US type_names[i], len) == 0)
190 {
191 type = type_values[i];
192 break;
193 }
194 }
8e669ac1 195
0756eb3c
PH
196 if (i >= sizeof(type_names)/sizeof(uschar *))
197 {
198 *errmsg = US"unsupported DNS record type";
199 return DEFER;
200 }
8e669ac1 201
7bb56e1f
PH
202 keystring = equals + 1;
203 while (isspace(*keystring)) keystring++;
0756eb3c 204 }
8e669ac1 205
7bb56e1f 206/* Initialize the resolver in case this is the first time it has been used. */
0756eb3c
PH
207
208dns_init(FALSE, FALSE);
0756eb3c 209
8e669ac1
PH
210/* The remainder of the string must be a list of domains. As long as the lookup
211for at least one of them succeeds, we return success. Failure means that none
212of them were found.
0756eb3c 213
8e669ac1
PH
214The original implementation did not support a list of domains. Adding the list
215feature is compatible, except in one case: when PTR records are being looked up
216for a single IPv6 address. Fortunately, we can hack in a compatibility feature
217here: If the type is PTR and no list separator is specified, and the entire
218remaining string is valid as an IP address, set an impossible separator so that
7bb56e1f 219it is treated as one item. */
33397d19 220
7bb56e1f 221if (type == T_PTR && keystring[0] != '<' &&
8e669ac1 222 string_is_ip_address(keystring, NULL) > 0)
7bb56e1f 223 sep = -1;
0756eb3c 224
7bb56e1f 225/* Now scan the list and do a lookup for each item */
0756eb3c 226
8e669ac1 227while ((domain = string_nextinlist(&keystring, &sep, buffer, sizeof(buffer)))
7bb56e1f 228 != NULL)
8e669ac1 229 {
7bb56e1f 230 uschar rbuffer[256];
ea3bc19b 231 int searchtype = (type == T_ZNS)? T_NS : /* record type we want */
8e669ac1 232 (type == T_MXH)? T_MX : type;
0756eb3c 233
7102e136
PH
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. */
8e669ac1
PH
238
239 if (type == T_PTR && string_is_ip_address(domain, NULL) > 0)
0756eb3c 240 {
7bb56e1f
PH
241 dns_build_reverse(domain, rbuffer);
242 domain = rbuffer;
0756eb3c 243 }
8e669ac1 244
7bb56e1f 245 DEBUG(D_lookup) debug_printf("dnsdb key: %s\n", domain);
8e669ac1
PH
246
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
ea3bc19b
PH
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,
8e669ac1 252 continue with the next domain. In the case of DEFER, adjust the final
c38d6da9 253 "nothing found" result, but carry on to the next domain. */
8e669ac1 254
7bb56e1f 255 rc = dns_special_lookup(&dnsa, domain, type, NULL);
8e669ac1 256
7bb56e1f 257 if (rc == DNS_NOMATCH || rc == DNS_NODATA) continue;
c38d6da9
PH
258 if (rc != DNS_SUCCEED)
259 {
ff4dbb19
PH
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 */
c38d6da9 263 }
8e669ac1 264
ea3bc19b
PH
265 /* Search the returned records */
266
7bb56e1f
PH
267 for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
268 rr != NULL;
269 rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
0756eb3c 270 {
ea3bc19b 271 if (rr->type != searchtype) continue;
8e669ac1
PH
272
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
7bb56e1f 275 support is not normally configured these days. */
8e669ac1 276
7bb56e1f
PH
277 if (type == T_A ||
278 #ifdef SUPPORT_A6
279 type == T_A6 ||
280 #endif
281 type == T_AAAA)
0756eb3c 282 {
7bb56e1f
PH
283 dns_address *da;
284 for (da = dns_address_from_rr(&dnsa, rr); da != NULL; da = da->next)
285 {
286 if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
8e669ac1 287 yield = string_cat(yield, &size, &ptr, da->address,
7bb56e1f
PH
288 Ustrlen(da->address));
289 }
290 continue;
0756eb3c 291 }
8e669ac1
PH
292
293 /* Other kinds of record just have one piece of data each, but there may be
7bb56e1f 294 several of them, of course. */
8e669ac1 295
7bb56e1f 296 if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
8e669ac1 297
7bb56e1f 298 if (type == T_TXT)
0756eb3c 299 {
7bb56e1f
PH
300 yield = string_cat(yield, &size, &ptr, (uschar *)(rr->data+1),
301 (rr->data)[0]);
0756eb3c 302 }
ea3bc19b 303 else /* T_CNAME, T_MX, T_MXH, T_NS, T_SRV, T_PTR */
0756eb3c 304 {
8e669ac1 305 int num;
7bb56e1f
PH
306 uschar s[264];
307 uschar *p = (uschar *)(rr->data);
8e669ac1 308
ea3bc19b
PH
309 if (type == T_MXH)
310 {
311 /* mxh ignores the priority number and includes only the hostnames */
312 GETSHORT(num, p); /* pointer is advanced */
313 }
314 else if (type == T_MX)
7bb56e1f 315 {
7bb56e1f
PH
316 GETSHORT(num, p); /* pointer is advanced */
317 sprintf(CS s, "%d ", num);
318 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
319 }
320 else if (type == T_SRV)
321 {
ea3bc19b 322 int weight, port;
7bb56e1f
PH
323 GETSHORT(num, p); /* pointer is advanced */
324 GETSHORT(weight, p);
325 GETSHORT(port, p);
326 sprintf(CS s, "%d %d %d ", num, weight, port);
327 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
328 }
8e669ac1 329
7bb56e1f
PH
330 rc = dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, p,
331 (DN_EXPAND_ARG4_TYPE)(s), sizeof(s));
8e669ac1 332
7bb56e1f
PH
333 /* If an overlong response was received, the data will have been
334 truncated and dn_expand may fail. */
8e669ac1 335
7bb56e1f
PH
336 if (rc < 0)
337 {
338 log_write(0, LOG_MAIN, "host name alias list truncated: type=%s "
339 "domain=%s", dns_text_type(type), domain);
340 break;
341 }
342 else yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
0756eb3c 343 }
7bb56e1f
PH
344 } /* Loop for list of returned records */
345 } /* Loop for list of domains */
346
347/* Reclaim unused memory */
0756eb3c 348
7bb56e1f
PH
349store_reset(yield + ptr + 1);
350
8e669ac1 351/* If ptr == 0 we have not found anything. Otherwise, insert the terminating
7bb56e1f
PH
352zero and return the result. */
353
c38d6da9 354if (ptr == 0) return failrc;
0756eb3c 355yield[ptr] = 0;
0756eb3c 356*result = yield;
0756eb3c
PH
357return OK;
358}
359
360/* End of lookups/dnsdb.c */