69
81
* Find entry point for dnsdb *
70
82
*************************************************/
72
/* See local README for interface description. */
84
/* See local README for interface description. The query in the "keystring" may
85
consist of a number of parts.
87
(a) If the first significant character is '>' then the next character is the
88
separator character that is used when multiple records are found. The default
91
(b) If the next sequence of characters is 'defer_FOO' followed by a comma,
92
the defer behaviour is set to FOO. The possible behaviours are: 'strict', where
93
any defer causes the whole lookup to defer; 'lax', where a defer causes the
94
whole lookup to defer only if none of the DNS queries succeeds; and 'never',
95
where all defers are as if the lookup failed. The default is 'lax'.
97
(c) If the next sequence of characters is a sequence of letters and digits
98
followed by '=', it is interpreted as the name of the DNS record type. The
101
(d) Then there follows list of domain names. This is a generalized Exim list,
102
which may start with '<' in order to set a specific separator. The default
103
separator, as always, is colon. */
75
106
dnsdb_find(void *handle, uschar *filename, uschar *keystring, int length,
76
uschar **result, uschar **errmsg)
107
uschar **result, uschar **errmsg, BOOL *do_cache)
113
int defer_mode = PASS;
83
uschar *equals = Ustrchr(keystring, '=');
116
uschar *outsep = US"\n";
117
uschar *equals, *domain, *found;
84
118
uschar buffer[256];
120
/* Because we're the working in the search pool, we try to reclaim as much
121
store as possible later, so we preallocate the result here */
123
uschar *yield = store_get(size);
109
198
if (i >= sizeof(type_names)/sizeof(uschar *))
111
200
*errmsg = US"unsupported DNS record type";
114
keystring += len + 1;
117
/* If the type is PTR, we have to construct the relevant magic lookup
118
key. This code is now in a separate function. */
122
dns_build_reverse(keystring, buffer);
126
DEBUG(D_lookup) debug_printf("dnsdb key: %s\n", keystring);
128
/* Initialize the resolver, in case this is the first time it is used
129
in this run. Then do the lookup and sort out the result. */
204
keystring = equals + 1;
205
while (isspace(*keystring)) keystring++;
208
/* Initialize the resolver in case this is the first time it has been used. */
131
210
dns_init(FALSE, FALSE);
132
rc = dns_lookup(&dnsa, keystring, type, NULL);
134
if (rc == DNS_NOMATCH || rc == DNS_NODATA) return FAIL;
135
if (rc != DNS_SUCCEED) return DEFER;
137
for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
139
rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
212
/* The remainder of the string must be a list of domains. As long as the lookup
213
for at least one of them succeeds, we return success. Failure means that none
216
The original implementation did not support a list of domains. Adding the list
217
feature is compatible, except in one case: when PTR records are being looked up
218
for a single IPv6 address. Fortunately, we can hack in a compatibility feature
219
here: If the type is PTR and no list separator is specified, and the entire
220
remaining string is valid as an IP address, set an impossible separator so that
221
it is treated as one item. */
223
if (type == T_PTR && keystring[0] != '<' &&
224
string_is_ip_address(keystring, NULL) > 0)
227
/* Now scan the list and do a lookup for each item */
229
while ((domain = string_nextinlist(&keystring, &sep, buffer, sizeof(buffer)))
141
int len = 0; /* Stop picky compilers warning */
144
if (rr->type != type) continue;
146
/* There may be several addresses from an A6 record. Put newlines between
147
them, just as for between several records. */
156
for (da = dns_address_from_rr(&dnsa, rr); da != NULL; da = da->next)
158
len = Ustrlen(da->address);
167
yield = string_cat(yield, &size, &ptr, US"\n", 1);
168
yield = string_cat(yield, &size, &ptr, da->address, len);
174
/* Other kinds of record just have one piece of data each. */
179
s = string_copyn((uschar *)(rr->data+1), len);
181
else /* T_CNAME, T_MX, T_NS, T_PTR */
183
uschar *p = (uschar *)(rr->data);
185
s = store_get(ssize);
189
GETSHORT(number, p); /* pointer is advanced */
190
sprintf(CS s, "%d ", number);
193
p += dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, p,
194
(DN_EXPAND_ARG4_TYPE)(s + len), ssize - len);
195
len += Ustrlen(s+len);
196
store_reset(s + len + 1);
207
yield = string_cat(yield, &size, &ptr, US"\n", 1);
208
yield = string_cat(yield, &size, &ptr, s, len);
233
int searchtype = (type == T_CSA)? T_SRV : /* record type we want */
234
(type == T_MXH)? T_MX :
235
(type == T_ZNS)? T_NS : type;
237
/* If the type is PTR or CSA, we have to construct the relevant magic lookup
238
key if the original is an IP address (some experimental protocols are using
239
PTR records for different purposes where the key string is a host name, and
240
Exim's extended CSA can be keyed by domains or IP addresses). This code for
241
doing the reversal is now in a separate function. */
243
if ((type == T_PTR || type == T_CSA) &&
244
string_is_ip_address(domain, NULL) > 0)
246
dns_build_reverse(domain, rbuffer);
250
DEBUG(D_lookup) debug_printf("dnsdb key: %s\n", domain);
252
/* Do the lookup and sort out the result. There are three special types that
253
are handled specially: T_CSA, T_ZNS and T_MXH. The former two are handled in
254
a special lookup function so that the facility could be used from other
255
parts of the Exim code. The latter affects only what happens later on in
256
this function, but for tidiness it is handled in a similar way. If the
257
lookup fails, continue with the next domain. In the case of DEFER, adjust
258
the final "nothing found" result, but carry on to the next domain. */
261
rc = dns_special_lookup(&dnsa, domain, type, &found);
263
if (rc == DNS_NOMATCH || rc == DNS_NODATA) continue;
264
if (rc != DNS_SUCCEED)
266
if (defer_mode == DEFER) return DEFER; /* always defer */
267
else if (defer_mode == PASS) failrc = DEFER; /* defer only if all do */
268
continue; /* treat defer as fail */
271
/* Search the returned records */
273
for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
275
rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
277
if (rr->type != searchtype) continue;
279
/* There may be several addresses from an A6 record. Put the configured
280
separator between them, just as for between several records. However, A6
281
support is not normally configured these days. */
290
for (da = dns_address_from_rr(&dnsa, rr); da != NULL; da = da->next)
292
if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
293
yield = string_cat(yield, &size, &ptr, da->address,
294
Ustrlen(da->address));
299
/* Other kinds of record just have one piece of data each, but there may be
300
several of them, of course. */
302
if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
306
yield = string_cat(yield, &size, &ptr, (uschar *)(rr->data+1),
309
else /* T_CNAME, T_CSA, T_MX, T_MXH, T_NS, T_PTR, T_SRV */
311
int priority, weight, port;
313
uschar *p = (uschar *)(rr->data);
317
/* mxh ignores the priority number and includes only the hostnames */
318
GETSHORT(priority, p);
320
else if (type == T_MX)
322
GETSHORT(priority, p);
323
sprintf(CS s, "%d ", priority);
324
yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
326
else if (type == T_SRV)
328
GETSHORT(priority, p);
331
sprintf(CS s, "%d %d %d ", priority, weight, port);
332
yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
334
else if (type == T_CSA)
336
/* See acl_verify_csa() for more comments about CSA. */
338
GETSHORT(priority, p);
342
if (priority != 1) continue; /* CSA version must be 1 */
344
/* If the CSA record we found is not the one we asked for, analyse
345
the subdomain assertions in the port field, else analyse the direct
346
authorization status in the weight field. */
350
if (port & 1) *s = 'X'; /* explicit authorization required */
351
else *s = '?'; /* no subdomain assertions here */
355
if (weight < 2) *s = 'N'; /* not authorized */
356
else if (weight == 2) *s = 'Y'; /* authorized */
357
else if (weight == 3) *s = '?'; /* unauthorizable */
358
else continue; /* invalid */
362
yield = string_cat(yield, &size, &ptr, s, 2);
365
/* GETSHORT() has advanced the pointer to the target domain. */
367
rc = dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, p,
368
(DN_EXPAND_ARG4_TYPE)(s), sizeof(s));
370
/* If an overlong response was received, the data will have been
371
truncated and dn_expand may fail. */
375
log_write(0, LOG_MAIN, "host name alias list truncated: type=%s "
376
"domain=%s", dns_text_type(type), domain);
379
else yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
381
} /* Loop for list of returned records */
382
} /* Loop for list of domains */
384
/* Reclaim unused memory */
386
store_reset(yield + ptr + 1);
388
/* If ptr == 0 we have not found anything. Otherwise, insert the terminating
389
zero and return the result. */
391
if (ptr == 0) return failrc;