networkstatus.c 76 KB

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