/contrib/bind9/lib/dns/rdata.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 1921 lines · 1446 code · 320 blank · 155 comment · 362 complexity · 2d127660783b6d128692f4d916a053ff MD5 · raw file

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