networkstatus.c 84 KB

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