networkstatus.c 66 KB

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