networkstatus.c 56 KB

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