]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
faa133f3 | 2 | /*** |
faa133f3 | 3 | Copyright 2014 Lennart Poettering |
faa133f3 LP |
4 | ***/ |
5 | ||
b5efdb8a | 6 | #include "alloc-util.h" |
4ad7f276 | 7 | #include "dns-domain.h" |
0f7091e6 | 8 | #include "dns-type.h" |
b5efdb8a | 9 | #include "resolved-dns-question.h" |
faa133f3 | 10 | |
da6053d0 | 11 | DnsQuestion *dns_question_new(size_t n) { |
faa133f3 LP |
12 | DnsQuestion *q; |
13 | ||
14 | assert(n > 0); | |
15 | ||
16 | q = malloc0(offsetof(DnsQuestion, keys) + sizeof(DnsResourceKey*) * n); | |
17 | if (!q) | |
18 | return NULL; | |
19 | ||
20 | q->n_ref = 1; | |
21 | q->n_allocated = n; | |
22 | ||
23 | return q; | |
24 | } | |
25 | ||
26 | DnsQuestion *dns_question_ref(DnsQuestion *q) { | |
27 | if (!q) | |
28 | return NULL; | |
29 | ||
30 | assert(q->n_ref > 0); | |
31 | q->n_ref++; | |
32 | return q; | |
33 | } | |
34 | ||
35 | DnsQuestion *dns_question_unref(DnsQuestion *q) { | |
36 | if (!q) | |
37 | return NULL; | |
38 | ||
39 | assert(q->n_ref > 0); | |
40 | ||
41 | if (q->n_ref == 1) { | |
da6053d0 | 42 | size_t i; |
faa133f3 LP |
43 | |
44 | for (i = 0; i < q->n_keys; i++) | |
45 | dns_resource_key_unref(q->keys[i]); | |
46 | free(q); | |
47 | } else | |
48 | q->n_ref--; | |
49 | ||
50 | return NULL; | |
51 | } | |
52 | ||
53 | int dns_question_add(DnsQuestion *q, DnsResourceKey *key) { | |
da6053d0 | 54 | size_t i; |
7e8e0422 LP |
55 | int r; |
56 | ||
faa133f3 LP |
57 | assert(key); |
58 | ||
8013e860 LP |
59 | if (!q) |
60 | return -ENOSPC; | |
61 | ||
7e8e0422 LP |
62 | for (i = 0; i < q->n_keys; i++) { |
63 | r = dns_resource_key_equal(q->keys[i], key); | |
64 | if (r < 0) | |
65 | return r; | |
66 | if (r > 0) | |
67 | return 0; | |
68 | } | |
69 | ||
faa133f3 LP |
70 | if (q->n_keys >= q->n_allocated) |
71 | return -ENOSPC; | |
72 | ||
73 | q->keys[q->n_keys++] = dns_resource_key_ref(key); | |
74 | return 0; | |
75 | } | |
76 | ||
801ad6a6 | 77 | int dns_question_matches_rr(DnsQuestion *q, DnsResourceRecord *rr, const char *search_domain) { |
da6053d0 | 78 | size_t i; |
faa133f3 LP |
79 | int r; |
80 | ||
faa133f3 LP |
81 | assert(rr); |
82 | ||
8013e860 LP |
83 | if (!q) |
84 | return 0; | |
85 | ||
faa133f3 | 86 | for (i = 0; i < q->n_keys; i++) { |
801ad6a6 | 87 | r = dns_resource_key_match_rr(q->keys[i], rr, search_domain); |
faa133f3 LP |
88 | if (r != 0) |
89 | return r; | |
90 | } | |
91 | ||
92 | return 0; | |
93 | } | |
94 | ||
542e0c84 | 95 | int dns_question_matches_cname_or_dname(DnsQuestion *q, DnsResourceRecord *rr, const char *search_domain) { |
da6053d0 | 96 | size_t i; |
faa133f3 LP |
97 | int r; |
98 | ||
faa133f3 LP |
99 | assert(rr); |
100 | ||
8013e860 LP |
101 | if (!q) |
102 | return 0; | |
103 | ||
542e0c84 LP |
104 | if (!IN_SET(rr->key->type, DNS_TYPE_CNAME, DNS_TYPE_DNAME)) |
105 | return 0; | |
106 | ||
faa133f3 | 107 | for (i = 0; i < q->n_keys; i++) { |
542e0c84 LP |
108 | /* For a {C,D}NAME record we can never find a matching {C,D}NAME record */ |
109 | if (!dns_type_may_redirect(q->keys[i]->type)) | |
110 | return 0; | |
111 | ||
5d27351f | 112 | r = dns_resource_key_match_cname_or_dname(q->keys[i], rr->key, search_domain); |
faa133f3 LP |
113 | if (r != 0) |
114 | return r; | |
115 | } | |
116 | ||
34b9656f | 117 | return 0; |
faa133f3 LP |
118 | } |
119 | ||
703e4f5e | 120 | int dns_question_is_valid_for_query(DnsQuestion *q) { |
faa133f3 | 121 | const char *name; |
da6053d0 | 122 | size_t i; |
faa133f3 LP |
123 | int r; |
124 | ||
8013e860 LP |
125 | if (!q) |
126 | return 0; | |
faa133f3 LP |
127 | |
128 | if (q->n_keys <= 0) | |
129 | return 0; | |
130 | ||
131 | if (q->n_keys > 65535) | |
132 | return 0; | |
133 | ||
1c02e7ba | 134 | name = dns_resource_key_name(q->keys[0]); |
faa133f3 LP |
135 | if (!name) |
136 | return 0; | |
137 | ||
138 | /* Check that all keys in this question bear the same name */ | |
0f7091e6 | 139 | for (i = 0; i < q->n_keys; i++) { |
34b9656f LP |
140 | assert(q->keys[i]); |
141 | ||
0f7091e6 | 142 | if (i > 0) { |
1c02e7ba | 143 | r = dns_name_equal(dns_resource_key_name(q->keys[i]), name); |
0f7091e6 LP |
144 | if (r <= 0) |
145 | return r; | |
146 | } | |
147 | ||
148 | if (!dns_type_is_valid_query(q->keys[i]->type)) | |
149 | return 0; | |
faa133f3 LP |
150 | } |
151 | ||
152 | return 1; | |
153 | } | |
154 | ||
6a21960c | 155 | int dns_question_contains(DnsQuestion *a, const DnsResourceKey *k) { |
da6053d0 | 156 | size_t j; |
1086182d LP |
157 | int r; |
158 | ||
1086182d LP |
159 | assert(k); |
160 | ||
8013e860 LP |
161 | if (!a) |
162 | return 0; | |
163 | ||
1086182d LP |
164 | for (j = 0; j < a->n_keys; j++) { |
165 | r = dns_resource_key_equal(a->keys[j], k); | |
166 | if (r != 0) | |
167 | return r; | |
168 | } | |
169 | ||
170 | return 0; | |
171 | } | |
172 | ||
173 | int dns_question_is_equal(DnsQuestion *a, DnsQuestion *b) { | |
da6053d0 | 174 | size_t j; |
1086182d LP |
175 | int r; |
176 | ||
b6800689 LP |
177 | if (a == b) |
178 | return 1; | |
179 | ||
8013e860 LP |
180 | if (!a) |
181 | return !b || b->n_keys == 0; | |
182 | if (!b) | |
183 | return a->n_keys == 0; | |
1086182d LP |
184 | |
185 | /* Checks if all keys in a are also contained b, and vice versa */ | |
186 | ||
187 | for (j = 0; j < a->n_keys; j++) { | |
188 | r = dns_question_contains(b, a->keys[j]); | |
189 | if (r <= 0) | |
190 | return r; | |
191 | } | |
192 | ||
193 | for (j = 0; j < b->n_keys; j++) { | |
194 | r = dns_question_contains(a, b->keys[j]); | |
195 | if (r <= 0) | |
196 | return r; | |
197 | } | |
198 | ||
199 | return 1; | |
200 | } | |
201 | ||
36d9205d | 202 | int dns_question_cname_redirect(DnsQuestion *q, const DnsResourceRecord *cname, DnsQuestion **ret) { |
faa133f3 | 203 | _cleanup_(dns_question_unrefp) DnsQuestion *n = NULL; |
23b298bc | 204 | DnsResourceKey *key; |
faa133f3 | 205 | bool same = true; |
faa133f3 LP |
206 | int r; |
207 | ||
36d9205d | 208 | assert(cname); |
faa133f3 | 209 | assert(ret); |
58db254a | 210 | assert(IN_SET(cname->key->type, DNS_TYPE_CNAME, DNS_TYPE_DNAME)); |
faa133f3 | 211 | |
23b298bc LP |
212 | if (dns_question_size(q) <= 0) { |
213 | *ret = NULL; | |
8013e860 LP |
214 | return 0; |
215 | } | |
216 | ||
23b298bc | 217 | DNS_QUESTION_FOREACH(key, q) { |
58db254a LP |
218 | _cleanup_free_ char *destination = NULL; |
219 | const char *d; | |
220 | ||
221 | if (cname->key->type == DNS_TYPE_CNAME) | |
222 | d = cname->cname.name; | |
223 | else { | |
1c02e7ba | 224 | r = dns_name_change_suffix(dns_resource_key_name(key), dns_resource_key_name(cname->key), cname->dname.name, &destination); |
58db254a LP |
225 | if (r < 0) |
226 | return r; | |
227 | if (r == 0) | |
228 | continue; | |
229 | ||
230 | d = destination; | |
231 | } | |
232 | ||
1c02e7ba | 233 | r = dns_name_equal(dns_resource_key_name(key), d); |
faa133f3 LP |
234 | if (r < 0) |
235 | return r; | |
236 | ||
237 | if (r == 0) { | |
238 | same = false; | |
239 | break; | |
240 | } | |
241 | } | |
242 | ||
23b298bc | 243 | /* Fully the same, indicate we didn't do a thing */ |
faa133f3 | 244 | if (same) { |
23b298bc | 245 | *ret = NULL; |
faa133f3 LP |
246 | return 0; |
247 | } | |
248 | ||
249 | n = dns_question_new(q->n_keys); | |
250 | if (!n) | |
251 | return -ENOMEM; | |
252 | ||
253 | /* Create a new question, and patch in the new name */ | |
23b298bc | 254 | DNS_QUESTION_FOREACH(key, q) { |
faa133f3 LP |
255 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *k = NULL; |
256 | ||
23b298bc | 257 | k = dns_resource_key_new_redirect(key, cname); |
faa133f3 LP |
258 | if (!k) |
259 | return -ENOMEM; | |
260 | ||
261 | r = dns_question_add(n, k); | |
262 | if (r < 0) | |
263 | return r; | |
264 | } | |
265 | ||
1cc6c93a | 266 | *ret = TAKE_PTR(n); |
faa133f3 LP |
267 | |
268 | return 1; | |
269 | } | |
45ec7efb | 270 | |
703e4f5e LP |
271 | const char *dns_question_first_name(DnsQuestion *q) { |
272 | ||
273 | if (!q) | |
274 | return NULL; | |
45ec7efb LP |
275 | |
276 | if (q->n_keys < 1) | |
277 | return NULL; | |
278 | ||
1c02e7ba | 279 | return dns_resource_key_name(q->keys[0]); |
45ec7efb LP |
280 | } |
281 | ||
23b298bc | 282 | int dns_question_new_address(DnsQuestion **ret, int family, const char *name, bool convert_idna) { |
45ec7efb | 283 | _cleanup_(dns_question_unrefp) DnsQuestion *q = NULL; |
23b298bc | 284 | _cleanup_free_ char *buf = NULL; |
45ec7efb LP |
285 | int r; |
286 | ||
287 | assert(ret); | |
288 | assert(name); | |
289 | ||
290 | if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC)) | |
291 | return -EAFNOSUPPORT; | |
292 | ||
23b298bc LP |
293 | if (convert_idna) { |
294 | r = dns_name_apply_idna(name, &buf); | |
295 | if (r < 0) | |
296 | return r; | |
ad1f3fe6 | 297 | if (r > 0 && !streq(name, buf)) |
87057e24 | 298 | name = buf; |
ad1f3fe6 ZJS |
299 | else |
300 | /* We did not manage to create convert the idna name, or it's | |
301 | * the same as the original name. We assume the caller already | |
302 | * created an uncoverted question, so let's not repeat work | |
303 | * unnecessarily. */ | |
304 | return -EALREADY; | |
23b298bc LP |
305 | } |
306 | ||
45ec7efb LP |
307 | q = dns_question_new(family == AF_UNSPEC ? 2 : 1); |
308 | if (!q) | |
309 | return -ENOMEM; | |
310 | ||
311 | if (family != AF_INET6) { | |
312 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; | |
313 | ||
314 | key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, name); | |
315 | if (!key) | |
316 | return -ENOMEM; | |
317 | ||
318 | r = dns_question_add(q, key); | |
319 | if (r < 0) | |
320 | return r; | |
321 | } | |
322 | ||
323 | if (family != AF_INET) { | |
324 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; | |
325 | ||
326 | key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, name); | |
327 | if (!key) | |
328 | return -ENOMEM; | |
329 | ||
330 | r = dns_question_add(q, key); | |
331 | if (r < 0) | |
332 | return r; | |
333 | } | |
334 | ||
1cc6c93a | 335 | *ret = TAKE_PTR(q); |
45ec7efb LP |
336 | |
337 | return 0; | |
338 | } | |
339 | ||
340 | int dns_question_new_reverse(DnsQuestion **ret, int family, const union in_addr_union *a) { | |
341 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; | |
342 | _cleanup_(dns_question_unrefp) DnsQuestion *q = NULL; | |
343 | _cleanup_free_ char *reverse = NULL; | |
344 | int r; | |
345 | ||
346 | assert(ret); | |
347 | assert(a); | |
348 | ||
349 | if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC)) | |
350 | return -EAFNOSUPPORT; | |
351 | ||
352 | r = dns_name_reverse(family, a, &reverse); | |
353 | if (r < 0) | |
354 | return r; | |
355 | ||
356 | q = dns_question_new(1); | |
357 | if (!q) | |
358 | return -ENOMEM; | |
359 | ||
360 | key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, reverse); | |
361 | if (!key) | |
362 | return -ENOMEM; | |
363 | ||
364 | reverse = NULL; | |
365 | ||
366 | r = dns_question_add(q, key); | |
367 | if (r < 0) | |
368 | return r; | |
369 | ||
1cc6c93a | 370 | *ret = TAKE_PTR(q); |
45ec7efb LP |
371 | |
372 | return 0; | |
373 | } | |
374 | ||
23b298bc LP |
375 | int dns_question_new_service( |
376 | DnsQuestion **ret, | |
377 | const char *service, | |
378 | const char *type, | |
379 | const char *domain, | |
380 | bool with_txt, | |
381 | bool convert_idna) { | |
382 | ||
45ec7efb LP |
383 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
384 | _cleanup_(dns_question_unrefp) DnsQuestion *q = NULL; | |
23b298bc LP |
385 | _cleanup_free_ char *buf = NULL, *joined = NULL; |
386 | const char *name; | |
45ec7efb LP |
387 | int r; |
388 | ||
389 | assert(ret); | |
23b298bc LP |
390 | |
391 | /* We support three modes of invocation: | |
392 | * | |
393 | * 1. Only a domain is specified, in which case we assume a properly encoded SRV RR name, including service | |
394 | * type and possibly a service name. If specified in this way we assume it's already IDNA converted if | |
395 | * that's necessary. | |
396 | * | |
397 | * 2. Both service type and a domain specified, in which case a normal SRV RR is assumed, without a DNS-SD | |
398 | * style prefix. In this case we'll IDNA convert the domain, if that's requested. | |
399 | * | |
400 | * 3. All three of service name, type and domain are specified, in which case a DNS-SD service is put | |
401 | * together. The service name is never IDNA converted, and the domain is if requested. | |
402 | * | |
403 | * It's not supported to specify a service name without a type, or no domain name. | |
404 | */ | |
405 | ||
406 | if (!domain) | |
407 | return -EINVAL; | |
408 | ||
409 | if (type) { | |
410 | if (convert_idna) { | |
411 | r = dns_name_apply_idna(domain, &buf); | |
412 | if (r < 0) | |
413 | return r; | |
87057e24 ZJS |
414 | if (r > 0) |
415 | domain = buf; | |
23b298bc LP |
416 | } |
417 | ||
418 | r = dns_service_join(service, type, domain, &joined); | |
419 | if (r < 0) | |
420 | return r; | |
421 | ||
422 | name = joined; | |
423 | } else { | |
424 | if (service) | |
425 | return -EINVAL; | |
426 | ||
427 | name = domain; | |
428 | } | |
45ec7efb LP |
429 | |
430 | q = dns_question_new(1 + with_txt); | |
431 | if (!q) | |
432 | return -ENOMEM; | |
433 | ||
434 | key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_SRV, name); | |
435 | if (!key) | |
436 | return -ENOMEM; | |
437 | ||
438 | r = dns_question_add(q, key); | |
439 | if (r < 0) | |
440 | return r; | |
441 | ||
442 | if (with_txt) { | |
443 | dns_resource_key_unref(key); | |
444 | key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_TXT, name); | |
445 | if (!key) | |
446 | return -ENOMEM; | |
447 | ||
448 | r = dns_question_add(q, key); | |
449 | if (r < 0) | |
450 | return r; | |
451 | } | |
452 | ||
1cc6c93a | 453 | *ret = TAKE_PTR(q); |
45ec7efb LP |
454 | |
455 | return 0; | |
456 | } |