fake-rfc2553.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* Taken for Dropbear from OpenSSH 5.5p1 */
  2. /*
  3. * Copyright (C) 2000-2003 Damien Miller. All rights reserved.
  4. * Copyright (C) 1999 WIDE Project. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. /*
  31. * Pseudo-implementation of RFC2553 name / address resolution functions
  32. *
  33. * But these functions are not implemented correctly. The minimum subset
  34. * is implemented for ssh use only. For example, this routine assumes
  35. * that ai_family is AF_INET. Don't use it for another purpose.
  36. */
  37. #include "includes.h"
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <netinet/in.h>
  41. #include <arpa/inet.h>
  42. #ifndef HAVE_GETNAMEINFO
  43. int getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
  44. size_t hostlen, char *serv, size_t servlen, int flags)
  45. {
  46. struct sockaddr_in *sin = (struct sockaddr_in *)sa;
  47. struct hostent *hp;
  48. char tmpserv[16];
  49. if (sa->sa_family != AF_UNSPEC && sa->sa_family != AF_INET)
  50. return (EAI_FAMILY);
  51. if (serv != NULL) {
  52. snprintf(tmpserv, sizeof(tmpserv), "%d", ntohs(sin->sin_port));
  53. if (strlcpy(serv, tmpserv, servlen) >= servlen)
  54. return (EAI_MEMORY);
  55. }
  56. if (host != NULL) {
  57. if (flags & NI_NUMERICHOST) {
  58. if (strlcpy(host, inet_ntoa(sin->sin_addr),
  59. hostlen) >= hostlen)
  60. return (EAI_MEMORY);
  61. else
  62. return (0);
  63. } else {
  64. hp = gethostbyaddr((char *)&sin->sin_addr,
  65. sizeof(struct in_addr), AF_INET);
  66. if (hp == NULL)
  67. return (EAI_NODATA);
  68. if (strlcpy(host, hp->h_name, hostlen) >= hostlen)
  69. return (EAI_MEMORY);
  70. else
  71. return (0);
  72. }
  73. }
  74. return (0);
  75. }
  76. #endif /* !HAVE_GETNAMEINFO */
  77. #ifndef HAVE_GAI_STRERROR
  78. #ifdef HAVE_CONST_GAI_STRERROR_PROTO
  79. const char *
  80. #else
  81. char *
  82. #endif
  83. gai_strerror(int err)
  84. {
  85. switch (err) {
  86. case EAI_NODATA:
  87. return ("no address associated with name");
  88. case EAI_MEMORY:
  89. return ("memory allocation failure.");
  90. case EAI_NONAME:
  91. return ("nodename nor servname provided, or not known");
  92. case EAI_FAMILY:
  93. return ("ai_family not supported");
  94. default:
  95. return ("unknown/invalid error.");
  96. }
  97. }
  98. #endif /* !HAVE_GAI_STRERROR */
  99. #ifndef HAVE_FREEADDRINFO
  100. void
  101. freeaddrinfo(struct addrinfo *ai)
  102. {
  103. struct addrinfo *next;
  104. for(; ai != NULL;) {
  105. next = ai->ai_next;
  106. free(ai);
  107. ai = next;
  108. }
  109. }
  110. #endif /* !HAVE_FREEADDRINFO */
  111. #ifndef HAVE_GETADDRINFO
  112. static struct
  113. addrinfo *malloc_ai(int port, u_long addr, const struct addrinfo *hints)
  114. {
  115. struct addrinfo *ai;
  116. ai = malloc(sizeof(*ai) + sizeof(struct sockaddr_in));
  117. if (ai == NULL)
  118. return (NULL);
  119. memset(ai, '\0', sizeof(*ai) + sizeof(struct sockaddr_in));
  120. ai->ai_addr = (struct sockaddr *)(ai + 1);
  121. /* XXX -- ssh doesn't use sa_len */
  122. ai->ai_addrlen = sizeof(struct sockaddr_in);
  123. ai->ai_addr->sa_family = ai->ai_family = AF_INET;
  124. ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
  125. ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
  126. /* XXX: the following is not generally correct, but does what we want */
  127. if (hints->ai_socktype)
  128. ai->ai_socktype = hints->ai_socktype;
  129. else
  130. ai->ai_socktype = SOCK_STREAM;
  131. if (hints->ai_protocol)
  132. ai->ai_protocol = hints->ai_protocol;
  133. return (ai);
  134. }
  135. int
  136. getaddrinfo(const char *hostname, const char *servname,
  137. const struct addrinfo *hints, struct addrinfo **res)
  138. {
  139. struct hostent *hp;
  140. struct servent *sp;
  141. struct in_addr in;
  142. int i;
  143. long int port;
  144. u_long addr;
  145. port = 0;
  146. if (hints && hints->ai_family != AF_UNSPEC &&
  147. hints->ai_family != AF_INET)
  148. return (EAI_FAMILY);
  149. if (servname != NULL) {
  150. char *cp;
  151. port = strtol(servname, &cp, 10);
  152. if (port > 0 && port <= 65535 && *cp == '\0')
  153. port = htons(port);
  154. else if ((sp = getservbyname(servname, NULL)) != NULL)
  155. port = sp->s_port;
  156. else
  157. port = 0;
  158. }
  159. if (hints && hints->ai_flags & AI_PASSIVE) {
  160. addr = htonl(0x00000000);
  161. if (hostname && inet_aton(hostname, &in) != 0)
  162. addr = in.s_addr;
  163. *res = malloc_ai(port, addr, hints);
  164. if (*res == NULL)
  165. return (EAI_MEMORY);
  166. return (0);
  167. }
  168. if (!hostname) {
  169. *res = malloc_ai(port, htonl(0x7f000001), hints);
  170. if (*res == NULL)
  171. return (EAI_MEMORY);
  172. return (0);
  173. }
  174. if (inet_aton(hostname, &in)) {
  175. *res = malloc_ai(port, in.s_addr, hints);
  176. if (*res == NULL)
  177. return (EAI_MEMORY);
  178. return (0);
  179. }
  180. /* Don't try DNS if AI_NUMERICHOST is set */
  181. if (hints && hints->ai_flags & AI_NUMERICHOST)
  182. return (EAI_NONAME);
  183. hp = gethostbyname(hostname);
  184. if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
  185. struct addrinfo *cur, *prev;
  186. cur = prev = *res = NULL;
  187. for (i = 0; hp->h_addr_list[i]; i++) {
  188. struct in_addr *in = (struct in_addr *)hp->h_addr_list[i];
  189. cur = malloc_ai(port, in->s_addr, hints);
  190. if (cur == NULL) {
  191. if (*res != NULL)
  192. freeaddrinfo(*res);
  193. return (EAI_MEMORY);
  194. }
  195. if (prev)
  196. prev->ai_next = cur;
  197. else
  198. *res = cur;
  199. prev = cur;
  200. }
  201. return (0);
  202. }
  203. return (EAI_NODATA);
  204. }
  205. #endif /* !HAVE_GETADDRINFO */