dirlist.c 12 KB

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