res_comp.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* Domain name processing functions.
  2. Copyright (C) 1995-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. /*
  16. * Copyright (c) 1985, 1993
  17. * The Regents of the University of California. All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. * 1. Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. * 2. Redistributions in binary form must reproduce the above copyright
  25. * notice, this list of conditions and the following disclaimer in the
  26. * documentation and/or other materials provided with the distribution.
  27. * 4. Neither the name of the University nor the names of its contributors
  28. * may be used to endorse or promote products derived from this software
  29. * without specific prior written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  32. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  35. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  37. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  39. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  40. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  41. * SUCH DAMAGE.
  42. */
  43. /*
  44. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  45. *
  46. * Permission to use, copy, modify, and distribute this software for any
  47. * purpose with or without fee is hereby granted, provided that the above
  48. * copyright notice and this permission notice appear in all copies, and that
  49. * the name of Digital Equipment Corporation not be used in advertising or
  50. * publicity pertaining to distribution of the document or software without
  51. * specific, written prior permission.
  52. *
  53. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  54. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  55. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  56. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  57. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  58. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  59. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  60. * SOFTWARE.
  61. */
  62. /*
  63. * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
  64. *
  65. * Permission to use, copy, modify, and distribute this software for any
  66. * purpose with or without fee is hereby granted, provided that the above
  67. * copyright notice and this permission notice appear in all copies.
  68. *
  69. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  70. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  71. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  72. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  73. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  74. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  75. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  76. * SOFTWARE.
  77. */
  78. #include <sys/types.h>
  79. #include <sys/param.h>
  80. #include <netinet/in.h>
  81. #include <arpa/nameser.h>
  82. #include <ctype.h>
  83. #include <resolv.h>
  84. #include <stdio.h>
  85. #include <string.h>
  86. #include <unistd.h>
  87. /*
  88. * Expand compressed domain name 'comp_dn' to full domain name.
  89. * 'msg' is a pointer to the beginning of the message,
  90. * 'eomorig' points to the first location after the message,
  91. * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
  92. * Return size of compressed name or -1 if there was an error.
  93. */
  94. int
  95. dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
  96. char *dst, int dstsiz)
  97. {
  98. int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
  99. if (n > 0 && dst[0] == '.')
  100. dst[0] = '\0';
  101. return (n);
  102. }
  103. libresolv_hidden_def (dn_expand)
  104. /*
  105. * Pack domain name 'exp_dn' in presentation form into 'comp_dn'.
  106. * Return the size of the compressed name or -1.
  107. * 'length' is the size of the array pointed to by 'comp_dn'.
  108. */
  109. int
  110. dn_comp(const char *src, u_char *dst, int dstsiz,
  111. u_char **dnptrs, u_char **lastdnptr)
  112. {
  113. return (ns_name_compress(src, dst, (size_t)dstsiz,
  114. (const u_char **)dnptrs,
  115. (const u_char **)lastdnptr));
  116. }
  117. libresolv_hidden_def (dn_comp)
  118. /*
  119. * Skip over a compressed domain name. Return the size or -1.
  120. */
  121. int
  122. dn_skipname(const u_char *ptr, const u_char *eom) {
  123. const u_char *saveptr = ptr;
  124. if (ns_name_skip(&ptr, eom) == -1)
  125. return (-1);
  126. return (ptr - saveptr);
  127. }
  128. libresolv_hidden_def (dn_skipname)
  129. /* Return true if the string consists of printable ASCII characters
  130. only. */
  131. static bool
  132. printable_string (const char *dn)
  133. {
  134. while (true)
  135. {
  136. char ch = *dn;
  137. if (ch == '\0')
  138. return true;
  139. if (ch <= ' ' || ch > '~')
  140. return false;
  141. ++dn;
  142. }
  143. }
  144. /* Return true if DN points to a name consisting only of [0-9a-zA-Z_-]
  145. characters. DN must be in DNS wire format, without
  146. compression. */
  147. static bool
  148. binary_hnok (const unsigned char *dn)
  149. {
  150. while (true)
  151. {
  152. size_t label_length = *dn;
  153. if (label_length == 0)
  154. break;
  155. ++dn;
  156. const unsigned char *label_end = dn + label_length;
  157. do
  158. {
  159. unsigned char ch = *dn;
  160. if (!(('0' <= ch && ch <= '9')
  161. || ('A' <= ch && ch <= 'Z')
  162. || ('a' <= ch && ch <= 'z')
  163. || ch == '-' || ch == '_'))
  164. return false;
  165. ++dn;
  166. }
  167. while (dn < label_end);
  168. }
  169. return true;
  170. }
  171. /* Return true if the binary domain name has a first labels which
  172. starts with '-'. */
  173. static inline bool
  174. binary_leading_dash (const unsigned char *dn)
  175. {
  176. return dn[0] > 0 && dn[1] == '-';
  177. }
  178. /* Return 1 if res_hnok is a valid host name. Labels must only
  179. contain [0-9a-zA-Z_-] characters, and the name must not start with
  180. a '-'. The latter is to avoid confusion with program options. */
  181. int
  182. res_hnok (const char *dn)
  183. {
  184. unsigned char buf[NS_MAXCDNAME];
  185. if (!printable_string (dn)
  186. || ns_name_pton (dn, buf, sizeof (buf)) < 0
  187. || binary_leading_dash (buf))
  188. return 0;
  189. return binary_hnok (buf);
  190. }
  191. libresolv_hidden_def (res_hnok)
  192. /* Hostname-like (A, MX, WKS) owners can have "*" as their first label
  193. but must otherwise be as a host name. */
  194. int
  195. res_ownok (const char *dn)
  196. {
  197. unsigned char buf[NS_MAXCDNAME];
  198. if (!printable_string (dn)
  199. || ns_name_pton (dn, buf, sizeof (buf)) < 0
  200. || binary_leading_dash (buf))
  201. return 0;
  202. if (buf[0] == 1 && buf [1] == '*')
  203. /* Skip over the leading "*." part. */
  204. return binary_hnok (buf + 2);
  205. else
  206. return binary_hnok (buf);
  207. }
  208. /* SOA RNAMEs and RP RNAMEs can have any byte in their first label,
  209. but the rest of the name has to look like a host name. */
  210. int
  211. res_mailok (const char *dn)
  212. {
  213. unsigned char buf[NS_MAXCDNAME];
  214. if (!printable_string (dn)
  215. || ns_name_pton (dn, buf, sizeof (buf)) < 0)
  216. return 0;
  217. unsigned char label_length = buf[0];
  218. /* "." is a valid missing representation */
  219. if (label_length == 0)
  220. return 1;
  221. /* Skip over the first label. */
  222. unsigned char *tail = buf + 1 + label_length;
  223. if (*tail == 0)
  224. /* More than one label is required (except for "."). */
  225. return 0;
  226. return binary_hnok (tail);
  227. }
  228. /* Return 1 if DN is a syntactically valid domain name. Empty names
  229. are accepted. */
  230. int
  231. res_dnok (const char *dn)
  232. {
  233. unsigned char buf[NS_MAXCDNAME];
  234. return printable_string (dn) && ns_name_pton (dn, buf, sizeof (buf)) >= 0;
  235. }
  236. libresolv_hidden_def (res_dnok)
  237. /*
  238. * This module must export the following externally-visible symbols:
  239. * ___putlong
  240. * ___putshort
  241. * __getlong
  242. * __getshort
  243. * Note that one _ comes from C and the others come from us.
  244. */
  245. void __putlong(uint32_t src, u_char *dst) { ns_put32(src, dst); }
  246. libresolv_hidden_def (__putlong)
  247. void __putshort(uint16_t src, u_char *dst) { ns_put16(src, dst); }
  248. libresolv_hidden_def (__putshort)
  249. uint32_t _getlong(const u_char *src) { return (ns_get32(src)); }
  250. uint16_t _getshort(const u_char *src) { return (ns_get16(src)); }
  251. #include <shlib-compat.h>
  252. #if SHLIB_COMPAT(libresolv, GLIBC_2_0, GLIBC_2_2)
  253. # undef dn_expand
  254. weak_alias (__dn_expand, dn_expand);
  255. #endif