directory.c 176 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2017, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #define DIRECTORY_PRIVATE
  6. #include "or.h"
  7. #include "backtrace.h"
  8. #include "bridges.h"
  9. #include "buffers.h"
  10. #include "circuitbuild.h"
  11. #include "config.h"
  12. #include "connection.h"
  13. #include "connection_edge.h"
  14. #include "control.h"
  15. #include "compat.h"
  16. #define DIRECTORY_PRIVATE
  17. #include "directory.h"
  18. #include "dirserv.h"
  19. #include "dirvote.h"
  20. #include "entrynodes.h"
  21. #include "geoip.h"
  22. #include "hs_cache.h"
  23. #include "hs_common.h"
  24. #include "main.h"
  25. #include "microdesc.h"
  26. #include "networkstatus.h"
  27. #include "nodelist.h"
  28. #include "policies.h"
  29. #include "relay.h"
  30. #include "rendclient.h"
  31. #include "rendcommon.h"
  32. #include "rendservice.h"
  33. #include "rephist.h"
  34. #include "router.h"
  35. #include "routerlist.h"
  36. #include "routerparse.h"
  37. #include "routerset.h"
  38. #include "shared_random.h"
  39. #if defined(EXPORTMALLINFO) && defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
  40. #if !defined(OpenBSD)
  41. #include <malloc.h>
  42. #endif
  43. #endif
  44. /**
  45. * \file directory.c
  46. * \brief Code to send and fetch information from directory authorities and
  47. * caches via HTTP.
  48. *
  49. * Directory caches and authorities use dirserv.c to generate the results of a
  50. * query and stream them to the connection; clients use routerparse.c to parse
  51. * them.
  52. *
  53. * Every directory request has a dir_connection_t on the client side and on
  54. * the server side. In most cases, the dir_connection_t object is a linked
  55. * connection, tunneled through an edge_connection_t so that it can be a
  56. * stream on the Tor network. The only non-tunneled connections are those
  57. * that are used to upload material (descriptors and votes) to authorities.
  58. * Among tunneled connections, some use one-hop circuits, and others use
  59. * multi-hop circuits for anonymity.
  60. *
  61. * Directory requests are launched by calling
  62. * directory_initiate_request(). This
  63. * launch the connection, will construct an HTTP request with
  64. * directory_send_command(), send the and wait for a response. The client
  65. * later handles the response with connection_dir_client_reached_eof(),
  66. * which passes the information received to another part of Tor.
  67. *
  68. * On the server side, requests are read in directory_handle_command(),
  69. * which dispatches first on the request type (GET or POST), and then on
  70. * the URL requested. GET requests are processed with a table-based
  71. * dispatcher in url_table[]. The process of handling larger GET requests
  72. * is complicated because we need to avoid allocating a copy of all the
  73. * data to be sent to the client in one huge buffer. Instead, we spool the
  74. * data into the buffer using logic in connection_dirserv_flushed_some() in
  75. * dirserv.c. (TODO: If we extended buf.c to have a zero-copy
  76. * reference-based buffer type, we could remove most of that code, at the
  77. * cost of a bit more reference counting.)
  78. **/
  79. /* In-points to directory.c:
  80. *
  81. * - directory_post_to_dirservers(), called from
  82. * router_upload_dir_desc_to_dirservers() in router.c
  83. * upload_service_descriptor() in rendservice.c
  84. * - directory_get_from_dirserver(), called from
  85. * rend_client_refetch_renddesc() in rendclient.c
  86. * run_scheduled_events() in main.c
  87. * do_hup() in main.c
  88. * - connection_dir_process_inbuf(), called from
  89. * connection_process_inbuf() in connection.c
  90. * - connection_dir_finished_flushing(), called from
  91. * connection_finished_flushing() in connection.c
  92. * - connection_dir_finished_connecting(), called from
  93. * connection_finished_connecting() in connection.c
  94. */
  95. static void directory_send_command(dir_connection_t *conn,
  96. int purpose, int direct, const char *resource,
  97. const char *payload, size_t payload_len,
  98. time_t if_modified_since);
  99. static int body_is_plausible(const char *body, size_t body_len, int purpose);
  100. static char *http_get_header(const char *headers, const char *which);
  101. static void http_set_address_origin(const char *headers, connection_t *conn);
  102. static void connection_dir_download_routerdesc_failed(dir_connection_t *conn);
  103. static void connection_dir_bridge_routerdesc_failed(dir_connection_t *conn);
  104. static void connection_dir_download_cert_failed(
  105. dir_connection_t *conn, int status_code);
  106. static void connection_dir_retry_bridges(smartlist_t *descs);
  107. static void dir_routerdesc_download_failed(smartlist_t *failed,
  108. int status_code,
  109. int router_purpose,
  110. int was_extrainfo,
  111. int was_descriptor_digests);
  112. static void dir_microdesc_download_failed(smartlist_t *failed,
  113. int status_code);
  114. static int client_likes_consensus(networkstatus_t *v, const char *want_url);
  115. static void connection_dir_close_consensus_fetches(
  116. dir_connection_t *except_this_one, const char *resource);
  117. /********* START VARIABLES **********/
  118. /** How far in the future do we allow a directory server to tell us it is
  119. * before deciding that one of us has the wrong time? */
  120. #define ALLOW_DIRECTORY_TIME_SKEW (30*60)
  121. #define X_ADDRESS_HEADER "X-Your-Address-Is: "
  122. /** HTTP cache control: how long do we tell proxies they can cache each
  123. * kind of document we serve? */
  124. #define FULL_DIR_CACHE_LIFETIME (60*60)
  125. #define RUNNINGROUTERS_CACHE_LIFETIME (20*60)
  126. #define DIRPORTFRONTPAGE_CACHE_LIFETIME (20*60)
  127. #define NETWORKSTATUS_CACHE_LIFETIME (5*60)
  128. #define ROUTERDESC_CACHE_LIFETIME (30*60)
  129. #define ROUTERDESC_BY_DIGEST_CACHE_LIFETIME (48*60*60)
  130. #define ROBOTS_CACHE_LIFETIME (24*60*60)
  131. #define MICRODESC_CACHE_LIFETIME (48*60*60)
  132. /********* END VARIABLES ************/
  133. /** Return false if the directory purpose <b>dir_purpose</b>
  134. * does not require an anonymous (three-hop) connection.
  135. *
  136. * Return true 1) by default, 2) if all directory actions have
  137. * specifically been configured to be over an anonymous connection,
  138. * or 3) if the router is a bridge */
  139. int
  140. purpose_needs_anonymity(uint8_t dir_purpose, uint8_t router_purpose,
  141. const char *resource)
  142. {
  143. if (get_options()->AllDirActionsPrivate)
  144. return 1;
  145. if (router_purpose == ROUTER_PURPOSE_BRIDGE) {
  146. if (dir_purpose == DIR_PURPOSE_FETCH_SERVERDESC
  147. && resource && !strcmp(resource, "authority.z")) {
  148. /* We are asking a bridge for its own descriptor. That doesn't need
  149. anonymity. */
  150. return 0;
  151. }
  152. /* Assume all other bridge stuff needs anonymity. */
  153. return 1; /* if no circuits yet, this might break bootstrapping, but it's
  154. * needed to be safe. */
  155. }
  156. switch (dir_purpose)
  157. {
  158. case DIR_PURPOSE_UPLOAD_DIR:
  159. case DIR_PURPOSE_UPLOAD_VOTE:
  160. case DIR_PURPOSE_UPLOAD_SIGNATURES:
  161. case DIR_PURPOSE_FETCH_STATUS_VOTE:
  162. case DIR_PURPOSE_FETCH_DETACHED_SIGNATURES:
  163. case DIR_PURPOSE_FETCH_CONSENSUS:
  164. case DIR_PURPOSE_FETCH_CERTIFICATE:
  165. case DIR_PURPOSE_FETCH_SERVERDESC:
  166. case DIR_PURPOSE_FETCH_EXTRAINFO:
  167. case DIR_PURPOSE_FETCH_MICRODESC:
  168. return 0;
  169. case DIR_PURPOSE_HAS_FETCHED_RENDDESC_V2:
  170. case DIR_PURPOSE_UPLOAD_RENDDESC_V2:
  171. case DIR_PURPOSE_FETCH_RENDDESC_V2:
  172. return 1;
  173. case DIR_PURPOSE_SERVER:
  174. default:
  175. log_warn(LD_BUG, "Called with dir_purpose=%d, router_purpose=%d",
  176. dir_purpose, router_purpose);
  177. tor_assert_nonfatal_unreached();
  178. return 1; /* Assume it needs anonymity; better safe than sorry. */
  179. }
  180. }
  181. /** Return a newly allocated string describing <b>auth</b>. Only describes
  182. * authority features. */
  183. STATIC char *
  184. authdir_type_to_string(dirinfo_type_t auth)
  185. {
  186. char *result;
  187. smartlist_t *lst = smartlist_new();
  188. if (auth & V3_DIRINFO)
  189. smartlist_add(lst, (void*)"V3");
  190. if (auth & BRIDGE_DIRINFO)
  191. smartlist_add(lst, (void*)"Bridge");
  192. if (smartlist_len(lst)) {
  193. result = smartlist_join_strings(lst, ", ", 0, NULL);
  194. } else {
  195. result = tor_strdup("[Not an authority]");
  196. }
  197. smartlist_free(lst);
  198. return result;
  199. }
  200. /** Return a string describing a given directory connection purpose. */
  201. STATIC const char *
  202. dir_conn_purpose_to_string(int purpose)
  203. {
  204. switch (purpose)
  205. {
  206. case DIR_PURPOSE_UPLOAD_DIR:
  207. return "server descriptor upload";
  208. case DIR_PURPOSE_UPLOAD_VOTE:
  209. return "server vote upload";
  210. case DIR_PURPOSE_UPLOAD_SIGNATURES:
  211. return "consensus signature upload";
  212. case DIR_PURPOSE_FETCH_SERVERDESC:
  213. return "server descriptor fetch";
  214. case DIR_PURPOSE_FETCH_EXTRAINFO:
  215. return "extra-info fetch";
  216. case DIR_PURPOSE_FETCH_CONSENSUS:
  217. return "consensus network-status fetch";
  218. case DIR_PURPOSE_FETCH_CERTIFICATE:
  219. return "authority cert fetch";
  220. case DIR_PURPOSE_FETCH_STATUS_VOTE:
  221. return "status vote fetch";
  222. case DIR_PURPOSE_FETCH_DETACHED_SIGNATURES:
  223. return "consensus signature fetch";
  224. case DIR_PURPOSE_FETCH_RENDDESC_V2:
  225. return "hidden-service v2 descriptor fetch";
  226. case DIR_PURPOSE_UPLOAD_RENDDESC_V2:
  227. return "hidden-service v2 descriptor upload";
  228. case DIR_PURPOSE_FETCH_MICRODESC:
  229. return "microdescriptor fetch";
  230. }
  231. log_warn(LD_BUG, "Called with unknown purpose %d", purpose);
  232. return "(unknown)";
  233. }
  234. /** Return the requisite directory information types. */
  235. STATIC dirinfo_type_t
  236. dir_fetch_type(int dir_purpose, int router_purpose, const char *resource)
  237. {
  238. dirinfo_type_t type;
  239. switch (dir_purpose) {
  240. case DIR_PURPOSE_FETCH_EXTRAINFO:
  241. type = EXTRAINFO_DIRINFO;
  242. if (router_purpose == ROUTER_PURPOSE_BRIDGE)
  243. type |= BRIDGE_DIRINFO;
  244. else
  245. type |= V3_DIRINFO;
  246. break;
  247. case DIR_PURPOSE_FETCH_SERVERDESC:
  248. if (router_purpose == ROUTER_PURPOSE_BRIDGE)
  249. type = BRIDGE_DIRINFO;
  250. else
  251. type = V3_DIRINFO;
  252. break;
  253. case DIR_PURPOSE_FETCH_STATUS_VOTE:
  254. case DIR_PURPOSE_FETCH_DETACHED_SIGNATURES:
  255. case DIR_PURPOSE_FETCH_CERTIFICATE:
  256. type = V3_DIRINFO;
  257. break;
  258. case DIR_PURPOSE_FETCH_CONSENSUS:
  259. type = V3_DIRINFO;
  260. if (resource && !strcmp(resource, "microdesc"))
  261. type |= MICRODESC_DIRINFO;
  262. break;
  263. case DIR_PURPOSE_FETCH_MICRODESC:
  264. type = MICRODESC_DIRINFO;
  265. break;
  266. default:
  267. log_warn(LD_BUG, "Unexpected purpose %d", (int)dir_purpose);
  268. type = NO_DIRINFO;
  269. break;
  270. }
  271. return type;
  272. }
  273. /** Return true iff <b>identity_digest</b> is the digest of a router which
  274. * says that it caches extrainfos. (If <b>is_authority</b> we always
  275. * believe that to be true.) */
  276. int
  277. router_supports_extrainfo(const char *identity_digest, int is_authority)
  278. {
  279. const node_t *node = node_get_by_id(identity_digest);
  280. if (node && node->ri) {
  281. if (node->ri->caches_extra_info)
  282. return 1;
  283. }
  284. if (is_authority) {
  285. return 1;
  286. }
  287. return 0;
  288. }
  289. /** Return true iff any trusted directory authority has accepted our
  290. * server descriptor.
  291. *
  292. * We consider any authority sufficient because waiting for all of
  293. * them means it never happens while any authority is down; we don't
  294. * go for something more complex in the middle (like \>1/3 or \>1/2 or
  295. * \>=1/2) because that doesn't seem necessary yet.
  296. */
  297. int
  298. directories_have_accepted_server_descriptor(void)
  299. {
  300. const smartlist_t *servers = router_get_trusted_dir_servers();
  301. const or_options_t *options = get_options();
  302. SMARTLIST_FOREACH(servers, dir_server_t *, d, {
  303. if ((d->type & options->PublishServerDescriptor_) &&
  304. d->has_accepted_serverdesc) {
  305. return 1;
  306. }
  307. });
  308. return 0;
  309. }
  310. /** Start a connection to every suitable directory authority, using
  311. * connection purpose <b>dir_purpose</b> and uploading <b>payload</b>
  312. * (of length <b>payload_len</b>). The dir_purpose should be one of
  313. * 'DIR_PURPOSE_UPLOAD_{DIR|VOTE|SIGNATURES}'.
  314. *
  315. * <b>router_purpose</b> describes the type of descriptor we're
  316. * publishing, if we're publishing a descriptor -- e.g. general or bridge.
  317. *
  318. * <b>type</b> specifies what sort of dir authorities (V3,
  319. * BRIDGE, etc) we should upload to.
  320. *
  321. * If <b>extrainfo_len</b> is nonzero, the first <b>payload_len</b> bytes of
  322. * <b>payload</b> hold a router descriptor, and the next <b>extrainfo_len</b>
  323. * bytes of <b>payload</b> hold an extra-info document. Upload the descriptor
  324. * to all authorities, and the extra-info document to all authorities that
  325. * support it.
  326. */
  327. void
  328. directory_post_to_dirservers(uint8_t dir_purpose, uint8_t router_purpose,
  329. dirinfo_type_t type,
  330. const char *payload,
  331. size_t payload_len, size_t extrainfo_len)
  332. {
  333. const or_options_t *options = get_options();
  334. dir_indirection_t indirection;
  335. const smartlist_t *dirservers = router_get_trusted_dir_servers();
  336. int found = 0;
  337. const int exclude_self = (dir_purpose == DIR_PURPOSE_UPLOAD_VOTE ||
  338. dir_purpose == DIR_PURPOSE_UPLOAD_SIGNATURES);
  339. tor_assert(dirservers);
  340. /* This tries dirservers which we believe to be down, but ultimately, that's
  341. * harmless, and we may as well err on the side of getting things uploaded.
  342. */
  343. SMARTLIST_FOREACH_BEGIN(dirservers, dir_server_t *, ds) {
  344. routerstatus_t *rs = &(ds->fake_status);
  345. size_t upload_len = payload_len;
  346. if ((type & ds->type) == 0)
  347. continue;
  348. if (exclude_self && router_digest_is_me(ds->digest)) {
  349. /* we don't upload to ourselves, but at least there's now at least
  350. * one authority of this type that has what we wanted to upload. */
  351. found = 1;
  352. continue;
  353. }
  354. if (options->StrictNodes &&
  355. routerset_contains_routerstatus(options->ExcludeNodes, rs, -1)) {
  356. log_warn(LD_DIR, "Wanted to contact authority '%s' for %s, but "
  357. "it's in our ExcludedNodes list and StrictNodes is set. "
  358. "Skipping.",
  359. ds->nickname,
  360. dir_conn_purpose_to_string(dir_purpose));
  361. continue;
  362. }
  363. found = 1; /* at least one authority of this type was listed */
  364. if (dir_purpose == DIR_PURPOSE_UPLOAD_DIR)
  365. ds->has_accepted_serverdesc = 0;
  366. if (extrainfo_len && router_supports_extrainfo(ds->digest, 1)) {
  367. upload_len += extrainfo_len;
  368. log_info(LD_DIR, "Uploading an extrainfo too (length %d)",
  369. (int) extrainfo_len);
  370. }
  371. if (purpose_needs_anonymity(dir_purpose, router_purpose, NULL)) {
  372. indirection = DIRIND_ANONYMOUS;
  373. } else if (!fascist_firewall_allows_dir_server(ds,
  374. FIREWALL_DIR_CONNECTION,
  375. 0)) {
  376. if (fascist_firewall_allows_dir_server(ds, FIREWALL_OR_CONNECTION, 0))
  377. indirection = DIRIND_ONEHOP;
  378. else
  379. indirection = DIRIND_ANONYMOUS;
  380. } else {
  381. indirection = DIRIND_DIRECT_CONN;
  382. }
  383. directory_request_t *req = directory_request_new(dir_purpose);
  384. directory_request_set_routerstatus(req, rs);
  385. directory_request_set_router_purpose(req, router_purpose);
  386. directory_request_set_indirection(req, indirection);
  387. directory_request_set_payload(req, payload, upload_len);
  388. directory_initiate_request(req);
  389. directory_request_free(req);
  390. } SMARTLIST_FOREACH_END(ds);
  391. if (!found) {
  392. char *s = authdir_type_to_string(type);
  393. log_warn(LD_DIR, "Publishing server descriptor to directory authorities "
  394. "of type '%s', but no authorities of that type listed!", s);
  395. tor_free(s);
  396. }
  397. }
  398. /** Return true iff, according to the values in <b>options</b>, we should be
  399. * using directory guards for direct downloads of directory information. */
  400. STATIC int
  401. should_use_directory_guards(const or_options_t *options)
  402. {
  403. /* Public (non-bridge) servers never use directory guards. */
  404. if (public_server_mode(options))
  405. return 0;
  406. /* If guards are disabled, we can't use directory guards.
  407. */
  408. if (!options->UseEntryGuards)
  409. return 0;
  410. /* If we're configured to fetch directory info aggressively or of a
  411. * nonstandard type, don't use directory guards. */
  412. if (options->DownloadExtraInfo || options->FetchDirInfoEarly ||
  413. options->FetchDirInfoExtraEarly || options->FetchUselessDescriptors)
  414. return 0;
  415. return 1;
  416. }
  417. /** Pick an unconstrained directory server from among our guards, the latest
  418. * networkstatus, or the fallback dirservers, for use in downloading
  419. * information of type <b>type</b>, and return its routerstatus. */
  420. static const routerstatus_t *
  421. directory_pick_generic_dirserver(dirinfo_type_t type, int pds_flags,
  422. uint8_t dir_purpose,
  423. circuit_guard_state_t **guard_state_out)
  424. {
  425. const routerstatus_t *rs = NULL;
  426. const or_options_t *options = get_options();
  427. if (options->UseBridges)
  428. log_warn(LD_BUG, "Called when we have UseBridges set.");
  429. if (should_use_directory_guards(options)) {
  430. const node_t *node = guards_choose_dirguard(guard_state_out);
  431. if (node)
  432. rs = node->rs;
  433. } else {
  434. /* anybody with a non-zero dirport will do */
  435. rs = router_pick_directory_server(type, pds_flags);
  436. }
  437. if (!rs) {
  438. log_info(LD_DIR, "No router found for %s; falling back to "
  439. "dirserver list.", dir_conn_purpose_to_string(dir_purpose));
  440. rs = router_pick_fallback_dirserver(type, pds_flags);
  441. }
  442. return rs;
  443. }
  444. /** Start a connection to a random running directory server, using
  445. * connection purpose <b>dir_purpose</b>, intending to fetch descriptors
  446. * of purpose <b>router_purpose</b>, and requesting <b>resource</b>.
  447. * Use <b>pds_flags</b> as arguments to router_pick_directory_server()
  448. * or router_pick_trusteddirserver().
  449. */
  450. MOCK_IMPL(void, directory_get_from_dirserver, (
  451. uint8_t dir_purpose,
  452. uint8_t router_purpose,
  453. const char *resource,
  454. int pds_flags,
  455. download_want_authority_t want_authority))
  456. {
  457. const routerstatus_t *rs = NULL;
  458. const or_options_t *options = get_options();
  459. int prefer_authority = (directory_fetches_from_authorities(options)
  460. || want_authority == DL_WANT_AUTHORITY);
  461. int require_authority = 0;
  462. int get_via_tor = purpose_needs_anonymity(dir_purpose, router_purpose,
  463. resource);
  464. dirinfo_type_t type = dir_fetch_type(dir_purpose, router_purpose, resource);
  465. time_t if_modified_since = 0;
  466. if (type == NO_DIRINFO)
  467. return;
  468. if (dir_purpose == DIR_PURPOSE_FETCH_CONSENSUS) {
  469. int flav = FLAV_NS;
  470. networkstatus_t *v;
  471. if (resource)
  472. flav = networkstatus_parse_flavor_name(resource);
  473. /* DEFAULT_IF_MODIFIED_SINCE_DELAY is 1/20 of the default consensus
  474. * period of 1 hour.
  475. */
  476. #define DEFAULT_IF_MODIFIED_SINCE_DELAY (180)
  477. if (flav != -1) {
  478. /* IF we have a parsed consensus of this type, we can do an
  479. * if-modified-time based on it. */
  480. v = networkstatus_get_latest_consensus_by_flavor(flav);
  481. if (v) {
  482. /* In networks with particularly short V3AuthVotingIntervals,
  483. * ask for the consensus if it's been modified since half the
  484. * V3AuthVotingInterval of the most recent consensus. */
  485. time_t ims_delay = DEFAULT_IF_MODIFIED_SINCE_DELAY;
  486. if (v->fresh_until > v->valid_after
  487. && ims_delay > (v->fresh_until - v->valid_after)/2) {
  488. ims_delay = (v->fresh_until - v->valid_after)/2;
  489. }
  490. if_modified_since = v->valid_after + ims_delay;
  491. }
  492. } else {
  493. /* Otherwise it might be a consensus we don't parse, but which we
  494. * do cache. Look at the cached copy, perhaps. */
  495. cached_dir_t *cd = dirserv_get_consensus(resource);
  496. /* We have no method of determining the voting interval from an
  497. * unparsed consensus, so we use the default. */
  498. if (cd)
  499. if_modified_since = cd->published + DEFAULT_IF_MODIFIED_SINCE_DELAY;
  500. }
  501. }
  502. if (!options->FetchServerDescriptors)
  503. return;
  504. circuit_guard_state_t *guard_state = NULL;
  505. if (!get_via_tor) {
  506. if (options->UseBridges && !(type & BRIDGE_DIRINFO)) {
  507. /* We want to ask a running bridge for which we have a descriptor.
  508. *
  509. * When we ask choose_random_entry() for a bridge, we specify what
  510. * sort of dir fetch we'll be doing, so it won't return a bridge
  511. * that can't answer our question.
  512. */
  513. const node_t *node = guards_choose_dirguard(&guard_state);
  514. if (node && node->ri) {
  515. /* every bridge has a routerinfo. */
  516. routerinfo_t *ri = node->ri;
  517. /* clients always make OR connections to bridges */
  518. tor_addr_port_t or_ap;
  519. directory_request_t *req = directory_request_new(dir_purpose);
  520. /* we are willing to use a non-preferred address if we need to */
  521. fascist_firewall_choose_address_node(node, FIREWALL_OR_CONNECTION, 0,
  522. &or_ap);
  523. directory_request_set_or_addr_port(req, &or_ap);
  524. directory_request_set_directory_id_digest(req,
  525. ri->cache_info.identity_digest);
  526. directory_request_set_router_purpose(req, router_purpose);
  527. directory_request_set_resource(req, resource);
  528. directory_request_set_if_modified_since(req, if_modified_since);
  529. directory_request_set_guard_state(req, guard_state);
  530. directory_initiate_request(req);
  531. directory_request_free(req);
  532. } else {
  533. if (guard_state) {
  534. entry_guard_cancel(&guard_state);
  535. }
  536. log_notice(LD_DIR, "Ignoring directory request, since no bridge "
  537. "nodes are available yet.");
  538. }
  539. return;
  540. } else {
  541. if (prefer_authority || (type & BRIDGE_DIRINFO)) {
  542. /* only ask authdirservers, and don't ask myself */
  543. rs = router_pick_trusteddirserver(type, pds_flags);
  544. if (rs == NULL && (pds_flags & (PDS_NO_EXISTING_SERVERDESC_FETCH|
  545. PDS_NO_EXISTING_MICRODESC_FETCH))) {
  546. /* We don't want to fetch from any authorities that we're currently
  547. * fetching server descriptors from, and we got no match. Did we
  548. * get no match because all the authorities have connections
  549. * fetching server descriptors (in which case we should just
  550. * return,) or because all the authorities are down or on fire or
  551. * unreachable or something (in which case we should go on with
  552. * our fallback code)? */
  553. pds_flags &= ~(PDS_NO_EXISTING_SERVERDESC_FETCH|
  554. PDS_NO_EXISTING_MICRODESC_FETCH);
  555. rs = router_pick_trusteddirserver(type, pds_flags);
  556. if (rs) {
  557. log_debug(LD_DIR, "Deferring serverdesc fetch: all authorities "
  558. "are in use.");
  559. return;
  560. }
  561. }
  562. if (rs == NULL && require_authority) {
  563. log_info(LD_DIR, "No authorities were available for %s: will try "
  564. "later.", dir_conn_purpose_to_string(dir_purpose));
  565. return;
  566. }
  567. }
  568. if (!rs && !(type & BRIDGE_DIRINFO)) {
  569. rs = directory_pick_generic_dirserver(type, pds_flags,
  570. dir_purpose,
  571. &guard_state);
  572. if (!rs)
  573. get_via_tor = 1; /* last resort: try routing it via Tor */
  574. }
  575. }
  576. }
  577. if (get_via_tor) {
  578. /* Never use fascistfirewall; we're going via Tor. */
  579. pds_flags |= PDS_IGNORE_FASCISTFIREWALL;
  580. rs = router_pick_directory_server(type, pds_flags);
  581. }
  582. /* If we have any hope of building an indirect conn, we know some router
  583. * descriptors. If (rs==NULL), we can't build circuits anyway, so
  584. * there's no point in falling back to the authorities in this case. */
  585. if (rs) {
  586. const dir_indirection_t indirection =
  587. get_via_tor ? DIRIND_ANONYMOUS : DIRIND_ONEHOP;
  588. directory_request_t *req = directory_request_new(dir_purpose);
  589. directory_request_set_routerstatus(req, rs);
  590. directory_request_set_router_purpose(req, router_purpose);
  591. directory_request_set_indirection(req, indirection);
  592. directory_request_set_resource(req, resource);
  593. directory_request_set_if_modified_since(req, if_modified_since);
  594. if (guard_state)
  595. directory_request_set_guard_state(req, guard_state);
  596. directory_initiate_request(req);
  597. directory_request_free(req);
  598. } else {
  599. log_notice(LD_DIR,
  600. "While fetching directory info, "
  601. "no running dirservers known. Will try again later. "
  602. "(purpose %d)", dir_purpose);
  603. if (!purpose_needs_anonymity(dir_purpose, router_purpose, resource)) {
  604. /* remember we tried them all and failed. */
  605. directory_all_unreachable(time(NULL));
  606. }
  607. }
  608. }
  609. /** As directory_get_from_dirserver, but initiates a request to <i>every</i>
  610. * directory authority other than ourself. Only for use by authorities when
  611. * searching for missing information while voting. */
  612. void
  613. directory_get_from_all_authorities(uint8_t dir_purpose,
  614. uint8_t router_purpose,
  615. const char *resource)
  616. {
  617. tor_assert(dir_purpose == DIR_PURPOSE_FETCH_STATUS_VOTE ||
  618. dir_purpose == DIR_PURPOSE_FETCH_DETACHED_SIGNATURES);
  619. SMARTLIST_FOREACH_BEGIN(router_get_trusted_dir_servers(),
  620. dir_server_t *, ds) {
  621. if (router_digest_is_me(ds->digest))
  622. continue;
  623. if (!(ds->type & V3_DIRINFO))
  624. continue;
  625. const routerstatus_t *rs = &ds->fake_status;
  626. directory_request_t *req = directory_request_new(dir_purpose);
  627. directory_request_set_routerstatus(req, rs);
  628. directory_request_set_router_purpose(req, router_purpose);
  629. directory_request_set_resource(req, resource);
  630. directory_initiate_request(req);
  631. directory_request_free(req);
  632. } SMARTLIST_FOREACH_END(ds);
  633. }
  634. /** Return true iff <b>ind</b> requires a multihop circuit. */
  635. static int
  636. dirind_is_anon(dir_indirection_t ind)
  637. {
  638. return ind == DIRIND_ANON_DIRPORT || ind == DIRIND_ANONYMOUS;
  639. }
  640. /* Choose reachable OR and Dir addresses and ports from status, copying them
  641. * into use_or_ap and use_dir_ap. If indirection is anonymous, then we're
  642. * connecting via another relay, so choose the primary IPv4 address and ports.
  643. *
  644. * status should have at least one reachable address, if we can't choose a
  645. * reachable address, warn and return -1. Otherwise, return 0.
  646. */
  647. static int
  648. directory_choose_address_routerstatus(const routerstatus_t *status,
  649. dir_indirection_t indirection,
  650. tor_addr_port_t *use_or_ap,
  651. tor_addr_port_t *use_dir_ap)
  652. {
  653. tor_assert(status != NULL);
  654. tor_assert(use_or_ap != NULL);
  655. tor_assert(use_dir_ap != NULL);
  656. const or_options_t *options = get_options();
  657. int have_or = 0, have_dir = 0;
  658. /* We expect status to have at least one reachable address if we're
  659. * connecting to it directly.
  660. *
  661. * Therefore, we can simply use the other address if the one we want isn't
  662. * allowed by the firewall.
  663. *
  664. * (When Tor uploads and downloads a hidden service descriptor, it uses
  665. * DIRIND_ANONYMOUS, except for Tor2Web, which uses DIRIND_ONEHOP.
  666. * So this code will only modify the address for Tor2Web's HS descriptor
  667. * fetches. Even Single Onion Servers (NYI) use DIRIND_ANONYMOUS, to avoid
  668. * HSDirs denying service by rejecting descriptors.)
  669. */
  670. /* Initialise the OR / Dir addresses */
  671. tor_addr_make_null(&use_or_ap->addr, AF_UNSPEC);
  672. use_or_ap->port = 0;
  673. tor_addr_make_null(&use_dir_ap->addr, AF_UNSPEC);
  674. use_dir_ap->port = 0;
  675. /* ORPort connections */
  676. if (indirection == DIRIND_ANONYMOUS) {
  677. if (status->addr) {
  678. /* Since we're going to build a 3-hop circuit and ask the 2nd relay
  679. * to extend to this address, always use the primary (IPv4) OR address */
  680. tor_addr_from_ipv4h(&use_or_ap->addr, status->addr);
  681. use_or_ap->port = status->or_port;
  682. have_or = 1;
  683. }
  684. } else if (indirection == DIRIND_ONEHOP) {
  685. /* We use an IPv6 address if we have one and we prefer it.
  686. * Use the preferred address and port if they are reachable, otherwise,
  687. * use the alternate address and port (if any).
  688. */
  689. have_or = fascist_firewall_choose_address_rs(status,
  690. FIREWALL_OR_CONNECTION, 0,
  691. use_or_ap);
  692. }
  693. /* DirPort connections
  694. * DIRIND_ONEHOP uses ORPort, but may fall back to the DirPort on relays */
  695. if (indirection == DIRIND_DIRECT_CONN ||
  696. indirection == DIRIND_ANON_DIRPORT ||
  697. (indirection == DIRIND_ONEHOP
  698. && !directory_must_use_begindir(options))) {
  699. have_dir = fascist_firewall_choose_address_rs(status,
  700. FIREWALL_DIR_CONNECTION, 0,
  701. use_dir_ap);
  702. }
  703. /* We rejected all addresses in the relay's status. This means we can't
  704. * connect to it. */
  705. if (!have_or && !have_dir) {
  706. static int logged_backtrace = 0;
  707. log_info(LD_BUG, "Rejected all OR and Dir addresses from %s when "
  708. "launching an outgoing directory connection to: IPv4 %s OR %d "
  709. "Dir %d IPv6 %s OR %d Dir %d", routerstatus_describe(status),
  710. fmt_addr32(status->addr), status->or_port,
  711. status->dir_port, fmt_addr(&status->ipv6_addr),
  712. status->ipv6_orport, status->dir_port);
  713. if (!logged_backtrace) {
  714. log_backtrace(LOG_INFO, LD_BUG, "Addresses came from");
  715. logged_backtrace = 1;
  716. }
  717. return -1;
  718. }
  719. return 0;
  720. }
  721. /** Same as directory_initiate_command_routerstatus(), but accepts
  722. * rendezvous data to fetch a hidden service descriptor. */
  723. void
  724. directory_initiate_command_routerstatus_rend(const routerstatus_t *status,
  725. uint8_t dir_purpose,
  726. uint8_t router_purpose,
  727. dir_indirection_t indirection,
  728. const char *resource,
  729. const char *payload,
  730. size_t payload_len,
  731. time_t if_modified_since,
  732. const rend_data_t *rend_query,
  733. circuit_guard_state_t *guard_state)
  734. {
  735. directory_request_t *req = directory_request_new(dir_purpose);
  736. directory_request_set_routerstatus(req, status);
  737. directory_request_set_router_purpose(req, router_purpose);
  738. directory_request_set_indirection(req, indirection);
  739. if (resource)
  740. directory_request_set_resource(req, resource);
  741. if (payload)
  742. directory_request_set_payload(req, payload, payload_len);
  743. if (if_modified_since)
  744. directory_request_set_if_modified_since(req, if_modified_since);
  745. if (rend_query)
  746. directory_request_set_rend_query(req, rend_query);
  747. if (guard_state)
  748. directory_request_set_guard_state(req, guard_state);
  749. /* We don't retry the alternate OR/Dir address for the same directory if
  750. * the address we choose fails (#6772).
  751. * Instead, we'll retry another directory on failure. */
  752. directory_initiate_request(req);
  753. directory_request_free(req);
  754. }
  755. /** Launch a new connection to the directory server <b>status</b> to
  756. * upload or download a server or rendezvous
  757. * descriptor. <b>dir_purpose</b> determines what
  758. * kind of directory connection we're launching, and must be one of
  759. * DIR_PURPOSE_{FETCH|UPLOAD}_{DIR|RENDDESC_V2}. <b>router_purpose</b>
  760. * specifies the descriptor purposes we have in mind (currently only
  761. * used for FETCH_DIR).
  762. *
  763. * When uploading, <b>payload</b> and <b>payload_len</b> determine the content
  764. * of the HTTP post. Otherwise, <b>payload</b> should be NULL.
  765. *
  766. * When fetching a rendezvous descriptor, <b>resource</b> is the service ID we
  767. * want to fetch.
  768. */
  769. MOCK_IMPL(void, directory_initiate_command_routerstatus,
  770. (const routerstatus_t *status,
  771. uint8_t dir_purpose,
  772. uint8_t router_purpose,
  773. dir_indirection_t indirection,
  774. const char *resource,
  775. const char *payload,
  776. size_t payload_len,
  777. time_t if_modified_since,
  778. circuit_guard_state_t *guard_state))
  779. {
  780. directory_initiate_command_routerstatus_rend(status, dir_purpose,
  781. router_purpose,
  782. indirection, resource,
  783. payload, payload_len,
  784. if_modified_since, NULL,
  785. guard_state);
  786. }
  787. /** Return true iff <b>conn</b> is the client side of a directory connection
  788. * we launched to ourself in order to determine the reachability of our
  789. * dir_port. */
  790. static int
  791. directory_conn_is_self_reachability_test(dir_connection_t *conn)
  792. {
  793. if (conn->requested_resource &&
  794. !strcmpstart(conn->requested_resource,"authority")) {
  795. const routerinfo_t *me = router_get_my_routerinfo();
  796. if (me &&
  797. router_digest_is_me(conn->identity_digest) &&
  798. tor_addr_eq_ipv4h(&conn->base_.addr, me->addr) && /*XXXX prop 118*/
  799. me->dir_port == conn->base_.port)
  800. return 1;
  801. }
  802. return 0;
  803. }
  804. /** Called when we are unable to complete the client's request to a directory
  805. * server due to a network error: Mark the router as down and try again if
  806. * possible.
  807. */
  808. static void
  809. connection_dir_request_failed(dir_connection_t *conn)
  810. {
  811. if (conn->guard_state) {
  812. /* We haven't seen a success on this guard state, so consider it to have
  813. * failed. */
  814. entry_guard_failed(&conn->guard_state);
  815. }
  816. if (directory_conn_is_self_reachability_test(conn)) {
  817. return; /* this was a test fetch. don't retry. */
  818. }
  819. if (!entry_list_is_constrained(get_options()))
  820. router_set_status(conn->identity_digest, 0); /* don't try this one again */
  821. if (conn->base_.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
  822. conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO) {
  823. log_info(LD_DIR, "Giving up on serverdesc/extrainfo fetch from "
  824. "directory server at '%s'; retrying",
  825. conn->base_.address);
  826. if (conn->router_purpose == ROUTER_PURPOSE_BRIDGE)
  827. connection_dir_bridge_routerdesc_failed(conn);
  828. connection_dir_download_routerdesc_failed(conn);
  829. } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_CONSENSUS) {
  830. if (conn->requested_resource)
  831. networkstatus_consensus_download_failed(0, conn->requested_resource);
  832. } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_CERTIFICATE) {
  833. log_info(LD_DIR, "Giving up on certificate fetch from directory server "
  834. "at '%s'; retrying",
  835. conn->base_.address);
  836. connection_dir_download_cert_failed(conn, 0);
  837. } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_DETACHED_SIGNATURES) {
  838. log_info(LD_DIR, "Giving up downloading detached signatures from '%s'",
  839. conn->base_.address);
  840. } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_STATUS_VOTE) {
  841. log_info(LD_DIR, "Giving up downloading votes from '%s'",
  842. conn->base_.address);
  843. } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_MICRODESC) {
  844. log_info(LD_DIR, "Giving up on downloading microdescriptors from "
  845. "directory server at '%s'; will retry", conn->base_.address);
  846. connection_dir_download_routerdesc_failed(conn);
  847. }
  848. }
  849. /** Helper: Attempt to fetch directly the descriptors of each bridge
  850. * listed in <b>failed</b>.
  851. */
  852. static void
  853. connection_dir_retry_bridges(smartlist_t *descs)
  854. {
  855. char digest[DIGEST_LEN];
  856. SMARTLIST_FOREACH(descs, const char *, cp,
  857. {
  858. if (base16_decode(digest, DIGEST_LEN, cp, strlen(cp)) != DIGEST_LEN) {
  859. log_warn(LD_BUG, "Malformed fingerprint in list: %s",
  860. escaped(cp));
  861. continue;
  862. }
  863. retry_bridge_descriptor_fetch_directly(digest);
  864. });
  865. }
  866. /** Called when an attempt to download one or more router descriptors
  867. * or extra-info documents on connection <b>conn</b> failed.
  868. */
  869. static void
  870. connection_dir_download_routerdesc_failed(dir_connection_t *conn)
  871. {
  872. /* No need to increment the failure count for routerdescs, since
  873. * it's not their fault. */
  874. /* No need to relaunch descriptor downloads here: we already do it
  875. * every 10 or 60 seconds (FOO_DESCRIPTOR_RETRY_INTERVAL) in main.c. */
  876. tor_assert(conn->base_.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
  877. conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO ||
  878. conn->base_.purpose == DIR_PURPOSE_FETCH_MICRODESC);
  879. (void) conn;
  880. }
  881. /** Called when an attempt to download a bridge's routerdesc from
  882. * one of the authorities failed due to a network error. If
  883. * possible attempt to download descriptors from the bridge directly.
  884. */
  885. static void
  886. connection_dir_bridge_routerdesc_failed(dir_connection_t *conn)
  887. {
  888. smartlist_t *which = NULL;
  889. /* Requests for bridge descriptors are in the form 'fp/', so ignore
  890. anything else. */
  891. if (!conn->requested_resource || strcmpstart(conn->requested_resource,"fp/"))
  892. return;
  893. which = smartlist_new();
  894. dir_split_resource_into_fingerprints(conn->requested_resource
  895. + strlen("fp/"),
  896. which, NULL, 0);
  897. tor_assert(conn->base_.purpose != DIR_PURPOSE_FETCH_EXTRAINFO);
  898. if (smartlist_len(which)) {
  899. connection_dir_retry_bridges(which);
  900. SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
  901. }
  902. smartlist_free(which);
  903. }
  904. /** Called when an attempt to fetch a certificate fails. */
  905. static void
  906. connection_dir_download_cert_failed(dir_connection_t *conn, int status)
  907. {
  908. const char *fp_pfx = "fp/";
  909. const char *fpsk_pfx = "fp-sk/";
  910. smartlist_t *failed;
  911. tor_assert(conn->base_.purpose == DIR_PURPOSE_FETCH_CERTIFICATE);
  912. if (!conn->requested_resource)
  913. return;
  914. failed = smartlist_new();
  915. /*
  916. * We have two cases download by fingerprint (resource starts
  917. * with "fp/") or download by fingerprint/signing key pair
  918. * (resource starts with "fp-sk/").
  919. */
  920. if (!strcmpstart(conn->requested_resource, fp_pfx)) {
  921. /* Download by fingerprint case */
  922. dir_split_resource_into_fingerprints(conn->requested_resource +
  923. strlen(fp_pfx),
  924. failed, NULL, DSR_HEX);
  925. SMARTLIST_FOREACH_BEGIN(failed, char *, cp) {
  926. /* Null signing key digest indicates download by fp only */
  927. authority_cert_dl_failed(cp, NULL, status);
  928. tor_free(cp);
  929. } SMARTLIST_FOREACH_END(cp);
  930. } else if (!strcmpstart(conn->requested_resource, fpsk_pfx)) {
  931. /* Download by (fp,sk) pairs */
  932. dir_split_resource_into_fingerprint_pairs(conn->requested_resource +
  933. strlen(fpsk_pfx), failed);
  934. SMARTLIST_FOREACH_BEGIN(failed, fp_pair_t *, cp) {
  935. authority_cert_dl_failed(cp->first, cp->second, status);
  936. tor_free(cp);
  937. } SMARTLIST_FOREACH_END(cp);
  938. } else {
  939. log_warn(LD_DIR,
  940. "Don't know what to do with failure for cert fetch %s",
  941. conn->requested_resource);
  942. }
  943. smartlist_free(failed);
  944. update_certificate_downloads(time(NULL));
  945. }
  946. /* Should this tor instance only use begindir for all its directory requests?
  947. */
  948. int
  949. directory_must_use_begindir(const or_options_t *options)
  950. {
  951. /* Clients, onion services, and bridges must use begindir,
  952. * relays and authorities do not have to */
  953. return !public_server_mode(options);
  954. }
  955. /** Evaluate the situation and decide if we should use an encrypted
  956. * "begindir-style" connection for this directory request.
  957. * 0) If there is no DirPort, yes.
  958. * 1) If or_port is 0, or it's a direct conn and or_port is firewalled
  959. * or we're a dir mirror, no.
  960. * 2) If we prefer to avoid begindir conns, and we're not fetching or
  961. * publishing a bridge relay descriptor, no.
  962. * 3) Else yes.
  963. * If returning 0, return in *reason why we can't use begindir.
  964. * reason must not be NULL.
  965. */
  966. static int
  967. directory_command_should_use_begindir(const or_options_t *options,
  968. const tor_addr_t *or_addr, int or_port,
  969. const tor_addr_t *dir_addr, int dir_port,
  970. dir_indirection_t indirection,
  971. const char **reason)
  972. {
  973. (void)dir_addr;
  974. tor_assert(reason);
  975. *reason = NULL;
  976. /* Reasons why we must use begindir */
  977. if (!dir_port) {
  978. *reason = "(using begindir - directory with no DirPort)";
  979. return 1; /* We don't know a DirPort -- must begindir. */
  980. }
  981. /* Reasons why we can't possibly use begindir */
  982. if (!or_port) {
  983. *reason = "directory with unknown ORPort";
  984. return 0; /* We don't know an ORPort -- no chance. */
  985. }
  986. if (indirection == DIRIND_DIRECT_CONN ||
  987. indirection == DIRIND_ANON_DIRPORT) {
  988. *reason = "DirPort connection";
  989. return 0;
  990. }
  991. if (indirection == DIRIND_ONEHOP) {
  992. /* We're firewalled and want a direct OR connection */
  993. if (!fascist_firewall_allows_address_addr(or_addr, or_port,
  994. FIREWALL_OR_CONNECTION, 0, 0)) {
  995. *reason = "ORPort not reachable";
  996. return 0;
  997. }
  998. }
  999. /* Reasons why we want to avoid using begindir */
  1000. if (indirection == DIRIND_ONEHOP) {
  1001. if (!directory_must_use_begindir(options)) {
  1002. *reason = "in relay mode";
  1003. return 0;
  1004. }
  1005. }
  1006. /* DIRIND_ONEHOP on a client, or DIRIND_ANONYMOUS
  1007. */
  1008. *reason = "(using begindir)";
  1009. return 1;
  1010. }
  1011. /** Helper for directory_initiate_command_rend: send the
  1012. * command to a server whose OR address/port is <b>or_addr</b>/<b>or_port</b>,
  1013. * whose directory address/port is <b>dir_addr</b>/<b>dir_port</b>, whose
  1014. * identity key digest is <b>digest</b>, with purposes <b>dir_purpose</b> and
  1015. * <b>router_purpose</b>, making an (in)direct connection as specified in
  1016. * <b>indirection</b>, with command <b>resource</b>, <b>payload</b> of
  1017. * <b>payload_len</b>, and asking for a result only <b>if_modified_since</b>.
  1018. */
  1019. void
  1020. directory_initiate_command(const tor_addr_t *or_addr, uint16_t or_port,
  1021. const tor_addr_t *dir_addr, uint16_t dir_port,
  1022. const char *digest,
  1023. uint8_t dir_purpose, uint8_t router_purpose,
  1024. dir_indirection_t indirection, const char *resource,
  1025. const char *payload, size_t payload_len,
  1026. time_t if_modified_since)
  1027. {
  1028. tor_addr_port_t or_ap, dir_ap;
  1029. /* Use the null tor_addr and 0 port if the address or port isn't valid. */
  1030. if (tor_addr_port_is_valid(or_addr, or_port, 0)) {
  1031. tor_addr_copy(&or_ap.addr, or_addr);
  1032. or_ap.port = or_port;
  1033. } else {
  1034. /* the family doesn't matter here, so make it IPv4 */
  1035. tor_addr_make_null(&or_ap.addr, AF_INET);
  1036. or_ap.port = or_port = 0;
  1037. }
  1038. if (tor_addr_port_is_valid(dir_addr, dir_port, 0)) {
  1039. tor_addr_copy(&dir_ap.addr, dir_addr);
  1040. dir_ap.port = dir_port;
  1041. } else {
  1042. /* the family doesn't matter here, so make it IPv4 */
  1043. tor_addr_make_null(&dir_ap.addr, AF_INET);
  1044. dir_ap.port = dir_port = 0;
  1045. }
  1046. directory_request_t *req = directory_request_new(dir_purpose);
  1047. directory_request_set_or_addr_port(req, &or_ap);
  1048. directory_request_set_dir_addr_port(req, &dir_ap);
  1049. directory_request_set_directory_id_digest(req, digest);
  1050. directory_request_set_router_purpose(req, router_purpose);
  1051. directory_request_set_indirection(req, indirection);
  1052. if (resource)
  1053. directory_request_set_resource(req, resource);
  1054. if (payload)
  1055. directory_request_set_payload(req, payload, payload_len);
  1056. if (if_modified_since)
  1057. directory_request_set_if_modified_since(req, if_modified_since);
  1058. directory_initiate_request(req);
  1059. directory_request_free(req);
  1060. }
  1061. struct directory_request_t {
  1062. tor_addr_port_t or_addr_port;
  1063. tor_addr_port_t dir_addr_port;
  1064. char digest[DIGEST_LEN];
  1065. const routerstatus_t *routerstatus;
  1066. uint8_t dir_purpose;
  1067. uint8_t router_purpose;
  1068. dir_indirection_t indirection;
  1069. const char *resource;
  1070. const char *payload;
  1071. size_t payload_len;
  1072. time_t if_modified_since;
  1073. const rend_data_t *rend_query;
  1074. circuit_guard_state_t *guard_state; // XXXX Does this belong?
  1075. };
  1076. directory_request_t *
  1077. directory_request_new(uint8_t dir_purpose)
  1078. {
  1079. tor_assert(dir_purpose >= DIR_PURPOSE_MIN_);
  1080. tor_assert(dir_purpose <= DIR_PURPOSE_MAX_);
  1081. tor_assert(dir_purpose != DIR_PURPOSE_SERVER);
  1082. tor_assert(dir_purpose != DIR_PURPOSE_HAS_FETCHED_RENDDESC_V2);
  1083. directory_request_t *result = tor_malloc_zero(sizeof(*result));
  1084. tor_addr_make_null(&result->or_addr_port.addr, AF_INET);
  1085. result->or_addr_port.port = 0;
  1086. tor_addr_make_null(&result->dir_addr_port.addr, AF_INET);
  1087. result->dir_addr_port.port = 0;
  1088. result->dir_purpose = dir_purpose;
  1089. result->router_purpose = ROUTER_PURPOSE_GENERAL;
  1090. result->indirection = DIRIND_ONEHOP;
  1091. return result;
  1092. }
  1093. void
  1094. directory_request_free(directory_request_t *req)
  1095. {
  1096. if (req == NULL)
  1097. return;
  1098. tor_free(req);
  1099. }
  1100. void
  1101. directory_request_set_or_addr_port(directory_request_t *req,
  1102. const tor_addr_port_t *p)
  1103. {
  1104. memcpy(&req->or_addr_port, p, sizeof(*p));
  1105. }
  1106. void
  1107. directory_request_set_dir_addr_port(directory_request_t *req,
  1108. const tor_addr_port_t *p)
  1109. {
  1110. memcpy(&req->dir_addr_port, p, sizeof(*p));
  1111. }
  1112. void
  1113. directory_request_set_directory_id_digest(directory_request_t *req,
  1114. const char *digest)
  1115. {
  1116. memcpy(req->digest, digest, DIGEST_LEN);
  1117. }
  1118. void
  1119. directory_request_set_router_purpose(directory_request_t *req,
  1120. uint8_t router_purpose)
  1121. {
  1122. tor_assert(router_purpose == ROUTER_PURPOSE_GENERAL ||
  1123. router_purpose == ROUTER_PURPOSE_BRIDGE);
  1124. // assert that it actually makes sense to set this purpose, given
  1125. // the dir_purpose.
  1126. req->router_purpose = router_purpose;
  1127. }
  1128. void
  1129. directory_request_set_indirection(directory_request_t *req,
  1130. dir_indirection_t indirection)
  1131. {
  1132. req->indirection = indirection;
  1133. }
  1134. // DOCDOC lifetime
  1135. void
  1136. directory_request_set_resource(directory_request_t *req,
  1137. const char *resource)
  1138. {
  1139. req->resource = resource;
  1140. }
  1141. // DOCDOC Lifetime
  1142. void
  1143. directory_request_set_payload(directory_request_t *req,
  1144. const char *payload,
  1145. size_t payload_len)
  1146. {
  1147. tor_assert(DIR_PURPOSE_IS_UPLOAD(req->dir_purpose) ||
  1148. // XXXX why not included?
  1149. req->dir_purpose == DIR_PURPOSE_UPLOAD_RENDDESC_V2);
  1150. req->payload = payload;
  1151. req->payload_len = payload_len;
  1152. }
  1153. void
  1154. directory_request_set_if_modified_since(directory_request_t *req,
  1155. time_t if_modified_since)
  1156. {
  1157. req->if_modified_since = if_modified_since;
  1158. }
  1159. // DOCDOC lifetime
  1160. void
  1161. directory_request_set_rend_query(directory_request_t *req,
  1162. const rend_data_t *query)
  1163. {
  1164. req->rend_query = query;
  1165. }
  1166. void
  1167. directory_request_set_guard_state(directory_request_t *req,
  1168. circuit_guard_state_t *state)
  1169. {
  1170. req->guard_state = state;
  1171. }
  1172. static int
  1173. directory_request_is_dir_specified(const directory_request_t *req)
  1174. {
  1175. return (req->or_addr_port.port || req->dir_addr_port.port) &&
  1176. ! tor_digest_is_zero(req->digest);
  1177. }
  1178. void
  1179. directory_request_set_routerstatus(directory_request_t *req,
  1180. const routerstatus_t *status)
  1181. {
  1182. req->routerstatus = status;
  1183. }
  1184. static int
  1185. directory_request_set_dir_from_routerstatus(directory_request_t *req)
  1186. {
  1187. const routerstatus_t *status = req->routerstatus;
  1188. if (status == NULL)
  1189. return -1;
  1190. const or_options_t *options = get_options();
  1191. const node_t *node;
  1192. tor_addr_port_t use_or_ap, use_dir_ap;
  1193. const int anonymized_connection = dirind_is_anon(req->indirection);
  1194. tor_assert(status != NULL);
  1195. node = node_get_by_id(status->identity_digest);
  1196. /* XXX The below check is wrong: !node means it's not in the consensus,
  1197. * but we haven't checked if we have a descriptor for it -- and also,
  1198. * we only care about the descriptor if it's a begindir-style anonymized
  1199. * connection. */
  1200. if (!node && anonymized_connection) {
  1201. log_info(LD_DIR, "Not sending anonymized request to directory '%s'; we "
  1202. "don't have its router descriptor.",
  1203. routerstatus_describe(status));
  1204. return -1;
  1205. }
  1206. if (options->ExcludeNodes && options->StrictNodes &&
  1207. routerset_contains_routerstatus(options->ExcludeNodes, status, -1)) {
  1208. log_warn(LD_DIR, "Wanted to contact directory mirror %s for %s, but "
  1209. "it's in our ExcludedNodes list and StrictNodes is set. "
  1210. "Skipping. This choice might make your Tor not work.",
  1211. routerstatus_describe(status),
  1212. dir_conn_purpose_to_string(req->dir_purpose));
  1213. return -1;
  1214. }
  1215. /* At this point, if we are a client making a direct connection to a
  1216. * directory server, we have selected a server that has at least one address
  1217. * allowed by ClientUseIPv4/6 and Reachable{"",OR,Dir}Addresses. This
  1218. * selection uses the preference in ClientPreferIPv6{OR,Dir}Port, if
  1219. * possible. (If UseBridges is set, clients always use IPv6, and prefer it
  1220. * by default.)
  1221. *
  1222. * Now choose an address that we can use to connect to the directory server.
  1223. */
  1224. if (directory_choose_address_routerstatus(status,
  1225. req->indirection, &use_or_ap,
  1226. &use_dir_ap) < 0) {
  1227. return -1;
  1228. }
  1229. directory_request_set_or_addr_port(req, &use_or_ap);
  1230. directory_request_set_dir_addr_port(req, &use_dir_ap);
  1231. directory_request_set_directory_id_digest(req, status->identity_digest);
  1232. return 0;
  1233. }
  1234. MOCK_IMPL(void,
  1235. directory_initiate_request,(directory_request_t *request))
  1236. {
  1237. tor_assert(request);
  1238. if (request->routerstatus) {
  1239. tor_assert_nonfatal(! directory_request_is_dir_specified(request));
  1240. if (directory_request_set_dir_from_routerstatus(request) < 0) {
  1241. return;
  1242. }
  1243. }
  1244. const tor_addr_port_t *or_addr_port = &request->or_addr_port;
  1245. const tor_addr_port_t *dir_addr_port = &request->dir_addr_port;
  1246. const char *digest = request->digest;
  1247. const uint8_t dir_purpose = request->dir_purpose;
  1248. const uint8_t router_purpose = request->router_purpose;
  1249. const dir_indirection_t indirection = request->indirection;
  1250. const char *resource = request->resource;
  1251. const char *payload = request->payload;
  1252. const size_t payload_len = request->payload_len;
  1253. const time_t if_modified_since = request->if_modified_since;
  1254. const rend_data_t *rend_query = request->rend_query;
  1255. circuit_guard_state_t *guard_state = request->guard_state;
  1256. tor_assert(or_addr_port->port || dir_addr_port->port);
  1257. tor_assert(digest);
  1258. dir_connection_t *conn;
  1259. const or_options_t *options = get_options();
  1260. int socket_error = 0;
  1261. const char *begindir_reason = NULL;
  1262. /* Should the connection be to a relay's OR port (and inside that we will
  1263. * send our directory request)? */
  1264. const int use_begindir = directory_command_should_use_begindir(options,
  1265. &or_addr_port->addr, or_addr_port->port,
  1266. &dir_addr_port->addr, dir_addr_port->port,
  1267. indirection,
  1268. &begindir_reason);
  1269. /* Will the connection go via a three-hop Tor circuit? Note that this
  1270. * is separate from whether it will use_begindir. */
  1271. const int anonymized_connection = dirind_is_anon(indirection);
  1272. /* What is the address we want to make the directory request to? If
  1273. * we're making a begindir request this is the ORPort of the relay
  1274. * we're contacting; if not a begindir request, this is its DirPort.
  1275. * Note that if anonymized_connection is true, we won't be initiating
  1276. * a connection directly to this address. */
  1277. tor_addr_t addr;
  1278. tor_addr_copy(&addr, &(use_begindir ? or_addr_port : dir_addr_port)->addr);
  1279. uint16_t port = (use_begindir ? or_addr_port : dir_addr_port)->port;
  1280. log_debug(LD_DIR, "anonymized %d, use_begindir %d.",
  1281. anonymized_connection, use_begindir);
  1282. log_debug(LD_DIR, "Initiating %s", dir_conn_purpose_to_string(dir_purpose));
  1283. if (purpose_needs_anonymity(dir_purpose, router_purpose, resource)) {
  1284. tor_assert(anonymized_connection ||
  1285. rend_non_anonymous_mode_enabled(options));
  1286. }
  1287. /* use encrypted begindir connections for everything except relays
  1288. * this provides better protection for directory fetches */
  1289. if (!use_begindir && directory_must_use_begindir(options)) {
  1290. log_warn(LD_BUG, "Client could not use begindir connection: %s",
  1291. begindir_reason ? begindir_reason : "(NULL)");
  1292. return;
  1293. }
  1294. /* ensure that we don't make direct connections when a SOCKS server is
  1295. * configured. */
  1296. if (!anonymized_connection && !use_begindir && !options->HTTPProxy &&
  1297. (options->Socks4Proxy || options->Socks5Proxy)) {
  1298. log_warn(LD_DIR, "Cannot connect to a directory server through a "
  1299. "SOCKS proxy!");
  1300. return;
  1301. }
  1302. /* Make sure that the destination addr and port we picked is viable. */
  1303. if (!port || tor_addr_is_null(&addr)) {
  1304. static int logged_backtrace = 0;
  1305. log_warn(LD_DIR,
  1306. "Cannot make an outgoing %sconnection without a remote %sPort.",
  1307. use_begindir ? "begindir " : "",
  1308. use_begindir ? "OR" : "Dir");
  1309. if (!logged_backtrace) {
  1310. log_backtrace(LOG_INFO, LD_BUG, "Address came from");
  1311. logged_backtrace = 1;
  1312. }
  1313. return;
  1314. }
  1315. conn = dir_connection_new(tor_addr_family(&addr));
  1316. /* set up conn so it's got all the data we need to remember */
  1317. tor_addr_copy(&conn->base_.addr, &addr);
  1318. conn->base_.port = port;
  1319. conn->base_.address = tor_addr_to_str_dup(&addr);
  1320. memcpy(conn->identity_digest, digest, DIGEST_LEN);
  1321. conn->base_.purpose = dir_purpose;
  1322. conn->router_purpose = router_purpose;
  1323. /* give it an initial state */
  1324. conn->base_.state = DIR_CONN_STATE_CONNECTING;
  1325. /* decide whether we can learn our IP address from this conn */
  1326. /* XXXX This is a bad name for this field now. */
  1327. conn->dirconn_direct = !anonymized_connection;
  1328. /* copy rendezvous data, if any */
  1329. if (rend_query)
  1330. conn->rend_data = rend_data_dup(rend_query);
  1331. if (!anonymized_connection && !use_begindir) {
  1332. /* then we want to connect to dirport directly */
  1333. if (options->HTTPProxy) {
  1334. tor_addr_copy(&addr, &options->HTTPProxyAddr);
  1335. port = options->HTTPProxyPort;
  1336. }
  1337. // In this case we should not have picked a directory guard.
  1338. if (BUG(guard_state)) {
  1339. entry_guard_cancel(&guard_state);
  1340. }
  1341. switch (connection_connect(TO_CONN(conn), conn->base_.address, &addr,
  1342. port, &socket_error)) {
  1343. case -1:
  1344. connection_mark_for_close(TO_CONN(conn));
  1345. return;
  1346. case 1:
  1347. /* start flushing conn */
  1348. conn->base_.state = DIR_CONN_STATE_CLIENT_SENDING;
  1349. /* fall through */
  1350. case 0:
  1351. /* queue the command on the outbuf */
  1352. directory_send_command(conn, dir_purpose, 1, resource,
  1353. payload, payload_len,
  1354. if_modified_since);
  1355. connection_watch_events(TO_CONN(conn), READ_EVENT | WRITE_EVENT);
  1356. /* writable indicates finish, readable indicates broken link,
  1357. error indicates broken link in windowsland. */
  1358. }
  1359. } else {
  1360. /* We will use a Tor circuit (maybe 1-hop, maybe 3-hop, maybe with
  1361. * begindir, maybe not with begindir) */
  1362. entry_connection_t *linked_conn;
  1363. /* Anonymized tunneled connections can never share a circuit.
  1364. * One-hop directory connections can share circuits with each other
  1365. * but nothing else. */
  1366. int iso_flags = anonymized_connection ? ISO_STREAM : ISO_SESSIONGRP;
  1367. /* If it's an anonymized connection, remember the fact that we
  1368. * wanted it for later: maybe we'll want it again soon. */
  1369. if (anonymized_connection && use_begindir)
  1370. rep_hist_note_used_internal(time(NULL), 0, 1);
  1371. else if (anonymized_connection && !use_begindir)
  1372. rep_hist_note_used_port(time(NULL), conn->base_.port);
  1373. // In this case we should not have a directory guard; we'll
  1374. // get a regular guard later when we build the circuit.
  1375. if (BUG(anonymized_connection && guard_state)) {
  1376. entry_guard_cancel(&guard_state);
  1377. }
  1378. conn->guard_state = guard_state;
  1379. /* make an AP connection
  1380. * populate it and add it at the right state
  1381. * hook up both sides
  1382. */
  1383. linked_conn =
  1384. connection_ap_make_link(TO_CONN(conn),
  1385. conn->base_.address, conn->base_.port,
  1386. digest,
  1387. SESSION_GROUP_DIRCONN, iso_flags,
  1388. use_begindir, !anonymized_connection);
  1389. if (!linked_conn) {
  1390. log_warn(LD_NET,"Making tunnel to dirserver failed.");
  1391. connection_mark_for_close(TO_CONN(conn));
  1392. return;
  1393. }
  1394. if (connection_add(TO_CONN(conn)) < 0) {
  1395. log_warn(LD_NET,"Unable to add connection for link to dirserver.");
  1396. connection_mark_for_close(TO_CONN(conn));
  1397. return;
  1398. }
  1399. conn->base_.state = DIR_CONN_STATE_CLIENT_SENDING;
  1400. /* queue the command on the outbuf */
  1401. directory_send_command(conn, dir_purpose, 0, resource,
  1402. payload, payload_len,
  1403. if_modified_since);
  1404. connection_watch_events(TO_CONN(conn), READ_EVENT|WRITE_EVENT);
  1405. connection_start_reading(ENTRY_TO_CONN(linked_conn));
  1406. }
  1407. }
  1408. /** Return true iff anything we say on <b>conn</b> is being encrypted before
  1409. * we send it to the client/server. */
  1410. int
  1411. connection_dir_is_encrypted(const dir_connection_t *conn)
  1412. {
  1413. /* Right now it's sufficient to see if conn is or has been linked, since
  1414. * the only thing it could be linked to is an edge connection on a
  1415. * circuit, and the only way it could have been unlinked is at the edge
  1416. * connection getting closed.
  1417. */
  1418. return TO_CONN(conn)->linked;
  1419. }
  1420. /** Helper for sorting
  1421. *
  1422. * sort strings alphabetically
  1423. */
  1424. static int
  1425. compare_strs_(const void **a, const void **b)
  1426. {
  1427. const char *s1 = *a, *s2 = *b;
  1428. return strcmp(s1, s2);
  1429. }
  1430. #define CONDITIONAL_CONSENSUS_FPR_LEN 3
  1431. #if (CONDITIONAL_CONSENSUS_FPR_LEN > DIGEST_LEN)
  1432. #error "conditional consensus fingerprint length is larger than digest length"
  1433. #endif
  1434. /** Return the URL we should use for a consensus download.
  1435. *
  1436. * Use the "conditional consensus downloading" feature described in
  1437. * dir-spec.txt, i.e.
  1438. * GET .../consensus/<b>fpr</b>+<b>fpr</b>+<b>fpr</b>
  1439. *
  1440. * If 'resource' is provided, it is the name of a consensus flavor to request.
  1441. */
  1442. static char *
  1443. directory_get_consensus_url(const char *resource)
  1444. {
  1445. char *url = NULL;
  1446. const char *hyphen, *flavor;
  1447. if (resource==NULL || strcmp(resource, "ns")==0) {
  1448. flavor = ""; /* Request ns consensuses as "", so older servers will work*/
  1449. hyphen = "";
  1450. } else {
  1451. flavor = resource;
  1452. hyphen = "-";
  1453. }
  1454. {
  1455. char *authority_id_list;
  1456. smartlist_t *authority_digests = smartlist_new();
  1457. SMARTLIST_FOREACH_BEGIN(router_get_trusted_dir_servers(),
  1458. dir_server_t *, ds) {
  1459. char *hex;
  1460. if (!(ds->type & V3_DIRINFO))
  1461. continue;
  1462. hex = tor_malloc(2*CONDITIONAL_CONSENSUS_FPR_LEN+1);
  1463. base16_encode(hex, 2*CONDITIONAL_CONSENSUS_FPR_LEN+1,
  1464. ds->v3_identity_digest, CONDITIONAL_CONSENSUS_FPR_LEN);
  1465. smartlist_add(authority_digests, hex);
  1466. } SMARTLIST_FOREACH_END(ds);
  1467. smartlist_sort(authority_digests, compare_strs_);
  1468. authority_id_list = smartlist_join_strings(authority_digests,
  1469. "+", 0, NULL);
  1470. tor_asprintf(&url, "/tor/status-vote/current/consensus%s%s/%s.z",
  1471. hyphen, flavor, authority_id_list);
  1472. SMARTLIST_FOREACH(authority_digests, char *, cp, tor_free(cp));
  1473. smartlist_free(authority_digests);
  1474. tor_free(authority_id_list);
  1475. }
  1476. return url;
  1477. }
  1478. /**
  1479. * Copies the ipv6 from source to destination, subject to buffer size limit
  1480. * size. If decorate is true, makes sure the copied address is decorated.
  1481. */
  1482. static void
  1483. copy_ipv6_address(char* destination, const char* source, size_t len,
  1484. int decorate) {
  1485. tor_assert(destination);
  1486. tor_assert(source);
  1487. if (decorate && source[0] != '[') {
  1488. tor_snprintf(destination, len, "[%s]", source);
  1489. } else {
  1490. strlcpy(destination, source, len);
  1491. }
  1492. }
  1493. /** Queue an appropriate HTTP command on conn-\>outbuf. The other args
  1494. * are as in directory_initiate_command().
  1495. */
  1496. static void
  1497. directory_send_command(dir_connection_t *conn,
  1498. int purpose, int direct, const char *resource,
  1499. const char *payload, size_t payload_len,
  1500. time_t if_modified_since)
  1501. {
  1502. char proxystring[256];
  1503. char hoststring[128];
  1504. /* NEEDS to be the same size hoststring.
  1505. Will be decorated with brackets around it if it is ipv6. */
  1506. char decorated_address[128];
  1507. smartlist_t *headers = smartlist_new();
  1508. char *url;
  1509. size_t url_len;
  1510. char request[8192];
  1511. size_t request_len, total_request_len = 0;
  1512. const char *httpcommand = NULL;
  1513. tor_assert(conn);
  1514. tor_assert(conn->base_.type == CONN_TYPE_DIR);
  1515. tor_free(conn->requested_resource);
  1516. if (resource)
  1517. conn->requested_resource = tor_strdup(resource);
  1518. /* decorate the ip address if it is ipv6 */
  1519. if (strchr(conn->base_.address, ':')) {
  1520. copy_ipv6_address(decorated_address, conn->base_.address,
  1521. sizeof(decorated_address), 1);
  1522. } else {
  1523. strlcpy(decorated_address, conn->base_.address, sizeof(decorated_address));
  1524. }
  1525. /* come up with a string for which Host: we want */
  1526. if (conn->base_.port == 80) {
  1527. strlcpy(hoststring, decorated_address, sizeof(hoststring));
  1528. } else {
  1529. tor_snprintf(hoststring, sizeof(hoststring), "%s:%d",
  1530. decorated_address, conn->base_.port);
  1531. }
  1532. /* Format if-modified-since */
  1533. if (if_modified_since) {
  1534. char b[RFC1123_TIME_LEN+1];
  1535. format_rfc1123_time(b, if_modified_since);
  1536. smartlist_add_asprintf(headers, "If-Modified-Since: %s\r\n", b);
  1537. }
  1538. /* come up with some proxy lines, if we're using one. */
  1539. if (direct && get_options()->HTTPProxy) {
  1540. char *base64_authenticator=NULL;
  1541. const char *authenticator = get_options()->HTTPProxyAuthenticator;
  1542. tor_snprintf(proxystring, sizeof(proxystring),"http://%s", hoststring);
  1543. if (authenticator) {
  1544. base64_authenticator = alloc_http_authenticator(authenticator);
  1545. if (!base64_authenticator)
  1546. log_warn(LD_BUG, "Encoding http authenticator failed");
  1547. }
  1548. if (base64_authenticator) {
  1549. smartlist_add_asprintf(headers,
  1550. "Proxy-Authorization: Basic %s\r\n",
  1551. base64_authenticator);
  1552. tor_free(base64_authenticator);
  1553. }
  1554. } else {
  1555. proxystring[0] = 0;
  1556. }
  1557. switch (purpose) {
  1558. case DIR_PURPOSE_FETCH_CONSENSUS:
  1559. /* resource is optional. If present, it's a flavor name */
  1560. tor_assert(!payload);
  1561. httpcommand = "GET";
  1562. url = directory_get_consensus_url(resource);
  1563. log_info(LD_DIR, "Downloading consensus from %s using %s",
  1564. hoststring, url);
  1565. break;
  1566. case DIR_PURPOSE_FETCH_CERTIFICATE:
  1567. tor_assert(resource);
  1568. tor_assert(!payload);
  1569. httpcommand = "GET";
  1570. tor_asprintf(&url, "/tor/keys/%s", resource);
  1571. break;
  1572. case DIR_PURPOSE_FETCH_STATUS_VOTE:
  1573. tor_assert(resource);
  1574. tor_assert(!payload);
  1575. httpcommand = "GET";
  1576. tor_asprintf(&url, "/tor/status-vote/next/%s.z", resource);
  1577. break;
  1578. case DIR_PURPOSE_FETCH_DETACHED_SIGNATURES:
  1579. tor_assert(!resource);
  1580. tor_assert(!payload);
  1581. httpcommand = "GET";
  1582. url = tor_strdup("/tor/status-vote/next/consensus-signatures.z");
  1583. break;
  1584. case DIR_PURPOSE_FETCH_SERVERDESC:
  1585. tor_assert(resource);
  1586. httpcommand = "GET";
  1587. tor_asprintf(&url, "/tor/server/%s", resource);
  1588. break;
  1589. case DIR_PURPOSE_FETCH_EXTRAINFO:
  1590. tor_assert(resource);
  1591. httpcommand = "GET";
  1592. tor_asprintf(&url, "/tor/extra/%s", resource);
  1593. break;
  1594. case DIR_PURPOSE_FETCH_MICRODESC:
  1595. tor_assert(resource);
  1596. httpcommand = "GET";
  1597. tor_asprintf(&url, "/tor/micro/%s", resource);
  1598. break;
  1599. case DIR_PURPOSE_UPLOAD_DIR: {
  1600. const char *why = router_get_descriptor_gen_reason();
  1601. tor_assert(!resource);
  1602. tor_assert(payload);
  1603. httpcommand = "POST";
  1604. url = tor_strdup("/tor/");
  1605. if (why) {
  1606. smartlist_add_asprintf(headers, "X-Desc-Gen-Reason: %s\r\n", why);
  1607. }
  1608. break;
  1609. }
  1610. case DIR_PURPOSE_UPLOAD_VOTE:
  1611. tor_assert(!resource);
  1612. tor_assert(payload);
  1613. httpcommand = "POST";
  1614. url = tor_strdup("/tor/post/vote");
  1615. break;
  1616. case DIR_PURPOSE_UPLOAD_SIGNATURES:
  1617. tor_assert(!resource);
  1618. tor_assert(payload);
  1619. httpcommand = "POST";
  1620. url = tor_strdup("/tor/post/consensus-signature");
  1621. break;
  1622. case DIR_PURPOSE_FETCH_RENDDESC_V2:
  1623. tor_assert(resource);
  1624. tor_assert(strlen(resource) <= REND_DESC_ID_V2_LEN_BASE32);
  1625. tor_assert(!payload);
  1626. httpcommand = "GET";
  1627. tor_asprintf(&url, "/tor/rendezvous2/%s", resource);
  1628. break;
  1629. case DIR_PURPOSE_UPLOAD_RENDDESC_V2:
  1630. tor_assert(!resource);
  1631. tor_assert(payload);
  1632. httpcommand = "POST";
  1633. url = tor_strdup("/tor/rendezvous2/publish");
  1634. break;
  1635. default:
  1636. tor_assert(0);
  1637. return;
  1638. }
  1639. /* warn in the non-tunneled case */
  1640. if (direct && (strlen(proxystring) + strlen(url) >= 4096)) {
  1641. log_warn(LD_BUG,
  1642. "Squid does not like URLs longer than 4095 bytes, and this "
  1643. "one is %d bytes long: %s%s",
  1644. (int)(strlen(proxystring) + strlen(url)), proxystring, url);
  1645. }
  1646. tor_snprintf(request, sizeof(request), "%s %s", httpcommand, proxystring);
  1647. request_len = strlen(request);
  1648. total_request_len += request_len;
  1649. connection_write_to_buf(request, request_len, TO_CONN(conn));
  1650. url_len = strlen(url);
  1651. total_request_len += url_len;
  1652. connection_write_to_buf(url, url_len, TO_CONN(conn));
  1653. tor_free(url);
  1654. if (!strcmp(httpcommand, "POST") || payload) {
  1655. smartlist_add_asprintf(headers, "Content-Length: %lu\r\n",
  1656. payload ? (unsigned long)payload_len : 0);
  1657. }
  1658. {
  1659. char *header = smartlist_join_strings(headers, "", 0, NULL);
  1660. tor_snprintf(request, sizeof(request), " HTTP/1.0\r\nHost: %s\r\n%s\r\n",
  1661. hoststring, header);
  1662. tor_free(header);
  1663. }
  1664. request_len = strlen(request);
  1665. total_request_len += request_len;
  1666. connection_write_to_buf(request, request_len, TO_CONN(conn));
  1667. if (payload) {
  1668. /* then send the payload afterwards too */
  1669. connection_write_to_buf(payload, payload_len, TO_CONN(conn));
  1670. total_request_len += payload_len;
  1671. }
  1672. SMARTLIST_FOREACH(headers, char *, h, tor_free(h));
  1673. smartlist_free(headers);
  1674. log_debug(LD_DIR,
  1675. "Sent request to directory server '%s:%d': "
  1676. "(purpose: %d, request size: " U64_FORMAT ", "
  1677. "payload size: " U64_FORMAT ")",
  1678. conn->base_.address, conn->base_.port,
  1679. conn->base_.purpose,
  1680. U64_PRINTF_ARG(total_request_len),
  1681. U64_PRINTF_ARG(payload ? payload_len : 0));
  1682. }
  1683. /** Parse an HTTP request string <b>headers</b> of the form
  1684. * \verbatim
  1685. * "\%s [http[s]://]\%s HTTP/1..."
  1686. * \endverbatim
  1687. * If it's well-formed, strdup the second \%s into *<b>url</b>, and
  1688. * nul-terminate it. If the url doesn't start with "/tor/", rewrite it
  1689. * so it does. Return 0.
  1690. * Otherwise, return -1.
  1691. */
  1692. STATIC int
  1693. parse_http_url(const char *headers, char **url)
  1694. {
  1695. char *s, *start, *tmp;
  1696. s = (char *)eat_whitespace_no_nl(headers);
  1697. if (!*s) return -1;
  1698. s = (char *)find_whitespace(s); /* get past GET/POST */
  1699. if (!*s) return -1;
  1700. s = (char *)eat_whitespace_no_nl(s);
  1701. if (!*s) return -1;
  1702. start = s; /* this is it, assuming it's valid */
  1703. s = (char *)find_whitespace(start);
  1704. if (!*s) return -1;
  1705. /* tolerate the http[s] proxy style of putting the hostname in the url */
  1706. if (s-start >= 4 && !strcmpstart(start,"http")) {
  1707. tmp = start + 4;
  1708. if (*tmp == 's')
  1709. tmp++;
  1710. if (s-tmp >= 3 && !strcmpstart(tmp,"://")) {
  1711. tmp = strchr(tmp+3, '/');
  1712. if (tmp && tmp < s) {
  1713. log_debug(LD_DIR,"Skipping over 'http[s]://hostname/' string");
  1714. start = tmp;
  1715. }
  1716. }
  1717. }
  1718. /* Check if the header is well formed (next sequence
  1719. * should be HTTP/1.X\r\n). Assumes we're supporting 1.0? */
  1720. {
  1721. unsigned minor_ver;
  1722. char ch;
  1723. char *e = (char *)eat_whitespace_no_nl(s);
  1724. if (2 != tor_sscanf(e, "HTTP/1.%u%c", &minor_ver, &ch)) {
  1725. return -1;
  1726. }
  1727. if (ch != '\r')
  1728. return -1;
  1729. }
  1730. if (s-start < 5 || strcmpstart(start,"/tor/")) { /* need to rewrite it */
  1731. *url = tor_malloc(s - start + 5);
  1732. strlcpy(*url,"/tor", s-start+5);
  1733. strlcat((*url)+4, start, s-start+1);
  1734. } else {
  1735. *url = tor_strndup(start, s-start);
  1736. }
  1737. return 0;
  1738. }
  1739. /** Return a copy of the first HTTP header in <b>headers</b> whose key is
  1740. * <b>which</b>. The key should be given with a terminating colon and space;
  1741. * this function copies everything after, up to but not including the
  1742. * following \\r\\n. */
  1743. static char *
  1744. http_get_header(const char *headers, const char *which)
  1745. {
  1746. const char *cp = headers;
  1747. while (cp) {
  1748. if (!strcasecmpstart(cp, which)) {
  1749. char *eos;
  1750. cp += strlen(which);
  1751. if ((eos = strchr(cp,'\r')))
  1752. return tor_strndup(cp, eos-cp);
  1753. else
  1754. return tor_strdup(cp);
  1755. }
  1756. cp = strchr(cp, '\n');
  1757. if (cp)
  1758. ++cp;
  1759. }
  1760. return NULL;
  1761. }
  1762. /** If <b>headers</b> indicates that a proxy was involved, then rewrite
  1763. * <b>conn</b>-\>address to describe our best guess of the address that
  1764. * originated this HTTP request. */
  1765. static void
  1766. http_set_address_origin(const char *headers, connection_t *conn)
  1767. {
  1768. char *fwd;
  1769. fwd = http_get_header(headers, "Forwarded-For: ");
  1770. if (!fwd)
  1771. fwd = http_get_header(headers, "X-Forwarded-For: ");
  1772. if (fwd) {
  1773. tor_addr_t toraddr;
  1774. if (tor_addr_parse(&toraddr,fwd) == -1 ||
  1775. tor_addr_is_internal(&toraddr,0)) {
  1776. log_debug(LD_DIR, "Ignoring local/internal IP %s", escaped(fwd));
  1777. tor_free(fwd);
  1778. return;
  1779. }
  1780. tor_free(conn->address);
  1781. conn->address = tor_strdup(fwd);
  1782. tor_free(fwd);
  1783. }
  1784. }
  1785. /** Parse an HTTP response string <b>headers</b> of the form
  1786. * \verbatim
  1787. * "HTTP/1.\%d \%d\%s\r\n...".
  1788. * \endverbatim
  1789. *
  1790. * If it's well-formed, assign the status code to *<b>code</b> and
  1791. * return 0. Otherwise, return -1.
  1792. *
  1793. * On success: If <b>date</b> is provided, set *date to the Date
  1794. * header in the http headers, or 0 if no such header is found. If
  1795. * <b>compression</b> is provided, set *<b>compression</b> to the
  1796. * compression method given in the Content-Encoding header, or 0 if no
  1797. * such header is found, or -1 if the value of the header is not
  1798. * recognized. If <b>reason</b> is provided, strdup the reason string
  1799. * into it.
  1800. */
  1801. int
  1802. parse_http_response(const char *headers, int *code, time_t *date,
  1803. compress_method_t *compression, char **reason)
  1804. {
  1805. unsigned n1, n2;
  1806. char datestr[RFC1123_TIME_LEN+1];
  1807. smartlist_t *parsed_headers;
  1808. tor_assert(headers);
  1809. tor_assert(code);
  1810. while (TOR_ISSPACE(*headers)) headers++; /* tolerate leading whitespace */
  1811. if (tor_sscanf(headers, "HTTP/1.%u %u", &n1, &n2) < 2 ||
  1812. (n1 != 0 && n1 != 1) ||
  1813. (n2 < 100 || n2 >= 600)) {
  1814. log_warn(LD_HTTP,"Failed to parse header %s",escaped(headers));
  1815. return -1;
  1816. }
  1817. *code = n2;
  1818. parsed_headers = smartlist_new();
  1819. smartlist_split_string(parsed_headers, headers, "\n",
  1820. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
  1821. if (reason) {
  1822. smartlist_t *status_line_elements = smartlist_new();
  1823. tor_assert(smartlist_len(parsed_headers));
  1824. smartlist_split_string(status_line_elements,
  1825. smartlist_get(parsed_headers, 0),
  1826. " ", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 3);
  1827. tor_assert(smartlist_len(status_line_elements) <= 3);
  1828. if (smartlist_len(status_line_elements) == 3) {
  1829. *reason = smartlist_get(status_line_elements, 2);
  1830. smartlist_set(status_line_elements, 2, NULL); /* Prevent free */
  1831. }
  1832. SMARTLIST_FOREACH(status_line_elements, char *, cp, tor_free(cp));
  1833. smartlist_free(status_line_elements);
  1834. }
  1835. if (date) {
  1836. *date = 0;
  1837. SMARTLIST_FOREACH(parsed_headers, const char *, s,
  1838. if (!strcmpstart(s, "Date: ")) {
  1839. strlcpy(datestr, s+6, sizeof(datestr));
  1840. /* This will do nothing on failure, so we don't need to check
  1841. the result. We shouldn't warn, since there are many other valid
  1842. date formats besides the one we use. */
  1843. parse_rfc1123_time(datestr, date);
  1844. break;
  1845. });
  1846. }
  1847. if (compression) {
  1848. const char *enc = NULL;
  1849. SMARTLIST_FOREACH(parsed_headers, const char *, s,
  1850. if (!strcmpstart(s, "Content-Encoding: ")) {
  1851. enc = s+18; break;
  1852. });
  1853. if (!enc || !strcmp(enc, "identity")) {
  1854. *compression = NO_METHOD;
  1855. } else if (!strcmp(enc, "deflate") || !strcmp(enc, "x-deflate")) {
  1856. *compression = ZLIB_METHOD;
  1857. } else if (!strcmp(enc, "gzip") || !strcmp(enc, "x-gzip")) {
  1858. *compression = GZIP_METHOD;
  1859. } else {
  1860. log_info(LD_HTTP, "Unrecognized content encoding: %s. Trying to deal.",
  1861. escaped(enc));
  1862. *compression = UNKNOWN_METHOD;
  1863. }
  1864. }
  1865. SMARTLIST_FOREACH(parsed_headers, char *, s, tor_free(s));
  1866. smartlist_free(parsed_headers);
  1867. return 0;
  1868. }
  1869. /** Return true iff <b>body</b> doesn't start with a plausible router or
  1870. * network-status or microdescriptor opening. This is a sign of possible
  1871. * compression. */
  1872. static int
  1873. body_is_plausible(const char *body, size_t len, int purpose)
  1874. {
  1875. int i;
  1876. if (len == 0)
  1877. return 1; /* empty bodies don't need decompression */
  1878. if (len < 32)
  1879. return 0;
  1880. if (purpose == DIR_PURPOSE_FETCH_MICRODESC) {
  1881. return (!strcmpstart(body,"onion-key"));
  1882. }
  1883. if (!strcmpstart(body,"router") ||
  1884. !strcmpstart(body,"network-status"))
  1885. return 1;
  1886. for (i=0;i<32;++i) {
  1887. if (!TOR_ISPRINT(body[i]) && !TOR_ISSPACE(body[i]))
  1888. return 0;
  1889. }
  1890. return 1;
  1891. }
  1892. /** Called when we've just fetched a bunch of router descriptors in
  1893. * <b>body</b>. The list <b>which</b>, if present, holds digests for
  1894. * descriptors we requested: descriptor digests if <b>descriptor_digests</b>
  1895. * is true, or identity digests otherwise. Parse the descriptors, validate
  1896. * them, and annotate them as having purpose <b>purpose</b> and as having been
  1897. * downloaded from <b>source</b>.
  1898. *
  1899. * Return the number of routers actually added. */
  1900. static int
  1901. load_downloaded_routers(const char *body, smartlist_t *which,
  1902. int descriptor_digests,
  1903. int router_purpose,
  1904. const char *source)
  1905. {
  1906. char buf[256];
  1907. char time_buf[ISO_TIME_LEN+1];
  1908. int added = 0;
  1909. int general = router_purpose == ROUTER_PURPOSE_GENERAL;
  1910. format_iso_time(time_buf, time(NULL));
  1911. tor_assert(source);
  1912. if (tor_snprintf(buf, sizeof(buf),
  1913. "@downloaded-at %s\n"
  1914. "@source %s\n"
  1915. "%s%s%s", time_buf, escaped(source),
  1916. !general ? "@purpose " : "",
  1917. !general ? router_purpose_to_string(router_purpose) : "",
  1918. !general ? "\n" : "")<0)
  1919. return added;
  1920. added = router_load_routers_from_string(body, NULL, SAVED_NOWHERE, which,
  1921. descriptor_digests, buf);
  1922. if (added && general)
  1923. control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_DESCRIPTORS,
  1924. count_loading_descriptors_progress());
  1925. return added;
  1926. }
  1927. /** We are a client, and we've finished reading the server's
  1928. * response. Parse it and act appropriately.
  1929. *
  1930. * If we're still happy with using this directory server in the future, return
  1931. * 0. Otherwise return -1; and the caller should consider trying the request
  1932. * again.
  1933. *
  1934. * The caller will take care of marking the connection for close.
  1935. */
  1936. static int
  1937. connection_dir_client_reached_eof(dir_connection_t *conn)
  1938. {
  1939. char *body;
  1940. char *headers;
  1941. char *reason = NULL;
  1942. size_t body_len = 0;
  1943. int status_code;
  1944. time_t date_header = 0;
  1945. long apparent_skew;
  1946. compress_method_t compression;
  1947. int plausible;
  1948. int skewed = 0;
  1949. int allow_partial = (conn->base_.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
  1950. conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO ||
  1951. conn->base_.purpose == DIR_PURPOSE_FETCH_MICRODESC);
  1952. time_t now = time(NULL);
  1953. int src_code;
  1954. size_t received_bytes;
  1955. received_bytes = connection_get_inbuf_len(TO_CONN(conn));
  1956. switch (connection_fetch_from_buf_http(TO_CONN(conn),
  1957. &headers, MAX_HEADERS_SIZE,
  1958. &body, &body_len, MAX_DIR_DL_SIZE,
  1959. allow_partial)) {
  1960. case -1: /* overflow */
  1961. log_warn(LD_PROTOCOL,
  1962. "'fetch' response too large (server '%s:%d'). Closing.",
  1963. conn->base_.address, conn->base_.port);
  1964. return -1;
  1965. case 0:
  1966. log_info(LD_HTTP,
  1967. "'fetch' response not all here, but we're at eof. Closing.");
  1968. return -1;
  1969. /* case 1, fall through */
  1970. }
  1971. if (parse_http_response(headers, &status_code, &date_header,
  1972. &compression, &reason) < 0) {
  1973. log_warn(LD_HTTP,"Unparseable headers (server '%s:%d'). Closing.",
  1974. conn->base_.address, conn->base_.port);
  1975. tor_free(body); tor_free(headers);
  1976. return -1;
  1977. }
  1978. if (!reason) reason = tor_strdup("[no reason given]");
  1979. tor_log(LOG_DEBUG, LD_DIR,
  1980. "Received response from directory server '%s:%d': %d %s "
  1981. "(purpose: %d, response size: " U64_FORMAT
  1982. #ifdef MEASUREMENTS_21206
  1983. ", data cells received: %d, data cells sent: %d"
  1984. #endif
  1985. ", compression: %d)",
  1986. conn->base_.address, conn->base_.port, status_code,
  1987. escaped(reason), conn->base_.purpose,
  1988. U64_PRINTF_ARG(received_bytes),
  1989. #ifdef MEASUREMENTS_21206
  1990. conn->data_cells_received, conn->data_cells_sent,
  1991. #endif
  1992. compression);
  1993. if (conn->guard_state) {
  1994. /* we count the connection as successful once we can read from it. We do
  1995. * not, however, delay use of the circuit here, since it's just for a
  1996. * one-hop directory request. */
  1997. /* XXXXprop271 note that this will not do the right thing for other
  1998. * waiting circuits that would be triggered by this circuit becoming
  1999. * complete/usable. But that's ok, I think.
  2000. */
  2001. entry_guard_succeeded(&conn->guard_state);
  2002. circuit_guard_state_free(conn->guard_state);
  2003. conn->guard_state = NULL;
  2004. }
  2005. /* now check if it's got any hints for us about our IP address. */
  2006. if (conn->dirconn_direct) {
  2007. char *guess = http_get_header(headers, X_ADDRESS_HEADER);
  2008. if (guess) {
  2009. router_new_address_suggestion(guess, conn);
  2010. tor_free(guess);
  2011. }
  2012. }
  2013. if (date_header > 0) {
  2014. /* The date header was written very soon after we sent our request,
  2015. * so compute the skew as the difference between sending the request
  2016. * and the date header. (We used to check now-date_header, but that's
  2017. * inaccurate if we spend a lot of time downloading.)
  2018. */
  2019. apparent_skew = conn->base_.timestamp_lastwritten - date_header;
  2020. if (labs(apparent_skew)>ALLOW_DIRECTORY_TIME_SKEW) {
  2021. int trusted = router_digest_is_trusted_dir(conn->identity_digest);
  2022. clock_skew_warning(TO_CONN(conn), apparent_skew, trusted, LD_HTTP,
  2023. "directory", "DIRSERV");
  2024. skewed = 1; /* don't check the recommended-versions line */
  2025. } else {
  2026. log_debug(LD_HTTP, "Time on received directory is within tolerance; "
  2027. "we are %ld seconds skewed. (That's okay.)", apparent_skew);
  2028. }
  2029. }
  2030. (void) skewed; /* skewed isn't used yet. */
  2031. if (status_code == 503) {
  2032. routerstatus_t *rs;
  2033. dir_server_t *ds;
  2034. const char *id_digest = conn->identity_digest;
  2035. log_info(LD_DIR,"Received http status code %d (%s) from server "
  2036. "'%s:%d'. I'll try again soon.",
  2037. status_code, escaped(reason), conn->base_.address,
  2038. conn->base_.port);
  2039. if ((rs = router_get_mutable_consensus_status_by_id(id_digest)))
  2040. rs->last_dir_503_at = now;
  2041. if ((ds = router_get_fallback_dirserver_by_digest(id_digest)))
  2042. ds->fake_status.last_dir_503_at = now;
  2043. tor_free(body); tor_free(headers); tor_free(reason);
  2044. return -1;
  2045. }
  2046. plausible = body_is_plausible(body, body_len, conn->base_.purpose);
  2047. if (compression != NO_METHOD || !plausible) {
  2048. char *new_body = NULL;
  2049. size_t new_len = 0;
  2050. compress_method_t guessed = detect_compression_method(body, body_len);
  2051. if (compression == UNKNOWN_METHOD || guessed != compression) {
  2052. /* Tell the user if we don't believe what we're told about compression.*/
  2053. const char *description1, *description2;
  2054. if (compression == ZLIB_METHOD)
  2055. description1 = "as deflated";
  2056. else if (compression == GZIP_METHOD)
  2057. description1 = "as gzipped";
  2058. else if (compression == NO_METHOD)
  2059. description1 = "as uncompressed";
  2060. else
  2061. description1 = "with an unknown Content-Encoding";
  2062. if (guessed == ZLIB_METHOD)
  2063. description2 = "deflated";
  2064. else if (guessed == GZIP_METHOD)
  2065. description2 = "gzipped";
  2066. else if (!plausible)
  2067. description2 = "confusing binary junk";
  2068. else
  2069. description2 = "uncompressed";
  2070. log_info(LD_HTTP, "HTTP body from server '%s:%d' was labeled %s, "
  2071. "but it seems to be %s.%s",
  2072. conn->base_.address, conn->base_.port, description1,
  2073. description2,
  2074. (compression>0 && guessed>0)?" Trying both.":"");
  2075. }
  2076. /* Try declared compression first if we can. */
  2077. if (compression == GZIP_METHOD || compression == ZLIB_METHOD)
  2078. tor_gzip_uncompress(&new_body, &new_len, body, body_len, compression,
  2079. !allow_partial, LOG_PROTOCOL_WARN);
  2080. /* Okay, if that didn't work, and we think that it was compressed
  2081. * differently, try that. */
  2082. if (!new_body &&
  2083. (guessed == GZIP_METHOD || guessed == ZLIB_METHOD) &&
  2084. compression != guessed)
  2085. tor_gzip_uncompress(&new_body, &new_len, body, body_len, guessed,
  2086. !allow_partial, LOG_PROTOCOL_WARN);
  2087. /* If we're pretty sure that we have a compressed directory, and
  2088. * we didn't manage to uncompress it, then warn and bail. */
  2089. if (!plausible && !new_body) {
  2090. log_fn(LOG_PROTOCOL_WARN, LD_HTTP,
  2091. "Unable to decompress HTTP body (server '%s:%d').",
  2092. conn->base_.address, conn->base_.port);
  2093. tor_free(body); tor_free(headers); tor_free(reason);
  2094. return -1;
  2095. }
  2096. if (new_body) {
  2097. tor_free(body);
  2098. body = new_body;
  2099. body_len = new_len;
  2100. }
  2101. }
  2102. if (conn->base_.purpose == DIR_PURPOSE_FETCH_CONSENSUS) {
  2103. int r;
  2104. const char *flavname = conn->requested_resource;
  2105. if (status_code != 200) {
  2106. int severity = (status_code == 304) ? LOG_INFO : LOG_WARN;
  2107. tor_log(severity, LD_DIR,
  2108. "Received http status code %d (%s) from server "
  2109. "'%s:%d' while fetching consensus directory.",
  2110. status_code, escaped(reason), conn->base_.address,
  2111. conn->base_.port);
  2112. tor_free(body); tor_free(headers); tor_free(reason);
  2113. networkstatus_consensus_download_failed(status_code, flavname);
  2114. return -1;
  2115. }
  2116. log_info(LD_DIR,"Received consensus directory (body size %d) from server "
  2117. "'%s:%d'", (int)body_len, conn->base_.address, conn->base_.port);
  2118. if ((r=networkstatus_set_current_consensus(body, flavname, 0,
  2119. conn->identity_digest))<0) {
  2120. log_fn(r<-1?LOG_WARN:LOG_INFO, LD_DIR,
  2121. "Unable to load %s consensus directory downloaded from "
  2122. "server '%s:%d'. I'll try again soon.",
  2123. flavname, conn->base_.address, conn->base_.port);
  2124. tor_free(body); tor_free(headers); tor_free(reason);
  2125. networkstatus_consensus_download_failed(0, flavname);
  2126. return -1;
  2127. }
  2128. /* If we launched other fetches for this consensus, cancel them. */
  2129. connection_dir_close_consensus_fetches(conn, flavname);
  2130. /* launches router downloads as needed */
  2131. routers_update_all_from_networkstatus(now, 3);
  2132. update_microdescs_from_networkstatus(now);
  2133. update_microdesc_downloads(now);
  2134. directory_info_has_arrived(now, 0, 0);
  2135. if (authdir_mode_v3(get_options())) {
  2136. sr_act_post_consensus(
  2137. networkstatus_get_latest_consensus_by_flavor(FLAV_NS));
  2138. }
  2139. log_info(LD_DIR, "Successfully loaded consensus.");
  2140. }
  2141. if (conn->base_.purpose == DIR_PURPOSE_FETCH_CERTIFICATE) {
  2142. if (status_code != 200) {
  2143. log_warn(LD_DIR,
  2144. "Received http status code %d (%s) from server "
  2145. "'%s:%d' while fetching \"/tor/keys/%s\".",
  2146. status_code, escaped(reason), conn->base_.address,
  2147. conn->base_.port, conn->requested_resource);
  2148. connection_dir_download_cert_failed(conn, status_code);
  2149. tor_free(body); tor_free(headers); tor_free(reason);
  2150. return -1;
  2151. }
  2152. log_info(LD_DIR,"Received authority certificates (body size %d) from "
  2153. "server '%s:%d'",
  2154. (int)body_len, conn->base_.address, conn->base_.port);
  2155. /*
  2156. * Tell trusted_dirs_load_certs_from_string() whether it was by fp
  2157. * or fp-sk pair.
  2158. */
  2159. src_code = -1;
  2160. if (!strcmpstart(conn->requested_resource, "fp/")) {
  2161. src_code = TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST;
  2162. } else if (!strcmpstart(conn->requested_resource, "fp-sk/")) {
  2163. src_code = TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_SK_DIGEST;
  2164. }
  2165. if (src_code != -1) {
  2166. if (trusted_dirs_load_certs_from_string(body, src_code, 1,
  2167. conn->identity_digest)<0) {
  2168. log_warn(LD_DIR, "Unable to parse fetched certificates");
  2169. /* if we fetched more than one and only some failed, the successful
  2170. * ones got flushed to disk so it's safe to call this on them */
  2171. connection_dir_download_cert_failed(conn, status_code);
  2172. } else {
  2173. directory_info_has_arrived(now, 0, 0);
  2174. log_info(LD_DIR, "Successfully loaded certificates from fetch.");
  2175. }
  2176. } else {
  2177. log_warn(LD_DIR,
  2178. "Couldn't figure out what to do with fetched certificates for "
  2179. "unknown resource %s",
  2180. conn->requested_resource);
  2181. connection_dir_download_cert_failed(conn, status_code);
  2182. }
  2183. }
  2184. if (conn->base_.purpose == DIR_PURPOSE_FETCH_STATUS_VOTE) {
  2185. const char *msg;
  2186. int st;
  2187. log_info(LD_DIR,"Got votes (body size %d) from server %s:%d",
  2188. (int)body_len, conn->base_.address, conn->base_.port);
  2189. if (status_code != 200) {
  2190. log_warn(LD_DIR,
  2191. "Received http status code %d (%s) from server "
  2192. "'%s:%d' while fetching \"/tor/status-vote/next/%s.z\".",
  2193. status_code, escaped(reason), conn->base_.address,
  2194. conn->base_.port, conn->requested_resource);
  2195. tor_free(body); tor_free(headers); tor_free(reason);
  2196. return -1;
  2197. }
  2198. dirvote_add_vote(body, &msg, &st);
  2199. if (st > 299) {
  2200. log_warn(LD_DIR, "Error adding retrieved vote: %s", msg);
  2201. } else {
  2202. log_info(LD_DIR, "Added vote(s) successfully [msg: %s]", msg);
  2203. }
  2204. }
  2205. if (conn->base_.purpose == DIR_PURPOSE_FETCH_DETACHED_SIGNATURES) {
  2206. const char *msg = NULL;
  2207. log_info(LD_DIR,"Got detached signatures (body size %d) from server %s:%d",
  2208. (int)body_len, conn->base_.address, conn->base_.port);
  2209. if (status_code != 200) {
  2210. log_warn(LD_DIR,
  2211. "Received http status code %d (%s) from server '%s:%d' while fetching "
  2212. "\"/tor/status-vote/next/consensus-signatures.z\".",
  2213. status_code, escaped(reason), conn->base_.address,
  2214. conn->base_.port);
  2215. tor_free(body); tor_free(headers); tor_free(reason);
  2216. return -1;
  2217. }
  2218. if (dirvote_add_signatures(body, conn->base_.address, &msg)<0) {
  2219. log_warn(LD_DIR, "Problem adding detached signatures from %s:%d: %s",
  2220. conn->base_.address, conn->base_.port, msg?msg:"???");
  2221. }
  2222. }
  2223. if (conn->base_.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
  2224. conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO) {
  2225. int was_ei = conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO;
  2226. smartlist_t *which = NULL;
  2227. int n_asked_for = 0;
  2228. int descriptor_digests = conn->requested_resource &&
  2229. !strcmpstart(conn->requested_resource,"d/");
  2230. log_info(LD_DIR,"Received %s (body size %d) from server '%s:%d'",
  2231. was_ei ? "extra server info" : "server info",
  2232. (int)body_len, conn->base_.address, conn->base_.port);
  2233. if (conn->requested_resource &&
  2234. (!strcmpstart(conn->requested_resource,"d/") ||
  2235. !strcmpstart(conn->requested_resource,"fp/"))) {
  2236. which = smartlist_new();
  2237. dir_split_resource_into_fingerprints(conn->requested_resource +
  2238. (descriptor_digests ? 2 : 3),
  2239. which, NULL, 0);
  2240. n_asked_for = smartlist_len(which);
  2241. }
  2242. if (status_code != 200) {
  2243. int dir_okay = status_code == 404 ||
  2244. (status_code == 400 && !strcmp(reason, "Servers unavailable."));
  2245. /* 404 means that it didn't have them; no big deal.
  2246. * Older (pre-0.1.1.8) servers said 400 Servers unavailable instead. */
  2247. log_fn(dir_okay ? LOG_INFO : LOG_WARN, LD_DIR,
  2248. "Received http status code %d (%s) from server '%s:%d' "
  2249. "while fetching \"/tor/server/%s\". I'll try again soon.",
  2250. status_code, escaped(reason), conn->base_.address,
  2251. conn->base_.port, conn->requested_resource);
  2252. if (!which) {
  2253. connection_dir_download_routerdesc_failed(conn);
  2254. } else {
  2255. dir_routerdesc_download_failed(which, status_code,
  2256. conn->router_purpose,
  2257. was_ei, descriptor_digests);
  2258. SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
  2259. smartlist_free(which);
  2260. }
  2261. tor_free(body); tor_free(headers); tor_free(reason);
  2262. return dir_okay ? 0 : -1;
  2263. }
  2264. /* Learn the routers, assuming we requested by fingerprint or "all"
  2265. * or "authority".
  2266. *
  2267. * We use "authority" to fetch our own descriptor for
  2268. * testing, and to fetch bridge descriptors for bootstrapping. Ignore
  2269. * the output of "authority" requests unless we are using bridges,
  2270. * since otherwise they'll be the response from reachability tests,
  2271. * and we don't really want to add that to our routerlist. */
  2272. if (which || (conn->requested_resource &&
  2273. (!strcmpstart(conn->requested_resource, "all") ||
  2274. (!strcmpstart(conn->requested_resource, "authority") &&
  2275. get_options()->UseBridges)))) {
  2276. /* as we learn from them, we remove them from 'which' */
  2277. if (was_ei) {
  2278. router_load_extrainfo_from_string(body, NULL, SAVED_NOWHERE, which,
  2279. descriptor_digests);
  2280. } else {
  2281. //router_load_routers_from_string(body, NULL, SAVED_NOWHERE, which,
  2282. // descriptor_digests, conn->router_purpose);
  2283. if (load_downloaded_routers(body, which, descriptor_digests,
  2284. conn->router_purpose,
  2285. conn->base_.address))
  2286. directory_info_has_arrived(now, 0, 0);
  2287. }
  2288. }
  2289. if (which) { /* mark remaining ones as failed */
  2290. log_info(LD_DIR, "Received %d/%d %s requested from %s:%d",
  2291. n_asked_for-smartlist_len(which), n_asked_for,
  2292. was_ei ? "extra-info documents" : "router descriptors",
  2293. conn->base_.address, (int)conn->base_.port);
  2294. if (smartlist_len(which)) {
  2295. dir_routerdesc_download_failed(which, status_code,
  2296. conn->router_purpose,
  2297. was_ei, descriptor_digests);
  2298. }
  2299. SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
  2300. smartlist_free(which);
  2301. }
  2302. if (directory_conn_is_self_reachability_test(conn))
  2303. router_dirport_found_reachable();
  2304. }
  2305. if (conn->base_.purpose == DIR_PURPOSE_FETCH_MICRODESC) {
  2306. smartlist_t *which = NULL;
  2307. log_info(LD_DIR,"Received answer to microdescriptor request (status %d, "
  2308. "body size %d) from server '%s:%d'",
  2309. status_code, (int)body_len, conn->base_.address,
  2310. conn->base_.port);
  2311. tor_assert(conn->requested_resource &&
  2312. !strcmpstart(conn->requested_resource, "d/"));
  2313. which = smartlist_new();
  2314. dir_split_resource_into_fingerprints(conn->requested_resource+2,
  2315. which, NULL,
  2316. DSR_DIGEST256|DSR_BASE64);
  2317. if (status_code != 200) {
  2318. log_info(LD_DIR, "Received status code %d (%s) from server "
  2319. "'%s:%d' while fetching \"/tor/micro/%s\". I'll try again "
  2320. "soon.",
  2321. status_code, escaped(reason), conn->base_.address,
  2322. (int)conn->base_.port, conn->requested_resource);
  2323. dir_microdesc_download_failed(which, status_code);
  2324. SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
  2325. smartlist_free(which);
  2326. tor_free(body); tor_free(headers); tor_free(reason);
  2327. return 0;
  2328. } else {
  2329. smartlist_t *mds;
  2330. mds = microdescs_add_to_cache(get_microdesc_cache(),
  2331. body, body+body_len, SAVED_NOWHERE, 0,
  2332. now, which);
  2333. if (smartlist_len(which)) {
  2334. /* Mark remaining ones as failed. */
  2335. dir_microdesc_download_failed(which, status_code);
  2336. }
  2337. if (mds && smartlist_len(mds)) {
  2338. control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_DESCRIPTORS,
  2339. count_loading_descriptors_progress());
  2340. directory_info_has_arrived(now, 0, 1);
  2341. }
  2342. SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
  2343. smartlist_free(which);
  2344. smartlist_free(mds);
  2345. }
  2346. }
  2347. if (conn->base_.purpose == DIR_PURPOSE_UPLOAD_DIR) {
  2348. switch (status_code) {
  2349. case 200: {
  2350. dir_server_t *ds =
  2351. router_get_trusteddirserver_by_digest(conn->identity_digest);
  2352. char *rejected_hdr = http_get_header(headers,
  2353. "X-Descriptor-Not-New: ");
  2354. if (rejected_hdr) {
  2355. if (!strcmp(rejected_hdr, "Yes")) {
  2356. log_info(LD_GENERAL,
  2357. "Authority '%s' declined our descriptor (not new)",
  2358. ds->nickname);
  2359. /* XXXX use this information; be sure to upload next one
  2360. * sooner. -NM */
  2361. /* XXXX++ On further thought, the task above implies that we're
  2362. * basing our regenerate-descriptor time on when we uploaded the
  2363. * last descriptor, not on the published time of the last
  2364. * descriptor. If those are different, that's a bad thing to
  2365. * do. -NM */
  2366. }
  2367. tor_free(rejected_hdr);
  2368. }
  2369. log_info(LD_GENERAL,"eof (status 200) after uploading server "
  2370. "descriptor: finished.");
  2371. control_event_server_status(
  2372. LOG_NOTICE, "ACCEPTED_SERVER_DESCRIPTOR DIRAUTH=%s:%d",
  2373. conn->base_.address, conn->base_.port);
  2374. ds->has_accepted_serverdesc = 1;
  2375. if (directories_have_accepted_server_descriptor())
  2376. control_event_server_status(LOG_NOTICE, "GOOD_SERVER_DESCRIPTOR");
  2377. }
  2378. break;
  2379. case 400:
  2380. log_warn(LD_GENERAL,"http status 400 (%s) response from "
  2381. "dirserver '%s:%d'. Please correct.",
  2382. escaped(reason), conn->base_.address, conn->base_.port);
  2383. control_event_server_status(LOG_WARN,
  2384. "BAD_SERVER_DESCRIPTOR DIRAUTH=%s:%d REASON=\"%s\"",
  2385. conn->base_.address, conn->base_.port, escaped(reason));
  2386. break;
  2387. default:
  2388. log_warn(LD_GENERAL,
  2389. "http status %d (%s) reason unexpected while uploading "
  2390. "descriptor to server '%s:%d').",
  2391. status_code, escaped(reason), conn->base_.address,
  2392. conn->base_.port);
  2393. break;
  2394. }
  2395. /* return 0 in all cases, since we don't want to mark any
  2396. * dirservers down just because they don't like us. */
  2397. }
  2398. if (conn->base_.purpose == DIR_PURPOSE_UPLOAD_VOTE) {
  2399. switch (status_code) {
  2400. case 200: {
  2401. log_notice(LD_DIR,"Uploaded a vote to dirserver %s:%d",
  2402. conn->base_.address, conn->base_.port);
  2403. }
  2404. break;
  2405. case 400:
  2406. log_warn(LD_DIR,"http status 400 (%s) response after uploading "
  2407. "vote to dirserver '%s:%d'. Please correct.",
  2408. escaped(reason), conn->base_.address, conn->base_.port);
  2409. break;
  2410. default:
  2411. log_warn(LD_GENERAL,
  2412. "http status %d (%s) reason unexpected while uploading "
  2413. "vote to server '%s:%d').",
  2414. status_code, escaped(reason), conn->base_.address,
  2415. conn->base_.port);
  2416. break;
  2417. }
  2418. /* return 0 in all cases, since we don't want to mark any
  2419. * dirservers down just because they don't like us. */
  2420. }
  2421. if (conn->base_.purpose == DIR_PURPOSE_UPLOAD_SIGNATURES) {
  2422. switch (status_code) {
  2423. case 200: {
  2424. log_notice(LD_DIR,"Uploaded signature(s) to dirserver %s:%d",
  2425. conn->base_.address, conn->base_.port);
  2426. }
  2427. break;
  2428. case 400:
  2429. log_warn(LD_DIR,"http status 400 (%s) response after uploading "
  2430. "signatures to dirserver '%s:%d'. Please correct.",
  2431. escaped(reason), conn->base_.address, conn->base_.port);
  2432. break;
  2433. default:
  2434. log_warn(LD_GENERAL,
  2435. "http status %d (%s) reason unexpected while uploading "
  2436. "signatures to server '%s:%d').",
  2437. status_code, escaped(reason), conn->base_.address,
  2438. conn->base_.port);
  2439. break;
  2440. }
  2441. /* return 0 in all cases, since we don't want to mark any
  2442. * dirservers down just because they don't like us. */
  2443. }
  2444. if (conn->base_.purpose == DIR_PURPOSE_FETCH_RENDDESC_V2) {
  2445. #define SEND_HS_DESC_FAILED_EVENT(reason) ( \
  2446. control_event_hs_descriptor_failed(conn->rend_data, \
  2447. conn->identity_digest, \
  2448. reason) )
  2449. #define SEND_HS_DESC_FAILED_CONTENT() ( \
  2450. control_event_hs_descriptor_content(rend_data_get_address(conn->rend_data), \
  2451. conn->requested_resource, \
  2452. conn->identity_digest, \
  2453. NULL) )
  2454. tor_assert(conn->rend_data);
  2455. log_info(LD_REND,"Received rendezvous descriptor (body size %d, status %d "
  2456. "(%s))",
  2457. (int)body_len, status_code, escaped(reason));
  2458. switch (status_code) {
  2459. case 200:
  2460. {
  2461. rend_cache_entry_t *entry = NULL;
  2462. if (rend_cache_store_v2_desc_as_client(body,
  2463. conn->requested_resource, conn->rend_data, &entry) < 0) {
  2464. log_warn(LD_REND,"Fetching v2 rendezvous descriptor failed. "
  2465. "Retrying at another directory.");
  2466. /* We'll retry when connection_about_to_close_connection()
  2467. * cleans this dir conn up. */
  2468. SEND_HS_DESC_FAILED_EVENT("BAD_DESC");
  2469. SEND_HS_DESC_FAILED_CONTENT();
  2470. } else {
  2471. char service_id[REND_SERVICE_ID_LEN_BASE32 + 1];
  2472. /* Should never be NULL here if we found the descriptor. */
  2473. tor_assert(entry);
  2474. rend_get_service_id(entry->parsed->pk, service_id);
  2475. /* success. notify pending connections about this. */
  2476. log_info(LD_REND, "Successfully fetched v2 rendezvous "
  2477. "descriptor.");
  2478. control_event_hs_descriptor_received(service_id,
  2479. conn->rend_data,
  2480. conn->identity_digest);
  2481. control_event_hs_descriptor_content(service_id,
  2482. conn->requested_resource,
  2483. conn->identity_digest,
  2484. body);
  2485. conn->base_.purpose = DIR_PURPOSE_HAS_FETCHED_RENDDESC_V2;
  2486. rend_client_desc_trynow(service_id);
  2487. memwipe(service_id, 0, sizeof(service_id));
  2488. }
  2489. break;
  2490. }
  2491. case 404:
  2492. /* Not there. We'll retry when
  2493. * connection_about_to_close_connection() cleans this conn up. */
  2494. log_info(LD_REND,"Fetching v2 rendezvous descriptor failed: "
  2495. "Retrying at another directory.");
  2496. SEND_HS_DESC_FAILED_EVENT("NOT_FOUND");
  2497. SEND_HS_DESC_FAILED_CONTENT();
  2498. break;
  2499. case 400:
  2500. log_warn(LD_REND, "Fetching v2 rendezvous descriptor failed: "
  2501. "http status 400 (%s). Dirserver didn't like our "
  2502. "v2 rendezvous query? Retrying at another directory.",
  2503. escaped(reason));
  2504. SEND_HS_DESC_FAILED_EVENT("QUERY_REJECTED");
  2505. SEND_HS_DESC_FAILED_CONTENT();
  2506. break;
  2507. default:
  2508. log_warn(LD_REND, "Fetching v2 rendezvous descriptor failed: "
  2509. "http status %d (%s) response unexpected while "
  2510. "fetching v2 hidden service descriptor (server '%s:%d'). "
  2511. "Retrying at another directory.",
  2512. status_code, escaped(reason), conn->base_.address,
  2513. conn->base_.port);
  2514. SEND_HS_DESC_FAILED_EVENT("UNEXPECTED");
  2515. SEND_HS_DESC_FAILED_CONTENT();
  2516. break;
  2517. }
  2518. }
  2519. if (conn->base_.purpose == DIR_PURPOSE_UPLOAD_RENDDESC_V2) {
  2520. #define SEND_HS_DESC_UPLOAD_FAILED_EVENT(reason) ( \
  2521. control_event_hs_descriptor_upload_failed( \
  2522. conn->identity_digest, \
  2523. rend_data_get_address(conn->rend_data), \
  2524. reason) )
  2525. log_info(LD_REND,"Uploaded rendezvous descriptor (status %d "
  2526. "(%s))",
  2527. status_code, escaped(reason));
  2528. /* Without the rend data, we'll have a problem identifying what has been
  2529. * uploaded for which service. */
  2530. tor_assert(conn->rend_data);
  2531. switch (status_code) {
  2532. case 200:
  2533. log_info(LD_REND,
  2534. "Uploading rendezvous descriptor: finished with status "
  2535. "200 (%s)", escaped(reason));
  2536. control_event_hs_descriptor_uploaded(conn->identity_digest,
  2537. rend_data_get_address(conn->rend_data));
  2538. rend_service_desc_has_uploaded(conn->rend_data);
  2539. break;
  2540. case 400:
  2541. log_warn(LD_REND,"http status 400 (%s) response from dirserver "
  2542. "'%s:%d'. Malformed rendezvous descriptor?",
  2543. escaped(reason), conn->base_.address, conn->base_.port);
  2544. SEND_HS_DESC_UPLOAD_FAILED_EVENT("UPLOAD_REJECTED");
  2545. break;
  2546. default:
  2547. log_warn(LD_REND,"http status %d (%s) response unexpected (server "
  2548. "'%s:%d').",
  2549. status_code, escaped(reason), conn->base_.address,
  2550. conn->base_.port);
  2551. SEND_HS_DESC_UPLOAD_FAILED_EVENT("UNEXPECTED");
  2552. break;
  2553. }
  2554. }
  2555. tor_free(body); tor_free(headers); tor_free(reason);
  2556. return 0;
  2557. }
  2558. /** Called when a directory connection reaches EOF. */
  2559. int
  2560. connection_dir_reached_eof(dir_connection_t *conn)
  2561. {
  2562. int retval;
  2563. if (conn->base_.state != DIR_CONN_STATE_CLIENT_READING) {
  2564. log_info(LD_HTTP,"conn reached eof, not reading. [state=%d] Closing.",
  2565. conn->base_.state);
  2566. connection_close_immediate(TO_CONN(conn)); /* error: give up on flushing */
  2567. connection_mark_for_close(TO_CONN(conn));
  2568. return -1;
  2569. }
  2570. retval = connection_dir_client_reached_eof(conn);
  2571. if (retval == 0) /* success */
  2572. conn->base_.state = DIR_CONN_STATE_CLIENT_FINISHED;
  2573. connection_mark_for_close(TO_CONN(conn));
  2574. return retval;
  2575. }
  2576. /** If any directory object is arriving, and it's over 10MB large, we're
  2577. * getting DoS'd. (As of 0.1.2.x, raw directories are about 1MB, and we never
  2578. * ask for more than 96 router descriptors at a time.)
  2579. */
  2580. #define MAX_DIRECTORY_OBJECT_SIZE (10*(1<<20))
  2581. #define MAX_VOTE_DL_SIZE (MAX_DIRECTORY_OBJECT_SIZE * 5)
  2582. /** Read handler for directory connections. (That's connections <em>to</em>
  2583. * directory servers and connections <em>at</em> directory servers.)
  2584. */
  2585. int
  2586. connection_dir_process_inbuf(dir_connection_t *conn)
  2587. {
  2588. size_t max_size;
  2589. tor_assert(conn);
  2590. tor_assert(conn->base_.type == CONN_TYPE_DIR);
  2591. /* Directory clients write, then read data until they receive EOF;
  2592. * directory servers read data until they get an HTTP command, then
  2593. * write their response (when it's finished flushing, they mark for
  2594. * close).
  2595. */
  2596. /* If we're on the dirserver side, look for a command. */
  2597. if (conn->base_.state == DIR_CONN_STATE_SERVER_COMMAND_WAIT) {
  2598. if (directory_handle_command(conn) < 0) {
  2599. connection_mark_for_close(TO_CONN(conn));
  2600. return -1;
  2601. }
  2602. return 0;
  2603. }
  2604. max_size =
  2605. (TO_CONN(conn)->purpose == DIR_PURPOSE_FETCH_STATUS_VOTE) ?
  2606. MAX_VOTE_DL_SIZE : MAX_DIRECTORY_OBJECT_SIZE;
  2607. if (connection_get_inbuf_len(TO_CONN(conn)) > max_size) {
  2608. log_warn(LD_HTTP,
  2609. "Too much data received from directory connection (%s): "
  2610. "denial of service attempt, or you need to upgrade?",
  2611. conn->base_.address);
  2612. connection_mark_for_close(TO_CONN(conn));
  2613. return -1;
  2614. }
  2615. if (!conn->base_.inbuf_reached_eof)
  2616. log_debug(LD_HTTP,"Got data, not eof. Leaving on inbuf.");
  2617. return 0;
  2618. }
  2619. /** Called when we're about to finally unlink and free a directory connection:
  2620. * perform necessary accounting and cleanup */
  2621. void
  2622. connection_dir_about_to_close(dir_connection_t *dir_conn)
  2623. {
  2624. connection_t *conn = TO_CONN(dir_conn);
  2625. if (conn->state < DIR_CONN_STATE_CLIENT_FINISHED) {
  2626. /* It's a directory connection and connecting or fetching
  2627. * failed: forget about this router, and maybe try again. */
  2628. connection_dir_request_failed(dir_conn);
  2629. }
  2630. /* If we were trying to fetch a v2 rend desc and did not succeed,
  2631. * retry as needed. (If a fetch is successful, the connection state
  2632. * is changed to DIR_PURPOSE_HAS_FETCHED_RENDDESC_V2 to mark that
  2633. * refetching is unnecessary.) */
  2634. if (conn->purpose == DIR_PURPOSE_FETCH_RENDDESC_V2 &&
  2635. dir_conn->rend_data &&
  2636. strlen(rend_data_get_address(dir_conn->rend_data)) ==
  2637. REND_SERVICE_ID_LEN_BASE32)
  2638. rend_client_refetch_v2_renddesc(dir_conn->rend_data);
  2639. }
  2640. /** Create an http response for the client <b>conn</b> out of
  2641. * <b>status</b> and <b>reason_phrase</b>. Write it to <b>conn</b>.
  2642. */
  2643. static void
  2644. write_http_status_line(dir_connection_t *conn, int status,
  2645. const char *reason_phrase)
  2646. {
  2647. char buf[256];
  2648. if (tor_snprintf(buf, sizeof(buf), "HTTP/1.0 %d %s\r\n\r\n",
  2649. status, reason_phrase ? reason_phrase : "OK") < 0) {
  2650. log_warn(LD_BUG,"status line too long.");
  2651. return;
  2652. }
  2653. log_debug(LD_DIRSERV,"Wrote status 'HTTP/1.0 %d %s'", status, reason_phrase);
  2654. connection_write_to_buf(buf, strlen(buf), TO_CONN(conn));
  2655. }
  2656. /** Write the header for an HTTP/1.0 response onto <b>conn</b>-\>outbuf,
  2657. * with <b>type</b> as the Content-Type.
  2658. *
  2659. * If <b>length</b> is nonnegative, it is the Content-Length.
  2660. * If <b>encoding</b> is provided, it is the Content-Encoding.
  2661. * If <b>cache_lifetime</b> is greater than 0, the content may be cached for
  2662. * up to cache_lifetime seconds. Otherwise, the content may not be cached. */
  2663. static void
  2664. write_http_response_header_impl(dir_connection_t *conn, ssize_t length,
  2665. const char *type, const char *encoding,
  2666. const char *extra_headers,
  2667. long cache_lifetime)
  2668. {
  2669. char date[RFC1123_TIME_LEN+1];
  2670. char tmp[1024];
  2671. char *cp;
  2672. time_t now = time(NULL);
  2673. tor_assert(conn);
  2674. format_rfc1123_time(date, now);
  2675. cp = tmp;
  2676. tor_snprintf(cp, sizeof(tmp),
  2677. "HTTP/1.0 200 OK\r\nDate: %s\r\n",
  2678. date);
  2679. cp += strlen(tmp);
  2680. if (type) {
  2681. tor_snprintf(cp, sizeof(tmp)-(cp-tmp), "Content-Type: %s\r\n", type);
  2682. cp += strlen(cp);
  2683. }
  2684. if (!is_local_addr(&conn->base_.addr)) {
  2685. /* Don't report the source address for a nearby/private connection.
  2686. * Otherwise we tend to mis-report in cases where incoming ports are
  2687. * being forwarded to a Tor server running behind the firewall. */
  2688. tor_snprintf(cp, sizeof(tmp)-(cp-tmp),
  2689. X_ADDRESS_HEADER "%s\r\n", conn->base_.address);
  2690. cp += strlen(cp);
  2691. }
  2692. if (encoding) {
  2693. tor_snprintf(cp, sizeof(tmp)-(cp-tmp),
  2694. "Content-Encoding: %s\r\n", encoding);
  2695. cp += strlen(cp);
  2696. }
  2697. if (length >= 0) {
  2698. tor_snprintf(cp, sizeof(tmp)-(cp-tmp),
  2699. "Content-Length: %ld\r\n", (long)length);
  2700. cp += strlen(cp);
  2701. }
  2702. if (cache_lifetime > 0) {
  2703. char expbuf[RFC1123_TIME_LEN+1];
  2704. format_rfc1123_time(expbuf, (time_t)(now + cache_lifetime));
  2705. /* We could say 'Cache-control: max-age=%d' here if we start doing
  2706. * http/1.1 */
  2707. tor_snprintf(cp, sizeof(tmp)-(cp-tmp),
  2708. "Expires: %s\r\n", expbuf);
  2709. cp += strlen(cp);
  2710. } else if (cache_lifetime == 0) {
  2711. /* We could say 'Cache-control: no-cache' here if we start doing
  2712. * http/1.1 */
  2713. strlcpy(cp, "Pragma: no-cache\r\n", sizeof(tmp)-(cp-tmp));
  2714. cp += strlen(cp);
  2715. }
  2716. if (extra_headers) {
  2717. strlcpy(cp, extra_headers, sizeof(tmp)-(cp-tmp));
  2718. cp += strlen(cp);
  2719. }
  2720. if (sizeof(tmp)-(cp-tmp) > 3)
  2721. memcpy(cp, "\r\n", 3);
  2722. else
  2723. tor_assert(0);
  2724. connection_write_to_buf(tmp, strlen(tmp), TO_CONN(conn));
  2725. }
  2726. /** As write_http_response_header_impl, but sets encoding and content-typed
  2727. * based on whether the response will be <b>compressed</b> or not. */
  2728. static void
  2729. write_http_response_header(dir_connection_t *conn, ssize_t length,
  2730. int compressed, long cache_lifetime)
  2731. {
  2732. write_http_response_header_impl(conn, length,
  2733. compressed?"application/octet-stream":"text/plain",
  2734. compressed?"deflate":"identity",
  2735. NULL,
  2736. cache_lifetime);
  2737. }
  2738. /** Decide whether a client would accept the consensus we have.
  2739. *
  2740. * Clients can say they only want a consensus if it's signed by more
  2741. * than half the authorities in a list. They pass this list in
  2742. * the url as "...consensus/<b>fpr</b>+<b>fpr</b>+<b>fpr</b>".
  2743. *
  2744. * <b>fpr</b> may be an abbreviated fingerprint, i.e. only a left substring
  2745. * of the full authority identity digest. (Only strings of even length,
  2746. * i.e. encodings of full bytes, are handled correctly. In the case
  2747. * of an odd number of hex digits the last one is silently ignored.)
  2748. *
  2749. * Returns 1 if more than half of the requested authorities signed the
  2750. * consensus, 0 otherwise.
  2751. */
  2752. int
  2753. client_likes_consensus(networkstatus_t *v, const char *want_url)
  2754. {
  2755. smartlist_t *want_authorities = smartlist_new();
  2756. int need_at_least;
  2757. int have = 0;
  2758. dir_split_resource_into_fingerprints(want_url, want_authorities, NULL, 0);
  2759. need_at_least = smartlist_len(want_authorities)/2+1;
  2760. SMARTLIST_FOREACH_BEGIN(want_authorities, const char *, d) {
  2761. char want_digest[DIGEST_LEN];
  2762. size_t want_len = strlen(d)/2;
  2763. if (want_len > DIGEST_LEN)
  2764. want_len = DIGEST_LEN;
  2765. if (base16_decode(want_digest, DIGEST_LEN, d, want_len*2)
  2766. != (int) want_len) {
  2767. log_fn(LOG_PROTOCOL_WARN, LD_DIR,
  2768. "Failed to decode requested authority digest %s.", escaped(d));
  2769. continue;
  2770. };
  2771. SMARTLIST_FOREACH_BEGIN(v->voters, networkstatus_voter_info_t *, vi) {
  2772. if (smartlist_len(vi->sigs) &&
  2773. tor_memeq(vi->identity_digest, want_digest, want_len)) {
  2774. have++;
  2775. break;
  2776. };
  2777. } SMARTLIST_FOREACH_END(vi);
  2778. /* early exit, if we already have enough */
  2779. if (have >= need_at_least)
  2780. break;
  2781. } SMARTLIST_FOREACH_END(d);
  2782. SMARTLIST_FOREACH(want_authorities, char *, d, tor_free(d));
  2783. smartlist_free(want_authorities);
  2784. return (have >= need_at_least);
  2785. }
  2786. /** Return the compression level we should use for sending a compressed
  2787. * response of size <b>n_bytes</b>. */
  2788. STATIC zlib_compression_level_t
  2789. choose_compression_level(ssize_t n_bytes)
  2790. {
  2791. if (! have_been_under_memory_pressure()) {
  2792. return HIGH_COMPRESSION; /* we have plenty of RAM. */
  2793. } else if (n_bytes < 0) {
  2794. return HIGH_COMPRESSION; /* unknown; might be big. */
  2795. } else if (n_bytes < 1024) {
  2796. return LOW_COMPRESSION;
  2797. } else if (n_bytes < 2048) {
  2798. return MEDIUM_COMPRESSION;
  2799. } else {
  2800. return HIGH_COMPRESSION;
  2801. }
  2802. }
  2803. /** Information passed to handle a GET request. */
  2804. typedef struct get_handler_args_t {
  2805. /** True if the client asked for compressed data. */
  2806. int compressed;
  2807. /** If nonzero, the time included an if-modified-since header with this
  2808. * value. */
  2809. time_t if_modified_since;
  2810. /** String containing the requested URL or resource. */
  2811. const char *url;
  2812. /** String containing the HTTP headers */
  2813. const char *headers;
  2814. } get_handler_args_t;
  2815. /** Entry for handling an HTTP GET request.
  2816. *
  2817. * This entry matches a request if "string" is equal to the requested
  2818. * resource, or if "is_prefix" is true and "string" is a prefix of the
  2819. * requested resource.
  2820. *
  2821. * The 'handler' function is called to handle the request. It receives
  2822. * an arguments structure, and must return 0 on success or -1 if we should
  2823. * close the connection.
  2824. **/
  2825. typedef struct url_table_ent_s {
  2826. const char *string;
  2827. int is_prefix;
  2828. int (*handler)(dir_connection_t *conn, const get_handler_args_t *args);
  2829. } url_table_ent_t;
  2830. static int handle_get_frontpage(dir_connection_t *conn,
  2831. const get_handler_args_t *args);
  2832. static int handle_get_current_consensus(dir_connection_t *conn,
  2833. const get_handler_args_t *args);
  2834. static int handle_get_status_vote(dir_connection_t *conn,
  2835. const get_handler_args_t *args);
  2836. static int handle_get_microdesc(dir_connection_t *conn,
  2837. const get_handler_args_t *args);
  2838. static int handle_get_descriptor(dir_connection_t *conn,
  2839. const get_handler_args_t *args);
  2840. static int handle_get_keys(dir_connection_t *conn,
  2841. const get_handler_args_t *args);
  2842. static int handle_get_hs_descriptor_v2(dir_connection_t *conn,
  2843. const get_handler_args_t *args);
  2844. static int handle_get_robots(dir_connection_t *conn,
  2845. const get_handler_args_t *args);
  2846. static int handle_get_networkstatus_bridges(dir_connection_t *conn,
  2847. const get_handler_args_t *args);
  2848. /** Table for handling GET requests. */
  2849. static const url_table_ent_t url_table[] = {
  2850. { "/tor/", 0, handle_get_frontpage },
  2851. { "/tor/status-vote/current/consensus", 1, handle_get_current_consensus },
  2852. { "/tor/status-vote/current/", 1, handle_get_status_vote },
  2853. { "/tor/status-vote/next/", 1, handle_get_status_vote },
  2854. { "/tor/micro/d/", 1, handle_get_microdesc },
  2855. { "/tor/server/", 1, handle_get_descriptor },
  2856. { "/tor/extra/", 1, handle_get_descriptor },
  2857. { "/tor/keys/", 1, handle_get_keys },
  2858. { "/tor/rendezvous2/", 1, handle_get_hs_descriptor_v2 },
  2859. { "/tor/hs/3/", 1, handle_get_hs_descriptor_v3 },
  2860. { "/tor/robots.txt", 0, handle_get_robots },
  2861. { "/tor/networkstatus-bridges", 0, handle_get_networkstatus_bridges },
  2862. { NULL, 0, NULL },
  2863. };
  2864. /** Helper function: called when a dirserver gets a complete HTTP GET
  2865. * request. Look for a request for a directory or for a rendezvous
  2866. * service descriptor. On finding one, write a response into
  2867. * conn-\>outbuf. If the request is unrecognized, send a 404.
  2868. * Return 0 if we handled this successfully, or -1 if we need to close
  2869. * the connection. */
  2870. MOCK_IMPL(STATIC int,
  2871. directory_handle_command_get,(dir_connection_t *conn, const char *headers,
  2872. const char *req_body, size_t req_body_len))
  2873. {
  2874. char *url, *url_mem, *header;
  2875. time_t if_modified_since = 0;
  2876. int compressed;
  2877. size_t url_len;
  2878. /* We ignore the body of a GET request. */
  2879. (void)req_body;
  2880. (void)req_body_len;
  2881. log_debug(LD_DIRSERV,"Received GET command.");
  2882. conn->base_.state = DIR_CONN_STATE_SERVER_WRITING;
  2883. if (parse_http_url(headers, &url) < 0) {
  2884. write_http_status_line(conn, 400, "Bad request");
  2885. return 0;
  2886. }
  2887. if ((header = http_get_header(headers, "If-Modified-Since: "))) {
  2888. struct tm tm;
  2889. if (parse_http_time(header, &tm) == 0) {
  2890. if (tor_timegm(&tm, &if_modified_since)<0) {
  2891. if_modified_since = 0;
  2892. } else {
  2893. log_debug(LD_DIRSERV, "If-Modified-Since is '%s'.", escaped(header));
  2894. }
  2895. }
  2896. /* The correct behavior on a malformed If-Modified-Since header is to
  2897. * act as if no If-Modified-Since header had been given. */
  2898. tor_free(header);
  2899. }
  2900. log_debug(LD_DIRSERV,"rewritten url as '%s'.", escaped(url));
  2901. url_mem = url;
  2902. url_len = strlen(url);
  2903. compressed = url_len > 2 && !strcmp(url+url_len-2, ".z");
  2904. if (compressed) {
  2905. url[url_len-2] = '\0';
  2906. url_len -= 2;
  2907. }
  2908. get_handler_args_t args;
  2909. args.url = url;
  2910. args.headers = headers;
  2911. args.if_modified_since = if_modified_since;
  2912. args.compressed = compressed;
  2913. int i, result = -1;
  2914. for (i = 0; url_table[i].string; ++i) {
  2915. int match;
  2916. if (url_table[i].is_prefix) {
  2917. match = !strcmpstart(url, url_table[i].string);
  2918. } else {
  2919. match = !strcmp(url, url_table[i].string);
  2920. }
  2921. if (match) {
  2922. result = url_table[i].handler(conn, &args);
  2923. goto done;
  2924. }
  2925. }
  2926. /* we didn't recognize the url */
  2927. write_http_status_line(conn, 404, "Not found");
  2928. result = 0;
  2929. done:
  2930. tor_free(url_mem);
  2931. return result;
  2932. }
  2933. /** Helper function for GET / or GET /tor/
  2934. */
  2935. static int
  2936. handle_get_frontpage(dir_connection_t *conn, const get_handler_args_t *args)
  2937. {
  2938. (void) args; /* unused */
  2939. const char *frontpage = get_dirportfrontpage();
  2940. if (frontpage) {
  2941. size_t dlen;
  2942. dlen = strlen(frontpage);
  2943. /* Let's return a disclaimer page (users shouldn't use V1 anymore,
  2944. and caches don't fetch '/', so this is safe). */
  2945. /* [We don't check for write_bucket_low here, since we want to serve
  2946. * this page no matter what.] */
  2947. write_http_response_header_impl(conn, dlen, "text/html", "identity",
  2948. NULL, DIRPORTFRONTPAGE_CACHE_LIFETIME);
  2949. connection_write_to_buf(frontpage, dlen, TO_CONN(conn));
  2950. } else {
  2951. write_http_status_line(conn, 404, "Not found");
  2952. }
  2953. return 0;
  2954. }
  2955. /** Warn that the consensus <b>v</b> of type <b>flavor</b> is too old and will
  2956. * not be served to clients. Rate-limit the warning to avoid logging an entry
  2957. * on every request.
  2958. */
  2959. static void
  2960. warn_consensus_is_too_old(networkstatus_t *v, const char *flavor, time_t now)
  2961. {
  2962. #define TOO_OLD_WARNING_INTERVAL (60*60)
  2963. static ratelim_t warned = RATELIM_INIT(TOO_OLD_WARNING_INTERVAL);
  2964. char timestamp[ISO_TIME_LEN+1];
  2965. char *dupes;
  2966. if ((dupes = rate_limit_log(&warned, now))) {
  2967. format_local_iso_time(timestamp, v->valid_until);
  2968. log_warn(LD_DIRSERV, "Our %s%sconsensus is too old, so we will not "
  2969. "serve it to clients. It was valid until %s local time and we "
  2970. "continued to serve it for up to 24 hours after it expired.%s",
  2971. flavor ? flavor : "", flavor ? " " : "", timestamp, dupes);
  2972. tor_free(dupes);
  2973. }
  2974. }
  2975. /** Helper function for GET /tor/status-vote/current/consensus
  2976. */
  2977. static int
  2978. handle_get_current_consensus(dir_connection_t *conn,
  2979. const get_handler_args_t *args)
  2980. {
  2981. const char *url = args->url;
  2982. const int compressed = args->compressed;
  2983. const time_t if_modified_since = args->if_modified_since;
  2984. int clear_spool = 0;
  2985. {
  2986. /* v3 network status fetch. */
  2987. long lifetime = NETWORKSTATUS_CACHE_LIFETIME;
  2988. networkstatus_t *v;
  2989. time_t now = time(NULL);
  2990. const char *want_fps = NULL;
  2991. char *flavor = NULL;
  2992. int flav = FLAV_NS;
  2993. #define CONSENSUS_URL_PREFIX "/tor/status-vote/current/consensus/"
  2994. #define CONSENSUS_FLAVORED_PREFIX "/tor/status-vote/current/consensus-"
  2995. /* figure out the flavor if any, and who we wanted to sign the thing */
  2996. if (!strcmpstart(url, CONSENSUS_FLAVORED_PREFIX)) {
  2997. const char *f, *cp;
  2998. f = url + strlen(CONSENSUS_FLAVORED_PREFIX);
  2999. cp = strchr(f, '/');
  3000. if (cp) {
  3001. want_fps = cp+1;
  3002. flavor = tor_strndup(f, cp-f);
  3003. } else {
  3004. flavor = tor_strdup(f);
  3005. }
  3006. flav = networkstatus_parse_flavor_name(flavor);
  3007. if (flav < 0)
  3008. flav = FLAV_NS;
  3009. } else {
  3010. if (!strcmpstart(url, CONSENSUS_URL_PREFIX))
  3011. want_fps = url+strlen(CONSENSUS_URL_PREFIX);
  3012. }
  3013. v = networkstatus_get_latest_consensus_by_flavor(flav);
  3014. if (v && !networkstatus_consensus_reasonably_live(v, now)) {
  3015. write_http_status_line(conn, 404, "Consensus is too old");
  3016. warn_consensus_is_too_old(v, flavor, now);
  3017. geoip_note_ns_response(GEOIP_REJECT_NOT_FOUND);
  3018. tor_free(flavor);
  3019. goto done;
  3020. }
  3021. if (v && want_fps &&
  3022. !client_likes_consensus(v, want_fps)) {
  3023. write_http_status_line(conn, 404, "Consensus not signed by sufficient "
  3024. "number of requested authorities");
  3025. geoip_note_ns_response(GEOIP_REJECT_NOT_ENOUGH_SIGS);
  3026. tor_free(flavor);
  3027. goto done;
  3028. }
  3029. conn->spool = smartlist_new();
  3030. clear_spool = 1;
  3031. {
  3032. spooled_resource_t *spooled;
  3033. if (flavor)
  3034. spooled = spooled_resource_new(DIR_SPOOL_NETWORKSTATUS,
  3035. (uint8_t*)flavor, strlen(flavor));
  3036. else
  3037. spooled = spooled_resource_new(DIR_SPOOL_NETWORKSTATUS,
  3038. NULL, 0);
  3039. tor_free(flavor);
  3040. smartlist_add(conn->spool, spooled);
  3041. }
  3042. lifetime = (v && v->fresh_until > now) ? v->fresh_until - now : 0;
  3043. if (!smartlist_len(conn->spool)) { /* we failed to create/cache cp */
  3044. write_http_status_line(conn, 503, "Network status object unavailable");
  3045. geoip_note_ns_response(GEOIP_REJECT_UNAVAILABLE);
  3046. goto done;
  3047. }
  3048. size_t size_guess = 0;
  3049. int n_expired = 0;
  3050. dirserv_spool_remove_missing_and_guess_size(conn, if_modified_since,
  3051. compressed,
  3052. &size_guess,
  3053. &n_expired);
  3054. if (!smartlist_len(conn->spool) && !n_expired) {
  3055. write_http_status_line(conn, 404, "Not found");
  3056. geoip_note_ns_response(GEOIP_REJECT_NOT_FOUND);
  3057. goto done;
  3058. } else if (!smartlist_len(conn->spool)) {
  3059. write_http_status_line(conn, 304, "Not modified");
  3060. geoip_note_ns_response(GEOIP_REJECT_NOT_MODIFIED);
  3061. goto done;
  3062. }
  3063. if (global_write_bucket_low(TO_CONN(conn), size_guess, 2)) {
  3064. log_debug(LD_DIRSERV,
  3065. "Client asked for network status lists, but we've been "
  3066. "writing too many bytes lately. Sending 503 Dir busy.");
  3067. write_http_status_line(conn, 503, "Directory busy, try again later");
  3068. geoip_note_ns_response(GEOIP_REJECT_BUSY);
  3069. goto done;
  3070. }
  3071. tor_addr_t addr;
  3072. if (tor_addr_parse(&addr, (TO_CONN(conn))->address) >= 0) {
  3073. geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS,
  3074. &addr, NULL,
  3075. time(NULL));
  3076. geoip_note_ns_response(GEOIP_SUCCESS);
  3077. /* Note that a request for a network status has started, so that we
  3078. * can measure the download time later on. */
  3079. if (conn->dirreq_id)
  3080. geoip_start_dirreq(conn->dirreq_id, size_guess, DIRREQ_TUNNELED);
  3081. else
  3082. geoip_start_dirreq(TO_CONN(conn)->global_identifier, size_guess,
  3083. DIRREQ_DIRECT);
  3084. }
  3085. clear_spool = 0;
  3086. write_http_response_header(conn, -1, compressed,
  3087. smartlist_len(conn->spool) == 1 ? lifetime : 0);
  3088. if (! compressed)
  3089. conn->zlib_state = tor_zlib_new(0, ZLIB_METHOD, HIGH_COMPRESSION);
  3090. /* Prime the connection with some data. */
  3091. const int initial_flush_result = connection_dirserv_flushed_some(conn);
  3092. tor_assert_nonfatal(initial_flush_result == 0);
  3093. goto done;
  3094. }
  3095. done:
  3096. if (clear_spool) {
  3097. dir_conn_clear_spool(conn);
  3098. }
  3099. return 0;
  3100. }
  3101. /** Helper function for GET /tor/status-vote/{current,next}/...
  3102. */
  3103. static int
  3104. handle_get_status_vote(dir_connection_t *conn, const get_handler_args_t *args)
  3105. {
  3106. const char *url = args->url;
  3107. const int compressed = args->compressed;
  3108. {
  3109. int current;
  3110. ssize_t body_len = 0;
  3111. ssize_t estimated_len = 0;
  3112. smartlist_t *items = smartlist_new();
  3113. smartlist_t *dir_items = smartlist_new();
  3114. int lifetime = 60; /* XXXX?? should actually use vote intervals. */
  3115. url += strlen("/tor/status-vote/");
  3116. current = !strcmpstart(url, "current/");
  3117. url = strchr(url, '/');
  3118. tor_assert(url);
  3119. ++url;
  3120. if (!strcmp(url, "consensus")) {
  3121. const char *item;
  3122. tor_assert(!current); /* we handle current consensus specially above,
  3123. * since it wants to be spooled. */
  3124. if ((item = dirvote_get_pending_consensus(FLAV_NS)))
  3125. smartlist_add(items, (char*)item);
  3126. } else if (!current && !strcmp(url, "consensus-signatures")) {
  3127. /* XXXX the spec says that we should implement
  3128. * current/consensus-signatures too. It doesn't seem to be needed,
  3129. * though. */
  3130. const char *item;
  3131. if ((item=dirvote_get_pending_detached_signatures()))
  3132. smartlist_add(items, (char*)item);
  3133. } else if (!strcmp(url, "authority")) {
  3134. const cached_dir_t *d;
  3135. int flags = DGV_BY_ID |
  3136. (current ? DGV_INCLUDE_PREVIOUS : DGV_INCLUDE_PENDING);
  3137. if ((d=dirvote_get_vote(NULL, flags)))
  3138. smartlist_add(dir_items, (cached_dir_t*)d);
  3139. } else {
  3140. const cached_dir_t *d;
  3141. smartlist_t *fps = smartlist_new();
  3142. int flags;
  3143. if (!strcmpstart(url, "d/")) {
  3144. url += 2;
  3145. flags = DGV_INCLUDE_PENDING | DGV_INCLUDE_PREVIOUS;
  3146. } else {
  3147. flags = DGV_BY_ID |
  3148. (current ? DGV_INCLUDE_PREVIOUS : DGV_INCLUDE_PENDING);
  3149. }
  3150. dir_split_resource_into_fingerprints(url, fps, NULL,
  3151. DSR_HEX|DSR_SORT_UNIQ);
  3152. SMARTLIST_FOREACH(fps, char *, fp, {
  3153. if ((d = dirvote_get_vote(fp, flags)))
  3154. smartlist_add(dir_items, (cached_dir_t*)d);
  3155. tor_free(fp);
  3156. });
  3157. smartlist_free(fps);
  3158. }
  3159. if (!smartlist_len(dir_items) && !smartlist_len(items)) {
  3160. write_http_status_line(conn, 404, "Not found");
  3161. goto vote_done;
  3162. }
  3163. SMARTLIST_FOREACH(dir_items, cached_dir_t *, d,
  3164. body_len += compressed ? d->dir_z_len : d->dir_len);
  3165. estimated_len += body_len;
  3166. SMARTLIST_FOREACH(items, const char *, item, {
  3167. size_t ln = strlen(item);
  3168. if (compressed) {
  3169. estimated_len += ln/2;
  3170. } else {
  3171. body_len += ln; estimated_len += ln;
  3172. }
  3173. });
  3174. if (global_write_bucket_low(TO_CONN(conn), estimated_len, 2)) {
  3175. write_http_status_line(conn, 503, "Directory busy, try again later");
  3176. goto vote_done;
  3177. }
  3178. write_http_response_header(conn, body_len ? body_len : -1, compressed,
  3179. lifetime);
  3180. if (smartlist_len(items)) {
  3181. if (compressed) {
  3182. conn->zlib_state = tor_zlib_new(1, ZLIB_METHOD,
  3183. choose_compression_level(estimated_len));
  3184. SMARTLIST_FOREACH(items, const char *, c,
  3185. connection_write_to_buf_zlib(c, strlen(c), conn, 0));
  3186. connection_write_to_buf_zlib("", 0, conn, 1);
  3187. } else {
  3188. SMARTLIST_FOREACH(items, const char *, c,
  3189. connection_write_to_buf(c, strlen(c), TO_CONN(conn)));
  3190. }
  3191. } else {
  3192. SMARTLIST_FOREACH(dir_items, cached_dir_t *, d,
  3193. connection_write_to_buf(compressed ? d->dir_z : d->dir,
  3194. compressed ? d->dir_z_len : d->dir_len,
  3195. TO_CONN(conn)));
  3196. }
  3197. vote_done:
  3198. smartlist_free(items);
  3199. smartlist_free(dir_items);
  3200. goto done;
  3201. }
  3202. done:
  3203. return 0;
  3204. }
  3205. /** Helper function for GET /tor/micro/d/...
  3206. */
  3207. static int
  3208. handle_get_microdesc(dir_connection_t *conn, const get_handler_args_t *args)
  3209. {
  3210. const char *url = args->url;
  3211. const int compressed = args->compressed;
  3212. int clear_spool = 1;
  3213. {
  3214. conn->spool = smartlist_new();
  3215. dir_split_resource_into_spoolable(url+strlen("/tor/micro/d/"),
  3216. DIR_SPOOL_MICRODESC,
  3217. conn->spool, NULL,
  3218. DSR_DIGEST256|DSR_BASE64|DSR_SORT_UNIQ);
  3219. size_t size_guess = 0;
  3220. dirserv_spool_remove_missing_and_guess_size(conn, 0, compressed,
  3221. &size_guess, NULL);
  3222. if (smartlist_len(conn->spool) == 0) {
  3223. write_http_status_line(conn, 404, "Not found");
  3224. goto done;
  3225. }
  3226. if (global_write_bucket_low(TO_CONN(conn), size_guess, 2)) {
  3227. log_info(LD_DIRSERV,
  3228. "Client asked for server descriptors, but we've been "
  3229. "writing too many bytes lately. Sending 503 Dir busy.");
  3230. write_http_status_line(conn, 503, "Directory busy, try again later");
  3231. goto done;
  3232. }
  3233. clear_spool = 0;
  3234. write_http_response_header(conn, -1, compressed, MICRODESC_CACHE_LIFETIME);
  3235. if (compressed)
  3236. conn->zlib_state = tor_zlib_new(1, ZLIB_METHOD,
  3237. choose_compression_level(size_guess));
  3238. const int initial_flush_result = connection_dirserv_flushed_some(conn);
  3239. tor_assert_nonfatal(initial_flush_result == 0);
  3240. goto done;
  3241. }
  3242. done:
  3243. if (clear_spool) {
  3244. dir_conn_clear_spool(conn);
  3245. }
  3246. return 0;
  3247. }
  3248. /** Helper function for GET /tor/{server,extra}/...
  3249. */
  3250. static int
  3251. handle_get_descriptor(dir_connection_t *conn, const get_handler_args_t *args)
  3252. {
  3253. const char *url = args->url;
  3254. const int compressed = args->compressed;
  3255. const or_options_t *options = get_options();
  3256. int clear_spool = 1;
  3257. if (!strcmpstart(url,"/tor/server/") ||
  3258. (!options->BridgeAuthoritativeDir &&
  3259. !options->BridgeRelay && !strcmpstart(url,"/tor/extra/"))) {
  3260. int res;
  3261. const char *msg = NULL;
  3262. int cache_lifetime = 0;
  3263. int is_extra = !strcmpstart(url,"/tor/extra/");
  3264. url += is_extra ? strlen("/tor/extra/") : strlen("/tor/server/");
  3265. dir_spool_source_t source;
  3266. time_t publish_cutoff = 0;
  3267. if (!strcmpstart(url, "d/")) {
  3268. source =
  3269. is_extra ? DIR_SPOOL_EXTRA_BY_DIGEST : DIR_SPOOL_SERVER_BY_DIGEST;
  3270. } else {
  3271. source =
  3272. is_extra ? DIR_SPOOL_EXTRA_BY_FP : DIR_SPOOL_SERVER_BY_FP;
  3273. /* We only want to apply a publish cutoff when we're requesting
  3274. * resources by fingerprint. */
  3275. publish_cutoff = time(NULL) - ROUTER_MAX_AGE_TO_PUBLISH;
  3276. }
  3277. conn->spool = smartlist_new();
  3278. res = dirserv_get_routerdesc_spool(conn->spool, url,
  3279. source,
  3280. connection_dir_is_encrypted(conn),
  3281. &msg);
  3282. if (!strcmpstart(url, "all")) {
  3283. cache_lifetime = FULL_DIR_CACHE_LIFETIME;
  3284. } else if (smartlist_len(conn->spool) == 1) {
  3285. cache_lifetime = ROUTERDESC_BY_DIGEST_CACHE_LIFETIME;
  3286. }
  3287. size_t size_guess = 0;
  3288. int n_expired = 0;
  3289. dirserv_spool_remove_missing_and_guess_size(conn, publish_cutoff,
  3290. compressed, &size_guess,
  3291. &n_expired);
  3292. /* If we are the bridge authority and the descriptor is a bridge
  3293. * descriptor, remember that we served this descriptor for desc stats. */
  3294. /* XXXX it's a bit of a kludge to have this here. */
  3295. if (get_options()->BridgeAuthoritativeDir &&
  3296. source == DIR_SPOOL_SERVER_BY_FP) {
  3297. SMARTLIST_FOREACH_BEGIN(conn->spool, spooled_resource_t *, spooled) {
  3298. const routerinfo_t *router =
  3299. router_get_by_id_digest((const char *)spooled->digest);
  3300. /* router can be NULL here when the bridge auth is asked for its own
  3301. * descriptor. */
  3302. if (router && router->purpose == ROUTER_PURPOSE_BRIDGE)
  3303. rep_hist_note_desc_served(router->cache_info.identity_digest);
  3304. } SMARTLIST_FOREACH_END(spooled);
  3305. }
  3306. if (res < 0 || size_guess == 0 || smartlist_len(conn->spool) == 0) {
  3307. if (msg == NULL)
  3308. msg = "Not found";
  3309. write_http_status_line(conn, 404, msg);
  3310. } else {
  3311. if (global_write_bucket_low(TO_CONN(conn), size_guess, 2)) {
  3312. log_info(LD_DIRSERV,
  3313. "Client asked for server descriptors, but we've been "
  3314. "writing too many bytes lately. Sending 503 Dir busy.");
  3315. write_http_status_line(conn, 503, "Directory busy, try again later");
  3316. dir_conn_clear_spool(conn);
  3317. goto done;
  3318. }
  3319. write_http_response_header(conn, -1, compressed, cache_lifetime);
  3320. if (compressed)
  3321. conn->zlib_state = tor_zlib_new(1, ZLIB_METHOD,
  3322. choose_compression_level(size_guess));
  3323. clear_spool = 0;
  3324. /* Prime the connection with some data. */
  3325. int initial_flush_result = connection_dirserv_flushed_some(conn);
  3326. tor_assert_nonfatal(initial_flush_result == 0);
  3327. }
  3328. goto done;
  3329. }
  3330. done:
  3331. if (clear_spool)
  3332. dir_conn_clear_spool(conn);
  3333. return 0;
  3334. }
  3335. /** Helper function for GET /tor/keys/...
  3336. */
  3337. static int
  3338. handle_get_keys(dir_connection_t *conn, const get_handler_args_t *args)
  3339. {
  3340. const char *url = args->url;
  3341. const int compressed = args->compressed;
  3342. const time_t if_modified_since = args->if_modified_since;
  3343. {
  3344. smartlist_t *certs = smartlist_new();
  3345. ssize_t len = -1;
  3346. if (!strcmp(url, "/tor/keys/all")) {
  3347. authority_cert_get_all(certs);
  3348. } else if (!strcmp(url, "/tor/keys/authority")) {
  3349. authority_cert_t *cert = get_my_v3_authority_cert();
  3350. if (cert)
  3351. smartlist_add(certs, cert);
  3352. } else if (!strcmpstart(url, "/tor/keys/fp/")) {
  3353. smartlist_t *fps = smartlist_new();
  3354. dir_split_resource_into_fingerprints(url+strlen("/tor/keys/fp/"),
  3355. fps, NULL,
  3356. DSR_HEX|DSR_SORT_UNIQ);
  3357. SMARTLIST_FOREACH(fps, char *, d, {
  3358. authority_cert_t *c = authority_cert_get_newest_by_id(d);
  3359. if (c) smartlist_add(certs, c);
  3360. tor_free(d);
  3361. });
  3362. smartlist_free(fps);
  3363. } else if (!strcmpstart(url, "/tor/keys/sk/")) {
  3364. smartlist_t *fps = smartlist_new();
  3365. dir_split_resource_into_fingerprints(url+strlen("/tor/keys/sk/"),
  3366. fps, NULL,
  3367. DSR_HEX|DSR_SORT_UNIQ);
  3368. SMARTLIST_FOREACH(fps, char *, d, {
  3369. authority_cert_t *c = authority_cert_get_by_sk_digest(d);
  3370. if (c) smartlist_add(certs, c);
  3371. tor_free(d);
  3372. });
  3373. smartlist_free(fps);
  3374. } else if (!strcmpstart(url, "/tor/keys/fp-sk/")) {
  3375. smartlist_t *fp_sks = smartlist_new();
  3376. dir_split_resource_into_fingerprint_pairs(url+strlen("/tor/keys/fp-sk/"),
  3377. fp_sks);
  3378. SMARTLIST_FOREACH(fp_sks, fp_pair_t *, pair, {
  3379. authority_cert_t *c = authority_cert_get_by_digests(pair->first,
  3380. pair->second);
  3381. if (c) smartlist_add(certs, c);
  3382. tor_free(pair);
  3383. });
  3384. smartlist_free(fp_sks);
  3385. } else {
  3386. write_http_status_line(conn, 400, "Bad request");
  3387. goto keys_done;
  3388. }
  3389. if (!smartlist_len(certs)) {
  3390. write_http_status_line(conn, 404, "Not found");
  3391. goto keys_done;
  3392. }
  3393. SMARTLIST_FOREACH(certs, authority_cert_t *, c,
  3394. if (c->cache_info.published_on < if_modified_since)
  3395. SMARTLIST_DEL_CURRENT(certs, c));
  3396. if (!smartlist_len(certs)) {
  3397. write_http_status_line(conn, 304, "Not modified");
  3398. goto keys_done;
  3399. }
  3400. len = 0;
  3401. SMARTLIST_FOREACH(certs, authority_cert_t *, c,
  3402. len += c->cache_info.signed_descriptor_len);
  3403. if (global_write_bucket_low(TO_CONN(conn), compressed?len/2:len, 2)) {
  3404. write_http_status_line(conn, 503, "Directory busy, try again later");
  3405. goto keys_done;
  3406. }
  3407. write_http_response_header(conn, compressed?-1:len, compressed, 60*60);
  3408. if (compressed) {
  3409. conn->zlib_state = tor_zlib_new(1, ZLIB_METHOD,
  3410. choose_compression_level(len));
  3411. SMARTLIST_FOREACH(certs, authority_cert_t *, c,
  3412. connection_write_to_buf_zlib(c->cache_info.signed_descriptor_body,
  3413. c->cache_info.signed_descriptor_len,
  3414. conn, 0));
  3415. connection_write_to_buf_zlib("", 0, conn, 1);
  3416. } else {
  3417. SMARTLIST_FOREACH(certs, authority_cert_t *, c,
  3418. connection_write_to_buf(c->cache_info.signed_descriptor_body,
  3419. c->cache_info.signed_descriptor_len,
  3420. TO_CONN(conn)));
  3421. }
  3422. keys_done:
  3423. smartlist_free(certs);
  3424. goto done;
  3425. }
  3426. done:
  3427. return 0;
  3428. }
  3429. /** Helper function for GET /tor/rendezvous2/
  3430. */
  3431. static int
  3432. handle_get_hs_descriptor_v2(dir_connection_t *conn,
  3433. const get_handler_args_t *args)
  3434. {
  3435. const char *url = args->url;
  3436. if (connection_dir_is_encrypted(conn)) {
  3437. /* Handle v2 rendezvous descriptor fetch request. */
  3438. const char *descp;
  3439. const char *query = url + strlen("/tor/rendezvous2/");
  3440. if (rend_valid_descriptor_id(query)) {
  3441. log_info(LD_REND, "Got a v2 rendezvous descriptor request for ID '%s'",
  3442. safe_str(escaped(query)));
  3443. switch (rend_cache_lookup_v2_desc_as_dir(query, &descp)) {
  3444. case 1: /* valid */
  3445. write_http_response_header(conn, strlen(descp), 0, 0);
  3446. connection_write_to_buf(descp, strlen(descp), TO_CONN(conn));
  3447. break;
  3448. case 0: /* well-formed but not present */
  3449. write_http_status_line(conn, 404, "Not found");
  3450. break;
  3451. case -1: /* not well-formed */
  3452. write_http_status_line(conn, 400, "Bad request");
  3453. break;
  3454. }
  3455. } else { /* not well-formed */
  3456. write_http_status_line(conn, 400, "Bad request");
  3457. }
  3458. goto done;
  3459. } else {
  3460. /* Not encrypted! */
  3461. write_http_status_line(conn, 404, "Not found");
  3462. }
  3463. done:
  3464. return 0;
  3465. }
  3466. /** Helper function for GET /tor/hs/3/<z>. Only for version 3.
  3467. */
  3468. STATIC int
  3469. handle_get_hs_descriptor_v3(dir_connection_t *conn,
  3470. const get_handler_args_t *args)
  3471. {
  3472. int retval;
  3473. const char *desc_str = NULL;
  3474. const char *pubkey_str = NULL;
  3475. const char *url = args->url;
  3476. /* Reject unencrypted dir connections */
  3477. if (!connection_dir_is_encrypted(conn)) {
  3478. write_http_status_line(conn, 404, "Not found");
  3479. goto done;
  3480. }
  3481. /* After the path prefix follows the base64 encoded blinded pubkey which we
  3482. * use to get the descriptor from the cache. Skip the prefix and get the
  3483. * pubkey. */
  3484. tor_assert(!strcmpstart(url, "/tor/hs/3/"));
  3485. pubkey_str = url + strlen("/tor/hs/3/");
  3486. retval = hs_cache_lookup_as_dir(HS_VERSION_THREE,
  3487. pubkey_str, &desc_str);
  3488. if (retval <= 0 || desc_str == NULL) {
  3489. write_http_status_line(conn, 404, "Not found");
  3490. goto done;
  3491. }
  3492. /* Found requested descriptor! Pass it to this nice client. */
  3493. write_http_response_header(conn, strlen(desc_str), 0, 0);
  3494. connection_write_to_buf(desc_str, strlen(desc_str), TO_CONN(conn));
  3495. done:
  3496. return 0;
  3497. }
  3498. /** Helper function for GET /tor/networkstatus-bridges
  3499. */
  3500. static int
  3501. handle_get_networkstatus_bridges(dir_connection_t *conn,
  3502. const get_handler_args_t *args)
  3503. {
  3504. const char *headers = args->headers;
  3505. const or_options_t *options = get_options();
  3506. if (options->BridgeAuthoritativeDir &&
  3507. options->BridgePassword_AuthDigest_ &&
  3508. connection_dir_is_encrypted(conn)) {
  3509. char *status;
  3510. char digest[DIGEST256_LEN];
  3511. char *header = http_get_header(headers, "Authorization: Basic ");
  3512. if (header)
  3513. crypto_digest256(digest, header, strlen(header), DIGEST_SHA256);
  3514. /* now make sure the password is there and right */
  3515. if (!header ||
  3516. tor_memneq(digest,
  3517. options->BridgePassword_AuthDigest_, DIGEST256_LEN)) {
  3518. write_http_status_line(conn, 404, "Not found");
  3519. tor_free(header);
  3520. goto done;
  3521. }
  3522. tor_free(header);
  3523. /* all happy now. send an answer. */
  3524. status = networkstatus_getinfo_by_purpose("bridge", time(NULL));
  3525. size_t dlen = strlen(status);
  3526. write_http_response_header(conn, dlen, 0, 0);
  3527. connection_write_to_buf(status, dlen, TO_CONN(conn));
  3528. tor_free(status);
  3529. goto done;
  3530. }
  3531. done:
  3532. return 0;
  3533. }
  3534. /** Helper function for GET robots.txt or /tor/robots.txt */
  3535. static int
  3536. handle_get_robots(dir_connection_t *conn, const get_handler_args_t *args)
  3537. {
  3538. (void)args;
  3539. {
  3540. const char robots[] = "User-agent: *\r\nDisallow: /\r\n";
  3541. size_t len = strlen(robots);
  3542. write_http_response_header(conn, len, 0, ROBOTS_CACHE_LIFETIME);
  3543. connection_write_to_buf(robots, len, TO_CONN(conn));
  3544. }
  3545. return 0;
  3546. }
  3547. /* Given the <b>url</b> from a POST request, try to extract the version number
  3548. * using the provided <b>prefix</b>. The version should be after the prefix and
  3549. * ending with the seperator "/". For instance:
  3550. * /tor/hs/3/publish
  3551. *
  3552. * On success, <b>end_pos</b> points to the position right after the version
  3553. * was found. On error, it is set to NULL.
  3554. *
  3555. * Return version on success else negative value. */
  3556. STATIC int
  3557. parse_hs_version_from_post(const char *url, const char *prefix,
  3558. const char **end_pos)
  3559. {
  3560. int ok;
  3561. unsigned long version;
  3562. const char *start;
  3563. char *end = NULL;
  3564. tor_assert(url);
  3565. tor_assert(prefix);
  3566. tor_assert(end_pos);
  3567. /* Check if the prefix does start the url. */
  3568. if (strcmpstart(url, prefix)) {
  3569. goto err;
  3570. }
  3571. /* Move pointer to the end of the prefix string. */
  3572. start = url + strlen(prefix);
  3573. /* Try this to be the HS version and if we are still at the separator, next
  3574. * will be move to the right value. */
  3575. version = tor_parse_long(start, 10, 0, INT_MAX, &ok, &end);
  3576. if (!ok) {
  3577. goto err;
  3578. }
  3579. *end_pos = end;
  3580. return (int) version;
  3581. err:
  3582. *end_pos = NULL;
  3583. return -1;
  3584. }
  3585. /* Handle the POST request for a hidden service descripror. The request is in
  3586. * <b>url</b>, the body of the request is in <b>body</b>. Return 200 on success
  3587. * else return 400 indicating a bad request. */
  3588. STATIC int
  3589. handle_post_hs_descriptor(const char *url, const char *body)
  3590. {
  3591. int version;
  3592. const char *end_pos;
  3593. tor_assert(url);
  3594. tor_assert(body);
  3595. version = parse_hs_version_from_post(url, "/tor/hs/", &end_pos);
  3596. if (version < 0) {
  3597. goto err;
  3598. }
  3599. /* We have a valid version number, now make sure it's a publish request. Use
  3600. * the end position just after the version and check for the command. */
  3601. if (strcmpstart(end_pos, "/publish")) {
  3602. goto err;
  3603. }
  3604. switch (version) {
  3605. case HS_VERSION_THREE:
  3606. if (hs_cache_store_as_dir(body) < 0) {
  3607. goto err;
  3608. }
  3609. log_info(LD_REND, "Publish request for HS descriptor handled "
  3610. "successfully.");
  3611. break;
  3612. default:
  3613. /* Unsupported version, return a bad request. */
  3614. goto err;
  3615. }
  3616. return 200;
  3617. err:
  3618. /* Bad request. */
  3619. return 400;
  3620. }
  3621. /** Helper function: called when a dirserver gets a complete HTTP POST
  3622. * request. Look for an uploaded server descriptor or rendezvous
  3623. * service descriptor. On finding one, process it and write a
  3624. * response into conn-\>outbuf. If the request is unrecognized, send a
  3625. * 400. Always return 0. */
  3626. MOCK_IMPL(STATIC int,
  3627. directory_handle_command_post,(dir_connection_t *conn, const char *headers,
  3628. const char *body, size_t body_len))
  3629. {
  3630. char *url = NULL;
  3631. const or_options_t *options = get_options();
  3632. log_debug(LD_DIRSERV,"Received POST command.");
  3633. conn->base_.state = DIR_CONN_STATE_SERVER_WRITING;
  3634. if (!public_server_mode(options)) {
  3635. log_info(LD_DIR, "Rejected dir post request from %s "
  3636. "since we're not a public relay.", conn->base_.address);
  3637. write_http_status_line(conn, 503, "Not acting as a public relay");
  3638. goto done;
  3639. }
  3640. if (parse_http_url(headers, &url) < 0) {
  3641. write_http_status_line(conn, 400, "Bad request");
  3642. return 0;
  3643. }
  3644. log_debug(LD_DIRSERV,"rewritten url as '%s'.", escaped(url));
  3645. /* Handle v2 rendezvous service publish request. */
  3646. if (connection_dir_is_encrypted(conn) &&
  3647. !strcmpstart(url,"/tor/rendezvous2/publish")) {
  3648. if (rend_cache_store_v2_desc_as_dir(body) < 0) {
  3649. log_warn(LD_REND, "Rejected v2 rend descriptor (body size %d) from %s.",
  3650. (int)body_len, conn->base_.address);
  3651. write_http_status_line(conn, 400,
  3652. "Invalid v2 service descriptor rejected");
  3653. } else {
  3654. write_http_status_line(conn, 200, "Service descriptor (v2) stored");
  3655. log_info(LD_REND, "Handled v2 rendezvous descriptor post: accepted");
  3656. }
  3657. goto done;
  3658. }
  3659. /* Handle HS descriptor publish request. */
  3660. /* XXX: This should be disabled with a consensus param until we want to
  3661. * the prop224 be deployed and thus use. */
  3662. if (connection_dir_is_encrypted(conn) && !strcmpstart(url, "/tor/hs/")) {
  3663. const char *msg = "HS descriptor stored successfully.";
  3664. /* We most probably have a publish request for an HS descriptor. */
  3665. int code = handle_post_hs_descriptor(url, body);
  3666. if (code != 200) {
  3667. msg = "Invalid HS descriptor. Rejected.";
  3668. }
  3669. write_http_status_line(conn, code, msg);
  3670. goto done;
  3671. }
  3672. if (!authdir_mode(options)) {
  3673. /* we just provide cached directories; we don't want to
  3674. * receive anything. */
  3675. write_http_status_line(conn, 400, "Nonauthoritative directory does not "
  3676. "accept posted server descriptors");
  3677. goto done;
  3678. }
  3679. if (authdir_mode_handles_descs(options, -1) &&
  3680. !strcmp(url,"/tor/")) { /* server descriptor post */
  3681. const char *msg = "[None]";
  3682. uint8_t purpose = authdir_mode_bridge(options) ?
  3683. ROUTER_PURPOSE_BRIDGE : ROUTER_PURPOSE_GENERAL;
  3684. was_router_added_t r = dirserv_add_multiple_descriptors(body, purpose,
  3685. conn->base_.address, &msg);
  3686. tor_assert(msg);
  3687. if (r == ROUTER_ADDED_NOTIFY_GENERATOR) {
  3688. /* Accepted with a message. */
  3689. log_info(LD_DIRSERV,
  3690. "Problematic router descriptor or extra-info from %s "
  3691. "(\"%s\").",
  3692. conn->base_.address, msg);
  3693. write_http_status_line(conn, 400, msg);
  3694. } else if (r == ROUTER_ADDED_SUCCESSFULLY) {
  3695. write_http_status_line(conn, 200, msg);
  3696. } else if (WRA_WAS_OUTDATED(r)) {
  3697. write_http_response_header_impl(conn, -1, NULL, NULL,
  3698. "X-Descriptor-Not-New: Yes\r\n", -1);
  3699. } else {
  3700. log_info(LD_DIRSERV,
  3701. "Rejected router descriptor or extra-info from %s "
  3702. "(\"%s\").",
  3703. conn->base_.address, msg);
  3704. write_http_status_line(conn, 400, msg);
  3705. }
  3706. goto done;
  3707. }
  3708. if (authdir_mode_v3(options) &&
  3709. !strcmp(url,"/tor/post/vote")) { /* v3 networkstatus vote */
  3710. const char *msg = "OK";
  3711. int status;
  3712. if (dirvote_add_vote(body, &msg, &status)) {
  3713. write_http_status_line(conn, status, "Vote stored");
  3714. } else {
  3715. tor_assert(msg);
  3716. log_warn(LD_DIRSERV, "Rejected vote from %s (\"%s\").",
  3717. conn->base_.address, msg);
  3718. write_http_status_line(conn, status, msg);
  3719. }
  3720. goto done;
  3721. }
  3722. if (authdir_mode_v3(options) &&
  3723. !strcmp(url,"/tor/post/consensus-signature")) { /* sigs on consensus. */
  3724. const char *msg = NULL;
  3725. if (dirvote_add_signatures(body, conn->base_.address, &msg)>=0) {
  3726. write_http_status_line(conn, 200, msg?msg:"Signatures stored");
  3727. } else {
  3728. log_warn(LD_DIR, "Unable to store signatures posted by %s: %s",
  3729. conn->base_.address, msg?msg:"???");
  3730. write_http_status_line(conn, 400, msg?msg:"Unable to store signatures");
  3731. }
  3732. goto done;
  3733. }
  3734. /* we didn't recognize the url */
  3735. write_http_status_line(conn, 404, "Not found");
  3736. done:
  3737. tor_free(url);
  3738. return 0;
  3739. }
  3740. /** Called when a dirserver receives data on a directory connection;
  3741. * looks for an HTTP request. If the request is complete, remove it
  3742. * from the inbuf, try to process it; otherwise, leave it on the
  3743. * buffer. Return a 0 on success, or -1 on error.
  3744. */
  3745. STATIC int
  3746. directory_handle_command(dir_connection_t *conn)
  3747. {
  3748. char *headers=NULL, *body=NULL;
  3749. size_t body_len=0;
  3750. int r;
  3751. tor_assert(conn);
  3752. tor_assert(conn->base_.type == CONN_TYPE_DIR);
  3753. switch (connection_fetch_from_buf_http(TO_CONN(conn),
  3754. &headers, MAX_HEADERS_SIZE,
  3755. &body, &body_len, MAX_DIR_UL_SIZE, 0)) {
  3756. case -1: /* overflow */
  3757. log_warn(LD_DIRSERV,
  3758. "Request too large from address '%s' to DirPort. Closing.",
  3759. safe_str(conn->base_.address));
  3760. return -1;
  3761. case 0:
  3762. log_debug(LD_DIRSERV,"command not all here yet.");
  3763. return 0;
  3764. /* case 1, fall through */
  3765. }
  3766. http_set_address_origin(headers, TO_CONN(conn));
  3767. // we should escape headers here as well,
  3768. // but we can't call escaped() twice, as it uses the same buffer
  3769. //log_debug(LD_DIRSERV,"headers %s, body %s.", headers, escaped(body));
  3770. if (!strncasecmp(headers,"GET",3))
  3771. r = directory_handle_command_get(conn, headers, body, body_len);
  3772. else if (!strncasecmp(headers,"POST",4))
  3773. r = directory_handle_command_post(conn, headers, body, body_len);
  3774. else {
  3775. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  3776. "Got headers %s with unknown command. Closing.",
  3777. escaped(headers));
  3778. r = -1;
  3779. }
  3780. tor_free(headers); tor_free(body);
  3781. return r;
  3782. }
  3783. /** Write handler for directory connections; called when all data has
  3784. * been flushed. Close the connection or wait for a response as
  3785. * appropriate.
  3786. */
  3787. int
  3788. connection_dir_finished_flushing(dir_connection_t *conn)
  3789. {
  3790. tor_assert(conn);
  3791. tor_assert(conn->base_.type == CONN_TYPE_DIR);
  3792. /* Note that we have finished writing the directory response. For direct
  3793. * connections this means we're done; for tunneled connections it's only
  3794. * an intermediate step. */
  3795. if (conn->dirreq_id)
  3796. geoip_change_dirreq_state(conn->dirreq_id, DIRREQ_TUNNELED,
  3797. DIRREQ_FLUSHING_DIR_CONN_FINISHED);
  3798. else
  3799. geoip_change_dirreq_state(TO_CONN(conn)->global_identifier,
  3800. DIRREQ_DIRECT,
  3801. DIRREQ_FLUSHING_DIR_CONN_FINISHED);
  3802. switch (conn->base_.state) {
  3803. case DIR_CONN_STATE_CONNECTING:
  3804. case DIR_CONN_STATE_CLIENT_SENDING:
  3805. log_debug(LD_DIR,"client finished sending command.");
  3806. conn->base_.state = DIR_CONN_STATE_CLIENT_READING;
  3807. return 0;
  3808. case DIR_CONN_STATE_SERVER_WRITING:
  3809. if (conn->spool) {
  3810. log_warn(LD_BUG, "Emptied a dirserv buffer, but it's still spooling!");
  3811. connection_mark_for_close(TO_CONN(conn));
  3812. } else {
  3813. log_debug(LD_DIRSERV, "Finished writing server response. Closing.");
  3814. connection_mark_for_close(TO_CONN(conn));
  3815. }
  3816. return 0;
  3817. default:
  3818. log_warn(LD_BUG,"called in unexpected state %d.",
  3819. conn->base_.state);
  3820. tor_fragile_assert();
  3821. return -1;
  3822. }
  3823. return 0;
  3824. }
  3825. /* We just got a new consensus! If there are other in-progress requests
  3826. * for this consensus flavor (for example because we launched several in
  3827. * parallel), cancel them.
  3828. *
  3829. * We do this check here (not just in
  3830. * connection_ap_handshake_attach_circuit()) to handle the edge case where
  3831. * a consensus fetch begins and ends before some other one tries to attach to
  3832. * a circuit, in which case the other one won't know that we're all happy now.
  3833. *
  3834. * Don't mark the conn that just gave us the consensus -- otherwise we
  3835. * would end up double-marking it when it cleans itself up.
  3836. */
  3837. static void
  3838. connection_dir_close_consensus_fetches(dir_connection_t *except_this_one,
  3839. const char *resource)
  3840. {
  3841. smartlist_t *conns_to_close =
  3842. connection_dir_list_by_purpose_and_resource(DIR_PURPOSE_FETCH_CONSENSUS,
  3843. resource);
  3844. SMARTLIST_FOREACH_BEGIN(conns_to_close, dir_connection_t *, d) {
  3845. if (d == except_this_one)
  3846. continue;
  3847. log_info(LD_DIR, "Closing consensus fetch (to %s) since one "
  3848. "has just arrived.", TO_CONN(d)->address);
  3849. connection_mark_for_close(TO_CONN(d));
  3850. } SMARTLIST_FOREACH_END(d);
  3851. smartlist_free(conns_to_close);
  3852. }
  3853. /** Connected handler for directory connections: begin sending data to the
  3854. * server, and return 0.
  3855. * Only used when connections don't immediately connect. */
  3856. int
  3857. connection_dir_finished_connecting(dir_connection_t *conn)
  3858. {
  3859. tor_assert(conn);
  3860. tor_assert(conn->base_.type == CONN_TYPE_DIR);
  3861. tor_assert(conn->base_.state == DIR_CONN_STATE_CONNECTING);
  3862. log_debug(LD_HTTP,"Dir connection to router %s:%u established.",
  3863. conn->base_.address,conn->base_.port);
  3864. /* start flushing conn */
  3865. conn->base_.state = DIR_CONN_STATE_CLIENT_SENDING;
  3866. return 0;
  3867. }
  3868. /** Decide which download schedule we want to use based on descriptor type
  3869. * in <b>dls</b> and <b>options</b>.
  3870. * Then return a list of int pointers defining download delays in seconds.
  3871. * Helper function for download_status_increment_failure(),
  3872. * download_status_reset(), and download_status_increment_attempt(). */
  3873. STATIC const smartlist_t *
  3874. find_dl_schedule(download_status_t *dls, const or_options_t *options)
  3875. {
  3876. const int dir_server = dir_server_mode(options);
  3877. const int multi_d = networkstatus_consensus_can_use_multiple_directories(
  3878. options);
  3879. const int we_are_bootstrapping = networkstatus_consensus_is_bootstrapping(
  3880. time(NULL));
  3881. const int use_fallbacks = networkstatus_consensus_can_use_extra_fallbacks(
  3882. options);
  3883. switch (dls->schedule) {
  3884. case DL_SCHED_GENERIC:
  3885. if (dir_server) {
  3886. return options->TestingServerDownloadSchedule;
  3887. } else {
  3888. return options->TestingClientDownloadSchedule;
  3889. }
  3890. case DL_SCHED_CONSENSUS:
  3891. if (!multi_d) {
  3892. return options->TestingServerConsensusDownloadSchedule;
  3893. } else {
  3894. if (we_are_bootstrapping) {
  3895. if (!use_fallbacks) {
  3896. /* A bootstrapping client without extra fallback directories */
  3897. return
  3898. options->ClientBootstrapConsensusAuthorityOnlyDownloadSchedule;
  3899. } else if (dls->want_authority) {
  3900. /* A bootstrapping client with extra fallback directories, but
  3901. * connecting to an authority */
  3902. return
  3903. options->ClientBootstrapConsensusAuthorityDownloadSchedule;
  3904. } else {
  3905. /* A bootstrapping client connecting to extra fallback directories
  3906. */
  3907. return
  3908. options->ClientBootstrapConsensusFallbackDownloadSchedule;
  3909. }
  3910. } else {
  3911. return options->TestingClientConsensusDownloadSchedule;
  3912. }
  3913. }
  3914. case DL_SCHED_BRIDGE:
  3915. return options->TestingBridgeDownloadSchedule;
  3916. default:
  3917. tor_assert(0);
  3918. }
  3919. /* Impossible, but gcc will fail with -Werror without a `return`. */
  3920. return NULL;
  3921. }
  3922. /** Decide which minimum and maximum delay step we want to use based on
  3923. * descriptor type in <b>dls</b> and <b>options</b>.
  3924. * Helper function for download_status_schedule_get_delay(). */
  3925. STATIC void
  3926. find_dl_min_and_max_delay(download_status_t *dls, const or_options_t *options,
  3927. int *min, int *max)
  3928. {
  3929. tor_assert(dls);
  3930. tor_assert(options);
  3931. tor_assert(min);
  3932. tor_assert(max);
  3933. /*
  3934. * For now, just use the existing schedule config stuff and pick the
  3935. * first/last entries off to get min/max delay for backoff purposes
  3936. */
  3937. const smartlist_t *schedule = find_dl_schedule(dls, options);
  3938. tor_assert(schedule != NULL && smartlist_len(schedule) >= 2);
  3939. *min = *((int *)(smartlist_get(schedule, 0)));
  3940. if (dls->backoff == DL_SCHED_DETERMINISTIC)
  3941. *max = *((int *)((smartlist_get(schedule, smartlist_len(schedule) - 1))));
  3942. else
  3943. *max = INT_MAX;
  3944. }
  3945. /** Advance one delay step. The algorithm is to use the previous delay to
  3946. * compute an increment, we construct a value uniformly at random between
  3947. * delay and MAX(delay*2,delay+1). We then clamp that value to be no larger
  3948. * than max_delay, and return it.
  3949. *
  3950. * Requires that delay is less than INT_MAX, and delay is in [0,max_delay].
  3951. */
  3952. STATIC int
  3953. next_random_exponential_delay(int delay, int max_delay)
  3954. {
  3955. /* Check preconditions */
  3956. if (BUG(max_delay < 0))
  3957. max_delay = 0;
  3958. if (BUG(delay > max_delay))
  3959. delay = max_delay;
  3960. if (delay == INT_MAX)
  3961. return INT_MAX; /* prevent overflow */
  3962. if (BUG(delay < 0))
  3963. delay = 0;
  3964. /* How much are we willing to add to the delay? */
  3965. int max_increment;
  3966. int multiplier = 3; /* no more than quadruple the previous delay */
  3967. if (get_options()->TestingTorNetwork) {
  3968. /* Decrease the multiplier in testing networks. This reduces the variance,
  3969. * so that bootstrap is more reliable. */
  3970. multiplier = 2; /* no more than triple the previous delay */
  3971. }
  3972. if (delay && delay < (INT_MAX-1) / multiplier) {
  3973. max_increment = delay * multiplier;
  3974. } else if (delay) {
  3975. max_increment = INT_MAX-1;
  3976. } else {
  3977. max_increment = 1;
  3978. }
  3979. if (BUG(max_increment < 1))
  3980. max_increment = 1;
  3981. /* the + 1 here is so that we always wait longer than last time. */
  3982. int increment = crypto_rand_int(max_increment)+1;
  3983. if (increment < max_delay - delay)
  3984. return delay + increment;
  3985. else
  3986. return max_delay;
  3987. }
  3988. /** Find the current delay for dls based on schedule or min_delay/
  3989. * max_delay if we're using exponential backoff. If dls->backoff is
  3990. * DL_SCHED_RANDOM_EXPONENTIAL, we must have 0 <= min_delay <= max_delay <=
  3991. * INT_MAX, but schedule may be set to NULL; otherwise schedule is required.
  3992. * This function sets dls->next_attempt_at based on now, and returns the delay.
  3993. * Helper for download_status_increment_failure and
  3994. * download_status_increment_attempt. */
  3995. STATIC int
  3996. download_status_schedule_get_delay(download_status_t *dls,
  3997. const smartlist_t *schedule,
  3998. int min_delay, int max_delay,
  3999. time_t now)
  4000. {
  4001. tor_assert(dls);
  4002. /* We don't need a schedule if we're using random exponential backoff */
  4003. tor_assert(dls->backoff == DL_SCHED_RANDOM_EXPONENTIAL ||
  4004. schedule != NULL);
  4005. /* If we're using random exponential backoff, we do need min/max delay */
  4006. tor_assert(dls->backoff != DL_SCHED_RANDOM_EXPONENTIAL ||
  4007. (min_delay >= 0 && max_delay >= min_delay));
  4008. int delay = INT_MAX;
  4009. uint8_t dls_schedule_position = (dls->increment_on
  4010. == DL_SCHED_INCREMENT_ATTEMPT
  4011. ? dls->n_download_attempts
  4012. : dls->n_download_failures);
  4013. if (dls->backoff == DL_SCHED_DETERMINISTIC) {
  4014. if (dls_schedule_position < smartlist_len(schedule))
  4015. delay = *(int *)smartlist_get(schedule, dls_schedule_position);
  4016. else if (dls_schedule_position == IMPOSSIBLE_TO_DOWNLOAD)
  4017. delay = INT_MAX;
  4018. else
  4019. delay = *(int *)smartlist_get(schedule, smartlist_len(schedule) - 1);
  4020. } else if (dls->backoff == DL_SCHED_RANDOM_EXPONENTIAL) {
  4021. /* Check if we missed a reset somehow */
  4022. IF_BUG_ONCE(dls->last_backoff_position > dls_schedule_position) {
  4023. dls->last_backoff_position = 0;
  4024. dls->last_delay_used = 0;
  4025. }
  4026. if (dls_schedule_position > 0) {
  4027. delay = dls->last_delay_used;
  4028. while (dls->last_backoff_position < dls_schedule_position) {
  4029. /* Do one increment step */
  4030. delay = next_random_exponential_delay(delay, max_delay);
  4031. /* Update our position */
  4032. ++(dls->last_backoff_position);
  4033. }
  4034. } else {
  4035. /* If we're just starting out, use the minimum delay */
  4036. delay = min_delay;
  4037. }
  4038. /* Clamp it within min/max if we have them */
  4039. if (min_delay >= 0 && delay < min_delay) delay = min_delay;
  4040. if (max_delay != INT_MAX && delay > max_delay) delay = max_delay;
  4041. /* Store it for next time */
  4042. dls->last_backoff_position = dls_schedule_position;
  4043. dls->last_delay_used = delay;
  4044. }
  4045. /* A negative delay makes no sense. Knowing that delay is
  4046. * non-negative allows us to safely do the wrapping check below. */
  4047. tor_assert(delay >= 0);
  4048. /* Avoid now+delay overflowing TIME_MAX, by comparing with a subtraction
  4049. * that won't overflow (since delay is non-negative). */
  4050. if (delay < INT_MAX && now <= TIME_MAX - delay) {
  4051. dls->next_attempt_at = now+delay;
  4052. } else {
  4053. dls->next_attempt_at = TIME_MAX;
  4054. }
  4055. return delay;
  4056. }
  4057. /* Log a debug message about item, which increments on increment_action, has
  4058. * incremented dls_n_download_increments times. The message varies based on
  4059. * was_schedule_incremented (if not, not_incremented_response is logged), and
  4060. * the values of increment, dls_next_attempt_at, and now.
  4061. * Helper for download_status_increment_failure and
  4062. * download_status_increment_attempt. */
  4063. static void
  4064. download_status_log_helper(const char *item, int was_schedule_incremented,
  4065. const char *increment_action,
  4066. const char *not_incremented_response,
  4067. uint8_t dls_n_download_increments, int increment,
  4068. time_t dls_next_attempt_at, time_t now)
  4069. {
  4070. if (item) {
  4071. if (!was_schedule_incremented)
  4072. log_debug(LD_DIR, "%s %s %d time(s); I'll try again %s.",
  4073. item, increment_action, (int)dls_n_download_increments,
  4074. not_incremented_response);
  4075. else if (increment == 0)
  4076. log_debug(LD_DIR, "%s %s %d time(s); I'll try again immediately.",
  4077. item, increment_action, (int)dls_n_download_increments);
  4078. else if (dls_next_attempt_at < TIME_MAX)
  4079. log_debug(LD_DIR, "%s %s %d time(s); I'll try again in %d seconds.",
  4080. item, increment_action, (int)dls_n_download_increments,
  4081. (int)(dls_next_attempt_at-now));
  4082. else
  4083. log_debug(LD_DIR, "%s %s %d time(s); Giving up for a while.",
  4084. item, increment_action, (int)dls_n_download_increments);
  4085. }
  4086. }
  4087. /** Determine when a failed download attempt should be retried.
  4088. * Called when an attempt to download <b>dls</b> has failed with HTTP status
  4089. * <b>status_code</b>. Increment the failure count (if the code indicates a
  4090. * real failure, or if we're a server) and set <b>dls</b>-\>next_attempt_at to
  4091. * an appropriate time in the future and return it.
  4092. * If <b>dls->increment_on</b> is DL_SCHED_INCREMENT_ATTEMPT, increment the
  4093. * failure count, and return a time in the far future for the next attempt (to
  4094. * avoid an immediate retry). */
  4095. time_t
  4096. download_status_increment_failure(download_status_t *dls, int status_code,
  4097. const char *item, int server, time_t now)
  4098. {
  4099. (void) status_code; // XXXX no longer used.
  4100. (void) server; // XXXX no longer used.
  4101. int increment = -1;
  4102. int min_delay = 0, max_delay = INT_MAX;
  4103. tor_assert(dls);
  4104. /* count the failure */
  4105. if (dls->n_download_failures < IMPOSSIBLE_TO_DOWNLOAD-1) {
  4106. ++dls->n_download_failures;
  4107. }
  4108. if (dls->increment_on == DL_SCHED_INCREMENT_FAILURE) {
  4109. /* We don't find out that a failure-based schedule has attempted a
  4110. * connection until that connection fails.
  4111. * We'll never find out about successful connections, but this doesn't
  4112. * matter, because schedules are reset after a successful download.
  4113. */
  4114. if (dls->n_download_attempts < IMPOSSIBLE_TO_DOWNLOAD-1)
  4115. ++dls->n_download_attempts;
  4116. /* only return a failure retry time if this schedule increments on failures
  4117. */
  4118. const smartlist_t *schedule = find_dl_schedule(dls, get_options());
  4119. find_dl_min_and_max_delay(dls, get_options(), &min_delay, &max_delay);
  4120. increment = download_status_schedule_get_delay(dls, schedule,
  4121. min_delay, max_delay, now);
  4122. }
  4123. download_status_log_helper(item, !dls->increment_on, "failed",
  4124. "concurrently", dls->n_download_failures,
  4125. increment, dls->next_attempt_at, now);
  4126. if (dls->increment_on == DL_SCHED_INCREMENT_ATTEMPT) {
  4127. /* stop this schedule retrying on failure, it will launch concurrent
  4128. * connections instead */
  4129. return TIME_MAX;
  4130. } else {
  4131. return dls->next_attempt_at;
  4132. }
  4133. }
  4134. /** Determine when the next download attempt should be made when using an
  4135. * attempt-based (potentially concurrent) download schedule.
  4136. * Called when an attempt to download <b>dls</b> is being initiated.
  4137. * Increment the attempt count and set <b>dls</b>-\>next_attempt_at to an
  4138. * appropriate time in the future and return it.
  4139. * If <b>dls->increment_on</b> is DL_SCHED_INCREMENT_FAILURE, don't increment
  4140. * the attempts, and return a time in the far future (to avoid launching a
  4141. * concurrent attempt). */
  4142. time_t
  4143. download_status_increment_attempt(download_status_t *dls, const char *item,
  4144. time_t now)
  4145. {
  4146. int delay = -1;
  4147. int min_delay = 0, max_delay = INT_MAX;
  4148. tor_assert(dls);
  4149. if (dls->increment_on == DL_SCHED_INCREMENT_FAILURE) {
  4150. /* this schedule should retry on failure, and not launch any concurrent
  4151. attempts */
  4152. log_warn(LD_BUG, "Tried to launch an attempt-based connection on a "
  4153. "failure-based schedule.");
  4154. return TIME_MAX;
  4155. }
  4156. if (dls->n_download_attempts < IMPOSSIBLE_TO_DOWNLOAD-1)
  4157. ++dls->n_download_attempts;
  4158. const smartlist_t *schedule = find_dl_schedule(dls, get_options());
  4159. find_dl_min_and_max_delay(dls, get_options(), &min_delay, &max_delay);
  4160. delay = download_status_schedule_get_delay(dls, schedule,
  4161. min_delay, max_delay, now);
  4162. download_status_log_helper(item, dls->increment_on, "attempted",
  4163. "on failure", dls->n_download_attempts,
  4164. delay, dls->next_attempt_at, now);
  4165. return dls->next_attempt_at;
  4166. }
  4167. /** Reset <b>dls</b> so that it will be considered downloadable
  4168. * immediately, and/or to show that we don't need it anymore.
  4169. *
  4170. * Must be called to initialise a download schedule, otherwise the zeroth item
  4171. * in the schedule will never be used.
  4172. *
  4173. * (We find the zeroth element of the download schedule, and set
  4174. * next_attempt_at to be the appropriate offset from 'now'. In most
  4175. * cases this means setting it to 'now', so the item will be immediately
  4176. * downloadable; in the case of bridge descriptors, the zeroth element
  4177. * is an hour from now.) */
  4178. void
  4179. download_status_reset(download_status_t *dls)
  4180. {
  4181. if (dls->n_download_failures == IMPOSSIBLE_TO_DOWNLOAD
  4182. || dls->n_download_attempts == IMPOSSIBLE_TO_DOWNLOAD)
  4183. return; /* Don't reset this. */
  4184. const smartlist_t *schedule = find_dl_schedule(dls, get_options());
  4185. dls->n_download_failures = 0;
  4186. dls->n_download_attempts = 0;
  4187. dls->next_attempt_at = time(NULL) + *(int *)smartlist_get(schedule, 0);
  4188. dls->last_backoff_position = 0;
  4189. dls->last_delay_used = 0;
  4190. /* Don't reset dls->want_authority or dls->increment_on */
  4191. }
  4192. /** Return the number of failures on <b>dls</b> since the last success (if
  4193. * any). */
  4194. int
  4195. download_status_get_n_failures(const download_status_t *dls)
  4196. {
  4197. return dls->n_download_failures;
  4198. }
  4199. /** Return the number of attempts to download <b>dls</b> since the last success
  4200. * (if any). This can differ from download_status_get_n_failures() due to
  4201. * outstanding concurrent attempts. */
  4202. int
  4203. download_status_get_n_attempts(const download_status_t *dls)
  4204. {
  4205. return dls->n_download_attempts;
  4206. }
  4207. /** Return the next time to attempt to download <b>dls</b>. */
  4208. time_t
  4209. download_status_get_next_attempt_at(const download_status_t *dls)
  4210. {
  4211. return dls->next_attempt_at;
  4212. }
  4213. /** Called when one or more routerdesc (or extrainfo, if <b>was_extrainfo</b>)
  4214. * fetches have failed (with uppercase fingerprints listed in <b>failed</b>,
  4215. * either as descriptor digests or as identity digests based on
  4216. * <b>was_descriptor_digests</b>).
  4217. */
  4218. static void
  4219. dir_routerdesc_download_failed(smartlist_t *failed, int status_code,
  4220. int router_purpose,
  4221. int was_extrainfo, int was_descriptor_digests)
  4222. {
  4223. char digest[DIGEST_LEN];
  4224. time_t now = time(NULL);
  4225. int server = directory_fetches_from_authorities(get_options());
  4226. if (!was_descriptor_digests) {
  4227. if (router_purpose == ROUTER_PURPOSE_BRIDGE) {
  4228. tor_assert(!was_extrainfo);
  4229. connection_dir_retry_bridges(failed);
  4230. }
  4231. return; /* FFFF should implement for other-than-router-purpose someday */
  4232. }
  4233. SMARTLIST_FOREACH_BEGIN(failed, const char *, cp) {
  4234. download_status_t *dls = NULL;
  4235. if (base16_decode(digest, DIGEST_LEN, cp, strlen(cp)) != DIGEST_LEN) {
  4236. log_warn(LD_BUG, "Malformed fingerprint in list: %s", escaped(cp));
  4237. continue;
  4238. }
  4239. if (was_extrainfo) {
  4240. signed_descriptor_t *sd =
  4241. router_get_by_extrainfo_digest(digest);
  4242. if (sd)
  4243. dls = &sd->ei_dl_status;
  4244. } else {
  4245. dls = router_get_dl_status_by_descriptor_digest(digest);
  4246. }
  4247. if (!dls || dls->n_download_failures >=
  4248. get_options()->TestingDescriptorMaxDownloadTries)
  4249. continue;
  4250. download_status_increment_failure(dls, status_code, cp, server, now);
  4251. } SMARTLIST_FOREACH_END(cp);
  4252. /* No need to relaunch descriptor downloads here: we already do it
  4253. * every 10 or 60 seconds (FOO_DESCRIPTOR_RETRY_INTERVAL) in main.c. */
  4254. }
  4255. /** Called when a connection to download microdescriptors has failed in whole
  4256. * or in part. <b>failed</b> is a list of every microdesc digest we didn't
  4257. * get. <b>status_code</b> is the http status code we received. Reschedule the
  4258. * microdesc downloads as appropriate. */
  4259. static void
  4260. dir_microdesc_download_failed(smartlist_t *failed,
  4261. int status_code)
  4262. {
  4263. networkstatus_t *consensus
  4264. = networkstatus_get_latest_consensus_by_flavor(FLAV_MICRODESC);
  4265. routerstatus_t *rs;
  4266. download_status_t *dls;
  4267. time_t now = time(NULL);
  4268. int server = directory_fetches_from_authorities(get_options());
  4269. if (! consensus)
  4270. return;
  4271. SMARTLIST_FOREACH_BEGIN(failed, const char *, d) {
  4272. rs = router_get_mutable_consensus_status_by_descriptor_digest(consensus,d);
  4273. if (!rs)
  4274. continue;
  4275. dls = &rs->dl_status;
  4276. if (dls->n_download_failures >=
  4277. get_options()->TestingMicrodescMaxDownloadTries)
  4278. continue;
  4279. {
  4280. char buf[BASE64_DIGEST256_LEN+1];
  4281. digest256_to_base64(buf, d);
  4282. download_status_increment_failure(dls, status_code, buf,
  4283. server, now);
  4284. }
  4285. } SMARTLIST_FOREACH_END(d);
  4286. }
  4287. /** Helper. Compare two fp_pair_t objects, and return negative, 0, or
  4288. * positive as appropriate. */
  4289. static int
  4290. compare_pairs_(const void **a, const void **b)
  4291. {
  4292. const fp_pair_t *fp1 = *a, *fp2 = *b;
  4293. int r;
  4294. if ((r = fast_memcmp(fp1->first, fp2->first, DIGEST_LEN)))
  4295. return r;
  4296. else
  4297. return fast_memcmp(fp1->second, fp2->second, DIGEST_LEN);
  4298. }
  4299. /** Divide a string <b>res</b> of the form FP1-FP2+FP3-FP4...[.z], where each
  4300. * FP is a hex-encoded fingerprint, into a sequence of distinct sorted
  4301. * fp_pair_t. Skip malformed pairs. On success, return 0 and add those
  4302. * fp_pair_t into <b>pairs_out</b>. On failure, return -1. */
  4303. int
  4304. dir_split_resource_into_fingerprint_pairs(const char *res,
  4305. smartlist_t *pairs_out)
  4306. {
  4307. smartlist_t *pairs_tmp = smartlist_new();
  4308. smartlist_t *pairs_result = smartlist_new();
  4309. smartlist_split_string(pairs_tmp, res, "+", 0, 0);
  4310. if (smartlist_len(pairs_tmp)) {
  4311. char *last = smartlist_get(pairs_tmp,smartlist_len(pairs_tmp)-1);
  4312. size_t last_len = strlen(last);
  4313. if (last_len > 2 && !strcmp(last+last_len-2, ".z")) {
  4314. last[last_len-2] = '\0';
  4315. }
  4316. }
  4317. SMARTLIST_FOREACH_BEGIN(pairs_tmp, char *, cp) {
  4318. if (strlen(cp) != HEX_DIGEST_LEN*2+1) {
  4319. log_info(LD_DIR,
  4320. "Skipping digest pair %s with non-standard length.", escaped(cp));
  4321. } else if (cp[HEX_DIGEST_LEN] != '-') {
  4322. log_info(LD_DIR,
  4323. "Skipping digest pair %s with missing dash.", escaped(cp));
  4324. } else {
  4325. fp_pair_t pair;
  4326. if (base16_decode(pair.first, DIGEST_LEN,
  4327. cp, HEX_DIGEST_LEN) != DIGEST_LEN ||
  4328. base16_decode(pair.second,DIGEST_LEN,
  4329. cp+HEX_DIGEST_LEN+1, HEX_DIGEST_LEN) != DIGEST_LEN) {
  4330. log_info(LD_DIR, "Skipping non-decodable digest pair %s", escaped(cp));
  4331. } else {
  4332. smartlist_add(pairs_result, tor_memdup(&pair, sizeof(pair)));
  4333. }
  4334. }
  4335. tor_free(cp);
  4336. } SMARTLIST_FOREACH_END(cp);
  4337. smartlist_free(pairs_tmp);
  4338. /* Uniq-and-sort */
  4339. smartlist_sort(pairs_result, compare_pairs_);
  4340. smartlist_uniq(pairs_result, compare_pairs_, tor_free_);
  4341. smartlist_add_all(pairs_out, pairs_result);
  4342. smartlist_free(pairs_result);
  4343. return 0;
  4344. }
  4345. /** Given a directory <b>resource</b> request, containing zero
  4346. * or more strings separated by plus signs, followed optionally by ".z", store
  4347. * the strings, in order, into <b>fp_out</b>. If <b>compressed_out</b> is
  4348. * non-NULL, set it to 1 if the resource ends in ".z", else set it to 0.
  4349. *
  4350. * If (flags & DSR_HEX), then delete all elements that aren't hex digests, and
  4351. * decode the rest. If (flags & DSR_BASE64), then use "-" rather than "+" as
  4352. * a separator, delete all the elements that aren't base64-encoded digests,
  4353. * and decode the rest. If (flags & DSR_DIGEST256), these digests should be
  4354. * 256 bits long; else they should be 160.
  4355. *
  4356. * If (flags & DSR_SORT_UNIQ), then sort the list and remove all duplicates.
  4357. */
  4358. int
  4359. dir_split_resource_into_fingerprints(const char *resource,
  4360. smartlist_t *fp_out, int *compressed_out,
  4361. int flags)
  4362. {
  4363. const int decode_hex = flags & DSR_HEX;
  4364. const int decode_base64 = flags & DSR_BASE64;
  4365. const int digests_are_256 = flags & DSR_DIGEST256;
  4366. const int sort_uniq = flags & DSR_SORT_UNIQ;
  4367. const int digest_len = digests_are_256 ? DIGEST256_LEN : DIGEST_LEN;
  4368. const int hex_digest_len = digests_are_256 ?
  4369. HEX_DIGEST256_LEN : HEX_DIGEST_LEN;
  4370. const int base64_digest_len = digests_are_256 ?
  4371. BASE64_DIGEST256_LEN : BASE64_DIGEST_LEN;
  4372. smartlist_t *fp_tmp = smartlist_new();
  4373. tor_assert(!(decode_hex && decode_base64));
  4374. tor_assert(fp_out);
  4375. smartlist_split_string(fp_tmp, resource, decode_base64?"-":"+", 0, 0);
  4376. if (compressed_out)
  4377. *compressed_out = 0;
  4378. if (smartlist_len(fp_tmp)) {
  4379. char *last = smartlist_get(fp_tmp,smartlist_len(fp_tmp)-1);
  4380. size_t last_len = strlen(last);
  4381. if (last_len > 2 && !strcmp(last+last_len-2, ".z")) {
  4382. last[last_len-2] = '\0';
  4383. if (compressed_out)
  4384. *compressed_out = 1;
  4385. }
  4386. }
  4387. if (decode_hex || decode_base64) {
  4388. const size_t encoded_len = decode_hex ? hex_digest_len : base64_digest_len;
  4389. int i;
  4390. char *cp, *d = NULL;
  4391. for (i = 0; i < smartlist_len(fp_tmp); ++i) {
  4392. cp = smartlist_get(fp_tmp, i);
  4393. if (strlen(cp) != encoded_len) {
  4394. log_info(LD_DIR,
  4395. "Skipping digest %s with non-standard length.", escaped(cp));
  4396. smartlist_del_keeporder(fp_tmp, i--);
  4397. goto again;
  4398. }
  4399. d = tor_malloc_zero(digest_len);
  4400. if (decode_hex ?
  4401. (base16_decode(d, digest_len, cp, hex_digest_len) != digest_len) :
  4402. (base64_decode(d, digest_len, cp, base64_digest_len)
  4403. != digest_len)) {
  4404. log_info(LD_DIR, "Skipping non-decodable digest %s", escaped(cp));
  4405. smartlist_del_keeporder(fp_tmp, i--);
  4406. goto again;
  4407. }
  4408. smartlist_set(fp_tmp, i, d);
  4409. d = NULL;
  4410. again:
  4411. tor_free(cp);
  4412. tor_free(d);
  4413. }
  4414. }
  4415. if (sort_uniq) {
  4416. if (decode_hex || decode_base64) {
  4417. if (digests_are_256) {
  4418. smartlist_sort_digests256(fp_tmp);
  4419. smartlist_uniq_digests256(fp_tmp);
  4420. } else {
  4421. smartlist_sort_digests(fp_tmp);
  4422. smartlist_uniq_digests(fp_tmp);
  4423. }
  4424. } else {
  4425. smartlist_sort_strings(fp_tmp);
  4426. smartlist_uniq_strings(fp_tmp);
  4427. }
  4428. }
  4429. smartlist_add_all(fp_out, fp_tmp);
  4430. smartlist_free(fp_tmp);
  4431. return 0;
  4432. }
  4433. /** As dir_split_resource_into_fingerprints, but instead fills
  4434. * <b>spool_out</b> with a list of spoolable_resource_t for the resource
  4435. * identified through <b>source</b>. */
  4436. int
  4437. dir_split_resource_into_spoolable(const char *resource,
  4438. dir_spool_source_t source,
  4439. smartlist_t *spool_out,
  4440. int *compressed_out,
  4441. int flags)
  4442. {
  4443. smartlist_t *fingerprints = smartlist_new();
  4444. tor_assert(flags & (DSR_HEX|DSR_BASE64));
  4445. const size_t digest_len =
  4446. (flags & DSR_DIGEST256) ? DIGEST256_LEN : DIGEST_LEN;
  4447. int r = dir_split_resource_into_fingerprints(resource, fingerprints,
  4448. compressed_out, flags);
  4449. /* This is not a very efficient implementation XXXX */
  4450. SMARTLIST_FOREACH_BEGIN(fingerprints, uint8_t *, digest) {
  4451. spooled_resource_t *spooled =
  4452. spooled_resource_new(source, digest, digest_len);
  4453. if (spooled)
  4454. smartlist_add(spool_out, spooled);
  4455. tor_free(digest);
  4456. } SMARTLIST_FOREACH_END(digest);
  4457. smartlist_free(fingerprints);
  4458. return r;
  4459. }