dirlist.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file dirlist.c
  8. * \brief Code to maintain our lists of directory authorities and
  9. * fallback directories.
  10. *
  11. * For the directory authorities, we have a list containing the public
  12. * identity key, and contact points, for each authority. The
  13. * authorities receive descriptors from relays, and publish consensuses,
  14. * descriptors, and microdescriptors. This list is pre-configured.
  15. *
  16. * Fallback directories are well-known, stable, but untrusted directory
  17. * caches that clients which have not yet bootstrapped can use to get
  18. * their first networkstatus consensus, in order to find out where the
  19. * Tor network really is. This list is pre-configured in
  20. * fallback_dirs.inc. Every authority also serves as a fallback.
  21. *
  22. * Both fallback directories and directory authorities are are
  23. * represented by a dir_server_t.
  24. */
  25. #include "core/or/or.h"
  26. #include "app/config/config.h"
  27. #include "core/or/policies.h"
  28. #include "feature/control/control.h"
  29. #include "feature/dircache/directory.h"
  30. #include "feature/nodelist/dirlist.h"
  31. #include "feature/nodelist/networkstatus.h"
  32. #include "feature/nodelist/nodelist.h"
  33. #include "feature/nodelist/routerlist.h"
  34. #include "feature/nodelist/routerset.h"
  35. #include "feature/relay/router.h"
  36. #include "lib/net/resolve.h"
  37. #include "feature/dirclient/dir_server_st.h"
  38. #include "feature/nodelist/node_st.h"
  39. /** Global list of a dir_server_t object for each directory
  40. * authority. */
  41. static smartlist_t *trusted_dir_servers = NULL;
  42. /** Global list of dir_server_t objects for all directory authorities
  43. * and all fallback directory servers. */
  44. static smartlist_t *fallback_dir_servers = NULL;
  45. /** Return the number of directory authorities whose type matches some bit set
  46. * in <b>type</b> */
  47. int
  48. get_n_authorities(dirinfo_type_t type)
  49. {
  50. int n = 0;
  51. if (!trusted_dir_servers)
  52. return 0;
  53. SMARTLIST_FOREACH(trusted_dir_servers, dir_server_t *, ds,
  54. if (ds->type & type)
  55. ++n);
  56. return n;
  57. }
  58. /** Return a smartlist containing a list of dir_server_t * for all
  59. * known trusted dirservers. Callers must not modify the list or its
  60. * contents.
  61. */
  62. smartlist_t *
  63. router_get_trusted_dir_servers_mutable(void)
  64. {
  65. if (!trusted_dir_servers)
  66. trusted_dir_servers = smartlist_new();
  67. return trusted_dir_servers;
  68. }
  69. smartlist_t *
  70. router_get_fallback_dir_servers_mutable(void)
  71. {
  72. if (!fallback_dir_servers)
  73. fallback_dir_servers = smartlist_new();
  74. return fallback_dir_servers;
  75. }
  76. const smartlist_t *
  77. router_get_trusted_dir_servers(void)
  78. {
  79. return router_get_trusted_dir_servers_mutable();
  80. }
  81. const smartlist_t *
  82. router_get_fallback_dir_servers(void)
  83. {
  84. return router_get_fallback_dir_servers_mutable();
  85. }
  86. /** Reset all internal variables used to count failed downloads of network
  87. * status objects. */
  88. void
  89. router_reset_status_download_failures(void)
  90. {
  91. mark_all_dirservers_up(fallback_dir_servers);
  92. }
  93. /** Return the dir_server_t for the directory authority whose identity
  94. * key hashes to <b>digest</b>, or NULL if no such authority is known.
  95. */
  96. dir_server_t *
  97. router_get_trusteddirserver_by_digest(const char *digest)
  98. {
  99. if (!trusted_dir_servers)
  100. return NULL;
  101. SMARTLIST_FOREACH(trusted_dir_servers, dir_server_t *, ds,
  102. {
  103. if (tor_memeq(ds->digest, digest, DIGEST_LEN))
  104. return ds;
  105. });
  106. return NULL;
  107. }
  108. /** Return the dir_server_t for the fallback dirserver whose identity
  109. * key hashes to <b>digest</b>, or NULL if no such fallback is in the list of
  110. * fallback_dir_servers. (fallback_dir_servers is affected by the FallbackDir
  111. * and UseDefaultFallbackDirs torrc options.)
  112. * The list of fallback directories includes the list of authorities.
  113. */
  114. dir_server_t *
  115. router_get_fallback_dirserver_by_digest(const char *digest)
  116. {
  117. if (!fallback_dir_servers)
  118. return NULL;
  119. if (!digest)
  120. return NULL;
  121. SMARTLIST_FOREACH(fallback_dir_servers, dir_server_t *, ds,
  122. {
  123. if (tor_memeq(ds->digest, digest, DIGEST_LEN))
  124. return ds;
  125. });
  126. return NULL;
  127. }
  128. /** Return 1 if any fallback dirserver's identity key hashes to <b>digest</b>,
  129. * or 0 if no such fallback is in the list of fallback_dir_servers.
  130. * (fallback_dir_servers is affected by the FallbackDir and
  131. * UseDefaultFallbackDirs torrc options.)
  132. * The list of fallback directories includes the list of authorities.
  133. */
  134. int
  135. router_digest_is_fallback_dir(const char *digest)
  136. {
  137. return (router_get_fallback_dirserver_by_digest(digest) != NULL);
  138. }
  139. /** Return the dir_server_t for the directory authority whose
  140. * v3 identity key hashes to <b>digest</b>, or NULL if no such authority
  141. * is known.
  142. */
  143. MOCK_IMPL(dir_server_t *,
  144. trusteddirserver_get_by_v3_auth_digest, (const char *digest))
  145. {
  146. if (!trusted_dir_servers)
  147. return NULL;
  148. SMARTLIST_FOREACH(trusted_dir_servers, dir_server_t *, ds,
  149. {
  150. if (tor_memeq(ds->v3_identity_digest, digest, DIGEST_LEN) &&
  151. (ds->type & V3_DIRINFO))
  152. return ds;
  153. });
  154. return NULL;
  155. }
  156. /** Mark as running every dir_server_t in <b>server_list</b>. */
  157. void
  158. mark_all_dirservers_up(smartlist_t *server_list)
  159. {
  160. if (server_list) {
  161. SMARTLIST_FOREACH_BEGIN(server_list, dir_server_t *, dir) {
  162. routerstatus_t *rs;
  163. node_t *node;
  164. dir->is_running = 1;
  165. node = node_get_mutable_by_id(dir->digest);
  166. if (node)
  167. node->is_running = 1;
  168. rs = router_get_mutable_consensus_status_by_id(dir->digest);
  169. if (rs) {
  170. rs->last_dir_503_at = 0;
  171. control_event_networkstatus_changed_single(rs);
  172. }
  173. } SMARTLIST_FOREACH_END(dir);
  174. }
  175. router_dir_info_changed();
  176. }
  177. /** Return true iff <b>digest</b> is the digest of the identity key of a
  178. * trusted directory matching at least one bit of <b>type</b>. If <b>type</b>
  179. * is zero (NO_DIRINFO), or ALL_DIRINFO, any authority is okay. */
  180. int
  181. router_digest_is_trusted_dir_type(const char *digest, dirinfo_type_t type)
  182. {
  183. if (!trusted_dir_servers)
  184. return 0;
  185. if (authdir_mode(get_options()) && router_digest_is_me(digest))
  186. return 1;
  187. SMARTLIST_FOREACH(trusted_dir_servers, dir_server_t *, ent,
  188. if (tor_memeq(digest, ent->digest, DIGEST_LEN)) {
  189. return (!type) || ((type & ent->type) != 0);
  190. });
  191. return 0;
  192. }
  193. /** Create a directory server at <b>address</b>:<b>port</b>, with OR identity
  194. * key <b>digest</b> which has DIGEST_LEN bytes. If <b>address</b> is NULL,
  195. * add ourself. If <b>is_authority</b>, this is a directory authority. Return
  196. * the new directory server entry on success or NULL on failure. */
  197. static dir_server_t *
  198. dir_server_new(int is_authority,
  199. const char *nickname,
  200. const tor_addr_t *addr,
  201. const char *hostname,
  202. uint16_t dir_port, uint16_t or_port,
  203. const tor_addr_port_t *addrport_ipv6,
  204. const char *digest, const char *v3_auth_digest,
  205. dirinfo_type_t type,
  206. double weight)
  207. {
  208. dir_server_t *ent;
  209. uint32_t a;
  210. char *hostname_ = NULL;
  211. tor_assert(digest);
  212. if (weight < 0)
  213. return NULL;
  214. if (tor_addr_family(addr) == AF_INET)
  215. a = tor_addr_to_ipv4h(addr);
  216. else
  217. return NULL;
  218. if (!hostname)
  219. hostname_ = tor_addr_to_str_dup(addr);
  220. else
  221. hostname_ = tor_strdup(hostname);
  222. ent = tor_malloc_zero(sizeof(dir_server_t));
  223. ent->nickname = nickname ? tor_strdup(nickname) : NULL;
  224. ent->address = hostname_;
  225. ent->addr = a;
  226. ent->dir_port = dir_port;
  227. ent->or_port = or_port;
  228. ent->is_running = 1;
  229. ent->is_authority = is_authority;
  230. ent->type = type;
  231. ent->weight = weight;
  232. if (addrport_ipv6) {
  233. if (tor_addr_family(&addrport_ipv6->addr) != AF_INET6) {
  234. log_warn(LD_BUG, "Hey, I got a non-ipv6 addr as addrport_ipv6.");
  235. tor_addr_make_unspec(&ent->ipv6_addr);
  236. } else {
  237. tor_addr_copy(&ent->ipv6_addr, &addrport_ipv6->addr);
  238. ent->ipv6_orport = addrport_ipv6->port;
  239. }
  240. } else {
  241. tor_addr_make_unspec(&ent->ipv6_addr);
  242. }
  243. memcpy(ent->digest, digest, DIGEST_LEN);
  244. if (v3_auth_digest && (type & V3_DIRINFO))
  245. memcpy(ent->v3_identity_digest, v3_auth_digest, DIGEST_LEN);
  246. if (nickname)
  247. tor_asprintf(&ent->description, "directory server \"%s\" at %s:%d",
  248. nickname, hostname_, (int)dir_port);
  249. else
  250. tor_asprintf(&ent->description, "directory server at %s:%d",
  251. hostname_, (int)dir_port);
  252. ent->fake_status.addr = ent->addr;
  253. tor_addr_copy(&ent->fake_status.ipv6_addr, &ent->ipv6_addr);
  254. memcpy(ent->fake_status.identity_digest, digest, DIGEST_LEN);
  255. if (nickname)
  256. strlcpy(ent->fake_status.nickname, nickname,
  257. sizeof(ent->fake_status.nickname));
  258. else
  259. ent->fake_status.nickname[0] = '\0';
  260. ent->fake_status.dir_port = ent->dir_port;
  261. ent->fake_status.or_port = ent->or_port;
  262. ent->fake_status.ipv6_orport = ent->ipv6_orport;
  263. return ent;
  264. }
  265. /** Create an authoritative directory server at
  266. * <b>address</b>:<b>port</b>, with identity key <b>digest</b>. If
  267. * <b>address</b> is NULL, add ourself. Return the new trusted directory
  268. * server entry on success or NULL if we couldn't add it. */
  269. dir_server_t *
  270. trusted_dir_server_new(const char *nickname, const char *address,
  271. uint16_t dir_port, uint16_t or_port,
  272. const tor_addr_port_t *ipv6_addrport,
  273. const char *digest, const char *v3_auth_digest,
  274. dirinfo_type_t type, double weight)
  275. {
  276. uint32_t a;
  277. tor_addr_t addr;
  278. char *hostname=NULL;
  279. dir_server_t *result;
  280. if (!address) { /* The address is us; we should guess. */
  281. if (resolve_my_address(LOG_WARN, get_options(),
  282. &a, NULL, &hostname) < 0) {
  283. log_warn(LD_CONFIG,
  284. "Couldn't find a suitable address when adding ourself as a "
  285. "trusted directory server.");
  286. return NULL;
  287. }
  288. if (!hostname)
  289. hostname = tor_dup_ip(a);
  290. } else {
  291. if (tor_lookup_hostname(address, &a)) {
  292. log_warn(LD_CONFIG,
  293. "Unable to lookup address for directory server at '%s'",
  294. address);
  295. return NULL;
  296. }
  297. hostname = tor_strdup(address);
  298. }
  299. tor_addr_from_ipv4h(&addr, a);
  300. result = dir_server_new(1, nickname, &addr, hostname,
  301. dir_port, or_port,
  302. ipv6_addrport,
  303. digest,
  304. v3_auth_digest, type, weight);
  305. tor_free(hostname);
  306. return result;
  307. }
  308. /** Return a new dir_server_t for a fallback directory server at
  309. * <b>addr</b>:<b>or_port</b>/<b>dir_port</b>, with identity key digest
  310. * <b>id_digest</b> */
  311. dir_server_t *
  312. fallback_dir_server_new(const tor_addr_t *addr,
  313. uint16_t dir_port, uint16_t or_port,
  314. const tor_addr_port_t *addrport_ipv6,
  315. const char *id_digest, double weight)
  316. {
  317. return dir_server_new(0, NULL, addr, NULL, dir_port, or_port,
  318. addrport_ipv6,
  319. id_digest,
  320. NULL, ALL_DIRINFO, weight);
  321. }
  322. /** Add a directory server to the global list(s). */
  323. void
  324. dir_server_add(dir_server_t *ent)
  325. {
  326. if (!trusted_dir_servers)
  327. trusted_dir_servers = smartlist_new();
  328. if (!fallback_dir_servers)
  329. fallback_dir_servers = smartlist_new();
  330. if (ent->is_authority)
  331. smartlist_add(trusted_dir_servers, ent);
  332. smartlist_add(fallback_dir_servers, ent);
  333. router_dir_info_changed();
  334. }
  335. #define dir_server_free(val) \
  336. FREE_AND_NULL(dir_server_t, dir_server_free_, (val))
  337. /** Free storage held in <b>ds</b>. */
  338. static void
  339. dir_server_free_(dir_server_t *ds)
  340. {
  341. if (!ds)
  342. return;
  343. tor_free(ds->nickname);
  344. tor_free(ds->description);
  345. tor_free(ds->address);
  346. tor_free(ds);
  347. }
  348. /** Remove all members from the list of dir servers. */
  349. void
  350. clear_dir_servers(void)
  351. {
  352. if (fallback_dir_servers) {
  353. SMARTLIST_FOREACH(fallback_dir_servers, dir_server_t *, ent,
  354. dir_server_free(ent));
  355. smartlist_clear(fallback_dir_servers);
  356. } else {
  357. fallback_dir_servers = smartlist_new();
  358. }
  359. if (trusted_dir_servers) {
  360. smartlist_clear(trusted_dir_servers);
  361. } else {
  362. trusted_dir_servers = smartlist_new();
  363. }
  364. router_dir_info_changed();
  365. }
  366. void
  367. dirlist_free_all(void)
  368. {
  369. clear_dir_servers();
  370. smartlist_free(trusted_dir_servers);
  371. smartlist_free(fallback_dir_servers);
  372. trusted_dir_servers = fallback_dir_servers = NULL;
  373. }