xref: /netbsd-src/external/mpl/bind/dist/lib/dns/rdata/generic/mx_15.c (revision cef8759bd76c1b621f8eab8faa6f208faabc2e15)
1 /*	$NetBSD: mx_15.c,v 1.5 2020/05/24 19:46:24 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * See the COPYRIGHT file distributed with this work for additional
11  * information regarding copyright ownership.
12  */
13 
14 #ifndef RDATA_GENERIC_MX_15_C
15 #define RDATA_GENERIC_MX_15_C
16 
17 #include <string.h>
18 
19 #include <isc/net.h>
20 
21 #include <dns/fixedname.h>
22 
23 #define RRTYPE_MX_ATTRIBUTES (0)
24 
25 static bool
26 check_mx(isc_token_t *token) {
27 	char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:123.123.123.123.")];
28 	struct in_addr addr;
29 	struct in6_addr addr6;
30 
31 	if (strlcpy(tmp, DNS_AS_STR(*token), sizeof(tmp)) >= sizeof(tmp)) {
32 		return (true);
33 	}
34 
35 	if (tmp[strlen(tmp) - 1] == '.') {
36 		tmp[strlen(tmp) - 1] = '\0';
37 	}
38 	if (inet_pton(AF_INET, tmp, &addr) == 1 ||
39 	    inet_pton(AF_INET6, tmp, &addr6) == 1)
40 	{
41 		return (false);
42 	}
43 
44 	return (true);
45 }
46 
47 static inline isc_result_t
48 fromtext_mx(ARGS_FROMTEXT) {
49 	isc_token_t token;
50 	dns_name_t name;
51 	isc_buffer_t buffer;
52 	bool ok;
53 
54 	REQUIRE(type == dns_rdatatype_mx);
55 
56 	UNUSED(type);
57 	UNUSED(rdclass);
58 
59 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
60 				      false));
61 	if (token.value.as_ulong > 0xffffU) {
62 		RETTOK(ISC_R_RANGE);
63 	}
64 	RETERR(uint16_tobuffer(token.value.as_ulong, target));
65 
66 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
67 				      false));
68 
69 	ok = true;
70 	if ((options & DNS_RDATA_CHECKMX) != 0) {
71 		ok = check_mx(&token);
72 	}
73 	if (!ok && (options & DNS_RDATA_CHECKMXFAIL) != 0) {
74 		RETTOK(DNS_R_MXISADDRESS);
75 	}
76 	if (!ok && callbacks != NULL) {
77 		warn_badmx(&token, lexer, callbacks);
78 	}
79 
80 	dns_name_init(&name, NULL);
81 	buffer_fromregion(&buffer, &token.value.as_region);
82 	if (origin == NULL) {
83 		origin = dns_rootname;
84 	}
85 	RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
86 	ok = true;
87 	if ((options & DNS_RDATA_CHECKNAMES) != 0) {
88 		ok = dns_name_ishostname(&name, false);
89 	}
90 	if (!ok && (options & DNS_RDATA_CHECKNAMESFAIL) != 0) {
91 		RETTOK(DNS_R_BADNAME);
92 	}
93 	if (!ok && callbacks != NULL) {
94 		warn_badname(&name, lexer, callbacks);
95 	}
96 	return (ISC_R_SUCCESS);
97 }
98 
99 static inline isc_result_t
100 totext_mx(ARGS_TOTEXT) {
101 	isc_region_t region;
102 	dns_name_t name;
103 	dns_name_t prefix;
104 	bool sub;
105 	char buf[sizeof("64000")];
106 	unsigned short num;
107 
108 	REQUIRE(rdata->type == dns_rdatatype_mx);
109 	REQUIRE(rdata->length != 0);
110 
111 	dns_name_init(&name, NULL);
112 	dns_name_init(&prefix, NULL);
113 
114 	dns_rdata_toregion(rdata, &region);
115 	num = uint16_fromregion(&region);
116 	isc_region_consume(&region, 2);
117 	snprintf(buf, sizeof(buf), "%u", num);
118 	RETERR(str_totext(buf, target));
119 
120 	RETERR(str_totext(" ", target));
121 
122 	dns_name_fromregion(&name, &region);
123 	sub = name_prefix(&name, tctx->origin, &prefix);
124 	return (dns_name_totext(&prefix, sub, target));
125 }
126 
127 static inline isc_result_t
128 fromwire_mx(ARGS_FROMWIRE) {
129 	dns_name_t name;
130 	isc_region_t sregion;
131 
132 	REQUIRE(type == dns_rdatatype_mx);
133 
134 	UNUSED(type);
135 	UNUSED(rdclass);
136 
137 	dns_decompress_setmethods(dctx, DNS_COMPRESS_GLOBAL14);
138 
139 	dns_name_init(&name, NULL);
140 
141 	isc_buffer_activeregion(source, &sregion);
142 	if (sregion.length < 2) {
143 		return (ISC_R_UNEXPECTEDEND);
144 	}
145 	RETERR(mem_tobuffer(target, sregion.base, 2));
146 	isc_buffer_forward(source, 2);
147 	return (dns_name_fromwire(&name, source, dctx, options, target));
148 }
149 
150 static inline isc_result_t
151 towire_mx(ARGS_TOWIRE) {
152 	dns_name_t name;
153 	dns_offsets_t offsets;
154 	isc_region_t region;
155 
156 	REQUIRE(rdata->type == dns_rdatatype_mx);
157 	REQUIRE(rdata->length != 0);
158 
159 	dns_compress_setmethods(cctx, DNS_COMPRESS_GLOBAL14);
160 
161 	dns_rdata_toregion(rdata, &region);
162 	RETERR(mem_tobuffer(target, region.base, 2));
163 	isc_region_consume(&region, 2);
164 
165 	dns_name_init(&name, offsets);
166 	dns_name_fromregion(&name, &region);
167 
168 	return (dns_name_towire(&name, cctx, target));
169 }
170 
171 static inline int
172 compare_mx(ARGS_COMPARE) {
173 	dns_name_t name1;
174 	dns_name_t name2;
175 	isc_region_t region1;
176 	isc_region_t region2;
177 	int order;
178 
179 	REQUIRE(rdata1->type == rdata2->type);
180 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
181 	REQUIRE(rdata1->type == dns_rdatatype_mx);
182 	REQUIRE(rdata1->length != 0);
183 	REQUIRE(rdata2->length != 0);
184 
185 	order = memcmp(rdata1->data, rdata2->data, 2);
186 	if (order != 0) {
187 		return (order < 0 ? -1 : 1);
188 	}
189 
190 	dns_name_init(&name1, NULL);
191 	dns_name_init(&name2, NULL);
192 
193 	dns_rdata_toregion(rdata1, &region1);
194 	dns_rdata_toregion(rdata2, &region2);
195 
196 	isc_region_consume(&region1, 2);
197 	isc_region_consume(&region2, 2);
198 
199 	dns_name_fromregion(&name1, &region1);
200 	dns_name_fromregion(&name2, &region2);
201 
202 	return (dns_name_rdatacompare(&name1, &name2));
203 }
204 
205 static inline isc_result_t
206 fromstruct_mx(ARGS_FROMSTRUCT) {
207 	dns_rdata_mx_t *mx = source;
208 	isc_region_t region;
209 
210 	REQUIRE(type == dns_rdatatype_mx);
211 	REQUIRE(mx != NULL);
212 	REQUIRE(mx->common.rdtype == type);
213 	REQUIRE(mx->common.rdclass == rdclass);
214 
215 	UNUSED(type);
216 	UNUSED(rdclass);
217 
218 	RETERR(uint16_tobuffer(mx->pref, target));
219 	dns_name_toregion(&mx->mx, &region);
220 	return (isc_buffer_copyregion(target, &region));
221 }
222 
223 static inline isc_result_t
224 tostruct_mx(ARGS_TOSTRUCT) {
225 	isc_region_t region;
226 	dns_rdata_mx_t *mx = target;
227 	dns_name_t name;
228 
229 	REQUIRE(rdata->type == dns_rdatatype_mx);
230 	REQUIRE(mx != NULL);
231 	REQUIRE(rdata->length != 0);
232 
233 	mx->common.rdclass = rdata->rdclass;
234 	mx->common.rdtype = rdata->type;
235 	ISC_LINK_INIT(&mx->common, link);
236 
237 	dns_name_init(&name, NULL);
238 	dns_rdata_toregion(rdata, &region);
239 	mx->pref = uint16_fromregion(&region);
240 	isc_region_consume(&region, 2);
241 	dns_name_fromregion(&name, &region);
242 	dns_name_init(&mx->mx, NULL);
243 	RETERR(name_duporclone(&name, mctx, &mx->mx));
244 	mx->mctx = mctx;
245 	return (ISC_R_SUCCESS);
246 }
247 
248 static inline void
249 freestruct_mx(ARGS_FREESTRUCT) {
250 	dns_rdata_mx_t *mx = source;
251 
252 	REQUIRE(mx != NULL);
253 	REQUIRE(mx->common.rdtype == dns_rdatatype_mx);
254 
255 	if (mx->mctx == NULL) {
256 		return;
257 	}
258 
259 	dns_name_free(&mx->mx, mx->mctx);
260 	mx->mctx = NULL;
261 }
262 
263 static unsigned char port25_offset[] = { 0, 3 };
264 static unsigned char port25_ndata[] = "\003_25\004_tcp";
265 static dns_name_t port25 = DNS_NAME_INITNONABSOLUTE(port25_ndata,
266 						    port25_offset);
267 
268 static inline isc_result_t
269 additionaldata_mx(ARGS_ADDLDATA) {
270 	isc_result_t result;
271 	dns_fixedname_t fixed;
272 	dns_name_t name;
273 	dns_offsets_t offsets;
274 	isc_region_t region;
275 
276 	REQUIRE(rdata->type == dns_rdatatype_mx);
277 
278 	dns_name_init(&name, offsets);
279 	dns_rdata_toregion(rdata, &region);
280 	isc_region_consume(&region, 2);
281 	dns_name_fromregion(&name, &region);
282 
283 	if (dns_name_equal(&name, dns_rootname)) {
284 		return (ISC_R_SUCCESS);
285 	}
286 
287 	result = (add)(arg, &name, dns_rdatatype_a);
288 	if (result != ISC_R_SUCCESS) {
289 		return (result);
290 	}
291 
292 	dns_fixedname_init(&fixed);
293 	result = dns_name_concatenate(&port25, &name,
294 				      dns_fixedname_name(&fixed), NULL);
295 	if (result != ISC_R_SUCCESS) {
296 		return (ISC_R_SUCCESS);
297 	}
298 
299 	return ((add)(arg, dns_fixedname_name(&fixed), dns_rdatatype_tlsa));
300 }
301 
302 static inline isc_result_t
303 digest_mx(ARGS_DIGEST) {
304 	isc_region_t r1, r2;
305 	dns_name_t name;
306 
307 	REQUIRE(rdata->type == dns_rdatatype_mx);
308 
309 	dns_rdata_toregion(rdata, &r1);
310 	r2 = r1;
311 	isc_region_consume(&r2, 2);
312 	r1.length = 2;
313 	RETERR((digest)(arg, &r1));
314 	dns_name_init(&name, NULL);
315 	dns_name_fromregion(&name, &r2);
316 	return (dns_name_digest(&name, digest, arg));
317 }
318 
319 static inline bool
320 checkowner_mx(ARGS_CHECKOWNER) {
321 	REQUIRE(type == dns_rdatatype_mx);
322 
323 	UNUSED(type);
324 	UNUSED(rdclass);
325 
326 	return (dns_name_ishostname(name, wildcard));
327 }
328 
329 static inline bool
330 checknames_mx(ARGS_CHECKNAMES) {
331 	isc_region_t region;
332 	dns_name_t name;
333 
334 	REQUIRE(rdata->type == dns_rdatatype_mx);
335 
336 	UNUSED(owner);
337 
338 	dns_rdata_toregion(rdata, &region);
339 	isc_region_consume(&region, 2);
340 	dns_name_init(&name, NULL);
341 	dns_name_fromregion(&name, &region);
342 	if (!dns_name_ishostname(&name, false)) {
343 		if (bad != NULL) {
344 			dns_name_clone(&name, bad);
345 		}
346 		return (false);
347 	}
348 	return (true);
349 }
350 
351 static inline int
352 casecompare_mx(ARGS_COMPARE) {
353 	return (compare_mx(rdata1, rdata2));
354 }
355 
356 #endif /* RDATA_GENERIC_MX_15_C */
357