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