networkstatus.c 81 KB

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