]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/resolve/resolved-dns-rr.c
systemd-resolved: split out inner loop
[thirdparty/systemd.git] / src / resolve / resolved-dns-rr.c
CommitLineData
74b2466e
LP
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
0dae31d4
ZJS
20#include <math.h>
21
b5efdb8a 22#include "alloc-util.h"
4ad7f276 23#include "dns-domain.h"
7263f724 24#include "dns-type.h"
95052df3 25#include "escape.h"
e4e73a63 26#include "hexdecoct.h"
fc8eec10 27#include "resolved-dns-dnssec.h"
07630cea 28#include "resolved-dns-packet.h"
e4e73a63 29#include "resolved-dns-rr.h"
8730bccf 30#include "string-table.h"
07630cea
LP
31#include "string-util.h"
32#include "strv.h"
d7671a3e 33#include "terminal-util.h"
74b2466e 34
faa133f3
LP
35DnsResourceKey* dns_resource_key_new(uint16_t class, uint16_t type, const char *name) {
36 DnsResourceKey *k;
37 size_t l;
74b2466e 38
faa133f3
LP
39 assert(name);
40
41 l = strlen(name);
42 k = malloc0(sizeof(DnsResourceKey) + l + 1);
43 if (!k)
44 return NULL;
45
46 k->n_ref = 1;
47 k->class = class;
48 k->type = type;
49
50 strcpy((char*) k + sizeof(DnsResourceKey), name);
51
52 return k;
53}
54
36d9205d 55DnsResourceKey* dns_resource_key_new_redirect(const DnsResourceKey *key, const DnsResourceRecord *cname) {
58db254a
LP
56 int r;
57
36d9205d
TG
58 assert(key);
59 assert(cname);
60
58db254a
LP
61 assert(IN_SET(cname->key->type, DNS_TYPE_CNAME, DNS_TYPE_DNAME));
62
63 if (cname->key->type == DNS_TYPE_CNAME)
64 return dns_resource_key_new(key->class, key->type, cname->cname.name);
65 else {
66 DnsResourceKey *k;
67 char *destination = NULL;
68
69 r = dns_name_change_suffix(DNS_RESOURCE_KEY_NAME(key), DNS_RESOURCE_KEY_NAME(cname->key), cname->dname.name, &destination);
70 if (r < 0)
71 return NULL;
72 if (r == 0)
73 return dns_resource_key_ref((DnsResourceKey*) key);
74
75 k = dns_resource_key_new_consume(key->class, key->type, destination);
76 if (!k) {
77 free(destination);
78 return NULL;
79 }
80
81 return k;
82 }
36d9205d
TG
83}
84
801ad6a6
LP
85int dns_resource_key_new_append_suffix(DnsResourceKey **ret, DnsResourceKey *key, char *name) {
86 DnsResourceKey *new_key;
87 char *joined;
88 int r;
89
90 assert(ret);
91 assert(key);
92 assert(name);
93
dc477e73 94 if (dns_name_is_root(name)) {
801ad6a6
LP
95 *ret = dns_resource_key_ref(key);
96 return 0;
97 }
98
99 r = dns_name_concat(DNS_RESOURCE_KEY_NAME(key), name, &joined);
100 if (r < 0)
101 return r;
102
103 new_key = dns_resource_key_new_consume(key->class, key->type, joined);
104 if (!new_key) {
105 free(joined);
106 return -ENOMEM;
107 }
108
109 *ret = new_key;
110 return 0;
111}
112
faa133f3
LP
113DnsResourceKey* dns_resource_key_new_consume(uint16_t class, uint16_t type, char *name) {
114 DnsResourceKey *k;
115
116 assert(name);
117
118 k = new0(DnsResourceKey, 1);
119 if (!k)
120 return NULL;
121
122 k->n_ref = 1;
123 k->class = class;
124 k->type = type;
125 k->_name = name;
126
127 return k;
128}
129
130DnsResourceKey* dns_resource_key_ref(DnsResourceKey *k) {
131
132 if (!k)
133 return NULL;
134
1b4f6e79
LP
135 /* Static/const keys created with DNS_RESOURCE_KEY_CONST will
136 * set this to -1, they should not be reffed/unreffed */
137 assert(k->n_ref != (unsigned) -1);
138
faa133f3
LP
139 assert(k->n_ref > 0);
140 k->n_ref++;
141
142 return k;
143}
144
145DnsResourceKey* dns_resource_key_unref(DnsResourceKey *k) {
146 if (!k)
147 return NULL;
148
1b4f6e79 149 assert(k->n_ref != (unsigned) -1);
faa133f3
LP
150 assert(k->n_ref > 0);
151
152 if (k->n_ref == 1) {
153 free(k->_name);
154 free(k);
155 } else
156 k->n_ref--;
157
158 return NULL;
159}
160
28b9b764
LP
161bool dns_resource_key_is_address(const DnsResourceKey *key) {
162 assert(key);
163
164 /* Check if this is an A or AAAA resource key */
165
166 return key->class == DNS_CLASS_IN && IN_SET(key->type, DNS_TYPE_A, DNS_TYPE_AAAA);
167}
168
faa133f3
LP
169int dns_resource_key_equal(const DnsResourceKey *a, const DnsResourceKey *b) {
170 int r;
171
4d247a6c
LP
172 if (a == b)
173 return 1;
174
faa133f3
LP
175 r = dns_name_equal(DNS_RESOURCE_KEY_NAME(a), DNS_RESOURCE_KEY_NAME(b));
176 if (r <= 0)
177 return r;
178
179 if (a->class != b->class)
180 return 0;
181
182 if (a->type != b->type)
183 return 0;
184
185 return 1;
186}
187
105e1512 188int dns_resource_key_match_rr(const DnsResourceKey *key, DnsResourceRecord *rr, const char *search_domain) {
801ad6a6
LP
189 int r;
190
faa133f3
LP
191 assert(key);
192 assert(rr);
193
4d247a6c
LP
194 if (key == rr->key)
195 return 1;
196
801ad6a6
LP
197 /* Checks if an rr matches the specified key. If a search
198 * domain is specified, it will also be checked if the key
199 * with the search domain suffixed might match the RR. */
200
faa133f3
LP
201 if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
202 return 0;
203
204 if (rr->key->type != key->type && key->type != DNS_TYPE_ANY)
205 return 0;
206
801ad6a6
LP
207 r = dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key));
208 if (r != 0)
209 return r;
210
211 if (search_domain) {
212 _cleanup_free_ char *joined = NULL;
213
214 r = dns_name_concat(DNS_RESOURCE_KEY_NAME(key), search_domain, &joined);
215 if (r < 0)
216 return r;
217
218 return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), joined);
219 }
220
221 return 0;
faa133f3
LP
222}
223
5d27351f 224int dns_resource_key_match_cname_or_dname(const DnsResourceKey *key, const DnsResourceKey *cname, const char *search_domain) {
801ad6a6
LP
225 int r;
226
faa133f3 227 assert(key);
5d27351f 228 assert(cname);
faa133f3 229
5d27351f 230 if (cname->class != key->class && key->class != DNS_CLASS_ANY)
faa133f3
LP
231 return 0;
232
5d27351f
TG
233 if (cname->type == DNS_TYPE_CNAME)
234 r = dns_name_equal(DNS_RESOURCE_KEY_NAME(key), DNS_RESOURCE_KEY_NAME(cname));
235 else if (cname->type == DNS_TYPE_DNAME)
236 r = dns_name_endswith(DNS_RESOURCE_KEY_NAME(key), DNS_RESOURCE_KEY_NAME(cname));
58db254a 237 else
faa133f3 238 return 0;
801ad6a6
LP
239
240 if (r != 0)
241 return r;
242
243 if (search_domain) {
244 _cleanup_free_ char *joined = NULL;
245
246 r = dns_name_concat(DNS_RESOURCE_KEY_NAME(key), search_domain, &joined);
247 if (r < 0)
248 return r;
249
5d27351f
TG
250 if (cname->type == DNS_TYPE_CNAME)
251 return dns_name_equal(joined, DNS_RESOURCE_KEY_NAME(cname));
252 else if (cname->type == DNS_TYPE_DNAME)
253 return dns_name_endswith(joined, DNS_RESOURCE_KEY_NAME(cname));
801ad6a6
LP
254 }
255
256 return 0;
547973de
LP
257}
258
259int dns_resource_key_match_soa(const DnsResourceKey *key, const DnsResourceKey *soa) {
260 assert(soa);
261 assert(key);
262
263 /* Checks whether 'soa' is a SOA record for the specified key. */
264
65b200e7 265 if (soa->class != key->class)
547973de 266 return 0;
801ad6a6 267
547973de
LP
268 if (soa->type != DNS_TYPE_SOA)
269 return 0;
270
0936416a 271 return dns_name_endswith(DNS_RESOURCE_KEY_NAME(key), DNS_RESOURCE_KEY_NAME(soa));
74b2466e
LP
272}
273
b826ab58 274static void dns_resource_key_hash_func(const void *i, struct siphash *state) {
322345fd 275 const DnsResourceKey *k = i;
322345fd 276
b826ab58 277 assert(k);
322345fd 278
b826ab58
TG
279 dns_name_hash_func(DNS_RESOURCE_KEY_NAME(k), state);
280 siphash24_compress(&k->class, sizeof(k->class), state);
281 siphash24_compress(&k->type, sizeof(k->type), state);
322345fd
LP
282}
283
d5099efc 284static int dns_resource_key_compare_func(const void *a, const void *b) {
322345fd
LP
285 const DnsResourceKey *x = a, *y = b;
286 int ret;
287
faa133f3 288 ret = dns_name_compare_func(DNS_RESOURCE_KEY_NAME(x), DNS_RESOURCE_KEY_NAME(y));
322345fd
LP
289 if (ret != 0)
290 return ret;
291
292 if (x->type < y->type)
293 return -1;
294 if (x->type > y->type)
295 return 1;
296
297 if (x->class < y->class)
298 return -1;
299 if (x->class > y->class)
300 return 1;
301
302 return 0;
303}
304
d5099efc
MS
305const struct hash_ops dns_resource_key_hash_ops = {
306 .hash = dns_resource_key_hash_func,
307 .compare = dns_resource_key_compare_func
308};
309
2d4c5cbc 310int dns_resource_key_to_string(const DnsResourceKey *key, char **ret) {
d23a27a9 311 char cbuf[strlen("CLASS") + DECIMAL_STR_MAX(uint16_t)], tbuf[strlen("TYPE") + DECIMAL_STR_MAX(uint16_t)];
f2af5ea3 312 const char *c, *t, *n;
2d4c5cbc
LP
313 char *s;
314
6af47493
LP
315 /* If we cannot convert the CLASS/TYPE into a known string,
316 use the format recommended by RFC 3597, Section 5. */
317
2d4c5cbc
LP
318 c = dns_class_to_string(key->class);
319 if (!c) {
d23a27a9 320 sprintf(cbuf, "CLASS%u", key->class);
2d4c5cbc
LP
321 c = cbuf;
322 }
323
324 t = dns_type_to_string(key->type);
325 if (!t){
d23a27a9 326 sprintf(tbuf, "TYPE%u", key->type);
2d4c5cbc
LP
327 t = tbuf;
328 }
329
f2af5ea3
LP
330 n = DNS_RESOURCE_KEY_NAME(key);
331 if (asprintf(&s, "%s%s %s %-5s", n, endswith(n, ".") ? "" : ".", c, t) < 0)
2d4c5cbc
LP
332 return -ENOMEM;
333
334 *ret = s;
335 return 0;
336}
337
f57e3cd5
LP
338bool dns_resource_key_reduce(DnsResourceKey **a, DnsResourceKey **b) {
339 assert(a);
340 assert(b);
341
342 /* Try to replace one RR key by another if they are identical, thus saving a bit of memory. Note that we do
343 * this only for RR keys, not for RRs themselves, as they carry a lot of additional metadata (where they come
344 * from, validity data, and suchlike), and cannot be replaced so easily by other RRs that have the same
345 * superficial data. */
346
347 if (!*a)
348 return false;
349 if (!*b)
350 return false;
351
352 /* We refuse merging const keys */
353 if ((*a)->n_ref == (unsigned) -1)
354 return false;
355 if ((*b)->n_ref == (unsigned) -1)
356 return false;
357
358 /* Already the same? */
359 if (*a == *b)
360 return true;
361
362 /* Are they really identical? */
363 if (dns_resource_key_equal(*a, *b) <= 0)
364 return false;
365
366 /* Keep the one which already has more references. */
367 if ((*a)->n_ref > (*b)->n_ref) {
368 dns_resource_key_unref(*b);
369 *b = dns_resource_key_ref(*a);
370 } else {
371 dns_resource_key_unref(*a);
372 *a = dns_resource_key_ref(*b);
373 }
374
375 return true;
376}
377
faa133f3 378DnsResourceRecord* dns_resource_record_new(DnsResourceKey *key) {
74b2466e
LP
379 DnsResourceRecord *rr;
380
381 rr = new0(DnsResourceRecord, 1);
382 if (!rr)
383 return NULL;
384
385 rr->n_ref = 1;
faa133f3 386 rr->key = dns_resource_key_ref(key);
ee3d6aff 387 rr->expiry = USEC_INFINITY;
97c67192 388 rr->n_skip_labels_signer = rr->n_skip_labels_source = (unsigned) -1;
faa133f3 389
74b2466e
LP
390 return rr;
391}
392
8bf52d3d
LP
393DnsResourceRecord* dns_resource_record_new_full(uint16_t class, uint16_t type, const char *name) {
394 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
395
396 key = dns_resource_key_new(class, type, name);
397 if (!key)
398 return NULL;
399
400 return dns_resource_record_new(key);
401}
402
74b2466e
LP
403DnsResourceRecord* dns_resource_record_ref(DnsResourceRecord *rr) {
404 if (!rr)
405 return NULL;
406
407 assert(rr->n_ref > 0);
408 rr->n_ref++;
409
410 return rr;
411}
412
413DnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr) {
414 if (!rr)
415 return NULL;
416
417 assert(rr->n_ref > 0);
418
419 if (rr->n_ref > 1) {
420 rr->n_ref--;
421 return NULL;
422 }
423
faa133f3 424 if (rr->key) {
9de3e329 425 switch(rr->key->type) {
9c92ce6d
LP
426
427 case DNS_TYPE_SRV:
428 free(rr->srv.name);
429 break;
430
9de3e329
ZJS
431 case DNS_TYPE_PTR:
432 case DNS_TYPE_NS:
433 case DNS_TYPE_CNAME:
8ac4e9e1 434 case DNS_TYPE_DNAME:
faa133f3 435 free(rr->ptr.name);
9de3e329 436 break;
9c92ce6d 437
9de3e329 438 case DNS_TYPE_HINFO:
faa133f3
LP
439 free(rr->hinfo.cpu);
440 free(rr->hinfo.os);
9de3e329 441 break;
9c92ce6d 442
9de3e329 443 case DNS_TYPE_TXT:
9c92ce6d 444 case DNS_TYPE_SPF:
2001c805 445 dns_txt_item_free_all(rr->txt.items);
9de3e329 446 break;
9c92ce6d 447
9de3e329 448 case DNS_TYPE_SOA:
7e8e0422
LP
449 free(rr->soa.mname);
450 free(rr->soa.rname);
9de3e329 451 break;
9c92ce6d 452
9de3e329 453 case DNS_TYPE_MX:
946c7094 454 free(rr->mx.exchange);
9de3e329 455 break;
9c92ce6d 456
abf126a3
TG
457 case DNS_TYPE_DS:
458 free(rr->ds.digest);
459 break;
460
42cc2eeb 461 case DNS_TYPE_SSHFP:
549c1a25 462 free(rr->sshfp.fingerprint);
42cc2eeb
LP
463 break;
464
8db0d2f5
ZJS
465 case DNS_TYPE_DNSKEY:
466 free(rr->dnskey.key);
467 break;
468
151226ab
ZJS
469 case DNS_TYPE_RRSIG:
470 free(rr->rrsig.signer);
471 free(rr->rrsig.signature);
472 break;
473
50f1e641
TG
474 case DNS_TYPE_NSEC:
475 free(rr->nsec.next_domain_name);
476 bitmap_free(rr->nsec.types);
477 break;
478
5d45a880
TG
479 case DNS_TYPE_NSEC3:
480 free(rr->nsec3.next_hashed_name);
481 free(rr->nsec3.salt);
482 bitmap_free(rr->nsec3.types);
483 break;
484
0dae31d4 485 case DNS_TYPE_LOC:
9de3e329
ZJS
486 case DNS_TYPE_A:
487 case DNS_TYPE_AAAA:
488 break;
9c92ce6d 489
48d45d2b
ZJS
490 case DNS_TYPE_TLSA:
491 free(rr->tlsa.data);
492 break;
493
95052df3
ZJS
494 case DNS_TYPE_CAA:
495 free(rr->caa.tag);
496 free(rr->caa.value);
497 break;
498
d93a16b8 499 case DNS_TYPE_OPENPGPKEY:
9de3e329 500 default:
faa133f3 501 free(rr->generic.data);
9de3e329 502 }
322345fd 503
a8812dd7 504 free(rr->wire_format);
faa133f3
LP
505 dns_resource_key_unref(rr->key);
506 }
322345fd 507
7b50eb2e 508 free(rr->to_string);
faa133f3 509 free(rr);
322345fd 510
322345fd
LP
511 return NULL;
512}
513
623a4c97
LP
514int dns_resource_record_new_reverse(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *hostname) {
515 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
516 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
517 _cleanup_free_ char *ptr = NULL;
518 int r;
519
520 assert(ret);
521 assert(address);
522 assert(hostname);
523
524 r = dns_name_reverse(family, address, &ptr);
525 if (r < 0)
526 return r;
527
528 key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, ptr);
529 if (!key)
530 return -ENOMEM;
531
532 ptr = NULL;
533
534 rr = dns_resource_record_new(key);
535 if (!rr)
536 return -ENOMEM;
537
538 rr->ptr.name = strdup(hostname);
539 if (!rr->ptr.name)
540 return -ENOMEM;
541
542 *ret = rr;
543 rr = NULL;
544
545 return 0;
546}
547
78c6a153
LP
548int dns_resource_record_new_address(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *name) {
549 DnsResourceRecord *rr;
550
551 assert(ret);
552 assert(address);
553 assert(family);
554
555 if (family == AF_INET) {
556
557 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, name);
558 if (!rr)
559 return -ENOMEM;
560
561 rr->a.in_addr = address->in;
562
563 } else if (family == AF_INET6) {
564
565 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_AAAA, name);
566 if (!rr)
567 return -ENOMEM;
568
569 rr->aaaa.in6_addr = address->in6;
570 } else
571 return -EAFNOSUPPORT;
572
573 *ret = rr;
574
575 return 0;
576}
577
a43a068a
ZJS
578#define FIELD_EQUAL(a, b, field) \
579 ((a).field ## _size == (b).field ## _size && \
580 memcmp((a).field, (b).field, (a).field ## _size) == 0)
581
322345fd
LP
582int dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b) {
583 int r;
584
585 assert(a);
586 assert(b);
587
4d247a6c
LP
588 if (a == b)
589 return 1;
590
faa133f3 591 r = dns_resource_key_equal(a->key, b->key);
322345fd
LP
592 if (r <= 0)
593 return r;
594
fd0b4602
LP
595 if (a->unparseable != b->unparseable)
596 return 0;
597
598 switch (a->unparseable ? _DNS_TYPE_INVALID : a->key->type) {
2d4c5cbc 599
9c92ce6d
LP
600 case DNS_TYPE_SRV:
601 r = dns_name_equal(a->srv.name, b->srv.name);
602 if (r <= 0)
603 return r;
604
605 return a->srv.priority == b->srv.priority &&
606 a->srv.weight == b->srv.weight &&
607 a->srv.port == b->srv.port;
608
2d4c5cbc
LP
609 case DNS_TYPE_PTR:
610 case DNS_TYPE_NS:
611 case DNS_TYPE_CNAME:
8ac4e9e1 612 case DNS_TYPE_DNAME:
322345fd 613 return dns_name_equal(a->ptr.name, b->ptr.name);
2d4c5cbc
LP
614
615 case DNS_TYPE_HINFO:
616 return strcaseeq(a->hinfo.cpu, b->hinfo.cpu) &&
617 strcaseeq(a->hinfo.os, b->hinfo.os);
618
9de3e329 619 case DNS_TYPE_SPF: /* exactly the same as TXT */
0f84a72e 620 case DNS_TYPE_TXT:
2001c805 621 return dns_txt_item_equal(a->txt.items, b->txt.items);
2e276efc 622
2d4c5cbc 623 case DNS_TYPE_A:
322345fd 624 return memcmp(&a->a.in_addr, &b->a.in_addr, sizeof(struct in_addr)) == 0;
2d4c5cbc
LP
625
626 case DNS_TYPE_AAAA:
322345fd 627 return memcmp(&a->aaaa.in6_addr, &b->aaaa.in6_addr, sizeof(struct in6_addr)) == 0;
2d4c5cbc
LP
628
629 case DNS_TYPE_SOA:
7e8e0422
LP
630 r = dns_name_equal(a->soa.mname, b->soa.mname);
631 if (r <= 0)
632 return r;
633 r = dns_name_equal(a->soa.rname, b->soa.rname);
634 if (r <= 0)
635 return r;
636
637 return a->soa.serial == b->soa.serial &&
638 a->soa.refresh == b->soa.refresh &&
639 a->soa.retry == b->soa.retry &&
640 a->soa.expire == b->soa.expire &&
641 a->soa.minimum == b->soa.minimum;
9c92ce6d 642
946c7094
ZJS
643 case DNS_TYPE_MX:
644 if (a->mx.priority != b->mx.priority)
645 return 0;
646
647 return dns_name_equal(a->mx.exchange, b->mx.exchange);
648
0dae31d4
ZJS
649 case DNS_TYPE_LOC:
650 assert(a->loc.version == b->loc.version);
651
652 return a->loc.size == b->loc.size &&
653 a->loc.horiz_pre == b->loc.horiz_pre &&
654 a->loc.vert_pre == b->loc.vert_pre &&
655 a->loc.latitude == b->loc.latitude &&
656 a->loc.longitude == b->loc.longitude &&
657 a->loc.altitude == b->loc.altitude;
658
abf126a3
TG
659 case DNS_TYPE_DS:
660 return a->ds.key_tag == b->ds.key_tag &&
661 a->ds.algorithm == b->ds.algorithm &&
662 a->ds.digest_type == b->ds.digest_type &&
a43a068a 663 FIELD_EQUAL(a->ds, b->ds, digest);
abf126a3 664
42cc2eeb
LP
665 case DNS_TYPE_SSHFP:
666 return a->sshfp.algorithm == b->sshfp.algorithm &&
667 a->sshfp.fptype == b->sshfp.fptype &&
a43a068a 668 FIELD_EQUAL(a->sshfp, b->sshfp, fingerprint);
42cc2eeb 669
8db0d2f5 670 case DNS_TYPE_DNSKEY:
f91dc240
LP
671 return a->dnskey.flags == b->dnskey.flags &&
672 a->dnskey.protocol == b->dnskey.protocol &&
8db0d2f5 673 a->dnskey.algorithm == b->dnskey.algorithm &&
a43a068a 674 FIELD_EQUAL(a->dnskey, b->dnskey, key);
8db0d2f5 675
151226ab
ZJS
676 case DNS_TYPE_RRSIG:
677 /* do the fast comparisons first */
a43a068a
ZJS
678 return a->rrsig.type_covered == b->rrsig.type_covered &&
679 a->rrsig.algorithm == b->rrsig.algorithm &&
680 a->rrsig.labels == b->rrsig.labels &&
681 a->rrsig.original_ttl == b->rrsig.original_ttl &&
682 a->rrsig.expiration == b->rrsig.expiration &&
683 a->rrsig.inception == b->rrsig.inception &&
684 a->rrsig.key_tag == b->rrsig.key_tag &&
685 FIELD_EQUAL(a->rrsig, b->rrsig, signature) &&
686 dns_name_equal(a->rrsig.signer, b->rrsig.signer);
151226ab 687
50f1e641
TG
688 case DNS_TYPE_NSEC:
689 return dns_name_equal(a->nsec.next_domain_name, b->nsec.next_domain_name) &&
690 bitmap_equal(a->nsec.types, b->nsec.types);
691
5d45a880
TG
692 case DNS_TYPE_NSEC3:
693 return a->nsec3.algorithm == b->nsec3.algorithm &&
a43a068a
ZJS
694 a->nsec3.flags == b->nsec3.flags &&
695 a->nsec3.iterations == b->nsec3.iterations &&
696 FIELD_EQUAL(a->nsec3, b->nsec3, salt) &&
697 FIELD_EQUAL(a->nsec3, b->nsec3, next_hashed_name) &&
698 bitmap_equal(a->nsec3.types, b->nsec3.types);
5d45a880 699
48d45d2b
ZJS
700 case DNS_TYPE_TLSA:
701 return a->tlsa.cert_usage == b->tlsa.cert_usage &&
702 a->tlsa.selector == b->tlsa.selector &&
703 a->tlsa.matching_type == b->tlsa.matching_type &&
a43a068a 704 FIELD_EQUAL(a->tlsa, b->tlsa, data);
48d45d2b 705
95052df3
ZJS
706 case DNS_TYPE_CAA:
707 return a->caa.flags == b->caa.flags &&
708 streq(a->caa.tag, b->caa.tag) &&
709 FIELD_EQUAL(a->caa, b->caa, value);
710
711 case DNS_TYPE_OPENPGPKEY:
2d4c5cbc 712 default:
a43a068a 713 return FIELD_EQUAL(a->generic, b->generic, data);
2d4c5cbc 714 }
322345fd
LP
715}
716
0dae31d4
ZJS
717static char* format_location(uint32_t latitude, uint32_t longitude, uint32_t altitude,
718 uint8_t size, uint8_t horiz_pre, uint8_t vert_pre) {
719 char *s;
720 char NS = latitude >= 1U<<31 ? 'N' : 'S';
721 char EW = longitude >= 1U<<31 ? 'E' : 'W';
722
723 int lat = latitude >= 1U<<31 ? (int) (latitude - (1U<<31)) : (int) ((1U<<31) - latitude);
724 int lon = longitude >= 1U<<31 ? (int) (longitude - (1U<<31)) : (int) ((1U<<31) - longitude);
725 double alt = altitude >= 10000000u ? altitude - 10000000u : -(double)(10000000u - altitude);
726 double siz = (size >> 4) * exp10((double) (size & 0xF));
727 double hor = (horiz_pre >> 4) * exp10((double) (horiz_pre & 0xF));
728 double ver = (vert_pre >> 4) * exp10((double) (vert_pre & 0xF));
729
730 if (asprintf(&s, "%d %d %.3f %c %d %d %.3f %c %.2fm %.2fm %.2fm %.2fm",
731 (lat / 60000 / 60),
732 (lat / 60000) % 60,
733 (lat % 60000) / 1000.,
734 NS,
735 (lon / 60000 / 60),
736 (lon / 60000) % 60,
737 (lon % 60000) / 1000.,
738 EW,
739 alt / 100.,
740 siz / 100.,
741 hor / 100.,
742 ver / 100.) < 0)
743 return NULL;
744
745 return s;
746}
747
7c6423e1
TG
748static int format_timestamp_dns(char *buf, size_t l, time_t sec) {
749 struct tm tm;
750
751 assert(buf);
752 assert(l > strlen("YYYYMMDDHHmmSS"));
753
754 if (!gmtime_r(&sec, &tm))
755 return -EINVAL;
756
757 if (strftime(buf, l, "%Y%m%d%H%M%S", &tm) <= 0)
758 return -EINVAL;
759
760 return 0;
761}
762
50f1e641
TG
763static char *format_types(Bitmap *types) {
764 _cleanup_strv_free_ char **strv = NULL;
765 _cleanup_free_ char *str = NULL;
cb57dd41 766 Iterator i;
50f1e641
TG
767 unsigned type;
768 int r;
769
cb57dd41 770 BITMAP_FOREACH(type, types, i) {
50f1e641 771 if (dns_type_to_string(type)) {
2c1fb4f7 772 r = strv_extend(&strv, dns_type_to_string(type));
50f1e641
TG
773 if (r < 0)
774 return NULL;
775 } else {
776 char *t;
777
778 r = asprintf(&t, "TYPE%u", type);
779 if (r < 0)
780 return NULL;
781
2c1fb4f7 782 r = strv_consume(&strv, t);
50f1e641
TG
783 if (r < 0)
784 return NULL;
785 }
786 }
787
788 str = strv_join(strv, " ");
789 if (!str)
790 return NULL;
791
792 return strjoin("( ", str, " )", NULL);
793}
794
2001c805
LP
795static char *format_txt(DnsTxtItem *first) {
796 DnsTxtItem *i;
797 size_t c = 1;
798 char *p, *s;
799
800 LIST_FOREACH(items, i, first)
801 c += i->length * 4 + 3;
802
803 p = s = new(char, c);
804 if (!s)
805 return NULL;
806
807 LIST_FOREACH(items, i, first) {
808 size_t j;
809
810 if (i != first)
811 *(p++) = ' ';
812
813 *(p++) = '"';
814
815 for (j = 0; j < i->length; j++) {
816 if (i->data[j] < ' ' || i->data[j] == '"' || i->data[j] >= 127) {
817 *(p++) = '\\';
818 *(p++) = '0' + (i->data[j] / 100);
819 *(p++) = '0' + ((i->data[j] / 10) % 10);
820 *(p++) = '0' + (i->data[j] % 10);
821 } else
822 *(p++) = i->data[j];
823 }
824
825 *(p++) = '"';
826 }
827
828 *p = 0;
829 return s;
830}
831
7b50eb2e 832const char *dns_resource_record_to_string(DnsResourceRecord *rr) {
8db0d2f5 833 _cleanup_free_ char *k = NULL, *t = NULL;
2d4c5cbc
LP
834 char *s;
835 int r;
322345fd 836
2d4c5cbc 837 assert(rr);
322345fd 838
7b50eb2e
LP
839 if (rr->to_string)
840 return rr->to_string;
841
2d4c5cbc
LP
842 r = dns_resource_key_to_string(rr->key, &k);
843 if (r < 0)
7b50eb2e 844 return NULL;
322345fd 845
0dae31d4 846 switch (rr->unparseable ? _DNS_TYPE_INVALID : rr->key->type) {
322345fd 847
9c92ce6d
LP
848 case DNS_TYPE_SRV:
849 r = asprintf(&s, "%s %u %u %u %s",
850 k,
851 rr->srv.priority,
852 rr->srv.weight,
853 rr->srv.port,
854 strna(rr->srv.name));
855 if (r < 0)
7b50eb2e 856 return NULL;
9c92ce6d
LP
857 break;
858
2d4c5cbc
LP
859 case DNS_TYPE_PTR:
860 case DNS_TYPE_NS:
861 case DNS_TYPE_CNAME:
8ac4e9e1 862 case DNS_TYPE_DNAME:
2d4c5cbc
LP
863 s = strjoin(k, " ", rr->ptr.name, NULL);
864 if (!s)
7b50eb2e 865 return NULL;
322345fd 866
2d4c5cbc 867 break;
322345fd 868
2d4c5cbc
LP
869 case DNS_TYPE_HINFO:
870 s = strjoin(k, " ", rr->hinfo.cpu, " ", rr->hinfo.os, NULL);
871 if (!s)
7b50eb2e 872 return NULL;
2d4c5cbc 873 break;
322345fd 874
9de3e329 875 case DNS_TYPE_SPF: /* exactly the same as TXT */
8db0d2f5 876 case DNS_TYPE_TXT:
2001c805 877 t = format_txt(rr->txt.items);
2e276efc 878 if (!t)
7b50eb2e 879 return NULL;
2e276efc
ZJS
880
881 s = strjoin(k, " ", t, NULL);
882 if (!s)
7b50eb2e 883 return NULL;
2e276efc 884 break;
2e276efc 885
2d4c5cbc
LP
886 case DNS_TYPE_A: {
887 _cleanup_free_ char *x = NULL;
322345fd 888
2d4c5cbc
LP
889 r = in_addr_to_string(AF_INET, (const union in_addr_union*) &rr->a.in_addr, &x);
890 if (r < 0)
7b50eb2e 891 return NULL;
322345fd 892
2d4c5cbc
LP
893 s = strjoin(k, " ", x, NULL);
894 if (!s)
7b50eb2e 895 return NULL;
2d4c5cbc
LP
896 break;
897 }
322345fd 898
8db0d2f5
ZJS
899 case DNS_TYPE_AAAA:
900 r = in_addr_to_string(AF_INET6, (const union in_addr_union*) &rr->aaaa.in6_addr, &t);
2d4c5cbc 901 if (r < 0)
7b50eb2e 902 return NULL;
322345fd 903
8db0d2f5 904 s = strjoin(k, " ", t, NULL);
2d4c5cbc 905 if (!s)
7b50eb2e 906 return NULL;
2d4c5cbc 907 break;
322345fd 908
2d4c5cbc
LP
909 case DNS_TYPE_SOA:
910 r = asprintf(&s, "%s %s %s %u %u %u %u %u",
911 k,
912 strna(rr->soa.mname),
913 strna(rr->soa.rname),
914 rr->soa.serial,
915 rr->soa.refresh,
916 rr->soa.retry,
917 rr->soa.expire,
918 rr->soa.minimum);
919 if (r < 0)
7b50eb2e 920 return NULL;
2d4c5cbc
LP
921 break;
922
946c7094
ZJS
923 case DNS_TYPE_MX:
924 r = asprintf(&s, "%s %u %s",
925 k,
926 rr->mx.priority,
927 rr->mx.exchange);
928 if (r < 0)
7b50eb2e 929 return NULL;
946c7094
ZJS
930 break;
931
8db0d2f5 932 case DNS_TYPE_LOC:
0dae31d4
ZJS
933 assert(rr->loc.version == 0);
934
8db0d2f5
ZJS
935 t = format_location(rr->loc.latitude,
936 rr->loc.longitude,
937 rr->loc.altitude,
938 rr->loc.size,
939 rr->loc.horiz_pre,
940 rr->loc.vert_pre);
941 if (!t)
7b50eb2e 942 return NULL;
0dae31d4 943
8db0d2f5 944 s = strjoin(k, " ", t, NULL);
0dae31d4 945 if (!s)
7b50eb2e 946 return NULL;
0dae31d4 947 break;
0dae31d4 948
abf126a3
TG
949 case DNS_TYPE_DS:
950 t = hexmem(rr->ds.digest, rr->ds.digest_size);
951 if (!t)
7b50eb2e 952 return NULL;
abf126a3
TG
953
954 r = asprintf(&s, "%s %u %u %u %s",
955 k,
956 rr->ds.key_tag,
957 rr->ds.algorithm,
958 rr->ds.digest_type,
959 t);
960 if (r < 0)
7b50eb2e 961 return NULL;
abf126a3
TG
962 break;
963
8db0d2f5 964 case DNS_TYPE_SSHFP:
549c1a25 965 t = hexmem(rr->sshfp.fingerprint, rr->sshfp.fingerprint_size);
8db0d2f5 966 if (!t)
7b50eb2e 967 return NULL;
42cc2eeb
LP
968
969 r = asprintf(&s, "%s %u %u %s",
970 k,
971 rr->sshfp.algorithm,
972 rr->sshfp.fptype,
8db0d2f5 973 t);
42cc2eeb 974 if (r < 0)
7b50eb2e 975 return NULL;
42cc2eeb 976 break;
42cc2eeb 977
ff3d6560 978 case DNS_TYPE_DNSKEY: {
8e54f5d9 979 _cleanup_free_ char *alg = NULL;
99e5ca6d 980 char *ss;
718af59e 981 int n;
fc8eec10
ZJS
982 uint16_t key_tag;
983
984 key_tag = dnssec_keytag(rr, true);
ff3d6560 985
8e54f5d9
LP
986 r = dnssec_algorithm_to_string_alloc(rr->dnskey.algorithm, &alg);
987 if (r < 0)
988 return NULL;
ff3d6560 989
718af59e 990 r = asprintf(&s, "%s %u %u %s %n",
8db0d2f5 991 k,
f91dc240
LP
992 rr->dnskey.flags,
993 rr->dnskey.protocol,
8e54f5d9 994 alg,
d7671a3e 995 &n);
8db0d2f5 996 if (r < 0)
7b50eb2e 997 return NULL;
d7671a3e
ZJS
998
999 r = base64_append(&s, n,
1000 rr->dnskey.key, rr->dnskey.key_size,
1001 8, columns());
1002 if (r < 0)
1003 return NULL;
1004
99e5ca6d 1005 r = asprintf(&ss, "%s\n"
718af59e
ZJS
1006 " -- Flags:%s%s%s\n"
1007 " -- Key tag: %u",
99e5ca6d 1008 s,
99e5ca6d
ZJS
1009 rr->dnskey.flags & DNSKEY_FLAG_SEP ? " SEP" : "",
1010 rr->dnskey.flags & DNSKEY_FLAG_REVOKE ? " REVOKE" : "",
fc8eec10 1011 rr->dnskey.flags & DNSKEY_FLAG_ZONE_KEY ? " ZONE_KEY" : "",
fc8eec10 1012 key_tag);
99e5ca6d
ZJS
1013 if (r < 0)
1014 return NULL;
1015 free(s);
1016 s = ss;
1017
8db0d2f5 1018 break;
ff3d6560 1019 }
2d4c5cbc 1020
151226ab 1021 case DNS_TYPE_RRSIG: {
8e54f5d9 1022 _cleanup_free_ char *alg = NULL;
7c6423e1 1023 char expiration[strlen("YYYYMMDDHHmmSS") + 1], inception[strlen("YYYYMMDDHHmmSS") + 1];
8e54f5d9 1024 const char *type;
d7671a3e 1025 int n;
151226ab
ZJS
1026
1027 type = dns_type_to_string(rr->rrsig.type_covered);
8e54f5d9
LP
1028
1029 r = dnssec_algorithm_to_string_alloc(rr->rrsig.algorithm, &alg);
1030 if (r < 0)
1031 return NULL;
151226ab 1032
7c6423e1
TG
1033 r = format_timestamp_dns(expiration, sizeof(expiration), rr->rrsig.expiration);
1034 if (r < 0)
7b50eb2e 1035 return NULL;
7c6423e1
TG
1036
1037 r = format_timestamp_dns(inception, sizeof(inception), rr->rrsig.inception);
1038 if (r < 0)
7b50eb2e 1039 return NULL;
7c6423e1 1040
151226ab
ZJS
1041 /* TYPE?? follows
1042 * http://tools.ietf.org/html/rfc3597#section-5 */
1043
d7671a3e 1044 r = asprintf(&s, "%s %s%.*u %s %u %u %s %s %u %s %n",
151226ab
ZJS
1045 k,
1046 type ?: "TYPE",
1047 type ? 0 : 1, type ? 0u : (unsigned) rr->rrsig.type_covered,
8e54f5d9 1048 alg,
151226ab
ZJS
1049 rr->rrsig.labels,
1050 rr->rrsig.original_ttl,
7c6423e1
TG
1051 expiration,
1052 inception,
151226ab
ZJS
1053 rr->rrsig.key_tag,
1054 rr->rrsig.signer,
d7671a3e 1055 &n);
151226ab 1056 if (r < 0)
7b50eb2e 1057 return NULL;
d7671a3e
ZJS
1058
1059 r = base64_append(&s, n,
1060 rr->rrsig.signature, rr->rrsig.signature_size,
1061 8, columns());
1062 if (r < 0)
1063 return NULL;
1064
151226ab
ZJS
1065 break;
1066 }
1067
50f1e641
TG
1068 case DNS_TYPE_NSEC:
1069 t = format_types(rr->nsec.types);
1070 if (!t)
7b50eb2e 1071 return NULL;
50f1e641
TG
1072
1073 r = asprintf(&s, "%s %s %s",
1074 k,
1075 rr->nsec.next_domain_name,
1076 t);
1077 if (r < 0)
7b50eb2e 1078 return NULL;
50f1e641
TG
1079 break;
1080
5d45a880
TG
1081 case DNS_TYPE_NSEC3: {
1082 _cleanup_free_ char *salt = NULL, *hash = NULL;
1083
f5430a3e 1084 if (rr->nsec3.salt_size > 0) {
5d45a880
TG
1085 salt = hexmem(rr->nsec3.salt, rr->nsec3.salt_size);
1086 if (!salt)
7b50eb2e 1087 return NULL;
5d45a880
TG
1088 }
1089
1090 hash = base32hexmem(rr->nsec3.next_hashed_name, rr->nsec3.next_hashed_name_size, false);
1091 if (!hash)
7b50eb2e 1092 return NULL;
5d45a880
TG
1093
1094 t = format_types(rr->nsec3.types);
1095 if (!t)
7b50eb2e 1096 return NULL;
5d45a880
TG
1097
1098 r = asprintf(&s, "%s %"PRIu8" %"PRIu8" %"PRIu16" %s %s %s",
1099 k,
1100 rr->nsec3.algorithm,
1101 rr->nsec3.flags,
1102 rr->nsec3.iterations,
f5430a3e 1103 rr->nsec3.salt_size > 0 ? salt : "-",
5d45a880
TG
1104 hash,
1105 t);
1106 if (r < 0)
7b50eb2e 1107 return NULL;
5d45a880
TG
1108
1109 break;
1110 }
1111
48d45d2b 1112 case DNS_TYPE_TLSA: {
cfb90da3
ZJS
1113 const char *cert_usage, *selector, *matching_type;
1114 char *ss;
48d45d2b
ZJS
1115 int n;
1116
cfb90da3
ZJS
1117 cert_usage = tlsa_cert_usage_to_string(rr->tlsa.cert_usage);
1118 selector = tlsa_selector_to_string(rr->tlsa.selector);
1119 matching_type = tlsa_matching_type_to_string(rr->tlsa.matching_type);
1120
48d45d2b
ZJS
1121 r = asprintf(&s, "%s %u %u %u %n",
1122 k,
1123 rr->tlsa.cert_usage,
1124 rr->tlsa.selector,
1125 rr->tlsa.matching_type,
1126 &n);
1127 if (r < 0)
1128 return NULL;
1129
1130 r = base64_append(&s, n,
1131 rr->tlsa.data, rr->tlsa.data_size,
1132 8, columns());
1133 if (r < 0)
1134 return NULL;
cfb90da3
ZJS
1135
1136 r = asprintf(&ss, "%s\n"
718af59e
ZJS
1137 " -- Cert. usage: %s\n"
1138 " -- Selector: %s\n"
1139 " -- Matching type: %s",
cfb90da3 1140 s,
718af59e
ZJS
1141 cert_usage,
1142 selector,
1143 matching_type);
cfb90da3
ZJS
1144 if (r < 0)
1145 return NULL;
1146 free(s);
1147 s = ss;
1148
48d45d2b
ZJS
1149 break;
1150 }
1151
95052df3
ZJS
1152 case DNS_TYPE_CAA: {
1153 _cleanup_free_ char *value;
1154
1155 value = octescape(rr->caa.value, rr->caa.value_size);
1156 if (!value)
1157 return NULL;
1158
718af59e 1159 r = asprintf(&s, "%s %u %s \"%s\"%s%s%s%.0u",
95052df3
ZJS
1160 k,
1161 rr->caa.flags,
1162 rr->caa.tag,
718af59e
ZJS
1163 value,
1164 rr->caa.flags ? "\n -- Flags:" : "",
1165 rr->caa.flags & CAA_FLAG_CRITICAL ? " critical" : "",
1166 rr->caa.flags & ~CAA_FLAG_CRITICAL ? " " : "",
1167 rr->caa.flags & ~CAA_FLAG_CRITICAL);
95052df3
ZJS
1168 if (r < 0)
1169 return NULL;
1170
1171 break;
1172 }
1173
d93a16b8
ZJS
1174 case DNS_TYPE_OPENPGPKEY: {
1175 int n;
1176
1177 r = asprintf(&s, "%s %n",
1178 k,
1179 &n);
1180 if (r < 0)
1181 return NULL;
1182
1183 r = base64_append(&s, n,
a43a068a 1184 rr->generic.data, rr->generic.data_size,
d93a16b8
ZJS
1185 8, columns());
1186 if (r < 0)
1187 return NULL;
1188 break;
1189 }
1190
8db0d2f5 1191 default:
a43a068a 1192 t = hexmem(rr->generic.data, rr->generic.data_size);
8db0d2f5 1193 if (!t)
7b50eb2e 1194 return NULL;
8db0d2f5 1195
6af47493 1196 /* Format as documented in RFC 3597, Section 5 */
a43a068a 1197 r = asprintf(&s, "%s \\# %zu %s", k, rr->generic.data_size, t);
d23a27a9 1198 if (r < 0)
7b50eb2e 1199 return NULL;
2d4c5cbc 1200 break;
8db0d2f5 1201 }
2d4c5cbc 1202
7b50eb2e
LP
1203 rr->to_string = s;
1204 return s;
2d4c5cbc 1205}
322345fd 1206
2e74028a
ZJS
1207ssize_t dns_resource_record_payload(DnsResourceRecord *rr, void **out) {
1208 assert(rr);
1209 assert(out);
1210
1211 switch(rr->unparseable ? _DNS_TYPE_INVALID : rr->key->type) {
1212 case DNS_TYPE_SRV:
1213 case DNS_TYPE_PTR:
1214 case DNS_TYPE_NS:
1215 case DNS_TYPE_CNAME:
1216 case DNS_TYPE_DNAME:
1217 case DNS_TYPE_HINFO:
1218 case DNS_TYPE_SPF:
1219 case DNS_TYPE_TXT:
1220 case DNS_TYPE_A:
1221 case DNS_TYPE_AAAA:
1222 case DNS_TYPE_SOA:
1223 case DNS_TYPE_MX:
1224 case DNS_TYPE_LOC:
1225 case DNS_TYPE_DS:
1226 case DNS_TYPE_SSHFP:
1227 case DNS_TYPE_DNSKEY:
1228 case DNS_TYPE_RRSIG:
1229 case DNS_TYPE_NSEC:
1230 case DNS_TYPE_NSEC3:
1231 return -EINVAL;
1232
1233 case DNS_TYPE_TLSA:
1234 *out = rr->tlsa.data;
1235 return rr->tlsa.data_size;
1236
1237
1238 case DNS_TYPE_OPENPGPKEY:
1239 default:
1240 *out = rr->generic.data;
1241 return rr->generic.data_size;
1242 }
1243}
1244
a8812dd7
LP
1245int dns_resource_record_to_wire_format(DnsResourceRecord *rr, bool canonical) {
1246
1247 DnsPacket packet = {
1248 .n_ref = 1,
1249 .protocol = DNS_PROTOCOL_DNS,
1250 .on_stack = true,
1251 .refuse_compression = true,
1252 .canonical_form = canonical,
1253 };
1254
1255 size_t start, rds;
1256 int r;
1257
1258 assert(rr);
1259
1260 /* Generates the RR in wire-format, optionally in the
1261 * canonical form as discussed in the DNSSEC RFC 4034, Section
1262 * 6.2. We allocate a throw-away DnsPacket object on the stack
1263 * here, because we need some book-keeping for memory
1264 * management, and can reuse the DnsPacket serializer, that
1265 * can generate the canonical form, too, but also knows label
1266 * compression and suchlike. */
1267
1268 if (rr->wire_format && rr->wire_format_canonical == canonical)
1269 return 0;
1270
1271 r = dns_packet_append_rr(&packet, rr, &start, &rds);
1272 if (r < 0)
1273 return r;
1274
1275 assert(start == 0);
1276 assert(packet._data);
1277
1278 free(rr->wire_format);
1279 rr->wire_format = packet._data;
1280 rr->wire_format_size = packet.size;
1281 rr->wire_format_rdata_offset = rds;
1282 rr->wire_format_canonical = canonical;
1283
1284 packet._data = NULL;
1285 dns_packet_unref(&packet);
1286
1287 return 0;
1288}
1289
97c67192
LP
1290int dns_resource_record_signer(DnsResourceRecord *rr, const char **ret) {
1291 const char *n;
1292 int r;
1293
1294 assert(rr);
1295 assert(ret);
1296
1297 /* Returns the RRset's signer, if it is known. */
1298
1299 if (rr->n_skip_labels_signer == (unsigned) -1)
1300 return -ENODATA;
1301
1302 n = DNS_RESOURCE_KEY_NAME(rr->key);
1303 r = dns_name_skip(n, rr->n_skip_labels_signer, &n);
1304 if (r < 0)
1305 return r;
1306 if (r == 0)
1307 return -EINVAL;
1308
1309 *ret = n;
1310 return 0;
1311}
1312
1313int dns_resource_record_source(DnsResourceRecord *rr, const char **ret) {
1314 const char *n;
1315 int r;
1316
1317 assert(rr);
1318 assert(ret);
1319
1320 /* Returns the RRset's synthesizing source, if it is known. */
1321
1322 if (rr->n_skip_labels_source == (unsigned) -1)
1323 return -ENODATA;
1324
1325 n = DNS_RESOURCE_KEY_NAME(rr->key);
1326 r = dns_name_skip(n, rr->n_skip_labels_source, &n);
1327 if (r < 0)
1328 return r;
1329 if (r == 0)
1330 return -EINVAL;
1331
1332 *ret = n;
1333 return 0;
1334}
1335
1336int dns_resource_record_is_signer(DnsResourceRecord *rr, const char *zone) {
1337 const char *signer;
1338 int r;
1339
ab481675
LP
1340 assert(rr);
1341
97c67192
LP
1342 r = dns_resource_record_signer(rr, &signer);
1343 if (r < 0)
1344 return r;
1345
1346 return dns_name_equal(zone, signer);
1347}
1348
ab481675
LP
1349int dns_resource_record_is_synthetic(DnsResourceRecord *rr) {
1350 int r;
1351
1352 assert(rr);
1353
1354 /* Returns > 0 if the RR is generated from a wildcard, and is not the asterisk name itself */
1355
1356 if (rr->n_skip_labels_source == (unsigned) -1)
1357 return -ENODATA;
1358
1359 if (rr->n_skip_labels_source == 0)
1360 return 0;
1361
1362 if (rr->n_skip_labels_source > 1)
1363 return 1;
1364
1365 r = dns_name_startswith(DNS_RESOURCE_KEY_NAME(rr->key), "*");
1366 if (r < 0)
1367 return r;
1368
1369 return !r;
1370}
1371
6d99904f 1372void dns_resource_record_hash_func(const void *i, struct siphash *state) {
c9c72065
LP
1373 const DnsResourceRecord *rr = i;
1374
1375 assert(rr);
1376
1377 dns_resource_key_hash_func(rr->key, state);
1378
1379 switch (rr->unparseable ? _DNS_TYPE_INVALID : rr->key->type) {
1380
1381 case DNS_TYPE_SRV:
1382 siphash24_compress(&rr->srv.priority, sizeof(rr->srv.priority), state);
1383 siphash24_compress(&rr->srv.weight, sizeof(rr->srv.weight), state);
1384 siphash24_compress(&rr->srv.port, sizeof(rr->srv.port), state);
1385 dns_name_hash_func(rr->srv.name, state);
1386 break;
1387
1388 case DNS_TYPE_PTR:
1389 case DNS_TYPE_NS:
1390 case DNS_TYPE_CNAME:
1391 case DNS_TYPE_DNAME:
1392 dns_name_hash_func(rr->ptr.name, state);
1393 break;
1394
1395 case DNS_TYPE_HINFO:
1396 string_hash_func(rr->hinfo.cpu, state);
1397 string_hash_func(rr->hinfo.os, state);
1398 break;
1399
1400 case DNS_TYPE_TXT:
1401 case DNS_TYPE_SPF: {
1402 DnsTxtItem *j;
1403
1404 LIST_FOREACH(items, j, rr->txt.items) {
1405 siphash24_compress(j->data, j->length, state);
1406
d5115566
LP
1407 /* Add an extra NUL byte, so that "a" followed by "b" doesn't result in the same hash as "ab"
1408 * followed by "". */
1409 siphash24_compress_byte(0, state);
c9c72065
LP
1410 }
1411 break;
1412 }
1413
1414 case DNS_TYPE_A:
1415 siphash24_compress(&rr->a.in_addr, sizeof(rr->a.in_addr), state);
1416 break;
1417
1418 case DNS_TYPE_AAAA:
1419 siphash24_compress(&rr->aaaa.in6_addr, sizeof(rr->aaaa.in6_addr), state);
1420 break;
1421
1422 case DNS_TYPE_SOA:
1423 dns_name_hash_func(rr->soa.mname, state);
1424 dns_name_hash_func(rr->soa.rname, state);
1425 siphash24_compress(&rr->soa.serial, sizeof(rr->soa.serial), state);
1426 siphash24_compress(&rr->soa.refresh, sizeof(rr->soa.refresh), state);
1427 siphash24_compress(&rr->soa.retry, sizeof(rr->soa.retry), state);
1428 siphash24_compress(&rr->soa.expire, sizeof(rr->soa.expire), state);
1429 siphash24_compress(&rr->soa.minimum, sizeof(rr->soa.minimum), state);
1430 break;
1431
1432 case DNS_TYPE_MX:
1433 siphash24_compress(&rr->mx.priority, sizeof(rr->mx.priority), state);
1434 dns_name_hash_func(rr->mx.exchange, state);
1435 break;
1436
1437 case DNS_TYPE_LOC:
1438 siphash24_compress(&rr->loc.version, sizeof(rr->loc.version), state);
1439 siphash24_compress(&rr->loc.size, sizeof(rr->loc.size), state);
1440 siphash24_compress(&rr->loc.horiz_pre, sizeof(rr->loc.horiz_pre), state);
1441 siphash24_compress(&rr->loc.vert_pre, sizeof(rr->loc.vert_pre), state);
1442 siphash24_compress(&rr->loc.latitude, sizeof(rr->loc.latitude), state);
1443 siphash24_compress(&rr->loc.longitude, sizeof(rr->loc.longitude), state);
1444 siphash24_compress(&rr->loc.altitude, sizeof(rr->loc.altitude), state);
1445 break;
1446
1447 case DNS_TYPE_SSHFP:
1448 siphash24_compress(&rr->sshfp.algorithm, sizeof(rr->sshfp.algorithm), state);
1449 siphash24_compress(&rr->sshfp.fptype, sizeof(rr->sshfp.fptype), state);
1450 siphash24_compress(rr->sshfp.fingerprint, rr->sshfp.fingerprint_size, state);
1451 break;
1452
1453 case DNS_TYPE_DNSKEY:
1454 siphash24_compress(&rr->dnskey.flags, sizeof(rr->dnskey.flags), state);
1455 siphash24_compress(&rr->dnskey.protocol, sizeof(rr->dnskey.protocol), state);
1456 siphash24_compress(&rr->dnskey.algorithm, sizeof(rr->dnskey.algorithm), state);
1457 siphash24_compress(rr->dnskey.key, rr->dnskey.key_size, state);
1458 break;
1459
1460 case DNS_TYPE_RRSIG:
1461 siphash24_compress(&rr->rrsig.type_covered, sizeof(rr->rrsig.type_covered), state);
1462 siphash24_compress(&rr->rrsig.algorithm, sizeof(rr->rrsig.algorithm), state);
1463 siphash24_compress(&rr->rrsig.labels, sizeof(rr->rrsig.labels), state);
1464 siphash24_compress(&rr->rrsig.original_ttl, sizeof(rr->rrsig.original_ttl), state);
1465 siphash24_compress(&rr->rrsig.expiration, sizeof(rr->rrsig.expiration), state);
1466 siphash24_compress(&rr->rrsig.inception, sizeof(rr->rrsig.inception), state);
1467 siphash24_compress(&rr->rrsig.key_tag, sizeof(rr->rrsig.key_tag), state);
1468 dns_name_hash_func(rr->rrsig.signer, state);
1469 siphash24_compress(rr->rrsig.signature, rr->rrsig.signature_size, state);
1470 break;
1471
1472 case DNS_TYPE_NSEC:
1473 dns_name_hash_func(rr->nsec.next_domain_name, state);
1474 /* FIXME: we leave out the type bitmap here. Hash
1475 * would be better if we'd take it into account
1476 * too. */
1477 break;
1478
1479 case DNS_TYPE_DS:
1480 siphash24_compress(&rr->ds.key_tag, sizeof(rr->ds.key_tag), state);
1481 siphash24_compress(&rr->ds.algorithm, sizeof(rr->ds.algorithm), state);
1482 siphash24_compress(&rr->ds.digest_type, sizeof(rr->ds.digest_type), state);
1483 siphash24_compress(rr->ds.digest, rr->ds.digest_size, state);
1484 break;
1485
1486 case DNS_TYPE_NSEC3:
1487 siphash24_compress(&rr->nsec3.algorithm, sizeof(rr->nsec3.algorithm), state);
1488 siphash24_compress(&rr->nsec3.flags, sizeof(rr->nsec3.flags), state);
1489 siphash24_compress(&rr->nsec3.iterations, sizeof(rr->nsec3.iterations), state);
1490 siphash24_compress(rr->nsec3.salt, rr->nsec3.salt_size, state);
1491 siphash24_compress(rr->nsec3.next_hashed_name, rr->nsec3.next_hashed_name_size, state);
1492 /* FIXME: We leave the bitmaps out */
1493 break;
48d45d2b
ZJS
1494
1495 case DNS_TYPE_TLSA:
1496 siphash24_compress(&rr->tlsa.cert_usage, sizeof(rr->tlsa.cert_usage), state);
1497 siphash24_compress(&rr->tlsa.selector, sizeof(rr->tlsa.selector), state);
1498 siphash24_compress(&rr->tlsa.matching_type, sizeof(rr->tlsa.matching_type), state);
fa45182e 1499 siphash24_compress(rr->tlsa.data, rr->tlsa.data_size, state);
48d45d2b 1500 break;
c9c72065 1501
95052df3
ZJS
1502 case DNS_TYPE_CAA:
1503 siphash24_compress(&rr->caa.flags, sizeof(rr->caa.flags), state);
1504 string_hash_func(rr->caa.tag, state);
1505 siphash24_compress(rr->caa.value, rr->caa.value_size, state);
48d45d2b 1506 break;
c9c72065 1507
d93a16b8 1508 case DNS_TYPE_OPENPGPKEY:
c9c72065 1509 default:
a43a068a 1510 siphash24_compress(rr->generic.data, rr->generic.data_size, state);
c9c72065
LP
1511 break;
1512 }
1513}
1514
1515static int dns_resource_record_compare_func(const void *a, const void *b) {
1516 const DnsResourceRecord *x = a, *y = b;
1517 int ret;
1518
1519 ret = dns_resource_key_compare_func(x->key, y->key);
1520 if (ret != 0)
1521 return ret;
1522
1523 if (dns_resource_record_equal(x, y))
1524 return 0;
1525
1f133e0d 1526 /* This is a bit dirty, we don't implement proper ordering, but
c9c72065
LP
1527 * the hashtable doesn't need ordering anyway, hence we don't
1528 * care. */
1529 return x < y ? -1 : 1;
1530}
1531
1532const struct hash_ops dns_resource_record_hash_ops = {
1533 .hash = dns_resource_record_hash_func,
1534 .compare = dns_resource_record_compare_func,
1535};
1536
2001c805
LP
1537DnsTxtItem *dns_txt_item_free_all(DnsTxtItem *i) {
1538 DnsTxtItem *n;
1539
1540 if (!i)
1541 return NULL;
1542
1543 n = i->items_next;
1544
1545 free(i);
1546 return dns_txt_item_free_all(n);
1547}
1548
1549bool dns_txt_item_equal(DnsTxtItem *a, DnsTxtItem *b) {
1550
4d247a6c
LP
1551 if (a == b)
1552 return true;
1553
2001c805
LP
1554 if (!a != !b)
1555 return false;
1556
1557 if (!a)
1558 return true;
1559
1560 if (a->length != b->length)
1561 return false;
1562
1563 if (memcmp(a->data, b->data, a->length) != 0)
1564 return false;
1565
1566 return dns_txt_item_equal(a->items_next, b->items_next);
1567}
8730bccf
LP
1568
1569static const char* const dnssec_algorithm_table[_DNSSEC_ALGORITHM_MAX_DEFINED] = {
6f717d08 1570 /* Mnemonics as listed on https://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml */
8730bccf
LP
1571 [DNSSEC_ALGORITHM_RSAMD5] = "RSAMD5",
1572 [DNSSEC_ALGORITHM_DH] = "DH",
1573 [DNSSEC_ALGORITHM_DSA] = "DSA",
1574 [DNSSEC_ALGORITHM_ECC] = "ECC",
1575 [DNSSEC_ALGORITHM_RSASHA1] = "RSASHA1",
1576 [DNSSEC_ALGORITHM_DSA_NSEC3_SHA1] = "DSA-NSEC3-SHA1",
1577 [DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1] = "RSASHA1-NSEC3-SHA1",
1578 [DNSSEC_ALGORITHM_RSASHA256] = "RSASHA256",
1579 [DNSSEC_ALGORITHM_RSASHA512] = "RSASHA512",
6f717d08
LP
1580 [DNSSEC_ALGORITHM_ECC_GOST] = "ECC-GOST",
1581 [DNSSEC_ALGORITHM_ECDSAP256SHA256] = "ECDSAP256SHA256",
1582 [DNSSEC_ALGORITHM_ECDSAP384SHA384] = "ECDSAP384SHA384",
8730bccf
LP
1583 [DNSSEC_ALGORITHM_INDIRECT] = "INDIRECT",
1584 [DNSSEC_ALGORITHM_PRIVATEDNS] = "PRIVATEDNS",
1585 [DNSSEC_ALGORITHM_PRIVATEOID] = "PRIVATEOID",
1586};
8e54f5d9 1587DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(dnssec_algorithm, int, 255);
8730bccf
LP
1588
1589static const char* const dnssec_digest_table[_DNSSEC_DIGEST_MAX_DEFINED] = {
6f717d08
LP
1590 /* Names as listed on https://www.iana.org/assignments/ds-rr-types/ds-rr-types.xhtml */
1591 [DNSSEC_DIGEST_SHA1] = "SHA-1",
1592 [DNSSEC_DIGEST_SHA256] = "SHA-256",
1593 [DNSSEC_DIGEST_GOST_R_34_11_94] = "GOST_R_34.11-94",
1594 [DNSSEC_DIGEST_SHA384] = "SHA-384",
8730bccf 1595};
8e54f5d9 1596DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(dnssec_digest, int, 255);