networkstatus.c 76 KB

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