networkstatus.c 81 KB

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