networkstatus.c 68 KB

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