aboutsummaryrefslogtreecommitdiff
path: root/src/lib/net/resolve.c
blob: 68a8c01ef470fdec3d8650a313899aa70c137b24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/* Copyright (c) 2003-2004, Roger Dingledine
 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
 * Copyright (c) 2007-2020, The Tor Project, Inc. */
/* See LICENSE for licensing information */

/**
 * \file resolve.c
 * \brief Use the libc DNS resolver to convert hostnames into addresses.
 **/

#define RESOLVE_PRIVATE
#include "lib/net/resolve.h"

#include "lib/net/address.h"
#include "lib/net/inaddr.h"
#include "lib/malloc/malloc.h"
#include "lib/string/parse_int.h"
#include "lib/string/util_string.h"

#include "ext/siphash.h"
#include "ext/ht.h"

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif

#include <string.h>

/** Similar behavior to Unix gethostbyname: resolve <b>name</b>, and set
 * *<b>addr</b> to the proper IP address, in host byte order.  Returns 0
 * on success, -1 on failure; 1 on transient failure.
 *
 * This function only accepts IPv4 addresses.
 *
 * (This function exists because standard windows gethostbyname
 * doesn't treat raw IP addresses properly.)
 */

MOCK_IMPL(int,
tor_lookup_hostname,(const char *name, uint32_t *addr))
{
  tor_addr_t myaddr;
  int ret;

  if (BUG(!addr))
    return -1;

  *addr = 0;

  if ((ret = tor_addr_lookup(name, AF_INET, &myaddr)))
    return ret;

  if (tor_addr_family(&myaddr) == AF_INET) {
    *addr = tor_addr_to_ipv4h(&myaddr);
    return ret;
  }

  return -1;
}

#ifdef HAVE_GETADDRINFO

/* Host lookup helper for tor_addr_lookup(), when getaddrinfo() is
 * available on this system.
 *
 * See tor_addr_lookup() for details.
 */
MOCK_IMPL(STATIC int,
tor_addr_lookup_host_impl,(const char *name,
                          uint16_t family,
                          tor_addr_t *addr))
{
  int err;
  struct addrinfo *res=NULL, *res_p;
  struct addrinfo *best=NULL;
  struct addrinfo hints;
  int result = -1;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = family;
  hints.ai_socktype = SOCK_STREAM;
  err = tor_getaddrinfo(name, NULL, &hints, &res);
  /* The check for 'res' here shouldn't be necessary, but it makes static
   * analysis tools happy. */
  if (!err && res) {
    best = NULL;
    for (res_p = res; res_p; res_p = res_p->ai_next) {
      if (family == AF_UNSPEC) {
        if (res_p->ai_family == AF_INET) {
          best = res_p;
          break;
        } else if (res_p->ai_family == AF_INET6 && !best) {
          best = res_p;
        }
      } else if (family == res_p->ai_family) {
        best = res_p;
        break;
      }
    }
    if (!best)
      best = res;
    if (best->ai_family == AF_INET) {
      tor_addr_from_in(addr,
                       &((struct sockaddr_in*)best->ai_addr)->sin_addr);
      result = 0;
    } else if (best->ai_family == AF_INET6) {
      tor_addr_from_in6(addr,
                        &((struct sockaddr_in6*)best->ai_addr)->sin6_addr);
      result = 0;
    }
    tor_freeaddrinfo(res);
    return result;
  }
  return (err == EAI_AGAIN) ? 1 : -1;
}

#else /* !defined(HAVE_GETADDRINFO) */

/* Host lookup helper for tor_addr_lookup(), which calls gethostbyname().
 * Used when getaddrinfo() is not available on this system.
 *
 * See tor_addr_lookup() for details.
 */
