networkstatus.c 56 KB

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