networkstatus.c 76 KB

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