networkstatus.c 79 KB

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