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