networkstatus.c 68 KB

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