Debian Codegraph @

src/shared/dns-question.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4
5 #include "alloc-util.h"
6 #include "dns-domain.h"
7 #include "dns-question.h"
8 #include "dns-rr.h"
9 #include "dns-type.h"
10 #include "json-util.h"
11 #include "socket-util.h"
12 #include "string-util.h"
13
14 DnsQuestion *dns_question_new(size_t n) {
15 DnsQuestion *q;
16
17 if (n > UINT16_MAX) /* We can only place 64K key in an question section at max */
18 n = UINT16_MAX;
19
20 q = malloc0(offsetof(DnsQuestion, items) + sizeof(DnsQuestionItem) * n);
21 if (!q)
22 return NULL;
23
24 q->n_ref = 1;
25 q->n_allocated = n;
26
27 return q;
28 }
29
30 static DnsQuestion *dns_question_free(DnsQuestion *q) {
31 DnsResourceKey *key;
32
33 assert(q);
34
35 DNS_QUESTION_FOREACH(key, q)
36 dns_resource_key_unref(key);
37
38 return mfree(q);
39 }
40
41 DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsQuestion, dns_question, dns_question_free);
42
43 int dns_question_add_raw(DnsQuestion *q, DnsResourceKey *key, DnsQuestionFlags flags) {
44 /* Insert without checking for duplicates. */
45
46 assert(key);
47 assert(q);
48
49 if (q->n_keys >= q->n_allocated)
50 return -ENOSPC;
51
52 q->items[q->n_keys++] = (DnsQuestionItem) {
53 .key = dns_resource_key_ref(key),
54 .flags = flags,
55 };
56 return 0;
57 }
58
59 static int dns_question_add_raw_all(DnsQuestion *a, DnsQuestion *b) {
60 DnsQuestionItem *item;
61 int r;
62
63 DNS_QUESTION_FOREACH_ITEM(item, b) {
64 r = dns_question_add_raw(a, item->key, item->flags);
65 if (r < 0)
66 return r;
67 }
68
69 return 0;
70 }
71
72 int dns_question_add(DnsQuestion *q, DnsResourceKey *key, DnsQuestionFlags flags) {
73 DnsQuestionItem *item;
74 int r;
75
76 assert(key);
77
78 if (!q)
79 return -ENOSPC;
80
81 DNS_QUESTION_FOREACH_ITEM(item, q) {
82 r = dns_resource_key_equal(item->key, key);
83 if (r < 0)
84 return r;
85 if (r > 0 && item->flags == flags)
86 return 0;
87 }
88
89 return dns_question_add_raw(q, key, flags);
90 }
91
92 static int dns_question_add_all(DnsQuestion *a, DnsQuestion *b) {
93 DnsQuestionItem *item;
94 int r;
95
96 DNS_QUESTION_FOREACH_ITEM(item, b) {
97 r = dns_question_add(a, item->key, item->flags);
98 if (r < 0)
99 return r;
100 }
101
102 return 0;
103 }
104
105 int dns_question_matches_rr(DnsQuestion *q, DnsResourceRecord *rr, const char *search_domain) {
106 DnsResourceKey *key;
107 int r;
108
109 assert(rr);
110
111 if (!q)
112 return 0;
113
114 DNS_QUESTION_FOREACH(key, q) {
115 r = dns_resource_key_match_rr(key, rr, search_domain);
116 if (r != 0)
117 return r;
118 }
119
120 return 0;
121 }
122
123 int dns_question_matches_cname_or_dname(DnsQuestion *q, DnsResourceRecord *rr, const char *search_domain) {
124 DnsResourceKey *key;
125 int r;
126
127 assert(rr);
128
129 if (!q)
130 return 0;
131
132 if (!IN_SET(rr->key->type, DNS_TYPE_CNAME, DNS_TYPE_DNAME))
133 return 0;
134
135 DNS_QUESTION_FOREACH(key, q) {
136 /* For a {C,D}NAME record we can never find a matching {C,D}NAME record */
137 if (!dns_type_may_redirect(key->type))
138 return 0;
139
140 r = dns_resource_key_match_cname_or_dname(key, rr->key, search_domain);
141 if (r != 0)
142 return r;
143 }
144
145 return 0;
146 }
147
148 int dns_question_is_valid_for_query(DnsQuestion *q) {
149 const char *name;
150 size_t i;
151 int r;
152
153 if (!q)
154 return 0;
155
156 if (q->n_keys <= 0)
157 return 0;
158
159 if (q->n_keys > 65535)
160 return 0;
161
162 name = dns_resource_key_name(q->items[0].key);
163 if (!name)
164 return 0;
165
166 /* Check that all keys in this question bear the same name */
167 for (i = 0; i < q->n_keys; i++) {
168 assert(q->items[i].key);
169
170 if (i > 0) {
171 r = dns_name_equal(dns_resource_key_name(q->items[i].key), name);
172 if (r <= 0)
173 return r;
174 }
175
176 if (!dns_type_is_valid_query(q->items[i].key->type))
177 return 0;
178 }
179
180 return 1;
181 }
182
183 int dns_question_contains_key(DnsQuestion *q, const DnsResourceKey *k) {
184 size_t j;
185 int r;
186
187 assert(k);
188
189 if (!q)
190 return 0;
191
192 for (j = 0; j < q->n_keys; j++) {
193 r = dns_resource_key_equal(q->items[j].key, k);
194 if (r != 0)
195 return r;
196 }
197
198 return 0;
199 }
200
201 static int dns_question_contains_item(DnsQuestion *q, const DnsQuestionItem *i) {
202 DnsQuestionItem *item;
203 int r;
204
205 assert(i);
206
207 DNS_QUESTION_FOREACH_ITEM(item, q) {
208 if (item->flags != i->flags)
209 continue;
210 r = dns_resource_key_equal(item->key, i->key);
211 if (r != 0)
212 return r;
213 }
214
215 return false;
216 }
217
218 int dns_question_is_equal(DnsQuestion *a, DnsQuestion *b) {
219 DnsQuestionItem *item;
220 int r;
221
222 if (a == b)
223 return 1;
224
225 if (!a)
226 return !b || b->n_keys == 0;
227 if (!b)
228 return a->n_keys == 0;
229
230 /* Checks if all items in a are also contained b, and vice versa */
231
232 DNS_QUESTION_FOREACH_ITEM(item, a) {
233 r = dns_question_contains_item(b, item);
234 if (r <= 0)
235 return r;
236 }
237 DNS_QUESTION_FOREACH_ITEM(item, b) {
238 r = dns_question_contains_item(a, item);
239 if (r <= 0)
240 return r;
241 }
242
243 return 1;
244 }
245
246 int dns_question_cname_redirect(DnsQuestion *q, const DnsResourceRecord *cname, DnsQuestion **ret) {
247 _cleanup_(dns_question_unrefp) DnsQuestion *n = NULL;
248 DnsResourceKey *key;
249 bool same = true;
250 int r;
251
252 assert(cname);
253 assert(ret);
254 assert(IN_SET(cname->key->type, DNS_TYPE_CNAME, DNS_TYPE_DNAME));
255
256 if (dns_question_size(q) <= 0) {
257 *ret = NULL;
258 return 0;
259 }
260
261 DNS_QUESTION_FOREACH(key, q) {
262 _cleanup_free_ char *destination = NULL;
263 const char *d;
264
265 if (cname->key->type == DNS_TYPE_CNAME)
266 d = cname->cname.name;
267 else {
268 r = dns_name_change_suffix(dns_resource_key_name(key), dns_resource_key_name(cname->key), cname->dname.name, &destination);
269 if (r < 0)
270 return r;
271 if (r == 0)
272 continue;
273
274 d = destination;
275 }
276
277 r = dns_name_equal(dns_resource_key_name(key), d);
278 if (r < 0)
279 return r;
280
281 if (r == 0) {
282 same = false;
283 break;
284 }
285 }
286
287 /* Fully the same, indicate we didn't do a thing */
288 if (same) {
289 *ret = NULL;
290 return 0;
291 }
292
293 n = dns_question_new(q->n_keys);
294 if (!n)
295 return -ENOMEM;
296
297 /* Create a new question, and patch in the new name */
298 DNS_QUESTION_FOREACH(key, q) {
299 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *k = NULL;
300
301 k = dns_resource_key_new_redirect(key, cname);
302 if (!k)
303 return -ENOMEM;
304
305 r = dns_question_add(n, k, 0);
306 if (r < 0)
307 return r;
308 }
309
310 *ret = TAKE_PTR(n);
311
312 return 1;
313 }
314
315 const char* dns_question_first_name(DnsQuestion *q) {
316
317 if (!q)
318 return NULL;
319
320 if (q->n_keys < 1)
321 return NULL;
322
323 return dns_resource_key_name(q->items[0].key);
324 }
325
326 int dns_question_new_address(DnsQuestion **ret, int family, const char *name, bool convert_idna) {
327 _cleanup_(dns_question_unrefp) DnsQuestion *q = NULL;
328 _cleanup_free_ char *buf = NULL;
329 int r;
330
331 assert(ret);
332 assert(name);
333
334 if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
335 return -EAFNOSUPPORT;
336
337 /* If IPv6 is off and the request has an unspecified lookup family, restrict it automatically to
338 * IPv4. */
339 if (family == AF_UNSPEC && !socket_ipv6_is_enabled())
340 family = AF_INET;
341
342 if (convert_idna) {
343 r = dns_name_apply_idna(name, &buf);
344 if (r < 0)
345 return r;
346 if (r > 0 && !streq(name, buf))
347 name = buf;
348 else
349 /* We did not manage to create convert the idna name, or it's
350 * the same as the original name. We assume the caller already
351 * created an unconverted question, so let's not repeat work
352 * unnecessarily. */
353 return -EALREADY;
354 }
355
356 q = dns_question_new(family == AF_UNSPEC ? 2 : 1);
357 if (!q)
358 return -ENOMEM;
359
360 if (family != AF_INET6) {
361 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
362
363 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, name);
364 if (!key)
365 return -ENOMEM;
366
367 r = dns_question_add(q, key, 0);
368 if (r < 0)
369 return r;
370 }
371
372 if (family != AF_INET) {
373 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
374
375 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, name);
376 if (!key)
377 return -ENOMEM;
378
379 r = dns_question_add(q, key, 0);
380 if (r < 0)
381 return r;
382 }
383
384 *ret = TAKE_PTR(q);
385
386 return 0;
387 }
388
389 int dns_question_new_reverse(DnsQuestion **ret, int family, const union in_addr_union *a) {
390 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
391 _cleanup_(dns_question_unrefp) DnsQuestion *q = NULL;
392 _cleanup_free_ char *reverse = NULL;
393 int r;
394
395 assert(ret);
396 assert(a);
397
398 if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
399 return -EAFNOSUPPORT;
400
401 r = dns_name_reverse(family, a, &reverse);
402 if (r < 0)
403 return r;
404
405 q = dns_question_new(1);
406 if (!q)
407 return -ENOMEM;
408
409 key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, reverse);
410 if (!key)
411 return -ENOMEM;
412
413 reverse = NULL;
414
415 r = dns_question_add(q, key, 0);
416 if (r < 0)
417 return r;
418
419 *ret = TAKE_PTR(q);
420
421 return 0;
422 }
423
424 int dns_question_new_service_pointer(DnsQuestion **ret, const char *type, const char *domain, bool convert_idna) {
425 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
426 _cleanup_(dns_question_unrefp) DnsQuestion *q = NULL;
427 _cleanup_free_ char *buf = NULL, *joined = NULL;
428 const char *name;
429 int r;
430
431 assert(ret);
432
433 if (!domain)
434 return -EINVAL;
435
436 if (type) {
437 if (convert_idna) {
438 r = dns_name_apply_idna(domain, &buf);
439 if (r < 0)
440 return r;
441 if (r > 0)
442 domain = buf;
443 }
444
445 r = dns_service_join(NULL, type, domain, &joined);
446 if (r < 0)
447 return r;
448
449 name = joined;
450 } else
451 name = domain;
452
453
454 q = dns_question_new(1);
455 if (!q)
456 return -ENOMEM;
457
458 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, name);
459 if (!key)
460 return -ENOMEM;
461
462 r = dns_question_add(q, key, 0);
463 if (r < 0)
464 return r;
465
466 *ret = TAKE_PTR(q);
467
468 return 0;
469 }
470
471 int dns_question_new_service(
472 DnsQuestion **ret,
473 const char *service,
474 const char *type,
475 const char *domain,
476 bool with_txt,
477 bool convert_idna) {
478
479 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
480 _cleanup_(dns_question_unrefp) DnsQuestion *q = NULL;
481 _cleanup_free_ char *buf = NULL, *joined = NULL;
482 const char *name;
483 int r;
484
485 assert(ret);
486
487 /* We support three modes of invocation:
488 *
489 * 1. Only a domain is specified, in which case we assume a properly encoded SRV RR name, including service
490 * type and possibly a service name. If specified in this way we assume it's already IDNA converted if
491 * that's necessary.
492 *
493 * 2. Both service type and a domain specified, in which case a normal SRV RR is assumed, without a DNS-SD
494 * style prefix. In this case we'll IDNA convert the domain, if that's requested.
495 *
496 * 3. All three of service name, type and domain are specified, in which case a DNS-SD service is put
497 * together. The service name is never IDNA converted, and the domain is if requested.
498 *
499 * It's not supported to specify a service name without a type, or no domain name.
500 */
501
502 if (!domain)
503 return -EINVAL;
504
505 if (type) {
506 if (convert_idna) {
507 r = dns_name_apply_idna(domain, &buf);
508 if (r < 0)
509 return r;
510 if (r > 0)
511 domain = buf;
512 }
513
514 r = dns_service_join(service, type, domain, &joined);
515 if (r < 0)
516 return r;
517
518 name = joined;
519 } else {
520 if (service)
521 return -EINVAL;
522
523 name = domain;
524 }
525
526 q = dns_question_new(1 + with_txt);
527 if (!q)
528 return -ENOMEM;
529
530 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_SRV, name);
531 if (!key)
532 return -ENOMEM;
533
534 r = dns_question_add(q, key, 0);
535 if (r < 0)
536 return r;
537
538 if (with_txt) {
539 dns_resource_key_unref(key);
540 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_TXT, name);
541 if (!key)
542 return -ENOMEM;
543
544 r = dns_question_add(q, key, 0);
545 if (r < 0)
546 return r;
547 }
548
549 *ret = TAKE_PTR(q);
550
551 return 0;
552 }
553
554 /*
555 * This function is not used in the code base, but is useful when debugging. Do not delete.
556 */
557 void dns_question_dump(DnsQuestion *question, FILE *f) {
558 DnsResourceKey *k;
559
560 if (!f)
561 f = stdout;
562
563 DNS_QUESTION_FOREACH(k, question) {
564 char buf[DNS_RESOURCE_KEY_STRING_MAX];
565
566 fputc('\t', f);
567 fputs(dns_resource_key_to_string(k, buf, sizeof(buf)), f);
568 fputc('\n', f);
569 }
570 }
571
572 int dns_question_merge(DnsQuestion *a, DnsQuestion *b, DnsQuestion **ret) {
573 _cleanup_(dns_question_unrefp) DnsQuestion *k = NULL;
574 int r;
575
576 assert(ret);
577
578 if (a == b || dns_question_size(b) <= 0) {
579 *ret = dns_question_ref(a);
580 return 0;
581 }
582
583 if (dns_question_size(a) <= 0) {
584 *ret = dns_question_ref(b);
585 return 0;
586 }
587
588 k = dns_question_new(dns_question_size(a) + dns_question_size(b));
589 if (!k)
590 return -ENOMEM;
591
592 r = dns_question_add_raw_all(k, a);
593 if (r < 0)
594 return r;
595
596 r = dns_question_add_all(k, b);
597 if (r < 0)
598 return r;
599
600 *ret = TAKE_PTR(k);
601 return 0;
602 }
603
604 int dns_json_dispatch_question(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
605 DnsQuestion **q = ASSERT_PTR(userdata);
606 int r;
607
608 if (!sd_json_variant_is_array(variant))
609 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array.", strna(name));
610
611 if (sd_json_variant_elements(variant) > DNS_QUESTION_ITEMS_MAX)
612 return json_log(variant, flags, SYNTHETIC_ERRNO(E2BIG), "Too many questions in a single query.");
613
614 _cleanup_(dns_question_unrefp) DnsQuestion *nq = NULL;
615 nq = dns_question_new(sd_json_variant_elements(variant));
616 if (!nq)
617 return json_log_oom(variant, flags);
618
619 sd_json_variant *i;
620 JSON_VARIANT_ARRAY_FOREACH(i, variant) {
621 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
622
623 static const sd_json_dispatch_field dispatch_table[] = {
624 { "key", SD_JSON_VARIANT_OBJECT, dns_json_dispatch_resource_key, 0, SD_JSON_MANDATORY },
625 {}
626 };
627
628 r = sd_json_dispatch(i, dispatch_table, flags, &key);
629 if (r < 0)
630 return r;
631
632 if (dns_question_add(nq, key, /* flags= */ 0) < 0)
633 return json_log_oom(variant, flags);
634 }
635
636 dns_question_unref(*q);
637 *q = TAKE_PTR(nq);
638 return 0;
639 }