networkstatus.c 80 KB

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