dirserv.c 134 KB

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