networkstatus.c 84 KB

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