dirserv.c 132 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2016, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #define DIRSERV_PRIVATE
  6. #include "or.h"
  7. #include "buffers.h"
  8. #include "config.h"
  9. #include "confparse.h"
  10. #include "channel.h"
  11. #include "channeltls.h"
  12. #include "command.h"
  13. #include "connection.h"
  14. #include "connection_or.h"
  15. #include "control.h"
  16. #include "directory.h"
  17. #include "dirserv.h"
  18. #include "dirvote.h"
  19. #include "hibernate.h"
  20. #include "keypin.h"
  21. #include "main.h"
  22. #include "microdesc.h"
  23. #include "networkstatus.h"
  24. #include "nodelist.h"
  25. #include "policies.h"
  26. #include "protover.h"
  27. #include "rephist.h"
  28. #include "router.h"
  29. #include "routerlist.h"
  30. #include "routerparse.h"
  31. #include "routerset.h"
  32. #include "torcert.h"
  33. /**
  34. * \file dirserv.c
  35. * \brief Directory server core implementation. Manages directory
  36. * contents and generates directories.
  37. *
  38. * This module implements most of directory cache functionality, and some of
  39. * the directory authority functionality. The directory.c module delegates
  40. * here in order to handle incoming requests from clients, via
  41. * connection_dirserv_flushed_some() and its kin. In order to save RAM, this
  42. * module is reponsible for spooling directory objects (in whole or in part)
  43. * onto buf_t instances, and then closing the dir_connection_t once the
  44. * objects are totally flushed.
  45. *
  46. * The directory.c module also delegates here for handling descriptor uploads
  47. * via dirserv_add_multiple_descriptors().
  48. *
  49. * Additionally, this module handles some aspects of voting, including:
  50. * deciding how to vote on individual flags (based on decisions reached in
  51. * rephist.c), of formatting routerstatus lines, and deciding what relays to
  52. * include in an authority's vote. (TODO: Those functions could profitably be
  53. * split off. They only live in this file because historically they were
  54. * shared among the v1, v2, and v3 directory code.)
  55. */
  56. /** How far in the future do we allow a router to get? (seconds) */
  57. #define ROUTER_ALLOW_SKEW (60*60*12)
  58. /** How many seconds do we wait before regenerating the directory? */
  59. #define DIR_REGEN_SLACK_TIME 30
  60. /** If we're a cache, keep this many networkstatuses around from non-trusted
  61. * directory authorities. */
  62. #define MAX_UNTRUSTED_NETWORKSTATUSES 16
  63. /** Total number of routers with measured bandwidth; this is set by
  64. * dirserv_count_measured_bws() before the loop in
  65. * dirserv_generate_networkstatus_vote_obj() and checked by
  66. * dirserv_get_credible_bandwidth() and
  67. * dirserv_compute_performance_thresholds() */
  68. static int routers_with_measured_bw = 0;
  69. static void directory_remove_invalid(void);
  70. static char *format_versions_list(config_line_t *ln);
  71. struct authdir_config_t;
  72. static uint32_t
  73. dirserv_get_status_impl(const char *fp, const char *nickname,
  74. uint32_t addr, uint16_t or_port,
  75. const char *platform, const char **msg,
  76. int severity);
  77. static void clear_cached_dir(cached_dir_t *d);
  78. static const signed_descriptor_t *get_signed_descriptor_by_fp(
  79. const char *fp,
  80. int extrainfo,
  81. time_t publish_cutoff);
  82. static was_router_added_t dirserv_add_extrainfo(extrainfo_t *ei,
  83. const char **msg);
  84. static uint32_t dirserv_get_bandwidth_for_router_kb(const routerinfo_t *ri);
  85. static uint32_t dirserv_get_credible_bandwidth_kb(const routerinfo_t *ri);
  86. /************** Fingerprint handling code ************/
  87. /* 1 Historically used to indicate Named */
  88. #define FP_INVALID 2 /**< Believed invalid. */
  89. #define FP_REJECT 4 /**< We will not publish this router. */
  90. /* 8 Historically used to avoid using this as a dir. */
  91. #define FP_BADEXIT 16 /**< We'll tell clients not to use this as an exit. */
  92. /* 32 Historically used to indicade Unnamed */
  93. /** Target of status_by_digest map. */
  94. typedef uint32_t router_status_t;
  95. static void add_fingerprint_to_dir(const char *fp,
  96. struct authdir_config_t *list,
  97. router_status_t add_status);
  98. /** List of nickname-\>identity fingerprint mappings for all the routers
  99. * that we name. Used to prevent router impersonation. */
  100. typedef struct authdir_config_t {
  101. strmap_t *fp_by_name; /**< Map from lc nickname to fingerprint. */
  102. digestmap_t *status_by_digest; /**< Map from digest to router_status_t. */
  103. } authdir_config_t;
  104. /** Should be static; exposed for testing. */
  105. static authdir_config_t *fingerprint_list = NULL;
  106. /** Allocate and return a new, empty, authdir_config_t. */
  107. static authdir_config_t *
  108. authdir_config_new(void)
  109. {
  110. authdir_config_t *list = tor_malloc_zero(sizeof(authdir_config_t));
  111. list->fp_by_name = strmap_new();
  112. list->status_by_digest = digestmap_new();
  113. return list;
  114. }
  115. /** Add the fingerprint <b>fp</b> to the smartlist of fingerprint_entry_t's
  116. * <b>list</b>, or-ing the currently set status flags with
  117. * <b>add_status</b>.
  118. */
  119. /* static */ void
  120. add_fingerprint_to_dir(const char *fp, authdir_config_t *list,
  121. router_status_t add_status)
  122. {
  123. char *fingerprint;
  124. char d[DIGEST_LEN];
  125. router_status_t *status;
  126. tor_assert(fp);
  127. tor_assert(list);
  128. fingerprint = tor_strdup(fp);
  129. tor_strstrip(fingerprint, " ");
  130. if (base16_decode(d, DIGEST_LEN,
  131. fingerprint, strlen(fingerprint)) != DIGEST_LEN) {
  132. log_warn(LD_DIRSERV, "Couldn't decode fingerprint \"%s\"",
  133. escaped(fp));
  134. tor_free(fingerprint);
  135. return;
  136. }
  137. status = digestmap_get(list->status_by_digest, d);
  138. if (!status) {
  139. status = tor_malloc_zero(sizeof(router_status_t));
  140. digestmap_set(list->status_by_digest, d, status);
  141. }
  142. tor_free(fingerprint);
  143. *status |= add_status;
  144. return;
  145. }
  146. /** Add the fingerprint for this OR to the global list of recognized
  147. * identity key fingerprints. */
  148. int
  149. dirserv_add_own_fingerprint(crypto_pk_t *pk)
  150. {
  151. char fp[FINGERPRINT_LEN+1];
  152. if (crypto_pk_get_fingerprint(pk, fp, 0)<0) {
  153. log_err(LD_BUG, "Error computing fingerprint");
  154. return -1;
  155. }
  156. if (!fingerprint_list)
  157. fingerprint_list = authdir_config_new();
  158. add_fingerprint_to_dir(fp, fingerprint_list, 0);
  159. return 0;
  160. }
  161. /** Load the nickname-\>fingerprint mappings stored in the approved-routers
  162. * file. The file format is line-based, with each non-blank holding one
  163. * nickname, some space, and a fingerprint for that nickname. On success,
  164. * replace the current fingerprint list with the new list and return 0. On
  165. * failure, leave the current fingerprint list untouched, and return -1. */
  166. int
  167. dirserv_load_fingerprint_file(void)
  168. {
  169. char *fname;
  170. char *cf;
  171. char *nickname, *fingerprint;
  172. authdir_config_t *fingerprint_list_new;
  173. int result;
  174. config_line_t *front=NULL, *list;
  175. fname = get_datadir_fname("approved-routers");
  176. log_info(LD_GENERAL,
  177. "Reloading approved fingerprints from \"%s\"...", fname);
  178. cf = read_file_to_str(fname, RFTS_IGNORE_MISSING, NULL);
  179. if (!cf) {
  180. log_warn(LD_FS, "Cannot open fingerprint file '%s'. That's ok.", fname);
  181. tor_free(fname);
  182. return 0;
  183. }
  184. tor_free(fname);
  185. result = config_get_lines(cf, &front, 0);
  186. tor_free(cf);
  187. if (result < 0) {
  188. log_warn(LD_CONFIG, "Error reading from fingerprint file");
  189. return -1;
  190. }
  191. fingerprint_list_new = authdir_config_new();
  192. for (list=front; list; list=list->next) {
  193. char digest_tmp[DIGEST_LEN];
  194. router_status_t add_status = 0;
  195. nickname = list->key; fingerprint = list->value;
  196. tor_strstrip(fingerprint, " "); /* remove spaces */
  197. if (strlen(fingerprint) != HEX_DIGEST_LEN ||
  198. base16_decode(digest_tmp, sizeof(digest_tmp),
  199. fingerprint, HEX_DIGEST_LEN) != sizeof(digest_tmp)) {
  200. log_notice(LD_CONFIG,
  201. "Invalid fingerprint (nickname '%s', "
  202. "fingerprint %s). Skipping.",
  203. nickname, fingerprint);
  204. continue;
  205. }
  206. if (!strcasecmp(nickname, "!reject")) {
  207. add_status = FP_REJECT;
  208. } else if (!strcasecmp(nickname, "!badexit")) {
  209. add_status = FP_BADEXIT;
  210. } else if (!strcasecmp(nickname, "!invalid")) {
  211. add_status = FP_INVALID;
  212. }
  213. add_fingerprint_to_dir(fingerprint, fingerprint_list_new, add_status);
  214. }
  215. config_free_lines(front);
  216. dirserv_free_fingerprint_list();
  217. fingerprint_list = fingerprint_list_new;
  218. /* Delete any routers whose fingerprints we no longer recognize */
  219. directory_remove_invalid();
  220. return 0;
  221. }
  222. /* If this is set, then we don't allow routers that have advertised an Ed25519
  223. * identity to stop doing so. This is going to be essential for good identity
  224. * security: otherwise anybody who can attack RSA-1024 but not Ed25519 could
  225. * just sign fake descriptors missing the Ed25519 key. But we won't actually
  226. * be able to prevent that kind of thing until we're confident that there
  227. * isn't actually a legit reason to downgrade to 0.2.5. So for now, we have
  228. * to leave this #undef.
  229. */
  230. #undef DISABLE_DISABLING_ED25519
  231. /** Check whether <b>router</b> has a nickname/identity key combination that
  232. * we recognize from the fingerprint list, or an IP we automatically act on
  233. * according to our configuration. Return the appropriate router status.
  234. *
  235. * If the status is 'FP_REJECT' and <b>msg</b> is provided, set
  236. * *<b>msg</b> to an explanation of why. */
  237. uint32_t
  238. dirserv_router_get_status(const routerinfo_t *router, const char **msg,
  239. int severity)
  240. {
  241. char d[DIGEST_LEN];
  242. const int key_pinning = get_options()->AuthDirPinKeys;
  243. if (crypto_pk_get_digest(router->identity_pkey, d)) {
  244. log_warn(LD_BUG,"Error computing fingerprint");
  245. if (msg)
  246. *msg = "Bug: Error computing fingerprint";
  247. return FP_REJECT;
  248. }
  249. /* dirserv_get_status_impl already rejects versions older than 0.2.4.18-rc,
  250. * and onion_curve25519_pkey was introduced in 0.2.4.8-alpha.
  251. * But just in case a relay doesn't provide or lies about its version, or
  252. * doesn't include an ntor key in its descriptor, check that it exists,
  253. * and is non-zero (clients check that it's non-zero before using it). */
  254. if (!routerinfo_has_curve25519_onion_key(router)) {
  255. log_fn(severity, LD_DIR,
  256. "Descriptor from router %s is missing an ntor curve25519 onion "
  257. "key.", router_describe(router));
  258. if (msg)
  259. *msg = "Missing ntor curve25519 onion key. Please upgrade!";
  260. return FP_REJECT;
  261. }
  262. if (router->cache_info.signing_key_cert) {
  263. /* This has an ed25519 identity key. */
  264. if (KEYPIN_MISMATCH ==
  265. keypin_check((const uint8_t*)router->cache_info.identity_digest,
  266. router->cache_info.signing_key_cert->signing_key.pubkey)) {
  267. log_fn(severity, LD_DIR,
  268. "Descriptor from router %s has an Ed25519 key, "
  269. "but the <rsa,ed25519> keys don't match what they were before.",
  270. router_describe(router));
  271. if (key_pinning) {
  272. if (msg) {
  273. *msg = "Ed25519 identity key or RSA identity key has changed.";
  274. }
  275. return FP_REJECT;
  276. }
  277. }
  278. } else {
  279. /* No ed25519 key */
  280. if (KEYPIN_MISMATCH == keypin_check_lone_rsa(
  281. (const uint8_t*)router->cache_info.identity_digest)) {
  282. log_fn(severity, LD_DIR,
  283. "Descriptor from router %s has no Ed25519 key, "
  284. "when we previously knew an Ed25519 for it. Ignoring for now, "
  285. "since Ed25519 keys are fairly new.",
  286. router_describe(router));
  287. #ifdef DISABLE_DISABLING_ED25519
  288. if (key_pinning) {
  289. if (msg) {
  290. *msg = "Ed25519 identity key has disappeared.";
  291. }
  292. return FP_REJECT;
  293. }
  294. #endif
  295. }
  296. }
  297. return dirserv_get_status_impl(d, router->nickname,
  298. router->addr, router->or_port,
  299. router->platform, msg, severity);
  300. }
  301. /** Return true if there is no point in downloading the router described by
  302. * <b>rs</b> because this directory would reject it. */
  303. int
  304. dirserv_would_reject_router(const routerstatus_t *rs)
  305. {
  306. uint32_t res;
  307. res = dirserv_get_status_impl(rs->identity_digest, rs->nickname,
  308. rs->addr, rs->or_port,
  309. NULL, NULL, LOG_DEBUG);
  310. return (res & FP_REJECT) != 0;
  311. }
  312. /** Helper: As dirserv_router_get_status, but takes the router fingerprint
  313. * (hex, no spaces), nickname, address (used for logging only), IP address, OR
  314. * port and platform (logging only) as arguments.
  315. *
  316. * Log messages at 'severity'. (There's not much point in
  317. * logging that we're rejecting servers we'll not download.)
  318. */
  319. static uint32_t
  320. dirserv_get_status_impl(const char *id_digest, const char *nickname,
  321. uint32_t addr, uint16_t or_port,
  322. const char *platform, const char **msg, int severity)
  323. {
  324. uint32_t result = 0;
  325. router_status_t *status_by_digest;
  326. if (!fingerprint_list)
  327. fingerprint_list = authdir_config_new();
  328. log_debug(LD_DIRSERV, "%d fingerprints, %d digests known.",
  329. strmap_size(fingerprint_list->fp_by_name),
  330. digestmap_size(fingerprint_list->status_by_digest));
  331. if (platform) {
  332. tor_version_t ver_tmp;
  333. if (tor_version_parse_platform(platform, &ver_tmp, 1) < 0) {
  334. if (msg) {
  335. *msg = "Malformed platform string.";
  336. }
  337. return FP_REJECT;
  338. }
  339. }
  340. /* Versions before Tor 0.2.4.18-rc are too old to support, and are
  341. * missing some important security fixes too. Disable them. */
  342. if (platform && !tor_version_as_new_as(platform,"0.2.4.18-rc")) {
  343. if (msg)
  344. *msg = "Tor version is insecure or unsupported. Please upgrade!";
  345. return FP_REJECT;
  346. }
  347. /* Tor 0.2.9.x where x<5 suffers from bug #20499, where relays don't
  348. * keep their consensus up to date so they make bad guards.
  349. * The simple fix is to just drop them from the network. */
  350. if (platform &&
  351. tor_version_as_new_as(platform,"0.2.9.0-alpha") &&
  352. !tor_version_as_new_as(platform,"0.2.9.5-alpha")) {
  353. if (msg)
  354. *msg = "Tor version contains bug 20499. Please upgrade!";
  355. return FP_REJECT;
  356. }
  357. status_by_digest = digestmap_get(fingerprint_list->status_by_digest,
  358. id_digest);
  359. if (status_by_digest)
  360. result |= *status_by_digest;
  361. if (result & FP_REJECT) {
  362. if (msg)
  363. *msg = "Fingerprint is marked rejected -- please contact us?";
  364. return FP_REJECT;
  365. } else if (result & FP_INVALID) {
  366. if (msg)
  367. *msg = "Fingerprint is marked invalid";
  368. }
  369. if (authdir_policy_badexit_address(addr, or_port)) {
  370. log_fn(severity, LD_DIRSERV,
  371. "Marking '%s' as bad exit because of address '%s'",
  372. nickname, fmt_addr32(addr));
  373. result |= FP_BADEXIT;
  374. }
  375. if (!authdir_policy_permits_address(addr, or_port)) {
  376. log_fn(severity, LD_DIRSERV, "Rejecting '%s' because of address '%s'",
  377. nickname, fmt_addr32(addr));
  378. if (msg)
  379. *msg = "Suspicious relay address range -- please contact us?";
  380. return FP_REJECT;
  381. }
  382. if (!authdir_policy_valid_address(addr, or_port)) {
  383. log_fn(severity, LD_DIRSERV,
  384. "Not marking '%s' valid because of address '%s'",
  385. nickname, fmt_addr32(addr));
  386. result |= FP_INVALID;
  387. }
  388. return result;
  389. }
  390. /** Clear the current fingerprint list. */
  391. void
  392. dirserv_free_fingerprint_list(void)
  393. {
  394. if (!fingerprint_list)
  395. return;
  396. strmap_free(fingerprint_list->fp_by_name, tor_free_);
  397. digestmap_free(fingerprint_list->status_by_digest, tor_free_);
  398. tor_free(fingerprint_list);
  399. }
  400. /*
  401. * Descriptor list
  402. */
  403. /** Return -1 if <b>ri</b> has a private or otherwise bad address,
  404. * unless we're configured to not care. Return 0 if all ok. */
  405. static int
  406. dirserv_router_has_valid_address(routerinfo_t *ri)
  407. {
  408. tor_addr_t addr;
  409. if (get_options()->DirAllowPrivateAddresses)
  410. return 0; /* whatever it is, we're fine with it */
  411. tor_addr_from_ipv4h(&addr, ri->addr);
  412. if (tor_addr_is_internal(&addr, 0)) {
  413. log_info(LD_DIRSERV,
  414. "Router %s published internal IP address. Refusing.",
  415. router_describe(ri));
  416. return -1; /* it's a private IP, we should reject it */
  417. }
  418. return 0;
  419. }
  420. /** Check whether we, as a directory server, want to accept <b>ri</b>. If so,
  421. * set its is_valid,running fields and return 0. Otherwise, return -1.
  422. *
  423. * If the router is rejected, set *<b>msg</b> to an explanation of why.
  424. *
  425. * If <b>complain</b> then explain at log-level 'notice' why we refused
  426. * a descriptor; else explain at log-level 'info'.
  427. */
  428. int
  429. authdir_wants_to_reject_router(routerinfo_t *ri, const char **msg,
  430. int complain, int *valid_out)
  431. {
  432. /* Okay. Now check whether the fingerprint is recognized. */
  433. time_t now;
  434. int severity = (complain && ri->contact_info) ? LOG_NOTICE : LOG_INFO;
  435. uint32_t status = dirserv_router_get_status(ri, msg, severity);
  436. tor_assert(msg);
  437. if (status & FP_REJECT)
  438. return -1; /* msg is already set. */
  439. /* Is there too much clock skew? */
  440. now = time(NULL);
  441. if (ri->cache_info.published_on > now+ROUTER_ALLOW_SKEW) {
  442. log_fn(severity, LD_DIRSERV, "Publication time for %s is too "
  443. "far (%d minutes) in the future; possible clock skew. Not adding "
  444. "(%s)",
  445. router_describe(ri),
  446. (int)((ri->cache_info.published_on-now)/60),
  447. esc_router_info(ri));
  448. *msg = "Rejected: Your clock is set too far in the future, or your "
  449. "timezone is not correct.";
  450. return -1;
  451. }
  452. if (ri->cache_info.published_on < now-ROUTER_MAX_AGE_TO_PUBLISH) {
  453. log_fn(severity, LD_DIRSERV,
  454. "Publication time for %s is too far "
  455. "(%d minutes) in the past. Not adding (%s)",
  456. router_describe(ri),
  457. (int)((now-ri->cache_info.published_on)/60),
  458. esc_router_info(ri));
  459. *msg = "Rejected: Server is expired, or your clock is too far in the past,"
  460. " or your timezone is not correct.";
  461. return -1;
  462. }
  463. if (dirserv_router_has_valid_address(ri) < 0) {
  464. log_fn(severity, LD_DIRSERV,
  465. "Router %s has invalid address. Not adding (%s).",
  466. router_describe(ri),
  467. esc_router_info(ri));
  468. *msg = "Rejected: Address is a private address.";
  469. return -1;
  470. }
  471. *valid_out = ! (status & FP_INVALID);
  472. return 0;
  473. }
  474. /** Update the relevant flags of <b>node</b> based on our opinion as a
  475. * directory authority in <b>authstatus</b>, as returned by
  476. * dirserv_router_get_status or equivalent. */
  477. void
  478. dirserv_set_node_flags_from_authoritative_status(node_t *node,
  479. uint32_t authstatus)
  480. {
  481. node->is_valid = (authstatus & FP_INVALID) ? 0 : 1;
  482. node->is_bad_exit = (authstatus & FP_BADEXIT) ? 1 : 0;
  483. }
  484. /** True iff <b>a</b> is more severe than <b>b</b>. */
  485. static int
  486. WRA_MORE_SEVERE(was_router_added_t a, was_router_added_t b)
  487. {
  488. return a < b;
  489. }
  490. /** As for dirserv_add_descriptor(), but accepts multiple documents, and
  491. * returns the most severe error that occurred for any one of them. */
  492. was_router_added_t
  493. dirserv_add_multiple_descriptors(const char *desc, uint8_t purpose,
  494. const char *source,
  495. const char **msg)
  496. {
  497. was_router_added_t r, r_tmp;
  498. const char *msg_out;
  499. smartlist_t *list;
  500. const char *s;
  501. int n_parsed = 0;
  502. time_t now = time(NULL);
  503. char annotation_buf[ROUTER_ANNOTATION_BUF_LEN];
  504. char time_buf[ISO_TIME_LEN+1];
  505. int general = purpose == ROUTER_PURPOSE_GENERAL;
  506. tor_assert(msg);
  507. r=ROUTER_ADDED_SUCCESSFULLY; /*Least severe return value. */
  508. format_iso_time(time_buf, now);
  509. if (tor_snprintf(annotation_buf, sizeof(annotation_buf),
  510. "@uploaded-at %s\n"
  511. "@source %s\n"
  512. "%s%s%s", time_buf, escaped(source),
  513. !general ? "@purpose " : "",
  514. !general ? router_purpose_to_string(purpose) : "",
  515. !general ? "\n" : "")<0) {
  516. *msg = "Couldn't format annotations";
  517. return -1;
  518. }
  519. s = desc;
  520. list = smartlist_new();
  521. if (!router_parse_list_from_string(&s, NULL, list, SAVED_NOWHERE, 0, 0,
  522. annotation_buf, NULL)) {
  523. SMARTLIST_FOREACH(list, routerinfo_t *, ri, {
  524. msg_out = NULL;
  525. tor_assert(ri->purpose == purpose);
  526. r_tmp = dirserv_add_descriptor(ri, &msg_out, source);
  527. if (WRA_MORE_SEVERE(r_tmp, r)) {
  528. r = r_tmp;
  529. *msg = msg_out;
  530. }
  531. });
  532. }
  533. n_parsed += smartlist_len(list);
  534. smartlist_clear(list);
  535. s = desc;
  536. if (!router_parse_list_from_string(&s, NULL, list, SAVED_NOWHERE, 1, 0,
  537. NULL, NULL)) {
  538. SMARTLIST_FOREACH(list, extrainfo_t *, ei, {
  539. msg_out = NULL;
  540. r_tmp = dirserv_add_extrainfo(ei, &msg_out);
  541. if (WRA_MORE_SEVERE(r_tmp, r)) {
  542. r = r_tmp;
  543. *msg = msg_out;
  544. }
  545. });
  546. }
  547. n_parsed += smartlist_len(list);
  548. smartlist_free(list);
  549. if (! *msg) {
  550. if (!n_parsed) {
  551. *msg = "No descriptors found in your POST.";
  552. if (WRA_WAS_ADDED(r))
  553. r = ROUTER_IS_ALREADY_KNOWN;
  554. } else {
  555. *msg = "(no message)";
  556. }
  557. }
  558. return r;
  559. }
  560. /** Examine the parsed server descriptor in <b>ri</b> and maybe insert it into
  561. * the list of server descriptors. Set *<b>msg</b> to a message that should be
  562. * passed back to the origin of this descriptor, or NULL if there is no such
  563. * message. Use <b>source</b> to produce better log messages.
  564. *
  565. * If <b>ri</b> is not added to the list of server descriptors, free it.
  566. * That means the caller must not access <b>ri</b> after this function
  567. * returns, since it might have been freed.
  568. *
  569. * Return the status of the operation.
  570. *
  571. * This function is only called when fresh descriptors are posted, not when
  572. * we re-load the cache.
  573. */
  574. was_router_added_t
  575. dirserv_add_descriptor(routerinfo_t *ri, const char **msg, const char *source)
  576. {
  577. was_router_added_t r;
  578. routerinfo_t *ri_old;
  579. char *desc, *nickname;
  580. const size_t desclen = ri->cache_info.signed_descriptor_len +
  581. ri->cache_info.annotations_len;
  582. const int key_pinning = get_options()->AuthDirPinKeys;
  583. *msg = NULL;
  584. /* If it's too big, refuse it now. Otherwise we'll cache it all over the
  585. * network and it'll clog everything up. */
  586. if (ri->cache_info.signed_descriptor_len > MAX_DESCRIPTOR_UPLOAD_SIZE) {
  587. log_notice(LD_DIR, "Somebody attempted to publish a router descriptor '%s'"
  588. " (source: %s) with size %d. Either this is an attack, or the "
  589. "MAX_DESCRIPTOR_UPLOAD_SIZE (%d) constant is too low.",
  590. ri->nickname, source, (int)ri->cache_info.signed_descriptor_len,
  591. MAX_DESCRIPTOR_UPLOAD_SIZE);
  592. *msg = "Router descriptor was too large.";
  593. control_event_or_authdir_new_descriptor("REJECTED",
  594. ri->cache_info.signed_descriptor_body,
  595. desclen, *msg);
  596. r = ROUTER_AUTHDIR_REJECTS;
  597. goto fail;
  598. }
  599. /* Check whether this descriptor is semantically identical to the last one
  600. * from this server. (We do this here and not in router_add_to_routerlist
  601. * because we want to be able to accept the newest router descriptor that
  602. * another authority has, so we all converge on the same one.) */
  603. ri_old = router_get_mutable_by_digest(ri->cache_info.identity_digest);
  604. if (ri_old && ri_old->cache_info.published_on < ri->cache_info.published_on
  605. && router_differences_are_cosmetic(ri_old, ri)
  606. && !router_is_me(ri)) {
  607. log_info(LD_DIRSERV,
  608. "Not replacing descriptor from %s (source: %s); "
  609. "differences are cosmetic.",
  610. router_describe(ri), source);
  611. *msg = "Not replacing router descriptor; no information has changed since "
  612. "the last one with this identity.";
  613. control_event_or_authdir_new_descriptor("DROPPED",
  614. ri->cache_info.signed_descriptor_body,
  615. desclen, *msg);
  616. r = ROUTER_IS_ALREADY_KNOWN;
  617. goto fail;
  618. }
  619. /* Do keypinning again ... this time, to add the pin if appropriate */
  620. int keypin_status;
  621. if (ri->cache_info.signing_key_cert) {
  622. keypin_status = keypin_check_and_add(
  623. (const uint8_t*)ri->cache_info.identity_digest,
  624. ri->cache_info.signing_key_cert->signing_key.pubkey,
  625. ! key_pinning);
  626. } else {
  627. keypin_status = keypin_check_lone_rsa(
  628. (const uint8_t*)ri->cache_info.identity_digest);
  629. #ifndef DISABLE_DISABLING_ED25519
  630. if (keypin_status == KEYPIN_MISMATCH)
  631. keypin_status = KEYPIN_NOT_FOUND;
  632. #endif
  633. }
  634. if (keypin_status == KEYPIN_MISMATCH && key_pinning) {
  635. log_info(LD_DIRSERV, "Dropping descriptor from %s (source: %s) because "
  636. "its key did not match an older RSA/Ed25519 keypair",
  637. router_describe(ri), source);
  638. *msg = "Looks like your keypair does not match its older value.";
  639. r = ROUTER_AUTHDIR_REJECTS;
  640. goto fail;
  641. }
  642. /* Make a copy of desc, since router_add_to_routerlist might free
  643. * ri and its associated signed_descriptor_t. */
  644. desc = tor_strndup(ri->cache_info.signed_descriptor_body, desclen);
  645. nickname = tor_strdup(ri->nickname);
  646. /* Tell if we're about to need to launch a test if we add this. */
  647. ri->needs_retest_if_added =
  648. dirserv_should_launch_reachability_test(ri, ri_old);
  649. r = router_add_to_routerlist(ri, msg, 0, 0);
  650. if (!WRA_WAS_ADDED(r)) {
  651. /* unless the routerinfo was fine, just out-of-date */
  652. if (WRA_WAS_REJECTED(r))
  653. control_event_or_authdir_new_descriptor("REJECTED", desc, desclen, *msg);
  654. log_info(LD_DIRSERV,
  655. "Did not add descriptor from '%s' (source: %s): %s.",
  656. nickname, source, *msg ? *msg : "(no message)");
  657. } else {
  658. smartlist_t *changed;
  659. control_event_or_authdir_new_descriptor("ACCEPTED", desc, desclen, *msg);
  660. changed = smartlist_new();
  661. smartlist_add(changed, ri);
  662. routerlist_descriptors_added(changed, 0);
  663. smartlist_free(changed);
  664. if (!*msg) {
  665. *msg = "Descriptor accepted";
  666. }
  667. log_info(LD_DIRSERV,
  668. "Added descriptor from '%s' (source: %s): %s.",
  669. nickname, source, *msg);
  670. }
  671. tor_free(desc);
  672. tor_free(nickname);
  673. return r;
  674. fail:
  675. {
  676. const char *desc_digest = ri->cache_info.signed_descriptor_digest;
  677. download_status_t *dls =
  678. router_get_dl_status_by_descriptor_digest(desc_digest);
  679. if (dls) {
  680. log_info(LD_GENERAL, "Marking router with descriptor %s as rejected, "
  681. "and therefore undownloadable",
  682. hex_str(desc_digest, DIGEST_LEN));
  683. download_status_mark_impossible(dls);
  684. }
  685. routerinfo_free(ri);
  686. }
  687. return r;
  688. }
  689. /** As dirserv_add_descriptor, but for an extrainfo_t <b>ei</b>. */
  690. static was_router_added_t
  691. dirserv_add_extrainfo(extrainfo_t *ei, const char **msg)
  692. {
  693. routerinfo_t *ri;
  694. int r;
  695. was_router_added_t rv;
  696. tor_assert(msg);
  697. *msg = NULL;
  698. /* Needs to be mutable so routerinfo_incompatible_with_extrainfo
  699. * can mess with some of the flags in ri->cache_info. */
  700. ri = router_get_mutable_by_digest(ei->cache_info.identity_digest);
  701. if (!ri) {
  702. *msg = "No corresponding router descriptor for extra-info descriptor";
  703. rv = ROUTER_BAD_EI;
  704. goto fail;
  705. }
  706. /* If it's too big, refuse it now. Otherwise we'll cache it all over the
  707. * network and it'll clog everything up. */
  708. if (ei->cache_info.signed_descriptor_len > MAX_EXTRAINFO_UPLOAD_SIZE) {
  709. log_notice(LD_DIR, "Somebody attempted to publish an extrainfo "
  710. "with size %d. Either this is an attack, or the "
  711. "MAX_EXTRAINFO_UPLOAD_SIZE (%d) constant is too low.",
  712. (int)ei->cache_info.signed_descriptor_len,
  713. MAX_EXTRAINFO_UPLOAD_SIZE);
  714. *msg = "Extrainfo document was too large";
  715. rv = ROUTER_BAD_EI;
  716. goto fail;
  717. }
  718. if ((r = routerinfo_incompatible_with_extrainfo(ri->identity_pkey, ei,
  719. &ri->cache_info, msg))) {
  720. if (r<0) {
  721. extrainfo_free(ei);
  722. return ROUTER_IS_ALREADY_KNOWN;
  723. }
  724. rv = ROUTER_BAD_EI;
  725. goto fail;
  726. }
  727. router_add_extrainfo_to_routerlist(ei, msg, 0, 0);
  728. return ROUTER_ADDED_SUCCESSFULLY;
  729. fail:
  730. {
  731. const char *d = ei->cache_info.signed_descriptor_digest;
  732. signed_descriptor_t *sd = router_get_by_extrainfo_digest((char*)d);
  733. if (sd) {
  734. log_info(LD_GENERAL, "Marking extrainfo with descriptor %s as "
  735. "rejected, and therefore undownloadable",
  736. hex_str((char*)d,DIGEST_LEN));
  737. download_status_mark_impossible(&sd->ei_dl_status);
  738. }
  739. extrainfo_free(ei);
  740. }
  741. return rv;
  742. }
  743. /** Remove all descriptors whose nicknames or fingerprints no longer
  744. * are allowed by our fingerprint list. (Descriptors that used to be
  745. * good can become bad when we reload the fingerprint list.)
  746. */
  747. static void
  748. directory_remove_invalid(void)
  749. {
  750. routerlist_t *rl = router_get_routerlist();
  751. smartlist_t *nodes = smartlist_new();
  752. smartlist_add_all(nodes, nodelist_get_list());
  753. SMARTLIST_FOREACH_BEGIN(nodes, node_t *, node) {
  754. const char *msg = NULL;
  755. routerinfo_t *ent = node->ri;
  756. char description[NODE_DESC_BUF_LEN];
  757. uint32_t r;
  758. if (!ent)
  759. continue;
  760. r = dirserv_router_get_status(ent, &msg, LOG_INFO);
  761. router_get_description(description, ent);
  762. if (r & FP_REJECT) {
  763. log_info(LD_DIRSERV, "Router %s is now rejected: %s",
  764. description, msg?msg:"");
  765. routerlist_remove(rl, ent, 0, time(NULL));
  766. continue;
  767. }
  768. if (bool_neq((r & FP_INVALID), !node->is_valid)) {
  769. log_info(LD_DIRSERV, "Router '%s' is now %svalid.", description,
  770. (r&FP_INVALID) ? "in" : "");
  771. node->is_valid = (r&FP_INVALID)?0:1;
  772. }
  773. if (bool_neq((r & FP_BADEXIT), node->is_bad_exit)) {
  774. log_info(LD_DIRSERV, "Router '%s' is now a %s exit", description,
  775. (r & FP_BADEXIT) ? "bad" : "good");
  776. node->is_bad_exit = (r&FP_BADEXIT) ? 1: 0;
  777. }
  778. } SMARTLIST_FOREACH_END(node);
  779. routerlist_assert_ok(rl);
  780. smartlist_free(nodes);
  781. }
  782. /**
  783. * Allocate and return a description of the status of the server <b>desc</b>,
  784. * for use in a v1-style router-status line. The server is listed
  785. * as running iff <b>is_live</b> is true.
  786. */
  787. static char *
  788. list_single_server_status(const routerinfo_t *desc, int is_live)
  789. {
  790. char buf[MAX_NICKNAME_LEN+HEX_DIGEST_LEN+4]; /* !nickname=$hexdigest\0 */
  791. char *cp;
  792. const node_t *node;
  793. tor_assert(desc);
  794. cp = buf;
  795. if (!is_live) {
  796. *cp++ = '!';
  797. }
  798. node = node_get_by_id(desc->cache_info.identity_digest);
  799. if (node && node->is_valid) {
  800. strlcpy(cp, desc->nickname, sizeof(buf)-(cp-buf));
  801. cp += strlen(cp);
  802. *cp++ = '=';
  803. }
  804. *cp++ = '$';
  805. base16_encode(cp, HEX_DIGEST_LEN+1, desc->cache_info.identity_digest,
  806. DIGEST_LEN);
  807. return tor_strdup(buf);
  808. }
  809. /* DOCDOC running_long_enough_to_decide_unreachable */
  810. static inline int
  811. running_long_enough_to_decide_unreachable(void)
  812. {
  813. return time_of_process_start
  814. + get_options()->TestingAuthDirTimeToLearnReachability < approx_time();
  815. }
  816. /** Each server needs to have passed a reachability test no more
  817. * than this number of seconds ago, or it is listed as down in
  818. * the directory. */
  819. #define REACHABLE_TIMEOUT (45*60)
  820. /** If we tested a router and found it reachable _at least this long_ after it
  821. * declared itself hibernating, it is probably done hibernating and we just
  822. * missed a descriptor from it. */
  823. #define HIBERNATION_PUBLICATION_SKEW (60*60)
  824. /** Treat a router as alive if
  825. * - It's me, and I'm not hibernating.
  826. * or - We've found it reachable recently. */
  827. void
  828. dirserv_set_router_is_running(routerinfo_t *router, time_t now)
  829. {
  830. /*XXXX This function is a mess. Separate out the part that calculates
  831. whether it's reachable and the part that tells rephist that the router was
  832. unreachable.
  833. */
  834. int answer;
  835. const or_options_t *options = get_options();
  836. node_t *node = node_get_mutable_by_id(router->cache_info.identity_digest);
  837. tor_assert(node);
  838. if (router_is_me(router)) {
  839. /* We always know if we are down ourselves. */
  840. answer = ! we_are_hibernating();
  841. } else if (router->is_hibernating &&
  842. (router->cache_info.published_on +
  843. HIBERNATION_PUBLICATION_SKEW) > node->last_reachable) {
  844. /* A hibernating router is down unless we (somehow) had contact with it
  845. * since it declared itself to be hibernating. */
  846. answer = 0;
  847. } else if (options->AssumeReachable) {
  848. /* If AssumeReachable, everybody is up unless they say they are down! */
  849. answer = 1;
  850. } else {
  851. /* Otherwise, a router counts as up if we found all announced OR
  852. ports reachable in the last REACHABLE_TIMEOUT seconds.
  853. XXX prop186 For now there's always one IPv4 and at most one
  854. IPv6 OR port.
  855. If we're not on IPv6, don't consider reachability of potential
  856. IPv6 OR port since that'd kill all dual stack relays until a
  857. majority of the dir auths have IPv6 connectivity. */
  858. answer = (now < node->last_reachable + REACHABLE_TIMEOUT &&
  859. (options->AuthDirHasIPv6Connectivity != 1 ||
  860. tor_addr_is_null(&router->ipv6_addr) ||
  861. now < node->last_reachable6 + REACHABLE_TIMEOUT));
  862. }
  863. if (!answer && running_long_enough_to_decide_unreachable()) {
  864. /* Not considered reachable. tell rephist about that.
  865. Because we launch a reachability test for each router every
  866. REACHABILITY_TEST_CYCLE_PERIOD seconds, then the router has probably
  867. been down since at least that time after we last successfully reached
  868. it.
  869. XXX ipv6
  870. */
  871. time_t when = now;
  872. if (node->last_reachable &&
  873. node->last_reachable + REACHABILITY_TEST_CYCLE_PERIOD < now)
  874. when = node->last_reachable + REACHABILITY_TEST_CYCLE_PERIOD;
  875. rep_hist_note_router_unreachable(router->cache_info.identity_digest, when);
  876. }
  877. node->is_running = answer;
  878. }
  879. /** Based on the routerinfo_ts in <b>routers</b>, allocate the
  880. * contents of a v1-style router-status line, and store it in
  881. * *<b>router_status_out</b>. Return 0 on success, -1 on failure.
  882. *
  883. * If for_controller is true, include the routers with very old descriptors.
  884. */
  885. int
  886. list_server_status_v1(smartlist_t *routers, char **router_status_out,
  887. int for_controller)
  888. {
  889. /* List of entries in a router-status style: An optional !, then an optional
  890. * equals-suffixed nickname, then a dollar-prefixed hexdigest. */
  891. smartlist_t *rs_entries;
  892. time_t now = time(NULL);
  893. time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
  894. const or_options_t *options = get_options();
  895. /* We include v2 dir auths here too, because they need to answer
  896. * controllers. Eventually we'll deprecate this whole function;
  897. * see also networkstatus_getinfo_by_purpose(). */
  898. int authdir = authdir_mode_publishes_statuses(options);
  899. tor_assert(router_status_out);
  900. rs_entries = smartlist_new();
  901. SMARTLIST_FOREACH_BEGIN(routers, routerinfo_t *, ri) {
  902. const node_t *node = node_get_by_id(ri->cache_info.identity_digest);
  903. tor_assert(node);
  904. if (authdir) {
  905. /* Update router status in routerinfo_t. */
  906. dirserv_set_router_is_running(ri, now);
  907. }
  908. if (for_controller) {
  909. char name_buf[MAX_VERBOSE_NICKNAME_LEN+2];
  910. char *cp = name_buf;
  911. if (!node->is_running)
  912. *cp++ = '!';
  913. router_get_verbose_nickname(cp, ri);
  914. smartlist_add(rs_entries, tor_strdup(name_buf));
  915. } else if (ri->cache_info.published_on >= cutoff) {
  916. smartlist_add(rs_entries, list_single_server_status(ri,
  917. node->is_running));
  918. }
  919. } SMARTLIST_FOREACH_END(ri);
  920. *router_status_out = smartlist_join_strings(rs_entries, " ", 0, NULL);
  921. SMARTLIST_FOREACH(rs_entries, char *, cp, tor_free(cp));
  922. smartlist_free(rs_entries);
  923. return 0;
  924. }
  925. /** Given a (possibly empty) list of config_line_t, each line of which contains
  926. * a list of comma-separated version numbers surrounded by optional space,
  927. * allocate and return a new string containing the version numbers, in order,
  928. * separated by commas. Used to generate Recommended(Client|Server)?Versions
  929. */
  930. static char *
  931. format_versions_list(config_line_t *ln)
  932. {
  933. smartlist_t *versions;
  934. char *result;
  935. versions = smartlist_new();
  936. for ( ; ln; ln = ln->next) {
  937. smartlist_split_string(versions, ln->value, ",",
  938. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  939. }
  940. sort_version_list(versions, 1);
  941. result = smartlist_join_strings(versions,",",0,NULL);
  942. SMARTLIST_FOREACH(versions,char *,s,tor_free(s));
  943. smartlist_free(versions);
  944. return result;
  945. }
  946. /** Return 1 if <b>ri</b>'s descriptor is "active" -- running, valid,
  947. * not hibernating, having observed bw greater 0, and not too old. Else
  948. * return 0.
  949. */
  950. static int
  951. router_is_active(const routerinfo_t *ri, const node_t *node, time_t now)
  952. {
  953. time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
  954. if (ri->cache_info.published_on < cutoff) {
  955. return 0;
  956. }
  957. if (!node->is_running || !node->is_valid || ri->is_hibernating) {
  958. return 0;
  959. }
  960. /* Only require bandwith capacity in non-test networks, or
  961. * if TestingTorNetwork, and TestingMinExitFlagThreshold is non-zero */
  962. if (!ri->bandwidthcapacity) {
  963. if (get_options()->TestingTorNetwork) {
  964. if (get_options()->TestingMinExitFlagThreshold > 0) {
  965. /* If we're in a TestingTorNetwork, and TestingMinExitFlagThreshold is,
  966. * then require bandwidthcapacity */
  967. return 0;
  968. }
  969. } else {
  970. /* If we're not in a TestingTorNetwork, then require bandwidthcapacity */
  971. return 0;
  972. }
  973. }
  974. return 1;
  975. }
  976. /********************************************************************/
  977. /* A set of functions to answer questions about how we'd like to behave
  978. * as a directory mirror/client. */
  979. /** Return 1 if we fetch our directory material directly from the
  980. * authorities, rather than from a mirror. */
  981. int
  982. directory_fetches_from_authorities(const or_options_t *options)
  983. {
  984. const routerinfo_t *me;
  985. uint32_t addr;
  986. int refuseunknown;
  987. if (options->FetchDirInfoEarly)
  988. return 1;
  989. if (options->BridgeRelay == 1)
  990. return 0;
  991. if (server_mode(options) &&
  992. router_pick_published_address(options, &addr, 1) < 0)
  993. return 1; /* we don't know our IP address; ask an authority. */
  994. refuseunknown = ! router_my_exit_policy_is_reject_star() &&
  995. should_refuse_unknown_exits(options);
  996. if (!dir_server_mode(options) && !refuseunknown)
  997. return 0;
  998. if (!server_mode(options) || !advertised_server_mode())
  999. return 0;
  1000. me = router_get_my_routerinfo();
  1001. if (!me || (!me->supports_tunnelled_dir_requests && !refuseunknown))
  1002. return 0; /* if we don't service directory requests, return 0 too */
  1003. return 1;
  1004. }
  1005. /** Return 1 if we should fetch new networkstatuses, descriptors, etc
  1006. * on the "mirror" schedule rather than the "client" schedule.
  1007. */
  1008. int
  1009. directory_fetches_dir_info_early(const or_options_t *options)
  1010. {
  1011. return directory_fetches_from_authorities(options);
  1012. }
  1013. /** Return 1 if we should fetch new networkstatuses, descriptors, etc
  1014. * on a very passive schedule -- waiting long enough for ordinary clients
  1015. * to probably have the info we want. These would include bridge users,
  1016. * and maybe others in the future e.g. if a Tor client uses another Tor
  1017. * client as a directory guard.
  1018. */
  1019. int
  1020. directory_fetches_dir_info_later(const or_options_t *options)
  1021. {
  1022. return options->UseBridges != 0;
  1023. }
  1024. /** Return true iff we want to fetch and keep certificates for authorities
  1025. * that we don't acknowledge as authorities ourself.
  1026. */
  1027. int
  1028. directory_caches_unknown_auth_certs(const or_options_t *options)
  1029. {
  1030. return dir_server_mode(options) || options->BridgeRelay;
  1031. }
  1032. /** Return 1 if we want to keep descriptors, networkstatuses, etc around.
  1033. * Else return 0.
  1034. * Check options->DirPort_set and directory_permits_begindir_requests()
  1035. * to see if we are willing to serve these directory documents to others via
  1036. * the DirPort and begindir-over-ORPort, respectively.
  1037. */
  1038. int
  1039. directory_caches_dir_info(const or_options_t *options)
  1040. {
  1041. if (options->BridgeRelay || dir_server_mode(options))
  1042. return 1;
  1043. if (!server_mode(options) || !advertised_server_mode())
  1044. return 0;
  1045. /* We need an up-to-date view of network info if we're going to try to
  1046. * block exit attempts from unknown relays. */
  1047. return ! router_my_exit_policy_is_reject_star() &&
  1048. should_refuse_unknown_exits(options);
  1049. }
  1050. /** Return 1 if we want to allow remote people to ask us directory
  1051. * requests via the "begin_dir" interface, which doesn't require
  1052. * having any separate port open. */
  1053. int
  1054. directory_permits_begindir_requests(const or_options_t *options)
  1055. {
  1056. return options->BridgeRelay != 0 || dir_server_mode(options);
  1057. }
  1058. /** Return 1 if we have no need to fetch new descriptors. This generally
  1059. * happens when we're not a dir cache and we haven't built any circuits
  1060. * lately.
  1061. */
  1062. int
  1063. directory_too_idle_to_fetch_descriptors(const or_options_t *options,
  1064. time_t now)
  1065. {
  1066. return !directory_caches_dir_info(options) &&
  1067. !options->FetchUselessDescriptors &&
  1068. rep_hist_circbuilding_dormant(now);
  1069. }
  1070. /********************************************************************/
  1071. /** Map from flavor name to the cached_dir_t for the v3 consensuses that we're
  1072. * currently serving. */
  1073. static strmap_t *cached_consensuses = NULL;
  1074. /** Decrement the reference count on <b>d</b>, and free it if it no longer has
  1075. * any references. */
  1076. void
  1077. cached_dir_decref(cached_dir_t *d)
  1078. {
  1079. if (!d || --d->refcnt > 0)
  1080. return;
  1081. clear_cached_dir(d);
  1082. tor_free(d);
  1083. }
  1084. /** Allocate and return a new cached_dir_t containing the string <b>s</b>,
  1085. * published at <b>published</b>. */
  1086. cached_dir_t *
  1087. new_cached_dir(char *s, time_t published)
  1088. {
  1089. cached_dir_t *d = tor_malloc_zero(sizeof(cached_dir_t));
  1090. d->refcnt = 1;
  1091. d->dir = s;
  1092. d->dir_len = strlen(s);
  1093. d->published = published;
  1094. if (tor_gzip_compress(&(d->dir_z), &(d->dir_z_len), d->dir, d->dir_len,
  1095. ZLIB_METHOD)) {
  1096. log_warn(LD_BUG, "Error compressing directory");
  1097. }
  1098. return d;
  1099. }
  1100. /** Remove all storage held in <b>d</b>, but do not free <b>d</b> itself. */
  1101. static void
  1102. clear_cached_dir(cached_dir_t *d)
  1103. {
  1104. tor_free(d->dir);
  1105. tor_free(d->dir_z);
  1106. memset(d, 0, sizeof(cached_dir_t));
  1107. }
  1108. /** Free all storage held by the cached_dir_t in <b>d</b>. */
  1109. static void
  1110. free_cached_dir_(void *_d)
  1111. {
  1112. cached_dir_t *d;
  1113. if (!_d)
  1114. return;
  1115. d = (cached_dir_t *)_d;
  1116. cached_dir_decref(d);
  1117. }
  1118. /** Replace the v3 consensus networkstatus of type <b>flavor_name</b> that
  1119. * we're serving with <b>networkstatus</b>, published at <b>published</b>. No
  1120. * validation is performed. */
  1121. void
  1122. dirserv_set_cached_consensus_networkstatus(const char *networkstatus,
  1123. const char *flavor_name,
  1124. const common_digests_t *digests,
  1125. time_t published)
  1126. {
  1127. cached_dir_t *new_networkstatus;
  1128. cached_dir_t *old_networkstatus;
  1129. if (!cached_consensuses)
  1130. cached_consensuses = strmap_new();
  1131. new_networkstatus = new_cached_dir(tor_strdup(networkstatus), published);
  1132. memcpy(&new_networkstatus->digests, digests, sizeof(common_digests_t));
  1133. old_networkstatus = strmap_set(cached_consensuses, flavor_name,
  1134. new_networkstatus);
  1135. if (old_networkstatus)
  1136. cached_dir_decref(old_networkstatus);
  1137. }
  1138. /** Return the latest downloaded consensus networkstatus in encoded, signed,
  1139. * optionally compressed format, suitable for sending to clients. */
  1140. cached_dir_t *
  1141. dirserv_get_consensus(const char *flavor_name)
  1142. {
  1143. if (!cached_consensuses)
  1144. return NULL;
  1145. return strmap_get(cached_consensuses, flavor_name);
  1146. }
  1147. /** If a router's uptime is at least this value, then it is always
  1148. * considered stable, regardless of the rest of the network. This
  1149. * way we resist attacks where an attacker doubles the size of the
  1150. * network using allegedly high-uptime nodes, displacing all the
  1151. * current guards. */
  1152. #define UPTIME_TO_GUARANTEE_STABLE (3600*24*30)
  1153. /** If a router's MTBF is at least this value, then it is always stable.
  1154. * See above. (Corresponds to about 7 days for current decay rates.) */
  1155. #define MTBF_TO_GUARANTEE_STABLE (60*60*24*5)
  1156. /** Similarly, every node with at least this much weighted time known can be
  1157. * considered familiar enough to be a guard. Corresponds to about 20 days for
  1158. * current decay rates.
  1159. */
  1160. #define TIME_KNOWN_TO_GUARANTEE_FAMILIAR (8*24*60*60)
  1161. /** Similarly, every node with sufficient WFU is around enough to be a guard.
  1162. */
  1163. #define WFU_TO_GUARANTEE_GUARD (0.98)
  1164. /* Thresholds for server performance: set by
  1165. * dirserv_compute_performance_thresholds, and used by
  1166. * generate_v2_networkstatus */
  1167. /** Any router with an uptime of at least this value is stable. */
  1168. static uint32_t stable_uptime = 0; /* start at a safe value */
  1169. /** Any router with an mtbf of at least this value is stable. */
  1170. static double stable_mtbf = 0.0;
  1171. /** If true, we have measured enough mtbf info to look at stable_mtbf rather
  1172. * than stable_uptime. */
  1173. static int enough_mtbf_info = 0;
  1174. /** Any router with a weighted fractional uptime of at least this much might
  1175. * be good as a guard. */
  1176. static double guard_wfu = 0.0;
  1177. /** Don't call a router a guard unless we've known about it for at least this
  1178. * many seconds. */
  1179. static long guard_tk = 0;
  1180. /** Any router with a bandwidth at least this high is "Fast" */
  1181. static uint32_t fast_bandwidth_kb = 0;
  1182. /** If exits can be guards, then all guards must have a bandwidth this
  1183. * high. */
  1184. static uint32_t guard_bandwidth_including_exits_kb = 0;
  1185. /** If exits can't be guards, then all guards must have a bandwidth this
  1186. * high. */
  1187. static uint32_t guard_bandwidth_excluding_exits_kb = 0;
  1188. /** Helper: estimate the uptime of a router given its stated uptime and the
  1189. * amount of time since it last stated its stated uptime. */
  1190. static inline long
  1191. real_uptime(const routerinfo_t *router, time_t now)
  1192. {
  1193. if (now < router->cache_info.published_on)
  1194. return router->uptime;
  1195. else
  1196. return router->uptime + (now - router->cache_info.published_on);
  1197. }
  1198. /** Return 1 if <b>router</b> is not suitable for these parameters, else 0.
  1199. * If <b>need_uptime</b> is non-zero, we require a minimum uptime.
  1200. * If <b>need_capacity</b> is non-zero, we require a minimum advertised
  1201. * bandwidth.
  1202. */
  1203. static int
  1204. dirserv_thinks_router_is_unreliable(time_t now,
  1205. routerinfo_t *router,
  1206. int need_uptime, int need_capacity)
  1207. {
  1208. if (need_uptime) {
  1209. if (!enough_mtbf_info) {
  1210. /* XXXX We should change the rule from
  1211. * "use uptime if we don't have mtbf data" to "don't advertise Stable on
  1212. * v3 if we don't have enough mtbf data." Or maybe not, since if we ever
  1213. * hit a point where we need to reset a lot of authorities at once,
  1214. * none of them would be in a position to declare Stable.
  1215. */
  1216. long uptime = real_uptime(router, now);
  1217. if ((unsigned)uptime < stable_uptime &&
  1218. (unsigned)uptime < UPTIME_TO_GUARANTEE_STABLE)
  1219. return 1;
  1220. } else {
  1221. double mtbf =
  1222. rep_hist_get_stability(router->cache_info.identity_digest, now);
  1223. if (mtbf < stable_mtbf &&
  1224. mtbf < MTBF_TO_GUARANTEE_STABLE)
  1225. return 1;
  1226. }
  1227. }
  1228. if (need_capacity) {
  1229. uint32_t bw_kb = dirserv_get_credible_bandwidth_kb(router);
  1230. if (bw_kb < fast_bandwidth_kb)
  1231. return 1;
  1232. }
  1233. return 0;
  1234. }
  1235. /** Return true iff <b>router</b> should be assigned the "HSDir" flag.
  1236. *
  1237. * Right now this means it advertises support for it, it has a high uptime,
  1238. * it's a directory cache, it has the Stable and Fast flags, and it's currently
  1239. * considered Running.
  1240. *
  1241. * This function needs to be called after router-\>is_running has
  1242. * been set.
  1243. */
  1244. static int
  1245. dirserv_thinks_router_is_hs_dir(const routerinfo_t *router,
  1246. const node_t *node, time_t now)
  1247. {
  1248. long uptime;
  1249. /* If we haven't been running for at least
  1250. * get_options()->MinUptimeHidServDirectoryV2 seconds, we can't
  1251. * have accurate data telling us a relay has been up for at least
  1252. * that long. We also want to allow a bit of slack: Reachability
  1253. * tests aren't instant. If we haven't been running long enough,
  1254. * trust the relay. */
  1255. if (stats_n_seconds_working >
  1256. get_options()->MinUptimeHidServDirectoryV2 * 1.1)
  1257. uptime = MIN(rep_hist_get_uptime(router->cache_info.identity_digest, now),
  1258. real_uptime(router, now));
  1259. else
  1260. uptime = real_uptime(router, now);
  1261. return (router->wants_to_be_hs_dir &&
  1262. router->supports_tunnelled_dir_requests &&
  1263. node->is_stable && node->is_fast &&
  1264. uptime >= get_options()->MinUptimeHidServDirectoryV2 &&
  1265. router_is_active(router, node, now));
  1266. }
  1267. /** Don't consider routers with less bandwidth than this when computing
  1268. * thresholds. */
  1269. #define ABSOLUTE_MIN_BW_VALUE_TO_CONSIDER_KB 4
  1270. /** Helper for dirserv_compute_performance_thresholds(): Decide whether to
  1271. * include a router in our calculations, and return true iff we should; the
  1272. * require_mbw parameter is passed in by
  1273. * dirserv_compute_performance_thresholds() and controls whether we ever
  1274. * count routers with only advertised bandwidths */
  1275. static int
  1276. router_counts_toward_thresholds(const node_t *node, time_t now,
  1277. const digestmap_t *omit_as_sybil,
  1278. int require_mbw)
  1279. {
  1280. /* Have measured bw? */
  1281. int have_mbw =
  1282. dirserv_has_measured_bw(node->identity);
  1283. uint64_t min_bw_kb = ABSOLUTE_MIN_BW_VALUE_TO_CONSIDER_KB;
  1284. const or_options_t *options = get_options();
  1285. if (options->TestingTorNetwork) {
  1286. min_bw_kb = (int64_t)options->TestingMinExitFlagThreshold / 1000;
  1287. }
  1288. return node->ri && router_is_active(node->ri, node, now) &&
  1289. !digestmap_get(omit_as_sybil, node->identity) &&
  1290. (dirserv_get_credible_bandwidth_kb(node->ri) >= min_bw_kb) &&
  1291. (have_mbw || !require_mbw);
  1292. }
  1293. /** Look through the routerlist, the Mean Time Between Failure history, and
  1294. * the Weighted Fractional Uptime history, and use them to set thresholds for
  1295. * the Stable, Fast, and Guard flags. Update the fields stable_uptime,
  1296. * stable_mtbf, enough_mtbf_info, guard_wfu, guard_tk, fast_bandwidth,
  1297. * guard_bandwidth_including_exits, and guard_bandwidth_excluding_exits.
  1298. *
  1299. * Also, set the is_exit flag of each router appropriately. */
  1300. static void
  1301. dirserv_compute_performance_thresholds(digestmap_t *omit_as_sybil)
  1302. {
  1303. int n_active, n_active_nonexit, n_familiar;
  1304. uint32_t *uptimes, *bandwidths_kb, *bandwidths_excluding_exits_kb;
  1305. long *tks;
  1306. double *mtbfs, *wfus;
  1307. smartlist_t *nodelist;
  1308. time_t now = time(NULL);
  1309. const or_options_t *options = get_options();
  1310. /* Require mbw? */
  1311. int require_mbw =
  1312. (routers_with_measured_bw >
  1313. options->MinMeasuredBWsForAuthToIgnoreAdvertised) ? 1 : 0;
  1314. /* initialize these all here, in case there are no routers */
  1315. stable_uptime = 0;
  1316. stable_mtbf = 0;
  1317. fast_bandwidth_kb = 0;
  1318. guard_bandwidth_including_exits_kb = 0;
  1319. guard_bandwidth_excluding_exits_kb = 0;
  1320. guard_tk = 0;
  1321. guard_wfu = 0;
  1322. nodelist_assert_ok();
  1323. nodelist = nodelist_get_list();
  1324. /* Initialize arrays that will hold values for each router. We'll
  1325. * sort them and use that to compute thresholds. */
  1326. n_active = n_active_nonexit = 0;
  1327. /* Uptime for every active router. */
  1328. uptimes = tor_calloc(smartlist_len(nodelist), sizeof(uint32_t));
  1329. /* Bandwidth for every active router. */
  1330. bandwidths_kb = tor_calloc(smartlist_len(nodelist), sizeof(uint32_t));
  1331. /* Bandwidth for every active non-exit router. */
  1332. bandwidths_excluding_exits_kb =
  1333. tor_calloc(smartlist_len(nodelist), sizeof(uint32_t));
  1334. /* Weighted mean time between failure for each active router. */
  1335. mtbfs = tor_calloc(smartlist_len(nodelist), sizeof(double));
  1336. /* Time-known for each active router. */
  1337. tks = tor_calloc(smartlist_len(nodelist), sizeof(long));
  1338. /* Weighted fractional uptime for each active router. */
  1339. wfus = tor_calloc(smartlist_len(nodelist), sizeof(double));
  1340. /* Now, fill in the arrays. */
  1341. SMARTLIST_FOREACH_BEGIN(nodelist, node_t *, node) {
  1342. if (options->BridgeAuthoritativeDir &&
  1343. node->ri &&
  1344. node->ri->purpose != ROUTER_PURPOSE_BRIDGE)
  1345. continue;
  1346. if (router_counts_toward_thresholds(node, now, omit_as_sybil,
  1347. require_mbw)) {
  1348. routerinfo_t *ri = node->ri;
  1349. const char *id = node->identity;
  1350. uint32_t bw_kb;
  1351. /* resolve spurious clang shallow analysis null pointer errors */
  1352. tor_assert(ri);
  1353. node->is_exit = (!router_exit_policy_rejects_all(ri) &&
  1354. exit_policy_is_general_exit(ri->exit_policy));
  1355. uptimes[n_active] = (uint32_t)real_uptime(ri, now);
  1356. mtbfs[n_active] = rep_hist_get_stability(id, now);
  1357. tks [n_active] = rep_hist_get_weighted_time_known(id, now);
  1358. bandwidths_kb[n_active] = bw_kb = dirserv_get_credible_bandwidth_kb(ri);
  1359. if (!node->is_exit || node->is_bad_exit) {
  1360. bandwidths_excluding_exits_kb[n_active_nonexit] = bw_kb;
  1361. ++n_active_nonexit;
  1362. }
  1363. ++n_active;
  1364. }
  1365. } SMARTLIST_FOREACH_END(node);
  1366. /* Now, compute thresholds. */
  1367. if (n_active) {
  1368. /* The median uptime is stable. */
  1369. stable_uptime = median_uint32(uptimes, n_active);
  1370. /* The median mtbf is stable, if we have enough mtbf info */
  1371. stable_mtbf = median_double(mtbfs, n_active);
  1372. /* The 12.5th percentile bandwidth is fast. */
  1373. fast_bandwidth_kb = find_nth_uint32(bandwidths_kb, n_active, n_active/8);
  1374. /* (Now bandwidths is sorted.) */
  1375. if (fast_bandwidth_kb < RELAY_REQUIRED_MIN_BANDWIDTH/(2 * 1000))
  1376. fast_bandwidth_kb = bandwidths_kb[n_active/4];
  1377. guard_bandwidth_including_exits_kb =
  1378. third_quartile_uint32(bandwidths_kb, n_active);
  1379. guard_tk = find_nth_long(tks, n_active, n_active/8);
  1380. }
  1381. if (guard_tk > TIME_KNOWN_TO_GUARANTEE_FAMILIAR)
  1382. guard_tk = TIME_KNOWN_TO_GUARANTEE_FAMILIAR;
  1383. {
  1384. /* We can vote on a parameter for the minimum and maximum. */
  1385. #define ABSOLUTE_MIN_VALUE_FOR_FAST_FLAG 4
  1386. int32_t min_fast_kb, max_fast_kb, min_fast, max_fast;
  1387. min_fast = networkstatus_get_param(NULL, "FastFlagMinThreshold",
  1388. ABSOLUTE_MIN_VALUE_FOR_FAST_FLAG,
  1389. ABSOLUTE_MIN_VALUE_FOR_FAST_FLAG,
  1390. INT32_MAX);
  1391. if (options->TestingTorNetwork) {
  1392. min_fast = (int32_t)options->TestingMinFastFlagThreshold;
  1393. }
  1394. max_fast = networkstatus_get_param(NULL, "FastFlagMaxThreshold",
  1395. INT32_MAX, min_fast, INT32_MAX);
  1396. min_fast_kb = min_fast / 1000;
  1397. max_fast_kb = max_fast / 1000;
  1398. if (fast_bandwidth_kb < (uint32_t)min_fast_kb)
  1399. fast_bandwidth_kb = min_fast_kb;
  1400. if (fast_bandwidth_kb > (uint32_t)max_fast_kb)
  1401. fast_bandwidth_kb = max_fast_kb;
  1402. }
  1403. /* Protect sufficiently fast nodes from being pushed out of the set
  1404. * of Fast nodes. */
  1405. if (options->AuthDirFastGuarantee &&
  1406. fast_bandwidth_kb > options->AuthDirFastGuarantee/1000)
  1407. fast_bandwidth_kb = (uint32_t)options->AuthDirFastGuarantee/1000;
  1408. /* Now that we have a time-known that 7/8 routers are known longer than,
  1409. * fill wfus with the wfu of every such "familiar" router. */
  1410. n_familiar = 0;
  1411. SMARTLIST_FOREACH_BEGIN(nodelist, node_t *, node) {
  1412. if (router_counts_toward_thresholds(node, now,
  1413. omit_as_sybil, require_mbw)) {
  1414. routerinfo_t *ri = node->ri;
  1415. const char *id = ri->cache_info.identity_digest;
  1416. long tk = rep_hist_get_weighted_time_known(id, now);
  1417. if (tk < guard_tk)
  1418. continue;
  1419. wfus[n_familiar++] = rep_hist_get_weighted_fractional_uptime(id, now);
  1420. }
  1421. } SMARTLIST_FOREACH_END(node);
  1422. if (n_familiar)
  1423. guard_wfu = median_double(wfus, n_familiar);
  1424. if (guard_wfu > WFU_TO_GUARANTEE_GUARD)
  1425. guard_wfu = WFU_TO_GUARANTEE_GUARD;
  1426. enough_mtbf_info = rep_hist_have_measured_enough_stability();
  1427. if (n_active_nonexit) {
  1428. guard_bandwidth_excluding_exits_kb =
  1429. find_nth_uint32(bandwidths_excluding_exits_kb,
  1430. n_active_nonexit, n_active_nonexit*3/4);
  1431. }
  1432. log_info(LD_DIRSERV,
  1433. "Cutoffs: For Stable, %lu sec uptime, %lu sec MTBF. "
  1434. "For Fast: %lu kilobytes/sec. "
  1435. "For Guard: WFU %.03f%%, time-known %lu sec, "
  1436. "and bandwidth %lu or %lu kilobytes/sec. "
  1437. "We%s have enough stability data.",
  1438. (unsigned long)stable_uptime,
  1439. (unsigned long)stable_mtbf,
  1440. (unsigned long)fast_bandwidth_kb,
  1441. guard_wfu*100,
  1442. (unsigned long)guard_tk,
  1443. (unsigned long)guard_bandwidth_including_exits_kb,
  1444. (unsigned long)guard_bandwidth_excluding_exits_kb,
  1445. enough_mtbf_info ? "" : " don't");
  1446. tor_free(uptimes);
  1447. tor_free(mtbfs);
  1448. tor_free(bandwidths_kb);
  1449. tor_free(bandwidths_excluding_exits_kb);
  1450. tor_free(tks);
  1451. tor_free(wfus);
  1452. }
  1453. /* Use dirserv_compute_performance_thresholds() to compute the thresholds
  1454. * for the status flags, specifically for bridges.
  1455. *
  1456. * This is only called by a Bridge Authority from
  1457. * networkstatus_getinfo_by_purpose().
  1458. */
  1459. void
  1460. dirserv_compute_bridge_flag_thresholds(void)
  1461. {
  1462. digestmap_t *omit_as_sybil = digestmap_new();
  1463. dirserv_compute_performance_thresholds(omit_as_sybil);
  1464. digestmap_free(omit_as_sybil, NULL);
  1465. }
  1466. /** Measured bandwidth cache entry */
  1467. typedef struct mbw_cache_entry_s {
  1468. long mbw_kb;
  1469. time_t as_of;
  1470. } mbw_cache_entry_t;
  1471. /** Measured bandwidth cache - keys are identity_digests, values are
  1472. * mbw_cache_entry_t *. */
  1473. static digestmap_t *mbw_cache = NULL;
  1474. /** Store a measured bandwidth cache entry when reading the measured
  1475. * bandwidths file. */
  1476. STATIC void
  1477. dirserv_cache_measured_bw(const measured_bw_line_t *parsed_line,
  1478. time_t as_of)
  1479. {
  1480. mbw_cache_entry_t *e = NULL;
  1481. tor_assert(parsed_line);
  1482. /* Allocate a cache if we need */
  1483. if (!mbw_cache) mbw_cache = digestmap_new();
  1484. /* Check if we have an existing entry */
  1485. e = digestmap_get(mbw_cache, parsed_line->node_id);
  1486. /* If we do, we can re-use it */
  1487. if (e) {
  1488. /* Check that we really are newer, and update */
  1489. if (as_of > e->as_of) {
  1490. e->mbw_kb = parsed_line->bw_kb;
  1491. e->as_of = as_of;
  1492. }
  1493. } else {
  1494. /* We'll have to insert a new entry */
  1495. e = tor_malloc(sizeof(*e));
  1496. e->mbw_kb = parsed_line->bw_kb;
  1497. e->as_of = as_of;
  1498. digestmap_set(mbw_cache, parsed_line->node_id, e);
  1499. }
  1500. }
  1501. /** Clear and free the measured bandwidth cache */
  1502. STATIC void
  1503. dirserv_clear_measured_bw_cache(void)
  1504. {
  1505. if (mbw_cache) {
  1506. /* Free the map and all entries */
  1507. digestmap_free(mbw_cache, tor_free_);
  1508. mbw_cache = NULL;
  1509. }
  1510. }
  1511. /** Scan the measured bandwidth cache and remove expired entries */
  1512. STATIC void
  1513. dirserv_expire_measured_bw_cache(time_t now)
  1514. {
  1515. if (mbw_cache) {
  1516. /* Iterate through the cache and check each entry */
  1517. DIGESTMAP_FOREACH_MODIFY(mbw_cache, k, mbw_cache_entry_t *, e) {
  1518. if (now > e->as_of + MAX_MEASUREMENT_AGE) {
  1519. tor_free(e);
  1520. MAP_DEL_CURRENT(k);
  1521. }
  1522. } DIGESTMAP_FOREACH_END;
  1523. /* Check if we cleared the whole thing and free if so */
  1524. if (digestmap_size(mbw_cache) == 0) {
  1525. digestmap_free(mbw_cache, tor_free_);
  1526. mbw_cache = 0;
  1527. }
  1528. }
  1529. }
  1530. /** Get the current size of the measured bandwidth cache */
  1531. STATIC int
  1532. dirserv_get_measured_bw_cache_size(void)
  1533. {
  1534. if (mbw_cache) return digestmap_size(mbw_cache);
  1535. else return 0;
  1536. }
  1537. /** Query the cache by identity digest, return value indicates whether
  1538. * we found it. The bw_out and as_of_out pointers receive the cached
  1539. * bandwidth value and the time it was cached if not NULL. */
  1540. STATIC int
  1541. dirserv_query_measured_bw_cache_kb(const char *node_id, long *bw_kb_out,
  1542. time_t *as_of_out)
  1543. {
  1544. mbw_cache_entry_t *v = NULL;
  1545. int rv = 0;
  1546. if (mbw_cache && node_id) {
  1547. v = digestmap_get(mbw_cache, node_id);
  1548. if (v) {
  1549. /* Found something */
  1550. rv = 1;
  1551. if (bw_kb_out) *bw_kb_out = v->mbw_kb;
  1552. if (as_of_out) *as_of_out = v->as_of;
  1553. }
  1554. }
  1555. return rv;
  1556. }
  1557. /** Predicate wrapper for dirserv_query_measured_bw_cache() */
  1558. STATIC int
  1559. dirserv_has_measured_bw(const char *node_id)
  1560. {
  1561. return dirserv_query_measured_bw_cache_kb(node_id, NULL, NULL);
  1562. }
  1563. /** Get the best estimate of a router's bandwidth for dirauth purposes,
  1564. * preferring measured to advertised values if available. */
  1565. static uint32_t
  1566. dirserv_get_bandwidth_for_router_kb(const routerinfo_t *ri)
  1567. {
  1568. uint32_t bw_kb = 0;
  1569. /*
  1570. * Yeah, measured bandwidths in measured_bw_line_t are (implicitly
  1571. * signed) longs and the ones router_get_advertised_bandwidth() returns
  1572. * are uint32_t.
  1573. */
  1574. long mbw_kb = 0;
  1575. if (ri) {
  1576. /*
  1577. * * First try to see if we have a measured bandwidth; don't bother with
  1578. * as_of_out here, on the theory that a stale measured bandwidth is still
  1579. * better to trust than an advertised one.
  1580. */
  1581. if (dirserv_query_measured_bw_cache_kb(ri->cache_info.identity_digest,
  1582. &mbw_kb, NULL)) {
  1583. /* Got one! */
  1584. bw_kb = (uint32_t)mbw_kb;
  1585. } else {
  1586. /* If not, fall back to advertised */
  1587. bw_kb = router_get_advertised_bandwidth(ri) / 1000;
  1588. }
  1589. }
  1590. return bw_kb;
  1591. }
  1592. /** Look through the routerlist, and using the measured bandwidth cache count
  1593. * how many measured bandwidths we know. This is used to decide whether we
  1594. * ever trust advertised bandwidths for purposes of assigning flags. */
  1595. static void
  1596. dirserv_count_measured_bws(const smartlist_t *routers)
  1597. {
  1598. /* Initialize this first */
  1599. routers_with_measured_bw = 0;
  1600. /* Iterate over the routerlist and count measured bandwidths */
  1601. SMARTLIST_FOREACH_BEGIN(routers, const routerinfo_t *, ri) {
  1602. /* Check if we know a measured bandwidth for this one */
  1603. if (dirserv_has_measured_bw(ri->cache_info.identity_digest)) {
  1604. ++routers_with_measured_bw;
  1605. }
  1606. } SMARTLIST_FOREACH_END(ri);
  1607. }
  1608. /** Return the bandwidth we believe for assigning flags; prefer measured
  1609. * over advertised, and if we have above a threshold quantity of measured
  1610. * bandwidths, we don't want to ever give flags to unmeasured routers, so
  1611. * return 0. */
  1612. static uint32_t
  1613. dirserv_get_credible_bandwidth_kb(const routerinfo_t *ri)
  1614. {
  1615. int threshold;
  1616. uint32_t bw_kb = 0;
  1617. long mbw_kb;
  1618. tor_assert(ri);
  1619. /* Check if we have a measured bandwidth, and check the threshold if not */
  1620. if (!(dirserv_query_measured_bw_cache_kb(ri->cache_info.identity_digest,
  1621. &mbw_kb, NULL))) {
  1622. threshold = get_options()->MinMeasuredBWsForAuthToIgnoreAdvertised;
  1623. if (routers_with_measured_bw > threshold) {
  1624. /* Return zero for unmeasured bandwidth if we are above threshold */
  1625. bw_kb = 0;
  1626. } else {
  1627. /* Return an advertised bandwidth otherwise */
  1628. bw_kb = router_get_advertised_bandwidth_capped(ri) / 1000;
  1629. }
  1630. } else {
  1631. /* We have the measured bandwidth in mbw */
  1632. bw_kb = (uint32_t)mbw_kb;
  1633. }
  1634. return bw_kb;
  1635. }
  1636. /** Give a statement of our current performance thresholds for inclusion
  1637. * in a vote document. */
  1638. char *
  1639. dirserv_get_flag_thresholds_line(void)
  1640. {
  1641. char *result=NULL;
  1642. const int measured_threshold =
  1643. get_options()->MinMeasuredBWsForAuthToIgnoreAdvertised;
  1644. const int enough_measured_bw = routers_with_measured_bw > measured_threshold;
  1645. tor_asprintf(&result,
  1646. "stable-uptime=%lu stable-mtbf=%lu "
  1647. "fast-speed=%lu "
  1648. "guard-wfu=%.03f%% guard-tk=%lu "
  1649. "guard-bw-inc-exits=%lu guard-bw-exc-exits=%lu "
  1650. "enough-mtbf=%d ignoring-advertised-bws=%d",
  1651. (unsigned long)stable_uptime,
  1652. (unsigned long)stable_mtbf,
  1653. (unsigned long)fast_bandwidth_kb*1000,
  1654. guard_wfu*100,
  1655. (unsigned long)guard_tk,
  1656. (unsigned long)guard_bandwidth_including_exits_kb*1000,
  1657. (unsigned long)guard_bandwidth_excluding_exits_kb*1000,
  1658. enough_mtbf_info ? 1 : 0,
  1659. enough_measured_bw ? 1 : 0);
  1660. return result;
  1661. }
  1662. /** Given a platform string as in a routerinfo_t (possibly null), return a
  1663. * newly allocated version string for a networkstatus document, or NULL if the
  1664. * platform doesn't give a Tor version. */
  1665. static char *
  1666. version_from_platform(const char *platform)
  1667. {
  1668. if (platform && !strcmpstart(platform, "Tor ")) {
  1669. const char *eos = find_whitespace(platform+4);
  1670. if (eos && !strcmpstart(eos, " (r")) {
  1671. /* XXXX Unify this logic with the other version extraction
  1672. * logic in routerparse.c. */
  1673. eos = find_whitespace(eos+1);
  1674. }
  1675. if (eos) {
  1676. return tor_strndup(platform, eos-platform);
  1677. }
  1678. }
  1679. return NULL;
  1680. }
  1681. /** Helper: write the router-status information in <b>rs</b> into a newly
  1682. * allocated character buffer. Use the same format as in network-status
  1683. * documents. If <b>version</b> is non-NULL, add a "v" line for the platform.
  1684. * Return 0 on success, -1 on failure.
  1685. *
  1686. * The format argument has one of the following values:
  1687. * NS_V2 - Output an entry suitable for a V2 NS opinion document
  1688. * NS_V3_CONSENSUS - Output the first portion of a V3 NS consensus entry
  1689. * NS_V3_CONSENSUS_MICRODESC - Output the first portion of a V3 microdesc
  1690. * consensus entry.
  1691. * NS_V3_VOTE - Output a complete V3 NS vote. If <b>vrs</b> is present,
  1692. * it contains additional information for the vote.
  1693. * NS_CONTROL_PORT - Output a NS document for the control port
  1694. */
  1695. char *
  1696. routerstatus_format_entry(const routerstatus_t *rs, const char *version,
  1697. const char *protocols,
  1698. routerstatus_format_type_t format,
  1699. const vote_routerstatus_t *vrs)
  1700. {
  1701. char *summary;
  1702. char *result = NULL;
  1703. char published[ISO_TIME_LEN+1];
  1704. char identity64[BASE64_DIGEST_LEN+1];
  1705. char digest64[BASE64_DIGEST_LEN+1];
  1706. smartlist_t *chunks = smartlist_new();
  1707. format_iso_time(published, rs->published_on);
  1708. digest_to_base64(identity64, rs->identity_digest);
  1709. digest_to_base64(digest64, rs->descriptor_digest);
  1710. smartlist_add_asprintf(chunks,
  1711. "r %s %s %s%s%s %s %d %d\n",
  1712. rs->nickname,
  1713. identity64,
  1714. (format==NS_V3_CONSENSUS_MICRODESC)?"":digest64,
  1715. (format==NS_V3_CONSENSUS_MICRODESC)?"":" ",
  1716. published,
  1717. fmt_addr32(rs->addr),
  1718. (int)rs->or_port,
  1719. (int)rs->dir_port);
  1720. /* TODO: Maybe we want to pass in what we need to build the rest of
  1721. * this here, instead of in the caller. Then we could use the
  1722. * networkstatus_type_t values, with an additional control port value
  1723. * added -MP */
  1724. /* V3 microdesc consensuses don't have "a" lines. */
  1725. if (format == NS_V3_CONSENSUS_MICRODESC)
  1726. goto done;
  1727. /* Possible "a" line. At most one for now. */
  1728. if (!tor_addr_is_null(&rs->ipv6_addr)) {
  1729. smartlist_add_asprintf(chunks, "a %s\n",
  1730. fmt_addrport(&rs->ipv6_addr, rs->ipv6_orport));
  1731. }
  1732. if (format == NS_V3_CONSENSUS)
  1733. goto done;
  1734. smartlist_add_asprintf(chunks,
  1735. "s%s%s%s%s%s%s%s%s%s%s\n",
  1736. /* These must stay in alphabetical order. */
  1737. rs->is_authority?" Authority":"",
  1738. rs->is_bad_exit?" BadExit":"",
  1739. rs->is_exit?" Exit":"",
  1740. rs->is_fast?" Fast":"",
  1741. rs->is_possible_guard?" Guard":"",
  1742. rs->is_hs_dir?" HSDir":"",
  1743. rs->is_flagged_running?" Running":"",
  1744. rs->is_stable?" Stable":"",
  1745. rs->is_v2_dir?" V2Dir":"",
  1746. rs->is_valid?" Valid":"");
  1747. /* length of "opt v \n" */
  1748. #define V_LINE_OVERHEAD 7
  1749. if (version && strlen(version) < MAX_V_LINE_LEN - V_LINE_OVERHEAD) {
  1750. smartlist_add_asprintf(chunks, "v %s\n", version);
  1751. }
  1752. if (protocols) {
  1753. smartlist_add_asprintf(chunks, "pr %s\n", protocols);
  1754. }
  1755. if (format != NS_V2) {
  1756. const routerinfo_t* desc = router_get_by_id_digest(rs->identity_digest);
  1757. uint32_t bw_kb;
  1758. if (format != NS_CONTROL_PORT) {
  1759. /* Blow up more or less nicely if we didn't get anything or not the
  1760. * thing we expected.
  1761. */
  1762. if (!desc) {
  1763. char id[HEX_DIGEST_LEN+1];
  1764. char dd[HEX_DIGEST_LEN+1];
  1765. base16_encode(id, sizeof(id), rs->identity_digest, DIGEST_LEN);
  1766. base16_encode(dd, sizeof(dd), rs->descriptor_digest, DIGEST_LEN);
  1767. log_warn(LD_BUG, "Cannot get any descriptor for %s "
  1768. "(wanted descriptor %s).",
  1769. id, dd);
  1770. goto err;
  1771. }
  1772. /* This assert could fire for the control port, because
  1773. * it can request NS documents before all descriptors
  1774. * have been fetched. Therefore, we only do this test when
  1775. * format != NS_CONTROL_PORT. */
  1776. if (tor_memneq(desc->cache_info.signed_descriptor_digest,
  1777. rs->descriptor_digest,
  1778. DIGEST_LEN)) {
  1779. char rl_d[HEX_DIGEST_LEN+1];
  1780. char rs_d[HEX_DIGEST_LEN+1];
  1781. char id[HEX_DIGEST_LEN+1];
  1782. base16_encode(rl_d, sizeof(rl_d),
  1783. desc->cache_info.signed_descriptor_digest, DIGEST_LEN);
  1784. base16_encode(rs_d, sizeof(rs_d), rs->descriptor_digest, DIGEST_LEN);
  1785. base16_encode(id, sizeof(id), rs->identity_digest, DIGEST_LEN);
  1786. log_err(LD_BUG, "descriptor digest in routerlist does not match "
  1787. "the one in routerstatus: %s vs %s "
  1788. "(router %s)\n",
  1789. rl_d, rs_d, id);
  1790. tor_assert(tor_memeq(desc->cache_info.signed_descriptor_digest,
  1791. rs->descriptor_digest,
  1792. DIGEST_LEN));
  1793. }
  1794. }
  1795. if (format == NS_CONTROL_PORT && rs->has_bandwidth) {
  1796. bw_kb = rs->bandwidth_kb;
  1797. } else {
  1798. tor_assert(desc);
  1799. bw_kb = router_get_advertised_bandwidth_capped(desc) / 1000;
  1800. }
  1801. smartlist_add_asprintf(chunks,
  1802. "w Bandwidth=%d", bw_kb);
  1803. if (format == NS_V3_VOTE && vrs && vrs->has_measured_bw) {
  1804. smartlist_add_asprintf(chunks,
  1805. " Measured=%d", vrs->measured_bw_kb);
  1806. }
  1807. /* Write down guardfraction information if we have it. */
  1808. if (format == NS_V3_VOTE && vrs && vrs->status.has_guardfraction) {
  1809. smartlist_add_asprintf(chunks,
  1810. " GuardFraction=%d",
  1811. vrs->status.guardfraction_percentage);
  1812. }
  1813. smartlist_add(chunks, tor_strdup("\n"));
  1814. if (desc) {
  1815. summary = policy_summarize(desc->exit_policy, AF_INET);
  1816. smartlist_add_asprintf(chunks, "p %s\n", summary);
  1817. tor_free(summary);
  1818. }
  1819. if (format == NS_V3_VOTE && vrs) {
  1820. if (tor_mem_is_zero((char*)vrs->ed25519_id, ED25519_PUBKEY_LEN)) {
  1821. smartlist_add(chunks, tor_strdup("id ed25519 none\n"));
  1822. } else {
  1823. char ed_b64[BASE64_DIGEST256_LEN+1];
  1824. digest256_to_base64(ed_b64, (const char*)vrs->ed25519_id);
  1825. smartlist_add_asprintf(chunks, "id ed25519 %s\n", ed_b64);
  1826. }
  1827. }
  1828. }
  1829. done:
  1830. result = smartlist_join_strings(chunks, "", 0, NULL);
  1831. err:
  1832. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  1833. smartlist_free(chunks);
  1834. return result;
  1835. }
  1836. /** Helper for sorting: compares two routerinfos first by address, and then by
  1837. * descending order of "usefulness". (An authority is more useful than a
  1838. * non-authority; a running router is more useful than a non-running router;
  1839. * and a router with more bandwidth is more useful than one with less.)
  1840. **/
  1841. static int
  1842. compare_routerinfo_by_ip_and_bw_(const void **a, const void **b)
  1843. {
  1844. routerinfo_t *first = *(routerinfo_t **)a, *second = *(routerinfo_t **)b;
  1845. int first_is_auth, second_is_auth;
  1846. uint32_t bw_kb_first, bw_kb_second;
  1847. const node_t *node_first, *node_second;
  1848. int first_is_running, second_is_running;
  1849. /* we return -1 if first should appear before second... that is,
  1850. * if first is a better router. */
  1851. if (first->addr < second->addr)
  1852. return -1;
  1853. else if (first->addr > second->addr)
  1854. return 1;
  1855. /* Potentially, this next bit could cause k n lg n memeq calls. But in
  1856. * reality, we will almost never get here, since addresses will usually be
  1857. * different. */
  1858. first_is_auth =
  1859. router_digest_is_trusted_dir(first->cache_info.identity_digest);
  1860. second_is_auth =
  1861. router_digest_is_trusted_dir(second->cache_info.identity_digest);
  1862. if (first_is_auth && !second_is_auth)
  1863. return -1;
  1864. else if (!first_is_auth && second_is_auth)
  1865. return 1;
  1866. node_first = node_get_by_id(first->cache_info.identity_digest);
  1867. node_second = node_get_by_id(second->cache_info.identity_digest);
  1868. first_is_running = node_first && node_first->is_running;
  1869. second_is_running = node_second && node_second->is_running;
  1870. if (first_is_running && !second_is_running)
  1871. return -1;
  1872. else if (!first_is_running && second_is_running)
  1873. return 1;
  1874. bw_kb_first = dirserv_get_bandwidth_for_router_kb(first);
  1875. bw_kb_second = dirserv_get_bandwidth_for_router_kb(second);
  1876. if (bw_kb_first > bw_kb_second)
  1877. return -1;
  1878. else if (bw_kb_first < bw_kb_second)
  1879. return 1;
  1880. /* They're equal! Compare by identity digest, so there's a
  1881. * deterministic order and we avoid flapping. */
  1882. return fast_memcmp(first->cache_info.identity_digest,
  1883. second->cache_info.identity_digest,
  1884. DIGEST_LEN);
  1885. }
  1886. /** Given a list of routerinfo_t in <b>routers</b>, return a new digestmap_t
  1887. * whose keys are the identity digests of those routers that we're going to
  1888. * exclude for Sybil-like appearance. */
  1889. static digestmap_t *
  1890. get_possible_sybil_list(const smartlist_t *routers)
  1891. {
  1892. const or_options_t *options = get_options();
  1893. digestmap_t *omit_as_sybil;
  1894. smartlist_t *routers_by_ip = smartlist_new();
  1895. uint32_t last_addr;
  1896. int addr_count;
  1897. /* Allow at most this number of Tor servers on a single IP address, ... */
  1898. int max_with_same_addr = options->AuthDirMaxServersPerAddr;
  1899. /* ... unless it's a directory authority, in which case allow more. */
  1900. int max_with_same_addr_on_authority = options->AuthDirMaxServersPerAuthAddr;
  1901. if (max_with_same_addr <= 0)
  1902. max_with_same_addr = INT_MAX;
  1903. if (max_with_same_addr_on_authority <= 0)
  1904. max_with_same_addr_on_authority = INT_MAX;
  1905. smartlist_add_all(routers_by_ip, routers);
  1906. smartlist_sort(routers_by_ip, compare_routerinfo_by_ip_and_bw_);
  1907. omit_as_sybil = digestmap_new();
  1908. last_addr = 0;
  1909. addr_count = 0;
  1910. SMARTLIST_FOREACH_BEGIN(routers_by_ip, routerinfo_t *, ri) {
  1911. if (last_addr != ri->addr) {
  1912. last_addr = ri->addr;
  1913. addr_count = 1;
  1914. } else if (++addr_count > max_with_same_addr) {
  1915. if (!router_addr_is_trusted_dir(ri->addr) ||
  1916. addr_count > max_with_same_addr_on_authority)
  1917. digestmap_set(omit_as_sybil, ri->cache_info.identity_digest, ri);
  1918. }
  1919. } SMARTLIST_FOREACH_END(ri);
  1920. smartlist_free(routers_by_ip);
  1921. return omit_as_sybil;
  1922. }
  1923. /** If there are entries in <b>routers</b> with exactly the same ed25519 keys,
  1924. * remove the older one. If they are exactly the same age, remove the one
  1925. * with the greater descriptor digest. May alter the order of the list. */
  1926. static void
  1927. routers_make_ed_keys_unique(smartlist_t *routers)
  1928. {
  1929. routerinfo_t *ri2;
  1930. digest256map_t *by_ed_key = digest256map_new();
  1931. SMARTLIST_FOREACH_BEGIN(routers, routerinfo_t *, ri) {
  1932. ri->omit_from_vote = 0;
  1933. if (ri->cache_info.signing_key_cert == NULL)
  1934. continue; /* No ed key */
  1935. const uint8_t *pk = ri->cache_info.signing_key_cert->signing_key.pubkey;
  1936. if ((ri2 = digest256map_get(by_ed_key, pk))) {
  1937. /* Duplicate; must omit one. Set the omit_from_vote flag in whichever
  1938. * one has the earlier published_on. */
  1939. const time_t ri_pub = ri->cache_info.published_on;
  1940. const time_t ri2_pub = ri2->cache_info.published_on;
  1941. if (ri2_pub < ri_pub ||
  1942. (ri2_pub == ri_pub &&
  1943. fast_memcmp(ri->cache_info.signed_descriptor_digest,
  1944. ri2->cache_info.signed_descriptor_digest,DIGEST_LEN)<0)) {
  1945. digest256map_set(by_ed_key, pk, ri);
  1946. ri2->omit_from_vote = 1;
  1947. } else {
  1948. ri->omit_from_vote = 1;
  1949. }
  1950. } else {
  1951. /* Add to map */
  1952. digest256map_set(by_ed_key, pk, ri);
  1953. }
  1954. } SMARTLIST_FOREACH_END(ri);
  1955. digest256map_free(by_ed_key, NULL);
  1956. /* Now remove every router where the omit_from_vote flag got set. */
  1957. SMARTLIST_FOREACH_BEGIN(routers, const routerinfo_t *, ri) {
  1958. if (ri->omit_from_vote) {
  1959. SMARTLIST_DEL_CURRENT(routers, ri);
  1960. }
  1961. } SMARTLIST_FOREACH_END(ri);
  1962. }
  1963. /** Extract status information from <b>ri</b> and from other authority
  1964. * functions and store it in <b>rs</b>>.
  1965. *
  1966. * We assume that ri-\>is_running has already been set, e.g. by
  1967. * dirserv_set_router_is_running(ri, now);
  1968. */
  1969. void
  1970. set_routerstatus_from_routerinfo(routerstatus_t *rs,
  1971. node_t *node,
  1972. routerinfo_t *ri,
  1973. time_t now,
  1974. int listbadexits)
  1975. {
  1976. const or_options_t *options = get_options();
  1977. uint32_t routerbw_kb = dirserv_get_credible_bandwidth_kb(ri);
  1978. memset(rs, 0, sizeof(routerstatus_t));
  1979. rs->is_authority =
  1980. router_digest_is_trusted_dir(ri->cache_info.identity_digest);
  1981. /* Already set by compute_performance_thresholds. */
  1982. rs->is_exit = node->is_exit;
  1983. rs->is_stable = node->is_stable =
  1984. !dirserv_thinks_router_is_unreliable(now, ri, 1, 0);
  1985. rs->is_fast = node->is_fast =
  1986. !dirserv_thinks_router_is_unreliable(now, ri, 0, 1);
  1987. rs->is_flagged_running = node->is_running; /* computed above */
  1988. rs->is_valid = node->is_valid;
  1989. if (node->is_fast && node->is_stable &&
  1990. ((options->AuthDirGuardBWGuarantee &&
  1991. routerbw_kb >= options->AuthDirGuardBWGuarantee/1000) ||
  1992. routerbw_kb >= MIN(guard_bandwidth_including_exits_kb,
  1993. guard_bandwidth_excluding_exits_kb))) {
  1994. long tk = rep_hist_get_weighted_time_known(
  1995. node->identity, now);
  1996. double wfu = rep_hist_get_weighted_fractional_uptime(
  1997. node->identity, now);
  1998. rs->is_possible_guard = (wfu >= guard_wfu && tk >= guard_tk) ? 1 : 0;
  1999. } else {
  2000. rs->is_possible_guard = 0;
  2001. }
  2002. rs->is_bad_exit = listbadexits && node->is_bad_exit;
  2003. rs->is_hs_dir = node->is_hs_dir =
  2004. dirserv_thinks_router_is_hs_dir(ri, node, now);
  2005. rs->is_named = rs->is_unnamed = 0;
  2006. rs->published_on = ri->cache_info.published_on;
  2007. memcpy(rs->identity_digest, node->identity, DIGEST_LEN);
  2008. memcpy(rs->descriptor_digest, ri->cache_info.signed_descriptor_digest,
  2009. DIGEST_LEN);
  2010. rs->addr = ri->addr;
  2011. strlcpy(rs->nickname, ri->nickname, sizeof(rs->nickname));
  2012. rs->or_port = ri->or_port;
  2013. rs->dir_port = ri->dir_port;
  2014. rs->is_v2_dir = ri->supports_tunnelled_dir_requests;
  2015. if (options->AuthDirHasIPv6Connectivity == 1 &&
  2016. !tor_addr_is_null(&ri->ipv6_addr) &&
  2017. node->last_reachable6 >= now - REACHABLE_TIMEOUT) {
  2018. /* We're configured as having IPv6 connectivity. There's an IPv6
  2019. OR port and it's reachable so copy it to the routerstatus. */
  2020. tor_addr_copy(&rs->ipv6_addr, &ri->ipv6_addr);
  2021. rs->ipv6_orport = ri->ipv6_orport;
  2022. }
  2023. if (options->TestingTorNetwork) {
  2024. dirserv_set_routerstatus_testing(rs);
  2025. }
  2026. }
  2027. /** Use TestingDirAuthVoteExit, TestingDirAuthVoteGuard, and
  2028. * TestingDirAuthVoteHSDir to give out the Exit, Guard, and HSDir flags,
  2029. * respectively. But don't set the corresponding node flags.
  2030. * Should only be called if TestingTorNetwork is set. */
  2031. STATIC void
  2032. dirserv_set_routerstatus_testing(routerstatus_t *rs)
  2033. {
  2034. const or_options_t *options = get_options();
  2035. tor_assert(options->TestingTorNetwork);
  2036. if (routerset_contains_routerstatus(options->TestingDirAuthVoteExit,
  2037. rs, 0)) {
  2038. rs->is_exit = 1;
  2039. } else if (options->TestingDirAuthVoteExitIsStrict) {
  2040. rs->is_exit = 0;
  2041. }
  2042. if (routerset_contains_routerstatus(options->TestingDirAuthVoteGuard,
  2043. rs, 0)) {
  2044. rs->is_possible_guard = 1;
  2045. } else if (options->TestingDirAuthVoteGuardIsStrict) {
  2046. rs->is_possible_guard = 0;
  2047. }
  2048. if (routerset_contains_routerstatus(options->TestingDirAuthVoteHSDir,
  2049. rs, 0)) {
  2050. rs->is_hs_dir = 1;
  2051. } else if (options->TestingDirAuthVoteHSDirIsStrict) {
  2052. rs->is_hs_dir = 0;
  2053. }
  2054. }
  2055. /** Routerstatus <b>rs</b> is part of a group of routers that are on
  2056. * too narrow an IP-space. Clear out its flags: we don't want people
  2057. * using it.
  2058. *
  2059. * Leave its BadExit flag alone though, since if we think it's a bad exit,
  2060. * we want to vote that way in case all the other authorities are voting
  2061. * Running and Exit.
  2062. */
  2063. static void
  2064. clear_status_flags_on_sybil(routerstatus_t *rs)
  2065. {
  2066. rs->is_authority = rs->is_exit = rs->is_stable = rs->is_fast =
  2067. rs->is_flagged_running = rs->is_named = rs->is_valid =
  2068. rs->is_hs_dir = rs->is_v2_dir = rs->is_possible_guard = 0;
  2069. /* FFFF we might want some mechanism to check later on if we
  2070. * missed zeroing any flags: it's easy to add a new flag but
  2071. * forget to add it to this clause. */
  2072. }
  2073. /** The guardfraction of the guard with identity fingerprint <b>guard_id</b>
  2074. * is <b>guardfraction_percentage</b>. See if we have a vote routerstatus for
  2075. * this guard in <b>vote_routerstatuses</b>, and if we do, register the
  2076. * information to it.
  2077. *
  2078. * Return 1 if we applied the information and 0 if we couldn't find a
  2079. * matching guard.
  2080. *
  2081. * Requires that <b>vote_routerstatuses</b> be sorted.
  2082. */
  2083. static int
  2084. guardfraction_line_apply(const char *guard_id,
  2085. uint32_t guardfraction_percentage,
  2086. smartlist_t *vote_routerstatuses)
  2087. {
  2088. vote_routerstatus_t *vrs = NULL;
  2089. tor_assert(vote_routerstatuses);
  2090. vrs = smartlist_bsearch(vote_routerstatuses, guard_id,
  2091. compare_digest_to_vote_routerstatus_entry);
  2092. if (!vrs) {
  2093. return 0;
  2094. }
  2095. vrs->status.has_guardfraction = 1;
  2096. vrs->status.guardfraction_percentage = guardfraction_percentage;
  2097. return 1;
  2098. }
  2099. /* Given a guard line from a guardfraction file, parse it and register
  2100. * its information to <b>vote_routerstatuses</b>.
  2101. *
  2102. * Return:
  2103. * * 1 if the line was proper and its information got registered.
  2104. * * 0 if the line was proper but no currently active guard was found
  2105. * to register the guardfraction information to.
  2106. * * -1 if the line could not be parsed and set <b>err_msg</b> to a
  2107. newly allocated string containing the error message.
  2108. */
  2109. static int
  2110. guardfraction_file_parse_guard_line(const char *guard_line,
  2111. smartlist_t *vote_routerstatuses,
  2112. char **err_msg)
  2113. {
  2114. char guard_id[DIGEST_LEN];
  2115. uint32_t guardfraction;
  2116. char *inputs_tmp = NULL;
  2117. int num_ok = 1;
  2118. smartlist_t *sl = smartlist_new();
  2119. int retval = -1;
  2120. tor_assert(err_msg);
  2121. /* guard_line should contain something like this:
  2122. <hex digest> <guardfraction> <appearances> */
  2123. smartlist_split_string(sl, guard_line, " ",
  2124. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 3);
  2125. if (smartlist_len(sl) < 3) {
  2126. tor_asprintf(err_msg, "bad line '%s'", guard_line);
  2127. goto done;
  2128. }
  2129. inputs_tmp = smartlist_get(sl, 0);
  2130. if (strlen(inputs_tmp) != HEX_DIGEST_LEN ||
  2131. base16_decode(guard_id, DIGEST_LEN,
  2132. inputs_tmp, HEX_DIGEST_LEN) != DIGEST_LEN) {
  2133. tor_asprintf(err_msg, "bad digest '%s'", inputs_tmp);
  2134. goto done;
  2135. }
  2136. inputs_tmp = smartlist_get(sl, 1);
  2137. /* Guardfraction is an integer in [0, 100]. */
  2138. guardfraction =
  2139. (uint32_t) tor_parse_long(inputs_tmp, 10, 0, 100, &num_ok, NULL);
  2140. if (!num_ok) {
  2141. tor_asprintf(err_msg, "wrong percentage '%s'", inputs_tmp);
  2142. goto done;
  2143. }
  2144. /* If routerstatuses were provided, apply this info to actual routers. */
  2145. if (vote_routerstatuses) {
  2146. retval = guardfraction_line_apply(guard_id, guardfraction,
  2147. vote_routerstatuses);
  2148. } else {
  2149. retval = 0; /* If we got this far, line was correctly formatted. */
  2150. }
  2151. done:
  2152. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  2153. smartlist_free(sl);
  2154. return retval;
  2155. }
  2156. /** Given an inputs line from a guardfraction file, parse it and
  2157. * register its information to <b>total_consensuses</b> and
  2158. * <b>total_days</b>.
  2159. *
  2160. * Return 0 if it parsed well. Return -1 if there was an error, and
  2161. * set <b>err_msg</b> to a newly allocated string containing the
  2162. * error message.
  2163. */
  2164. static int
  2165. guardfraction_file_parse_inputs_line(const char *inputs_line,
  2166. int *total_consensuses,
  2167. int *total_days,
  2168. char **err_msg)
  2169. {
  2170. int retval = -1;
  2171. char *inputs_tmp = NULL;
  2172. int num_ok = 1;
  2173. smartlist_t *sl = smartlist_new();
  2174. tor_assert(err_msg);
  2175. /* Second line is inputs information:
  2176. * n-inputs <total_consensuses> <total_days>. */
  2177. smartlist_split_string(sl, inputs_line, " ",
  2178. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 3);
  2179. if (smartlist_len(sl) < 2) {
  2180. tor_asprintf(err_msg, "incomplete line '%s'", inputs_line);
  2181. goto done;
  2182. }
  2183. inputs_tmp = smartlist_get(sl, 0);
  2184. *total_consensuses =
  2185. (int) tor_parse_long(inputs_tmp, 10, 0, INT_MAX, &num_ok, NULL);
  2186. if (!num_ok) {
  2187. tor_asprintf(err_msg, "unparseable consensus '%s'", inputs_tmp);
  2188. goto done;
  2189. }
  2190. inputs_tmp = smartlist_get(sl, 1);
  2191. *total_days =
  2192. (int) tor_parse_long(inputs_tmp, 10, 0, INT_MAX, &num_ok, NULL);
  2193. if (!num_ok) {
  2194. tor_asprintf(err_msg, "unparseable days '%s'", inputs_tmp);
  2195. goto done;
  2196. }
  2197. retval = 0;
  2198. done:
  2199. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  2200. smartlist_free(sl);
  2201. return retval;
  2202. }
  2203. /* Maximum age of a guardfraction file that we are willing to accept. */
  2204. #define MAX_GUARDFRACTION_FILE_AGE (7*24*60*60) /* approx a week */
  2205. /** Static strings of guardfraction files. */
  2206. #define GUARDFRACTION_DATE_STR "written-at"
  2207. #define GUARDFRACTION_INPUTS "n-inputs"
  2208. #define GUARDFRACTION_GUARD "guard-seen"
  2209. #define GUARDFRACTION_VERSION "guardfraction-file-version"
  2210. /** Given a guardfraction file in a string, parse it and register the
  2211. * guardfraction information to the provided vote routerstatuses.
  2212. *
  2213. * This is the rough format of the guardfraction file:
  2214. *
  2215. * guardfraction-file-version 1
  2216. * written-at <date and time>
  2217. * n-inputs <number of consesuses parsed> <number of days considered>
  2218. *
  2219. * guard-seen <fpr 1> <guardfraction percentage> <consensus appearances>
  2220. * guard-seen <fpr 2> <guardfraction percentage> <consensus appearances>
  2221. * guard-seen <fpr 3> <guardfraction percentage> <consensus appearances>
  2222. * guard-seen <fpr 4> <guardfraction percentage> <consensus appearances>
  2223. * guard-seen <fpr 5> <guardfraction percentage> <consensus appearances>
  2224. * ...
  2225. *
  2226. * Return -1 if the parsing failed and 0 if it went smoothly. Parsing
  2227. * should tolerate errors in all lines but the written-at header.
  2228. */
  2229. STATIC int
  2230. dirserv_read_guardfraction_file_from_str(const char *guardfraction_file_str,
  2231. smartlist_t *vote_routerstatuses)
  2232. {
  2233. config_line_t *front=NULL, *line;
  2234. int ret_tmp;
  2235. int retval = -1;
  2236. int current_line_n = 0; /* line counter for better log messages */
  2237. /* Guardfraction info to be parsed */
  2238. int total_consensuses = 0;
  2239. int total_days = 0;
  2240. /* Stats */
  2241. int guards_read_n = 0;
  2242. int guards_applied_n = 0;
  2243. /* Parse file and split it in lines */
  2244. ret_tmp = config_get_lines(guardfraction_file_str, &front, 0);
  2245. if (ret_tmp < 0) {
  2246. log_warn(LD_CONFIG, "Error reading from guardfraction file");
  2247. goto done;
  2248. }
  2249. /* Sort routerstatuses (needed later when applying guardfraction info) */
  2250. if (vote_routerstatuses)
  2251. smartlist_sort(vote_routerstatuses, compare_vote_routerstatus_entries);
  2252. for (line = front; line; line=line->next) {
  2253. current_line_n++;
  2254. if (!strcmp(line->key, GUARDFRACTION_VERSION)) {
  2255. int num_ok = 1;
  2256. unsigned int version;
  2257. version =
  2258. (unsigned int) tor_parse_long(line->value,
  2259. 10, 0, INT_MAX, &num_ok, NULL);
  2260. if (!num_ok || version != 1) {
  2261. log_warn(LD_GENERAL, "Got unknown guardfraction version %d.", version);
  2262. goto done;
  2263. }
  2264. } else if (!strcmp(line->key, GUARDFRACTION_DATE_STR)) {
  2265. time_t file_written_at;
  2266. time_t now = time(NULL);
  2267. /* First line is 'written-at <date>' */
  2268. if (parse_iso_time(line->value, &file_written_at) < 0) {
  2269. log_warn(LD_CONFIG, "Guardfraction:%d: Bad date '%s'. Ignoring",
  2270. current_line_n, line->value);
  2271. goto done; /* don't tolerate failure here. */
  2272. }
  2273. if (file_written_at < now - MAX_GUARDFRACTION_FILE_AGE) {
  2274. log_warn(LD_CONFIG, "Guardfraction:%d: was written very long ago '%s'",
  2275. current_line_n, line->value);
  2276. goto done; /* don't tolerate failure here. */
  2277. }
  2278. } else if (!strcmp(line->key, GUARDFRACTION_INPUTS)) {
  2279. char *err_msg = NULL;
  2280. if (guardfraction_file_parse_inputs_line(line->value,
  2281. &total_consensuses,
  2282. &total_days,
  2283. &err_msg) < 0) {
  2284. log_warn(LD_CONFIG, "Guardfraction:%d: %s",
  2285. current_line_n, err_msg);
  2286. tor_free(err_msg);
  2287. continue;
  2288. }
  2289. } else if (!strcmp(line->key, GUARDFRACTION_GUARD)) {
  2290. char *err_msg = NULL;
  2291. ret_tmp = guardfraction_file_parse_guard_line(line->value,
  2292. vote_routerstatuses,
  2293. &err_msg);
  2294. if (ret_tmp < 0) { /* failed while parsing the guard line */
  2295. log_warn(LD_CONFIG, "Guardfraction:%d: %s",
  2296. current_line_n, err_msg);
  2297. tor_free(err_msg);
  2298. continue;
  2299. }
  2300. /* Successfully parsed guard line. Check if it was applied properly. */
  2301. guards_read_n++;
  2302. if (ret_tmp > 0) {
  2303. guards_applied_n++;
  2304. }
  2305. } else {
  2306. log_warn(LD_CONFIG, "Unknown guardfraction line %d (%s %s)",
  2307. current_line_n, line->key, line->value);
  2308. }
  2309. }
  2310. retval = 0;
  2311. log_info(LD_CONFIG,
  2312. "Successfully parsed guardfraction file with %d consensuses over "
  2313. "%d days. Parsed %d nodes and applied %d of them%s.",
  2314. total_consensuses, total_days, guards_read_n, guards_applied_n,
  2315. vote_routerstatuses ? "" : " (no routerstatus provided)" );
  2316. done:
  2317. config_free_lines(front);
  2318. if (retval < 0) {
  2319. return retval;
  2320. } else {
  2321. return guards_read_n;
  2322. }
  2323. }
  2324. /** Read a guardfraction file at <b>fname</b> and load all its
  2325. * information to <b>vote_routerstatuses</b>. */
  2326. int
  2327. dirserv_read_guardfraction_file(const char *fname,
  2328. smartlist_t *vote_routerstatuses)
  2329. {
  2330. char *guardfraction_file_str;
  2331. /* Read file to a string */
  2332. guardfraction_file_str = read_file_to_str(fname, RFTS_IGNORE_MISSING, NULL);
  2333. if (!guardfraction_file_str) {
  2334. log_warn(LD_FS, "Cannot open guardfraction file '%s'. Failing.", fname);
  2335. return -1;
  2336. }
  2337. return dirserv_read_guardfraction_file_from_str(guardfraction_file_str,
  2338. vote_routerstatuses);
  2339. }
  2340. /**
  2341. * Helper function to parse out a line in the measured bandwidth file
  2342. * into a measured_bw_line_t output structure. Returns -1 on failure
  2343. * or 0 on success.
  2344. */
  2345. STATIC int
  2346. measured_bw_line_parse(measured_bw_line_t *out, const char *orig_line)
  2347. {
  2348. char *line = tor_strdup(orig_line);
  2349. char *cp = line;
  2350. int got_bw = 0;
  2351. int got_node_id = 0;
  2352. char *strtok_state; /* lame sauce d'jour */
  2353. cp = tor_strtok_r(cp, " \t", &strtok_state);
  2354. if (!cp) {
  2355. log_warn(LD_DIRSERV, "Invalid line in bandwidth file: %s",
  2356. escaped(orig_line));
  2357. tor_free(line);
  2358. return -1;
  2359. }
  2360. if (orig_line[strlen(orig_line)-1] != '\n') {
  2361. log_warn(LD_DIRSERV, "Incomplete line in bandwidth file: %s",
  2362. escaped(orig_line));
  2363. tor_free(line);
  2364. return -1;
  2365. }
  2366. do {
  2367. if (strcmpstart(cp, "bw=") == 0) {
  2368. int parse_ok = 0;
  2369. char *endptr;
  2370. if (got_bw) {
  2371. log_warn(LD_DIRSERV, "Double bw= in bandwidth file line: %s",
  2372. escaped(orig_line));
  2373. tor_free(line);
  2374. return -1;
  2375. }
  2376. cp+=strlen("bw=");
  2377. out->bw_kb = tor_parse_long(cp, 0, 0, LONG_MAX, &parse_ok, &endptr);
  2378. if (!parse_ok || (*endptr && !TOR_ISSPACE(*endptr))) {
  2379. log_warn(LD_DIRSERV, "Invalid bandwidth in bandwidth file line: %s",
  2380. escaped(orig_line));
  2381. tor_free(line);
  2382. return -1;
  2383. }
  2384. got_bw=1;
  2385. } else if (strcmpstart(cp, "node_id=$") == 0) {
  2386. if (got_node_id) {
  2387. log_warn(LD_DIRSERV, "Double node_id= in bandwidth file line: %s",
  2388. escaped(orig_line));
  2389. tor_free(line);
  2390. return -1;
  2391. }
  2392. cp+=strlen("node_id=$");
  2393. if (strlen(cp) != HEX_DIGEST_LEN ||
  2394. base16_decode(out->node_id, DIGEST_LEN,
  2395. cp, HEX_DIGEST_LEN) != DIGEST_LEN) {
  2396. log_warn(LD_DIRSERV, "Invalid node_id in bandwidth file line: %s",
  2397. escaped(orig_line));
  2398. tor_free(line);
  2399. return -1;
  2400. }
  2401. strlcpy(out->node_hex, cp, sizeof(out->node_hex));
  2402. got_node_id=1;
  2403. }
  2404. } while ((cp = tor_strtok_r(NULL, " \t", &strtok_state)));
  2405. if (got_bw && got_node_id) {
  2406. tor_free(line);
  2407. return 0;
  2408. } else {
  2409. log_warn(LD_DIRSERV, "Incomplete line in bandwidth file: %s",
  2410. escaped(orig_line));
  2411. tor_free(line);
  2412. return -1;
  2413. }
  2414. }
  2415. /**
  2416. * Helper function to apply a parsed measurement line to a list
  2417. * of bandwidth statuses. Returns true if a line is found,
  2418. * false otherwise.
  2419. */
  2420. STATIC int
  2421. measured_bw_line_apply(measured_bw_line_t *parsed_line,
  2422. smartlist_t *routerstatuses)
  2423. {
  2424. vote_routerstatus_t *rs = NULL;
  2425. if (!routerstatuses)
  2426. return 0;
  2427. rs = smartlist_bsearch(routerstatuses, parsed_line->node_id,
  2428. compare_digest_to_vote_routerstatus_entry);
  2429. if (rs) {
  2430. rs->has_measured_bw = 1;
  2431. rs->measured_bw_kb = (uint32_t)parsed_line->bw_kb;
  2432. } else {
  2433. log_info(LD_DIRSERV, "Node ID %s not found in routerstatus list",
  2434. parsed_line->node_hex);
  2435. }
  2436. return rs != NULL;
  2437. }
  2438. /**
  2439. * Read the measured bandwidth file and apply it to the list of
  2440. * vote_routerstatus_t. Returns -1 on error, 0 otherwise.
  2441. */
  2442. int
  2443. dirserv_read_measured_bandwidths(const char *from_file,
  2444. smartlist_t *routerstatuses)
  2445. {
  2446. char line[512];
  2447. FILE *fp = tor_fopen_cloexec(from_file, "r");
  2448. int applied_lines = 0;
  2449. time_t file_time, now;
  2450. int ok;
  2451. if (fp == NULL) {
  2452. log_warn(LD_CONFIG, "Can't open bandwidth file at configured location: %s",
  2453. from_file);
  2454. return -1;
  2455. }
  2456. if (!fgets(line, sizeof(line), fp)
  2457. || !strlen(line) || line[strlen(line)-1] != '\n') {
  2458. log_warn(LD_DIRSERV, "Long or truncated time in bandwidth file: %s",
  2459. escaped(line));
  2460. fclose(fp);
  2461. return -1;
  2462. }
  2463. line[strlen(line)-1] = '\0';
  2464. file_time = (time_t)tor_parse_ulong(line, 10, 0, ULONG_MAX, &ok, NULL);
  2465. if (!ok) {
  2466. log_warn(LD_DIRSERV, "Non-integer time in bandwidth file: %s",
  2467. escaped(line));
  2468. fclose(fp);
  2469. return -1;
  2470. }
  2471. now = time(NULL);
  2472. if ((now - file_time) > MAX_MEASUREMENT_AGE) {
  2473. log_warn(LD_DIRSERV, "Bandwidth measurement file stale. Age: %u",
  2474. (unsigned)(time(NULL) - file_time));
  2475. fclose(fp);
  2476. return -1;
  2477. }
  2478. if (routerstatuses)
  2479. smartlist_sort(routerstatuses, compare_vote_routerstatus_entries);
  2480. while (!feof(fp)) {
  2481. measured_bw_line_t parsed_line;
  2482. if (fgets(line, sizeof(line), fp) && strlen(line)) {
  2483. if (measured_bw_line_parse(&parsed_line, line) != -1) {
  2484. /* Also cache the line for dirserv_get_bandwidth_for_router() */
  2485. dirserv_cache_measured_bw(&parsed_line, file_time);
  2486. if (measured_bw_line_apply(&parsed_line, routerstatuses) > 0)
  2487. applied_lines++;
  2488. }
  2489. }
  2490. }
  2491. /* Now would be a nice time to clean the cache, too */
  2492. dirserv_expire_measured_bw_cache(now);
  2493. fclose(fp);
  2494. log_info(LD_DIRSERV,
  2495. "Bandwidth measurement file successfully read. "
  2496. "Applied %d measurements.", applied_lines);
  2497. return 0;
  2498. }
  2499. /** Return a new networkstatus_t* containing our current opinion. (For v3
  2500. * authorities) */
  2501. networkstatus_t *
  2502. dirserv_generate_networkstatus_vote_obj(crypto_pk_t *private_key,
  2503. authority_cert_t *cert)
  2504. {
  2505. const or_options_t *options = get_options();
  2506. networkstatus_t *v3_out = NULL;
  2507. uint32_t addr;
  2508. char *hostname = NULL, *client_versions = NULL, *server_versions = NULL;
  2509. const char *contact;
  2510. smartlist_t *routers, *routerstatuses;
  2511. char identity_digest[DIGEST_LEN];
  2512. char signing_key_digest[DIGEST_LEN];
  2513. int listbadexits = options->AuthDirListBadExits;
  2514. routerlist_t *rl = router_get_routerlist();
  2515. time_t now = time(NULL);
  2516. time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
  2517. networkstatus_voter_info_t *voter = NULL;
  2518. vote_timing_t timing;
  2519. digestmap_t *omit_as_sybil = NULL;
  2520. const int vote_on_reachability = running_long_enough_to_decide_unreachable();
  2521. smartlist_t *microdescriptors = NULL;
  2522. tor_assert(private_key);
  2523. tor_assert(cert);
  2524. if (crypto_pk_get_digest(private_key, signing_key_digest)<0) {
  2525. log_err(LD_BUG, "Error computing signing key digest");
  2526. return NULL;
  2527. }
  2528. if (crypto_pk_get_digest(cert->identity_key, identity_digest)<0) {
  2529. log_err(LD_BUG, "Error computing identity key digest");
  2530. return NULL;
  2531. }
  2532. if (resolve_my_address(LOG_WARN, options, &addr, NULL, &hostname)<0) {
  2533. log_warn(LD_NET, "Couldn't resolve my hostname");
  2534. return NULL;
  2535. }
  2536. if (!hostname || !strchr(hostname, '.')) {
  2537. tor_free(hostname);
  2538. hostname = tor_dup_ip(addr);
  2539. }
  2540. if (options->VersioningAuthoritativeDir) {
  2541. client_versions = format_versions_list(options->RecommendedClientVersions);
  2542. server_versions = format_versions_list(options->RecommendedServerVersions);
  2543. }
  2544. contact = get_options()->ContactInfo;
  2545. if (!contact)
  2546. contact = "(none)";
  2547. /*
  2548. * Do this so dirserv_compute_performance_thresholds() and
  2549. * set_routerstatus_from_routerinfo() see up-to-date bandwidth info.
  2550. */
  2551. if (options->V3BandwidthsFile) {
  2552. dirserv_read_measured_bandwidths(options->V3BandwidthsFile, NULL);
  2553. } else {
  2554. /*
  2555. * No bandwidths file; clear the measured bandwidth cache in case we had
  2556. * one last time around.
  2557. */
  2558. if (dirserv_get_measured_bw_cache_size() > 0) {
  2559. dirserv_clear_measured_bw_cache();
  2560. }
  2561. }
  2562. /* precompute this part, since we need it to decide what "stable"
  2563. * means. */
  2564. SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri, {
  2565. dirserv_set_router_is_running(ri, now);
  2566. });
  2567. routers = smartlist_new();
  2568. smartlist_add_all(routers, rl->routers);
  2569. routers_make_ed_keys_unique(routers);
  2570. /* After this point, don't use rl->routers; use 'routers' instead. */
  2571. routers_sort_by_identity(routers);
  2572. omit_as_sybil = get_possible_sybil_list(routers);
  2573. DIGESTMAP_FOREACH(omit_as_sybil, sybil_id, void *, ignore) {
  2574. (void) ignore;
  2575. rep_hist_make_router_pessimal(sybil_id, now);
  2576. } DIGESTMAP_FOREACH_END;
  2577. /* Count how many have measured bandwidths so we know how to assign flags;
  2578. * this must come before dirserv_compute_performance_thresholds() */
  2579. dirserv_count_measured_bws(routers);
  2580. dirserv_compute_performance_thresholds(omit_as_sybil);
  2581. routerstatuses = smartlist_new();
  2582. microdescriptors = smartlist_new();
  2583. SMARTLIST_FOREACH_BEGIN(routers, routerinfo_t *, ri) {
  2584. if (ri->cache_info.published_on >= cutoff) {
  2585. routerstatus_t *rs;
  2586. vote_routerstatus_t *vrs;
  2587. node_t *node = node_get_mutable_by_id(ri->cache_info.identity_digest);
  2588. if (!node)
  2589. continue;
  2590. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  2591. rs = &vrs->status;
  2592. set_routerstatus_from_routerinfo(rs, node, ri, now,
  2593. listbadexits);
  2594. if (ri->cache_info.signing_key_cert) {
  2595. memcpy(vrs->ed25519_id,
  2596. ri->cache_info.signing_key_cert->signing_key.pubkey,
  2597. ED25519_PUBKEY_LEN);
  2598. }
  2599. if (digestmap_get(omit_as_sybil, ri->cache_info.identity_digest))
  2600. clear_status_flags_on_sybil(rs);
  2601. if (!vote_on_reachability)
  2602. rs->is_flagged_running = 0;
  2603. vrs->version = version_from_platform(ri->platform);
  2604. if (ri->protocol_list) {
  2605. vrs->protocols = tor_strdup(ri->protocol_list);
  2606. } else {
  2607. vrs->protocols = tor_strdup(
  2608. protover_compute_for_old_tor(vrs->version));
  2609. }
  2610. vrs->microdesc = dirvote_format_all_microdesc_vote_lines(ri, now,
  2611. microdescriptors);
  2612. smartlist_add(routerstatuses, vrs);
  2613. }
  2614. } SMARTLIST_FOREACH_END(ri);
  2615. {
  2616. smartlist_t *added =
  2617. microdescs_add_list_to_cache(get_microdesc_cache(),
  2618. microdescriptors, SAVED_NOWHERE, 0);
  2619. smartlist_free(added);
  2620. smartlist_free(microdescriptors);
  2621. }
  2622. smartlist_free(routers);
  2623. digestmap_free(omit_as_sybil, NULL);
  2624. /* Apply guardfraction information to routerstatuses. */
  2625. if (options->GuardfractionFile) {
  2626. dirserv_read_guardfraction_file(options->GuardfractionFile,
  2627. routerstatuses);
  2628. }
  2629. /* This pass through applies the measured bw lines to the routerstatuses */
  2630. if (options->V3BandwidthsFile) {
  2631. dirserv_read_measured_bandwidths(options->V3BandwidthsFile,
  2632. routerstatuses);
  2633. } else {
  2634. /*
  2635. * No bandwidths file; clear the measured bandwidth cache in case we had
  2636. * one last time around.
  2637. */
  2638. if (dirserv_get_measured_bw_cache_size() > 0) {
  2639. dirserv_clear_measured_bw_cache();
  2640. }
  2641. }
  2642. v3_out = tor_malloc_zero(sizeof(networkstatus_t));
  2643. v3_out->type = NS_TYPE_VOTE;
  2644. dirvote_get_preferred_voting_intervals(&timing);
  2645. v3_out->published = now;
  2646. {
  2647. char tbuf[ISO_TIME_LEN+1];
  2648. networkstatus_t *current_consensus =
  2649. networkstatus_get_live_consensus(now);
  2650. long last_consensus_interval; /* only used to pick a valid_after */
  2651. if (current_consensus)
  2652. last_consensus_interval = current_consensus->fresh_until -
  2653. current_consensus->valid_after;
  2654. else
  2655. last_consensus_interval = options->TestingV3AuthInitialVotingInterval;
  2656. v3_out->valid_after =
  2657. dirvote_get_start_of_next_interval(now, (int)last_consensus_interval,
  2658. options->TestingV3AuthVotingStartOffset);
  2659. format_iso_time(tbuf, v3_out->valid_after);
  2660. log_notice(LD_DIR,"Choosing valid-after time in vote as %s: "
  2661. "consensus_set=%d, last_interval=%d",
  2662. tbuf, current_consensus?1:0, (int)last_consensus_interval);
  2663. }
  2664. v3_out->fresh_until = v3_out->valid_after + timing.vote_interval;
  2665. v3_out->valid_until = v3_out->valid_after +
  2666. (timing.vote_interval * timing.n_intervals_valid);
  2667. v3_out->vote_seconds = timing.vote_delay;
  2668. v3_out->dist_seconds = timing.dist_delay;
  2669. tor_assert(v3_out->vote_seconds > 0);
  2670. tor_assert(v3_out->dist_seconds > 0);
  2671. tor_assert(timing.n_intervals_valid > 0);
  2672. v3_out->client_versions = client_versions;
  2673. v3_out->server_versions = server_versions;
  2674. /* These are hardwired, to avoid disaster. */
  2675. v3_out->recommended_relay_protocols =
  2676. tor_strdup("Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
  2677. "Link=4 LinkAuth=1 Microdesc=1-2 Relay=2");
  2678. v3_out->recommended_client_protocols =
  2679. tor_strdup("Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
  2680. "Link=4 LinkAuth=1 Microdesc=1-2 Relay=2");
  2681. v3_out->required_client_protocols =
  2682. tor_strdup("Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
  2683. "Link=4 LinkAuth=1 Microdesc=1-2 Relay=2");
  2684. v3_out->required_relay_protocols =
  2685. tor_strdup("Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
  2686. "Link=3-4 LinkAuth=1 Microdesc=1 Relay=1-2");
  2687. /* We are not allowed to vote to require anything we don't have. */
  2688. tor_assert(protover_all_supported(v3_out->required_relay_protocols, NULL));
  2689. tor_assert(protover_all_supported(v3_out->required_client_protocols, NULL));
  2690. /* We should not recommend anything we don't have. */
  2691. tor_assert_nonfatal(protover_all_supported(
  2692. v3_out->recommended_relay_protocols, NULL));
  2693. tor_assert_nonfatal(protover_all_supported(
  2694. v3_out->recommended_client_protocols, NULL));
  2695. v3_out->package_lines = smartlist_new();
  2696. {
  2697. config_line_t *cl;
  2698. for (cl = get_options()->RecommendedPackages; cl; cl = cl->next) {
  2699. if (validate_recommended_package_line(cl->value))
  2700. smartlist_add(v3_out->package_lines, tor_strdup(cl->value));
  2701. }
  2702. }
  2703. v3_out->known_flags = smartlist_new();
  2704. smartlist_split_string(v3_out->known_flags,
  2705. "Authority Exit Fast Guard Stable V2Dir Valid HSDir",
  2706. 0, SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  2707. if (vote_on_reachability)
  2708. smartlist_add(v3_out->known_flags, tor_strdup("Running"));
  2709. if (listbadexits)
  2710. smartlist_add(v3_out->known_flags, tor_strdup("BadExit"));
  2711. smartlist_sort_strings(v3_out->known_flags);
  2712. if (options->ConsensusParams) {
  2713. v3_out->net_params = smartlist_new();
  2714. smartlist_split_string(v3_out->net_params,
  2715. options->ConsensusParams, NULL, 0, 0);
  2716. smartlist_sort_strings(v3_out->net_params);
  2717. }
  2718. voter = tor_malloc_zero(sizeof(networkstatus_voter_info_t));
  2719. voter->nickname = tor_strdup(options->Nickname);
  2720. memcpy(voter->identity_digest, identity_digest, DIGEST_LEN);
  2721. voter->sigs = smartlist_new();
  2722. voter->address = hostname;
  2723. voter->addr = addr;
  2724. voter->dir_port = router_get_advertised_dir_port(options, 0);
  2725. voter->or_port = router_get_advertised_or_port(options);
  2726. voter->contact = tor_strdup(contact);
  2727. if (options->V3AuthUseLegacyKey) {
  2728. authority_cert_t *c = get_my_v3_legacy_cert();
  2729. if (c) {
  2730. if (crypto_pk_get_digest(c->identity_key, voter->legacy_id_digest)) {
  2731. log_warn(LD_BUG, "Unable to compute digest of legacy v3 identity key");
  2732. memset(voter->legacy_id_digest, 0, DIGEST_LEN);
  2733. }
  2734. }
  2735. }
  2736. v3_out->voters = smartlist_new();
  2737. smartlist_add(v3_out->voters, voter);
  2738. v3_out->cert = authority_cert_dup(cert);
  2739. v3_out->routerstatus_list = routerstatuses;
  2740. /* Note: networkstatus_digest is unset; it won't get set until we actually
  2741. * format the vote. */
  2742. return v3_out;
  2743. }
  2744. /** As dirserv_get_routerdescs(), but instead of getting signed_descriptor_t
  2745. * pointers, adds copies of digests to fps_out, and doesn't use the
  2746. * /tor/server/ prefix. For a /d/ request, adds descriptor digests; for other
  2747. * requests, adds identity digests.
  2748. */
  2749. int
  2750. dirserv_get_routerdesc_fingerprints(smartlist_t *fps_out, const char *key,
  2751. const char **msg, int for_unencrypted_conn,
  2752. int is_extrainfo)
  2753. {
  2754. int by_id = 1;
  2755. *msg = NULL;
  2756. if (!strcmp(key, "all")) {
  2757. routerlist_t *rl = router_get_routerlist();
  2758. SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
  2759. smartlist_add(fps_out,
  2760. tor_memdup(r->cache_info.identity_digest, DIGEST_LEN)));
  2761. /* Treat "all" requests as if they were unencrypted */
  2762. for_unencrypted_conn = 1;
  2763. } else if (!strcmp(key, "authority")) {
  2764. const routerinfo_t *ri = router_get_my_routerinfo();
  2765. if (ri)
  2766. smartlist_add(fps_out,
  2767. tor_memdup(ri->cache_info.identity_digest, DIGEST_LEN));
  2768. } else if (!strcmpstart(key, "d/")) {
  2769. by_id = 0;
  2770. key += strlen("d/");
  2771. dir_split_resource_into_fingerprints(key, fps_out, NULL,
  2772. DSR_HEX|DSR_SORT_UNIQ);
  2773. } else if (!strcmpstart(key, "fp/")) {
  2774. key += strlen("fp/");
  2775. dir_split_resource_into_fingerprints(key, fps_out, NULL,
  2776. DSR_HEX|DSR_SORT_UNIQ);
  2777. } else {
  2778. *msg = "Key not recognized";
  2779. return -1;
  2780. }
  2781. if (for_unencrypted_conn) {
  2782. /* Remove anything that insists it not be sent unencrypted. */
  2783. SMARTLIST_FOREACH_BEGIN(fps_out, char *, cp) {
  2784. const signed_descriptor_t *sd;
  2785. if (by_id)
  2786. sd = get_signed_descriptor_by_fp(cp,is_extrainfo,0);
  2787. else if (is_extrainfo)
  2788. sd = extrainfo_get_by_descriptor_digest(cp);
  2789. else
  2790. sd = router_get_by_descriptor_digest(cp);
  2791. if (sd && !sd->send_unencrypted) {
  2792. tor_free(cp);
  2793. SMARTLIST_DEL_CURRENT(fps_out, cp);
  2794. }
  2795. } SMARTLIST_FOREACH_END(cp);
  2796. }
  2797. if (!smartlist_len(fps_out)) {
  2798. *msg = "Servers unavailable";
  2799. return -1;
  2800. }
  2801. return 0;
  2802. }
  2803. /** Add a signed_descriptor_t to <b>descs_out</b> for each router matching
  2804. * <b>key</b>. The key should be either
  2805. * - "/tor/server/authority" for our own routerinfo;
  2806. * - "/tor/server/all" for all the routerinfos we have, concatenated;
  2807. * - "/tor/server/fp/FP" where FP is a plus-separated sequence of
  2808. * hex identity digests; or
  2809. * - "/tor/server/d/D" where D is a plus-separated sequence
  2810. * of server descriptor digests, in hex.
  2811. *
  2812. * Return 0 if we found some matching descriptors, or -1 if we do not
  2813. * have any descriptors, no matching descriptors, or if we did not
  2814. * recognize the key (URL).
  2815. * If -1 is returned *<b>msg</b> will be set to an appropriate error
  2816. * message.
  2817. *
  2818. * XXXX rename this function. It's only called from the controller.
  2819. * XXXX in fact, refactor this function, merging as much as possible.
  2820. */
  2821. int
  2822. dirserv_get_routerdescs(smartlist_t *descs_out, const char *key,
  2823. const char **msg)
  2824. {
  2825. *msg = NULL;
  2826. if (!strcmp(key, "/tor/server/all")) {
  2827. routerlist_t *rl = router_get_routerlist();
  2828. SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
  2829. smartlist_add(descs_out, &(r->cache_info)));
  2830. } else if (!strcmp(key, "/tor/server/authority")) {
  2831. const routerinfo_t *ri = router_get_my_routerinfo();
  2832. if (ri)
  2833. smartlist_add(descs_out, (void*) &(ri->cache_info));
  2834. } else if (!strcmpstart(key, "/tor/server/d/")) {
  2835. smartlist_t *digests = smartlist_new();
  2836. key += strlen("/tor/server/d/");
  2837. dir_split_resource_into_fingerprints(key, digests, NULL,
  2838. DSR_HEX|DSR_SORT_UNIQ);
  2839. SMARTLIST_FOREACH(digests, const char *, d,
  2840. {
  2841. signed_descriptor_t *sd = router_get_by_descriptor_digest(d);
  2842. if (sd)
  2843. smartlist_add(descs_out,sd);
  2844. });
  2845. SMARTLIST_FOREACH(digests, char *, d, tor_free(d));
  2846. smartlist_free(digests);
  2847. } else if (!strcmpstart(key, "/tor/server/fp/")) {
  2848. smartlist_t *digests = smartlist_new();
  2849. time_t cutoff = time(NULL) - ROUTER_MAX_AGE_TO_PUBLISH;
  2850. key += strlen("/tor/server/fp/");
  2851. dir_split_resource_into_fingerprints(key, digests, NULL,
  2852. DSR_HEX|DSR_SORT_UNIQ);
  2853. SMARTLIST_FOREACH_BEGIN(digests, const char *, d) {
  2854. if (router_digest_is_me(d)) {
  2855. /* calling router_get_my_routerinfo() to make sure it exists */
  2856. const routerinfo_t *ri = router_get_my_routerinfo();
  2857. if (ri)
  2858. smartlist_add(descs_out, (void*) &(ri->cache_info));
  2859. } else {
  2860. const routerinfo_t *ri = router_get_by_id_digest(d);
  2861. /* Don't actually serve a descriptor that everyone will think is
  2862. * expired. This is an (ugly) workaround to keep buggy 0.1.1.10
  2863. * Tors from downloading descriptors that they will throw away.
  2864. */
  2865. if (ri && ri->cache_info.published_on > cutoff)
  2866. smartlist_add(descs_out, (void*) &(ri->cache_info));
  2867. }
  2868. } SMARTLIST_FOREACH_END(d);
  2869. SMARTLIST_FOREACH(digests, char *, d, tor_free(d));
  2870. smartlist_free(digests);
  2871. } else {
  2872. *msg = "Key not recognized";
  2873. return -1;
  2874. }
  2875. if (!smartlist_len(descs_out)) {
  2876. *msg = "Servers unavailable";
  2877. return -1;
  2878. }
  2879. return 0;
  2880. }
  2881. /** Called when a TLS handshake has completed successfully with a
  2882. * router listening at <b>address</b>:<b>or_port</b>, and has yielded
  2883. * a certificate with digest <b>digest_rcvd</b>.
  2884. *
  2885. * Inform the reachability checker that we could get to this relay.
  2886. */
  2887. void
  2888. dirserv_orconn_tls_done(const tor_addr_t *addr,
  2889. uint16_t or_port,
  2890. const char *digest_rcvd)
  2891. {
  2892. node_t *node = NULL;
  2893. tor_addr_port_t orport;
  2894. routerinfo_t *ri = NULL;
  2895. time_t now = time(NULL);
  2896. tor_assert(addr);
  2897. tor_assert(digest_rcvd);
  2898. node = node_get_mutable_by_id(digest_rcvd);
  2899. if (node == NULL || node->ri == NULL)
  2900. return;
  2901. ri = node->ri;
  2902. tor_addr_copy(&orport.addr, addr);
  2903. orport.port = or_port;
  2904. if (router_has_orport(ri, &orport)) {
  2905. /* Found the right router. */
  2906. if (!authdir_mode_bridge(get_options()) ||
  2907. ri->purpose == ROUTER_PURPOSE_BRIDGE) {
  2908. char addrstr[TOR_ADDR_BUF_LEN];
  2909. /* This is a bridge or we're not a bridge authorititative --
  2910. mark it as reachable. */
  2911. log_info(LD_DIRSERV, "Found router %s to be reachable at %s:%d. Yay.",
  2912. router_describe(ri),
  2913. tor_addr_to_str(addrstr, addr, sizeof(addrstr), 1),
  2914. ri->or_port);
  2915. if (tor_addr_family(addr) == AF_INET) {
  2916. rep_hist_note_router_reachable(digest_rcvd, addr, or_port, now);
  2917. node->last_reachable = now;
  2918. } else if (tor_addr_family(addr) == AF_INET6) {
  2919. /* No rephist for IPv6. */
  2920. node->last_reachable6 = now;
  2921. }
  2922. }
  2923. }
  2924. }
  2925. /** Called when we, as an authority, receive a new router descriptor either as
  2926. * an upload or a download. Used to decide whether to relaunch reachability
  2927. * testing for the server. */
  2928. int
  2929. dirserv_should_launch_reachability_test(const routerinfo_t *ri,
  2930. const routerinfo_t *ri_old)
  2931. {
  2932. if (!authdir_mode_handles_descs(get_options(), ri->purpose))
  2933. return 0;
  2934. if (!ri_old) {
  2935. /* New router: Launch an immediate reachability test, so we will have an
  2936. * opinion soon in case we're generating a consensus soon */
  2937. return 1;
  2938. }
  2939. if (ri_old->is_hibernating && !ri->is_hibernating) {
  2940. /* It just came out of hibernation; launch a reachability test */
  2941. return 1;
  2942. }
  2943. if (! routers_have_same_or_addrs(ri, ri_old)) {
  2944. /* Address or port changed; launch a reachability test */
  2945. return 1;
  2946. }
  2947. return 0;
  2948. }
  2949. /** Helper function for dirserv_test_reachability(). Start a TLS
  2950. * connection to <b>router</b>, and annotate it with when we started
  2951. * the test. */
  2952. void
  2953. dirserv_single_reachability_test(time_t now, routerinfo_t *router)
  2954. {
  2955. channel_t *chan = NULL;
  2956. node_t *node = NULL;
  2957. tor_addr_t router_addr;
  2958. (void) now;
  2959. tor_assert(router);
  2960. node = node_get_mutable_by_id(router->cache_info.identity_digest);
  2961. tor_assert(node);
  2962. /* IPv4. */
  2963. log_debug(LD_OR,"Testing reachability of %s at %s:%u.",
  2964. router->nickname, fmt_addr32(router->addr), router->or_port);
  2965. tor_addr_from_ipv4h(&router_addr, router->addr);
  2966. chan = channel_tls_connect(&router_addr, router->or_port,
  2967. router->cache_info.identity_digest);
  2968. if (chan) command_setup_channel(chan);
  2969. /* Possible IPv6. */
  2970. if (get_options()->AuthDirHasIPv6Connectivity == 1 &&
  2971. !tor_addr_is_null(&router->ipv6_addr)) {
  2972. char addrstr[TOR_ADDR_BUF_LEN];
  2973. log_debug(LD_OR, "Testing reachability of %s at %s:%u.",
  2974. router->nickname,
  2975. tor_addr_to_str(addrstr, &router->ipv6_addr, sizeof(addrstr), 1),
  2976. router->ipv6_orport);
  2977. chan = channel_tls_connect(&router->ipv6_addr, router->ipv6_orport,
  2978. router->cache_info.identity_digest);
  2979. if (chan) command_setup_channel(chan);
  2980. }
  2981. }
  2982. /** Auth dir server only: load balance such that we only
  2983. * try a few connections per call.
  2984. *
  2985. * The load balancing is such that if we get called once every ten
  2986. * seconds, we will cycle through all the tests in
  2987. * REACHABILITY_TEST_CYCLE_PERIOD seconds (a bit over 20 minutes).
  2988. */
  2989. void
  2990. dirserv_test_reachability(time_t now)
  2991. {
  2992. /* XXX decide what to do here; see or-talk thread "purging old router
  2993. * information, revocation." -NM
  2994. * We can't afford to mess with this in 0.1.2.x. The reason is that
  2995. * if we stop doing reachability tests on some of routerlist, then
  2996. * we'll for-sure think they're down, which may have unexpected
  2997. * effects in other parts of the code. It doesn't hurt much to do
  2998. * the testing, and directory authorities are easy to upgrade. Let's
  2999. * wait til 0.2.0. -RD */
  3000. // time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
  3001. routerlist_t *rl = router_get_routerlist();
  3002. static char ctr = 0;
  3003. int bridge_auth = authdir_mode_bridge(get_options());
  3004. SMARTLIST_FOREACH_BEGIN(rl->routers, routerinfo_t *, router) {
  3005. const char *id_digest = router->cache_info.identity_digest;
  3006. if (router_is_me(router))
  3007. continue;
  3008. if (bridge_auth && router->purpose != ROUTER_PURPOSE_BRIDGE)
  3009. continue; /* bridge authorities only test reachability on bridges */
  3010. // if (router->cache_info.published_on > cutoff)
  3011. // continue;
  3012. if ((((uint8_t)id_digest[0]) % REACHABILITY_MODULO_PER_TEST) == ctr) {
  3013. dirserv_single_reachability_test(now, router);
  3014. }
  3015. } SMARTLIST_FOREACH_END(router);
  3016. ctr = (ctr + 1) % REACHABILITY_MODULO_PER_TEST; /* increment ctr */
  3017. }
  3018. /** Given a fingerprint <b>fp</b> which is either set if we're looking for a
  3019. * v2 status, or zeroes if we're looking for a v3 status, or a NUL-padded
  3020. * flavor name if we want a flavored v3 status, return a pointer to the
  3021. * appropriate cached dir object, or NULL if there isn't one available. */
  3022. static cached_dir_t *
  3023. lookup_cached_dir_by_fp(const char *fp)
  3024. {
  3025. cached_dir_t *d = NULL;
  3026. if (tor_digest_is_zero(fp) && cached_consensuses) {
  3027. d = strmap_get(cached_consensuses, "ns");
  3028. } else if (memchr(fp, '\0', DIGEST_LEN) && cached_consensuses &&
  3029. (d = strmap_get(cached_consensuses, fp))) {
  3030. /* this here interface is a nasty hack XXXX */;
  3031. }
  3032. return d;
  3033. }
  3034. /** Remove from <b>fps</b> every networkstatus key where both
  3035. * a) we have a networkstatus document and
  3036. * b) it is not newer than <b>cutoff</b>.
  3037. *
  3038. * Return 1 if any items were present at all; else return 0.
  3039. */
  3040. int
  3041. dirserv_remove_old_statuses(smartlist_t *fps, time_t cutoff)
  3042. {
  3043. int found_any = 0;
  3044. SMARTLIST_FOREACH_BEGIN(fps, char *, digest) {
  3045. cached_dir_t *d = lookup_cached_dir_by_fp(digest);
  3046. if (!d)
  3047. continue;
  3048. found_any = 1;
  3049. if (d->published <= cutoff) {
  3050. tor_free(digest);
  3051. SMARTLIST_DEL_CURRENT(fps, digest);
  3052. }
  3053. } SMARTLIST_FOREACH_END(digest);
  3054. return found_any;
  3055. }
  3056. /** Return the cache-info for identity fingerprint <b>fp</b>, or
  3057. * its extra-info document if <b>extrainfo</b> is true. Return
  3058. * NULL if not found or if the descriptor is older than
  3059. * <b>publish_cutoff</b>. */
  3060. static const signed_descriptor_t *
  3061. get_signed_descriptor_by_fp(const char *fp, int extrainfo,
  3062. time_t publish_cutoff)
  3063. {
  3064. if (router_digest_is_me(fp)) {
  3065. if (extrainfo)
  3066. return &(router_get_my_extrainfo()->cache_info);
  3067. else
  3068. return &(router_get_my_routerinfo()->cache_info);
  3069. } else {
  3070. const routerinfo_t *ri = router_get_by_id_digest(fp);
  3071. if (ri &&
  3072. ri->cache_info.published_on > publish_cutoff) {
  3073. if (extrainfo)
  3074. return extrainfo_get_by_descriptor_digest(
  3075. ri->cache_info.extra_info_digest);
  3076. else
  3077. return &ri->cache_info;
  3078. }
  3079. }
  3080. return NULL;
  3081. }
  3082. /** Return true iff we have any of the documents (extrainfo or routerdesc)
  3083. * specified by the fingerprints in <b>fps</b> and <b>spool_src</b>. Used to
  3084. * decide whether to send a 404. */
  3085. int
  3086. dirserv_have_any_serverdesc(smartlist_t *fps, int spool_src)
  3087. {
  3088. time_t publish_cutoff = time(NULL)-ROUTER_MAX_AGE_TO_PUBLISH;
  3089. SMARTLIST_FOREACH_BEGIN(fps, const char *, fp) {
  3090. switch (spool_src)
  3091. {
  3092. case DIR_SPOOL_EXTRA_BY_DIGEST:
  3093. if (extrainfo_get_by_descriptor_digest(fp)) return 1;
  3094. break;
  3095. case DIR_SPOOL_SERVER_BY_DIGEST:
  3096. if (router_get_by_descriptor_digest(fp)) return 1;
  3097. break;
  3098. case DIR_SPOOL_EXTRA_BY_FP:
  3099. case DIR_SPOOL_SERVER_BY_FP:
  3100. if (get_signed_descriptor_by_fp(fp,
  3101. spool_src == DIR_SPOOL_EXTRA_BY_FP, publish_cutoff))
  3102. return 1;
  3103. break;
  3104. }
  3105. } SMARTLIST_FOREACH_END(fp);
  3106. return 0;
  3107. }
  3108. /** Return true iff any of the 256-bit elements in <b>fps</b> is the digest of
  3109. * a microdescriptor we have. */
  3110. int
  3111. dirserv_have_any_microdesc(const smartlist_t *fps)
  3112. {
  3113. microdesc_cache_t *cache = get_microdesc_cache();
  3114. SMARTLIST_FOREACH(fps, const char *, fp,
  3115. if (microdesc_cache_lookup_by_digest256(cache, fp))
  3116. return 1);
  3117. return 0;
  3118. }
  3119. /** Return an approximate estimate of the number of bytes that will
  3120. * be needed to transmit the server descriptors (if is_serverdescs --
  3121. * they can be either d/ or fp/ queries) or networkstatus objects (if
  3122. * !is_serverdescs) listed in <b>fps</b>. If <b>compressed</b> is set,
  3123. * we guess how large the data will be after compression.
  3124. *
  3125. * The return value is an estimate; it might be larger or smaller.
  3126. **/
  3127. size_t
  3128. dirserv_estimate_data_size(smartlist_t *fps, int is_serverdescs,
  3129. int compressed)
  3130. {
  3131. size_t result;
  3132. tor_assert(fps);
  3133. if (is_serverdescs) {
  3134. int n = smartlist_len(fps);
  3135. const routerinfo_t *me = router_get_my_routerinfo();
  3136. result = (me?me->cache_info.signed_descriptor_len:2048) * n;
  3137. if (compressed)
  3138. result /= 2; /* observed compressibility is between 35 and 55%. */
  3139. } else {
  3140. result = 0;
  3141. SMARTLIST_FOREACH(fps, const char *, digest, {
  3142. cached_dir_t *dir = lookup_cached_dir_by_fp(digest);
  3143. if (dir)
  3144. result += compressed ? dir->dir_z_len : dir->dir_len;
  3145. });
  3146. }
  3147. return result;
  3148. }
  3149. /** Given a list of microdescriptor hashes, guess how many bytes will be
  3150. * needed to transmit them, and return the guess. */
  3151. size_t
  3152. dirserv_estimate_microdesc_size(const smartlist_t *fps, int compressed)
  3153. {
  3154. size_t result = smartlist_len(fps) * microdesc_average_size(NULL);
  3155. if (compressed)
  3156. result /= 2;
  3157. return result;
  3158. }
  3159. /** When we're spooling data onto our outbuf, add more whenever we dip
  3160. * below this threshold. */
  3161. #define DIRSERV_BUFFER_MIN 16384
  3162. /** Spooling helper: called when we have no more data to spool to <b>conn</b>.
  3163. * Flushes any remaining data to be (un)compressed, and changes the spool
  3164. * source to NONE. Returns 0 on success, negative on failure. */
  3165. static int
  3166. connection_dirserv_finish_spooling(dir_connection_t *conn)
  3167. {
  3168. if (conn->zlib_state) {
  3169. connection_write_to_buf_zlib("", 0, conn, 1);
  3170. tor_zlib_free(conn->zlib_state);
  3171. conn->zlib_state = NULL;
  3172. }
  3173. conn->dir_spool_src = DIR_SPOOL_NONE;
  3174. return 0;
  3175. }
  3176. /** Spooling helper: called when we're sending a bunch of server descriptors,
  3177. * and the outbuf has become too empty. Pulls some entries from
  3178. * fingerprint_stack, and writes the corresponding servers onto outbuf. If we
  3179. * run out of entries, flushes the zlib state and sets the spool source to
  3180. * NONE. Returns 0 on success, negative on failure.
  3181. */
  3182. static int
  3183. connection_dirserv_add_servers_to_outbuf(dir_connection_t *conn)
  3184. {
  3185. int by_fp = (conn->dir_spool_src == DIR_SPOOL_SERVER_BY_FP ||
  3186. conn->dir_spool_src == DIR_SPOOL_EXTRA_BY_FP);
  3187. int extra = (conn->dir_spool_src == DIR_SPOOL_EXTRA_BY_FP ||
  3188. conn->dir_spool_src == DIR_SPOOL_EXTRA_BY_DIGEST);
  3189. time_t publish_cutoff = time(NULL)-ROUTER_MAX_AGE_TO_PUBLISH;
  3190. const or_options_t *options = get_options();
  3191. while (smartlist_len(conn->fingerprint_stack) &&
  3192. connection_get_outbuf_len(TO_CONN(conn)) < DIRSERV_BUFFER_MIN) {
  3193. const char *body;
  3194. char *fp = smartlist_pop_last(conn->fingerprint_stack);
  3195. const signed_descriptor_t *sd = NULL;
  3196. if (by_fp) {
  3197. sd = get_signed_descriptor_by_fp(fp, extra, publish_cutoff);
  3198. } else {
  3199. sd = extra ? extrainfo_get_by_descriptor_digest(fp)
  3200. : router_get_by_descriptor_digest(fp);
  3201. }
  3202. tor_free(fp);
  3203. if (!sd)
  3204. continue;
  3205. if (!connection_dir_is_encrypted(conn) && !sd->send_unencrypted) {
  3206. /* we did this check once before (so we could have an accurate size
  3207. * estimate and maybe send a 404 if somebody asked for only bridges on a
  3208. * connection), but we need to do it again in case a previously
  3209. * unknown bridge descriptor has shown up between then and now. */
  3210. continue;
  3211. }
  3212. /** If we are the bridge authority and the descriptor is a bridge
  3213. * descriptor, remember that we served this descriptor for desc stats. */
  3214. if (options->BridgeAuthoritativeDir && by_fp) {
  3215. const routerinfo_t *router =
  3216. router_get_by_id_digest(sd->identity_digest);
  3217. /* router can be NULL here when the bridge auth is asked for its own
  3218. * descriptor. */
  3219. if (router && router->purpose == ROUTER_PURPOSE_BRIDGE)
  3220. rep_hist_note_desc_served(sd->identity_digest);
  3221. }
  3222. body = signed_descriptor_get_body(sd);
  3223. if (conn->zlib_state) {
  3224. int last = ! smartlist_len(conn->fingerprint_stack);
  3225. connection_write_to_buf_zlib(body, sd->signed_descriptor_len, conn,
  3226. last);
  3227. if (last) {
  3228. tor_zlib_free(conn->zlib_state);
  3229. conn->zlib_state = NULL;
  3230. }
  3231. } else {
  3232. connection_write_to_buf(body,
  3233. sd->signed_descriptor_len,
  3234. TO_CONN(conn));
  3235. }
  3236. }
  3237. if (!smartlist_len(conn->fingerprint_stack)) {
  3238. /* We just wrote the last one; finish up. */
  3239. if (conn->zlib_state) {
  3240. connection_write_to_buf_zlib("", 0, conn, 1);
  3241. tor_zlib_free(conn->zlib_state);
  3242. conn->zlib_state = NULL;
  3243. }
  3244. conn->dir_spool_src = DIR_SPOOL_NONE;
  3245. smartlist_free(conn->fingerprint_stack);
  3246. conn->fingerprint_stack = NULL;
  3247. }
  3248. return 0;
  3249. }
  3250. /** Spooling helper: called when we're sending a bunch of microdescriptors,
  3251. * and the outbuf has become too empty. Pulls some entries from
  3252. * fingerprint_stack, and writes the corresponding microdescs onto outbuf. If
  3253. * we run out of entries, flushes the zlib state and sets the spool source to
  3254. * NONE. Returns 0 on success, negative on failure.
  3255. */
  3256. static int
  3257. connection_dirserv_add_microdescs_to_outbuf(dir_connection_t *conn)
  3258. {
  3259. microdesc_cache_t *cache = get_microdesc_cache();
  3260. while (smartlist_len(conn->fingerprint_stack) &&
  3261. connection_get_outbuf_len(TO_CONN(conn)) < DIRSERV_BUFFER_MIN) {
  3262. char *fp256 = smartlist_pop_last(conn->fingerprint_stack);
  3263. microdesc_t *md = microdesc_cache_lookup_by_digest256(cache, fp256);
  3264. tor_free(fp256);
  3265. if (!md || !md->body)
  3266. continue;
  3267. if (conn->zlib_state) {
  3268. int last = !smartlist_len(conn->fingerprint_stack);
  3269. connection_write_to_buf_zlib(md->body, md->bodylen, conn, last);
  3270. if (last) {
  3271. tor_zlib_free(conn->zlib_state);
  3272. conn->zlib_state = NULL;
  3273. }
  3274. } else {
  3275. connection_write_to_buf(md->body, md->bodylen, TO_CONN(conn));
  3276. }
  3277. }
  3278. if (!smartlist_len(conn->fingerprint_stack)) {
  3279. if (conn->zlib_state) {
  3280. connection_write_to_buf_zlib("", 0, conn, 1);
  3281. tor_zlib_free(conn->zlib_state);
  3282. conn->zlib_state = NULL;
  3283. }
  3284. conn->dir_spool_src = DIR_SPOOL_NONE;
  3285. smartlist_free(conn->fingerprint_stack);
  3286. conn->fingerprint_stack = NULL;
  3287. }
  3288. return 0;
  3289. }
  3290. /** Spooling helper: Called when we're sending a directory or networkstatus,
  3291. * and the outbuf has become too empty. Pulls some bytes from
  3292. * <b>conn</b>-\>cached_dir-\>dir_z, uncompresses them if appropriate, and
  3293. * puts them on the outbuf. If we run out of entries, flushes the zlib state
  3294. * and sets the spool source to NONE. Returns 0 on success, negative on
  3295. * failure. */
  3296. static int
  3297. connection_dirserv_add_dir_bytes_to_outbuf(dir_connection_t *conn)
  3298. {
  3299. ssize_t bytes;
  3300. int64_t remaining;
  3301. bytes = DIRSERV_BUFFER_MIN - connection_get_outbuf_len(TO_CONN(conn));
  3302. tor_assert(bytes > 0);
  3303. tor_assert(conn->cached_dir);
  3304. if (bytes < 8192)
  3305. bytes = 8192;
  3306. remaining = conn->cached_dir->dir_z_len - conn->cached_dir_offset;
  3307. if (bytes > remaining)
  3308. bytes = (ssize_t) remaining;
  3309. if (conn->zlib_state) {
  3310. connection_write_to_buf_zlib(
  3311. conn->cached_dir->dir_z + conn->cached_dir_offset,
  3312. bytes, conn, bytes == remaining);
  3313. } else {
  3314. connection_write_to_buf(conn->cached_dir->dir_z + conn->cached_dir_offset,
  3315. bytes, TO_CONN(conn));
  3316. }
  3317. conn->cached_dir_offset += bytes;
  3318. if (conn->cached_dir_offset == (int)conn->cached_dir->dir_z_len) {
  3319. /* We just wrote the last one; finish up. */
  3320. connection_dirserv_finish_spooling(conn);
  3321. cached_dir_decref(conn->cached_dir);
  3322. conn->cached_dir = NULL;
  3323. }
  3324. return 0;
  3325. }
  3326. /** Spooling helper: Called when we're spooling networkstatus objects on
  3327. * <b>conn</b>, and the outbuf has become too empty. If the current
  3328. * networkstatus object (in <b>conn</b>-\>cached_dir) has more data, pull data
  3329. * from there. Otherwise, pop the next fingerprint from fingerprint_stack,
  3330. * and start spooling the next networkstatus. (A digest of all 0 bytes is
  3331. * treated as a request for the current consensus.) If we run out of entries,
  3332. * flushes the zlib state and sets the spool source to NONE. Returns 0 on
  3333. * success, negative on failure. */
  3334. static int
  3335. connection_dirserv_add_networkstatus_bytes_to_outbuf(dir_connection_t *conn)
  3336. {
  3337. while (connection_get_outbuf_len(TO_CONN(conn)) < DIRSERV_BUFFER_MIN) {
  3338. if (conn->cached_dir) {
  3339. int uncompressing = (conn->zlib_state != NULL);
  3340. int r = connection_dirserv_add_dir_bytes_to_outbuf(conn);
  3341. if (conn->dir_spool_src == DIR_SPOOL_NONE) {
  3342. /* add_dir_bytes thinks we're done with the cached_dir. But we
  3343. * may have more cached_dirs! */
  3344. conn->dir_spool_src = DIR_SPOOL_NETWORKSTATUS;
  3345. /* This bit is tricky. If we were uncompressing the last
  3346. * networkstatus, we may need to make a new zlib object to
  3347. * uncompress the next one. */
  3348. if (uncompressing && ! conn->zlib_state &&
  3349. conn->fingerprint_stack &&
  3350. smartlist_len(conn->fingerprint_stack)) {
  3351. conn->zlib_state = tor_zlib_new(0, ZLIB_METHOD, HIGH_COMPRESSION);
  3352. }
  3353. }
  3354. if (r) return r;
  3355. } else if (conn->fingerprint_stack &&
  3356. smartlist_len(conn->fingerprint_stack)) {
  3357. /* Add another networkstatus; start serving it. */
  3358. char *fp = smartlist_pop_last(conn->fingerprint_stack);
  3359. cached_dir_t *d = lookup_cached_dir_by_fp(fp);
  3360. tor_free(fp);
  3361. if (d) {
  3362. ++d->refcnt;
  3363. conn->cached_dir = d;
  3364. conn->cached_dir_offset = 0;
  3365. }
  3366. } else {
  3367. connection_dirserv_finish_spooling(conn);
  3368. smartlist_free(conn->fingerprint_stack);
  3369. conn->fingerprint_stack = NULL;
  3370. return 0;
  3371. }
  3372. }
  3373. return 0;
  3374. }
  3375. /** Called whenever we have flushed some directory data in state
  3376. * SERVER_WRITING. */
  3377. int
  3378. connection_dirserv_flushed_some(dir_connection_t *conn)
  3379. {
  3380. tor_assert(conn->base_.state == DIR_CONN_STATE_SERVER_WRITING);
  3381. if (connection_get_outbuf_len(TO_CONN(conn)) >= DIRSERV_BUFFER_MIN)
  3382. return 0;
  3383. switch (conn->dir_spool_src) {
  3384. case DIR_SPOOL_EXTRA_BY_DIGEST:
  3385. case DIR_SPOOL_EXTRA_BY_FP:
  3386. case DIR_SPOOL_SERVER_BY_DIGEST:
  3387. case DIR_SPOOL_SERVER_BY_FP:
  3388. return connection_dirserv_add_servers_to_outbuf(conn);
  3389. case DIR_SPOOL_MICRODESC:
  3390. return connection_dirserv_add_microdescs_to_outbuf(conn);
  3391. case DIR_SPOOL_CACHED_DIR:
  3392. return connection_dirserv_add_dir_bytes_to_outbuf(conn);
  3393. case DIR_SPOOL_NETWORKSTATUS:
  3394. return connection_dirserv_add_networkstatus_bytes_to_outbuf(conn);
  3395. case DIR_SPOOL_NONE:
  3396. default:
  3397. return 0;
  3398. }
  3399. }
  3400. /** Return true iff <b>line</b> is a valid RecommendedPackages line.
  3401. */
  3402. /*
  3403. The grammar is:
  3404. "package" SP PACKAGENAME SP VERSION SP URL SP DIGESTS NL
  3405. PACKAGENAME = NONSPACE
  3406. VERSION = NONSPACE
  3407. URL = NONSPACE
  3408. DIGESTS = DIGEST | DIGESTS SP DIGEST
  3409. DIGEST = DIGESTTYPE "=" DIGESTVAL
  3410. NONSPACE = one or more non-space printing characters
  3411. DIGESTVAL = DIGESTTYPE = one or more non-=, non-" " characters.
  3412. SP = " "
  3413. NL = a newline
  3414. */
  3415. int
  3416. validate_recommended_package_line(const char *line)
  3417. {
  3418. const char *cp = line;
  3419. #define WORD() \
  3420. do { \
  3421. if (*cp == ' ') \
  3422. return 0; \
  3423. cp = strchr(cp, ' '); \
  3424. if (!cp) \
  3425. return 0; \
  3426. } while (0)
  3427. WORD(); /* skip packagename */
  3428. ++cp;
  3429. WORD(); /* skip version */
  3430. ++cp;
  3431. WORD(); /* Skip URL */
  3432. ++cp;
  3433. /* Skip digesttype=digestval + */
  3434. int n_entries = 0;
  3435. while (1) {
  3436. const char *start_of_word = cp;
  3437. const char *end_of_word = strchr(cp, ' ');
  3438. if (! end_of_word)
  3439. end_of_word = cp + strlen(cp);
  3440. if (start_of_word == end_of_word)
  3441. return 0;
  3442. const char *eq = memchr(start_of_word, '=', end_of_word - start_of_word);
  3443. if (!eq)
  3444. return 0;
  3445. if (eq == start_of_word)
  3446. return 0;
  3447. if (eq == end_of_word - 1)
  3448. return 0;
  3449. if (memchr(eq+1, '=', end_of_word - (eq+1)))
  3450. return 0;
  3451. ++n_entries;
  3452. if (0 == *end_of_word)
  3453. break;
  3454. cp = end_of_word + 1;
  3455. }
  3456. /* If we reach this point, we have at least 1 entry. */
  3457. tor_assert(n_entries > 0);
  3458. return 1;
  3459. }
  3460. /** Release all storage used by the directory server. */
  3461. void
  3462. dirserv_free_all(void)
  3463. {
  3464. dirserv_free_fingerprint_list();
  3465. strmap_free(cached_consensuses, free_cached_dir_);
  3466. cached_consensuses = NULL;
  3467. dirserv_clear_measured_bw_cache();
  3468. }