networkstatus.c 66 KB

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