directory.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976
  1. /* Copyright 2001-2004 Roger Dingledine.
  2. * Copyright 2004-2006 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
  31. directory_initiate_command(const char *address, uint32_t addr, uint16_t port,
  32. const char *platform,
  33. const char *digest, uint8_t purpose,
  34. int private_connection, const char *resource,
  35. const char *payload, size_t payload_len);
  36. static void
  37. directory_send_command(connection_t *conn, const char *platform,
  38. int purpose, const char *resource,
  39. const char *payload, size_t payload_len);
  40. static int directory_handle_command(connection_t *conn);
  41. static int body_is_plausible(const char *body, size_t body_len, int purpose);
  42. static int purpose_is_private(uint8_t purpose);
  43. static char *http_get_header(const char *headers, const char *which);
  44. static char *http_get_origin(const char *headers, connection_t *conn);
  45. static void connection_dir_download_networkstatus_failed(connection_t *conn);
  46. static void connection_dir_download_routerdesc_failed(connection_t *conn);
  47. static void dir_networkstatus_download_failed(smartlist_t *failed);
  48. static void dir_routerdesc_download_failed(smartlist_t *failed);
  49. static void note_request(const char *key, size_t bytes);
  50. /********* START VARIABLES **********/
  51. /** How far in the future do we allow a directory server to tell us it is
  52. * before deciding that one of us has the wrong time? */
  53. #define ALLOW_DIRECTORY_TIME_SKEW (30*60)
  54. /********* END VARIABLES ************/
  55. /** Return true iff the directory purpose 'purpose' must use an
  56. * anonymous connection to a directory. */
  57. static int
  58. purpose_is_private(uint8_t purpose)
  59. {
  60. if (get_options()->AllDirActionsPrivate)
  61. return 1;
  62. if (purpose == DIR_PURPOSE_FETCH_DIR ||
  63. purpose == DIR_PURPOSE_UPLOAD_DIR ||
  64. purpose == DIR_PURPOSE_FETCH_RUNNING_LIST ||
  65. purpose == DIR_PURPOSE_FETCH_NETWORKSTATUS ||
  66. purpose == DIR_PURPOSE_FETCH_SERVERDESC)
  67. return 0;
  68. return 1;
  69. }
  70. /** Start a connection to every known directory server, using
  71. * connection purpose 'purpose' and uploading the payload 'payload'
  72. * (length 'payload_len'). The purpose should be one of
  73. * 'DIR_PURPOSE_UPLOAD_DIR' or 'DIR_PURPOSE_UPLOAD_RENDDESC'.
  74. */
  75. void
  76. directory_post_to_dirservers(uint8_t purpose, const char *payload,
  77. size_t payload_len)
  78. {
  79. smartlist_t *dirservers;
  80. int post_via_tor;
  81. int post_to_v1_only;
  82. dirservers = router_get_trusted_dir_servers();
  83. tor_assert(dirservers);
  84. /* Only old dirservers handle rendezvous descriptor publishing. */
  85. post_to_v1_only = (purpose == DIR_PURPOSE_UPLOAD_RENDDESC);
  86. /* This tries dirservers which we believe to be down, but ultimately, that's
  87. * harmless, and we may as well err on the side of getting things uploaded.
  88. */
  89. SMARTLIST_FOREACH(dirservers, trusted_dir_server_t *, ds,
  90. {
  91. routerstatus_t *rs = &(ds->fake_status);
  92. if (post_to_v1_only && !ds->is_v1_authority)
  93. continue;
  94. post_via_tor = purpose_is_private(purpose) ||
  95. !fascist_firewall_allows_address_dir(ds->addr, ds->dir_port);
  96. directory_initiate_command_routerstatus(rs, purpose, post_via_tor,
  97. NULL, payload, payload_len);
  98. });
  99. }
  100. /** Start a connection to a random running directory server, using
  101. * connection purpose 'purpose' and requesting 'resource'.
  102. * If <b>retry_if_no_servers</b>, then if all the possible servers seem
  103. * down, mark them up and try again.
  104. */
  105. void
  106. directory_get_from_dirserver(uint8_t purpose, const char *resource,
  107. int retry_if_no_servers)
  108. {
  109. routerstatus_t *rs = NULL;
  110. or_options_t *options = get_options();
  111. int prefer_authority = server_mode(options) && options->DirPort != 0;
  112. int directconn = !purpose_is_private(purpose);
  113. int need_v1_support = purpose == DIR_PURPOSE_FETCH_DIR ||
  114. purpose == DIR_PURPOSE_FETCH_RUNNING_LIST ||
  115. purpose == DIR_PURPOSE_FETCH_RENDDESC;
  116. int need_v2_support = purpose == DIR_PURPOSE_FETCH_NETWORKSTATUS ||
  117. purpose == DIR_PURPOSE_FETCH_SERVERDESC;
  118. if (!options->FetchServerDescriptors &&
  119. (need_v1_support || need_v2_support))
  120. return;
  121. if (directconn) {
  122. if (prefer_authority) {
  123. /* only ask authdirservers, and don't ask myself */
  124. rs = router_pick_trusteddirserver(need_v1_support, 1, 1,
  125. retry_if_no_servers);
  126. }
  127. if (!rs) {
  128. /* anybody with a non-zero dirport will do */
  129. rs = router_pick_directory_server(1, 1, need_v2_support,
  130. retry_if_no_servers);
  131. if (!rs) {
  132. const char *which;
  133. if (purpose == DIR_PURPOSE_FETCH_DIR)
  134. which = "directory";
  135. else if (purpose == DIR_PURPOSE_FETCH_RUNNING_LIST)
  136. which = "status list";
  137. else if (purpose == DIR_PURPOSE_FETCH_NETWORKSTATUS)
  138. which = "network status";
  139. else // if (purpose == DIR_PURPOSE_FETCH_NETWORKSTATUS)
  140. which = "server descriptors";
  141. log_info(LD_DIR,
  142. "No router found for %s; falling back to dirserver list",
  143. which);
  144. rs = router_pick_trusteddirserver(need_v1_support, 1, 1,
  145. retry_if_no_servers);
  146. if (!rs)
  147. directconn = 0; /* last resort: try routing it via Tor */
  148. }
  149. }
  150. }
  151. if (!directconn) {
  152. /* Never use fascistfirewall; we're going via Tor. */
  153. if (purpose == DIR_PURPOSE_FETCH_RENDDESC) {
  154. /* only ask authdirservers, any of them will do */
  155. rs = router_pick_trusteddirserver(1, 0, 0, retry_if_no_servers);
  156. } else {
  157. /* anybody with a non-zero dirport will do. Disregard firewalls. */
  158. rs = router_pick_directory_server(1, 0, need_v2_support,
  159. retry_if_no_servers);
  160. /* If we have any hope of building an indirect conn, we know some router
  161. * descriptors. If (rs==NULL), we can't build circuits anyway, so
  162. * there's no point in falling back to the authorities in this case. */
  163. }
  164. }
  165. if (rs)
  166. directory_initiate_command_routerstatus(rs, purpose, !directconn,
  167. resource, NULL, 0);
  168. else {
  169. log_notice(LD_DIR,
  170. "No running dirservers known. Will try again later. "
  171. "(purpose %d)", purpose);
  172. if (!purpose_is_private(purpose)) {
  173. /* remember we tried them all and failed. */
  174. directory_all_unreachable(time(NULL));
  175. }
  176. }
  177. }
  178. /** Launch a new connection to the directory server <b>router</b> to upload or
  179. * download a service or rendezvous descriptor. <b>purpose</b> determines what
  180. * kind of directory connection we're launching, and must be one of
  181. * DIR_PURPOSE_{FETCH|UPLOAD}_{DIR|RENDDESC}.
  182. *
  183. * When uploading, <b>payload</b> and <b>payload_len</b> determine the content
  184. * of the HTTP post. Otherwise, <b>payload</b> should be NULL.
  185. *
  186. * When fetching a rendezvous descriptor, <b>resource</b> is the service ID we
  187. * want to fetch.
  188. */
  189. void
  190. directory_initiate_command_router(routerinfo_t *router,
  191. uint8_t purpose,
  192. int private_connection,
  193. const char *resource,
  194. const char *payload,
  195. size_t payload_len)
  196. {
  197. directory_initiate_command(router->address, router->addr, router->dir_port,
  198. router->platform,
  199. router->cache_info.identity_digest,
  200. purpose, private_connection, resource,
  201. payload, payload_len);
  202. }
  203. /** Launch a new connection to the directory server <b>status</b> to upload or
  204. * download a service or rendezvous descriptor. <b>purpose</b> determines what
  205. * kind of directory connection we're launching, and must be one of
  206. * DIR_PURPOSE_{FETCH|UPLOAD}_{DIR|RENDDESC}.
  207. *
  208. * When uploading, <b>payload</b> and <b>payload_len</b> determine the content
  209. * of the HTTP post. Otherwise, <b>payload</b> should be NULL.
  210. *
  211. * When fetching a rendezvous descriptor, <b>resource</b> is the service ID we
  212. * want to fetch.
  213. */
  214. void
  215. directory_initiate_command_routerstatus(routerstatus_t *status,
  216. uint8_t purpose,
  217. int private_connection,
  218. const char *resource,
  219. const char *payload,
  220. size_t payload_len)
  221. {
  222. const char *platform = NULL;
  223. routerinfo_t *router;
  224. char address_buf[INET_NTOA_BUF_LEN];
  225. struct in_addr in;
  226. const char *address;
  227. if ((router = router_get_by_digest(status->identity_digest))) {
  228. platform = router->platform;
  229. address = router->address;
  230. } else {
  231. in.s_addr = htonl(status->addr);
  232. tor_inet_ntoa(&in, address_buf, sizeof(address_buf));
  233. address = address_buf;
  234. }
  235. directory_initiate_command(address, status->addr, status->dir_port,
  236. platform, status->identity_digest,
  237. purpose, private_connection, resource,
  238. payload, payload_len);
  239. }
  240. /** Called when we are unable to complete the client's request to a
  241. * directory server: Mark the router as down and try again if possible.
  242. */
  243. void
  244. connection_dir_request_failed(connection_t *conn)
  245. {
  246. if (router_digest_is_me(conn->identity_digest))
  247. return; /* this was a test fetch. don't retry. */
  248. router_set_status(conn->identity_digest, 0); /* don't try him again */
  249. if (conn->purpose == DIR_PURPOSE_FETCH_DIR ||
  250. conn->purpose == DIR_PURPOSE_FETCH_RUNNING_LIST) {
  251. log_info(LD_DIR, "Giving up on directory server at '%s:%d'; retrying",
  252. conn->address, conn->port);
  253. directory_get_from_dirserver(conn->purpose, NULL,
  254. 0 /* don't retry_if_no_servers */);
  255. } else if (conn->purpose == DIR_PURPOSE_FETCH_NETWORKSTATUS) {
  256. log_info(LD_DIR, "Giving up on directory server at '%s'; retrying",
  257. conn->address);
  258. connection_dir_download_networkstatus_failed(conn);
  259. } else if (conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC) {
  260. log_info(LD_DIR, "Giving up on directory server at '%s'; retrying",
  261. conn->address);
  262. connection_dir_download_routerdesc_failed(conn);
  263. }
  264. }
  265. /** Called when an attempt to download one or more network status
  266. * documents on connection <b>conn</b> failed. Decide whether to
  267. * retry the fetch now, later, or never.
  268. */
  269. static void
  270. connection_dir_download_networkstatus_failed(connection_t *conn)
  271. {
  272. if (!conn->requested_resource) {
  273. /* We never reached directory_send_command, which means that we never
  274. * opened a network connection. Either we're out of sockets, or the
  275. * network is down. Either way, retrying would be pointless. */
  276. return;
  277. }
  278. if (!strcmpstart(conn->requested_resource, "all")) {
  279. /* We're a non-authoritative directory cache; try again. */
  280. smartlist_t *trusted_dirs = router_get_trusted_dir_servers();
  281. SMARTLIST_FOREACH(trusted_dirs, trusted_dir_server_t *, ds,
  282. ++ds->n_networkstatus_failures);
  283. directory_get_from_dirserver(conn->purpose, "all.z",
  284. 0 /* don't retry_if_no_servers */);
  285. } else if (!strcmpstart(conn->requested_resource, "fp/")) {
  286. /* We were trying to download by fingerprint; mark them all as having
  287. * failed, and possibly retry them later.*/
  288. smartlist_t *failed = smartlist_create();
  289. dir_split_resource_into_fingerprints(conn->requested_resource+3,
  290. failed, NULL, 0);
  291. if (smartlist_len(failed)) {
  292. dir_networkstatus_download_failed(failed);
  293. SMARTLIST_FOREACH(failed, char *, cp, tor_free(cp));
  294. }
  295. smartlist_free(failed);
  296. }
  297. }
  298. /** Called when an attempt to download one or more router descriptors
  299. * on connection <b>conn</b> failed.
  300. */
  301. static void
  302. connection_dir_download_routerdesc_failed(connection_t *conn)
  303. {
  304. /* Try again. No need to increment the failure count for routerdescs, since
  305. * it's not their fault.*/
  306. /* update_router_descriptor_downloads(time(NULL)); */
  307. (void) conn;
  308. /* XXXX Why did the above get commented out? -NM */
  309. }
  310. /** Helper for directory_initiate_command_(router|trusted_dir): send the
  311. * command to a server whose address is <b>address</b>, whose IP is
  312. * <b>addr</b>, whose directory port is <b>dir_port</b>, whose tor version is
  313. * <b>platform</b>, and whose identity key digest is <b>digest</b>. The
  314. * <b>platform</b> argument is optional; the others are required. */
  315. static void
  316. directory_initiate_command(const char *address, uint32_t addr,
  317. uint16_t dir_port, const char *platform,
  318. const char *digest, uint8_t purpose,
  319. int private_connection, const char *resource,
  320. const char *payload, size_t payload_len)
  321. {
  322. connection_t *conn;
  323. tor_assert(address);
  324. tor_assert(addr);
  325. tor_assert(dir_port);
  326. tor_assert(digest);
  327. switch (purpose) {
  328. case DIR_PURPOSE_FETCH_DIR:
  329. log_debug(LD_DIR,"initiating directory fetch");
  330. break;
  331. case DIR_PURPOSE_FETCH_RENDDESC:
  332. log_debug(LD_DIR,"initiating hidden-service descriptor fetch");
  333. break;
  334. case DIR_PURPOSE_UPLOAD_DIR:
  335. log_debug(LD_OR,"initiating server descriptor upload");
  336. break;
  337. case DIR_PURPOSE_UPLOAD_RENDDESC:
  338. log_debug(LD_REND,"initiating hidden-service descriptor upload");
  339. break;
  340. case DIR_PURPOSE_FETCH_RUNNING_LIST:
  341. log_debug(LD_DIR,"initiating running-routers fetch");
  342. break;
  343. case DIR_PURPOSE_FETCH_NETWORKSTATUS:
  344. log_debug(LD_DIR,"initiating network-status fetch");
  345. break;
  346. case DIR_PURPOSE_FETCH_SERVERDESC:
  347. log_debug(LD_DIR,"initiating server descriptor fetch");
  348. break;
  349. default:
  350. log_err(LD_BUG, "Unrecognized directory connection purpose.");
  351. tor_assert(0);
  352. }
  353. conn = connection_new(CONN_TYPE_DIR);
  354. /* set up conn so it's got all the data we need to remember */
  355. conn->addr = addr;
  356. conn->port = dir_port;
  357. conn->address = tor_strdup(address);
  358. memcpy(conn->identity_digest, digest, DIGEST_LEN);
  359. conn->purpose = purpose;
  360. /* give it an initial state */
  361. conn->state = DIR_CONN_STATE_CONNECTING;
  362. if (!private_connection) {
  363. /* then we want to connect directly */
  364. if (get_options()->HttpProxy) {
  365. addr = get_options()->HttpProxyAddr;
  366. dir_port = get_options()->HttpProxyPort;
  367. }
  368. switch (connection_connect(conn, conn->address, addr, dir_port)) {
  369. case -1:
  370. connection_dir_request_failed(conn); /* retry if we want */
  371. connection_free(conn);
  372. return;
  373. case 1:
  374. conn->state = DIR_CONN_STATE_CLIENT_SENDING; /* start flushing conn */
  375. /* fall through */
  376. case 0:
  377. /* queue the command on the outbuf */
  378. directory_send_command(conn, platform, purpose, resource,
  379. payload, payload_len);
  380. connection_watch_events(conn, EV_READ | EV_WRITE);
  381. /* writable indicates finish, readable indicates broken link,
  382. error indicates broken link in windowsland. */
  383. }
  384. } else { /* we want to connect via tor */
  385. /* make an AP connection
  386. * populate it and add it at the right state
  387. * socketpair and hook up both sides
  388. */
  389. conn->s = connection_ap_make_bridge(conn->address, conn->port);
  390. if (conn->s < 0) {
  391. log_warn(LD_NET,"Making AP bridge to dirserver failed.");
  392. connection_mark_for_close(conn);
  393. return;
  394. }
  395. if (connection_add(conn) < 0) {
  396. log_warn(LD_NET,"Unable to add AP bridge to dirserver.");
  397. connection_mark_for_close(conn);
  398. return;
  399. }
  400. conn->state = DIR_CONN_STATE_CLIENT_SENDING;
  401. /* queue the command on the outbuf */
  402. directory_send_command(conn, platform, purpose, resource,
  403. payload, payload_len);
  404. connection_watch_events(conn, EV_READ | EV_WRITE);
  405. }
  406. }
  407. /** Queue an appropriate HTTP command on conn-\>outbuf. The other args
  408. * are as in directory_initiate_command.
  409. */
  410. static void
  411. directory_send_command(connection_t *conn, const char *platform,
  412. int purpose, const char *resource,
  413. const char *payload, size_t payload_len)
  414. {
  415. char proxystring[256];
  416. char proxyauthstring[256];
  417. char hoststring[128];
  418. char *url;
  419. char request[8192];
  420. const char *httpcommand = NULL;
  421. size_t len;
  422. tor_assert(conn);
  423. tor_assert(conn->type == CONN_TYPE_DIR);
  424. tor_free(conn->requested_resource);
  425. if (resource)
  426. conn->requested_resource = tor_strdup(resource);
  427. /* come up with a string for which Host: we want */
  428. if (conn->port == 80) {
  429. strlcpy(hoststring, conn->address, sizeof(hoststring));
  430. } else {
  431. tor_snprintf(hoststring, sizeof(hoststring),"%s:%d",
  432. conn->address, conn->port);
  433. }
  434. /* come up with some proxy lines, if we're using one. */
  435. if (get_options()->HttpProxy) {
  436. char *base64_authenticator=NULL;
  437. const char *authenticator = get_options()->HttpProxyAuthenticator;
  438. tor_snprintf(proxystring, sizeof(proxystring),"http://%s", hoststring);
  439. if (authenticator) {
  440. base64_authenticator = alloc_http_authenticator(authenticator);
  441. if (!base64_authenticator)
  442. log_warn(LD_BUG, "Encoding http authenticator failed");
  443. }
  444. if (base64_authenticator) {
  445. tor_snprintf(proxyauthstring, sizeof(proxyauthstring),
  446. "\r\nProxy-Authorization: Basic %s",
  447. base64_authenticator);
  448. tor_free(base64_authenticator);
  449. } else {
  450. proxyauthstring[0] = 0;
  451. }
  452. } else {
  453. proxystring[0] = 0;
  454. proxyauthstring[0] = 0;
  455. }
  456. switch (purpose) {
  457. case DIR_PURPOSE_FETCH_DIR:
  458. tor_assert(!resource);
  459. tor_assert(!payload);
  460. log_debug(LD_DIR,
  461. "Asking for compressed directory from server running %s",
  462. platform?escaped(platform):"<unknown version>");
  463. httpcommand = "GET";
  464. url = tor_strdup("/tor/dir.z");
  465. break;
  466. case DIR_PURPOSE_FETCH_RUNNING_LIST:
  467. tor_assert(!resource);
  468. tor_assert(!payload);
  469. httpcommand = "GET";
  470. url = tor_strdup("/tor/running-routers");
  471. break;
  472. case DIR_PURPOSE_FETCH_NETWORKSTATUS:
  473. httpcommand = "GET";
  474. len = strlen(resource)+32;
  475. url = tor_malloc(len);
  476. tor_snprintf(url, len, "/tor/status/%s", resource);
  477. break;
  478. case DIR_PURPOSE_FETCH_SERVERDESC:
  479. httpcommand = "GET";
  480. len = strlen(resource)+32;
  481. url = tor_malloc(len);
  482. tor_snprintf(url, len, "/tor/server/%s", resource);
  483. break;
  484. case DIR_PURPOSE_UPLOAD_DIR:
  485. tor_assert(!resource);
  486. tor_assert(payload);
  487. httpcommand = "POST";
  488. url = tor_strdup("/tor/");
  489. break;
  490. case DIR_PURPOSE_FETCH_RENDDESC:
  491. tor_assert(resource);
  492. tor_assert(!payload);
  493. /* this must be true or we wouldn't be doing the lookup */
  494. tor_assert(strlen(resource) <= REND_SERVICE_ID_LEN);
  495. /* This breaks the function abstraction. */
  496. strlcpy(conn->rend_query, resource, sizeof(conn->rend_query));
  497. httpcommand = "GET";
  498. /* Request the most recent versioned descriptor. */
  499. // (XXXX We were going to switch this to fetch rendezvous1 descriptors,
  500. // but that never got testing, and it wasn't a good design.)
  501. len = strlen(resource)+32;
  502. url = tor_malloc(len);
  503. tor_snprintf(url, len, "/tor/rendezvous/%s", resource);
  504. break;
  505. case DIR_PURPOSE_UPLOAD_RENDDESC:
  506. tor_assert(!resource);
  507. tor_assert(payload);
  508. httpcommand = "POST";
  509. url = tor_strdup("/tor/rendezvous/publish");
  510. break;
  511. default:
  512. tor_assert(0);
  513. return;
  514. }
  515. if (strlen(proxystring) + strlen(url) >= 4096) {
  516. log_warn(LD_BUG,
  517. "Bug: squid does not like URLs longer than 4095 bytes, this "
  518. "one is %d bytes long: %s%s",
  519. (int)(strlen(proxystring) + strlen(url)), proxystring, url);
  520. }
  521. tor_snprintf(request, sizeof(request), "%s %s", httpcommand, proxystring);
  522. connection_write_to_buf(request, strlen(request), conn);
  523. connection_write_to_buf(url, strlen(url), conn);
  524. tor_free(url);
  525. if (!strcmp(httpcommand, "GET") && !payload) {
  526. tor_snprintf(request, sizeof(request),
  527. " HTTP/1.0\r\nHost: %s%s\r\n\r\n",
  528. hoststring,
  529. proxyauthstring);
  530. } else {
  531. tor_snprintf(request, sizeof(request),
  532. " HTTP/1.0\r\nContent-Length: %lu\r\nHost: %s%s\r\n\r\n",
  533. payload ? (unsigned long)payload_len : 0,
  534. hoststring,
  535. proxyauthstring);
  536. }
  537. connection_write_to_buf(request, strlen(request), conn);
  538. if (payload) {
  539. /* then send the payload afterwards too */
  540. connection_write_to_buf(payload, payload_len, conn);
  541. }
  542. }
  543. /** Parse an HTTP request string <b>headers</b> of the form
  544. * \verbatim
  545. * "\%s [http[s]://]\%s HTTP/1..."
  546. * \endverbatim
  547. * If it's well-formed, strdup the second \%s into *<b>url</b>, and
  548. * null-terminate it. If the url doesn't start with "/tor/", rewrite it
  549. * so it does. Return 0.
  550. * Otherwise, return -1.
  551. */
  552. static int
  553. parse_http_url(char *headers, char **url)
  554. {
  555. char *s, *start, *tmp;
  556. s = (char *)eat_whitespace_no_nl(headers);
  557. if (!*s) return -1;
  558. s = (char *)find_whitespace(s); /* get past GET/POST */
  559. if (!*s) return -1;
  560. s = (char *)eat_whitespace_no_nl(s);
  561. if (!*s) return -1;
  562. start = s; /* this is it, assuming it's valid */
  563. s = (char *)find_whitespace(start);
  564. if (!*s) return -1;
  565. /* tolerate the http[s] proxy style of putting the hostname in the url */
  566. if (s-start >= 4 && !strcmpstart(start,"http")) {
  567. tmp = start + 4;
  568. if (*tmp == 's')
  569. tmp++;
  570. if (s-tmp >= 3 && !strcmpstart(tmp,"://")) {
  571. tmp = strchr(tmp+3, '/');
  572. if (tmp && tmp < s) {
  573. log_debug(LD_DIR,"Skipping over 'http[s]://hostname' string");
  574. start = tmp;
  575. }
  576. }
  577. }
  578. if (s-start < 5 || strcmpstart(start,"/tor/")) { /* need to rewrite it */
  579. *url = tor_malloc(s - start + 5);
  580. strlcpy(*url,"/tor", s-start+5);
  581. strlcat((*url)+4, start, s-start+1);
  582. } else {
  583. *url = tor_strndup(start, s-start);
  584. }
  585. return 0;
  586. }
  587. /** Return a copy of the first HTTP header in <b>headers</b> whose key is
  588. * <b>which</b>. The key should be given with a terminating colon and space;
  589. * this function copies everything after, up to but not including the
  590. * following \\r\\n. */
  591. static char *
  592. http_get_header(const char *headers, const char *which)
  593. {
  594. const char *cp = headers;
  595. while (cp) {
  596. if (!strcmpstart(cp, which)) {
  597. char *eos;
  598. cp += strlen(which);
  599. if ((eos = strchr(cp,'\r')))
  600. return tor_strndup(cp, eos-cp);
  601. else
  602. return tor_strdup(cp);
  603. }
  604. cp = strchr(cp, '\n');
  605. if (cp)
  606. ++cp;
  607. }
  608. return NULL;
  609. }
  610. /** Allocate and return a string describing the source of an HTTP request with
  611. * headers <b>headers</b> received on <b>conn</b>. The format is either
  612. * "'1.2.3.4'", or "'1.2.3.4' (forwarded for '5.6.7.8')".
  613. */
  614. static char *
  615. http_get_origin(const char *headers, connection_t *conn)
  616. {
  617. char *fwd;
  618. fwd = http_get_header(headers, "Forwarded-For: ");
  619. if (!fwd)
  620. fwd = http_get_header(headers, "X-Forwarded-For: ");
  621. if (fwd) {
  622. size_t len = strlen(fwd)+strlen(conn->address)+32;
  623. char *result = tor_malloc(len);
  624. tor_snprintf(result, len, "'%s' (forwarded for %s)", conn->address,
  625. escaped(fwd));
  626. tor_free(fwd);
  627. return result;
  628. } else {
  629. size_t len = strlen(conn->address)+3;
  630. char *result = tor_malloc(len);
  631. tor_snprintf(result, len, "'%s'", conn->address);
  632. return result;
  633. }
  634. }
  635. /** Parse an HTTP response string <b>headers</b> of the form
  636. * \verbatim
  637. * "HTTP/1.\%d \%d\%s\r\n...".
  638. * \endverbatim
  639. *
  640. * If it's well-formed, assign the status code to *<b>code</b> and
  641. * return 0. Otherwise, return -1.
  642. *
  643. * On success: If <b>date</b> is provided, set *date to the Date
  644. * header in the http headers, or 0 if no such header is found. If
  645. * <b>compression</b> is provided, set *<b>compression</b> to the
  646. * compression method given in the Content-Encoding header, or 0 if no
  647. * such header is found, or -1 if the value of the header is not
  648. * recognized. If <b>reason</b> is provided, strdup the reason string
  649. * into it.
  650. */
  651. int
  652. parse_http_response(const char *headers, int *code, time_t *date,
  653. int *compression, char **reason)
  654. {
  655. int n1, n2;
  656. char datestr[RFC1123_TIME_LEN+1];
  657. smartlist_t *parsed_headers;
  658. tor_assert(headers);
  659. tor_assert(code);
  660. while (TOR_ISSPACE(*headers)) headers++; /* tolerate leading whitespace */
  661. if (sscanf(headers, "HTTP/1.%d %d", &n1, &n2) < 2 ||
  662. (n1 != 0 && n1 != 1) ||
  663. (n2 < 100 || n2 >= 600)) {
  664. log_warn(LD_HTTP,"Failed to parse header %s",escaped(headers));
  665. return -1;
  666. }
  667. *code = n2;
  668. parsed_headers = smartlist_create();
  669. smartlist_split_string(parsed_headers, headers, "\n",
  670. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
  671. if (reason) {
  672. smartlist_t *status_line_elements = smartlist_create();
  673. tor_assert(smartlist_len(parsed_headers));
  674. smartlist_split_string(status_line_elements,
  675. smartlist_get(parsed_headers, 0),
  676. " ", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 3);
  677. tor_assert(smartlist_len(status_line_elements) <= 3);
  678. if (smartlist_len(status_line_elements) == 3) {
  679. *reason = smartlist_get(status_line_elements, 2);
  680. smartlist_set(status_line_elements, 2, NULL); /* Prevent free */
  681. }
  682. SMARTLIST_FOREACH(status_line_elements, char *, cp, tor_free(cp));
  683. smartlist_free(status_line_elements);
  684. }
  685. if (date) {
  686. *date = 0;
  687. SMARTLIST_FOREACH(parsed_headers, const char *, s,
  688. if (!strcmpstart(s, "Date: ")) {
  689. strlcpy(datestr, s+6, sizeof(datestr));
  690. /* This will do nothing on failure, so we don't need to check
  691. the result. We shouldn't warn, since there are many other valid
  692. date formats besides the one we use. */
  693. parse_rfc1123_time(datestr, date);
  694. break;
  695. });
  696. }
  697. if (compression) {
  698. const char *enc = NULL;
  699. SMARTLIST_FOREACH(parsed_headers, const char *, s,
  700. if (!strcmpstart(s, "Content-Encoding: ")) {
  701. enc = s+18; break;
  702. });
  703. if (!enc || !strcmp(enc, "identity")) {
  704. *compression = 0;
  705. } else if (!strcmp(enc, "deflate") || !strcmp(enc, "x-deflate")) {
  706. *compression = ZLIB_METHOD;
  707. } else if (!strcmp(enc, "gzip") || !strcmp(enc, "x-gzip")) {
  708. *compression = GZIP_METHOD;
  709. } else {
  710. log_info(LD_HTTP, "Unrecognized content encoding: %s. Trying to deal.",
  711. escaped(enc));
  712. *compression = -1;
  713. }
  714. }
  715. SMARTLIST_FOREACH(parsed_headers, char *, s, tor_free(s));
  716. smartlist_free(parsed_headers);
  717. return 0;
  718. }
  719. /** Return true iff <b>body</b> doesn't start with a plausible router or
  720. * running-list or directory opening. This is a sign of possible compression.
  721. **/
  722. static int
  723. body_is_plausible(const char *body, size_t len, int purpose)
  724. {
  725. int i;
  726. if (len == 0)
  727. return 1; /* empty bodies don't need decompression */
  728. if (len < 32)
  729. return 0;
  730. if (purpose != DIR_PURPOSE_FETCH_RENDDESC) {
  731. if (!strcmpstart(body,"router") ||
  732. !strcmpstart(body,"signed-directory") ||
  733. !strcmpstart(body,"network-status") ||
  734. !strcmpstart(body,"running-routers"))
  735. return 1;
  736. for (i=0;i<32;++i) {
  737. if (!TOR_ISPRINT(body[i]) && !TOR_ISSPACE(body[i]))
  738. return 0;
  739. }
  740. return 1;
  741. } else {
  742. return 1;
  743. }
  744. }
  745. /** We are a client, and we've finished reading the server's
  746. * response. Parse and it and act appropriately.
  747. *
  748. * If we're still happy with using this directory server in the future, return
  749. * 0. Otherwise return -1; and the caller should consider trying the request
  750. * again.
  751. *
  752. * The caller will take care of marking the connection for close.
  753. */
  754. static int
  755. connection_dir_client_reached_eof(connection_t *conn)
  756. {
  757. char *body;
  758. char *headers;
  759. char *reason = NULL;
  760. size_t body_len=0, orig_len=0;
  761. int status_code;
  762. time_t now, date_header=0;
  763. int delta;
  764. int compression;
  765. int plausible;
  766. int skewed=0;
  767. int allow_partial = conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC;
  768. int was_compressed=0;
  769. switch (fetch_from_buf_http(conn->inbuf,
  770. &headers, MAX_HEADERS_SIZE,
  771. &body, &body_len, MAX_DIR_SIZE,
  772. allow_partial)) {
  773. case -1: /* overflow */
  774. log_warn(LD_PROTOCOL,
  775. "'fetch' response too large (server '%s:%d'). Closing.",
  776. conn->address, conn->port);
  777. return -1;
  778. case 0:
  779. log_info(LD_HTTP,
  780. "'fetch' response not all here, but we're at eof. Closing.");
  781. return -1;
  782. /* case 1, fall through */
  783. }
  784. orig_len = body_len;
  785. if (parse_http_response(headers, &status_code, &date_header,
  786. &compression, &reason) < 0) {
  787. log_warn(LD_HTTP,"Unparseable headers (server '%s:%d'). Closing.",
  788. conn->address, conn->port);
  789. tor_free(body); tor_free(headers);
  790. return -1;
  791. }
  792. if (!reason) reason = tor_strdup("[no reason given]");
  793. log_debug(LD_DIR,
  794. "Received response from directory server '%s:%d': %d %s",
  795. conn->address, conn->port, status_code, escaped(reason));
  796. if (date_header > 0) {
  797. now = time(NULL);
  798. delta = now-date_header;
  799. if (abs(delta)>ALLOW_DIRECTORY_TIME_SKEW) {
  800. log_fn(router_digest_is_trusted_dir(conn->identity_digest) ?
  801. LOG_WARN : LOG_INFO,
  802. LD_HTTP,
  803. "Received directory with skewed time (server '%s:%d'): "
  804. "we are %d minutes %s, or the directory is %d minutes %s.",
  805. conn->address, conn->port,
  806. abs(delta)/60, delta>0 ? "ahead" : "behind",
  807. abs(delta)/60, delta>0 ? "behind" : "ahead");
  808. skewed = 1; /* don't check the recommended-versions line */
  809. } else {
  810. log_debug(LD_HTTP, "Time on received directory is within tolerance; "
  811. "we are %d seconds skewed. (That's okay.)", delta);
  812. }
  813. }
  814. if (status_code == 503) {
  815. log_info(LD_DIR,"Received http status code %d (%s) from server "
  816. "'%s:%d'. I'll try again soon.",
  817. status_code, escaped(reason), conn->address, conn->port);
  818. tor_free(body); tor_free(headers); tor_free(reason);
  819. return -1;
  820. }
  821. plausible = body_is_plausible(body, body_len, conn->purpose);
  822. if (compression || !plausible) {
  823. char *new_body = NULL;
  824. size_t new_len = 0;
  825. int guessed = detect_compression_method(body, body_len);
  826. if (compression <= 0 || guessed != compression) {
  827. /* Tell the user if we don't believe what we're told about compression.*/
  828. const char *description1, *description2;
  829. if (compression == ZLIB_METHOD)
  830. description1 = "as deflated";
  831. else if (compression == GZIP_METHOD)
  832. description1 = "as gzipped";
  833. else if (compression == 0)
  834. description1 = "as uncompressed";
  835. else
  836. description1 = "with an unknown Content-Encoding";
  837. if (guessed == ZLIB_METHOD)
  838. description2 = "deflated";
  839. else if (guessed == GZIP_METHOD)
  840. description2 = "gzipped";
  841. else if (!plausible)
  842. description2 = "confusing binary junk";
  843. else
  844. description2 = "uncompressed";
  845. log_info(LD_HTTP, "HTTP body from server '%s:%d' was labeled %s, "
  846. "but it seems to be %s.%s",
  847. conn->address, conn->port, description1, description2,
  848. (compression>0 && guessed>0)?" Trying both.":"");
  849. }
  850. /* Try declared compression first if we can. */
  851. if (compression > 0)
  852. tor_gzip_uncompress(&new_body, &new_len, body, body_len, compression,
  853. allow_partial, LOG_PROTOCOL_WARN);
  854. /* Okay, if that didn't work, and we think that it was compressed
  855. * differently, try that. */
  856. if (!new_body && guessed > 0 && compression != guessed)
  857. tor_gzip_uncompress(&new_body, &new_len, body, body_len, guessed,
  858. allow_partial, LOG_PROTOCOL_WARN);
  859. /* If we're pretty sure that we have a compressed directory, and
  860. * we didn't manage to uncompress it, then warn and bail. */
  861. if (!plausible && !new_body) {
  862. log_fn(LOG_PROTOCOL_WARN, LD_HTTP,
  863. "Unable to decompress HTTP body (server '%s:%d').",
  864. conn->address, conn->port);
  865. tor_free(body); tor_free(headers); tor_free(reason);
  866. return -1;
  867. }
  868. if (new_body) {
  869. tor_free(body);
  870. body = new_body;
  871. body_len = new_len;
  872. was_compressed = 1;
  873. }
  874. }
  875. if (conn->purpose == DIR_PURPOSE_FETCH_DIR) {
  876. /* fetch/process the directory to cache it. */
  877. log_info(LD_DIR,"Received directory (size %d) from server '%s:%d'",
  878. (int)body_len, conn->address, conn->port);
  879. if (status_code != 200) {
  880. log_warn(LD_DIR,"Received http status code %d (%s) from server "
  881. "'%s:%d'. I'll try again soon.",
  882. status_code, escaped(reason), conn->address, conn->port);
  883. tor_free(body); tor_free(headers); tor_free(reason);
  884. return -1;
  885. }
  886. if (router_parse_directory(body) < 0) {
  887. log_notice(LD_DIR,"I failed to parse the directory I fetched from "
  888. "'%s:%d'. Ignoring.", conn->address, conn->port);
  889. }
  890. note_request(was_compressed?"dl/dir.z":"dl/dir", orig_len);
  891. }
  892. if (conn->purpose == DIR_PURPOSE_FETCH_RUNNING_LIST) {
  893. /* just update our list of running routers, if this list is new info */
  894. log_info(LD_DIR,"Received running-routers list (size %d)", (int)body_len);
  895. if (status_code != 200) {
  896. log_warn(LD_DIR,"Received http status code %d (%s) from server "
  897. "'%s:%d'. I'll try again soon.",
  898. status_code, escaped(reason), conn->address, conn->port);
  899. tor_free(body); tor_free(headers); tor_free(reason);
  900. return -1;
  901. }
  902. if (router_parse_runningrouters(body)<0) {
  903. log_warn(LD_DIR,
  904. "Bad running-routers from server '%s:%d'. I'll try again soon.",
  905. conn->address, conn->port);
  906. tor_free(body); tor_free(headers); tor_free(reason);
  907. return -1;
  908. }
  909. note_request(was_compressed?"dl/running-routers.z":
  910. "dl/running-routers", orig_len);
  911. }
  912. if (conn->purpose == DIR_PURPOSE_FETCH_NETWORKSTATUS) {
  913. smartlist_t *which = NULL;
  914. char *cp;
  915. log_info(LD_DIR,"Received networkstatus objects (size %d) from server "
  916. "'%s:%d'",(int) body_len, conn->address, conn->port);
  917. if (status_code != 200) {
  918. log_warn(LD_DIR,
  919. "Received http status code %d (%s) from server "
  920. "'%s:%d' while fetching \"/tor/status/%s\". I'll try again soon.",
  921. status_code, escaped(reason), conn->address, conn->port,
  922. conn->requested_resource);
  923. tor_free(body); tor_free(headers); tor_free(reason);
  924. connection_dir_download_networkstatus_failed(conn);
  925. return -1;
  926. }
  927. note_request(was_compressed?"dl/status.z":"dl/status", orig_len);
  928. if (conn->requested_resource &&
  929. !strcmpstart(conn->requested_resource,"fp/")) {
  930. which = smartlist_create();
  931. dir_split_resource_into_fingerprints(conn->requested_resource+3,
  932. which, NULL, 0);
  933. } else if (conn->requested_resource &&
  934. !strcmpstart(conn->requested_resource, "all")) {
  935. which = smartlist_create();
  936. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  937. trusted_dir_server_t *, ds,
  938. {
  939. char *cp = tor_malloc(HEX_DIGEST_LEN+1);
  940. base16_encode(cp, HEX_DIGEST_LEN+1, ds->digest, DIGEST_LEN);
  941. smartlist_add(which, cp);
  942. });
  943. }
  944. cp = body;
  945. while (*cp) {
  946. char *next = strstr(cp, "\nnetwork-status-version");
  947. if (next)
  948. next[1] = '\0';
  949. /* learn from it, and then remove it from 'which' */
  950. if (router_set_networkstatus(cp, time(NULL), NS_FROM_DIR, which)<0)
  951. break;
  952. if (next) {
  953. next[1] = 'n';
  954. cp = next+1;
  955. }
  956. else
  957. break;
  958. }
  959. routers_update_all_from_networkstatus(); /*launches router downloads*/
  960. directory_info_has_arrived(time(NULL), 0);
  961. if (which) {
  962. if (smartlist_len(which)) {
  963. dir_networkstatus_download_failed(which);
  964. }
  965. SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
  966. smartlist_free(which);
  967. }
  968. }
  969. if (conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC) {
  970. smartlist_t *which = NULL;
  971. int n_asked_for = 0;
  972. log_info(LD_DIR,"Received server info (size %d) from server '%s:%d'",
  973. (int)body_len, conn->address, conn->port);
  974. note_request(was_compressed?"dl/server.z":"dl/server", orig_len);
  975. if (conn->requested_resource &&
  976. !strcmpstart(conn->requested_resource,"d/")) {
  977. which = smartlist_create();
  978. dir_split_resource_into_fingerprints(conn->requested_resource+2,
  979. which, NULL, 0);
  980. n_asked_for = smartlist_len(which);
  981. }
  982. if (status_code != 200) {
  983. int dir_okay = status_code == 404 ||
  984. (status_code == 400 && !strcmp(reason, "Servers unavailable."));
  985. /* 404 means that it didn't have them; no big deal.
  986. * Older (pre-0.1.1.8) servers said 400 Servers unavailable instead. */
  987. log_fn(dir_okay ? LOG_INFO : LOG_WARN, LD_DIR,
  988. "Received http status code %d (%s) from server '%s:%d' "
  989. "while fetching \"/tor/server/%s\". I'll try again soon.",
  990. status_code, escaped(reason), conn->address, conn->port,
  991. conn->requested_resource);
  992. if (!which) {
  993. connection_dir_download_routerdesc_failed(conn);
  994. } else {
  995. dir_routerdesc_download_failed(which);
  996. SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
  997. smartlist_free(which);
  998. }
  999. tor_free(body); tor_free(headers); tor_free(reason);
  1000. return dir_okay ? 0 : -1;
  1001. }
  1002. /* Learn the routers, assuming we requested by fingerprint or "all".
  1003. * Right now, we only use "authority" to fetch ourself, so we don't want
  1004. * to risk replacing ourself with a router running at the addr:port we
  1005. * think we have.
  1006. */
  1007. if (which || (conn->requested_resource &&
  1008. !strcmpstart(conn->requested_resource, "all"))) {
  1009. /* as we learn from them, we remove them from 'which' */
  1010. router_load_routers_from_string(body, 0, which);
  1011. directory_info_has_arrived(time(NULL), 0);
  1012. }
  1013. if (which) { /* mark remaining ones as failed */
  1014. log_info(LD_DIR, "Received %d/%d routers requested from %s:%d",
  1015. n_asked_for-smartlist_len(which), n_asked_for,
  1016. conn->address, (int)conn->port);
  1017. if (smartlist_len(which)) {
  1018. dir_routerdesc_download_failed(which);
  1019. }
  1020. SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
  1021. smartlist_free(which);
  1022. }
  1023. if (conn->requested_resource &&
  1024. !strcmpstart(conn->requested_resource,"authority")) {
  1025. /* this might have been a dirport reachability test. see if it is. */
  1026. routerinfo_t *me = router_get_my_routerinfo();
  1027. if (me &&
  1028. router_digest_is_me(conn->identity_digest) &&
  1029. me->addr == conn->addr &&
  1030. me->dir_port == conn->port)
  1031. router_dirport_found_reachable();
  1032. }
  1033. }
  1034. if (conn->purpose == DIR_PURPOSE_UPLOAD_DIR) {
  1035. switch (status_code) {
  1036. case 200:
  1037. log_info(LD_GENERAL,"eof (status 200) after uploading server "
  1038. "descriptor: finished.");
  1039. break;
  1040. case 400:
  1041. log_warn(LD_GENERAL,"http status 400 (%s) response from "
  1042. "dirserver '%s:%d'. Please correct.",
  1043. escaped(reason), conn->address, conn->port);
  1044. break;
  1045. case 403:
  1046. log_warn(LD_GENERAL,
  1047. "http status 403 (%s) response from dirserver "
  1048. "'%s:%d'. Is your clock skewed? Have you mailed us your key "
  1049. "fingerprint? Are you using the right key? Are you using a "
  1050. "private IP address? See http://tor.eff.org/doc/"
  1051. "tor-doc-server.html",escaped(reason), conn->address, conn->port);
  1052. break;
  1053. default:
  1054. log_warn(LD_GENERAL,
  1055. "http status %d (%s) reason unexpected (server '%s:%d').",
  1056. status_code, escaped(reason), conn->address, conn->port);
  1057. break;
  1058. }
  1059. /* return 0 in all cases, since we don't want to mark any
  1060. * dirservers down just because they don't like us. */
  1061. }
  1062. if (conn->purpose == DIR_PURPOSE_FETCH_RENDDESC) {
  1063. log_info(LD_REND,"Received rendezvous descriptor (size %d, status %d "
  1064. "(%s))",
  1065. (int)body_len, status_code, escaped(reason));
  1066. switch (status_code) {
  1067. case 200:
  1068. if (rend_cache_store(body, body_len) < 0) {
  1069. log_warn(LD_REND,"Failed to store rendezvous descriptor.");
  1070. /* alice's ap_stream will notice when connection_mark_for_close
  1071. * cleans it up */
  1072. } else {
  1073. /* success. notify pending connections about this. */
  1074. conn->purpose = DIR_PURPOSE_HAS_FETCHED_RENDDESC;
  1075. rend_client_desc_here(conn->rend_query);
  1076. }
  1077. break;
  1078. case 404:
  1079. /* not there. pending connections will be notified when
  1080. * connection_mark_for_close cleans it up. */
  1081. break;
  1082. case 400:
  1083. log_warn(LD_REND,
  1084. "http status 400 (%s). Dirserver didn't like our "
  1085. "rendezvous query?", escaped(reason));
  1086. break;
  1087. default:
  1088. log_warn(LD_REND,"http status %d (%s) response unexpected (server "
  1089. "'%s:%d').",
  1090. status_code, escaped(reason), conn->address, conn->port);
  1091. break;
  1092. }
  1093. }
  1094. if (conn->purpose == DIR_PURPOSE_UPLOAD_RENDDESC) {
  1095. switch (status_code) {
  1096. case 200:
  1097. log_info(LD_REND,
  1098. "Uploading rendezvous descriptor: finished with status "
  1099. "200 (%s)", escaped(reason));
  1100. break;
  1101. case 400:
  1102. log_warn(LD_REND,"http status 400 (%s) response from dirserver "
  1103. "'%s:%d'. Malformed rendezvous descriptor?",
  1104. escaped(reason), conn->address, conn->port);
  1105. break;
  1106. default:
  1107. log_warn(LD_REND,"http status %d (%s) response unexpected (server "
  1108. "'%s:%d').",
  1109. status_code, escaped(reason), conn->address, conn->port);
  1110. break;
  1111. }
  1112. }
  1113. tor_free(body); tor_free(headers); tor_free(reason);
  1114. return 0;
  1115. }
  1116. /** Called when a directory connection reaches EOF */
  1117. int
  1118. connection_dir_reached_eof(connection_t *conn)
  1119. {
  1120. int retval;
  1121. if (conn->state != DIR_CONN_STATE_CLIENT_READING) {
  1122. log_info(LD_HTTP,"conn reached eof, not reading. Closing.");
  1123. connection_close_immediate(conn); /* error: give up on flushing */
  1124. connection_mark_for_close(conn);
  1125. return -1;
  1126. }
  1127. retval = connection_dir_client_reached_eof(conn);
  1128. if (retval == 0) /* success */
  1129. conn->state = DIR_CONN_STATE_CLIENT_FINISHED;
  1130. connection_mark_for_close(conn);
  1131. return retval;
  1132. }
  1133. /** Read handler for directory connections. (That's connections <em>to</em>
  1134. * directory servers and connections <em>at</em> directory servers.)
  1135. */
  1136. int
  1137. connection_dir_process_inbuf(connection_t *conn)
  1138. {
  1139. tor_assert(conn);
  1140. tor_assert(conn->type == CONN_TYPE_DIR);
  1141. /* Directory clients write, then read data until they receive EOF;
  1142. * directory servers read data until they get an HTTP command, then
  1143. * write their response (when it's finished flushing, they mark for
  1144. * close).
  1145. */
  1146. /* If we're on the dirserver side, look for a command. */
  1147. if (conn->state == DIR_CONN_STATE_SERVER_COMMAND_WAIT) {
  1148. if (directory_handle_command(conn) < 0) {
  1149. connection_mark_for_close(conn);
  1150. return -1;
  1151. }
  1152. return 0;
  1153. }
  1154. /* XXX for READ states, might want to make sure inbuf isn't too big */
  1155. if (!conn->inbuf_reached_eof)
  1156. log_debug(LD_HTTP,"Got data, not eof. Leaving on inbuf.");
  1157. return 0;
  1158. }
  1159. /** Create an http response for the client <b>conn</b> out of
  1160. * <b>status</b> and <b>reason_phrase</b>. Write it to <b>conn</b>.
  1161. */
  1162. static void
  1163. write_http_status_line(connection_t *conn, int status,
  1164. const char *reason_phrase)
  1165. {
  1166. char buf[256];
  1167. if (tor_snprintf(buf, sizeof(buf), "HTTP/1.0 %d %s\r\n\r\n",
  1168. status, reason_phrase) < 0) {
  1169. log_warn(LD_BUG,"Bug: status line too long.");
  1170. return;
  1171. }
  1172. connection_write_to_buf(buf, strlen(buf), conn);
  1173. }
  1174. /** Helper function: return 1 if there are any dir conns of purpose
  1175. * <b>purpose</b> that are going elsewhere than our own ORPort/Dirport.
  1176. * Else return 0.
  1177. */
  1178. static int
  1179. already_fetching_directory(int purpose)
  1180. {
  1181. int i, n;
  1182. connection_t *conn;
  1183. connection_t **carray;
  1184. get_connection_array(&carray,&n);
  1185. for (i=0;i<n;i++) {
  1186. conn = carray[i];
  1187. if (conn->type == CONN_TYPE_DIR &&
  1188. conn->purpose == purpose &&
  1189. !conn->marked_for_close &&
  1190. !router_digest_is_me(conn->identity_digest))
  1191. return 1;
  1192. }
  1193. return 0;
  1194. }
  1195. #undef INSTRUMENT_DOWNLOADS
  1196. #ifdef INSTRUMENT_DOWNLOADS
  1197. /** DOCDOC */
  1198. static strmap_t *request_bytes_map = NULL;
  1199. /** DOCDOC */
  1200. static void
  1201. note_request(const char *key, size_t bytes)
  1202. {
  1203. uint64_t *n;
  1204. if (!request_bytes_map)
  1205. request_bytes_map = strmap_new();
  1206. n = strmap_get(request_bytes_map, key);
  1207. if (!n) {
  1208. n = tor_malloc_zero(sizeof(uint64_t));
  1209. strmap_set(request_bytes_map, key, n);
  1210. }
  1211. *n += bytes;
  1212. }
  1213. /** DOCDOC */
  1214. char *
  1215. directory_dump_request_log(void)
  1216. {
  1217. smartlist_t *lines;
  1218. char tmp[256];
  1219. char *result;
  1220. strmap_iter_t *iter;
  1221. if (!request_bytes_map)
  1222. request_bytes_map = strmap_new();
  1223. lines = smartlist_create();
  1224. for (iter = strmap_iter_init(request_bytes_map);
  1225. !strmap_iter_done(iter);
  1226. iter = strmap_iter_next(request_bytes_map, iter)) {
  1227. const char *key;
  1228. void *val;
  1229. uint64_t *n;
  1230. strmap_iter_get(iter, &key, &val);
  1231. n = val;
  1232. tor_snprintf(tmp, sizeof(tmp), "%s "U64_FORMAT"\n",
  1233. key, U64_PRINTF_ARG(*n));
  1234. smartlist_add(lines, tor_strdup(tmp));
  1235. }
  1236. smartlist_sort_strings(lines);
  1237. result = smartlist_join_strings(lines, "", 0, NULL);
  1238. SMARTLIST_FOREACH(lines, char *, cp, tor_free(cp));
  1239. smartlist_free(lines);
  1240. return result;
  1241. }
  1242. #else
  1243. static void
  1244. note_request(const char *key, size_t bytes)
  1245. {
  1246. (void)key;
  1247. (void)bytes;
  1248. }
  1249. char *
  1250. directory_dump_request_log(void)
  1251. {
  1252. return tor_strdup("Not supported.");
  1253. }
  1254. #endif
  1255. /** Helper function: called when a dirserver gets a complete HTTP GET
  1256. * request. Look for a request for a directory or for a rendezvous
  1257. * service descriptor. On finding one, write a response into
  1258. * conn-\>outbuf. If the request is unrecognized, send a 400.
  1259. * Always return 0. */
  1260. static int
  1261. directory_handle_command_get(connection_t *conn, char *headers,
  1262. char *body, size_t body_len)
  1263. {
  1264. size_t dlen;
  1265. const char *cp;
  1266. char *url = NULL;
  1267. char tmp[8192];
  1268. char date[RFC1123_TIME_LEN+1];
  1269. /* We ignore the body of a GET request. */
  1270. (void)body;
  1271. (void)body_len;
  1272. log_debug(LD_DIRSERV,"Received GET command.");
  1273. conn->state = DIR_CONN_STATE_SERVER_WRITING;
  1274. if (parse_http_url(headers, &url) < 0) {
  1275. write_http_status_line(conn, 400, "Bad request");
  1276. return 0;
  1277. }
  1278. log_debug(LD_DIRSERV,"rewritten url as '%s'.", url);
  1279. if (!strcmp(url,"/tor/") || !strcmp(url,"/tor/dir.z")) { /* dir fetch */
  1280. int deflated = !strcmp(url,"/tor/dir.z");
  1281. dlen = dirserv_get_directory(&cp, deflated);
  1282. if (dlen == 0) {
  1283. log_notice(LD_DIRSERV,"Client asked for the mirrored directory, but we "
  1284. "don't have a good one yet. Sending 503 Dir not available.");
  1285. write_http_status_line(conn, 503, "Directory unavailable");
  1286. /* try to get a new one now */
  1287. if (!already_fetching_directory(DIR_PURPOSE_FETCH_DIR))
  1288. directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL, 1);
  1289. tor_free(url);
  1290. return 0;
  1291. }
  1292. if (global_write_bucket_empty()) {
  1293. log_info(LD_DIRSERV,
  1294. "Client asked for the mirrored directory, but we've been "
  1295. "writing too many bytes lately. Sending 503 Dir busy.");
  1296. write_http_status_line(conn, 503, "Directory busy, try again later");
  1297. tor_free(url);
  1298. return 0;
  1299. }
  1300. note_request(url, dlen);
  1301. tor_free(url);
  1302. log_debug(LD_DIRSERV,"Dumping %sdirectory to client.",
  1303. deflated?"deflated ":"");
  1304. format_rfc1123_time(date, time(NULL));
  1305. tor_snprintf(tmp, sizeof(tmp),
  1306. "HTTP/1.0 200 OK\r\nDate: %s\r\nContent-Length: %d\r\n"
  1307. "Content-Type: %s\r\nContent-Encoding: %s\r\n\r\n",
  1308. date,
  1309. (int)dlen,
  1310. deflated?"application/octet-stream":"text/plain",
  1311. deflated?"deflate":"identity");
  1312. connection_write_to_buf(tmp, strlen(tmp), conn);
  1313. connection_write_to_buf(cp, dlen, conn);
  1314. return 0;
  1315. }
  1316. if (!strcmp(url,"/tor/running-routers") ||
  1317. !strcmp(url,"/tor/running-routers.z")) { /* running-routers fetch */
  1318. int deflated = !strcmp(url,"/tor/running-routers.z");
  1319. dlen = dirserv_get_runningrouters(&cp, deflated);
  1320. note_request(url, dlen);
  1321. tor_free(url);
  1322. if (!dlen) { /* we failed to create/cache cp */
  1323. write_http_status_line(conn, 503, "Directory unavailable");
  1324. /* try to get a new one now */
  1325. if (!already_fetching_directory(DIR_PURPOSE_FETCH_RUNNING_LIST))
  1326. directory_get_from_dirserver(DIR_PURPOSE_FETCH_RUNNING_LIST, NULL, 1);
  1327. return 0;
  1328. }
  1329. format_rfc1123_time(date, time(NULL));
  1330. tor_snprintf(tmp, sizeof(tmp),
  1331. "HTTP/1.0 200 OK\r\nDate: %s\r\nContent-Length: %d\r\n"
  1332. "Content-Type: %s\r\nContent-Encoding: %s\r\n\r\n",
  1333. date,
  1334. (int)dlen,
  1335. deflated?"application/octet-stream":"text/plain",
  1336. deflated?"deflate":"identity");
  1337. connection_write_to_buf(tmp, strlen(tmp), conn);
  1338. connection_write_to_buf(cp, strlen(cp), conn);
  1339. return 0;
  1340. }
  1341. if (!strcmpstart(url,"/tor/status/")) {
  1342. /* v2 network status fetch. */
  1343. size_t url_len = strlen(url);
  1344. int deflated = !strcmp(url+url_len-2, ".z");
  1345. smartlist_t *dir_objs = smartlist_create();
  1346. const char *request_type = NULL;
  1347. const char *key = url + strlen("/tor/status/");
  1348. if (deflated)
  1349. url[url_len-2] = '\0';
  1350. dirserv_get_networkstatus_v2(dir_objs, key);
  1351. if (!strcmpstart(key, "fp/"))
  1352. request_type = deflated?"/tor/status/fp.z":"/tor/status/fp";
  1353. else if (!strcmpstart(key, "authority"))
  1354. request_type = deflated?"/tor/status/authority.z":
  1355. "/tor/status/authority";
  1356. else if (!strcmpstart(key, "all"))
  1357. request_type = deflated?"/tor/status/all.z":"/tor/status/all";
  1358. else
  1359. request_type = "/tor/status/?";
  1360. tor_free(url);
  1361. if (!smartlist_len(dir_objs)) { /* we failed to create/cache cp */
  1362. write_http_status_line(conn, 503, "Network status object unavailable");
  1363. smartlist_free(dir_objs);
  1364. return 0;
  1365. }
  1366. dlen = 0;
  1367. SMARTLIST_FOREACH(dir_objs, cached_dir_t *, d,
  1368. dlen += deflated?d->dir_z_len:d->dir_len);
  1369. note_request(request_type,dlen);
  1370. format_rfc1123_time(date, time(NULL));
  1371. tor_snprintf(tmp, sizeof(tmp),
  1372. "HTTP/1.0 200 OK\r\nDate: %s\r\nContent-Length: %d\r\n"
  1373. "Content-Type: %s\r\nContent-Encoding: %s\r\n\r\n",
  1374. date,
  1375. (int)dlen,
  1376. deflated?"application/octet-stream":"text/plain",
  1377. deflated?"deflate":"identity");
  1378. connection_write_to_buf(tmp, strlen(tmp), conn);
  1379. SMARTLIST_FOREACH(dir_objs, cached_dir_t *, d,
  1380. {
  1381. if (deflated)
  1382. connection_write_to_buf(d->dir_z, d->dir_z_len, conn);
  1383. else
  1384. connection_write_to_buf(d->dir, d->dir_len, conn);
  1385. });
  1386. smartlist_free(dir_objs);
  1387. return 0;
  1388. }
  1389. if (!strcmpstart(url,"/tor/server/")) {
  1390. size_t url_len = strlen(url);
  1391. int deflated = !strcmp(url+url_len-2, ".z");
  1392. int res;
  1393. const char *msg;
  1394. smartlist_t *descs = smartlist_create();
  1395. const char *request_type = NULL;
  1396. if (deflated)
  1397. url[url_len-2] = '\0';
  1398. res = dirserv_get_routerdescs(descs, url, &msg);
  1399. if (!strcmpstart(url, "/tor/server/fp/"))
  1400. request_type = deflated?"/tor/server/fp.z":"/tor/server/fp";
  1401. else if (!strcmpstart(url, "/tor/server/authority"))
  1402. request_type = deflated?"/tor/server/authority.z":
  1403. "/tor/server/authority";
  1404. else if (!strcmpstart(url, "/tor/server/all"))
  1405. request_type = deflated?"/tor/server/all.z":"/tor/server/all";
  1406. else if (!strcmpstart(url, "/tor/server/d/"))
  1407. request_type = deflated?"/tor/server/d.z":"/tor/server/d";
  1408. else
  1409. request_type = "/tor/server/?";
  1410. tor_free(url);
  1411. if (res < 0)
  1412. write_http_status_line(conn, 404, msg);
  1413. else {
  1414. size_t len = 0;
  1415. format_rfc1123_time(date, time(NULL));
  1416. SMARTLIST_FOREACH(descs, signed_descriptor_t *, ri,
  1417. len += ri->signed_descriptor_len);
  1418. if (deflated) {
  1419. size_t compressed_len;
  1420. char *compressed;
  1421. char *inp = tor_malloc(len+smartlist_len(descs)+1);
  1422. char *cp = inp;
  1423. SMARTLIST_FOREACH(descs, signed_descriptor_t *, ri,
  1424. {
  1425. const char *body = signed_descriptor_get_body(ri);
  1426. memcpy(cp, body, ri->signed_descriptor_len);
  1427. cp += ri->signed_descriptor_len;
  1428. *cp++ = '\n';
  1429. });
  1430. *cp = '\0';
  1431. /* XXXX This could be way more efficiently handled; let's see if it
  1432. * shows up under oprofile. */
  1433. if (tor_gzip_compress(&compressed, &compressed_len,
  1434. inp, cp-inp, ZLIB_METHOD)<0) {
  1435. tor_free(inp);
  1436. smartlist_free(descs);
  1437. return -1;
  1438. }
  1439. tor_free(inp);
  1440. tor_snprintf(tmp, sizeof(tmp),
  1441. "HTTP/1.0 200 OK\r\nDate: %s\r\nContent-Length: %d\r\n"
  1442. "Content-Type: application/octet-stream\r\n"
  1443. "Content-Encoding: deflate\r\n\r\n",
  1444. date,
  1445. (int)compressed_len);
  1446. note_request(request_type, compressed_len);
  1447. connection_write_to_buf(tmp, strlen(tmp), conn);
  1448. connection_write_to_buf(compressed, compressed_len, conn);
  1449. tor_free(compressed);
  1450. } else {
  1451. tor_snprintf(tmp, sizeof(tmp),
  1452. "HTTP/1.0 200 OK\r\nDate: %s\r\nContent-Length: %d\r\n"
  1453. "Content-Type: text/plain\r\n\r\n",
  1454. date,
  1455. (int)len);
  1456. note_request(request_type, len);
  1457. connection_write_to_buf(tmp, strlen(tmp), conn);
  1458. SMARTLIST_FOREACH(descs, signed_descriptor_t *, ri,
  1459. {
  1460. const char *body = signed_descriptor_get_body(ri);
  1461. connection_write_to_buf(body, ri->signed_descriptor_len, conn);
  1462. });
  1463. }
  1464. }
  1465. smartlist_free(descs);
  1466. return 0;
  1467. }
  1468. if (!strcmpstart(url,"/tor/rendezvous/") ||
  1469. !strcmpstart(url,"/tor/rendezvous1/")) {
  1470. /* rendezvous descriptor fetch */
  1471. const char *descp;
  1472. size_t desc_len;
  1473. int versioned = !strcmpstart(url,"/tor/rendezvous1/");
  1474. const char *query = url+strlen("/tor/rendezvous/")+(versioned?1:0);
  1475. if (!authdir_mode(get_options())) {
  1476. /* We don't hand out rend descs. In fact, it could be a security
  1477. * risk, since rend_cache_lookup_desc() below would provide it
  1478. * if we're gone to the site recently, and 404 if we haven't.
  1479. *
  1480. * Reject. */
  1481. write_http_status_line(conn, 400, "Nonauthoritative directory does not "
  1482. "store rendezvous descriptors");
  1483. tor_free(url);
  1484. return 0;
  1485. }
  1486. switch (rend_cache_lookup_desc(query, versioned?-1:0, &descp, &desc_len)) {
  1487. case 1: /* valid */
  1488. format_rfc1123_time(date, time(NULL));
  1489. tor_snprintf(tmp, sizeof(tmp),
  1490. "HTTP/1.0 200 OK\r\nDate: %s\r\nContent-Length: %d\r\n"
  1491. "Content-Type: application/octet-stream\r\n\r\n",
  1492. date,
  1493. (int)desc_len);
  1494. note_request("/tor/rendezvous?/", desc_len);
  1495. connection_write_to_buf(tmp, strlen(tmp), conn);
  1496. /* need to send descp separately, because it may include nuls */
  1497. connection_write_to_buf(descp, desc_len, conn);
  1498. break;
  1499. case 0: /* well-formed but not present */
  1500. write_http_status_line(conn, 404, "Not found");
  1501. break;
  1502. case -1: /* not well-formed */
  1503. write_http_status_line(conn, 400, "Bad request");
  1504. break;
  1505. }
  1506. tor_free(url);
  1507. return 0;
  1508. }
  1509. if (!strcmpstart(url,"/tor/bytes.txt")) {
  1510. char *bytes = directory_dump_request_log();
  1511. size_t len = strlen(bytes);
  1512. format_rfc1123_time(date, time(NULL));
  1513. tor_snprintf(tmp, sizeof(tmp),
  1514. "HTTP/1.0 200 OK\r\nDate: %s\r\nContent-Length: %d\r\n"
  1515. "Content-Type: text/plain\r\n\r\n",
  1516. date,
  1517. (int)len);
  1518. connection_write_to_buf(tmp, strlen(tmp), conn);
  1519. connection_write_to_buf(bytes, len, conn);
  1520. tor_free(bytes);
  1521. tor_free(url);
  1522. return 0;
  1523. }
  1524. if (!strcmp(url,"/tor/robots.txt")) { /* /robots.txt will have been
  1525. rewritten to /tor/robots.txt */
  1526. char robots[] = "User-agent: *\r\nDisallow: /\r\n";
  1527. size_t len = strlen(robots);
  1528. format_rfc1123_time(date, time(NULL));
  1529. tor_snprintf(tmp, sizeof(tmp),
  1530. "HTTP/1.0 200 OK\r\nDate: %s\r\nContent-Length: %d\r\n"
  1531. "Content-Type: text/plain\r\n\r\n",
  1532. date,
  1533. (int)len);
  1534. connection_write_to_buf(tmp, strlen(tmp), conn);
  1535. connection_write_to_buf(robots, len, conn);
  1536. tor_free(url);
  1537. return 0;
  1538. }
  1539. if (!strcmp(url,"/tor/dir-all-weaselhack") && (conn->addr == 0x7f000001ul) &&
  1540. authdir_mode(get_options())) {
  1541. /* XXX until weasel rewrites his scripts XXXX012 */
  1542. char *new_directory=NULL;
  1543. if (dirserv_dump_directory_to_string(&new_directory,
  1544. get_identity_key(), 1)) {
  1545. log_warn(LD_BUG, "Error creating full v1 directory.");
  1546. tor_free(new_directory);
  1547. write_http_status_line(conn, 503, "Directory unavailable");
  1548. return 0;
  1549. }
  1550. dlen = strlen(new_directory);
  1551. format_rfc1123_time(date, time(NULL));
  1552. tor_snprintf(tmp, sizeof(tmp),
  1553. "HTTP/1.0 200 OK\r\nDate: %s\r\nContent-Length: %d\r\n"
  1554. "Content-Type: text/plain\r\nContent-Encoding: identity\r\n\r\n",
  1555. date,
  1556. (int)dlen);
  1557. connection_write_to_buf(tmp, strlen(tmp), conn);
  1558. connection_write_to_buf(new_directory, dlen, conn);
  1559. tor_free(new_directory);
  1560. }
  1561. /* we didn't recognize the url */
  1562. write_http_status_line(conn, 404, "Not found");
  1563. tor_free(url);
  1564. return 0;
  1565. }
  1566. /** Helper function: called when a dirserver gets a complete HTTP POST
  1567. * request. Look for an uploaded server descriptor or rendezvous
  1568. * service descriptor. On finding one, process it and write a
  1569. * response into conn-\>outbuf. If the request is unrecognized, send a
  1570. * 400. Always return 0. */
  1571. static int
  1572. directory_handle_command_post(connection_t *conn, char *headers,
  1573. char *body, size_t body_len)
  1574. {
  1575. const char *cp;
  1576. char *origin = NULL;
  1577. char *url = NULL;
  1578. log_debug(LD_DIRSERV,"Received POST command.");
  1579. conn->state = DIR_CONN_STATE_SERVER_WRITING;
  1580. if (!authdir_mode(get_options())) {
  1581. /* we just provide cached directories; we don't want to
  1582. * receive anything. */
  1583. write_http_status_line(conn, 400, "Nonauthoritative directory does not "
  1584. "accept posted server descriptors");
  1585. return 0;
  1586. }
  1587. if (parse_http_url(headers, &url) < 0) {
  1588. write_http_status_line(conn, 400, "Bad request");
  1589. return 0;
  1590. }
  1591. log_debug(LD_DIRSERV,"rewritten url as '%s'.", url);
  1592. origin = http_get_origin(headers, conn);
  1593. if (!strcmp(url,"/tor/")) { /* server descriptor post */
  1594. const char *msg;
  1595. int r = dirserv_add_descriptor(body, &msg);
  1596. tor_assert(msg);
  1597. if (r > 0)
  1598. dirserv_get_directory(&cp, 0); /* rebuild and write to disk */
  1599. switch (r) {
  1600. case -2:
  1601. case -1:
  1602. case 1:
  1603. log_notice(LD_DIRSERV,"Rejected router descriptor from %s.", origin);
  1604. /* malformed descriptor, or something wrong */
  1605. write_http_status_line(conn, 400, msg);
  1606. break;
  1607. case 0: /* accepted but discarded */
  1608. case 2: /* accepted */
  1609. write_http_status_line(conn, 200, msg);
  1610. break;
  1611. }
  1612. goto done;
  1613. }
  1614. if (!strcmpstart(url,"/tor/rendezvous/publish")) {
  1615. /* rendezvous descriptor post */
  1616. if (rend_cache_store(body, body_len) < 0) {
  1617. // char tmp[1024*2+1];
  1618. log_fn(LOG_PROTOCOL_WARN, LD_DIRSERV,
  1619. "Rejected rend descriptor (length %d) from %s.",
  1620. (int)body_len, origin);
  1621. #if 0
  1622. if (body_len <= 1024) {
  1623. base16_encode(tmp, sizeof(tmp), body, body_len);
  1624. log_notice(LD_DIRSERV,"Body was: %s", escaped(tmp));
  1625. }
  1626. #endif
  1627. write_http_status_line(conn, 400, "Invalid service descriptor rejected");
  1628. } else {
  1629. write_http_status_line(conn, 200, "Service descriptor stored");
  1630. }
  1631. goto done;
  1632. }
  1633. /* we didn't recognize the url */
  1634. write_http_status_line(conn, 404, "Not found");
  1635. done:
  1636. tor_free(url);
  1637. tor_free(origin);
  1638. return 0;
  1639. }
  1640. /** Called when a dirserver receives data on a directory connection;
  1641. * looks for an HTTP request. If the request is complete, remove it
  1642. * from the inbuf, try to process it; otherwise, leave it on the
  1643. * buffer. Return a 0 on success, or -1 on error.
  1644. */
  1645. static int
  1646. directory_handle_command(connection_t *conn)
  1647. {
  1648. char *headers=NULL, *body=NULL;
  1649. size_t body_len=0;
  1650. int r;
  1651. tor_assert(conn);
  1652. tor_assert(conn->type == CONN_TYPE_DIR);
  1653. switch (fetch_from_buf_http(conn->inbuf,
  1654. &headers, MAX_HEADERS_SIZE,
  1655. &body, &body_len, MAX_BODY_SIZE, 0)) {
  1656. case -1: /* overflow */
  1657. log_warn(LD_DIRSERV,
  1658. "Invalid input from address '%s'. Closing.", conn->address);
  1659. return -1;
  1660. case 0:
  1661. log_debug(LD_DIRSERV,"command not all here yet.");
  1662. return 0;
  1663. /* case 1, fall through */
  1664. }
  1665. //log_debug(LD_DIRSERV,"headers %s, body %s.", headers, body);
  1666. if (!strncasecmp(headers,"GET",3))
  1667. r = directory_handle_command_get(conn, headers, body, body_len);
  1668. else if (!strncasecmp(headers,"POST",4))
  1669. r = directory_handle_command_post(conn, headers, body, body_len);
  1670. else {
  1671. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  1672. "Got headers %s with unknown command. Closing.",
  1673. escaped(headers));
  1674. r = -1;
  1675. }
  1676. tor_free(headers); tor_free(body);
  1677. return r;
  1678. }
  1679. /** Write handler for directory connections; called when all data has
  1680. * been flushed. Close the connection or wait for a response as
  1681. * appropriate.
  1682. */
  1683. int
  1684. connection_dir_finished_flushing(connection_t *conn)
  1685. {
  1686. tor_assert(conn);
  1687. tor_assert(conn->type == CONN_TYPE_DIR);
  1688. switch (conn->state) {
  1689. case DIR_CONN_STATE_CLIENT_SENDING:
  1690. log_debug(LD_DIR,"client finished sending command.");
  1691. conn->state = DIR_CONN_STATE_CLIENT_READING;
  1692. connection_stop_writing(conn);
  1693. return 0;
  1694. case DIR_CONN_STATE_SERVER_WRITING:
  1695. log_debug(LD_DIRSERV,"Finished writing server response. Closing.");
  1696. connection_mark_for_close(conn);
  1697. return 0;
  1698. default:
  1699. log_warn(LD_BUG,"Bug: called in unexpected state %d.", conn->state);
  1700. tor_fragile_assert();
  1701. return -1;
  1702. }
  1703. return 0;
  1704. }
  1705. /** Connected handler for directory connections: begin sending data to the
  1706. * server */
  1707. int
  1708. connection_dir_finished_connecting(connection_t *conn)
  1709. {
  1710. tor_assert(conn);
  1711. tor_assert(conn->type == CONN_TYPE_DIR);
  1712. tor_assert(conn->state == DIR_CONN_STATE_CONNECTING);
  1713. log_debug(LD_HTTP,"Dir connection to router %s:%u established.",
  1714. conn->address,conn->port);
  1715. conn->state = DIR_CONN_STATE_CLIENT_SENDING; /* start flushing conn */
  1716. return 0;
  1717. }
  1718. /** Called when one or more networkstatus fetches have failed (with uppercase
  1719. * fingerprints listed in <b>failed</>). Mark those fingerprints as having
  1720. * failed once. */
  1721. static void
  1722. dir_networkstatus_download_failed(smartlist_t *failed)
  1723. {
  1724. SMARTLIST_FOREACH(failed, const char *, fp,
  1725. {
  1726. char digest[DIGEST_LEN];
  1727. trusted_dir_server_t *dir;
  1728. base16_decode(digest, DIGEST_LEN, fp, strlen(fp));
  1729. dir = router_get_trusteddirserver_by_digest(digest);
  1730. if (dir)
  1731. ++dir->n_networkstatus_failures;
  1732. });
  1733. }
  1734. /** Called when one or more routerdesc fetches have failed (with uppercase
  1735. * fingerprints listed in <b>failed</b>). */
  1736. static void
  1737. dir_routerdesc_download_failed(smartlist_t *failed)
  1738. {
  1739. char digest[DIGEST_LEN];
  1740. local_routerstatus_t *rs;
  1741. time_t now = time(NULL);
  1742. int server = server_mode(get_options()) && get_options()->DirPort;
  1743. SMARTLIST_FOREACH(failed, const char *, cp,
  1744. {
  1745. base16_decode(digest, DIGEST_LEN, cp, strlen(cp));
  1746. rs = router_get_combined_status_by_digest(digest);
  1747. if (!rs || rs->n_download_failures >= MAX_ROUTERDESC_DOWNLOAD_FAILURES)
  1748. continue;
  1749. ++rs->n_download_failures;
  1750. if (server) {
  1751. switch (rs->n_download_failures) {
  1752. case 1: rs->next_attempt_at = 0; break;
  1753. case 2: rs->next_attempt_at = 0; break;
  1754. case 3: rs->next_attempt_at = now+60; break;
  1755. case 4: rs->next_attempt_at = now+60; break;
  1756. case 5: rs->next_attempt_at = now+60*2; break;
  1757. case 6: rs->next_attempt_at = now+60*5; break;
  1758. case 7: rs->next_attempt_at = now+60*15; break;
  1759. default: rs->next_attempt_at = TIME_MAX; break;
  1760. }
  1761. } else {
  1762. switch (rs->n_download_failures) {
  1763. case 1: rs->next_attempt_at = 0; break;
  1764. case 2: rs->next_attempt_at = now+60; break;
  1765. case 3: rs->next_attempt_at = now+60*5; break;
  1766. case 4: rs->next_attempt_at = now+60*10; break;
  1767. default: rs->next_attempt_at = TIME_MAX; break;
  1768. }
  1769. }
  1770. if (rs->next_attempt_at == 0)
  1771. log_debug(LD_DIR, "%s failed %d time(s); I'll try again immediately.",
  1772. cp, (int)rs->n_download_failures);
  1773. else if (rs->next_attempt_at < TIME_MAX)
  1774. log_debug(LD_DIR, "%s failed %d time(s); I'll try again in %d seconds.",
  1775. cp, (int)rs->n_download_failures,
  1776. (int)(rs->next_attempt_at-now));
  1777. else
  1778. log_debug(LD_DIR, "%s failed %d time(s); Giving up for a while.",
  1779. cp, (int)rs->n_download_failures);
  1780. });
  1781. /* update_router_descriptor_downloads(time(NULL)); */
  1782. }
  1783. /* Given a directory <b>resource</b> request, containing zero
  1784. * or more strings separated by plus signs, followed optionally by ".z", store
  1785. * the strings, in order, into <b>fp_out</b>. If <b>compressed_out</b> is
  1786. * non-NULL, set it to 1 if the resource ends in ".z", else set it to 0. If
  1787. * decode_hex is true, then delete all elements that aren't hex digests, and
  1788. * decode the rest.
  1789. */
  1790. int
  1791. dir_split_resource_into_fingerprints(const char *resource,
  1792. smartlist_t *fp_out, int *compressed_out,
  1793. int decode_hex)
  1794. {
  1795. int old_len;
  1796. tor_assert(fp_out);
  1797. old_len = smartlist_len(fp_out);
  1798. smartlist_split_string(fp_out, resource, "+", 0, 0);
  1799. if (compressed_out)
  1800. *compressed_out = 0;
  1801. if (smartlist_len(fp_out) > old_len) {
  1802. char *last = smartlist_get(fp_out,smartlist_len(fp_out)-1);
  1803. size_t last_len = strlen(last);
  1804. if (last_len > 2 && !strcmp(last+last_len-2, ".z")) {
  1805. last[last_len-2] = '\0';
  1806. if (compressed_out)
  1807. *compressed_out = 1;
  1808. }
  1809. }
  1810. if (decode_hex) {
  1811. int i;
  1812. char *cp, *d = NULL;
  1813. for (i = old_len; i < smartlist_len(fp_out); ++i) {
  1814. cp = smartlist_get(fp_out, i);
  1815. if (strlen(cp) != HEX_DIGEST_LEN) {
  1816. log_info(LD_DIR,
  1817. "Skipping digest %s with non-standard length.", escaped(cp));
  1818. smartlist_del(fp_out, i--);
  1819. goto again;
  1820. }
  1821. d = tor_malloc_zero(DIGEST_LEN);
  1822. if (base16_decode(d, DIGEST_LEN, cp, HEX_DIGEST_LEN)<0) {
  1823. log_info(LD_DIR, "Skipping non-decodable digest %s", escaped(cp));
  1824. smartlist_del(fp_out, i--);
  1825. goto again;
  1826. }
  1827. smartlist_set(fp_out, i, d);
  1828. d = NULL;
  1829. again:
  1830. tor_free(cp);
  1831. tor_free(d);
  1832. }
  1833. }
  1834. return 0;
  1835. }