networkstatus.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917
  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-2013, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file networkstatus.c
  8. * \brief Functions and structures for handling network status documents as a
  9. * client or cache.
  10. */
  11. #define NETWORKSTATUS_PRIVATE
  12. #include "or.h"
  13. #include "channel.h"
  14. #include "circuitmux.h"
  15. #include "circuitmux_ewma.h"
  16. #include "circuitstats.h"
  17. #include "config.h"
  18. #include "connection.h"
  19. #include "connection_or.h"
  20. #include "control.h"
  21. #include "directory.h"
  22. #include "dirserv.h"
  23. #include "dirvote.h"
  24. #include "entrynodes.h"
  25. #include "main.h"
  26. #include "microdesc.h"
  27. #include "networkstatus.h"
  28. #include "nodelist.h"
  29. #include "relay.h"
  30. #include "router.h"
  31. #include "routerlist.h"
  32. #include "routerparse.h"
  33. #include "transports.h"
  34. /** Map from lowercase nickname to identity digest of named server, if any. */
  35. static strmap_t *named_server_map = NULL;
  36. /** Map from lowercase nickname to (void*)1 for all names that are listed
  37. * as unnamed for some server in the consensus. */
  38. static strmap_t *unnamed_server_map = NULL;
  39. /** Most recently received and validated v3 consensus network status,
  40. * of whichever type we are using for our own circuits. This will be the same
  41. * as one of current_ns_consensus or current_md_consensus.
  42. */
  43. #define current_consensus \
  44. (we_use_microdescriptors_for_circuits(get_options()) ? \
  45. current_md_consensus : current_ns_consensus)
  46. /** Most recently received and validated v3 "ns"-flavored consensus network
  47. * status. */
  48. static networkstatus_t *current_ns_consensus = NULL;
  49. /** Most recently received and validated v3 "microdec"-flavored consensus
  50. * network status. */
  51. static networkstatus_t *current_md_consensus = NULL;
  52. /** A v3 consensus networkstatus that we've received, but which we don't
  53. * have enough certificates to be happy about. */
  54. typedef struct consensus_waiting_for_certs_t {
  55. /** The consensus itself. */
  56. networkstatus_t *consensus;
  57. /** The encoded version of the consensus, nul-terminated. */
  58. char *body;
  59. /** When did we set the current value of consensus_waiting_for_certs? If
  60. * this is too recent, we shouldn't try to fetch a new consensus for a
  61. * little while, to give ourselves time to get certificates for this one. */
  62. time_t set_at;
  63. /** Set to 1 if we've been holding on to it for so long we should maybe
  64. * treat it as being bad. */
  65. int dl_failed;
  66. } consensus_waiting_for_certs_t;
  67. /** An array, for each flavor of consensus we might want, of consensuses that
  68. * we have downloaded, but which we cannot verify due to having insufficient
  69. * authority certificates. */
  70. static consensus_waiting_for_certs_t
  71. consensus_waiting_for_certs[N_CONSENSUS_FLAVORS];
  72. /** A time before which we shouldn't try to replace the current consensus:
  73. * this will be at some point after the next consensus becomes valid, but
  74. * before the current consensus becomes invalid. */
  75. static time_t time_to_download_next_consensus[N_CONSENSUS_FLAVORS];
  76. /** Download status for the current consensus networkstatus. */
  77. static download_status_t consensus_dl_status[N_CONSENSUS_FLAVORS];
  78. /** True iff we have logged a warning about this OR's version being older than
  79. * listed by the authorities. */
  80. static int have_warned_about_old_version = 0;
  81. /** True iff we have logged a warning about this OR's version being newer than
  82. * listed by the authorities. */
  83. static int have_warned_about_new_version = 0;
  84. static void routerstatus_list_update_named_server_map(void);
  85. /** Forget that we've warned about anything networkstatus-related, so we will
  86. * give fresh warnings if the same behavior happens again. */
  87. void
  88. networkstatus_reset_warnings(void)
  89. {
  90. if (current_consensus) {
  91. SMARTLIST_FOREACH(nodelist_get_list(), node_t *, node,
  92. node->name_lookup_warned = 0);
  93. }
  94. have_warned_about_old_version = 0;
  95. have_warned_about_new_version = 0;
  96. }
  97. /** Reset the descriptor download failure count on all networkstatus docs, so
  98. * that we can retry any long-failed documents immediately.
  99. */
  100. void
  101. networkstatus_reset_download_failures(void)
  102. {
  103. int i;
  104. for (i=0; i < N_CONSENSUS_FLAVORS; ++i)
  105. download_status_reset(&consensus_dl_status[i]);
  106. }
  107. /** Read every cached v3 consensus networkstatus from the disk. */
  108. int
  109. router_reload_consensus_networkstatus(void)
  110. {
  111. char *filename;
  112. char *s;
  113. const unsigned int flags = NSSET_FROM_CACHE | NSSET_DONT_DOWNLOAD_CERTS;
  114. int flav;
  115. /* FFFF Suppress warnings if cached consensus is bad? */
  116. for (flav = 0; flav < N_CONSENSUS_FLAVORS; ++flav) {
  117. char buf[128];
  118. const char *flavor = networkstatus_get_flavor_name(flav);
  119. if (flav == FLAV_NS) {
  120. filename = get_datadir_fname("cached-consensus");
  121. } else {
  122. tor_snprintf(buf, sizeof(buf), "cached-%s-consensus", flavor);
  123. filename = get_datadir_fname(buf);
  124. }
  125. s = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
  126. if (s) {
  127. if (networkstatus_set_current_consensus(s, flavor, flags) < -1) {
  128. log_warn(LD_FS, "Couldn't load consensus %s networkstatus from \"%s\"",
  129. flavor, filename);
  130. }
  131. tor_free(s);
  132. }
  133. tor_free(filename);
  134. if (flav == FLAV_NS) {
  135. filename = get_datadir_fname("unverified-consensus");
  136. } else {
  137. tor_snprintf(buf, sizeof(buf), "unverified-%s-consensus", flavor);
  138. filename = get_datadir_fname(buf);
  139. }
  140. s = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
  141. if (s) {
  142. if (networkstatus_set_current_consensus(s, flavor,
  143. flags|NSSET_WAS_WAITING_FOR_CERTS)) {
  144. log_info(LD_FS, "Couldn't load consensus %s networkstatus from \"%s\"",
  145. flavor, filename);
  146. }
  147. tor_free(s);
  148. }
  149. tor_free(filename);
  150. }
  151. if (!current_consensus) {
  152. if (!named_server_map)
  153. named_server_map = strmap_new();
  154. if (!unnamed_server_map)
  155. unnamed_server_map = strmap_new();
  156. }
  157. update_certificate_downloads(time(NULL));
  158. routers_update_all_from_networkstatus(time(NULL), 3);
  159. update_microdescs_from_networkstatus(time(NULL));
  160. return 0;
  161. }
  162. /** Free all storage held by the vote_routerstatus object <b>rs</b>. */
  163. STATIC void
  164. vote_routerstatus_free(vote_routerstatus_t *rs)
  165. {
  166. vote_microdesc_hash_t *h, *next;
  167. if (!rs)
  168. return;
  169. tor_free(rs->version);
  170. tor_free(rs->status.exitsummary);
  171. for (h = rs->microdesc; h; h = next) {
  172. tor_free(h->microdesc_hash_line);
  173. next = h->next;
  174. tor_free(h);
  175. }
  176. tor_free(rs);
  177. }
  178. /** Free all storage held by the routerstatus object <b>rs</b>. */
  179. void
  180. routerstatus_free(routerstatus_t *rs)
  181. {
  182. if (!rs)
  183. return;
  184. tor_free(rs->exitsummary);
  185. tor_free(rs);
  186. }
  187. /** Free all storage held in <b>sig</b> */
  188. void
  189. document_signature_free(document_signature_t *sig)
  190. {
  191. tor_free(sig->signature);
  192. tor_free(sig);
  193. }
  194. /** Return a newly allocated copy of <b>sig</b> */
  195. document_signature_t *
  196. document_signature_dup(const document_signature_t *sig)
  197. {
  198. document_signature_t *r = tor_memdup(sig, sizeof(document_signature_t));
  199. if (r->signature)
  200. r->signature = tor_memdup(sig->signature, sig->signature_len);
  201. return r;
  202. }
  203. /** Free all storage held in <b>ns</b>. */
  204. void
  205. networkstatus_vote_free(networkstatus_t *ns)
  206. {
  207. if (!ns)
  208. return;
  209. tor_free(ns->client_versions);
  210. tor_free(ns->server_versions);
  211. if (ns->known_flags) {
  212. SMARTLIST_FOREACH(ns->known_flags, char *, c, tor_free(c));
  213. smartlist_free(ns->known_flags);
  214. }
  215. if (ns->weight_params) {
  216. SMARTLIST_FOREACH(ns->weight_params, char *, c, tor_free(c));
  217. smartlist_free(ns->weight_params);
  218. }
  219. if (ns->net_params) {
  220. SMARTLIST_FOREACH(ns->net_params, char *, c, tor_free(c));
  221. smartlist_free(ns->net_params);
  222. }
  223. if (ns->supported_methods) {
  224. SMARTLIST_FOREACH(ns->supported_methods, char *, c, tor_free(c));
  225. smartlist_free(ns->supported_methods);
  226. }
  227. if (ns->voters) {
  228. SMARTLIST_FOREACH_BEGIN(ns->voters, networkstatus_voter_info_t *, voter) {
  229. tor_free(voter->nickname);
  230. tor_free(voter->address);
  231. tor_free(voter->contact);
  232. if (voter->sigs) {
  233. SMARTLIST_FOREACH(voter->sigs, document_signature_t *, sig,
  234. document_signature_free(sig));
  235. smartlist_free(voter->sigs);
  236. }
  237. tor_free(voter);
  238. } SMARTLIST_FOREACH_END(voter);
  239. smartlist_free(ns->voters);
  240. }
  241. authority_cert_free(ns->cert);
  242. if (ns->routerstatus_list) {
  243. if (ns->type == NS_TYPE_VOTE || ns->type == NS_TYPE_OPINION) {
  244. SMARTLIST_FOREACH(ns->routerstatus_list, vote_routerstatus_t *, rs,
  245. vote_routerstatus_free(rs));
  246. } else {
  247. SMARTLIST_FOREACH(ns->routerstatus_list, routerstatus_t *, rs,
  248. routerstatus_free(rs));
  249. }
  250. smartlist_free(ns->routerstatus_list);
  251. }
  252. digestmap_free(ns->desc_digest_map, NULL);
  253. memwipe(ns, 11, sizeof(*ns));
  254. tor_free(ns);
  255. }
  256. /** Return the voter info from <b>vote</b> for the voter whose identity digest
  257. * is <b>identity</b>, or NULL if no such voter is associated with
  258. * <b>vote</b>. */
  259. networkstatus_voter_info_t *
  260. networkstatus_get_voter_by_id(networkstatus_t *vote,
  261. const char *identity)
  262. {
  263. if (!vote || !vote->voters)
  264. return NULL;
  265. SMARTLIST_FOREACH(vote->voters, networkstatus_voter_info_t *, voter,
  266. if (fast_memeq(voter->identity_digest, identity, DIGEST_LEN))
  267. return voter);
  268. return NULL;
  269. }
  270. /** Check whether the signature <b>sig</b> is correctly signed with the
  271. * signing key in <b>cert</b>. Return -1 if <b>cert</b> doesn't match the
  272. * signing key; otherwise set the good_signature or bad_signature flag on
  273. * <b>voter</b>, and return 0. */
  274. int
  275. networkstatus_check_document_signature(const networkstatus_t *consensus,
  276. document_signature_t *sig,
  277. const authority_cert_t *cert)
  278. {
  279. char key_digest[DIGEST_LEN];
  280. const int dlen = sig->alg == DIGEST_SHA1 ? DIGEST_LEN : DIGEST256_LEN;
  281. char *signed_digest;
  282. size_t signed_digest_len;
  283. if (crypto_pk_get_digest(cert->signing_key, key_digest)<0)
  284. return -1;
  285. if (tor_memneq(sig->signing_key_digest, key_digest, DIGEST_LEN) ||
  286. tor_memneq(sig->identity_digest, cert->cache_info.identity_digest,
  287. DIGEST_LEN))
  288. return -1;
  289. if (authority_cert_is_blacklisted(cert)) {
  290. /* We implement blacklisting for authority signing keys by treating
  291. * all their signatures as always bad. That way we don't get into
  292. * crazy loops of dropping and re-fetching signatures. */
  293. log_warn(LD_DIR, "Ignoring a consensus signature made with deprecated"
  294. " signing key %s",
  295. hex_str(cert->signing_key_digest, DIGEST_LEN));
  296. sig->bad_signature = 1;
  297. return 0;
  298. }
  299. signed_digest_len = crypto_pk_keysize(cert->signing_key);
  300. signed_digest = tor_malloc(signed_digest_len);
  301. if (crypto_pk_public_checksig(cert->signing_key,
  302. signed_digest,
  303. signed_digest_len,
  304. sig->signature,
  305. sig->signature_len) < dlen ||
  306. tor_memneq(signed_digest, consensus->digests.d[sig->alg], dlen)) {
  307. log_warn(LD_DIR, "Got a bad signature on a networkstatus vote");
  308. sig->bad_signature = 1;
  309. } else {
  310. sig->good_signature = 1;
  311. }
  312. tor_free(signed_digest);
  313. return 0;
  314. }
  315. /** Given a v3 networkstatus consensus in <b>consensus</b>, check every
  316. * as-yet-unchecked signature on <b>consensus</b>. Return 1 if there is a
  317. * signature from every recognized authority on it, 0 if there are
  318. * enough good signatures from recognized authorities on it, -1 if we might
  319. * get enough good signatures by fetching missing certificates, and -2
  320. * otherwise. Log messages at INFO or WARN: if <b>warn</b> is over 1, warn
  321. * about every problem; if warn is at least 1, warn only if we can't get
  322. * enough signatures; if warn is negative, log nothing at all. */
  323. int
  324. networkstatus_check_consensus_signature(networkstatus_t *consensus,
  325. int warn)
  326. {
  327. int n_good = 0;
  328. int n_missing_key = 0, n_dl_failed_key = 0;
  329. int n_bad = 0;
  330. int n_unknown = 0;
  331. int n_no_signature = 0;
  332. int n_v3_authorities = get_n_authorities(V3_DIRINFO);
  333. int n_required = n_v3_authorities/2 + 1;
  334. smartlist_t *list_good = smartlist_new();
  335. smartlist_t *list_no_signature = smartlist_new();
  336. smartlist_t *need_certs_from = smartlist_new();
  337. smartlist_t *unrecognized = smartlist_new();
  338. smartlist_t *missing_authorities = smartlist_new();
  339. int severity;
  340. time_t now = time(NULL);
  341. tor_assert(consensus->type == NS_TYPE_CONSENSUS);
  342. SMARTLIST_FOREACH_BEGIN(consensus->voters, networkstatus_voter_info_t *,
  343. voter) {
  344. int good_here = 0;
  345. int bad_here = 0;
  346. int unknown_here = 0;
  347. int missing_key_here = 0, dl_failed_key_here = 0;
  348. SMARTLIST_FOREACH_BEGIN(voter->sigs, document_signature_t *, sig) {
  349. if (!sig->good_signature && !sig->bad_signature &&
  350. sig->signature) {
  351. /* we can try to check the signature. */
  352. int is_v3_auth = trusteddirserver_get_by_v3_auth_digest(
  353. sig->identity_digest) != NULL;
  354. authority_cert_t *cert =
  355. authority_cert_get_by_digests(sig->identity_digest,
  356. sig->signing_key_digest);
  357. tor_assert(tor_memeq(sig->identity_digest, voter->identity_digest,
  358. DIGEST_LEN));
  359. if (!is_v3_auth) {
  360. smartlist_add(unrecognized, voter);
  361. ++unknown_here;
  362. continue;
  363. } else if (!cert || cert->expires < now) {
  364. smartlist_add(need_certs_from, voter);
  365. ++missing_key_here;
  366. if (authority_cert_dl_looks_uncertain(sig->identity_digest))
  367. ++dl_failed_key_here;
  368. continue;
  369. }
  370. if (networkstatus_check_document_signature(consensus, sig, cert) < 0) {
  371. smartlist_add(need_certs_from, voter);
  372. ++missing_key_here;
  373. if (authority_cert_dl_looks_uncertain(sig->identity_digest))
  374. ++dl_failed_key_here;
  375. continue;
  376. }
  377. }
  378. if (sig->good_signature)
  379. ++good_here;
  380. else if (sig->bad_signature)
  381. ++bad_here;
  382. } SMARTLIST_FOREACH_END(sig);
  383. if (good_here) {
  384. ++n_good;
  385. smartlist_add(list_good, voter->nickname);
  386. } else if (bad_here) {
  387. ++n_bad;
  388. } else if (missing_key_here) {
  389. ++n_missing_key;
  390. if (dl_failed_key_here)
  391. ++n_dl_failed_key;
  392. } else if (unknown_here) {
  393. ++n_unknown;
  394. } else {
  395. ++n_no_signature;
  396. smartlist_add(list_no_signature, voter->nickname);
  397. }
  398. } SMARTLIST_FOREACH_END(voter);
  399. /* Now see whether we're missing any voters entirely. */
  400. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  401. dir_server_t *, ds,
  402. {
  403. if ((ds->type & V3_DIRINFO) &&
  404. !networkstatus_get_voter_by_id(consensus, ds->v3_identity_digest))
  405. smartlist_add(missing_authorities, ds);
  406. });
  407. if (warn > 1 || (warn >= 0 &&
  408. (n_good + n_missing_key - n_dl_failed_key < n_required))) {
  409. severity = LOG_WARN;
  410. } else {
  411. severity = LOG_INFO;
  412. }
  413. if (warn >= 0) {
  414. SMARTLIST_FOREACH(unrecognized, networkstatus_voter_info_t *, voter,
  415. {
  416. tor_log(severity, LD_DIR, "Consensus includes unrecognized authority "
  417. "'%s' at %s:%d (contact %s; identity %s)",
  418. voter->nickname, voter->address, (int)voter->dir_port,
  419. voter->contact?voter->contact:"n/a",
  420. hex_str(voter->identity_digest, DIGEST_LEN));
  421. });
  422. SMARTLIST_FOREACH(need_certs_from, networkstatus_voter_info_t *, voter,
  423. {
  424. tor_log(severity, LD_DIR, "Looks like we need to download a new "
  425. "certificate from authority '%s' at %s:%d (contact %s; "
  426. "identity %s)",
  427. voter->nickname, voter->address, (int)voter->dir_port,
  428. voter->contact?voter->contact:"n/a",
  429. hex_str(voter->identity_digest, DIGEST_LEN));
  430. });
  431. SMARTLIST_FOREACH(missing_authorities, dir_server_t *, ds,
  432. {
  433. tor_log(severity, LD_DIR, "Consensus does not include configured "
  434. "authority '%s' at %s:%d (identity %s)",
  435. ds->nickname, ds->address, (int)ds->dir_port,
  436. hex_str(ds->v3_identity_digest, DIGEST_LEN));
  437. });
  438. {
  439. char *joined;
  440. smartlist_t *sl = smartlist_new();
  441. char *tmp = smartlist_join_strings(list_good, " ", 0, NULL);
  442. smartlist_add_asprintf(sl,
  443. "A consensus needs %d good signatures from recognized "
  444. "authorities for us to accept it. This one has %d (%s).",
  445. n_required, n_good, tmp);
  446. tor_free(tmp);
  447. if (n_no_signature) {
  448. tmp = smartlist_join_strings(list_no_signature, " ", 0, NULL);
  449. smartlist_add_asprintf(sl,
  450. "%d (%s) of the authorities we know didn't sign it.",
  451. n_no_signature, tmp);
  452. tor_free(tmp);
  453. }
  454. if (n_unknown) {
  455. smartlist_add_asprintf(sl,
  456. "It has %d signatures from authorities we don't "
  457. "recognize.", n_unknown);
  458. }
  459. if (n_bad) {
  460. smartlist_add_asprintf(sl, "%d of the signatures on it didn't verify "
  461. "correctly.", n_bad);
  462. }
  463. if (n_missing_key) {
  464. smartlist_add_asprintf(sl,
  465. "We were unable to check %d of the signatures, "
  466. "because we were missing the keys.", n_missing_key);
  467. }
  468. joined = smartlist_join_strings(sl, " ", 0, NULL);
  469. tor_log(severity, LD_DIR, "%s", joined);
  470. tor_free(joined);
  471. SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
  472. smartlist_free(sl);
  473. }
  474. }
  475. smartlist_free(list_good);
  476. smartlist_free(list_no_signature);
  477. smartlist_free(unrecognized);
  478. smartlist_free(need_certs_from);
  479. smartlist_free(missing_authorities);
  480. if (n_good == n_v3_authorities)
  481. return 1;
  482. else if (n_good >= n_required)
  483. return 0;
  484. else if (n_good + n_missing_key >= n_required)
  485. return -1;
  486. else
  487. return -2;
  488. }
  489. /** How far in the future do we allow a network-status to get before removing
  490. * it? (seconds) */
  491. #define NETWORKSTATUS_ALLOW_SKEW (24*60*60)
  492. /** Helper for bsearching a list of routerstatus_t pointers: compare a
  493. * digest in the key to the identity digest of a routerstatus_t. */
  494. int
  495. compare_digest_to_routerstatus_entry(const void *_key, const void **_member)
  496. {
  497. const char *key = _key;
  498. const routerstatus_t *rs = *_member;
  499. return tor_memcmp(key, rs->identity_digest, DIGEST_LEN);
  500. }
  501. /** Helper for bsearching a list of routerstatus_t pointers: compare a
  502. * digest in the key to the identity digest of a routerstatus_t. */
  503. int
  504. compare_digest_to_vote_routerstatus_entry(const void *_key,
  505. const void **_member)
  506. {
  507. const char *key = _key;
  508. const vote_routerstatus_t *vrs = *_member;
  509. return tor_memcmp(key, vrs->status.identity_digest, DIGEST_LEN);
  510. }
  511. /** As networkstatus_find_entry, but do not return a const pointer */
  512. routerstatus_t *
  513. networkstatus_vote_find_mutable_entry(networkstatus_t *ns, const char *digest)
  514. {
  515. return smartlist_bsearch(ns->routerstatus_list, digest,
  516. compare_digest_to_routerstatus_entry);
  517. }
  518. /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
  519. * NULL if none was found. */
  520. const routerstatus_t *
  521. networkstatus_vote_find_entry(networkstatus_t *ns, const char *digest)
  522. {
  523. return networkstatus_vote_find_mutable_entry(ns, digest);
  524. }
  525. /*XXXX MOVE make this static once functions are moved into this file. */
  526. /** Search the routerstatuses in <b>ns</b> for one whose identity digest is
  527. * <b>digest</b>. Return value and set *<b>found_out</b> as for
  528. * smartlist_bsearch_idx(). */
  529. int
  530. networkstatus_vote_find_entry_idx(networkstatus_t *ns,
  531. const char *digest, int *found_out)
  532. {
  533. return smartlist_bsearch_idx(ns->routerstatus_list, digest,
  534. compare_digest_to_routerstatus_entry,
  535. found_out);
  536. }
  537. /** As router_get_consensus_status_by_descriptor_digest, but does not return
  538. * a const pointer. */
  539. routerstatus_t *
  540. router_get_mutable_consensus_status_by_descriptor_digest(
  541. networkstatus_t *consensus,
  542. const char *digest)
  543. {
  544. if (!consensus)
  545. consensus = current_consensus;
  546. if (!consensus)
  547. return NULL;
  548. if (!consensus->desc_digest_map) {
  549. digestmap_t *m = consensus->desc_digest_map = digestmap_new();
  550. SMARTLIST_FOREACH(consensus->routerstatus_list,
  551. routerstatus_t *, rs,
  552. {
  553. digestmap_set(m, rs->descriptor_digest, rs);
  554. });
  555. }
  556. return digestmap_get(consensus->desc_digest_map, digest);
  557. }
  558. /** Return the consensus view of the status of the router whose current
  559. * <i>descriptor</i> digest in <b>consensus</b> is <b>digest</b>, or NULL if
  560. * no such router is known. */
  561. const routerstatus_t *
  562. router_get_consensus_status_by_descriptor_digest(networkstatus_t *consensus,
  563. const char *digest)
  564. {
  565. return router_get_mutable_consensus_status_by_descriptor_digest(
  566. consensus, digest);
  567. }
  568. /** Given the digest of a router descriptor, return its current download
  569. * status, or NULL if the digest is unrecognized. */
  570. download_status_t *
  571. router_get_dl_status_by_descriptor_digest(const char *d)
  572. {
  573. routerstatus_t *rs;
  574. if (!current_ns_consensus)
  575. return NULL;
  576. if ((rs = router_get_mutable_consensus_status_by_descriptor_digest(
  577. current_ns_consensus, d)))
  578. return &rs->dl_status;
  579. return NULL;
  580. }
  581. /** As router_get_consensus_status_by_id, but do not return a const pointer */
  582. routerstatus_t *
  583. router_get_mutable_consensus_status_by_id(const char *digest)
  584. {
  585. if (!current_consensus)
  586. return NULL;
  587. return smartlist_bsearch(current_consensus->routerstatus_list, digest,
  588. compare_digest_to_routerstatus_entry);
  589. }
  590. /** Return the consensus view of the status of the router whose identity
  591. * digest is <b>digest</b>, or NULL if we don't know about any such router. */
  592. const routerstatus_t *
  593. router_get_consensus_status_by_id(const char *digest)
  594. {
  595. return router_get_mutable_consensus_status_by_id(digest);
  596. }
  597. /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
  598. * the corresponding routerstatus_t, or NULL if none exists. Warn the
  599. * user if <b>warn_if_unnamed</b> is set, and they have specified a router by
  600. * nickname, but the Named flag isn't set for that router. */
  601. const routerstatus_t *
  602. router_get_consensus_status_by_nickname(const char *nickname,
  603. int warn_if_unnamed)
  604. {
  605. const node_t *node = node_get_by_nickname(nickname, warn_if_unnamed);
  606. if (node)
  607. return node->rs;
  608. else
  609. return NULL;
  610. }
  611. /** Return the identity digest that's mapped to officially by
  612. * <b>nickname</b>. */
  613. const char *
  614. networkstatus_get_router_digest_by_nickname(const char *nickname)
  615. {
  616. if (!named_server_map)
  617. return NULL;
  618. return strmap_get_lc(named_server_map, nickname);
  619. }
  620. /** Return true iff <b>nickname</b> is disallowed from being the nickname
  621. * of any server. */
  622. int
  623. networkstatus_nickname_is_unnamed(const char *nickname)
  624. {
  625. if (!unnamed_server_map)
  626. return 0;
  627. return strmap_get_lc(unnamed_server_map, nickname) != NULL;
  628. }
  629. /** How frequently do directory authorities re-download fresh networkstatus
  630. * documents? */
  631. #define AUTHORITY_NS_CACHE_INTERVAL (10*60)
  632. /** How frequently do non-authority directory caches re-download fresh
  633. * networkstatus documents? */
  634. #define NONAUTHORITY_NS_CACHE_INTERVAL (60*60)
  635. /** Return true iff, given the options listed in <b>options</b>, <b>flavor</b>
  636. * is the flavor of a consensus networkstatus that we would like to fetch. */
  637. static int
  638. we_want_to_fetch_flavor(const or_options_t *options, int flavor)
  639. {
  640. if (flavor < 0 || flavor > N_CONSENSUS_FLAVORS) {
  641. /* This flavor is crazy; we don't want it */
  642. /*XXXX handle unrecognized flavors later */
  643. return 0;
  644. }
  645. if (authdir_mode_v3(options) || directory_caches_dir_info(options)) {
  646. /* We want to serve all flavors to others, regardless if we would use
  647. * it ourselves. */
  648. return 1;
  649. }
  650. if (options->FetchUselessDescriptors) {
  651. /* In order to get all descriptors, we need to fetch all consensuses. */
  652. return 1;
  653. }
  654. /* Otherwise, we want the flavor only if we want to use it to build
  655. * circuits. */
  656. return flavor == usable_consensus_flavor();
  657. }
  658. /** How long will we hang onto a possibly live consensus for which we're
  659. * fetching certs before we check whether there is a better one? */
  660. #define DELAY_WHILE_FETCHING_CERTS (20*60)
  661. /** If we want to download a fresh consensus, launch a new download as
  662. * appropriate. */
  663. static void
  664. update_consensus_networkstatus_downloads(time_t now)
  665. {
  666. int i;
  667. const or_options_t *options = get_options();
  668. for (i=0; i < N_CONSENSUS_FLAVORS; ++i) {
  669. /* XXXX need some way to download unknown flavors if we are caching. */
  670. const char *resource;
  671. consensus_waiting_for_certs_t *waiting;
  672. networkstatus_t *c;
  673. if (! we_want_to_fetch_flavor(options, i))
  674. continue;
  675. c = networkstatus_get_latest_consensus_by_flavor(i);
  676. if (! (c && c->valid_after <= now && now <= c->valid_until)) {
  677. /* No live consensus? Get one now!*/
  678. time_to_download_next_consensus[i] = now;
  679. }
  680. if (time_to_download_next_consensus[i] > now)
  681. continue; /* Wait until the current consensus is older. */
  682. resource = networkstatus_get_flavor_name(i);
  683. if (!download_status_is_ready(&consensus_dl_status[i], now,
  684. options->TestingConsensusMaxDownloadTries))
  685. continue; /* We failed downloading a consensus too recently. */
  686. if (connection_dir_get_by_purpose_and_resource(
  687. DIR_PURPOSE_FETCH_CONSENSUS, resource))
  688. continue; /* There's an in-progress download.*/
  689. waiting = &consensus_waiting_for_certs[i];
  690. if (waiting->consensus) {
  691. /* XXXX make sure this doesn't delay sane downloads. */
  692. if (waiting->set_at + DELAY_WHILE_FETCHING_CERTS > now) {
  693. continue; /* We're still getting certs for this one. */
  694. } else {
  695. if (!waiting->dl_failed) {
  696. download_status_failed(&consensus_dl_status[i], 0);
  697. waiting->dl_failed=1;
  698. }
  699. }
  700. }
  701. log_info(LD_DIR, "Launching %s networkstatus consensus download.",
  702. networkstatus_get_flavor_name(i));
  703. directory_get_from_dirserver(DIR_PURPOSE_FETCH_CONSENSUS,
  704. ROUTER_PURPOSE_GENERAL, resource,
  705. PDS_RETRY_IF_NO_SERVERS);
  706. }
  707. }
  708. /** Called when an attempt to download a consensus fails: note that the
  709. * failure occurred, and possibly retry. */
  710. void
  711. networkstatus_consensus_download_failed(int status_code, const char *flavname)
  712. {
  713. int flav = networkstatus_parse_flavor_name(flavname);
  714. if (flav >= 0) {
  715. tor_assert(flav < N_CONSENSUS_FLAVORS);
  716. /* XXXX handle unrecognized flavors */
  717. download_status_failed(&consensus_dl_status[flav], status_code);
  718. /* Retry immediately, if appropriate. */
  719. update_consensus_networkstatus_downloads(time(NULL));
  720. }
  721. }
  722. /** How long do we (as a cache) wait after a consensus becomes non-fresh
  723. * before trying to fetch another? */
  724. #define CONSENSUS_MIN_SECONDS_BEFORE_CACHING 120
  725. /** Update the time at which we'll consider replacing the current
  726. * consensus of flavor <b>flav</b> */
  727. static void
  728. update_consensus_networkstatus_fetch_time_impl(time_t now, int flav)
  729. {
  730. const or_options_t *options = get_options();
  731. networkstatus_t *c = networkstatus_get_latest_consensus_by_flavor(flav);
  732. const char *flavor = networkstatus_get_flavor_name(flav);
  733. if (! we_want_to_fetch_flavor(get_options(), flav))
  734. return;
  735. if (c && c->valid_after <= now && now <= c->valid_until) {
  736. long dl_interval;
  737. long interval = c->fresh_until - c->valid_after;
  738. long min_sec_before_caching = CONSENSUS_MIN_SECONDS_BEFORE_CACHING;
  739. time_t start;
  740. if (min_sec_before_caching > interval/16) {
  741. /* Usually we allow 2-minutes slop factor in case clocks get
  742. desynchronized a little. If we're on a private network with
  743. a crazy-fast voting interval, though, 2 minutes may be too
  744. much. */
  745. min_sec_before_caching = interval/16;
  746. }
  747. if (directory_fetches_dir_info_early(options)) {
  748. /* We want to cache the next one at some point after this one
  749. * is no longer fresh... */
  750. start = (time_t)(c->fresh_until + min_sec_before_caching);
  751. /* Some clients may need the consensus sooner than others. */
  752. if (options->FetchDirInfoExtraEarly || authdir_mode_v3(options)) {
  753. dl_interval = 60;
  754. if (min_sec_before_caching + dl_interval > interval)
  755. dl_interval = interval/2;
  756. } else {
  757. /* But only in the first half-interval after that. */
  758. dl_interval = interval/2;
  759. }
  760. } else {
  761. /* We're an ordinary client or a bridge. Give all the caches enough
  762. * time to download the consensus. */
  763. start = (time_t)(c->fresh_until + (interval*3)/4);
  764. /* But download the next one well before this one is expired. */
  765. dl_interval = ((c->valid_until - start) * 7 )/ 8;
  766. /* If we're a bridge user, make use of the numbers we just computed
  767. * to choose the rest of the interval *after* them. */
  768. if (directory_fetches_dir_info_later(options)) {
  769. /* Give all the *clients* enough time to download the consensus. */
  770. start = (time_t)(start + dl_interval + min_sec_before_caching);
  771. /* But try to get it before ours actually expires. */
  772. dl_interval = (c->valid_until - start) - min_sec_before_caching;
  773. }
  774. }
  775. if (dl_interval < 1)
  776. dl_interval = 1;
  777. /* We must not try to replace c while it's still fresh: */
  778. tor_assert(c->fresh_until < start);
  779. /* We must download the next one before c is invalid: */
  780. tor_assert(start+dl_interval < c->valid_until);
  781. time_to_download_next_consensus[flav] =
  782. start + crypto_rand_int((int)dl_interval);
  783. {
  784. char tbuf1[ISO_TIME_LEN+1];
  785. char tbuf2[ISO_TIME_LEN+1];
  786. char tbuf3[ISO_TIME_LEN+1];
  787. format_local_iso_time(tbuf1, c->fresh_until);
  788. format_local_iso_time(tbuf2, c->valid_until);
  789. format_local_iso_time(tbuf3, time_to_download_next_consensus[flav]);
  790. log_info(LD_DIR, "Live %s consensus %s the most recent until %s and "
  791. "will expire at %s; fetching the next one at %s.",
  792. flavor, (c->fresh_until > now) ? "will be" : "was",
  793. tbuf1, tbuf2, tbuf3);
  794. }
  795. } else {
  796. time_to_download_next_consensus[flav] = now;
  797. log_info(LD_DIR, "No live %s consensus; we should fetch one immediately.",
  798. flavor);
  799. }
  800. }
  801. /** Update the time at which we'll consider replacing the current
  802. * consensus of flavor 'flavor' */
  803. void
  804. update_consensus_networkstatus_fetch_time(time_t now)
  805. {
  806. int i;
  807. for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
  808. if (we_want_to_fetch_flavor(get_options(), i))
  809. update_consensus_networkstatus_fetch_time_impl(now, i);
  810. }
  811. }
  812. /** Return 1 if there's a reason we shouldn't try any directory
  813. * fetches yet (e.g. we demand bridges and none are yet known).
  814. * Else return 0.
  815. * If we return 1 and <b>msg_out</b> is provided, set <b>msg_out</b>
  816. * to an explanation of why directory fetches are delayed. (If we
  817. * return 0, we set msg_out to NULL.)
  818. */
  819. int
  820. should_delay_dir_fetches(const or_options_t *options, const char **msg_out)
  821. {
  822. if (msg_out) {
  823. *msg_out = NULL;
  824. }
  825. if (options->DisableNetwork) {
  826. if (msg_out) {
  827. *msg_out = "DisableNetwork is set.";
  828. }
  829. log_info(LD_DIR, "Delaying dir fetches (DisableNetwork is set)");
  830. return 1;
  831. }
  832. if (options->UseBridges) {
  833. if (!any_bridge_descriptors_known()) {
  834. if (msg_out) {
  835. *msg_out = "No running bridges";
  836. }
  837. log_info(LD_DIR, "Delaying dir fetches (no running bridges known)");
  838. return 1;
  839. }
  840. if (pt_proxies_configuration_pending()) {
  841. if (msg_out) {
  842. *msg_out = "Pluggable transport proxies still configuring";
  843. }
  844. log_info(LD_DIR, "Delaying dir fetches (pt proxies still configuring)");
  845. return 1;
  846. }
  847. }
  848. return 0;
  849. }
  850. /** Launch requests for networkstatus documents and authority certificates as
  851. * appropriate. */
  852. void
  853. update_networkstatus_downloads(time_t now)
  854. {
  855. const or_options_t *options = get_options();
  856. if (should_delay_dir_fetches(options, NULL))
  857. return;
  858. update_consensus_networkstatus_downloads(now);
  859. update_certificate_downloads(now);
  860. }
  861. /** Launch requests as appropriate for missing directory authority
  862. * certificates. */
  863. void
  864. update_certificate_downloads(time_t now)
  865. {
  866. int i;
  867. for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
  868. if (consensus_waiting_for_certs[i].consensus)
  869. authority_certs_fetch_missing(consensus_waiting_for_certs[i].consensus,
  870. now);
  871. }
  872. if (current_ns_consensus)
  873. authority_certs_fetch_missing(current_ns_consensus, now);
  874. if (current_md_consensus)
  875. authority_certs_fetch_missing(current_md_consensus, now);
  876. }
  877. /** Return 1 if we have a consensus but we don't have enough certificates
  878. * to start using it yet. */
  879. int
  880. consensus_is_waiting_for_certs(void)
  881. {
  882. return consensus_waiting_for_certs[usable_consensus_flavor()].consensus
  883. ? 1 : 0;
  884. }
  885. /** Return the most recent consensus that we have downloaded, or NULL if we
  886. * don't have one. */
  887. networkstatus_t *
  888. networkstatus_get_latest_consensus(void)
  889. {
  890. return current_consensus;
  891. }
  892. /** Return the latest consensus we have whose flavor matches <b>f</b>, or NULL
  893. * if we don't have one. */
  894. networkstatus_t *
  895. networkstatus_get_latest_consensus_by_flavor(consensus_flavor_t f)
  896. {
  897. if (f == FLAV_NS)
  898. return current_ns_consensus;
  899. else if (f == FLAV_MICRODESC)
  900. return current_md_consensus;
  901. else {
  902. tor_assert(0);
  903. return NULL;
  904. }
  905. }
  906. /** Return the most recent consensus that we have downloaded, or NULL if it is
  907. * no longer live. */
  908. networkstatus_t *
  909. networkstatus_get_live_consensus(time_t now)
  910. {
  911. if (current_consensus &&
  912. current_consensus->valid_after <= now &&
  913. now <= current_consensus->valid_until)
  914. return current_consensus;
  915. else
  916. return NULL;
  917. }
  918. /* XXXX remove this in favor of get_live_consensus. But actually,
  919. * leave something like it for bridge users, who need to not totally
  920. * lose if they spend a while fetching a new consensus. */
  921. /** As networkstatus_get_live_consensus(), but is way more tolerant of expired
  922. * consensuses. */
  923. networkstatus_t *
  924. networkstatus_get_reasonably_live_consensus(time_t now, int flavor)
  925. {
  926. #define REASONABLY_LIVE_TIME (24*60*60)
  927. networkstatus_t *consensus =
  928. networkstatus_get_latest_consensus_by_flavor(flavor);
  929. if (consensus &&
  930. consensus->valid_after <= now &&
  931. now <= consensus->valid_until+REASONABLY_LIVE_TIME)
  932. return consensus;
  933. else
  934. return NULL;
  935. }
  936. /** Given two router status entries for the same router identity, return 1 if
  937. * if the contents have changed between them. Otherwise, return 0. */
  938. static int
  939. routerstatus_has_changed(const routerstatus_t *a, const routerstatus_t *b)
  940. {
  941. tor_assert(tor_memeq(a->identity_digest, b->identity_digest, DIGEST_LEN));
  942. return strcmp(a->nickname, b->nickname) ||
  943. fast_memneq(a->descriptor_digest, b->descriptor_digest, DIGEST_LEN) ||
  944. a->addr != b->addr ||
  945. a->or_port != b->or_port ||
  946. a->dir_port != b->dir_port ||
  947. a->is_authority != b->is_authority ||
  948. a->is_exit != b->is_exit ||
  949. a->is_stable != b->is_stable ||
  950. a->is_fast != b->is_fast ||
  951. a->is_flagged_running != b->is_flagged_running ||
  952. a->is_named != b->is_named ||
  953. a->is_unnamed != b->is_unnamed ||
  954. a->is_valid != b->is_valid ||
  955. a->is_possible_guard != b->is_possible_guard ||
  956. a->is_bad_exit != b->is_bad_exit ||
  957. a->is_hs_dir != b->is_hs_dir ||
  958. a->version_known != b->version_known;
  959. }
  960. /** Notify controllers of any router status entries that changed between
  961. * <b>old_c</b> and <b>new_c</b>. */
  962. static void
  963. notify_control_networkstatus_changed(const networkstatus_t *old_c,
  964. const networkstatus_t *new_c)
  965. {
  966. smartlist_t *changed;
  967. if (old_c == new_c)
  968. return;
  969. /* tell the controller exactly which relays are still listed, as well
  970. * as what they're listed as */
  971. control_event_newconsensus(new_c);
  972. if (!control_event_is_interesting(EVENT_NS))
  973. return;
  974. if (!old_c) {
  975. control_event_networkstatus_changed(new_c->routerstatus_list);
  976. return;
  977. }
  978. changed = smartlist_new();
  979. SMARTLIST_FOREACH_JOIN(
  980. old_c->routerstatus_list, const routerstatus_t *, rs_old,
  981. new_c->routerstatus_list, const routerstatus_t *, rs_new,
  982. tor_memcmp(rs_old->identity_digest,
  983. rs_new->identity_digest, DIGEST_LEN),
  984. smartlist_add(changed, (void*) rs_new)) {
  985. if (routerstatus_has_changed(rs_old, rs_new))
  986. smartlist_add(changed, (void*)rs_new);
  987. } SMARTLIST_FOREACH_JOIN_END(rs_old, rs_new);
  988. control_event_networkstatus_changed(changed);
  989. smartlist_free(changed);
  990. }
  991. /** Copy all the ancillary information (like router download status and so on)
  992. * from <b>old_c</b> to <b>new_c</b>. */
  993. static void
  994. networkstatus_copy_old_consensus_info(networkstatus_t *new_c,
  995. const networkstatus_t *old_c)
  996. {
  997. if (old_c == new_c)
  998. return;
  999. if (!old_c || !smartlist_len(old_c->routerstatus_list))
  1000. return;
  1001. SMARTLIST_FOREACH_JOIN(old_c->routerstatus_list, routerstatus_t *, rs_old,
  1002. new_c->routerstatus_list, routerstatus_t *, rs_new,
  1003. tor_memcmp(rs_old->identity_digest,
  1004. rs_new->identity_digest, DIGEST_LEN),
  1005. STMT_NIL) {
  1006. /* Okay, so we're looking at the same identity. */
  1007. rs_new->last_dir_503_at = rs_old->last_dir_503_at;
  1008. if (tor_memeq(rs_old->descriptor_digest, rs_new->descriptor_digest,
  1009. DIGEST_LEN)) {
  1010. /* And the same descriptor too! */
  1011. memcpy(&rs_new->dl_status, &rs_old->dl_status,sizeof(download_status_t));
  1012. }
  1013. } SMARTLIST_FOREACH_JOIN_END(rs_old, rs_new);
  1014. }
  1015. /** Try to replace the current cached v3 networkstatus with the one in
  1016. * <b>consensus</b>. If we don't have enough certificates to validate it,
  1017. * store it in consensus_waiting_for_certs and launch a certificate fetch.
  1018. *
  1019. * If flags & NSSET_FROM_CACHE, this networkstatus has come from the disk
  1020. * cache. If flags & NSSET_WAS_WAITING_FOR_CERTS, this networkstatus was
  1021. * already received, but we were waiting for certificates on it. If flags &
  1022. * NSSET_DONT_DOWNLOAD_CERTS, do not launch certificate downloads as needed.
  1023. * If flags & NSSET_ACCEPT_OBSOLETE, then we should be willing to take this
  1024. * consensus, even if it comes from many days in the past.
  1025. *
  1026. * Return 0 on success, <0 on failure. On failure, caller should increment
  1027. * the failure count as appropriate.
  1028. *
  1029. * We return -1 for mild failures that don't need to be reported to the
  1030. * user, and -2 for more serious problems.
  1031. */
  1032. int
  1033. networkstatus_set_current_consensus(const char *consensus,
  1034. const char *flavor,
  1035. unsigned flags)
  1036. {
  1037. networkstatus_t *c=NULL;
  1038. int r, result = -1;
  1039. time_t now = time(NULL);
  1040. const or_options_t *options = get_options();
  1041. char *unverified_fname = NULL, *consensus_fname = NULL;
  1042. int flav = networkstatus_parse_flavor_name(flavor);
  1043. const unsigned from_cache = flags & NSSET_FROM_CACHE;
  1044. const unsigned was_waiting_for_certs = flags & NSSET_WAS_WAITING_FOR_CERTS;
  1045. const unsigned dl_certs = !(flags & NSSET_DONT_DOWNLOAD_CERTS);
  1046. const unsigned accept_obsolete = flags & NSSET_ACCEPT_OBSOLETE;
  1047. const unsigned require_flavor = flags & NSSET_REQUIRE_FLAVOR;
  1048. const digests_t *current_digests = NULL;
  1049. consensus_waiting_for_certs_t *waiting = NULL;
  1050. time_t current_valid_after = 0;
  1051. int free_consensus = 1; /* Free 'c' at the end of the function */
  1052. int old_ewma_enabled;
  1053. if (flav < 0) {
  1054. /* XXXX we don't handle unrecognized flavors yet. */
  1055. log_warn(LD_BUG, "Unrecognized consensus flavor %s", flavor);
  1056. return -2;
  1057. }
  1058. /* Make sure it's parseable. */
  1059. c = networkstatus_parse_vote_from_string(consensus, NULL, NS_TYPE_CONSENSUS);
  1060. if (!c) {
  1061. log_warn(LD_DIR, "Unable to parse networkstatus consensus");
  1062. result = -2;
  1063. goto done;
  1064. }
  1065. if ((int)c->flavor != flav) {
  1066. /* This wasn't the flavor we thought we were getting. */
  1067. if (require_flavor) {
  1068. log_warn(LD_DIR, "Got consensus with unexpected flavor %s (wanted %s)",
  1069. networkstatus_get_flavor_name(c->flavor), flavor);
  1070. goto done;
  1071. }
  1072. flav = c->flavor;
  1073. flavor = networkstatus_get_flavor_name(flav);
  1074. }
  1075. if (flav != usable_consensus_flavor() &&
  1076. !directory_caches_dir_info(options)) {
  1077. /* This consensus is totally boring to us: we won't use it, and we won't
  1078. * serve it. Drop it. */
  1079. goto done;
  1080. }
  1081. if (from_cache && !accept_obsolete &&
  1082. c->valid_until < now-OLD_ROUTER_DESC_MAX_AGE) {
  1083. log_info(LD_DIR, "Loaded an expired consensus. Discarding.");
  1084. goto done;
  1085. }
  1086. if (!strcmp(flavor, "ns")) {
  1087. consensus_fname = get_datadir_fname("cached-consensus");
  1088. unverified_fname = get_datadir_fname("unverified-consensus");
  1089. if (current_ns_consensus) {
  1090. current_digests = &current_ns_consensus->digests;
  1091. current_valid_after = current_ns_consensus->valid_after;
  1092. }
  1093. } else if (!strcmp(flavor, "microdesc")) {
  1094. consensus_fname = get_datadir_fname("cached-microdesc-consensus");
  1095. unverified_fname = get_datadir_fname("unverified-microdesc-consensus");
  1096. if (current_md_consensus) {
  1097. current_digests = &current_md_consensus->digests;
  1098. current_valid_after = current_md_consensus->valid_after;
  1099. }
  1100. } else {
  1101. cached_dir_t *cur;
  1102. char buf[128];
  1103. tor_snprintf(buf, sizeof(buf), "cached-%s-consensus", flavor);
  1104. consensus_fname = get_datadir_fname(buf);
  1105. tor_snprintf(buf, sizeof(buf), "unverified-%s-consensus", flavor);
  1106. unverified_fname = get_datadir_fname(buf);
  1107. cur = dirserv_get_consensus(flavor);
  1108. if (cur) {
  1109. current_digests = &cur->digests;
  1110. current_valid_after = cur->published;
  1111. }
  1112. }
  1113. if (current_digests &&
  1114. tor_memeq(&c->digests, current_digests, sizeof(c->digests))) {
  1115. /* We already have this one. That's a failure. */
  1116. log_info(LD_DIR, "Got a %s consensus we already have", flavor);
  1117. goto done;
  1118. }
  1119. if (current_valid_after && c->valid_after <= current_valid_after) {
  1120. /* We have a newer one. There's no point in accepting this one,
  1121. * even if it's great. */
  1122. log_info(LD_DIR, "Got a %s consensus at least as old as the one we have",
  1123. flavor);
  1124. goto done;
  1125. }
  1126. /* Make sure it's signed enough. */
  1127. if ((r=networkstatus_check_consensus_signature(c, 1))<0) {
  1128. if (r == -1) {
  1129. /* Okay, so it _might_ be signed enough if we get more certificates. */
  1130. if (!was_waiting_for_certs) {
  1131. log_info(LD_DIR,
  1132. "Not enough certificates to check networkstatus consensus");
  1133. }
  1134. if (!current_valid_after ||
  1135. c->valid_after > current_valid_after) {
  1136. waiting = &consensus_waiting_for_certs[flav];
  1137. networkstatus_vote_free(waiting->consensus);
  1138. tor_free(waiting->body);
  1139. waiting->consensus = c;
  1140. free_consensus = 0;
  1141. waiting->body = tor_strdup(consensus);
  1142. waiting->set_at = now;
  1143. waiting->dl_failed = 0;
  1144. if (!from_cache) {
  1145. write_str_to_file(unverified_fname, consensus, 0);
  1146. }
  1147. if (dl_certs)
  1148. authority_certs_fetch_missing(c, now);
  1149. /* This case is not a success or a failure until we get the certs
  1150. * or fail to get the certs. */
  1151. result = 0;
  1152. } else {
  1153. /* Even if we had enough signatures, we'd never use this as the
  1154. * latest consensus. */
  1155. if (was_waiting_for_certs && from_cache)
  1156. if (unlink(unverified_fname) != 0) {
  1157. log_warn(LD_FS,
  1158. "Failed to unlink %s: %s",
  1159. unverified_fname, strerror(errno));
  1160. }
  1161. }
  1162. goto done;
  1163. } else {
  1164. /* This can never be signed enough: Kill it. */
  1165. if (!was_waiting_for_certs) {
  1166. log_warn(LD_DIR, "Not enough good signatures on networkstatus "
  1167. "consensus");
  1168. result = -2;
  1169. }
  1170. if (was_waiting_for_certs && (r < -1) && from_cache) {
  1171. if (unlink(unverified_fname) != 0) {
  1172. log_warn(LD_FS,
  1173. "Failed to unlink %s: %s",
  1174. unverified_fname, strerror(errno));
  1175. }
  1176. }
  1177. goto done;
  1178. }
  1179. }
  1180. if (!from_cache && flav == usable_consensus_flavor())
  1181. control_event_client_status(LOG_NOTICE, "CONSENSUS_ARRIVED");
  1182. /* Are we missing any certificates at all? */
  1183. if (r != 1 && dl_certs)
  1184. authority_certs_fetch_missing(c, now);
  1185. if (flav == usable_consensus_flavor()) {
  1186. notify_control_networkstatus_changed(current_consensus, c);
  1187. }
  1188. if (flav == FLAV_NS) {
  1189. if (current_ns_consensus) {
  1190. networkstatus_copy_old_consensus_info(c, current_ns_consensus);
  1191. networkstatus_vote_free(current_ns_consensus);
  1192. /* Defensive programming : we should set current_consensus very soon,
  1193. * but we're about to call some stuff in the meantime, and leaving this
  1194. * dangling pointer around has proven to be trouble. */
  1195. current_ns_consensus = NULL;
  1196. }
  1197. current_ns_consensus = c;
  1198. free_consensus = 0; /* avoid free */
  1199. } else if (flav == FLAV_MICRODESC) {
  1200. if (current_md_consensus) {
  1201. networkstatus_copy_old_consensus_info(c, current_md_consensus);
  1202. networkstatus_vote_free(current_md_consensus);
  1203. /* more defensive programming */
  1204. current_md_consensus = NULL;
  1205. }
  1206. current_md_consensus = c;
  1207. free_consensus = 0; /* avoid free */
  1208. }
  1209. waiting = &consensus_waiting_for_certs[flav];
  1210. if (waiting->consensus &&
  1211. waiting->consensus->valid_after <= c->valid_after) {
  1212. networkstatus_vote_free(waiting->consensus);
  1213. waiting->consensus = NULL;
  1214. if (consensus != waiting->body)
  1215. tor_free(waiting->body);
  1216. else
  1217. waiting->body = NULL;
  1218. waiting->set_at = 0;
  1219. waiting->dl_failed = 0;
  1220. if (unlink(unverified_fname) != 0) {
  1221. log_warn(LD_FS,
  1222. "Failed to unlink %s: %s",
  1223. unverified_fname, strerror(errno));
  1224. }
  1225. }
  1226. /* Reset the failure count only if this consensus is actually valid. */
  1227. if (c->valid_after <= now && now <= c->valid_until) {
  1228. download_status_reset(&consensus_dl_status[flav]);
  1229. } else {
  1230. if (!from_cache)
  1231. download_status_failed(&consensus_dl_status[flav], 0);
  1232. }
  1233. if (flav == usable_consensus_flavor()) {
  1234. /* XXXXNM Microdescs: needs a non-ns variant. ???? NM*/
  1235. update_consensus_networkstatus_fetch_time(now);
  1236. nodelist_set_consensus(current_consensus);
  1237. dirvote_recalculate_timing(options, now);
  1238. routerstatus_list_update_named_server_map();
  1239. /* Update ewma and adjust policy if needed; first cache the old value */
  1240. old_ewma_enabled = cell_ewma_enabled();
  1241. /* Change the cell EWMA settings */
  1242. cell_ewma_set_scale_factor(options, networkstatus_get_latest_consensus());
  1243. /* If we just enabled ewma, set the cmux policy on all active channels */
  1244. if (cell_ewma_enabled() && !old_ewma_enabled) {
  1245. channel_set_cmux_policy_everywhere(&ewma_policy);
  1246. } else if (!cell_ewma_enabled() && old_ewma_enabled) {
  1247. /* Turn it off everywhere */
  1248. channel_set_cmux_policy_everywhere(NULL);
  1249. }
  1250. /* XXXX024 this call might be unnecessary here: can changing the
  1251. * current consensus really alter our view of any OR's rate limits? */
  1252. connection_or_update_token_buckets(get_connection_array(), options);
  1253. circuit_build_times_new_consensus_params(get_circuit_build_times_mutable(),
  1254. current_consensus);
  1255. }
  1256. if (directory_caches_dir_info(options)) {
  1257. dirserv_set_cached_consensus_networkstatus(consensus,
  1258. flavor,
  1259. &c->digests,
  1260. c->valid_after);
  1261. }
  1262. if (!from_cache) {
  1263. write_str_to_file(consensus_fname, consensus, 0);
  1264. }
  1265. /** If a consensus appears more than this many seconds before its declared
  1266. * valid-after time, declare that our clock is skewed. */
  1267. #define EARLY_CONSENSUS_NOTICE_SKEW 60
  1268. if (now < c->valid_after - EARLY_CONSENSUS_NOTICE_SKEW) {
  1269. char tbuf[ISO_TIME_LEN+1];
  1270. char dbuf[64];
  1271. long delta = now - c->valid_after;
  1272. format_iso_time(tbuf, c->valid_after);
  1273. format_time_interval(dbuf, sizeof(dbuf), delta);
  1274. log_warn(LD_GENERAL, "Our clock is %s behind the time published in the "
  1275. "consensus network status document (%s UTC). Tor needs an "
  1276. "accurate clock to work correctly. Please check your time and "
  1277. "date settings!", dbuf, tbuf);
  1278. control_event_general_status(LOG_WARN,
  1279. "CLOCK_SKEW MIN_SKEW=%ld SOURCE=CONSENSUS", delta);
  1280. }
  1281. router_dir_info_changed();
  1282. result = 0;
  1283. done:
  1284. if (free_consensus)
  1285. networkstatus_vote_free(c);
  1286. tor_free(consensus_fname);
  1287. tor_free(unverified_fname);
  1288. return result;
  1289. }
  1290. /** Called when we have gotten more certificates: see whether we can
  1291. * now verify a pending consensus. */
  1292. void
  1293. networkstatus_note_certs_arrived(void)
  1294. {
  1295. int i;
  1296. for (i=0; i<N_CONSENSUS_FLAVORS; ++i) {
  1297. consensus_waiting_for_certs_t *waiting = &consensus_waiting_for_certs[i];
  1298. if (!waiting->consensus)
  1299. continue;
  1300. if (networkstatus_check_consensus_signature(waiting->consensus, 0)>=0) {
  1301. char *waiting_body = waiting->body;
  1302. if (!networkstatus_set_current_consensus(
  1303. waiting_body,
  1304. networkstatus_get_flavor_name(i),
  1305. NSSET_WAS_WAITING_FOR_CERTS)) {
  1306. tor_free(waiting_body);
  1307. }
  1308. }
  1309. }
  1310. }
  1311. /** If the network-status list has changed since the last time we called this
  1312. * function, update the status of every routerinfo from the network-status
  1313. * list. If <b>dir_version</b> is 2, it's a v2 networkstatus that changed.
  1314. * If <b>dir_version</b> is 3, it's a v3 consensus that changed.
  1315. */
  1316. void
  1317. routers_update_all_from_networkstatus(time_t now, int dir_version)
  1318. {
  1319. routerlist_t *rl = router_get_routerlist();
  1320. networkstatus_t *consensus = networkstatus_get_reasonably_live_consensus(now,
  1321. FLAV_NS);
  1322. if (!consensus || dir_version < 3) /* nothing more we should do */
  1323. return;
  1324. /* calls router_dir_info_changed() when it's done -- more routers
  1325. * might be up or down now, which might affect whether there's enough
  1326. * directory info. */
  1327. routers_update_status_from_consensus_networkstatus(rl->routers, 0);
  1328. SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri,
  1329. ri->cache_info.routerlist_index = ri_sl_idx);
  1330. if (rl->old_routers)
  1331. signed_descs_update_status_from_consensus_networkstatus(rl->old_routers);
  1332. if (!have_warned_about_old_version) {
  1333. int is_server = server_mode(get_options());
  1334. version_status_t status;
  1335. const char *recommended = is_server ?
  1336. consensus->server_versions : consensus->client_versions;
  1337. status = tor_version_is_obsolete(VERSION, recommended);
  1338. if (status == VS_RECOMMENDED) {
  1339. log_info(LD_GENERAL, "The directory authorities say my version is ok.");
  1340. } else if (status == VS_EMPTY) {
  1341. log_info(LD_GENERAL,
  1342. "The directory authorities don't recommend any versions.");
  1343. } else if (status == VS_NEW || status == VS_NEW_IN_SERIES) {
  1344. if (!have_warned_about_new_version) {
  1345. log_notice(LD_GENERAL, "This version of Tor (%s) is newer than any "
  1346. "recommended version%s, according to the directory "
  1347. "authorities. Recommended versions are: %s",
  1348. VERSION,
  1349. status == VS_NEW_IN_SERIES ? " in its series" : "",
  1350. recommended);
  1351. have_warned_about_new_version = 1;
  1352. control_event_general_status(LOG_WARN, "DANGEROUS_VERSION "
  1353. "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
  1354. VERSION, "NEW", recommended);
  1355. }
  1356. } else {
  1357. log_warn(LD_GENERAL, "Please upgrade! "
  1358. "This version of Tor (%s) is %s, according to the directory "
  1359. "authorities. Recommended versions are: %s",
  1360. VERSION,
  1361. status == VS_OLD ? "obsolete" : "not recommended",
  1362. recommended);
  1363. have_warned_about_old_version = 1;
  1364. control_event_general_status(LOG_WARN, "DANGEROUS_VERSION "
  1365. "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
  1366. VERSION, status == VS_OLD ? "OBSOLETE" : "UNRECOMMENDED",
  1367. recommended);
  1368. }
  1369. }
  1370. }
  1371. /** Update our view of the list of named servers from the most recently
  1372. * retrieved networkstatus consensus. */
  1373. static void
  1374. routerstatus_list_update_named_server_map(void)
  1375. {
  1376. if (!current_consensus)
  1377. return;
  1378. strmap_free(named_server_map, tor_free_);
  1379. named_server_map = strmap_new();
  1380. strmap_free(unnamed_server_map, NULL);
  1381. unnamed_server_map = strmap_new();
  1382. SMARTLIST_FOREACH_BEGIN(current_consensus->routerstatus_list,
  1383. const routerstatus_t *, rs) {
  1384. if (rs->is_named) {
  1385. strmap_set_lc(named_server_map, rs->nickname,
  1386. tor_memdup(rs->identity_digest, DIGEST_LEN));
  1387. }
  1388. if (rs->is_unnamed) {
  1389. strmap_set_lc(unnamed_server_map, rs->nickname, (void*)1);
  1390. }
  1391. } SMARTLIST_FOREACH_END(rs);
  1392. }
  1393. /** Given a list <b>routers</b> of routerinfo_t *, update each status field
  1394. * according to our current consensus networkstatus. May re-order
  1395. * <b>routers</b>. */
  1396. void
  1397. routers_update_status_from_consensus_networkstatus(smartlist_t *routers,
  1398. int reset_failures)
  1399. {
  1400. const or_options_t *options = get_options();
  1401. int authdir = authdir_mode_v3(options);
  1402. networkstatus_t *ns = current_consensus;
  1403. if (!ns || !smartlist_len(ns->routerstatus_list))
  1404. return;
  1405. routers_sort_by_identity(routers);
  1406. SMARTLIST_FOREACH_JOIN(ns->routerstatus_list, routerstatus_t *, rs,
  1407. routers, routerinfo_t *, router,
  1408. tor_memcmp(rs->identity_digest,
  1409. router->cache_info.identity_digest, DIGEST_LEN),
  1410. {
  1411. }) {
  1412. /* Is it the same descriptor, or only the same identity? */
  1413. if (tor_memeq(router->cache_info.signed_descriptor_digest,
  1414. rs->descriptor_digest, DIGEST_LEN)) {
  1415. if (ns->valid_until > router->cache_info.last_listed_as_valid_until)
  1416. router->cache_info.last_listed_as_valid_until = ns->valid_until;
  1417. }
  1418. if (authdir) {
  1419. /* If we _are_ an authority, we should check whether this router
  1420. * is one that will cause us to need a reachability test. */
  1421. routerinfo_t *old_router =
  1422. router_get_mutable_by_digest(router->cache_info.identity_digest);
  1423. if (old_router != router) {
  1424. router->needs_retest_if_added =
  1425. dirserv_should_launch_reachability_test(router, old_router);
  1426. }
  1427. }
  1428. if (reset_failures) {
  1429. download_status_reset(&rs->dl_status);
  1430. }
  1431. } SMARTLIST_FOREACH_JOIN_END(rs, router);
  1432. router_dir_info_changed();
  1433. }
  1434. /** Given a list of signed_descriptor_t, update their fields (mainly, when
  1435. * they were last listed) from the most recent consensus. */
  1436. void
  1437. signed_descs_update_status_from_consensus_networkstatus(smartlist_t *descs)
  1438. {
  1439. networkstatus_t *ns = current_ns_consensus;
  1440. if (!ns)
  1441. return;
  1442. if (!ns->desc_digest_map) {
  1443. char dummy[DIGEST_LEN];
  1444. /* instantiates the digest map. */
  1445. memset(dummy, 0, sizeof(dummy));
  1446. router_get_consensus_status_by_descriptor_digest(ns, dummy);
  1447. }
  1448. SMARTLIST_FOREACH(descs, signed_descriptor_t *, d,
  1449. {
  1450. const routerstatus_t *rs = digestmap_get(ns->desc_digest_map,
  1451. d->signed_descriptor_digest);
  1452. if (rs) {
  1453. if (ns->valid_until > d->last_listed_as_valid_until)
  1454. d->last_listed_as_valid_until = ns->valid_until;
  1455. }
  1456. });
  1457. }
  1458. /** Generate networkstatus lines for a single routerstatus_t object, and
  1459. * return the result in a newly allocated string. Used only by controller
  1460. * interface (for now.) */
  1461. char *
  1462. networkstatus_getinfo_helper_single(const routerstatus_t *rs)
  1463. {
  1464. return routerstatus_format_entry(rs, NULL, NS_CONTROL_PORT, NULL);
  1465. }
  1466. /** Alloc and return a string describing routerstatuses for the most
  1467. * recent info of each router we know about that is of purpose
  1468. * <b>purpose_string</b>. Return NULL if unrecognized purpose.
  1469. *
  1470. * Right now this function is oriented toward listing bridges (you
  1471. * shouldn't use this for general-purpose routers, since those
  1472. * should be listed from the consensus, not from the routers list). */
  1473. char *
  1474. networkstatus_getinfo_by_purpose(const char *purpose_string, time_t now)
  1475. {
  1476. time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
  1477. char *answer;
  1478. routerlist_t *rl = router_get_routerlist();
  1479. smartlist_t *statuses;
  1480. uint8_t purpose = router_purpose_from_string(purpose_string);
  1481. routerstatus_t rs;
  1482. int bridge_auth = authdir_mode_bridge(get_options());
  1483. if (purpose == ROUTER_PURPOSE_UNKNOWN) {
  1484. log_info(LD_DIR, "Unrecognized purpose '%s' when listing router statuses.",
  1485. purpose_string);
  1486. return NULL;
  1487. }
  1488. statuses = smartlist_new();
  1489. SMARTLIST_FOREACH_BEGIN(rl->routers, routerinfo_t *, ri) {
  1490. node_t *node = node_get_mutable_by_id(ri->cache_info.identity_digest);
  1491. if (!node)
  1492. continue;
  1493. if (ri->cache_info.published_on < cutoff)
  1494. continue;
  1495. if (ri->purpose != purpose)
  1496. continue;
  1497. if (bridge_auth && ri->purpose == ROUTER_PURPOSE_BRIDGE)
  1498. dirserv_set_router_is_running(ri, now);
  1499. /* then generate and write out status lines for each of them */
  1500. set_routerstatus_from_routerinfo(&rs, node, ri, now, 0, 0);
  1501. smartlist_add(statuses, networkstatus_getinfo_helper_single(&rs));
  1502. } SMARTLIST_FOREACH_END(ri);
  1503. answer = smartlist_join_strings(statuses, "", 0, NULL);
  1504. SMARTLIST_FOREACH(statuses, char *, cp, tor_free(cp));
  1505. smartlist_free(statuses);
  1506. return answer;
  1507. }
  1508. /** Write out router status entries for all our bridge descriptors. */
  1509. void
  1510. networkstatus_dump_bridge_status_to_file(time_t now)
  1511. {
  1512. char *status = networkstatus_getinfo_by_purpose("bridge", now);
  1513. const or_options_t *options = get_options();
  1514. char *fname = NULL;
  1515. char *thresholds = NULL, *thresholds_and_status = NULL;
  1516. routerlist_t *rl = router_get_routerlist();
  1517. dirserv_compute_bridge_flag_thresholds(rl);
  1518. thresholds = dirserv_get_flag_thresholds_line();
  1519. tor_asprintf(&thresholds_and_status, "flag-thresholds %s\n%s",
  1520. thresholds, status);
  1521. tor_asprintf(&fname, "%s"PATH_SEPARATOR"networkstatus-bridges",
  1522. options->DataDirectory);
  1523. write_str_to_file(fname,thresholds_and_status,0);
  1524. tor_free(thresholds);
  1525. tor_free(thresholds_and_status);
  1526. tor_free(fname);
  1527. tor_free(status);
  1528. }
  1529. /* DOCDOC get_net_param_from_list */
  1530. static int32_t
  1531. get_net_param_from_list(smartlist_t *net_params, const char *param_name,
  1532. int32_t default_val, int32_t min_val, int32_t max_val)
  1533. {
  1534. int32_t res = default_val;
  1535. size_t name_len = strlen(param_name);
  1536. tor_assert(max_val > min_val);
  1537. tor_assert(min_val <= default_val);
  1538. tor_assert(max_val >= default_val);
  1539. SMARTLIST_FOREACH_BEGIN(net_params, const char *, p) {
  1540. if (!strcmpstart(p, param_name) && p[name_len] == '=') {
  1541. int ok=0;
  1542. long v = tor_parse_long(p+name_len+1, 10, INT32_MIN,
  1543. INT32_MAX, &ok, NULL);
  1544. if (ok) {
  1545. res = (int32_t) v;
  1546. break;
  1547. }
  1548. }
  1549. } SMARTLIST_FOREACH_END(p);
  1550. if (res < min_val) {
  1551. log_warn(LD_DIR, "Consensus parameter %s is too small. Got %d, raising to "
  1552. "%d.", param_name, res, min_val);
  1553. res = min_val;
  1554. } else if (res > max_val) {
  1555. log_warn(LD_DIR, "Consensus parameter %s is too large. Got %d, capping to "
  1556. "%d.", param_name, res, max_val);
  1557. res = max_val;
  1558. }
  1559. return res;
  1560. }
  1561. /** Return the value of a integer parameter from the networkstatus <b>ns</b>
  1562. * whose name is <b>param_name</b>. If <b>ns</b> is NULL, try loading the
  1563. * latest consensus ourselves. Return <b>default_val</b> if no latest
  1564. * consensus, or if it has no parameter called <b>param_name</b>.
  1565. * Make sure the value parsed from the consensus is at least
  1566. * <b>min_val</b> and at most <b>max_val</b> and raise/cap the parsed value
  1567. * if necessary. */
  1568. int32_t
  1569. networkstatus_get_param(const networkstatus_t *ns, const char *param_name,
  1570. int32_t default_val, int32_t min_val, int32_t max_val)
  1571. {
  1572. if (!ns) /* if they pass in null, go find it ourselves */
  1573. ns = networkstatus_get_latest_consensus();
  1574. if (!ns || !ns->net_params)
  1575. return default_val;
  1576. return get_net_param_from_list(ns->net_params, param_name,
  1577. default_val, min_val, max_val);
  1578. }
  1579. /**
  1580. * Retrieve the consensus parameter that governs the
  1581. * fixed-point precision of our network balancing 'bandwidth-weights'
  1582. * (which are themselves integer consensus values). We divide them
  1583. * by this value and ensure they never exceed this value.
  1584. */
  1585. int
  1586. networkstatus_get_weight_scale_param(networkstatus_t *ns)
  1587. {
  1588. return networkstatus_get_param(ns, "bwweightscale",
  1589. BW_WEIGHT_SCALE,
  1590. BW_MIN_WEIGHT_SCALE,
  1591. BW_MAX_WEIGHT_SCALE);
  1592. }
  1593. /** Return the value of a integer bw weight parameter from the networkstatus
  1594. * <b>ns</b> whose name is <b>weight_name</b>. If <b>ns</b> is NULL, try
  1595. * loading the latest consensus ourselves. Return <b>default_val</b> if no
  1596. * latest consensus, or if it has no parameter called <b>weight_name</b>. */
  1597. int32_t
  1598. networkstatus_get_bw_weight(networkstatus_t *ns, const char *weight_name,
  1599. int32_t default_val)
  1600. {
  1601. int32_t param;
  1602. int max;
  1603. if (!ns) /* if they pass in null, go find it ourselves */
  1604. ns = networkstatus_get_latest_consensus();
  1605. if (!ns || !ns->weight_params)
  1606. return default_val;
  1607. max = networkstatus_get_weight_scale_param(ns);
  1608. param = get_net_param_from_list(ns->weight_params, weight_name,
  1609. default_val, -1,
  1610. BW_MAX_WEIGHT_SCALE);
  1611. if (param > max) {
  1612. log_warn(LD_DIR, "Value of consensus weight %s was too large, capping "
  1613. "to %d", weight_name, max);
  1614. param = max;
  1615. }
  1616. return param;
  1617. }
  1618. /** Return the name of the consensus flavor <b>flav</b> as used to identify
  1619. * the flavor in directory documents. */
  1620. const char *
  1621. networkstatus_get_flavor_name(consensus_flavor_t flav)
  1622. {
  1623. switch (flav) {
  1624. case FLAV_NS:
  1625. return "ns";
  1626. case FLAV_MICRODESC:
  1627. return "microdesc";
  1628. default:
  1629. tor_fragile_assert();
  1630. return "??";
  1631. }
  1632. }
  1633. /** Return the consensus_flavor_t value for the flavor called <b>flavname</b>,
  1634. * or -1 if the flavor is not recognized. */
  1635. int
  1636. networkstatus_parse_flavor_name(const char *flavname)
  1637. {
  1638. if (!strcmp(flavname, "ns"))
  1639. return FLAV_NS;
  1640. else if (!strcmp(flavname, "microdesc"))
  1641. return FLAV_MICRODESC;
  1642. else
  1643. return -1;
  1644. }
  1645. /** Return 0 if this routerstatus is obsolete, too new, isn't
  1646. * running, or otherwise not a descriptor that we would make any
  1647. * use of even if we had it. Else return 1. */
  1648. int
  1649. client_would_use_router(const routerstatus_t *rs, time_t now,
  1650. const or_options_t *options)
  1651. {
  1652. if (!rs->is_flagged_running && !options->FetchUselessDescriptors) {
  1653. /* If we had this router descriptor, we wouldn't even bother using it.
  1654. * But, if we want to have a complete list, fetch it anyway. */
  1655. return 0;
  1656. }
  1657. if (rs->published_on + options->TestingEstimatedDescriptorPropagationTime
  1658. > now) {
  1659. /* Most caches probably don't have this descriptor yet. */
  1660. return 0;
  1661. }
  1662. if (rs->published_on + OLD_ROUTER_DESC_MAX_AGE < now) {
  1663. /* We'd drop it immediately for being too old. */
  1664. return 0;
  1665. }
  1666. return 1;
  1667. }
  1668. /** If <b>question</b> is a string beginning with "ns/" in a format the
  1669. * control interface expects for a GETINFO question, set *<b>answer</b> to a
  1670. * newly-allocated string containing networkstatus lines for the appropriate
  1671. * ORs. Return 0 on success, -1 on unrecognized question format. */
  1672. int
  1673. getinfo_helper_networkstatus(control_connection_t *conn,
  1674. const char *question, char **answer,
  1675. const char **errmsg)
  1676. {
  1677. const routerstatus_t *status;
  1678. (void) conn;
  1679. if (!current_consensus) {
  1680. *answer = tor_strdup("");
  1681. return 0;
  1682. }
  1683. if (!strcmp(question, "ns/all")) {
  1684. smartlist_t *statuses = smartlist_new();
  1685. SMARTLIST_FOREACH(current_consensus->routerstatus_list,
  1686. const routerstatus_t *, rs,
  1687. {
  1688. smartlist_add(statuses, networkstatus_getinfo_helper_single(rs));
  1689. });
  1690. *answer = smartlist_join_strings(statuses, "", 0, NULL);
  1691. SMARTLIST_FOREACH(statuses, char *, cp, tor_free(cp));
  1692. smartlist_free(statuses);
  1693. return 0;
  1694. } else if (!strcmpstart(question, "ns/id/")) {
  1695. char d[DIGEST_LEN];
  1696. const char *q = question + 6;
  1697. if (*q == '$')
  1698. ++q;
  1699. if (base16_decode(d, DIGEST_LEN, q, strlen(q))) {
  1700. *errmsg = "Data not decodeable as hex";
  1701. return -1;
  1702. }
  1703. status = router_get_consensus_status_by_id(d);
  1704. } else if (!strcmpstart(question, "ns/name/")) {
  1705. status = router_get_consensus_status_by_nickname(question+8, 0);
  1706. } else if (!strcmpstart(question, "ns/purpose/")) {
  1707. *answer = networkstatus_getinfo_by_purpose(question+11, time(NULL));
  1708. return *answer ? 0 : -1;
  1709. } else {
  1710. return 0;
  1711. }
  1712. if (status)
  1713. *answer = networkstatus_getinfo_helper_single(status);
  1714. return 0;
  1715. }
  1716. /** Free all storage held locally in this module. */
  1717. void
  1718. networkstatus_free_all(void)
  1719. {
  1720. int i;
  1721. networkstatus_vote_free(current_ns_consensus);
  1722. networkstatus_vote_free(current_md_consensus);
  1723. current_md_consensus = current_ns_consensus = NULL;
  1724. for (i=0; i < N_CONSENSUS_FLAVORS; ++i) {
  1725. consensus_waiting_for_certs_t *waiting = &consensus_waiting_for_certs[i];
  1726. if (waiting->consensus) {
  1727. networkstatus_vote_free(waiting->consensus);
  1728. waiting->consensus = NULL;
  1729. }
  1730. tor_free(waiting->body);
  1731. }
  1732. strmap_free(named_server_map, tor_free_);
  1733. strmap_free(unnamed_server_map, NULL);
  1734. }