networkstatus.c 68 KB

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