MOCK_IMPL(STATIC int,
tor_addr_lookup_host_impl,(const char *name,
                          uint16_t family,
                           tor_addr_t *addr))
{
  (void) family;
  struct hostent *ent;
  int err;
#ifdef HAVE_GETHOSTBYNAME_R_6_ARG
  char buf[2048];
  struct hostent hostent;
  int r;
  r = gethostbyname_r(name, &hostent, buf, sizeof(buf), &ent, &err);
#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
  char buf[2048];
  struct hostent hostent;
  ent = gethostbyname_r(name, &hostent, buf, sizeof(buf), &err);
#elif defined(HAVE_GETHOSTBYNAME_R_3_ARG)
  struct hostent_data data;
  struct hostent hent;
  memset(&data, 0, sizeof(data));
  err = gethostbyname_r(name, &hent, &data);
  ent = err ? NULL : &hent;
#else
  ent = gethostbyname(name);
#ifdef _WIN32
  err = WSAGetLastError();
#else
  err = h_errno;
#endif /* defined(_WIN32) */
#endif /* defined(HAVE_GETHOSTBYNAME_R_6_ARG) || ... */
  if (ent) {
    if (ent->h_addrtype == AF_INET) {
      tor_addr_from_in(addr, (struct in_addr*) ent->h_addr);
    } else if (ent->h_addrtype == AF_INET6) {
      tor_addr_from_in6(addr, (struct in6_addr*) ent->h_addr);
    } else {
      tor_assert(0); // LCOV_EXCL_LINE: gethostbyname() returned bizarre type
    }
    return 0;
  }
#ifdef _WIN32
  return (err == WSATRY_AGAIN) ? 1 : -1;
#else
  return (err == TRY_AGAIN) ? 1 : -1;
#endif
}
#endif /* defined(HAVE_GETADDRINFO) */

/** Similar behavior to Unix gethostbyname: resolve <b>name</b>, and set
 * *<b>addr</b> to the proper IP address and family. The <b>family</b>
 * argument (which must be AF_INET, AF_INET6, or AF_UNSPEC) declares a
 * <i>preferred</i> family, though another one may be returned if only one
 * family is implemented for this address.
 *
 * Like tor_addr_parse(), this function accepts IPv6 addresses with or without
 * square brackets.
 *
 * Return 0 on success, -1 on failure; 1 on transient failure.
 */
MOCK_IMPL(int,
tor_addr_lookup,(const char *name, uint16_t family, tor_addr_t *addr))
{
  /* Perhaps eventually this should be replaced by a tor_getaddrinfo or
   * something.
   */
  int parsed_family = 0;
  int result = -1;

  tor_assert(name);
  tor_assert(addr);
  tor_assert(family == AF_INET || family == AF_INET6 || family == AF_UNSPEC);

  if (!*name) {
    /* Empty address is an error. */
    goto permfail;
  }

  /* Is it an IP address? */
  parsed_family = tor_addr_parse(addr, name);

  if (parsed_family >= 0) {
    /* If the IP address family matches, or was unspecified */
    if (parsed_family == family || family == AF_UNSPEC) {
      goto success;
    } else {
      goto permfail;
    }
  } else {
    /* Clear the address after a failed tor_addr_parse(). */
    memset(addr, 0, sizeof(tor_addr_t));
    result = tor_addr_lookup_host_impl(name, family, addr);
    goto done;
  }

 /* If we weren't successful, and haven't already set the result,
  * assume it's a permanent failure */
 permfail:
  result = -1;
  goto done;
 success:
  result = 0;

 /* We have set the result, now it's time to clean up */
 done:
  if (result) {
    /* Clear the address on error */
    memset(addr, 0, sizeof(tor_addr_t));
  }
  return result;
}

/** Parse an address or address-port combination from <b>s</b>, resolve the
 * address as needed, and put the result in <b>addr_out</b> and (optionally)
 * <b>port_out</b>.
 *
 * Like tor_addr_port_parse(), this function accepts:
 *  - IPv6 address and port, when the IPv6 address is in square brackets,
 *  - IPv6 address with square brackets,
 *  - IPv6 address without square brackets.
 *
 * Return 0 on success, negative on failure. */
