networkstatus.c 83 KB

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