networkstatus.c 69 KB

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