networkstatus.c 69 KB

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