networkstatus.c 57 KB

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