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