PageRenderTime 96ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/libc/resolv/res_comp.c

https://bitbucket.org/freebsd/freebsd-base
C | 278 lines | 136 code | 25 blank | 117 comment | 51 complexity | a9220bb192e691e5ef969f7a030510c2 MD5 | raw file
  1. /*-
  2. * SPDX-License-Identifier: (BSD-3-Clause AND ISC)
  3. *
  4. * Copyright (c) 1985, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the University nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. /*
  32. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  33. *
  34. * Permission to use, copy, modify, and distribute this software for any
  35. * purpose with or without fee is hereby granted, provided that the above
  36. * copyright notice and this permission notice appear in all copies, and that
  37. * the name of Digital Equipment Corporation not be used in advertising or
  38. * publicity pertaining to distribution of the document or software without
  39. * specific, written prior permission.
  40. *
  41. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  42. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  43. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  44. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  45. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  46. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  47. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  48. * SOFTWARE.
  49. */
  50. /*
  51. * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
  52. * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
  53. *
  54. * Permission to use, copy, modify, and distribute this software for any
  55. * purpose with or without fee is hereby granted, provided that the above
  56. * copyright notice and this permission notice appear in all copies.
  57. *
  58. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
  59. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  60. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
  61. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  62. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  63. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  64. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  65. */
  66. #if defined(LIBC_SCCS) && !defined(lint)
  67. static const char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93";
  68. static const char rcsid[] = "$Id: res_comp.c,v 1.5 2005/07/28 06:51:50 marka Exp $";
  69. #endif /* LIBC_SCCS and not lint */
  70. #include <sys/cdefs.h>
  71. __FBSDID("$FreeBSD$");
  72. #include "port_before.h"
  73. #include <sys/param.h>
  74. #include <netinet/in.h>
  75. #include <arpa/nameser.h>
  76. #include <ctype.h>
  77. #include <resolv.h>
  78. #include <stdio.h>
  79. #include <string.h>
  80. #include <unistd.h>
  81. #include "port_after.h"
  82. /*%
  83. * Expand compressed domain name 'src' to full domain name.
  84. *
  85. * \li 'msg' is a pointer to the beginning of the message,
  86. * \li 'eom' points to the first location after the message,
  87. * \li 'dst' is a pointer to a buffer of size 'dstsiz' for the result.
  88. * \li Return size of compressed name or -1 if there was an error.
  89. */
  90. int
  91. dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
  92. char *dst, int dstsiz)
  93. {
  94. int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
  95. if (n > 0 && dst[0] == '.')
  96. dst[0] = '\0';
  97. return (n);
  98. }
  99. /*%
  100. * Pack domain name 'exp_dn' in presentation form into 'comp_dn'.
  101. *
  102. * \li Return the size of the compressed name or -1.
  103. * \li 'length' is the size of the array pointed to by 'comp_dn'.
  104. */
  105. int
  106. dn_comp(const char *src, u_char *dst, int dstsiz,
  107. u_char **dnptrs, u_char **lastdnptr)
  108. {
  109. return (ns_name_compress(src, dst, (size_t)dstsiz,
  110. (const u_char **)dnptrs,
  111. (const u_char **)lastdnptr));
  112. }
  113. /*%
  114. * Skip over a compressed domain name. Return the size or -1.
  115. */
  116. int
  117. dn_skipname(const u_char *ptr, const u_char *eom) {
  118. const u_char *saveptr = ptr;
  119. if (ns_name_skip(&ptr, eom) == -1)
  120. return (-1);
  121. return (ptr - saveptr);
  122. }
  123. /*%
  124. * Verify that a domain name uses an acceptable character set.
  125. *
  126. * Note the conspicuous absence of ctype macros in these definitions. On
  127. * non-ASCII hosts, we can't depend on string literals or ctype macros to
  128. * tell us anything about network-format data. The rest of the BIND system
  129. * is not careful about this, but for some reason, we're doing it right here.
  130. */
  131. #define PERIOD 0x2e
  132. #define hyphenchar(c) ((c) == 0x2d)
  133. #define bslashchar(c) ((c) == 0x5c)
  134. #define underscorechar(c) ((c) == 0x5f)
  135. #define periodchar(c) ((c) == PERIOD)
  136. #define asterchar(c) ((c) == 0x2a)
  137. #define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
  138. || ((c) >= 0x61 && (c) <= 0x7a))
  139. #define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
  140. #ifdef RES_ENFORCE_RFC1034
  141. #define borderchar(c) (alphachar(c) || digitchar(c))
  142. #else
  143. #define borderchar(c) (alphachar(c) || digitchar(c) || underscorechar(c))
  144. #endif
  145. #define middlechar(c) (borderchar(c) || hyphenchar(c))
  146. #define domainchar(c) ((c) > 0x20 && (c) < 0x7f)
  147. int
  148. res_hnok(const char *dn) {
  149. int pch = PERIOD, ch = *dn++;
  150. while (ch != '\0') {
  151. int nch = *dn++;
  152. if (periodchar(ch)) {
  153. (void)NULL;
  154. } else if (periodchar(pch)) {
  155. if (!borderchar(ch))
  156. return (0);
  157. } else if (periodchar(nch) || nch == '\0') {
  158. if (!borderchar(ch))
  159. return (0);
  160. } else {
  161. if (!middlechar(ch))
  162. return (0);
  163. }
  164. pch = ch, ch = nch;
  165. }
  166. return (1);
  167. }
  168. /*%
  169. * hostname-like (A, MX, WKS) owners can have "*" as their first label
  170. * but must otherwise be as a host name.
  171. */
  172. int
  173. res_ownok(const char *dn) {
  174. if (asterchar(dn[0])) {
  175. if (periodchar(dn[1]))
  176. return (res_hnok(dn+2));
  177. if (dn[1] == '\0')
  178. return (1);
  179. }
  180. return (res_hnok(dn));
  181. }
  182. /*%
  183. * SOA RNAMEs and RP RNAMEs can have any printable character in their first
  184. * label, but the rest of the name has to look like a host name.
  185. */
  186. int
  187. res_mailok(const char *dn) {
  188. int ch, escaped = 0;
  189. /* "." is a valid missing representation */
  190. if (*dn == '\0')
  191. return (1);
  192. /* otherwise <label>.<hostname> */
  193. while ((ch = *dn++) != '\0') {
  194. if (!domainchar(ch))
  195. return (0);
  196. if (!escaped && periodchar(ch))
  197. break;
  198. if (escaped)
  199. escaped = 0;
  200. else if (bslashchar(ch))
  201. escaped = 1;
  202. }
  203. if (periodchar(ch))
  204. return (res_hnok(dn));
  205. return (0);
  206. }
  207. /*%
  208. * This function is quite liberal, since RFC1034's character sets are only
  209. * recommendations.
  210. */
  211. int
  212. res_dnok(const char *dn) {
  213. int ch;
  214. while ((ch = *dn++) != '\0')
  215. if (!domainchar(ch))
  216. return (0);
  217. return (1);
  218. }
  219. #ifdef BIND_4_COMPAT
  220. /*%
  221. * This module must export the following externally-visible symbols:
  222. * ___putlong
  223. * ___putshort
  224. * __getlong
  225. * __getshort
  226. * Note that one _ comes from C and the others come from us.
  227. */
  228. #ifdef SOLARIS2
  229. #ifdef __putlong
  230. #undef __putlong
  231. #endif
  232. #ifdef __putshort
  233. #undef __putshort
  234. #endif
  235. #pragma weak putlong = __putlong
  236. #pragma weak putshort = __putshort
  237. #endif /* SOLARIS2 */
  238. void __putlong(u_int32_t src, u_char *dst) { ns_put32(src, dst); }
  239. void __putshort(u_int16_t src, u_char *dst) { ns_put16(src, dst); }
  240. #ifndef __ultrix__
  241. u_int32_t _getlong(const u_char *src) { return (ns_get32(src)); }
  242. u_int16_t _getshort(const u_char *src) { return (ns_get16(src)); }
  243. #endif /*__ultrix__*/
  244. #endif /*BIND_4_COMPAT*/
  245. /*
  246. * Weak aliases for applications that use certain private entry points,
  247. * and fail to include <resolv.h>.
  248. */
  249. #undef dn_comp
  250. __weak_reference(__dn_comp, dn_comp);
  251. #undef dn_expand
  252. __weak_reference(__dn_expand, dn_expand);
  253. /*! \file */