Tor  0.4.5.0-alpha-dev
dirlist.c
Go to the documentation of this file.
1 /* Copyright (c) 2001 Matej Pfajfar.
2  * Copyright (c) 2001-2004, Roger Dingledine.
3  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4  * Copyright (c) 2007-2020, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
7 /**
8  * \file dirlist.c
9  * \brief Code to maintain our lists of directory authorities and
10  * fallback directories.
11  *
12  * For the directory authorities, we have a list containing the public
13  * identity key, and contact points, for each authority. The
14  * authorities receive descriptors from relays, and publish consensuses,
15  * descriptors, and microdescriptors. This list is pre-configured.
16  *
17  * Fallback directories are well-known, stable, but untrusted directory
18  * caches that clients which have not yet bootstrapped can use to get
19  * their first networkstatus consensus, in order to find out where the
20  * Tor network really is. This list is pre-configured in
21  * fallback_dirs.inc. Every authority also serves as a fallback.
22  *
23  * Both fallback directories and directory authorities are are
24  * represented by a dir_server_t.
25  */
26 
27 #include "core/or/or.h"
28 
29 #include "app/config/config.h"
31 #include "core/or/policies.h"
40 #include "feature/relay/router.h"
41 #include "lib/net/resolve.h"
42 
45 
46 /** Global list of a dir_server_t object for each directory
47  * authority. */
49 /** Global list of dir_server_t objects for all directory authorities
50  * and all fallback directory servers. */
52 
53 /** Helper: From a given trusted directory entry, add the v4 or/and v6 address
54  * to the nodelist address set. */
55 static void
57 {
58  tor_assert(dir);
60 
61  /* Add IPv4 and then IPv6 if applicable. */
62  nodelist_add_addr_to_address_set(&dir->ipv4_addr);
63  if (!tor_addr_is_null(&dir->ipv6_addr)) {
65  }
66 }
67 
68 /** Go over the trusted directory server list and add their address(es) to the
69  * nodelist address set. This is called everytime a new consensus is set. */
70 MOCK_IMPL(void,
72 {
73  if (!trusted_dir_servers) {
74  return;
75  }
76 
78  if (ent->is_authority) {
80  }
81  } SMARTLIST_FOREACH_END(ent);
82 }
83 
84 /** Return the number of directory authorities whose type matches some bit set
85  * in <b>type</b> */
86 int
88 {
89  int n = 0;
91  return 0;
93  if (ds->type & type)
94  ++n);
95  return n;
96 }
97 
98 /** Return a smartlist containing a list of dir_server_t * for all
99  * known trusted dirservers. Callers must not modify the list or its
100  * contents.
101  */
102 smartlist_t *
104 {
105  if (!trusted_dir_servers)
107 
108  return trusted_dir_servers;
109 }
110 
111 smartlist_t *
112 router_get_fallback_dir_servers_mutable(void)
113 {
116 
117  return fallback_dir_servers;
118 }
119 
120 const smartlist_t *
121 router_get_trusted_dir_servers(void)
122 {
124 }
125 
126 const smartlist_t *
127 router_get_fallback_dir_servers(void)
128 {
129  return router_get_fallback_dir_servers_mutable();
130 }
131 
132 /** Reset all internal variables used to count failed downloads of network
133  * status objects. */
134 void
136 {
138 }
139 
140 /** Return the dir_server_t for the directory authority whose identity
141  * key hashes to <b>digest</b>, or NULL if no such authority is known.
142  */
143 dir_server_t *
145 {
146  if (!trusted_dir_servers)
147  return NULL;
148 
150  {
151  if (tor_memeq(ds->digest, digest, DIGEST_LEN))
152  return ds;
153  });
154 
155  return NULL;
156 }
157 
158 /** Return the dir_server_t for the fallback dirserver whose identity
159  * key hashes to <b>digest</b>, or NULL if no such fallback is in the list of
160  * fallback_dir_servers. (fallback_dir_servers is affected by the FallbackDir
161  * and UseDefaultFallbackDirs torrc options.)
162  * The list of fallback directories includes the list of authorities.
163  */
164 dir_server_t *
166 {
168  return NULL;
169 
170  if (!digest)
171  return NULL;
172 
174  {
175  if (tor_memeq(ds->digest, digest, DIGEST_LEN))
176  return ds;
177  });
178 
179  return NULL;
180 }
181 
182 /** Return 1 if any fallback dirserver's identity key hashes to <b>digest</b>,
183  * or 0 if no such fallback is in the list of fallback_dir_servers.
184  * (fallback_dir_servers is affected by the FallbackDir and
185  * UseDefaultFallbackDirs torrc options.)
186  * The list of fallback directories includes the list of authorities.
187  */
188 int
189 router_digest_is_fallback_dir(const char *digest)
190 {
191  return (router_get_fallback_dirserver_by_digest(digest) != NULL);
192 }
193 
194 /** Return the dir_server_t for the directory authority whose
195  * v3 identity key hashes to <b>digest</b>, or NULL if no such authority
196  * is known.
197  */
200 {
201  if (!trusted_dir_servers)
202  return NULL;
203 
205  {
206  if (tor_memeq(ds->v3_identity_digest, digest, DIGEST_LEN) &&
207  (ds->type & V3_DIRINFO))
208  return ds;
209  });
210 
211  return NULL;
212 }
213 
214 /** Mark as running every dir_server_t in <b>server_list</b>. */
215 void
217 {
218  if (server_list) {
219  SMARTLIST_FOREACH_BEGIN(server_list, dir_server_t *, dir) {
220  routerstatus_t *rs;
221  node_t *node;
222  dir->is_running = 1;
223  node = node_get_mutable_by_id(dir->digest);
224  if (node)
225  node->is_running = 1;
227  if (rs) {
228  rs->last_dir_503_at = 0;
230  }
231  } SMARTLIST_FOREACH_END(dir);
232  }
234 }
235 
236 /** Return true iff <b>digest</b> is the digest of the identity key of a
237  * trusted directory matching at least one bit of <b>type</b>. If <b>type</b>
238  * is zero (NO_DIRINFO), or ALL_DIRINFO, any authority is okay. */
239 int
241 {
242  if (!trusted_dir_servers)
243  return 0;
244  if (authdir_mode(get_options()) && router_digest_is_me(digest))
245  return 1;
247  if (tor_memeq(digest, ent->digest, DIGEST_LEN)) {
248  return (!type) || ((type & ent->type) != 0);
249  });
250  return 0;
251 }
252 
253 /** Return true iff the given address matches a trusted directory that matches
254  * at least one bit of type.
255  *
256  * If type is NO_DIRINFO or ALL_DIRINFO, any authority is matched. */
257 bool
259 {
260  int family = tor_addr_family(addr);
261 
262  if (!trusted_dir_servers) {
263  return false;
264  }
265 
267  /* Ignore entries that don't match the given type. */
268  if (type != NO_DIRINFO && (type & ent->type) == 0) {
269  continue;
270  }
271  /* Match IPv4 or IPv6 address. */
272  if ((family == AF_INET && tor_addr_eq(addr, &ent->ipv4_addr)) ||
273  (family == AF_INET6 && tor_addr_eq(addr, &ent->ipv6_addr))) {
274  return true;
275  }
276  } SMARTLIST_FOREACH_END(ent);
277 
278  return false;
279 }
280 
281 /** Create a directory server at <b>address</b>:<b>port</b>, with OR identity
282  * key <b>digest</b> which has DIGEST_LEN bytes. If <b>address</b> is NULL,
283  * add ourself. If <b>is_authority</b>, this is a directory authority. Return
284  * the new directory server entry on success or NULL on failure. */
285 static dir_server_t *
287  const char *nickname,
288  const tor_addr_t *ipv4_addr,
289  const char *hostname,
290  uint16_t ipv4_dirport, uint16_t ipv4_orport,
291  const tor_addr_port_t *addrport_ipv6,
292  const char *digest, const char *v3_auth_digest,
293  dirinfo_type_t type,
294  double weight)
295 {
296  dir_server_t *ent;
297  char *hostname_ = NULL;
298 
299  tor_assert(digest);
300 
301  if (weight < 0)
302  return NULL;
303 
304  if (!ipv4_addr) {
305  return NULL;
306  }
307 
308  if (!hostname)
309  hostname_ = tor_addr_to_str_dup(ipv4_addr);
310  else
311  hostname_ = tor_strdup(hostname);
312 
313  ent = tor_malloc_zero(sizeof(dir_server_t));
314  ent->nickname = nickname ? tor_strdup(nickname) : NULL;
315  ent->address = hostname_;
316  tor_addr_copy(&ent->ipv4_addr, ipv4_addr);
317  ent->ipv4_dirport = ipv4_dirport;
318  ent->ipv4_orport = ipv4_orport;
319  ent->is_running = 1;
320  ent->is_authority = is_authority;
321  ent->type = type;
322  ent->weight = weight;
323  if (addrport_ipv6 && tor_addr_port_is_valid_ap(addrport_ipv6, 0)) {
324  if (tor_addr_family(&addrport_ipv6->addr) != AF_INET6) {
325  log_warn(LD_BUG, "Hey, I got a non-ipv6 addr as addrport_ipv6.");
327  } else {
328  tor_addr_copy(&ent->ipv6_addr, &addrport_ipv6->addr);
329  ent->ipv6_orport = addrport_ipv6->port;
330  }
331  } else {
333  }
334 
335  memcpy(ent->digest, digest, DIGEST_LEN);
336  if (v3_auth_digest && (type & V3_DIRINFO))
337  memcpy(ent->v3_identity_digest, v3_auth_digest, DIGEST_LEN);
338 
339  if (nickname)
340  tor_asprintf(&ent->description, "directory server \"%s\" at %s:%" PRIu16,
341  nickname, hostname_, ipv4_dirport);
342  else
343  tor_asprintf(&ent->description, "directory server at %s:%" PRIu16,
344  hostname_, ipv4_dirport);
345 
346  tor_addr_copy(&ent->fake_status.ipv4_addr, &ent->ipv4_addr);
348  memcpy(ent->fake_status.identity_digest, digest, DIGEST_LEN);
349  if (nickname)
350  strlcpy(ent->fake_status.nickname, nickname,
351  sizeof(ent->fake_status.nickname));
352  else
353  ent->fake_status.nickname[0] = '\0';
355  ent->fake_status.ipv4_orport = ent->ipv4_orport;
356  ent->fake_status.ipv6_orport = ent->ipv6_orport;
357 
358  return ent;
359 }
360 
361 /** Create an authoritative directory server at <b>address</b>:<b>port</b>,
362  * with identity key <b>digest</b>. If <b>ipv4_addr_str</b> is NULL, add
363  * ourself. Return the new trusted directory server entry on success or NULL
364  * if we couldn't add it. */
365 dir_server_t *
366 trusted_dir_server_new(const char *nickname, const char *address,
367  uint16_t ipv4_dirport, uint16_t ipv4_orport,
368  const tor_addr_port_t *ipv6_addrport,
369  const char *digest, const char *v3_auth_digest,
370  dirinfo_type_t type, double weight)
371 {
372  tor_addr_t ipv4_addr;
373  char *hostname=NULL;
374  dir_server_t *result;
375 
376  if (!address) { /* The address is us; we should guess. */
377  if (!find_my_address(get_options(), AF_INET, LOG_WARN, &ipv4_addr,
378  NULL, &hostname)) {
379  log_warn(LD_CONFIG,
380  "Couldn't find a suitable address when adding ourself as a "
381  "trusted directory server.");
382  return NULL;
383  }
384  if (!hostname)
385  hostname = tor_addr_to_str_dup(&ipv4_addr);
386 
387  if (!hostname)
388  return NULL;
389  } else {
390  if (tor_addr_lookup(address, AF_INET, &ipv4_addr)) {
391  log_warn(LD_CONFIG,
392  "Unable to lookup address for directory server at '%s'",
393  address);
394  return NULL;
395  }
396  hostname = tor_strdup(address);
397  }
398 
399  result = dir_server_new(1, nickname, &ipv4_addr, hostname,
401  ipv6_addrport,
402  digest,
403  v3_auth_digest, type, weight);
404  tor_free(hostname);
405  return result;
406 }
407 
408 /** Return a new dir_server_t for a fallback directory server at
409  * <b>addr</b>:<b>or_port</b>/<b>dir_port</b>, with identity key digest
410  * <b>id_digest</b> */
411 dir_server_t *
413  uint16_t ipv4_dirport, uint16_t ipv4_orport,
414  const tor_addr_port_t *addrport_ipv6,
415  const char *id_digest, double weight)
416 {
417  return dir_server_new(0, NULL, ipv4_addr, NULL, ipv4_dirport, ipv4_orport,
418  addrport_ipv6, id_digest, NULL, ALL_DIRINFO, weight);
419 }
420 
421 /** Add a directory server to the global list(s). */
422 void
424 {
425  if (!trusted_dir_servers)
429 
430  if (ent->is_authority)
432 
435 }
436 
437 #define dir_server_free(val) \
438  FREE_AND_NULL(dir_server_t, dir_server_free_, (val))
439 
440 /** Free storage held in <b>ds</b>. */
441 static void
443 {
444  if (!ds)
445  return;
446 
447  tor_free(ds->nickname);
448  tor_free(ds->description);
449  tor_free(ds->address);
450  tor_free(ds);
451 }
452 
453 /** Remove all members from the list of dir servers. */
454 void
456 {
457  if (fallback_dir_servers) {
459  dir_server_free(ent));
461  } else {
463  }
464  if (trusted_dir_servers) {
466  } else {
468  }
470 }
471 
472 void
473 dirlist_free_all(void)
474 {
476  smartlist_free(trusted_dir_servers);
477  smartlist_free(fallback_dir_servers);
479 }
routerstatus_t::ipv4_dirport
uint16_t ipv4_dirport
Definition: routerstatus_st.h:34
tor_free
#define tor_free(p)
Definition: malloc.h:52
dir_server_st.h
Trusted/fallback directory server structure.
routerstatus_t::is_authority
unsigned int is_authority
Definition: routerstatus_st.h:37
tor_addr_family
static sa_family_t tor_addr_family(const tor_addr_t *a)
Definition: address.h:187
tor_addr_t
Definition: address.h:69
MOCK_IMPL
#define MOCK_IMPL(rv, funcname, arglist)
Definition: testsupport.h:133
tor_assert
#define tor_assert(expr)
Definition: util_bug.h:102
LD_BUG
#define LD_BUG
Definition: log.h:86
router.h
Header file for router.c.
fallback_dir_servers
static smartlist_t * fallback_dir_servers
Definition: dirlist.c:51
router_digest_is_me
int router_digest_is_me(const char *digest)
Definition: router.c:1698
dir_server_t::v3_identity_digest
char v3_identity_digest[DIGEST_LEN]
Definition: dir_server_st.h:34
tor_addr_lookup
int tor_addr_lookup(const char *name, uint16_t family, tor_addr_t *addr)
Definition: resolve.c:190
router_addr_is_trusted_dir_type
bool router_addr_is_trusted_dir_type(const tor_addr_t *addr, dirinfo_type_t type)
Definition: dirlist.c:258
smartlist_add
void smartlist_add(smartlist_t *sl, void *element)
Definition: smartlist_core.c:117
router_digest_is_fallback_dir
int router_digest_is_fallback_dir(const char *digest)
Definition: dirlist.c:189
routerstatus_t
Definition: routerstatus_st.h:19
routerstatus_t::nickname
char nickname[MAX_NICKNAME_LEN+1]
Definition: routerstatus_st.h:25
find_my_address
bool find_my_address(const or_options_t *options, int family, int warn_severity, tor_addr_t *addr_out, resolved_addr_method_t *method_out, char **hostname_out)
Attempt to find our IP address that can be used as our external reachable address.
Definition: resolve_addr.c:695
smartlist_new
smartlist_t * smartlist_new(void)
Definition: smartlist_core.c:26
tor_addr_make_unspec
void tor_addr_make_unspec(tor_addr_t *a)
Definition: address.c:225
trusted_dir_server_new
dir_server_t * trusted_dir_server_new(const char *nickname, const char *address, uint16_t ipv4_dirport, uint16_t ipv4_orport, const tor_addr_port_t *ipv6_addrport, const char *digest, const char *v3_auth_digest, dirinfo_type_t type, double weight)
Definition: dirlist.c:366
SMARTLIST_FOREACH
#define SMARTLIST_FOREACH(sl, type, var, cmd)
Definition: smartlist_foreach.h:112
router_get_trusteddirserver_by_digest
dir_server_t * router_get_trusteddirserver_by_digest(const char *digest)
Definition: dirlist.c:144
networkstatus.h
Header file for networkstatus.c.
tor_memeq
int tor_memeq(const void *a, const void *b, size_t sz)
Definition: di_ops.c:107
dirlist_add_trusted_dir_addresses
void dirlist_add_trusted_dir_addresses(void)
Definition: dirlist.c:71
dirlist.h
Header file for dirlist.c.
get_n_authorities
int get_n_authorities(dirinfo_type_t type)
Definition: dirlist.c:87
DIGEST_LEN
#define DIGEST_LEN
Definition: digest_sizes.h:20
nodelist_add_addr_to_address_set
void nodelist_add_addr_to_address_set(const tor_addr_t *addr)
Definition: nodelist.c:476
dir_server_free_
static void dir_server_free_(dir_server_t *ds)
Definition: dirlist.c:442
node_get_mutable_by_id
node_t * node_get_mutable_by_id(const char *identity_digest)
Definition: nodelist.c:194
resolve.h
Header for resolve.c.
tor_addr_port_t
Definition: address.h:81
routerstatus_t::ipv4_orport
uint16_t ipv4_orport
Definition: routerstatus_st.h:33
dir_server_t::ipv4_orport
uint16_t ipv4_orport
Definition: dir_server_st.h:29
node_t
Definition: node_st.h:34
dir_server_t::is_authority
unsigned int is_authority
Definition: dir_server_st.h:38
directory.h
Header file for directory.c.
trusted_dir_servers
static smartlist_t * trusted_dir_servers
Definition: dirlist.c:48
router_digest_is_trusted_dir_type
int router_digest_is_trusted_dir_type(const char *digest, dirinfo_type_t type)
Definition: dirlist.c:240
authmode.h
Header file for directory authority mode.
nodelist.h
Header file for nodelist.c.
routerlist.h
Header file for routerlist.c.
dir_server_t::fake_status
routerstatus_t fake_status
Definition: dir_server_st.h:51
routerset.h
Header file for routerset.c.
router_get_trusted_dir_servers_mutable
smartlist_t * router_get_trusted_dir_servers_mutable(void)
Definition: dirlist.c:103
LD_CONFIG
#define LD_CONFIG
Definition: log.h:68
dir_server_t
Definition: dir_server_st.h:21
tor_addr_to_str_dup
char * tor_addr_to_str_dup(const tor_addr_t *addr)
Definition: address.c:1164
dir_server_t::digest
char digest[DIGEST_LEN]
Definition: dir_server_st.h:33
fallback_dir_server_new
dir_server_t * fallback_dir_server_new(const tor_addr_t *ipv4_addr, uint16_t ipv4_dirport, uint16_t ipv4_orport, const tor_addr_port_t *addrport_ipv6, const char *id_digest, double weight)
Definition: dirlist.c:412
control_events.h
Header file for control_events.c.
get_options
const or_options_t * get_options(void)
Definition: config.c:928
routerstatus_t::last_dir_503_at
time_t last_dir_503_at
Definition: routerstatus_st.h:84
dir_server_new
static dir_server_t * dir_server_new(int is_authority, const char *nickname, const tor_addr_t *ipv4_addr, const char *hostname, uint16_t ipv4_dirport, uint16_t ipv4_orport, const tor_addr_port_t *addrport_ipv6, const char *digest, const char *v3_auth_digest, dirinfo_type_t type, double weight)
Definition: dirlist.c:286
smartlist_clear
void smartlist_clear(smartlist_t *sl)
Definition: smartlist_core.c:50
dir_server_t::ipv4_dirport
uint16_t ipv4_dirport
Definition: dir_server_st.h:28
resolve_addr.h
Header file for resolve_addr.c.
dir_server_t::is_running
unsigned int is_running
Definition: dir_server_st.h:37
trusteddirserver_get_by_v3_auth_digest
dir_server_t * trusteddirserver_get_by_v3_auth_digest(const char *digest)
Definition: dirlist.c:199
router_get_fallback_dirserver_by_digest
dir_server_t * router_get_fallback_dirserver_by_digest(const char *digest)
Definition: dirlist.c:165
V3_DIRINFO
@ V3_DIRINFO
Definition: or.h:908
router_reset_status_download_failures
void router_reset_status_download_failures(void)
Definition: dirlist.c:135
tor_addr_is_null
int tor_addr_is_null(const tor_addr_t *addr)
Definition: address.c:780
clear_dir_servers
void clear_dir_servers(void)
Definition: dirlist.c:455
dirinfo_type_t
dirinfo_type_t
Definition: or.h:905
dir_server_t::type
dirinfo_type_t type
Definition: dir_server_st.h:46
tor_asprintf
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
SMARTLIST_FOREACH_BEGIN
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
Definition: smartlist_foreach.h:78
LOG_WARN
#define LOG_WARN
Definition: log.h:53
node_st.h
Node information structure.
routerstatus_t::ipv6_addr
tor_addr_t ipv6_addr
Definition: routerstatus_st.h:35
add_trusted_dir_to_nodelist_addr_set
static void add_trusted_dir_to_nodelist_addr_set(const dir_server_t *dir)
Definition: dirlist.c:56
policies.h
Header file for policies.c.
dir_server_t::ipv6_orport
uint16_t ipv6_orport
Definition: dir_server_st.h:31
config.h
Header file for config.c.
routerstatus_t::ipv6_orport
uint16_t ipv6_orport
Definition: routerstatus_st.h:36
router_get_mutable_consensus_status_by_id
routerstatus_t * router_get_mutable_consensus_status_by_id(const char *digest)
Definition: networkstatus.c:830
mark_all_dirservers_up
void mark_all_dirservers_up(smartlist_t *server_list)
Definition: dirlist.c:216
control_event_networkstatus_changed_single
int control_event_networkstatus_changed_single(const routerstatus_t *rs)
Definition: control_events.c:1643
routerstatus_t::identity_digest
char identity_digest[DIGEST_LEN]
Definition: routerstatus_st.h:27
router_dir_info_changed
void router_dir_info_changed(void)
Definition: nodelist.c:2389
node_t::is_running
unsigned int is_running
Definition: node_st.h:63
smartlist_t
Definition: smartlist_core.h:26
tor_addr_eq
#define tor_addr_eq(a, b)
Definition: address.h:280
authdir_mode
int authdir_mode(const or_options_t *options)
Definition: authmode.c:25
dir_server_add
void dir_server_add(dir_server_t *ent)
Definition: dirlist.c:423
tor_addr_copy
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
Definition: address.c:933
dir_server_t::ipv6_addr
tor_addr_t ipv6_addr
Definition: dir_server_st.h:30
dir_server_t::address
char * address
Definition: dir_server_st.h:24
or.h
Master header file for Tor-specific functionality.