Tor  0.4.3.0-alpha-dev
dirclient.c
Go to the documentation of this file.
1 /* Copyright (c) 2001-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 /**
7  * @file dirclient.c
8  * @brief Download directory information
9  **/
10 
11 #define DIRCLIENT_PRIVATE
12 
13 #include "core/or/or.h"
14 
15 #include "app/config/config.h"
17 #include "core/mainloop/mainloop.h"
19 #include "core/or/policies.h"
20 #include "feature/client/bridges.h"
32 #include "feature/hs/hs_cache.h"
33 #include "feature/hs/hs_client.h"
34 #include "feature/hs/hs_control.h"
46 #include "feature/relay/selftest.h"
47 #include "feature/rend/rendcache.h"
52 
53 #include "lib/compress/compress.h"
56 #include "lib/encoding/confline.h"
57 #include "lib/err/backtrace.h"
58 
67 
68 /** Maximum size, in bytes, for any directory object that we've downloaded. */
69 #define MAX_DIR_DL_SIZE ((1<<24)-1) /* 16 MB - 1 */
70 
71 /** How far in the future do we allow a directory server to tell us it is
72  * before deciding that one of us has the wrong time? */
73 #define ALLOW_DIRECTORY_TIME_SKEW (30*60)
74 
75 static int body_is_plausible(const char *body, size_t body_len, int purpose);
79  dir_connection_t *conn, int status_code);
80 static void connection_dir_retry_bridges(smartlist_t *descs);
82  int status_code,
83  int router_purpose,
84  int was_extrainfo,
85  int was_descriptor_digests);
86 static void dir_microdesc_download_failed(smartlist_t *failed,
87  int status_code,
88  const char *dir_id);
90  const int direct,
91  const directory_request_t *req);
92 static void connection_dir_close_consensus_fetches(
93  dir_connection_t *except_this_one, const char *resource);
94 
95 /** Return a string describing a given directory connection purpose. */
96 STATIC const char *
98 {
99  switch (purpose)
100  {
102  return "server descriptor upload";
104  return "server vote upload";
106  return "consensus signature upload";
108  return "server descriptor fetch";
110  return "extra-info fetch";
112  return "consensus network-status fetch";
114  return "authority cert fetch";
116  return "status vote fetch";
118  return "consensus signature fetch";
120  return "hidden-service v2 descriptor fetch";
122  return "hidden-service v2 descriptor upload";
124  return "hidden-service descriptor fetch";
126  return "hidden-service descriptor upload";
128  return "microdescriptor fetch";
129  }
130 
131  log_warn(LD_BUG, "Called with unknown purpose %d", purpose);
132  return "(unknown)";
133 }
134 
135 /** Return the requisite directory information types. */
137 dir_fetch_type(int dir_purpose, int router_purpose, const char *resource)
138 {
139  dirinfo_type_t type;
140  switch (dir_purpose) {
142  type = EXTRAINFO_DIRINFO;
143  if (router_purpose == ROUTER_PURPOSE_BRIDGE)
144  type |= BRIDGE_DIRINFO;
145  else
146  type |= V3_DIRINFO;
147  break;
149  if (router_purpose == ROUTER_PURPOSE_BRIDGE)
150  type = BRIDGE_DIRINFO;
151  else
152  type = V3_DIRINFO;
153  break;
157  type = V3_DIRINFO;
158  break;
160  type = V3_DIRINFO;
161  if (resource && !strcmp(resource, "microdesc"))
162  type |= MICRODESC_DIRINFO;
163  break;
165  type = MICRODESC_DIRINFO;
166  break;
167  default:
168  log_warn(LD_BUG, "Unexpected purpose %d", (int)dir_purpose);
169  type = NO_DIRINFO;
170  break;
171  }
172  return type;
173 }
174 
175 /** Return true iff <b>identity_digest</b> is the digest of a router which
176  * says that it caches extrainfos. (If <b>is_authority</b> we always
177  * believe that to be true.) */
178 int
179 router_supports_extrainfo(const char *identity_digest, int is_authority)
180 {
181  const node_t *node = node_get_by_id(identity_digest);
182 
183  if (node && node->ri) {
184  if (node->ri->caches_extra_info)
185  return 1;
186  }
187  if (is_authority) {
188  return 1;
189  }
190  return 0;
191 }
192 
193 /** Return true iff any trusted directory authority has accepted our
194  * server descriptor.
195  *
196  * We consider any authority sufficient because waiting for all of
197  * them means it never happens while any authority is down; we don't
198  * go for something more complex in the middle (like >1/3 or >1/2 or
199  * >=1/2) because that doesn't seem necessary yet.
200  */
201 int
203 {
204  const smartlist_t *servers = router_get_trusted_dir_servers();
205  const or_options_t *options = get_options();
206  SMARTLIST_FOREACH(servers, dir_server_t *, d, {
207  if ((d->type & options->PublishServerDescriptor_) &&
208  d->has_accepted_serverdesc) {
209  return 1;
210  }
211  });
212  return 0;
213 }
214 
215 /** Start a connection to every suitable directory authority, using
216  * connection purpose <b>dir_purpose</b> and uploading <b>payload</b>
217  * (of length <b>payload_len</b>). The dir_purpose should be one of
218  * 'DIR_PURPOSE_UPLOAD_{DIR|VOTE|SIGNATURES}'.
219  *
220  * <b>router_purpose</b> describes the type of descriptor we're
221  * publishing, if we're publishing a descriptor -- e.g. general or bridge.
222  *
223  * <b>type</b> specifies what sort of dir authorities (V3,
224  * BRIDGE, etc) we should upload to.
225  *
226  * If <b>extrainfo_len</b> is nonzero, the first <b>payload_len</b> bytes of
227  * <b>payload</b> hold a router descriptor, and the next <b>extrainfo_len</b>
228  * bytes of <b>payload</b> hold an extra-info document. Upload the descriptor
229  * to all authorities, and the extra-info document to all authorities that
230  * support it.
231  */
232 void
233 directory_post_to_dirservers(uint8_t dir_purpose, uint8_t router_purpose,
234  dirinfo_type_t type,
235  const char *payload,
236  size_t payload_len, size_t extrainfo_len)
237 {
238  const or_options_t *options = get_options();
239  dir_indirection_t indirection;
240  const smartlist_t *dirservers = router_get_trusted_dir_servers();
241  int found = 0;
242  const int exclude_self = (dir_purpose == DIR_PURPOSE_UPLOAD_VOTE ||
243  dir_purpose == DIR_PURPOSE_UPLOAD_SIGNATURES);
244  tor_assert(dirservers);
245  /* This tries dirservers which we believe to be down, but ultimately, that's
246  * harmless, and we may as well err on the side of getting things uploaded.
247  */
248  SMARTLIST_FOREACH_BEGIN(dirservers, dir_server_t *, ds) {
249  routerstatus_t *rs = &(ds->fake_status);
250  size_t upload_len = payload_len;
251 
252  if ((type & ds->type) == 0)
253  continue;
254 
255  if (exclude_self && router_digest_is_me(ds->digest)) {
256  /* we don't upload to ourselves, but at least there's now at least
257  * one authority of this type that has what we wanted to upload. */
258  found = 1;
259  continue;
260  }
261 
262  if (options->StrictNodes &&
263  routerset_contains_routerstatus(options->ExcludeNodes, rs, -1)) {
264  log_warn(LD_DIR, "Wanted to contact authority '%s' for %s, but "
265  "it's in our ExcludedNodes list and StrictNodes is set. "
266  "Skipping.",
267  ds->nickname,
268  dir_conn_purpose_to_string(dir_purpose));
269  continue;
270  }
271 
272  found = 1; /* at least one authority of this type was listed */
273  if (dir_purpose == DIR_PURPOSE_UPLOAD_DIR)
274  ds->has_accepted_serverdesc = 0;
275 
276  if (extrainfo_len && router_supports_extrainfo(ds->digest, 1)) {
277  upload_len += extrainfo_len;
278  log_info(LD_DIR, "Uploading an extrainfo too (length %d)",
279  (int) extrainfo_len);
280  }
281  if (purpose_needs_anonymity(dir_purpose, router_purpose, NULL)) {
282  indirection = DIRIND_ANONYMOUS;
283  } else if (!fascist_firewall_allows_dir_server(ds,
284  FIREWALL_DIR_CONNECTION,
285  0)) {
286  if (fascist_firewall_allows_dir_server(ds, FIREWALL_OR_CONNECTION, 0))
287  indirection = DIRIND_ONEHOP;
288  else
289  indirection = DIRIND_ANONYMOUS;
290  } else {
291  indirection = DIRIND_DIRECT_CONN;
292  }
293 
294  directory_request_t *req = directory_request_new(dir_purpose);
296  directory_request_set_router_purpose(req, router_purpose);
297  directory_request_set_indirection(req, indirection);
298  directory_request_set_payload(req, payload, upload_len);
300  directory_request_free(req);
301  } SMARTLIST_FOREACH_END(ds);
302  if (!found) {
303  char *s = authdir_type_to_string(type);
304  log_warn(LD_DIR, "Publishing server descriptor to directory authorities "
305  "of type '%s', but no authorities of that type listed!", s);
306  tor_free(s);
307  }
308 }
309 
310 /** Return true iff, according to the values in <b>options</b>, we should be
311  * using directory guards for direct downloads of directory information. */
312 STATIC int
314 {
315  /* Public (non-bridge) servers never use directory guards. */
316  if (public_server_mode(options))
317  return 0;
318  /* If guards are disabled, we can't use directory guards.
319  */
320  if (!options->UseEntryGuards)
321  return 0;
322  /* If we're configured to fetch directory info aggressively or of a
323  * nonstandard type, don't use directory guards. */
324  if (options->DownloadExtraInfo || options->FetchDirInfoEarly ||
325  options->FetchDirInfoExtraEarly || options->FetchUselessDescriptors)
326  return 0;
327  return 1;
328 }
329 
330 /** Pick an unconstrained directory server from among our guards, the latest
331  * networkstatus, or the fallback dirservers, for use in downloading
332  * information of type <b>type</b>, and return its routerstatus. */
333 static const routerstatus_t *
335  uint8_t dir_purpose,
336  circuit_guard_state_t **guard_state_out)
337 {
338  const routerstatus_t *rs = NULL;
339  const or_options_t *options = get_options();
340 
341  if (options->UseBridges)
342  log_warn(LD_BUG, "Called when we have UseBridges set.");
343 
344  if (should_use_directory_guards(options)) {
345  const node_t *node = guards_choose_dirguard(dir_purpose, guard_state_out);
346  if (node)
347  rs = node->rs;
348  } else {
349  /* anybody with a non-zero dirport will do */
350  rs = router_pick_directory_server(type, pds_flags);
351  }
352  if (!rs) {
353  log_info(LD_DIR, "No router found for %s; falling back to "
354  "dirserver list.", dir_conn_purpose_to_string(dir_purpose));
355  rs = router_pick_fallback_dirserver(type, pds_flags);
356  }
357 
358  return rs;
359 }
360 
361 /**
362  * Set the extra fields in <b>req</b> that are used when requesting a
363  * consensus of type <b>resource</b>.
364  *
365  * Right now, these fields are if-modified-since and x-or-diff-from-consensus.
366  */
367 static void
369  const char *resource)
370 {
371  time_t if_modified_since = 0;
372  uint8_t or_diff_from[DIGEST256_LEN];
373  int or_diff_from_is_set = 0;
374 
375  /* DEFAULT_IF_MODIFIED_SINCE_DELAY is 1/20 of the default consensus
376  * period of 1 hour.
377  */
378  const int DEFAULT_IF_MODIFIED_SINCE_DELAY = 180;
379  const int32_t DEFAULT_TRY_DIFF_FOR_CONSENSUS_NEWER = 72;
380  const int32_t MIN_TRY_DIFF_FOR_CONSENSUS_NEWER = 0;
381  const int32_t MAX_TRY_DIFF_FOR_CONSENSUS_NEWER = 8192;
382  const char TRY_DIFF_FOR_CONSENSUS_NEWER_NAME[] =
383  "try-diff-for-consensus-newer-than";
384 
385  int flav = FLAV_NS;
386  if (resource)
387  flav = networkstatus_parse_flavor_name(resource);
388 
389  int32_t max_age_for_diff = 3600 *
391  TRY_DIFF_FOR_CONSENSUS_NEWER_NAME,
392  DEFAULT_TRY_DIFF_FOR_CONSENSUS_NEWER,
393  MIN_TRY_DIFF_FOR_CONSENSUS_NEWER,
394  MAX_TRY_DIFF_FOR_CONSENSUS_NEWER);
395 
396  if (flav != -1) {
397  /* IF we have a parsed consensus of this type, we can do an
398  * if-modified-time based on it. */
399  networkstatus_t *v;
401  if (v) {
402  /* In networks with particularly short V3AuthVotingIntervals,
403  * ask for the consensus if it's been modified since half the
404  * V3AuthVotingInterval of the most recent consensus. */
405  time_t ims_delay = DEFAULT_IF_MODIFIED_SINCE_DELAY;
406  if (v->fresh_until > v->valid_after
407  && ims_delay > (v->fresh_until - v->valid_after)/2) {
408  ims_delay = (v->fresh_until - v->valid_after)/2;
409  }
410  if_modified_since = v->valid_after + ims_delay;
411  if (v->valid_after >= approx_time() - max_age_for_diff) {
412  memcpy(or_diff_from, v->digest_sha3_as_signed, DIGEST256_LEN);
413  or_diff_from_is_set = 1;
414  }
415  }
416  } else {
417  /* Otherwise it might be a consensus we don't parse, but which we
418  * do cache. Look at the cached copy, perhaps. */
419  cached_dir_t *cd = dirserv_get_consensus(resource);
420  /* We have no method of determining the voting interval from an
421  * unparsed consensus, so we use the default. */
422  if (cd) {
423  if_modified_since = cd->published + DEFAULT_IF_MODIFIED_SINCE_DELAY;
424  if (cd->published >= approx_time() - max_age_for_diff) {
425  memcpy(or_diff_from, cd->digest_sha3_as_signed, DIGEST256_LEN);
426  or_diff_from_is_set = 1;
427  }
428  }
429  }
430 
431  if (if_modified_since > 0)
432  directory_request_set_if_modified_since(req, if_modified_since);
433  if (or_diff_from_is_set) {
434  char hex[HEX_DIGEST256_LEN + 1];
435  base16_encode(hex, sizeof(hex),
436  (const char*)or_diff_from, sizeof(or_diff_from));
437  directory_request_add_header(req, X_OR_DIFF_FROM_CONSENSUS_HEADER, hex);
438  }
439 }
440 /** Start a connection to a random running directory server, using
441  * connection purpose <b>dir_purpose</b>, intending to fetch descriptors
442  * of purpose <b>router_purpose</b>, and requesting <b>resource</b>.
443  * Use <b>pds_flags</b> as arguments to router_pick_directory_server()
444  * or router_pick_trusteddirserver().
445  */
446 MOCK_IMPL(void,
448  uint8_t dir_purpose,
449  uint8_t router_purpose,
450  const char *resource,
451  int pds_flags,
452  download_want_authority_t want_authority))
453 {
454  const routerstatus_t *rs = NULL;
455  const or_options_t *options = get_options();
456  int prefer_authority = (directory_fetches_from_authorities(options)
457  || want_authority == DL_WANT_AUTHORITY);
458  int require_authority = 0;
459  int get_via_tor = purpose_needs_anonymity(dir_purpose, router_purpose,
460  resource);
461  dirinfo_type_t type = dir_fetch_type(dir_purpose, router_purpose, resource);
462 
463  if (type == NO_DIRINFO)
464  return;
465 
466  if (!options->FetchServerDescriptors)
467  return;
468 
469  circuit_guard_state_t *guard_state = NULL;
470  if (!get_via_tor) {
471  if (options->UseBridges && !(type & BRIDGE_DIRINFO)) {
472  /* We want to ask a running bridge for which we have a descriptor.
473  *
474  * When we ask choose_random_entry() for a bridge, we specify what
475  * sort of dir fetch we'll be doing, so it won't return a bridge
476  * that can't answer our question.
477  */
478  const node_t *node = guards_choose_dirguard(dir_purpose, &guard_state);
479  if (node && node->ri) {
480  /* every bridge has a routerinfo. */
481  routerinfo_t *ri = node->ri;
482  /* clients always make OR connections to bridges */
483  tor_addr_port_t or_ap;
484  directory_request_t *req = directory_request_new(dir_purpose);
485  /* we are willing to use a non-preferred address if we need to */
486  fascist_firewall_choose_address_node(node, FIREWALL_OR_CONNECTION, 0,
487  &or_ap);
490  ri->cache_info.identity_digest);
491  directory_request_set_router_purpose(req, router_purpose);
492  directory_request_set_resource(req, resource);
493  if (dir_purpose == DIR_PURPOSE_FETCH_CONSENSUS)
495  directory_request_set_guard_state(req, guard_state);
497  directory_request_free(req);
498  } else {
499  if (guard_state) {
500  entry_guard_cancel(&guard_state);
501  }
502  log_notice(LD_DIR, "Ignoring directory request, since no bridge "
503  "nodes are available yet.");
504  }
505 
506  return;
507  } else {
508  if (prefer_authority || (type & BRIDGE_DIRINFO)) {
509  /* only ask authdirservers, and don't ask myself */
510  rs = router_pick_trusteddirserver(type, pds_flags);
511  if (rs == NULL && (pds_flags & (PDS_NO_EXISTING_SERVERDESC_FETCH|
513  /* We don't want to fetch from any authorities that we're currently
514  * fetching server descriptors from, and we got no match. Did we
515  * get no match because all the authorities have connections
516  * fetching server descriptors (in which case we should just
517  * return,) or because all the authorities are down or on fire or
518  * unreachable or something (in which case we should go on with
519  * our fallback code)? */
520  pds_flags &= ~(PDS_NO_EXISTING_SERVERDESC_FETCH|
522  rs = router_pick_trusteddirserver(type, pds_flags);
523  if (rs) {
524  log_debug(LD_DIR, "Deferring serverdesc fetch: all authorities "
525  "are in use.");
526  return;
527  }
528  }
529  if (rs == NULL && require_authority) {
530  log_info(LD_DIR, "No authorities were available for %s: will try "
531  "later.", dir_conn_purpose_to_string(dir_purpose));
532  return;
533  }
534  }
535  if (!rs && !(type & BRIDGE_DIRINFO)) {
536  rs = directory_pick_generic_dirserver(type, pds_flags,
537  dir_purpose,
538  &guard_state);
539  if (!rs)
540  get_via_tor = 1; /* last resort: try routing it via Tor */
541  }
542  }
543  }
544 
545  if (get_via_tor) {
546  /* Never use fascistfirewall; we're going via Tor. */
547  pds_flags |= PDS_IGNORE_FASCISTFIREWALL;
548  rs = router_pick_directory_server(type, pds_flags);
549  }
550 
551  /* If we have any hope of building an indirect conn, we know some router
552  * descriptors. If (rs==NULL), we can't build circuits anyway, so
553  * there's no point in falling back to the authorities in this case. */
554  if (rs) {
555  const dir_indirection_t indirection =
556  get_via_tor ? DIRIND_ANONYMOUS : DIRIND_ONEHOP;
557  directory_request_t *req = directory_request_new(dir_purpose);
559  directory_request_set_router_purpose(req, router_purpose);
560  directory_request_set_indirection(req, indirection);
561  directory_request_set_resource(req, resource);
562  if (dir_purpose == DIR_PURPOSE_FETCH_CONSENSUS)
564  if (guard_state)
565  directory_request_set_guard_state(req, guard_state);
567  directory_request_free(req);
568  } else {
569  log_notice(LD_DIR,
570  "While fetching directory info, "
571  "no running dirservers known. Will try again later. "
572  "(purpose %d)", dir_purpose);
573  if (!purpose_needs_anonymity(dir_purpose, router_purpose, resource)) {
574  /* remember we tried them all and failed. */
575  directory_all_unreachable(time(NULL));
576  }
577  }
578 }
579 
580 /** As directory_get_from_dirserver, but initiates a request to <i>every</i>
581  * directory authority other than ourself. Only for use by authorities when
582  * searching for missing information while voting. */
583 void
585  uint8_t router_purpose,
586  const char *resource)
587 {
588  tor_assert(dir_purpose == DIR_PURPOSE_FETCH_STATUS_VOTE ||
590 
591  SMARTLIST_FOREACH_BEGIN(router_get_trusted_dir_servers(),
592  dir_server_t *, ds) {
593  if (router_digest_is_me(ds->digest))
594  continue;
595  if (!(ds->type & V3_DIRINFO))
596  continue;
597  const routerstatus_t *rs = &ds->fake_status;
598  directory_request_t *req = directory_request_new(dir_purpose);
600  directory_request_set_router_purpose(req, router_purpose);
601  directory_request_set_resource(req, resource);
603  directory_request_free(req);
604  } SMARTLIST_FOREACH_END(ds);
605 }
606 
607 /** Return true iff <b>ind</b> requires a multihop circuit. */
608 static int
610 {
611  return ind == DIRIND_ANON_DIRPORT || ind == DIRIND_ANONYMOUS;
612 }
613 
614 /* Choose reachable OR and Dir addresses and ports from status, copying them
615  * into use_or_ap and use_dir_ap. If indirection is anonymous, then we're
616  * connecting via another relay, so choose the primary IPv4 address and ports.
617  *
618  * status should have at least one reachable address, if we can't choose a
619  * reachable address, warn and return -1. Otherwise, return 0.
620  */
621 static int
622 directory_choose_address_routerstatus(const routerstatus_t *status,
623  dir_indirection_t indirection,
624  tor_addr_port_t *use_or_ap,
625  tor_addr_port_t *use_dir_ap)
626 {
627  tor_assert(status != NULL);
628  tor_assert(use_or_ap != NULL);
629  tor_assert(use_dir_ap != NULL);
630 
631  const or_options_t *options = get_options();
632  int have_or = 0, have_dir = 0;
633 
634  /* We expect status to have at least one reachable address if we're
635  * connecting to it directly.
636  *
637  * Therefore, we can simply use the other address if the one we want isn't
638  * allowed by the firewall.
639  *
640  * (When Tor uploads and downloads a hidden service descriptor, it uses
641  * DIRIND_ANONYMOUS. Even Single Onion Servers (NYI) use DIRIND_ANONYMOUS,
642  * to avoid HSDirs denying service by rejecting descriptors.)
643  */
644 
645  /* Initialise the OR / Dir addresses */
646  tor_addr_make_null(&use_or_ap->addr, AF_UNSPEC);
647  use_or_ap->port = 0;
648  tor_addr_make_null(&use_dir_ap->addr, AF_UNSPEC);
649  use_dir_ap->port = 0;
650 
651  /* ORPort connections */
652  if (indirection == DIRIND_ANONYMOUS) {
653  if (status->addr) {
654  /* Since we're going to build a 3-hop circuit and ask the 2nd relay
655  * to extend to this address, always use the primary (IPv4) OR address */
656  tor_addr_from_ipv4h(&use_or_ap->addr, status->addr);
657  use_or_ap->port = status->or_port;
658  have_or = 1;
659  }
660  } else if (indirection == DIRIND_ONEHOP) {
661  /* We use an IPv6 address if we have one and we prefer it.
662  * Use the preferred address and port if they are reachable, otherwise,
663  * use the alternate address and port (if any).
664  */
665  fascist_firewall_choose_address_rs(status, FIREWALL_OR_CONNECTION, 0,
666  use_or_ap);
667  have_or = tor_addr_port_is_valid_ap(use_or_ap, 0);
668  }
669 
670  /* DirPort connections
671  * DIRIND_ONEHOP uses ORPort, but may fall back to the DirPort on relays */
672  if (indirection == DIRIND_DIRECT_CONN ||
673  indirection == DIRIND_ANON_DIRPORT ||
674  (indirection == DIRIND_ONEHOP
675  && !directory_must_use_begindir(options))) {
676  fascist_firewall_choose_address_rs(status, FIREWALL_DIR_CONNECTION, 0,
677  use_dir_ap);
678  have_dir = tor_addr_port_is_valid_ap(use_dir_ap, 0);
679  }
680 
681  /* We rejected all addresses in the relay's status. This means we can't
682  * connect to it. */
683  if (!have_or && !have_dir) {
684  static int logged_backtrace = 0;
685  log_info(LD_BUG, "Rejected all OR and Dir addresses from %s when "
686  "launching an outgoing directory connection to: IPv4 %s OR %d "
687  "Dir %d IPv6 %s OR %d Dir %d", routerstatus_describe(status),
688  fmt_addr32(status->addr), status->or_port,
689  status->dir_port, fmt_addr(&status->ipv6_addr),
690  status->ipv6_orport, status->dir_port);
691  if (!logged_backtrace) {
692  log_backtrace(LOG_INFO, LD_BUG, "Addresses came from");
693  logged_backtrace = 1;
694  }
695  return -1;
696  }
697 
698  return 0;
699 }
700 
701 /** Return true iff <b>conn</b> is the client side of a directory connection
702  * we launched to ourself in order to determine the reachability of our
703  * dir_port. */
704 static int
706 {
707  if (conn->requested_resource &&
708  !strcmpstart(conn->requested_resource,"authority")) {
710  if (me &&
712  tor_addr_eq_ipv4h(&conn->base_.addr, me->addr) && /*XXXX prop 118*/
713  me->dir_port == conn->base_.port)
714  return 1;
715  }
716  return 0;
717 }
718 
719 /** Called when we are unable to complete the client's request to a directory
720  * server due to a network error: Mark the router as down and try again if
721  * possible.
722  */
723 void
725 {
726  if (conn->guard_state) {
727  /* We haven't seen a success on this guard state, so consider it to have
728  * failed. */
730  }
732  return; /* this was a test fetch. don't retry. */
733  }
735  router_set_status(conn->identity_digest, 0); /* don't try this one again */
736  if (conn->base_.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
737  conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO) {
738  log_info(LD_DIR, "Giving up on serverdesc/extrainfo fetch from "
739  "directory server at '%s'; retrying",
740  conn->base_.address);
744  } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_CONSENSUS) {
745  if (conn->requested_resource)
747  } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_CERTIFICATE) {
748  log_info(LD_DIR, "Giving up on certificate fetch from directory server "
749  "at '%s'; retrying",
750  conn->base_.address);
752  } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_DETACHED_SIGNATURES) {
753  log_info(LD_DIR, "Giving up downloading detached signatures from '%s'",
754  conn->base_.address);
755  } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_STATUS_VOTE) {
756  log_info(LD_DIR, "Giving up downloading votes from '%s'",
757  conn->base_.address);
758  } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_MICRODESC) {
759  log_info(LD_DIR, "Giving up on downloading microdescriptors from "
760  "directory server at '%s'; will retry", conn->base_.address);
762  }
763 }
764 
765 /** Helper: Attempt to fetch directly the descriptors of each bridge
766  * listed in <b>failed</b>.
767  */
768 static void
770 {
771  char digest[DIGEST_LEN];
772  SMARTLIST_FOREACH(descs, const char *, cp,
773  {
774  if (base16_decode(digest, DIGEST_LEN, cp, strlen(cp)) != DIGEST_LEN) {
775  log_warn(LD_BUG, "Malformed fingerprint in list: %s",
776  escaped(cp));
777  continue;
778  }
780  });
781 }
782 
783 /** Called when an attempt to download one or more router descriptors
784  * or extra-info documents on connection <b>conn</b> failed.
785  */
786 static void
788 {
789  /* No need to increment the failure count for routerdescs, since
790  * it's not their fault. */
791 
792  /* No need to relaunch descriptor downloads here: we already do it
793  * every 10 or 60 seconds (FOO_DESCRIPTOR_RETRY_INTERVAL) in main.c. */
795  conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO ||
796  conn->base_.purpose == DIR_PURPOSE_FETCH_MICRODESC);
797 
798  (void) conn;
799 }
800 
801 /** Called when an attempt to download a bridge's routerdesc from
802  * one of the authorities failed due to a network error. If
803  * possible attempt to download descriptors from the bridge directly.
804  */
805 static void
807 {
808  smartlist_t *which = NULL;
809 
810  /* Requests for bridge descriptors are in the form 'fp/', so ignore
811  anything else. */
812  if (!conn->requested_resource || strcmpstart(conn->requested_resource,"fp/"))
813  return;
814 
815  which = smartlist_new();
817  + strlen("fp/"),
818  which, NULL, 0);
819 
821  if (smartlist_len(which)) {
823  SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
824  }
825  smartlist_free(which);
826 }
827 
828 /** Called when an attempt to fetch a certificate fails. */
829 static void
831 {
832  const char *fp_pfx = "fp/";
833  const char *fpsk_pfx = "fp-sk/";
834  smartlist_t *failed;
836 
837  if (!conn->requested_resource)
838  return;
839  failed = smartlist_new();
840  /*
841  * We have two cases download by fingerprint (resource starts
842  * with "fp/") or download by fingerprint/signing key pair
843  * (resource starts with "fp-sk/").
844  */
845  if (!strcmpstart(conn->requested_resource, fp_pfx)) {
846  /* Download by fingerprint case */
848  strlen(fp_pfx),
849  failed, NULL, DSR_HEX);
850  SMARTLIST_FOREACH_BEGIN(failed, char *, cp) {
851  /* Null signing key digest indicates download by fp only */
852  authority_cert_dl_failed(cp, NULL, status);
853  tor_free(cp);
854  } SMARTLIST_FOREACH_END(cp);
855  } else if (!strcmpstart(conn->requested_resource, fpsk_pfx)) {
856  /* Download by (fp,sk) pairs */
858  strlen(fpsk_pfx), failed);
859  SMARTLIST_FOREACH_BEGIN(failed, fp_pair_t *, cp) {
860  authority_cert_dl_failed(cp->first, cp->second, status);
861  tor_free(cp);
862  } SMARTLIST_FOREACH_END(cp);
863  } else {
864  log_warn(LD_DIR,
865  "Don't know what to do with failure for cert fetch %s",
866  conn->requested_resource);
867  }
868 
869  smartlist_free(failed);
870 
871  update_certificate_downloads(time(NULL));
872 }
873 
874 /* Should this tor instance only use begindir for all its directory requests?
875  */
876 int
877 directory_must_use_begindir(const or_options_t *options)
878 {
879  /* Clients, onion services, and bridges must use begindir,
880  * relays and authorities do not have to */
881  return !public_server_mode(options);
882 }
883 
884 /** Evaluate the situation and decide if we should use an encrypted
885  * "begindir-style" connection for this directory request.
886  * 0) If there is no DirPort, yes.
887  * 1) If or_port is 0, or it's a direct conn and or_port is firewalled
888  * or we're a dir mirror, no.
889  * 2) If we prefer to avoid begindir conns, and we're not fetching or
890  * publishing a bridge relay descriptor, no.
891  * 3) Else yes.
892  * If returning 0, return in *reason why we can't use begindir.
893  * reason must not be NULL.
894  */
895 static int
897  const directory_request_t *req,
898  const char **reason)
899 {
900  const tor_addr_t *or_addr = &req->or_addr_port.addr;
901  //const tor_addr_t *dir_addr = &req->dir_addr_port.addr;
902  const int or_port = req->or_addr_port.port;
903  const int dir_port = req->dir_addr_port.port;
904 
905  const dir_indirection_t indirection = req->indirection;
906 
907  tor_assert(reason);
908  *reason = NULL;
909 
910  /* Reasons why we must use begindir */
911  if (!dir_port) {
912  *reason = "(using begindir - directory with no DirPort)";
913  return 1; /* We don't know a DirPort -- must begindir. */
914  }
915  /* Reasons why we can't possibly use begindir */
916  if (!or_port) {
917  *reason = "directory with unknown ORPort";
918  return 0; /* We don't know an ORPort -- no chance. */
919  }
920  if (indirection == DIRIND_DIRECT_CONN ||
921  indirection == DIRIND_ANON_DIRPORT) {
922  *reason = "DirPort connection";
923  return 0;
924  }
925  if (indirection == DIRIND_ONEHOP) {
926  /* We're firewalled and want a direct OR connection */
927  if (!fascist_firewall_allows_address_addr(or_addr, or_port,
928  FIREWALL_OR_CONNECTION, 0, 0)) {
929  *reason = "ORPort not reachable";
930  return 0;
931  }
932  }
933  /* Reasons why we want to avoid using begindir */
934  if (indirection == DIRIND_ONEHOP) {
935  if (!directory_must_use_begindir(options)) {
936  *reason = "in relay mode";
937  return 0;
938  }
939  }
940  /* DIRIND_ONEHOP on a client, or DIRIND_ANONYMOUS
941  */
942  *reason = "(using begindir)";
943  return 1;
944 }
945 
946 /**
947  * Create and return a new directory_request_t with purpose
948  * <b>dir_purpose</b>.
949  */
951 directory_request_new(uint8_t dir_purpose)
952 {
953  tor_assert(dir_purpose >= DIR_PURPOSE_MIN_);
954  tor_assert(dir_purpose <= DIR_PURPOSE_MAX_);
955  tor_assert(dir_purpose != DIR_PURPOSE_SERVER);
958 
959  directory_request_t *result = tor_malloc_zero(sizeof(*result));
960  tor_addr_make_null(&result->or_addr_port.addr, AF_INET);
961  result->or_addr_port.port = 0;
962  tor_addr_make_null(&result->dir_addr_port.addr, AF_INET);
963  result->dir_addr_port.port = 0;
964  result->dir_purpose = dir_purpose;
965  result->router_purpose = ROUTER_PURPOSE_GENERAL;
966  result->indirection = DIRIND_ONEHOP;
967  return result;
968 }
969 /**
970  * Release all resources held by <b>req</b>.
971  */
972 void
974 {
975  if (req == NULL)
976  return;
977  config_free_lines(req->additional_headers);
978  tor_free(req);
979 }
980 /**
981  * Set the address and OR port to use for this directory request. If there is
982  * no OR port, we'll have to connect over the dirport. (If there are both,
983  * the indirection setting determines which to use.)
984  */
985 void
987  const tor_addr_port_t *p)
988 {
989  memcpy(&req->or_addr_port, p, sizeof(*p));
990 }
991 /**
992  * Set the address and dirport to use for this directory request. If there
993  * is no dirport, we'll have to connect over the OR port. (If there are both,
994  * the indirection setting determines which to use.)
995  */
996 void
998  const tor_addr_port_t *p)
999 {
1000  memcpy(&req->dir_addr_port, p, sizeof(*p));
1001 }
1002 /**
1003  * Set the RSA identity digest of the directory to use for this directory
1004  * request.
1005  */
1006 void
1008  const char *digest)
1009 {
1010  memcpy(req->digest, digest, DIGEST_LEN);
1011 }
1012 /**
1013  * Set the router purpose associated with uploaded and downloaded router
1014  * descriptors and extrainfo documents in this directory request. The purpose
1015  * must be one of ROUTER_PURPOSE_GENERAL (the default) or
1016  * ROUTER_PURPOSE_BRIDGE.
1017  */
1018 void
1020  uint8_t router_purpose)
1021 {
1022  tor_assert(router_purpose == ROUTER_PURPOSE_GENERAL ||
1023  router_purpose == ROUTER_PURPOSE_BRIDGE);
1024  // assert that it actually makes sense to set this purpose, given
1025  // the dir_purpose.
1026  req->router_purpose = router_purpose;
1027 }
1028 /**
1029  * Set the indirection to be used for the directory request. The indirection
1030  * parameter configures whether to connect to a DirPort or ORPort, and whether
1031  * to anonymize the connection. DIRIND_ONEHOP (use ORPort, don't anonymize)
1032  * is the default. See dir_indirection_t for more information.
1033  */
1034 void
1036  dir_indirection_t indirection)
1037 {
1038  req->indirection = indirection;
1039 }
1040 
1041 /**
1042  * Set a pointer to the resource to request from a directory. Different
1043  * request types use resources to indicate different components of their URL.
1044  * Note that only an alias to <b>resource</b> is stored, so the
1045  * <b>resource</b> must outlive the request.
1046  */
1047 void
1049  const char *resource)
1050 {
1051  req->resource = resource;
1052 }
1053 /**
1054  * Set a pointer to the payload to include with this directory request, along
1055  * with its length. Note that only an alias to <b>payload</b> is stored, so
1056  * the <b>payload</b> must outlive the request.
1057  */
1058 void
1060  const char *payload,
1061  size_t payload_len)
1062 {
1063  tor_assert(DIR_PURPOSE_IS_UPLOAD(req->dir_purpose));
1064 
1065  req->payload = payload;
1066  req->payload_len = payload_len;
1067 }
1068 /**
1069  * Set an if-modified-since date to send along with the request. The
1070  * default is 0 (meaning, send no if-modified-since header).
1071  */
1072 void
1074  time_t if_modified_since)
1075 {
1076  req->if_modified_since = if_modified_since;
1077 }
1078 
1079 /** Include a header of name <b>key</b> with content <b>val</b> in the
1080  * request. Neither may include newlines or other odd characters. Their
1081  * ordering is not currently guaranteed.
1082  *
1083  * Note that, as elsewhere in this module, header keys include a trailing
1084  * colon and space.
1085  */
1086 void
1088  const char *key,
1089  const char *val)
1090 {
1091  config_line_prepend(&req->additional_headers, key, val);
1092 }
1093 /**
1094  * Set an object containing HS data to be associated with this request. Note
1095  * that only an alias to <b>query</b> is stored, so the <b>query</b> object
1096  * must outlive the request.
1097  */
1098 void
1100  const rend_data_t *query)
1101 {
1102  if (query) {
1103  tor_assert(req->dir_purpose == DIR_PURPOSE_FETCH_RENDDESC_V2 ||
1104  req->dir_purpose == DIR_PURPOSE_UPLOAD_RENDDESC_V2);
1105  }
1106  req->rend_query = query;
1107 }
1108 /**
1109  * Set an object containing HS connection identifier to be associated with
1110  * this request. Note that only an alias to <b>ident</b> is stored, so the
1111  * <b>ident</b> object must outlive the request.
1112  */
1113 void
1115  const hs_ident_dir_conn_t *ident)
1116 {
1117  if (ident) {
1118  tor_assert(req->dir_purpose == DIR_PURPOSE_UPLOAD_HSDESC);
1119  }
1120  req->hs_ident = ident;
1121 }
1122 /**
1123  * Set an object containing HS connection identifier to be associated with
1124  * this fetch request. Note that only an alias to <b>ident</b> is stored, so
1125  * the <b>ident</b> object must outlive the request.
1126  */
1127 void
1129  const hs_ident_dir_conn_t *ident)
1130 {
1131  if (ident) {
1132  tor_assert(req->dir_purpose == DIR_PURPOSE_FETCH_HSDESC);
1133  }
1134  req->hs_ident = ident;
1135 }
1136 /** Set a static circuit_guard_state_t object to affliate with the request in
1137  * <b>req</b>. This object will receive notification when the attempt to
1138  * connect to the guard either succeeds or fails. */
1139 void
1141  circuit_guard_state_t *state)
1142 {
1143  req->guard_state = state;
1144 }
1145 
1146 /**
1147  * Internal: Return true if any information for contacting the directory in
1148  * <b>req</b> has been set, other than by the routerstatus. */
1149 static int
1151 {
1152  /* We only check for ports here, since we don't use an addr unless the port
1153  * is set */
1154  return (req->or_addr_port.port ||
1155  req->dir_addr_port.port ||
1156  ! tor_digest_is_zero(req->digest));
1157 }
1158 
1159 /**
1160  * Set the routerstatus to use for the directory associated with this
1161  * request. If this option is set, then no other function to set the
1162  * directory's address or identity should be called.
1163  */
1164 void
1166  const routerstatus_t *status)
1167 {
1168  req->routerstatus = status;
1169 }
1170 /**
1171  * Helper: update the addresses, ports, and identities in <b>req</b>
1172  * from the routerstatus object in <b>req</b>. Return 0 on success.
1173  * On failure, warn and return -1.
1174  */
1175 static int
1177 
1178 {
1179  const routerstatus_t *status = req->routerstatus;
1180  if (BUG(status == NULL))
1181  return -1;
1182  const or_options_t *options = get_options();
1183  const node_t *node;
1184  tor_addr_port_t use_or_ap, use_dir_ap;
1185  const int anonymized_connection = dirind_is_anon(req->indirection);
1186 
1187  tor_assert(status != NULL);
1188 
1189  node = node_get_by_id(status->identity_digest);
1190 
1191  /* XXX The below check is wrong: !node means it's not in the consensus,
1192  * but we haven't checked if we have a descriptor for it -- and also,
1193  * we only care about the descriptor if it's a begindir-style anonymized
1194  * connection. */
1195  if (!node && anonymized_connection) {
1196  log_info(LD_DIR, "Not sending anonymized request to directory '%s'; we "
1197  "don't have its router descriptor.",
1198  routerstatus_describe(status));
1199  return -1;
1200  }
1201 
1202  if (options->ExcludeNodes && options->StrictNodes &&
1203  routerset_contains_routerstatus(options->ExcludeNodes, status, -1)) {
1204  log_warn(LD_DIR, "Wanted to contact directory mirror %s for %s, but "
1205  "it's in our ExcludedNodes list and StrictNodes is set. "
1206  "Skipping. This choice might make your Tor not work.",
1207  routerstatus_describe(status),
1208  dir_conn_purpose_to_string(req->dir_purpose));
1209  return -1;
1210  }
1211 
1212  /* At this point, if we are a client making a direct connection to a
1213  * directory server, we have selected a server that has at least one address
1214  * allowed by ClientUseIPv4/6 and Reachable{"",OR,Dir}Addresses. This
1215  * selection uses the preference in ClientPreferIPv6{OR,Dir}Port, if
1216  * possible. (If UseBridges is set, clients always use IPv6, and prefer it
1217  * by default.)
1218  *
1219  * Now choose an address that we can use to connect to the directory server.
1220  */
1221  if (directory_choose_address_routerstatus(status,
1222  req->indirection, &use_or_ap,
1223  &use_dir_ap) < 0) {
1224  return -1;
1225  }
1226 
1227  directory_request_set_or_addr_port(req, &use_or_ap);
1228  directory_request_set_dir_addr_port(req, &use_dir_ap);
1229  directory_request_set_directory_id_digest(req, status->identity_digest);
1230  return 0;
1231 }
1232 
1233 /**
1234  * Launch the provided directory request, configured in <b>request</b>.
1235  * After this function is called, you can free <b>request</b>.
1236  */
1237 MOCK_IMPL(void,
1239 {
1240  tor_assert(request);
1241  if (request->routerstatus) {
1242  tor_assert_nonfatal(
1245  return;
1246  }
1247  }
1248 
1249  const tor_addr_port_t *or_addr_port = &request->or_addr_port;
1250  const tor_addr_port_t *dir_addr_port = &request->dir_addr_port;
1251  const char *digest = request->digest;
1252  const uint8_t dir_purpose = request->dir_purpose;
1253  const uint8_t router_purpose = request->router_purpose;
1254  const dir_indirection_t indirection = request->indirection;
1255  const char *resource = request->resource;
1256  const rend_data_t *rend_query = request->rend_query;
1257  const hs_ident_dir_conn_t *hs_ident = request->hs_ident;
1258  circuit_guard_state_t *guard_state = request->guard_state;
1259 
1260  tor_assert(or_addr_port->port || dir_addr_port->port);
1261  tor_assert(digest);
1262 
1263  dir_connection_t *conn;
1264  const or_options_t *options = get_options();
1265  int socket_error = 0;
1266  const char *begindir_reason = NULL;
1267  /* Should the connection be to a relay's OR port (and inside that we will
1268  * send our directory request)? */
1269  const int use_begindir =
1270  directory_command_should_use_begindir(options, request, &begindir_reason);
1271 
1272  /* Will the connection go via a three-hop Tor circuit? Note that this
1273  * is separate from whether it will use_begindir. */
1274  const int anonymized_connection = dirind_is_anon(indirection);
1275 
1276  /* What is the address we want to make the directory request to? If
1277  * we're making a begindir request this is the ORPort of the relay
1278  * we're contacting; if not a begindir request, this is its DirPort.
1279  * Note that if anonymized_connection is true, we won't be initiating
1280  * a connection directly to this address. */
1281  tor_addr_t addr;
1282  tor_addr_copy(&addr, &(use_begindir ? or_addr_port : dir_addr_port)->addr);
1283  uint16_t port = (use_begindir ? or_addr_port : dir_addr_port)->port;
1284 
1285  log_debug(LD_DIR, "anonymized %d, use_begindir %d.",
1286  anonymized_connection, use_begindir);
1287 
1288  log_debug(LD_DIR, "Initiating %s", dir_conn_purpose_to_string(dir_purpose));
1289 
1290  if (purpose_needs_anonymity(dir_purpose, router_purpose, resource)) {
1291  tor_assert(anonymized_connection ||
1292  rend_non_anonymous_mode_enabled(options));
1293  }
1294 
1295  /* use encrypted begindir connections for everything except relays
1296  * this provides better protection for directory fetches */
1297  if (!use_begindir && directory_must_use_begindir(options)) {
1298  log_warn(LD_BUG, "Client could not use begindir connection: %s",
1299  begindir_reason ? begindir_reason : "(NULL)");
1300  return;
1301  }
1302 
1303  /* ensure that we don't make direct connections when a SOCKS server is
1304  * configured. */
1305  if (!anonymized_connection && !use_begindir && !options->HTTPProxy &&
1306  (options->Socks4Proxy || options->Socks5Proxy)) {
1307  log_warn(LD_DIR, "Cannot connect to a directory server through a "
1308  "SOCKS proxy!");
1309  return;
1310  }
1311 
1312  /* Make sure that the destination addr and port we picked is viable. */
1313  if (!port || tor_addr_is_null(&addr)) {
1314  static int logged_backtrace = 0;
1315  log_warn(LD_DIR,
1316  "Cannot make an outgoing %sconnection without a remote %sPort.",
1317  use_begindir ? "begindir " : "",
1318  use_begindir ? "OR" : "Dir");
1319  if (!logged_backtrace) {
1320  log_backtrace(LOG_INFO, LD_BUG, "Address came from");
1321  logged_backtrace = 1;
1322  }
1323  return;
1324  }
1325 
1326  conn = dir_connection_new(tor_addr_family(&addr));
1327 
1328  /* set up conn so it's got all the data we need to remember */
1329  tor_addr_copy(&conn->base_.addr, &addr);
1330  conn->base_.port = port;
1331  conn->base_.address = tor_addr_to_str_dup(&addr);
1332  memcpy(conn->identity_digest, digest, DIGEST_LEN);
1333 
1334  conn->base_.purpose = dir_purpose;
1335  conn->router_purpose = router_purpose;
1336 
1337  /* give it an initial state */
1338  conn->base_.state = DIR_CONN_STATE_CONNECTING;
1339 
1340  /* decide whether we can learn our IP address from this conn */
1341  /* XXXX This is a bad name for this field now. */
1342  conn->dirconn_direct = !anonymized_connection;
1343 
1344  /* copy rendezvous data, if any */
1345  if (rend_query) {
1346  /* We can't have both v2 and v3+ identifier. */
1347  tor_assert_nonfatal(!hs_ident);
1348  conn->rend_data = rend_data_dup(rend_query);
1349  }
1350  if (hs_ident) {
1351  /* We can't have both v2 and v3+ identifier. */
1352  tor_assert_nonfatal(!rend_query);
1353  conn->hs_ident = hs_ident_dir_conn_dup(hs_ident);
1354  }
1355 
1356  if (!anonymized_connection && !use_begindir) {
1357  /* then we want to connect to dirport directly */
1358 
1359  if (options->HTTPProxy) {
1360  tor_addr_copy(&addr, &options->HTTPProxyAddr);
1361  port = options->HTTPProxyPort;
1362  }
1363 
1364  // In this case we should not have picked a directory guard.
1365  if (BUG(guard_state)) {
1366  entry_guard_cancel(&guard_state);
1367  }
1368 
1369  switch (connection_connect(TO_CONN(conn), conn->base_.address, &addr,
1370  port, &socket_error)) {
1371  case -1:
1372  connection_mark_for_close(TO_CONN(conn));
1373  return;
1374  case 1:
1375  /* start flushing conn */
1376  conn->base_.state = DIR_CONN_STATE_CLIENT_SENDING;
1377  /* fall through */
1378  case 0:
1379  /* queue the command on the outbuf */
1380  directory_send_command(conn, 1, request);
1382  /* writable indicates finish, readable indicates broken link,
1383  error indicates broken link in windowsland. */
1384  }
1385  } else {
1386  /* We will use a Tor circuit (maybe 1-hop, maybe 3-hop, maybe with
1387  * begindir, maybe not with begindir) */
1388 
1389  entry_connection_t *linked_conn;
1390 
1391  /* Anonymized tunneled connections can never share a circuit.
1392  * One-hop directory connections can share circuits with each other
1393  * but nothing else. */
1394  int iso_flags = anonymized_connection ? ISO_STREAM : ISO_SESSIONGRP;
1395 
1396  /* If it's an anonymized connection, remember the fact that we
1397  * wanted it for later: maybe we'll want it again soon. */
1398  if (anonymized_connection && use_begindir)
1399  rep_hist_note_used_internal(time(NULL), 0, 1);
1400  else if (anonymized_connection && !use_begindir)
1401  rep_hist_note_used_port(time(NULL), conn->base_.port);
1402 
1403  // In this case we should not have a directory guard; we'll
1404  // get a regular guard later when we build the circuit.
1405  if (BUG(anonymized_connection && guard_state)) {
1406  entry_guard_cancel(&guard_state);
1407  }
1408 
1409  conn->guard_state = guard_state;
1410 
1411  /* make an AP connection
1412  * populate it and add it at the right state
1413  * hook up both sides
1414  */
1415  linked_conn =
1417  conn->base_.address, conn->base_.port,
1418  digest,
1419  SESSION_GROUP_DIRCONN, iso_flags,
1420  use_begindir, !anonymized_connection);
1421  if (!linked_conn) {
1422  log_warn(LD_NET,"Making tunnel to dirserver failed.");
1423  connection_mark_for_close(TO_CONN(conn));
1424  return;
1425  }
1426 
1427  if (connection_add(TO_CONN(conn)) < 0) {
1428  log_warn(LD_NET,"Unable to add connection for link to dirserver.");
1429  connection_mark_for_close(TO_CONN(conn));
1430  return;
1431  }
1432  conn->base_.state = DIR_CONN_STATE_CLIENT_SENDING;
1433  /* queue the command on the outbuf */
1434  directory_send_command(conn, 0, request);
1435 
1438  }
1439 }
1440 
1441 /** Helper for sorting
1442  *
1443  * sort strings alphabetically
1444  *
1445  * XXXX we have a smartlist_sort_strings() function, right?
1446  */
1447 static int
1448 compare_strs_(const void **a, const void **b)
1449 {
1450  const char *s1 = *a, *s2 = *b;
1451  return strcmp(s1, s2);
1452 }
1453 
1454 #define CONDITIONAL_CONSENSUS_FPR_LEN 3
1455 #if (CONDITIONAL_CONSENSUS_FPR_LEN > DIGEST_LEN)
1456 #error "conditional consensus fingerprint length is larger than digest length"
1457 #endif
1458 
1459 /** Return the URL we should use for a consensus download.
1460  *
1461  * Use the "conditional consensus downloading" feature described in
1462  * dir-spec.txt, i.e.
1463  * GET .../consensus/<b>fpr</b>+<b>fpr</b>+<b>fpr</b>
1464  *
1465  * If 'resource' is provided, it is the name of a consensus flavor to request.
1466  */
1467 static char *
1468 directory_get_consensus_url(const char *resource)
1469 {
1470  char *url = NULL;
1471  const char *hyphen, *flavor;
1472  if (resource==NULL || strcmp(resource, "ns")==0) {
1473  flavor = ""; /* Request ns consensuses as "", so older servers will work*/
1474  hyphen = "";
1475  } else {
1476  flavor = resource;
1477  hyphen = "-";
1478  }
1479 
1480  {
1481  char *authority_id_list;
1482  smartlist_t *authority_digests = smartlist_new();
1483 
1484  SMARTLIST_FOREACH_BEGIN(router_get_trusted_dir_servers(),
1485  dir_server_t *, ds) {
1486  char *hex;
1487  if (!(ds->type & V3_DIRINFO))
1488  continue;
1489 
1490  hex = tor_malloc(2*CONDITIONAL_CONSENSUS_FPR_LEN+1);
1491  base16_encode(hex, 2*CONDITIONAL_CONSENSUS_FPR_LEN+1,
1492  ds->v3_identity_digest, CONDITIONAL_CONSENSUS_FPR_LEN);
1493  smartlist_add(authority_digests, hex);
1494  } SMARTLIST_FOREACH_END(ds);
1495  smartlist_sort(authority_digests, compare_strs_);
1496  authority_id_list = smartlist_join_strings(authority_digests,
1497  "+", 0, NULL);
1498 
1499  tor_asprintf(&url, "/tor/status-vote/current/consensus%s%s/%s.z",
1500  hyphen, flavor, authority_id_list);
1501 
1502  SMARTLIST_FOREACH(authority_digests, char *, cp, tor_free(cp));
1503  smartlist_free(authority_digests);
1504  tor_free(authority_id_list);
1505  }
1506  return url;
1507 }
1508 
1509 /**
1510  * Copies the ipv6 from source to destination, subject to buffer size limit
1511  * size. If decorate is true, makes sure the copied address is decorated.
1512  */
1513 static void
1514 copy_ipv6_address(char* destination, const char* source, size_t len,
1515  int decorate) {
1516  tor_assert(destination);
1517  tor_assert(source);
1518 
1519  if (decorate && source[0] != '[') {
1520  tor_snprintf(destination, len, "[%s]", source);
1521  } else {
1522  strlcpy(destination, source, len);
1523  }
1524 }
1525 
1526 /** Queue an appropriate HTTP command for <b>request</b> on
1527  * <b>conn</b>->outbuf. If <b>direct</b> is true, we're making a
1528  * non-anonymized connection to the dirport.
1529  */
1530 static void
1532  const int direct,
1533  const directory_request_t *req)
1534 {
1535  tor_assert(req);
1536  const int purpose = req->dir_purpose;
1537  const char *resource = req->resource;
1538  const char *payload = req->payload;
1539  const size_t payload_len = req->payload_len;
1540  const time_t if_modified_since = req->if_modified_since;
1541  const int anonymized_connection = dirind_is_anon(req->indirection);
1542 
1543  char proxystring[256];
1544  char hoststring[128];
1545  /* NEEDS to be the same size hoststring.
1546  Will be decorated with brackets around it if it is ipv6. */
1547  char decorated_address[128];
1548  smartlist_t *headers = smartlist_new();
1549  char *url;
1550  char *accept_encoding;
1551  size_t url_len;
1552  char request[8192];
1553  size_t request_len, total_request_len = 0;
1554  const char *httpcommand = NULL;
1555 
1556  tor_assert(conn);
1557  tor_assert(conn->base_.type == CONN_TYPE_DIR);
1558 
1560  if (resource)
1561  conn->requested_resource = tor_strdup(resource);
1562 
1563  /* decorate the ip address if it is ipv6 */
1564  if (strchr(conn->base_.address, ':')) {
1565  copy_ipv6_address(decorated_address, conn->base_.address,
1566  sizeof(decorated_address), 1);
1567  } else {
1568  strlcpy(decorated_address, conn->base_.address, sizeof(decorated_address));
1569  }
1570 
1571  /* come up with a string for which Host: we want */
1572  if (conn->base_.port == 80) {
1573  strlcpy(hoststring, decorated_address, sizeof(hoststring));
1574  } else {
1575  tor_snprintf(hoststring, sizeof(hoststring), "%s:%d",
1576  decorated_address, conn->base_.port);
1577  }
1578 
1579  /* Format if-modified-since */
1580  if (if_modified_since) {
1581  char b[RFC1123_TIME_LEN+1];
1582  format_rfc1123_time(b, if_modified_since);
1583  smartlist_add_asprintf(headers, "If-Modified-Since: %s\r\n", b);
1584  }
1585 
1586  /* come up with some proxy lines, if we're using one. */
1587  if (direct && get_options()->HTTPProxy) {
1588  char *base64_authenticator=NULL;
1589  const char *authenticator = get_options()->HTTPProxyAuthenticator;
1590 
1591  tor_snprintf(proxystring, sizeof(proxystring),"http://%s", hoststring);
1592  if (authenticator) {
1593  base64_authenticator = alloc_http_authenticator(authenticator);
1594  if (!base64_authenticator)
1595  log_warn(LD_BUG, "Encoding http authenticator failed");
1596  }
1597  if (base64_authenticator) {
1598  smartlist_add_asprintf(headers,
1599  "Proxy-Authorization: Basic %s\r\n",
1600  base64_authenticator);
1601  tor_free(base64_authenticator);
1602  }
1603  } else {
1604  proxystring[0] = 0;
1605  }
1606 
1607  if (! anonymized_connection) {
1608  /* Add Accept-Encoding. */
1609  accept_encoding = accept_encoding_header();
1610  smartlist_add_asprintf(headers, "Accept-Encoding: %s\r\n",
1611  accept_encoding);
1612  tor_free(accept_encoding);
1613  }
1614 
1615  /* Add additional headers, if any */
1616  {
1617  config_line_t *h;
1618  for (h = req->additional_headers; h; h = h->next) {
1619  smartlist_add_asprintf(headers, "%s%s\r\n", h->key, h->value);
1620  }
1621  }
1622 
1623  switch (purpose) {
1625  /* resource is optional. If present, it's a flavor name */
1626  tor_assert(!payload);
1627  httpcommand = "GET";
1628  url = directory_get_consensus_url(resource);
1629  log_info(LD_DIR, "Downloading consensus from %s using %s",
1630  hoststring, url);
1631  break;
1633  tor_assert(resource);
1634  tor_assert(!payload);
1635  httpcommand = "GET";
1636  tor_asprintf(&url, "/tor/keys/%s", resource);
1637  break;
1639  tor_assert(resource);
1640  tor_assert(!payload);
1641  httpcommand = "GET";
1642  tor_asprintf(&url, "/tor/status-vote/next/%s.z", resource);
1643  break;
1645  tor_assert(!resource);
1646  tor_assert(!payload);
1647  httpcommand = "GET";
1648  url = tor_strdup("/tor/status-vote/next/consensus-signatures.z");
1649  break;
1651  tor_assert(resource);
1652  httpcommand = "GET";
1653  tor_asprintf(&url, "/tor/server/%s", resource);
1654  break;
1656  tor_assert(resource);
1657  httpcommand = "GET";
1658  tor_asprintf(&url, "/tor/extra/%s", resource);
1659  break;
1661  tor_assert(resource);
1662  httpcommand = "GET";
1663  tor_asprintf(&url, "/tor/micro/%s", resource);
1664  break;
1665  case DIR_PURPOSE_UPLOAD_DIR: {
1666  const char *why = router_get_descriptor_gen_reason();
1667  tor_assert(!resource);
1668  tor_assert(payload);
1669  httpcommand = "POST";
1670  url = tor_strdup("/tor/");
1671  if (!why) {
1672  why = "for no reason at all";
1673  }
1674  smartlist_add_asprintf(headers, "X-Desc-Gen-Reason: %s\r\n", why);
1675  break;
1676  }
1678  tor_assert(!resource);
1679  tor_assert(payload);
1680  httpcommand = "POST";
1681  url = tor_strdup("/tor/post/vote");
1682  break;
1684  tor_assert(!resource);
1685  tor_assert(payload);
1686  httpcommand = "POST";
1687  url = tor_strdup("/tor/post/consensus-signature");
1688  break;
1690  tor_assert(resource);
1691  tor_assert(strlen(resource) <= REND_DESC_ID_V2_LEN_BASE32);
1692  tor_assert(!payload);
1693  httpcommand = "GET";
1694  tor_asprintf(&url, "/tor/rendezvous2/%s", resource);
1695  break;
1697  tor_assert(resource);
1698  tor_assert(strlen(resource) <= ED25519_BASE64_LEN);
1699  tor_assert(!payload);
1700  httpcommand = "GET";
1701  tor_asprintf(&url, "/tor/hs/3/%s", resource);
1702  break;
1704  tor_assert(!resource);
1705  tor_assert(payload);
1706  httpcommand = "POST";
1707  url = tor_strdup("/tor/rendezvous2/publish");
1708  break;
1710  tor_assert(resource);
1711  tor_assert(payload);
1712  httpcommand = "POST";
1713  tor_asprintf(&url, "/tor/hs/%s/publish", resource);
1714  break;
1715  default:
1716  tor_assert(0);
1717  return;
1718  }
1719 
1720  /* warn in the non-tunneled case */
1721  if (direct && (strlen(proxystring) + strlen(url) >= 4096)) {
1722  log_warn(LD_BUG,
1723  "Squid does not like URLs longer than 4095 bytes, and this "
1724  "one is %d bytes long: %s%s",
1725  (int)(strlen(proxystring) + strlen(url)), proxystring, url);
1726  }
1727 
1728  tor_snprintf(request, sizeof(request), "%s %s", httpcommand, proxystring);
1729 
1730  request_len = strlen(request);
1731  total_request_len += request_len;
1732  connection_buf_add(request, request_len, TO_CONN(conn));
1733 
1734  url_len = strlen(url);
1735  total_request_len += url_len;
1736  connection_buf_add(url, url_len, TO_CONN(conn));
1737  tor_free(url);
1738 
1739  if (!strcmp(httpcommand, "POST") || payload) {
1740  smartlist_add_asprintf(headers, "Content-Length: %lu\r\n",
1741  payload ? (unsigned long)payload_len : 0);
1742  }
1743 
1744  {
1745  char *header = smartlist_join_strings(headers, "", 0, NULL);
1746  tor_snprintf(request, sizeof(request), " HTTP/1.0\r\nHost: %s\r\n%s\r\n",
1747  hoststring, header);
1748  tor_free(header);
1749  }
1750 
1751  request_len = strlen(request);
1752  total_request_len += request_len;
1753  connection_buf_add(request, request_len, TO_CONN(conn));
1754 
1755  if (payload) {
1756  /* then send the payload afterwards too */
1757  connection_buf_add(payload, payload_len, TO_CONN(conn));
1758  total_request_len += payload_len;
1759  }
1760 
1761  SMARTLIST_FOREACH(headers, char *, h, tor_free(h));
1762  smartlist_free(headers);
1763 
1764  log_debug(LD_DIR,
1765  "Sent request to directory server '%s:%d': "
1766  "(purpose: %d, request size: %"TOR_PRIuSZ", "
1767  "payload size: %"TOR_PRIuSZ")",
1768  conn->base_.address, conn->base_.port,
1769  conn->base_.purpose,
1770  (total_request_len),
1771  (payload ? payload_len : 0));
1772 }
1773 
1774 /** Return true iff <b>body</b> doesn't start with a plausible router or
1775  * network-status or microdescriptor opening. This is a sign of possible
1776  * compression. */
1777 static int
1778 body_is_plausible(const char *body, size_t len, int purpose)
1779 {
1780  int i;
1781  if (len == 0)
1782  return 1; /* empty bodies don't need decompression */
1783  if (len < 32)
1784  return 0;
1785  if (purpose == DIR_PURPOSE_FETCH_MICRODESC) {
1786  return (!strcmpstart(body,"onion-key"));
1787  }
1788 
1789  if (!strcmpstart(body,"router") ||
1790  !strcmpstart(body,"network-status"))
1791  return 1;
1792  for (i=0;i<32;++i) {
1793  if (!TOR_ISPRINT(body[i]) && !TOR_ISSPACE(body[i]))
1794  return 0;
1795  }
1796 
1797  return 1;
1798 }
1799 
1800 /** Called when we've just fetched a bunch of router descriptors in
1801  * <b>body</b>. The list <b>which</b>, if present, holds digests for
1802  * descriptors we requested: descriptor digests if <b>descriptor_digests</b>
1803  * is true, or identity digests otherwise. Parse the descriptors, validate
1804  * them, and annotate them as having purpose <b>purpose</b> and as having been
1805  * downloaded from <b>source</b>.
1806  *
1807  * Return the number of routers actually added. */
1808 static int
1809 load_downloaded_routers(const char *body, smartlist_t *which,
1810  int descriptor_digests,
1811  int router_purpose,
1812  const char *source)
1813 {
1814  char buf[256];
1815  char time_buf[ISO_TIME_LEN+1];
1816  int added = 0;
1817  int general = router_purpose == ROUTER_PURPOSE_GENERAL;
1818  format_iso_time(time_buf, time(NULL));
1819  tor_assert(source);
1820 
1821  if (tor_snprintf(buf, sizeof(buf),
1822  "@downloaded-at %s\n"
1823  "@source %s\n"
1824  "%s%s%s", time_buf, escaped(source),
1825  !general ? "@purpose " : "",
1826  !general ? router_purpose_to_string(router_purpose) : "",
1827  !general ? "\n" : "")<0)
1828  return added;
1829 
1830  added = router_load_routers_from_string(body, NULL, SAVED_NOWHERE, which,
1831  descriptor_digests, buf);
1832  if (added && general)
1833  control_event_boot_dir(BOOTSTRAP_STATUS_LOADING_DESCRIPTORS,
1835  return added;
1836 }
1837 
1839  const response_handler_args_t *);
1841  const response_handler_args_t *);
1843  const response_handler_args_t *);
1845  const response_handler_args_t *);
1847  const response_handler_args_t *);
1849  const response_handler_args_t *);
1851  const response_handler_args_t *);
1853  const response_handler_args_t *);
1855  const response_handler_args_t *);
1857  const response_handler_args_t *);
1858 
1859 static int
1860 dir_client_decompress_response_body(char **bodyp, size_t *bodylenp,
1861  dir_connection_t *conn,
1862  compress_method_t compression,
1863  int anonymized_connection)
1864 {
1865  int rv = 0;
1866  const char *body = *bodyp;
1867  size_t body_len = *bodylenp;
1868  int allow_partial = (conn->base_.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
1869  conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO ||
1870  conn->base_.purpose == DIR_PURPOSE_FETCH_MICRODESC);
1871 
1872  int plausible = body_is_plausible(body, body_len, conn->base_.purpose);
1873 
1874  if (plausible && compression == NO_METHOD) {
1875  return 0;
1876  }
1877 
1878  int severity = LOG_DEBUG;
1879  char *new_body = NULL;
1880  size_t new_len = 0;
1881  const char *description1, *description2;
1882  int want_to_try_both = 0;
1883  int tried_both = 0;
1884  compress_method_t guessed = detect_compression_method(body, body_len);
1885 
1886  description1 = compression_method_get_human_name(compression);
1887 
1888  if (BUG(description1 == NULL))
1889  description1 = compression_method_get_human_name(UNKNOWN_METHOD);
1890 
1891  if (guessed == UNKNOWN_METHOD && !plausible)
1892  description2 = "confusing binary junk";
1893  else
1894  description2 = compression_method_get_human_name(guessed);
1895 
1896  /* Tell the user if we don't believe what we're told about compression.*/
1897  want_to_try_both = (compression == UNKNOWN_METHOD ||
1898  guessed != compression);
1899  if (want_to_try_both) {
1900  severity = LOG_PROTOCOL_WARN;
1901  }
1902 
1903  tor_log(severity, LD_HTTP,
1904  "HTTP body from server '%s:%d' was labeled as %s, "
1905  "%s it seems to be %s.%s",
1906  conn->base_.address, conn->base_.port, description1,
1907  guessed != compression?"but":"and",
1908  description2,
1909  (compression>0 && guessed>0 && want_to_try_both)?
1910  " Trying both.":"");
1911 
1912  /* Try declared compression first if we can.
1913  * tor_compress_supports_method() also returns true for NO_METHOD.
1914  * Ensure that the server is not sending us data compressed using a
1915  * compression method that is not allowed for anonymous connections. */
1916  if (anonymized_connection &&
1919  rv = -1;
1920  goto done;
1921  }
1922 
1923  if (tor_compress_supports_method(compression)) {
1924  tor_uncompress(&new_body, &new_len, body, body_len, compression,
1925  !allow_partial, LOG_PROTOCOL_WARN);
1926  if (new_body) {
1927  /* We succeeded with the declared compression method. Great! */
1928  rv = 0;
1929  goto done;
1930  }
1931  }
1932 
1933  /* Okay, if that didn't work, and we think that it was compressed
1934  * differently, try that. */
1935  if (anonymized_connection &&
1938  rv = -1;
1939  goto done;
1940  }
1941 
1942  if (tor_compress_supports_method(guessed) &&
1943  compression != guessed) {
1944  tor_uncompress(&new_body, &new_len, body, body_len, guessed,
1945  !allow_partial, LOG_INFO);
1946  tried_both = 1;
1947  }
1948  /* If we're pretty sure that we have a compressed directory, and
1949  * we didn't manage to uncompress it, then warn and bail. */
1950  if (!plausible && !new_body) {
1951  log_fn(LOG_PROTOCOL_WARN, LD_HTTP,
1952  "Unable to decompress HTTP body (tried %s%s%s, server '%s:%d').",
1953  description1,
1954  tried_both?" and ":"",
1955  tried_both?description2:"",
1956  conn->base_.address, conn->base_.port);
1957  rv = -1;
1958  goto done;
1959  }
1960 
1961  done:
1962  if (new_body) {
1963  if (rv == 0) {
1964  /* success! */
1965  tor_free(*bodyp);
1966  *bodyp = new_body;
1967  *bodylenp = new_len;
1968  } else {
1969  tor_free(new_body);
1970  }
1971  }
1972 
1973  return rv;
1974 }
1975 
1976 /** We are a client, and we've finished reading the server's
1977  * response. Parse it and act appropriately.
1978  *
1979  * If we're still happy with using this directory server in the future, return
1980  * 0. Otherwise return -1; and the caller should consider trying the request
1981  * again.
1982  *
1983  * The caller will take care of marking the connection for close.
1984  */
1985 static int
1987 {
1988  char *body = NULL;
1989  char *headers = NULL;
1990  char *reason = NULL;
1991  size_t body_len = 0;
1992  int status_code;
1993  time_t date_header = 0;
1994  long apparent_skew;
1995  compress_method_t compression;
1996  int skewed = 0;
1997  int rv;
1998  int allow_partial = (conn->base_.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
1999  conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO ||
2000  conn->base_.purpose == DIR_PURPOSE_FETCH_MICRODESC);
2001  size_t received_bytes;
2002  const int anonymized_connection =
2003  purpose_needs_anonymity(conn->base_.purpose,
2004  conn->router_purpose,
2005  conn->requested_resource);
2006 
2007  received_bytes = connection_get_inbuf_len(TO_CONN(conn));
2008 
2010  &headers, MAX_HEADERS_SIZE,
2011  &body, &body_len, MAX_DIR_DL_SIZE,
2012  allow_partial)) {
2013  case -1: /* overflow */
2014  log_warn(LD_PROTOCOL,
2015  "'fetch' response too large (server '%s:%d'). Closing.",
2016  conn->base_.address, conn->base_.port);
2017  return -1;
2018  case 0:
2019  log_info(LD_HTTP,
2020  "'fetch' response not all here, but we're at eof. Closing.");
2021  return -1;
2022  /* case 1, fall through */
2023  }
2024 
2025  if (parse_http_response(headers, &status_code, &date_header,
2026  &compression, &reason) < 0) {
2027  log_warn(LD_HTTP,"Unparseable headers (server '%s:%d'). Closing.",
2028  conn->base_.address, conn->base_.port);
2029 
2030  rv = -1;
2031  goto done;
2032  }
2033  if (!reason) reason = tor_strdup("[no reason given]");
2034 
2036  "Received response from directory server '%s:%d': %d %s "
2037  "(purpose: %d, response size: %"TOR_PRIuSZ
2038 #ifdef MEASUREMENTS_21206
2039  ", data cells received: %d, data cells sent: %d"
2040 #endif
2041  ", compression: %d)",
2042  conn->base_.address, conn->base_.port, status_code,
2043  escaped(reason), conn->base_.purpose,
2044  (received_bytes),
2045 #ifdef MEASUREMENTS_21206
2046  conn->data_cells_received, conn->data_cells_sent,
2047 #endif
2048  compression);
2049 
2050  if (conn->guard_state) {
2051  /* we count the connection as successful once we can read from it. We do
2052  * not, however, delay use of the circuit here, since it's just for a
2053  * one-hop directory request. */
2054  /* XXXXprop271 note that this will not do the right thing for other
2055  * waiting circuits that would be triggered by this circuit becoming
2056  * complete/usable. But that's ok, I think.
2057  */
2059  circuit_guard_state_free(conn->guard_state);
2060  conn->guard_state = NULL;
2061  }
2062 
2063  /* now check if it's got any hints for us about our IP address. */
2064  if (conn->dirconn_direct) {
2065  char *guess = http_get_header(headers, X_ADDRESS_HEADER);
2066  if (guess) {
2067  router_new_address_suggestion(guess, conn);
2068  tor_free(guess);
2069  }
2070  }
2071 
2072  if (date_header > 0) {
2073  /* The date header was written very soon after we sent our request,
2074  * so compute the skew as the difference between sending the request
2075  * and the date header. (We used to check now-date_header, but that's
2076  * inaccurate if we spend a lot of time downloading.)
2077  */
2078  apparent_skew = conn->base_.timestamp_last_write_allowed - date_header;
2079  if (labs(apparent_skew)>ALLOW_DIRECTORY_TIME_SKEW) {
2080  int trusted = router_digest_is_trusted_dir(conn->identity_digest);
2081  clock_skew_warning(TO_CONN(conn), apparent_skew, trusted, LD_HTTP,
2082  "directory", "DIRSERV");
2083  skewed = 1; /* don't check the recommended-versions line */
2084  } else {
2085  log_debug(LD_HTTP, "Time on received directory is within tolerance; "
2086  "we are %ld seconds skewed. (That's okay.)", apparent_skew);
2087  }
2088  }
2089  (void) skewed; /* skewed isn't used yet. */
2090 
2091  if (status_code == 503) {
2092  routerstatus_t *rs;
2093  dir_server_t *ds;
2094  const char *id_digest = conn->identity_digest;
2095  log_info(LD_DIR,"Received http status code %d (%s) from server "
2096  "'%s:%d'. I'll try again soon.",
2097  status_code, escaped(reason), conn->base_.address,
2098  conn->base_.port);
2099  time_t now = approx_time();
2100  if ((rs = router_get_mutable_consensus_status_by_id(id_digest)))
2101  rs->last_dir_503_at = now;
2102  if ((ds = router_get_fallback_dirserver_by_digest(id_digest)))
2103  ds->fake_status.last_dir_503_at = now;
2104 
2105  rv = -1;
2106  goto done;
2107  }
2108 
2109  if (dir_client_decompress_response_body(&body, &body_len,
2110  conn, compression, anonymized_connection) < 0) {
2111  rv = -1;
2112  goto done;
2113  }
2114 
2115  response_handler_args_t args;
2116  memset(&args, 0, sizeof(args));
2117  args.status_code = status_code;
2118  args.reason = reason;
2119  args.body = body;
2120  args.body_len = body_len;
2121  args.headers = headers;
2122 
2123  switch (conn->base_.purpose) {
2125  rv = handle_response_fetch_consensus(conn, &args);
2126  break;
2128  rv = handle_response_fetch_certificate(conn, &args);
2129  break;
2131  rv = handle_response_fetch_status_vote(conn, &args);
2132  break;
2135  break;
2138  rv = handle_response_fetch_desc(conn, &args);
2139  break;
2141  rv = handle_response_fetch_microdesc(conn, &args);
2142  break;
2144  rv = handle_response_fetch_renddesc_v2(conn, &args);
2145  break;
2147  rv = handle_response_upload_dir(conn, &args);
2148  break;
2150  rv = handle_response_upload_signatures(conn, &args);
2151  break;
2153  rv = handle_response_upload_vote(conn, &args);
2154  break;
2156  rv = handle_response_upload_renddesc_v2(conn, &args);
2157  break;
2159  rv = handle_response_upload_hsdesc(conn, &args);
2160  break;
2162  rv = handle_response_fetch_hsdesc_v3(conn, &args);
2163  break;
2164  default:
2165  tor_assert_nonfatal_unreached();
2166  rv = -1;
2167  break;
2168  }
2169 
2170  done:
2171  tor_free(body);
2172  tor_free(headers);
2173  tor_free(reason);
2174  return rv;
2175 }
2176 
2177 /**
2178  * Handler function: processes a response to a request for a networkstatus
2179  * consensus document by checking the consensus, storing it, and marking
2180  * router requests as reachable.
2181  **/
2182 STATIC int
2184  const response_handler_args_t *args)
2185 {
2187  const int status_code = args->status_code;
2188  const char *body = args->body;
2189  const size_t body_len = args->body_len;
2190  const char *reason = args->reason;
2191  const time_t now = approx_time();
2192 
2193  const char *consensus;
2194  char *new_consensus = NULL;
2195  const char *sourcename;
2196 
2197  int r;
2198  const char *flavname = conn->requested_resource;
2199  if (status_code != 200) {
2200  int severity = (status_code == 304) ? LOG_INFO : LOG_WARN;
2201  tor_log(severity, LD_DIR,
2202  "Received http status code %d (%s) from server "
2203  "'%s:%d' while fetching consensus directory.",
2204  status_code, escaped(reason), conn->base_.address,
2205  conn->base_.port);
2206  networkstatus_consensus_download_failed(status_code, flavname);
2207  return -1;
2208  }
2209 
2210  if (looks_like_a_consensus_diff(body, body_len)) {
2211  /* First find our previous consensus. Maybe it's in ram, maybe not. */
2212  cached_dir_t *cd = dirserv_get_consensus(flavname);
2213  const char *consensus_body = NULL;
2214  size_t consensus_body_len;
2215  tor_mmap_t *mapped_consensus = NULL;
2216  if (cd) {
2217  consensus_body = cd->dir;
2218  consensus_body_len = cd->dir_len;
2219  } else {
2220  mapped_consensus = networkstatus_map_cached_consensus(flavname);
2221  if (mapped_consensus) {
2222  consensus_body = mapped_consensus->data;
2223  consensus_body_len = mapped_consensus->size;
2224  }
2225  }
2226  if (!consensus_body) {
2227  log_warn(LD_DIR, "Received a consensus diff, but we can't find "
2228  "any %s-flavored consensus in our current cache.",flavname);
2229  tor_munmap_file(mapped_consensus);
2231  // XXXX if this happens too much, see below
2232  return -1;
2233  }
2234 
2235  new_consensus = consensus_diff_apply(consensus_body, consensus_body_len,
2236  body, body_len);
2237  tor_munmap_file(mapped_consensus);
2238  if (new_consensus == NULL) {
2239  log_warn(LD_DIR, "Could not apply consensus diff received from server "
2240  "'%s:%d'", conn->base_.address, conn->base_.port);
2241  // XXXX If this happens too many times, we should maybe not use
2242  // XXXX this directory for diffs any more?
2244  return -1;
2245  }
2246  log_info(LD_DIR, "Applied consensus diff (size %d) from server "
2247  "'%s:%d', resulting in a new consensus document (size %d).",
2248  (int)body_len, conn->base_.address, conn->base_.port,
2249  (int)strlen(new_consensus));
2250  consensus = new_consensus;
2251  sourcename = "generated based on a diff";
2252  } else {
2253  log_info(LD_DIR,"Received consensus directory (body size %d) from server "
2254  "'%s:%d'", (int)body_len, conn->base_.address, conn->base_.port);
2255  consensus = body;
2256  sourcename = "downloaded";
2257  }
2258 
2259  if ((r=networkstatus_set_current_consensus(consensus,
2260  strlen(consensus),
2261  flavname, 0,
2262  conn->identity_digest))<0) {
2264  "Unable to load %s consensus directory %s from "
2265  "server '%s:%d'. I'll try again soon.",
2266  flavname, sourcename, conn->base_.address, conn->base_.port);
2268  tor_free(new_consensus);
2269  return -1;
2270  }
2271 
2272  /* If we launched other fetches for this consensus, cancel them. */
2273  connection_dir_close_consensus_fetches(conn, flavname);
2274 
2275  /* update the list of routers and directory guards */
2278  directory_info_has_arrived(now, 0, 0);
2279 
2280  if (authdir_mode_v3(get_options())) {
2283  }
2284  log_info(LD_DIR, "Successfully loaded consensus.");
2285 
2286  tor_free(new_consensus);
2287  return 0;
2288 }
2289 
2290 /**
2291  * Handler function: processes a response to a request for one or more
2292  * authority certificates
2293  **/
2294 static int
2296  const response_handler_args_t *args)
2297 {
2299  const int status_code = args->status_code;
2300  const char *reason = args->reason;
2301  const char *body = args->body;
2302  const size_t body_len = args->body_len;
2303 
2304  if (status_code != 200) {
2305  log_warn(LD_DIR,
2306  "Received http status code %d (%s) from server "
2307  "'%s:%d' while fetching \"/tor/keys/%s\".",
2308  status_code, escaped(reason), conn->base_.address,
2309  conn->base_.port, conn->requested_resource);
2310  connection_dir_download_cert_failed(conn, status_code);
2311  return -1;
2312  }
2313  log_info(LD_DIR,"Received authority certificates (body size %d) from "
2314  "server '%s:%d'",
2315  (int)body_len, conn->base_.address, conn->base_.port);
2316 
2317  /*
2318  * Tell trusted_dirs_load_certs_from_string() whether it was by fp
2319  * or fp-sk pair.
2320  */
2321  int src_code = -1;
2322  if (!strcmpstart(conn->requested_resource, "fp/")) {
2323  src_code = TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST;
2324  } else if (!strcmpstart(conn->requested_resource, "fp-sk/")) {
2325  src_code = TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_SK_DIGEST;
2326  }
2327 
2328  if (src_code != -1) {
2329  if (trusted_dirs_load_certs_from_string(body, src_code, 1,
2330  conn->identity_digest)<0) {
2331  log_warn(LD_DIR, "Unable to parse fetched certificates");
2332  /* if we fetched more than one and only some failed, the successful
2333  * ones got flushed to disk so it's safe to call this on them */
2334  connection_dir_download_cert_failed(conn, status_code);
2335  } else {
2336  time_t now = approx_time();
2337  directory_info_has_arrived(now, 0, 0);
2338  log_info(LD_DIR, "Successfully loaded certificates from fetch.");
2339  }
2340  } else {
2341  log_warn(LD_DIR,
2342  "Couldn't figure out what to do with fetched certificates for "
2343  "unknown resource %s",
2344  conn->requested_resource);
2345  connection_dir_download_cert_failed(conn, status_code);
2346  }
2347  return 0;
2348 }
2349 
2350 /**
2351  * Handler function: processes a response to a request for an authority's
2352  * current networkstatus vote.
2353  **/
2354 static int
2356  const response_handler_args_t *args)
2357 {
2359  const int status_code = args->status_code;
2360  const char *reason = args->reason;
2361  const char *body = args->body;
2362  const size_t body_len = args->body_len;
2363 
2364  const char *msg;
2365  int st;
2366  log_info(LD_DIR,"Got votes (body size %d) from server %s:%d",
2367  (int)body_len, conn->base_.address, conn->base_.port);
2368  if (status_code != 200) {
2369  log_warn(LD_DIR,
2370  "Received http status code %d (%s) from server "
2371  "'%s:%d' while fetching \"/tor/status-vote/next/%s.z\".",
2372  status_code, escaped(reason), conn->base_.address,
2373  conn->base_.port, conn->requested_resource);
2374  return -1;
2375  }
2376  dirvote_add_vote(body, &msg, &st);
2377  if (st > 299) {
2378  log_warn(LD_DIR, "Error adding retrieved vote: %s", msg);
2379  } else {
2380  log_info(LD_DIR, "Added vote(s) successfully [msg: %s]", msg);
2381  }
2382 
2383  return 0;
2384 }
2385 
2386 /**
2387  * Handler function: processes a response to a request for the signatures
2388  * that an authority knows about on a given consensus.
2389  **/
2390 static int
2392  const response_handler_args_t *args)
2393 {
2395  const int status_code = args->status_code;
2396  const char *reason = args->reason;
2397  const char *body = args->body;
2398  const size_t body_len = args->body_len;
2399 
2400  const char *msg = NULL;
2401  log_info(LD_DIR,"Got detached signatures (body size %d) from server %s:%d",
2402  (int)body_len, conn->base_.address, conn->base_.port);
2403  if (status_code != 200) {
2404  log_warn(LD_DIR,
2405  "Received http status code %d (%s) from server '%s:%d' while fetching "
2406  "\"/tor/status-vote/next/consensus-signatures.z\".",
2407  status_code, escaped(reason), conn->base_.address,
2408  conn->base_.port);
2409  return -1;
2410  }
2411  if (dirvote_add_signatures(body, conn->base_.address, &msg)<0) {
2412  log_warn(LD_DIR, "Problem adding detached signatures from %s:%d: %s",
2413  conn->base_.address, conn->base_.port, msg?msg:"???");
2414  }
2415 
2416  return 0;
2417 }
2418 
2419 /**
2420  * Handler function: processes a response to a request for a group of server
2421  * descriptors or an extrainfo documents.
2422  **/
2423 static int
2425  const response_handler_args_t *args)
2426 {
2428  conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO);
2429  const int status_code = args->status_code;
2430  const char *reason = args->reason;
2431  const char *body = args->body;
2432  const size_t body_len = args->body_len;
2433 
2434  int was_ei = conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO;
2435  smartlist_t *which = NULL;
2436  int n_asked_for = 0;
2437  int descriptor_digests = conn->requested_resource &&
2438  !strcmpstart(conn->requested_resource,"d/");
2439  log_info(LD_DIR,"Received %s (body size %d) from server '%s:%d'",
2440  was_ei ? "extra server info" : "server info",
2441  (int)body_len, conn->base_.address, conn->base_.port);
2442  if (conn->requested_resource &&
2443  (!strcmpstart(conn->requested_resource,"d/") ||
2444  !strcmpstart(conn->requested_resource,"fp/"))) {
2445  which = smartlist_new();
2447  (descriptor_digests ? 2 : 3),
2448  which, NULL, 0);
2449  n_asked_for = smartlist_len(which);
2450  }
2451  if (status_code != 200) {
2452  int dir_okay = status_code == 404 ||
2453  (status_code == 400 && !strcmp(reason, "Servers unavailable."));
2454  /* 404 means that it didn't have them; no big deal.
2455  * Older (pre-0.1.1.8) servers said 400 Servers unavailable instead. */
2456  log_fn(dir_okay ? LOG_INFO : LOG_WARN, LD_DIR,
2457  "Received http status code %d (%s) from server '%s:%d' "
2458  "while fetching \"/tor/server/%s\". I'll try again soon.",
2459  status_code, escaped(reason), conn->base_.address,
2460  conn->base_.port, conn->requested_resource);
2461  if (!which) {
2463  } else {
2464  dir_routerdesc_download_failed(which, status_code,
2465  conn->router_purpose,
2466  was_ei, descriptor_digests);
2467  SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
2468  smartlist_free(which);
2469  }
2470  return dir_okay ? 0 : -1;
2471  }
2472  /* Learn the routers, assuming we requested by fingerprint or "all"
2473  * or "authority".
2474  *
2475  * We use "authority" to fetch our own descriptor for
2476  * testing, and to fetch bridge descriptors for bootstrapping. Ignore
2477  * the output of "authority" requests unless we are using bridges,
2478  * since otherwise they'll be the response from reachability tests,
2479  * and we don't really want to add that to our routerlist. */
2480  if (which || (conn->requested_resource &&
2481  (!strcmpstart(conn->requested_resource, "all") ||
2482  (!strcmpstart(conn->requested_resource, "authority") &&
2483  get_options()->UseBridges)))) {
2484  /* as we learn from them, we remove them from 'which' */
2485  if (was_ei) {
2487  descriptor_digests);
2488  } else {
2489  //router_load_routers_from_string(body, NULL, SAVED_NOWHERE, which,
2490  // descriptor_digests, conn->router_purpose);
2491  if (load_downloaded_routers(body, which, descriptor_digests,
2492  conn->router_purpose,
2493  conn->base_.address)) {
2494  time_t now = approx_time();
2495  directory_info_has_arrived(now, 0, 1);
2496  }
2497  }
2498  }
2499  if (which) { /* mark remaining ones as failed */
2500  log_info(LD_DIR, "Received %d/%d %s requested from %s:%d",
2501  n_asked_for-smartlist_len(which), n_asked_for,
2502  was_ei ? "extra-info documents" : "router descriptors",
2503  conn->base_.address, (int)conn->base_.port);
2504  if (smartlist_len(which)) {
2505  dir_routerdesc_download_failed(which, status_code,
2506  conn->router_purpose,
2507  was_ei, descriptor_digests);
2508  }
2509  SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
2510  smartlist_free(which);
2511  }
2514 
2515  return 0;
2516 }
2517 
2518 /**
2519  * Handler function: processes a response to a request for a group of
2520  * microdescriptors
2521  **/
2522 STATIC int
2524  const response_handler_args_t *args)
2525 {
2527  const int status_code = args->status_code;
2528  const char *reason = args->reason;
2529  const char *body = args->body;
2530  const size_t body_len = args->body_len;
2531 
2532  smartlist_t *which = NULL;
2533  log_info(LD_DIR,"Received answer to microdescriptor request (status %d, "
2534  "body size %d) from server '%s:%d'",
2535  status_code, (int)body_len, conn->base_.address,
2536  conn->base_.port);
2538  !strcmpstart(conn->requested_resource, "d/"));
2539  tor_assert_nonfatal(!fast_mem_is_zero(conn->identity_digest, DIGEST_LEN));
2540  which = smartlist_new();
2542  which, NULL,
2543  DSR_DIGEST256|DSR_BASE64);
2544  if (status_code != 200) {
2545  log_info(LD_DIR, "Received status code %d (%s) from server "
2546  "'%s:%d' while fetching \"/tor/micro/%s\". I'll try again "
2547  "soon.",
2548  status_code, escaped(reason), conn->base_.address,
2549  (int)conn->base_.port, conn->requested_resource);
2550  dir_microdesc_download_failed(which, status_code, conn->identity_digest);
2551  SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
2552  smartlist_free(which);
2553  return 0;
2554  } else {
2555  smartlist_t *mds;
2556  time_t now = approx_time();
2558  body, body+body_len, SAVED_NOWHERE, 0,
2559  now, which);
2560  if (smartlist_len(which)) {
2561  /* Mark remaining ones as failed. */
2562  dir_microdesc_download_failed(which, status_code, conn->identity_digest);
2563  }
2564  if (mds && smartlist_len(mds)) {
2565  control_event_boot_dir(BOOTSTRAP_STATUS_LOADING_DESCRIPTORS,
2567  directory_info_has_arrived(now, 0, 1);
2568  }
2569  SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
2570  smartlist_free(which);
2571  smartlist_free(mds);
2572  }
2573 
2574  return 0;
2575 }
2576 
2577 /**
2578  * Handler function: processes a response to a POST request to upload our
2579  * router descriptor.
2580  **/
2581 static int
2583  const response_handler_args_t *args)
2584 {
2585  tor_assert(conn->base_.purpose == DIR_PURPOSE_UPLOAD_DIR);
2586  const int status_code = args->status_code;
2587  const char *reason = args->reason;
2588  const char *headers = args->headers;
2589 
2590  switch (status_code) {
2591  case 200: {
2592  dir_server_t *ds =
2594  char *rejected_hdr = http_get_header(headers,
2595  "X-Descriptor-Not-New: ");
2596  if (rejected_hdr) {
2597  if (!strcmp(rejected_hdr, "Yes")) {
2598  log_info(LD_GENERAL,
2599  "Authority '%s' declined our descriptor (not new)",
2600  ds->nickname);
2601  /* XXXX use this information; be sure to upload next one
2602  * sooner. -NM */
2603  /* XXXX++ On further thought, the task above implies that we're
2604  * basing our regenerate-descriptor time on when we uploaded the
2605  * last descriptor, not on the published time of the last
2606  * descriptor. If those are different, that's a bad thing to
2607  * do. -NM */
2608  }
2609  tor_free(rejected_hdr);
2610  }
2611  log_info(LD_GENERAL,"eof (status 200) after uploading server "
2612  "descriptor: finished.");
2614  LOG_NOTICE, "ACCEPTED_SERVER_DESCRIPTOR DIRAUTH=%s:%d",
2615  conn->base_.address, conn->base_.port);
2616 
2617  ds->has_accepted_serverdesc = 1;
2619  control_event_server_status(LOG_NOTICE, "GOOD_SERVER_DESCRIPTOR");
2620  }
2621  break;
2622  case 400:
2623  log_warn(LD_GENERAL,"http status 400 (%s) response from "
2624  "dirserver '%s:%d'. Please correct.",
2625  escaped(reason), conn->base_.address, conn->base_.port);
2627  "BAD_SERVER_DESCRIPTOR DIRAUTH=%s:%d REASON=\"%s\"",
2628  conn->base_.address, conn->base_.port, escaped(reason));
2629  break;
2630  default:
2631  log_warn(LD_GENERAL,
2632  "HTTP status %d (%s) was unexpected while uploading "
2633  "descriptor to server '%s:%d'. Possibly the server is "
2634  "misconfigured?",
2635  status_code, escaped(reason), conn->base_.address,
2636  conn->base_.port);
2637  break;
2638  }
2639  /* return 0 in all cases, since we don't want to mark any
2640  * dirservers down just because they don't like us. */
2641 
2642  return 0;
2643 }
2644 
2645 /**
2646  * Handler function: processes a response to POST request to upload our
2647  * own networkstatus vote.
2648  **/
2649 static int
2651  const response_handler_args_t *args)
2652 {
2653  tor_assert(conn->base_.purpose == DIR_PURPOSE_UPLOAD_VOTE);
2654  const int status_code = args->status_code;
2655  const char *reason = args->reason;
2656 
2657  switch (status_code) {
2658  case 200: {
2659  log_notice(LD_DIR,"Uploaded a vote to dirserver %s:%d",
2660  conn->base_.address, conn->base_.port);
2661  }
2662  break;
2663  case 400:
2664  log_warn(LD_DIR,"http status 400 (%s) response after uploading "
2665  "vote to dirserver '%s:%d'. Please correct.",
2666  escaped(reason), conn->base_.address, conn->base_.port);
2667  break;
2668  default:
2669  log_warn(LD_GENERAL,
2670  "HTTP status %d (%s) was unexpected while uploading "
2671  "vote to server '%s:%d'.",
2672  status_code, escaped(reason), conn->base_.address,
2673  conn->base_.port);
2674  break;
2675  }
2676  /* return 0 in all cases, since we don't want to mark any
2677  * dirservers down just because they don't like us. */
2678  return 0;
2679 }
2680 
2681 /**
2682  * Handler function: processes a response to POST request to upload our
2683  * view of the signatures on the current consensus.
2684  **/
2685 static int
2687  const response_handler_args_t *args)
2688 {
2690  const int status_code = args->status_code;
2691  const char *reason = args->reason;
2692 
2693  switch (status_code) {
2694  case 200: {
2695  log_notice(LD_DIR,"Uploaded signature(s) to dirserver %s:%d",
2696  conn->base_.address, conn->base_.port);
2697  }
2698  break;
2699  case 400:
2700  log_warn(LD_DIR,"http status 400 (%s) response after uploading "
2701  "signatures to dirserver '%s:%d'. Please correct.",
2702  escaped(reason), conn->base_.address, conn->base_.port);
2703  break;
2704  default:
2705  log_warn(LD_GENERAL,
2706  "HTTP status %d (%s) was unexpected while uploading "
2707  "signatures to server '%s:%d'.",
2708  status_code, escaped(reason), conn->base_.address,
2709  conn->base_.port);
2710  break;
2711  }
2712  /* return 0 in all cases, since we don't want to mark any
2713  * dirservers down just because they don't like us. */
2714 
2715  return 0;
2716 }
2717 
2718 /**
2719  * Handler function: processes a response to a request for a v3 hidden service
2720  * descriptor.
2721  **/
2722 STATIC int
2724  const response_handler_args_t *args)
2725 {
2726  const int status_code = args->status_code;
2727  const char *reason = args->reason;
2728  const char *body = args->body;
2729  const size_t body_len = args->body_len;
2730 
2731  tor_assert(conn->hs_ident);
2732 
2733  log_info(LD_REND,"Received v3 hsdesc (body size %d, status %d (%s))",
2734  (int)body_len, status_code, escaped(reason));
2735 
2736  switch (status_code) {
2737  case 200:
2738  /* We got something: Try storing it in the cache. */
2739  if (hs_cache_store_as_client(body, &conn->hs_ident->identity_pk) < 0) {
2740  log_info(LD_REND, "Failed to store hidden service descriptor");
2741  /* Fire control port FAILED event. */
2742  hs_control_desc_event_failed(conn->hs_ident, conn->identity_digest,
2743  "BAD_DESC");
2744  hs_control_desc_event_content(conn->hs_ident, conn->identity_digest,
2745  NULL);
2746  } else {
2747  log_info(LD_REND, "Stored hidden service descriptor successfully.");
2748  TO_CONN(conn)->purpose = DIR_PURPOSE_HAS_FETCHED_HSDESC;
2749  hs_client_desc_has_arrived(conn->hs_ident);
2750  /* Fire control port RECEIVED event. */
2751  hs_control_desc_event_received(conn->hs_ident, conn->identity_digest);
2752  hs_control_desc_event_content(conn->hs_ident, conn->identity_digest,
2753  body);
2754  }
2755  break;
2756  case 404:
2757  /* Not there. We'll retry when connection_about_to_close_connection()
2758  * tries to clean this conn up. */
2759  log_info(LD_REND, "Fetching hidden service v3 descriptor not found: "
2760  "Retrying at another directory.");
2761  /* Fire control port FAILED event. */
2762  hs_control_desc_event_failed(conn->hs_ident, conn->identity_digest,
2763  "NOT_FOUND");
2764  hs_control_desc_event_content(conn->hs_ident, conn->identity_digest,
2765  NULL);
2766  break;
2767  case 400:
2768  log_warn(LD_REND, "Fetching v3 hidden service descriptor failed: "
2769  "http status 400 (%s). Dirserver didn't like our "
2770  "query? Retrying at another directory.",
2771  escaped(reason));
2772  /* Fire control port FAILED event. */
2773  hs_control_desc_event_failed(conn->hs_ident, conn->identity_digest,
2774  "QUERY_REJECTED");
2775  hs_control_desc_event_content(conn->hs_ident, conn->identity_digest,
2776  NULL);
2777  break;
2778  default:
2779  log_warn(LD_REND, "Fetching v3 hidden service descriptor failed: "
2780  "http status %d (%s) response unexpected from HSDir server "
2781  "'%s:%d'. Retrying at another directory.",
2782  status_code, escaped(reason), TO_CONN(conn)->address,
2783  TO_CONN(conn)->port);
2784  /* Fire control port FAILED event. */
2785  hs_control_desc_event_failed(conn->hs_ident, conn->identity_digest,
2786  "UNEXPECTED");
2787  hs_control_desc_event_content(conn->hs_ident, conn->identity_digest,
2788  NULL);
2789  break;
2790  }
2791 
2792  return 0;
2793 }
2794 
2795 /**
2796  * Handler function: processes a response to a request for a v2 hidden service
2797  * descriptor.
2798  **/
2799 static int
2801  const response_handler_args_t *args)
2802 {
2804  const int status_code = args->status_code;
2805  const char *reason = args->reason;
2806  const char *body = args->body;
2807  const size_t body_len = args->body_len;
2808 
2809 #define SEND_HS_DESC_FAILED_EVENT(reason) \
2810  (control_event_hsv2_descriptor_failed(conn->rend_data, \
2811  conn->identity_digest, \
2812  reason))
2813 #define SEND_HS_DESC_FAILED_CONTENT() \
2814  (control_event_hs_descriptor_content( \
2815  rend_data_get_address(conn->rend_data), \
2816  conn->requested_resource, \
2817  conn->identity_digest, \
2818  NULL))
2819 
2820  tor_assert(conn->rend_data);
2821  log_info(LD_REND,"Received rendezvous descriptor (body size %d, status %d "
2822  "(%s))",
2823  (int)body_len, status_code, escaped(reason));
2824  switch (status_code) {
2825  case 200:
2826  {
2827  rend_cache_entry_t *entry = NULL;
2828 
2830  conn->requested_resource,
2831  conn->rend_data, &entry) < 0) {
2832  log_warn(LD_REND,"Fetching v2 rendezvous descriptor failed. "
2833  "Retrying at another directory.");
2834  /* We'll retry when connection_about_to_close_connection()
2835  * cleans this dir conn up. */
2836  SEND_HS_DESC_FAILED_EVENT("BAD_DESC");
2837  SEND_HS_DESC_FAILED_CONTENT();
2838  } else {
2839  char service_id[REND_SERVICE_ID_LEN_BASE32 + 1];
2840  /* Should never be NULL here if we found the descriptor. */
2841  tor_assert(entry);
2842  rend_get_service_id(entry->parsed->pk, service_id);
2843 
2844  /* success. notify pending connections about this. */
2845  log_info(LD_REND, "Successfully fetched v2 rendezvous "
2846  "descriptor.");
2848  conn->rend_data,
2849  conn->identity_digest);
2851  conn->requested_resource,
2852  conn->identity_digest,
2853  body);
2855  rend_client_desc_trynow(service_id);
2856  memwipe(service_id, 0, sizeof(service_id));
2857  }
2858  break;
2859  }
2860  case 404:
2861  /* Not there. We'll retry when
2862  * connection_about_to_close_connection() cleans this conn up. */
2863  log_info(LD_REND,"Fetching v2 rendezvous descriptor failed: "
2864  "Retrying at another directory.");
2865  SEND_HS_DESC_FAILED_EVENT("NOT_FOUND");
2866  SEND_HS_DESC_FAILED_CONTENT();
2867  break;
2868  case 400:
2869  log_warn(LD_REND, "Fetching v2 rendezvous descriptor failed: "
2870  "http status 400 (%s). Dirserver didn't like our "
2871  "v2 rendezvous query? Retrying at another directory.",
2872  escaped(reason));
2873  SEND_HS_DESC_FAILED_EVENT("QUERY_REJECTED");
2874  SEND_HS_DESC_FAILED_CONTENT();
2875  break;
2876  default:
2877  log_warn(LD_REND, "Fetching v2 rendezvous descriptor failed: "
2878  "http status %d (%s) response unexpected while "
2879  "fetching v2 hidden service descriptor (server '%s:%d'). "
2880  "Retrying at another directory.",
2881  status_code, escaped(reason), conn->base_.address,
2882  conn->base_.port);
2883  SEND_HS_DESC_FAILED_EVENT("UNEXPECTED");
2884  SEND_HS_DESC_FAILED_CONTENT();
2885  break;
2886  }
2887 
2888  return 0;
2889 }
2890 
2891 /**
2892  * Handler function: processes a response to a POST request to upload a v2
2893  * hidden service descriptor.
2894  **/
2895 static int
2897  const response_handler_args_t *args)
2898 {
2900  const int status_code = args->status_code;
2901  const char *reason = args->reason;
2902 
2903 #define SEND_HS_DESC_UPLOAD_FAILED_EVENT(reason) \
2904  (control_event_hs_descriptor_upload_failed( \
2905  conn->identity_digest, \
2906  rend_data_get_address(conn->rend_data), \
2907  reason))
2908 
2909  log_info(LD_REND,"Uploaded rendezvous descriptor (status %d "
2910  "(%s))",
2911  status_code, escaped(reason));
2912  /* Without the rend data, we'll have a problem identifying what has been
2913  * uploaded for which service. */
2914  tor_assert(conn->rend_data);
2915  switch (status_code) {
2916  case 200:
2917  log_info(LD_REND,
2918  "Uploading rendezvous descriptor: finished with status "
2919  "200 (%s)", escaped(reason));
2923  break;
2924  case 400:
2925  log_warn(LD_REND,"http status 400 (%s) response from dirserver "
2926  "'%s:%d'. Malformed rendezvous descriptor?",
2927  escaped(reason), conn->base_.address, conn->base_.port);
2928  SEND_HS_DESC_UPLOAD_FAILED_EVENT("UPLOAD_REJECTED");
2929  break;
2930  default:
2931  log_warn(LD_REND,"http status %d (%s) response unexpected (server "
2932  "'%s:%d').",
2933  status_code, escaped(reason), conn->base_.address,
2934  conn->base_.port);
2935  SEND_HS_DESC_UPLOAD_FAILED_EVENT("UNEXPECTED");
2936  break;
2937  }
2938 
2939  return 0;
2940 }
2941 
2942 /**
2943  * Handler function: processes a response to a POST request to upload an
2944  * hidden service descriptor.
2945  **/
2946 static int
2948  const response_handler_args_t *args)
2949 {
2950  const int status_code = args->status_code;
2951  const char *reason = args->reason;
2952 
2953  tor_assert(conn);
2955 
2956  log_info(LD_REND, "Uploaded hidden service descriptor (status %d "
2957  "(%s))",
2958  status_code, escaped(reason));
2959  /* For this directory response, it MUST have an hidden service identifier on
2960  * this connection. */
2961  tor_assert(conn->hs_ident);
2962  switch (status_code) {
2963  case 200:
2964  log_info(LD_REND, "Uploading hidden service descriptor: "
2965  "finished with status 200 (%s)", escaped(reason));
2966  hs_control_desc_event_uploaded(conn->hs_ident, conn->identity_digest);
2967  break;
2968  case 400:
2969  log_fn(LOG_PROTOCOL_WARN, LD_REND,
2970  "Uploading hidden service descriptor: http "
2971  "status 400 (%s) response from dirserver "
2972  "'%s:%d'. Malformed hidden service descriptor?",
2973  escaped(reason), conn->base_.address, conn->base_.port);
2974  hs_control_desc_event_failed(conn->hs_ident, conn->identity_digest,
2975  "UPLOAD_REJECTED");
2976  break;
2977  default:
2978  log_warn(LD_REND, "Uploading hidden service descriptor: http "
2979  "status %d (%s) response unexpected (server "
2980  "'%s:%d').",
2981  status_code, escaped(reason), conn->base_.address,
2982  conn->base_.port);
2983  hs_control_desc_event_failed(conn->hs_ident, conn->identity_digest,
2984  "UNEXPECTED");
2985  break;
2986  }
2987 
2988  return 0;
2989 }
2990 
2991 /** Called when a directory connection reaches EOF. */
2992 int
2994 {
2995  int retval;
2996  if (conn->base_.state != DIR_CONN_STATE_CLIENT_READING) {
2997  log_info(LD_HTTP,"conn reached eof, not reading. [state=%d] Closing.",
2998  conn->base_.state);
2999  connection_close_immediate(TO_CONN(conn)); /* error: give up on flushing */
3000  connection_mark_for_close(TO_CONN(conn));
3001  return -1;
3002  }
3003 
3004  retval = connection_dir_client_reached_eof(conn);
3005  if (retval == 0) /* success */
3006  conn->base_.state = DIR_CONN_STATE_CLIENT_FINISHED;
3007  connection_mark_for_close(TO_CONN(conn));
3008  return retval;
3009 }
3010 /** We are closing a dir connection: If <b>dir_conn</b> is a dir connection
3011  * that tried to fetch an HS descriptor, check if it successfully fetched it,
3012  * or if we need to try again. */
3013 void
3015 {
3016  connection_t *conn = TO_CONN(dir_conn);
3017 
3018  /* If we were trying to fetch a v2 rend desc and did not succeed, retry as
3019  * needed. (If a fetch is successful, the connection state is changed to
3020  * DIR_PURPOSE_HAS_FETCHED_RENDDESC_V2 or DIR_PURPOSE_HAS_FETCHED_HSDESC to
3021  * mark that refetching is unnecessary.) */
3022  if (conn->purpose == DIR_PURPOSE_FETCH_RENDDESC_V2 &&
3023  dir_conn->rend_data &&
3025  rend_data_get_address(dir_conn->rend_data))) {
3027  }
3028 
3029  /* Check for v3 rend desc fetch */
3030  if (conn->purpose == DIR_PURPOSE_FETCH_HSDESC &&
3031  dir_conn->hs_ident &&
3032  !ed25519_public_key_is_zero(&dir_conn->hs_ident->identity_pk)) {
3033  hs_client_refetch_hsdesc(&dir_conn->hs_ident->identity_pk);
3034  }
3035 }
3036 
3037 /** Array of compression methods to use (if supported) for requesting
3038  * compressed data, ordered from best to worst. */
3040  LZMA_METHOD,
3041  ZSTD_METHOD,
3042  ZLIB_METHOD,
3043  GZIP_METHOD,
3044  NO_METHOD
3045 };
3046 
3047 /** Array of allowed compression methods to use (if supported) when receiving a
3048  * response from a request that was required to be anonymous. */
3050  ZLIB_METHOD,
3051  GZIP_METHOD,
3052  NO_METHOD
3053 };
3054 
3055 /** Return a newly allocated string containing a comma separated list of
3056  * supported encodings. */
3057 STATIC char *
3059 {
3060  smartlist_t *methods = smartlist_new();
3061  char *header = NULL;
3062  compress_method_t method;
3063  unsigned i;
3064 
3065  for (i = 0; i < ARRAY_LENGTH(client_meth_pref); ++i) {
3066  method = client_meth_pref[i];
3067  if (tor_compress_supports_method(method))
3068  smartlist_add(methods, (char *)compression_method_get_name(method));
3069  }
3070 
3071  header = smartlist_join_strings(methods, ", ", 0, NULL);
3072  smartlist_free(methods);
3073 
3074  return header;
3075 }
3076 
3077 /** Check if the given compression method is allowed for a connection that is
3078  * supposed to be anonymous. Returns 1 if the compression method is allowed,
3079  * otherwise 0. */
3080 STATIC int
3082 {
3083  unsigned u;
3084 
3086  ++u) {
3087  compress_method_t allowed_method =
3089 
3090  if (! tor_compress_supports_method(allowed_method))
3091  continue;
3092 
3093  if (method == allowed_method)
3094  return 1;
3095  }
3096 
3097  return 0;
3098 }
3099 
3100 /** Log a warning when a remote server has sent us a document using a
3101  * compression method that is not allowed for anonymous directory requests. */
3102 STATIC void
3104 {
3105  log_fn(LOG_PROTOCOL_WARN, LD_HTTP,
3106  "Received a %s HTTP response, which is not "
3107  "allowed for anonymous directory requests.",
3109 }
3110 
3111 /* We just got a new consensus! If there are other in-progress requests
3112  * for this consensus flavor (for example because we launched several in
3113  * parallel), cancel them.
3114  *
3115  * We do this check here (not just in
3116  * connection_ap_handshake_attach_circuit()) to handle the edge case where
3117  * a consensus fetch begins and ends before some other one tries to attach to
3118  * a circuit, in which case the other one won't know that we're all happy now.
3119  *
3120  * Don't mark the conn that just gave us the consensus -- otherwise we
3121  * would end up double-marking it when it cleans itself up.
3122  */
3123 static void
3124 connection_dir_close_consensus_fetches(dir_connection_t *except_this_one,
3125  const char *resource)
3126 {
3127  smartlist_t *conns_to_close =
3129  resource);
3130  SMARTLIST_FOREACH_BEGIN(conns_to_close, dir_connection_t *, d) {
3131  if (d == except_this_one)
3132  continue;
3133  log_info(LD_DIR, "Closing consensus fetch (to %s) since one "
3134  "has just arrived.", TO_CONN(d)->address);
3135  connection_mark_for_close(TO_CONN(d));
3136  } SMARTLIST_FOREACH_END(d);
3137  smartlist_free(conns_to_close);
3138 }
3139 /** Called when one or more routerdesc (or extrainfo, if <b>was_extrainfo</b>)
3140  * fetches have failed (with uppercase fingerprints listed in <b>failed</b>,
3141  * either as descriptor digests or as identity digests based on
3142  * <b>was_descriptor_digests</b>).
3143  */
3144 static void
3146  int router_purpose,
3147  int was_extrainfo, int was_descriptor_digests)
3148 {
3149  char digest[DIGEST_LEN];
3150  time_t now = time(NULL);
3152  if (!was_descriptor_digests) {
3153  if (router_purpose == ROUTER_PURPOSE_BRIDGE) {
3154  tor_assert(!was_extrainfo);
3156  }
3157  return; /* FFFF should implement for other-than-router-purpose someday */
3158  }
3159  SMARTLIST_FOREACH_BEGIN(failed, const char *, cp) {
3160  download_status_t *dls = NULL;
3161  if (base16_decode(digest, DIGEST_LEN, cp, strlen(cp)) != DIGEST_LEN) {
3162  log_warn(LD_BUG, "Malformed fingerprint in list: %s", escaped(cp));
3163  continue;
3164  }
3165  if (was_extrainfo) {
3166  signed_descriptor_t *sd =
3168  if (sd)
3169  dls = &sd->ei_dl_status;
3170  } else {
3172  }
3173  if (!dls)
3174  continue;
3175  download_status_increment_failure(dls, status_code, cp, server, now);
3176  } SMARTLIST_FOREACH_END(cp);
3177 
3178  /* No need to relaunch descriptor downloads here: we already do it
3179  * every 10 or 60 seconds (FOO_DESCRIPTOR_RETRY_INTERVAL) in main.c. */
3180 }
3181 
3182 /** Called when a connection to download microdescriptors from relay with
3183  * <b>dir_id</b> has failed in whole or in part. <b>failed</b> is a list
3184  * of every microdesc digest we didn't get. <b>status_code</b> is the http
3185  * status code we received. Reschedule the microdesc downloads as
3186  * appropriate. */
3187 static void
3189  int status_code, const char *dir_id)
3190 {
3191  networkstatus_t *consensus
3193  routerstatus_t *rs;
3194  download_status_t *dls;
3195  time_t now = time(NULL);
3197 
3198  if (! consensus)
3199  return;
3200 
3201  /* We failed to fetch a microdescriptor from 'dir_id', note it down
3202  * so that we don't try the same relay next time... */
3203  microdesc_note_outdated_dirserver(dir_id);
3204 
3205  SMARTLIST_FOREACH_BEGIN(failed, const char *, d) {
3207  if (!rs)
3208  continue;
3209  dls = &rs->dl_status;
3210 
3211  { /* Increment the failure count for this md fetch */
3212  char buf[BASE64_DIGEST256_LEN+1];
3213  digest256_to_base64(buf, d);
3214  log_info(LD_DIR, "Failed to download md %s from %s",
3215  buf, hex_str(dir_id, DIGEST_LEN));
3216  download_status_increment_failure(dls, status_code, buf,
3217  server, now);
3218  }
3219  } SMARTLIST_FOREACH_END(d);
3220 }
compress_method_t detect_compression_method(const char *in, size_t in_len)
Definition: compress.c:284
Header file for dirserv.c.
void rep_hist_note_used_internal(time_t now, int need_uptime, int need_capacity)
#define DIR_PURPOSE_UPLOAD_HSDESC
Definition: directory.h:72
#define DIR_PURPOSE_SERVER
Definition: directory.h:62
#define MAX_DIR_DL_SIZE
Definition: dirclient.c:69
Header file for rendcommon.c.
routerstatus_t * router_get_mutable_consensus_status_by_id(const char *digest)
STATIC int should_use_directory_guards(const or_options_t *options)
Definition: dirclient.c:313
char * authdir_type_to_string(dirinfo_type_t auth)
Definition: directory.c:148
Header file for dirclient.c.
uint16_t dir_port
Definition: routerinfo_st.h:26
void rend_service_desc_has_uploaded(const rend_data_t *rend_data)
Definition: rendservice.c:4048
STATIC void warn_disallowed_anonymous_compression_method(compress_method_t method)
Definition: dirclient.c:3103
void directory_all_unreachable(time_t now)
Definition: mainloop.c:1103
Header for confline.c.
static int handle_response_fetch_detached_signatures(dir_connection_t *, const response_handler_args_t *)
Definition: dirclient.c:2391
Router descriptor structure.
Header file for circuitbuild.c.
dir_indirection_t
Definition: dirclient.h:32
void directory_request_set_dir_addr_port(directory_request_t *req, const tor_addr_port_t *p)
Definition: dirclient.c:997
Definition: node_st.h:33
#define ISO_STREAM
Definition: or.h:974
STATIC const char * dir_conn_purpose_to_string(int purpose)
Definition: dirclient.c:97
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
Header for backtrace.c.
#define TO_CONN(c)
Definition: or.h:735
STATIC char * accept_encoding_header(void)
Definition: dirclient.c:3058
static int directory_conn_is_self_reachability_test(dir_connection_t *conn)
Definition: dirclient.c:705
int32_t networkstatus_get_param(const networkstatus_t *ns, const char *param_name, int32_t default_val, int32_t min_val, int32_t max_val)
void entry_guard_cancel(circuit_guard_state_t **guard_state_p)
Definition: entrynodes.c:2459
char * HTTPProxy
#define MOCK_IMPL(rv, funcname, arglist)
Definition: testsupport.h:133
download_want_authority_t
Definition: or.h:778
char * HTTPProxyAuthenticator
Header file containing client data for the HS subsytem.
Header file for node_select.c.
static int directory_request_set_dir_from_routerstatus(directory_request_t *req)
Definition: dirclient.c:1176
void hs_control_desc_event_received(const hs_ident_dir_conn_t *ident, const char *hsdir_id_digest)
Definition: hs_control.c:89
static int body_is_plausible(const char *body, size_t body_len, int purpose)
Definition: dirclient.c:1778
routerstatus_t * router_get_mutable_consensus_status_by_descriptor_digest(networkstatus_t *consensus, const char *digest)
unsigned int purpose
Definition: connection_st.h:51
Header file for connection.c.
unsigned int has_accepted_serverdesc
Definition: dir_server_st.h:43
static void connection_dir_download_cert_failed(dir_connection_t *conn, int status_code)
Definition: dirclient.c:830
#define LD_GENERAL
Definition: log.h:62
dir_connection_t * dir_connection_new(int socket_family)
Definition: connection.c:360
unsigned int dirconn_direct
const routerstatus_t * router_pick_trusteddirserver(dirinfo_type_t type, int flags)
Definition: node_select.c:995
#define DIR_PURPOSE_FETCH_STATUS_VOTE
Definition: directory.h:50
#define PDS_NO_EXISTING_SERVERDESC_FETCH
Definition: node_select.h:61
Headers for compress.c.
void control_event_boot_dir(bootstrap_status_t status, int progress)
static int tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u)
Definition: address.h:193
#define LOG_INFO
Definition: log.h:45
Header file for describe.c.
int tor_compress_supports_method(compress_method_t method)
Definition: compress.c:304
Header file for nodelist.c.
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition: log.c:628
void tor_addr_make_null(tor_addr_t *a, sa_family_t family)
Definition: address.c:235
int dir_split_resource_into_fingerprint_pairs(const char *res, smartlist_t *pairs_out)
Definition: directory.c:569
rend_service_descriptor_t * parsed
Definition: rendcache.h:34
Header file for directory.c.
int FetchServerDescriptors
void smartlist_add(smartlist_t *sl, void *element)
STATIC int allowed_anonymous_connection_compression_method(compress_method_t method)
Definition: dirclient.c:3081
static int load_downloaded_routers(const char *body, smartlist_t *which, int descriptor_digests, int router_purpose, const char *source)
Definition: dirclient.c:1809
time_t published
Definition: cached_dir_st.h:22
#define SESSION_GROUP_DIRCONN
Definition: or.h:983
#define MAX_HEADERS_SIZE
Definition: or.h:123
Node information structure.
static compress_method_t client_meth_allowed_anonymous_compression[]
Definition: dirclient.c:3049
static void dir_routerdesc_download_failed(smartlist_t *failed, int status_code, int router_purpose, int was_extrainfo, int was_descriptor_digests)
Definition: dirclient.c:3145
#define ED25519_BASE64_LEN
Definition: x25519_sizes.h:40
dirinfo_type_t PublishServerDescriptor_
#define REND_DESC_ID_V2_LEN_BASE32
Definition: or.h:354
struct directory_request_t directory_request_t
Definition: dirclient.h:52
Header file for config.c.
Header file for authcert.c.
char identity_digest[DIGEST_LEN]
char * Socks4Proxy
#define DIR_PURPOSE_UPLOAD_DIR
Definition: directory.h:43
#define HEX_DIGEST256_LEN
Definition: crypto_digest.h:37
void router_set_status(const char *digest, int up)
Definition: nodelist.c:2241
char * alloc_http_authenticator(const char *authenticator)
Definition: connection.c:4691
#define DIR_CONN_STATE_CLIENT_READING
Definition: directory.h:23
const or_options_t * get_options(void)
Definition: config.c:941
int fast_mem_is_zero(const char *mem, size_t len)
Definition: util_string.c:74
#define tor_assert(expr)
Definition: util_bug.h:102
smartlist_t * microdescs_add_to_cache(microdesc_cache_t *cache, const char *s, const char *eos, saved_location_t where, int no_save, time_t listed_at, smartlist_t *requested_digests256)
Definition: microdesc.c:291
Parsed v2 HS descriptor structure.
Header file for microdesc.c.
#define LD_HTTP
Definition: log.h:76
void update_certificate_downloads(time_t now)
void routers_update_all_from_networkstatus(time_t now, int dir_version)
int directory_fetches_from_authorities(const or_options_t *options)
Definition: dirserv.c:77
networkstatus_t * networkstatus_get_latest_consensus_by_flavor(consensus_flavor_t f)
void directory_request_set_directory_id_digest(directory_request_t *req, const char *digest)
Definition: dirclient.c:1007
void networkstatus_consensus_download_failed(int status_code, const char *flavname)
download_status_t * router_get_dl_status_by_descriptor_digest(const char *d)
const routerstatus_t * router_pick_directory_server(dirinfo_type_t type, int flags)
Definition: node_select.c:71
int strcmpstart(const char *s1, const char *s2)
Definition: util_string.c:206
#define tor_free(p)
Definition: malloc.h:52
int connection_fetch_from_buf_http(connection_t *conn, char **headers_out, size_t max_headerlen, char **body_out, size_t *body_used, size_t max_bodylen, int force_complete)
Definition: connection.c:3907
#define DIR_PURPOSE_FETCH_DETACHED_SIGNATURES
Definition: directory.h:53
#define LOG_NOTICE
Definition: log.h:50
void update_microdescs_from_networkstatus(time_t now)
Definition: microdesc.c:1031
int router_supports_extrainfo(const char *identity_digest, int is_authority)
Definition: dirclient.c:179
#define DIR_PURPOSE_FETCH_CONSENSUS
Definition: directory.h:56
Cached large directory object structure.
Header file for mainloop.c.
dirinfo_type_t
Definition: or.h:891
void memwipe(void *mem, uint8_t byte, size_t sz)
Definition: crypto_util.c:57
hs_ident_dir_conn_t * hs_ident_dir_conn_dup(const hs_ident_dir_conn_t *src)
Definition: hs_ident.c:47
#define DIR_CONN_STATE_CLIENT_SENDING
Definition: directory.h:21
pending_vote_t * dirvote_add_vote(const char *vote_body, const char **msg_out, int *status_out)
Definition: dirvote.c:3131
void directory_request_set_resource(directory_request_t *req, const char *resource)
Definition: dirclient.c:1048
smartlist_t * smartlist_new(void)
static int directory_request_dir_contact_info_specified(const directory_request_t *req)
Definition: dirclient.c:1150
void directory_post_to_dirservers(uint8_t dir_purpose, uint8_t router_purpose, dirinfo_type_t type, const char *payload, size_t payload_len, size_t extrainfo_len)
Definition: dirclient.c:233
STATIC int handle_response_fetch_consensus(dir_connection_t *conn, const response_handler_args_t *args)
Definition: dirclient.c:2183
int tor_uncompress(char **out, size_t *out_len, const char *in, size_t in_len, compress_method_t method, int complete_only, int protocol_warn_level)
Definition: compress.c:268
#define DIR_PURPOSE_UPLOAD_SIGNATURES
Definition: directory.h:47
dir_server_t * router_get_trusteddirserver_by_digest(const char *digest)
Definition: dirlist.c:112
time_t download_status_increment_failure(download_status_t *dls, int status_code, const char *item, int server, time_t now)
Definition: dlstatus.c:250
#define STATIC
Definition: testsupport.h:32
static void directory_send_command(dir_connection_t *conn, const int direct, const directory_request_t *req)
Definition: dirclient.c:1531
void hs_control_desc_event_failed(const hs_ident_dir_conn_t *ident, const char *hsdir_id_digest, const char *reason)
Definition: hs_control.c:65
#define ENTRY_TO_CONN(c)
Definition: or.h:738
int trusted_dirs_load_certs_from_string(const char *contents, int source, int flush, const char *source_dir)
Definition: authcert.c:373
#define DIR_PURPOSE_FETCH_EXTRAINFO
Definition: directory.h:41
Header file for routerset.c.
Header file for directory authority mode.
void hs_client_desc_has_arrived(const hs_ident_dir_conn_t *ident)
Definition: hs_client.c:1693
int networkstatus_parse_flavor_name(const char *flavname)
static int handle_response_fetch_desc(dir_connection_t *, const response_handler_args_t *)
Definition: dirclient.c:2424
#define PDS_IGNORE_FASCISTFIREWALL
Definition: node_select.h:54
directory_request_t * directory_request_new(uint8_t dir_purpose)
Definition: dirclient.c:951
int hs_cache_store_as_client(const char *desc_str, const ed25519_public_key_t *identity_pk)
Definition: hs_cache.c:775
Trusted/fallback directory server structure.
void hs_control_desc_event_content(const hs_ident_dir_conn_t *ident, const char *hsdir_id_digest, const char *body)
Definition: hs_control.c:178
#define DIGEST256_LEN
Definition: digest_sizes.h:23
static char * directory_get_consensus_url(const char *resource)
Definition: dirclient.c:1468
Header file for policies.c.
int count_loading_descriptors_progress(void)
Definition: nodelist.c:2641
Definition: rendcache.h:29
void entry_guard_failed(circuit_guard_state_t **guard_state_p)
Definition: entrynodes.c:2480
void control_event_hs_descriptor_uploaded(const char *id_digest, const char *onion_address)
#define DIR_CONN_STATE_CLIENT_FINISHED
Definition: directory.h:25
size_t dir_len
Definition: cached_dir_st.h:20
static int dirind_is_anon(dir_indirection_t ind)
Definition: dirclient.c:609
Common functions for cryptographic routines.
void directory_info_has_arrived(time_t now, int from_cache, int suppress_logs)
Definition: mainloop.c:1121
static compress_method_t client_meth_pref[]
Definition: dirclient.c:3039
const char * compression_method_get_name(compress_method_t method)
Definition: compress.c:364
#define DIR_PURPOSE_FETCH_SERVERDESC
Definition: directory.h:38
void rend_client_desc_trynow(const char *query)
Definition: rendclient.c:915
#define tor_addr_from_ipv4h(dest, v4addr)
Definition: address.h:287
guard_usable_t entry_guard_succeeded(circuit_guard_state_t **guard_state_p)
Definition: entrynodes.c:2432
#define DIR_PURPOSE_UPLOAD_VOTE
Definition: directory.h:45
Header for crypto_format.c.
void fascist_firewall_choose_address_rs(const routerstatus_t *rs, firewall_connection_t fw_connection, int pref_only, tor_addr_port_t *ap)
Definition: policies.c:985
Header file for routermode.c.
void router_new_address_suggestion(const char *suggestion, const dir_connection_t *d_conn)
Definition: router.c:2567
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
void connection_dir_client_request_failed(dir_connection_t *conn)
Definition: dirclient.c:724
static int handle_response_fetch_renddesc_v2(dir_connection_t *, const response_handler_args_t *)
Definition: dirclient.c:2800
void directory_get_from_all_authorities(uint8_t dir_purpose, uint8_t router_purpose, const char *resource)
Definition: dirclient.c:584
#define DIGEST_LEN
Definition: digest_sizes.h:20
void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern,...)
Definition: smartlist.c:36
int ed25519_public_key_is_zero(const ed25519_public_key_t *pubkey)
Header file for rendcache.c.
Header file for circuitbuild.c.
void hs_control_desc_event_uploaded(const hs_ident_dir_conn_t *ident, const char *hsdir_id_digest)
Definition: hs_control.c:159
Master header file for Tor-specific functionality.
signed_descriptor_t * router_get_by_extrainfo_digest(const char *digest)
Definition: routerlist.c:711
Header for consdiff.c.
int purpose_needs_anonymity(uint8_t dir_purpose, uint8_t router_purpose, const char *resource)
Definition: directory.c:98
void connection_close_immediate(connection_t *conn)
Definition: connection.c:844
const char * hex_str(const char *from, size_t fromlen)
Definition: binascii.c:34
void control_event_hs_descriptor_content(const char *onion_address, const char *desc_id, const char *hsdir_id_digest, const char *content)
const char * router_get_descriptor_gen_reason(void)
Definition: router.c:1698
download_status_t ei_dl_status
#define DIR_PURPOSE_FETCH_HSDESC
Definition: directory.h:74
#define DIR_PURPOSE_FETCH_CERTIFICATE
Definition: directory.h:59
#define ALLOW_DIRECTORY_TIME_SKEW
Definition: dirclient.c:73
const char * data
Definition: mmap.h:26
const node_t * node_get_by_id(const char *identity_digest)
Definition: nodelist.c:223
void authority_cert_dl_failed(const char *id_digest, const char *signing_key_digest, int status)
Definition: authcert.c:683
int FetchDirInfoExtraEarly
const char * fmt_addr32(uint32_t addr)
Definition: address.c:1181
#define ROUTER_PURPOSE_BRIDGE
int rend_valid_v2_service_id(const char *query)
Definition: rendcommon.c:719
#define ROUTER_PURPOSE_GENERAL
#define LOG_WARN
Definition: log.h:53
Entry connection structure.
int tor_addr_is_null(const tor_addr_t *addr)
Definition: address.c:770
char * Socks5Proxy
static int handle_response_upload_renddesc_v2(dir_connection_t *, const response_handler_args_t *)
Definition: dirclient.c:2896
static void connection_dir_download_routerdesc_failed(dir_connection_t *conn)
Definition: dirclient.c:787
const char * routerstatus_describe(const routerstatus_t *rs)
Definition: describe.c:184
rend_data_t * rend_data
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:478
int rend_cache_store_v2_desc_as_client(const char *desc, const char *desc_id_base32, const rend_data_t *rend_query, rend_cache_entry_t **entry)
Definition: rendcache.c:828
int fascist_firewall_allows_address_addr(const tor_addr_t *addr, uint16_t port, firewall_connection_t fw_connection, int pref_only, int pref_ipv6)
Definition: policies.c:557
int parse_http_response(const char *headers, int *code, time_t *date, compress_method_t *compression, char **reason)
Definition: directory.c:348
#define DIR_PURPOSE_IS_UPLOAD(p)
Definition: directory.h:82
int connection_connect(connection_t *conn, const char *address, const tor_addr_t *addr, uint16_t port, int *socket_error)
Definition: connection.c:2185
void directory_request_free_(directory_request_t *req)
Definition: dirclient.c:973
#define LD_REND
Definition: log.h:84
Header file for rendservice.c.
struct circuit_guard_state_t * guard_state
void retry_bridge_descriptor_fetch_directly(const char *digest)
Definition: bridges.c:709
rend_data_t * rend_data_dup(const rend_data_t *data)
Definition: hs_common.c:385
void control_event_hsv2_descriptor_received(const char *onion_address, const rend_data_t *rend_data, const char *hsdir_id_digest)
int tor_digest_is_zero(const char *digest)
Definition: util_string.c:96
void digest256_to_base64(char *d64, const char *digest)
static void connection_dir_retry_bridges(smartlist_t *descs)
Definition: dirclient.c:769
void directory_request_fetch_set_hs_ident(directory_request_t *req, const hs_ident_dir_conn_t *ident)
Definition: dirclient.c:1128
int directories_have_accepted_server_descriptor(void)
Definition: dirclient.c:202
Client/server directory connection structure.
void connection_dir_client_refetch_hsdesc_if_needed(dir_connection_t *dir_conn)
Definition: dirclient.c:3014
unsigned int type
Definition: connection_st.h:50
void directory_get_from_dirserver(uint8_t dir_purpose, uint8_t router_purpose, const char *resource, int pds_flags, download_want_authority_t want_authority)
Definition: dirclient.c:452
void format_rfc1123_time(char *buf, time_t t)
Definition: time_fmt.c:182
#define LD_DIR
Definition: log.h:88
entry_connection_t * connection_ap_make_link(connection_t *partner, char *address, uint16_t port, const char *digest, int session_group, int isolation_flags, int use_begindir, int want_onehop)
char * http_get_header(const char *headers, const char *which)
Definition: directory.c:313
static void dir_consensus_request_set_additional_headers(directory_request_t *req, const char *resource)
Definition: dirclient.c:368
void directory_request_set_guard_state(directory_request_t *req, circuit_guard_state_t *state)
Definition: dirclient.c:1140
void config_line_prepend(config_line_t **lst, const char *key, const char *val)
Definition: confline.c:53
Header file for connection_edge.c.
#define REND_SERVICE_ID_LEN_BASE32
Definition: or.h:331
unsigned int caches_extra_info
Definition: routerinfo_st.h:72
Header file for fp_pair.c.
static int compare_strs_(const void **a, const void **b)
Definition: dirclient.c:1448
static int handle_response_fetch_certificate(dir_connection_t *, const response_handler_args_t *)
Definition: dirclient.c:2295
Header file containing control port event related code.
cached_dir_t * dirserv_get_consensus(const char *flavor_name)
Definition: dirserv.c:263
int tor_snprintf(char *str, size_t size, const char *format,...)
Definition: printf.c:27
void sr_act_post_consensus(const networkstatus_t *consensus)
char * tor_addr_to_str_dup(const tor_addr_t *addr)
Definition: address.c:1134
void format_iso_time(char *buf, time_t t)
Definition: time_fmt.c:295
const routerinfo_t * router_get_my_routerinfo(void)
Definition: router.c:1623
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out)
Definition: smartlist.c:279
static void dir_microdesc_download_failed(smartlist_t *failed, int status_code, const char *dir_id)
Definition: dirclient.c:3188
const char * rend_data_get_address(const rend_data_t *rend_data)
Definition: hs_common.c:527
void router_dirport_found_reachable(void)
Definition: selftest.c:247
microdesc_cache_t * get_microdesc_cache(void)
Definition: microdesc.c:249
void clock_skew_warning(const connection_t *conn, long apparent_skew, int trusted, log_domain_mask_t domain, const char *received, const char *source)
Definition: connection.c:5549
Header file for dirvote.c.
#define SMARTLIST_FOREACH(sl, type, var, cmd)
void connection_start_reading(connection_t *conn)
Definition: mainloop.c:632
time_t last_dir_503_at
void directory_request_set_router_purpose(directory_request_t *req, uint8_t router_purpose)
Definition: dirclient.c:1019
dir_server_t * router_get_fallback_dirserver_by_digest(const char *digest)
Definition: dirlist.c:133
int networkstatus_set_current_consensus(const char *consensus, size_t consensus_len, const char *flavor, unsigned flags, const char *source_dir)
const char * escaped(const char *s)
Definition: escape.c:126
uint16_t port
#define DIR_PURPOSE_UPLOAD_RENDDESC_V2
Definition: directory.h:65
static int handle_response_upload_signatures(dir_connection_t *, const response_handler_args_t *)
Definition: dirclient.c:2686
static int directory_command_should_use_begindir(const or_options_t *options, const directory_request_t *req, const char **reason)
Definition: dirclient.c:896
int hs_client_refetch_hsdesc(const ed25519_public_key_t *identity_pk)
Definition: hs_client.c:1348
#define ISO_SESSIONGRP
Definition: or.h:970
char identity_digest[DIGEST_LEN]
Header file for dlstatus.c.
#define fmt_addr(a)
Definition: address.h:211
void directory_initiate_request(directory_request_t *request)
Definition: dirclient.c:1238
#define ARRAY_LENGTH(x)
const char * router_purpose_to_string(uint8_t p)
Definition: routerinfo.c:59
#define log_fn(severity, domain, args,...)
Definition: log.h:287
int rend_get_service_id(crypto_pk_t *pk, char *out)
Definition: rendcommon.c:706
int dir_split_resource_into_fingerprints(const char *resource, smartlist_t *fp_out, int *compressed_out, int flags)
Definition: directory.c:628
int router_digest_is_me(const char *digest)
Definition: router.c:1586
#define DIR_PURPOSE_HAS_FETCHED_RENDDESC_V2
Definition: directory.h:35
int fascist_firewall_allows_dir_server(const dir_server_t *ds, firewall_connection_t fw_connection, int pref_only)
Definition: policies.c:771
#define DIR_PURPOSE_FETCH_MICRODESC
Definition: directory.h:70
uint8_t digest_sha3_as_signed[DIGEST256_LEN]
Definition: cached_dir_st.h:25
Definition: or.h:894
time_t approx_time(void)
Definition: approx_time.c:32
static const routerstatus_t * directory_pick_generic_dirserver(dirinfo_type_t type, int pds_flags, uint8_t dir_purpose, circuit_guard_state_t **guard_state_out)
Definition: dirclient.c:334
void fascist_firewall_choose_address_node(const node_t *node, firewall_connection_t fw_connection, int pref_only, tor_addr_port_t *ap)
Definition: policies.c:1100
#define DIR_PURPOSE_HAS_FETCHED_HSDESC
Definition: directory.h:77
#define LOG_DEBUG
Definition: log.h:42
void directory_request_set_indirection(directory_request_t *req, dir_indirection_t indirection)
Definition: dirclient.c:1035
#define BASE64_DIGEST256_LEN
Definition: crypto_digest.h:29
Header file for dirlist.c.
Header file for hs_cache.c.
#define PDS_NO_EXISTING_MICRODESC_FETCH
Definition: node_select.h:67
char * consensus_diff_apply(const char *consensus, size_t consensus_len, const char *diff, size_t diff_len)
Definition: consdiff.c:1381
void directory_request_add_header(directory_request_t *req, const char *key, const char *val)
Definition: dirclient.c:1087
int connection_dir_reached_eof(dir_connection_t *conn)
Definition: dirclient.c:2993
static int handle_response_upload_dir(dir_connection_t *, const response_handler_args_t *)
Definition: dirclient.c:2582
int public_server_mode(const or_options_t *options)
Definition: routermode.c:43
void rep_hist_note_used_port(time_t now, uint16_t port)
int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:506
size_t size
Definition: mmap.h:27
Header file for selftest.c.
tor_addr_t addr
#define CONN_TYPE_DIR
Definition: connection.h:35
uint32_t addr
Definition: routerinfo_st.h:24
routerstatus_t fake_status
Definition: dir_server_st.h:51
static int handle_response_fetch_status_vote(dir_connection_t *, const response_handler_args_t *)
Definition: dirclient.c:2355
STATIC dirinfo_type_t dir_fetch_type(int dir_purpose, int router_purpose, const char *resource)
Definition: dirclient.c:137
compress_method_t
Definition: compress.h:21
int entry_list_is_constrained(const or_options_t *options)
Definition: entrynodes.c:3274
int control_event_server_status(int severity, const char *format,...)
smartlist_t * connection_dir_list_by_purpose_and_resource(int purpose, const char *resource)
Definition: connection.c:4564
#define LD_NET
Definition: log.h:66
STATIC int handle_response_fetch_hsdesc_v3(dir_connection_t *conn, const response_handler_args_t *args)
Definition: dirclient.c:2723
void connection_watch_events(connection_t *conn, watchable_events_t events)
Definition: mainloop.c:494
int looks_like_a_consensus_diff(const char *document, size_t len)
Definition: consdiff.c:1416
void directory_request_set_rend_query(directory_request_t *req, const rend_data_t *query)
Definition: dirclient.c:1099
static int handle_response_upload_vote(dir_connection_t *, const response_handler_args_t *)
Definition: dirclient.c:2650
int routerset_contains_routerstatus(const routerset_t *set, const routerstatus_t *rs, country_t country)
Definition: routerset.c:322
static int connection_dir_client_reached_eof(dir_connection_t *conn)
Definition: dirclient.c:1986
#define DIR_CONN_STATE_CONNECTING
Definition: directory.h:19
void directory_request_upload_set_hs_ident(directory_request_t *req, const hs_ident_dir_conn_t *ident)
Definition: dirclient.c:1114
int dirvote_add_signatures(const char *detached_signatures_body, const char *source, const char **msg)
Definition: dirvote.c:3634
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
Definition: address.c:904
void directory_request_set_payload(directory_request_t *req, const char *payload, size_t payload_len)
Definition: dirclient.c:1059
tor_addr_t HTTPProxyAddr
Header file for routerinfo.c.
Header file for rendclient.c.
Header file for control_events.c.
#define LD_PROTOCOL
Definition: log.h:72
void directory_request_set_routerstatus(directory_request_t *req, const routerstatus_t *status)
Definition: dirclient.c:1165
const char * compression_method_get_human_name(compress_method_t method)
Definition: compress.c:390
void smartlist_sort(smartlist_t *sl, int(*compare)(const void **a, const void **b))
Definition: smartlist.c:334
void rend_client_refetch_v2_renddesc(rend_data_t *rend_query)
Definition: rendclient.c:709
STATIC int handle_response_fetch_microdesc(dir_connection_t *conn, const response_handler_args_t *args)
Definition: dirclient.c:2523
ed25519_public_key_t identity_pk
Definition: hs_ident.h:90
#define DIR_PURPOSE_FETCH_RENDDESC_V2
Definition: directory.h:68
static void connection_dir_bridge_routerdesc_failed(dir_connection_t *conn)
Definition: dirclient.c:806
This file contains ABI/API of the shared random protocol defined in proposal #250....
time_t timestamp_last_write_allowed
static void copy_ipv6_address(char *destination, const char *source, size_t len, int decorate)
Definition: dirclient.c:1514
uint8_t state
Definition: connection_st.h:49
const node_t * guards_choose_dirguard(uint8_t dir_purpose, circuit_guard_state_t **guard_state_out)
Definition: entrynodes.c:3689
void directory_request_set_if_modified_since(directory_request_t *req, time_t if_modified_since)
Definition: dirclient.c:1073
Header file for networkstatus.c.
uint16_t HTTPProxyPort
#define LD_BUG
Definition: log.h:86
Header file for predict_ports.c.
void directory_request_set_or_addr_port(directory_request_t *req, const tor_addr_port_t *p)
Definition: dirclient.c:986
Header file for routerlist.c.
int FetchUselessDescriptors
int router_load_routers_from_string(const char *s, const char *eos, saved_location_t saved_location, smartlist_t *requested_fingerprints, int descriptor_digests, const char *prepend_annotations)
Definition: routerlist.c:2026
static sa_family_t tor_addr_family(const tor_addr_t *a)
Definition: address.h:179
const routerstatus_t * router_pick_fallback_dirserver(dirinfo_type_t type, int flags)
Definition: node_select.c:1006
routerset_t * ExcludeNodes
Definition: or_options_st.h:81
Networkstatus consensus/vote structure.
static int handle_response_upload_hsdesc(dir_connection_t *, const response_handler_args_t *)
Definition: dirclient.c:2947
tor_mmap_t * networkstatus_map_cached_consensus(const char *flavorname)
void router_load_extrainfo_from_string(const char *s, const char *eos, saved_location_t saved_location, smartlist_t *requested_fingerprints, int descriptor_digests)
Definition: routerlist.c:2125