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