directory.c 205 KB

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