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