networkstatus.c 81 KB

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