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