1
0

ns_name.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Copyright (c) 1996,1999 by Internet Software Consortium.
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  9. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  10. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  11. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  13. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  14. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  15. * SOFTWARE.
  16. */
  17. /*
  18. * Modified by Hotbird64 for use with vlmcs.
  19. */
  20. #ifndef CONFIG
  21. #define CONFIG "config.h"
  22. #endif // CONFIG
  23. #include CONFIG
  24. #ifdef DNS_PARSER_INTERNAL
  25. #ifndef NO_DNS
  26. #include <sys/types.h>
  27. #include <errno.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <limits.h>
  33. #include "types.h"
  34. #include "ns_name.h"
  35. #ifdef SPRINTF_CHAR
  36. # define SPRINTF(x) strlen(sprintf/**/x)
  37. #else
  38. # define SPRINTF(x) ((size_t)sprintf x)
  39. #endif
  40. #define NS_TYPE_ELT 0x40 /* EDNS0 extended label type */
  41. #define DNS_LABELTYPE_BITSTRING 0x41
  42. #define NS_MAXCDNAME 255
  43. #define NS_CMPRSFLGS 0xc0
  44. /* Data. */
  45. static char digits[] = "0123456789";
  46. /* Forward. */
  47. static int special_vlmcsd(int);
  48. static int printable_vlmcsd(int);
  49. static int labellen_vlmcsd(const uint8_t *);
  50. static int decode_bitstring_vlmcsd(const char **, char *, const char *);
  51. /*
  52. * ns_name_ntop(src, dst, dstsiz)
  53. * Convert an encoded domain name to printable ascii as per RFC1035.
  54. * return:
  55. * Number of bytes written to buffer, or -1 (with errno set)
  56. * notes:
  57. * The root is returned as "."
  58. * All other domains are returned in non absolute form
  59. */
  60. static int
  61. ns_name_ntop_vlmcsd(const uint8_t *src, char *dst, size_t dstsiz)
  62. {
  63. const uint8_t *cp;
  64. char *dn, *eom;
  65. uint8_t c;
  66. uint32_t n;
  67. int l;
  68. cp = src;
  69. dn = dst;
  70. eom = dst + dstsiz;
  71. while ((n = *cp++) != 0) {
  72. if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
  73. /* Some kind of compression pointer. */
  74. errno = EMSGSIZE;
  75. return (-1);
  76. }
  77. if (dn != dst) {
  78. if (dn >= eom) {
  79. errno = EMSGSIZE;
  80. return (-1);
  81. }
  82. *dn++ = '.';
  83. }
  84. if ((l = labellen_vlmcsd(cp - 1)) < 0) {
  85. errno = EMSGSIZE; /* XXX */
  86. return(-1);
  87. }
  88. if (dn + l >= eom) {
  89. errno = EMSGSIZE;
  90. return (-1);
  91. }
  92. if ((n & NS_CMPRSFLGS) == NS_TYPE_ELT) {
  93. int m;
  94. if (n != DNS_LABELTYPE_BITSTRING) {
  95. /* XXX: labellen should reject this case */
  96. errno = EINVAL;
  97. return(-1);
  98. }
  99. if ((m = decode_bitstring_vlmcsd((const char **)&cp, dn, eom)) < 0)
  100. {
  101. errno = EMSGSIZE;
  102. return(-1);
  103. }
  104. dn += m;
  105. continue;
  106. }
  107. for ((void)NULL; l > 0; l--) {
  108. c = *cp++;
  109. if (special_vlmcsd(c)) {
  110. if (dn + 1 >= eom) {
  111. errno = EMSGSIZE;
  112. return (-1);
  113. }
  114. *dn++ = '\\';
  115. *dn++ = (char)c;
  116. } else if (!printable_vlmcsd(c)) {
  117. if (dn + 3 >= eom) {
  118. errno = EMSGSIZE;
  119. return (-1);
  120. }
  121. *dn++ = '\\';
  122. *dn++ = digits[c / 100];
  123. *dn++ = digits[(c % 100) / 10];
  124. *dn++ = digits[c % 10];
  125. } else {
  126. if (dn >= eom) {
  127. errno = EMSGSIZE;
  128. return (-1);
  129. }
  130. *dn++ = (char)c;
  131. }
  132. }
  133. }
  134. if (dn == dst) {
  135. if (dn >= eom) {
  136. errno = EMSGSIZE;
  137. return (-1);
  138. }
  139. *dn++ = '.';
  140. }
  141. if (dn >= eom) {
  142. errno = EMSGSIZE;
  143. return (-1);
  144. }
  145. *dn++ = '\0';
  146. return (dn - dst);
  147. }
  148. static int
  149. ns_name_unpack_vlmcsd(const uint8_t *msg, const uint8_t *eom, const uint8_t *src,
  150. uint8_t *dst, size_t dstsiz)
  151. {
  152. const uint8_t *srcp, *dstlim;
  153. uint8_t *dstp;
  154. int n, len, checked, l;
  155. len = -1;
  156. checked = 0;
  157. dstp = dst;
  158. srcp = src;
  159. dstlim = dst + dstsiz;
  160. if (srcp < msg || srcp >= eom) {
  161. errno = EMSGSIZE;
  162. return (-1);
  163. }
  164. /* Fetch next label in domain name. */
  165. while ((n = *srcp++) != 0) {
  166. /* Check for indirection. */
  167. switch (n & NS_CMPRSFLGS) {
  168. case 0:
  169. case NS_TYPE_ELT:
  170. /* Limit checks. */
  171. if ((l = labellen_vlmcsd(srcp - 1)) < 0) {
  172. errno = EMSGSIZE;
  173. return(-1);
  174. }
  175. if (dstp + l + 1 >= dstlim || srcp + l >= eom) {
  176. errno = EMSGSIZE;
  177. return (-1);
  178. }
  179. checked += l + 1;
  180. *dstp++ = n;
  181. memcpy(dstp, srcp, l);
  182. dstp += l;
  183. srcp += l;
  184. break;
  185. case NS_CMPRSFLGS:
  186. if (srcp >= eom) {
  187. errno = EMSGSIZE;
  188. return (-1);
  189. }
  190. if (len < 0)
  191. len = srcp - src + 1;
  192. srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
  193. if (srcp < msg || srcp >= eom) { /* Out of range. */
  194. errno = EMSGSIZE;
  195. return (-1);
  196. }
  197. checked += 2;
  198. /*
  199. * Check for loops in the compressed name;
  200. * if we've looked at the whole message,
  201. * there must be a loop.
  202. */
  203. if (checked >= eom - msg) {
  204. errno = EMSGSIZE;
  205. return (-1);
  206. }
  207. break;
  208. default:
  209. errno = EMSGSIZE;
  210. return (-1); /* flag error */
  211. }
  212. }
  213. *dstp = '\0';
  214. if (len < 0)
  215. len = srcp - src;
  216. return (len);
  217. }
  218. /*
  219. * ns_name_uncompress_vlmcsd(msg, eom, src, dst, dstsiz)
  220. * Expand compressed domain name to presentation format.
  221. * return:
  222. * Number of bytes read out of `src', or -1 (with errno set).
  223. * note:
  224. * Root domain returns as "." not "".
  225. */
  226. int
  227. ns_name_uncompress_vlmcsd(const uint8_t *msg, const uint8_t *eom, const uint8_t *src,
  228. char *dst, size_t dstsiz)
  229. {
  230. uint8_t tmp[NS_MAXCDNAME];
  231. int n;
  232. if ((n = ns_name_unpack_vlmcsd(msg, eom, src, tmp, sizeof tmp)) == -1)
  233. return (-1);
  234. if (ns_name_ntop_vlmcsd(tmp, dst, dstsiz) == -1)
  235. return (-1);
  236. return (n);
  237. }
  238. /*
  239. * special(ch)
  240. * Thinking in noninternationalized USASCII (per the DNS spec),
  241. * is this characted special ("in need of quoting") ?
  242. * return:
  243. * boolean.
  244. */
  245. static int
  246. special_vlmcsd(int ch) {
  247. switch (ch) {
  248. case 0x22: /* '"' */
  249. case 0x2E: /* '.' */
  250. case 0x3B: /* ';' */
  251. case 0x5C: /* '\\' */
  252. case 0x28: /* '(' */
  253. case 0x29: /* ')' */
  254. /* Special modifiers in zone files. */
  255. case 0x40: /* '@' */
  256. case 0x24: /* '$' */
  257. return (1);
  258. default:
  259. return (0);
  260. }
  261. }
  262. /*
  263. * printable(ch)
  264. * Thinking in noninternationalized USASCII (per the DNS spec),
  265. * is this character visible and not a space when printed ?
  266. * return:
  267. * boolean.
  268. */
  269. static int
  270. printable_vlmcsd(int ch) {
  271. return (ch > 0x20 && ch < 0x7f);
  272. }
  273. static int
  274. decode_bitstring_vlmcsd(const char **cpp, char *dn, const char *eom)
  275. {
  276. const char *cp = *cpp;
  277. char *beg = dn, tc;
  278. int b, blen, plen;
  279. if ((blen = (*cp & 0xff)) == 0)
  280. blen = 256;
  281. plen = (blen + 3) / 4;
  282. plen += sizeof("\\[x/]") + (blen > 99 ? 3 : (blen > 9) ? 2 : 1);
  283. if (dn + plen >= eom)
  284. return(-1);
  285. cp++;
  286. dn += SPRINTF((dn, "\\[x"));
  287. for (b = blen; b > 7; b -= 8, cp++)
  288. dn += SPRINTF((dn, "%02x", *cp & 0xff));
  289. if (b > 4) {
  290. tc = *cp++;
  291. dn += SPRINTF((dn, "%02x", tc & (0xff << (8 - b))));
  292. } else if (b > 0) {
  293. tc = *cp++;
  294. dn += SPRINTF((dn, "%1x",
  295. ((tc >> 4) & 0x0f) & (0x0f << (4 - b))));
  296. }
  297. dn += SPRINTF((dn, "/%d]", blen));
  298. *cpp = cp;
  299. return(dn - beg);
  300. }
  301. static int
  302. labellen_vlmcsd(const uint8_t *lp)
  303. {
  304. int bitlen;
  305. uint8_t l = *lp;
  306. if ((l & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
  307. /* should be avoided by the caller */
  308. return(-1);
  309. }
  310. if ((l & NS_CMPRSFLGS) == NS_TYPE_ELT) {
  311. if (l == DNS_LABELTYPE_BITSTRING) {
  312. if ((bitlen = *(lp + 1)) == 0)
  313. bitlen = 256;
  314. return((bitlen + 7 ) / 8 + 1);
  315. }
  316. return(-1); /* unknwon ELT */
  317. }
  318. return(l);
  319. }
  320. #endif // !NO_DNS
  321. #endif // DNS_PARSER_INTERNAL