networkstatus.c 75 KB

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