int
tor_addr_port_lookup(const char *s, tor_addr_t *addr_out, uint16_t *port_out)
{
  tor_addr_t addr;
  uint16_t portval = 0;
  char *tmp = NULL;
  int rv = 0;
  int result;

  tor_assert(s);
  tor_assert(addr_out);

  s = eat_whitespace(s);

  /* Try parsing s as an address:port first, so we don't have to duplicate
   * the logic that rejects IPv6:Port with no square brackets. */
  rv = tor_addr_port_parse(LOG_WARN, s, &addr, &portval, 0);
  /* That was easy, no DNS required. */
  if (rv == 0)
    goto success;

  /* Now let's check for malformed IPv6 addresses and ports:
   * tor_addr_port_parse() requires squared brackes if there is a port,
   * and we want tor_addr_port_lookup() to have the same requirement.
   * But we strip the port using tor_addr_port_split(), so tor_addr_lookup()
   * only sees the address, and will accept it without square brackets. */
  int family = tor_addr_parse(&addr, s);
  /* If tor_addr_parse() succeeds where tor_addr_port_parse() failed, we need
   * to reject this address as malformed. */
  if (family >= 0) {
    /* Double-check it's an IPv6 address. If not, we have a parsing bug.
     */
    tor_assertf_nonfatal(family == AF_INET6,
                         "Wrong family: %d (should be IPv6: %d) which "
                         "failed IP:port parsing, but passed IP parsing. "
                         "input string: '%s'; parsed address: '%s'.",
                         family, AF_INET6, s, fmt_addr(&addr));
    goto err;
  }

  /* Now we have a hostname. Let's split off the port, if any. */
  rv = tor_addr_port_split(LOG_WARN, s, &tmp, &portval);
  if (rv < 0)
    goto err;

  /* And feed the hostname to the lookup function. */
  if (tor_addr_lookup(tmp, AF_UNSPEC, &addr) != 0)
    goto err;

 success:
  if (port_out)
    *port_out = portval;
  tor_addr_copy(addr_out, &addr);
  result = 0;
  goto done;

 err:
  /* Clear the address and port on error */
  memset(addr_out, 0, sizeof(tor_addr_t));
  if (port_out)
    *port_out = 0;
  result = -1;

 /* We have set the result, now it's time to clean up */
 done:
  tor_free(tmp);
  return result;
}

#ifdef USE_SANDBOX_GETADDRINFO
/** True if we should only return cached values */
static int sandbox_getaddrinfo_is_active = 0;

/** Cache entry for getaddrinfo results; used when sandboxing is implemented
 * so that we can consult the cache when the sandbox prevents us from doing
 * getaddrinfo.
 *
 * We support only a limited range of getaddrinfo calls, where servname is null
 * and hints contains only socktype=SOCK_STREAM, family in INET,INET6,UNSPEC.
 */
typedef struct cached_getaddrinfo_item_t {
  HT_ENTRY(cached_getaddrinfo_item_t) node;
  char *name;
  int family;
  /** set if no error; otherwise NULL */
  struct addrinfo *res;
  /** 0 for no error; otherwise an EAI_* value */
  int err;
} cached_getaddrinfo_item_t;

static unsigned
cached_getaddrinfo_item_hash(const cached_getaddrinfo_item_t *item)
{
  return (unsigned)siphash24g(item->name, strlen(item->name)) + item->family;
}

static unsigned
cached_getaddrinfo_items_eq(const cached_getaddrinfo_item_t *a,
                            const cached_getaddrinfo_item_t *b)
{
  return (a->family == b->family) && 0 == strcmp(a->name, b->name);
}

#define cached_getaddrinfo_item_free(item)              \
  FREE_AND_NULL(cached_getaddrinfo_item_t,              \
                cached_getaddrinfo_item_free_, (item))

static void
cached_getaddrinfo_item_free_(cached_getaddrinfo_item_t *item)
{
  if (item == NULL)
    return;

  tor_free(item->name);
  if (item->res)
    freeaddrinfo(item->res);
  tor_free(item);
}

static HT_HEAD(getaddrinfo_cache, cached_getaddrinfo_item_t)
     getaddrinfo_cache = HT_INITIALIZER();

HT_PROTOTYPE(getaddrinfo_cache, cached_getaddrinfo_item_t, node,
             cached_getaddrinfo_item_hash,
             cached_getaddrinfo_items_eq);
