networkstatus.c 68 KB

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