]>
Commit | Line | Data |
---|---|---|
74b2466e LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | #pragma once | |
4 | ||
5 | /*** | |
6 | This file is part of systemd. | |
7 | ||
8 | Copyright 2014 Lennart Poettering | |
9 | ||
10 | systemd is free software; you can redistribute it and/or modify it | |
11 | under the terms of the GNU Lesser General Public License as published by | |
12 | the Free Software Foundation; either version 2.1 of the License, or | |
13 | (at your option) any later version. | |
14 | ||
15 | systemd is distributed in the hope that it will be useful, but | |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 | Lesser General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU Lesser General Public License | |
21 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
22 | ***/ | |
23 | ||
74b2466e LP |
24 | #include <netinet/in.h> |
25 | ||
50f1e641 | 26 | #include "bitmap.h" |
71d35b6b | 27 | #include "dns-type.h" |
322345fd | 28 | #include "hashmap.h" |
623a4c97 | 29 | #include "in-addr-util.h" |
2001c805 | 30 | #include "list.h" |
74b2466e LP |
31 | |
32 | typedef struct DnsResourceKey DnsResourceKey; | |
33 | typedef struct DnsResourceRecord DnsResourceRecord; | |
2001c805 | 34 | typedef struct DnsTxtItem DnsTxtItem; |
74b2466e | 35 | |
8730bccf | 36 | /* DNSKEY RR flags */ |
8730bccf | 37 | #define DNSKEY_FLAG_SEP (UINT16_C(1) << 0) |
28b8191e LP |
38 | #define DNSKEY_FLAG_REVOKE (UINT16_C(1) << 7) |
39 | #define DNSKEY_FLAG_ZONE_KEY (UINT16_C(1) << 8) | |
8730bccf | 40 | |
23502de3 DM |
41 | /* mDNS RR flags */ |
42 | #define MDNS_RR_CACHE_FLUSH (UINT16_C(1) << 15) | |
43 | ||
8730bccf LP |
44 | /* DNSSEC algorithm identifiers, see |
45 | * http://tools.ietf.org/html/rfc4034#appendix-A.1 and | |
46 | * https://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml */ | |
47 | enum { | |
48 | DNSSEC_ALGORITHM_RSAMD5 = 1, | |
49 | DNSSEC_ALGORITHM_DH, | |
50 | DNSSEC_ALGORITHM_DSA, | |
51 | DNSSEC_ALGORITHM_ECC, | |
52 | DNSSEC_ALGORITHM_RSASHA1, | |
53 | DNSSEC_ALGORITHM_DSA_NSEC3_SHA1, | |
54 | DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1, | |
6f717d08 LP |
55 | DNSSEC_ALGORITHM_RSASHA256 = 8, /* RFC 5702 */ |
56 | DNSSEC_ALGORITHM_RSASHA512 = 10, /* RFC 5702 */ | |
57 | DNSSEC_ALGORITHM_ECC_GOST = 12, /* RFC 5933 */ | |
e0240c64 LP |
58 | DNSSEC_ALGORITHM_ECDSAP256SHA256 = 13, /* RFC 6605 */ |
59 | DNSSEC_ALGORITHM_ECDSAP384SHA384 = 14, /* RFC 6605 */ | |
8730bccf LP |
60 | DNSSEC_ALGORITHM_INDIRECT = 252, |
61 | DNSSEC_ALGORITHM_PRIVATEDNS, | |
62 | DNSSEC_ALGORITHM_PRIVATEOID, | |
63 | _DNSSEC_ALGORITHM_MAX_DEFINED | |
64 | }; | |
65 | ||
66 | /* DNSSEC digest identifiers, see | |
67 | * https://www.iana.org/assignments/ds-rr-types/ds-rr-types.xhtml */ | |
68 | enum { | |
69 | DNSSEC_DIGEST_SHA1 = 1, | |
6f717d08 LP |
70 | DNSSEC_DIGEST_SHA256 = 2, /* RFC 4509 */ |
71 | DNSSEC_DIGEST_GOST_R_34_11_94 = 3, /* RFC 5933 */ | |
72 | DNSSEC_DIGEST_SHA384 = 4, /* RFC 6605 */ | |
8730bccf LP |
73 | _DNSSEC_DIGEST_MAX_DEFINED |
74 | }; | |
75 | ||
d15ad742 LP |
76 | /* DNSSEC NSEC3 hash algorithms, see |
77 | * https://www.iana.org/assignments/dnssec-nsec3-parameters/dnssec-nsec3-parameters.xhtml */ | |
78 | enum { | |
79 | NSEC3_ALGORITHM_SHA1 = 1, | |
80 | _NSEC3_ALGORITHM_MAX_DEFINED | |
81 | }; | |
82 | ||
74b2466e | 83 | struct DnsResourceKey { |
faa133f3 LP |
84 | unsigned n_ref; |
85 | uint16_t class, type; | |
86 | char *_name; /* don't access directy, use DNS_RESOURCE_KEY_NAME()! */ | |
74b2466e LP |
87 | }; |
88 | ||
1b4f6e79 LP |
89 | /* Creates a temporary resource key. This is only useful to quickly |
90 | * look up something, without allocating a full DnsResourceKey object | |
91 | * for it. Note that it is not OK to take references to this kind of | |
92 | * resource key object. */ | |
93 | #define DNS_RESOURCE_KEY_CONST(c, t, n) \ | |
94 | ((DnsResourceKey) { \ | |
95 | .n_ref = (unsigned) -1, \ | |
96 | .class = c, \ | |
97 | .type = t, \ | |
98 | ._name = (char*) n, \ | |
99 | }) | |
100 | ||
101 | ||
2001c805 LP |
102 | struct DnsTxtItem { |
103 | size_t length; | |
104 | LIST_FIELDS(DnsTxtItem, items); | |
105 | uint8_t data[]; | |
106 | }; | |
107 | ||
74b2466e LP |
108 | struct DnsResourceRecord { |
109 | unsigned n_ref; | |
faa133f3 | 110 | DnsResourceKey *key; |
7b50eb2e | 111 | char *to_string; |
74b2466e | 112 | uint32_t ttl; |
ee3d6aff | 113 | usec_t expiry; /* RRSIG signature expiry */ |
a8812dd7 LP |
114 | bool unparseable:1; |
115 | bool wire_format_canonical:1; | |
116 | void *wire_format; | |
117 | size_t wire_format_size; | |
118 | size_t wire_format_rdata_offset; | |
74b2466e LP |
119 | union { |
120 | struct { | |
121 | void *data; | |
f5430a3e | 122 | size_t size; |
d75acfb0 | 123 | } generic, opt; |
74b2466e | 124 | |
9c92ce6d LP |
125 | struct { |
126 | uint16_t priority; | |
127 | uint16_t weight; | |
128 | uint16_t port; | |
129 | char *name; | |
130 | } srv; | |
74b2466e LP |
131 | |
132 | struct { | |
133 | char *name; | |
8ac4e9e1 | 134 | } ptr, ns, cname, dname; |
74b2466e LP |
135 | |
136 | struct { | |
137 | char *cpu; | |
138 | char *os; | |
139 | } hinfo; | |
140 | ||
2e276efc | 141 | struct { |
2001c805 | 142 | DnsTxtItem *items; |
c0eb11cf | 143 | } txt, spf; |
74b2466e LP |
144 | |
145 | struct { | |
146 | struct in_addr in_addr; | |
147 | } a; | |
148 | ||
149 | struct { | |
150 | struct in6_addr in6_addr; | |
151 | } aaaa; | |
7e8e0422 LP |
152 | |
153 | struct { | |
154 | char *mname; | |
155 | char *rname; | |
156 | uint32_t serial; | |
157 | uint32_t refresh; | |
158 | uint32_t retry; | |
159 | uint32_t expire; | |
160 | uint32_t minimum; | |
161 | } soa; | |
946c7094 ZJS |
162 | |
163 | struct { | |
164 | uint16_t priority; | |
165 | char *exchange; | |
166 | } mx; | |
0dae31d4 | 167 | |
6af47493 | 168 | /* https://tools.ietf.org/html/rfc1876 */ |
0dae31d4 ZJS |
169 | struct { |
170 | uint8_t version; | |
171 | uint8_t size; | |
172 | uint8_t horiz_pre; | |
173 | uint8_t vert_pre; | |
174 | uint32_t latitude; | |
175 | uint32_t longitude; | |
176 | uint32_t altitude; | |
177 | } loc; | |
42cc2eeb | 178 | |
549c1a25 | 179 | /* https://tools.ietf.org/html/rfc4255#section-3.1 */ |
42cc2eeb LP |
180 | struct { |
181 | uint8_t algorithm; | |
182 | uint8_t fptype; | |
549c1a25 TG |
183 | void *fingerprint; |
184 | size_t fingerprint_size; | |
42cc2eeb | 185 | } sshfp; |
8db0d2f5 ZJS |
186 | |
187 | /* http://tools.ietf.org/html/rfc4034#section-2.1 */ | |
188 | struct { | |
f91dc240 LP |
189 | uint16_t flags; |
190 | uint8_t protocol; | |
8db0d2f5 ZJS |
191 | uint8_t algorithm; |
192 | void* key; | |
193 | size_t key_size; | |
194 | } dnskey; | |
151226ab ZJS |
195 | |
196 | /* http://tools.ietf.org/html/rfc4034#section-3.1 */ | |
197 | struct { | |
198 | uint16_t type_covered; | |
199 | uint8_t algorithm; | |
200 | uint8_t labels; | |
201 | uint32_t original_ttl; | |
202 | uint32_t expiration; | |
203 | uint32_t inception; | |
204 | uint16_t key_tag; | |
205 | char *signer; | |
206 | void *signature; | |
207 | size_t signature_size; | |
208 | } rrsig; | |
50f1e641 | 209 | |
9ead3519 | 210 | /* https://tools.ietf.org/html/rfc4034#section-4.1 */ |
50f1e641 TG |
211 | struct { |
212 | char *next_domain_name; | |
213 | Bitmap *types; | |
214 | } nsec; | |
5d45a880 | 215 | |
6af47493 LP |
216 | /* https://tools.ietf.org/html/rfc4034#section-5.1 */ |
217 | struct { | |
218 | uint16_t key_tag; | |
219 | uint8_t algorithm; | |
220 | uint8_t digest_type; | |
221 | void *digest; | |
222 | size_t digest_size; | |
223 | } ds; | |
224 | ||
5d45a880 TG |
225 | struct { |
226 | uint8_t algorithm; | |
227 | uint8_t flags; | |
228 | uint16_t iterations; | |
229 | void *salt; | |
230 | size_t salt_size; | |
231 | void *next_hashed_name; | |
232 | size_t next_hashed_name_size; | |
233 | Bitmap *types; | |
234 | } nsec3; | |
74b2466e LP |
235 | }; |
236 | }; | |
237 | ||
faa133f3 | 238 | static inline const char* DNS_RESOURCE_KEY_NAME(const DnsResourceKey *key) { |
85aeaccc | 239 | if (!key) |
faa133f3 LP |
240 | return NULL; |
241 | ||
242 | if (key->_name) | |
243 | return key->_name; | |
244 | ||
245 | return (char*) key + sizeof(DnsResourceKey); | |
246 | } | |
74b2466e | 247 | |
85aeaccc LP |
248 | static inline const void* DNS_RESOURCE_RECORD_RDATA(DnsResourceRecord *rr) { |
249 | if (!rr) | |
250 | return NULL; | |
251 | ||
252 | if (!rr->wire_format) | |
253 | return NULL; | |
254 | ||
255 | assert(rr->wire_format_rdata_offset <= rr->wire_format_size); | |
256 | return (uint8_t*) rr->wire_format + rr->wire_format_rdata_offset; | |
257 | } | |
258 | ||
259 | static inline size_t DNS_RESOURCE_RECORD_RDATA_SIZE(DnsResourceRecord *rr) { | |
260 | if (!rr) | |
261 | return 0; | |
262 | if (!rr->wire_format) | |
263 | return 0; | |
264 | ||
265 | assert(rr->wire_format_rdata_offset <= rr->wire_format_size); | |
266 | return rr->wire_format_size - rr->wire_format_rdata_offset; | |
267 | } | |
268 | ||
faa133f3 | 269 | DnsResourceKey* dns_resource_key_new(uint16_t class, uint16_t type, const char *name); |
36d9205d | 270 | DnsResourceKey* dns_resource_key_new_redirect(const DnsResourceKey *key, const DnsResourceRecord *cname); |
801ad6a6 | 271 | int dns_resource_key_new_append_suffix(DnsResourceKey **ret, DnsResourceKey *key, char *name); |
faa133f3 LP |
272 | DnsResourceKey* dns_resource_key_new_consume(uint16_t class, uint16_t type, char *name); |
273 | DnsResourceKey* dns_resource_key_ref(DnsResourceKey *key); | |
274 | DnsResourceKey* dns_resource_key_unref(DnsResourceKey *key); | |
28b9b764 | 275 | bool dns_resource_key_is_address(const DnsResourceKey *key); |
faa133f3 | 276 | int dns_resource_key_equal(const DnsResourceKey *a, const DnsResourceKey *b); |
105e1512 | 277 | int dns_resource_key_match_rr(const DnsResourceKey *key, DnsResourceRecord *rr, const char *search_domain); |
5d27351f | 278 | int dns_resource_key_match_cname_or_dname(const DnsResourceKey *key, const DnsResourceKey *cname, const char *search_domain); |
547973de | 279 | int dns_resource_key_match_soa(const DnsResourceKey *key, const DnsResourceKey *soa); |
2d4c5cbc | 280 | int dns_resource_key_to_string(const DnsResourceKey *key, char **ret); |
faa133f3 | 281 | DEFINE_TRIVIAL_CLEANUP_FUNC(DnsResourceKey*, dns_resource_key_unref); |
322345fd | 282 | |
7778dfff DM |
283 | static inline bool dns_key_is_shared(const DnsResourceKey *key) { |
284 | return IN_SET(key->type, DNS_TYPE_PTR); | |
285 | } | |
286 | ||
faa133f3 | 287 | DnsResourceRecord* dns_resource_record_new(DnsResourceKey *key); |
8bf52d3d | 288 | DnsResourceRecord* dns_resource_record_new_full(uint16_t class, uint16_t type, const char *name); |
74b2466e LP |
289 | DnsResourceRecord* dns_resource_record_ref(DnsResourceRecord *rr); |
290 | DnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr); | |
623a4c97 | 291 | int dns_resource_record_new_reverse(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *name); |
78c6a153 | 292 | int dns_resource_record_new_address(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *name); |
322345fd | 293 | int dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b); |
7b50eb2e | 294 | const char* dns_resource_record_to_string(DnsResourceRecord *rr); |
faa133f3 | 295 | DEFINE_TRIVIAL_CLEANUP_FUNC(DnsResourceRecord*, dns_resource_record_unref); |
322345fd | 296 | |
a8812dd7 LP |
297 | int dns_resource_record_to_wire_format(DnsResourceRecord *rr, bool canonical); |
298 | ||
2001c805 LP |
299 | DnsTxtItem *dns_txt_item_free_all(DnsTxtItem *i); |
300 | bool dns_txt_item_equal(DnsTxtItem *a, DnsTxtItem *b); | |
301 | ||
d5099efc | 302 | extern const struct hash_ops dns_resource_key_hash_ops; |
c9c72065 | 303 | extern const struct hash_ops dns_resource_record_hash_ops; |
8730bccf | 304 | |
8e54f5d9 | 305 | int dnssec_algorithm_to_string_alloc(int i, char **ret); |
8730bccf LP |
306 | int dnssec_algorithm_from_string(const char *s) _pure_; |
307 | ||
8e54f5d9 | 308 | int dnssec_digest_to_string_alloc(int i, char **ret); |
8730bccf | 309 | int dnssec_digest_from_string(const char *s) _pure_; |