networkstatus.c 70 KB

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