]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - contrib/bind9/lib/dns/rdata.c
Upgrade to 9.6-ESV-R7-P3:
[FreeBSD/stable/8.git] / contrib / bind9 / lib / dns / rdata.c
1 /*
2  * Copyright (C) 2004-2009, 2011, 2012  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1998-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id$ */
19
20 /*! \file */
21
22 #include <config.h>
23 #include <ctype.h>
24
25 #include <isc/base64.h>
26 #include <isc/hex.h>
27 #include <isc/lex.h>
28 #include <isc/mem.h>
29 #include <isc/parseint.h>
30 #include <isc/print.h>
31 #include <isc/string.h>
32 #include <isc/stdlib.h>
33 #include <isc/util.h>
34
35 #include <dns/callbacks.h>
36 #include <dns/cert.h>
37 #include <dns/compress.h>
38 #include <dns/enumtype.h>
39 #include <dns/keyflags.h>
40 #include <dns/keyvalues.h>
41 #include <dns/rcode.h>
42 #include <dns/rdata.h>
43 #include <dns/rdataclass.h>
44 #include <dns/rdatastruct.h>
45 #include <dns/rdatatype.h>
46 #include <dns/result.h>
47 #include <dns/secalg.h>
48 #include <dns/secproto.h>
49 #include <dns/time.h>
50 #include <dns/ttl.h>
51
52 #define RETERR(x) \
53         do { \
54                 isc_result_t _r = (x); \
55                 if (_r != ISC_R_SUCCESS) \
56                         return (_r); \
57         } while (0)
58
59 #define RETTOK(x) \
60         do { \
61                 isc_result_t _r = (x); \
62                 if (_r != ISC_R_SUCCESS) { \
63                         isc_lex_ungettoken(lexer, &token); \
64                         return (_r); \
65                 } \
66         } while (0)
67
68 #define DNS_AS_STR(t) ((t).value.as_textregion.base)
69
70 #define ARGS_FROMTEXT   int rdclass, dns_rdatatype_t type, \
71                         isc_lex_t *lexer, dns_name_t *origin, \
72                         unsigned int options, isc_buffer_t *target, \
73                         dns_rdatacallbacks_t *callbacks
74
75 #define ARGS_TOTEXT     dns_rdata_t *rdata, dns_rdata_textctx_t *tctx, \
76                         isc_buffer_t *target
77
78 #define ARGS_FROMWIRE   int rdclass, dns_rdatatype_t type, \
79                         isc_buffer_t *source, dns_decompress_t *dctx, \
80                         unsigned int options, isc_buffer_t *target
81
82 #define ARGS_TOWIRE     dns_rdata_t *rdata, dns_compress_t *cctx, \
83                         isc_buffer_t *target
84
85 #define ARGS_COMPARE    const dns_rdata_t *rdata1, const dns_rdata_t *rdata2
86
87 #define ARGS_FROMSTRUCT int rdclass, dns_rdatatype_t type, \
88                         void *source, isc_buffer_t *target
89
90 #define ARGS_TOSTRUCT   dns_rdata_t *rdata, void *target, isc_mem_t *mctx
91
92 #define ARGS_FREESTRUCT void *source
93
94 #define ARGS_ADDLDATA   dns_rdata_t *rdata, dns_additionaldatafunc_t add, \
95                         void *arg
96
97 #define ARGS_DIGEST     dns_rdata_t *rdata, dns_digestfunc_t digest, void *arg
98
99 #define ARGS_CHECKOWNER dns_name_t *name, dns_rdataclass_t rdclass, \
100                         dns_rdatatype_t type, isc_boolean_t wildcard
101
102 #define ARGS_CHECKNAMES dns_rdata_t *rdata, dns_name_t *owner, dns_name_t *bad
103
104
105 /*%
106  * Context structure for the totext_ functions.
107  * Contains formatting options for rdata-to-text
108  * conversion.
109  */
110 typedef struct dns_rdata_textctx {
111         dns_name_t *origin;     /*%< Current origin, or NULL. */
112         unsigned int flags;     /*%< DNS_STYLEFLAG_*  */
113         unsigned int width;     /*%< Width of rdata column. */
114         const char *linebreak;  /*%< Line break string. */
115 } dns_rdata_textctx_t;
116
117 static isc_result_t
118 txt_totext(isc_region_t *source, isc_buffer_t *target);
119
120 static isc_result_t
121 txt_fromtext(isc_textregion_t *source, isc_buffer_t *target);
122
123 static isc_result_t
124 txt_fromwire(isc_buffer_t *source, isc_buffer_t *target);
125
126 static isc_boolean_t
127 name_prefix(dns_name_t *name, dns_name_t *origin, dns_name_t *target);
128
129 static unsigned int
130 name_length(dns_name_t *name);
131
132 static isc_result_t
133 str_totext(const char *source, isc_buffer_t *target);
134
135 static isc_result_t
136 inet_totext(int af, isc_region_t *src, isc_buffer_t *target);
137
138 static isc_boolean_t
139 buffer_empty(isc_buffer_t *source);
140
141 static void
142 buffer_fromregion(isc_buffer_t *buffer, isc_region_t *region);
143
144 static isc_result_t
145 uint32_tobuffer(isc_uint32_t, isc_buffer_t *target);
146
147 static isc_result_t
148 uint16_tobuffer(isc_uint32_t, isc_buffer_t *target);
149
150 static isc_result_t
151 uint8_tobuffer(isc_uint32_t, isc_buffer_t *target);
152
153 static isc_result_t
154 name_tobuffer(dns_name_t *name, isc_buffer_t *target);
155
156 static isc_uint32_t
157 uint32_fromregion(isc_region_t *region);
158
159 static isc_uint16_t
160 uint16_fromregion(isc_region_t *region);
161
162 static isc_uint8_t
163 uint8_fromregion(isc_region_t *region);
164
165 static isc_uint8_t
166 uint8_consume_fromregion(isc_region_t *region);
167
168 static isc_result_t
169 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length);
170
171 static int
172 hexvalue(char value);
173
174 static int
175 decvalue(char value);
176
177 static isc_result_t
178 btoa_totext(unsigned char *inbuf, int inbuflen, isc_buffer_t *target);
179
180 static isc_result_t
181 atob_tobuffer(isc_lex_t *lexer, isc_buffer_t *target);
182
183 static void
184 default_fromtext_callback(dns_rdatacallbacks_t *callbacks, const char *, ...)
185      ISC_FORMAT_PRINTF(2, 3);
186
187 static void
188 fromtext_error(void (*callback)(dns_rdatacallbacks_t *, const char *, ...),
189                dns_rdatacallbacks_t *callbacks, const char *name,
190                unsigned long line, isc_token_t *token, isc_result_t result);
191
192 static void
193 fromtext_warneof(isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks);
194
195 static isc_result_t
196 rdata_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
197              isc_buffer_t *target);
198
199 static void
200 warn_badname(dns_name_t *name, isc_lex_t *lexer,
201              dns_rdatacallbacks_t *callbacks);
202
203 static void
204 warn_badmx(isc_token_t *token, isc_lex_t *lexer,
205            dns_rdatacallbacks_t *callbacks);
206
207 static isc_uint16_t
208 uint16_consume_fromregion(isc_region_t *region);
209
210 static isc_result_t
211 unknown_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
212                isc_buffer_t *target);
213
214 static inline int
215 getquad(const void *src, struct in_addr *dst,
216         isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks)
217 {
218         int result;
219         struct in_addr *tmp;
220
221         result = inet_aton(src, dst);
222         if (result == 1 && callbacks != NULL &&
223             inet_pton(AF_INET, src, &tmp) != 1) {
224                 const char *name = isc_lex_getsourcename(lexer);
225                 if (name == NULL)
226                         name = "UNKNOWN";
227                 (*callbacks->warn)(callbacks, "%s:%lu: \"%s\" "
228                                    "is not a decimal dotted quad", name,
229                                    isc_lex_getsourceline(lexer), src);
230         }
231         return (result);
232 }
233
234 static inline isc_result_t
235 name_duporclone(dns_name_t *source, isc_mem_t *mctx, dns_name_t *target) {
236
237         if (mctx != NULL)
238                 return (dns_name_dup(source, mctx, target));
239         dns_name_clone(source, target);
240         return (ISC_R_SUCCESS);
241 }
242
243 static inline void *
244 mem_maybedup(isc_mem_t *mctx, void *source, size_t length) {
245         void *new;
246
247         if (mctx == NULL)
248                 return (source);
249         new = isc_mem_allocate(mctx, length);
250         if (new != NULL)
251                 memcpy(new, source, length);
252
253         return (new);
254 }
255
256 static const char hexdigits[] = "0123456789abcdef";
257 static const char decdigits[] = "0123456789";
258
259 #include "code.h"
260
261 #define META 0x0001
262 #define RESERVED 0x0002
263
264 /***
265  *** Initialization
266  ***/
267
268 void
269 dns_rdata_init(dns_rdata_t *rdata) {
270
271         REQUIRE(rdata != NULL);
272
273         rdata->data = NULL;
274         rdata->length = 0;
275         rdata->rdclass = 0;
276         rdata->type = 0;
277         rdata->flags = 0;
278         ISC_LINK_INIT(rdata, link);
279         /* ISC_LIST_INIT(rdata->list); */
280 }
281
282 #if 1
283 #define DNS_RDATA_INITIALIZED(rdata) \
284         ((rdata)->data == NULL && (rdata)->length == 0 && \
285          (rdata)->rdclass == 0 && (rdata)->type == 0 && (rdata)->flags == 0 && \
286          !ISC_LINK_LINKED((rdata), link))
287 #else
288 #ifdef ISC_LIST_CHECKINIT
289 #define DNS_RDATA_INITIALIZED(rdata) \
290         (!ISC_LINK_LINKED((rdata), link))
291 #else
292 #define DNS_RDATA_INITIALIZED(rdata) ISC_TRUE
293 #endif
294 #endif
295
296 #define DNS_RDATA_VALIDFLAGS(rdata) \
297         (((rdata)->flags & ~(DNS_RDATA_UPDATE|DNS_RDATA_OFFLINE)) == 0)
298
299 void
300 dns_rdata_reset(dns_rdata_t *rdata) {
301
302         REQUIRE(rdata != NULL);
303
304         REQUIRE(!ISC_LINK_LINKED(rdata, link));
305         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
306
307         rdata->data = NULL;
308         rdata->length = 0;
309         rdata->rdclass = 0;
310         rdata->type = 0;
311         rdata->flags = 0;
312 }
313
314 /***
315  ***
316  ***/
317
318 void
319 dns_rdata_clone(const dns_rdata_t *src, dns_rdata_t *target) {
320
321         REQUIRE(src != NULL);
322         REQUIRE(target != NULL);
323
324         REQUIRE(DNS_RDATA_INITIALIZED(target));
325
326         REQUIRE(DNS_RDATA_VALIDFLAGS(src));
327         REQUIRE(DNS_RDATA_VALIDFLAGS(target));
328
329         target->data = src->data;
330         target->length = src->length;
331         target->rdclass = src->rdclass;
332         target->type = src->type;
333         target->flags = src->flags;
334 }
335
336
337 /***
338  *** Comparisons
339  ***/
340
341 int
342 dns_rdata_compare(const dns_rdata_t *rdata1, const dns_rdata_t *rdata2) {
343         int result = 0;
344         isc_boolean_t use_default = ISC_FALSE;
345
346         REQUIRE(rdata1 != NULL);
347         REQUIRE(rdata2 != NULL);
348         REQUIRE(rdata1->length == 0 || rdata1->data != NULL);
349         REQUIRE(rdata2->length == 0 || rdata2->data != NULL);
350         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata1));
351         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata2));
352
353         if (rdata1->rdclass != rdata2->rdclass)
354                 return (rdata1->rdclass < rdata2->rdclass ? -1 : 1);
355
356         if (rdata1->type != rdata2->type)
357                 return (rdata1->type < rdata2->type ? -1 : 1);
358
359         COMPARESWITCH
360
361         if (use_default) {
362                 isc_region_t r1;
363                 isc_region_t r2;
364
365                 dns_rdata_toregion(rdata1, &r1);
366                 dns_rdata_toregion(rdata2, &r2);
367                 result = isc_region_compare(&r1, &r2);
368         }
369         return (result);
370 }
371
372 /***
373  *** Conversions
374  ***/
375
376 void
377 dns_rdata_fromregion(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
378                      dns_rdatatype_t type, isc_region_t *r)
379 {
380
381         REQUIRE(rdata != NULL);
382         REQUIRE(DNS_RDATA_INITIALIZED(rdata));
383         REQUIRE(r != NULL);
384
385         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
386
387         rdata->data = r->base;
388         rdata->length = r->length;
389         rdata->rdclass = rdclass;
390         rdata->type = type;
391         rdata->flags = 0;
392 }
393
394 void
395 dns_rdata_toregion(const dns_rdata_t *rdata, isc_region_t *r) {
396
397         REQUIRE(rdata != NULL);
398         REQUIRE(r != NULL);
399         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
400
401         r->base = rdata->data;
402         r->length = rdata->length;
403 }
404
405 isc_result_t
406 dns_rdata_fromwire(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
407                    dns_rdatatype_t type, isc_buffer_t *source,
408                    dns_decompress_t *dctx, unsigned int options,
409                    isc_buffer_t *target)
410 {
411         isc_result_t result = ISC_R_NOTIMPLEMENTED;
412         isc_region_t region;
413         isc_buffer_t ss;
414         isc_buffer_t st;
415         isc_boolean_t use_default = ISC_FALSE;
416         isc_uint32_t activelength;
417         size_t length;
418
419         REQUIRE(dctx != NULL);
420         if (rdata != NULL) {
421                 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
422                 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
423         }
424
425         if (type == 0)
426                 return (DNS_R_FORMERR);
427
428         ss = *source;
429         st = *target;
430
431         activelength = isc_buffer_activelength(source);
432         INSIST(activelength < 65536);
433
434         FROMWIRESWITCH
435
436         if (use_default) {
437                 if (activelength > isc_buffer_availablelength(target))
438                         result = ISC_R_NOSPACE;
439                 else {
440                         isc_buffer_putmem(target, isc_buffer_current(source),
441                                           activelength);
442                         isc_buffer_forward(source, activelength);
443                         result = ISC_R_SUCCESS;
444                 }
445         }
446
447         /*
448          * Reject any rdata that expands out to more than DNS_RDATA_MAXLENGTH
449          * as we cannot transmit it.
450          */
451         length = isc_buffer_usedlength(target) - isc_buffer_usedlength(&st);
452         if (result == ISC_R_SUCCESS && length > DNS_RDATA_MAXLENGTH)
453                 result = DNS_R_FORMERR;
454
455         /*
456          * We should have consumed all of our buffer.
457          */
458         if (result == ISC_R_SUCCESS && !buffer_empty(source))
459                 result = DNS_R_EXTRADATA;
460
461         if (rdata != NULL && result == ISC_R_SUCCESS) {
462                 region.base = isc_buffer_used(&st);
463                 region.length = length;
464                 dns_rdata_fromregion(rdata, rdclass, type, &region);
465         }
466
467         if (result != ISC_R_SUCCESS) {
468                 *source = ss;
469                 *target = st;
470         }
471         return (result);
472 }
473
474 isc_result_t
475 dns_rdata_towire(dns_rdata_t *rdata, dns_compress_t *cctx,
476                  isc_buffer_t *target)
477 {
478         isc_result_t result = ISC_R_NOTIMPLEMENTED;
479         isc_boolean_t use_default = ISC_FALSE;
480         isc_region_t tr;
481         isc_buffer_t st;
482
483         REQUIRE(rdata != NULL);
484         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
485
486         /*
487          * Some DynDNS meta-RRs have empty rdata.
488          */
489         if ((rdata->flags & DNS_RDATA_UPDATE) != 0) {
490                 INSIST(rdata->length == 0);
491                 return (ISC_R_SUCCESS);
492         }
493
494         st = *target;
495
496         TOWIRESWITCH
497
498         if (use_default) {
499                 isc_buffer_availableregion(target, &tr);
500                 if (tr.length < rdata->length)
501                         return (ISC_R_NOSPACE);
502                 memcpy(tr.base, rdata->data, rdata->length);
503                 isc_buffer_add(target, rdata->length);
504                 return (ISC_R_SUCCESS);
505         }
506         if (result != ISC_R_SUCCESS) {
507                 *target = st;
508                 INSIST(target->used < 65536);
509                 dns_compress_rollback(cctx, (isc_uint16_t)target->used);
510         }
511         return (result);
512 }
513
514 /*
515  * If the binary data in 'src' is valid uncompressed wire format
516  * rdata of class 'rdclass' and type 'type', return ISC_R_SUCCESS
517  * and copy the validated rdata to 'dest'.  Otherwise return an error.
518  */
519 static isc_result_t
520 rdata_validate(isc_buffer_t *src, isc_buffer_t *dest, dns_rdataclass_t rdclass,
521             dns_rdatatype_t type)
522 {
523         dns_decompress_t dctx;
524         dns_rdata_t rdata = DNS_RDATA_INIT;
525         isc_result_t result;
526
527         dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_NONE);
528         isc_buffer_setactive(src, isc_buffer_usedlength(src));
529         result = dns_rdata_fromwire(&rdata, rdclass, type, src,
530                                     &dctx, 0, dest);
531         dns_decompress_invalidate(&dctx);
532
533         return (result);
534 }
535
536 static isc_result_t
537 unknown_fromtext(dns_rdataclass_t rdclass, dns_rdatatype_t type,
538                  isc_lex_t *lexer, isc_mem_t *mctx, isc_buffer_t *target)
539 {
540         isc_result_t result;
541         isc_buffer_t *buf = NULL;
542         isc_token_t token;
543
544         if (type == 0 || dns_rdatatype_ismeta(type))
545                 return (DNS_R_METATYPE);
546
547         result = isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
548                                         ISC_FALSE);
549         if (result == ISC_R_SUCCESS && token.value.as_ulong > 65535U)
550                 return (ISC_R_RANGE);
551         result = isc_buffer_allocate(mctx, &buf, token.value.as_ulong);
552         if (result != ISC_R_SUCCESS)
553                 return (result);
554
555         result = isc_hex_tobuffer(lexer, buf,
556                                   (unsigned int)token.value.as_ulong);
557         if (result != ISC_R_SUCCESS)
558                goto failure;
559         if (isc_buffer_usedlength(buf) != token.value.as_ulong) {
560                 result = ISC_R_UNEXPECTEDEND;
561                 goto failure;
562         }
563
564         if (dns_rdatatype_isknown(type)) {
565                 result = rdata_validate(buf, target, rdclass, type);
566         } else {
567                 isc_region_t r;
568                 isc_buffer_usedregion(buf, &r);
569                 result = isc_buffer_copyregion(target, &r);
570         }
571         if (result != ISC_R_SUCCESS)
572                 goto failure;
573
574         isc_buffer_free(&buf);
575         return (ISC_R_SUCCESS);
576
577  failure:
578         isc_buffer_free(&buf);
579         return (result);
580 }
581
582 isc_result_t
583 dns_rdata_fromtext(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
584                    dns_rdatatype_t type, isc_lex_t *lexer,
585                    dns_name_t *origin, unsigned int options, isc_mem_t *mctx,
586                    isc_buffer_t *target, dns_rdatacallbacks_t *callbacks)
587 {
588         isc_result_t result = ISC_R_NOTIMPLEMENTED;
589         isc_region_t region;
590         isc_buffer_t st;
591         isc_token_t token;
592         unsigned int lexoptions = ISC_LEXOPT_EOL | ISC_LEXOPT_EOF |
593                                   ISC_LEXOPT_DNSMULTILINE | ISC_LEXOPT_ESCAPE;
594         char *name;
595         unsigned long line;
596         void (*callback)(dns_rdatacallbacks_t *, const char *, ...);
597         isc_result_t tresult;
598         size_t length;
599
600         REQUIRE(origin == NULL || dns_name_isabsolute(origin) == ISC_TRUE);
601         if (rdata != NULL) {
602                 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
603                 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
604         }
605         if (callbacks != NULL) {
606                 REQUIRE(callbacks->warn != NULL);
607                 REQUIRE(callbacks->error != NULL);
608         }
609
610         st = *target;
611
612         if (callbacks != NULL)
613                 callback = callbacks->error;
614         else
615                 callback = default_fromtext_callback;
616
617         result = isc_lex_getmastertoken(lexer, &token, isc_tokentype_qstring,
618                                         ISC_FALSE);
619         if (result != ISC_R_SUCCESS) {
620                 name = isc_lex_getsourcename(lexer);
621                 line = isc_lex_getsourceline(lexer);
622                 fromtext_error(callback, callbacks, name, line, NULL, result);
623                 return (result);
624         }
625
626         if (strcmp(DNS_AS_STR(token), "\\#") == 0)
627                 result = unknown_fromtext(rdclass, type, lexer, mctx, target);
628         else {
629                 isc_lex_ungettoken(lexer, &token);
630
631                 FROMTEXTSWITCH
632         }
633
634         /*
635          * Consume to end of line / file.
636          * If not at end of line initially set error code.
637          * Call callback via fromtext_error once if there was an error.
638          */
639         do {
640                 name = isc_lex_getsourcename(lexer);
641                 line = isc_lex_getsourceline(lexer);
642                 tresult = isc_lex_gettoken(lexer, lexoptions, &token);
643                 if (tresult != ISC_R_SUCCESS) {
644                         if (result == ISC_R_SUCCESS)
645                                 result = tresult;
646                         if (callback != NULL)
647                                 fromtext_error(callback, callbacks, name,
648                                                line, NULL, result);
649                         break;
650                 } else if (token.type != isc_tokentype_eol &&
651                            token.type != isc_tokentype_eof) {
652                         if (result == ISC_R_SUCCESS)
653                                 result = DNS_R_EXTRATOKEN;
654                         if (callback != NULL) {
655                                 fromtext_error(callback, callbacks, name,
656                                                line, &token, result);
657                                 callback = NULL;
658                         }
659                 } else if (result != ISC_R_SUCCESS && callback != NULL) {
660                         fromtext_error(callback, callbacks, name, line,
661                                        &token, result);
662                         break;
663                 } else {
664                         if (token.type == isc_tokentype_eof)
665                                 fromtext_warneof(lexer, callbacks);
666                         break;
667                 }
668         } while (1);
669
670         length = isc_buffer_usedlength(target) - isc_buffer_usedlength(&st);
671         if (result == ISC_R_SUCCESS && length > DNS_RDATA_MAXLENGTH)
672                 result = ISC_R_NOSPACE;
673
674         if (rdata != NULL && result == ISC_R_SUCCESS) {
675                 region.base = isc_buffer_used(&st);
676                 region.length = length;
677                 dns_rdata_fromregion(rdata, rdclass, type, &region);
678         }
679         if (result != ISC_R_SUCCESS) {
680                 *target = st;
681         }
682         return (result);
683 }
684
685 static isc_result_t
686 unknown_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
687                isc_buffer_t *target)
688 {
689         isc_result_t result;
690         char buf[sizeof("65535")];
691         isc_region_t sr;
692
693         strlcpy(buf, "\\# ", sizeof(buf));
694         result = str_totext(buf, target);
695         if (result != ISC_R_SUCCESS)
696                 return (result);
697
698         dns_rdata_toregion(rdata, &sr);
699         INSIST(sr.length < 65536);
700         snprintf(buf, sizeof(buf), "%u", sr.length);
701         result = str_totext(buf, target);
702         if (result != ISC_R_SUCCESS)
703                 return (result);
704
705         if (sr.length != 0U) {
706                 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
707                         result = str_totext(" ( ", target);
708                 else
709                         result = str_totext(" ", target);
710
711                 if (result != ISC_R_SUCCESS)
712                         return (result);
713
714                 if (tctx->width == 0) /* No splitting */
715                         result = isc_hex_totext(&sr, 0, "", target);
716                 else
717                         result = isc_hex_totext(&sr, tctx->width - 2,
718                                                 tctx->linebreak,
719                                                 target);
720                 if (result == ISC_R_SUCCESS &&
721                     (tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
722                         result = str_totext(" )", target);
723         }
724         return (result);
725 }
726
727 static isc_result_t
728 rdata_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
729              isc_buffer_t *target)
730 {
731         isc_result_t result = ISC_R_NOTIMPLEMENTED;
732         isc_boolean_t use_default = ISC_FALSE;
733
734         REQUIRE(rdata != NULL);
735         REQUIRE(tctx->origin == NULL ||
736                 dns_name_isabsolute(tctx->origin) == ISC_TRUE);
737
738         /*
739          * Some DynDNS meta-RRs have empty rdata.
740          */
741         if ((rdata->flags & DNS_RDATA_UPDATE) != 0) {
742                 INSIST(rdata->length == 0);
743                 return (ISC_R_SUCCESS);
744         }
745
746         TOTEXTSWITCH
747
748         if (use_default)
749                 result = unknown_totext(rdata, tctx, target);
750
751         return (result);
752 }
753
754 isc_result_t
755 dns_rdata_totext(dns_rdata_t *rdata, dns_name_t *origin, isc_buffer_t *target)
756 {
757         dns_rdata_textctx_t tctx;
758
759         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
760
761         /*
762          * Set up formatting options for single-line output.
763          */
764         tctx.origin = origin;
765         tctx.flags = 0;
766         tctx.width = 60;
767         tctx.linebreak = " ";
768         return (rdata_totext(rdata, &tctx, target));
769 }
770
771 isc_result_t
772 dns_rdata_tofmttext(dns_rdata_t *rdata, dns_name_t *origin,
773                     unsigned int flags, unsigned int width,
774                     const char *linebreak, isc_buffer_t *target)
775 {
776         dns_rdata_textctx_t tctx;
777
778         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
779
780         /*
781          * Set up formatting options for formatted output.
782          */
783         tctx.origin = origin;
784         tctx.flags = flags;
785         if ((flags & DNS_STYLEFLAG_MULTILINE) != 0) {
786                 tctx.width = width;
787                 tctx.linebreak = linebreak;
788         } else {
789                 tctx.width = 60; /* Used for hex word length only. */
790                 tctx.linebreak = " ";
791         }
792         return (rdata_totext(rdata, &tctx, target));
793 }
794
795 isc_result_t
796 dns_rdata_fromstruct(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
797                      dns_rdatatype_t type, void *source,
798                      isc_buffer_t *target)
799 {
800         isc_result_t result = ISC_R_NOTIMPLEMENTED;
801         isc_buffer_t st;
802         isc_region_t region;
803         isc_boolean_t use_default = ISC_FALSE;
804         size_t length;
805
806         REQUIRE(source != NULL);
807         if (rdata != NULL) {
808                 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
809                 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
810         }
811
812         st = *target;
813
814         FROMSTRUCTSWITCH
815
816         if (use_default)
817                 (void)NULL;
818
819         length = isc_buffer_usedlength(target) - isc_buffer_usedlength(&st);
820         if (result == ISC_R_SUCCESS && length > DNS_RDATA_MAXLENGTH)
821                 result = ISC_R_NOSPACE;
822
823         if (rdata != NULL && result == ISC_R_SUCCESS) {
824                 region.base = isc_buffer_used(&st);
825                 region.length = length;
826                 dns_rdata_fromregion(rdata, rdclass, type, &region);
827         }
828         if (result != ISC_R_SUCCESS)
829                 *target = st;
830         return (result);
831 }
832
833 isc_result_t
834 dns_rdata_tostruct(dns_rdata_t *rdata, void *target, isc_mem_t *mctx) {
835         isc_result_t result = ISC_R_NOTIMPLEMENTED;
836         isc_boolean_t use_default = ISC_FALSE;
837
838         REQUIRE(rdata != NULL);
839         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
840
841         TOSTRUCTSWITCH
842
843         if (use_default)
844                 (void)NULL;
845
846         return (result);
847 }
848
849 void
850 dns_rdata_freestruct(void *source) {
851         dns_rdatacommon_t *common = source;
852         REQUIRE(source != NULL);
853
854         FREESTRUCTSWITCH
855 }
856
857 isc_result_t
858 dns_rdata_additionaldata(dns_rdata_t *rdata, dns_additionaldatafunc_t add,
859                          void *arg)
860 {
861         isc_result_t result = ISC_R_NOTIMPLEMENTED;
862         isc_boolean_t use_default = ISC_FALSE;
863
864         /*
865          * Call 'add' for each name and type from 'rdata' which is subject to
866          * additional section processing.
867          */
868
869         REQUIRE(rdata != NULL);
870         REQUIRE(add != NULL);
871         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
872
873         ADDITIONALDATASWITCH
874
875         /* No additional processing for unknown types */
876         if (use_default)
877                 result = ISC_R_SUCCESS;
878
879         return (result);
880 }
881
882 isc_result_t
883 dns_rdata_digest(dns_rdata_t *rdata, dns_digestfunc_t digest, void *arg) {
884         isc_result_t result = ISC_R_NOTIMPLEMENTED;
885         isc_boolean_t use_default = ISC_FALSE;
886         isc_region_t r;
887
888         /*
889          * Send 'rdata' in DNSSEC canonical form to 'digest'.
890          */
891
892         REQUIRE(rdata != NULL);
893         REQUIRE(digest != NULL);
894         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
895
896         DIGESTSWITCH
897
898         if (use_default) {
899                 dns_rdata_toregion(rdata, &r);
900                 result = (digest)(arg, &r);
901         }
902
903         return (result);
904 }
905
906 isc_boolean_t
907 dns_rdata_checkowner(dns_name_t *name, dns_rdataclass_t rdclass,
908                      dns_rdatatype_t type, isc_boolean_t wildcard)
909 {
910         isc_boolean_t result;
911
912         CHECKOWNERSWITCH
913         return (result);
914 }
915
916 isc_boolean_t
917 dns_rdata_checknames(dns_rdata_t *rdata, dns_name_t *owner, dns_name_t *bad)
918 {
919         isc_boolean_t result;
920
921         CHECKNAMESSWITCH
922         return (result);
923 }
924
925 unsigned int
926 dns_rdatatype_attributes(dns_rdatatype_t type)
927 {
928         RDATATYPE_ATTRIBUTE_SW
929         if (type >= (dns_rdatatype_t)128 && type < (dns_rdatatype_t)255)
930                 return (DNS_RDATATYPEATTR_UNKNOWN | DNS_RDATATYPEATTR_META);
931         return (DNS_RDATATYPEATTR_UNKNOWN);
932 }
933
934 isc_result_t
935 dns_rdatatype_fromtext(dns_rdatatype_t *typep, isc_textregion_t *source) {
936         unsigned int hash;
937         unsigned int n;
938         unsigned char a, b;
939
940         n = source->length;
941
942         if (n == 0)
943                 return (DNS_R_UNKNOWN);
944
945         a = tolower((unsigned char)source->base[0]);
946         b = tolower((unsigned char)source->base[n - 1]);
947
948         hash = ((a + n) * b) % 256;
949
950         /*
951          * This switch block is inlined via \#define, and will use "return"
952          * to return a result to the caller if it is a valid (known)
953          * rdatatype name.
954          */
955         RDATATYPE_FROMTEXT_SW(hash, source->base, n, typep);
956
957         if (source->length > 4 && source->length < (4 + sizeof("65000")) &&
958             strncasecmp("type", source->base, 4) == 0) {
959                 char buf[sizeof("65000")];
960                 char *endp;
961                 unsigned int val;
962
963                 strncpy(buf, source->base + 4, source->length - 4);
964                 buf[source->length - 4] = '\0';
965                 val = strtoul(buf, &endp, 10);
966                 if (*endp == '\0' && val <= 0xffff) {
967                         *typep = (dns_rdatatype_t)val;
968                         return (ISC_R_SUCCESS);
969                 }
970         }
971
972         return (DNS_R_UNKNOWN);
973 }
974
975 isc_result_t
976 dns_rdatatype_totext(dns_rdatatype_t type, isc_buffer_t *target) {
977         char buf[sizeof("TYPE65535")];
978
979         RDATATYPE_TOTEXT_SW
980         snprintf(buf, sizeof(buf), "TYPE%u", type);
981         return (str_totext(buf, target));
982 }
983
984 void
985 dns_rdatatype_format(dns_rdatatype_t rdtype,
986                      char *array, unsigned int size)
987 {
988         isc_result_t result;
989         isc_buffer_t buf;
990
991         if (size == 0U)
992                 return;
993
994         isc_buffer_init(&buf, array, size);
995         result = dns_rdatatype_totext(rdtype, &buf);
996         /*
997          * Null terminate.
998          */
999         if (result == ISC_R_SUCCESS) {
1000                 if (isc_buffer_availablelength(&buf) >= 1)
1001                         isc_buffer_putuint8(&buf, 0);
1002                 else
1003                         result = ISC_R_NOSPACE;
1004         }
1005         if (result != ISC_R_SUCCESS)
1006                 strlcpy(array, "<unknown>", size);
1007 }
1008
1009 /*
1010  * Private function.
1011  */
1012
1013 static unsigned int
1014 name_length(dns_name_t *name) {
1015         return (name->length);
1016 }
1017
1018 static isc_result_t
1019 txt_totext(isc_region_t *source, isc_buffer_t *target) {
1020         unsigned int tl;
1021         unsigned int n;
1022         unsigned char *sp;
1023         char *tp;
1024         isc_region_t region;
1025
1026         isc_buffer_availableregion(target, &region);
1027         sp = source->base;
1028         tp = (char *)region.base;
1029         tl = region.length;
1030
1031         n = *sp++;
1032
1033         REQUIRE(n + 1 <= source->length);
1034
1035         if (tl < 1)
1036                 return (ISC_R_NOSPACE);
1037         *tp++ = '"';
1038         tl--;
1039         while (n--) {
1040                 if (*sp < 0x20 || *sp >= 0x7f) {
1041                         if (tl < 4)
1042                                 return (ISC_R_NOSPACE);
1043                         *tp++ = 0x5c;
1044                         *tp++ = 0x30 + ((*sp / 100) % 10);
1045                         *tp++ = 0x30 + ((*sp / 10) % 10);
1046                         *tp++ = 0x30 + (*sp % 10);
1047                         sp++;
1048                         tl -= 4;
1049                         continue;
1050                 }
1051                 /* double quote, semi-colon, backslash */
1052                 if (*sp == 0x22 || *sp == 0x3b || *sp == 0x5c) {
1053                         if (tl < 2)
1054                                 return (ISC_R_NOSPACE);
1055                         *tp++ = '\\';
1056                         tl--;
1057                 }
1058                 if (tl < 1)
1059                         return (ISC_R_NOSPACE);
1060                 *tp++ = *sp++;
1061                 tl--;
1062         }
1063         if (tl < 1)
1064                 return (ISC_R_NOSPACE);
1065         *tp++ = '"';
1066         tl--;
1067         isc_buffer_add(target, tp - (char *)region.base);
1068         isc_region_consume(source, *source->base + 1);
1069         return (ISC_R_SUCCESS);
1070 }
1071
1072 static isc_result_t
1073 txt_fromtext(isc_textregion_t *source, isc_buffer_t *target) {
1074         isc_region_t tregion;
1075         isc_boolean_t escape;
1076         unsigned int n, nrem;
1077         char *s;
1078         unsigned char *t;
1079         int d;
1080         int c;
1081
1082         isc_buffer_availableregion(target, &tregion);
1083         s = source->base;
1084         n = source->length;
1085         t = tregion.base;
1086         nrem = tregion.length;
1087         escape = ISC_FALSE;
1088         if (nrem < 1)
1089                 return (ISC_R_NOSPACE);
1090         /*
1091          * Length byte.
1092          */
1093         nrem--;
1094         t++;
1095         /*
1096          * Maximum text string length.
1097          */
1098         if (nrem > 255)
1099                 nrem = 255;
1100         while (n-- != 0) {
1101                 c = (*s++) & 0xff;
1102                 if (escape && (d = decvalue((char)c)) != -1) {
1103                         c = d;
1104                         if (n == 0)
1105                                 return (DNS_R_SYNTAX);
1106                         n--;
1107                         if ((d = decvalue(*s++)) != -1)
1108                                 c = c * 10 + d;
1109                         else
1110                                 return (DNS_R_SYNTAX);
1111                         if (n == 0)
1112                                 return (DNS_R_SYNTAX);
1113                         n--;
1114                         if ((d = decvalue(*s++)) != -1)
1115                                 c = c * 10 + d;
1116                         else
1117                                 return (DNS_R_SYNTAX);
1118                         if (c > 255)
1119                                 return (DNS_R_SYNTAX);
1120                 } else if (!escape && c == '\\') {
1121                         escape = ISC_TRUE;
1122                         continue;
1123                 }
1124                 escape = ISC_FALSE;
1125                 if (nrem == 0)
1126                         return ((tregion.length <= 256U) ?
1127                                 ISC_R_NOSPACE : DNS_R_SYNTAX);
1128                 *t++ = c;
1129                 nrem--;
1130         }
1131         if (escape)
1132                 return (DNS_R_SYNTAX);
1133         *tregion.base = t - tregion.base - 1;
1134         isc_buffer_add(target, *tregion.base + 1);
1135         return (ISC_R_SUCCESS);
1136 }
1137
1138 static isc_result_t
1139 txt_fromwire(isc_buffer_t *source, isc_buffer_t *target) {
1140         unsigned int n;
1141         isc_region_t sregion;
1142         isc_region_t tregion;
1143
1144         isc_buffer_activeregion(source, &sregion);
1145         if (sregion.length == 0)
1146                 return(ISC_R_UNEXPECTEDEND);
1147         n = *sregion.base + 1;
1148         if (n > sregion.length)
1149                 return (ISC_R_UNEXPECTEDEND);
1150
1151         isc_buffer_availableregion(target, &tregion);
1152         if (n > tregion.length)
1153                 return (ISC_R_NOSPACE);
1154
1155         memcpy(tregion.base, sregion.base, n);
1156         isc_buffer_forward(source, n);
1157         isc_buffer_add(target, n);
1158         return (ISC_R_SUCCESS);
1159 }
1160
1161 static isc_boolean_t
1162 name_prefix(dns_name_t *name, dns_name_t *origin, dns_name_t *target) {
1163         int l1, l2;
1164
1165         if (origin == NULL)
1166                 goto return_false;
1167
1168         if (dns_name_compare(origin, dns_rootname) == 0)
1169                 goto return_false;
1170
1171         if (!dns_name_issubdomain(name, origin))
1172                 goto return_false;
1173
1174         l1 = dns_name_countlabels(name);
1175         l2 = dns_name_countlabels(origin);
1176
1177         if (l1 == l2)
1178                 goto return_false;
1179
1180         /* Master files should be case preserving. */
1181         dns_name_getlabelsequence(name, l1 - l2, l2, target);
1182         if (!dns_name_caseequal(origin, target))
1183                 goto return_false;
1184
1185         dns_name_getlabelsequence(name, 0, l1 - l2, target);
1186         return (ISC_TRUE);
1187
1188 return_false:
1189         *target = *name;
1190         return (ISC_FALSE);
1191 }
1192
1193 static isc_result_t
1194 str_totext(const char *source, isc_buffer_t *target) {
1195         unsigned int l;
1196         isc_region_t region;
1197
1198         isc_buffer_availableregion(target, &region);
1199         l = strlen(source);
1200
1201         if (l > region.length)
1202                 return (ISC_R_NOSPACE);
1203
1204         memcpy(region.base, source, l);
1205         isc_buffer_add(target, l);
1206         return (ISC_R_SUCCESS);
1207 }
1208
1209 static isc_result_t
1210 inet_totext(int af, isc_region_t *src, isc_buffer_t *target) {
1211         char tmpbuf[64];
1212
1213         /* Note - inet_ntop doesn't do size checking on its input. */
1214         if (inet_ntop(af, src->base, tmpbuf, sizeof(tmpbuf)) == NULL)
1215                 return (ISC_R_NOSPACE);
1216         if (strlen(tmpbuf) > isc_buffer_availablelength(target))
1217                 return (ISC_R_NOSPACE);
1218         isc_buffer_putstr(target, tmpbuf);
1219         return (ISC_R_SUCCESS);
1220 }
1221
1222 static isc_boolean_t
1223 buffer_empty(isc_buffer_t *source) {
1224         return((source->current == source->active) ? ISC_TRUE : ISC_FALSE);
1225 }
1226
1227 static void
1228 buffer_fromregion(isc_buffer_t *buffer, isc_region_t *region) {
1229         isc_buffer_init(buffer, region->base, region->length);
1230         isc_buffer_add(buffer, region->length);
1231         isc_buffer_setactive(buffer, region->length);
1232 }
1233
1234 static isc_result_t
1235 uint32_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1236         isc_region_t region;
1237
1238         isc_buffer_availableregion(target, &region);
1239         if (region.length < 4)
1240                 return (ISC_R_NOSPACE);
1241         isc_buffer_putuint32(target, value);
1242         return (ISC_R_SUCCESS);
1243 }
1244
1245 static isc_result_t
1246 uint16_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1247         isc_region_t region;
1248
1249         if (value > 0xffff)
1250                 return (ISC_R_RANGE);
1251         isc_buffer_availableregion(target, &region);
1252         if (region.length < 2)
1253                 return (ISC_R_NOSPACE);
1254         isc_buffer_putuint16(target, (isc_uint16_t)value);
1255         return (ISC_R_SUCCESS);
1256 }
1257
1258 static isc_result_t
1259 uint8_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1260         isc_region_t region;
1261
1262         if (value > 0xff)
1263                 return (ISC_R_RANGE);
1264         isc_buffer_availableregion(target, &region);
1265         if (region.length < 1)
1266                 return (ISC_R_NOSPACE);
1267         isc_buffer_putuint8(target, (isc_uint8_t)value);
1268         return (ISC_R_SUCCESS);
1269 }
1270
1271 static isc_result_t
1272 name_tobuffer(dns_name_t *name, isc_buffer_t *target) {
1273         isc_region_t r;
1274         dns_name_toregion(name, &r);
1275         return (isc_buffer_copyregion(target, &r));
1276 }
1277
1278 static isc_uint32_t
1279 uint32_fromregion(isc_region_t *region) {
1280         isc_uint32_t value;
1281
1282         REQUIRE(region->length >= 4);
1283         value = region->base[0] << 24;
1284         value |= region->base[1] << 16;
1285         value |= region->base[2] << 8;
1286         value |= region->base[3];
1287         return(value);
1288 }
1289
1290 static isc_uint16_t
1291 uint16_consume_fromregion(isc_region_t *region) {
1292         isc_uint16_t r = uint16_fromregion(region);
1293
1294         isc_region_consume(region, 2);
1295         return r;
1296 }
1297
1298 static isc_uint16_t
1299 uint16_fromregion(isc_region_t *region) {
1300
1301         REQUIRE(region->length >= 2);
1302
1303         return ((region->base[0] << 8) | region->base[1]);
1304 }
1305
1306 static isc_uint8_t
1307 uint8_fromregion(isc_region_t *region) {
1308
1309         REQUIRE(region->length >= 1);
1310
1311         return (region->base[0]);
1312 }
1313
1314 static isc_uint8_t
1315 uint8_consume_fromregion(isc_region_t *region) {
1316         isc_uint8_t r = uint8_fromregion(region);
1317
1318         isc_region_consume(region, 1);
1319         return r;
1320 }
1321
1322 static isc_result_t
1323 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
1324         isc_region_t tr;
1325
1326         isc_buffer_availableregion(target, &tr);
1327         if (length > tr.length)
1328                 return (ISC_R_NOSPACE);
1329         memcpy(tr.base, base, length);
1330         isc_buffer_add(target, length);
1331         return (ISC_R_SUCCESS);
1332 }
1333
1334 static int
1335 hexvalue(char value) {
1336         char *s;
1337         unsigned char c;
1338
1339         c = (unsigned char)value;
1340
1341         if (!isascii(c))
1342                 return (-1);
1343         if (isupper(c))
1344                 c = tolower(c);
1345         if ((s = strchr(hexdigits, c)) == NULL)
1346                 return (-1);
1347         return (s - hexdigits);
1348 }
1349
1350 static int
1351 decvalue(char value) {
1352         char *s;
1353
1354         /*
1355          * isascii() is valid for full range of int values, no need to
1356          * mask or cast.
1357          */
1358         if (!isascii(value))
1359                 return (-1);
1360         if ((s = strchr(decdigits, value)) == NULL)
1361                 return (-1);
1362         return (s - decdigits);
1363 }
1364
1365 static const char atob_digits[86] =
1366         "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`" \
1367         "abcdefghijklmnopqrstu";
1368 /*
1369  * Subroutines to convert between 8 bit binary bytes and printable ASCII.
1370  * Computes the number of bytes, and three kinds of simple checksums.
1371  * Incoming bytes are collected into 32-bit words, then printed in base 85:
1372  *      exp(85,5) > exp(2,32)
1373  * The ASCII characters used are between '!' and 'u';
1374  * 'z' encodes 32-bit zero; 'x' is used to mark the end of encoded data.
1375  *
1376  * Originally by Paul Rutter (philabs!per) and Joe Orost (petsd!joe) for
1377  * the atob/btoa programs, released with the compress program, in mod.sources.
1378  * Modified by Mike Schwartz 8/19/86 for use in BIND.
1379  * Modified to be re-entrant 3/2/99.
1380  */
1381
1382
1383 struct state {
1384         isc_int32_t Ceor;
1385         isc_int32_t Csum;
1386         isc_int32_t Crot;
1387         isc_int32_t word;
1388         isc_int32_t bcount;
1389 };
1390
1391 #define Ceor state->Ceor
1392 #define Csum state->Csum
1393 #define Crot state->Crot
1394 #define word state->word
1395 #define bcount state->bcount
1396
1397 #define times85(x)      ((((((x<<2)+x)<<2)+x)<<2)+x)
1398
1399 static isc_result_t     byte_atob(int c, isc_buffer_t *target,
1400                                   struct state *state);
1401 static isc_result_t     putbyte(int c, isc_buffer_t *, struct state *state);
1402 static isc_result_t     byte_btoa(int c, isc_buffer_t *, struct state *state);
1403
1404 /*
1405  * Decode ASCII-encoded byte c into binary representation and
1406  * place into *bufp, advancing bufp.
1407  */
1408 static isc_result_t
1409 byte_atob(int c, isc_buffer_t *target, struct state *state) {
1410         char *s;
1411         if (c == 'z') {
1412                 if (bcount != 0)
1413                         return(DNS_R_SYNTAX);
1414                 else {
1415                         RETERR(putbyte(0, target, state));
1416                         RETERR(putbyte(0, target, state));
1417                         RETERR(putbyte(0, target, state));
1418                         RETERR(putbyte(0, target, state));
1419                 }
1420         } else if ((s = strchr(atob_digits, c)) != NULL) {
1421                 if (bcount == 0) {
1422                         word = s - atob_digits;
1423                         ++bcount;
1424                 } else if (bcount < 4) {
1425                         word = times85(word);
1426                         word += s - atob_digits;
1427                         ++bcount;
1428                 } else {
1429                         word = times85(word);
1430                         word += s - atob_digits;
1431                         RETERR(putbyte((word >> 24) & 0xff, target, state));
1432                         RETERR(putbyte((word >> 16) & 0xff, target, state));
1433                         RETERR(putbyte((word >> 8) & 0xff, target, state));
1434                         RETERR(putbyte(word & 0xff, target, state));
1435                         word = 0;
1436                         bcount = 0;
1437                 }
1438         } else
1439                 return(DNS_R_SYNTAX);
1440         return(ISC_R_SUCCESS);
1441 }
1442
1443 /*
1444  * Compute checksum info and place c into target.
1445  */
1446 static isc_result_t
1447 putbyte(int c, isc_buffer_t *target, struct state *state) {
1448         isc_region_t tr;
1449
1450         Ceor ^= c;
1451         Csum += c;
1452         Csum += 1;
1453         if ((Crot & 0x80000000)) {
1454                 Crot <<= 1;
1455                 Crot += 1;
1456         } else {
1457                 Crot <<= 1;
1458         }
1459         Crot += c;
1460         isc_buffer_availableregion(target, &tr);
1461         if (tr.length < 1)
1462                 return (ISC_R_NOSPACE);
1463         tr.base[0] = c;
1464         isc_buffer_add(target, 1);
1465         return (ISC_R_SUCCESS);
1466 }
1467
1468 /*
1469  * Read the ASCII-encoded data from inbuf, of length inbuflen, and convert
1470  * it into T_UNSPEC (binary data) in outbuf, not to exceed outbuflen bytes;
1471  * outbuflen must be divisible by 4.  (Note: this is because outbuf is filled
1472  * in 4 bytes at a time.  If the actual data doesn't end on an even 4-byte
1473  * boundary, there will be no problem...it will be padded with 0 bytes, and
1474  * numbytes will indicate the correct number of bytes.  The main point is
1475  * that since the buffer is filled in 4 bytes at a time, even if there is
1476  * not a full 4 bytes of data at the end, there has to be room to 0-pad the
1477  * data, so the buffer must be of size divisible by 4).  Place the number of
1478  * output bytes in numbytes, and return a failure/success status.
1479  */
1480
1481 static isc_result_t
1482 atob_tobuffer(isc_lex_t *lexer, isc_buffer_t *target) {
1483         long oeor, osum, orot;
1484         struct state statebuf, *state= &statebuf;
1485         isc_token_t token;
1486         char c;
1487         char *e;
1488
1489         Ceor = Csum = Crot = word = bcount = 0;
1490
1491         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1492                                       ISC_FALSE));
1493         while (token.value.as_textregion.length != 0) {
1494                 if ((c = token.value.as_textregion.base[0]) == 'x') {
1495                         break;
1496                 } else
1497                         RETERR(byte_atob(c, target, state));
1498                 isc_textregion_consume(&token.value.as_textregion, 1);
1499         }
1500
1501         /*
1502          * Number of bytes.
1503          */
1504         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
1505                                       ISC_FALSE));
1506         if ((token.value.as_ulong % 4) != 0U)
1507                 isc_buffer_subtract(target,  4 - (token.value.as_ulong % 4));
1508
1509         /*
1510          * Checksum.
1511          */
1512         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1513                                       ISC_FALSE));
1514         oeor = strtol(DNS_AS_STR(token), &e, 16);
1515         if (*e != 0)
1516                 return (DNS_R_SYNTAX);
1517
1518         /*
1519          * Checksum.
1520          */
1521         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1522                                       ISC_FALSE));
1523         osum = strtol(DNS_AS_STR(token), &e, 16);
1524         if (*e != 0)
1525                 return (DNS_R_SYNTAX);
1526
1527         /*
1528          * Checksum.
1529          */
1530         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1531                                       ISC_FALSE));
1532         orot = strtol(DNS_AS_STR(token), &e, 16);
1533         if (*e != 0)
1534                 return (DNS_R_SYNTAX);
1535
1536         if ((oeor != Ceor) || (osum != Csum) || (orot != Crot))
1537                 return(DNS_R_BADCKSUM);
1538         return (ISC_R_SUCCESS);
1539 }
1540
1541 /*
1542  * Encode binary byte c into ASCII representation and place into *bufp,
1543  * advancing bufp.
1544  */
1545 static isc_result_t
1546 byte_btoa(int c, isc_buffer_t *target, struct state *state) {
1547         isc_region_t tr;
1548
1549         isc_buffer_availableregion(target, &tr);
1550         Ceor ^= c;
1551         Csum += c;
1552         Csum += 1;
1553         if ((Crot & 0x80000000)) {
1554                 Crot <<= 1;
1555                 Crot += 1;
1556         } else {
1557                 Crot <<= 1;
1558         }
1559         Crot += c;
1560
1561         word <<= 8;
1562         word |= c;
1563         if (bcount == 3) {
1564                 if (word == 0) {
1565                         if (tr.length < 1)
1566                                 return (ISC_R_NOSPACE);
1567                         tr.base[0] = 'z';
1568                         isc_buffer_add(target, 1);
1569                 } else {
1570                     register int tmp = 0;
1571                     register isc_int32_t tmpword = word;
1572
1573                     if (tmpword < 0) {
1574                            /*
1575                             * Because some don't support u_long.
1576                             */
1577                         tmp = 32;
1578                         tmpword -= (isc_int32_t)(85 * 85 * 85 * 85 * 32);
1579                     }
1580                     if (tmpword < 0) {
1581                         tmp = 64;
1582                         tmpword -= (isc_int32_t)(85 * 85 * 85 * 85 * 32);
1583                     }
1584                         if (tr.length < 5)
1585                                 return (ISC_R_NOSPACE);
1586                         tr.base[0] = atob_digits[(tmpword /
1587                                               (isc_int32_t)(85 * 85 * 85 * 85))
1588                                                 + tmp];
1589                         tmpword %= (isc_int32_t)(85 * 85 * 85 * 85);
1590                         tr.base[1] = atob_digits[tmpword / (85 * 85 * 85)];
1591                         tmpword %= (85 * 85 * 85);
1592                         tr.base[2] = atob_digits[tmpword / (85 * 85)];
1593                         tmpword %= (85 * 85);
1594                         tr.base[3] = atob_digits[tmpword / 85];
1595                         tmpword %= 85;
1596                         tr.base[4] = atob_digits[tmpword];
1597                         isc_buffer_add(target, 5);
1598                 }
1599                 bcount = 0;
1600         } else {
1601                 bcount += 1;
1602         }
1603         return (ISC_R_SUCCESS);
1604 }
1605
1606
1607 /*
1608  * Encode the binary data from inbuf, of length inbuflen, into a
1609  * target.  Return success/failure status
1610  */
1611 static isc_result_t
1612 btoa_totext(unsigned char *inbuf, int inbuflen, isc_buffer_t *target) {
1613         int inc;
1614         struct state statebuf, *state = &statebuf;
1615         char buf[sizeof("x 2000000000 ffffffff ffffffff ffffffff")];
1616
1617         Ceor = Csum = Crot = word = bcount = 0;
1618         for (inc = 0; inc < inbuflen; inbuf++, inc++)
1619                 RETERR(byte_btoa(*inbuf, target, state));
1620
1621         while (bcount != 0)
1622                 RETERR(byte_btoa(0, target, state));
1623
1624         /*
1625          * Put byte count and checksum information at end of buffer,
1626          * delimited by 'x'
1627          */
1628         snprintf(buf, sizeof(buf), "x %d %x %x %x", inbuflen, Ceor, Csum, Crot);
1629         return (str_totext(buf, target));
1630 }
1631
1632
1633 static void
1634 default_fromtext_callback(dns_rdatacallbacks_t *callbacks, const char *fmt,
1635                           ...)
1636 {
1637         va_list ap;
1638
1639         UNUSED(callbacks);
1640
1641         va_start(ap, fmt);
1642         vfprintf(stderr, fmt, ap);
1643         va_end(ap);
1644         fprintf(stderr, "\n");
1645 }
1646
1647 static void
1648 fromtext_warneof(isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks) {
1649         if (isc_lex_isfile(lexer) && callbacks != NULL) {
1650                 const char *name = isc_lex_getsourcename(lexer);
1651                 if (name == NULL)
1652                         name = "UNKNOWN";
1653                 (*callbacks->warn)(callbacks,
1654                                    "%s:%lu: file does not end with newline",
1655                                    name, isc_lex_getsourceline(lexer));
1656         }
1657 }
1658
1659 static void
1660 warn_badmx(isc_token_t *token, isc_lex_t *lexer,
1661            dns_rdatacallbacks_t *callbacks)
1662 {
1663         const char *file;
1664         unsigned long line;
1665
1666         if (lexer != NULL) {
1667                 file = isc_lex_getsourcename(lexer);
1668                 line = isc_lex_getsourceline(lexer);
1669                 (*callbacks->warn)(callbacks, "%s:%u: warning: '%s': %s",
1670                                    file, line, DNS_AS_STR(*token),
1671                                    dns_result_totext(DNS_R_MXISADDRESS));
1672         }
1673 }
1674
1675 static void
1676 warn_badname(dns_name_t *name, isc_lex_t *lexer,
1677              dns_rdatacallbacks_t *callbacks)
1678 {
1679         const char *file;
1680         unsigned long line;
1681         char namebuf[DNS_NAME_FORMATSIZE];
1682
1683         if (lexer != NULL) {
1684                 file = isc_lex_getsourcename(lexer);
1685                 line = isc_lex_getsourceline(lexer);
1686                 dns_name_format(name, namebuf, sizeof(namebuf));
1687                 (*callbacks->warn)(callbacks, "%s:%u: warning: %s: %s",
1688                                    file, line, namebuf,
1689                                    dns_result_totext(DNS_R_BADNAME));
1690         }
1691 }
1692
1693 static void
1694 fromtext_error(void (*callback)(dns_rdatacallbacks_t *, const char *, ...),
1695                dns_rdatacallbacks_t *callbacks, const char *name,
1696                unsigned long line, isc_token_t *token, isc_result_t result)
1697 {
1698         if (name == NULL)
1699                 name = "UNKNOWN";
1700
1701         if (token != NULL) {
1702                 switch (token->type) {
1703                 case isc_tokentype_eol:
1704                         (*callback)(callbacks, "%s: %s:%lu: near eol: %s",
1705                                     "dns_rdata_fromtext", name, line,
1706                                     dns_result_totext(result));
1707                         break;
1708                 case isc_tokentype_eof:
1709                         (*callback)(callbacks, "%s: %s:%lu: near eof: %s",
1710                                     "dns_rdata_fromtext", name, line,
1711                                     dns_result_totext(result));
1712                         break;
1713                 case isc_tokentype_number:
1714                         (*callback)(callbacks, "%s: %s:%lu: near %lu: %s",
1715                                     "dns_rdata_fromtext", name, line,
1716                                     token->value.as_ulong,
1717                                     dns_result_totext(result));
1718                         break;
1719                 case isc_tokentype_string:
1720                 case isc_tokentype_qstring:
1721                         (*callback)(callbacks, "%s: %s:%lu: near '%s': %s",
1722                                     "dns_rdata_fromtext", name, line,
1723                                     DNS_AS_STR(*token),
1724                                     dns_result_totext(result));
1725                         break;
1726                 default:
1727                         (*callback)(callbacks, "%s: %s:%lu: %s",
1728                                     "dns_rdata_fromtext", name, line,
1729                                     dns_result_totext(result));
1730                         break;
1731                 }
1732         } else {
1733                 (*callback)(callbacks, "dns_rdata_fromtext: %s:%lu: %s",
1734                             name, line, dns_result_totext(result));
1735         }
1736 }
1737
1738 dns_rdatatype_t
1739 dns_rdata_covers(dns_rdata_t *rdata) {
1740         if (rdata->type == 46)
1741                 return (covers_rrsig(rdata));
1742         return (covers_sig(rdata));
1743 }
1744
1745 isc_boolean_t
1746 dns_rdatatype_ismeta(dns_rdatatype_t type) {
1747         if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_META) != 0)
1748                 return (ISC_TRUE);
1749         return (ISC_FALSE);
1750 }
1751
1752 isc_boolean_t
1753 dns_rdatatype_issingleton(dns_rdatatype_t type) {
1754         if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_SINGLETON)
1755             != 0)
1756                 return (ISC_TRUE);
1757         return (ISC_FALSE);
1758 }
1759
1760 isc_boolean_t
1761 dns_rdatatype_notquestion(dns_rdatatype_t type) {
1762         if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_NOTQUESTION)
1763             != 0)
1764                 return (ISC_TRUE);
1765         return (ISC_FALSE);
1766 }
1767
1768 isc_boolean_t
1769 dns_rdatatype_questiononly(dns_rdatatype_t type) {
1770         if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_QUESTIONONLY)
1771             != 0)
1772                 return (ISC_TRUE);
1773         return (ISC_FALSE);
1774 }
1775
1776 isc_boolean_t
1777 dns_rdatatype_atparent(dns_rdatatype_t type) {
1778         if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_ATPARENT) != 0)
1779                 return (ISC_TRUE);
1780         return (ISC_FALSE);
1781 }
1782
1783 isc_boolean_t
1784 dns_rdataclass_ismeta(dns_rdataclass_t rdclass) {
1785
1786         if (rdclass == dns_rdataclass_reserved0
1787             || rdclass == dns_rdataclass_none
1788             || rdclass == dns_rdataclass_any)
1789                 return (ISC_TRUE);
1790
1791         return (ISC_FALSE);  /* Assume it is not a meta class. */
1792 }
1793
1794 isc_boolean_t
1795 dns_rdatatype_isdnssec(dns_rdatatype_t type) {
1796         if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_DNSSEC) != 0)
1797                 return (ISC_TRUE);
1798         return (ISC_FALSE);
1799 }
1800
1801 isc_boolean_t
1802 dns_rdatatype_iszonecutauth(dns_rdatatype_t type) {
1803         if ((dns_rdatatype_attributes(type)
1804              & (DNS_RDATATYPEATTR_DNSSEC | DNS_RDATATYPEATTR_ZONECUTAUTH))
1805             != 0)
1806                 return (ISC_TRUE);
1807         return (ISC_FALSE);
1808 }
1809
1810 isc_boolean_t
1811 dns_rdatatype_isknown(dns_rdatatype_t type) {
1812         if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_UNKNOWN)
1813             == 0)
1814                 return (ISC_TRUE);
1815         return (ISC_FALSE);
1816 }