networkstatus.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221
  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-2010, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file networkstatus.c
  8. * \brief Functions and structures for handling network status documents as a
  9. * client or cache.
  10. */
  11. #include "or.h"
  12. #include "circuitbuild.h"
  13. #include "config.h"
  14. #include "connection.h"
  15. #include "control.h"
  16. #include "directory.h"
  17. #include "dirserv.h"
  18. #include "dirvote.h"
  19. #include "networkstatus.h"
  20. #include "relay.h"
  21. #include "router.h"
  22. #include "routerlist.h"
  23. #include "routerparse.h"
  24. /* For tracking v2 networkstatus documents. Only caches do this now. */
  25. /** Map from descriptor digest of routers listed in the v2 networkstatus
  26. * documents to download_status_t* */
  27. static digestmap_t *v2_download_status_map = NULL;
  28. /** Global list of all of the current v2 network_status documents that we know
  29. * about. This list is kept sorted by published_on. */
  30. static smartlist_t *networkstatus_v2_list = NULL;
  31. /** True iff any member of networkstatus_v2_list has changed since the last
  32. * time we called download_status_map_update_from_v2_networkstatus() */
  33. static int networkstatus_v2_list_has_changed = 0;
  34. /** Map from lowercase nickname to identity digest of named server, if any. */
  35. static strmap_t *named_server_map = NULL;
  36. /** Map from lowercase nickname to (void*)1 for all names that are listed
  37. * as unnamed for some server in the consensus. */
  38. static strmap_t *unnamed_server_map = NULL;
  39. /** Most recently received and validated v3 consensus network status. */
  40. static networkstatus_t *current_consensus = NULL;
  41. /** A v3 consensus networkstatus that we've received, but which we don't
  42. * have enough certificates to be happy about. */
  43. typedef struct consensus_waiting_for_certs_t {
  44. /** The consensus itself. */
  45. networkstatus_t *consensus;
  46. /** The encoded version of the consensus, nul-terminated. */
  47. char *body;
  48. /** When did we set the current value of consensus_waiting_for_certs? If
  49. * this is too recent, we shouldn't try to fetch a new consensus for a
  50. * little while, to give ourselves time to get certificates for this one. */
  51. time_t set_at;
  52. /** Set to 1 if we've been holding on to it for so long we should maybe
  53. * treat it as being bad. */
  54. int dl_failed;
  55. } consensus_waiting_for_certs_t;
  56. static consensus_waiting_for_certs_t
  57. consensus_waiting_for_certs[N_CONSENSUS_FLAVORS];
  58. /** The last time we tried to download a networkstatus, or 0 for "never". We
  59. * use this to rate-limit download attempts for directory caches (including
  60. * mirrors). Clients don't use this now. */
  61. static time_t last_networkstatus_download_attempted = 0;
  62. /** A time before which we shouldn't try to replace the current consensus:
  63. * this will be at some point after the next consensus becomes valid, but
  64. * before the current consensus becomes invalid. */
  65. static time_t time_to_download_next_consensus = 0;
  66. /** Download status for the current consensus networkstatus. */
  67. static download_status_t consensus_dl_status[N_CONSENSUS_FLAVORS];
  68. /** True iff we have logged a warning about this OR's version being older than
  69. * listed by the authorities. */
  70. static int have_warned_about_old_version = 0;
  71. /** True iff we have logged a warning about this OR's version being newer than
  72. * listed by the authorities. */
  73. static int have_warned_about_new_version = 0;
  74. static void download_status_map_update_from_v2_networkstatus(void);
  75. static void routerstatus_list_update_named_server_map(void);
  76. /** Forget that we've warned about anything networkstatus-related, so we will
  77. * give fresh warnings if the same behavior happens again. */
  78. void
  79. networkstatus_reset_warnings(void)
  80. {
  81. if (current_consensus) {
  82. SMARTLIST_FOREACH(current_consensus->routerstatus_list,
  83. routerstatus_t *, rs,
  84. rs->name_lookup_warned = 0);
  85. }
  86. have_warned_about_old_version = 0;
  87. have_warned_about_new_version = 0;
  88. }
  89. /** Reset the descriptor download failure count on all networkstatus docs, so
  90. * that we can retry any long-failed documents immediately.
  91. */
  92. void
  93. networkstatus_reset_download_failures(void)
  94. {
  95. int i;
  96. const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
  97. SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
  98. SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
  99. {
  100. if (!router_get_by_descriptor_digest(rs->descriptor_digest))
  101. rs->need_to_mirror = 1;
  102. }));;
  103. for (i=0; i < N_CONSENSUS_FLAVORS; ++i)
  104. download_status_reset(&consensus_dl_status[i]);
  105. if (v2_download_status_map) {
  106. digestmap_iter_t *iter;
  107. digestmap_t *map = v2_download_status_map;
  108. const char *key;
  109. void *val;
  110. download_status_t *dls;
  111. for (iter = digestmap_iter_init(map); !digestmap_iter_done(iter);
  112. iter = digestmap_iter_next(map, iter) ) {
  113. digestmap_iter_get(iter, &key, &val);
  114. dls = val;
  115. download_status_reset(dls);
  116. }
  117. }
  118. }
  119. /** Repopulate our list of network_status_t objects from the list cached on
  120. * disk. Return 0 on success, -1 on failure. */
  121. int
  122. router_reload_v2_networkstatus(void)
  123. {
  124. smartlist_t *entries;
  125. struct stat st;
  126. char *s;
  127. char *filename = get_datadir_fname("cached-status");
  128. int maybe_delete = !directory_caches_v2_dir_info(get_options());
  129. time_t now = time(NULL);
  130. if (!networkstatus_v2_list)
  131. networkstatus_v2_list = smartlist_create();
  132. entries = tor_listdir(filename);
  133. if (!entries) { /* dir doesn't exist */
  134. tor_free(filename);
  135. return 0;
  136. } else if (!smartlist_len(entries) && maybe_delete) {
  137. rmdir(filename);
  138. tor_free(filename);
  139. smartlist_free(entries);
  140. return 0;
  141. }
  142. tor_free(filename);
  143. SMARTLIST_FOREACH(entries, const char *, fn, {
  144. char buf[DIGEST_LEN];
  145. if (maybe_delete) {
  146. filename = get_datadir_fname2("cached-status", fn);
  147. remove_file_if_very_old(filename, now);
  148. tor_free(filename);
  149. continue;
  150. }
  151. if (strlen(fn) != HEX_DIGEST_LEN ||
  152. base16_decode(buf, sizeof(buf), fn, strlen(fn))) {
  153. log_info(LD_DIR,
  154. "Skipping cached-status file with unexpected name \"%s\"",fn);
  155. continue;
  156. }
  157. filename = get_datadir_fname2("cached-status", fn);
  158. s = read_file_to_str(filename, 0, &st);
  159. if (s) {
  160. if (router_set_networkstatus_v2(s, st.st_mtime, NS_FROM_CACHE,
  161. NULL)<0) {
  162. log_warn(LD_FS, "Couldn't load networkstatus from \"%s\"",filename);
  163. }
  164. tor_free(s);
  165. }
  166. tor_free(filename);
  167. });
  168. SMARTLIST_FOREACH(entries, char *, fn, tor_free(fn));
  169. smartlist_free(entries);
  170. networkstatus_v2_list_clean(time(NULL));
  171. routers_update_all_from_networkstatus(time(NULL), 2);
  172. return 0;
  173. }
  174. /** Read every cached v3 consensus networkstatus from the disk. */
  175. int
  176. router_reload_consensus_networkstatus(void)
  177. {
  178. char *filename;
  179. char *s;
  180. struct stat st;
  181. or_options_t *options = get_options();
  182. const unsigned int flags = NSSET_FROM_CACHE | NSSET_DONT_DOWNLOAD_CERTS;
  183. int flav;
  184. /* FFFF Suppress warnings if cached consensus is bad? */
  185. for (flav = 0; flav < N_CONSENSUS_FLAVORS; ++flav) {
  186. char buf[128];
  187. const char *flavor = networkstatus_get_flavor_name(flav);
  188. if (flav == FLAV_NS) {
  189. filename = get_datadir_fname("cached-consensus");
  190. } else {
  191. tor_snprintf(buf, sizeof(buf), "cached-%s-consensus", flavor);
  192. filename = get_datadir_fname(buf);
  193. }
  194. s = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
  195. if (s) {
  196. if (networkstatus_set_current_consensus(s, flavor, flags) < -1) {
  197. log_warn(LD_FS, "Couldn't load consensus %s networkstatus from \"%s\"",
  198. flavor, filename);
  199. }
  200. tor_free(s);
  201. }
  202. tor_free(filename);
  203. if (flav == FLAV_NS) {
  204. filename = get_datadir_fname("unverified-consensus");
  205. } else {
  206. tor_snprintf(buf, sizeof(buf), "unverified-%s-consensus", flavor);
  207. filename = get_datadir_fname(buf);
  208. }
  209. s = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
  210. if (s) {
  211. if (networkstatus_set_current_consensus(s, flavor,
  212. flags|NSSET_WAS_WAITING_FOR_CERTS)) {
  213. log_info(LD_FS, "Couldn't load consensus %s networkstatus from \"%s\"",
  214. flavor, filename);
  215. }
  216. tor_free(s);
  217. }
  218. tor_free(filename);
  219. }
  220. if (!current_consensus ||
  221. (stat(options->FallbackNetworkstatusFile, &st)==0 &&
  222. st.st_mtime > current_consensus->valid_after)) {
  223. s = read_file_to_str(options->FallbackNetworkstatusFile,
  224. RFTS_IGNORE_MISSING, NULL);
  225. if (s) {
  226. if (networkstatus_set_current_consensus(s, "ns",
  227. flags|NSSET_ACCEPT_OBSOLETE)) {
  228. log_info(LD_FS, "Couldn't load consensus networkstatus from \"%s\"",
  229. options->FallbackNetworkstatusFile);
  230. } else {
  231. log_notice(LD_FS,
  232. "Loaded fallback consensus networkstatus from \"%s\"",
  233. options->FallbackNetworkstatusFile);
  234. }
  235. tor_free(s);
  236. }
  237. }
  238. if (!current_consensus) {
  239. if (!named_server_map)
  240. named_server_map = strmap_new();
  241. if (!unnamed_server_map)
  242. unnamed_server_map = strmap_new();
  243. }
  244. update_certificate_downloads(time(NULL));
  245. routers_update_all_from_networkstatus(time(NULL), 3);
  246. return 0;
  247. }
  248. /** Free all storage held by the vote_routerstatus object <b>rs</b>. */
  249. static void
  250. vote_routerstatus_free(vote_routerstatus_t *rs)
  251. {
  252. vote_microdesc_hash_t *h, *next;
  253. if (!rs)
  254. return;
  255. tor_free(rs->version);
  256. tor_free(rs->status.exitsummary);
  257. for (h = rs->microdesc; h; h = next) {
  258. tor_free(h->microdesc_hash_line);
  259. next = h->next;
  260. tor_free(h);
  261. }
  262. tor_free(rs);
  263. }
  264. /** Free all storage held by the routerstatus object <b>rs</b>. */
  265. void
  266. routerstatus_free(routerstatus_t *rs)
  267. {
  268. if (!rs)
  269. return;
  270. tor_free(rs->exitsummary);
  271. tor_free(rs);
  272. }
  273. /** Free all storage held by the networkstatus object <b>ns</b>. */
  274. void
  275. networkstatus_v2_free(networkstatus_v2_t *ns)
  276. {
  277. if (!ns)
  278. return;
  279. tor_free(ns->source_address);
  280. tor_free(ns->contact);
  281. if (ns->signing_key)
  282. crypto_free_pk_env(ns->signing_key);
  283. tor_free(ns->client_versions);
  284. tor_free(ns->server_versions);
  285. if (ns->entries) {
  286. SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
  287. routerstatus_free(rs));
  288. smartlist_free(ns->entries);
  289. }
  290. tor_free(ns);
  291. }
  292. /** Free all storage held in <b>sig</b> */
  293. void
  294. document_signature_free(document_signature_t *sig)
  295. {
  296. tor_free(sig->signature);
  297. tor_free(sig);
  298. }
  299. /** Return a newly allocated copy of <b>sig</b> */
  300. document_signature_t *
  301. document_signature_dup(const document_signature_t *sig)
  302. {
  303. document_signature_t *r = tor_memdup(sig, sizeof(document_signature_t));
  304. if (r->signature)
  305. r->signature = tor_memdup(sig->signature, sig->signature_len);
  306. return r;
  307. }
  308. /** Free all storage held in <b>ns</b>. */
  309. void
  310. networkstatus_vote_free(networkstatus_t *ns)
  311. {
  312. if (!ns)
  313. return;
  314. tor_free(ns->client_versions);
  315. tor_free(ns->server_versions);
  316. if (ns->known_flags) {
  317. SMARTLIST_FOREACH(ns->known_flags, char *, c, tor_free(c));
  318. smartlist_free(ns->known_flags);
  319. }
  320. if (ns->net_params) {
  321. SMARTLIST_FOREACH(ns->net_params, char *, c, tor_free(c));
  322. smartlist_free(ns->net_params);
  323. }
  324. if (ns->supported_methods) {
  325. SMARTLIST_FOREACH(ns->supported_methods, char *, c, tor_free(c));
  326. smartlist_free(ns->supported_methods);
  327. }
  328. if (ns->voters) {
  329. SMARTLIST_FOREACH_BEGIN(ns->voters, networkstatus_voter_info_t *, voter) {
  330. tor_free(voter->nickname);
  331. tor_free(voter->address);
  332. tor_free(voter->contact);
  333. if (voter->sigs) {
  334. SMARTLIST_FOREACH(voter->sigs, document_signature_t *, sig,
  335. document_signature_free(sig));
  336. smartlist_free(voter->sigs);
  337. }
  338. tor_free(voter);
  339. } SMARTLIST_FOREACH_END(voter);
  340. smartlist_free(ns->voters);
  341. }
  342. authority_cert_free(ns->cert);
  343. if (ns->routerstatus_list) {
  344. if (ns->type == NS_TYPE_VOTE || ns->type == NS_TYPE_OPINION) {
  345. SMARTLIST_FOREACH(ns->routerstatus_list, vote_routerstatus_t *, rs,
  346. vote_routerstatus_free(rs));
  347. } else {
  348. SMARTLIST_FOREACH(ns->routerstatus_list, routerstatus_t *, rs,
  349. routerstatus_free(rs));
  350. }
  351. smartlist_free(ns->routerstatus_list);
  352. }
  353. digestmap_free(ns->desc_digest_map, NULL);
  354. memset(ns, 11, sizeof(*ns));
  355. tor_free(ns);
  356. }
  357. /** Return the voter info from <b>vote</b> for the voter whose identity digest
  358. * is <b>identity</b>, or NULL if no such voter is associated with
  359. * <b>vote</b>. */
  360. networkstatus_voter_info_t *
  361. networkstatus_get_voter_by_id(networkstatus_t *vote,
  362. const char *identity)
  363. {
  364. if (!vote || !vote->voters)
  365. return NULL;
  366. SMARTLIST_FOREACH(vote->voters, networkstatus_voter_info_t *, voter,
  367. if (!memcmp(voter->identity_digest, identity, DIGEST_LEN))
  368. return voter);
  369. return NULL;
  370. }
  371. /** Check whether the signature <b>sig</b> is correctly signed with the
  372. * signing key in <b>cert</b>. Return -1 if <b>cert</b> doesn't match the
  373. * signing key; otherwise set the good_signature or bad_signature flag on
  374. * <b>voter</b>, and return 0. */
  375. int
  376. networkstatus_check_document_signature(const networkstatus_t *consensus,
  377. document_signature_t *sig,
  378. const authority_cert_t *cert)
  379. {
  380. char key_digest[DIGEST_LEN];
  381. const int dlen = sig->alg == DIGEST_SHA1 ? DIGEST_LEN : DIGEST256_LEN;
  382. char *signed_digest;
  383. size_t signed_digest_len;
  384. if (crypto_pk_get_digest(cert->signing_key, key_digest)<0)
  385. return -1;
  386. if (memcmp(sig->signing_key_digest, key_digest, DIGEST_LEN) ||
  387. memcmp(sig->identity_digest, cert->cache_info.identity_digest,
  388. DIGEST_LEN))
  389. return -1;
  390. signed_digest_len = crypto_pk_keysize(cert->signing_key);
  391. signed_digest = tor_malloc(signed_digest_len);
  392. if (crypto_pk_public_checksig(cert->signing_key,
  393. signed_digest,
  394. sig->signature,
  395. sig->signature_len) < dlen ||
  396. memcmp(signed_digest, consensus->digests.d[sig->alg], dlen)) {
  397. log_warn(LD_DIR, "Got a bad signature on a networkstatus vote");
  398. sig->bad_signature = 1;
  399. } else {
  400. sig->good_signature = 1;
  401. }
  402. tor_free(signed_digest);
  403. return 0;
  404. }
  405. /** Given a v3 networkstatus consensus in <b>consensus</b>, check every
  406. * as-yet-unchecked signature on <b>consensus</b>. Return 1 if there is a
  407. * signature from every recognized authority on it, 0 if there are
  408. * enough good signatures from recognized authorities on it, -1 if we might
  409. * get enough good signatures by fetching missing certificates, and -2
  410. * otherwise. Log messages at INFO or WARN: if <b>warn</b> is over 1, warn
  411. * about every problem; if warn is at least 1, warn only if we can't get
  412. * enough signatures; if warn is negative, log nothing at all. */
  413. int
  414. networkstatus_check_consensus_signature(networkstatus_t *consensus,
  415. int warn)
  416. {
  417. int n_good = 0;
  418. int n_missing_key = 0;
  419. int n_bad = 0;
  420. int n_unknown = 0;
  421. int n_no_signature = 0;
  422. int n_v3_authorities = get_n_authorities(V3_AUTHORITY);
  423. int n_required = n_v3_authorities/2 + 1;
  424. smartlist_t *need_certs_from = smartlist_create();
  425. smartlist_t *unrecognized = smartlist_create();
  426. smartlist_t *missing_authorities = smartlist_create();
  427. int severity;
  428. time_t now = time(NULL);
  429. tor_assert(consensus->type == NS_TYPE_CONSENSUS);
  430. SMARTLIST_FOREACH_BEGIN(consensus->voters, networkstatus_voter_info_t *,
  431. voter) {
  432. int good_here = 0;
  433. int bad_here = 0;
  434. int missing_key_here = 0;
  435. SMARTLIST_FOREACH_BEGIN(voter->sigs, document_signature_t *, sig) {
  436. if (!sig->good_signature && !sig->bad_signature &&
  437. sig->signature) {
  438. /* we can try to check the signature. */
  439. int is_v3_auth = trusteddirserver_get_by_v3_auth_digest(
  440. sig->identity_digest) != NULL;
  441. authority_cert_t *cert =
  442. authority_cert_get_by_digests(sig->identity_digest,
  443. sig->signing_key_digest);
  444. tor_assert(!memcmp(sig->identity_digest, voter->identity_digest,
  445. DIGEST_LEN));
  446. if (!is_v3_auth) {
  447. smartlist_add(unrecognized, voter);
  448. ++n_unknown;
  449. continue;
  450. } else if (!cert || cert->expires < now) {
  451. smartlist_add(need_certs_from, voter);
  452. ++missing_key_here;
  453. continue;
  454. }
  455. if (networkstatus_check_document_signature(consensus, sig, cert) < 0) {
  456. smartlist_add(need_certs_from, voter);
  457. ++missing_key_here;
  458. continue;
  459. }
  460. }
  461. if (sig->good_signature)
  462. ++good_here;
  463. else if (sig->bad_signature)
  464. ++bad_here;
  465. } SMARTLIST_FOREACH_END(sig);
  466. if (good_here)
  467. ++n_good;
  468. else if (bad_here)
  469. ++n_bad;
  470. else if (missing_key_here)
  471. ++n_missing_key;
  472. else
  473. ++n_no_signature;
  474. } SMARTLIST_FOREACH_END(voter);
  475. /* Now see whether we're missing any voters entirely. */
  476. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  477. trusted_dir_server_t *, ds,
  478. {
  479. if ((ds->type & V3_AUTHORITY) &&
  480. !networkstatus_get_voter_by_id(consensus, ds->v3_identity_digest))
  481. smartlist_add(missing_authorities, ds);
  482. });
  483. if (warn > 1 || (warn >= 0 && n_good < n_required))
  484. severity = LOG_WARN;
  485. else
  486. severity = LOG_INFO;
  487. if (warn >= 0) {
  488. SMARTLIST_FOREACH(unrecognized, networkstatus_voter_info_t *, voter,
  489. {
  490. log_info(LD_DIR, "Consensus includes unrecognized authority '%s' "
  491. "at %s:%d (contact %s; identity %s)",
  492. voter->nickname, voter->address, (int)voter->dir_port,
  493. voter->contact?voter->contact:"n/a",
  494. hex_str(voter->identity_digest, DIGEST_LEN));
  495. });
  496. SMARTLIST_FOREACH(need_certs_from, networkstatus_voter_info_t *, voter,
  497. {
  498. log_info(LD_DIR, "Looks like we need to download a new certificate "
  499. "from authority '%s' at %s:%d (contact %s; identity %s)",
  500. voter->nickname, voter->address, (int)voter->dir_port,
  501. voter->contact?voter->contact:"n/a",
  502. hex_str(voter->identity_digest, DIGEST_LEN));
  503. });
  504. SMARTLIST_FOREACH(missing_authorities, trusted_dir_server_t *, ds,
  505. {
  506. log_info(LD_DIR, "Consensus does not include configured "
  507. "authority '%s' at %s:%d (identity %s)",
  508. ds->nickname, ds->address, (int)ds->dir_port,
  509. hex_str(ds->v3_identity_digest, DIGEST_LEN));
  510. });
  511. log(severity, LD_DIR,
  512. "%d unknown, %d missing key, %d good, %d bad, %d no signature, "
  513. "%d required", n_unknown, n_missing_key, n_good, n_bad,
  514. n_no_signature, n_required);
  515. }
  516. smartlist_free(unrecognized);
  517. smartlist_free(need_certs_from);
  518. smartlist_free(missing_authorities);
  519. if (n_good == n_v3_authorities)
  520. return 1;
  521. else if (n_good >= n_required)
  522. return 0;
  523. else if (n_good + n_missing_key >= n_required)
  524. return -1;
  525. else
  526. return -2;
  527. }
  528. /** Helper: return a newly allocated string containing the name of the filename
  529. * where we plan to cache the network status with the given identity digest. */
  530. char *
  531. networkstatus_get_cache_filename(const char *identity_digest)
  532. {
  533. char fp[HEX_DIGEST_LEN+1];
  534. base16_encode(fp, HEX_DIGEST_LEN+1, identity_digest, DIGEST_LEN);
  535. return get_datadir_fname2("cached-status", fp);
  536. }
  537. /** Helper for smartlist_sort: Compare two networkstatus objects by
  538. * publication date. */
  539. static int
  540. _compare_networkstatus_v2_published_on(const void **_a, const void **_b)
  541. {
  542. const networkstatus_v2_t *a = *_a, *b = *_b;
  543. if (a->published_on < b->published_on)
  544. return -1;
  545. else if (a->published_on > b->published_on)
  546. return 1;
  547. else
  548. return 0;
  549. }
  550. /** Add the parsed v2 networkstatus in <b>ns</b> (with original document in
  551. * <b>s</b>) to the disk cache (and the in-memory directory server cache) as
  552. * appropriate. */
  553. static int
  554. add_networkstatus_to_cache(const char *s,
  555. v2_networkstatus_source_t source,
  556. networkstatus_v2_t *ns)
  557. {
  558. if (source != NS_FROM_CACHE) {
  559. char *fn = networkstatus_get_cache_filename(ns->identity_digest);
  560. if (write_str_to_file(fn, s, 0)<0) {
  561. log_notice(LD_FS, "Couldn't write cached network status to \"%s\"", fn);
  562. }
  563. tor_free(fn);
  564. }
  565. if (directory_caches_v2_dir_info(get_options()))
  566. dirserv_set_cached_networkstatus_v2(s,
  567. ns->identity_digest,
  568. ns->published_on);
  569. return 0;
  570. }
  571. /** How far in the future do we allow a network-status to get before removing
  572. * it? (seconds) */
  573. #define NETWORKSTATUS_ALLOW_SKEW (24*60*60)
  574. /** Given a string <b>s</b> containing a network status that we received at
  575. * <b>arrived_at</b> from <b>source</b>, try to parse it, see if we want to
  576. * store it, and put it into our cache as necessary.
  577. *
  578. * If <b>source</b> is NS_FROM_DIR or NS_FROM_CACHE, do not replace our
  579. * own networkstatus_t (if we're an authoritative directory server).
  580. *
  581. * If <b>source</b> is NS_FROM_CACHE, do not write our networkstatus_t to the
  582. * cache.
  583. *
  584. * If <b>requested_fingerprints</b> is provided, it must contain a list of
  585. * uppercased identity fingerprints. Do not update any networkstatus whose
  586. * fingerprint is not on the list; after updating a networkstatus, remove its
  587. * fingerprint from the list.
  588. *
  589. * Return 0 on success, -1 on failure.
  590. *
  591. * Callers should make sure that routers_update_all_from_networkstatus() is
  592. * invoked after this function succeeds.
  593. */
  594. int
  595. router_set_networkstatus_v2(const char *s, time_t arrived_at,
  596. v2_networkstatus_source_t source,
  597. smartlist_t *requested_fingerprints)
  598. {
  599. networkstatus_v2_t *ns;
  600. int i, found;
  601. time_t now;
  602. int skewed = 0;
  603. trusted_dir_server_t *trusted_dir = NULL;
  604. const char *source_desc = NULL;
  605. char fp[HEX_DIGEST_LEN+1];
  606. char published[ISO_TIME_LEN+1];
  607. if (!directory_caches_v2_dir_info(get_options()))
  608. return 0; /* Don't bother storing it. */
  609. ns = networkstatus_v2_parse_from_string(s);
  610. if (!ns) {
  611. log_warn(LD_DIR, "Couldn't parse network status.");
  612. return -1;
  613. }
  614. base16_encode(fp, HEX_DIGEST_LEN+1, ns->identity_digest, DIGEST_LEN);
  615. if (!(trusted_dir =
  616. router_get_trusteddirserver_by_digest(ns->identity_digest)) ||
  617. !(trusted_dir->type & V2_AUTHORITY)) {
  618. log_info(LD_DIR, "Network status was signed, but not by an authoritative "
  619. "directory we recognize.");
  620. source_desc = fp;
  621. } else {
  622. source_desc = trusted_dir->description;
  623. }
  624. now = time(NULL);
  625. if (arrived_at > now)
  626. arrived_at = now;
  627. ns->received_on = arrived_at;
  628. format_iso_time(published, ns->published_on);
  629. if (ns->published_on > now + NETWORKSTATUS_ALLOW_SKEW) {
  630. char dbuf[64];
  631. long delta = now - ns->published_on;
  632. format_time_interval(dbuf, sizeof(dbuf), delta);
  633. log_warn(LD_GENERAL, "Network status from %s was published %s in the "
  634. "future (%s GMT). Check your time and date settings! "
  635. "Not caching.",
  636. source_desc, dbuf, published);
  637. control_event_general_status(LOG_WARN,
  638. "CLOCK_SKEW MIN_SKEW=%ld SOURCE=NETWORKSTATUS:%s:%d",
  639. delta, ns->source_address, ns->source_dirport);
  640. skewed = 1;
  641. }
  642. if (!networkstatus_v2_list)
  643. networkstatus_v2_list = smartlist_create();
  644. if ( (source == NS_FROM_DIR_BY_FP || source == NS_FROM_DIR_ALL) &&
  645. router_digest_is_me(ns->identity_digest)) {
  646. /* Don't replace our own networkstatus when we get it from somebody else.*/
  647. networkstatus_v2_free(ns);
  648. return 0;
  649. }
  650. if (requested_fingerprints) {
  651. if (smartlist_string_isin(requested_fingerprints, fp)) {
  652. smartlist_string_remove(requested_fingerprints, fp);
  653. } else {
  654. if (source != NS_FROM_DIR_ALL) {
  655. char *requested =
  656. smartlist_join_strings(requested_fingerprints," ",0,NULL);
  657. log_warn(LD_DIR,
  658. "We received a network status with a fingerprint (%s) that we "
  659. "never requested. (We asked for: %s.) Dropping.",
  660. fp, requested);
  661. tor_free(requested);
  662. return 0;
  663. }
  664. }
  665. }
  666. if (!trusted_dir) {
  667. if (!skewed) {
  668. /* We got a non-trusted networkstatus, and we're a directory cache.
  669. * This means that we asked an authority, and it told us about another
  670. * authority we didn't recognize. */
  671. log_info(LD_DIR,
  672. "We do not recognize authority (%s) but we are willing "
  673. "to cache it.", fp);
  674. add_networkstatus_to_cache(s, source, ns);
  675. networkstatus_v2_free(ns);
  676. }
  677. return 0;
  678. }
  679. found = 0;
  680. for (i=0; i < smartlist_len(networkstatus_v2_list); ++i) {
  681. networkstatus_v2_t *old_ns = smartlist_get(networkstatus_v2_list, i);
  682. if (!memcmp(old_ns->identity_digest, ns->identity_digest, DIGEST_LEN)) {
  683. if (!memcmp(old_ns->networkstatus_digest,
  684. ns->networkstatus_digest, DIGEST_LEN)) {
  685. /* Same one we had before. */
  686. networkstatus_v2_free(ns);
  687. tor_assert(trusted_dir);
  688. log_info(LD_DIR,
  689. "Not replacing network-status from %s (published %s); "
  690. "we already have it.",
  691. trusted_dir->description, published);
  692. if (old_ns->received_on < arrived_at) {
  693. if (source != NS_FROM_CACHE) {
  694. char *fn;
  695. fn = networkstatus_get_cache_filename(old_ns->identity_digest);
  696. /* We use mtime to tell when it arrived, so update that. */
  697. touch_file(fn);
  698. tor_free(fn);
  699. }
  700. old_ns->received_on = arrived_at;
  701. }
  702. download_status_failed(&trusted_dir->v2_ns_dl_status, 0);
  703. return 0;
  704. } else if (old_ns->published_on >= ns->published_on) {
  705. char old_published[ISO_TIME_LEN+1];
  706. format_iso_time(old_published, old_ns->published_on);
  707. tor_assert(trusted_dir);
  708. log_info(LD_DIR,
  709. "Not replacing network-status from %s (published %s);"
  710. " we have a newer one (published %s) for this authority.",
  711. trusted_dir->description, published,
  712. old_published);
  713. networkstatus_v2_free(ns);
  714. download_status_failed(&trusted_dir->v2_ns_dl_status, 0);
  715. return 0;
  716. } else {
  717. networkstatus_v2_free(old_ns);
  718. smartlist_set(networkstatus_v2_list, i, ns);
  719. found = 1;
  720. break;
  721. }
  722. }
  723. }
  724. if (source != NS_FROM_CACHE && trusted_dir) {
  725. download_status_reset(&trusted_dir->v2_ns_dl_status);
  726. }
  727. if (!found)
  728. smartlist_add(networkstatus_v2_list, ns);
  729. /** Retain any routerinfo mentioned in a V2 networkstatus for at least this
  730. * long. */
  731. #define V2_NETWORKSTATUS_ROUTER_LIFETIME (3*60*60)
  732. {
  733. time_t live_until = ns->published_on + V2_NETWORKSTATUS_ROUTER_LIFETIME;
  734. SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
  735. {
  736. signed_descriptor_t *sd =
  737. router_get_by_descriptor_digest(rs->descriptor_digest);
  738. if (sd) {
  739. if (sd->last_listed_as_valid_until < live_until)
  740. sd->last_listed_as_valid_until = live_until;
  741. } else {
  742. rs->need_to_mirror = 1;
  743. }
  744. });
  745. }
  746. log_info(LD_DIR, "Setting networkstatus %s %s (published %s)",
  747. source == NS_FROM_CACHE?"cached from":
  748. ((source == NS_FROM_DIR_BY_FP || source == NS_FROM_DIR_ALL) ?
  749. "downloaded from":"generated for"),
  750. trusted_dir->description, published);
  751. networkstatus_v2_list_has_changed = 1;
  752. smartlist_sort(networkstatus_v2_list,
  753. _compare_networkstatus_v2_published_on);
  754. if (!skewed)
  755. add_networkstatus_to_cache(s, source, ns);
  756. return 0;
  757. }
  758. /** Remove all very-old network_status_t objects from memory and from the
  759. * disk cache. */
  760. void
  761. networkstatus_v2_list_clean(time_t now)
  762. {
  763. int i;
  764. if (!networkstatus_v2_list)
  765. return;
  766. for (i = 0; i < smartlist_len(networkstatus_v2_list); ++i) {
  767. networkstatus_v2_t *ns = smartlist_get(networkstatus_v2_list, i);
  768. char *fname = NULL;
  769. if (ns->published_on + MAX_NETWORKSTATUS_AGE > now)
  770. continue;
  771. /* Okay, this one is too old. Remove it from the list, and delete it
  772. * from the cache. */
  773. smartlist_del(networkstatus_v2_list, i--);
  774. fname = networkstatus_get_cache_filename(ns->identity_digest);
  775. if (file_status(fname) == FN_FILE) {
  776. log_info(LD_DIR, "Removing too-old networkstatus in %s", fname);
  777. unlink(fname);
  778. }
  779. tor_free(fname);
  780. if (directory_caches_v2_dir_info(get_options())) {
  781. dirserv_set_cached_networkstatus_v2(NULL, ns->identity_digest, 0);
  782. }
  783. networkstatus_v2_free(ns);
  784. }
  785. /* And now go through the directory cache for any cached untrusted
  786. * networkstatuses and other network info. */
  787. dirserv_clear_old_networkstatuses(now - MAX_NETWORKSTATUS_AGE);
  788. dirserv_clear_old_v1_info(now);
  789. }
  790. /** Helper for bsearching a list of routerstatus_t pointers: compare a
  791. * digest in the key to the identity digest of a routerstatus_t. */
  792. int
  793. compare_digest_to_routerstatus_entry(const void *_key, const void **_member)
  794. {
  795. const char *key = _key;
  796. const routerstatus_t *rs = *_member;
  797. return memcmp(key, rs->identity_digest, DIGEST_LEN);
  798. }
  799. /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
  800. * NULL if none was found. */
  801. routerstatus_t *
  802. networkstatus_v2_find_entry(networkstatus_v2_t *ns, const char *digest)
  803. {
  804. return smartlist_bsearch(ns->entries, digest,
  805. compare_digest_to_routerstatus_entry);
  806. }
  807. /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
  808. * NULL if none was found. */
  809. routerstatus_t *
  810. networkstatus_vote_find_entry(networkstatus_t *ns, const char *digest)
  811. {
  812. return smartlist_bsearch(ns->routerstatus_list, digest,
  813. compare_digest_to_routerstatus_entry);
  814. }
  815. /*XXXX make this static once functions are moved into this file. */
  816. /** Search the routerstatuses in <b>ns</b> for one whose identity digest is
  817. * <b>digest</b>. Return value and set *<b>found_out</b> as for
  818. * smartlist_bsearch_idx(). */
  819. int
  820. networkstatus_vote_find_entry_idx(networkstatus_t *ns,
  821. const char *digest, int *found_out)
  822. {
  823. return smartlist_bsearch_idx(ns->routerstatus_list, digest,
  824. compare_digest_to_routerstatus_entry,
  825. found_out);
  826. }
  827. /** Return a list of the v2 networkstatus documents. */
  828. const smartlist_t *
  829. networkstatus_get_v2_list(void)
  830. {
  831. if (!networkstatus_v2_list)
  832. networkstatus_v2_list = smartlist_create();
  833. return networkstatus_v2_list;
  834. }
  835. /** Return the consensus view of the status of the router whose current
  836. * <i>descriptor</i> digest is <b>digest</b>, or NULL if no such router is
  837. * known. */
  838. routerstatus_t *
  839. router_get_consensus_status_by_descriptor_digest(const char *digest)
  840. {
  841. if (!current_consensus) return NULL;
  842. if (!current_consensus->desc_digest_map) {
  843. digestmap_t * m = current_consensus->desc_digest_map = digestmap_new();
  844. SMARTLIST_FOREACH(current_consensus->routerstatus_list,
  845. routerstatus_t *, rs,
  846. {
  847. digestmap_set(m, rs->descriptor_digest, rs);
  848. });
  849. }
  850. return digestmap_get(current_consensus->desc_digest_map, digest);
  851. }
  852. /** Given the digest of a router descriptor, return its current download
  853. * status, or NULL if the digest is unrecognized. */
  854. download_status_t *
  855. router_get_dl_status_by_descriptor_digest(const char *d)
  856. {
  857. routerstatus_t *rs;
  858. if ((rs = router_get_consensus_status_by_descriptor_digest(d)))
  859. return &rs->dl_status;
  860. if (v2_download_status_map)
  861. return digestmap_get(v2_download_status_map, d);
  862. return NULL;
  863. }
  864. /** Return the consensus view of the status of the router whose identity
  865. * digest is <b>digest</b>, or NULL if we don't know about any such router. */
  866. routerstatus_t *
  867. router_get_consensus_status_by_id(const char *digest)
  868. {
  869. if (!current_consensus)
  870. return NULL;
  871. return smartlist_bsearch(current_consensus->routerstatus_list, digest,
  872. compare_digest_to_routerstatus_entry);
  873. }
  874. /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
  875. * the corresponding routerstatus_t, or NULL if none exists. Warn the
  876. * user if <b>warn_if_unnamed</b> is set, and they have specified a router by
  877. * nickname, but the Named flag isn't set for that router. */
  878. routerstatus_t *
  879. router_get_consensus_status_by_nickname(const char *nickname,
  880. int warn_if_unnamed)
  881. {
  882. char digest[DIGEST_LEN];
  883. routerstatus_t *best=NULL;
  884. smartlist_t *matches=NULL;
  885. const char *named_id=NULL;
  886. if (!current_consensus || !nickname)
  887. return NULL;
  888. /* Is this name really a hexadecimal identity digest? */
  889. if (nickname[0] == '$') {
  890. if (base16_decode(digest, DIGEST_LEN, nickname+1, strlen(nickname+1))<0)
  891. return NULL;
  892. return networkstatus_vote_find_entry(current_consensus, digest);
  893. } else if (strlen(nickname) == HEX_DIGEST_LEN &&
  894. (base16_decode(digest, DIGEST_LEN, nickname, strlen(nickname))==0)) {
  895. return networkstatus_vote_find_entry(current_consensus, digest);
  896. }
  897. /* Is there a server that is Named with this name? */
  898. if (named_server_map)
  899. named_id = strmap_get_lc(named_server_map, nickname);
  900. if (named_id)
  901. return networkstatus_vote_find_entry(current_consensus, named_id);
  902. /* Okay; is this name listed as Unnamed? */
  903. if (unnamed_server_map &&
  904. strmap_get_lc(unnamed_server_map, nickname)) {
  905. log_info(LD_GENERAL, "The name %s is listed as Unnamed; it is not the "
  906. "canonical name of any server we know.", escaped(nickname));
  907. return NULL;
  908. }
  909. /* This name is not canonical for any server; go through the list and
  910. * see who it matches. */
  911. /*XXXX This is inefficient; optimize it if it matters. */
  912. matches = smartlist_create();
  913. SMARTLIST_FOREACH(current_consensus->routerstatus_list,
  914. routerstatus_t *, lrs,
  915. {
  916. if (!strcasecmp(lrs->nickname, nickname)) {
  917. if (lrs->is_named) {
  918. tor_fragile_assert() /* This should never happen. */
  919. smartlist_free(matches);
  920. return lrs;
  921. } else {
  922. if (lrs->is_unnamed) {
  923. tor_fragile_assert(); /* nor should this. */
  924. smartlist_clear(matches);
  925. best=NULL;
  926. break;
  927. }
  928. smartlist_add(matches, lrs);
  929. best = lrs;
  930. }
  931. }
  932. });
  933. if (smartlist_len(matches)>1 && warn_if_unnamed) {
  934. int any_unwarned=0;
  935. SMARTLIST_FOREACH(matches, routerstatus_t *, lrs,
  936. {
  937. if (! lrs->name_lookup_warned) {
  938. lrs->name_lookup_warned=1;
  939. any_unwarned=1;
  940. }
  941. });
  942. if (any_unwarned) {
  943. log_warn(LD_CONFIG,"There are multiple matches for the nickname \"%s\","
  944. " but none is listed as named by the directory authorities. "
  945. "Choosing one arbitrarily.", nickname);
  946. }
  947. } else if (warn_if_unnamed && best && !best->name_lookup_warned) {
  948. char fp[HEX_DIGEST_LEN+1];
  949. base16_encode(fp, sizeof(fp),
  950. best->identity_digest, DIGEST_LEN);
  951. log_warn(LD_CONFIG,
  952. "When looking up a status, you specified a server \"%s\" by name, "
  953. "but the directory authorities do not have any key registered for "
  954. "this nickname -- so it could be used by any server, "
  955. "not just the one you meant. "
  956. "To make sure you get the same server in the future, refer to "
  957. "it by key, as \"$%s\".", nickname, fp);
  958. best->name_lookup_warned = 1;
  959. }
  960. smartlist_free(matches);
  961. return best;
  962. }
  963. /** Return the identity digest that's mapped to officially by
  964. * <b>nickname</b>. */
  965. const char *
  966. networkstatus_get_router_digest_by_nickname(const char *nickname)
  967. {
  968. if (!named_server_map)
  969. return NULL;
  970. return strmap_get_lc(named_server_map, nickname);
  971. }
  972. /** Return true iff <b>nickname</b> is disallowed from being the nickname
  973. * of any server. */
  974. int
  975. networkstatus_nickname_is_unnamed(const char *nickname)
  976. {
  977. if (!unnamed_server_map)
  978. return 0;
  979. return strmap_get_lc(unnamed_server_map, nickname) != NULL;
  980. }
  981. /** How frequently do directory authorities re-download fresh networkstatus
  982. * documents? */
  983. #define AUTHORITY_NS_CACHE_INTERVAL (10*60)
  984. /** How frequently do non-authority directory caches re-download fresh
  985. * networkstatus documents? */
  986. #define NONAUTHORITY_NS_CACHE_INTERVAL (60*60)
  987. /** We are a directory server, and so cache network_status documents.
  988. * Initiate downloads as needed to update them. For v2 authorities,
  989. * this means asking each trusted directory for its network-status.
  990. * For caches, this means asking a random v2 authority for all
  991. * network-statuses.
  992. */
  993. static void
  994. update_v2_networkstatus_cache_downloads(time_t now)
  995. {
  996. int authority = authdir_mode_v2(get_options());
  997. int interval =
  998. authority ? AUTHORITY_NS_CACHE_INTERVAL : NONAUTHORITY_NS_CACHE_INTERVAL;
  999. const smartlist_t *trusted_dir_servers = router_get_trusted_dir_servers();
  1000. if (last_networkstatus_download_attempted + interval >= now)
  1001. return;
  1002. last_networkstatus_download_attempted = now;
  1003. if (authority) {
  1004. /* An authority launches a separate connection for everybody. */
  1005. SMARTLIST_FOREACH_BEGIN(trusted_dir_servers, trusted_dir_server_t *, ds)
  1006. {
  1007. char resource[HEX_DIGEST_LEN+6]; /* fp/hexdigit.z\0 */
  1008. tor_addr_t addr;
  1009. if (!(ds->type & V2_AUTHORITY))
  1010. continue;
  1011. if (router_digest_is_me(ds->digest))
  1012. continue;
  1013. tor_addr_from_ipv4h(&addr, ds->addr);
  1014. /* Is this quite sensible with IPv6 or multiple addresses? */
  1015. if (connection_get_by_type_addr_port_purpose(
  1016. CONN_TYPE_DIR, &addr, ds->dir_port,
  1017. DIR_PURPOSE_FETCH_V2_NETWORKSTATUS)) {
  1018. /* XXX the above dir_port won't be accurate if we're
  1019. * doing a tunneled conn. In that case it should be or_port.
  1020. * How to guess from here? Maybe make the function less general
  1021. * and have it know that it's looking for dir conns. -RD */
  1022. /* Only directory caches download v2 networkstatuses, and they
  1023. * don't use tunneled connections. I think it's okay to ignore
  1024. * this. */
  1025. continue;
  1026. }
  1027. strlcpy(resource, "fp/", sizeof(resource));
  1028. base16_encode(resource+3, sizeof(resource)-3, ds->digest, DIGEST_LEN);
  1029. strlcat(resource, ".z", sizeof(resource));
  1030. directory_initiate_command_routerstatus(
  1031. &ds->fake_status, DIR_PURPOSE_FETCH_V2_NETWORKSTATUS,
  1032. ROUTER_PURPOSE_GENERAL,
  1033. 0, /* Not private */
  1034. resource,
  1035. NULL, 0 /* No payload. */,
  1036. 0 /* No I-M-S. */);
  1037. }
  1038. SMARTLIST_FOREACH_END(ds);
  1039. } else {
  1040. /* A non-authority cache launches one connection to a random authority. */
  1041. /* (Check whether we're currently fetching network-status objects.) */
  1042. if (!connection_get_by_type_purpose(CONN_TYPE_DIR,
  1043. DIR_PURPOSE_FETCH_V2_NETWORKSTATUS))
  1044. directory_get_from_dirserver(DIR_PURPOSE_FETCH_V2_NETWORKSTATUS,
  1045. ROUTER_PURPOSE_GENERAL, "all.z",
  1046. PDS_RETRY_IF_NO_SERVERS);
  1047. }
  1048. }
  1049. /** How many times will we try to fetch a consensus before we give up? */
  1050. #define CONSENSUS_NETWORKSTATUS_MAX_DL_TRIES 8
  1051. /** How long will we hang onto a possibly live consensus for which we're
  1052. * fetching certs before we check whether there is a better one? */
  1053. #define DELAY_WHILE_FETCHING_CERTS (20*60)
  1054. /** If we want to download a fresh consensus, launch a new download as
  1055. * appropriate. */
  1056. static void
  1057. update_consensus_networkstatus_downloads(time_t now)
  1058. {
  1059. or_options_t *options = get_options();
  1060. int i;
  1061. if (!networkstatus_get_live_consensus(now))
  1062. time_to_download_next_consensus = now; /* No live consensus? Get one now!*/
  1063. if (time_to_download_next_consensus > now)
  1064. return; /* Wait until the current consensus is older. */
  1065. if (authdir_mode_v3(options))
  1066. return; /* Authorities never fetch a consensus */
  1067. /* XXXXNM Microdescs: may need to download more types. */
  1068. if (!download_status_is_ready(&consensus_dl_status[FLAV_NS], now,
  1069. CONSENSUS_NETWORKSTATUS_MAX_DL_TRIES))
  1070. return; /* We failed downloading a consensus too recently. */
  1071. if (connection_get_by_type_purpose(CONN_TYPE_DIR,
  1072. DIR_PURPOSE_FETCH_CONSENSUS))
  1073. return; /* There's an in-progress download.*/
  1074. for (i=0; i < N_CONSENSUS_FLAVORS; ++i) {
  1075. consensus_waiting_for_certs_t *waiting = &consensus_waiting_for_certs[i];
  1076. if (waiting->consensus) {
  1077. /* XXXX make sure this doesn't delay sane downloads. */
  1078. if (waiting->set_at + DELAY_WHILE_FETCHING_CERTS > now)
  1079. return; /* We're still getting certs for this one. */
  1080. else {
  1081. if (!waiting->dl_failed) {
  1082. download_status_failed(&consensus_dl_status[FLAV_NS], 0);
  1083. waiting->dl_failed=1;
  1084. }
  1085. }
  1086. }
  1087. }
  1088. log_info(LD_DIR, "Launching networkstatus consensus download.");
  1089. directory_get_from_dirserver(DIR_PURPOSE_FETCH_CONSENSUS,
  1090. ROUTER_PURPOSE_GENERAL, NULL,
  1091. PDS_RETRY_IF_NO_SERVERS);
  1092. }
  1093. /** Called when an attempt to download a consensus fails: note that the
  1094. * failure occurred, and possibly retry. */
  1095. void
  1096. networkstatus_consensus_download_failed(int status_code)
  1097. {
  1098. /* XXXXNM Microdescs: may need to handle more types. */
  1099. download_status_failed(&consensus_dl_status[FLAV_NS], status_code);
  1100. /* Retry immediately, if appropriate. */
  1101. update_consensus_networkstatus_downloads(time(NULL));
  1102. }
  1103. /** How long do we (as a cache) wait after a consensus becomes non-fresh
  1104. * before trying to fetch another? */
  1105. #define CONSENSUS_MIN_SECONDS_BEFORE_CACHING 120
  1106. /** Update the time at which we'll consider replacing the current
  1107. * consensus. */
  1108. void
  1109. update_consensus_networkstatus_fetch_time(time_t now)
  1110. {
  1111. or_options_t *options = get_options();
  1112. networkstatus_t *c = networkstatus_get_live_consensus(now);
  1113. if (c) {
  1114. long dl_interval;
  1115. long interval = c->fresh_until - c->valid_after;
  1116. time_t start;
  1117. if (directory_fetches_dir_info_early(options)) {
  1118. /* We want to cache the next one at some point after this one
  1119. * is no longer fresh... */
  1120. start = c->fresh_until + CONSENSUS_MIN_SECONDS_BEFORE_CACHING;
  1121. /* Some clients may need the consensus sooner than others. */
  1122. if (options->FetchDirInfoExtraEarly) {
  1123. dl_interval = 60;
  1124. } else {
  1125. /* But only in the first half-interval after that. */
  1126. dl_interval = interval/2;
  1127. }
  1128. } else {
  1129. /* We're an ordinary client or a bridge. Give all the caches enough
  1130. * time to download the consensus. */
  1131. start = c->fresh_until + (interval*3)/4;
  1132. /* But download the next one well before this one is expired. */
  1133. dl_interval = ((c->valid_until - start) * 7 )/ 8;
  1134. /* If we're a bridge user, make use of the numbers we just computed
  1135. * to choose the rest of the interval *after* them. */
  1136. if (directory_fetches_dir_info_later(options)) {
  1137. /* Give all the *clients* enough time to download the consensus. */
  1138. start = start + dl_interval + CONSENSUS_MIN_SECONDS_BEFORE_CACHING;
  1139. /* But try to get it before ours actually expires. */
  1140. dl_interval = (c->valid_until - start) -
  1141. CONSENSUS_MIN_SECONDS_BEFORE_CACHING;
  1142. }
  1143. }
  1144. if (dl_interval < 1)
  1145. dl_interval = 1;
  1146. /* We must not try to replace c while it's still fresh: */
  1147. tor_assert(c->fresh_until < start);
  1148. /* We must download the next one before c is invalid: */
  1149. tor_assert(start+dl_interval < c->valid_until);
  1150. time_to_download_next_consensus = start +crypto_rand_int((int)dl_interval);
  1151. {
  1152. char tbuf1[ISO_TIME_LEN+1];
  1153. char tbuf2[ISO_TIME_LEN+1];
  1154. char tbuf3[ISO_TIME_LEN+1];
  1155. format_local_iso_time(tbuf1, c->fresh_until);
  1156. format_local_iso_time(tbuf2, c->valid_until);
  1157. format_local_iso_time(tbuf3, time_to_download_next_consensus);
  1158. log_info(LD_DIR, "Live consensus %s the most recent until %s and will "
  1159. "expire at %s; fetching the next one at %s.",
  1160. (c->fresh_until > now) ? "will be" : "was",
  1161. tbuf1, tbuf2, tbuf3);
  1162. }
  1163. } else {
  1164. time_to_download_next_consensus = now;
  1165. log_info(LD_DIR, "No live consensus; we should fetch one immediately.");
  1166. }
  1167. }
  1168. /** Return 1 if there's a reason we shouldn't try any directory
  1169. * fetches yet (e.g. we demand bridges and none are yet known).
  1170. * Else return 0. */
  1171. int
  1172. should_delay_dir_fetches(or_options_t *options)
  1173. {
  1174. if (options->UseBridges && !any_bridge_descriptors_known()) {
  1175. log_info(LD_DIR, "delaying dir fetches (no running bridges known)");
  1176. return 1;
  1177. }
  1178. return 0;
  1179. }
  1180. /** Launch requests for networkstatus documents and authority certificates as
  1181. * appropriate. */
  1182. void
  1183. update_networkstatus_downloads(time_t now)
  1184. {
  1185. or_options_t *options = get_options();
  1186. if (should_delay_dir_fetches(options))
  1187. return;
  1188. if (directory_fetches_dir_info_early(options))
  1189. update_v2_networkstatus_cache_downloads(now);
  1190. update_consensus_networkstatus_downloads(now);
  1191. update_certificate_downloads(now);
  1192. }
  1193. /** Launch requests as appropriate for missing directory authority
  1194. * certificates. */
  1195. void
  1196. update_certificate_downloads(time_t now)
  1197. {
  1198. int i;
  1199. for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
  1200. if (consensus_waiting_for_certs[i].consensus)
  1201. authority_certs_fetch_missing(consensus_waiting_for_certs[i].consensus,
  1202. now);
  1203. }
  1204. authority_certs_fetch_missing(current_consensus, now);
  1205. }
  1206. /** Return 1 if we have a consensus but we don't have enough certificates
  1207. * to start using it yet. */
  1208. int
  1209. consensus_is_waiting_for_certs(void)
  1210. {
  1211. return consensus_waiting_for_certs[USABLE_CONSENSUS_FLAVOR].consensus
  1212. ? 1 : 0;
  1213. }
  1214. /** Return the network status with a given identity digest. */
  1215. networkstatus_v2_t *
  1216. networkstatus_v2_get_by_digest(const char *digest)
  1217. {
  1218. SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
  1219. {
  1220. if (!memcmp(ns->identity_digest, digest, DIGEST_LEN))
  1221. return ns;
  1222. });
  1223. return NULL;
  1224. }
  1225. /** Return the most recent consensus that we have downloaded, or NULL if we
  1226. * don't have one. */
  1227. networkstatus_t *
  1228. networkstatus_get_latest_consensus(void)
  1229. {
  1230. return current_consensus;
  1231. }
  1232. /** Return the most recent consensus that we have downloaded, or NULL if it is
  1233. * no longer live. */
  1234. networkstatus_t *
  1235. networkstatus_get_live_consensus(time_t now)
  1236. {
  1237. if (current_consensus &&
  1238. current_consensus->valid_after <= now &&
  1239. now <= current_consensus->valid_until)
  1240. return current_consensus;
  1241. else
  1242. return NULL;
  1243. }
  1244. /* XXXX remove this in favor of get_live_consensus. But actually,
  1245. * leave something like it for bridge users, who need to not totally
  1246. * lose if they spend a while fetching a new consensus. */
  1247. /** As networkstatus_get_live_consensus(), but is way more tolerant of expired
  1248. * consensuses. */
  1249. networkstatus_t *
  1250. networkstatus_get_reasonably_live_consensus(time_t now)
  1251. {
  1252. #define REASONABLY_LIVE_TIME (24*60*60)
  1253. if (current_consensus &&
  1254. current_consensus->valid_after <= now &&
  1255. now <= current_consensus->valid_until+REASONABLY_LIVE_TIME)
  1256. return current_consensus;
  1257. else
  1258. return NULL;
  1259. }
  1260. /** Given two router status entries for the same router identity, return 1 if
  1261. * if the contents have changed between them. Otherwise, return 0. */
  1262. static int
  1263. routerstatus_has_changed(const routerstatus_t *a, const routerstatus_t *b)
  1264. {
  1265. tor_assert(!memcmp(a->identity_digest, b->identity_digest, DIGEST_LEN));
  1266. return strcmp(a->nickname, b->nickname) ||
  1267. memcmp(a->descriptor_digest, b->descriptor_digest, DIGEST_LEN) ||
  1268. a->addr != b->addr ||
  1269. a->or_port != b->or_port ||
  1270. a->dir_port != b->dir_port ||
  1271. a->is_authority != b->is_authority ||
  1272. a->is_exit != b->is_exit ||
  1273. a->is_stable != b->is_stable ||
  1274. a->is_fast != b->is_fast ||
  1275. a->is_running != b->is_running ||
  1276. a->is_named != b->is_named ||
  1277. a->is_unnamed != b->is_unnamed ||
  1278. a->is_valid != b->is_valid ||
  1279. a->is_v2_dir != b->is_v2_dir ||
  1280. a->is_possible_guard != b->is_possible_guard ||
  1281. a->is_bad_exit != b->is_bad_exit ||
  1282. a->is_bad_directory != b->is_bad_directory ||
  1283. a->is_hs_dir != b->is_hs_dir ||
  1284. a->version_known != b->version_known ||
  1285. a->version_supports_begindir != b->version_supports_begindir ||
  1286. a->version_supports_extrainfo_upload !=
  1287. b->version_supports_extrainfo_upload ||
  1288. a->version_supports_conditional_consensus !=
  1289. b->version_supports_conditional_consensus ||
  1290. a->version_supports_v3_dir != b->version_supports_v3_dir;
  1291. }
  1292. /** Notify controllers of any router status entries that changed between
  1293. * <b>old_c</b> and <b>new_c</b>. */
  1294. static void
  1295. notify_control_networkstatus_changed(const networkstatus_t *old_c,
  1296. const networkstatus_t *new_c)
  1297. {
  1298. smartlist_t *changed;
  1299. if (old_c == new_c)
  1300. return;
  1301. /* tell the controller exactly which relays are still listed, as well
  1302. * as what they're listed as */
  1303. control_event_newconsensus(new_c);
  1304. if (!control_event_is_interesting(EVENT_NS))
  1305. return;
  1306. if (!old_c) {
  1307. control_event_networkstatus_changed(new_c->routerstatus_list);
  1308. return;
  1309. }
  1310. changed = smartlist_create();
  1311. SMARTLIST_FOREACH_JOIN(old_c->routerstatus_list, routerstatus_t *, rs_old,
  1312. new_c->routerstatus_list, routerstatus_t *, rs_new,
  1313. memcmp(rs_old->identity_digest,
  1314. rs_new->identity_digest, DIGEST_LEN),
  1315. smartlist_add(changed, rs_new)) {
  1316. if (routerstatus_has_changed(rs_old, rs_new))
  1317. smartlist_add(changed, rs_new);
  1318. } SMARTLIST_FOREACH_JOIN_END(rs_old, rs_new);
  1319. control_event_networkstatus_changed(changed);
  1320. smartlist_free(changed);
  1321. }
  1322. /** Copy all the ancillary information (like router download status and so on)
  1323. * from <b>old_c</b> to <b>new_c</b>. */
  1324. static void
  1325. networkstatus_copy_old_consensus_info(networkstatus_t *new_c,
  1326. const networkstatus_t *old_c)
  1327. {
  1328. if (old_c == new_c)
  1329. return;
  1330. if (!old_c || !smartlist_len(old_c->routerstatus_list))
  1331. return;
  1332. SMARTLIST_FOREACH_JOIN(old_c->routerstatus_list, routerstatus_t *, rs_old,
  1333. new_c->routerstatus_list, routerstatus_t *, rs_new,
  1334. memcmp(rs_old->identity_digest,
  1335. rs_new->identity_digest, DIGEST_LEN),
  1336. STMT_NIL) {
  1337. /* Okay, so we're looking at the same identity. */
  1338. rs_new->name_lookup_warned = rs_old->name_lookup_warned;
  1339. rs_new->last_dir_503_at = rs_old->last_dir_503_at;
  1340. if (!memcmp(rs_old->descriptor_digest, rs_new->descriptor_digest,
  1341. DIGEST_LEN)) {
  1342. /* And the same descriptor too! */
  1343. memcpy(&rs_new->dl_status, &rs_old->dl_status,sizeof(download_status_t));
  1344. }
  1345. } SMARTLIST_FOREACH_JOIN_END(rs_old, rs_new);
  1346. }
  1347. /** Try to replace the current cached v3 networkstatus with the one in
  1348. * <b>consensus</b>. If we don't have enough certificates to validate it,
  1349. * store it in consensus_waiting_for_certs and launch a certificate fetch.
  1350. *
  1351. * If flags & NSSET_FROM_CACHE, this networkstatus has come from the disk
  1352. * cache. If flags & NSSET_WAS_WAITING_FOR_CERTS, this networkstatus was
  1353. * already received, but we were waiting for certificates on it. If flags &
  1354. * NSSET_DONT_DOWNLOAD_CERTS, do not launch certificate downloads as needed.
  1355. * If flags & NSSET_ACCEPT_OBSOLETE, then we should be willing to take this
  1356. * consensus, even if it comes from many days in the past.
  1357. *
  1358. * Return 0 on success, <0 on failure. On failure, caller should increment
  1359. * the failure count as appropriate.
  1360. *
  1361. * We return -1 for mild failures that don't need to be reported to the
  1362. * user, and -2 for more serious problems.
  1363. */
  1364. int
  1365. networkstatus_set_current_consensus(const char *consensus,
  1366. const char *flavor,
  1367. unsigned flags)
  1368. {
  1369. networkstatus_t *c=NULL;
  1370. int r, result = -1;
  1371. time_t now = time(NULL);
  1372. char *unverified_fname = NULL, *consensus_fname = NULL;
  1373. int flav = networkstatus_parse_flavor_name(flavor);
  1374. const unsigned from_cache = flags & NSSET_FROM_CACHE;
  1375. const unsigned was_waiting_for_certs = flags & NSSET_WAS_WAITING_FOR_CERTS;
  1376. const unsigned dl_certs = !(flags & NSSET_DONT_DOWNLOAD_CERTS);
  1377. const unsigned accept_obsolete = flags & NSSET_ACCEPT_OBSOLETE;
  1378. const unsigned require_flavor = flags & NSSET_REQUIRE_FLAVOR;
  1379. const digests_t *current_digests = NULL;
  1380. consensus_waiting_for_certs_t *waiting = NULL;
  1381. time_t current_valid_after = 0;
  1382. if (flav < 0) {
  1383. /* XXXX we don't handle unrecognized flavors yet. */
  1384. log_warn(LD_BUG, "Unrecognized consensus flavor %s", flavor);
  1385. return -2;
  1386. }
  1387. /* Make sure it's parseable. */
  1388. c = networkstatus_parse_vote_from_string(consensus, NULL, NS_TYPE_CONSENSUS);
  1389. if (!c) {
  1390. log_warn(LD_DIR, "Unable to parse networkstatus consensus");
  1391. result = -2;
  1392. goto done;
  1393. }
  1394. if ((int)c->flavor != flav) {
  1395. /* This wasn't the flavor we thought we were getting. */
  1396. if (require_flavor) {
  1397. log_warn(LD_DIR, "Got consensus with unexpected flavor %s (wanted %s)",
  1398. networkstatus_get_flavor_name(c->flavor), flavor);
  1399. goto done;
  1400. }
  1401. flav = c->flavor;
  1402. flavor = networkstatus_get_flavor_name(flav);
  1403. }
  1404. if (flav != USABLE_CONSENSUS_FLAVOR &&
  1405. !directory_caches_dir_info(get_options())) {
  1406. /* This consensus is totally boring to us: we won't use it, and we won't
  1407. * serve it. Drop it. */
  1408. goto done;
  1409. }
  1410. if (from_cache && !accept_obsolete &&
  1411. c->valid_until < now-OLD_ROUTER_DESC_MAX_AGE) {
  1412. /* XXX022 when we try to make fallbackconsensus work again, we should
  1413. * consider taking this out. Until then, believing obsolete consensuses
  1414. * is causing more harm than good. See also bug 887. */
  1415. log_info(LD_DIR, "Loaded an expired consensus. Discarding.");
  1416. goto done;
  1417. }
  1418. if (!strcmp(flavor, "ns")) {
  1419. consensus_fname = get_datadir_fname("cached-consensus");
  1420. unverified_fname = get_datadir_fname("unverified-consensus");
  1421. if (current_consensus) {
  1422. current_digests = &current_consensus->digests;
  1423. current_valid_after = current_consensus->valid_after;
  1424. }
  1425. } else {
  1426. cached_dir_t *cur;
  1427. char buf[128];
  1428. tor_snprintf(buf, sizeof(buf), "cached-%s-consensus", flavor);
  1429. consensus_fname = get_datadir_fname(buf);
  1430. tor_snprintf(buf, sizeof(buf), "unverified-%s-consensus", flavor);
  1431. unverified_fname = get_datadir_fname(buf);
  1432. cur = dirserv_get_consensus(flavor);
  1433. if (cur) {
  1434. current_digests = &cur->digests;
  1435. current_valid_after = cur->published;
  1436. }
  1437. }
  1438. if (current_digests &&
  1439. !memcmp(&c->digests, current_digests, sizeof(c->digests))) {
  1440. /* We already have this one. That's a failure. */
  1441. log_info(LD_DIR, "Got a %s consensus we already have", flavor);
  1442. goto done;
  1443. }
  1444. if (current_valid_after && c->valid_after <= current_valid_after) {
  1445. /* We have a newer one. There's no point in accepting this one,
  1446. * even if it's great. */
  1447. log_info(LD_DIR, "Got a %s consensus at least as old as the one we have",
  1448. flavor);
  1449. goto done;
  1450. }
  1451. /* Make sure it's signed enough. */
  1452. if ((r=networkstatus_check_consensus_signature(c, 1))<0) {
  1453. if (r == -1) {
  1454. /* Okay, so it _might_ be signed enough if we get more certificates. */
  1455. if (!was_waiting_for_certs) {
  1456. log_info(LD_DIR,
  1457. "Not enough certificates to check networkstatus consensus");
  1458. }
  1459. if (!current_valid_after ||
  1460. c->valid_after > current_valid_after) {
  1461. waiting = &consensus_waiting_for_certs[flav];
  1462. networkstatus_vote_free(waiting->consensus);
  1463. tor_free(waiting->body);
  1464. waiting->consensus = c;
  1465. c = NULL; /* Prevent free. */
  1466. waiting->body = tor_strdup(consensus);
  1467. waiting->set_at = now;
  1468. waiting->dl_failed = 0;
  1469. if (!from_cache) {
  1470. write_str_to_file(unverified_fname, consensus, 0);
  1471. }
  1472. if (dl_certs)
  1473. authority_certs_fetch_missing(c, now);
  1474. /* This case is not a success or a failure until we get the certs
  1475. * or fail to get the certs. */
  1476. result = 0;
  1477. } else {
  1478. /* Even if we had enough signatures, we'd never use this as the
  1479. * latest consensus. */
  1480. if (was_waiting_for_certs && from_cache)
  1481. unlink(unverified_fname);
  1482. }
  1483. goto done;
  1484. } else {
  1485. /* This can never be signed enough: Kill it. */
  1486. if (!was_waiting_for_certs) {
  1487. log_warn(LD_DIR, "Not enough good signatures on networkstatus "
  1488. "consensus");
  1489. result = -2;
  1490. }
  1491. if (was_waiting_for_certs && (r < -1) && from_cache)
  1492. unlink(unverified_fname);
  1493. goto done;
  1494. }
  1495. }
  1496. if (!from_cache && flav == USABLE_CONSENSUS_FLAVOR)
  1497. control_event_client_status(LOG_NOTICE, "CONSENSUS_ARRIVED");
  1498. /* Are we missing any certificates at all? */
  1499. if (r != 1 && dl_certs)
  1500. authority_certs_fetch_missing(c, now);
  1501. if (flav == USABLE_CONSENSUS_FLAVOR) {
  1502. notify_control_networkstatus_changed(current_consensus, c);
  1503. if (current_consensus) {
  1504. networkstatus_copy_old_consensus_info(c, current_consensus);
  1505. networkstatus_vote_free(current_consensus);
  1506. }
  1507. }
  1508. waiting = &consensus_waiting_for_certs[flav];
  1509. if (waiting->consensus &&
  1510. waiting->consensus->valid_after <= c->valid_after) {
  1511. networkstatus_vote_free(waiting->consensus);
  1512. waiting->consensus = NULL;
  1513. if (consensus != waiting->body)
  1514. tor_free(waiting->body);
  1515. else
  1516. waiting->body = NULL;
  1517. waiting->set_at = 0;
  1518. waiting->dl_failed = 0;
  1519. unlink(unverified_fname);
  1520. }
  1521. /* Reset the failure count only if this consensus is actually valid. */
  1522. if (c->valid_after <= now && now <= c->valid_until) {
  1523. download_status_reset(&consensus_dl_status[flav]);
  1524. } else {
  1525. if (!from_cache)
  1526. download_status_failed(&consensus_dl_status[flav], 0);
  1527. }
  1528. if (directory_caches_dir_info(get_options())) {
  1529. dirserv_set_cached_consensus_networkstatus(consensus,
  1530. flavor,
  1531. &c->digests,
  1532. c->valid_after);
  1533. }
  1534. if (flav == USABLE_CONSENSUS_FLAVOR) {
  1535. current_consensus = c;
  1536. c = NULL; /* Prevent free. */
  1537. /* XXXXNM Microdescs: needs a non-ns variant. */
  1538. update_consensus_networkstatus_fetch_time(now);
  1539. dirvote_recalculate_timing(get_options(), now);
  1540. routerstatus_list_update_named_server_map();
  1541. cell_ewma_set_scale_factor(get_options(), current_consensus);
  1542. circuit_build_times_new_consensus_params(&circ_times, current_consensus);
  1543. }
  1544. if (!from_cache) {
  1545. write_str_to_file(consensus_fname, consensus, 0);
  1546. }
  1547. if (ftime_definitely_before(now, current_consensus->valid_after)) {
  1548. char tbuf[ISO_TIME_LEN+1];
  1549. char dbuf[64];
  1550. long delta = now - current_consensus->valid_after;
  1551. format_iso_time(tbuf, current_consensus->valid_after);
  1552. format_time_interval(dbuf, sizeof(dbuf), delta);
  1553. log_warn(LD_GENERAL, "Our clock is %s behind the time published in the "
  1554. "consensus network status document (%s GMT). Tor needs an "
  1555. "accurate clock to work correctly. Please check your time and "
  1556. "date settings!", dbuf, tbuf);
  1557. control_event_general_status(LOG_WARN,
  1558. "CLOCK_SKEW MIN_SKEW=%ld SOURCE=CONSENSUS", delta);
  1559. }
  1560. router_dir_info_changed();
  1561. result = 0;
  1562. done:
  1563. networkstatus_vote_free(c);
  1564. tor_free(consensus_fname);
  1565. tor_free(unverified_fname);
  1566. return result;
  1567. }
  1568. /** Called when we have gotten more certificates: see whether we can
  1569. * now verify a pending consensus. */
  1570. void
  1571. networkstatus_note_certs_arrived(void)
  1572. {
  1573. int i;
  1574. for (i=0; i<N_CONSENSUS_FLAVORS; ++i) {
  1575. consensus_waiting_for_certs_t *waiting = &consensus_waiting_for_certs[i];
  1576. if (!waiting->consensus)
  1577. continue;
  1578. if (networkstatus_check_consensus_signature(waiting->consensus, 0)>=0) {
  1579. if (!networkstatus_set_current_consensus(
  1580. waiting->body,
  1581. networkstatus_get_flavor_name(i),
  1582. NSSET_WAS_WAITING_FOR_CERTS)) {
  1583. tor_free(waiting->body);
  1584. }
  1585. }
  1586. }
  1587. }
  1588. /** If the network-status list has changed since the last time we called this
  1589. * function, update the status of every routerinfo from the network-status
  1590. * list. If <b>dir_version</b> is 2, it's a v2 networkstatus that changed.
  1591. * If <b>dir_version</b> is 3, it's a v3 consensus that changed.
  1592. */
  1593. void
  1594. routers_update_all_from_networkstatus(time_t now, int dir_version)
  1595. {
  1596. routerlist_t *rl = router_get_routerlist();
  1597. networkstatus_t *consensus = networkstatus_get_live_consensus(now);
  1598. if (networkstatus_v2_list_has_changed)
  1599. download_status_map_update_from_v2_networkstatus();
  1600. if (!consensus || dir_version < 3) /* nothing more we should do */
  1601. return;
  1602. /* calls router_dir_info_changed() when it's done -- more routers
  1603. * might be up or down now, which might affect whether there's enough
  1604. * directory info. */
  1605. routers_update_status_from_consensus_networkstatus(rl->routers, 0);
  1606. SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri,
  1607. ri->cache_info.routerlist_index = ri_sl_idx);
  1608. if (rl->old_routers)
  1609. signed_descs_update_status_from_consensus_networkstatus(rl->old_routers);
  1610. if (!have_warned_about_old_version) {
  1611. int is_server = server_mode(get_options());
  1612. version_status_t status;
  1613. const char *recommended = is_server ?
  1614. consensus->server_versions : consensus->client_versions;
  1615. status = tor_version_is_obsolete(VERSION, recommended);
  1616. if (status == VS_RECOMMENDED) {
  1617. log_info(LD_GENERAL, "The directory authorities say my version is ok.");
  1618. } else if (status == VS_EMPTY) {
  1619. log_info(LD_GENERAL,
  1620. "The directory authorities don't recommend any versions.");
  1621. } else if (status == VS_NEW || status == VS_NEW_IN_SERIES) {
  1622. if (!have_warned_about_new_version) {
  1623. log_notice(LD_GENERAL, "This version of Tor (%s) is newer than any "
  1624. "recommended version%s, according to the directory "
  1625. "authorities. Recommended versions are: %s",
  1626. VERSION,
  1627. status == VS_NEW_IN_SERIES ? " in its series" : "",
  1628. recommended);
  1629. have_warned_about_new_version = 1;
  1630. control_event_general_status(LOG_WARN, "DANGEROUS_VERSION "
  1631. "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
  1632. VERSION, "NEW", recommended);
  1633. }
  1634. } else {
  1635. log_warn(LD_GENERAL, "Please upgrade! "
  1636. "This version of Tor (%s) is %s, according to the directory "
  1637. "authorities. Recommended versions are: %s",
  1638. VERSION,
  1639. status == VS_OLD ? "obsolete" : "not recommended",
  1640. recommended);
  1641. have_warned_about_old_version = 1;
  1642. control_event_general_status(LOG_WARN, "DANGEROUS_VERSION "
  1643. "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
  1644. VERSION, status == VS_OLD ? "OBSOLETE" : "UNRECOMMENDED",
  1645. recommended);
  1646. }
  1647. }
  1648. }
  1649. /** Update v2_download_status_map to contain an entry for every router
  1650. * descriptor listed in the v2 networkstatuses. */
  1651. static void
  1652. download_status_map_update_from_v2_networkstatus(void)
  1653. {
  1654. digestmap_t *dl_status;
  1655. if (!networkstatus_v2_list)
  1656. return;
  1657. if (!v2_download_status_map)
  1658. v2_download_status_map = digestmap_new();
  1659. dl_status = digestmap_new();
  1660. SMARTLIST_FOREACH_BEGIN(networkstatus_v2_list, networkstatus_v2_t *, ns) {
  1661. SMARTLIST_FOREACH_BEGIN(ns->entries, routerstatus_t *, rs) {
  1662. const char *d = rs->descriptor_digest;
  1663. download_status_t *s;
  1664. if (digestmap_get(dl_status, d))
  1665. continue;
  1666. if (!(s = digestmap_remove(v2_download_status_map, d))) {
  1667. s = tor_malloc_zero(sizeof(download_status_t));
  1668. }
  1669. digestmap_set(dl_status, d, s);
  1670. } SMARTLIST_FOREACH_END(rs);
  1671. } SMARTLIST_FOREACH_END(ns);
  1672. digestmap_free(v2_download_status_map, _tor_free);
  1673. v2_download_status_map = dl_status;
  1674. networkstatus_v2_list_has_changed = 0;
  1675. }
  1676. /** Update our view of the list of named servers from the most recently
  1677. * retrieved networkstatus consensus. */
  1678. static void
  1679. routerstatus_list_update_named_server_map(void)
  1680. {
  1681. if (!current_consensus)
  1682. return;
  1683. strmap_free(named_server_map, _tor_free);
  1684. named_server_map = strmap_new();
  1685. strmap_free(unnamed_server_map, NULL);
  1686. unnamed_server_map = strmap_new();
  1687. SMARTLIST_FOREACH(current_consensus->routerstatus_list, routerstatus_t *, rs,
  1688. {
  1689. if (rs->is_named) {
  1690. strmap_set_lc(named_server_map, rs->nickname,
  1691. tor_memdup(rs->identity_digest, DIGEST_LEN));
  1692. }
  1693. if (rs->is_unnamed) {
  1694. strmap_set_lc(unnamed_server_map, rs->nickname, (void*)1);
  1695. }
  1696. });
  1697. }
  1698. /** Given a list <b>routers</b> of routerinfo_t *, update each status field
  1699. * according to our current consensus networkstatus. May re-order
  1700. * <b>routers</b>. */
  1701. void
  1702. routers_update_status_from_consensus_networkstatus(smartlist_t *routers,
  1703. int reset_failures)
  1704. {
  1705. trusted_dir_server_t *ds;
  1706. or_options_t *options = get_options();
  1707. int authdir = authdir_mode_v2(options) || authdir_mode_v3(options);
  1708. int namingdir = authdir && options->NamingAuthoritativeDir;
  1709. networkstatus_t *ns = current_consensus;
  1710. if (!ns || !smartlist_len(ns->routerstatus_list))
  1711. return;
  1712. if (!networkstatus_v2_list)
  1713. networkstatus_v2_list = smartlist_create();
  1714. routers_sort_by_identity(routers);
  1715. SMARTLIST_FOREACH_JOIN(ns->routerstatus_list, routerstatus_t *, rs,
  1716. routers, routerinfo_t *, router,
  1717. memcmp(rs->identity_digest,
  1718. router->cache_info.identity_digest, DIGEST_LEN),
  1719. {
  1720. /* We have no routerstatus for this router. Clear flags and skip it. */
  1721. if (!namingdir)
  1722. router->is_named = 0;
  1723. if (!authdir) {
  1724. if (router->purpose == ROUTER_PURPOSE_GENERAL)
  1725. router_clear_status_flags(router);
  1726. }
  1727. }) {
  1728. /* We have a routerstatus for this router. */
  1729. const char *digest = router->cache_info.identity_digest;
  1730. ds = router_get_trusteddirserver_by_digest(digest);
  1731. if (!namingdir) {
  1732. if (rs->is_named && !strcasecmp(router->nickname, rs->nickname))
  1733. router->is_named = 1;
  1734. else
  1735. router->is_named = 0;
  1736. }
  1737. /* Is it the same descriptor, or only the same identity? */
  1738. if (!memcmp(router->cache_info.signed_descriptor_digest,
  1739. rs->descriptor_digest, DIGEST_LEN)) {
  1740. if (ns->valid_until > router->cache_info.last_listed_as_valid_until)
  1741. router->cache_info.last_listed_as_valid_until = ns->valid_until;
  1742. }
  1743. if (!authdir) {
  1744. /* If we're not an authdir, believe others. */
  1745. router->is_valid = rs->is_valid;
  1746. router->is_running = rs->is_running;
  1747. router->is_fast = rs->is_fast;
  1748. router->is_stable = rs->is_stable;
  1749. router->is_possible_guard = rs->is_possible_guard;
  1750. router->is_exit = rs->is_exit;
  1751. router->is_bad_directory = rs->is_bad_directory;
  1752. router->is_bad_exit = rs->is_bad_exit;
  1753. router->is_hs_dir = rs->is_hs_dir;
  1754. } else {
  1755. /* If we _are_ an authority, we should check whether this router
  1756. * is one that will cause us to need a reachability test. */
  1757. routerinfo_t *old_router =
  1758. router_get_by_digest(router->cache_info.identity_digest);
  1759. if (old_router != router) {
  1760. router->needs_retest_if_added =
  1761. dirserv_should_launch_reachability_test(router, old_router);
  1762. }
  1763. }
  1764. if (router->is_running && ds) {
  1765. download_status_reset(&ds->v2_ns_dl_status);
  1766. }
  1767. if (reset_failures) {
  1768. download_status_reset(&rs->dl_status);
  1769. }
  1770. } SMARTLIST_FOREACH_JOIN_END(rs, router);
  1771. /* Now update last_listed_as_valid_until from v2 networkstatuses. */
  1772. /* XXXX If this is slow, we need to rethink the code. */
  1773. SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns, {
  1774. time_t live_until = ns->published_on + V2_NETWORKSTATUS_ROUTER_LIFETIME;
  1775. SMARTLIST_FOREACH_JOIN(ns->entries, routerstatus_t *, rs,
  1776. routers, routerinfo_t *, ri,
  1777. memcmp(rs->identity_digest,
  1778. ri->cache_info.identity_digest, DIGEST_LEN),
  1779. STMT_NIL) {
  1780. if (!memcmp(ri->cache_info.signed_descriptor_digest,
  1781. rs->descriptor_digest, DIGEST_LEN)) {
  1782. if (live_until > ri->cache_info.last_listed_as_valid_until)
  1783. ri->cache_info.last_listed_as_valid_until = live_until;
  1784. }
  1785. } SMARTLIST_FOREACH_JOIN_END(rs, ri);
  1786. });
  1787. router_dir_info_changed();
  1788. }
  1789. /** Given a list of signed_descriptor_t, update their fields (mainly, when
  1790. * they were last listed) from the most recent consensus. */
  1791. void
  1792. signed_descs_update_status_from_consensus_networkstatus(smartlist_t *descs)
  1793. {
  1794. networkstatus_t *ns = current_consensus;
  1795. if (!ns)
  1796. return;
  1797. if (!ns->desc_digest_map) {
  1798. char dummy[DIGEST_LEN];
  1799. /* instantiates the digest map. */
  1800. memset(dummy, 0, sizeof(dummy));
  1801. router_get_consensus_status_by_descriptor_digest(dummy);
  1802. }
  1803. SMARTLIST_FOREACH(descs, signed_descriptor_t *, d,
  1804. {
  1805. routerstatus_t *rs = digestmap_get(ns->desc_digest_map,
  1806. d->signed_descriptor_digest);
  1807. if (rs) {
  1808. if (ns->valid_until > d->last_listed_as_valid_until)
  1809. d->last_listed_as_valid_until = ns->valid_until;
  1810. }
  1811. });
  1812. }
  1813. /** Generate networkstatus lines for a single routerstatus_t object, and
  1814. * return the result in a newly allocated string. Used only by controller
  1815. * interface (for now.) */
  1816. char *
  1817. networkstatus_getinfo_helper_single(routerstatus_t *rs)
  1818. {
  1819. char buf[RS_ENTRY_LEN+1];
  1820. routerstatus_format_entry(buf, sizeof(buf), rs, NULL, NS_CONTROL_PORT);
  1821. return tor_strdup(buf);
  1822. }
  1823. /** Alloc and return a string describing routerstatuses for the most
  1824. * recent info of each router we know about that is of purpose
  1825. * <b>purpose_string</b>. Return NULL if unrecognized purpose.
  1826. *
  1827. * Right now this function is oriented toward listing bridges (you
  1828. * shouldn't use this for general-purpose routers, since those
  1829. * should be listed from the consensus, not from the routers list). */
  1830. char *
  1831. networkstatus_getinfo_by_purpose(const char *purpose_string, time_t now)
  1832. {
  1833. time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
  1834. char *answer;
  1835. routerlist_t *rl = router_get_routerlist();
  1836. smartlist_t *statuses;
  1837. uint8_t purpose = router_purpose_from_string(purpose_string);
  1838. routerstatus_t rs;
  1839. int bridge_auth = authdir_mode_bridge(get_options());
  1840. if (purpose == ROUTER_PURPOSE_UNKNOWN) {
  1841. log_info(LD_DIR, "Unrecognized purpose '%s' when listing router statuses.",
  1842. purpose_string);
  1843. return NULL;
  1844. }
  1845. statuses = smartlist_create();
  1846. SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri, {
  1847. if (ri->cache_info.published_on < cutoff)
  1848. continue;
  1849. if (ri->purpose != purpose)
  1850. continue;
  1851. if (bridge_auth && ri->purpose == ROUTER_PURPOSE_BRIDGE)
  1852. dirserv_set_router_is_running(ri, now);
  1853. /* then generate and write out status lines for each of them */
  1854. set_routerstatus_from_routerinfo(&rs, ri, now, 0, 0, 0);
  1855. smartlist_add(statuses, networkstatus_getinfo_helper_single(&rs));
  1856. });
  1857. answer = smartlist_join_strings(statuses, "", 0, NULL);
  1858. SMARTLIST_FOREACH(statuses, char *, cp, tor_free(cp));
  1859. smartlist_free(statuses);
  1860. return answer;
  1861. }
  1862. /** Write out router status entries for all our bridge descriptors. */
  1863. void
  1864. networkstatus_dump_bridge_status_to_file(time_t now)
  1865. {
  1866. char *status = networkstatus_getinfo_by_purpose("bridge", now);
  1867. or_options_t *options = get_options();
  1868. size_t len = strlen(options->DataDirectory) + 32;
  1869. char *fname = tor_malloc(len);
  1870. tor_snprintf(fname, len, "%s"PATH_SEPARATOR"networkstatus-bridges",
  1871. options->DataDirectory);
  1872. write_str_to_file(fname,status,0);
  1873. tor_free(fname);
  1874. tor_free(status);
  1875. }
  1876. int32_t
  1877. get_net_param_from_list(smartlist_t *net_params, const char *param_name,
  1878. int default_val)
  1879. {
  1880. size_t name_len = strlen(param_name);
  1881. SMARTLIST_FOREACH_BEGIN(net_params, const char *, p) {
  1882. if (!strcmpstart(p, param_name) && p[name_len] == '=') {
  1883. int ok=0;
  1884. long v = tor_parse_long(p+name_len+1, 10, INT32_MIN,
  1885. INT32_MAX, &ok, NULL);
  1886. if (ok)
  1887. return (int32_t) v;
  1888. }
  1889. } SMARTLIST_FOREACH_END(p);
  1890. return default_val;
  1891. }
  1892. /** Return the value of a integer parameter from the networkstatus <b>ns</b>
  1893. * whose name is <b>param_name</b>. If <b>ns</b> is NULL, try loading the
  1894. * latest consensus ourselves. Return <b>default_val</b> if no latest
  1895. * consensus, or if it has no parameter called <b>param_name</b>. */
  1896. int32_t
  1897. networkstatus_get_param(networkstatus_t *ns, const char *param_name,
  1898. int32_t default_val)
  1899. {
  1900. if (!ns) /* if they pass in null, go find it ourselves */
  1901. ns = networkstatus_get_latest_consensus();
  1902. if (!ns || !ns->net_params)
  1903. return default_val;
  1904. return get_net_param_from_list(ns->net_params, param_name, default_val);
  1905. }
  1906. /** Return the value of a integer bw weight parameter from the networkstatus
  1907. * <b>ns</b> whose name is <b>weight_name</b>. If <b>ns</b> is NULL, try
  1908. * loading the latest consensus ourselves. Return <b>default_val</b> if no
  1909. * latest consensus, or if it has no parameter called <b>param_name</b>. */
  1910. int32_t
  1911. networkstatus_get_bw_weight(networkstatus_t *ns, const char *weight_name,
  1912. int32_t default_val)
  1913. {
  1914. if (!ns) /* if they pass in null, go find it ourselves */
  1915. ns = networkstatus_get_latest_consensus();
  1916. if (!ns || !ns->weight_params)
  1917. return default_val;
  1918. return get_net_param_from_list(ns->weight_params, weight_name, default_val);
  1919. }
  1920. /** Return the name of the consensus flavor <b>flav</b> as used to identify
  1921. * the flavor in directory documents. */
  1922. const char *
  1923. networkstatus_get_flavor_name(consensus_flavor_t flav)
  1924. {
  1925. switch (flav) {
  1926. case FLAV_NS:
  1927. return "ns";
  1928. case FLAV_MICRODESC:
  1929. return "microdesc";
  1930. default:
  1931. tor_fragile_assert();
  1932. return "??";
  1933. }
  1934. }
  1935. /** Return the consensus_flavor_t value for the flavor called <b>flavname</b>,
  1936. * or -1 if the flavor is not recognized. */
  1937. int
  1938. networkstatus_parse_flavor_name(const char *flavname)
  1939. {
  1940. if (!strcmp(flavname, "ns"))
  1941. return FLAV_NS;
  1942. else if (!strcmp(flavname, "microdesc"))
  1943. return FLAV_MICRODESC;
  1944. else
  1945. return -1;
  1946. }
  1947. /** If <b>question</b> is a string beginning with "ns/" in a format the
  1948. * control interface expects for a GETINFO question, set *<b>answer</b> to a
  1949. * newly-allocated string containing networkstatus lines for the appropriate
  1950. * ORs. Return 0 on success, -1 on unrecognized question format. */
  1951. int
  1952. getinfo_helper_networkstatus(control_connection_t *conn,
  1953. const char *question, char **answer,
  1954. const char **errmsg)
  1955. {
  1956. routerstatus_t *status;
  1957. (void) conn;
  1958. if (!current_consensus) {
  1959. *answer = tor_strdup("");
  1960. return 0;
  1961. }
  1962. if (!strcmp(question, "ns/all")) {
  1963. smartlist_t *statuses = smartlist_create();
  1964. SMARTLIST_FOREACH(current_consensus->routerstatus_list,
  1965. routerstatus_t *, rs,
  1966. {
  1967. smartlist_add(statuses, networkstatus_getinfo_helper_single(rs));
  1968. });
  1969. *answer = smartlist_join_strings(statuses, "", 0, NULL);
  1970. SMARTLIST_FOREACH(statuses, char *, cp, tor_free(cp));
  1971. smartlist_free(statuses);
  1972. return 0;
  1973. } else if (!strcmpstart(question, "ns/id/")) {
  1974. char d[DIGEST_LEN];
  1975. if (base16_decode(d, DIGEST_LEN, question+6, strlen(question+6))) {
  1976. *errmsg = "Data not decodeable as hex";
  1977. return -1;
  1978. }
  1979. status = router_get_consensus_status_by_id(d);
  1980. } else if (!strcmpstart(question, "ns/name/")) {
  1981. status = router_get_consensus_status_by_nickname(question+8, 0);
  1982. } else if (!strcmpstart(question, "ns/purpose/")) {
  1983. *answer = networkstatus_getinfo_by_purpose(question+11, time(NULL));
  1984. return *answer ? 0 : -1;
  1985. } else {
  1986. return 0;
  1987. }
  1988. if (status)
  1989. *answer = networkstatus_getinfo_helper_single(status);
  1990. return 0;
  1991. }
  1992. /** Free all storage held locally in this module. */
  1993. void
  1994. networkstatus_free_all(void)
  1995. {
  1996. int i;
  1997. if (networkstatus_v2_list) {
  1998. SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
  1999. networkstatus_v2_free(ns));
  2000. smartlist_free(networkstatus_v2_list);
  2001. networkstatus_v2_list = NULL;
  2002. }
  2003. digestmap_free(v2_download_status_map, _tor_free);
  2004. v2_download_status_map = NULL;
  2005. networkstatus_vote_free(current_consensus);
  2006. current_consensus = NULL;
  2007. for (i=0; i < N_CONSENSUS_FLAVORS; ++i) {
  2008. consensus_waiting_for_certs_t *waiting = &consensus_waiting_for_certs[i];
  2009. if (waiting->consensus) {
  2010. networkstatus_vote_free(waiting->consensus);
  2011. waiting->consensus = NULL;
  2012. }
  2013. tor_free(waiting->body);
  2014. }
  2015. strmap_free(named_server_map, _tor_free);
  2016. strmap_free(unnamed_server_map, NULL);
  2017. }