networkstatus.c 82 KB

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