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