resolve.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file resolve.c
  7. * \brief Use the libc DNS resolver to convert hostnames into addresses.
  8. **/
  9. #include "lib/net/resolve.h"
  10. #include "lib/net/address.h"
  11. #include "lib/net/inaddr.h"
  12. #include "lib/malloc/malloc.h"
  13. #include "lib/string/parse_int.h"
  14. #include "lib/string/util_string.h"
  15. #include "ext/siphash.h"
  16. #include "ext/ht.h"
  17. #ifdef HAVE_SYS_TYPES_H
  18. #include <sys/types.h>
  19. #endif
  20. #ifdef HAVE_SYS_SOCKET_H
  21. #include <sys/socket.h>
  22. #endif
  23. #ifdef HAVE_NETDB_H
  24. #include <netdb.h>
  25. #endif
  26. #include <string.h>
  27. /** Similar behavior to Unix gethostbyname: resolve <b>name</b>, and set
  28. * *<b>addr</b> to the proper IP address, in host byte order. Returns 0
  29. * on success, -1 on failure; 1 on transient failure.
  30. *
  31. * This function only accepts IPv4 addresses.
  32. *
  33. * (This function exists because standard windows gethostbyname
  34. * doesn't treat raw IP addresses properly.)
  35. */
  36. MOCK_IMPL(int,
  37. tor_lookup_hostname,(const char *name, uint32_t *addr))
  38. {
  39. tor_addr_t myaddr;
  40. int ret;
  41. if (BUG(!addr))
  42. return -1;
  43. *addr = 0;
  44. if ((ret = tor_addr_lookup(name, AF_INET, &myaddr)))
  45. return ret;
  46. if (tor_addr_family(&myaddr) == AF_INET) {
  47. *addr = tor_addr_to_ipv4h(&myaddr);
  48. return ret;
  49. }
  50. return -1;
  51. }
  52. #ifdef HAVE_GETADDRINFO
  53. /* Host lookup helper for tor_addr_lookup(), when getaddrinfo() is
  54. * available on this system.
  55. *
  56. * See tor_addr_lookup() for details.
  57. */
  58. static int
  59. tor_addr_lookup_host_impl(const char *name,
  60. uint16_t family,
  61. tor_addr_t *addr)
  62. {
  63. int err;
  64. struct addrinfo *res=NULL, *res_p;
  65. struct addrinfo *best=NULL;
  66. struct addrinfo hints;
  67. int result = -1;
  68. memset(&hints, 0, sizeof(hints));
  69. hints.ai_family = family;
  70. hints.ai_socktype = SOCK_STREAM;
  71. err = tor_getaddrinfo(name, NULL, &hints, &res);
  72. /* The check for 'res' here shouldn't be necessary, but it makes static
  73. * analysis tools happy. */
  74. if (!err && res) {
  75. best = NULL;
  76. for (res_p = res; res_p; res_p = res_p->ai_next) {
  77. if (family == AF_UNSPEC) {
  78. if (res_p->ai_family == AF_INET) {
  79. best = res_p;
  80. break;
  81. } else if (res_p->ai_family == AF_INET6 && !best) {
  82. best = res_p;
  83. }
  84. } else if (family == res_p->ai_family) {
  85. best = res_p;
  86. break;
  87. }
  88. }
  89. if (!best)
  90. best = res;
  91. if (best->ai_family == AF_INET) {
  92. tor_addr_from_in(addr,
  93. &((struct sockaddr_in*)best->ai_addr)->sin_addr);
  94. result = 0;
  95. } else if (best->ai_family == AF_INET6) {
  96. tor_addr_from_in6(addr,
  97. &((struct sockaddr_in6*)best->ai_addr)->sin6_addr);
  98. result = 0;
  99. }
  100. tor_freeaddrinfo(res);
  101. return result;
  102. }
  103. return (err == EAI_AGAIN) ? 1 : -1;
  104. }
  105. #else /* !defined(HAVE_GETADDRINFO) */
  106. /* Host lookup helper for tor_addr_lookup(), which calls gethostbyname().
  107. * Used when getaddrinfo() is not available on this system.
  108. *
  109. * See tor_addr_lookup() for details.
  110. */
  111. static int
  112. tor_addr_lookup_host_impl(const char *name,
  113. uint16_t family,
  114. tor_addr_t *addr)
  115. {
  116. (void) family;
  117. struct hostent *ent;
  118. int err;
  119. #ifdef HAVE_GETHOSTBYNAME_R_6_ARG
  120. char buf[2048];
  121. struct hostent hostent;
  122. int r;
  123. r = gethostbyname_r(name, &hostent, buf, sizeof(buf), &ent, &err);
  124. #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
  125. char buf[2048];
  126. struct hostent hostent;
  127. ent = gethostbyname_r(name, &hostent, buf, sizeof(buf), &err);
  128. #elif defined(HAVE_GETHOSTBYNAME_R_3_ARG)
  129. struct hostent_data data;
  130. struct hostent hent;
  131. memset(&data, 0, sizeof(data));
  132. err = gethostbyname_r(name, &hent, &data);
  133. ent = err ? NULL : &hent;
  134. #else
  135. ent = gethostbyname(name);
  136. #ifdef _WIN32
  137. err = WSAGetLastError();
  138. #else
  139. err = h_errno;
  140. #endif /* defined(_WIN32) */
  141. #endif /* defined(HAVE_GETHOSTBYNAME_R_6_ARG) || ... */
  142. if (ent) {
  143. if (ent->h_addrtype == AF_INET) {
  144. tor_addr_from_in(addr, (struct in_addr*) ent->h_addr);
  145. } else if (ent->h_addrtype == AF_INET6) {
  146. tor_addr_from_in6(addr, (struct in6_addr*) ent->h_addr);
  147. } else {
  148. tor_assert(0); // LCOV_EXCL_LINE: gethostbyname() returned bizarre type
  149. }
  150. return 0;
  151. }
  152. #ifdef _WIN32
  153. return (err == WSATRY_AGAIN) ? 1 : -1;
  154. #else
  155. return (err == TRY_AGAIN) ? 1 : -1;
  156. #endif
  157. }
  158. #endif /* defined(HAVE_GETADDRINFO) */
  159. /** Similar behavior to Unix gethostbyname: resolve <b>name</b>, and set
  160. * *<b>addr</b> to the proper IP address and family. The <b>family</b>
  161. * argument (which must be AF_INET, AF_INET6, or AF_UNSPEC) declares a
  162. * <i>preferred</i> family, though another one may be returned if only one
  163. * family is implemented for this address.
  164. *
  165. * Like tor_addr_parse(), this function accepts IPv6 addresses with or without
  166. * square brackets.
  167. *
  168. * Return 0 on success, -1 on failure; 1 on transient failure.
  169. */
  170. MOCK_IMPL(int,
  171. tor_addr_lookup,(const char *name, uint16_t family, tor_addr_t *addr))
  172. {
  173. /* Perhaps eventually this should be replaced by a tor_getaddrinfo or
  174. * something.
  175. */
  176. int parsed_family = 0;
  177. int result = -1;
  178. tor_assert(name);
  179. tor_assert(addr);
  180. tor_assert(family == AF_INET || family == AF_INET6 || family == AF_UNSPEC);
  181. if (!*name) {
  182. /* Empty address is an error. */
  183. goto permfail;
  184. }
  185. /* Is it an IP address? */
  186. parsed_family = tor_addr_parse(addr, name);
  187. if (parsed_family >= 0) {
  188. /* If the IP address family matches, or was unspecified */
  189. if (parsed_family == family || family == AF_UNSPEC) {
  190. goto success;
  191. } else {
  192. goto permfail;
  193. }
  194. } else {
  195. /* Clear the address after a failed tor_addr_parse(). */
  196. memset(addr, 0, sizeof(tor_addr_t));
  197. result = tor_addr_lookup_host_impl(name, family, addr);
  198. goto done;
  199. }
  200. /* If we weren't successful, and haven't already set the result,
  201. * assume it's a permanent failure */
  202. permfail:
  203. result = -1;
  204. goto done;
  205. success:
  206. result = 0;
  207. /* We have set the result, now it's time to clean up */
  208. done:
  209. if (result) {
  210. /* Clear the address on error */
  211. memset(addr, 0, sizeof(tor_addr_t));
  212. }
  213. return result;
  214. }
  215. /** Parse an address or address-port combination from <b>s</b>, resolve the
  216. * address as needed, and put the result in <b>addr_out</b> and (optionally)
  217. * <b>port_out</b>.
  218. *
  219. * Like tor_addr_port_parse(), this function accepts:
  220. * - IPv6 address and port, when the IPv6 address is in square brackets,
  221. * - IPv6 address with square brackets,
  222. * - IPv6 address without square brackets.
  223. *
  224. * Return 0 on success, negative on failure. */
  225. int
  226. tor_addr_port_lookup(const char *s, tor_addr_t *addr_out, uint16_t *port_out)
  227. {
  228. tor_addr_t addr;
  229. uint16_t portval = 0;
  230. char *tmp = NULL;
  231. int rv = 0;
  232. int result;
  233. tor_assert(s);
  234. tor_assert(addr_out);
  235. s = eat_whitespace(s);
  236. /* Try parsing s as an address:port first, so we don't have to duplicate
  237. * the logic that rejects IPv6:Port with no square brackets. */
  238. rv = tor_addr_port_parse(LOG_WARN, s, &addr, &portval, 0);
  239. /* That was easy, no DNS required. */
  240. if (rv == 0)
  241. goto success;
  242. /* Now let's check for malformed IPv6 addresses and ports:
  243. * tor_addr_port_parse() requires squared brackes if there is a port,
  244. * and we want tor_addr_port_lookup() to have the same requirement.
  245. * But we strip the port using tor_addr_port_split(), so tor_addr_lookup()
  246. * only sees the address, and will accept it without square brackets. */
  247. int family = tor_addr_parse(&addr, s);
  248. /* If tor_addr_parse() succeeds where tor_addr_port_parse() failed, we need
  249. * to reject this address as malformed. */
  250. if (family >= 0) {
  251. /* Double-check it's an IPv6 address. If not, we have a parsing bug.
  252. */
  253. tor_assertf_nonfatal(family == AF_INET6,
  254. "Wrong family: %d (should be IPv6: %d) which "
  255. "failed IP:port parsing, but passed IP parsing. "
  256. "input string: '%s'; parsed address: '%s'.",
  257. family, AF_INET6, s, fmt_addr(&addr));
  258. goto err;
  259. }
  260. /* Now we have a hostname. Let's split off the port, if any. */
  261. rv = tor_addr_port_split(LOG_WARN, s, &tmp, &portval);
  262. if (rv < 0)
  263. goto err;
  264. /* And feed the hostname to the lookup function. */
  265. if (tor_addr_lookup(tmp, AF_UNSPEC, &addr) != 0)
  266. goto err;
  267. success:
  268. if (port_out)
  269. *port_out = portval;
  270. tor_addr_copy(addr_out, &addr);
  271. result = 0;
  272. goto done;
  273. err:
  274. /* Clear the address and port on error */
  275. memset(addr_out, 0, sizeof(tor_addr_t));
  276. if (port_out)
  277. *port_out = 0;
  278. result = -1;
  279. /* We have set the result, now it's time to clean up */
  280. done:
  281. tor_free(tmp);
  282. return result;
  283. }
  284. #ifdef USE_SANDBOX_GETADDRINFO
  285. /** True if we should only return cached values */
  286. static int sandbox_getaddrinfo_is_active = 0;
  287. /** Cache entry for getaddrinfo results; used when sandboxing is implemented
  288. * so that we can consult the cache when the sandbox prevents us from doing
  289. * getaddrinfo.
  290. *
  291. * We support only a limited range of getaddrinfo calls, where servname is null
  292. * and hints contains only socktype=SOCK_STREAM, family in INET,INET6,UNSPEC.
  293. */
  294. typedef struct cached_getaddrinfo_item_t {
  295. HT_ENTRY(cached_getaddrinfo_item_t) node;
  296. char *name;
  297. int family;
  298. /** set if no error; otherwise NULL */
  299. struct addrinfo *res;
  300. /** 0 for no error; otherwise an EAI_* value */
  301. int err;
  302. } cached_getaddrinfo_item_t;
  303. static unsigned
  304. cached_getaddrinfo_item_hash(const cached_getaddrinfo_item_t *item)
  305. {
  306. return (unsigned)siphash24g(item->name, strlen(item->name)) + item->family;
  307. }
  308. static unsigned
  309. cached_getaddrinfo_items_eq(const cached_getaddrinfo_item_t *a,
  310. const cached_getaddrinfo_item_t *b)
  311. {
  312. return (a->family == b->family) && 0 == strcmp(a->name, b->name);
  313. }
  314. #define cached_getaddrinfo_item_free(item) \
  315. FREE_AND_NULL(cached_getaddrinfo_item_t, \
  316. cached_getaddrinfo_item_free_, (item))
  317. static void
  318. cached_getaddrinfo_item_free_(cached_getaddrinfo_item_t *item)
  319. {
  320. if (item == NULL)
  321. return;
  322. tor_free(item->name);
  323. if (item->res)
  324. freeaddrinfo(item->res);
  325. tor_free(item);
  326. }
  327. static HT_HEAD(getaddrinfo_cache, cached_getaddrinfo_item_t)
  328. getaddrinfo_cache = HT_INITIALIZER();
  329. HT_PROTOTYPE(getaddrinfo_cache, cached_getaddrinfo_item_t, node,
  330. cached_getaddrinfo_item_hash,
  331. cached_getaddrinfo_items_eq)
  332. HT_GENERATE2(getaddrinfo_cache, cached_getaddrinfo_item_t, node,
  333. cached_getaddrinfo_item_hash,
  334. cached_getaddrinfo_items_eq,
  335. 0.6, tor_reallocarray_, tor_free_)
  336. /** If true, don't try to cache getaddrinfo results. */
  337. static int sandbox_getaddrinfo_cache_disabled = 0;
  338. /** Tell the sandbox layer not to try to cache getaddrinfo results. Used as in
  339. * tor-resolve, when we have no intention of initializing crypto or of
  340. * installing the sandbox.*/
  341. void
  342. sandbox_disable_getaddrinfo_cache(void)
  343. {
  344. sandbox_getaddrinfo_cache_disabled = 1;
  345. }
  346. void
  347. tor_freeaddrinfo(struct addrinfo *ai)
  348. {
  349. if (sandbox_getaddrinfo_cache_disabled)
  350. freeaddrinfo(ai);
  351. }
  352. int
  353. tor_getaddrinfo(const char *name, const char *servname,
  354. const struct addrinfo *hints,
  355. struct addrinfo **res)
  356. {
  357. int err;
  358. struct cached_getaddrinfo_item_t search, *item;
  359. if (sandbox_getaddrinfo_cache_disabled) {
  360. return getaddrinfo(name, NULL, hints, res);
  361. }
  362. if (servname != NULL) {
  363. log_warn(LD_BUG, "called with non-NULL servname");
  364. return EAI_NONAME;
  365. }
  366. if (name == NULL) {
  367. log_warn(LD_BUG, "called with NULL name");
  368. return EAI_NONAME;
  369. }
  370. *res = NULL;
  371. memset(&search, 0, sizeof(search));
  372. search.name = (char *) name;
  373. search.family = hints ? hints->ai_family : AF_UNSPEC;
  374. item = HT_FIND(getaddrinfo_cache, &getaddrinfo_cache, &search);
  375. if (! sandbox_getaddrinfo_is_active) {
  376. /* If the sandbox is not turned on yet, then getaddrinfo and store the
  377. result. */
  378. err = getaddrinfo(name, NULL, hints, res);
  379. log_info(LD_NET,"(Sandbox) getaddrinfo %s.", err ? "failed" : "succeeded");
  380. if (! item) {
  381. item = tor_malloc_zero(sizeof(*item));
  382. item->name = tor_strdup(name);
  383. item->family = hints ? hints->ai_family : AF_UNSPEC;
  384. HT_INSERT(getaddrinfo_cache, &getaddrinfo_cache, item);
  385. }
  386. if (item->res) {
  387. freeaddrinfo(item->res);
  388. item->res = NULL;
  389. }
  390. item->res = *res;
  391. item->err = err;
  392. return err;
  393. }
  394. /* Otherwise, the sandbox is on. If we have an item, yield its cached
  395. result. */
  396. if (item) {
  397. *res = item->res;
  398. return item->err;
  399. }
  400. /* getting here means something went wrong */
  401. log_err(LD_BUG,"(Sandbox) failed to get address %s!", name);
  402. return EAI_NONAME;
  403. }
  404. int
  405. tor_add_addrinfo(const char *name)
  406. {
  407. struct addrinfo *res;
  408. struct addrinfo hints;
  409. int i;
  410. static const int families[] = { AF_INET, AF_INET6, AF_UNSPEC };
  411. memset(&hints, 0, sizeof(hints));
  412. hints.ai_socktype = SOCK_STREAM;
  413. for (i = 0; i < 3; ++i) {
  414. hints.ai_family = families[i];
  415. res = NULL;
  416. (void) tor_getaddrinfo(name, NULL, &hints, &res);
  417. if (res)
  418. tor_freeaddrinfo(res);
  419. }
  420. return 0;
  421. }
  422. void
  423. tor_free_getaddrinfo_cache(void)
  424. {
  425. cached_getaddrinfo_item_t **next, **item, *this;
  426. for (item = HT_START(getaddrinfo_cache, &getaddrinfo_cache);
  427. item;
  428. item = next) {
  429. this = *item;
  430. next = HT_NEXT_RMV(getaddrinfo_cache, &getaddrinfo_cache, item);
  431. cached_getaddrinfo_item_free(this);
  432. }
  433. HT_CLEAR(getaddrinfo_cache, &getaddrinfo_cache);
  434. }
  435. void
  436. tor_make_getaddrinfo_cache_active(void)
  437. {
  438. sandbox_getaddrinfo_is_active = 1;
  439. }
  440. #else /* !defined(USE_SANDBOX_GETADDRINFO) */
  441. void
  442. sandbox_disable_getaddrinfo_cache(void)
  443. {
  444. }
  445. void
  446. tor_make_getaddrinfo_cache_active(void)
  447. {
  448. }
  449. #endif /* defined(USE_SANDBOX_GETADDRINFO) */