Tidying: coverity issues
[exim.git] / src / src / lookups / dnsdb.c
1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
4
5 /* Copyright (c) University of Cambridge 1995 - 2015 */
6 /* See the file NOTICE for conditions of use and distribution. */
7
8 #include "../exim.h"
9 #include "lf_functions.h"
10
11
12
13 /* Ancient systems (e.g. SunOS4) don't appear to have T_TXT defined in their
14 header files. */
15
16 #ifndef T_TXT
17 #define T_TXT 16
18 #endif
19
20 /* Many systems do not have T_SPF. */
21 #ifndef T_SPF
22 #define T_SPF 99
23 #endif
24
25 /* New TLSA record for DANE */
26 #ifndef T_TLSA
27 #define T_TLSA 52
28 #endif
29
30 /* Table of recognized DNS record types and their integer values. */
31
32 static const char *type_names[] = {
33 "a",
34 #if HAVE_IPV6
35 "a+",
36 "aaaa",
37 #endif
38 "cname",
39 "csa",
40 "mx",
41 "mxh",
42 "ns",
43 "ptr",
44 "soa",
45 "spf",
46 "srv",
47 "tlsa",
48 "txt",
49 "zns"
50 };
51
52 static int type_values[] = {
53 T_A,
54 #if HAVE_IPV6
55 T_ADDRESSES, /* Private type for AAAA + A */
56 T_AAAA,
57 #endif
58 T_CNAME,
59 T_CSA, /* Private type for "Client SMTP Authorization". */
60 T_MX,
61 T_MXH, /* Private type for "MX hostnames" */
62 T_NS,
63 T_PTR,
64 T_SOA,
65 T_SPF,
66 T_SRV,
67 T_TLSA,
68 T_TXT,
69 T_ZNS /* Private type for "zone nameservers" */
70 };
71
72
73 /*************************************************
74 * Open entry point *
75 *************************************************/
76
77 /* See local README for interface description. */
78
79 static void *
80 dnsdb_open(uschar *filename, uschar **errmsg)
81 {
82 filename = filename; /* Keep picky compilers happy */
83 errmsg = errmsg; /* Ditto */
84 return (void *)(-1); /* Any non-0 value */
85 }
86
87
88
89 /*************************************************
90 * Find entry point for dnsdb *
91 *************************************************/
92
93 /* See local README for interface description. The query in the "keystring" may
94 consist of a number of parts.
95
96 (a) If the first significant character is '>' then the next character is the
97 separator character that is used when multiple records are found. The default
98 separator is newline.
99
100 (b) If the next character is ',' then the next character is the separator
101 character used for multiple items of text in "TXT" records. Alternatively,
102 if the next character is ';' then these multiple items are concatenated with
103 no separator. With neither of these options specified, only the first item
104 is output. Similarly for "SPF" records, but the default for joining multiple
105 items in one SPF record is the empty string, for direct concatenation.
106
107 (c) Options, all comma-terminated, in any order. Any unrecognised option
108 terminates option processing. Recognised options are:
109
110 - 'defer_FOO': set the defer behaviour to FOO. The possible behaviours are:
111 'strict', where any defer causes the whole lookup to defer; 'lax', where a defer
112 causes the whole lookup to defer only if none of the DNS queries succeeds; and
113 'never', where all defers are as if the lookup failed. The default is 'lax'.
114
115 - 'dnssec_FOO', with 'strict', 'lax' and 'never' (default). The meanings are
116 require, try and don't-try dnssec respectively.
117
118 - 'retrans_VAL', set the timeout value. VAL is an Exim time specification
119 (eg "5s"). The default is set by the main configuration option 'dns_retrans'.
120
121 - 'retry_VAL', set the retry count on timeouts. VAL is an integer. The
122 default is set by the main configuration option "dns_retry".
123
124 (d) If the next sequence of characters is a sequence of letters and digits
125 followed by '=', it is interpreted as the name of the DNS record type. The
126 default is "TXT".
127
128 (e) Then there follows list of domain names. This is a generalized Exim list,
129 which may start with '<' in order to set a specific separator. The default
130 separator, as always, is colon. */
131
132 static int
133 dnsdb_find(void *handle, uschar *filename, const uschar *keystring, int length,
134 uschar **result, uschar **errmsg, uint *do_cache)
135 {
136 int rc;
137 int size = 256;
138 int ptr = 0;
139 int sep = 0;
140 int defer_mode = PASS;
141 int dnssec_mode = OK;
142 int save_retrans = dns_retrans;
143 int save_retry = dns_retry;
144 int type;
145 int failrc = FAIL;
146 const uschar *outsep = CUS"\n";
147 const uschar *outsep2 = NULL;
148 uschar *equals, *domain, *found;
149
150 /* Because we're the working in the search pool, we try to reclaim as much
151 store as possible later, so we preallocate the result here */
152
153 uschar *yield = store_get(size);
154
155 dns_record *rr;
156 dns_answer dnsa;
157 dns_scan dnss;
158
159 handle = handle; /* Keep picky compilers happy */
160 filename = filename;
161 length = length;
162 do_cache = do_cache;
163
164 /* If the string starts with '>' we change the output separator.
165 If it's followed by ';' or ',' we set the TXT output separator. */
166
167 while (isspace(*keystring)) keystring++;
168 if (*keystring == '>')
169 {
170 outsep = keystring + 1;
171 keystring += 2;
172 if (*keystring == ',')
173 {
174 outsep2 = keystring + 1;
175 keystring += 2;
176 }
177 else if (*keystring == ';')
178 {
179 outsep2 = US"";
180 keystring++;
181 }
182 while (isspace(*keystring)) keystring++;
183 }
184
185 /* Check for a modifier keyword. */
186
187 for (;;)
188 {
189 if (strncmpic(keystring, US"defer_", 6) == 0)
190 {
191 keystring += 6;
192 if (strncmpic(keystring, US"strict", 6) == 0)
193 { defer_mode = DEFER; keystring += 6; }
194 else if (strncmpic(keystring, US"lax", 3) == 0)
195 { defer_mode = PASS; keystring += 3; }
196 else if (strncmpic(keystring, US"never", 5) == 0)
197 { defer_mode = OK; keystring += 5; }
198 else
199 {
200 *errmsg = US"unsupported dnsdb defer behaviour";
201 return DEFER;
202 }
203 }
204 else if (strncmpic(keystring, US"dnssec_", 7) == 0)
205 {
206 keystring += 7;
207 if (strncmpic(keystring, US"strict", 6) == 0)
208 { dnssec_mode = DEFER; keystring += 6; }
209 else if (strncmpic(keystring, US"lax", 3) == 0)
210 { dnssec_mode = PASS; keystring += 3; }
211 else if (strncmpic(keystring, US"never", 5) == 0)
212 { dnssec_mode = OK; keystring += 5; }
213 else
214 {
215 *errmsg = US"unsupported dnsdb dnssec behaviour";
216 return DEFER;
217 }
218 }
219 else if (strncmpic(keystring, US"retrans_", 8) == 0)
220 {
221 int timeout_sec;
222 if ((timeout_sec = readconf_readtime(keystring += 8, ',', FALSE)) <= 0)
223 {
224 *errmsg = US"unsupported dnsdb timeout value";
225 return DEFER;
226 }
227 dns_retrans = timeout_sec;
228 while (*keystring != ',') keystring++;
229 }
230 else if (strncmpic(keystring, US"retry_", 6) == 0)
231 {
232 int retries;
233 if ((retries = (int)strtol(CCS keystring + 6, CSS &keystring, 0)) < 0)
234 {
235 *errmsg = US"unsupported dnsdb retry count";
236 return DEFER;
237 }
238 dns_retry = retries;
239 }
240 else
241 break;
242
243 while (isspace(*keystring)) keystring++;
244 if (*keystring++ != ',')
245 {
246 *errmsg = US"dnsdb modifier syntax error";
247 return DEFER;
248 }
249 while (isspace(*keystring)) keystring++;
250 }
251
252 /* Figure out the "type" value if it is not T_TXT.
253 If the keystring contains an = this must be preceded by a valid type name. */
254
255 type = T_TXT;
256 if ((equals = Ustrchr(keystring, '=')) != NULL)
257 {
258 int i, len;
259 uschar *tend = equals;
260
261 while (tend > keystring && isspace(tend[-1])) tend--;
262 len = tend - keystring;
263
264 for (i = 0; i < sizeof(type_names)/sizeof(uschar *); i++)
265 {
266 if (len == Ustrlen(type_names[i]) &&
267 strncmpic(keystring, US type_names[i], len) == 0)
268 {
269 type = type_values[i];
270 break;
271 }
272 }
273
274 if (i >= sizeof(type_names)/sizeof(uschar *))
275 {
276 *errmsg = US"unsupported DNS record type";
277 return DEFER;
278 }
279
280 keystring = equals + 1;
281 while (isspace(*keystring)) keystring++;
282 }
283
284 /* Initialize the resolver in case this is the first time it has been used. */
285
286 dns_init(FALSE, FALSE, dnssec_mode != OK);
287
288 /* The remainder of the string must be a list of domains. As long as the lookup
289 for at least one of them succeeds, we return success. Failure means that none
290 of them were found.
291
292 The original implementation did not support a list of domains. Adding the list
293 feature is compatible, except in one case: when PTR records are being looked up
294 for a single IPv6 address. Fortunately, we can hack in a compatibility feature
295 here: If the type is PTR and no list separator is specified, and the entire
296 remaining string is valid as an IP address, set an impossible separator so that
297 it is treated as one item. */
298
299 if (type == T_PTR && keystring[0] != '<' &&
300 string_is_ip_address(keystring, NULL) != 0)
301 sep = -1;
302
303 /* SPF strings should be concatenated without a separator, thus make
304 it the default if not defined (see RFC 4408 section 3.1.3).
305 Multiple SPF records are forbidden (section 3.1.2) but are currently
306 not handled specially, thus they are concatenated with \n by default.
307 MX priority and value are space-separated by default.
308 SRV and TLSA record parts are space-separated by default. */
309
310 if (!outsep2) switch(type)
311 {
312 case T_SPF: outsep2 = US""; break;
313 case T_SRV: case T_MX: case T_TLSA: outsep2 = US" "; break;
314 }
315
316 /* Now scan the list and do a lookup for each item */
317
318 while ((domain = string_nextinlist(&keystring, &sep, NULL, 0)))
319 {
320 uschar rbuffer[256];
321 int searchtype = (type == T_CSA)? T_SRV : /* record type we want */
322 (type == T_MXH)? T_MX :
323 (type == T_ZNS)? T_NS : type;
324
325 /* If the type is PTR or CSA, we have to construct the relevant magic lookup
326 key if the original is an IP address (some experimental protocols are using
327 PTR records for different purposes where the key string is a host name, and
328 Exim's extended CSA can be keyed by domains or IP addresses). This code for
329 doing the reversal is now in a separate function. */
330
331 if ((type == T_PTR || type == T_CSA) &&
332 string_is_ip_address(domain, NULL) != 0)
333 {
334 dns_build_reverse(domain, rbuffer);
335 domain = rbuffer;
336 }
337
338 do
339 {
340 DEBUG(D_lookup) debug_printf("dnsdb key: %s\n", domain);
341
342 /* Do the lookup and sort out the result. There are four special types that
343 are handled specially: T_CSA, T_ZNS, T_ADDRESSES and T_MXH.
344 The first two are handled in a special lookup function so that the facility
345 could be used from other parts of the Exim code. T_ADDRESSES is handled by looping
346 over the types of A lookup. T_MXH affects only what happens later on in
347 this function, but for tidiness it is handled by the "special". If the
348 lookup fails, continue with the next domain. In the case of DEFER, adjust
349 the final "nothing found" result, but carry on to the next domain. */
350
351 found = domain;
352 #if HAVE_IPV6
353 if (type == T_ADDRESSES) /* NB cannot happen unless HAVE_IPV6 */
354 {
355 if (searchtype == T_ADDRESSES) searchtype = T_AAAA;
356 else if (searchtype == T_AAAA) searchtype = T_A;
357 rc = dns_special_lookup(&dnsa, domain, searchtype, CUSS &found);
358 }
359 else
360 #endif
361 rc = dns_special_lookup(&dnsa, domain, type, CUSS &found);
362
363 lookup_dnssec_authenticated = dnssec_mode==OK ? NULL
364 : dns_is_secure(&dnsa) ? US"yes" : US"no";
365
366 if (rc == DNS_NOMATCH || rc == DNS_NODATA) continue;
367 if ( rc != DNS_SUCCEED
368 || (dnssec_mode == DEFER && !dns_is_secure(&dnsa))
369 )
370 {
371 if (defer_mode == DEFER)
372 {
373 dns_retrans = save_retrans;
374 dns_retry = save_retry;
375 dns_init(FALSE, FALSE, FALSE); /* clr dnssec bit */
376 return DEFER; /* always defer */
377 }
378 if (defer_mode == PASS) failrc = DEFER; /* defer only if all do */
379 continue; /* treat defer as fail */
380 }
381
382
383 /* Search the returned records */
384
385 for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
386 rr != NULL;
387 rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
388 {
389 if (rr->type != searchtype) continue;
390
391 if (*do_cache > rr->ttl)
392 *do_cache = rr->ttl;
393
394 if (type == T_A || type == T_AAAA || type == T_ADDRESSES)
395 {
396 dns_address *da;
397 for (da = dns_address_from_rr(&dnsa, rr); da; da = da->next)
398 {
399 if (ptr != 0) yield = string_catn(yield, &size, &ptr, outsep, 1);
400 yield = string_cat(yield, &size, &ptr, da->address);
401 }
402 continue;
403 }
404
405 /* Other kinds of record just have one piece of data each, but there may be
406 several of them, of course. */
407
408 if (ptr != 0) yield = string_catn(yield, &size, &ptr, outsep, 1);
409
410 if (type == T_TXT || type == T_SPF)
411 {
412 if (outsep2 == NULL)
413 {
414 /* output only the first item of data */
415 yield = string_catn(yield, &size, &ptr, (uschar *)(rr->data+1),
416 (rr->data)[0]);
417 }
418 else
419 {
420 /* output all items */
421 int data_offset = 0;
422 while (data_offset < rr->size)
423 {
424 uschar chunk_len = (rr->data)[data_offset++];
425 if (outsep2[0] != '\0' && data_offset != 1)
426 yield = string_catn(yield, &size, &ptr, outsep2, 1);
427 yield = string_catn(yield, &size, &ptr,
428 US ((rr->data)+data_offset), chunk_len);
429 data_offset += chunk_len;
430 }
431 }
432 }
433 else if (type == T_TLSA)
434 {
435 uint8_t usage, selector, matching_type;
436 uint16_t i, payload_length;
437 uschar s[MAX_TLSA_EXPANDED_SIZE];
438 uschar * sp = s;
439 uschar * p = US rr->data;
440
441 usage = *p++;
442 selector = *p++;
443 matching_type = *p++;
444 /* What's left after removing the first 3 bytes above */
445 payload_length = rr->size - 3;
446 sp += sprintf(CS s, "%d%c%d%c%d%c", usage, *outsep2,
447 selector, *outsep2, matching_type, *outsep2);
448 /* Now append the cert/identifier, one hex char at a time */
449 for (i=0;
450 i < payload_length && sp-s < (MAX_TLSA_EXPANDED_SIZE - 4);
451 i++)
452 sp += sprintf(CS sp, "%02x", (unsigned char)p[i]);
453
454 yield = string_cat(yield, &size, &ptr, s);
455 }
456 else /* T_CNAME, T_CSA, T_MX, T_MXH, T_NS, T_PTR, T_SOA, T_SRV */
457 {
458 int priority, weight, port;
459 uschar s[264];
460 uschar * p = US rr->data;
461
462 switch (type)
463 {
464 case T_MXH:
465 /* mxh ignores the priority number and includes only the hostnames */
466 GETSHORT(priority, p);
467 break;
468
469 case T_MX:
470 GETSHORT(priority, p);
471 sprintf(CS s, "%d%c", priority, *outsep2);
472 yield = string_cat(yield, &size, &ptr, s);
473 break;
474
475 case T_SRV:
476 GETSHORT(priority, p);
477 GETSHORT(weight, p);
478 GETSHORT(port, p);
479 sprintf(CS s, "%d%c%d%c%d%c", priority, *outsep2,
480 weight, *outsep2, port, *outsep2);
481 yield = string_cat(yield, &size, &ptr, s);
482 break;
483
484 case T_CSA:
485 /* See acl_verify_csa() for more comments about CSA. */
486 GETSHORT(priority, p);
487 GETSHORT(weight, p);
488 GETSHORT(port, p);
489
490 if (priority != 1) continue; /* CSA version must be 1 */
491
492 /* If the CSA record we found is not the one we asked for, analyse
493 the subdomain assertions in the port field, else analyse the direct
494 authorization status in the weight field. */
495
496 if (Ustrcmp(found, domain) != 0)
497 {
498 if (port & 1) *s = 'X'; /* explicit authorization required */
499 else *s = '?'; /* no subdomain assertions here */
500 }
501 else
502 {
503 if (weight < 2) *s = 'N'; /* not authorized */
504 else if (weight == 2) *s = 'Y'; /* authorized */
505 else if (weight == 3) *s = '?'; /* unauthorizable */
506 else continue; /* invalid */
507 }
508
509 s[1] = ' ';
510 yield = string_catn(yield, &size, &ptr, s, 2);
511 break;
512
513 default:
514 break;
515 }
516
517 /* GETSHORT() has advanced the pointer to the target domain. */
518
519 rc = dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, p,
520 (DN_EXPAND_ARG4_TYPE)s, sizeof(s));
521
522 /* If an overlong response was received, the data will have been
523 truncated and dn_expand may fail. */
524
525 if (rc < 0)
526 {
527 log_write(0, LOG_MAIN, "host name alias list truncated: type=%s "
528 "domain=%s", dns_text_type(type), domain);
529 break;
530 }
531 else yield = string_cat(yield, &size, &ptr, s);
532
533 if (type == T_SOA && outsep2 != NULL)
534 {
535 unsigned long serial, refresh, retry, expire, minimum;
536
537 p += rc;
538 yield = string_catn(yield, &size, &ptr, outsep2, 1);
539
540 rc = dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, p,
541 (DN_EXPAND_ARG4_TYPE)s, sizeof(s));
542 if (rc < 0)
543 {
544 log_write(0, LOG_MAIN, "responsible-mailbox truncated: type=%s "
545 "domain=%s", dns_text_type(type), domain);
546 break;
547 }
548 else yield = string_cat(yield, &size, &ptr, s);
549
550 p += rc;
551 GETLONG(serial, p); GETLONG(refresh, p);
552 GETLONG(retry, p); GETLONG(expire, p); GETLONG(minimum, p);
553 sprintf(CS s, "%c%lu%c%lu%c%lu%c%lu%c%lu",
554 *outsep2, serial, *outsep2, refresh,
555 *outsep2, retry, *outsep2, expire, *outsep2, minimum);
556 yield = string_cat(yield, &size, &ptr, s);
557 }
558 }
559 } /* Loop for list of returned records */
560
561 /* Loop for set of A-lookupu types */
562 } while (type == T_ADDRESSES && searchtype != T_A);
563
564 } /* Loop for list of domains */
565
566 /* Reclaim unused memory */
567
568 store_reset(yield + ptr + 1);
569
570 /* If ptr == 0 we have not found anything. Otherwise, insert the terminating
571 zero and return the result. */
572
573 dns_retrans = save_retrans;
574 dns_retry = save_retry;
575 dns_init(FALSE, FALSE, FALSE); /* clear the dnssec bit for getaddrbyname */
576
577 if (ptr == 0) return failrc;
578 yield[ptr] = 0;
579 *result = yield;
580 return OK;
581 }
582
583
584
585 /*************************************************
586 * Version reporting entry point *
587 *************************************************/
588
589 /* See local README for interface description. */
590
591 #include "../version.h"
592
593 void
594 dnsdb_version_report(FILE *f)
595 {
596 #ifdef DYNLOOKUP
597 fprintf(f, "Library version: DNSDB: Exim version %s\n", EXIM_VERSION_STR);
598 #endif
599 }
600
601
602 static lookup_info _lookup_info = {
603 US"dnsdb", /* lookup name */
604 lookup_querystyle, /* query style */
605 dnsdb_open, /* open function */
606 NULL, /* check function */
607 dnsdb_find, /* find function */
608 NULL, /* no close function */
609 NULL, /* no tidy function */
610 NULL, /* no quoting function */
611 dnsdb_version_report /* version reporting */
612 };
613
614 #ifdef DYNLOOKUP
615 #define dnsdb_lookup_module_info _lookup_module_info
616 #endif
617
618 static lookup_info *_lookup_list[] = { &_lookup_info };
619 lookup_module_info dnsdb_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
620
621 /* vi: aw ai sw=2
622 */
623 /* End of lookups/dnsdb.c */