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