directory.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588
  1. /* Copyright 2001-2004 Roger Dingledine.
  2. * Copyright 2004-2007 Roger Dingledine, Nick Mathewson. */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. const char directory_c_id[] =
  6. "$Id$";
  7. #include "or.h"
  8. /**
  9. * \file directory.c
  10. * \brief Code to send and fetch directories and router
  11. * descriptors via HTTP. Directories use dirserv.c to generate the
  12. * results; clients use routers.c to parse them.
  13. **/
  14. /* In-points to directory.c:
  15. *
  16. * - directory_post_to_dirservers(), called from
  17. * router_upload_dir_desc_to_dirservers() in router.c
  18. * upload_service_descriptor() in rendservice.c
  19. * - directory_get_from_dirserver(), called from
  20. * rend_client_refetch_renddesc() in rendclient.c
  21. * run_scheduled_events() in main.c
  22. * do_hup() in main.c
  23. * - connection_dir_process_inbuf(), called from
  24. * connection_process_inbuf() in connection.c
  25. * - connection_dir_finished_flushing(), called from
  26. * connection_finished_flushing() in connection.c
  27. * - connection_dir_finished_connecting(), called from
  28. * connection_finished_connecting() in connection.c
  29. */
  30. static void directory_send_command(dir_connection_t *conn,
  31. int purpose, int direct, const char *resource,
  32. const char *payload, size_t payload_len);
  33. static int directory_handle_command(dir_connection_t *conn);
  34. static int body_is_plausible(const char *body, size_t body_len, int purpose);
  35. static int purpose_needs_anonymity(uint8_t dir_purpose,
  36. uint8_t router_purpose);
  37. static char *http_get_header(const char *headers, const char *which);
  38. static void http_set_address_origin(const char *headers, connection_t *conn);
  39. static void connection_dir_download_networkstatus_failed(
  40. dir_connection_t *conn, int status_code);
  41. static void connection_dir_download_routerdesc_failed(dir_connection_t *conn);
  42. static void dir_networkstatus_download_failed(smartlist_t *failed,
  43. int status_code);
  44. static void dir_routerdesc_download_failed(smartlist_t *failed,
  45. int status_code,
  46. int was_extrainfo);
  47. static void note_request(const char *key, size_t bytes);
  48. /********* START VARIABLES **********/
  49. /** How far in the future do we allow a directory server to tell us it is
  50. * before deciding that one of us has the wrong time? */
  51. #define ALLOW_DIRECTORY_TIME_SKEW (30*60)
  52. #define X_ADDRESS_HEADER "X-Your-Address-Is: "
  53. /** HTTP cache control: how long do we tell proxies they can cache each
  54. * kind of document we serve? */
  55. #define FULL_DIR_CACHE_LIFETIME (60*60)
  56. #define RUNNINGROUTERS_CACHE_LIFETIME (20*60)
  57. #define NETWORKSTATUS_CACHE_LIFETIME (5*60)
  58. #define ROUTERDESC_CACHE_LIFETIME (30*60)
  59. #define ROUTERDESC_BY_DIGEST_CACHE_LIFETIME (48*60*60)
  60. #define ROBOTS_CACHE_LIFETIME (24*60*60)
  61. /********* END VARIABLES ************/
  62. /** Return true iff the directory purpose 'purpose' must use an
  63. * anonymous connection to a directory. */
  64. static int
  65. purpose_needs_anonymity(uint8_t dir_purpose, uint8_t router_purpose)
  66. {
  67. if (get_options()->AllDirActionsPrivate)
  68. return 1;
  69. if (router_purpose == ROUTER_PURPOSE_BRIDGE)
  70. return 1; /* if we have to ask, better make it anonymous */
  71. if (dir_purpose == DIR_PURPOSE_FETCH_DIR ||
  72. dir_purpose == DIR_PURPOSE_UPLOAD_DIR ||
  73. dir_purpose == DIR_PURPOSE_UPLOAD_VOTE ||
  74. dir_purpose == DIR_PURPOSE_UPLOAD_SIGNATURES ||
  75. dir_purpose == DIR_PURPOSE_FETCH_RUNNING_LIST ||
  76. dir_purpose == DIR_PURPOSE_FETCH_NETWORKSTATUS ||
  77. dir_purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
  78. dir_purpose == DIR_PURPOSE_FETCH_EXTRAINFO)
  79. return 0;
  80. return 1;
  81. }
  82. /** Return a newly allocated string describing <b>auth</b>. */
  83. char *
  84. authority_type_to_string(authority_type_t auth)
  85. {
  86. char *result;
  87. smartlist_t *lst = smartlist_create();
  88. if (auth & V1_AUTHORITY)
  89. smartlist_add(lst, (void*)"V1");
  90. if (auth & V2_AUTHORITY)
  91. smartlist_add(lst, (void*)"V2");
  92. if (auth & BRIDGE_AUTHORITY)
  93. smartlist_add(lst, (void*)"Bridge");
  94. if (auth & HIDSERV_AUTHORITY)
  95. smartlist_add(lst, (void*)"Hidden service");
  96. if (smartlist_len(lst)) {
  97. result = smartlist_join_strings(lst, ", ", 0, NULL);
  98. } else {
  99. result = tor_strdup("[Not an authority]");
  100. }
  101. smartlist_free(lst);
  102. return result;
  103. }
  104. /** Return true iff <b>identity_digest</b> is the digest of a router we
  105. * believe to support extrainfo downloads. (If <b>is_authority</b> we do
  106. * additional checking that's only valid for authorities.) */
  107. int
  108. router_supports_extrainfo(const char *identity_digest, int is_authority)
  109. {
  110. routerinfo_t *ri = router_get_by_digest(identity_digest);
  111. local_routerstatus_t *lrs;
  112. if (ri) {
  113. if (ri->caches_extra_info)
  114. return 1;
  115. if (is_authority && ri->platform &&
  116. tor_version_as_new_as(ri->platform, "Tor 0.2.0.0-alpha-dev (r10070)"))
  117. return 1;
  118. }
  119. if (is_authority) {
  120. lrs = router_get_combined_status_by_digest(identity_digest);
  121. if (lrs && lrs->status.version_supports_extrainfo_upload)
  122. return 1;
  123. }
  124. return 0;
  125. }
  126. /** Start a connection to every suitable directory authority, using
  127. * connection purpose 'purpose' and uploading the payload 'payload'
  128. * (length 'payload_len'). The purpose should be one of
  129. * 'DIR_PURPOSE_UPLOAD_DIR' or 'DIR_PURPOSE_UPLOAD_RENDDESC'.
  130. *
  131. * <b>type</b> specifies what sort of dir authorities (V1, V2,
  132. * HIDSERV, BRIDGE) we should upload to.
  133. *
  134. * If <b>extrainfo_len</b> is nonzero, the first <b>payload_len</b> bytes of
  135. * <b>payload</b> hold a router descriptor, and the next <b>extrainfo_len</b>
  136. * bytes of <b>payload</b> hold an extra-info document. Upload the descriptor
  137. * to all authorities, and the extra-info document to all authorities that
  138. * support it.
  139. */
  140. void
  141. directory_post_to_dirservers(uint8_t dir_purpose, uint8_t router_purpose,
  142. authority_type_t type,
  143. const char *payload,
  144. size_t payload_len, size_t extrainfo_len)
  145. {
  146. int post_via_tor;
  147. smartlist_t *dirservers = router_get_trusted_dir_servers();
  148. int found = 0;
  149. tor_assert(dirservers);
  150. /* This tries dirservers which we believe to be down, but ultimately, that's
  151. * harmless, and we may as well err on the side of getting things uploaded.
  152. */
  153. SMARTLIST_FOREACH(dirservers, trusted_dir_server_t *, ds,
  154. {
  155. routerstatus_t *rs = &(ds->fake_status.status);
  156. size_t upload_len = payload_len;
  157. if ((type & ds->type) == 0)
  158. continue;
  159. found = 1; /* at least one authority of this type was listed */
  160. if (dir_purpose == DIR_PURPOSE_UPLOAD_DIR)
  161. ds->has_accepted_serverdesc = 0;
  162. if (extrainfo_len && router_supports_extrainfo(ds->digest, 1)) {
  163. upload_len += extrainfo_len;
  164. log_info(LD_DIR, "Uploading an extrainfo (length %d)",
  165. (int) extrainfo_len);
  166. }
  167. post_via_tor = purpose_needs_anonymity(dir_purpose, router_purpose) ||
  168. !fascist_firewall_allows_address_dir(ds->addr, ds->dir_port);
  169. directory_initiate_command_routerstatus(rs, dir_purpose,
  170. router_purpose,
  171. post_via_tor,
  172. NULL, payload, upload_len);
  173. });
  174. if (!found) {
  175. char *s = authority_type_to_string(type);
  176. log_warn(LD_DIR, "Publishing server descriptor to directory authorities "
  177. "of type '%s', but no authorities of that type listed!", s);
  178. tor_free(s);
  179. }
  180. }
  181. /** Start a connection to a random running directory server, using
  182. * connection purpose <b>dir_purpose</b>, intending to fetch descriptors
  183. * of purpose <b>router_purpose</b>, and requesting <b>resource</b>.
  184. * If <b>retry_if_no_servers</b>, then if all the possible servers seem
  185. * down, mark them up and try again.
  186. */
  187. void
  188. directory_get_from_dirserver(uint8_t dir_purpose, uint8_t router_purpose,
  189. const char *resource, int retry_if_no_servers)
  190. {
  191. routerstatus_t *rs = NULL;
  192. or_options_t *options = get_options();
  193. int prefer_authority = server_mode(options) && options->DirPort != 0;
  194. int get_via_tor = purpose_needs_anonymity(dir_purpose, router_purpose);
  195. authority_type_t type;
  196. /* FFFF we could break this switch into its own function, and call
  197. * it elsewhere in directory.c. -RD */
  198. switch (dir_purpose) {
  199. case DIR_PURPOSE_FETCH_EXTRAINFO:
  200. type = EXTRAINFO_CACHE |
  201. (router_purpose == ROUTER_PURPOSE_BRIDGE ? BRIDGE_AUTHORITY :
  202. V2_AUTHORITY);
  203. break;
  204. case DIR_PURPOSE_FETCH_NETWORKSTATUS:
  205. case DIR_PURPOSE_FETCH_SERVERDESC:
  206. type = (router_purpose == ROUTER_PURPOSE_BRIDGE ? BRIDGE_AUTHORITY :
  207. V2_AUTHORITY);
  208. break;
  209. case DIR_PURPOSE_FETCH_DIR:
  210. case DIR_PURPOSE_FETCH_RUNNING_LIST:
  211. type = V1_AUTHORITY;
  212. break;
  213. case DIR_PURPOSE_FETCH_RENDDESC:
  214. type = HIDSERV_AUTHORITY;
  215. break;
  216. default:
  217. log_warn(LD_BUG, "Unexpected purpose %d", (int)dir_purpose);
  218. return;
  219. }
  220. if (!options->FetchServerDescriptors && type != HIDSERV_AUTHORITY)
  221. return;
  222. if (!get_via_tor && options->UseBridges) {
  223. /* want to ask a running bridge for which we have a descriptor. */
  224. routerinfo_t *ri = choose_random_entry(NULL);
  225. if (ri) {
  226. directory_initiate_command(ri->address, ri->addr,
  227. ri->or_port, 0,
  228. 1, ri->cache_info.identity_digest,
  229. dir_purpose,
  230. router_purpose,
  231. 0, resource, NULL, 0);
  232. } else
  233. log_notice(LD_DIR, "Ignoring directory request, since no bridge "
  234. "nodes are available yet.");
  235. return;
  236. } else if (!get_via_tor) {
  237. if (prefer_authority) {
  238. /* only ask authdirservers, and don't ask myself */
  239. rs = router_pick_trusteddirserver(type, 1, 1,
  240. retry_if_no_servers);
  241. }
  242. if (!rs) {
  243. /* anybody with a non-zero dirport will do */
  244. rs = router_pick_directory_server(1, 1, type,
  245. retry_if_no_servers);
  246. if (!rs) {
  247. const char *which;
  248. if (dir_purpose == DIR_PURPOSE_FETCH_DIR)
  249. which = "directory";
  250. else if (dir_purpose == DIR_PURPOSE_FETCH_RUNNING_LIST)
  251. which = "status list";
  252. else if (dir_purpose == DIR_PURPOSE_FETCH_NETWORKSTATUS)
  253. which = "network status";
  254. else // if (dir_purpose == DIR_PURPOSE_FETCH_NETWORKSTATUS)
  255. which = "server descriptors";
  256. log_info(LD_DIR,
  257. "No router found for %s; falling back to dirserver list",
  258. which);
  259. rs = router_pick_trusteddirserver(type, 1, 1,
  260. retry_if_no_servers);
  261. if (!rs)
  262. get_via_tor = 1; /* last resort: try routing it via Tor */
  263. }
  264. }
  265. } else { /* get_via_tor */
  266. /* Never use fascistfirewall; we're going via Tor. */
  267. if (dir_purpose == DIR_PURPOSE_FETCH_RENDDESC) {
  268. /* only ask hidserv authorities, any of them will do */
  269. rs = router_pick_trusteddirserver(HIDSERV_AUTHORITY, 0, 0,
  270. retry_if_no_servers);
  271. } else {
  272. /* anybody with a non-zero dirport will do. Disregard firewalls. */
  273. rs = router_pick_directory_server(1, 0, type,
  274. retry_if_no_servers);
  275. /* If we have any hope of building an indirect conn, we know some router
  276. * descriptors. If (rs==NULL), we can't build circuits anyway, so
  277. * there's no point in falling back to the authorities in this case. */
  278. }
  279. }
  280. if (rs)
  281. directory_initiate_command_routerstatus(rs, dir_purpose,
  282. router_purpose,
  283. get_via_tor,
  284. resource, NULL, 0);
  285. else {
  286. log_notice(LD_DIR,
  287. "While fetching directory info, "
  288. "no running dirservers known. Will try again later. "
  289. "(purpose %d)", dir_purpose);
  290. if (!purpose_needs_anonymity(dir_purpose, router_purpose)) {
  291. /* remember we tried them all and failed. */
  292. directory_all_unreachable(time(NULL));
  293. }
  294. }
  295. }
  296. /** Launch a new connection to the directory server <b>status</b> to
  297. * upload or download a server or rendezvous
  298. * descriptor. <b>dir_purpose</b> determines what
  299. * kind of directory connection we're launching, and must be one of
  300. * DIR_PURPOSE_{FETCH|UPLOAD}_{DIR|RENDDESC}. <b>router_purpose</b>
  301. * specifies the descriptor purposes we have in mind (currently only
  302. * used for FETCH_DIR).
  303. *
  304. * When uploading, <b>payload</b> and <b>payload_len</b> determine the content
  305. * of the HTTP post. Otherwise, <b>payload</b> should be NULL.
  306. *
  307. * When fetching a rendezvous descriptor, <b>resource</b> is the service ID we
  308. * want to fetch.
  309. */
  310. void
  311. directory_initiate_command_routerstatus(routerstatus_t *status,
  312. uint8_t dir_purpose,
  313. uint8_t router_purpose,
  314. int anonymized_connection,
  315. const char *resource,
  316. const char *payload,
  317. size_t payload_len)
  318. {
  319. routerinfo_t *router;
  320. char address_buf[INET_NTOA_BUF_LEN+1];
  321. struct in_addr in;
  322. const char *address;
  323. if ((router = router_get_by_digest(status->identity_digest))) {
  324. address = router->address;
  325. } else {
  326. in.s_addr = htonl(status->addr);
  327. tor_inet_ntoa(&in, address_buf, sizeof(address_buf));
  328. address = address_buf;
  329. }
  330. directory_initiate_command(address, status->addr,
  331. status->or_port, status->dir_port,
  332. status->version_supports_begindir,
  333. status->identity_digest,
  334. dir_purpose, router_purpose,
  335. anonymized_connection, resource,
  336. payload, payload_len);
  337. }
  338. /** Return true iff <b>conn</b> is the client side of a directory connection
  339. * we launched to ourself in order to determine the reachability of our
  340. * dir_port. */
  341. static int
  342. directory_conn_is_self_reachability_test(dir_connection_t *conn)
  343. {
  344. if (conn->requested_resource &&
  345. !strcmpstart(conn->requested_resource,"authority")) {
  346. routerinfo_t *me = router_get_my_routerinfo();
  347. if (me &&
  348. router_digest_is_me(conn->identity_digest) &&
  349. me->addr == conn->_base.addr &&
  350. me->dir_port == conn->_base.port)
  351. return 1;
  352. }
  353. return 0;
  354. }
  355. /** Called when we are unable to complete the client's request to a directory
  356. * server due to a network error: Mark the router as down and try again if
  357. * possible.
  358. */
  359. void
  360. connection_dir_request_failed(dir_connection_t *conn)
  361. {
  362. if (directory_conn_is_self_reachability_test(conn)) {
  363. routerinfo_t *me = router_get_my_routerinfo();
  364. if (me)
  365. control_event_server_status(LOG_WARN,
  366. "REACHABILITY_FAILED DIRADDRESS=%s:%d",
  367. me->address, me->dir_port);
  368. return; /* this was a test fetch. don't retry. */
  369. }
  370. router_set_status(conn->identity_digest, 0); /* don't try him again */
  371. if (conn->_base.purpose == DIR_PURPOSE_FETCH_DIR ||
  372. conn->_base.purpose == DIR_PURPOSE_FETCH_RUNNING_LIST) {
  373. log_info(LD_DIR, "Giving up on directory server at '%s:%d'; retrying",
  374. conn->_base.address, conn->_base.port);
  375. directory_get_from_dirserver(conn->_base.purpose, conn->router_purpose,
  376. NULL, 0 /* don't retry_if_no_servers */);
  377. } else if (conn->_base.purpose == DIR_PURPOSE_FETCH_NETWORKSTATUS) {
  378. log_info(LD_DIR, "Giving up on directory server at '%s'; retrying",
  379. conn->_base.address);
  380. connection_dir_download_networkstatus_failed(conn, -1);
  381. } else if (conn->_base.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
  382. conn->_base.purpose == DIR_PURPOSE_FETCH_EXTRAINFO) {
  383. log_info(LD_DIR, "Giving up on directory server at '%s'; retrying",
  384. conn->_base.address);
  385. connection_dir_download_routerdesc_failed(conn);
  386. }
  387. }
  388. /** Called when an attempt to download one or more network status
  389. * documents on connection <b>conn</b> failed. Decide whether to
  390. * retry the fetch now, later, or never.
  391. */
  392. static void
  393. connection_dir_download_networkstatus_failed(dir_connection_t *conn,
  394. int status_code)
  395. {
  396. if (!conn->requested_resource) {
  397. /* We never reached directory_send_command, which means that we never
  398. * opened a network connection. Either we're out of sockets, or the
  399. * network is down. Either way, retrying would be pointless. */
  400. return;
  401. }
  402. if (!strcmpstart(conn->requested_resource, "all")) {
  403. /* We're a non-authoritative directory cache; try again. Ignore status
  404. * code, since we don't want to keep trying forever in a tight loop
  405. * if all the authorities are shutting us out. */
  406. smartlist_t *trusted_dirs = router_get_trusted_dir_servers();
  407. SMARTLIST_FOREACH(trusted_dirs, trusted_dir_server_t *, ds,
  408. ++ds->n_networkstatus_failures);
  409. directory_get_from_dirserver(conn->_base.purpose, conn->router_purpose,
  410. "all.z", 0 /* don't retry_if_no_servers */);
  411. } else if (!strcmpstart(conn->requested_resource, "fp/")) {
  412. /* We were trying to download by fingerprint; mark them all as having
  413. * failed, and possibly retry them later.*/
  414. smartlist_t *failed = smartlist_create();
  415. dir_split_resource_into_fingerprints(conn->requested_resource+3,
  416. failed, NULL, 0, 0);
  417. if (smartlist_len(failed)) {
  418. dir_networkstatus_download_failed(failed, status_code);
  419. SMARTLIST_FOREACH(failed, char *, cp, tor_free(cp));
  420. }
  421. smartlist_free(failed);
  422. }
  423. }
  424. /** Called when an attempt to download one or more router descriptors
  425. * or extra-info documents on connection <b>conn</b> failed.
  426. */
  427. static void
  428. connection_dir_download_routerdesc_failed(dir_connection_t *conn)
  429. {
  430. /* No need to increment the failure count for routerdescs, since
  431. * it's not their fault. */
  432. /* No need to relaunch descriptor downloads here: we already do it
  433. * every 10 seconds (DESCRIPTOR_RETRY_INTERVAL) in main.c. */
  434. tor_assert(conn->_base.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
  435. conn->_base.purpose == DIR_PURPOSE_FETCH_EXTRAINFO);
  436. (void) conn;
  437. }
  438. /** Helper for directory_initiate_command_(router|trusted_dir): send the
  439. * command to a server whose address is <b>address</b>, whose IP is
  440. * <b>addr</b>, whose directory port is <b>dir_port</b>, whose tor version
  441. * <b>supports_begindir</b>, and whose identity key digest is
  442. * <b>digest</b>. */
  443. void
  444. directory_initiate_command(const char *address, uint32_t addr,
  445. uint16_t or_port, uint16_t dir_port,
  446. int supports_begindir, const char *digest,
  447. uint8_t dir_purpose, uint8_t router_purpose,
  448. int anonymized_connection, const char *resource,
  449. const char *payload, size_t payload_len)
  450. {
  451. dir_connection_t *conn;
  452. or_options_t *options = get_options();
  453. int want_to_tunnel = options->TunnelDirConns && supports_begindir &&
  454. !anonymized_connection && or_port &&
  455. fascist_firewall_allows_address_or(addr, or_port);
  456. tor_assert(address);
  457. tor_assert(addr);
  458. tor_assert(or_port || dir_port);
  459. tor_assert(digest);
  460. log_debug(LD_DIR, "anonymized %d, want_to_tunnel %d.",
  461. anonymized_connection, want_to_tunnel);
  462. switch (dir_purpose) {
  463. case DIR_PURPOSE_FETCH_DIR:
  464. log_debug(LD_DIR,"initiating directory fetch");
  465. break;
  466. case DIR_PURPOSE_FETCH_RENDDESC:
  467. log_debug(LD_DIR,"initiating hidden-service descriptor fetch");
  468. break;
  469. case DIR_PURPOSE_UPLOAD_DIR:
  470. log_debug(LD_OR,"initiating server descriptor upload");
  471. break;
  472. case DIR_PURPOSE_UPLOAD_RENDDESC:
  473. log_debug(LD_REND,"initiating hidden-service descriptor upload");
  474. break;
  475. case DIR_PURPOSE_UPLOAD_VOTE:
  476. log_debug(LD_OR,"initiating server vote upload");
  477. break;
  478. case DIR_PURPOSE_UPLOAD_SIGNATURES:
  479. log_debug(LD_OR,"initiating consensus signature upload");
  480. break;
  481. case DIR_PURPOSE_FETCH_RUNNING_LIST:
  482. log_debug(LD_DIR,"initiating running-routers fetch");
  483. break;
  484. case DIR_PURPOSE_FETCH_NETWORKSTATUS:
  485. log_debug(LD_DIR,"initiating network-status fetch");
  486. break;
  487. case DIR_PURPOSE_FETCH_SERVERDESC:
  488. log_debug(LD_DIR,"initiating server descriptor fetch");
  489. break;
  490. case DIR_PURPOSE_FETCH_EXTRAINFO:
  491. log_debug(LD_DIR,"initiating extra-info fetch");
  492. break;
  493. default:
  494. log_err(LD_BUG, "Unrecognized directory connection purpose.");
  495. tor_assert(0);
  496. }
  497. conn = TO_DIR_CONN(connection_new(CONN_TYPE_DIR, AF_INET));
  498. /* set up conn so it's got all the data we need to remember */
  499. conn->_base.addr = addr;
  500. conn->_base.port = want_to_tunnel ? or_port : dir_port;
  501. conn->_base.address = tor_strdup(address);
  502. memcpy(conn->identity_digest, digest, DIGEST_LEN);
  503. conn->_base.purpose = dir_purpose;
  504. conn->router_purpose = router_purpose;
  505. /* give it an initial state */
  506. conn->_base.state = DIR_CONN_STATE_CONNECTING;
  507. if (!anonymized_connection && !want_to_tunnel) {
  508. /* then we want to connect directly */
  509. conn->dirconn_direct = 1;
  510. if (options->HttpProxy) {
  511. addr = options->HttpProxyAddr;
  512. dir_port = options->HttpProxyPort;
  513. }
  514. switch (connection_connect(TO_CONN(conn), conn->_base.address, addr,
  515. dir_port)) {
  516. case -1:
  517. connection_dir_request_failed(conn); /* retry if we want */
  518. connection_free(TO_CONN(conn));
  519. return;
  520. case 1:
  521. /* start flushing conn */
  522. conn->_base.state = DIR_CONN_STATE_CLIENT_SENDING;
  523. /* fall through */
  524. case 0:
  525. /* queue the command on the outbuf */
  526. directory_send_command(conn, dir_purpose, 1, resource,
  527. payload, payload_len);
  528. connection_watch_events(TO_CONN(conn), EV_READ | EV_WRITE);
  529. /* writable indicates finish, readable indicates broken link,
  530. error indicates broken link in windowsland. */
  531. }
  532. } else { /* we want to connect via tor */
  533. edge_connection_t *linked_conn;
  534. /* make an AP connection
  535. * populate it and add it at the right state
  536. * hook up both sides
  537. */
  538. conn->dirconn_direct = 0;
  539. linked_conn =
  540. connection_ap_make_link(conn->_base.address, conn->_base.port,
  541. digest,
  542. anonymized_connection ?
  543. SOCKS_COMMAND_CONNECT :
  544. SOCKS_COMMAND_CONNECT_DIR);
  545. if (!linked_conn) {
  546. log_warn(LD_NET,"Making tunnel to dirserver failed.");
  547. connection_mark_for_close(TO_CONN(conn));
  548. return;
  549. }
  550. connection_link_connections(TO_CONN(conn), TO_CONN(linked_conn));
  551. if (connection_add(TO_CONN(conn)) < 0) {
  552. log_warn(LD_NET,"Unable to add connection for link to dirserver.");
  553. connection_mark_for_close(TO_CONN(conn));
  554. return;
  555. }
  556. conn->_base.state = DIR_CONN_STATE_CLIENT_SENDING;
  557. /* queue the command on the outbuf */
  558. directory_send_command(conn, dir_purpose, 0, resource,
  559. payload, payload_len);
  560. connection_watch_events(TO_CONN(conn), EV_READ | EV_WRITE);
  561. connection_start_reading(TO_CONN(linked_conn));
  562. }
  563. }
  564. /** Queue an appropriate HTTP command on conn-\>outbuf. The other args
  565. * are as in directory_initiate_command.
  566. */
  567. static void
  568. directory_send_command(dir_connection_t *conn,
  569. int purpose, int direct, const char *resource,
  570. const char *payload, size_t payload_len)
  571. {
  572. char proxystring[256];
  573. char proxyauthstring[256];
  574. char hoststring[128];
  575. char *url;
  576. char request[8192];
  577. const char *httpcommand = NULL;
  578. size_t len;
  579. tor_assert(conn);
  580. tor_assert(conn->_base.type == CONN_TYPE_DIR);
  581. tor_free(conn->requested_resource);
  582. if (resource)
  583. conn->requested_resource = tor_strdup(resource);
  584. /* come up with a string for which Host: we want */
  585. if (conn->_base.port == 80) {
  586. strlcpy(hoststring, conn->_base.address, sizeof(hoststring));
  587. } else {
  588. tor_snprintf(hoststring, sizeof(hoststring),"%s:%d",
  589. conn->_base.address, conn->_base.port);
  590. }
  591. /* come up with some proxy lines, if we're using one. */
  592. if (direct && get_options()->HttpProxy) {
  593. char *base64_authenticator=NULL;
  594. const char *authenticator = get_options()->HttpProxyAuthenticator;
  595. tor_snprintf(proxystring, sizeof(proxystring),"http://%s", hoststring);
  596. if (authenticator) {
  597. base64_authenticator = alloc_http_authenticator(authenticator);
  598. if (!base64_authenticator)
  599. log_warn(LD_BUG, "Encoding http authenticator failed");
  600. }
  601. if (base64_authenticator) {
  602. tor_snprintf(proxyauthstring, sizeof(proxyauthstring),
  603. "\r\nProxy-Authorization: Basic %s",
  604. base64_authenticator);
  605. tor_free(base64_authenticator);
  606. } else {
  607. proxyauthstring[0] = 0;
  608. }
  609. } else {
  610. proxystring[0] = 0;
  611. proxyauthstring[0] = 0;
  612. }
  613. switch (purpose) {
  614. case DIR_PURPOSE_FETCH_DIR:
  615. tor_assert(!resource);
  616. tor_assert(!payload);
  617. httpcommand = "GET";
  618. url = tor_strdup("/tor/dir.z");
  619. break;
  620. case DIR_PURPOSE_FETCH_RUNNING_LIST:
  621. tor_assert(!resource);
  622. tor_assert(!payload);
  623. httpcommand = "GET";
  624. url = tor_strdup("/tor/running-routers");
  625. break;
  626. case DIR_PURPOSE_FETCH_NETWORKSTATUS:
  627. httpcommand = "GET";
  628. len = strlen(resource)+32;
  629. url = tor_malloc(len);
  630. tor_snprintf(url, len, "/tor/status/%s", resource);
  631. break;
  632. case DIR_PURPOSE_FETCH_SERVERDESC:
  633. httpcommand = "GET";
  634. len = strlen(resource)+32;
  635. url = tor_malloc(len);
  636. tor_snprintf(url, len, "/tor/server/%s", resource);
  637. break;
  638. case DIR_PURPOSE_FETCH_EXTRAINFO:
  639. httpcommand = "GET";
  640. len = strlen(resource)+32;
  641. url = tor_malloc(len);
  642. tor_snprintf(url, len, "/tor/extra/%s", resource);
  643. break;
  644. case DIR_PURPOSE_UPLOAD_DIR:
  645. tor_assert(!resource);
  646. tor_assert(payload);
  647. httpcommand = "POST";
  648. url = tor_strdup("/tor/");
  649. break;
  650. case DIR_PURPOSE_UPLOAD_VOTE:
  651. tor_assert(!resource);
  652. tor_assert(payload);
  653. httpcommand = "POST";
  654. url = tor_strdup("/tor/post/vote");
  655. break;
  656. case DIR_PURPOSE_UPLOAD_SIGNATURES:
  657. tor_assert(!resource);
  658. tor_assert(payload);
  659. httpcommand = "POST";
  660. url = tor_strdup("/tor/post/consensus-signature");
  661. break;
  662. case DIR_PURPOSE_FETCH_RENDDESC:
  663. tor_assert(resource);
  664. tor_assert(!payload);
  665. /* this must be true or we wouldn't be doing the lookup */
  666. tor_assert(strlen(resource) <= REND_SERVICE_ID_LEN);
  667. /* This breaks the function abstraction. */
  668. strlcpy(conn->rend_query, resource, sizeof(conn->rend_query));
  669. httpcommand = "GET";
  670. /* Request the most recent versioned descriptor. */
  671. // (XXXX We were going to switch this to fetch rendezvous1 descriptors,
  672. // but that never got testing, and it wasn't a good design.)
  673. len = strlen(resource)+32;
  674. url = tor_malloc(len);
  675. tor_snprintf(url, len, "/tor/rendezvous/%s", resource);
  676. break;
  677. case DIR_PURPOSE_UPLOAD_RENDDESC:
  678. tor_assert(!resource);
  679. tor_assert(payload);
  680. httpcommand = "POST";
  681. url = tor_strdup("/tor/rendezvous/publish");
  682. break;
  683. default:
  684. tor_assert(0);
  685. return;
  686. }
  687. if (strlen(proxystring) + strlen(url) >= 4096) {
  688. log_warn(LD_BUG,
  689. "Squid does not like URLs longer than 4095 bytes, and this "
  690. "one is %d bytes long: %s%s",
  691. (int)(strlen(proxystring) + strlen(url)), proxystring, url);
  692. }
  693. tor_snprintf(request, sizeof(request), "%s %s", httpcommand, proxystring);
  694. connection_write_to_buf(request, strlen(request), TO_CONN(conn));
  695. connection_write_to_buf(url, strlen(url), TO_CONN(conn));
  696. tor_free(url);
  697. if (!strcmp(httpcommand, "GET") && !payload) {
  698. tor_snprintf(request, sizeof(request),
  699. " HTTP/1.0\r\nHost: %s%s\r\n\r\n",
  700. hoststring,
  701. proxyauthstring);
  702. } else {
  703. tor_snprintf(request, sizeof(request),
  704. " HTTP/1.0\r\nContent-Length: %lu\r\nHost: %s%s\r\n\r\n",
  705. payload ? (unsigned long)payload_len : 0,
  706. hoststring,
  707. proxyauthstring);
  708. }
  709. connection_write_to_buf(request, strlen(request), TO_CONN(conn));
  710. if (payload) {
  711. /* then send the payload afterwards too */
  712. connection_write_to_buf(payload, payload_len, TO_CONN(conn));
  713. }
  714. }
  715. /** Parse an HTTP request string <b>headers</b> of the form
  716. * \verbatim
  717. * "\%s [http[s]://]\%s HTTP/1..."
  718. * \endverbatim
  719. * If it's well-formed, strdup the second \%s into *<b>url</b>, and
  720. * nul-terminate it. If the url doesn't start with "/tor/", rewrite it
  721. * so it does. Return 0.
  722. * Otherwise, return -1.
  723. */
  724. static int
  725. parse_http_url(const char *headers, char **url)
  726. {
  727. char *s, *start, *tmp;
  728. s = (char *)eat_whitespace_no_nl(headers);
  729. if (!*s) return -1;
  730. s = (char *)find_whitespace(s); /* get past GET/POST */
  731. if (!*s) return -1;
  732. s = (char *)eat_whitespace_no_nl(s);
  733. if (!*s) return -1;
  734. start = s; /* this is it, assuming it's valid */
  735. s = (char *)find_whitespace(start);
  736. if (!*s) return -1;
  737. /* tolerate the http[s] proxy style of putting the hostname in the url */
  738. if (s-start >= 4 && !strcmpstart(start,"http")) {
  739. tmp = start + 4;
  740. if (*tmp == 's')
  741. tmp++;
  742. if (s-tmp >= 3 && !strcmpstart(tmp,"://")) {
  743. tmp = strchr(tmp+3, '/');
  744. if (tmp && tmp < s) {
  745. log_debug(LD_DIR,"Skipping over 'http[s]://hostname' string");
  746. start = tmp;
  747. }
  748. }
  749. }
  750. if (s-start < 5 || strcmpstart(start,"/tor/")) { /* need to rewrite it */
  751. *url = tor_malloc(s - start + 5);
  752. strlcpy(*url,"/tor", s-start+5);
  753. strlcat((*url)+4, start, s-start+1);
  754. } else {
  755. *url = tor_strndup(start, s-start);
  756. }
  757. return 0;
  758. }
  759. /** Return a copy of the first HTTP header in <b>headers</b> whose key is
  760. * <b>which</b>. The key should be given with a terminating colon and space;
  761. * this function copies everything after, up to but not including the
  762. * following \\r\\n. */
  763. static char *
  764. http_get_header(const char *headers, const char *which)
  765. {
  766. const char *cp = headers;
  767. while (cp) {
  768. if (!strcmpstart(cp, which)) {
  769. char *eos;
  770. cp += strlen(which);
  771. if ((eos = strchr(cp,'\r')))
  772. return tor_strndup(cp, eos-cp);
  773. else
  774. return tor_strdup(cp);
  775. }
  776. cp = strchr(cp, '\n');
  777. if (cp)
  778. ++cp;
  779. }
  780. return NULL;
  781. }
  782. /** If <b>headers</b> indicates that a proxy was involved, then rewrite
  783. * <b>conn</b>-\>address to describe our best guess of the address that
  784. * originated this HTTP request. */
  785. static void
  786. http_set_address_origin(const char *headers, connection_t *conn)
  787. {
  788. char *fwd;
  789. fwd = http_get_header(headers, "Forwarded-For: ");
  790. if (!fwd)
  791. fwd = http_get_header(headers, "X-Forwarded-For: ");
  792. if (fwd) {
  793. struct in_addr in;
  794. if (!tor_inet_aton(fwd, &in) || is_internal_IP(ntohl(in.s_addr), 0)) {
  795. log_debug(LD_DIR, "Ignoring unrecognized or internal IP %s",
  796. escaped(fwd));
  797. tor_free(fwd);
  798. return;
  799. }
  800. tor_free(conn->address);
  801. conn->address = tor_strdup(fwd);
  802. tor_free(fwd);
  803. }
  804. }
  805. /** Parse an HTTP response string <b>headers</b> of the form
  806. * \verbatim
  807. * "HTTP/1.\%d \%d\%s\r\n...".
  808. * \endverbatim
  809. *
  810. * If it's well-formed, assign the status code to *<b>code</b> and
  811. * return 0. Otherwise, return -1.
  812. *
  813. * On success: If <b>date</b> is provided, set *date to the Date
  814. * header in the http headers, or 0 if no such header is found. If
  815. * <b>compression</b> is provided, set *<b>compression</b> to the
  816. * compression method given in the Content-Encoding header, or 0 if no
  817. * such header is found, or -1 if the value of the header is not
  818. * recognized. If <b>reason</b> is provided, strdup the reason string
  819. * into it.
  820. */
  821. int
  822. parse_http_response(const char *headers, int *code, time_t *date,
  823. compress_method_t *compression, char **reason)
  824. {
  825. int n1, n2;
  826. char datestr[RFC1123_TIME_LEN+1];
  827. smartlist_t *parsed_headers;
  828. tor_assert(headers);
  829. tor_assert(code);
  830. while (TOR_ISSPACE(*headers)) headers++; /* tolerate leading whitespace */
  831. if (sscanf(headers, "HTTP/1.%d %d", &n1, &n2) < 2 ||
  832. (n1 != 0 && n1 != 1) ||
  833. (n2 < 100 || n2 >= 600)) {
  834. log_warn(LD_HTTP,"Failed to parse header %s",escaped(headers));
  835. return -1;
  836. }
  837. *code = n2;
  838. parsed_headers = smartlist_create();
  839. smartlist_split_string(parsed_headers, headers, "\n",
  840. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
  841. if (reason) {
  842. smartlist_t *status_line_elements = smartlist_create();
  843. tor_assert(smartlist_len(parsed_headers));
  844. smartlist_split_string(status_line_elements,
  845. smartlist_get(parsed_headers, 0),
  846. " ", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 3);
  847. tor_assert(smartlist_len(status_line_elements) <= 3);
  848. if (smartlist_len(status_line_elements) == 3) {
  849. *reason = smartlist_get(status_line_elements, 2);
  850. smartlist_set(status_line_elements, 2, NULL); /* Prevent free */
  851. }
  852. SMARTLIST_FOREACH(status_line_elements, char *, cp, tor_free(cp));
  853. smartlist_free(status_line_elements);
  854. }
  855. if (date) {
  856. *date = 0;
  857. SMARTLIST_FOREACH(parsed_headers, const char *, s,
  858. if (!strcmpstart(s, "Date: ")) {
  859. strlcpy(datestr, s+6, sizeof(datestr));
  860. /* This will do nothing on failure, so we don't need to check
  861. the result. We shouldn't warn, since there are many other valid
  862. date formats besides the one we use. */
  863. parse_rfc1123_time(datestr, date);
  864. break;
  865. });
  866. }
  867. if (compression) {
  868. const char *enc = NULL;
  869. SMARTLIST_FOREACH(parsed_headers, const char *, s,
  870. if (!strcmpstart(s, "Content-Encoding: ")) {
  871. enc = s+18; break;
  872. });
  873. if (!enc || !strcmp(enc, "identity")) {
  874. *compression = NO_METHOD;
  875. } else if (!strcmp(enc, "deflate") || !strcmp(enc, "x-deflate")) {
  876. *compression = ZLIB_METHOD;
  877. } else if (!strcmp(enc, "gzip") || !strcmp(enc, "x-gzip")) {
  878. *compression = GZIP_METHOD;
  879. } else {
  880. log_info(LD_HTTP, "Unrecognized content encoding: %s. Trying to deal.",
  881. escaped(enc));
  882. *compression = UNKNOWN_METHOD;
  883. }
  884. }
  885. SMARTLIST_FOREACH(parsed_headers, char *, s, tor_free(s));
  886. smartlist_free(parsed_headers);
  887. return 0;
  888. }
  889. /** Return true iff <b>body</b> doesn't start with a plausible router or
  890. * running-list or directory opening. This is a sign of possible compression.
  891. **/
  892. static int
  893. body_is_plausible(const char *body, size_t len, int purpose)
  894. {
  895. int i;
  896. if (len == 0)
  897. return 1; /* empty bodies don't need decompression */
  898. if (len < 32)
  899. return 0;
  900. if (purpose != DIR_PURPOSE_FETCH_RENDDESC) {
  901. if (!strcmpstart(body,"router") ||
  902. !strcmpstart(body,"signed-directory") ||
  903. !strcmpstart(body,"network-status") ||
  904. !strcmpstart(body,"running-routers"))
  905. return 1;
  906. for (i=0;i<32;++i) {
  907. if (!TOR_ISPRINT(body[i]) && !TOR_ISSPACE(body[i]))
  908. return 0;
  909. }
  910. return 1;
  911. } else {
  912. return 1;
  913. }
  914. }
  915. /** We are a client, and we've finished reading the server's
  916. * response. Parse and it and act appropriately.
  917. *
  918. * If we're still happy with using this directory server in the future, return
  919. * 0. Otherwise return -1; and the caller should consider trying the request
  920. * again.
  921. *
  922. * The caller will take care of marking the connection for close.
  923. */
  924. static int
  925. connection_dir_client_reached_eof(dir_connection_t *conn)
  926. {
  927. char *body;
  928. char *headers;
  929. char *reason = NULL;
  930. size_t body_len=0, orig_len=0;
  931. int status_code;
  932. time_t date_header=0;
  933. int delta;
  934. compress_method_t compression;
  935. int plausible;
  936. int skewed=0;
  937. int allow_partial = (conn->_base.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
  938. conn->_base.purpose == DIR_PURPOSE_FETCH_EXTRAINFO);
  939. int was_compressed=0;
  940. time_t now = time(NULL);
  941. switch (fetch_from_buf_http(conn->_base.inbuf,
  942. &headers, MAX_HEADERS_SIZE,
  943. &body, &body_len, MAX_DIR_DL_SIZE,
  944. allow_partial)) {
  945. case -1: /* overflow */
  946. log_warn(LD_PROTOCOL,
  947. "'fetch' response too large (server '%s:%d'). Closing.",
  948. conn->_base.address, conn->_base.port);
  949. return -1;
  950. case 0:
  951. log_info(LD_HTTP,
  952. "'fetch' response not all here, but we're at eof. Closing.");
  953. return -1;
  954. /* case 1, fall through */
  955. }
  956. orig_len = body_len;
  957. if (parse_http_response(headers, &status_code, &date_header,
  958. &compression, &reason) < 0) {
  959. log_warn(LD_HTTP,"Unparseable headers (server '%s:%d'). Closing.",
  960. conn->_base.address, conn->_base.port);
  961. tor_free(body); tor_free(headers);
  962. return -1;
  963. }
  964. if (!reason) reason = tor_strdup("[no reason given]");
  965. log_debug(LD_DIR,
  966. "Received response from directory server '%s:%d': %d %s",
  967. conn->_base.address, conn->_base.port, status_code,
  968. escaped(reason));
  969. /* now check if it's got any hints for us about our IP address. */
  970. if (conn->dirconn_direct) {
  971. char *guess = http_get_header(headers, X_ADDRESS_HEADER);
  972. if (guess) {
  973. router_new_address_suggestion(guess);
  974. tor_free(guess);
  975. }
  976. }
  977. if (date_header > 0) {
  978. /* The date header was written very soon after we sent our request,
  979. * so compute the skew as the difference between sending the request
  980. * and the date header. (We used to check now-date_header, but that's
  981. * inaccurate if we spend a lot of time downloading.)
  982. */
  983. delta = conn->_base.timestamp_lastwritten - date_header;
  984. if (abs(delta)>ALLOW_DIRECTORY_TIME_SKEW) {
  985. int trusted = router_digest_is_trusted_dir(conn->identity_digest);
  986. log_fn(trusted ? LOG_WARN : LOG_INFO,
  987. LD_HTTP,
  988. "Received directory with skewed time (server '%s:%d'): "
  989. "we are %d minutes %s, or the directory is %d minutes %s.",
  990. conn->_base.address, conn->_base.port,
  991. abs(delta)/60, delta>0 ? "ahead" : "behind",
  992. abs(delta)/60, delta>0 ? "behind" : "ahead");
  993. skewed = 1; /* don't check the recommended-versions line */
  994. control_event_general_status(trusted ? LOG_WARN : LOG_NOTICE,
  995. "CLOCK_SKEW SKEW=%d SOURCE=DIRSERV:%s:%d",
  996. delta, conn->_base.address, conn->_base.port);
  997. } else {
  998. log_debug(LD_HTTP, "Time on received directory is within tolerance; "
  999. "we are %d seconds skewed. (That's okay.)", delta);
  1000. }
  1001. }
  1002. (void) skewed; /* skewed isn't used yet. */
  1003. if (status_code == 503) {
  1004. local_routerstatus_t *rs;
  1005. trusted_dir_server_t *ds;
  1006. log_info(LD_DIR,"Received http status code %d (%s) from server "
  1007. "'%s:%d'. I'll try again soon.",
  1008. status_code, escaped(reason), conn->_base.address,
  1009. conn->_base.port);
  1010. if ((rs = router_get_combined_status_by_digest(conn->identity_digest)))
  1011. rs->last_dir_503_at = now;
  1012. if ((ds = router_get_trusteddirserver_by_digest(conn->identity_digest)))
  1013. ds->fake_status.last_dir_503_at = now;
  1014. tor_free(body); tor_free(headers); tor_free(reason);
  1015. return -1;
  1016. }
  1017. plausible = body_is_plausible(body, body_len, conn->_base.purpose);
  1018. if (compression != NO_METHOD || !plausible) {
  1019. char *new_body = NULL;
  1020. size_t new_len = 0;
  1021. compress_method_t guessed = detect_compression_method(body, body_len);
  1022. if (compression == UNKNOWN_METHOD || guessed != compression) {
  1023. /* Tell the user if we don't believe what we're told about compression.*/
  1024. const char *description1, *description2;
  1025. if (compression == ZLIB_METHOD)
  1026. description1 = "as deflated";
  1027. else if (compression == GZIP_METHOD)
  1028. description1 = "as gzipped";
  1029. else if (compression == NO_METHOD)
  1030. description1 = "as uncompressed";
  1031. else
  1032. description1 = "with an unknown Content-Encoding";
  1033. if (guessed == ZLIB_METHOD)
  1034. description2 = "deflated";
  1035. else if (guessed == GZIP_METHOD)
  1036. description2 = "gzipped";
  1037. else if (!plausible)
  1038. description2 = "confusing binary junk";
  1039. else
  1040. description2 = "uncompressed";
  1041. log_info(LD_HTTP, "HTTP body from server '%s:%d' was labeled %s, "
  1042. "but it seems to be %s.%s",
  1043. conn->_base.address, conn->_base.port, description1,
  1044. description2,
  1045. (compression>0 && guessed>0)?" Trying both.":"");
  1046. }
  1047. /* Try declared compression first if we can. */
  1048. if (compression == GZIP_METHOD || compression == ZLIB_METHOD)
  1049. tor_gzip_uncompress(&new_body, &new_len, body, body_len, compression,
  1050. !allow_partial, LOG_PROTOCOL_WARN);
  1051. /* Okay, if that didn't work, and we think that it was compressed
  1052. * differently, try that. */
  1053. if (!new_body &&
  1054. (guessed == GZIP_METHOD || guessed == ZLIB_METHOD) &&
  1055. compression != guessed)
  1056. tor_gzip_uncompress(&new_body, &new_len, body, body_len, guessed,
  1057. !allow_partial, LOG_PROTOCOL_WARN);
  1058. /* If we're pretty sure that we have a compressed directory, and
  1059. * we didn't manage to uncompress it, then warn and bail. */
  1060. if (!plausible && !new_body) {
  1061. log_fn(LOG_PROTOCOL_WARN, LD_HTTP,
  1062. "Unable to decompress HTTP body (server '%s:%d').",
  1063. conn->_base.address, conn->_base.port);
  1064. tor_free(body); tor_free(headers); tor_free(reason);
  1065. return -1;
  1066. }
  1067. if (new_body) {
  1068. tor_free(body);
  1069. body = new_body;
  1070. body_len = new_len;
  1071. was_compressed = 1;
  1072. }
  1073. }
  1074. if (conn->_base.purpose == DIR_PURPOSE_FETCH_DIR) {
  1075. /* fetch/process the directory to cache it. */
  1076. log_info(LD_DIR,"Received directory (size %d) from server '%s:%d'",
  1077. (int)body_len, conn->_base.address, conn->_base.port);
  1078. if (status_code != 200) {
  1079. log_warn(LD_DIR,"Received http status code %d (%s) from server "
  1080. "'%s:%d' while fetching directory. I'll try again soon.",
  1081. status_code, escaped(reason), conn->_base.address,
  1082. conn->_base.port);
  1083. tor_free(body); tor_free(headers); tor_free(reason);
  1084. return -1;
  1085. }
  1086. if (router_parse_directory(body) < 0) {
  1087. log_notice(LD_DIR,"I failed to parse the directory I fetched from "
  1088. "'%s:%d'. Ignoring.", conn->_base.address, conn->_base.port);
  1089. }
  1090. note_request(was_compressed?"dl/dir.z":"dl/dir", orig_len);
  1091. }
  1092. if (conn->_base.purpose == DIR_PURPOSE_FETCH_RUNNING_LIST) {
  1093. /* just update our list of running routers, if this list is new info */
  1094. log_info(LD_DIR,"Received running-routers list (size %d)", (int)body_len);
  1095. if (status_code != 200) {
  1096. log_warn(LD_DIR,"Received http status code %d (%s) from server "
  1097. "'%s:%d' while fetching running-routers. I'll try again soon.",
  1098. status_code, escaped(reason), conn->_base.address,
  1099. conn->_base.port);
  1100. tor_free(body); tor_free(headers); tor_free(reason);
  1101. return -1;
  1102. }
  1103. if (router_parse_runningrouters(body)<0) {
  1104. log_warn(LD_DIR,
  1105. "Bad running-routers from server '%s:%d'. I'll try again soon.",
  1106. conn->_base.address, conn->_base.port);
  1107. tor_free(body); tor_free(headers); tor_free(reason);
  1108. return -1;
  1109. }
  1110. note_request(was_compressed?"dl/running-routers.z":
  1111. "dl/running-routers", orig_len);
  1112. }
  1113. if (conn->_base.purpose == DIR_PURPOSE_FETCH_NETWORKSTATUS) {
  1114. smartlist_t *which = NULL;
  1115. networkstatus_source_t source;
  1116. char *cp;
  1117. log_info(LD_DIR,"Received networkstatus objects (size %d) from server "
  1118. "'%s:%d'",(int) body_len, conn->_base.address, conn->_base.port);
  1119. if (status_code != 200) {
  1120. /* XXXX020 This warning tends to freak out clients who get a 403. */
  1121. log_warn(LD_DIR,
  1122. "Received http status code %d (%s) from server "
  1123. "'%s:%d' while fetching \"/tor/status/%s\". I'll try again soon.",
  1124. status_code, escaped(reason), conn->_base.address,
  1125. conn->_base.port, conn->requested_resource);
  1126. tor_free(body); tor_free(headers); tor_free(reason);
  1127. connection_dir_download_networkstatus_failed(conn, status_code);
  1128. return -1;
  1129. }
  1130. note_request(was_compressed?"dl/status.z":"dl/status", orig_len);
  1131. if (conn->requested_resource &&
  1132. !strcmpstart(conn->requested_resource,"fp/")) {
  1133. source = NS_FROM_DIR_BY_FP;
  1134. which = smartlist_create();
  1135. dir_split_resource_into_fingerprints(conn->requested_resource+3,
  1136. which, NULL, 0, 0);
  1137. } else if (conn->requested_resource &&
  1138. !strcmpstart(conn->requested_resource, "all")) {
  1139. source = NS_FROM_DIR_ALL;
  1140. which = smartlist_create();
  1141. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  1142. trusted_dir_server_t *, ds,
  1143. {
  1144. char *hex = tor_malloc(HEX_DIGEST_LEN+1);
  1145. base16_encode(hex, HEX_DIGEST_LEN+1, ds->digest, DIGEST_LEN);
  1146. smartlist_add(which, hex);
  1147. });
  1148. } else {
  1149. /* Can we even end up here? -- weasel*/
  1150. source = NS_FROM_DIR_BY_FP;
  1151. log_warn(LD_BUG, "We received a networkstatus but we didn't ask "
  1152. "for it by fp, nor did we ask for all.");
  1153. }
  1154. cp = body;
  1155. while (*cp) {
  1156. char *next = strstr(cp, "\nnetwork-status-version");
  1157. if (next)
  1158. next[1] = '\0';
  1159. /* learn from it, and then remove it from 'which' */
  1160. if (router_set_networkstatus(cp, now, source, which)<0)
  1161. break;
  1162. if (next) {
  1163. next[1] = 'n';
  1164. cp = next+1;
  1165. } else
  1166. break;
  1167. }
  1168. routers_update_all_from_networkstatus(now); /*launches router downloads*/
  1169. directory_info_has_arrived(now, 0);
  1170. if (which) {
  1171. if (smartlist_len(which)) {
  1172. dir_networkstatus_download_failed(which, status_code);
  1173. }
  1174. SMARTLIST_FOREACH(which, char *, s, tor_free(s));
  1175. smartlist_free(which);
  1176. }
  1177. }
  1178. if (conn->_base.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
  1179. conn->_base.purpose == DIR_PURPOSE_FETCH_EXTRAINFO) {
  1180. int was_ei = conn->_base.purpose == DIR_PURPOSE_FETCH_EXTRAINFO;
  1181. smartlist_t *which = NULL;
  1182. int n_asked_for = 0;
  1183. log_info(LD_DIR,"Received %s (size %d) from server '%s:%d'",
  1184. was_ei ? "extra server info" : "server info",
  1185. (int)body_len, conn->_base.address, conn->_base.port);
  1186. if (was_ei)
  1187. note_request(was_compressed?"dl/extra.z":"dl/extra", orig_len);
  1188. else
  1189. note_request(was_compressed?"dl/server.z":"dl/server", orig_len);
  1190. if (conn->requested_resource &&
  1191. !strcmpstart(conn->requested_resource,"d/")) {
  1192. which = smartlist_create();
  1193. dir_split_resource_into_fingerprints(conn->requested_resource+2,
  1194. which, NULL, 0, 0);
  1195. n_asked_for = smartlist_len(which);
  1196. }
  1197. if (status_code != 200) {
  1198. int dir_okay = status_code == 404 ||
  1199. (status_code == 400 && !strcmp(reason, "Servers unavailable."));
  1200. /* 404 means that it didn't have them; no big deal.
  1201. * Older (pre-0.1.1.8) servers said 400 Servers unavailable instead. */
  1202. /* XXXX020 This warning tends to freak out clients who get a 403. */
  1203. log_fn(dir_okay ? LOG_INFO : LOG_WARN, LD_DIR,
  1204. "Received http status code %d (%s) from server '%s:%d' "
  1205. "while fetching \"/tor/server/%s\". I'll try again soon.",
  1206. status_code, escaped(reason), conn->_base.address,
  1207. conn->_base.port, conn->requested_resource);
  1208. if (!which) {
  1209. connection_dir_download_routerdesc_failed(conn);
  1210. } else {
  1211. dir_routerdesc_download_failed(which, status_code, was_ei);
  1212. SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
  1213. smartlist_free(which);
  1214. }
  1215. tor_free(body); tor_free(headers); tor_free(reason);
  1216. return dir_okay ? 0 : -1;
  1217. }
  1218. /* Learn the routers, assuming we requested by fingerprint or "all"
  1219. * or "authority".
  1220. *
  1221. * We use "authority" to fetch our own descriptor for
  1222. * testing, and to fetch bridge descriptors for bootstrapping. Ignore
  1223. * the output of "authority" requests unless we are using bridges,
  1224. * since otherwise they'll be the response from reachability tests,
  1225. * and we don't really want to add that to our routerlist. */
  1226. if (which || (conn->requested_resource &&
  1227. (!strcmpstart(conn->requested_resource, "all") ||
  1228. (!strcmpstart(conn->requested_resource, "authority") &&
  1229. get_options()->UseBridges)))) {
  1230. /* as we learn from them, we remove them from 'which' */
  1231. if (was_ei) {
  1232. router_load_extrainfo_from_string(body, NULL, SAVED_NOWHERE, which);
  1233. } else {
  1234. router_load_routers_from_string(body, NULL, SAVED_NOWHERE, which,
  1235. conn->router_purpose);
  1236. directory_info_has_arrived(now, 0);
  1237. }
  1238. }
  1239. if (which) { /* mark remaining ones as failed */
  1240. log_info(LD_DIR, "Received %d/%d routers requested from %s:%d",
  1241. n_asked_for-smartlist_len(which), n_asked_for,
  1242. conn->_base.address, (int)conn->_base.port);
  1243. if (smartlist_len(which)) {
  1244. dir_routerdesc_download_failed(which, status_code, was_ei);
  1245. }
  1246. SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
  1247. smartlist_free(which);
  1248. }
  1249. if (directory_conn_is_self_reachability_test(conn))
  1250. router_dirport_found_reachable();
  1251. }
  1252. if (conn->_base.purpose == DIR_PURPOSE_UPLOAD_DIR) {
  1253. switch (status_code) {
  1254. case 200: {
  1255. int all_done = 1;
  1256. trusted_dir_server_t *ds =
  1257. router_get_trusteddirserver_by_digest(conn->identity_digest);
  1258. smartlist_t *servers;
  1259. log_info(LD_GENERAL,"eof (status 200) after uploading server "
  1260. "descriptor: finished.");
  1261. control_event_server_status(
  1262. LOG_NOTICE, "ACCEPTED_SERVER_DESCRIPTOR DIRAUTH=%s:%d",
  1263. conn->_base.address, conn->_base.port);
  1264. ds->has_accepted_serverdesc = 1;
  1265. servers = router_get_trusted_dir_servers();
  1266. SMARTLIST_FOREACH(servers, trusted_dir_server_t *, d, {
  1267. if ((d->type & (V1_AUTHORITY|V2_AUTHORITY)) &&
  1268. !d->has_accepted_serverdesc) {
  1269. all_done = 0;
  1270. break;
  1271. }
  1272. });
  1273. if (all_done)
  1274. control_event_server_status(LOG_NOTICE, "GOOD_SERVER_DESCRIPTOR");
  1275. }
  1276. break;
  1277. case 400:
  1278. log_warn(LD_GENERAL,"http status 400 (%s) response from "
  1279. "dirserver '%s:%d'. Please correct.",
  1280. escaped(reason), conn->_base.address, conn->_base.port);
  1281. control_event_server_status(LOG_WARN,
  1282. "BAD_SERVER_DESCRIPTOR DIRAUTH=%s:%d REASON=\"%s\"",
  1283. conn->_base.address, conn->_base.port, escaped(reason));
  1284. break;
  1285. case 403:
  1286. log_warn(LD_GENERAL,
  1287. "http status 403 (%s) response from dirserver "
  1288. "'%s:%d'. Is your clock skewed? Have you mailed us your key "
  1289. "fingerprint? Are you using the right key? Are you using a "
  1290. "private IP address? See http://tor.eff.org/doc/"
  1291. "tor-doc-server.html",escaped(reason), conn->_base.address,
  1292. conn->_base.port);
  1293. control_event_server_status(LOG_WARN,
  1294. "BAD_SERVER_DESCRIPTOR DIRAUTH=%s:%d REASON=\"%s\"",
  1295. conn->_base.address, conn->_base.port, escaped(reason));
  1296. break;
  1297. default:
  1298. log_warn(LD_GENERAL,
  1299. "http status %d (%s) reason unexpected while uploading "
  1300. "descriptor to server '%s:%d').",
  1301. status_code, escaped(reason), conn->_base.address,
  1302. conn->_base.port);
  1303. break;
  1304. }
  1305. /* return 0 in all cases, since we don't want to mark any
  1306. * dirservers down just because they don't like us. */
  1307. }
  1308. if (conn->_base.purpose == DIR_PURPOSE_UPLOAD_VOTE) {
  1309. switch (status_code) {
  1310. case 200: {
  1311. log_notice(LD_DIR,"Uploaded a vote to dirserver %s:%d",
  1312. conn->_base.address, conn->_base.port);
  1313. }
  1314. break;
  1315. case 400:
  1316. log_warn(LD_DIR,"http status 400 (%s) response after uploading "
  1317. "vote to dirserver '%s:%d'. Please correct.",
  1318. escaped(reason), conn->_base.address, conn->_base.port);
  1319. break;
  1320. default:
  1321. log_warn(LD_GENERAL,
  1322. "http status %d (%s) reason unexpected while uploading "
  1323. "vote to server '%s:%d').",
  1324. status_code, escaped(reason), conn->_base.address,
  1325. conn->_base.port);
  1326. break;
  1327. }
  1328. /* return 0 in all cases, since we don't want to mark any
  1329. * dirservers down just because they don't like us. */
  1330. }
  1331. if (conn->_base.purpose == DIR_PURPOSE_UPLOAD_SIGNATURES) {
  1332. switch (status_code) {
  1333. case 200: {
  1334. log_notice(LD_DIR,"Uploaded signature(s) to dirserver %s:%d",
  1335. conn->_base.address, conn->_base.port);
  1336. }
  1337. break;
  1338. case 400:
  1339. log_warn(LD_DIR,"http status 400 (%s) response after uploading "
  1340. "signatures to dirserver '%s:%d'. Please correct.",
  1341. escaped(reason), conn->_base.address, conn->_base.port);
  1342. break;
  1343. default:
  1344. log_warn(LD_GENERAL,
  1345. "http status %d (%s) reason unexpected while uploading "
  1346. "signatures to server '%s:%d').",
  1347. status_code, escaped(reason), conn->_base.address,
  1348. conn->_base.port);
  1349. break;
  1350. }
  1351. /* return 0 in all cases, since we don't want to mark any
  1352. * dirservers down just because they don't like us. */
  1353. }
  1354. if (conn->_base.purpose == DIR_PURPOSE_FETCH_RENDDESC) {
  1355. log_info(LD_REND,"Received rendezvous descriptor (size %d, status %d "
  1356. "(%s))",
  1357. (int)body_len, status_code, escaped(reason));
  1358. switch (status_code) {
  1359. case 200:
  1360. if (rend_cache_store(body, body_len, 0) < 0) {
  1361. log_warn(LD_REND,"Failed to store rendezvous descriptor.");
  1362. /* alice's ap_stream will notice when connection_mark_for_close
  1363. * cleans it up */
  1364. } else {
  1365. /* success. notify pending connections about this. */
  1366. conn->_base.purpose = DIR_PURPOSE_HAS_FETCHED_RENDDESC;
  1367. rend_client_desc_here(conn->rend_query);
  1368. }
  1369. break;
  1370. case 404:
  1371. /* not there. pending connections will be notified when
  1372. * connection_mark_for_close cleans it up. */
  1373. break;
  1374. case 400:
  1375. log_warn(LD_REND,
  1376. "http status 400 (%s). Dirserver didn't like our "
  1377. "rendezvous query?", escaped(reason));
  1378. break;
  1379. default:
  1380. log_warn(LD_REND,"http status %d (%s) response unexpected while "
  1381. "fetching hidden service descriptor (server '%s:%d').",
  1382. status_code, escaped(reason), conn->_base.address,
  1383. conn->_base.port);
  1384. break;
  1385. }
  1386. }
  1387. if (conn->_base.purpose == DIR_PURPOSE_UPLOAD_RENDDESC) {
  1388. log_info(LD_REND,"Uploaded rendezvous descriptor (status %d (%s))",
  1389. status_code, escaped(reason));
  1390. switch (status_code) {
  1391. case 200:
  1392. log_info(LD_REND,
  1393. "Uploading rendezvous descriptor: finished with status "
  1394. "200 (%s)", escaped(reason));
  1395. break;
  1396. case 400:
  1397. log_warn(LD_REND,"http status 400 (%s) response from dirserver "
  1398. "'%s:%d'. Malformed rendezvous descriptor?",
  1399. escaped(reason), conn->_base.address, conn->_base.port);
  1400. break;
  1401. default:
  1402. log_warn(LD_REND,"http status %d (%s) response unexpected (server "
  1403. "'%s:%d').",
  1404. status_code, escaped(reason), conn->_base.address,
  1405. conn->_base.port);
  1406. break;
  1407. }
  1408. }
  1409. tor_free(body); tor_free(headers); tor_free(reason);
  1410. return 0;
  1411. }
  1412. /** Called when a directory connection reaches EOF */
  1413. int
  1414. connection_dir_reached_eof(dir_connection_t *conn)
  1415. {
  1416. int retval;
  1417. if (conn->_base.state != DIR_CONN_STATE_CLIENT_READING) {
  1418. log_info(LD_HTTP,"conn reached eof, not reading. [state=%d] Closing.",
  1419. conn->_base.state);
  1420. connection_close_immediate(TO_CONN(conn)); /* error: give up on flushing */
  1421. connection_mark_for_close(TO_CONN(conn));
  1422. return -1;
  1423. }
  1424. retval = connection_dir_client_reached_eof(conn);
  1425. if (retval == 0) /* success */
  1426. conn->_base.state = DIR_CONN_STATE_CLIENT_FINISHED;
  1427. connection_mark_for_close(TO_CONN(conn));
  1428. return retval;
  1429. }
  1430. /** If any directory object is arriving, and it's over 10MB large, we're
  1431. * getting DoS'd. (As of 0.1.2.x, raw directories are about 1MB, and we never
  1432. * ask for more than 96 router descriptors at a time.)
  1433. */
  1434. #define MAX_DIRECTORY_OBJECT_SIZE (10*(1<<20))
  1435. /** Read handler for directory connections. (That's connections <em>to</em>
  1436. * directory servers and connections <em>at</em> directory servers.)
  1437. */
  1438. int
  1439. connection_dir_process_inbuf(dir_connection_t *conn)
  1440. {
  1441. tor_assert(conn);
  1442. tor_assert(conn->_base.type == CONN_TYPE_DIR);
  1443. /* Directory clients write, then read data until they receive EOF;
  1444. * directory servers read data until they get an HTTP command, then
  1445. * write their response (when it's finished flushing, they mark for
  1446. * close).
  1447. */
  1448. /* If we're on the dirserver side, look for a command. */
  1449. if (conn->_base.state == DIR_CONN_STATE_SERVER_COMMAND_WAIT) {
  1450. if (directory_handle_command(conn) < 0) {
  1451. connection_mark_for_close(TO_CONN(conn));
  1452. return -1;
  1453. }
  1454. return 0;
  1455. }
  1456. if (buf_datalen(conn->_base.inbuf) > MAX_DIRECTORY_OBJECT_SIZE) {
  1457. log_warn(LD_HTTP, "Too much data received from directory connection: "
  1458. "denial of service attempt, or you need to upgrade?");
  1459. connection_mark_for_close(TO_CONN(conn));
  1460. return -1;
  1461. }
  1462. if (!conn->_base.inbuf_reached_eof)
  1463. log_debug(LD_HTTP,"Got data, not eof. Leaving on inbuf.");
  1464. return 0;
  1465. }
  1466. /** Create an http response for the client <b>conn</b> out of
  1467. * <b>status</b> and <b>reason_phrase</b>. Write it to <b>conn</b>.
  1468. */
  1469. static void
  1470. write_http_status_line(dir_connection_t *conn, int status,
  1471. const char *reason_phrase)
  1472. {
  1473. char buf[256];
  1474. if (tor_snprintf(buf, sizeof(buf), "HTTP/1.0 %d %s\r\n\r\n",
  1475. status, reason_phrase ? reason_phrase : "OK") < 0) {
  1476. log_warn(LD_BUG,"status line too long.");
  1477. return;
  1478. }
  1479. connection_write_to_buf(buf, strlen(buf), TO_CONN(conn));
  1480. }
  1481. /** Write the header for an HTTP/1.0 response onto <b>conn</b>-\>outbuf,
  1482. * with <b>type</b> as the Content-Type.
  1483. *
  1484. * If <b>length</b> is nonnegative, it is the Content-Length.
  1485. * If <b>encoding</b> is provided, it is the Content-Encoding.
  1486. * If <b>cache_lifetime</b> is greater than 0, the content may be cached for
  1487. * up to cache_lifetime seconds. Otherwise, the content may not be cached. */
  1488. static void
  1489. write_http_response_header_impl(dir_connection_t *conn, ssize_t length,
  1490. const char *type, const char *encoding,
  1491. int cache_lifetime)
  1492. {
  1493. char date[RFC1123_TIME_LEN+1];
  1494. char tmp[1024];
  1495. char *cp;
  1496. time_t now = time(NULL);
  1497. tor_assert(conn);
  1498. tor_assert(type);
  1499. format_rfc1123_time(date, now);
  1500. cp = tmp;
  1501. tor_snprintf(cp, sizeof(tmp),
  1502. "HTTP/1.0 200 OK\r\nDate: %s\r\nContent-Type: %s\r\n",
  1503. date, type);
  1504. cp += strlen(tmp);
  1505. if (!is_internal_IP(conn->_base.addr, 0)) {
  1506. /* Don't report the source address for a localhost/private connection. */
  1507. tor_snprintf(cp, sizeof(tmp)-(cp-tmp),
  1508. X_ADDRESS_HEADER "%s\r\n", conn->_base.address);
  1509. cp += strlen(cp);
  1510. }
  1511. if (encoding) {
  1512. tor_snprintf(cp, sizeof(tmp)-(cp-tmp),
  1513. "Content-Encoding: %s\r\n", encoding);
  1514. cp += strlen(cp);
  1515. }
  1516. if (length >= 0) {
  1517. tor_snprintf(cp, sizeof(tmp)-(cp-tmp),
  1518. "Content-Length: %ld\r\n", (long)length);
  1519. cp += strlen(cp);
  1520. }
  1521. if (cache_lifetime > 0) {
  1522. char expbuf[RFC1123_TIME_LEN+1];
  1523. format_rfc1123_time(expbuf, now + cache_lifetime);
  1524. /* We could say 'Cache-control: max-age=%d' here if we start doing
  1525. * http/1.1 */
  1526. tor_snprintf(cp, sizeof(tmp)-(cp-tmp),
  1527. "Expires: %s\r\n", expbuf);
  1528. cp += strlen(cp);
  1529. } else {
  1530. /* We could say 'Cache-control: no-cache' here if we start doing
  1531. * http/1.1 */
  1532. strlcpy(cp, "Pragma: no-cache\r\n", sizeof(tmp)-(cp-tmp));
  1533. cp += strlen(cp);
  1534. }
  1535. if (sizeof(tmp)-(cp-tmp) > 3)
  1536. memcpy(cp, "\r\n", 3);
  1537. else
  1538. tor_assert(0);
  1539. connection_write_to_buf(tmp, strlen(tmp), TO_CONN(conn));
  1540. }
  1541. /** As write_http_response_header_impl, but sets encoding and content-typed
  1542. * based on whether the response will be <b>deflated</b> or not. */
  1543. static void
  1544. write_http_response_header(dir_connection_t *conn, ssize_t length,
  1545. int deflated, int cache_lifetime)
  1546. {
  1547. write_http_response_header_impl(conn, length,
  1548. deflated?"application/octet-stream":"text/plain",
  1549. deflated?"deflate":"identity",
  1550. cache_lifetime);
  1551. }
  1552. /** Helper function: return 1 if there are any dir conns of purpose
  1553. * <b>purpose</b> that are going elsewhere than our own ORPort/Dirport.
  1554. * Else return 0.
  1555. */
  1556. static int
  1557. already_fetching_directory(int purpose)
  1558. {
  1559. smartlist_t *conns = get_connection_array();
  1560. SMARTLIST_FOREACH(conns, connection_t *, conn,
  1561. {
  1562. if (conn->type == CONN_TYPE_DIR &&
  1563. conn->purpose == purpose &&
  1564. !conn->marked_for_close &&
  1565. !router_digest_is_me(TO_DIR_CONN(conn)->identity_digest))
  1566. return 1;
  1567. });
  1568. return 0;
  1569. }
  1570. #ifdef INSTRUMENT_DOWNLOADS
  1571. /** Map used to keep track of how much data we've up/downloaded in what kind
  1572. * of request. Maps from request type to pointer to uint64_t. */
  1573. static strmap_t *request_bytes_map = NULL;
  1574. /** Called when we just transmitted or received <b>bytes</b> worth of data
  1575. * because of a request of type <b>key</b> (an arbitrary identifier): adds
  1576. * <b>bytes</b> to the total associated with key. */
  1577. static void
  1578. note_request(const char *key, size_t bytes)
  1579. {
  1580. uint64_t *n;
  1581. if (!request_bytes_map)
  1582. request_bytes_map = strmap_new();
  1583. n = strmap_get(request_bytes_map, key);
  1584. if (!n) {
  1585. n = tor_malloc_zero(sizeof(uint64_t));
  1586. strmap_set(request_bytes_map, key, n);
  1587. }
  1588. *n += bytes;
  1589. }
  1590. /** Return a newly allocated string holding a summary of bytes used per
  1591. * request type. */
  1592. char *
  1593. directory_dump_request_log(void)
  1594. {
  1595. smartlist_t *lines;
  1596. char tmp[256];
  1597. char *result;
  1598. strmap_iter_t *iter;
  1599. if (!request_bytes_map)
  1600. request_bytes_map = strmap_new();
  1601. lines = smartlist_create();
  1602. for (iter = strmap_iter_init(request_bytes_map);
  1603. !strmap_iter_done(iter);
  1604. iter = strmap_iter_next(request_bytes_map, iter)) {
  1605. const char *key;
  1606. void *val;
  1607. uint64_t *n;
  1608. strmap_iter_get(iter, &key, &val);
  1609. n = val;
  1610. tor_snprintf(tmp, sizeof(tmp), "%s "U64_FORMAT"\n",
  1611. key, U64_PRINTF_ARG(*n));
  1612. smartlist_add(lines, tor_strdup(tmp));
  1613. }
  1614. smartlist_sort_strings(lines);
  1615. result = smartlist_join_strings(lines, "", 0, NULL);
  1616. SMARTLIST_FOREACH(lines, char *, cp, tor_free(cp));
  1617. smartlist_free(lines);
  1618. return result;
  1619. }
  1620. #else
  1621. static void
  1622. note_request(const char *key, size_t bytes)
  1623. {
  1624. (void)key;
  1625. (void)bytes;
  1626. }
  1627. char *
  1628. directory_dump_request_log(void)
  1629. {
  1630. return tor_strdup("Not supported.");
  1631. }
  1632. #endif
  1633. /** Helper function: called when a dirserver gets a complete HTTP GET
  1634. * request. Look for a request for a directory or for a rendezvous
  1635. * service descriptor. On finding one, write a response into
  1636. * conn-\>outbuf. If the request is unrecognized, send a 400.
  1637. * Always return 0. */
  1638. static int
  1639. directory_handle_command_get(dir_connection_t *conn, const char *headers,
  1640. const char *body, size_t body_len)
  1641. {
  1642. size_t dlen;
  1643. char *url, *url_mem, *header;
  1644. or_options_t *options = get_options();
  1645. time_t if_modified_since = 0;
  1646. int deflated = 0;
  1647. size_t url_len;
  1648. /* We ignore the body of a GET request. */
  1649. (void)body;
  1650. (void)body_len;
  1651. log_debug(LD_DIRSERV,"Received GET command.");
  1652. conn->_base.state = DIR_CONN_STATE_SERVER_WRITING;
  1653. if (parse_http_url(headers, &url) < 0) {
  1654. write_http_status_line(conn, 400, "Bad request");
  1655. return 0;
  1656. }
  1657. if ((header = http_get_header(headers, "If-Modified-Since: "))) {
  1658. struct tm tm;
  1659. if (parse_http_time(header, &tm) == 0) {
  1660. if_modified_since = tor_timegm(&tm);
  1661. }
  1662. /* The correct behavior on a malformed If-Modified-Since header is to
  1663. * act as if no If-Modified-Since header had been given. */
  1664. tor_free(header);
  1665. }
  1666. log_debug(LD_DIRSERV,"rewritten url as '%s'.", url);
  1667. url_mem = url;
  1668. url_len = strlen(url);
  1669. deflated = url_len > 2 && !strcmp(url+url_len-2, ".z");
  1670. if (deflated) {
  1671. url[url_len-2] = '\0';
  1672. url_len -= 2;
  1673. }
  1674. if (!strcmp(url,"/tor/") || !strcmp(url,"/tor/dir")) { /* dir fetch */
  1675. cached_dir_t *d = dirserv_get_directory();
  1676. if (!d) {
  1677. log_notice(LD_DIRSERV,"Client asked for the mirrored directory, but we "
  1678. "don't have a good one yet. Sending 503 Dir not available.");
  1679. write_http_status_line(conn, 503, "Directory unavailable");
  1680. /* try to get a new one now */
  1681. if (!already_fetching_directory(DIR_PURPOSE_FETCH_DIR) &&
  1682. !should_delay_dir_fetches(options))
  1683. directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR,
  1684. ROUTER_PURPOSE_GENERAL, NULL, 1);
  1685. goto done;
  1686. }
  1687. if (d->published < if_modified_since) {
  1688. write_http_status_line(conn, 304, "Not modified");
  1689. goto done;
  1690. }
  1691. dlen = deflated ? d->dir_z_len : d->dir_len;
  1692. if (global_write_bucket_low(TO_CONN(conn), dlen, 1)) {
  1693. log_info(LD_DIRSERV,
  1694. "Client asked for the mirrored directory, but we've been "
  1695. "writing too many bytes lately. Sending 503 Dir busy.");
  1696. write_http_status_line(conn, 503, "Directory busy, try again later");
  1697. goto done;
  1698. }
  1699. note_request(url, dlen);
  1700. log_debug(LD_DIRSERV,"Dumping %sdirectory to client.",
  1701. deflated?"deflated ":"");
  1702. write_http_response_header(conn, dlen, deflated,
  1703. FULL_DIR_CACHE_LIFETIME);
  1704. conn->cached_dir = d;
  1705. conn->cached_dir_offset = 0;
  1706. if (! deflated)
  1707. conn->zlib_state = tor_zlib_new(0, ZLIB_METHOD);
  1708. ++d->refcnt;
  1709. /* Prime the connection with some data. */
  1710. conn->dir_spool_src = DIR_SPOOL_CACHED_DIR;
  1711. connection_dirserv_flushed_some(conn);
  1712. goto done;
  1713. }
  1714. if (!strcmp(url,"/tor/running-routers")) { /* running-routers fetch */
  1715. cached_dir_t *d = dirserv_get_runningrouters();
  1716. if (!d) {
  1717. write_http_status_line(conn, 503, "Directory unavailable");
  1718. /* try to get a new one now */
  1719. if (!already_fetching_directory(DIR_PURPOSE_FETCH_RUNNING_LIST) &&
  1720. !should_delay_dir_fetches(options))
  1721. directory_get_from_dirserver(DIR_PURPOSE_FETCH_RUNNING_LIST,
  1722. ROUTER_PURPOSE_GENERAL, NULL, 1);
  1723. goto done;
  1724. }
  1725. if (d->published < if_modified_since) {
  1726. write_http_status_line(conn, 304, "Not modified");
  1727. goto done;
  1728. }
  1729. dlen = deflated ? d->dir_z_len : d->dir_len;
  1730. if (global_write_bucket_low(TO_CONN(conn), dlen, 1)) {
  1731. log_info(LD_DIRSERV,
  1732. "Client asked for running-routers, but we've been "
  1733. "writing too many bytes lately. Sending 503 Dir busy.");
  1734. write_http_status_line(conn, 503, "Directory busy, try again later");
  1735. goto done;
  1736. }
  1737. note_request(url, dlen);
  1738. write_http_response_header(conn, dlen, deflated,
  1739. RUNNINGROUTERS_CACHE_LIFETIME);
  1740. connection_write_to_buf(deflated ? d->dir_z : d->dir, dlen, TO_CONN(conn));
  1741. goto done;
  1742. }
  1743. if (!strcmpstart(url,"/tor/status/")
  1744. || !strcmp(url, "/tor/status-vote/current/consensus")) {
  1745. /* v2 or v3 network status fetch. */
  1746. smartlist_t *dir_fps = smartlist_create();
  1747. int is_v3 = !strcmpstart(url, "/tor/status-vote");
  1748. const char *request_type = NULL;
  1749. const char *key = url + strlen("/tor/status/");
  1750. if (!is_v3) {
  1751. dirserv_get_networkstatus_v2_fingerprints(dir_fps, key);
  1752. if (!strcmpstart(key, "fp/"))
  1753. request_type = deflated?"/tor/status/fp.z":"/tor/status/fp";
  1754. else if (!strcmpstart(key, "authority"))
  1755. request_type = deflated?"/tor/status/authority.z":
  1756. "/tor/status/authority";
  1757. else if (!strcmpstart(key, "all"))
  1758. request_type = deflated?"/tor/status/all.z":"/tor/status/all";
  1759. else
  1760. request_type = "/tor/status/?";
  1761. } else {
  1762. smartlist_add(dir_fps, tor_memdup("\0\0\0\0\0\0\0\0\0\0"
  1763. "\0\0\0\0\0\0\0\0\0\0", 20));
  1764. request_type = deflated?"v3.z":"v3";
  1765. }
  1766. if (!smartlist_len(dir_fps)) { /* we failed to create/cache cp */
  1767. write_http_status_line(conn, 503, "Network status object unavailable");
  1768. smartlist_free(dir_fps);
  1769. goto done;
  1770. }
  1771. if (!dirserv_remove_old_statuses(dir_fps, if_modified_since)) {
  1772. write_http_status_line(conn, 404, "Not found");
  1773. SMARTLIST_FOREACH(dir_fps, char *, cp, tor_free(cp));
  1774. smartlist_free(dir_fps);
  1775. goto done;
  1776. } else if (!smartlist_len(dir_fps)) {
  1777. write_http_status_line(conn, 304, "Not modified");
  1778. SMARTLIST_FOREACH(dir_fps, char *, cp, tor_free(cp));
  1779. smartlist_free(dir_fps);
  1780. goto done;
  1781. }
  1782. dlen = dirserv_estimate_data_size(dir_fps, 0, deflated);
  1783. if (global_write_bucket_low(TO_CONN(conn), dlen, 2)) {
  1784. log_info(LD_DIRSERV,
  1785. "Client asked for network status lists, but we've been "
  1786. "writing too many bytes lately. Sending 503 Dir busy.");
  1787. write_http_status_line(conn, 503, "Directory busy, try again later");
  1788. SMARTLIST_FOREACH(dir_fps, char *, fp, tor_free(fp));
  1789. smartlist_free(dir_fps);
  1790. goto done;
  1791. }
  1792. // note_request(request_type,dlen);
  1793. (void) request_type;
  1794. write_http_response_header(conn, -1, deflated,
  1795. smartlist_len(dir_fps) == 1 ? NETWORKSTATUS_CACHE_LIFETIME:0);
  1796. conn->fingerprint_stack = dir_fps;
  1797. if (! deflated)
  1798. conn->zlib_state = tor_zlib_new(0, ZLIB_METHOD);
  1799. /* Prime the connection with some data. */
  1800. conn->dir_spool_src = DIR_SPOOL_NETWORKSTATUS;
  1801. connection_dirserv_flushed_some(conn);
  1802. goto done;
  1803. }
  1804. if (!strcmpstart(url,"/tor/status-vote/current/") ||
  1805. !strcmpstart(url,"/tor/status-vote/next/")) {
  1806. /*XXXX020 implement if-modified-since and 503-rate-limiting */
  1807. int current = 1;
  1808. ssize_t body_len = 0;
  1809. smartlist_t *items = smartlist_create();
  1810. smartlist_t *dir_items = smartlist_create();
  1811. int lifetime = 60; /* XXXX020 should actually use vote intervals. */
  1812. url += strlen("/tor/status-vote/");
  1813. current = !strcmpstart(url, "current/");
  1814. url = strchr(url, '/');
  1815. tor_assert(url);
  1816. ++url;
  1817. if (!strcmp(url, "consensus")) {
  1818. const char *item;
  1819. tor_assert(!current); /* we handle current consensus specially above,
  1820. * since it wants to be spooled. */
  1821. if ((item = dirvote_get_pending_consensus()))
  1822. smartlist_add(items, (char*)item);
  1823. } else if (current && !strcmp(url, "consensus-signatures")) {
  1824. /* XXXX020 the spec says that we should implement
  1825. * currrent/consensus-signatures too. Why? */
  1826. const char *item;
  1827. if ((item=dirvote_get_pending_detached_signatures()))
  1828. smartlist_add(items, (char*)item);
  1829. } else if (current && !strcmp(url, "authority")) {
  1830. const cached_dir_t *d;
  1831. if ((d=dirvote_get_vote(NULL)))
  1832. smartlist_add(dir_items, (cached_dir_t*)d);
  1833. } else if (current) {
  1834. const cached_dir_t *d;
  1835. smartlist_t *fps = smartlist_create();
  1836. dir_split_resource_into_fingerprints(url, fps, NULL, 1, 1);
  1837. SMARTLIST_FOREACH(fps, char *, fp, {
  1838. if ((d = dirvote_get_vote(fp)))
  1839. smartlist_add(dir_items, (cached_dir_t*)d);
  1840. tor_free(fp);
  1841. });
  1842. smartlist_free(fps);
  1843. }
  1844. if (!smartlist_len(dir_items) && !smartlist_len(items)) {
  1845. write_http_status_line(conn, 404, "Not found");
  1846. goto done;
  1847. }
  1848. SMARTLIST_FOREACH(items, const char *, item,
  1849. if (!deflated)
  1850. body_len += strlen(item));
  1851. SMARTLIST_FOREACH(dir_items, cached_dir_t *, d,
  1852. body_len += deflated ? d->dir_z_len : d->dir_len);
  1853. write_http_response_header(conn, body_len ? body_len : -1, deflated,
  1854. lifetime);
  1855. if (smartlist_len(items)) {
  1856. if (deflated) {
  1857. conn->zlib_state = tor_zlib_new(1, ZLIB_METHOD);
  1858. SMARTLIST_FOREACH(items, const char *, c,
  1859. connection_write_to_buf_zlib(c, strlen(c), conn, 0));
  1860. connection_write_to_buf_zlib("", 0, conn, 1);
  1861. } else {
  1862. SMARTLIST_FOREACH(items, const char *, c,
  1863. connection_write_to_buf(c, strlen(c), TO_CONN(conn)));
  1864. }
  1865. } else {
  1866. SMARTLIST_FOREACH(dir_items, cached_dir_t *, d,
  1867. connection_write_to_buf(deflated ? d->dir_z : d->dir,
  1868. deflated ? d->dir_z_len : d->dir_len,
  1869. TO_CONN(conn)));
  1870. }
  1871. goto done;
  1872. }
  1873. if (!strcmpstart(url,"/tor/server/") ||
  1874. !strcmpstart(url,"/tor/extra/")) {
  1875. int res;
  1876. const char *msg;
  1877. const char *request_type = NULL;
  1878. int cache_lifetime = 0;
  1879. int is_extra = !strcmpstart(url,"/tor/extra/");
  1880. url += is_extra ? strlen("/tor/extra/") : strlen("/tor/server/");
  1881. conn->fingerprint_stack = smartlist_create();
  1882. res = dirserv_get_routerdesc_fingerprints(conn->fingerprint_stack, url,
  1883. &msg);
  1884. if (!strcmpstart(url, "fp/")) {
  1885. request_type = deflated?"/tor/server/fp.z":"/tor/server/fp";
  1886. if (smartlist_len(conn->fingerprint_stack) == 1)
  1887. cache_lifetime = ROUTERDESC_CACHE_LIFETIME;
  1888. } else if (!strcmpstart(url, "authority")) {
  1889. request_type = deflated?"/tor/server/authority.z":
  1890. "/tor/server/authority";
  1891. cache_lifetime = ROUTERDESC_CACHE_LIFETIME;
  1892. } else if (!strcmpstart(url, "all")) {
  1893. request_type = deflated?"/tor/server/all.z":"/tor/server/all";
  1894. cache_lifetime = FULL_DIR_CACHE_LIFETIME;
  1895. } else if (!strcmpstart(url, "d/")) {
  1896. request_type = deflated?"/tor/server/d.z":"/tor/server/d";
  1897. if (smartlist_len(conn->fingerprint_stack) == 1)
  1898. cache_lifetime = ROUTERDESC_BY_DIGEST_CACHE_LIFETIME;
  1899. } else {
  1900. request_type = "/tor/server/?";
  1901. }
  1902. (void) request_type; /* usable for note_request. */
  1903. if (!strcmpstart(url, "d/"))
  1904. conn->dir_spool_src =
  1905. is_extra ? DIR_SPOOL_EXTRA_BY_DIGEST : DIR_SPOOL_SERVER_BY_DIGEST;
  1906. else
  1907. conn->dir_spool_src =
  1908. is_extra ? DIR_SPOOL_EXTRA_BY_FP : DIR_SPOOL_SERVER_BY_FP;
  1909. if (!dirserv_have_any_serverdesc(conn->fingerprint_stack,
  1910. conn->dir_spool_src)) {
  1911. res = -1;
  1912. msg = "Not found";
  1913. }
  1914. if (res < 0)
  1915. write_http_status_line(conn, 404, msg);
  1916. else {
  1917. dlen = dirserv_estimate_data_size(conn->fingerprint_stack,
  1918. 1, deflated);
  1919. if (global_write_bucket_low(TO_CONN(conn), dlen, 2)) {
  1920. log_info(LD_DIRSERV,
  1921. "Client asked for server descriptors, but we've been "
  1922. "writing too many bytes lately. Sending 503 Dir busy.");
  1923. write_http_status_line(conn, 503, "Directory busy, try again later");
  1924. goto done;
  1925. }
  1926. write_http_response_header(conn, -1, deflated, cache_lifetime);
  1927. if (deflated)
  1928. conn->zlib_state = tor_zlib_new(1, ZLIB_METHOD);
  1929. /* Prime the connection with some data. */
  1930. connection_dirserv_flushed_some(conn);
  1931. }
  1932. goto done;
  1933. }
  1934. if (!strcmpstart(url,"/tor/keys/")) {
  1935. smartlist_t *certs = smartlist_create();
  1936. ssize_t len = -1;
  1937. if (!strcmp(url, "/tor/keys/all")) {
  1938. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  1939. trusted_dir_server_t *, ds,
  1940. {
  1941. if (!ds->v3_certs)
  1942. continue;
  1943. SMARTLIST_FOREACH(ds->v3_certs, authority_cert_t *, cert,
  1944. if (cert->cache_info.published_on >= if_modified_since)
  1945. smartlist_add(certs, cert));
  1946. });
  1947. } else if (!strcmp(url, "/tor/keys/authority")) {
  1948. authority_cert_t *cert = get_my_v3_authority_cert();
  1949. if (cert)
  1950. smartlist_add(certs, cert);
  1951. } else if (!strcmpstart(url, "/tor/keys/fp/")) {
  1952. smartlist_t *fps = smartlist_create();
  1953. dir_split_resource_into_fingerprints(url+strlen("/tor/keys/fp/"),
  1954. fps, NULL, 1, 1);
  1955. SMARTLIST_FOREACH(fps, char *, d, {
  1956. authority_cert_t *c = authority_cert_get_newest_by_id(d);
  1957. if (c) smartlist_add(certs, c);
  1958. tor_free(d);
  1959. });
  1960. smartlist_free(fps);
  1961. } else if (!strcmpstart(url, "/tor/keys/sk/")) {
  1962. smartlist_t *fps = smartlist_create();
  1963. dir_split_resource_into_fingerprints(url+strlen("/tor/keys/sk/"),
  1964. fps, NULL, 1, 1);
  1965. SMARTLIST_FOREACH(fps, char *, d, {
  1966. authority_cert_t *c = authority_cert_get_by_sk_digest(d);
  1967. if (c) smartlist_add(certs, c);
  1968. tor_free(d);
  1969. });
  1970. smartlist_free(fps);
  1971. } else {
  1972. write_http_status_line(conn, 400, "Bad request");
  1973. smartlist_free(certs);
  1974. goto done;
  1975. }
  1976. if (!smartlist_len(certs)) {
  1977. write_http_status_line(conn, 404, "Not found");
  1978. smartlist_free(certs);
  1979. goto done;
  1980. }
  1981. if (!deflated) {
  1982. len = 0;
  1983. SMARTLIST_FOREACH(certs, authority_cert_t *, c,
  1984. len += c->cache_info.signed_descriptor_len);
  1985. }
  1986. write_http_response_header(conn, len, deflated, 60*60);
  1987. if (deflated) {
  1988. conn->zlib_state = tor_zlib_new(1, ZLIB_METHOD);
  1989. SMARTLIST_FOREACH(certs, authority_cert_t *, c,
  1990. connection_write_to_buf_zlib(c->cache_info.signed_descriptor_body,
  1991. c->cache_info.signed_descriptor_len,
  1992. conn, 0));
  1993. connection_write_to_buf_zlib("", 0, conn, 1);
  1994. } else {
  1995. SMARTLIST_FOREACH(certs, authority_cert_t *, c,
  1996. connection_write_to_buf(c->cache_info.signed_descriptor_body,
  1997. c->cache_info.signed_descriptor_len,
  1998. TO_CONN(conn)));
  1999. }
  2000. smartlist_free(certs);
  2001. goto done;
  2002. }
  2003. if (options->HSAuthoritativeDir &&
  2004. (!strcmpstart(url,"/tor/rendezvous/") ||
  2005. !strcmpstart(url,"/tor/rendezvous1/"))) {
  2006. /* rendezvous descriptor fetch */
  2007. const char *descp;
  2008. size_t desc_len;
  2009. int versioned = !strcmpstart(url,"/tor/rendezvous1/");
  2010. const char *query = url+strlen("/tor/rendezvous/")+(versioned?1:0);
  2011. log_info(LD_REND, "Handling rendezvous descriptor get");
  2012. switch (rend_cache_lookup_desc(query, versioned?-1:0, &descp, &desc_len)) {
  2013. case 1: /* valid */
  2014. write_http_response_header_impl(conn, desc_len,
  2015. "application/octet-stream",
  2016. NULL, 0);
  2017. note_request("/tor/rendezvous?/", desc_len);
  2018. /* need to send descp separately, because it may include nuls */
  2019. connection_write_to_buf(descp, desc_len, TO_CONN(conn));
  2020. /* report successful fetch to statistic */
  2021. if (options->HSAuthorityRecordStats) {
  2022. hs_usage_note_fetch_total(query, time(NULL));
  2023. hs_usage_note_fetch_successful(query, time(NULL));
  2024. }
  2025. break;
  2026. case 0: /* well-formed but not present */
  2027. write_http_status_line(conn, 404, "Not found");
  2028. /* report (unsuccessful) fetch to statistic */
  2029. if (options->HSAuthorityRecordStats) {
  2030. hs_usage_note_fetch_total(query, time(NULL));
  2031. }
  2032. break;
  2033. case -1: /* not well-formed */
  2034. write_http_status_line(conn, 400, "Bad request");
  2035. break;
  2036. }
  2037. goto done;
  2038. }
  2039. if (!strcmpstart(url,"/tor/bytes.txt")) {
  2040. char *bytes = directory_dump_request_log();
  2041. size_t len = strlen(bytes);
  2042. write_http_response_header(conn, len, 0, 0);
  2043. connection_write_to_buf(bytes, len, TO_CONN(conn));
  2044. tor_free(bytes);
  2045. goto done;
  2046. }
  2047. if (!strcmp(url,"/tor/robots.txt")) { /* /robots.txt will have been
  2048. rewritten to /tor/robots.txt */
  2049. char robots[] = "User-agent: *\r\nDisallow: /\r\n";
  2050. size_t len = strlen(robots);
  2051. write_http_response_header(conn, len, 0, ROBOTS_CACHE_LIFETIME);
  2052. connection_write_to_buf(robots, len, TO_CONN(conn));
  2053. goto done;
  2054. }
  2055. if (!strcmp(url,"/tor/dir-all-weaselhack") &&
  2056. (conn->_base.addr == 0x7f000001ul) &&
  2057. authdir_mode_v2(options) &&
  2058. !authdir_mode_bridge(options)) {
  2059. /* until weasel rewrites his scripts at noreply */
  2060. char *new_directory=NULL;
  2061. if (dirserv_dump_directory_to_string(&new_directory,
  2062. get_identity_key(), 1)) {
  2063. log_warn(LD_BUG, "Error creating full v1 directory.");
  2064. tor_free(new_directory);
  2065. write_http_status_line(conn, 503, "Directory unavailable");
  2066. goto done;
  2067. }
  2068. dlen = strlen(new_directory);
  2069. write_http_response_header(conn, dlen, 0, 0);
  2070. connection_write_to_buf(new_directory, dlen, TO_CONN(conn));
  2071. tor_free(new_directory);
  2072. goto done;
  2073. }
  2074. /* we didn't recognize the url */
  2075. write_http_status_line(conn, 404, "Not found");
  2076. done:
  2077. tor_free(url_mem);
  2078. return 0;
  2079. }
  2080. /** Helper function: called when a dirserver gets a complete HTTP POST
  2081. * request. Look for an uploaded server descriptor or rendezvous
  2082. * service descriptor. On finding one, process it and write a
  2083. * response into conn-\>outbuf. If the request is unrecognized, send a
  2084. * 400. Always return 0. */
  2085. static int
  2086. directory_handle_command_post(dir_connection_t *conn, const char *headers,
  2087. const char *body, size_t body_len)
  2088. {
  2089. char *url = NULL;
  2090. or_options_t *options = get_options();
  2091. log_debug(LD_DIRSERV,"Received POST command.");
  2092. conn->_base.state = DIR_CONN_STATE_SERVER_WRITING;
  2093. if (!authdir_mode(options)) {
  2094. /* we just provide cached directories; we don't want to
  2095. * receive anything. */
  2096. write_http_status_line(conn, 400, "Nonauthoritative directory does not "
  2097. "accept posted server descriptors");
  2098. return 0;
  2099. }
  2100. if (parse_http_url(headers, &url) < 0) {
  2101. write_http_status_line(conn, 400, "Bad request");
  2102. return 0;
  2103. }
  2104. log_debug(LD_DIRSERV,"rewritten url as '%s'.", url);
  2105. if (authdir_mode_handles_descs(options) &&
  2106. !strcmp(url,"/tor/")) { /* server descriptor post */
  2107. const char *msg = NULL;
  2108. uint8_t purpose = authdir_mode_bridge(options) ?
  2109. ROUTER_PURPOSE_CONTROLLER : ROUTER_PURPOSE_GENERAL;
  2110. int r = dirserv_add_multiple_descriptors(body, purpose, &msg);
  2111. tor_assert(msg);
  2112. if (r > 0)
  2113. dirserv_get_directory(); /* rebuild and write to disk */
  2114. switch (r) {
  2115. case -2:
  2116. case -1:
  2117. case 1:
  2118. log_notice(LD_DIRSERV,
  2119. "Rejected router descriptor or extra-info from %s.",
  2120. conn->_base.address);
  2121. /* malformed descriptor, or something wrong */
  2122. write_http_status_line(conn, 400, msg);
  2123. break;
  2124. case 0: /* accepted but discarded */
  2125. case 2: /* accepted */
  2126. write_http_status_line(conn, 200, msg);
  2127. break;
  2128. }
  2129. goto done;
  2130. }
  2131. if (options->HSAuthoritativeDir &&
  2132. !strcmpstart(url,"/tor/rendezvous/publish")) {
  2133. /* rendezvous descriptor post */
  2134. log_info(LD_REND, "Handling rendezvous descriptor post.");
  2135. if (rend_cache_store(body, body_len, 1) < 0) {
  2136. // char tmp[1024*2+1];
  2137. log_fn(LOG_PROTOCOL_WARN, LD_DIRSERV,
  2138. "Rejected rend descriptor (length %d) from %s.",
  2139. (int)body_len, conn->_base.address);
  2140. write_http_status_line(conn, 400, "Invalid service descriptor rejected");
  2141. } else {
  2142. write_http_status_line(conn, 200, "Service descriptor stored");
  2143. }
  2144. goto done;
  2145. }
  2146. if (authdir_mode_v3(options) &&
  2147. !strcmp(url,"/tor/post/vote")) { /* server descriptor post */
  2148. const char *msg = "OK";
  2149. int status;
  2150. if (dirvote_add_vote(body, &msg, &status)) {
  2151. write_http_status_line(conn, status, "Vote stored");
  2152. } else {
  2153. tor_assert(msg);
  2154. write_http_status_line(conn, status, msg);
  2155. }
  2156. goto done;
  2157. }
  2158. if (authdir_mode_v3(options) &&
  2159. !strcmp(url,"/tor/post/consensus-signature")) { /* sigs on consensus. */
  2160. if (dirvote_add_signatures(body)>=0) {
  2161. write_http_status_line(conn, 200, "Signatures stored");
  2162. } else {
  2163. write_http_status_line(conn, 400, "Unable to store signatures");
  2164. }
  2165. goto done;
  2166. }
  2167. /* we didn't recognize the url */
  2168. write_http_status_line(conn, 404, "Not found");
  2169. done:
  2170. tor_free(url);
  2171. return 0;
  2172. }
  2173. /** Called when a dirserver receives data on a directory connection;
  2174. * looks for an HTTP request. If the request is complete, remove it
  2175. * from the inbuf, try to process it; otherwise, leave it on the
  2176. * buffer. Return a 0 on success, or -1 on error.
  2177. */
  2178. static int
  2179. directory_handle_command(dir_connection_t *conn)
  2180. {
  2181. char *headers=NULL, *body=NULL;
  2182. size_t body_len=0;
  2183. int r;
  2184. tor_assert(conn);
  2185. tor_assert(conn->_base.type == CONN_TYPE_DIR);
  2186. switch (fetch_from_buf_http(conn->_base.inbuf,
  2187. &headers, MAX_HEADERS_SIZE,
  2188. &body, &body_len, MAX_DIR_UL_SIZE, 0)) {
  2189. case -1: /* overflow */
  2190. log_warn(LD_DIRSERV,
  2191. "Invalid input from address '%s'. Closing.",
  2192. conn->_base.address);
  2193. return -1;
  2194. case 0:
  2195. log_debug(LD_DIRSERV,"command not all here yet.");
  2196. return 0;
  2197. /* case 1, fall through */
  2198. }
  2199. http_set_address_origin(headers, TO_CONN(conn));
  2200. //log_debug(LD_DIRSERV,"headers %s, body %s.", headers, body);
  2201. if (!strncasecmp(headers,"GET",3))
  2202. r = directory_handle_command_get(conn, headers, body, body_len);
  2203. else if (!strncasecmp(headers,"POST",4))
  2204. r = directory_handle_command_post(conn, headers, body, body_len);
  2205. else {
  2206. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  2207. "Got headers %s with unknown command. Closing.",
  2208. escaped(headers));
  2209. r = -1;
  2210. }
  2211. tor_free(headers); tor_free(body);
  2212. return r;
  2213. }
  2214. /** Write handler for directory connections; called when all data has
  2215. * been flushed. Close the connection or wait for a response as
  2216. * appropriate.
  2217. */
  2218. int
  2219. connection_dir_finished_flushing(dir_connection_t *conn)
  2220. {
  2221. tor_assert(conn);
  2222. tor_assert(conn->_base.type == CONN_TYPE_DIR);
  2223. switch (conn->_base.state) {
  2224. case DIR_CONN_STATE_CLIENT_SENDING:
  2225. log_debug(LD_DIR,"client finished sending command.");
  2226. conn->_base.state = DIR_CONN_STATE_CLIENT_READING;
  2227. connection_stop_writing(TO_CONN(conn));
  2228. return 0;
  2229. case DIR_CONN_STATE_SERVER_WRITING:
  2230. log_debug(LD_DIRSERV,"Finished writing server response. Closing.");
  2231. connection_mark_for_close(TO_CONN(conn));
  2232. return 0;
  2233. default:
  2234. log_warn(LD_BUG,"called in unexpected state %d.",
  2235. conn->_base.state);
  2236. tor_fragile_assert();
  2237. return -1;
  2238. }
  2239. return 0;
  2240. }
  2241. /** Connected handler for directory connections: begin sending data to the
  2242. * server */
  2243. int
  2244. connection_dir_finished_connecting(dir_connection_t *conn)
  2245. {
  2246. tor_assert(conn);
  2247. tor_assert(conn->_base.type == CONN_TYPE_DIR);
  2248. tor_assert(conn->_base.state == DIR_CONN_STATE_CONNECTING);
  2249. log_debug(LD_HTTP,"Dir connection to router %s:%u established.",
  2250. conn->_base.address,conn->_base.port);
  2251. conn->_base.state = DIR_CONN_STATE_CLIENT_SENDING; /* start flushing conn */
  2252. return 0;
  2253. }
  2254. /** Called when one or more networkstatus fetches have failed (with uppercase
  2255. * fingerprints listed in <b>failed</b>). Mark those fingerprints as having
  2256. * failed once, unless they failed with status code 503. */
  2257. static void
  2258. dir_networkstatus_download_failed(smartlist_t *failed, int status_code)
  2259. {
  2260. if (status_code == 503)
  2261. return;
  2262. SMARTLIST_FOREACH(failed, const char *, fp,
  2263. {
  2264. char digest[DIGEST_LEN];
  2265. trusted_dir_server_t *dir;
  2266. base16_decode(digest, DIGEST_LEN, fp, strlen(fp));
  2267. dir = router_get_trusteddirserver_by_digest(digest);
  2268. if (dir)
  2269. ++dir->n_networkstatus_failures;
  2270. });
  2271. }
  2272. /** Called when one or more routerdesc (or extrainfo, if <b>was_extrainfo</b>)
  2273. * fetches have failed (with uppercase fingerprints listed in
  2274. * <b>failed</b>). */
  2275. static void
  2276. dir_routerdesc_download_failed(smartlist_t *failed, int status_code,
  2277. int was_extrainfo)
  2278. {
  2279. char digest[DIGEST_LEN];
  2280. time_t now = time(NULL);
  2281. int server = server_mode(get_options()) && get_options()->DirPort;
  2282. SMARTLIST_FOREACH(failed, const char *, cp,
  2283. {
  2284. download_status_t *dls = NULL;
  2285. base16_decode(digest, DIGEST_LEN, cp, strlen(cp));
  2286. if (was_extrainfo) {
  2287. signed_descriptor_t *sd =
  2288. router_get_by_extrainfo_digest(digest);
  2289. if (sd)
  2290. dls = &sd->ei_dl_status;
  2291. } else {
  2292. local_routerstatus_t *rs =
  2293. router_get_combined_status_by_descriptor_digest(digest);
  2294. if (rs)
  2295. dls = &rs->dl_status;
  2296. }
  2297. if (!dls || dls->n_download_failures >= MAX_ROUTERDESC_DOWNLOAD_FAILURES)
  2298. continue;
  2299. if (status_code != 503 || server)
  2300. ++dls->n_download_failures;
  2301. if (server) {
  2302. switch (dls->n_download_failures) {
  2303. case 0: dls->next_attempt_at = 0; break;
  2304. case 1: dls->next_attempt_at = 0; break;
  2305. case 2: dls->next_attempt_at = 0; break;
  2306. case 3: dls->next_attempt_at = now+60; break;
  2307. case 4: dls->next_attempt_at = now+60; break;
  2308. case 5: dls->next_attempt_at = now+60*2; break;
  2309. case 6: dls->next_attempt_at = now+60*5; break;
  2310. case 7: dls->next_attempt_at = now+60*15; break;
  2311. default: dls->next_attempt_at = TIME_MAX; break;
  2312. }
  2313. } else {
  2314. switch (dls->n_download_failures) {
  2315. case 0: dls->next_attempt_at = 0; break;
  2316. case 1: dls->next_attempt_at = 0; break;
  2317. case 2: dls->next_attempt_at = now+60; break;
  2318. case 3: dls->next_attempt_at = now+60*5; break;
  2319. case 4: dls->next_attempt_at = now+60*10; break;
  2320. default: dls->next_attempt_at = TIME_MAX; break;
  2321. }
  2322. }
  2323. if (dls->next_attempt_at == 0)
  2324. log_debug(LD_DIR, "%s failed %d time(s); I'll try again immediately.",
  2325. cp, (int)dls->n_download_failures);
  2326. else if (dls->next_attempt_at < TIME_MAX)
  2327. log_debug(LD_DIR, "%s failed %d time(s); I'll try again in %d seconds.",
  2328. cp, (int)dls->n_download_failures,
  2329. (int)(dls->next_attempt_at-now));
  2330. else
  2331. log_debug(LD_DIR, "%s failed %d time(s); Giving up for a while.",
  2332. cp, (int)dls->n_download_failures);
  2333. });
  2334. /* No need to relaunch descriptor downloads here: we already do it
  2335. * every 10 seconds (DESCRIPTOR_RETRY_INTERVAL) in main.c. */
  2336. }
  2337. /** Given a directory <b>resource</b> request, containing zero
  2338. * or more strings separated by plus signs, followed optionally by ".z", store
  2339. * the strings, in order, into <b>fp_out</b>. If <b>compressed_out</b> is
  2340. * non-NULL, set it to 1 if the resource ends in ".z", else set it to 0. If
  2341. * decode_hex is true, then delete all elements that aren't hex digests, and
  2342. * decode the rest. If sort_uniq is true, then sort the list and remove
  2343. * all duplicates.
  2344. */
  2345. int
  2346. dir_split_resource_into_fingerprints(const char *resource,
  2347. smartlist_t *fp_out, int *compressed_out,
  2348. int decode_hex, int sort_uniq)
  2349. {
  2350. smartlist_t *fp_tmp = smartlist_create();
  2351. tor_assert(fp_out);
  2352. smartlist_split_string(fp_tmp, resource, "+", 0, 0);
  2353. if (compressed_out)
  2354. *compressed_out = 0;
  2355. if (smartlist_len(fp_tmp)) {
  2356. char *last = smartlist_get(fp_tmp,smartlist_len(fp_tmp)-1);
  2357. size_t last_len = strlen(last);
  2358. if (last_len > 2 && !strcmp(last+last_len-2, ".z")) {
  2359. last[last_len-2] = '\0';
  2360. if (compressed_out)
  2361. *compressed_out = 1;
  2362. }
  2363. }
  2364. if (decode_hex) {
  2365. int i;
  2366. char *cp, *d = NULL;
  2367. for (i = 0; i < smartlist_len(fp_tmp); ++i) {
  2368. cp = smartlist_get(fp_tmp, i);
  2369. if (strlen(cp) != HEX_DIGEST_LEN) {
  2370. log_info(LD_DIR,
  2371. "Skipping digest %s with non-standard length.", escaped(cp));
  2372. smartlist_del_keeporder(fp_tmp, i--);
  2373. goto again;
  2374. }
  2375. d = tor_malloc_zero(DIGEST_LEN);
  2376. if (base16_decode(d, DIGEST_LEN, cp, HEX_DIGEST_LEN)<0) {
  2377. log_info(LD_DIR, "Skipping non-decodable digest %s", escaped(cp));
  2378. smartlist_del_keeporder(fp_tmp, i--);
  2379. goto again;
  2380. }
  2381. smartlist_set(fp_tmp, i, d);
  2382. d = NULL;
  2383. again:
  2384. tor_free(cp);
  2385. tor_free(d);
  2386. }
  2387. }
  2388. if (sort_uniq) {
  2389. smartlist_t *fp_tmp2 = smartlist_create();
  2390. int i;
  2391. if (decode_hex)
  2392. smartlist_sort_digests(fp_tmp);
  2393. else
  2394. smartlist_sort_strings(fp_tmp);
  2395. if (smartlist_len(fp_tmp))
  2396. smartlist_add(fp_tmp2, smartlist_get(fp_tmp, 0));
  2397. for (i = 1; i < smartlist_len(fp_tmp); ++i) {
  2398. char *cp = smartlist_get(fp_tmp, i);
  2399. char *last = smartlist_get(fp_tmp2, smartlist_len(fp_tmp2)-1);
  2400. if ((decode_hex && memcmp(cp, last, DIGEST_LEN))
  2401. || (!decode_hex && strcasecmp(cp, last)))
  2402. smartlist_add(fp_tmp2, cp);
  2403. else
  2404. tor_free(cp);
  2405. }
  2406. smartlist_free(fp_tmp);
  2407. fp_tmp = fp_tmp2;
  2408. }
  2409. smartlist_add_all(fp_out, fp_tmp);
  2410. smartlist_free(fp_tmp);
  2411. return 0;
  2412. }