1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7  *
8  * See the COPYRIGHT file distributed with this work for additional
9  * information regarding copyright ownership.
10  */
11 
12 /*! \file */
13 
14 #include <ctype.h>
15 #include <inttypes.h>
16 #include <stdbool.h>
17 
18 #include <isc/base64.h>
19 #include <isc/hex.h>
20 #include <isc/lex.h>
21 #include <isc/mem.h>
22 #include <isc/parseint.h>
23 #include <isc/print.h>
24 #include <isc/result.h>
25 #include <isc/string.h>
26 #include <isc/util.h>
27 
28 #include <dns/callbacks.h>
29 #include <dns/cert.h>
30 #include <dns/compress.h>
31 #include <dns/dsdigest.h>
32 #include <dns/enumtype.h>
33 #include <dns/fixedname.h>
34 #include <dns/keyflags.h>
35 #include <dns/keyvalues.h>
36 #include <dns/message.h>
37 #include <dns/rcode.h>
38 #include <dns/rdata.h>
39 #include <dns/rdataclass.h>
40 #include <dns/rdataset.h>
41 #include <dns/rdatastruct.h>
42 #include <dns/rdatatype.h>
43 #include <dns/secalg.h>
44 #include <dns/secproto.h>
45 #include <dns/time.h>
46 #include <dns/ttl.h>
47 
48 #define RETERR(x)                        \
49 	do {                             \
50 		isc_result_t _r = (x);   \
51 		if (_r != ISC_R_SUCCESS) \
52 			return ((_r));   \
53 	} while (0)
54 
55 #define RETTOK(x)                                          \
56 	do {                                               \
57 		isc_result_t _r = (x);                     \
58 		if (_r != ISC_R_SUCCESS) {                 \
59 			isc_lex_ungettoken(lexer, &token); \
60 			return (_r);                       \
61 		}                                          \
62 	} while (0)
63 
64 #define CHECK(op)                            \
65 	do {                                 \
66 		result = (op);               \
67 		if (result != ISC_R_SUCCESS) \
68 			goto cleanup;        \
69 	} while (0)
70 
71 #define CHECKTOK(op)                                       \
72 	do {                                               \
73 		result = (op);                             \
74 		if (result != ISC_R_SUCCESS) {             \
75 			isc_lex_ungettoken(lexer, &token); \
76 			goto cleanup;                      \
77 		}                                          \
78 	} while (0)
79 
80 #define DNS_AS_STR(t) ((t).value.as_textregion.base)
81 
82 #define ARGS_FROMTEXT                                           \
83 	int rdclass, dns_rdatatype_t type, isc_lex_t *lexer,    \
84 		const dns_name_t *origin, unsigned int options, \
85 		isc_buffer_t *target, dns_rdatacallbacks_t *callbacks
86 
87 #define CALL_FROMTEXT rdclass, type, lexer, origin, options, target, callbacks
88 
89 #define ARGS_TOTEXT \
90 	dns_rdata_t *rdata, dns_rdata_textctx_t *tctx, isc_buffer_t *target
91 
92 #define CALL_TOTEXT rdata, tctx, target
93 
94 #define ARGS_FROMWIRE                                            \
95 	int rdclass, dns_rdatatype_t type, isc_buffer_t *source, \
96 		dns_decompress_t *dctx, unsigned int options,    \
97 		isc_buffer_t *target
98 
99 #define CALL_FROMWIRE rdclass, type, source, dctx, options, target
100 
101 #define ARGS_TOWIRE \
102 	dns_rdata_t *rdata, dns_compress_t *cctx, isc_buffer_t *target
103 
104 #define CALL_TOWIRE rdata, cctx, target
105 
106 #define ARGS_COMPARE const dns_rdata_t *rdata1, const dns_rdata_t *rdata2
107 
108 #define CALL_COMPARE rdata1, rdata2
109 
110 #define ARGS_FROMSTRUCT \
111 	int rdclass, dns_rdatatype_t type, void *source, isc_buffer_t *target
112 
113 #define CALL_FROMSTRUCT rdclass, type, source, target
114 
115 #define ARGS_TOSTRUCT const dns_rdata_t *rdata, void *target, isc_mem_t *mctx
116 
117 #define CALL_TOSTRUCT rdata, target, mctx
118 
119 #define ARGS_FREESTRUCT void *source
120 
121 #define CALL_FREESTRUCT source
122 
123 #define ARGS_ADDLDATA                                \
124 	dns_rdata_t *rdata, const dns_name_t *owner, \
125 		dns_additionaldatafunc_t add, void *arg
126 
127 #define CALL_ADDLDATA rdata, owner, add, arg
128 
129 #define ARGS_DIGEST dns_rdata_t *rdata, dns_digestfunc_t digest, void *arg
130 
131 #define CALL_DIGEST rdata, digest, arg
132 
133 #define ARGS_CHECKOWNER                                   \
134 	const dns_name_t *name, dns_rdataclass_t rdclass, \
135 		dns_rdatatype_t type, bool wildcard
136 
137 #define CALL_CHECKOWNER name, rdclass, type, wildcard
138 
139 #define ARGS_CHECKNAMES \
140 	dns_rdata_t *rdata, const dns_name_t *owner, dns_name_t *bad
141 
142 #define CALL_CHECKNAMES rdata, owner, bad
143 
144 /*%
145  * Context structure for the totext_ functions.
146  * Contains formatting options for rdata-to-text
147  * conversion.
148  */
149 typedef struct dns_rdata_textctx {
150 	const dns_name_t *origin;      /*%< Current origin, or NULL. */
151 	dns_masterstyle_flags_t flags; /*%< DNS_STYLEFLAG_*  */
152 	unsigned int width;	       /*%< Width of rdata column. */
153 	const char *linebreak;	       /*%< Line break string. */
154 } dns_rdata_textctx_t;
155 
156 static isc_result_t
157 txt_totext(isc_region_t *source, bool quote, isc_buffer_t *target);
158 
159 static isc_result_t
160 txt_fromtext(isc_textregion_t *source, isc_buffer_t *target);
161 
162 static isc_result_t
163 txt_fromwire(isc_buffer_t *source, isc_buffer_t *target);
164 
165 static isc_result_t
166 commatxt_fromtext(isc_textregion_t *source, bool comma, isc_buffer_t *target);
167 
168 static isc_result_t
169 commatxt_totext(isc_region_t *source, bool quote, bool comma,
170 		isc_buffer_t *target);
171 
172 static isc_result_t
173 multitxt_totext(isc_region_t *source, isc_buffer_t *target);
174 
175 static isc_result_t
176 multitxt_fromtext(isc_textregion_t *source, isc_buffer_t *target);
177 
178 static bool
179 name_prefix(dns_name_t *name, const dns_name_t *origin, dns_name_t *target);
180 
181 static unsigned int
182 name_length(const dns_name_t *name);
183 
184 static isc_result_t
185 str_totext(const char *source, isc_buffer_t *target);
186 
187 static isc_result_t
188 inet_totext(int af, uint32_t flags, isc_region_t *src, isc_buffer_t *target);
189 
190 static bool
191 buffer_empty(isc_buffer_t *source);
192 
193 static void
194 buffer_fromregion(isc_buffer_t *buffer, isc_region_t *region);
195 
196 static isc_result_t
197 uint32_tobuffer(uint32_t, isc_buffer_t *target);
198 
199 static isc_result_t
200 uint16_tobuffer(uint32_t, isc_buffer_t *target);
201 
202 static isc_result_t
203 uint8_tobuffer(uint32_t, isc_buffer_t *target);
204 
205 static isc_result_t
206 name_tobuffer(const dns_name_t *name, isc_buffer_t *target);
207 
208 static uint32_t
209 uint32_fromregion(isc_region_t *region);
210 
211 static uint16_t
212 uint16_fromregion(isc_region_t *region);
213 
214 static uint8_t
215 uint8_fromregion(isc_region_t *region);
216 
217 static uint8_t
218 uint8_consume_fromregion(isc_region_t *region);
219 
220 static isc_result_t
221 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length);
222 
223 static int
224 hexvalue(char value);
225 
226 static int
227 decvalue(char value);
228 
229 static void
230 default_fromtext_callback(dns_rdatacallbacks_t *callbacks, const char *, ...)
231 	ISC_FORMAT_PRINTF(2, 3);
232 
233 static void
234 fromtext_error(void (*callback)(dns_rdatacallbacks_t *, const char *, ...),
235 	       dns_rdatacallbacks_t *callbacks, const char *name,
236 	       unsigned long line, isc_token_t *token, isc_result_t result);
237 
238 static void
239 fromtext_warneof(isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks);
240 
241 static isc_result_t
242 rdata_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
243 	     isc_buffer_t *target);
244 
245 static void
246 warn_badname(const dns_name_t *name, isc_lex_t *lexer,
247 	     dns_rdatacallbacks_t *callbacks);
248 
249 static void
250 warn_badmx(isc_token_t *token, isc_lex_t *lexer,
251 	   dns_rdatacallbacks_t *callbacks);
252 
253 static uint16_t
254 uint16_consume_fromregion(isc_region_t *region);
255 
256 static isc_result_t
257 unknown_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
258 	       isc_buffer_t *target);
259 
260 static inline isc_result_t generic_fromtext_key(ARGS_FROMTEXT);
261 
262 static inline isc_result_t generic_totext_key(ARGS_TOTEXT);
263 
264 static inline isc_result_t generic_fromwire_key(ARGS_FROMWIRE);
265 
266 static inline isc_result_t generic_fromstruct_key(ARGS_FROMSTRUCT);
267 
268 static inline isc_result_t generic_tostruct_key(ARGS_TOSTRUCT);
269 
270 static inline void generic_freestruct_key(ARGS_FREESTRUCT);
271 
272 static isc_result_t generic_fromtext_txt(ARGS_FROMTEXT);
273 
274 static isc_result_t generic_totext_txt(ARGS_TOTEXT);
275 
276 static isc_result_t generic_fromwire_txt(ARGS_FROMWIRE);
277 
278 static isc_result_t generic_fromstruct_txt(ARGS_FROMSTRUCT);
279 
280 static isc_result_t generic_tostruct_txt(ARGS_TOSTRUCT);
281 
282 static void generic_freestruct_txt(ARGS_FREESTRUCT);
283 
284 static isc_result_t
285 generic_txt_first(dns_rdata_txt_t *txt);
286 
287 static isc_result_t
288 generic_txt_next(dns_rdata_txt_t *txt);
289 
290 static isc_result_t
291 generic_txt_current(dns_rdata_txt_t *txt, dns_rdata_txt_string_t *string);
292 
293 static isc_result_t generic_totext_ds(ARGS_TOTEXT);
294 
295 static isc_result_t generic_tostruct_ds(ARGS_TOSTRUCT);
296 
297 static isc_result_t generic_fromtext_ds(ARGS_FROMTEXT);
298 
299 static isc_result_t generic_fromwire_ds(ARGS_FROMWIRE);
300 
301 static isc_result_t generic_fromstruct_ds(ARGS_FROMSTRUCT);
302 
303 static isc_result_t generic_fromtext_tlsa(ARGS_FROMTEXT);
304 
305 static isc_result_t generic_totext_tlsa(ARGS_TOTEXT);
306 
307 static isc_result_t generic_fromwire_tlsa(ARGS_FROMWIRE);
308 
309 static isc_result_t generic_fromstruct_tlsa(ARGS_FROMSTRUCT);
310 
311 static isc_result_t generic_tostruct_tlsa(ARGS_TOSTRUCT);
312 
313 static void generic_freestruct_tlsa(ARGS_FREESTRUCT);
314 
315 static isc_result_t generic_fromtext_in_svcb(ARGS_FROMTEXT);
316 static isc_result_t generic_totext_in_svcb(ARGS_TOTEXT);
317 static isc_result_t generic_fromwire_in_svcb(ARGS_FROMWIRE);
318 static isc_result_t generic_towire_in_svcb(ARGS_TOWIRE);
319 static isc_result_t generic_fromstruct_in_svcb(ARGS_FROMSTRUCT);
320 static isc_result_t generic_tostruct_in_svcb(ARGS_TOSTRUCT);
321 static void generic_freestruct_in_svcb(ARGS_FREESTRUCT);
322 static isc_result_t generic_additionaldata_in_svcb(ARGS_ADDLDATA);
323 static bool generic_checknames_in_svcb(ARGS_CHECKNAMES);
324 static isc_result_t
325 generic_rdata_in_svcb_first(dns_rdata_in_svcb_t *);
326 static isc_result_t
327 generic_rdata_in_svcb_next(dns_rdata_in_svcb_t *);
328 static void
329 generic_rdata_in_svcb_current(dns_rdata_in_svcb_t *, isc_region_t *);
330 
331 /*% INT16 Size */
332 #define NS_INT16SZ 2
333 /*% IPv6 Address Size */
334 #define NS_LOCATORSZ 8
335 
336 /*
337  * Active Directory gc._msdcs.<forest> prefix.
338  */
339 static unsigned char gc_msdcs_data[] = "\002gc\006_msdcs";
340 static unsigned char gc_msdcs_offset[] = { 0, 3 };
341 
342 static dns_name_t const gc_msdcs = DNS_NAME_INITNONABSOLUTE(gc_msdcs_data,
343 							    gc_msdcs_offset);
344 
345 /*%
346  *	convert presentation level address to network order binary form.
347  * \return
348  *	1 if `src' is a valid [RFC1884 2.2] address, else 0.
349  * \note
350  *	(1) does not touch `dst' unless it's returning 1.
351  */
352 static inline int
locator_pton(const char * src,unsigned char * dst)353 locator_pton(const char *src, unsigned char *dst) {
354 	static const char xdigits_l[] = "0123456789abcdef",
355 			  xdigits_u[] = "0123456789ABCDEF";
356 	unsigned char tmp[NS_LOCATORSZ];
357 	unsigned char *tp = tmp, *endp;
358 	const char *xdigits;
359 	int ch, seen_xdigits;
360 	unsigned int val;
361 
362 	memset(tp, '\0', NS_LOCATORSZ);
363 	endp = tp + NS_LOCATORSZ;
364 	seen_xdigits = 0;
365 	val = 0;
366 	while ((ch = *src++) != '\0') {
367 		const char *pch;
368 
369 		pch = strchr((xdigits = xdigits_l), ch);
370 		if (pch == NULL) {
371 			pch = strchr((xdigits = xdigits_u), ch);
372 		}
373 		if (pch != NULL) {
374 			val <<= 4;
375 			val |= (pch - xdigits);
376 			if (++seen_xdigits > 4) {
377 				return (0);
378 			}
379 			continue;
380 		}
381 		if (ch == ':') {
382 			if (!seen_xdigits) {
383 				return (0);
384 			}
385 			if (tp + NS_INT16SZ > endp) {
386 				return (0);
387 			}
388 			*tp++ = (unsigned char)(val >> 8) & 0xff;
389 			*tp++ = (unsigned char)val & 0xff;
390 			seen_xdigits = 0;
391 			val = 0;
392 			continue;
393 		}
394 		return (0);
395 	}
396 	if (seen_xdigits) {
397 		if (tp + NS_INT16SZ > endp) {
398 			return (0);
399 		}
400 		*tp++ = (unsigned char)(val >> 8) & 0xff;
401 		*tp++ = (unsigned char)val & 0xff;
402 	}
403 	if (tp != endp) {
404 		return (0);
405 	}
406 	memmove(dst, tmp, NS_LOCATORSZ);
407 	return (1);
408 }
409 
410 static inline isc_result_t
name_duporclone(const dns_name_t * source,isc_mem_t * mctx,dns_name_t * target)411 name_duporclone(const dns_name_t *source, isc_mem_t *mctx, dns_name_t *target) {
412 	if (mctx != NULL) {
413 		dns_name_dup(source, mctx, target);
414 	} else {
415 		dns_name_clone(source, target);
416 	}
417 	return (ISC_R_SUCCESS);
418 }
419 
420 static inline void *
mem_maybedup(isc_mem_t * mctx,void * source,size_t length)421 mem_maybedup(isc_mem_t *mctx, void *source, size_t length) {
422 	void *copy;
423 
424 	if (mctx == NULL) {
425 		return (source);
426 	}
427 	copy = isc_mem_allocate(mctx, length);
428 	memmove(copy, source, length);
429 
430 	return (copy);
431 }
432 
433 static inline isc_result_t
typemap_fromtext(isc_lex_t * lexer,isc_buffer_t * target,bool allow_empty)434 typemap_fromtext(isc_lex_t *lexer, isc_buffer_t *target, bool allow_empty) {
435 	isc_token_t token;
436 	unsigned char bm[8 * 1024]; /* 64k bits */
437 	dns_rdatatype_t covered, max_used;
438 	int octet;
439 	unsigned int max_octet, newend, end;
440 	int window;
441 	bool first = true;
442 
443 	max_used = 0;
444 	bm[0] = 0;
445 	end = 0;
446 
447 	do {
448 		RETERR(isc_lex_getmastertoken(lexer, &token,
449 					      isc_tokentype_string, true));
450 		if (token.type != isc_tokentype_string) {
451 			break;
452 		}
453 		RETTOK(dns_rdatatype_fromtext(&covered,
454 					      &token.value.as_textregion));
455 		if (covered > max_used) {
456 			newend = covered / 8;
457 			if (newend > end) {
458 				memset(&bm[end + 1], 0, newend - end);
459 				end = newend;
460 			}
461 			max_used = covered;
462 		}
463 		bm[covered / 8] |= (0x80 >> (covered % 8));
464 		first = false;
465 	} while (1);
466 	isc_lex_ungettoken(lexer, &token);
467 	if (!allow_empty && first) {
468 		return (DNS_R_FORMERR);
469 	}
470 
471 	for (window = 0; window < 256; window++) {
472 		if (max_used < window * 256) {
473 			break;
474 		}
475 
476 		max_octet = max_used - (window * 256);
477 		if (max_octet >= 256) {
478 			max_octet = 31;
479 		} else {
480 			max_octet /= 8;
481 		}
482 
483 		/*
484 		 * Find if we have a type in this window.
485 		 */
486 		for (octet = max_octet; octet >= 0; octet--) {
487 			if (bm[window * 32 + octet] != 0) {
488 				break;
489 			}
490 		}
491 		if (octet < 0) {
492 			continue;
493 		}
494 		RETERR(uint8_tobuffer(window, target));
495 		RETERR(uint8_tobuffer(octet + 1, target));
496 		RETERR(mem_tobuffer(target, &bm[window * 32], octet + 1));
497 	}
498 	return (ISC_R_SUCCESS);
499 }
500 
501 static inline isc_result_t
typemap_totext(isc_region_t * sr,dns_rdata_textctx_t * tctx,isc_buffer_t * target)502 typemap_totext(isc_region_t *sr, dns_rdata_textctx_t *tctx,
503 	       isc_buffer_t *target) {
504 	unsigned int i, j, k;
505 	unsigned int window, len;
506 	bool first = true;
507 
508 	for (i = 0; i < sr->length; i += len) {
509 		if (tctx != NULL &&
510 		    (tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) {
511 			RETERR(str_totext(tctx->linebreak, target));
512 			first = true;
513 		}
514 		INSIST(i + 2 <= sr->length);
515 		window = sr->base[i];
516 		len = sr->base[i + 1];
517 		INSIST(len > 0 && len <= 32);
518 		i += 2;
519 		INSIST(i + len <= sr->length);
520 		for (j = 0; j < len; j++) {
521 			dns_rdatatype_t t;
522 			if (sr->base[i + j] == 0) {
523 				continue;
524 			}
525 			for (k = 0; k < 8; k++) {
526 				if ((sr->base[i + j] & (0x80 >> k)) == 0) {
527 					continue;
528 				}
529 				t = window * 256 + j * 8 + k;
530 				if (!first) {
531 					RETERR(str_totext(" ", target));
532 				}
533 				first = false;
534 				if (dns_rdatatype_isknown(t)) {
535 					RETERR(dns_rdatatype_totext(t, target));
536 				} else {
537 					char buf[sizeof("TYPE65535")];
538 					snprintf(buf, sizeof(buf), "TYPE%u", t);
539 					RETERR(str_totext(buf, target));
540 				}
541 			}
542 		}
543 	}
544 	return (ISC_R_SUCCESS);
545 }
546 
547 static isc_result_t
typemap_test(isc_region_t * sr,bool allow_empty)548 typemap_test(isc_region_t *sr, bool allow_empty) {
549 	unsigned int window, lastwindow = 0;
550 	unsigned int len;
551 	bool first = true;
552 	unsigned int i;
553 
554 	for (i = 0; i < sr->length; i += len) {
555 		/*
556 		 * Check for overflow.
557 		 */
558 		if (i + 2 > sr->length) {
559 			RETERR(DNS_R_FORMERR);
560 		}
561 		window = sr->base[i];
562 		len = sr->base[i + 1];
563 		i += 2;
564 		/*
565 		 * Check that bitmap windows are in the correct order.
566 		 */
567 		if (!first && window <= lastwindow) {
568 			RETERR(DNS_R_FORMERR);
569 		}
570 		/*
571 		 * Check for legal lengths.
572 		 */
573 		if (len < 1 || len > 32) {
574 			RETERR(DNS_R_FORMERR);
575 		}
576 		/*
577 		 * Check for overflow.
578 		 */
579 		if (i + len > sr->length) {
580 			RETERR(DNS_R_FORMERR);
581 		}
582 		/*
583 		 * The last octet of the bitmap must be non zero.
584 		 */
585 		if (sr->base[i + len - 1] == 0) {
586 			RETERR(DNS_R_FORMERR);
587 		}
588 		lastwindow = window;
589 		first = false;
590 	}
591 	if (i != sr->length) {
592 		return (DNS_R_EXTRADATA);
593 	}
594 	if (!allow_empty && first) {
595 		RETERR(DNS_R_FORMERR);
596 	}
597 	return (ISC_R_SUCCESS);
598 }
599 
600 static const char hexdigits[] = "0123456789abcdef";
601 static const char decdigits[] = "0123456789";
602 
603 #include "code.h"
604 
605 #define META	 0x0001
606 #define RESERVED 0x0002
607 
608 /***
609  *** Initialization
610  ***/
611 
612 void
dns_rdata_init(dns_rdata_t * rdata)613 dns_rdata_init(dns_rdata_t *rdata) {
614 	REQUIRE(rdata != NULL);
615 
616 	rdata->data = NULL;
617 	rdata->length = 0;
618 	rdata->rdclass = 0;
619 	rdata->type = 0;
620 	rdata->flags = 0;
621 	ISC_LINK_INIT(rdata, link);
622 	/* ISC_LIST_INIT(rdata->list); */
623 }
624 
625 void
dns_rdata_reset(dns_rdata_t * rdata)626 dns_rdata_reset(dns_rdata_t *rdata) {
627 	REQUIRE(rdata != NULL);
628 
629 	REQUIRE(!ISC_LINK_LINKED(rdata, link));
630 	REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
631 
632 	rdata->data = NULL;
633 	rdata->length = 0;
634 	rdata->rdclass = 0;
635 	rdata->type = 0;
636 	rdata->flags = 0;
637 }
638 
639 /***
640  ***
641  ***/
642 
643 void
dns_rdata_clone(const dns_rdata_t * src,dns_rdata_t * target)644 dns_rdata_clone(const dns_rdata_t *src, dns_rdata_t *target) {
645 	REQUIRE(src != NULL);
646 	REQUIRE(target != NULL);
647 
648 	REQUIRE(DNS_RDATA_INITIALIZED(target));
649 
650 	REQUIRE(DNS_RDATA_VALIDFLAGS(src));
651 	REQUIRE(DNS_RDATA_VALIDFLAGS(target));
652 
653 	target->data = src->data;
654 	target->length = src->length;
655 	target->rdclass = src->rdclass;
656 	target->type = src->type;
657 	target->flags = src->flags;
658 }
659 
660 /***
661  *** Comparisons
662  ***/
663 
664 int
dns_rdata_compare(const dns_rdata_t * rdata1,const dns_rdata_t * rdata2)665 dns_rdata_compare(const dns_rdata_t *rdata1, const dns_rdata_t *rdata2) {
666 	int result = 0;
667 	bool use_default = false;
668 
669 	REQUIRE(rdata1 != NULL);
670 	REQUIRE(rdata2 != NULL);
671 	REQUIRE(rdata1->length == 0 || rdata1->data != NULL);
672 	REQUIRE(rdata2->length == 0 || rdata2->data != NULL);
673 	REQUIRE(DNS_RDATA_VALIDFLAGS(rdata1));
674 	REQUIRE(DNS_RDATA_VALIDFLAGS(rdata2));
675 
676 	if (rdata1->rdclass != rdata2->rdclass) {
677 		return (rdata1->rdclass < rdata2->rdclass ? -1 : 1);
678 	}
679 
680 	if (rdata1->type != rdata2->type) {
681 		return (rdata1->type < rdata2->type ? -1 : 1);
682 	}
683 
684 	COMPARESWITCH
685 
686 	if (use_default) {
687 		isc_region_t r1;
688 		isc_region_t r2;
689 
690 		dns_rdata_toregion(rdata1, &r1);
691 		dns_rdata_toregion(rdata2, &r2);
692 		result = isc_region_compare(&r1, &r2);
693 	}
694 	return (result);
695 }
696 
697 int
dns_rdata_casecompare(const dns_rdata_t * rdata1,const dns_rdata_t * rdata2)698 dns_rdata_casecompare(const dns_rdata_t *rdata1, const dns_rdata_t *rdata2) {
699 	int result = 0;
700 	bool use_default = false;
701 
702 	REQUIRE(rdata1 != NULL);
703 	REQUIRE(rdata2 != NULL);
704 	REQUIRE(rdata1->length == 0 || rdata1->data != NULL);
705 	REQUIRE(rdata2->length == 0 || rdata2->data != NULL);
706 	REQUIRE(DNS_RDATA_VALIDFLAGS(rdata1));
707 	REQUIRE(DNS_RDATA_VALIDFLAGS(rdata2));
708 
709 	if (rdata1->rdclass != rdata2->rdclass) {
710 		return (rdata1->rdclass < rdata2->rdclass ? -1 : 1);
711 	}
712 
713 	if (rdata1->type != rdata2->type) {
714 		return (rdata1->type < rdata2->type ? -1 : 1);
715 	}
716 
717 	CASECOMPARESWITCH
718 
719 	if (use_default) {
720 		isc_region_t r1;
721 		isc_region_t r2;
722 
723 		dns_rdata_toregion(rdata1, &r1);
724 		dns_rdata_toregion(rdata2, &r2);
725 		result = isc_region_compare(&r1, &r2);
726 	}
727 	return (result);
728 }
729 
730 /***
731  *** Conversions
732  ***/
733 
734 void
dns_rdata_fromregion(dns_rdata_t * rdata,dns_rdataclass_t rdclass,dns_rdatatype_t type,isc_region_t * r)735 dns_rdata_fromregion(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
736 		     dns_rdatatype_t type, isc_region_t *r) {
737 	REQUIRE(rdata != NULL);
738 	REQUIRE(DNS_RDATA_INITIALIZED(rdata));
739 	REQUIRE(r != NULL);
740 
741 	REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
742 
743 	rdata->data = r->base;
744 	rdata->length = r->length;
745 	rdata->rdclass = rdclass;
746 	rdata->type = type;
747 	rdata->flags = 0;
748 }
749 
750 void
dns_rdata_toregion(const dns_rdata_t * rdata,isc_region_t * r)751 dns_rdata_toregion(const dns_rdata_t *rdata, isc_region_t *r) {
752 	REQUIRE(rdata != NULL);
753 	REQUIRE(r != NULL);
754 	REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
755 
756 	r->base = rdata->data;
757 	r->length = rdata->length;
758 }
759 
760 isc_result_t
dns_rdata_fromwire(dns_rdata_t * rdata,dns_rdataclass_t rdclass,dns_rdatatype_t type,isc_buffer_t * source,dns_decompress_t * dctx,unsigned int options,isc_buffer_t * target)761 dns_rdata_fromwire(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
762 		   dns_rdatatype_t type, isc_buffer_t *source,
763 		   dns_decompress_t *dctx, unsigned int options,
764 		   isc_buffer_t *target) {
765 	isc_result_t result = ISC_R_NOTIMPLEMENTED;
766 	isc_region_t region;
767 	isc_buffer_t ss;
768 	isc_buffer_t st;
769 	bool use_default = false;
770 	uint32_t activelength;
771 	unsigned int length;
772 
773 	REQUIRE(dctx != NULL);
774 	if (rdata != NULL) {
775 		REQUIRE(DNS_RDATA_INITIALIZED(rdata));
776 		REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
777 	}
778 	REQUIRE(source != NULL);
779 	REQUIRE(target != NULL);
780 
781 	if (type == 0) {
782 		return (DNS_R_FORMERR);
783 	}
784 
785 	ss = *source;
786 	st = *target;
787 
788 	activelength = isc_buffer_activelength(source);
789 	INSIST(activelength < 65536);
790 
791 	FROMWIRESWITCH
792 
793 	if (use_default) {
794 		if (activelength > isc_buffer_availablelength(target)) {
795 			result = ISC_R_NOSPACE;
796 		} else {
797 			isc_buffer_putmem(target, isc_buffer_current(source),
798 					  activelength);
799 			isc_buffer_forward(source, activelength);
800 			result = ISC_R_SUCCESS;
801 		}
802 	}
803 
804 	/*
805 	 * Reject any rdata that expands out to more than DNS_RDATA_MAXLENGTH
806 	 * as we cannot transmit it.
807 	 */
808 	length = isc_buffer_usedlength(target) - isc_buffer_usedlength(&st);
809 	if (result == ISC_R_SUCCESS && length > DNS_RDATA_MAXLENGTH) {
810 		result = DNS_R_FORMERR;
811 	}
812 
813 	/*
814 	 * We should have consumed all of our buffer.
815 	 */
816 	if (result == ISC_R_SUCCESS && !buffer_empty(source)) {
817 		result = DNS_R_EXTRADATA;
818 	}
819 
820 	if (rdata != NULL && result == ISC_R_SUCCESS) {
821 		region.base = isc_buffer_used(&st);
822 		region.length = length;
823 		dns_rdata_fromregion(rdata, rdclass, type, &region);
824 	}
825 
826 	if (result != ISC_R_SUCCESS) {
827 		*source = ss;
828 		*target = st;
829 	}
830 	return (result);
831 }
832 
833 isc_result_t
dns_rdata_towire(dns_rdata_t * rdata,dns_compress_t * cctx,isc_buffer_t * target)834 dns_rdata_towire(dns_rdata_t *rdata, dns_compress_t *cctx,
835 		 isc_buffer_t *target) {
836 	isc_result_t result = ISC_R_NOTIMPLEMENTED;
837 	bool use_default = false;
838 	isc_region_t tr;
839 	isc_buffer_t st;
840 
841 	REQUIRE(rdata != NULL);
842 	REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
843 
844 	/*
845 	 * Some DynDNS meta-RRs have empty rdata.
846 	 */
847 	if ((rdata->flags & DNS_RDATA_UPDATE) != 0) {
848 		INSIST(rdata->length == 0);
849 		return (ISC_R_SUCCESS);
850 	}
851 
852 	st = *target;
853 
854 	TOWIRESWITCH
855 
856 	if (use_default) {
857 		isc_buffer_availableregion(target, &tr);
858 		if (tr.length < rdata->length) {
859 			return (ISC_R_NOSPACE);
860 		}
861 		memmove(tr.base, rdata->data, rdata->length);
862 		isc_buffer_add(target, rdata->length);
863 		return (ISC_R_SUCCESS);
864 	}
865 	if (result != ISC_R_SUCCESS) {
866 		*target = st;
867 		INSIST(target->used < 65536);
868 		dns_compress_rollback(cctx, (uint16_t)target->used);
869 	}
870 	return (result);
871 }
872 
873 /*
874  * If the binary data in 'src' is valid uncompressed wire format
875  * rdata of class 'rdclass' and type 'type', return ISC_R_SUCCESS
876  * and copy the validated rdata to 'dest'.  Otherwise return an error.
877  */
878 static isc_result_t
rdata_validate(isc_buffer_t * src,isc_buffer_t * dest,dns_rdataclass_t rdclass,dns_rdatatype_t type)879 rdata_validate(isc_buffer_t *src, isc_buffer_t *dest, dns_rdataclass_t rdclass,
880 	       dns_rdatatype_t type) {
881 	dns_decompress_t dctx;
882 	isc_result_t result;
883 
884 	dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_NONE);
885 	isc_buffer_setactive(src, isc_buffer_usedlength(src));
886 	result = dns_rdata_fromwire(NULL, rdclass, type, src, &dctx, 0, dest);
887 	dns_decompress_invalidate(&dctx);
888 
889 	return (result);
890 }
891 
892 static isc_result_t
unknown_fromtext(dns_rdataclass_t rdclass,dns_rdatatype_t type,isc_lex_t * lexer,isc_mem_t * mctx,isc_buffer_t * target)893 unknown_fromtext(dns_rdataclass_t rdclass, dns_rdatatype_t type,
894 		 isc_lex_t *lexer, isc_mem_t *mctx, isc_buffer_t *target) {
895 	isc_result_t result;
896 	isc_buffer_t *buf = NULL;
897 	isc_token_t token;
898 
899 	if (type == 0 || dns_rdatatype_ismeta(type)) {
900 		return (DNS_R_METATYPE);
901 	}
902 
903 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
904 				      false));
905 	if (token.value.as_ulong > 65535U) {
906 		return (ISC_R_RANGE);
907 	}
908 	isc_buffer_allocate(mctx, &buf, token.value.as_ulong);
909 
910 	if (token.value.as_ulong != 0U) {
911 		result = isc_hex_tobuffer(lexer, buf,
912 					  (unsigned int)token.value.as_ulong);
913 		if (result != ISC_R_SUCCESS) {
914 			goto failure;
915 		}
916 		if (isc_buffer_usedlength(buf) != token.value.as_ulong) {
917 			result = ISC_R_UNEXPECTEDEND;
918 			goto failure;
919 		}
920 	}
921 
922 	if (dns_rdatatype_isknown(type)) {
923 		result = rdata_validate(buf, target, rdclass, type);
924 	} else {
925 		isc_region_t r;
926 		isc_buffer_usedregion(buf, &r);
927 		result = isc_buffer_copyregion(target, &r);
928 	}
929 	if (result != ISC_R_SUCCESS) {
930 		goto failure;
931 	}
932 
933 	isc_buffer_free(&buf);
934 	return (ISC_R_SUCCESS);
935 
936 failure:
937 	isc_buffer_free(&buf);
938 	return (result);
939 }
940 
941 isc_result_t
dns_rdata_fromtext(dns_rdata_t * rdata,dns_rdataclass_t rdclass,dns_rdatatype_t type,isc_lex_t * lexer,const dns_name_t * origin,unsigned int options,isc_mem_t * mctx,isc_buffer_t * target,dns_rdatacallbacks_t * callbacks)942 dns_rdata_fromtext(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
943 		   dns_rdatatype_t type, isc_lex_t *lexer,
944 		   const dns_name_t *origin, unsigned int options,
945 		   isc_mem_t *mctx, isc_buffer_t *target,
946 		   dns_rdatacallbacks_t *callbacks) {
947 	isc_result_t result = ISC_R_NOTIMPLEMENTED;
948 	isc_region_t region;
949 	isc_buffer_t st;
950 	isc_token_t token;
951 	unsigned int lexoptions = ISC_LEXOPT_EOL | ISC_LEXOPT_EOF |
952 				  ISC_LEXOPT_DNSMULTILINE | ISC_LEXOPT_ESCAPE;
953 	char *name;
954 	unsigned long line;
955 	void (*callback)(dns_rdatacallbacks_t *, const char *, ...);
956 	isc_result_t tresult;
957 	unsigned int length;
958 	bool unknown;
959 
960 	REQUIRE(origin == NULL || dns_name_isabsolute(origin));
961 	if (rdata != NULL) {
962 		REQUIRE(DNS_RDATA_INITIALIZED(rdata));
963 		REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
964 	}
965 	if (callbacks != NULL) {
966 		REQUIRE(callbacks->warn != NULL);
967 		REQUIRE(callbacks->error != NULL);
968 	}
969 
970 	st = *target;
971 
972 	if (callbacks != NULL) {
973 		callback = callbacks->error;
974 	} else {
975 		callback = default_fromtext_callback;
976 	}
977 
978 	result = isc_lex_getmastertoken(lexer, &token, isc_tokentype_qstring,
979 					true);
980 	if (result != ISC_R_SUCCESS) {
981 		name = isc_lex_getsourcename(lexer);
982 		line = isc_lex_getsourceline(lexer);
983 		fromtext_error(callback, callbacks, name, line, NULL, result);
984 		return (result);
985 	}
986 
987 	unknown = false;
988 	if (token.type == isc_tokentype_string &&
989 	    strcmp(DNS_AS_STR(token), "\\#") == 0) {
990 		/*
991 		 * If this is a TXT record '\#' could be a escaped '#'.
992 		 * Look to see if the next token is a number and if so
993 		 * treat it as a unknown record format.
994 		 */
995 		if (type == dns_rdatatype_txt) {
996 			result = isc_lex_getmastertoken(
997 				lexer, &token, isc_tokentype_number, false);
998 			if (result == ISC_R_SUCCESS) {
999 				isc_lex_ungettoken(lexer, &token);
1000 			}
1001 		}
1002 
1003 		if (result == ISC_R_SUCCESS) {
1004 			unknown = true;
1005 			result = unknown_fromtext(rdclass, type, lexer, mctx,
1006 						  target);
1007 		} else {
1008 			options |= DNS_RDATA_UNKNOWNESCAPE;
1009 		}
1010 	} else {
1011 		isc_lex_ungettoken(lexer, &token);
1012 	}
1013 
1014 	if (!unknown) {
1015 		FROMTEXTSWITCH
1016 
1017 		/*
1018 		 * Consume to end of line / file.
1019 		 * If not at end of line initially set error code.
1020 		 * Call callback via fromtext_error once if there was an error.
1021 		 */
1022 	}
1023 	do {
1024 		name = isc_lex_getsourcename(lexer);
1025 		line = isc_lex_getsourceline(lexer);
1026 		tresult = isc_lex_gettoken(lexer, lexoptions, &token);
1027 		if (tresult != ISC_R_SUCCESS) {
1028 			if (result == ISC_R_SUCCESS) {
1029 				result = tresult;
1030 			}
1031 			if (callback != NULL) {
1032 				fromtext_error(callback, callbacks, name, line,
1033 					       NULL, result);
1034 			}
1035 			break;
1036 		} else if (token.type != isc_tokentype_eol &&
1037 			   token.type != isc_tokentype_eof) {
1038 			if (result == ISC_R_SUCCESS) {
1039 				result = DNS_R_EXTRATOKEN;
1040 			}
1041 			if (callback != NULL) {
1042 				fromtext_error(callback, callbacks, name, line,
1043 					       &token, result);
1044 				callback = NULL;
1045 			}
1046 		} else if (result != ISC_R_SUCCESS && callback != NULL) {
1047 			fromtext_error(callback, callbacks, name, line, &token,
1048 				       result);
1049 			break;
1050 		} else {
1051 			if (token.type == isc_tokentype_eof) {
1052 				fromtext_warneof(lexer, callbacks);
1053 			}
1054 			break;
1055 		}
1056 	} while (1);
1057 
1058 	length = isc_buffer_usedlength(target) - isc_buffer_usedlength(&st);
1059 	if (result == ISC_R_SUCCESS && length > DNS_RDATA_MAXLENGTH) {
1060 		result = ISC_R_NOSPACE;
1061 	}
1062 
1063 	if (rdata != NULL && result == ISC_R_SUCCESS) {
1064 		region.base = isc_buffer_used(&st);
1065 		region.length = length;
1066 		dns_rdata_fromregion(rdata, rdclass, type, &region);
1067 	}
1068 	if (result != ISC_R_SUCCESS) {
1069 		*target = st;
1070 	}
1071 	return (result);
1072 }
1073 
1074 static isc_result_t
unknown_totext(dns_rdata_t * rdata,dns_rdata_textctx_t * tctx,isc_buffer_t * target)1075 unknown_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
1076 	       isc_buffer_t *target) {
1077 	isc_result_t result;
1078 	char buf[sizeof("65535")];
1079 	isc_region_t sr;
1080 
1081 	strlcpy(buf, "\\# ", sizeof(buf));
1082 	result = str_totext(buf, target);
1083 	if (result != ISC_R_SUCCESS) {
1084 		return (result);
1085 	}
1086 
1087 	dns_rdata_toregion(rdata, &sr);
1088 	INSIST(sr.length < 65536);
1089 	snprintf(buf, sizeof(buf), "%u", sr.length);
1090 	result = str_totext(buf, target);
1091 	if (result != ISC_R_SUCCESS) {
1092 		return (result);
1093 	}
1094 
1095 	if (sr.length != 0U) {
1096 		if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) {
1097 			result = str_totext(" ( ", target);
1098 		} else {
1099 			result = str_totext(" ", target);
1100 		}
1101 
1102 		if (result != ISC_R_SUCCESS) {
1103 			return (result);
1104 		}
1105 
1106 		if (tctx->width == 0) { /* No splitting */
1107 			result = isc_hex_totext(&sr, 0, "", target);
1108 		} else {
1109 			result = isc_hex_totext(&sr, tctx->width - 2,
1110 						tctx->linebreak, target);
1111 		}
1112 		if (result == ISC_R_SUCCESS &&
1113 		    (tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) {
1114 			result = str_totext(" )", target);
1115 		}
1116 	}
1117 	return (result);
1118 }
1119 
1120 static isc_result_t
rdata_totext(dns_rdata_t * rdata,dns_rdata_textctx_t * tctx,isc_buffer_t * target)1121 rdata_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
1122 	     isc_buffer_t *target) {
1123 	isc_result_t result = ISC_R_NOTIMPLEMENTED;
1124 	bool use_default = false;
1125 	unsigned int cur;
1126 
1127 	REQUIRE(rdata != NULL);
1128 	REQUIRE(tctx->origin == NULL || dns_name_isabsolute(tctx->origin));
1129 
1130 	/*
1131 	 * Some DynDNS meta-RRs have empty rdata.
1132 	 */
1133 	if ((rdata->flags & DNS_RDATA_UPDATE) != 0) {
1134 		INSIST(rdata->length == 0);
1135 		return (ISC_R_SUCCESS);
1136 	}
1137 
1138 	if ((tctx->flags & DNS_STYLEFLAG_UNKNOWNFORMAT) != 0) {
1139 		return (unknown_totext(rdata, tctx, target));
1140 	}
1141 
1142 	cur = isc_buffer_usedlength(target);
1143 
1144 	TOTEXTSWITCH
1145 
1146 	if (use_default || (result == ISC_R_NOTIMPLEMENTED)) {
1147 		unsigned int u = isc_buffer_usedlength(target);
1148 
1149 		INSIST(u >= cur);
1150 		isc_buffer_subtract(target, u - cur);
1151 		result = unknown_totext(rdata, tctx, target);
1152 	}
1153 
1154 	return (result);
1155 }
1156 
1157 isc_result_t
dns_rdata_totext(dns_rdata_t * rdata,const dns_name_t * origin,isc_buffer_t * target)1158 dns_rdata_totext(dns_rdata_t *rdata, const dns_name_t *origin,
1159 		 isc_buffer_t *target) {
1160 	dns_rdata_textctx_t tctx;
1161 
1162 	REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
1163 
1164 	/*
1165 	 * Set up formatting options for single-line output.
1166 	 */
1167 	tctx.origin = origin;
1168 	tctx.flags = 0;
1169 	tctx.width = 60;
1170 	tctx.linebreak = " ";
1171 	return (rdata_totext(rdata, &tctx, target));
1172 }
1173 
1174 isc_result_t
dns_rdata_tofmttext(dns_rdata_t * rdata,const dns_name_t * origin,dns_masterstyle_flags_t flags,unsigned int width,unsigned int split_width,const char * linebreak,isc_buffer_t * target)1175 dns_rdata_tofmttext(dns_rdata_t *rdata, const dns_name_t *origin,
1176 		    dns_masterstyle_flags_t flags, unsigned int width,
1177 		    unsigned int split_width, const char *linebreak,
1178 		    isc_buffer_t *target) {
1179 	dns_rdata_textctx_t tctx;
1180 
1181 	REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
1182 
1183 	/*
1184 	 * Set up formatting options for formatted output.
1185 	 */
1186 	tctx.origin = origin;
1187 	tctx.flags = flags;
1188 	if (split_width == 0xffffffff) {
1189 		tctx.width = width;
1190 	} else {
1191 		tctx.width = split_width;
1192 	}
1193 
1194 	if ((flags & DNS_STYLEFLAG_MULTILINE) != 0) {
1195 		tctx.linebreak = linebreak;
1196 	} else {
1197 		if (split_width == 0xffffffff) {
1198 			tctx.width = 60; /* Used for hex word length only. */
1199 		}
1200 		tctx.linebreak = " ";
1201 	}
1202 	return (rdata_totext(rdata, &tctx, target));
1203 }
1204 
1205 isc_result_t
dns_rdata_fromstruct(dns_rdata_t * rdata,dns_rdataclass_t rdclass,dns_rdatatype_t type,void * source,isc_buffer_t * target)1206 dns_rdata_fromstruct(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
1207 		     dns_rdatatype_t type, void *source, isc_buffer_t *target) {
1208 	isc_result_t result = ISC_R_NOTIMPLEMENTED;
1209 	isc_buffer_t st;
1210 	isc_region_t region;
1211 	bool use_default = false;
1212 	unsigned int length;
1213 
1214 	REQUIRE(source != NULL);
1215 	if (rdata != NULL) {
1216 		REQUIRE(DNS_RDATA_INITIALIZED(rdata));
1217 		REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
1218 	}
1219 
1220 	st = *target;
1221 
1222 	FROMSTRUCTSWITCH
1223 
1224 	if (use_default) {
1225 		(void)NULL;
1226 	}
1227 
1228 	length = isc_buffer_usedlength(target) - isc_buffer_usedlength(&st);
1229 	if (result == ISC_R_SUCCESS && length > DNS_RDATA_MAXLENGTH) {
1230 		result = ISC_R_NOSPACE;
1231 	}
1232 
1233 	if (rdata != NULL && result == ISC_R_SUCCESS) {
1234 		region.base = isc_buffer_used(&st);
1235 		region.length = length;
1236 		dns_rdata_fromregion(rdata, rdclass, type, &region);
1237 	}
1238 	if (result != ISC_R_SUCCESS) {
1239 		*target = st;
1240 	}
1241 	return (result);
1242 }
1243 
1244 isc_result_t
dns_rdata_tostruct(const dns_rdata_t * rdata,void * target,isc_mem_t * mctx)1245 dns_rdata_tostruct(const dns_rdata_t *rdata, void *target, isc_mem_t *mctx) {
1246 	isc_result_t result = ISC_R_NOTIMPLEMENTED;
1247 	bool use_default = false;
1248 
1249 	REQUIRE(rdata != NULL);
1250 	REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
1251 	REQUIRE((rdata->flags & DNS_RDATA_UPDATE) == 0);
1252 
1253 	TOSTRUCTSWITCH
1254 
1255 	if (use_default) {
1256 		(void)NULL;
1257 	}
1258 
1259 	return (result);
1260 }
1261 
1262 void
dns_rdata_freestruct(void * source)1263 dns_rdata_freestruct(void *source) {
1264 	dns_rdatacommon_t *common = source;
1265 	REQUIRE(common != NULL);
1266 
1267 	FREESTRUCTSWITCH
1268 }
1269 
1270 isc_result_t
dns_rdata_additionaldata(dns_rdata_t * rdata,const dns_name_t * owner,dns_additionaldatafunc_t add,void * arg)1271 dns_rdata_additionaldata(dns_rdata_t *rdata, const dns_name_t *owner,
1272 			 dns_additionaldatafunc_t add, void *arg) {
1273 	isc_result_t result = ISC_R_NOTIMPLEMENTED;
1274 	bool use_default = false;
1275 
1276 	/*
1277 	 * Call 'add' for each name and type from 'rdata' which is subject to
1278 	 * additional section processing.
1279 	 */
1280 
1281 	REQUIRE(rdata != NULL);
1282 	REQUIRE(add != NULL);
1283 	REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
1284 
1285 	ADDITIONALDATASWITCH
1286 
1287 	/* No additional processing for unknown types */
1288 	if (use_default) {
1289 		result = ISC_R_SUCCESS;
1290 	}
1291 
1292 	return (result);
1293 }
1294 
1295 isc_result_t
dns_rdata_digest(dns_rdata_t * rdata,dns_digestfunc_t digest,void * arg)1296 dns_rdata_digest(dns_rdata_t *rdata, dns_digestfunc_t digest, void *arg) {
1297 	isc_result_t result = ISC_R_NOTIMPLEMENTED;
1298 	bool use_default = false;
1299 	isc_region_t r;
1300 
1301 	/*
1302 	 * Send 'rdata' in DNSSEC canonical form to 'digest'.
1303 	 */
1304 
1305 	REQUIRE(rdata != NULL);
1306 	REQUIRE(digest != NULL);
1307 	REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
1308 
1309 	DIGESTSWITCH
1310 
1311 	if (use_default) {
1312 		dns_rdata_toregion(rdata, &r);
1313 		result = (digest)(arg, &r);
1314 	}
1315 
1316 	return (result);
1317 }
1318 
1319 bool
dns_rdata_checkowner(const dns_name_t * name,dns_rdataclass_t rdclass,dns_rdatatype_t type,bool wildcard)1320 dns_rdata_checkowner(const dns_name_t *name, dns_rdataclass_t rdclass,
1321 		     dns_rdatatype_t type, bool wildcard) {
1322 	bool result;
1323 
1324 	CHECKOWNERSWITCH
1325 	return (result);
1326 }
1327 
1328 bool
dns_rdata_checknames(dns_rdata_t * rdata,const dns_name_t * owner,dns_name_t * bad)1329 dns_rdata_checknames(dns_rdata_t *rdata, const dns_name_t *owner,
1330 		     dns_name_t *bad) {
1331 	bool result;
1332 
1333 	CHECKNAMESSWITCH
1334 	return (result);
1335 }
1336 
1337 unsigned int
dns_rdatatype_attributes(dns_rdatatype_t type)1338 dns_rdatatype_attributes(dns_rdatatype_t type) {
1339 	RDATATYPE_ATTRIBUTE_SW
1340 	if (type >= (dns_rdatatype_t)128 && type <= (dns_rdatatype_t)255) {
1341 		return (DNS_RDATATYPEATTR_UNKNOWN | DNS_RDATATYPEATTR_META);
1342 	}
1343 	return (DNS_RDATATYPEATTR_UNKNOWN);
1344 }
1345 
1346 isc_result_t
dns_rdatatype_fromtext(dns_rdatatype_t * typep,isc_textregion_t * source)1347 dns_rdatatype_fromtext(dns_rdatatype_t *typep, isc_textregion_t *source) {
1348 	unsigned int hash;
1349 	unsigned int n;
1350 	unsigned char a, b;
1351 
1352 	n = source->length;
1353 
1354 	if (n == 0) {
1355 		return (DNS_R_UNKNOWN);
1356 	}
1357 
1358 	a = tolower((unsigned char)source->base[0]);
1359 	b = tolower((unsigned char)source->base[n - 1]);
1360 
1361 	hash = ((a + n) * b) % 256;
1362 
1363 	/*
1364 	 * This switch block is inlined via \#define, and will use "return"
1365 	 * to return a result to the caller if it is a valid (known)
1366 	 * rdatatype name.
1367 	 */
1368 	RDATATYPE_FROMTEXT_SW(hash, source->base, n, typep);
1369 
1370 	if (source->length > 4 && source->length < (4 + sizeof("65000")) &&
1371 	    strncasecmp("type", source->base, 4) == 0)
1372 	{
1373 		char buf[sizeof("65000")];
1374 		char *endp;
1375 		unsigned int val;
1376 
1377 		/*
1378 		 * source->base is not required to be NUL terminated.
1379 		 * Copy up to remaining bytes and NUL terminate.
1380 		 */
1381 		snprintf(buf, sizeof(buf), "%.*s", (int)(source->length - 4),
1382 			 source->base + 4);
1383 		val = strtoul(buf, &endp, 10);
1384 		if (*endp == '\0' && val <= 0xffff) {
1385 			*typep = (dns_rdatatype_t)val;
1386 			return (ISC_R_SUCCESS);
1387 		}
1388 	}
1389 
1390 	return (DNS_R_UNKNOWN);
1391 }
1392 
1393 isc_result_t
dns_rdatatype_totext(dns_rdatatype_t type,isc_buffer_t * target)1394 dns_rdatatype_totext(dns_rdatatype_t type, isc_buffer_t *target) {
1395 	RDATATYPE_TOTEXT_SW
1396 
1397 	return (dns_rdatatype_tounknowntext(type, target));
1398 }
1399 
1400 isc_result_t
dns_rdatatype_tounknowntext(dns_rdatatype_t type,isc_buffer_t * target)1401 dns_rdatatype_tounknowntext(dns_rdatatype_t type, isc_buffer_t *target) {
1402 	char buf[sizeof("TYPE65535")];
1403 
1404 	snprintf(buf, sizeof(buf), "TYPE%u", type);
1405 	return (str_totext(buf, target));
1406 }
1407 
1408 void
dns_rdatatype_format(dns_rdatatype_t rdtype,char * array,unsigned int size)1409 dns_rdatatype_format(dns_rdatatype_t rdtype, char *array, unsigned int size) {
1410 	isc_result_t result;
1411 	isc_buffer_t buf;
1412 
1413 	if (size == 0U) {
1414 		return;
1415 	}
1416 
1417 	isc_buffer_init(&buf, array, size);
1418 	result = dns_rdatatype_totext(rdtype, &buf);
1419 	/*
1420 	 * Null terminate.
1421 	 */
1422 	if (result == ISC_R_SUCCESS) {
1423 		if (isc_buffer_availablelength(&buf) >= 1) {
1424 			isc_buffer_putuint8(&buf, 0);
1425 		} else {
1426 			result = ISC_R_NOSPACE;
1427 		}
1428 	}
1429 	if (result != ISC_R_SUCCESS) {
1430 		strlcpy(array, "<unknown>", size);
1431 	}
1432 }
1433 
1434 /*
1435  * Private function.
1436  */
1437 
1438 static unsigned int
name_length(const dns_name_t * name)1439 name_length(const dns_name_t *name) {
1440 	return (name->length);
1441 }
1442 
1443 static isc_result_t
commatxt_totext(isc_region_t * source,bool quote,bool comma,isc_buffer_t * target)1444 commatxt_totext(isc_region_t *source, bool quote, bool comma,
1445 		isc_buffer_t *target) {
1446 	unsigned int tl;
1447 	unsigned int n;
1448 	unsigned char *sp;
1449 	char *tp;
1450 	isc_region_t region;
1451 
1452 	isc_buffer_availableregion(target, &region);
1453 	sp = source->base;
1454 	tp = (char *)region.base;
1455 	tl = region.length;
1456 
1457 	n = *sp++;
1458 
1459 	REQUIRE(n + 1 <= source->length);
1460 	if (n == 0U) {
1461 		REQUIRE(quote);
1462 	}
1463 
1464 	if (quote) {
1465 		if (tl < 1) {
1466 			return (ISC_R_NOSPACE);
1467 		}
1468 		*tp++ = '"';
1469 		tl--;
1470 	}
1471 	while (n--) {
1472 		/*
1473 		 * \DDD space (0x20) if not quoting.
1474 		 */
1475 		if (*sp < (quote ? ' ' : '!') || *sp >= 0x7f) {
1476 			if (tl < 4) {
1477 				return (ISC_R_NOSPACE);
1478 			}
1479 			*tp++ = '\\';
1480 			*tp++ = '0' + ((*sp / 100) % 10);
1481 			*tp++ = '0' + ((*sp / 10) % 10);
1482 			*tp++ = '0' + (*sp % 10);
1483 			sp++;
1484 			tl -= 4;
1485 			continue;
1486 		}
1487 		/*
1488 		 * Escape double quote and backslash.  If we are not
1489 		 * enclosing the string in double quotes, also escape
1490 		 * at sign (@) and semicolon (;) unless comma is set.
1491 		 * If comma is set, then only escape commas (,).
1492 		 */
1493 		if (*sp == '"' || *sp == '\\' || (comma && *sp == ',') ||
1494 		    (!comma && !quote && (*sp == '@' || *sp == ';')))
1495 		{
1496 			if (tl < 2) {
1497 				return (ISC_R_NOSPACE);
1498 			}
1499 			*tp++ = '\\';
1500 			tl--;
1501 			/*
1502 			 * Perform comma escape processing.
1503 			 * ',' => '\\,'
1504 			 * '\' => '\\\\'
1505 			 */
1506 			if (comma && (*sp == ',' || *sp == '\\')) {
1507 				if (tl < ((*sp == '\\') ? 3 : 2)) {
1508 					return (ISC_R_NOSPACE);
1509 				}
1510 				*tp++ = '\\';
1511 				tl--;
1512 				if (*sp == '\\') {
1513 					*tp++ = '\\';
1514 					tl--;
1515 				}
1516 			}
1517 		}
1518 		if (tl < 1) {
1519 			return (ISC_R_NOSPACE);
1520 		}
1521 		*tp++ = *sp++;
1522 		tl--;
1523 	}
1524 	if (quote) {
1525 		if (tl < 1) {
1526 			return (ISC_R_NOSPACE);
1527 		}
1528 		*tp++ = '"';
1529 		tl--;
1530 		POST(tl);
1531 	}
1532 	isc_buffer_add(target, (unsigned int)(tp - (char *)region.base));
1533 	isc_region_consume(source, *source->base + 1);
1534 	return (ISC_R_SUCCESS);
1535 }
1536 
1537 static isc_result_t
txt_totext(isc_region_t * source,bool quote,isc_buffer_t * target)1538 txt_totext(isc_region_t *source, bool quote, isc_buffer_t *target) {
1539 	return (commatxt_totext(source, quote, false, target));
1540 }
1541 
1542 static isc_result_t
commatxt_fromtext(isc_textregion_t * source,bool comma,isc_buffer_t * target)1543 commatxt_fromtext(isc_textregion_t *source, bool comma, isc_buffer_t *target) {
1544 	isc_region_t tregion;
1545 	bool escape = false, comma_escape = false, seen_comma = false;
1546 	unsigned int n, nrem;
1547 	char *s;
1548 	unsigned char *t;
1549 	int d;
1550 	int c;
1551 
1552 	isc_buffer_availableregion(target, &tregion);
1553 	s = source->base;
1554 	n = source->length;
1555 	t = tregion.base;
1556 	nrem = tregion.length;
1557 	if (nrem < 1) {
1558 		return (ISC_R_NOSPACE);
1559 	}
1560 	/*
1561 	 * Length byte.
1562 	 */
1563 	nrem--;
1564 	t++;
1565 	/*
1566 	 * Maximum text string length.
1567 	 */
1568 	if (nrem > 255) {
1569 		nrem = 255;
1570 	}
1571 	while (n-- != 0) {
1572 		c = (*s++) & 0xff;
1573 		if (escape && (d = decvalue((char)c)) != -1) {
1574 			c = d;
1575 			if (n == 0) {
1576 				return (DNS_R_SYNTAX);
1577 			}
1578 			n--;
1579 			if ((d = decvalue(*s++)) != -1) {
1580 				c = c * 10 + d;
1581 			} else {
1582 				return (DNS_R_SYNTAX);
1583 			}
1584 			if (n == 0) {
1585 				return (DNS_R_SYNTAX);
1586 			}
1587 			n--;
1588 			if ((d = decvalue(*s++)) != -1) {
1589 				c = c * 10 + d;
1590 			} else {
1591 				return (DNS_R_SYNTAX);
1592 			}
1593 			if (c > 255) {
1594 				return (DNS_R_SYNTAX);
1595 			}
1596 		} else if (!escape && c == '\\') {
1597 			escape = true;
1598 			continue;
1599 		}
1600 		escape = false;
1601 		/*
1602 		 * Level 1 escape processing complete.
1603 		 * If comma is set perform comma escape processing.
1604 		 *
1605 		 * Level 1	Level 2		ALPN's
1606 		 * h1\,h2   =>	h1,h2   =>	h1 and h2
1607 		 * h1\\,h2  =>	h1\,h2  =>	h1,h2
1608 		 * h1\\h2   =>	h1\h2   =>	h1h2
1609 		 * h1\\\\h2 =>	h1\\h2  =>	h1\h2
1610 		 */
1611 		if (comma && !comma_escape && c == ',') {
1612 			seen_comma = true;
1613 			break;
1614 		}
1615 		if (comma && !comma_escape && c == '\\') {
1616 			comma_escape = true;
1617 			continue;
1618 		}
1619 		comma_escape = false;
1620 		if (nrem == 0) {
1621 			return ((tregion.length <= 256U) ? ISC_R_NOSPACE
1622 							 : DNS_R_SYNTAX);
1623 		}
1624 		*t++ = c;
1625 		nrem--;
1626 	}
1627 
1628 	/*
1629 	 * Incomplete escape processing?
1630 	 */
1631 	if (escape || (comma && comma_escape)) {
1632 		return (DNS_R_SYNTAX);
1633 	}
1634 
1635 	if (comma) {
1636 		/*
1637 		 * Disallow empty ALPN at start (",h1") or in the
1638 		 * middle ("h1,,h2").
1639 		 */
1640 		if (s == source->base || (seen_comma && s == source->base + 1))
1641 		{
1642 			return (DNS_R_SYNTAX);
1643 		}
1644 		isc_textregion_consume(source, s - source->base);
1645 		/*
1646 		 * Disallow empty ALPN at end ("h1,").
1647 		 */
1648 		if (seen_comma && source->length == 0) {
1649 			return (DNS_R_SYNTAX);
1650 		}
1651 	}
1652 	*tregion.base = (unsigned char)(t - tregion.base - 1);
1653 	isc_buffer_add(target, *tregion.base + 1);
1654 	return (ISC_R_SUCCESS);
1655 }
1656 
1657 static isc_result_t
txt_fromtext(isc_textregion_t * source,isc_buffer_t * target)1658 txt_fromtext(isc_textregion_t *source, isc_buffer_t *target) {
1659 	return (commatxt_fromtext(source, false, target));
1660 }
1661 
1662 static isc_result_t
txt_fromwire(isc_buffer_t * source,isc_buffer_t * target)1663 txt_fromwire(isc_buffer_t *source, isc_buffer_t *target) {
1664 	unsigned int n;
1665 	isc_region_t sregion;
1666 	isc_region_t tregion;
1667 
1668 	isc_buffer_activeregion(source, &sregion);
1669 	if (sregion.length == 0) {
1670 		return (ISC_R_UNEXPECTEDEND);
1671 	}
1672 	n = *sregion.base + 1;
1673 	if (n > sregion.length) {
1674 		return (ISC_R_UNEXPECTEDEND);
1675 	}
1676 
1677 	isc_buffer_availableregion(target, &tregion);
1678 	if (n > tregion.length) {
1679 		return (ISC_R_NOSPACE);
1680 	}
1681 
1682 	if (tregion.base != sregion.base) {
1683 		memmove(tregion.base, sregion.base, n);
1684 	}
1685 	isc_buffer_forward(source, n);
1686 	isc_buffer_add(target, n);
1687 	return (ISC_R_SUCCESS);
1688 }
1689 
1690 /*
1691  * Conversion of TXT-like rdata fields without length limits.
1692  */
1693 static isc_result_t
multitxt_totext(isc_region_t * source,isc_buffer_t * target)1694 multitxt_totext(isc_region_t *source, isc_buffer_t *target) {
1695 	unsigned int tl;
1696 	unsigned int n0, n;
1697 	unsigned char *sp;
1698 	char *tp;
1699 	isc_region_t region;
1700 
1701 	isc_buffer_availableregion(target, &region);
1702 	sp = source->base;
1703 	tp = (char *)region.base;
1704 	tl = region.length;
1705 
1706 	if (tl < 1) {
1707 		return (ISC_R_NOSPACE);
1708 	}
1709 	*tp++ = '"';
1710 	tl--;
1711 	do {
1712 		n = source->length;
1713 		n0 = source->length - 1;
1714 
1715 		while (n--) {
1716 			if (*sp < ' ' || *sp >= 0x7f) {
1717 				if (tl < 4) {
1718 					return (ISC_R_NOSPACE);
1719 				}
1720 				*tp++ = '\\';
1721 				*tp++ = '0' + ((*sp / 100) % 10);
1722 				*tp++ = '0' + ((*sp / 10) % 10);
1723 				*tp++ = '0' + (*sp % 10);
1724 				sp++;
1725 				tl -= 4;
1726 				continue;
1727 			}
1728 			/* double quote, backslash */
1729 			if (*sp == '"' || *sp == '\\') {
1730 				if (tl < 2) {
1731 					return (ISC_R_NOSPACE);
1732 				}
1733 				*tp++ = '\\';
1734 				tl--;
1735 			}
1736 			if (tl < 1) {
1737 				return (ISC_R_NOSPACE);
1738 			}
1739 			*tp++ = *sp++;
1740 			tl--;
1741 		}
1742 		isc_region_consume(source, n0 + 1);
1743 	} while (source->length != 0);
1744 	if (tl < 1) {
1745 		return (ISC_R_NOSPACE);
1746 	}
1747 	*tp++ = '"';
1748 	tl--;
1749 	POST(tl);
1750 	isc_buffer_add(target, (unsigned int)(tp - (char *)region.base));
1751 	return (ISC_R_SUCCESS);
1752 }
1753 
1754 static isc_result_t
multitxt_fromtext(isc_textregion_t * source,isc_buffer_t * target)1755 multitxt_fromtext(isc_textregion_t *source, isc_buffer_t *target) {
1756 	isc_region_t tregion;
1757 	bool escape;
1758 	unsigned int n, nrem;
1759 	char *s;
1760 	unsigned char *t0, *t;
1761 	int d;
1762 	int c;
1763 
1764 	s = source->base;
1765 	n = source->length;
1766 	escape = false;
1767 
1768 	do {
1769 		isc_buffer_availableregion(target, &tregion);
1770 		t0 = t = tregion.base;
1771 		nrem = tregion.length;
1772 		if (nrem < 1) {
1773 			return (ISC_R_NOSPACE);
1774 		}
1775 
1776 		while (n != 0) {
1777 			--n;
1778 			c = (*s++) & 0xff;
1779 			if (escape && (d = decvalue((char)c)) != -1) {
1780 				c = d;
1781 				if (n == 0) {
1782 					return (DNS_R_SYNTAX);
1783 				}
1784 				n--;
1785 				if ((d = decvalue(*s++)) != -1) {
1786 					c = c * 10 + d;
1787 				} else {
1788 					return (DNS_R_SYNTAX);
1789 				}
1790 				if (n == 0) {
1791 					return (DNS_R_SYNTAX);
1792 				}
1793 				n--;
1794 				if ((d = decvalue(*s++)) != -1) {
1795 					c = c * 10 + d;
1796 				} else {
1797 					return (DNS_R_SYNTAX);
1798 				}
1799 				if (c > 255) {
1800 					return (DNS_R_SYNTAX);
1801 				}
1802 			} else if (!escape && c == '\\') {
1803 				escape = true;
1804 				continue;
1805 			}
1806 			escape = false;
1807 			*t++ = c;
1808 			nrem--;
1809 			if (nrem == 0) {
1810 				break;
1811 			}
1812 		}
1813 		if (escape) {
1814 			return (DNS_R_SYNTAX);
1815 		}
1816 
1817 		isc_buffer_add(target, (unsigned int)(t - t0));
1818 	} while (n != 0);
1819 	return (ISC_R_SUCCESS);
1820 }
1821 
1822 static bool
name_prefix(dns_name_t * name,const dns_name_t * origin,dns_name_t * target)1823 name_prefix(dns_name_t *name, const dns_name_t *origin, dns_name_t *target) {
1824 	int l1, l2;
1825 
1826 	if (origin == NULL) {
1827 		goto return_false;
1828 	}
1829 
1830 	if (dns_name_compare(origin, dns_rootname) == 0) {
1831 		goto return_false;
1832 	}
1833 
1834 	if (!dns_name_issubdomain(name, origin)) {
1835 		goto return_false;
1836 	}
1837 
1838 	l1 = dns_name_countlabels(name);
1839 	l2 = dns_name_countlabels(origin);
1840 
1841 	if (l1 == l2) {
1842 		goto return_false;
1843 	}
1844 
1845 	/* Master files should be case preserving. */
1846 	dns_name_getlabelsequence(name, l1 - l2, l2, target);
1847 	if (!dns_name_caseequal(origin, target)) {
1848 		goto return_false;
1849 	}
1850 
1851 	dns_name_getlabelsequence(name, 0, l1 - l2, target);
1852 	return (true);
1853 
1854 return_false:
1855 	*target = *name;
1856 	return (false);
1857 }
1858 
1859 static isc_result_t
str_totext(const char * source,isc_buffer_t * target)1860 str_totext(const char *source, isc_buffer_t *target) {
1861 	unsigned int l;
1862 	isc_region_t region;
1863 
1864 	isc_buffer_availableregion(target, &region);
1865 	l = strlen(source);
1866 
1867 	if (l > region.length) {
1868 		return (ISC_R_NOSPACE);
1869 	}
1870 
1871 	memmove(region.base, source, l);
1872 	isc_buffer_add(target, l);
1873 	return (ISC_R_SUCCESS);
1874 }
1875 
1876 static isc_result_t
inet_totext(int af,uint32_t flags,isc_region_t * src,isc_buffer_t * target)1877 inet_totext(int af, uint32_t flags, isc_region_t *src, isc_buffer_t *target) {
1878 	char tmpbuf[64];
1879 
1880 	/* Note - inet_ntop doesn't do size checking on its input. */
1881 	if (inet_ntop(af, src->base, tmpbuf, sizeof(tmpbuf)) == NULL) {
1882 		return (ISC_R_NOSPACE);
1883 	}
1884 	if (strlen(tmpbuf) > isc_buffer_availablelength(target)) {
1885 		return (ISC_R_NOSPACE);
1886 	}
1887 	isc_buffer_putstr(target, tmpbuf);
1888 
1889 	/*
1890 	 * An IPv6 address ending in "::" breaks YAML
1891 	 * parsing, so append 0 in that case.
1892 	 */
1893 	if (af == AF_INET6 && (flags & DNS_STYLEFLAG_YAML) != 0) {
1894 		isc_textregion_t tr;
1895 		isc_buffer_usedregion(target, (isc_region_t *)&tr);
1896 		if (tr.base[tr.length - 1] == ':') {
1897 			if (isc_buffer_availablelength(target) == 0) {
1898 				return (ISC_R_NOSPACE);
1899 			}
1900 			isc_buffer_putmem(target, (const unsigned char *)"0",
1901 					  1);
1902 		}
1903 	}
1904 
1905 	return (ISC_R_SUCCESS);
1906 }
1907 
1908 static bool
buffer_empty(isc_buffer_t * source)1909 buffer_empty(isc_buffer_t *source) {
1910 	return ((source->current == source->active) ? true : false);
1911 }
1912 
1913 static void
buffer_fromregion(isc_buffer_t * buffer,isc_region_t * region)1914 buffer_fromregion(isc_buffer_t *buffer, isc_region_t *region) {
1915 	isc_buffer_init(buffer, region->base, region->length);
1916 	isc_buffer_add(buffer, region->length);
1917 	isc_buffer_setactive(buffer, region->length);
1918 }
1919 
1920 static isc_result_t
uint32_tobuffer(uint32_t value,isc_buffer_t * target)1921 uint32_tobuffer(uint32_t value, isc_buffer_t *target) {
1922 	isc_region_t region;
1923 
1924 	isc_buffer_availableregion(target, &region);
1925 	if (region.length < 4) {
1926 		return (ISC_R_NOSPACE);
1927 	}
1928 	isc_buffer_putuint32(target, value);
1929 	return (ISC_R_SUCCESS);
1930 }
1931 
1932 static isc_result_t
uint16_tobuffer(uint32_t value,isc_buffer_t * target)1933 uint16_tobuffer(uint32_t value, isc_buffer_t *target) {
1934 	isc_region_t region;
1935 
1936 	if (value > 0xffff) {
1937 		return (ISC_R_RANGE);
1938 	}
1939 	isc_buffer_availableregion(target, &region);
1940 	if (region.length < 2) {
1941 		return (ISC_R_NOSPACE);
1942 	}
1943 	isc_buffer_putuint16(target, (uint16_t)value);
1944 	return (ISC_R_SUCCESS);
1945 }
1946 
1947 static isc_result_t
uint8_tobuffer(uint32_t value,isc_buffer_t * target)1948 uint8_tobuffer(uint32_t value, isc_buffer_t *target) {
1949 	isc_region_t region;
1950 
1951 	if (value > 0xff) {
1952 		return (ISC_R_RANGE);
1953 	}
1954 	isc_buffer_availableregion(target, &region);
1955 	if (region.length < 1) {
1956 		return (ISC_R_NOSPACE);
1957 	}
1958 	isc_buffer_putuint8(target, (uint8_t)value);
1959 	return (ISC_R_SUCCESS);
1960 }
1961 
1962 static isc_result_t
name_tobuffer(const dns_name_t * name,isc_buffer_t * target)1963 name_tobuffer(const dns_name_t *name, isc_buffer_t *target) {
1964 	isc_region_t r;
1965 	dns_name_toregion(name, &r);
1966 	return (isc_buffer_copyregion(target, &r));
1967 }
1968 
1969 static uint32_t
uint32_fromregion(isc_region_t * region)1970 uint32_fromregion(isc_region_t *region) {
1971 	uint32_t value;
1972 
1973 	REQUIRE(region->length >= 4);
1974 	value = (uint32_t)region->base[0] << 24;
1975 	value |= (uint32_t)region->base[1] << 16;
1976 	value |= (uint32_t)region->base[2] << 8;
1977 	value |= (uint32_t)region->base[3];
1978 	return (value);
1979 }
1980 
1981 static uint16_t
uint16_consume_fromregion(isc_region_t * region)1982 uint16_consume_fromregion(isc_region_t *region) {
1983 	uint16_t r = uint16_fromregion(region);
1984 
1985 	isc_region_consume(region, 2);
1986 	return (r);
1987 }
1988 
1989 static uint16_t
uint16_fromregion(isc_region_t * region)1990 uint16_fromregion(isc_region_t *region) {
1991 	REQUIRE(region->length >= 2);
1992 
1993 	return ((region->base[0] << 8) | region->base[1]);
1994 }
1995 
1996 static uint8_t
uint8_fromregion(isc_region_t * region)1997 uint8_fromregion(isc_region_t *region) {
1998 	REQUIRE(region->length >= 1);
1999 
2000 	return (region->base[0]);
2001 }
2002 
2003 static uint8_t
uint8_consume_fromregion(isc_region_t * region)2004 uint8_consume_fromregion(isc_region_t *region) {
2005 	uint8_t r = uint8_fromregion(region);
2006 
2007 	isc_region_consume(region, 1);
2008 	return (r);
2009 }
2010 
2011 static isc_result_t
mem_tobuffer(isc_buffer_t * target,void * base,unsigned int length)2012 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
2013 	isc_region_t tr;
2014 
2015 	if (length == 0U) {
2016 		return (ISC_R_SUCCESS);
2017 	}
2018 
2019 	isc_buffer_availableregion(target, &tr);
2020 	if (length > tr.length) {
2021 		return (ISC_R_NOSPACE);
2022 	}
2023 	if (tr.base != base) {
2024 		memmove(tr.base, base, length);
2025 	}
2026 	isc_buffer_add(target, length);
2027 	return (ISC_R_SUCCESS);
2028 }
2029 
2030 static int
hexvalue(char value)2031 hexvalue(char value) {
2032 	const char *s;
2033 	unsigned char c;
2034 
2035 	c = (unsigned char)value;
2036 
2037 	if (!isascii(c)) {
2038 		return (-1);
2039 	}
2040 	if (isupper(c)) {
2041 		c = tolower(c);
2042 	}
2043 	if ((s = strchr(hexdigits, c)) == NULL) {
2044 		return (-1);
2045 	}
2046 	return ((int)(s - hexdigits));
2047 }
2048 
2049 static int
decvalue(char value)2050 decvalue(char value) {
2051 	const char *s;
2052 
2053 	/*
2054 	 * isascii() is valid for full range of int values, no need to
2055 	 * mask or cast.
2056 	 */
2057 	if (!isascii(value)) {
2058 		return (-1);
2059 	}
2060 	if ((s = strchr(decdigits, value)) == NULL) {
2061 		return (-1);
2062 	}
2063 	return ((int)(s - decdigits));
2064 }
2065 
2066 static void
default_fromtext_callback(dns_rdatacallbacks_t * callbacks,const char * fmt,...)2067 default_fromtext_callback(dns_rdatacallbacks_t *callbacks, const char *fmt,
2068 			  ...) {
2069 	va_list ap;
2070 
2071 	UNUSED(callbacks);
2072 
2073 	va_start(ap, fmt);
2074 	vfprintf(stderr, fmt, ap);
2075 	va_end(ap);
2076 	fprintf(stderr, "\n");
2077 }
2078 
2079 static void
fromtext_warneof(isc_lex_t * lexer,dns_rdatacallbacks_t * callbacks)2080 fromtext_warneof(isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks) {
2081 	if (isc_lex_isfile(lexer) && callbacks != NULL) {
2082 		const char *name = isc_lex_getsourcename(lexer);
2083 		if (name == NULL) {
2084 			name = "UNKNOWN";
2085 		}
2086 		(*callbacks->warn)(callbacks,
2087 				   "%s:%lu: file does not end with newline",
2088 				   name, isc_lex_getsourceline(lexer));
2089 	}
2090 }
2091 
2092 static void
warn_badmx(isc_token_t * token,isc_lex_t * lexer,dns_rdatacallbacks_t * callbacks)2093 warn_badmx(isc_token_t *token, isc_lex_t *lexer,
2094 	   dns_rdatacallbacks_t *callbacks) {
2095 	const char *file;
2096 	unsigned long line;
2097 
2098 	if (lexer != NULL) {
2099 		file = isc_lex_getsourcename(lexer);
2100 		line = isc_lex_getsourceline(lexer);
2101 		(*callbacks->warn)(callbacks, "%s:%u: warning: '%s': %s", file,
2102 				   line, DNS_AS_STR(*token),
2103 				   isc_result_totext(DNS_R_MXISADDRESS));
2104 	}
2105 }
2106 
2107 static void
warn_badname(const dns_name_t * name,isc_lex_t * lexer,dns_rdatacallbacks_t * callbacks)2108 warn_badname(const dns_name_t *name, isc_lex_t *lexer,
2109 	     dns_rdatacallbacks_t *callbacks) {
2110 	const char *file;
2111 	unsigned long line;
2112 	char namebuf[DNS_NAME_FORMATSIZE];
2113 
2114 	if (lexer != NULL) {
2115 		file = isc_lex_getsourcename(lexer);
2116 		line = isc_lex_getsourceline(lexer);
2117 		dns_name_format(name, namebuf, sizeof(namebuf));
2118 		(*callbacks->warn)(callbacks, "%s:%u: warning: %s: %s", file,
2119 				   line, namebuf,
2120 				   isc_result_totext(DNS_R_BADNAME));
2121 	}
2122 }
2123 
2124 static void
fromtext_error(void (* callback)(dns_rdatacallbacks_t *,const char *,...),dns_rdatacallbacks_t * callbacks,const char * name,unsigned long line,isc_token_t * token,isc_result_t result)2125 fromtext_error(void (*callback)(dns_rdatacallbacks_t *, const char *, ...),
2126 	       dns_rdatacallbacks_t *callbacks, const char *name,
2127 	       unsigned long line, isc_token_t *token, isc_result_t result) {
2128 	if (name == NULL) {
2129 		name = "UNKNOWN";
2130 	}
2131 
2132 	if (token != NULL) {
2133 		switch (token->type) {
2134 		case isc_tokentype_eol:
2135 			(*callback)(callbacks, "%s: %s:%lu: near eol: %s",
2136 				    "dns_rdata_fromtext", name, line,
2137 				    isc_result_totext(result));
2138 			break;
2139 		case isc_tokentype_eof:
2140 			(*callback)(callbacks, "%s: %s:%lu: near eof: %s",
2141 				    "dns_rdata_fromtext", name, line,
2142 				    isc_result_totext(result));
2143 			break;
2144 		case isc_tokentype_number:
2145 			(*callback)(callbacks, "%s: %s:%lu: near %lu: %s",
2146 				    "dns_rdata_fromtext", name, line,
2147 				    token->value.as_ulong,
2148 				    isc_result_totext(result));
2149 			break;
2150 		case isc_tokentype_string:
2151 		case isc_tokentype_qstring:
2152 			(*callback)(callbacks, "%s: %s:%lu: near '%s': %s",
2153 				    "dns_rdata_fromtext", name, line,
2154 				    DNS_AS_STR(*token),
2155 				    isc_result_totext(result));
2156 			break;
2157 		default:
2158 			(*callback)(callbacks, "%s: %s:%lu: %s",
2159 				    "dns_rdata_fromtext", name, line,
2160 				    isc_result_totext(result));
2161 			break;
2162 		}
2163 	} else {
2164 		(*callback)(callbacks, "dns_rdata_fromtext: %s:%lu: %s", name,
2165 			    line, isc_result_totext(result));
2166 	}
2167 }
2168 
2169 dns_rdatatype_t
dns_rdata_covers(dns_rdata_t * rdata)2170 dns_rdata_covers(dns_rdata_t *rdata) {
2171 	if (rdata->type == dns_rdatatype_rrsig) {
2172 		return (covers_rrsig(rdata));
2173 	}
2174 	return (covers_sig(rdata));
2175 }
2176 
2177 bool
dns_rdatatype_ismeta(dns_rdatatype_t type)2178 dns_rdatatype_ismeta(dns_rdatatype_t type) {
2179 	if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_META) != 0) {
2180 		return (true);
2181 	}
2182 	return (false);
2183 }
2184 
2185 bool
dns_rdatatype_issingleton(dns_rdatatype_t type)2186 dns_rdatatype_issingleton(dns_rdatatype_t type) {
2187 	if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_SINGLETON) != 0)
2188 	{
2189 		return (true);
2190 	}
2191 	return (false);
2192 }
2193 
2194 bool
dns_rdatatype_notquestion(dns_rdatatype_t type)2195 dns_rdatatype_notquestion(dns_rdatatype_t type) {
2196 	if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_NOTQUESTION) !=
2197 	    0) {
2198 		return (true);
2199 	}
2200 	return (false);
2201 }
2202 
2203 bool
dns_rdatatype_questiononly(dns_rdatatype_t type)2204 dns_rdatatype_questiononly(dns_rdatatype_t type) {
2205 	if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_QUESTIONONLY) !=
2206 	    0) {
2207 		return (true);
2208 	}
2209 	return (false);
2210 }
2211 
2212 bool
dns_rdatatype_atcname(dns_rdatatype_t type)2213 dns_rdatatype_atcname(dns_rdatatype_t type) {
2214 	if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_ATCNAME) != 0) {
2215 		return (true);
2216 	}
2217 	return (false);
2218 }
2219 
2220 bool
dns_rdatatype_atparent(dns_rdatatype_t type)2221 dns_rdatatype_atparent(dns_rdatatype_t type) {
2222 	if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_ATPARENT) != 0)
2223 	{
2224 		return (true);
2225 	}
2226 	return (false);
2227 }
2228 
2229 bool
dns_rdatatype_followadditional(dns_rdatatype_t type)2230 dns_rdatatype_followadditional(dns_rdatatype_t type) {
2231 	if ((dns_rdatatype_attributes(type) &
2232 	     DNS_RDATATYPEATTR_FOLLOWADDITIONAL) != 0) {
2233 		return (true);
2234 	}
2235 	return (false);
2236 }
2237 
2238 bool
dns_rdataclass_ismeta(dns_rdataclass_t rdclass)2239 dns_rdataclass_ismeta(dns_rdataclass_t rdclass) {
2240 	if (rdclass == dns_rdataclass_reserved0 ||
2241 	    rdclass == dns_rdataclass_none || rdclass == dns_rdataclass_any)
2242 	{
2243 		return (true);
2244 	}
2245 
2246 	return (false); /* Assume it is not a meta class. */
2247 }
2248 
2249 bool
dns_rdatatype_isdnssec(dns_rdatatype_t type)2250 dns_rdatatype_isdnssec(dns_rdatatype_t type) {
2251 	if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_DNSSEC) != 0) {
2252 		return (true);
2253 	}
2254 	return (false);
2255 }
2256 
2257 bool
dns_rdatatype_iszonecutauth(dns_rdatatype_t type)2258 dns_rdatatype_iszonecutauth(dns_rdatatype_t type) {
2259 	if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_ZONECUTAUTH) !=
2260 	    0) {
2261 		return (true);
2262 	}
2263 	return (false);
2264 }
2265 
2266 bool
dns_rdatatype_isknown(dns_rdatatype_t type)2267 dns_rdatatype_isknown(dns_rdatatype_t type) {
2268 	if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_UNKNOWN) == 0) {
2269 		return (true);
2270 	}
2271 	return (false);
2272 }
2273 
2274 void
dns_rdata_exists(dns_rdata_t * rdata,dns_rdatatype_t type)2275 dns_rdata_exists(dns_rdata_t *rdata, dns_rdatatype_t type) {
2276 	REQUIRE(rdata != NULL);
2277 	REQUIRE(DNS_RDATA_INITIALIZED(rdata));
2278 
2279 	rdata->data = NULL;
2280 	rdata->length = 0;
2281 	rdata->flags = DNS_RDATA_UPDATE;
2282 	rdata->type = type;
2283 	rdata->rdclass = dns_rdataclass_any;
2284 }
2285 
2286 void
dns_rdata_notexist(dns_rdata_t * rdata,dns_rdatatype_t type)2287 dns_rdata_notexist(dns_rdata_t *rdata, dns_rdatatype_t type) {
2288 	REQUIRE(rdata != NULL);
2289 	REQUIRE(DNS_RDATA_INITIALIZED(rdata));
2290 
2291 	rdata->data = NULL;
2292 	rdata->length = 0;
2293 	rdata->flags = DNS_RDATA_UPDATE;
2294 	rdata->type = type;
2295 	rdata->rdclass = dns_rdataclass_none;
2296 }
2297 
2298 void
dns_rdata_deleterrset(dns_rdata_t * rdata,dns_rdatatype_t type)2299 dns_rdata_deleterrset(dns_rdata_t *rdata, dns_rdatatype_t type) {
2300 	REQUIRE(rdata != NULL);
2301 	REQUIRE(DNS_RDATA_INITIALIZED(rdata));
2302 
2303 	rdata->data = NULL;
2304 	rdata->length = 0;
2305 	rdata->flags = DNS_RDATA_UPDATE;
2306 	rdata->type = type;
2307 	rdata->rdclass = dns_rdataclass_any;
2308 }
2309 
2310 void
dns_rdata_makedelete(dns_rdata_t * rdata)2311 dns_rdata_makedelete(dns_rdata_t *rdata) {
2312 	REQUIRE(rdata != NULL);
2313 
2314 	rdata->rdclass = dns_rdataclass_none;
2315 }
2316 
2317 const char *
dns_rdata_updateop(dns_rdata_t * rdata,dns_section_t section)2318 dns_rdata_updateop(dns_rdata_t *rdata, dns_section_t section) {
2319 	REQUIRE(rdata != NULL);
2320 	REQUIRE(DNS_RDATA_INITIALIZED(rdata));
2321 
2322 	switch (section) {
2323 	case DNS_SECTION_PREREQUISITE:
2324 		switch (rdata->rdclass) {
2325 		case dns_rdataclass_none:
2326 			switch (rdata->type) {
2327 			case dns_rdatatype_any:
2328 				return ("domain doesn't exist");
2329 			default:
2330 				return ("rrset doesn't exist");
2331 			}
2332 		case dns_rdataclass_any:
2333 			switch (rdata->type) {
2334 			case dns_rdatatype_any:
2335 				return ("domain exists");
2336 			default:
2337 				return ("rrset exists (value independent)");
2338 			}
2339 		default:
2340 			return ("rrset exists (value dependent)");
2341 		}
2342 	case DNS_SECTION_UPDATE:
2343 		switch (rdata->rdclass) {
2344 		case dns_rdataclass_none:
2345 			return ("delete");
2346 		case dns_rdataclass_any:
2347 			switch (rdata->type) {
2348 			case dns_rdatatype_any:
2349 				return ("delete all rrsets");
2350 			default:
2351 				return ("delete rrset");
2352 			}
2353 		default:
2354 			return ("add");
2355 		}
2356 	}
2357 	return ("invalid");
2358 }
2359