networkstatus.c 65 KB

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