networkstatus.c 62 KB

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