Remove obsolete $Cambridge$ CVS revision strings.
[exim.git] / src / src / lookups / dnsdb.c
CommitLineData
0756eb3c
PH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
0a49a7a4 5/* Copyright (c) University of Cambridge 1995 - 2009 */
0756eb3c
PH
6/* See the file NOTICE for conditions of use and distribution. */
7
8#include "../exim.h"
9#include "lf_functions.h"
0756eb3c
PH
10
11
12
13/* Ancient systems (e.g. SunOS4) don't appear to have T_TXT defined in their
14header files. */
15
16#ifndef T_TXT
17#define T_TXT 16
18#endif
19
20/* Table of recognized DNS record types and their integer values. */
21
1ba28e2b 22static const char *type_names[] = {
0756eb3c
PH
23 "a",
24#if HAVE_IPV6
25 "aaaa",
26 #ifdef SUPPORT_A6
27 "a6",
28 #endif
29#endif
30 "cname",
e5a9dba6 31 "csa",
0756eb3c 32 "mx",
ea3bc19b 33 "mxh",
0756eb3c
PH
34 "ns",
35 "ptr",
36 "srv",
33397d19 37 "txt",
8e669ac1 38 "zns"
33397d19 39};
0756eb3c
PH
40
41static int type_values[] = {
42 T_A,
43#if HAVE_IPV6
44 T_AAAA,
45 #ifdef SUPPORT_A6
46 T_A6,
47 #endif
48#endif
49 T_CNAME,
e5a9dba6 50 T_CSA, /* Private type for "Client SMTP Authorization". */
0756eb3c 51 T_MX,
ea3bc19b 52 T_MXH, /* Private type for "MX hostnames" */
0756eb3c
PH
53 T_NS,
54 T_PTR,
55 T_SRV,
33397d19
PH
56 T_TXT,
57 T_ZNS /* Private type for "zone nameservers" */
58};
0756eb3c
PH
59
60
61/*************************************************
62* Open entry point *
63*************************************************/
64
65/* See local README for interface description. */
66
e6d225ae 67static void *
0756eb3c
PH
68dnsdb_open(uschar *filename, uschar **errmsg)
69{
70filename = filename; /* Keep picky compilers happy */
71errmsg = errmsg; /* Ditto */
72return (void *)(-1); /* Any non-0 value */
73}
74
75
76
77/*************************************************
78* Find entry point for dnsdb *
79*************************************************/
80
7bb56e1f
PH
81/* See local README for interface description. The query in the "keystring" may
82consist of a number of parts.
83
8e669ac1
PH
84(a) If the first significant character is '>' then the next character is the
85separator character that is used when multiple records are found. The default
7bb56e1f
PH
86separator is newline.
87
0d0c6357
NM
88(b) If the next character is ',' then the next character is the separator
89character used for multiple items of text in "TXT" records. Alternatively,
90if the next character is ';' then these multiple items are concatenated with
91no separator. With neither of these options specified, only the first item
92is output.
93
94(c) If the next sequence of characters is 'defer_FOO' followed by a comma,
ff4dbb19
PH
95the defer behaviour is set to FOO. The possible behaviours are: 'strict', where
96any defer causes the whole lookup to defer; 'lax', where a defer causes the
97whole lookup to defer only if none of the DNS queries succeeds; and 'never',
98where all defers are as if the lookup failed. The default is 'lax'.
99
0d0c6357 100(d) If the next sequence of characters is a sequence of letters and digits
8e669ac1 101followed by '=', it is interpreted as the name of the DNS record type. The
ff4dbb19 102default is "TXT".
7bb56e1f 103
0d0c6357 104(e) Then there follows list of domain names. This is a generalized Exim list,
8e669ac1 105which may start with '<' in order to set a specific separator. The default
7bb56e1f 106separator, as always, is colon. */
0756eb3c 107
e6d225ae 108static int
0756eb3c
PH
109dnsdb_find(void *handle, uschar *filename, uschar *keystring, int length,
110 uschar **result, uschar **errmsg, BOOL *do_cache)
111{
112int rc;
113int size = 256;
114int ptr = 0;
7bb56e1f 115int sep = 0;
ff4dbb19 116int defer_mode = PASS;
0756eb3c 117int type = T_TXT;
c38d6da9 118int failrc = FAIL;
7bb56e1f 119uschar *outsep = US"\n";
0d0c6357 120uschar *outsep2 = NULL;
e5a9dba6 121uschar *equals, *domain, *found;
0756eb3c
PH
122uschar buffer[256];
123
124/* Because we're the working in the search pool, we try to reclaim as much
125store as possible later, so we preallocate the result here */
126
127uschar *yield = store_get(size);
128
129dns_record *rr;
130dns_answer dnsa;
131dns_scan dnss;
132
133handle = handle; /* Keep picky compilers happy */
134filename = filename;
135length = length;
136do_cache = do_cache;
137
0d0c6357
NM
138/* If the string starts with '>' we change the output separator.
139If it's followed by ';' or ',' we set the TXT output separator. */
0756eb3c 140
7bb56e1f
PH
141while (isspace(*keystring)) keystring++;
142if (*keystring == '>')
0756eb3c 143 {
7bb56e1f 144 outsep = keystring + 1;
8e669ac1 145 keystring += 2;
0d0c6357
NM
146 if (*keystring == ',')
147 {
148 outsep2 = keystring + 1;
149 keystring += 2;
150 }
151 else if (*keystring == ';')
152 {
153 outsep2 = US"";
154 keystring++;
155 }
7bb56e1f 156 while (isspace(*keystring)) keystring++;
8e669ac1 157 }
7bb56e1f 158
ff4dbb19
PH
159/* Check for a defer behaviour keyword. */
160
161if (strncmpic(keystring, US"defer_", 6) == 0)
162 {
163 keystring += 6;
164 if (strncmpic(keystring, US"strict", 6) == 0)
165 {
166 defer_mode = DEFER;
167 keystring += 6;
168 }
169 else if (strncmpic(keystring, US"lax", 3) == 0)
170 {
171 defer_mode = PASS;
172 keystring += 3;
173 }
174 else if (strncmpic(keystring, US"never", 5) == 0)
175 {
176 defer_mode = OK;
177 keystring += 5;
178 }
179 else
180 {
181 *errmsg = US"unsupported dnsdb defer behaviour";
182 return DEFER;
183 }
184 while (isspace(*keystring)) keystring++;
185 if (*keystring++ != ',')
186 {
187 *errmsg = US"dnsdb defer behaviour syntax error";
188 return DEFER;
189 }
190 while (isspace(*keystring)) keystring++;
191 }
192
7bb56e1f
PH
193/* If the keystring contains an = this must be preceded by a valid type name. */
194
195if ((equals = Ustrchr(keystring, '=')) != NULL)
196 {
197 int i, len;
198 uschar *tend = equals;
8e669ac1
PH
199
200 while (tend > keystring && isspace(tend[-1])) tend--;
201 len = tend - keystring;
202
0756eb3c
PH
203 for (i = 0; i < sizeof(type_names)/sizeof(uschar *); i++)
204 {
205 if (len == Ustrlen(type_names[i]) &&
206 strncmpic(keystring, US type_names[i], len) == 0)
207 {
208 type = type_values[i];
209 break;
210 }
211 }
8e669ac1 212
0756eb3c
PH
213 if (i >= sizeof(type_names)/sizeof(uschar *))
214 {
215 *errmsg = US"unsupported DNS record type";
216 return DEFER;
217 }
8e669ac1 218
7bb56e1f
PH
219 keystring = equals + 1;
220 while (isspace(*keystring)) keystring++;
0756eb3c 221 }
8e669ac1 222
7bb56e1f 223/* Initialize the resolver in case this is the first time it has been used. */
0756eb3c
PH
224
225dns_init(FALSE, FALSE);
0756eb3c 226
8e669ac1
PH
227/* The remainder of the string must be a list of domains. As long as the lookup
228for at least one of them succeeds, we return success. Failure means that none
229of them were found.
0756eb3c 230
8e669ac1
PH
231The original implementation did not support a list of domains. Adding the list
232feature is compatible, except in one case: when PTR records are being looked up
233for a single IPv6 address. Fortunately, we can hack in a compatibility feature
234here: If the type is PTR and no list separator is specified, and the entire
235remaining string is valid as an IP address, set an impossible separator so that
7bb56e1f 236it is treated as one item. */
33397d19 237
7bb56e1f 238if (type == T_PTR && keystring[0] != '<' &&
7e66e54d 239 string_is_ip_address(keystring, NULL) != 0)
7bb56e1f 240 sep = -1;
0756eb3c 241
7bb56e1f 242/* Now scan the list and do a lookup for each item */
0756eb3c 243
8e669ac1 244while ((domain = string_nextinlist(&keystring, &sep, buffer, sizeof(buffer)))
7bb56e1f 245 != NULL)
8e669ac1 246 {
7bb56e1f 247 uschar rbuffer[256];
e5a9dba6
PH
248 int searchtype = (type == T_CSA)? T_SRV : /* record type we want */
249 (type == T_MXH)? T_MX :
250 (type == T_ZNS)? T_NS : type;
251
252 /* If the type is PTR or CSA, we have to construct the relevant magic lookup
253 key if the original is an IP address (some experimental protocols are using
254 PTR records for different purposes where the key string is a host name, and
255 Exim's extended CSA can be keyed by domains or IP addresses). This code for
256 doing the reversal is now in a separate function. */
257
258 if ((type == T_PTR || type == T_CSA) &&
7e66e54d 259 string_is_ip_address(domain, NULL) != 0)
0756eb3c 260 {
7bb56e1f
PH
261 dns_build_reverse(domain, rbuffer);
262 domain = rbuffer;
0756eb3c 263 }
8e669ac1 264
7bb56e1f 265 DEBUG(D_lookup) debug_printf("dnsdb key: %s\n", domain);
8e669ac1 266
28e6ef29
TF
267 /* Do the lookup and sort out the result. There are three special types that
268 are handled specially: T_CSA, T_ZNS and T_MXH. The former two are handled in
269 a special lookup function so that the facility could be used from other
270 parts of the Exim code. The latter affects only what happens later on in
271 this function, but for tidiness it is handled in a similar way. If the
272 lookup fails, continue with the next domain. In the case of DEFER, adjust
273 the final "nothing found" result, but carry on to the next domain. */
8e669ac1 274
e5a9dba6
PH
275 found = domain;
276 rc = dns_special_lookup(&dnsa, domain, type, &found);
8e669ac1 277
7bb56e1f 278 if (rc == DNS_NOMATCH || rc == DNS_NODATA) continue;
c38d6da9
PH
279 if (rc != DNS_SUCCEED)
280 {
ff4dbb19
PH
281 if (defer_mode == DEFER) return DEFER; /* always defer */
282 else if (defer_mode == PASS) failrc = DEFER; /* defer only if all do */
283 continue; /* treat defer as fail */
c38d6da9 284 }
8e669ac1 285
ea3bc19b
PH
286 /* Search the returned records */
287
7bb56e1f
PH
288 for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
289 rr != NULL;
290 rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
0756eb3c 291 {
ea3bc19b 292 if (rr->type != searchtype) continue;
8e669ac1
PH
293
294 /* There may be several addresses from an A6 record. Put the configured
295 separator between them, just as for between several records. However, A6
7bb56e1f 296 support is not normally configured these days. */
8e669ac1 297
7bb56e1f
PH
298 if (type == T_A ||
299 #ifdef SUPPORT_A6
300 type == T_A6 ||
301 #endif
302 type == T_AAAA)
0756eb3c 303 {
7bb56e1f
PH
304 dns_address *da;
305 for (da = dns_address_from_rr(&dnsa, rr); da != NULL; da = da->next)
306 {
307 if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
8e669ac1 308 yield = string_cat(yield, &size, &ptr, da->address,
7bb56e1f
PH
309 Ustrlen(da->address));
310 }
311 continue;
0756eb3c 312 }
8e669ac1
PH
313
314 /* Other kinds of record just have one piece of data each, but there may be
7bb56e1f 315 several of them, of course. */
8e669ac1 316
7bb56e1f 317 if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
8e669ac1 318
7bb56e1f 319 if (type == T_TXT)
0756eb3c 320 {
0d0c6357
NM
321 if (outsep2 == NULL)
322 {
323 /* output only the first item of data */
324 yield = string_cat(yield, &size, &ptr, (uschar *)(rr->data+1),
325 (rr->data)[0]);
326 }
327 else
80a47a2c 328 {
0d0c6357
NM
329 /* output all items */
330 int data_offset = 0;
331 while (data_offset < rr->size)
332 {
333 uschar chunk_len = (rr->data)[data_offset++];
334 if (outsep2[0] != '\0' && data_offset != 1)
335 yield = string_cat(yield, &size, &ptr, outsep2, 1);
336 yield = string_cat(yield, &size, &ptr,
337 (uschar *)((rr->data)+data_offset), chunk_len);
338 data_offset += chunk_len;
339 }
80a47a2c 340 }
0756eb3c 341 }
e5a9dba6 342 else /* T_CNAME, T_CSA, T_MX, T_MXH, T_NS, T_PTR, T_SRV */
0756eb3c 343 {
e5a9dba6 344 int priority, weight, port;
7bb56e1f
PH
345 uschar s[264];
346 uschar *p = (uschar *)(rr->data);
8e669ac1 347
ea3bc19b
PH
348 if (type == T_MXH)
349 {
350 /* mxh ignores the priority number and includes only the hostnames */
e5a9dba6 351 GETSHORT(priority, p);
ea3bc19b
PH
352 }
353 else if (type == T_MX)
7bb56e1f 354 {
e5a9dba6
PH
355 GETSHORT(priority, p);
356 sprintf(CS s, "%d ", priority);
7bb56e1f
PH
357 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
358 }
359 else if (type == T_SRV)
360 {
e5a9dba6 361 GETSHORT(priority, p);
7bb56e1f
PH
362 GETSHORT(weight, p);
363 GETSHORT(port, p);
e5a9dba6 364 sprintf(CS s, "%d %d %d ", priority, weight, port);
7bb56e1f
PH
365 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
366 }
e5a9dba6
PH
367 else if (type == T_CSA)
368 {
369 /* See acl_verify_csa() for more comments about CSA. */
370
371 GETSHORT(priority, p);
372 GETSHORT(weight, p);
373 GETSHORT(port, p);
374
375 if (priority != 1) continue; /* CSA version must be 1 */
376
377 /* If the CSA record we found is not the one we asked for, analyse
378 the subdomain assertions in the port field, else analyse the direct
379 authorization status in the weight field. */
380
381 if (found != domain)
382 {
383 if (port & 1) *s = 'X'; /* explicit authorization required */
384 else *s = '?'; /* no subdomain assertions here */
385 }
386 else
387 {
388 if (weight < 2) *s = 'N'; /* not authorized */
389 else if (weight == 2) *s = 'Y'; /* authorized */
390 else if (weight == 3) *s = '?'; /* unauthorizable */
391 else continue; /* invalid */
392 }
393
394 s[1] = ' ';
395 yield = string_cat(yield, &size, &ptr, s, 2);
396 }
397
398 /* GETSHORT() has advanced the pointer to the target domain. */
8e669ac1 399
7bb56e1f
PH
400 rc = dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, p,
401 (DN_EXPAND_ARG4_TYPE)(s), sizeof(s));
8e669ac1 402
7bb56e1f
PH
403 /* If an overlong response was received, the data will have been
404 truncated and dn_expand may fail. */
8e669ac1 405
7bb56e1f
PH
406 if (rc < 0)
407 {
408 log_write(0, LOG_MAIN, "host name alias list truncated: type=%s "
409 "domain=%s", dns_text_type(type), domain);
410 break;
411 }
412 else yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
0756eb3c 413 }
7bb56e1f
PH
414 } /* Loop for list of returned records */
415 } /* Loop for list of domains */
416
417/* Reclaim unused memory */
0756eb3c 418
7bb56e1f
PH
419store_reset(yield + ptr + 1);
420
8e669ac1 421/* If ptr == 0 we have not found anything. Otherwise, insert the terminating
7bb56e1f
PH
422zero and return the result. */
423
c38d6da9 424if (ptr == 0) return failrc;
0756eb3c 425yield[ptr] = 0;
0756eb3c 426*result = yield;
0756eb3c
PH
427return OK;
428}
429
6545de78
PP
430
431
432/*************************************************
433* Version reporting entry point *
434*************************************************/
435
436/* See local README for interface description. */
437
438#include "../version.h"
439
440void
441dnsdb_version_report(FILE *f)
442{
443#ifdef DYNLOOKUP
444fprintf(f, "Library version: DNSDB: Exim version %s\n", EXIM_VERSION_STR);
445#endif
446}
447
448
e6d225ae
DW
449static lookup_info _lookup_info = {
450 US"dnsdb", /* lookup name */
451 lookup_querystyle, /* query style */
452 dnsdb_open, /* open function */
453 NULL, /* check function */
454 dnsdb_find, /* find function */
455 NULL, /* no close function */
456 NULL, /* no tidy function */
6545de78
PP
457 NULL, /* no quoting function */
458 dnsdb_version_report /* version reporting */
e6d225ae
DW
459};
460
461#ifdef DYNLOOKUP
462#define dnsdb_lookup_module_info _lookup_module_info
463#endif
464
465static lookup_info *_lookup_list[] = { &_lookup_info };
466lookup_module_info dnsdb_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
467
0756eb3c 468/* End of lookups/dnsdb.c */