networkstatus.c 82 KB

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