networkstatus.c 70 KB

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