]>
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 LP |
36 | /* DNSKEY RR flags */ |
37 | #define DNSKEY_FLAG_ZONE_KEY (UINT16_C(1) << 8) | |
38 | #define DNSKEY_FLAG_SEP (UINT16_C(1) << 0) | |
39 | ||
23502de3 DM |
40 | /* mDNS RR flags */ |
41 | #define MDNS_RR_CACHE_FLUSH (UINT16_C(1) << 15) | |
42 | ||
8730bccf LP |
43 | /* DNSSEC algorithm identifiers, see |
44 | * http://tools.ietf.org/html/rfc4034#appendix-A.1 and | |
45 | * https://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml */ | |
46 | enum { | |
47 | DNSSEC_ALGORITHM_RSAMD5 = 1, | |
48 | DNSSEC_ALGORITHM_DH, | |
49 | DNSSEC_ALGORITHM_DSA, | |
50 | DNSSEC_ALGORITHM_ECC, | |
51 | DNSSEC_ALGORITHM_RSASHA1, | |
52 | DNSSEC_ALGORITHM_DSA_NSEC3_SHA1, | |
53 | DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1, | |
54 | DNSSEC_ALGORITHM_RSASHA256 = 8, /* RFC 5702 */ | |
55 | DNSSEC_ALGORITHM_RSASHA512 = 10, /* RFC 5702 */ | |
56 | DNSSEC_ALGORITHM_INDIRECT = 252, | |
57 | DNSSEC_ALGORITHM_PRIVATEDNS, | |
58 | DNSSEC_ALGORITHM_PRIVATEOID, | |
59 | _DNSSEC_ALGORITHM_MAX_DEFINED | |
60 | }; | |
61 | ||
62 | /* DNSSEC digest identifiers, see | |
63 | * https://www.iana.org/assignments/ds-rr-types/ds-rr-types.xhtml */ | |
64 | enum { | |
65 | DNSSEC_DIGEST_SHA1 = 1, | |
66 | DNSSEC_DIGEST_SHA256 = 2, | |
67 | _DNSSEC_DIGEST_MAX_DEFINED | |
68 | }; | |
69 | ||
74b2466e | 70 | struct DnsResourceKey { |
faa133f3 LP |
71 | unsigned n_ref; |
72 | uint16_t class, type; | |
73 | char *_name; /* don't access directy, use DNS_RESOURCE_KEY_NAME()! */ | |
74b2466e LP |
74 | }; |
75 | ||
1b4f6e79 LP |
76 | /* Creates a temporary resource key. This is only useful to quickly |
77 | * look up something, without allocating a full DnsResourceKey object | |
78 | * for it. Note that it is not OK to take references to this kind of | |
79 | * resource key object. */ | |
80 | #define DNS_RESOURCE_KEY_CONST(c, t, n) \ | |
81 | ((DnsResourceKey) { \ | |
82 | .n_ref = (unsigned) -1, \ | |
83 | .class = c, \ | |
84 | .type = t, \ | |
85 | ._name = (char*) n, \ | |
86 | }) | |
87 | ||
88 | ||
2001c805 LP |
89 | struct DnsTxtItem { |
90 | size_t length; | |
91 | LIST_FIELDS(DnsTxtItem, items); | |
92 | uint8_t data[]; | |
93 | }; | |
94 | ||
74b2466e LP |
95 | struct DnsResourceRecord { |
96 | unsigned n_ref; | |
faa133f3 | 97 | DnsResourceKey *key; |
7b50eb2e | 98 | char *to_string; |
74b2466e | 99 | uint32_t ttl; |
a8812dd7 LP |
100 | bool unparseable:1; |
101 | bool wire_format_canonical:1; | |
102 | void *wire_format; | |
103 | size_t wire_format_size; | |
104 | size_t wire_format_rdata_offset; | |
74b2466e LP |
105 | union { |
106 | struct { | |
107 | void *data; | |
f5430a3e | 108 | size_t size; |
d75acfb0 | 109 | } generic, opt; |
74b2466e | 110 | |
9c92ce6d LP |
111 | struct { |
112 | uint16_t priority; | |
113 | uint16_t weight; | |
114 | uint16_t port; | |
115 | char *name; | |
116 | } srv; | |
74b2466e LP |
117 | |
118 | struct { | |
119 | char *name; | |
8ac4e9e1 | 120 | } ptr, ns, cname, dname; |
74b2466e LP |
121 | |
122 | struct { | |
123 | char *cpu; | |
124 | char *os; | |
125 | } hinfo; | |
126 | ||
2e276efc | 127 | struct { |
2001c805 | 128 | DnsTxtItem *items; |
c0eb11cf | 129 | } txt, spf; |
74b2466e LP |
130 | |
131 | struct { | |
132 | struct in_addr in_addr; | |
133 | } a; | |
134 | ||
135 | struct { | |
136 | struct in6_addr in6_addr; | |
137 | } aaaa; | |
7e8e0422 LP |
138 | |
139 | struct { | |
140 | char *mname; | |
141 | char *rname; | |
142 | uint32_t serial; | |
143 | uint32_t refresh; | |
144 | uint32_t retry; | |
145 | uint32_t expire; | |
146 | uint32_t minimum; | |
147 | } soa; | |
946c7094 ZJS |
148 | |
149 | struct { | |
150 | uint16_t priority; | |
151 | char *exchange; | |
152 | } mx; | |
0dae31d4 ZJS |
153 | |
154 | struct { | |
155 | uint8_t version; | |
156 | uint8_t size; | |
157 | uint8_t horiz_pre; | |
158 | uint8_t vert_pre; | |
159 | uint32_t latitude; | |
160 | uint32_t longitude; | |
161 | uint32_t altitude; | |
162 | } loc; | |
42cc2eeb | 163 | |
abf126a3 TG |
164 | struct { |
165 | uint16_t key_tag; | |
166 | uint8_t algorithm; | |
167 | uint8_t digest_type; | |
168 | void *digest; | |
169 | size_t digest_size; | |
170 | } ds; | |
171 | ||
549c1a25 | 172 | /* https://tools.ietf.org/html/rfc4255#section-3.1 */ |
42cc2eeb LP |
173 | struct { |
174 | uint8_t algorithm; | |
175 | uint8_t fptype; | |
549c1a25 TG |
176 | void *fingerprint; |
177 | size_t fingerprint_size; | |
42cc2eeb | 178 | } sshfp; |
8db0d2f5 ZJS |
179 | |
180 | /* http://tools.ietf.org/html/rfc4034#section-2.1 */ | |
181 | struct { | |
f91dc240 LP |
182 | uint16_t flags; |
183 | uint8_t protocol; | |
8db0d2f5 ZJS |
184 | uint8_t algorithm; |
185 | void* key; | |
186 | size_t key_size; | |
187 | } dnskey; | |
151226ab ZJS |
188 | |
189 | /* http://tools.ietf.org/html/rfc4034#section-3.1 */ | |
190 | struct { | |
191 | uint16_t type_covered; | |
192 | uint8_t algorithm; | |
193 | uint8_t labels; | |
194 | uint32_t original_ttl; | |
195 | uint32_t expiration; | |
196 | uint32_t inception; | |
197 | uint16_t key_tag; | |
198 | char *signer; | |
199 | void *signature; | |
200 | size_t signature_size; | |
201 | } rrsig; | |
50f1e641 | 202 | |
9ead3519 | 203 | /* https://tools.ietf.org/html/rfc4034#section-4.1 */ |
50f1e641 TG |
204 | struct { |
205 | char *next_domain_name; | |
206 | Bitmap *types; | |
207 | } nsec; | |
5d45a880 TG |
208 | |
209 | struct { | |
210 | uint8_t algorithm; | |
211 | uint8_t flags; | |
212 | uint16_t iterations; | |
213 | void *salt; | |
214 | size_t salt_size; | |
215 | void *next_hashed_name; | |
216 | size_t next_hashed_name_size; | |
217 | Bitmap *types; | |
218 | } nsec3; | |
74b2466e LP |
219 | }; |
220 | }; | |
221 | ||
faa133f3 LP |
222 | static inline const char* DNS_RESOURCE_KEY_NAME(const DnsResourceKey *key) { |
223 | if (_unlikely_(!key)) | |
224 | return NULL; | |
225 | ||
226 | if (key->_name) | |
227 | return key->_name; | |
228 | ||
229 | return (char*) key + sizeof(DnsResourceKey); | |
230 | } | |
74b2466e | 231 | |
faa133f3 | 232 | DnsResourceKey* dns_resource_key_new(uint16_t class, uint16_t type, const char *name); |
36d9205d | 233 | DnsResourceKey* dns_resource_key_new_redirect(const DnsResourceKey *key, const DnsResourceRecord *cname); |
801ad6a6 | 234 | int dns_resource_key_new_append_suffix(DnsResourceKey **ret, DnsResourceKey *key, char *name); |
faa133f3 LP |
235 | DnsResourceKey* dns_resource_key_new_consume(uint16_t class, uint16_t type, char *name); |
236 | DnsResourceKey* dns_resource_key_ref(DnsResourceKey *key); | |
237 | DnsResourceKey* dns_resource_key_unref(DnsResourceKey *key); | |
28b9b764 | 238 | bool dns_resource_key_is_address(const DnsResourceKey *key); |
faa133f3 | 239 | int dns_resource_key_equal(const DnsResourceKey *a, const DnsResourceKey *b); |
105e1512 | 240 | int dns_resource_key_match_rr(const DnsResourceKey *key, DnsResourceRecord *rr, const char *search_domain); |
5d27351f | 241 | int dns_resource_key_match_cname_or_dname(const DnsResourceKey *key, const DnsResourceKey *cname, const char *search_domain); |
547973de | 242 | int dns_resource_key_match_soa(const DnsResourceKey *key, const DnsResourceKey *soa); |
2d4c5cbc | 243 | int dns_resource_key_to_string(const DnsResourceKey *key, char **ret); |
faa133f3 | 244 | DEFINE_TRIVIAL_CLEANUP_FUNC(DnsResourceKey*, dns_resource_key_unref); |
322345fd | 245 | |
7778dfff DM |
246 | static inline bool dns_key_is_shared(const DnsResourceKey *key) { |
247 | return IN_SET(key->type, DNS_TYPE_PTR); | |
248 | } | |
249 | ||
faa133f3 | 250 | DnsResourceRecord* dns_resource_record_new(DnsResourceKey *key); |
8bf52d3d | 251 | DnsResourceRecord* dns_resource_record_new_full(uint16_t class, uint16_t type, const char *name); |
74b2466e LP |
252 | DnsResourceRecord* dns_resource_record_ref(DnsResourceRecord *rr); |
253 | DnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr); | |
623a4c97 | 254 | int dns_resource_record_new_reverse(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *name); |
78c6a153 | 255 | int dns_resource_record_new_address(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *name); |
322345fd | 256 | int dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b); |
7b50eb2e | 257 | const char* dns_resource_record_to_string(DnsResourceRecord *rr); |
faa133f3 | 258 | DEFINE_TRIVIAL_CLEANUP_FUNC(DnsResourceRecord*, dns_resource_record_unref); |
322345fd | 259 | |
a8812dd7 LP |
260 | int dns_resource_record_to_wire_format(DnsResourceRecord *rr, bool canonical); |
261 | ||
2001c805 LP |
262 | DnsTxtItem *dns_txt_item_free_all(DnsTxtItem *i); |
263 | bool dns_txt_item_equal(DnsTxtItem *a, DnsTxtItem *b); | |
264 | ||
d5099efc | 265 | extern const struct hash_ops dns_resource_key_hash_ops; |
8730bccf LP |
266 | |
267 | const char* dnssec_algorithm_to_string(int i) _const_; | |
268 | int dnssec_algorithm_from_string(const char *s) _pure_; | |
269 | ||
270 | const char *dnssec_digest_to_string(int i) _const_; | |
271 | int dnssec_digest_from_string(const char *s) _pure_; |