networkstatus.c 68 KB

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