directory.c 139 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2015, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "or.h"
  6. #include "buffers.h"
  7. #include "circuitbuild.h"
  8. #include "config.h"
  9. #include "connection.h"
  10. #include "connection_edge.h"
  11. #include "control.h"
  12. #include "directory.h"
  13. #include "dirserv.h"
  14. #include "dirvote.h"
  15. #include "entrynodes.h"
  16. #include "geoip.h"
  17. #include "main.h"
  18. #include "microdesc.h"
  19. #include "networkstatus.h"
  20. #include "nodelist.h"
  21. #include "policies.h"
  22. #include "relay.h"
  23. #include "rendclient.h"
  24. #include "rendcommon.h"
  25. #include "rendservice.h"
  26. #include "rephist.h"
  27. #include "router.h"
  28. #include "routerlist.h"
  29. #include "routerparse.h"
  30. #include "routerset.h"
  31. #if defined(EXPORTMALLINFO) && defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
  32. #ifndef OPENBSD
  33. #include <malloc.h>
  34. #endif
  35. #endif
  36. /**
  37. * \file directory.c
  38. * \brief Code to send and fetch directories and router
  39. * descriptors via HTTP. Directories use dirserv.c to generate the
  40. * results; clients use routers.c to parse them.
  41. **/
  42. /* In-points to directory.c:
  43. *
  44. * - directory_post_to_dirservers(), called from
  45. * router_upload_dir_desc_to_dirservers() in router.c
  46. * upload_service_descriptor() in rendservice.c
  47. * - directory_get_from_dirserver(), called from
  48. * rend_client_refetch_renddesc() in rendclient.c
  49. * run_scheduled_events() in main.c
  50. * do_hup() in main.c
  51. * - connection_dir_process_inbuf(), called from
  52. * connection_process_inbuf() in connection.c
  53. * - connection_dir_finished_flushing(), called from
  54. * connection_finished_flushing() in connection.c
  55. * - connection_dir_finished_connecting(), called from
  56. * connection_finished_connecting() in connection.c
  57. */
  58. static void directory_send_command(dir_connection_t *conn,
  59. int purpose, int direct, const char *resource,
  60. const char *payload, size_t payload_len,
  61. time_t if_modified_since);
  62. static int directory_handle_command(dir_connection_t *conn);
  63. static int body_is_plausible(const char *body, size_t body_len, int purpose);
  64. static char *http_get_header(const char *headers, const char *which);
  65. static void http_set_address_origin(const char *headers, connection_t *conn);
  66. static void connection_dir_download_routerdesc_failed(dir_connection_t *conn);
  67. static void connection_dir_bridge_routerdesc_failed(dir_connection_t *conn);
  68. static void connection_dir_download_cert_failed(
  69. dir_connection_t *conn, int status_code);
  70. static void connection_dir_retry_bridges(smartlist_t *descs);
  71. static void dir_routerdesc_download_failed(smartlist_t *failed,
  72. int status_code,
  73. int router_purpose,
  74. int was_extrainfo,
  75. int was_descriptor_digests);
  76. static void dir_microdesc_download_failed(smartlist_t *failed,
  77. int status_code);
  78. static void note_client_request(int purpose, int compressed, size_t bytes);
  79. static int client_likes_consensus(networkstatus_t *v, const char *want_url);
  80. static void directory_initiate_command_rend(const tor_addr_t *addr,
  81. uint16_t or_port,
  82. uint16_t dir_port,
  83. const char *digest,
  84. uint8_t dir_purpose,
  85. uint8_t router_purpose,
  86. dir_indirection_t indirection,
  87. const char *resource,
  88. const char *payload,
  89. size_t payload_len,
  90. time_t if_modified_since,
  91. const rend_data_t *rend_query);
  92. /********* START VARIABLES **********/
  93. /** How far in the future do we allow a directory server to tell us it is
  94. * before deciding that one of us has the wrong time? */
  95. #define ALLOW_DIRECTORY_TIME_SKEW (30*60)
  96. #define X_ADDRESS_HEADER "X-Your-Address-Is: "
  97. /** HTTP cache control: how long do we tell proxies they can cache each
  98. * kind of document we serve? */
  99. #define FULL_DIR_CACHE_LIFETIME (60*60)
  100. #define RUNNINGROUTERS_CACHE_LIFETIME (20*60)
  101. #define DIRPORTFRONTPAGE_CACHE_LIFETIME (20*60)
  102. #define NETWORKSTATUS_CACHE_LIFETIME (5*60)
  103. #define ROUTERDESC_CACHE_LIFETIME (30*60)
  104. #define ROUTERDESC_BY_DIGEST_CACHE_LIFETIME (48*60*60)
  105. #define ROBOTS_CACHE_LIFETIME (24*60*60)
  106. #define MICRODESC_CACHE_LIFETIME (48*60*60)
  107. /********* END VARIABLES ************/
  108. /** Return true iff the directory purpose <b>dir_purpose</b> (and if it's
  109. * fetching descriptors, it's fetching them for <b>router_purpose</b>)
  110. * must use an anonymous connection to a directory. */
  111. STATIC int
  112. purpose_needs_anonymity(uint8_t dir_purpose, uint8_t router_purpose)
  113. {
  114. if (get_options()->AllDirActionsPrivate)
  115. return 1;
  116. if (router_purpose == ROUTER_PURPOSE_BRIDGE)
  117. return 1; /* if no circuits yet, this might break bootstrapping, but it's
  118. * needed to be safe. */
  119. if (dir_purpose == DIR_PURPOSE_UPLOAD_DIR ||
  120. dir_purpose == DIR_PURPOSE_UPLOAD_VOTE ||
  121. dir_purpose == DIR_PURPOSE_UPLOAD_SIGNATURES ||
  122. dir_purpose == DIR_PURPOSE_FETCH_STATUS_VOTE ||
  123. dir_purpose == DIR_PURPOSE_FETCH_DETACHED_SIGNATURES ||
  124. dir_purpose == DIR_PURPOSE_FETCH_CONSENSUS ||
  125. dir_purpose == DIR_PURPOSE_FETCH_CERTIFICATE ||
  126. dir_purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
  127. dir_purpose == DIR_PURPOSE_FETCH_EXTRAINFO ||
  128. dir_purpose == DIR_PURPOSE_FETCH_MICRODESC)
  129. return 0;
  130. return 1;
  131. }
  132. /** Return a newly allocated string describing <b>auth</b>. Only describes
  133. * authority features. */
  134. static char *
  135. authdir_type_to_string(dirinfo_type_t auth)
  136. {
  137. char *result;
  138. smartlist_t *lst = smartlist_new();
  139. if (auth & V3_DIRINFO)
  140. smartlist_add(lst, (void*)"V3");
  141. if (auth & BRIDGE_DIRINFO)
  142. smartlist_add(lst, (void*)"Bridge");
  143. if (smartlist_len(lst)) {
  144. result = smartlist_join_strings(lst, ", ", 0, NULL);
  145. } else {
  146. result = tor_strdup("[Not an authority]");
  147. }
  148. smartlist_free(lst);
  149. return result;
  150. }
  151. /** Return a string describing a given directory connection purpose. */
  152. static const char *
  153. dir_conn_purpose_to_string(int purpose)
  154. {
  155. switch (purpose)
  156. {
  157. case DIR_PURPOSE_UPLOAD_DIR:
  158. return "server descriptor upload";
  159. case DIR_PURPOSE_UPLOAD_VOTE:
  160. return "server vote upload";
  161. case DIR_PURPOSE_UPLOAD_SIGNATURES:
  162. return "consensus signature upload";
  163. case DIR_PURPOSE_FETCH_SERVERDESC:
  164. return "server descriptor fetch";
  165. case DIR_PURPOSE_FETCH_EXTRAINFO:
  166. return "extra-info fetch";
  167. case DIR_PURPOSE_FETCH_CONSENSUS:
  168. return "consensus network-status fetch";
  169. case DIR_PURPOSE_FETCH_CERTIFICATE:
  170. return "authority cert fetch";
  171. case DIR_PURPOSE_FETCH_STATUS_VOTE:
  172. return "status vote fetch";
  173. case DIR_PURPOSE_FETCH_DETACHED_SIGNATURES:
  174. return "consensus signature fetch";
  175. case DIR_PURPOSE_FETCH_RENDDESC_V2:
  176. return "hidden-service v2 descriptor fetch";
  177. case DIR_PURPOSE_UPLOAD_RENDDESC_V2:
  178. return "hidden-service v2 descriptor upload";
  179. case DIR_PURPOSE_FETCH_MICRODESC:
  180. return "microdescriptor fetch";
  181. }
  182. log_warn(LD_BUG, "Called with unknown purpose %d", purpose);
  183. return "(unknown)";
  184. }
  185. /** Return the requisite directory information types. */
  186. STATIC dirinfo_type_t
  187. dir_fetch_type(int dir_purpose, int router_purpose, const char *resource)
  188. {
  189. dirinfo_type_t type;
  190. switch (dir_purpose) {
  191. case DIR_PURPOSE_FETCH_EXTRAINFO:
  192. type = EXTRAINFO_DIRINFO;
  193. if (router_purpose == ROUTER_PURPOSE_BRIDGE)
  194. type |= BRIDGE_DIRINFO;
  195. else
  196. type |= V3_DIRINFO;
  197. break;
  198. case DIR_PURPOSE_FETCH_SERVERDESC:
  199. if (router_purpose == ROUTER_PURPOSE_BRIDGE)
  200. type = BRIDGE_DIRINFO;
  201. else
  202. type = V3_DIRINFO;
  203. break;
  204. case DIR_PURPOSE_FETCH_STATUS_VOTE:
  205. case DIR_PURPOSE_FETCH_DETACHED_SIGNATURES:
  206. case DIR_PURPOSE_FETCH_CERTIFICATE:
  207. type = V3_DIRINFO;
  208. break;
  209. case DIR_PURPOSE_FETCH_CONSENSUS:
  210. type = V3_DIRINFO;
  211. if (resource && !strcmp(resource, "microdesc"))
  212. type |= MICRODESC_DIRINFO;
  213. break;
  214. case DIR_PURPOSE_FETCH_MICRODESC:
  215. type = MICRODESC_DIRINFO;
  216. break;
  217. default:
  218. log_warn(LD_BUG, "Unexpected purpose %d", (int)dir_purpose);
  219. type = NO_DIRINFO;
  220. break;
  221. }
  222. return type;
  223. }
  224. /** Return true iff <b>identity_digest</b> is the digest of a router which
  225. * says that it caches extrainfos. (If <b>is_authority</b> we always
  226. * believe that to be true.) */
  227. int
  228. router_supports_extrainfo(const char *identity_digest, int is_authority)
  229. {
  230. const node_t *node = node_get_by_id(identity_digest);
  231. if (node && node->ri) {
  232. if (node->ri->caches_extra_info)
  233. return 1;
  234. }
  235. if (is_authority) {
  236. return 1;
  237. }
  238. return 0;
  239. }
  240. /** Return true iff any trusted directory authority has accepted our
  241. * server descriptor.
  242. *
  243. * We consider any authority sufficient because waiting for all of
  244. * them means it never happens while any authority is down; we don't
  245. * go for something more complex in the middle (like \>1/3 or \>1/2 or
  246. * \>=1/2) because that doesn't seem necessary yet.
  247. */
  248. int
  249. directories_have_accepted_server_descriptor(void)
  250. {
  251. const smartlist_t *servers = router_get_trusted_dir_servers();
  252. const or_options_t *options = get_options();
  253. SMARTLIST_FOREACH(servers, dir_server_t *, d, {
  254. if ((d->type & options->PublishServerDescriptor_) &&
  255. d->has_accepted_serverdesc) {
  256. return 1;
  257. }
  258. });
  259. return 0;
  260. }
  261. /** Start a connection to every suitable directory authority, using
  262. * connection purpose <b>dir_purpose</b> and uploading <b>payload</b>
  263. * (of length <b>payload_len</b>). The dir_purpose should be one of
  264. * 'DIR_PURPOSE_UPLOAD_{DIR|VOTE|SIGNATURES}'.
  265. *
  266. * <b>router_purpose</b> describes the type of descriptor we're
  267. * publishing, if we're publishing a descriptor -- e.g. general or bridge.
  268. *
  269. * <b>type</b> specifies what sort of dir authorities (V3,
  270. * BRIDGE, etc) we should upload to.
  271. *
  272. * If <b>extrainfo_len</b> is nonzero, the first <b>payload_len</b> bytes of
  273. * <b>payload</b> hold a router descriptor, and the next <b>extrainfo_len</b>
  274. * bytes of <b>payload</b> hold an extra-info document. Upload the descriptor
  275. * to all authorities, and the extra-info document to all authorities that
  276. * support it.
  277. */
  278. void
  279. directory_post_to_dirservers(uint8_t dir_purpose, uint8_t router_purpose,
  280. dirinfo_type_t type,
  281. const char *payload,
  282. size_t payload_len, size_t extrainfo_len)
  283. {
  284. const or_options_t *options = get_options();
  285. dir_indirection_t indirection;
  286. const smartlist_t *dirservers = router_get_trusted_dir_servers();
  287. int found = 0;
  288. const int exclude_self = (dir_purpose == DIR_PURPOSE_UPLOAD_VOTE ||
  289. dir_purpose == DIR_PURPOSE_UPLOAD_SIGNATURES);
  290. tor_assert(dirservers);
  291. /* This tries dirservers which we believe to be down, but ultimately, that's
  292. * harmless, and we may as well err on the side of getting things uploaded.
  293. */
  294. SMARTLIST_FOREACH_BEGIN(dirservers, dir_server_t *, ds) {
  295. routerstatus_t *rs = &(ds->fake_status);
  296. size_t upload_len = payload_len;
  297. tor_addr_t ds_addr;
  298. if ((type & ds->type) == 0)
  299. continue;
  300. if (exclude_self && router_digest_is_me(ds->digest)) {
  301. /* we don't upload to ourselves, but at least there's now at least
  302. * one authority of this type that has what we wanted to upload. */
  303. found = 1;
  304. continue;
  305. }
  306. if (options->StrictNodes &&
  307. routerset_contains_routerstatus(options->ExcludeNodes, rs, -1)) {
  308. log_warn(LD_DIR, "Wanted to contact authority '%s' for %s, but "
  309. "it's in our ExcludedNodes list and StrictNodes is set. "
  310. "Skipping.",
  311. ds->nickname,
  312. dir_conn_purpose_to_string(dir_purpose));
  313. continue;
  314. }
  315. found = 1; /* at least one authority of this type was listed */
  316. if (dir_purpose == DIR_PURPOSE_UPLOAD_DIR)
  317. ds->has_accepted_serverdesc = 0;
  318. if (extrainfo_len && router_supports_extrainfo(ds->digest, 1)) {
  319. upload_len += extrainfo_len;
  320. log_info(LD_DIR, "Uploading an extrainfo too (length %d)",
  321. (int) extrainfo_len);
  322. }
  323. tor_addr_from_ipv4h(&ds_addr, ds->addr);
  324. if (purpose_needs_anonymity(dir_purpose, router_purpose)) {
  325. indirection = DIRIND_ANONYMOUS;
  326. } else if (!fascist_firewall_allows_address_dir(&ds_addr,ds->dir_port)) {
  327. if (fascist_firewall_allows_address_or(&ds_addr,ds->or_port))
  328. indirection = DIRIND_ONEHOP;
  329. else
  330. indirection = DIRIND_ANONYMOUS;
  331. } else {
  332. indirection = DIRIND_DIRECT_CONN;
  333. }
  334. directory_initiate_command_routerstatus(rs, dir_purpose,
  335. router_purpose,
  336. indirection,
  337. NULL, payload, upload_len, 0);
  338. } SMARTLIST_FOREACH_END(ds);
  339. if (!found) {
  340. char *s = authdir_type_to_string(type);
  341. log_warn(LD_DIR, "Publishing server descriptor to directory authorities "
  342. "of type '%s', but no authorities of that type listed!", s);
  343. tor_free(s);
  344. }
  345. }
  346. /** Return true iff, according to the values in <b>options</b>, we should be
  347. * using directory guards for direct downloads of directory information. */
  348. static int
  349. should_use_directory_guards(const or_options_t *options)
  350. {
  351. /* Public (non-bridge) servers never use directory guards. */
  352. if (public_server_mode(options))
  353. return 0;
  354. /* If guards are disabled, or directory guards are disabled, we can't
  355. * use directory guards.
  356. */
  357. if (!options->UseEntryGuards || !options->UseEntryGuardsAsDirGuards)
  358. return 0;
  359. /* If we're configured to fetch directory info aggressively or of a
  360. * nonstandard type, don't use directory guards. */
  361. if (options->DownloadExtraInfo || options->FetchDirInfoEarly ||
  362. options->FetchDirInfoExtraEarly || options->FetchUselessDescriptors)
  363. return 0;
  364. return 1;
  365. }
  366. /** Pick an unconstrained directory server from among our guards, the latest
  367. * networkstatus, or the fallback dirservers, for use in downloading
  368. * information of type <b>type</b>, and return its routerstatus. */
  369. static const routerstatus_t *
  370. directory_pick_generic_dirserver(dirinfo_type_t type, int pds_flags,
  371. uint8_t dir_purpose)
  372. {
  373. const routerstatus_t *rs = NULL;
  374. const or_options_t *options = get_options();
  375. if (options->UseBridges)
  376. log_warn(LD_BUG, "Called when we have UseBridges set.");
  377. if (should_use_directory_guards(options)) {
  378. const node_t *node = choose_random_dirguard(type);
  379. if (node)
  380. rs = node->rs;
  381. } else {
  382. /* anybody with a non-zero dirport will do */
  383. rs = router_pick_directory_server(type, pds_flags);
  384. }
  385. if (!rs) {
  386. log_info(LD_DIR, "No router found for %s; falling back to "
  387. "dirserver list.", dir_conn_purpose_to_string(dir_purpose));
  388. rs = router_pick_fallback_dirserver(type, pds_flags);
  389. }
  390. return rs;
  391. }
  392. /** Start a connection to a random running directory server, using
  393. * connection purpose <b>dir_purpose</b>, intending to fetch descriptors
  394. * of purpose <b>router_purpose</b>, and requesting <b>resource</b>.
  395. * Use <b>pds_flags</b> as arguments to router_pick_directory_server()
  396. * or router_pick_trusteddirserver().
  397. */
  398. MOCK_IMPL(void, directory_get_from_dirserver, (uint8_t dir_purpose,
  399. uint8_t router_purpose,
  400. const char *resource,
  401. int pds_flags))
  402. {
  403. const routerstatus_t *rs = NULL;
  404. const or_options_t *options = get_options();
  405. int prefer_authority = directory_fetches_from_authorities(options);
  406. int require_authority = 0;
  407. int get_via_tor = purpose_needs_anonymity(dir_purpose, router_purpose);
  408. dirinfo_type_t type = dir_fetch_type(dir_purpose, router_purpose, resource);
  409. time_t if_modified_since = 0;
  410. if (type == NO_DIRINFO)
  411. return;
  412. if (dir_purpose == DIR_PURPOSE_FETCH_CONSENSUS) {
  413. int flav = FLAV_NS;
  414. networkstatus_t *v;
  415. if (resource)
  416. flav = networkstatus_parse_flavor_name(resource);
  417. /* DEFAULT_IF_MODIFIED_SINCE_DELAY is 1/20 of the default consensus
  418. * period of 1 hour.
  419. */
  420. #define DEFAULT_IF_MODIFIED_SINCE_DELAY (180)
  421. if (flav != -1) {
  422. /* IF we have a parsed consensus of this type, we can do an
  423. * if-modified-time based on it. */
  424. v = networkstatus_get_latest_consensus_by_flavor(flav);
  425. if (v) {
  426. /* In networks with particularly short V3AuthVotingIntervals,
  427. * ask for the consensus if it's been modified since half the
  428. * V3AuthVotingInterval of the most recent consensus. */
  429. time_t ims_delay = DEFAULT_IF_MODIFIED_SINCE_DELAY;
  430. if (v->fresh_until > v->valid_after
  431. && ims_delay > (v->fresh_until - v->valid_after)/2) {
  432. ims_delay = (v->fresh_until - v->valid_after)/2;
  433. }
  434. if_modified_since = v->valid_after + ims_delay;
  435. }
  436. } else {
  437. /* Otherwise it might be a consensus we don't parse, but which we
  438. * do cache. Look at the cached copy, perhaps. */
  439. cached_dir_t *cd = dirserv_get_consensus(resource);
  440. /* We have no method of determining the voting interval from an
  441. * unparsed consensus, so we use the default. */
  442. if (cd)
  443. if_modified_since = cd->published + DEFAULT_IF_MODIFIED_SINCE_DELAY;
  444. }
  445. }
  446. if (!options->FetchServerDescriptors)
  447. return;
  448. if (!get_via_tor) {
  449. if (options->UseBridges && !(type & BRIDGE_DIRINFO)) {
  450. /* We want to ask a running bridge for which we have a descriptor.
  451. *
  452. * When we ask choose_random_entry() for a bridge, we specify what
  453. * sort of dir fetch we'll be doing, so it won't return a bridge
  454. * that can't answer our question.
  455. */
  456. /* XXX024 Not all bridges handle conditional consensus downloading,
  457. * so, for now, never assume the server supports that. -PP */
  458. const node_t *node = choose_random_dirguard(type);
  459. if (node && node->ri) {
  460. /* every bridge has a routerinfo. */
  461. tor_addr_t addr;
  462. routerinfo_t *ri = node->ri;
  463. node_get_addr(node, &addr);
  464. directory_initiate_command(&addr,
  465. ri->or_port, 0/*no dirport*/,
  466. ri->cache_info.identity_digest,
  467. dir_purpose,
  468. router_purpose,
  469. DIRIND_ONEHOP,
  470. resource, NULL, 0, if_modified_since);
  471. } else
  472. log_notice(LD_DIR, "Ignoring directory request, since no bridge "
  473. "nodes are available yet.");
  474. return;
  475. } else {
  476. if (prefer_authority || (type & BRIDGE_DIRINFO)) {
  477. /* only ask authdirservers, and don't ask myself */
  478. rs = router_pick_trusteddirserver(type, pds_flags);
  479. if (rs == NULL && (pds_flags & (PDS_NO_EXISTING_SERVERDESC_FETCH|
  480. PDS_NO_EXISTING_MICRODESC_FETCH))) {
  481. /* We don't want to fetch from any authorities that we're currently
  482. * fetching server descriptors from, and we got no match. Did we
  483. * get no match because all the authorities have connections
  484. * fetching server descriptors (in which case we should just
  485. * return,) or because all the authorities are down or on fire or
  486. * unreachable or something (in which case we should go on with
  487. * our fallback code)? */
  488. pds_flags &= ~(PDS_NO_EXISTING_SERVERDESC_FETCH|
  489. PDS_NO_EXISTING_MICRODESC_FETCH);
  490. rs = router_pick_trusteddirserver(type, pds_flags);
  491. if (rs) {
  492. log_debug(LD_DIR, "Deferring serverdesc fetch: all authorities "
  493. "are in use.");
  494. return;
  495. }
  496. }
  497. if (rs == NULL && require_authority) {
  498. log_info(LD_DIR, "No authorities were available for %s: will try "
  499. "later.", dir_conn_purpose_to_string(dir_purpose));
  500. return;
  501. }
  502. }
  503. if (!rs && !(type & BRIDGE_DIRINFO)) {
  504. /* */
  505. rs = directory_pick_generic_dirserver(type, pds_flags,
  506. dir_purpose);
  507. if (!rs)
  508. get_via_tor = 1; /* last resort: try routing it via Tor */
  509. }
  510. }
  511. }
  512. if (get_via_tor) {
  513. /* Never use fascistfirewall; we're going via Tor. */
  514. pds_flags |= PDS_IGNORE_FASCISTFIREWALL;
  515. rs = router_pick_directory_server(type, pds_flags);
  516. }
  517. /* If we have any hope of building an indirect conn, we know some router
  518. * descriptors. If (rs==NULL), we can't build circuits anyway, so
  519. * there's no point in falling back to the authorities in this case. */
  520. if (rs) {
  521. const dir_indirection_t indirection =
  522. get_via_tor ? DIRIND_ANONYMOUS : DIRIND_ONEHOP;
  523. directory_initiate_command_routerstatus(rs, dir_purpose,
  524. router_purpose,
  525. indirection,
  526. resource, NULL, 0,
  527. if_modified_since);
  528. } else {
  529. log_notice(LD_DIR,
  530. "While fetching directory info, "
  531. "no running dirservers known. Will try again later. "
  532. "(purpose %d)", dir_purpose);
  533. if (!purpose_needs_anonymity(dir_purpose, router_purpose)) {
  534. /* remember we tried them all and failed. */
  535. directory_all_unreachable(time(NULL));
  536. }
  537. }
  538. }
  539. /** As directory_get_from_dirserver, but initiates a request to <i>every</i>
  540. * directory authority other than ourself. Only for use by authorities when
  541. * searching for missing information while voting. */
  542. void
  543. directory_get_from_all_authorities(uint8_t dir_purpose,
  544. uint8_t router_purpose,
  545. const char *resource)
  546. {
  547. tor_assert(dir_purpose == DIR_PURPOSE_FETCH_STATUS_VOTE ||
  548. dir_purpose == DIR_PURPOSE_FETCH_DETACHED_SIGNATURES);
  549. SMARTLIST_FOREACH_BEGIN(router_get_trusted_dir_servers(),
  550. dir_server_t *, ds) {
  551. routerstatus_t *rs;
  552. if (router_digest_is_me(ds->digest))
  553. continue;
  554. if (!(ds->type & V3_DIRINFO))
  555. continue;
  556. rs = &ds->fake_status;
  557. directory_initiate_command_routerstatus(rs, dir_purpose, router_purpose,
  558. DIRIND_ONEHOP, resource, NULL,
  559. 0, 0);
  560. } SMARTLIST_FOREACH_END(ds);
  561. }
  562. /** Return true iff <b>ind</b> requires a multihop circuit. */
  563. static int
  564. dirind_is_anon(dir_indirection_t ind)
  565. {
  566. return ind == DIRIND_ANON_DIRPORT || ind == DIRIND_ANONYMOUS;
  567. }
  568. /** Same as directory_initiate_command_routerstatus(), but accepts
  569. * rendezvous data to fetch a hidden service descriptor. */
  570. void
  571. directory_initiate_command_routerstatus_rend(const routerstatus_t *status,
  572. uint8_t dir_purpose,
  573. uint8_t router_purpose,
  574. dir_indirection_t indirection,
  575. const char *resource,
  576. const char *payload,
  577. size_t payload_len,
  578. time_t if_modified_since,
  579. const rend_data_t *rend_query)
  580. {
  581. const or_options_t *options = get_options();
  582. const node_t *node;
  583. tor_addr_t addr;
  584. const int anonymized_connection = dirind_is_anon(indirection);
  585. node = node_get_by_id(status->identity_digest);
  586. if (!node && anonymized_connection) {
  587. log_info(LD_DIR, "Not sending anonymized request to directory '%s'; we "
  588. "don't have its router descriptor.",
  589. routerstatus_describe(status));
  590. return;
  591. }
  592. tor_addr_from_ipv4h(&addr, status->addr);
  593. if (options->ExcludeNodes && options->StrictNodes &&
  594. routerset_contains_routerstatus(options->ExcludeNodes, status, -1)) {
  595. log_warn(LD_DIR, "Wanted to contact directory mirror %s for %s, but "
  596. "it's in our ExcludedNodes list and StrictNodes is set. "
  597. "Skipping. This choice might make your Tor not work.",
  598. routerstatus_describe(status),
  599. dir_conn_purpose_to_string(dir_purpose));
  600. return;
  601. }
  602. directory_initiate_command_rend(&addr,
  603. status->or_port, status->dir_port,
  604. status->identity_digest,
  605. dir_purpose, router_purpose,
  606. indirection, resource,
  607. payload, payload_len, if_modified_since,
  608. rend_query);
  609. }
  610. /** Launch a new connection to the directory server <b>status</b> to
  611. * upload or download a server or rendezvous
  612. * descriptor. <b>dir_purpose</b> determines what
  613. * kind of directory connection we're launching, and must be one of
  614. * DIR_PURPOSE_{FETCH|UPLOAD}_{DIR|RENDDESC_V2}. <b>router_purpose</b>
  615. * specifies the descriptor purposes we have in mind (currently only
  616. * used for FETCH_DIR).
  617. *
  618. * When uploading, <b>payload</b> and <b>payload_len</b> determine the content
  619. * of the HTTP post. Otherwise, <b>payload</b> should be NULL.
  620. *
  621. * When fetching a rendezvous descriptor, <b>resource</b> is the service ID we
  622. * want to fetch.
  623. */
  624. void
  625. directory_initiate_command_routerstatus(const routerstatus_t *status,
  626. uint8_t dir_purpose,
  627. uint8_t router_purpose,
  628. dir_indirection_t indirection,
  629. const char *resource,
  630. const char *payload,
  631. size_t payload_len,
  632. time_t if_modified_since)
  633. {
  634. directory_initiate_command_routerstatus_rend(status, dir_purpose,
  635. router_purpose,
  636. indirection, resource,
  637. payload, payload_len,
  638. if_modified_since, NULL);
  639. }
  640. /** Return true iff <b>conn</b> is the client side of a directory connection
  641. * we launched to ourself in order to determine the reachability of our
  642. * dir_port. */
  643. static int
  644. directory_conn_is_self_reachability_test(dir_connection_t *conn)
  645. {
  646. if (conn->requested_resource &&
  647. !strcmpstart(conn->requested_resource,"authority")) {
  648. const routerinfo_t *me = router_get_my_routerinfo();
  649. if (me &&
  650. router_digest_is_me(conn->identity_digest) &&
  651. tor_addr_eq_ipv4h(&conn->base_.addr, me->addr) && /*XXXX prop 118*/
  652. me->dir_port == conn->base_.port)
  653. return 1;
  654. }
  655. return 0;
  656. }
  657. /** Called when we are unable to complete the client's request to a directory
  658. * server due to a network error: Mark the router as down and try again if
  659. * possible.
  660. */
  661. static void
  662. connection_dir_request_failed(dir_connection_t *conn)
  663. {
  664. if (directory_conn_is_self_reachability_test(conn)) {
  665. return; /* this was a test fetch. don't retry. */
  666. }
  667. if (!entry_list_is_constrained(get_options()))
  668. router_set_status(conn->identity_digest, 0); /* don't try him again */
  669. if (conn->base_.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
  670. conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO) {
  671. log_info(LD_DIR, "Giving up on serverdesc/extrainfo fetch from "
  672. "directory server at '%s'; retrying",
  673. conn->base_.address);
  674. if (conn->router_purpose == ROUTER_PURPOSE_BRIDGE)
  675. connection_dir_bridge_routerdesc_failed(conn);
  676. connection_dir_download_routerdesc_failed(conn);
  677. } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_CONSENSUS) {
  678. if (conn->requested_resource)
  679. networkstatus_consensus_download_failed(0, conn->requested_resource);
  680. } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_CERTIFICATE) {
  681. log_info(LD_DIR, "Giving up on certificate fetch from directory server "
  682. "at '%s'; retrying",
  683. conn->base_.address);
  684. connection_dir_download_cert_failed(conn, 0);
  685. } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_DETACHED_SIGNATURES) {
  686. log_info(LD_DIR, "Giving up downloading detached signatures from '%s'",
  687. conn->base_.address);
  688. } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_STATUS_VOTE) {
  689. log_info(LD_DIR, "Giving up downloading votes from '%s'",
  690. conn->base_.address);
  691. } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_MICRODESC) {
  692. log_info(LD_DIR, "Giving up on downloading microdescriptors from "
  693. "directory server at '%s'; will retry", conn->base_.address);
  694. connection_dir_download_routerdesc_failed(conn);
  695. }
  696. }
  697. /** Helper: Attempt to fetch directly the descriptors of each bridge
  698. * listed in <b>failed</b>.
  699. */
  700. static void
  701. connection_dir_retry_bridges(smartlist_t *descs)
  702. {
  703. char digest[DIGEST_LEN];
  704. SMARTLIST_FOREACH(descs, const char *, cp,
  705. {
  706. if (base16_decode(digest, DIGEST_LEN, cp, strlen(cp))<0) {
  707. log_warn(LD_BUG, "Malformed fingerprint in list: %s",
  708. escaped(cp));
  709. continue;
  710. }
  711. retry_bridge_descriptor_fetch_directly(digest);
  712. });
  713. }
  714. /** Called when an attempt to download one or more router descriptors
  715. * or extra-info documents on connection <b>conn</b> failed.
  716. */
  717. static void
  718. connection_dir_download_routerdesc_failed(dir_connection_t *conn)
  719. {
  720. /* No need to increment the failure count for routerdescs, since
  721. * it's not their fault. */
  722. /* No need to relaunch descriptor downloads here: we already do it
  723. * every 10 or 60 seconds (FOO_DESCRIPTOR_RETRY_INTERVAL) in main.c. */
  724. tor_assert(conn->base_.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
  725. conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO ||
  726. conn->base_.purpose == DIR_PURPOSE_FETCH_MICRODESC);
  727. (void) conn;
  728. }
  729. /** Called when an attempt to download a bridge's routerdesc from
  730. * one of the authorities failed due to a network error. If
  731. * possible attempt to download descriptors from the bridge directly.
  732. */
  733. static void
  734. connection_dir_bridge_routerdesc_failed(dir_connection_t *conn)
  735. {
  736. smartlist_t *which = NULL;
  737. /* Requests for bridge descriptors are in the form 'fp/', so ignore
  738. anything else. */
  739. if (!conn->requested_resource || strcmpstart(conn->requested_resource,"fp/"))
  740. return;
  741. which = smartlist_new();
  742. dir_split_resource_into_fingerprints(conn->requested_resource
  743. + strlen("fp/"),
  744. which, NULL, 0);
  745. tor_assert(conn->base_.purpose != DIR_PURPOSE_FETCH_EXTRAINFO);
  746. if (smartlist_len(which)) {
  747. connection_dir_retry_bridges(which);
  748. SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
  749. }
  750. smartlist_free(which);
  751. }
  752. /** Called when an attempt to fetch a certificate fails. */
  753. static void
  754. connection_dir_download_cert_failed(dir_connection_t *conn, int status)
  755. {
  756. const char *fp_pfx = "fp/";
  757. const char *fpsk_pfx = "fp-sk/";
  758. smartlist_t *failed;
  759. tor_assert(conn->base_.purpose == DIR_PURPOSE_FETCH_CERTIFICATE);
  760. if (!conn->requested_resource)
  761. return;
  762. failed = smartlist_new();
  763. /*
  764. * We have two cases download by fingerprint (resource starts
  765. * with "fp/") or download by fingerprint/signing key pair
  766. * (resource starts with "fp-sk/").
  767. */
  768. if (!strcmpstart(conn->requested_resource, fp_pfx)) {
  769. /* Download by fingerprint case */
  770. dir_split_resource_into_fingerprints(conn->requested_resource +
  771. strlen(fp_pfx),
  772. failed, NULL, DSR_HEX);
  773. SMARTLIST_FOREACH_BEGIN(failed, char *, cp) {
  774. /* Null signing key digest indicates download by fp only */
  775. authority_cert_dl_failed(cp, NULL, status);
  776. tor_free(cp);
  777. } SMARTLIST_FOREACH_END(cp);
  778. } else if (!strcmpstart(conn->requested_resource, fpsk_pfx)) {
  779. /* Download by (fp,sk) pairs */
  780. dir_split_resource_into_fingerprint_pairs(conn->requested_resource +
  781. strlen(fpsk_pfx), failed);
  782. SMARTLIST_FOREACH_BEGIN(failed, fp_pair_t *, cp) {
  783. authority_cert_dl_failed(cp->first, cp->second, status);
  784. tor_free(cp);
  785. } SMARTLIST_FOREACH_END(cp);
  786. } else {
  787. log_warn(LD_DIR,
  788. "Don't know what to do with failure for cert fetch %s",
  789. conn->requested_resource);
  790. }
  791. smartlist_free(failed);
  792. update_certificate_downloads(time(NULL));
  793. }
  794. /** Evaluate the situation and decide if we should use an encrypted
  795. * "begindir-style" connection for this directory request.
  796. * 1) If or_port is 0, or it's a direct conn and or_port is firewalled
  797. * or we're a dir mirror, no.
  798. * 2) If we prefer to avoid begindir conns, and we're not fetching or
  799. * publishing a bridge relay descriptor, no.
  800. * 3) Else yes.
  801. */
  802. static int
  803. directory_command_should_use_begindir(const or_options_t *options,
  804. const tor_addr_t *addr,
  805. int or_port, uint8_t router_purpose,
  806. dir_indirection_t indirection)
  807. {
  808. (void) router_purpose;
  809. if (!or_port)
  810. return 0; /* We don't know an ORPort -- no chance. */
  811. if (indirection == DIRIND_DIRECT_CONN || indirection == DIRIND_ANON_DIRPORT)
  812. return 0;
  813. if (indirection == DIRIND_ONEHOP)
  814. if (!fascist_firewall_allows_address_or(addr, or_port) ||
  815. directory_fetches_from_authorities(options))
  816. return 0; /* We're firewalled or are acting like a relay -- also no. */
  817. return 1;
  818. }
  819. /** Helper for directory_initiate_command_routerstatus: send the
  820. * command to a server whose address is <b>address</b>, whose IP is
  821. * <b>addr</b>, whose directory port is <b>dir_port</b>, whose tor version
  822. * <b>supports_begindir</b>, and whose identity key digest is
  823. * <b>digest</b>. */
  824. void
  825. directory_initiate_command(const tor_addr_t *_addr,
  826. uint16_t or_port, uint16_t dir_port,
  827. const char *digest,
  828. uint8_t dir_purpose, uint8_t router_purpose,
  829. dir_indirection_t indirection, const char *resource,
  830. const char *payload, size_t payload_len,
  831. time_t if_modified_since)
  832. {
  833. directory_initiate_command_rend(_addr, or_port, dir_port,
  834. digest, dir_purpose,
  835. router_purpose, indirection,
  836. resource, payload, payload_len,
  837. if_modified_since, NULL);
  838. }
  839. /** Return non-zero iff a directory connection with purpose
  840. * <b>dir_purpose</b> reveals sensitive information about a Tor
  841. * instance's client activities. (Such connections must be performed
  842. * through normal three-hop Tor circuits.) */
  843. static int
  844. is_sensitive_dir_purpose(uint8_t dir_purpose)
  845. {
  846. return ((dir_purpose == DIR_PURPOSE_HAS_FETCHED_RENDDESC_V2) ||
  847. (dir_purpose == DIR_PURPOSE_UPLOAD_RENDDESC_V2) ||
  848. (dir_purpose == DIR_PURPOSE_FETCH_RENDDESC_V2));
  849. }
  850. /** Same as directory_initiate_command(), but accepts rendezvous data to
  851. * fetch a hidden service descriptor. */
  852. static void
  853. directory_initiate_command_rend(const tor_addr_t *_addr,
  854. uint16_t or_port, uint16_t dir_port,
  855. const char *digest,
  856. uint8_t dir_purpose, uint8_t router_purpose,
  857. dir_indirection_t indirection,
  858. const char *resource,
  859. const char *payload, size_t payload_len,
  860. time_t if_modified_since,
  861. const rend_data_t *rend_query)
  862. {
  863. dir_connection_t *conn;
  864. const or_options_t *options = get_options();
  865. int socket_error = 0;
  866. int use_begindir = directory_command_should_use_begindir(options, _addr,
  867. or_port, router_purpose, indirection);
  868. const int anonymized_connection = dirind_is_anon(indirection);
  869. tor_addr_t addr;
  870. tor_assert(_addr);
  871. tor_assert(or_port || dir_port);
  872. tor_assert(digest);
  873. tor_addr_copy(&addr, _addr);
  874. log_debug(LD_DIR, "anonymized %d, use_begindir %d.",
  875. anonymized_connection, use_begindir);
  876. log_debug(LD_DIR, "Initiating %s", dir_conn_purpose_to_string(dir_purpose));
  877. #ifndef NON_ANONYMOUS_MODE_ENABLED
  878. tor_assert(!(is_sensitive_dir_purpose(dir_purpose) &&
  879. !anonymized_connection));
  880. #else
  881. (void)is_sensitive_dir_purpose;
  882. #endif
  883. /* ensure that we don't make direct connections when a SOCKS server is
  884. * configured. */
  885. if (!anonymized_connection && !use_begindir && !options->HTTPProxy &&
  886. (options->Socks4Proxy || options->Socks5Proxy)) {
  887. log_warn(LD_DIR, "Cannot connect to a directory server through a "
  888. "SOCKS proxy!");
  889. return;
  890. }
  891. conn = dir_connection_new(tor_addr_family(&addr));
  892. /* set up conn so it's got all the data we need to remember */
  893. tor_addr_copy(&conn->base_.addr, &addr);
  894. conn->base_.port = use_begindir ? or_port : dir_port;
  895. conn->base_.address = tor_dup_addr(&addr);
  896. memcpy(conn->identity_digest, digest, DIGEST_LEN);
  897. conn->base_.purpose = dir_purpose;
  898. conn->router_purpose = router_purpose;
  899. /* give it an initial state */
  900. conn->base_.state = DIR_CONN_STATE_CONNECTING;
  901. /* decide whether we can learn our IP address from this conn */
  902. /* XXXX This is a bad name for this field now. */
  903. conn->dirconn_direct = !anonymized_connection;
  904. /* copy rendezvous data, if any */
  905. if (rend_query)
  906. conn->rend_data = rend_data_dup(rend_query);
  907. if (!anonymized_connection && !use_begindir) {
  908. /* then we want to connect to dirport directly */
  909. if (options->HTTPProxy) {
  910. tor_addr_copy(&addr, &options->HTTPProxyAddr);
  911. dir_port = options->HTTPProxyPort;
  912. }
  913. switch (connection_connect(TO_CONN(conn), conn->base_.address, &addr,
  914. dir_port, &socket_error)) {
  915. case -1:
  916. connection_mark_for_close(TO_CONN(conn));
  917. return;
  918. case 1:
  919. /* start flushing conn */
  920. conn->base_.state = DIR_CONN_STATE_CLIENT_SENDING;
  921. /* fall through */
  922. case 0:
  923. /* queue the command on the outbuf */
  924. directory_send_command(conn, dir_purpose, 1, resource,
  925. payload, payload_len,
  926. if_modified_since);
  927. connection_watch_events(TO_CONN(conn), READ_EVENT | WRITE_EVENT);
  928. /* writable indicates finish, readable indicates broken link,
  929. error indicates broken link in windowsland. */
  930. }
  931. } else { /* we want to connect via a tor connection */
  932. entry_connection_t *linked_conn;
  933. /* Anonymized tunneled connections can never share a circuit.
  934. * One-hop directory connections can share circuits with each other
  935. * but nothing else. */
  936. int iso_flags = anonymized_connection ? ISO_STREAM : ISO_SESSIONGRP;
  937. /* If it's an anonymized connection, remember the fact that we
  938. * wanted it for later: maybe we'll want it again soon. */
  939. if (anonymized_connection && use_begindir)
  940. rep_hist_note_used_internal(time(NULL), 0, 1);
  941. else if (anonymized_connection && !use_begindir)
  942. rep_hist_note_used_port(time(NULL), conn->base_.port);
  943. /* make an AP connection
  944. * populate it and add it at the right state
  945. * hook up both sides
  946. */
  947. linked_conn =
  948. connection_ap_make_link(TO_CONN(conn),
  949. conn->base_.address, conn->base_.port,
  950. digest,
  951. SESSION_GROUP_DIRCONN, iso_flags,
  952. use_begindir, conn->dirconn_direct);
  953. if (!linked_conn) {
  954. log_warn(LD_NET,"Making tunnel to dirserver failed.");
  955. connection_mark_for_close(TO_CONN(conn));
  956. return;
  957. }
  958. if (connection_add(TO_CONN(conn)) < 0) {
  959. log_warn(LD_NET,"Unable to add connection for link to dirserver.");
  960. connection_mark_for_close(TO_CONN(conn));
  961. return;
  962. }
  963. conn->base_.state = DIR_CONN_STATE_CLIENT_SENDING;
  964. /* queue the command on the outbuf */
  965. directory_send_command(conn, dir_purpose, 0, resource,
  966. payload, payload_len,
  967. if_modified_since);
  968. connection_watch_events(TO_CONN(conn), READ_EVENT|WRITE_EVENT);
  969. IF_HAS_BUFFEREVENT(ENTRY_TO_CONN(linked_conn), {
  970. connection_watch_events(ENTRY_TO_CONN(linked_conn),
  971. READ_EVENT|WRITE_EVENT);
  972. }) ELSE_IF_NO_BUFFEREVENT
  973. connection_start_reading(ENTRY_TO_CONN(linked_conn));
  974. }
  975. }
  976. /** Return true iff anything we say on <b>conn</b> is being encrypted before
  977. * we send it to the client/server. */
  978. int
  979. connection_dir_is_encrypted(dir_connection_t *conn)
  980. {
  981. /* Right now it's sufficient to see if conn is or has been linked, since
  982. * the only thing it could be linked to is an edge connection on a
  983. * circuit, and the only way it could have been unlinked is at the edge
  984. * connection getting closed.
  985. */
  986. return TO_CONN(conn)->linked;
  987. }
  988. /** Helper for sorting
  989. *
  990. * sort strings alphabetically
  991. */
  992. static int
  993. compare_strs_(const void **a, const void **b)
  994. {
  995. const char *s1 = *a, *s2 = *b;
  996. return strcmp(s1, s2);
  997. }
  998. #define CONDITIONAL_CONSENSUS_FPR_LEN 3
  999. #if (CONDITIONAL_CONSENSUS_FPR_LEN > DIGEST_LEN)
  1000. #error "conditional consensus fingerprint length is larger than digest length"
  1001. #endif
  1002. /** Return the URL we should use for a consensus download.
  1003. *
  1004. * This url depends on whether or not the server we go to
  1005. * is sufficiently new to support conditional consensus downloading,
  1006. * i.e. GET .../consensus/<b>fpr</b>+<b>fpr</b>+<b>fpr</b>
  1007. *
  1008. * If 'resource' is provided, it is the name of a consensus flavor to request.
  1009. */
  1010. static char *
  1011. directory_get_consensus_url(const char *resource)
  1012. {
  1013. char *url = NULL;
  1014. const char *hyphen, *flavor;
  1015. if (resource==NULL || strcmp(resource, "ns")==0) {
  1016. flavor = ""; /* Request ns consensuses as "", so older servers will work*/
  1017. hyphen = "";
  1018. } else {
  1019. flavor = resource;
  1020. hyphen = "-";
  1021. }
  1022. {
  1023. char *authority_id_list;
  1024. smartlist_t *authority_digests = smartlist_new();
  1025. SMARTLIST_FOREACH_BEGIN(router_get_trusted_dir_servers(),
  1026. dir_server_t *, ds) {
  1027. char *hex;
  1028. if (!(ds->type & V3_DIRINFO))
  1029. continue;
  1030. hex = tor_malloc(2*CONDITIONAL_CONSENSUS_FPR_LEN+1);
  1031. base16_encode(hex, 2*CONDITIONAL_CONSENSUS_FPR_LEN+1,
  1032. ds->v3_identity_digest, CONDITIONAL_CONSENSUS_FPR_LEN);
  1033. smartlist_add(authority_digests, hex);
  1034. } SMARTLIST_FOREACH_END(ds);
  1035. smartlist_sort(authority_digests, compare_strs_);
  1036. authority_id_list = smartlist_join_strings(authority_digests,
  1037. "+", 0, NULL);
  1038. tor_asprintf(&url, "/tor/status-vote/current/consensus%s%s/%s.z",
  1039. hyphen, flavor, authority_id_list);
  1040. SMARTLIST_FOREACH(authority_digests, char *, cp, tor_free(cp));
  1041. smartlist_free(authority_digests);
  1042. tor_free(authority_id_list);
  1043. }
  1044. return url;
  1045. }
  1046. /** Queue an appropriate HTTP command on conn-\>outbuf. The other args
  1047. * are as in directory_initiate_command().
  1048. */
  1049. static void
  1050. directory_send_command(dir_connection_t *conn,
  1051. int purpose, int direct, const char *resource,
  1052. const char *payload, size_t payload_len,
  1053. time_t if_modified_since)
  1054. {
  1055. char proxystring[256];
  1056. char hoststring[128];
  1057. smartlist_t *headers = smartlist_new();
  1058. char *url;
  1059. char request[8192];
  1060. const char *httpcommand = NULL;
  1061. tor_assert(conn);
  1062. tor_assert(conn->base_.type == CONN_TYPE_DIR);
  1063. tor_free(conn->requested_resource);
  1064. if (resource)
  1065. conn->requested_resource = tor_strdup(resource);
  1066. /* come up with a string for which Host: we want */
  1067. if (conn->base_.port == 80) {
  1068. strlcpy(hoststring, conn->base_.address, sizeof(hoststring));
  1069. } else {
  1070. tor_snprintf(hoststring, sizeof(hoststring),"%s:%d",
  1071. conn->base_.address, conn->base_.port);
  1072. }
  1073. /* Format if-modified-since */
  1074. if (if_modified_since) {
  1075. char b[RFC1123_TIME_LEN+1];
  1076. format_rfc1123_time(b, if_modified_since);
  1077. smartlist_add_asprintf(headers, "If-Modified-Since: %s\r\n", b);
  1078. }
  1079. /* come up with some proxy lines, if we're using one. */
  1080. if (direct && get_options()->HTTPProxy) {
  1081. char *base64_authenticator=NULL;
  1082. const char *authenticator = get_options()->HTTPProxyAuthenticator;
  1083. tor_snprintf(proxystring, sizeof(proxystring),"http://%s", hoststring);
  1084. if (authenticator) {
  1085. base64_authenticator = alloc_http_authenticator(authenticator);
  1086. if (!base64_authenticator)
  1087. log_warn(LD_BUG, "Encoding http authenticator failed");
  1088. }
  1089. if (base64_authenticator) {
  1090. smartlist_add_asprintf(headers,
  1091. "Proxy-Authorization: Basic %s\r\n",
  1092. base64_authenticator);
  1093. tor_free(base64_authenticator);
  1094. }
  1095. } else {
  1096. proxystring[0] = 0;
  1097. }
  1098. switch (purpose) {
  1099. case DIR_PURPOSE_FETCH_CONSENSUS:
  1100. /* resource is optional. If present, it's a flavor name */
  1101. tor_assert(!payload);
  1102. httpcommand = "GET";
  1103. url = directory_get_consensus_url(resource);
  1104. log_info(LD_DIR, "Downloading consensus from %s using %s",
  1105. hoststring, url);
  1106. break;
  1107. case DIR_PURPOSE_FETCH_CERTIFICATE:
  1108. tor_assert(resource);
  1109. tor_assert(!payload);
  1110. httpcommand = "GET";
  1111. tor_asprintf(&url, "/tor/keys/%s", resource);
  1112. break;
  1113. case DIR_PURPOSE_FETCH_STATUS_VOTE:
  1114. tor_assert(resource);
  1115. tor_assert(!payload);
  1116. httpcommand = "GET";
  1117. tor_asprintf(&url, "/tor/status-vote/next/%s.z", resource);
  1118. break;
  1119. case DIR_PURPOSE_FETCH_DETACHED_SIGNATURES:
  1120. tor_assert(!resource);
  1121. tor_assert(!payload);
  1122. httpcommand = "GET";
  1123. url = tor_strdup("/tor/status-vote/next/consensus-signatures.z");
  1124. break;
  1125. case DIR_PURPOSE_FETCH_SERVERDESC:
  1126. tor_assert(resource);
  1127. httpcommand = "GET";
  1128. tor_asprintf(&url, "/tor/server/%s", resource);
  1129. break;
  1130. case DIR_PURPOSE_FETCH_EXTRAINFO:
  1131. tor_assert(resource);
  1132. httpcommand = "GET";
  1133. tor_asprintf(&url, "/tor/extra/%s", resource);
  1134. break;
  1135. case DIR_PURPOSE_FETCH_MICRODESC:
  1136. tor_assert(resource);
  1137. httpcommand = "GET";
  1138. tor_asprintf(&url, "/tor/micro/%s", resource);
  1139. break;
  1140. case DIR_PURPOSE_UPLOAD_DIR: {
  1141. const char *why = router_get_descriptor_gen_reason();
  1142. tor_assert(!resource);
  1143. tor_assert(payload);
  1144. httpcommand = "POST";
  1145. url = tor_strdup("/tor/");
  1146. if (why) {
  1147. smartlist_add_asprintf(headers, "X-Desc-Gen-Reason: %s\r\n", why);
  1148. }
  1149. break;
  1150. }
  1151. case DIR_PURPOSE_UPLOAD_VOTE:
  1152. tor_assert(!resource);
  1153. tor_assert(payload);
  1154. httpcommand = "POST";
  1155. url = tor_strdup("/tor/post/vote");
  1156. break;
  1157. case DIR_PURPOSE_UPLOAD_SIGNATURES:
  1158. tor_assert(!resource);
  1159. tor_assert(payload);
  1160. httpcommand = "POST";
  1161. url = tor_strdup("/tor/post/consensus-signature");
  1162. break;
  1163. case DIR_PURPOSE_FETCH_RENDDESC_V2:
  1164. tor_assert(resource);
  1165. tor_assert(strlen(resource) <= REND_DESC_ID_V2_LEN_BASE32);
  1166. tor_assert(!payload);
  1167. httpcommand = "GET";
  1168. tor_asprintf(&url, "/tor/rendezvous2/%s", resource);
  1169. break;
  1170. case DIR_PURPOSE_UPLOAD_RENDDESC_V2:
  1171. tor_assert(!resource);
  1172. tor_assert(payload);
  1173. httpcommand = "POST";
  1174. url = tor_strdup("/tor/rendezvous2/publish");
  1175. break;
  1176. default:
  1177. tor_assert(0);
  1178. return;
  1179. }
  1180. /* warn in the non-tunneled case */
  1181. if (direct && (strlen(proxystring) + strlen(url) >= 4096)) {
  1182. log_warn(LD_BUG,
  1183. "Squid does not like URLs longer than 4095 bytes, and this "
  1184. "one is %d bytes long: %s%s",
  1185. (int)(strlen(proxystring) + strlen(url)), proxystring, url);
  1186. }
  1187. tor_snprintf(request, sizeof(request), "%s %s", httpcommand, proxystring);
  1188. connection_write_to_buf(request, strlen(request), TO_CONN(conn));
  1189. connection_write_to_buf(url, strlen(url), TO_CONN(conn));
  1190. tor_free(url);
  1191. if (!strcmp(httpcommand, "POST") || payload) {
  1192. smartlist_add_asprintf(headers, "Content-Length: %lu\r\n",
  1193. payload ? (unsigned long)payload_len : 0);
  1194. }
  1195. {
  1196. char *header = smartlist_join_strings(headers, "", 0, NULL);
  1197. tor_snprintf(request, sizeof(request), " HTTP/1.0\r\nHost: %s\r\n%s\r\n",
  1198. hoststring, header);
  1199. tor_free(header);
  1200. }
  1201. connection_write_to_buf(request, strlen(request), TO_CONN(conn));
  1202. if (payload) {
  1203. /* then send the payload afterwards too */
  1204. connection_write_to_buf(payload, payload_len, TO_CONN(conn));
  1205. }
  1206. SMARTLIST_FOREACH(headers, char *, h, tor_free(h));
  1207. smartlist_free(headers);
  1208. }
  1209. /** Parse an HTTP request string <b>headers</b> of the form
  1210. * \verbatim
  1211. * "\%s [http[s]://]\%s HTTP/1..."
  1212. * \endverbatim
  1213. * If it's well-formed, strdup the second \%s into *<b>url</b>, and
  1214. * nul-terminate it. If the url doesn't start with "/tor/", rewrite it
  1215. * so it does. Return 0.
  1216. * Otherwise, return -1.
  1217. */
  1218. STATIC int
  1219. parse_http_url(const char *headers, char **url)
  1220. {
  1221. char *s, *start, *tmp;
  1222. s = (char *)eat_whitespace_no_nl(headers);
  1223. if (!*s) return -1;
  1224. s = (char *)find_whitespace(s); /* get past GET/POST */
  1225. if (!*s) return -1;
  1226. s = (char *)eat_whitespace_no_nl(s);
  1227. if (!*s) return -1;
  1228. start = s; /* this is it, assuming it's valid */
  1229. s = (char *)find_whitespace(start);
  1230. if (!*s) return -1;
  1231. /* tolerate the http[s] proxy style of putting the hostname in the url */
  1232. if (s-start >= 4 && !strcmpstart(start,"http")) {
  1233. tmp = start + 4;
  1234. if (*tmp == 's')
  1235. tmp++;
  1236. if (s-tmp >= 3 && !strcmpstart(tmp,"://")) {
  1237. tmp = strchr(tmp+3, '/');
  1238. if (tmp && tmp < s) {
  1239. log_debug(LD_DIR,"Skipping over 'http[s]://hostname/' string");
  1240. start = tmp;
  1241. }
  1242. }
  1243. }
  1244. /* Check if the header is well formed (next sequence
  1245. * should be HTTP/1.X\r\n). Assumes we're supporting 1.0? */
  1246. {
  1247. unsigned minor_ver;
  1248. char ch;
  1249. char *e = (char *)eat_whitespace_no_nl(s);
  1250. if (2 != tor_sscanf(e, "HTTP/1.%u%c", &minor_ver, &ch)) {
  1251. return -1;
  1252. }
  1253. if (ch != '\r')
  1254. return -1;
  1255. }
  1256. if (s-start < 5 || strcmpstart(start,"/tor/")) { /* need to rewrite it */
  1257. *url = tor_malloc(s - start + 5);
  1258. strlcpy(*url,"/tor", s-start+5);
  1259. strlcat((*url)+4, start, s-start+1);
  1260. } else {
  1261. *url = tor_strndup(start, s-start);
  1262. }
  1263. return 0;
  1264. }
  1265. /** Return a copy of the first HTTP header in <b>headers</b> whose key is
  1266. * <b>which</b>. The key should be given with a terminating colon and space;
  1267. * this function copies everything after, up to but not including the
  1268. * following \\r\\n. */
  1269. static char *
  1270. http_get_header(const char *headers, const char *which)
  1271. {
  1272. const char *cp = headers;
  1273. while (cp) {
  1274. if (!strcasecmpstart(cp, which)) {
  1275. char *eos;
  1276. cp += strlen(which);
  1277. if ((eos = strchr(cp,'\r')))
  1278. return tor_strndup(cp, eos-cp);
  1279. else
  1280. return tor_strdup(cp);
  1281. }
  1282. cp = strchr(cp, '\n');
  1283. if (cp)
  1284. ++cp;
  1285. }
  1286. return NULL;
  1287. }
  1288. /** If <b>headers</b> indicates that a proxy was involved, then rewrite
  1289. * <b>conn</b>-\>address to describe our best guess of the address that
  1290. * originated this HTTP request. */
  1291. static void
  1292. http_set_address_origin(const char *headers, connection_t *conn)
  1293. {
  1294. char *fwd;
  1295. fwd = http_get_header(headers, "Forwarded-For: ");
  1296. if (!fwd)
  1297. fwd = http_get_header(headers, "X-Forwarded-For: ");
  1298. if (fwd) {
  1299. tor_addr_t toraddr;
  1300. if (tor_addr_parse(&toraddr,fwd) == -1 ||
  1301. tor_addr_is_internal(&toraddr,0)) {
  1302. log_debug(LD_DIR, "Ignoring local/internal IP %s", escaped(fwd));
  1303. tor_free(fwd);
  1304. return;
  1305. }
  1306. tor_free(conn->address);
  1307. conn->address = tor_strdup(fwd);
  1308. tor_free(fwd);
  1309. }
  1310. }
  1311. /** Parse an HTTP response string <b>headers</b> of the form
  1312. * \verbatim
  1313. * "HTTP/1.\%d \%d\%s\r\n...".
  1314. * \endverbatim
  1315. *
  1316. * If it's well-formed, assign the status code to *<b>code</b> and
  1317. * return 0. Otherwise, return -1.
  1318. *
  1319. * On success: If <b>date</b> is provided, set *date to the Date
  1320. * header in the http headers, or 0 if no such header is found. If
  1321. * <b>compression</b> is provided, set *<b>compression</b> to the
  1322. * compression method given in the Content-Encoding header, or 0 if no
  1323. * such header is found, or -1 if the value of the header is not
  1324. * recognized. If <b>reason</b> is provided, strdup the reason string
  1325. * into it.
  1326. */
  1327. int
  1328. parse_http_response(const char *headers, int *code, time_t *date,
  1329. compress_method_t *compression, char **reason)
  1330. {
  1331. unsigned n1, n2;
  1332. char datestr[RFC1123_TIME_LEN+1];
  1333. smartlist_t *parsed_headers;
  1334. tor_assert(headers);
  1335. tor_assert(code);
  1336. while (TOR_ISSPACE(*headers)) headers++; /* tolerate leading whitespace */
  1337. if (tor_sscanf(headers, "HTTP/1.%u %u", &n1, &n2) < 2 ||
  1338. (n1 != 0 && n1 != 1) ||
  1339. (n2 < 100 || n2 >= 600)) {
  1340. log_warn(LD_HTTP,"Failed to parse header %s",escaped(headers));
  1341. return -1;
  1342. }
  1343. *code = n2;
  1344. parsed_headers = smartlist_new();
  1345. smartlist_split_string(parsed_headers, headers, "\n",
  1346. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
  1347. if (reason) {
  1348. smartlist_t *status_line_elements = smartlist_new();
  1349. tor_assert(smartlist_len(parsed_headers));
  1350. smartlist_split_string(status_line_elements,
  1351. smartlist_get(parsed_headers, 0),
  1352. " ", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 3);
  1353. tor_assert(smartlist_len(status_line_elements) <= 3);
  1354. if (smartlist_len(status_line_elements) == 3) {
  1355. *reason = smartlist_get(status_line_elements, 2);
  1356. smartlist_set(status_line_elements, 2, NULL); /* Prevent free */
  1357. }
  1358. SMARTLIST_FOREACH(status_line_elements, char *, cp, tor_free(cp));
  1359. smartlist_free(status_line_elements);
  1360. }
  1361. if (date) {
  1362. *date = 0;
  1363. SMARTLIST_FOREACH(parsed_headers, const char *, s,
  1364. if (!strcmpstart(s, "Date: ")) {
  1365. strlcpy(datestr, s+6, sizeof(datestr));
  1366. /* This will do nothing on failure, so we don't need to check
  1367. the result. We shouldn't warn, since there are many other valid
  1368. date formats besides the one we use. */
  1369. parse_rfc1123_time(datestr, date);
  1370. break;
  1371. });
  1372. }
  1373. if (compression) {
  1374. const char *enc = NULL;
  1375. SMARTLIST_FOREACH(parsed_headers, const char *, s,
  1376. if (!strcmpstart(s, "Content-Encoding: ")) {
  1377. enc = s+18; break;
  1378. });
  1379. if (!enc || !strcmp(enc, "identity")) {
  1380. *compression = NO_METHOD;
  1381. } else if (!strcmp(enc, "deflate") || !strcmp(enc, "x-deflate")) {
  1382. *compression = ZLIB_METHOD;
  1383. } else if (!strcmp(enc, "gzip") || !strcmp(enc, "x-gzip")) {
  1384. *compression = GZIP_METHOD;
  1385. } else {
  1386. log_info(LD_HTTP, "Unrecognized content encoding: %s. Trying to deal.",
  1387. escaped(enc));
  1388. *compression = UNKNOWN_METHOD;
  1389. }
  1390. }
  1391. SMARTLIST_FOREACH(parsed_headers, char *, s, tor_free(s));
  1392. smartlist_free(parsed_headers);
  1393. return 0;
  1394. }
  1395. /** Return true iff <b>body</b> doesn't start with a plausible router or
  1396. * network-status or microdescriptor opening. This is a sign of possible
  1397. * compression. */
  1398. static int
  1399. body_is_plausible(const char *body, size_t len, int purpose)
  1400. {
  1401. int i;
  1402. if (len == 0)
  1403. return 1; /* empty bodies don't need decompression */
  1404. if (len < 32)
  1405. return 0;
  1406. if (purpose == DIR_PURPOSE_FETCH_MICRODESC) {
  1407. return (!strcmpstart(body,"onion-key"));
  1408. }
  1409. if (1) {
  1410. if (!strcmpstart(body,"router") ||
  1411. !strcmpstart(body,"network-status"))
  1412. return 1;
  1413. for (i=0;i<32;++i) {
  1414. if (!TOR_ISPRINT(body[i]) && !TOR_ISSPACE(body[i]))
  1415. return 0;
  1416. }
  1417. }
  1418. return 1;
  1419. }
  1420. /** Called when we've just fetched a bunch of router descriptors in
  1421. * <b>body</b>. The list <b>which</b>, if present, holds digests for
  1422. * descriptors we requested: descriptor digests if <b>descriptor_digests</b>
  1423. * is true, or identity digests otherwise. Parse the descriptors, validate
  1424. * them, and annotate them as having purpose <b>purpose</b> and as having been
  1425. * downloaded from <b>source</b>.
  1426. *
  1427. * Return the number of routers actually added. */
  1428. static int
  1429. load_downloaded_routers(const char *body, smartlist_t *which,
  1430. int descriptor_digests,
  1431. int router_purpose,
  1432. const char *source)
  1433. {
  1434. char buf[256];
  1435. char time_buf[ISO_TIME_LEN+1];
  1436. int added = 0;
  1437. int general = router_purpose == ROUTER_PURPOSE_GENERAL;
  1438. format_iso_time(time_buf, time(NULL));
  1439. tor_assert(source);
  1440. if (tor_snprintf(buf, sizeof(buf),
  1441. "@downloaded-at %s\n"
  1442. "@source %s\n"
  1443. "%s%s%s", time_buf, escaped(source),
  1444. !general ? "@purpose " : "",
  1445. !general ? router_purpose_to_string(router_purpose) : "",
  1446. !general ? "\n" : "")<0)
  1447. return added;
  1448. added = router_load_routers_from_string(body, NULL, SAVED_NOWHERE, which,
  1449. descriptor_digests, buf);
  1450. if (general)
  1451. control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_DESCRIPTORS,
  1452. count_loading_descriptors_progress());
  1453. return added;
  1454. }
  1455. /** We are a client, and we've finished reading the server's
  1456. * response. Parse it and act appropriately.
  1457. *
  1458. * If we're still happy with using this directory server in the future, return
  1459. * 0. Otherwise return -1; and the caller should consider trying the request
  1460. * again.
  1461. *
  1462. * The caller will take care of marking the connection for close.
  1463. */
  1464. static int
  1465. connection_dir_client_reached_eof(dir_connection_t *conn)
  1466. {
  1467. char *body;
  1468. char *headers;
  1469. char *reason = NULL;
  1470. size_t body_len = 0, orig_len = 0;
  1471. int status_code;
  1472. time_t date_header = 0;
  1473. long delta;
  1474. compress_method_t compression;
  1475. int plausible;
  1476. int skewed = 0;
  1477. int allow_partial = (conn->base_.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
  1478. conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO ||
  1479. conn->base_.purpose == DIR_PURPOSE_FETCH_MICRODESC);
  1480. int was_compressed = 0;
  1481. time_t now = time(NULL);
  1482. int src_code;
  1483. switch (connection_fetch_from_buf_http(TO_CONN(conn),
  1484. &headers, MAX_HEADERS_SIZE,
  1485. &body, &body_len, MAX_DIR_DL_SIZE,
  1486. allow_partial)) {
  1487. case -1: /* overflow */
  1488. log_warn(LD_PROTOCOL,
  1489. "'fetch' response too large (server '%s:%d'). Closing.",
  1490. conn->base_.address, conn->base_.port);
  1491. return -1;
  1492. case 0:
  1493. log_info(LD_HTTP,
  1494. "'fetch' response not all here, but we're at eof. Closing.");
  1495. return -1;
  1496. /* case 1, fall through */
  1497. }
  1498. orig_len = body_len;
  1499. if (parse_http_response(headers, &status_code, &date_header,
  1500. &compression, &reason) < 0) {
  1501. log_warn(LD_HTTP,"Unparseable headers (server '%s:%d'). Closing.",
  1502. conn->base_.address, conn->base_.port);
  1503. tor_free(body); tor_free(headers);
  1504. return -1;
  1505. }
  1506. if (!reason) reason = tor_strdup("[no reason given]");
  1507. log_debug(LD_DIR,
  1508. "Received response from directory server '%s:%d': %d %s "
  1509. "(purpose: %d)",
  1510. conn->base_.address, conn->base_.port, status_code,
  1511. escaped(reason),
  1512. conn->base_.purpose);
  1513. /* now check if it's got any hints for us about our IP address. */
  1514. if (conn->dirconn_direct) {
  1515. char *guess = http_get_header(headers, X_ADDRESS_HEADER);
  1516. if (guess) {
  1517. router_new_address_suggestion(guess, conn);
  1518. tor_free(guess);
  1519. }
  1520. }
  1521. if (date_header > 0) {
  1522. /* The date header was written very soon after we sent our request,
  1523. * so compute the skew as the difference between sending the request
  1524. * and the date header. (We used to check now-date_header, but that's
  1525. * inaccurate if we spend a lot of time downloading.)
  1526. */
  1527. delta = conn->base_.timestamp_lastwritten - date_header;
  1528. if (labs(delta)>ALLOW_DIRECTORY_TIME_SKEW) {
  1529. char dbuf[64];
  1530. int trusted = router_digest_is_trusted_dir(conn->identity_digest);
  1531. format_time_interval(dbuf, sizeof(dbuf), delta);
  1532. log_fn(trusted ? LOG_WARN : LOG_INFO,
  1533. LD_HTTP,
  1534. "Received directory with skewed time (server '%s:%d'): "
  1535. "It seems that our clock is %s by %s, or that theirs is %s. "
  1536. "Tor requires an accurate clock to work: please check your time, "
  1537. "timezone, and date settings.",
  1538. conn->base_.address, conn->base_.port,
  1539. delta>0 ? "ahead" : "behind", dbuf,
  1540. delta>0 ? "behind" : "ahead");
  1541. skewed = 1; /* don't check the recommended-versions line */
  1542. if (trusted)
  1543. control_event_general_status(LOG_WARN,
  1544. "CLOCK_SKEW SKEW=%ld SOURCE=DIRSERV:%s:%d",
  1545. delta, conn->base_.address, conn->base_.port);
  1546. } else {
  1547. log_debug(LD_HTTP, "Time on received directory is within tolerance; "
  1548. "we are %ld seconds skewed. (That's okay.)", delta);
  1549. }
  1550. }
  1551. (void) skewed; /* skewed isn't used yet. */
  1552. if (status_code == 503) {
  1553. routerstatus_t *rs;
  1554. dir_server_t *ds;
  1555. const char *id_digest = conn->identity_digest;
  1556. log_info(LD_DIR,"Received http status code %d (%s) from server "
  1557. "'%s:%d'. I'll try again soon.",
  1558. status_code, escaped(reason), conn->base_.address,
  1559. conn->base_.port);
  1560. if ((rs = router_get_mutable_consensus_status_by_id(id_digest)))
  1561. rs->last_dir_503_at = now;
  1562. if ((ds = router_get_fallback_dirserver_by_digest(id_digest)))
  1563. ds->fake_status.last_dir_503_at = now;
  1564. tor_free(body); tor_free(headers); tor_free(reason);
  1565. return -1;
  1566. }
  1567. plausible = body_is_plausible(body, body_len, conn->base_.purpose);
  1568. if (compression != NO_METHOD || !plausible) {
  1569. char *new_body = NULL;
  1570. size_t new_len = 0;
  1571. compress_method_t guessed = detect_compression_method(body, body_len);
  1572. if (compression == UNKNOWN_METHOD || guessed != compression) {
  1573. /* Tell the user if we don't believe what we're told about compression.*/
  1574. const char *description1, *description2;
  1575. if (compression == ZLIB_METHOD)
  1576. description1 = "as deflated";
  1577. else if (compression == GZIP_METHOD)
  1578. description1 = "as gzipped";
  1579. else if (compression == NO_METHOD)
  1580. description1 = "as uncompressed";
  1581. else
  1582. description1 = "with an unknown Content-Encoding";
  1583. if (guessed == ZLIB_METHOD)
  1584. description2 = "deflated";
  1585. else if (guessed == GZIP_METHOD)
  1586. description2 = "gzipped";
  1587. else if (!plausible)
  1588. description2 = "confusing binary junk";
  1589. else
  1590. description2 = "uncompressed";
  1591. log_info(LD_HTTP, "HTTP body from server '%s:%d' was labeled %s, "
  1592. "but it seems to be %s.%s",
  1593. conn->base_.address, conn->base_.port, description1,
  1594. description2,
  1595. (compression>0 && guessed>0)?" Trying both.":"");
  1596. }
  1597. /* Try declared compression first if we can. */
  1598. if (compression == GZIP_METHOD || compression == ZLIB_METHOD)
  1599. tor_gzip_uncompress(&new_body, &new_len, body, body_len, compression,
  1600. !allow_partial, LOG_PROTOCOL_WARN);
  1601. /* Okay, if that didn't work, and we think that it was compressed
  1602. * differently, try that. */
  1603. if (!new_body &&
  1604. (guessed == GZIP_METHOD || guessed == ZLIB_METHOD) &&
  1605. compression != guessed)
  1606. tor_gzip_uncompress(&new_body, &new_len, body, body_len, guessed,
  1607. !allow_partial, LOG_PROTOCOL_WARN);
  1608. /* If we're pretty sure that we have a compressed directory, and
  1609. * we didn't manage to uncompress it, then warn and bail. */
  1610. if (!plausible && !new_body) {
  1611. log_fn(LOG_PROTOCOL_WARN, LD_HTTP,
  1612. "Unable to decompress HTTP body (server '%s:%d').",
  1613. conn->base_.address, conn->base_.port);
  1614. tor_free(body); tor_free(headers); tor_free(reason);
  1615. return -1;
  1616. }
  1617. if (new_body) {
  1618. tor_free(body);
  1619. body = new_body;
  1620. body_len = new_len;
  1621. was_compressed = 1;
  1622. }
  1623. }
  1624. if (conn->base_.purpose == DIR_PURPOSE_FETCH_CONSENSUS) {
  1625. int r;
  1626. const char *flavname = conn->requested_resource;
  1627. if (status_code != 200) {
  1628. int severity = (status_code == 304) ? LOG_INFO : LOG_WARN;
  1629. tor_log(severity, LD_DIR,
  1630. "Received http status code %d (%s) from server "
  1631. "'%s:%d' while fetching consensus directory.",
  1632. status_code, escaped(reason), conn->base_.address,
  1633. conn->base_.port);
  1634. tor_free(body); tor_free(headers); tor_free(reason);
  1635. networkstatus_consensus_download_failed(status_code, flavname);
  1636. return -1;
  1637. }
  1638. log_info(LD_DIR,"Received consensus directory (size %d) from server "
  1639. "'%s:%d'", (int)body_len, conn->base_.address, conn->base_.port);
  1640. if ((r=networkstatus_set_current_consensus(body, flavname, 0))<0) {
  1641. log_fn(r<-1?LOG_WARN:LOG_INFO, LD_DIR,
  1642. "Unable to load %s consensus directory downloaded from "
  1643. "server '%s:%d'. I'll try again soon.",
  1644. flavname, conn->base_.address, conn->base_.port);
  1645. tor_free(body); tor_free(headers); tor_free(reason);
  1646. networkstatus_consensus_download_failed(0, flavname);
  1647. return -1;
  1648. }
  1649. /* launches router downloads as needed */
  1650. routers_update_all_from_networkstatus(now, 3);
  1651. update_microdescs_from_networkstatus(now);
  1652. update_microdesc_downloads(now);
  1653. directory_info_has_arrived(now, 0);
  1654. log_info(LD_DIR, "Successfully loaded consensus.");
  1655. }
  1656. if (conn->base_.purpose == DIR_PURPOSE_FETCH_CERTIFICATE) {
  1657. if (status_code != 200) {
  1658. log_warn(LD_DIR,
  1659. "Received http status code %d (%s) from server "
  1660. "'%s:%d' while fetching \"/tor/keys/%s\".",
  1661. status_code, escaped(reason), conn->base_.address,
  1662. conn->base_.port, conn->requested_resource);
  1663. connection_dir_download_cert_failed(conn, status_code);
  1664. tor_free(body); tor_free(headers); tor_free(reason);
  1665. return -1;
  1666. }
  1667. log_info(LD_DIR,"Received authority certificates (size %d) from server "
  1668. "'%s:%d'", (int)body_len, conn->base_.address, conn->base_.port);
  1669. /*
  1670. * Tell trusted_dirs_load_certs_from_string() whether it was by fp
  1671. * or fp-sk pair.
  1672. */
  1673. src_code = -1;
  1674. if (!strcmpstart(conn->requested_resource, "fp/")) {
  1675. src_code = TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST;
  1676. } else if (!strcmpstart(conn->requested_resource, "fp-sk/")) {
  1677. src_code = TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_SK_DIGEST;
  1678. }
  1679. if (src_code != -1) {
  1680. if (trusted_dirs_load_certs_from_string(body, src_code, 1)<0) {
  1681. log_warn(LD_DIR, "Unable to parse fetched certificates");
  1682. /* if we fetched more than one and only some failed, the successful
  1683. * ones got flushed to disk so it's safe to call this on them */
  1684. connection_dir_download_cert_failed(conn, status_code);
  1685. } else {
  1686. directory_info_has_arrived(now, 0);
  1687. log_info(LD_DIR, "Successfully loaded certificates from fetch.");
  1688. }
  1689. } else {
  1690. log_warn(LD_DIR,
  1691. "Couldn't figure out what to do with fetched certificates for "
  1692. "unknown resource %s",
  1693. conn->requested_resource);
  1694. connection_dir_download_cert_failed(conn, status_code);
  1695. }
  1696. }
  1697. if (conn->base_.purpose == DIR_PURPOSE_FETCH_STATUS_VOTE) {
  1698. const char *msg;
  1699. int st;
  1700. log_info(LD_DIR,"Got votes (size %d) from server %s:%d",
  1701. (int)body_len, conn->base_.address, conn->base_.port);
  1702. if (status_code != 200) {
  1703. log_warn(LD_DIR,
  1704. "Received http status code %d (%s) from server "
  1705. "'%s:%d' while fetching \"/tor/status-vote/next/%s.z\".",
  1706. status_code, escaped(reason), conn->base_.address,
  1707. conn->base_.port, conn->requested_resource);
  1708. tor_free(body); tor_free(headers); tor_free(reason);
  1709. return -1;
  1710. }
  1711. dirvote_add_vote(body, &msg, &st);
  1712. if (st > 299) {
  1713. log_warn(LD_DIR, "Error adding retrieved vote: %s", msg);
  1714. } else {
  1715. log_info(LD_DIR, "Added vote(s) successfully [msg: %s]", msg);
  1716. }
  1717. }
  1718. if (conn->base_.purpose == DIR_PURPOSE_FETCH_DETACHED_SIGNATURES) {
  1719. const char *msg = NULL;
  1720. log_info(LD_DIR,"Got detached signatures (size %d) from server %s:%d",
  1721. (int)body_len, conn->base_.address, conn->base_.port);
  1722. if (status_code != 200) {
  1723. log_warn(LD_DIR,
  1724. "Received http status code %d (%s) from server '%s:%d' while fetching "
  1725. "\"/tor/status-vote/next/consensus-signatures.z\".",
  1726. status_code, escaped(reason), conn->base_.address,
  1727. conn->base_.port);
  1728. tor_free(body); tor_free(headers); tor_free(reason);
  1729. return -1;
  1730. }
  1731. if (dirvote_add_signatures(body, conn->base_.address, &msg)<0) {
  1732. log_warn(LD_DIR, "Problem adding detached signatures from %s:%d: %s",
  1733. conn->base_.address, conn->base_.port, msg?msg:"???");
  1734. }
  1735. }
  1736. if (conn->base_.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
  1737. conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO) {
  1738. int was_ei = conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO;
  1739. smartlist_t *which = NULL;
  1740. int n_asked_for = 0;
  1741. int descriptor_digests = conn->requested_resource &&
  1742. !strcmpstart(conn->requested_resource,"d/");
  1743. log_info(LD_DIR,"Received %s (size %d) from server '%s:%d'",
  1744. was_ei ? "extra server info" : "server info",
  1745. (int)body_len, conn->base_.address, conn->base_.port);
  1746. if (conn->requested_resource &&
  1747. (!strcmpstart(conn->requested_resource,"d/") ||
  1748. !strcmpstart(conn->requested_resource,"fp/"))) {
  1749. which = smartlist_new();
  1750. dir_split_resource_into_fingerprints(conn->requested_resource +
  1751. (descriptor_digests ? 2 : 3),
  1752. which, NULL, 0);
  1753. n_asked_for = smartlist_len(which);
  1754. }
  1755. if (status_code != 200) {
  1756. int dir_okay = status_code == 404 ||
  1757. (status_code == 400 && !strcmp(reason, "Servers unavailable."));
  1758. /* 404 means that it didn't have them; no big deal.
  1759. * Older (pre-0.1.1.8) servers said 400 Servers unavailable instead. */
  1760. log_fn(dir_okay ? LOG_INFO : LOG_WARN, LD_DIR,
  1761. "Received http status code %d (%s) from server '%s:%d' "
  1762. "while fetching \"/tor/server/%s\". I'll try again soon.",
  1763. status_code, escaped(reason), conn->base_.address,
  1764. conn->base_.port, conn->requested_resource);
  1765. if (!which) {
  1766. connection_dir_download_routerdesc_failed(conn);
  1767. } else {
  1768. dir_routerdesc_download_failed(which, status_code,
  1769. conn->router_purpose,
  1770. was_ei, descriptor_digests);
  1771. SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
  1772. smartlist_free(which);
  1773. }
  1774. tor_free(body); tor_free(headers); tor_free(reason);
  1775. return dir_okay ? 0 : -1;
  1776. }
  1777. /* Learn the routers, assuming we requested by fingerprint or "all"
  1778. * or "authority".
  1779. *
  1780. * We use "authority" to fetch our own descriptor for
  1781. * testing, and to fetch bridge descriptors for bootstrapping. Ignore
  1782. * the output of "authority" requests unless we are using bridges,
  1783. * since otherwise they'll be the response from reachability tests,
  1784. * and we don't really want to add that to our routerlist. */
  1785. if (which || (conn->requested_resource &&
  1786. (!strcmpstart(conn->requested_resource, "all") ||
  1787. (!strcmpstart(conn->requested_resource, "authority") &&
  1788. get_options()->UseBridges)))) {
  1789. /* as we learn from them, we remove them from 'which' */
  1790. if (was_ei) {
  1791. router_load_extrainfo_from_string(body, NULL, SAVED_NOWHERE, which,
  1792. descriptor_digests);
  1793. } else {
  1794. //router_load_routers_from_string(body, NULL, SAVED_NOWHERE, which,
  1795. // descriptor_digests, conn->router_purpose);
  1796. if (load_downloaded_routers(body, which, descriptor_digests,
  1797. conn->router_purpose,
  1798. conn->base_.address))
  1799. directory_info_has_arrived(now, 0);
  1800. }
  1801. }
  1802. if (which) { /* mark remaining ones as failed */
  1803. log_info(LD_DIR, "Received %d/%d %s requested from %s:%d",
  1804. n_asked_for-smartlist_len(which), n_asked_for,
  1805. was_ei ? "extra-info documents" : "router descriptors",
  1806. conn->base_.address, (int)conn->base_.port);
  1807. if (smartlist_len(which)) {
  1808. dir_routerdesc_download_failed(which, status_code,
  1809. conn->router_purpose,
  1810. was_ei, descriptor_digests);
  1811. }
  1812. SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
  1813. smartlist_free(which);
  1814. }
  1815. if (directory_conn_is_self_reachability_test(conn))
  1816. router_dirport_found_reachable();
  1817. }
  1818. if (conn->base_.purpose == DIR_PURPOSE_FETCH_MICRODESC) {
  1819. smartlist_t *which = NULL;
  1820. log_info(LD_DIR,"Received answer to microdescriptor request (status %d, "
  1821. "size %d) from server '%s:%d'",
  1822. status_code, (int)body_len, conn->base_.address,
  1823. conn->base_.port);
  1824. tor_assert(conn->requested_resource &&
  1825. !strcmpstart(conn->requested_resource, "d/"));
  1826. which = smartlist_new();
  1827. dir_split_resource_into_fingerprints(conn->requested_resource+2,
  1828. which, NULL,
  1829. DSR_DIGEST256|DSR_BASE64);
  1830. if (status_code != 200) {
  1831. log_info(LD_DIR, "Received status code %d (%s) from server "
  1832. "'%s:%d' while fetching \"/tor/micro/%s\". I'll try again "
  1833. "soon.",
  1834. status_code, escaped(reason), conn->base_.address,
  1835. (int)conn->base_.port, conn->requested_resource);
  1836. dir_microdesc_download_failed(which, status_code);
  1837. SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
  1838. smartlist_free(which);
  1839. tor_free(body); tor_free(headers); tor_free(reason);
  1840. return 0;
  1841. } else {
  1842. smartlist_t *mds;
  1843. mds = microdescs_add_to_cache(get_microdesc_cache(),
  1844. body, body+body_len, SAVED_NOWHERE, 0,
  1845. now, which);
  1846. if (smartlist_len(which)) {
  1847. /* Mark remaining ones as failed. */
  1848. dir_microdesc_download_failed(which, status_code);
  1849. }
  1850. control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_DESCRIPTORS,
  1851. count_loading_descriptors_progress());
  1852. SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
  1853. smartlist_free(which);
  1854. smartlist_free(mds);
  1855. }
  1856. }
  1857. if (conn->base_.purpose == DIR_PURPOSE_UPLOAD_DIR) {
  1858. switch (status_code) {
  1859. case 200: {
  1860. dir_server_t *ds =
  1861. router_get_trusteddirserver_by_digest(conn->identity_digest);
  1862. char *rejected_hdr = http_get_header(headers,
  1863. "X-Descriptor-Not-New: ");
  1864. if (rejected_hdr) {
  1865. if (!strcmp(rejected_hdr, "Yes")) {
  1866. log_info(LD_GENERAL,
  1867. "Authority '%s' declined our descriptor (not new)",
  1868. ds->nickname);
  1869. /* XXXX use this information; be sure to upload next one
  1870. * sooner. -NM */
  1871. /* XXXX023 On further thought, the task above implies that we're
  1872. * basing our regenerate-descriptor time on when we uploaded the
  1873. * last descriptor, not on the published time of the last
  1874. * descriptor. If those are different, that's a bad thing to
  1875. * do. -NM */
  1876. }
  1877. tor_free(rejected_hdr);
  1878. }
  1879. log_info(LD_GENERAL,"eof (status 200) after uploading server "
  1880. "descriptor: finished.");
  1881. control_event_server_status(
  1882. LOG_NOTICE, "ACCEPTED_SERVER_DESCRIPTOR DIRAUTH=%s:%d",
  1883. conn->base_.address, conn->base_.port);
  1884. ds->has_accepted_serverdesc = 1;
  1885. if (directories_have_accepted_server_descriptor())
  1886. control_event_server_status(LOG_NOTICE, "GOOD_SERVER_DESCRIPTOR");
  1887. }
  1888. break;
  1889. case 400:
  1890. log_warn(LD_GENERAL,"http status 400 (%s) response from "
  1891. "dirserver '%s:%d'. Please correct.",
  1892. escaped(reason), conn->base_.address, conn->base_.port);
  1893. control_event_server_status(LOG_WARN,
  1894. "BAD_SERVER_DESCRIPTOR DIRAUTH=%s:%d REASON=\"%s\"",
  1895. conn->base_.address, conn->base_.port, escaped(reason));
  1896. break;
  1897. default:
  1898. log_warn(LD_GENERAL,
  1899. "http status %d (%s) reason unexpected while uploading "
  1900. "descriptor to server '%s:%d').",
  1901. status_code, escaped(reason), conn->base_.address,
  1902. conn->base_.port);
  1903. break;
  1904. }
  1905. /* return 0 in all cases, since we don't want to mark any
  1906. * dirservers down just because they don't like us. */
  1907. }
  1908. if (conn->base_.purpose == DIR_PURPOSE_UPLOAD_VOTE) {
  1909. switch (status_code) {
  1910. case 200: {
  1911. log_notice(LD_DIR,"Uploaded a vote to dirserver %s:%d",
  1912. conn->base_.address, conn->base_.port);
  1913. }
  1914. break;
  1915. case 400:
  1916. log_warn(LD_DIR,"http status 400 (%s) response after uploading "
  1917. "vote to dirserver '%s:%d'. Please correct.",
  1918. escaped(reason), conn->base_.address, conn->base_.port);
  1919. break;
  1920. default:
  1921. log_warn(LD_GENERAL,
  1922. "http status %d (%s) reason unexpected while uploading "
  1923. "vote to server '%s:%d').",
  1924. status_code, escaped(reason), conn->base_.address,
  1925. conn->base_.port);
  1926. break;
  1927. }
  1928. /* return 0 in all cases, since we don't want to mark any
  1929. * dirservers down just because they don't like us. */
  1930. }
  1931. if (conn->base_.purpose == DIR_PURPOSE_UPLOAD_SIGNATURES) {
  1932. switch (status_code) {
  1933. case 200: {
  1934. log_notice(LD_DIR,"Uploaded signature(s) to dirserver %s:%d",
  1935. conn->base_.address, conn->base_.port);
  1936. }
  1937. break;
  1938. case 400:
  1939. log_warn(LD_DIR,"http status 400 (%s) response after uploading "
  1940. "signatures to dirserver '%s:%d'. Please correct.",
  1941. escaped(reason), conn->base_.address, conn->base_.port);
  1942. break;
  1943. default:
  1944. log_warn(LD_GENERAL,
  1945. "http status %d (%s) reason unexpected while uploading "
  1946. "signatures to server '%s:%d').",
  1947. status_code, escaped(reason), conn->base_.address,
  1948. conn->base_.port);
  1949. break;
  1950. }
  1951. /* return 0 in all cases, since we don't want to mark any
  1952. * dirservers down just because they don't like us. */
  1953. }
  1954. if (conn->base_.purpose == DIR_PURPOSE_FETCH_RENDDESC_V2) {
  1955. #define SEND_HS_DESC_FAILED_EVENT(reason) ( \
  1956. control_event_hs_descriptor_failed(conn->rend_data, \
  1957. conn->identity_digest, \
  1958. reason) )
  1959. #define SEND_HS_DESC_FAILED_CONTENT() ( \
  1960. control_event_hs_descriptor_content(conn->rend_data->onion_address, \
  1961. conn->requested_resource, \
  1962. conn->identity_digest, \
  1963. NULL) )
  1964. tor_assert(conn->rend_data);
  1965. log_info(LD_REND,"Received rendezvous descriptor (size %d, status %d "
  1966. "(%s))",
  1967. (int)body_len, status_code, escaped(reason));
  1968. switch (status_code) {
  1969. case 200:
  1970. {
  1971. rend_cache_entry_t *entry = NULL;
  1972. switch (rend_cache_store_v2_desc_as_client(body,
  1973. conn->requested_resource, conn->rend_data,
  1974. &entry)) {
  1975. case RCS_BADDESC:
  1976. case RCS_NOTDIR: /* Impossible */
  1977. log_warn(LD_REND,"Fetching v2 rendezvous descriptor failed. "
  1978. "Retrying at another directory.");
  1979. /* We'll retry when connection_about_to_close_connection()
  1980. * cleans this dir conn up. */
  1981. SEND_HS_DESC_FAILED_EVENT("BAD_DESC");
  1982. SEND_HS_DESC_FAILED_CONTENT();
  1983. break;
  1984. case RCS_OKAY:
  1985. default:
  1986. {
  1987. char service_id[REND_SERVICE_ID_LEN_BASE32 + 1];
  1988. /* Should never be NULL here for an OKAY returned code. */
  1989. tor_assert(entry);
  1990. rend_get_service_id(entry->parsed->pk, service_id);
  1991. /* success. notify pending connections about this. */
  1992. log_info(LD_REND, "Successfully fetched v2 rendezvous "
  1993. "descriptor.");
  1994. control_event_hs_descriptor_received(service_id,
  1995. conn->rend_data,
  1996. conn->identity_digest);
  1997. control_event_hs_descriptor_content(service_id,
  1998. conn->requested_resource,
  1999. conn->identity_digest,
  2000. body);
  2001. conn->base_.purpose = DIR_PURPOSE_HAS_FETCHED_RENDDESC_V2;
  2002. rend_client_desc_trynow(service_id);
  2003. memwipe(service_id, 0, sizeof(service_id));
  2004. break;
  2005. }
  2006. }
  2007. break;
  2008. }
  2009. case 404:
  2010. /* Not there. We'll retry when
  2011. * connection_about_to_close_connection() cleans this conn up. */
  2012. log_info(LD_REND,"Fetching v2 rendezvous descriptor failed: "
  2013. "Retrying at another directory.");
  2014. SEND_HS_DESC_FAILED_EVENT("NOT_FOUND");
  2015. SEND_HS_DESC_FAILED_CONTENT();
  2016. break;
  2017. case 400:
  2018. log_warn(LD_REND, "Fetching v2 rendezvous descriptor failed: "
  2019. "http status 400 (%s). Dirserver didn't like our "
  2020. "v2 rendezvous query? Retrying at another directory.",
  2021. escaped(reason));
  2022. SEND_HS_DESC_FAILED_EVENT("QUERY_REJECTED");
  2023. SEND_HS_DESC_FAILED_CONTENT();
  2024. break;
  2025. default:
  2026. log_warn(LD_REND, "Fetching v2 rendezvous descriptor failed: "
  2027. "http status %d (%s) response unexpected while "
  2028. "fetching v2 hidden service descriptor (server '%s:%d'). "
  2029. "Retrying at another directory.",
  2030. status_code, escaped(reason), conn->base_.address,
  2031. conn->base_.port);
  2032. SEND_HS_DESC_FAILED_EVENT("UNEXPECTED");
  2033. SEND_HS_DESC_FAILED_CONTENT();
  2034. break;
  2035. }
  2036. }
  2037. if (conn->base_.purpose == DIR_PURPOSE_UPLOAD_RENDDESC_V2) {
  2038. #define SEND_HS_DESC_UPLOAD_FAILED_EVENT(reason) ( \
  2039. control_event_hs_descriptor_upload_failed(conn->identity_digest, \
  2040. reason) )
  2041. log_info(LD_REND,"Uploaded rendezvous descriptor (status %d "
  2042. "(%s))",
  2043. status_code, escaped(reason));
  2044. switch (status_code) {
  2045. case 200:
  2046. log_info(LD_REND,
  2047. "Uploading rendezvous descriptor: finished with status "
  2048. "200 (%s)", escaped(reason));
  2049. control_event_hs_descriptor_uploaded(conn->identity_digest);
  2050. rend_service_desc_has_uploaded(conn->rend_data);
  2051. break;
  2052. case 400:
  2053. log_warn(LD_REND,"http status 400 (%s) response from dirserver "
  2054. "'%s:%d'. Malformed rendezvous descriptor?",
  2055. escaped(reason), conn->base_.address, conn->base_.port);
  2056. SEND_HS_DESC_UPLOAD_FAILED_EVENT("UPLOAD_REJECTED");
  2057. break;
  2058. default:
  2059. log_warn(LD_REND,"http status %d (%s) response unexpected (server "
  2060. "'%s:%d').",
  2061. status_code, escaped(reason), conn->base_.address,
  2062. conn->base_.port);
  2063. SEND_HS_DESC_UPLOAD_FAILED_EVENT("UNEXPECTED");
  2064. break;
  2065. }
  2066. }
  2067. note_client_request(conn->base_.purpose, was_compressed, orig_len);
  2068. tor_free(body); tor_free(headers); tor_free(reason);
  2069. return 0;
  2070. }
  2071. /** Called when a directory connection reaches EOF. */
  2072. int
  2073. connection_dir_reached_eof(dir_connection_t *conn)
  2074. {
  2075. int retval;
  2076. if (conn->base_.state != DIR_CONN_STATE_CLIENT_READING) {
  2077. log_info(LD_HTTP,"conn reached eof, not reading. [state=%d] Closing.",
  2078. conn->base_.state);
  2079. connection_close_immediate(TO_CONN(conn)); /* error: give up on flushing */
  2080. connection_mark_for_close(TO_CONN(conn));
  2081. return -1;
  2082. }
  2083. retval = connection_dir_client_reached_eof(conn);
  2084. if (retval == 0) /* success */
  2085. conn->base_.state = DIR_CONN_STATE_CLIENT_FINISHED;
  2086. connection_mark_for_close(TO_CONN(conn));
  2087. return retval;
  2088. }
  2089. /** If any directory object is arriving, and it's over 10MB large, we're
  2090. * getting DoS'd. (As of 0.1.2.x, raw directories are about 1MB, and we never
  2091. * ask for more than 96 router descriptors at a time.)
  2092. */
  2093. #define MAX_DIRECTORY_OBJECT_SIZE (10*(1<<20))
  2094. #define MAX_VOTE_DL_SIZE (MAX_DIRECTORY_OBJECT_SIZE * 5)
  2095. /** Read handler for directory connections. (That's connections <em>to</em>
  2096. * directory servers and connections <em>at</em> directory servers.)
  2097. */
  2098. int
  2099. connection_dir_process_inbuf(dir_connection_t *conn)
  2100. {
  2101. size_t max_size;
  2102. tor_assert(conn);
  2103. tor_assert(conn->base_.type == CONN_TYPE_DIR);
  2104. /* Directory clients write, then read data until they receive EOF;
  2105. * directory servers read data until they get an HTTP command, then
  2106. * write their response (when it's finished flushing, they mark for
  2107. * close).
  2108. */
  2109. /* If we're on the dirserver side, look for a command. */
  2110. if (conn->base_.state == DIR_CONN_STATE_SERVER_COMMAND_WAIT) {
  2111. if (directory_handle_command(conn) < 0) {
  2112. connection_mark_for_close(TO_CONN(conn));
  2113. return -1;
  2114. }
  2115. return 0;
  2116. }
  2117. max_size =
  2118. (TO_CONN(conn)->purpose == DIR_PURPOSE_FETCH_STATUS_VOTE) ?
  2119. MAX_VOTE_DL_SIZE : MAX_DIRECTORY_OBJECT_SIZE;
  2120. if (connection_get_inbuf_len(TO_CONN(conn)) > max_size) {
  2121. log_warn(LD_HTTP,
  2122. "Too much data received from directory connection (%s): "
  2123. "denial of service attempt, or you need to upgrade?",
  2124. conn->base_.address);
  2125. connection_mark_for_close(TO_CONN(conn));
  2126. return -1;
  2127. }
  2128. if (!conn->base_.inbuf_reached_eof)
  2129. log_debug(LD_HTTP,"Got data, not eof. Leaving on inbuf.");
  2130. return 0;
  2131. }
  2132. /** Called when we're about to finally unlink and free a directory connection:
  2133. * perform necessary accounting and cleanup */
  2134. void
  2135. connection_dir_about_to_close(dir_connection_t *dir_conn)
  2136. {
  2137. connection_t *conn = TO_CONN(dir_conn);
  2138. if (conn->state < DIR_CONN_STATE_CLIENT_FINISHED) {
  2139. /* It's a directory connection and connecting or fetching
  2140. * failed: forget about this router, and maybe try again. */
  2141. connection_dir_request_failed(dir_conn);
  2142. }
  2143. /* If we were trying to fetch a v2 rend desc and did not succeed,
  2144. * retry as needed. (If a fetch is successful, the connection state
  2145. * is changed to DIR_PURPOSE_HAS_FETCHED_RENDDESC_V2 to mark that
  2146. * refetching is unnecessary.) */
  2147. if (conn->purpose == DIR_PURPOSE_FETCH_RENDDESC_V2 &&
  2148. dir_conn->rend_data &&
  2149. strlen(dir_conn->rend_data->onion_address) == REND_SERVICE_ID_LEN_BASE32)
  2150. rend_client_refetch_v2_renddesc(dir_conn->rend_data);
  2151. }
  2152. /** Create an http response for the client <b>conn</b> out of
  2153. * <b>status</b> and <b>reason_phrase</b>. Write it to <b>conn</b>.
  2154. */
  2155. static void
  2156. write_http_status_line(dir_connection_t *conn, int status,
  2157. const char *reason_phrase)
  2158. {
  2159. char buf[256];
  2160. if (tor_snprintf(buf, sizeof(buf), "HTTP/1.0 %d %s\r\n\r\n",
  2161. status, reason_phrase ? reason_phrase : "OK") < 0) {
  2162. log_warn(LD_BUG,"status line too long.");
  2163. return;
  2164. }
  2165. log_debug(LD_DIRSERV,"Wrote status 'HTTP/1.0 %d %s'", status, reason_phrase);
  2166. connection_write_to_buf(buf, strlen(buf), TO_CONN(conn));
  2167. }
  2168. /** Write the header for an HTTP/1.0 response onto <b>conn</b>-\>outbuf,
  2169. * with <b>type</b> as the Content-Type.
  2170. *
  2171. * If <b>length</b> is nonnegative, it is the Content-Length.
  2172. * If <b>encoding</b> is provided, it is the Content-Encoding.
  2173. * If <b>cache_lifetime</b> is greater than 0, the content may be cached for
  2174. * up to cache_lifetime seconds. Otherwise, the content may not be cached. */
  2175. static void
  2176. write_http_response_header_impl(dir_connection_t *conn, ssize_t length,
  2177. const char *type, const char *encoding,
  2178. const char *extra_headers,
  2179. long cache_lifetime)
  2180. {
  2181. char date[RFC1123_TIME_LEN+1];
  2182. char tmp[1024];
  2183. char *cp;
  2184. time_t now = time(NULL);
  2185. tor_assert(conn);
  2186. format_rfc1123_time(date, now);
  2187. cp = tmp;
  2188. tor_snprintf(cp, sizeof(tmp),
  2189. "HTTP/1.0 200 OK\r\nDate: %s\r\n",
  2190. date);
  2191. cp += strlen(tmp);
  2192. if (type) {
  2193. tor_snprintf(cp, sizeof(tmp)-(cp-tmp), "Content-Type: %s\r\n", type);
  2194. cp += strlen(cp);
  2195. }
  2196. if (!is_local_addr(&conn->base_.addr)) {
  2197. /* Don't report the source address for a nearby/private connection.
  2198. * Otherwise we tend to mis-report in cases where incoming ports are
  2199. * being forwarded to a Tor server running behind the firewall. */
  2200. tor_snprintf(cp, sizeof(tmp)-(cp-tmp),
  2201. X_ADDRESS_HEADER "%s\r\n", conn->base_.address);
  2202. cp += strlen(cp);
  2203. }
  2204. if (encoding) {
  2205. tor_snprintf(cp, sizeof(tmp)-(cp-tmp),
  2206. "Content-Encoding: %s\r\n", encoding);
  2207. cp += strlen(cp);
  2208. }
  2209. if (length >= 0) {
  2210. tor_snprintf(cp, sizeof(tmp)-(cp-tmp),
  2211. "Content-Length: %ld\r\n", (long)length);
  2212. cp += strlen(cp);
  2213. }
  2214. if (cache_lifetime > 0) {
  2215. char expbuf[RFC1123_TIME_LEN+1];
  2216. format_rfc1123_time(expbuf, (time_t)(now + cache_lifetime));
  2217. /* We could say 'Cache-control: max-age=%d' here if we start doing
  2218. * http/1.1 */
  2219. tor_snprintf(cp, sizeof(tmp)-(cp-tmp),
  2220. "Expires: %s\r\n", expbuf);
  2221. cp += strlen(cp);
  2222. } else if (cache_lifetime == 0) {
  2223. /* We could say 'Cache-control: no-cache' here if we start doing
  2224. * http/1.1 */
  2225. strlcpy(cp, "Pragma: no-cache\r\n", sizeof(tmp)-(cp-tmp));
  2226. cp += strlen(cp);
  2227. }
  2228. if (extra_headers) {
  2229. strlcpy(cp, extra_headers, sizeof(tmp)-(cp-tmp));
  2230. cp += strlen(cp);
  2231. }
  2232. if (sizeof(tmp)-(cp-tmp) > 3)
  2233. memcpy(cp, "\r\n", 3);
  2234. else
  2235. tor_assert(0);
  2236. connection_write_to_buf(tmp, strlen(tmp), TO_CONN(conn));
  2237. }
  2238. /** As write_http_response_header_impl, but sets encoding and content-typed
  2239. * based on whether the response will be <b>compressed</b> or not. */
  2240. static void
  2241. write_http_response_header(dir_connection_t *conn, ssize_t length,
  2242. int compressed, long cache_lifetime)
  2243. {
  2244. write_http_response_header_impl(conn, length,
  2245. compressed?"application/octet-stream":"text/plain",
  2246. compressed?"deflate":"identity",
  2247. NULL,
  2248. cache_lifetime);
  2249. }
  2250. #if defined(INSTRUMENT_DOWNLOADS) || defined(RUNNING_DOXYGEN)
  2251. /* DOCDOC */
  2252. typedef struct request_t {
  2253. uint64_t bytes; /**< How many bytes have we transferred? */
  2254. uint64_t count; /**< How many requests have we made? */
  2255. } request_t;
  2256. /** Map used to keep track of how much data we've up/downloaded in what kind
  2257. * of request. Maps from request type to pointer to request_t. */
  2258. static strmap_t *request_map = NULL;
  2259. /** Record that a client request of <b>purpose</b> was made, and that
  2260. * <b>bytes</b> bytes of possibly <b>compressed</b> data were sent/received.
  2261. * Used to keep track of how much we've up/downloaded in what kind of
  2262. * request. */
  2263. static void
  2264. note_client_request(int purpose, int compressed, size_t bytes)
  2265. {
  2266. char *key;
  2267. const char *kind = NULL;
  2268. switch (purpose) {
  2269. case DIR_PURPOSE_FETCH_CONSENSUS: kind = "dl/consensus"; break;
  2270. case DIR_PURPOSE_FETCH_CERTIFICATE: kind = "dl/cert"; break;
  2271. case DIR_PURPOSE_FETCH_STATUS_VOTE: kind = "dl/vote"; break;
  2272. case DIR_PURPOSE_FETCH_DETACHED_SIGNATURES: kind = "dl/detached_sig";
  2273. break;
  2274. case DIR_PURPOSE_FETCH_SERVERDESC: kind = "dl/server"; break;
  2275. case DIR_PURPOSE_FETCH_EXTRAINFO: kind = "dl/extra"; break;
  2276. case DIR_PURPOSE_UPLOAD_DIR: kind = "dl/ul-dir"; break;
  2277. case DIR_PURPOSE_UPLOAD_VOTE: kind = "dl/ul-vote"; break;
  2278. case DIR_PURPOSE_UPLOAD_SIGNATURES: kind = "dl/ul-sig"; break;
  2279. case DIR_PURPOSE_FETCH_RENDDESC_V2: kind = "dl/rend2"; break;
  2280. case DIR_PURPOSE_UPLOAD_RENDDESC_V2: kind = "dl/ul-rend2"; break;
  2281. }
  2282. if (kind) {
  2283. tor_asprintf(&key, "%s%s", kind, compressed?".z":"");
  2284. } else {
  2285. tor_asprintf(&key, "unknown purpose (%d)%s",
  2286. purpose, compressed?".z":"");
  2287. }
  2288. note_request(key, bytes);
  2289. tor_free(key);
  2290. }
  2291. /** Helper: initialize the request map to instrument downloads. */
  2292. static void
  2293. ensure_request_map_initialized(void)
  2294. {
  2295. if (!request_map)
  2296. request_map = strmap_new();
  2297. }
  2298. /** Called when we just transmitted or received <b>bytes</b> worth of data
  2299. * because of a request of type <b>key</b> (an arbitrary identifier): adds
  2300. * <b>bytes</b> to the total associated with key. */
  2301. void
  2302. note_request(const char *key, size_t bytes)
  2303. {
  2304. request_t *r;
  2305. ensure_request_map_initialized();
  2306. r = strmap_get(request_map, key);
  2307. if (!r) {
  2308. r = tor_malloc_zero(sizeof(request_t));
  2309. strmap_set(request_map, key, r);
  2310. }
  2311. r->bytes += bytes;
  2312. r->count++;
  2313. }
  2314. /** Return a newly allocated string holding a summary of bytes used per
  2315. * request type. */
  2316. char *
  2317. directory_dump_request_log(void)
  2318. {
  2319. smartlist_t *lines;
  2320. char *result;
  2321. strmap_iter_t *iter;
  2322. ensure_request_map_initialized();
  2323. lines = smartlist_new();
  2324. for (iter = strmap_iter_init(request_map);
  2325. !strmap_iter_done(iter);
  2326. iter = strmap_iter_next(request_map, iter)) {
  2327. const char *key;
  2328. void *val;
  2329. request_t *r;
  2330. strmap_iter_get(iter, &key, &val);
  2331. r = val;
  2332. smartlist_add_asprintf(lines, "%s "U64_FORMAT" "U64_FORMAT"\n",
  2333. key, U64_PRINTF_ARG(r->bytes), U64_PRINTF_ARG(r->count));
  2334. }
  2335. smartlist_sort_strings(lines);
  2336. result = smartlist_join_strings(lines, "", 0, NULL);
  2337. SMARTLIST_FOREACH(lines, char *, cp, tor_free(cp));
  2338. smartlist_free(lines);
  2339. return result;
  2340. }
  2341. #else
  2342. static void
  2343. note_client_request(int purpose, int compressed, size_t bytes)
  2344. {
  2345. (void)purpose;
  2346. (void)compressed;
  2347. (void)bytes;
  2348. }
  2349. void
  2350. note_request(const char *key, size_t bytes)
  2351. {
  2352. (void)key;
  2353. (void)bytes;
  2354. }
  2355. char *
  2356. directory_dump_request_log(void)
  2357. {
  2358. return tor_strdup("Not supported.");
  2359. }
  2360. #endif
  2361. /** Decide whether a client would accept the consensus we have.
  2362. *
  2363. * Clients can say they only want a consensus if it's signed by more
  2364. * than half the authorities in a list. They pass this list in
  2365. * the url as "...consensus/<b>fpr</b>+<b>fpr</b>+<b>fpr</b>".
  2366. *
  2367. * <b>fpr</b> may be an abbreviated fingerprint, i.e. only a left substring
  2368. * of the full authority identity digest. (Only strings of even length,
  2369. * i.e. encodings of full bytes, are handled correctly. In the case
  2370. * of an odd number of hex digits the last one is silently ignored.)
  2371. *
  2372. * Returns 1 if more than half of the requested authorities signed the
  2373. * consensus, 0 otherwise.
  2374. */
  2375. int
  2376. client_likes_consensus(networkstatus_t *v, const char *want_url)
  2377. {
  2378. smartlist_t *want_authorities = smartlist_new();
  2379. int need_at_least;
  2380. int have = 0;
  2381. dir_split_resource_into_fingerprints(want_url, want_authorities, NULL, 0);
  2382. need_at_least = smartlist_len(want_authorities)/2+1;
  2383. SMARTLIST_FOREACH_BEGIN(want_authorities, const char *, d) {
  2384. char want_digest[DIGEST_LEN];
  2385. size_t want_len = strlen(d)/2;
  2386. if (want_len > DIGEST_LEN)
  2387. want_len = DIGEST_LEN;
  2388. if (base16_decode(want_digest, DIGEST_LEN, d, want_len*2) < 0) {
  2389. log_fn(LOG_PROTOCOL_WARN, LD_DIR,
  2390. "Failed to decode requested authority digest %s.", escaped(d));
  2391. continue;
  2392. };
  2393. SMARTLIST_FOREACH_BEGIN(v->voters, networkstatus_voter_info_t *, vi) {
  2394. if (smartlist_len(vi->sigs) &&
  2395. tor_memeq(vi->identity_digest, want_digest, want_len)) {
  2396. have++;
  2397. break;
  2398. };
  2399. } SMARTLIST_FOREACH_END(vi);
  2400. /* early exit, if we already have enough */
  2401. if (have >= need_at_least)
  2402. break;
  2403. } SMARTLIST_FOREACH_END(d);
  2404. SMARTLIST_FOREACH(want_authorities, char *, d, tor_free(d));
  2405. smartlist_free(want_authorities);
  2406. return (have >= need_at_least);
  2407. }
  2408. /** Return the compression level we should use for sending a compressed
  2409. * response of size <b>n_bytes</b>. */
  2410. static zlib_compression_level_t
  2411. choose_compression_level(ssize_t n_bytes)
  2412. {
  2413. if (! have_been_under_memory_pressure()) {
  2414. return HIGH_COMPRESSION; /* we have plenty of RAM. */
  2415. } else if (n_bytes < 0) {
  2416. return HIGH_COMPRESSION; /* unknown; might be big. */
  2417. } else if (n_bytes < 1024) {
  2418. return LOW_COMPRESSION;
  2419. } else if (n_bytes < 2048) {
  2420. return MEDIUM_COMPRESSION;
  2421. } else {
  2422. return HIGH_COMPRESSION;
  2423. }
  2424. }
  2425. /** Helper function: called when a dirserver gets a complete HTTP GET
  2426. * request. Look for a request for a directory or for a rendezvous
  2427. * service descriptor. On finding one, write a response into
  2428. * conn-\>outbuf. If the request is unrecognized, send a 400.
  2429. * Always return 0. */
  2430. STATIC int
  2431. directory_handle_command_get(dir_connection_t *conn, const char *headers,
  2432. const char *req_body, size_t req_body_len)
  2433. {
  2434. size_t dlen;
  2435. char *url, *url_mem, *header;
  2436. const or_options_t *options = get_options();
  2437. time_t if_modified_since = 0;
  2438. int compressed;
  2439. size_t url_len;
  2440. /* We ignore the body of a GET request. */
  2441. (void)req_body;
  2442. (void)req_body_len;
  2443. log_debug(LD_DIRSERV,"Received GET command.");
  2444. conn->base_.state = DIR_CONN_STATE_SERVER_WRITING;
  2445. if (parse_http_url(headers, &url) < 0) {
  2446. write_http_status_line(conn, 400, "Bad request");
  2447. return 0;
  2448. }
  2449. if ((header = http_get_header(headers, "If-Modified-Since: "))) {
  2450. struct tm tm;
  2451. if (parse_http_time(header, &tm) == 0) {
  2452. if (tor_timegm(&tm, &if_modified_since)<0) {
  2453. if_modified_since = 0;
  2454. } else {
  2455. log_debug(LD_DIRSERV, "If-Modified-Since is '%s'.", escaped(header));
  2456. }
  2457. }
  2458. /* The correct behavior on a malformed If-Modified-Since header is to
  2459. * act as if no If-Modified-Since header had been given. */
  2460. tor_free(header);
  2461. }
  2462. log_debug(LD_DIRSERV,"rewritten url as '%s'.", escaped(url));
  2463. url_mem = url;
  2464. url_len = strlen(url);
  2465. compressed = url_len > 2 && !strcmp(url+url_len-2, ".z");
  2466. if (compressed) {
  2467. url[url_len-2] = '\0';
  2468. url_len -= 2;
  2469. }
  2470. if (!strcmp(url,"/tor/")) {
  2471. const char *frontpage = get_dirportfrontpage();
  2472. if (frontpage) {
  2473. dlen = strlen(frontpage);
  2474. /* Let's return a disclaimer page (users shouldn't use V1 anymore,
  2475. and caches don't fetch '/', so this is safe). */
  2476. /* [We don't check for write_bucket_low here, since we want to serve
  2477. * this page no matter what.] */
  2478. note_request(url, dlen);
  2479. write_http_response_header_impl(conn, dlen, "text/html", "identity",
  2480. NULL, DIRPORTFRONTPAGE_CACHE_LIFETIME);
  2481. connection_write_to_buf(frontpage, dlen, TO_CONN(conn));
  2482. goto done;
  2483. }
  2484. /* if no disclaimer file, fall through and continue */
  2485. }
  2486. if (!strcmpstart(url, "/tor/status-vote/current/consensus")) {
  2487. /* v3 network status fetch. */
  2488. smartlist_t *dir_fps = smartlist_new();
  2489. const char *request_type = NULL;
  2490. long lifetime = NETWORKSTATUS_CACHE_LIFETIME;
  2491. if (1) {
  2492. networkstatus_t *v;
  2493. time_t now = time(NULL);
  2494. const char *want_fps = NULL;
  2495. char *flavor = NULL;
  2496. int flav = FLAV_NS;
  2497. #define CONSENSUS_URL_PREFIX "/tor/status-vote/current/consensus/"
  2498. #define CONSENSUS_FLAVORED_PREFIX "/tor/status-vote/current/consensus-"
  2499. /* figure out the flavor if any, and who we wanted to sign the thing */
  2500. if (!strcmpstart(url, CONSENSUS_FLAVORED_PREFIX)) {
  2501. const char *f, *cp;
  2502. f = url + strlen(CONSENSUS_FLAVORED_PREFIX);
  2503. cp = strchr(f, '/');
  2504. if (cp) {
  2505. want_fps = cp+1;
  2506. flavor = tor_strndup(f, cp-f);
  2507. } else {
  2508. flavor = tor_strdup(f);
  2509. }
  2510. flav = networkstatus_parse_flavor_name(flavor);
  2511. if (flav < 0)
  2512. flav = FLAV_NS;
  2513. } else {
  2514. if (!strcmpstart(url, CONSENSUS_URL_PREFIX))
  2515. want_fps = url+strlen(CONSENSUS_URL_PREFIX);
  2516. }
  2517. v = networkstatus_get_latest_consensus_by_flavor(flav);
  2518. if (v && want_fps &&
  2519. !client_likes_consensus(v, want_fps)) {
  2520. write_http_status_line(conn, 404, "Consensus not signed by sufficient "
  2521. "number of requested authorities");
  2522. smartlist_free(dir_fps);
  2523. geoip_note_ns_response(GEOIP_REJECT_NOT_ENOUGH_SIGS);
  2524. tor_free(flavor);
  2525. goto done;
  2526. }
  2527. {
  2528. char *fp = tor_malloc_zero(DIGEST_LEN);
  2529. if (flavor)
  2530. strlcpy(fp, flavor, DIGEST_LEN);
  2531. tor_free(flavor);
  2532. smartlist_add(dir_fps, fp);
  2533. }
  2534. request_type = compressed?"v3.z":"v3";
  2535. lifetime = (v && v->fresh_until > now) ? v->fresh_until - now : 0;
  2536. }
  2537. if (!smartlist_len(dir_fps)) { /* we failed to create/cache cp */
  2538. write_http_status_line(conn, 503, "Network status object unavailable");
  2539. smartlist_free(dir_fps);
  2540. geoip_note_ns_response(GEOIP_REJECT_UNAVAILABLE);
  2541. goto done;
  2542. }
  2543. if (!dirserv_remove_old_statuses(dir_fps, if_modified_since)) {
  2544. write_http_status_line(conn, 404, "Not found");
  2545. SMARTLIST_FOREACH(dir_fps, char *, cp, tor_free(cp));
  2546. smartlist_free(dir_fps);
  2547. geoip_note_ns_response(GEOIP_REJECT_NOT_FOUND);
  2548. goto done;
  2549. } else if (!smartlist_len(dir_fps)) {
  2550. write_http_status_line(conn, 304, "Not modified");
  2551. SMARTLIST_FOREACH(dir_fps, char *, cp, tor_free(cp));
  2552. smartlist_free(dir_fps);
  2553. geoip_note_ns_response(GEOIP_REJECT_NOT_MODIFIED);
  2554. goto done;
  2555. }
  2556. dlen = dirserv_estimate_data_size(dir_fps, 0, compressed);
  2557. if (global_write_bucket_low(TO_CONN(conn), dlen, 2)) {
  2558. log_debug(LD_DIRSERV,
  2559. "Client asked for network status lists, but we've been "
  2560. "writing too many bytes lately. Sending 503 Dir busy.");
  2561. write_http_status_line(conn, 503, "Directory busy, try again later");
  2562. SMARTLIST_FOREACH(dir_fps, char *, fp, tor_free(fp));
  2563. smartlist_free(dir_fps);
  2564. geoip_note_ns_response(GEOIP_REJECT_BUSY);
  2565. goto done;
  2566. }
  2567. if (1) {
  2568. struct in_addr in;
  2569. tor_addr_t addr;
  2570. if (tor_inet_aton((TO_CONN(conn))->address, &in)) {
  2571. tor_addr_from_ipv4h(&addr, ntohl(in.s_addr));
  2572. geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS,
  2573. &addr, NULL,
  2574. time(NULL));
  2575. geoip_note_ns_response(GEOIP_SUCCESS);
  2576. /* Note that a request for a network status has started, so that we
  2577. * can measure the download time later on. */
  2578. if (conn->dirreq_id)
  2579. geoip_start_dirreq(conn->dirreq_id, dlen, DIRREQ_TUNNELED);
  2580. else
  2581. geoip_start_dirreq(TO_CONN(conn)->global_identifier, dlen,
  2582. DIRREQ_DIRECT);
  2583. }
  2584. }
  2585. // note_request(request_type,dlen);
  2586. (void) request_type;
  2587. write_http_response_header(conn, -1, compressed,
  2588. smartlist_len(dir_fps) == 1 ? lifetime : 0);
  2589. conn->fingerprint_stack = dir_fps;
  2590. if (! compressed)
  2591. conn->zlib_state = tor_zlib_new(0, ZLIB_METHOD, HIGH_COMPRESSION);
  2592. /* Prime the connection with some data. */
  2593. conn->dir_spool_src = DIR_SPOOL_NETWORKSTATUS;
  2594. connection_dirserv_flushed_some(conn);
  2595. goto done;
  2596. }
  2597. if (!strcmpstart(url,"/tor/status-vote/current/") ||
  2598. !strcmpstart(url,"/tor/status-vote/next/")) {
  2599. /* XXXX If-modified-since is only implemented for the current
  2600. * consensus: that's probably fine, since it's the only vote document
  2601. * people fetch much. */
  2602. int current;
  2603. ssize_t body_len = 0;
  2604. ssize_t estimated_len = 0;
  2605. smartlist_t *items = smartlist_new();
  2606. smartlist_t *dir_items = smartlist_new();
  2607. int lifetime = 60; /* XXXX023 should actually use vote intervals. */
  2608. url += strlen("/tor/status-vote/");
  2609. current = !strcmpstart(url, "current/");
  2610. url = strchr(url, '/');
  2611. tor_assert(url);
  2612. ++url;
  2613. if (!strcmp(url, "consensus")) {
  2614. const char *item;
  2615. tor_assert(!current); /* we handle current consensus specially above,
  2616. * since it wants to be spooled. */
  2617. if ((item = dirvote_get_pending_consensus(FLAV_NS)))
  2618. smartlist_add(items, (char*)item);
  2619. } else if (!current && !strcmp(url, "consensus-signatures")) {
  2620. /* XXXX the spec says that we should implement
  2621. * current/consensus-signatures too. It doesn't seem to be needed,
  2622. * though. */
  2623. const char *item;
  2624. if ((item=dirvote_get_pending_detached_signatures()))
  2625. smartlist_add(items, (char*)item);
  2626. } else if (!strcmp(url, "authority")) {
  2627. const cached_dir_t *d;
  2628. int flags = DGV_BY_ID |
  2629. (current ? DGV_INCLUDE_PREVIOUS : DGV_INCLUDE_PENDING);
  2630. if ((d=dirvote_get_vote(NULL, flags)))
  2631. smartlist_add(dir_items, (cached_dir_t*)d);
  2632. } else {
  2633. const cached_dir_t *d;
  2634. smartlist_t *fps = smartlist_new();
  2635. int flags;
  2636. if (!strcmpstart(url, "d/")) {
  2637. url += 2;
  2638. flags = DGV_INCLUDE_PENDING | DGV_INCLUDE_PREVIOUS;
  2639. } else {
  2640. flags = DGV_BY_ID |
  2641. (current ? DGV_INCLUDE_PREVIOUS : DGV_INCLUDE_PENDING);
  2642. }
  2643. dir_split_resource_into_fingerprints(url, fps, NULL,
  2644. DSR_HEX|DSR_SORT_UNIQ);
  2645. SMARTLIST_FOREACH(fps, char *, fp, {
  2646. if ((d = dirvote_get_vote(fp, flags)))
  2647. smartlist_add(dir_items, (cached_dir_t*)d);
  2648. tor_free(fp);
  2649. });
  2650. smartlist_free(fps);
  2651. }
  2652. if (!smartlist_len(dir_items) && !smartlist_len(items)) {
  2653. write_http_status_line(conn, 404, "Not found");
  2654. goto vote_done;
  2655. }
  2656. SMARTLIST_FOREACH(dir_items, cached_dir_t *, d,
  2657. body_len += compressed ? d->dir_z_len : d->dir_len);
  2658. estimated_len += body_len;
  2659. SMARTLIST_FOREACH(items, const char *, item, {
  2660. size_t ln = strlen(item);
  2661. if (compressed) {
  2662. estimated_len += ln/2;
  2663. } else {
  2664. body_len += ln; estimated_len += ln;
  2665. }
  2666. });
  2667. if (global_write_bucket_low(TO_CONN(conn), estimated_len, 2)) {
  2668. write_http_status_line(conn, 503, "Directory busy, try again later");
  2669. goto vote_done;
  2670. }
  2671. write_http_response_header(conn, body_len ? body_len : -1, compressed,
  2672. lifetime);
  2673. if (smartlist_len(items)) {
  2674. if (compressed) {
  2675. conn->zlib_state = tor_zlib_new(1, ZLIB_METHOD,
  2676. choose_compression_level(estimated_len));
  2677. SMARTLIST_FOREACH(items, const char *, c,
  2678. connection_write_to_buf_zlib(c, strlen(c), conn, 0));
  2679. connection_write_to_buf_zlib("", 0, conn, 1);
  2680. } else {
  2681. SMARTLIST_FOREACH(items, const char *, c,
  2682. connection_write_to_buf(c, strlen(c), TO_CONN(conn)));
  2683. }
  2684. } else {
  2685. SMARTLIST_FOREACH(dir_items, cached_dir_t *, d,
  2686. connection_write_to_buf(compressed ? d->dir_z : d->dir,
  2687. compressed ? d->dir_z_len : d->dir_len,
  2688. TO_CONN(conn)));
  2689. }
  2690. vote_done:
  2691. smartlist_free(items);
  2692. smartlist_free(dir_items);
  2693. goto done;
  2694. }
  2695. if (!strcmpstart(url, "/tor/micro/d/")) {
  2696. smartlist_t *fps = smartlist_new();
  2697. dir_split_resource_into_fingerprints(url+strlen("/tor/micro/d/"),
  2698. fps, NULL,
  2699. DSR_DIGEST256|DSR_BASE64|DSR_SORT_UNIQ);
  2700. if (!dirserv_have_any_microdesc(fps)) {
  2701. write_http_status_line(conn, 404, "Not found");
  2702. SMARTLIST_FOREACH(fps, char *, fp, tor_free(fp));
  2703. smartlist_free(fps);
  2704. goto done;
  2705. }
  2706. dlen = dirserv_estimate_microdesc_size(fps, compressed);
  2707. if (global_write_bucket_low(TO_CONN(conn), dlen, 2)) {
  2708. log_info(LD_DIRSERV,
  2709. "Client asked for server descriptors, but we've been "
  2710. "writing too many bytes lately. Sending 503 Dir busy.");
  2711. write_http_status_line(conn, 503, "Directory busy, try again later");
  2712. SMARTLIST_FOREACH(fps, char *, fp, tor_free(fp));
  2713. smartlist_free(fps);
  2714. goto done;
  2715. }
  2716. write_http_response_header(conn, -1, compressed, MICRODESC_CACHE_LIFETIME);
  2717. conn->dir_spool_src = DIR_SPOOL_MICRODESC;
  2718. conn->fingerprint_stack = fps;
  2719. if (compressed)
  2720. conn->zlib_state = tor_zlib_new(1, ZLIB_METHOD,
  2721. choose_compression_level(dlen));
  2722. connection_dirserv_flushed_some(conn);
  2723. goto done;
  2724. }
  2725. if (!strcmpstart(url,"/tor/server/") ||
  2726. (!options->BridgeAuthoritativeDir &&
  2727. !options->BridgeRelay && !strcmpstart(url,"/tor/extra/"))) {
  2728. int res;
  2729. const char *msg;
  2730. const char *request_type = NULL;
  2731. int cache_lifetime = 0;
  2732. int is_extra = !strcmpstart(url,"/tor/extra/");
  2733. url += is_extra ? strlen("/tor/extra/") : strlen("/tor/server/");
  2734. conn->fingerprint_stack = smartlist_new();
  2735. res = dirserv_get_routerdesc_fingerprints(conn->fingerprint_stack, url,
  2736. &msg,
  2737. !connection_dir_is_encrypted(conn),
  2738. is_extra);
  2739. if (!strcmpstart(url, "fp/")) {
  2740. request_type = compressed?"/tor/server/fp.z":"/tor/server/fp";
  2741. if (smartlist_len(conn->fingerprint_stack) == 1)
  2742. cache_lifetime = ROUTERDESC_CACHE_LIFETIME;
  2743. } else if (!strcmpstart(url, "authority")) {
  2744. request_type = compressed?"/tor/server/authority.z":
  2745. "/tor/server/authority";
  2746. cache_lifetime = ROUTERDESC_CACHE_LIFETIME;
  2747. } else if (!strcmpstart(url, "all")) {
  2748. request_type = compressed?"/tor/server/all.z":"/tor/server/all";
  2749. cache_lifetime = FULL_DIR_CACHE_LIFETIME;
  2750. } else if (!strcmpstart(url, "d/")) {
  2751. request_type = compressed?"/tor/server/d.z":"/tor/server/d";
  2752. if (smartlist_len(conn->fingerprint_stack) == 1)
  2753. cache_lifetime = ROUTERDESC_BY_DIGEST_CACHE_LIFETIME;
  2754. } else {
  2755. request_type = "/tor/server/?";
  2756. }
  2757. (void) request_type; /* usable for note_request. */
  2758. if (!strcmpstart(url, "d/"))
  2759. conn->dir_spool_src =
  2760. is_extra ? DIR_SPOOL_EXTRA_BY_DIGEST : DIR_SPOOL_SERVER_BY_DIGEST;
  2761. else
  2762. conn->dir_spool_src =
  2763. is_extra ? DIR_SPOOL_EXTRA_BY_FP : DIR_SPOOL_SERVER_BY_FP;
  2764. if (!dirserv_have_any_serverdesc(conn->fingerprint_stack,
  2765. conn->dir_spool_src)) {
  2766. res = -1;
  2767. msg = "Not found";
  2768. }
  2769. if (res < 0)
  2770. write_http_status_line(conn, 404, msg);
  2771. else {
  2772. dlen = dirserv_estimate_data_size(conn->fingerprint_stack,
  2773. 1, compressed);
  2774. if (global_write_bucket_low(TO_CONN(conn), dlen, 2)) {
  2775. log_info(LD_DIRSERV,
  2776. "Client asked for server descriptors, but we've been "
  2777. "writing too many bytes lately. Sending 503 Dir busy.");
  2778. write_http_status_line(conn, 503, "Directory busy, try again later");
  2779. conn->dir_spool_src = DIR_SPOOL_NONE;
  2780. goto done;
  2781. }
  2782. write_http_response_header(conn, -1, compressed, cache_lifetime);
  2783. if (compressed)
  2784. conn->zlib_state = tor_zlib_new(1, ZLIB_METHOD,
  2785. choose_compression_level(dlen));
  2786. /* Prime the connection with some data. */
  2787. connection_dirserv_flushed_some(conn);
  2788. }
  2789. goto done;
  2790. }
  2791. if (!strcmpstart(url,"/tor/keys/")) {
  2792. smartlist_t *certs = smartlist_new();
  2793. ssize_t len = -1;
  2794. if (!strcmp(url, "/tor/keys/all")) {
  2795. authority_cert_get_all(certs);
  2796. } else if (!strcmp(url, "/tor/keys/authority")) {
  2797. authority_cert_t *cert = get_my_v3_authority_cert();
  2798. if (cert)
  2799. smartlist_add(certs, cert);
  2800. } else if (!strcmpstart(url, "/tor/keys/fp/")) {
  2801. smartlist_t *fps = smartlist_new();
  2802. dir_split_resource_into_fingerprints(url+strlen("/tor/keys/fp/"),
  2803. fps, NULL,
  2804. DSR_HEX|DSR_SORT_UNIQ);
  2805. SMARTLIST_FOREACH(fps, char *, d, {
  2806. authority_cert_t *c = authority_cert_get_newest_by_id(d);
  2807. if (c) smartlist_add(certs, c);
  2808. tor_free(d);
  2809. });
  2810. smartlist_free(fps);
  2811. } else if (!strcmpstart(url, "/tor/keys/sk/")) {
  2812. smartlist_t *fps = smartlist_new();
  2813. dir_split_resource_into_fingerprints(url+strlen("/tor/keys/sk/"),
  2814. fps, NULL,
  2815. DSR_HEX|DSR_SORT_UNIQ);
  2816. SMARTLIST_FOREACH(fps, char *, d, {
  2817. authority_cert_t *c = authority_cert_get_by_sk_digest(d);
  2818. if (c) smartlist_add(certs, c);
  2819. tor_free(d);
  2820. });
  2821. smartlist_free(fps);
  2822. } else if (!strcmpstart(url, "/tor/keys/fp-sk/")) {
  2823. smartlist_t *fp_sks = smartlist_new();
  2824. dir_split_resource_into_fingerprint_pairs(url+strlen("/tor/keys/fp-sk/"),
  2825. fp_sks);
  2826. SMARTLIST_FOREACH(fp_sks, fp_pair_t *, pair, {
  2827. authority_cert_t *c = authority_cert_get_by_digests(pair->first,
  2828. pair->second);
  2829. if (c) smartlist_add(certs, c);
  2830. tor_free(pair);
  2831. });
  2832. smartlist_free(fp_sks);
  2833. } else {
  2834. write_http_status_line(conn, 400, "Bad request");
  2835. goto keys_done;
  2836. }
  2837. if (!smartlist_len(certs)) {
  2838. write_http_status_line(conn, 404, "Not found");
  2839. goto keys_done;
  2840. }
  2841. SMARTLIST_FOREACH(certs, authority_cert_t *, c,
  2842. if (c->cache_info.published_on < if_modified_since)
  2843. SMARTLIST_DEL_CURRENT(certs, c));
  2844. if (!smartlist_len(certs)) {
  2845. write_http_status_line(conn, 304, "Not modified");
  2846. goto keys_done;
  2847. }
  2848. len = 0;
  2849. SMARTLIST_FOREACH(certs, authority_cert_t *, c,
  2850. len += c->cache_info.signed_descriptor_len);
  2851. if (global_write_bucket_low(TO_CONN(conn), compressed?len/2:len, 2)) {
  2852. write_http_status_line(conn, 503, "Directory busy, try again later");
  2853. goto keys_done;
  2854. }
  2855. write_http_response_header(conn, compressed?-1:len, compressed, 60*60);
  2856. if (compressed) {
  2857. conn->zlib_state = tor_zlib_new(1, ZLIB_METHOD,
  2858. choose_compression_level(len));
  2859. SMARTLIST_FOREACH(certs, authority_cert_t *, c,
  2860. connection_write_to_buf_zlib(c->cache_info.signed_descriptor_body,
  2861. c->cache_info.signed_descriptor_len,
  2862. conn, 0));
  2863. connection_write_to_buf_zlib("", 0, conn, 1);
  2864. } else {
  2865. SMARTLIST_FOREACH(certs, authority_cert_t *, c,
  2866. connection_write_to_buf(c->cache_info.signed_descriptor_body,
  2867. c->cache_info.signed_descriptor_len,
  2868. TO_CONN(conn)));
  2869. }
  2870. keys_done:
  2871. smartlist_free(certs);
  2872. goto done;
  2873. }
  2874. if (connection_dir_is_encrypted(conn) &&
  2875. !strcmpstart(url,"/tor/rendezvous2/")) {
  2876. /* Handle v2 rendezvous descriptor fetch request. */
  2877. const char *descp;
  2878. const char *query = url + strlen("/tor/rendezvous2/");
  2879. if (rend_valid_descriptor_id(query)) {
  2880. log_info(LD_REND, "Got a v2 rendezvous descriptor request for ID '%s'",
  2881. safe_str(escaped(query)));
  2882. switch (rend_cache_lookup_v2_desc_as_dir(query, &descp)) {
  2883. case 1: /* valid */
  2884. write_http_response_header(conn, strlen(descp), 0, 0);
  2885. connection_write_to_buf(descp, strlen(descp), TO_CONN(conn));
  2886. break;
  2887. case 0: /* well-formed but not present */
  2888. write_http_status_line(conn, 404, "Not found");
  2889. break;
  2890. case -1: /* not well-formed */
  2891. write_http_status_line(conn, 400, "Bad request");
  2892. break;
  2893. }
  2894. } else { /* not well-formed */
  2895. write_http_status_line(conn, 400, "Bad request");
  2896. }
  2897. goto done;
  2898. }
  2899. if (options->BridgeAuthoritativeDir &&
  2900. options->BridgePassword_AuthDigest_ &&
  2901. connection_dir_is_encrypted(conn) &&
  2902. !strcmp(url,"/tor/networkstatus-bridges")) {
  2903. char *status;
  2904. char digest[DIGEST256_LEN];
  2905. header = http_get_header(headers, "Authorization: Basic ");
  2906. if (header)
  2907. crypto_digest256(digest, header, strlen(header), DIGEST_SHA256);
  2908. /* now make sure the password is there and right */
  2909. if (!header ||
  2910. tor_memneq(digest,
  2911. options->BridgePassword_AuthDigest_, DIGEST256_LEN)) {
  2912. write_http_status_line(conn, 404, "Not found");
  2913. tor_free(header);
  2914. goto done;
  2915. }
  2916. tor_free(header);
  2917. /* all happy now. send an answer. */
  2918. status = networkstatus_getinfo_by_purpose("bridge", time(NULL));
  2919. dlen = strlen(status);
  2920. write_http_response_header(conn, dlen, 0, 0);
  2921. connection_write_to_buf(status, dlen, TO_CONN(conn));
  2922. tor_free(status);
  2923. goto done;
  2924. }
  2925. if (!strcmpstart(url,"/tor/bytes.txt")) {
  2926. char *bytes = directory_dump_request_log();
  2927. size_t len = strlen(bytes);
  2928. write_http_response_header(conn, len, 0, 0);
  2929. connection_write_to_buf(bytes, len, TO_CONN(conn));
  2930. tor_free(bytes);
  2931. goto done;
  2932. }
  2933. if (!strcmp(url,"/tor/robots.txt")) { /* /robots.txt will have been
  2934. rewritten to /tor/robots.txt */
  2935. char robots[] = "User-agent: *\r\nDisallow: /\r\n";
  2936. size_t len = strlen(robots);
  2937. write_http_response_header(conn, len, 0, ROBOTS_CACHE_LIFETIME);
  2938. connection_write_to_buf(robots, len, TO_CONN(conn));
  2939. goto done;
  2940. }
  2941. #if defined(EXPORTMALLINFO) && defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
  2942. #define ADD_MALLINFO_LINE(x) do { \
  2943. smartlist_add_asprintf(lines, "%s %d\n", #x, mi.x); \
  2944. }while(0);
  2945. if (!strcmp(url,"/tor/mallinfo.txt") &&
  2946. (tor_addr_eq_ipv4h(&conn->base_.addr, 0x7f000001ul))) {
  2947. char *result;
  2948. size_t len;
  2949. struct mallinfo mi;
  2950. smartlist_t *lines;
  2951. memset(&mi, 0, sizeof(mi));
  2952. mi = mallinfo();
  2953. lines = smartlist_new();
  2954. ADD_MALLINFO_LINE(arena)
  2955. ADD_MALLINFO_LINE(ordblks)
  2956. ADD_MALLINFO_LINE(smblks)
  2957. ADD_MALLINFO_LINE(hblks)
  2958. ADD_MALLINFO_LINE(hblkhd)
  2959. ADD_MALLINFO_LINE(usmblks)
  2960. ADD_MALLINFO_LINE(fsmblks)
  2961. ADD_MALLINFO_LINE(uordblks)
  2962. ADD_MALLINFO_LINE(fordblks)
  2963. ADD_MALLINFO_LINE(keepcost)
  2964. result = smartlist_join_strings(lines, "", 0, NULL);
  2965. SMARTLIST_FOREACH(lines, char *, cp, tor_free(cp));
  2966. smartlist_free(lines);
  2967. len = strlen(result);
  2968. write_http_response_header(conn, len, 0, 0);
  2969. connection_write_to_buf(result, len, TO_CONN(conn));
  2970. tor_free(result);
  2971. goto done;
  2972. }
  2973. #endif
  2974. /* we didn't recognize the url */
  2975. write_http_status_line(conn, 404, "Not found");
  2976. done:
  2977. tor_free(url_mem);
  2978. return 0;
  2979. }
  2980. /** Helper function: called when a dirserver gets a complete HTTP POST
  2981. * request. Look for an uploaded server descriptor or rendezvous
  2982. * service descriptor. On finding one, process it and write a
  2983. * response into conn-\>outbuf. If the request is unrecognized, send a
  2984. * 400. Always return 0. */
  2985. static int
  2986. directory_handle_command_post(dir_connection_t *conn, const char *headers,
  2987. const char *body, size_t body_len)
  2988. {
  2989. char *url = NULL;
  2990. const or_options_t *options = get_options();
  2991. log_debug(LD_DIRSERV,"Received POST command.");
  2992. conn->base_.state = DIR_CONN_STATE_SERVER_WRITING;
  2993. if (parse_http_url(headers, &url) < 0) {
  2994. write_http_status_line(conn, 400, "Bad request");
  2995. return 0;
  2996. }
  2997. log_debug(LD_DIRSERV,"rewritten url as '%s'.", escaped(url));
  2998. /* Handle v2 rendezvous service publish request. */
  2999. if (connection_dir_is_encrypted(conn) &&
  3000. !strcmpstart(url,"/tor/rendezvous2/publish")) {
  3001. switch (rend_cache_store_v2_desc_as_dir(body)) {
  3002. case RCS_NOTDIR:
  3003. log_info(LD_REND, "Rejected v2 rend descriptor (length %d) from %s "
  3004. "since we're not currently a hidden service directory.",
  3005. (int)body_len, conn->base_.address);
  3006. write_http_status_line(conn, 503, "Currently not acting as v2 "
  3007. "hidden service directory");
  3008. break;
  3009. case RCS_BADDESC:
  3010. log_warn(LD_REND, "Rejected v2 rend descriptor (length %d) from %s.",
  3011. (int)body_len, conn->base_.address);
  3012. write_http_status_line(conn, 400,
  3013. "Invalid v2 service descriptor rejected");
  3014. break;
  3015. case RCS_OKAY:
  3016. default:
  3017. write_http_status_line(conn, 200, "Service descriptor (v2) stored");
  3018. log_info(LD_REND, "Handled v2 rendezvous descriptor post: accepted");
  3019. }
  3020. goto done;
  3021. }
  3022. if (!authdir_mode(options)) {
  3023. /* we just provide cached directories; we don't want to
  3024. * receive anything. */
  3025. write_http_status_line(conn, 400, "Nonauthoritative directory does not "
  3026. "accept posted server descriptors");
  3027. goto done;
  3028. }
  3029. if (authdir_mode_handles_descs(options, -1) &&
  3030. !strcmp(url,"/tor/")) { /* server descriptor post */
  3031. const char *msg = "[None]";
  3032. uint8_t purpose = authdir_mode_bridge(options) ?
  3033. ROUTER_PURPOSE_BRIDGE : ROUTER_PURPOSE_GENERAL;
  3034. was_router_added_t r = dirserv_add_multiple_descriptors(body, purpose,
  3035. conn->base_.address, &msg);
  3036. tor_assert(msg);
  3037. if (r == ROUTER_ADDED_NOTIFY_GENERATOR) {
  3038. /* Accepted with a message. */
  3039. log_info(LD_DIRSERV,
  3040. "Problematic router descriptor or extra-info from %s "
  3041. "(\"%s\").",
  3042. conn->base_.address, msg);
  3043. write_http_status_line(conn, 400, msg);
  3044. } else if (r == ROUTER_ADDED_SUCCESSFULLY) {
  3045. write_http_status_line(conn, 200, msg);
  3046. } else if (WRA_WAS_OUTDATED(r)) {
  3047. write_http_response_header_impl(conn, -1, NULL, NULL,
  3048. "X-Descriptor-Not-New: Yes\r\n", -1);
  3049. } else {
  3050. log_info(LD_DIRSERV,
  3051. "Rejected router descriptor or extra-info from %s "
  3052. "(\"%s\").",
  3053. conn->base_.address, msg);
  3054. write_http_status_line(conn, 400, msg);
  3055. }
  3056. goto done;
  3057. }
  3058. if (authdir_mode_v3(options) &&
  3059. !strcmp(url,"/tor/post/vote")) { /* v3 networkstatus vote */
  3060. const char *msg = "OK";
  3061. int status;
  3062. if (dirvote_add_vote(body, &msg, &status)) {
  3063. write_http_status_line(conn, status, "Vote stored");
  3064. } else {
  3065. tor_assert(msg);
  3066. log_warn(LD_DIRSERV, "Rejected vote from %s (\"%s\").",
  3067. conn->base_.address, msg);
  3068. write_http_status_line(conn, status, msg);
  3069. }
  3070. goto done;
  3071. }
  3072. if (authdir_mode_v3(options) &&
  3073. !strcmp(url,"/tor/post/consensus-signature")) { /* sigs on consensus. */
  3074. const char *msg = NULL;
  3075. if (dirvote_add_signatures(body, conn->base_.address, &msg)>=0) {
  3076. write_http_status_line(conn, 200, msg?msg:"Signatures stored");
  3077. } else {
  3078. log_warn(LD_DIR, "Unable to store signatures posted by %s: %s",
  3079. conn->base_.address, msg?msg:"???");
  3080. write_http_status_line(conn, 400, msg?msg:"Unable to store signatures");
  3081. }
  3082. goto done;
  3083. }
  3084. /* we didn't recognize the url */
  3085. write_http_status_line(conn, 404, "Not found");
  3086. done:
  3087. tor_free(url);
  3088. return 0;
  3089. }
  3090. /** Called when a dirserver receives data on a directory connection;
  3091. * looks for an HTTP request. If the request is complete, remove it
  3092. * from the inbuf, try to process it; otherwise, leave it on the
  3093. * buffer. Return a 0 on success, or -1 on error.
  3094. */
  3095. static int
  3096. directory_handle_command(dir_connection_t *conn)
  3097. {
  3098. char *headers=NULL, *body=NULL;
  3099. size_t body_len=0;
  3100. int r;
  3101. tor_assert(conn);
  3102. tor_assert(conn->base_.type == CONN_TYPE_DIR);
  3103. switch (connection_fetch_from_buf_http(TO_CONN(conn),
  3104. &headers, MAX_HEADERS_SIZE,
  3105. &body, &body_len, MAX_DIR_UL_SIZE, 0)) {
  3106. case -1: /* overflow */
  3107. log_warn(LD_DIRSERV,
  3108. "Request too large from address '%s' to DirPort. Closing.",
  3109. safe_str(conn->base_.address));
  3110. return -1;
  3111. case 0:
  3112. log_debug(LD_DIRSERV,"command not all here yet.");
  3113. return 0;
  3114. /* case 1, fall through */
  3115. }
  3116. http_set_address_origin(headers, TO_CONN(conn));
  3117. // we should escape headers here as well,
  3118. // but we can't call escaped() twice, as it uses the same buffer
  3119. //log_debug(LD_DIRSERV,"headers %s, body %s.", headers, escaped(body));
  3120. if (!strncasecmp(headers,"GET",3))
  3121. r = directory_handle_command_get(conn, headers, body, body_len);
  3122. else if (!strncasecmp(headers,"POST",4))
  3123. r = directory_handle_command_post(conn, headers, body, body_len);
  3124. else {
  3125. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  3126. "Got headers %s with unknown command. Closing.",
  3127. escaped(headers));
  3128. r = -1;
  3129. }
  3130. tor_free(headers); tor_free(body);
  3131. return r;
  3132. }
  3133. /** Write handler for directory connections; called when all data has
  3134. * been flushed. Close the connection or wait for a response as
  3135. * appropriate.
  3136. */
  3137. int
  3138. connection_dir_finished_flushing(dir_connection_t *conn)
  3139. {
  3140. tor_assert(conn);
  3141. tor_assert(conn->base_.type == CONN_TYPE_DIR);
  3142. /* Note that we have finished writing the directory response. For direct
  3143. * connections this means we're done; for tunneled connections it's only
  3144. * an intermediate step. */
  3145. if (conn->dirreq_id)
  3146. geoip_change_dirreq_state(conn->dirreq_id, DIRREQ_TUNNELED,
  3147. DIRREQ_FLUSHING_DIR_CONN_FINISHED);
  3148. else
  3149. geoip_change_dirreq_state(TO_CONN(conn)->global_identifier,
  3150. DIRREQ_DIRECT,
  3151. DIRREQ_FLUSHING_DIR_CONN_FINISHED);
  3152. switch (conn->base_.state) {
  3153. case DIR_CONN_STATE_CONNECTING:
  3154. case DIR_CONN_STATE_CLIENT_SENDING:
  3155. log_debug(LD_DIR,"client finished sending command.");
  3156. conn->base_.state = DIR_CONN_STATE_CLIENT_READING;
  3157. return 0;
  3158. case DIR_CONN_STATE_SERVER_WRITING:
  3159. if (conn->dir_spool_src != DIR_SPOOL_NONE) {
  3160. #ifdef USE_BUFFEREVENTS
  3161. /* This can happen with paired bufferevents, since a paired connection
  3162. * can flush immediately when you write to it, making the subsequent
  3163. * check in connection_handle_write_cb() decide that the connection
  3164. * is flushed. */
  3165. log_debug(LD_DIRSERV, "Emptied a dirserv buffer, but still spooling.");
  3166. #else
  3167. log_warn(LD_BUG, "Emptied a dirserv buffer, but it's still spooling!");
  3168. connection_mark_for_close(TO_CONN(conn));
  3169. #endif
  3170. } else {
  3171. log_debug(LD_DIRSERV, "Finished writing server response. Closing.");
  3172. connection_mark_for_close(TO_CONN(conn));
  3173. }
  3174. return 0;
  3175. default:
  3176. log_warn(LD_BUG,"called in unexpected state %d.",
  3177. conn->base_.state);
  3178. tor_fragile_assert();
  3179. return -1;
  3180. }
  3181. return 0;
  3182. }
  3183. /** Connected handler for directory connections: begin sending data to the
  3184. * server */
  3185. int
  3186. connection_dir_finished_connecting(dir_connection_t *conn)
  3187. {
  3188. tor_assert(conn);
  3189. tor_assert(conn->base_.type == CONN_TYPE_DIR);
  3190. tor_assert(conn->base_.state == DIR_CONN_STATE_CONNECTING);
  3191. log_debug(LD_HTTP,"Dir connection to router %s:%u established.",
  3192. conn->base_.address,conn->base_.port);
  3193. conn->base_.state = DIR_CONN_STATE_CLIENT_SENDING; /* start flushing conn */
  3194. return 0;
  3195. }
  3196. /** Decide which download schedule we want to use based on descriptor type
  3197. * in <b>dls</b> and whether we are acting as directory <b>server</b>, and
  3198. * then return a list of int pointers defining download delays in seconds.
  3199. * Helper function for download_status_increment_failure() and
  3200. * download_status_reset(). */
  3201. static const smartlist_t *
  3202. find_dl_schedule_and_len(download_status_t *dls, int server)
  3203. {
  3204. switch (dls->schedule) {
  3205. case DL_SCHED_GENERIC:
  3206. if (server)
  3207. return get_options()->TestingServerDownloadSchedule;
  3208. else
  3209. return get_options()->TestingClientDownloadSchedule;
  3210. case DL_SCHED_CONSENSUS:
  3211. if (server)
  3212. return get_options()->TestingServerConsensusDownloadSchedule;
  3213. else
  3214. return get_options()->TestingClientConsensusDownloadSchedule;
  3215. case DL_SCHED_BRIDGE:
  3216. return get_options()->TestingBridgeDownloadSchedule;
  3217. default:
  3218. tor_assert(0);
  3219. }
  3220. /* Impossible, but gcc will fail with -Werror without a `return`. */
  3221. return NULL;
  3222. }
  3223. /** Called when an attempt to download <b>dls</b> has failed with HTTP status
  3224. * <b>status_code</b>. Increment the failure count (if the code indicates a
  3225. * real failure) and set <b>dls</b>-\>next_attempt_at to an appropriate time
  3226. * in the future. */
  3227. time_t
  3228. download_status_increment_failure(download_status_t *dls, int status_code,
  3229. const char *item, int server, time_t now)
  3230. {
  3231. const smartlist_t *schedule;
  3232. int increment;
  3233. tor_assert(dls);
  3234. if (status_code != 503 || server) {
  3235. if (dls->n_download_failures < IMPOSSIBLE_TO_DOWNLOAD-1)
  3236. ++dls->n_download_failures;
  3237. }
  3238. schedule = find_dl_schedule_and_len(dls, server);
  3239. if (dls->n_download_failures < smartlist_len(schedule))
  3240. increment = *(int *)smartlist_get(schedule, dls->n_download_failures);
  3241. else if (dls->n_download_failures == IMPOSSIBLE_TO_DOWNLOAD)
  3242. increment = INT_MAX;
  3243. else
  3244. increment = *(int *)smartlist_get(schedule, smartlist_len(schedule) - 1);
  3245. if (increment < INT_MAX)
  3246. dls->next_attempt_at = now+increment;
  3247. else
  3248. dls->next_attempt_at = TIME_MAX;
  3249. if (item) {
  3250. if (increment == 0)
  3251. log_debug(LD_DIR, "%s failed %d time(s); I'll try again immediately.",
  3252. item, (int)dls->n_download_failures);
  3253. else if (dls->next_attempt_at < TIME_MAX)
  3254. log_debug(LD_DIR, "%s failed %d time(s); I'll try again in %d seconds.",
  3255. item, (int)dls->n_download_failures,
  3256. (int)(dls->next_attempt_at-now));
  3257. else
  3258. log_debug(LD_DIR, "%s failed %d time(s); Giving up for a while.",
  3259. item, (int)dls->n_download_failures);
  3260. }
  3261. return dls->next_attempt_at;
  3262. }
  3263. /** Reset <b>dls</b> so that it will be considered downloadable
  3264. * immediately, and/or to show that we don't need it anymore.
  3265. *
  3266. * (We find the zeroth element of the download schedule, and set
  3267. * next_attempt_at to be the appropriate offset from 'now'. In most
  3268. * cases this means setting it to 'now', so the item will be immediately
  3269. * downloadable; in the case of bridge descriptors, the zeroth element
  3270. * is an hour from now.) */
  3271. void
  3272. download_status_reset(download_status_t *dls)
  3273. {
  3274. if (dls->n_download_failures == IMPOSSIBLE_TO_DOWNLOAD)
  3275. return; /* Don't reset this. */
  3276. const smartlist_t *schedule = find_dl_schedule_and_len(
  3277. dls, get_options()->DirPort_set);
  3278. dls->n_download_failures = 0;
  3279. dls->next_attempt_at = time(NULL) + *(int *)smartlist_get(schedule, 0);
  3280. }
  3281. /** Return the number of failures on <b>dls</b> since the last success (if
  3282. * any). */
  3283. int
  3284. download_status_get_n_failures(const download_status_t *dls)
  3285. {
  3286. return dls->n_download_failures;
  3287. }
  3288. /** Called when one or more routerdesc (or extrainfo, if <b>was_extrainfo</b>)
  3289. * fetches have failed (with uppercase fingerprints listed in <b>failed</b>,
  3290. * either as descriptor digests or as identity digests based on
  3291. * <b>was_descriptor_digests</b>).
  3292. */
  3293. static void
  3294. dir_routerdesc_download_failed(smartlist_t *failed, int status_code,
  3295. int router_purpose,
  3296. int was_extrainfo, int was_descriptor_digests)
  3297. {
  3298. char digest[DIGEST_LEN];
  3299. time_t now = time(NULL);
  3300. int server = directory_fetches_from_authorities(get_options());
  3301. if (!was_descriptor_digests) {
  3302. if (router_purpose == ROUTER_PURPOSE_BRIDGE) {
  3303. tor_assert(!was_extrainfo);
  3304. connection_dir_retry_bridges(failed);
  3305. }
  3306. return; /* FFFF should implement for other-than-router-purpose someday */
  3307. }
  3308. SMARTLIST_FOREACH_BEGIN(failed, const char *, cp) {
  3309. download_status_t *dls = NULL;
  3310. if (base16_decode(digest, DIGEST_LEN, cp, strlen(cp)) < 0) {
  3311. log_warn(LD_BUG, "Malformed fingerprint in list: %s", escaped(cp));
  3312. continue;
  3313. }
  3314. if (was_extrainfo) {
  3315. signed_descriptor_t *sd =
  3316. router_get_by_extrainfo_digest(digest);
  3317. if (sd)
  3318. dls = &sd->ei_dl_status;
  3319. } else {
  3320. dls = router_get_dl_status_by_descriptor_digest(digest);
  3321. }
  3322. if (!dls || dls->n_download_failures >=
  3323. get_options()->TestingDescriptorMaxDownloadTries)
  3324. continue;
  3325. download_status_increment_failure(dls, status_code, cp, server, now);
  3326. } SMARTLIST_FOREACH_END(cp);
  3327. /* No need to relaunch descriptor downloads here: we already do it
  3328. * every 10 or 60 seconds (FOO_DESCRIPTOR_RETRY_INTERVAL) in main.c. */
  3329. }
  3330. /** Called when a connection to download microdescriptors has failed in whole
  3331. * or in part. <b>failed</b> is a list of every microdesc digest we didn't
  3332. * get. <b>status_code</b> is the http status code we received. Reschedule the
  3333. * microdesc downloads as appropriate. */
  3334. static void
  3335. dir_microdesc_download_failed(smartlist_t *failed,
  3336. int status_code)
  3337. {
  3338. networkstatus_t *consensus
  3339. = networkstatus_get_latest_consensus_by_flavor(FLAV_MICRODESC);
  3340. routerstatus_t *rs;
  3341. download_status_t *dls;
  3342. time_t now = time(NULL);
  3343. int server = directory_fetches_from_authorities(get_options());
  3344. if (! consensus)
  3345. return;
  3346. SMARTLIST_FOREACH_BEGIN(failed, const char *, d) {
  3347. rs = router_get_mutable_consensus_status_by_descriptor_digest(consensus,d);
  3348. if (!rs)
  3349. continue;
  3350. dls = &rs->dl_status;
  3351. if (dls->n_download_failures >=
  3352. get_options()->TestingMicrodescMaxDownloadTries)
  3353. continue;
  3354. {
  3355. char buf[BASE64_DIGEST256_LEN+1];
  3356. digest256_to_base64(buf, d);
  3357. download_status_increment_failure(dls, status_code, buf,
  3358. server, now);
  3359. }
  3360. } SMARTLIST_FOREACH_END(d);
  3361. }
  3362. /** Helper. Compare two fp_pair_t objects, and return negative, 0, or
  3363. * positive as appropriate. */
  3364. static int
  3365. compare_pairs_(const void **a, const void **b)
  3366. {
  3367. const fp_pair_t *fp1 = *a, *fp2 = *b;
  3368. int r;
  3369. if ((r = fast_memcmp(fp1->first, fp2->first, DIGEST_LEN)))
  3370. return r;
  3371. else
  3372. return fast_memcmp(fp1->second, fp2->second, DIGEST_LEN);
  3373. }
  3374. /** Divide a string <b>res</b> of the form FP1-FP2+FP3-FP4...[.z], where each
  3375. * FP is a hex-encoded fingerprint, into a sequence of distinct sorted
  3376. * fp_pair_t. Skip malformed pairs. On success, return 0 and add those
  3377. * fp_pair_t into <b>pairs_out</b>. On failure, return -1. */
  3378. int
  3379. dir_split_resource_into_fingerprint_pairs(const char *res,
  3380. smartlist_t *pairs_out)
  3381. {
  3382. smartlist_t *pairs_tmp = smartlist_new();
  3383. smartlist_t *pairs_result = smartlist_new();
  3384. smartlist_split_string(pairs_tmp, res, "+", 0, 0);
  3385. if (smartlist_len(pairs_tmp)) {
  3386. char *last = smartlist_get(pairs_tmp,smartlist_len(pairs_tmp)-1);
  3387. size_t last_len = strlen(last);
  3388. if (last_len > 2 && !strcmp(last+last_len-2, ".z")) {
  3389. last[last_len-2] = '\0';
  3390. }
  3391. }
  3392. SMARTLIST_FOREACH_BEGIN(pairs_tmp, char *, cp) {
  3393. if (strlen(cp) != HEX_DIGEST_LEN*2+1) {
  3394. log_info(LD_DIR,
  3395. "Skipping digest pair %s with non-standard length.", escaped(cp));
  3396. } else if (cp[HEX_DIGEST_LEN] != '-') {
  3397. log_info(LD_DIR,
  3398. "Skipping digest pair %s with missing dash.", escaped(cp));
  3399. } else {
  3400. fp_pair_t pair;
  3401. if (base16_decode(pair.first, DIGEST_LEN, cp, HEX_DIGEST_LEN)<0 ||
  3402. base16_decode(pair.second,
  3403. DIGEST_LEN, cp+HEX_DIGEST_LEN+1, HEX_DIGEST_LEN)<0) {
  3404. log_info(LD_DIR, "Skipping non-decodable digest pair %s", escaped(cp));
  3405. } else {
  3406. smartlist_add(pairs_result, tor_memdup(&pair, sizeof(pair)));
  3407. }
  3408. }
  3409. tor_free(cp);
  3410. } SMARTLIST_FOREACH_END(cp);
  3411. smartlist_free(pairs_tmp);
  3412. /* Uniq-and-sort */
  3413. smartlist_sort(pairs_result, compare_pairs_);
  3414. smartlist_uniq(pairs_result, compare_pairs_, tor_free_);
  3415. smartlist_add_all(pairs_out, pairs_result);
  3416. smartlist_free(pairs_result);
  3417. return 0;
  3418. }
  3419. /** Given a directory <b>resource</b> request, containing zero
  3420. * or more strings separated by plus signs, followed optionally by ".z", store
  3421. * the strings, in order, into <b>fp_out</b>. If <b>compressed_out</b> is
  3422. * non-NULL, set it to 1 if the resource ends in ".z", else set it to 0.
  3423. *
  3424. * If (flags & DSR_HEX), then delete all elements that aren't hex digests, and
  3425. * decode the rest. If (flags & DSR_BASE64), then use "-" rather than "+" as
  3426. * a separator, delete all the elements that aren't base64-encoded digests,
  3427. * and decode the rest. If (flags & DSR_DIGEST256), these digests should be
  3428. * 256 bits long; else they should be 160.
  3429. *
  3430. * If (flags & DSR_SORT_UNIQ), then sort the list and remove all duplicates.
  3431. */
  3432. int
  3433. dir_split_resource_into_fingerprints(const char *resource,
  3434. smartlist_t *fp_out, int *compressed_out,
  3435. int flags)
  3436. {
  3437. const int decode_hex = flags & DSR_HEX;
  3438. const int decode_base64 = flags & DSR_BASE64;
  3439. const int digests_are_256 = flags & DSR_DIGEST256;
  3440. const int sort_uniq = flags & DSR_SORT_UNIQ;
  3441. const int digest_len = digests_are_256 ? DIGEST256_LEN : DIGEST_LEN;
  3442. const int hex_digest_len = digests_are_256 ?
  3443. HEX_DIGEST256_LEN : HEX_DIGEST_LEN;
  3444. const int base64_digest_len = digests_are_256 ?
  3445. BASE64_DIGEST256_LEN : BASE64_DIGEST_LEN;
  3446. smartlist_t *fp_tmp = smartlist_new();
  3447. tor_assert(!(decode_hex && decode_base64));
  3448. tor_assert(fp_out);
  3449. smartlist_split_string(fp_tmp, resource, decode_base64?"-":"+", 0, 0);
  3450. if (compressed_out)
  3451. *compressed_out = 0;
  3452. if (smartlist_len(fp_tmp)) {
  3453. char *last = smartlist_get(fp_tmp,smartlist_len(fp_tmp)-1);
  3454. size_t last_len = strlen(last);
  3455. if (last_len > 2 && !strcmp(last+last_len-2, ".z")) {
  3456. last[last_len-2] = '\0';
  3457. if (compressed_out)
  3458. *compressed_out = 1;
  3459. }
  3460. }
  3461. if (decode_hex || decode_base64) {
  3462. const size_t encoded_len = decode_hex ? hex_digest_len : base64_digest_len;
  3463. int i;
  3464. char *cp, *d = NULL;
  3465. for (i = 0; i < smartlist_len(fp_tmp); ++i) {
  3466. cp = smartlist_get(fp_tmp, i);
  3467. if (strlen(cp) != encoded_len) {
  3468. log_info(LD_DIR,
  3469. "Skipping digest %s with non-standard length.", escaped(cp));
  3470. smartlist_del_keeporder(fp_tmp, i--);
  3471. goto again;
  3472. }
  3473. d = tor_malloc_zero(digest_len);
  3474. if (decode_hex ?
  3475. (base16_decode(d, digest_len, cp, hex_digest_len)<0) :
  3476. (base64_decode(d, digest_len, cp, base64_digest_len)<0)) {
  3477. log_info(LD_DIR, "Skipping non-decodable digest %s", escaped(cp));
  3478. smartlist_del_keeporder(fp_tmp, i--);
  3479. goto again;
  3480. }
  3481. smartlist_set(fp_tmp, i, d);
  3482. d = NULL;
  3483. again:
  3484. tor_free(cp);
  3485. tor_free(d);
  3486. }
  3487. }
  3488. if (sort_uniq) {
  3489. if (decode_hex || decode_base64) {
  3490. if (digests_are_256) {
  3491. smartlist_sort_digests256(fp_tmp);
  3492. smartlist_uniq_digests256(fp_tmp);
  3493. } else {
  3494. smartlist_sort_digests(fp_tmp);
  3495. smartlist_uniq_digests(fp_tmp);
  3496. }
  3497. } else {
  3498. smartlist_sort_strings(fp_tmp);
  3499. smartlist_uniq_strings(fp_tmp);
  3500. }
  3501. }
  3502. smartlist_add_all(fp_out, fp_tmp);
  3503. smartlist_free(fp_tmp);
  3504. return 0;
  3505. }