HT_GENERATE2(getaddrinfo_cache, cached_getaddrinfo_item_t, node,
             cached_getaddrinfo_item_hash,
             cached_getaddrinfo_items_eq,
             0.6, tor_reallocarray_, tor_free_);

/** If true, don't try to cache getaddrinfo results. */
static int sandbox_getaddrinfo_cache_disabled = 0;

/** Tell the sandbox layer not to try to cache getaddrinfo results. Used as in
 * tor-resolve, when we have no intention of initializing crypto or of
 * installing the sandbox.*/
void
sandbox_disable_getaddrinfo_cache(void)
{
  sandbox_getaddrinfo_cache_disabled = 1;
}

void
tor_freeaddrinfo(struct addrinfo *ai)
{
  if (sandbox_getaddrinfo_cache_disabled)
    freeaddrinfo(ai);
}

int
tor_getaddrinfo(const char *name, const char *servname,
                const struct addrinfo *hints,
                struct addrinfo **res)
{
  int err;
  struct cached_getaddrinfo_item_t search, *item;

  if (sandbox_getaddrinfo_cache_disabled) {
    return getaddrinfo(name, NULL, hints, res);
  }

  if (servname != NULL) {
    log_warn(LD_BUG, "called with non-NULL servname");
    return EAI_NONAME;
  }
  if (name == NULL) {
    log_warn(LD_BUG, "called with NULL name");
    return EAI_NONAME;
  }

  *res = NULL;

  memset(&search, 0, sizeof(search));
  search.name = (char *) name;
  search.family = hints ? hints->ai_family : AF_UNSPEC;
  item = HT_FIND(getaddrinfo_cache, &getaddrinfo_cache, &search);

  if (! sandbox_getaddrinfo_is_active) {
    /* If the sandbox is not turned on yet, then getaddrinfo and store the
       result. */

    err = getaddrinfo(name, NULL, hints, res);
    log_info(LD_NET,"(Sandbox) getaddrinfo %s.", err ? "failed" : "succeeded");

    if (! item) {
      item = tor_malloc_zero(sizeof(*item));
      item->name = tor_strdup(name);
      item->family = hints ? hints->ai_family : AF_UNSPEC;
      HT_INSERT(getaddrinfo_cache, &getaddrinfo_cache, item);
    }

    if (item->res) {
      freeaddrinfo(item->res);
      item->res = NULL;
    }
    item->res = *res;
    item->err = err;
    return err;
  }

  /* Otherwise, the sandbox is on.  If we have an item, yield its cached
     result. */
  if (item) {
    *res = item->res;
    return item->err;
  }

  /* getting here means something went wrong */
  log_err(LD_BUG,"(Sandbox) failed to get address %s!", name);
  return EAI_NONAME;
}

int
tor_add_addrinfo(const char *name)
{
  struct addrinfo *res;
  struct addrinfo hints;
  int i;
  static const int families[] = { AF_INET, AF_INET6, AF_UNSPEC };

  memset(&hints, 0, sizeof(hints));
  hints.ai_socktype = SOCK_STREAM;
  for (i = 0; i < 3; ++i) {
    hints.ai_family = families[i];

    res = NULL;
    (void) tor_getaddrinfo(name, NULL, &hints, &res);
    if (res)
      tor_freeaddrinfo(res);
  }

  return 0;
}

void
tor_free_getaddrinfo_cache(void)
{
  cached_getaddrinfo_item_t **next, **item, *this;

  for (item = HT_START(getaddrinfo_cache, &getaddrinfo_cache);
       item;
       item = next) {
    this = *item;
    next = HT_NEXT_RMV(getaddrinfo_cache, &getaddrinfo_cache, item);
    cached_getaddrinfo_item_free(this);
  }

  HT_CLEAR(getaddrinfo_cache, &getaddrinfo_cache);
}

void
tor_make_getaddrinfo_cache_active(void)
{
  sandbox_getaddrinfo_is_active = 1;
}
#else /* !defined(USE_SANDBOX_GETADDRINFO) */
void
sandbox_disable_getaddrinfo_cache(void)
{
}
void
tor_make_getaddrinfo_cache_active(void)
{
}
#endif /* defined(USE_SANDBOX_GETADDRINFO) */