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