dircache.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #define DIRCACHE_PRIVATE
  6. #include "core/or/or.h"
  7. #include "app/config/config.h"
  8. #include "core/mainloop/connection.h"
  9. #include "core/or/relay.h"
  10. #include "feature/dirauth/dirvote.h"
  11. #include "feature/dirauth/authmode.h"
  12. #include "feature/dirauth/process_descs.h"
  13. #include "feature/dircache/conscache.h"
  14. #include "feature/dircache/consdiffmgr.h"
  15. #include "feature/dircache/dircache.h"
  16. #include "feature/dircache/dirserv.h"
  17. #include "feature/dircommon/directory.h"
  18. #include "feature/dircommon/fp_pair.h"
  19. #include "feature/hs/hs_cache.h"
  20. #include "feature/nodelist/authcert.h"
  21. #include "feature/nodelist/networkstatus.h"
  22. #include "feature/nodelist/routerlist.h"
  23. #include "feature/relay/routermode.h"
  24. #include "feature/rend/rendcache.h"
  25. #include "feature/stats/geoip_stats.h"
  26. #include "feature/stats/rephist.h"
  27. #include "lib/compress/compress.h"
  28. #include "feature/dircache/cached_dir_st.h"
  29. #include "feature/dircommon/dir_connection_st.h"
  30. #include "feature/nodelist/authority_cert_st.h"
  31. #include "feature/nodelist/networkstatus_st.h"
  32. #include "feature/nodelist/routerinfo_st.h"
  33. /** Maximum size, in bytes, for any directory object that we're accepting
  34. * as an upload. */
  35. #define MAX_DIR_UL_SIZE ((1<<24)-1) /* 16MB-1 */
  36. /** HTTP cache control: how long do we tell proxies they can cache each
  37. * kind of document we serve? */
  38. #define FULL_DIR_CACHE_LIFETIME (60*60)
  39. #define RUNNINGROUTERS_CACHE_LIFETIME (20*60)
  40. #define DIRPORTFRONTPAGE_CACHE_LIFETIME (20*60)
  41. #define NETWORKSTATUS_CACHE_LIFETIME (5*60)
  42. #define ROUTERDESC_CACHE_LIFETIME (30*60)
  43. #define ROUTERDESC_BY_DIGEST_CACHE_LIFETIME (48*60*60)
  44. #define ROBOTS_CACHE_LIFETIME (24*60*60)
  45. #define MICRODESC_CACHE_LIFETIME (48*60*60)
  46. /** Parse an HTTP request string <b>headers</b> of the form
  47. * \verbatim
  48. * "\%s [http[s]://]\%s HTTP/1..."
  49. * \endverbatim
  50. * If it's well-formed, strdup the second \%s into *<b>url</b>, and
  51. * nul-terminate it. If the url doesn't start with "/tor/", rewrite it
  52. * so it does. Return 0.
  53. * Otherwise, return -1.
  54. */
  55. STATIC int
  56. parse_http_url(const char *headers, char **url)
  57. {
  58. char *command = NULL;
  59. if (parse_http_command(headers, &command, url) < 0) {
  60. return -1;
  61. }
  62. if (strcmpstart(*url, "/tor/")) {
  63. char *new_url = NULL;
  64. tor_asprintf(&new_url, "/tor%s%s",
  65. *url[0] == '/' ? "" : "/",
  66. *url);
  67. tor_free(*url);
  68. *url = new_url;
  69. }
  70. tor_free(command);
  71. return 0;
  72. }
  73. /** Create an http response for the client <b>conn</b> out of
  74. * <b>status</b> and <b>reason_phrase</b>. Write it to <b>conn</b>.
  75. */
  76. static void
  77. write_short_http_response(dir_connection_t *conn, int status,
  78. const char *reason_phrase)
  79. {
  80. char *buf = NULL;
  81. char *datestring = NULL;
  82. IF_BUG_ONCE(!reason_phrase) { /* bullet-proofing */
  83. reason_phrase = "unspecified";
  84. }
  85. if (server_mode(get_options())) {
  86. /* include the Date: header, but only if we're a relay or bridge */
  87. char datebuf[RFC1123_TIME_LEN+1];
  88. format_rfc1123_time(datebuf, time(NULL));
  89. tor_asprintf(&datestring, "Date: %s\r\n", datebuf);
  90. }
  91. tor_asprintf(&buf, "HTTP/1.0 %d %s\r\n%s\r\n",
  92. status, reason_phrase, datestring?datestring:"");
  93. log_debug(LD_DIRSERV,"Wrote status 'HTTP/1.0 %d %s'", status, reason_phrase);
  94. connection_buf_add(buf, strlen(buf), TO_CONN(conn));
  95. tor_free(datestring);
  96. tor_free(buf);
  97. }
  98. /** Write the header for an HTTP/1.0 response onto <b>conn</b>-\>outbuf,
  99. * with <b>type</b> as the Content-Type.
  100. *
  101. * If <b>length</b> is nonnegative, it is the Content-Length.
  102. * If <b>encoding</b> is provided, it is the Content-Encoding.
  103. * If <b>cache_lifetime</b> is greater than 0, the content may be cached for
  104. * up to cache_lifetime seconds. Otherwise, the content may not be cached. */
  105. static void
  106. write_http_response_header_impl(dir_connection_t *conn, ssize_t length,
  107. const char *type, const char *encoding,
  108. const char *extra_headers,
  109. long cache_lifetime)
  110. {
  111. char date[RFC1123_TIME_LEN+1];
  112. time_t now = time(NULL);
  113. buf_t *buf = buf_new_with_capacity(1024);
  114. tor_assert(conn);
  115. format_rfc1123_time(date, now);
  116. buf_add_printf(buf, "HTTP/1.0 200 OK\r\nDate: %s\r\n", date);
  117. if (type) {
  118. buf_add_printf(buf, "Content-Type: %s\r\n", type);
  119. }
  120. if (!is_local_addr(&conn->base_.addr)) {
  121. /* Don't report the source address for a nearby/private connection.
  122. * Otherwise we tend to mis-report in cases where incoming ports are
  123. * being forwarded to a Tor server running behind the firewall. */
  124. buf_add_printf(buf, X_ADDRESS_HEADER "%s\r\n", conn->base_.address);
  125. }
  126. if (encoding) {
  127. buf_add_printf(buf, "Content-Encoding: %s\r\n", encoding);
  128. }
  129. if (length >= 0) {
  130. buf_add_printf(buf, "Content-Length: %ld\r\n", (long)length);
  131. }
  132. if (cache_lifetime > 0) {
  133. char expbuf[RFC1123_TIME_LEN+1];
  134. format_rfc1123_time(expbuf, (time_t)(now + cache_lifetime));
  135. /* We could say 'Cache-control: max-age=%d' here if we start doing
  136. * http/1.1 */
  137. buf_add_printf(buf, "Expires: %s\r\n", expbuf);
  138. } else if (cache_lifetime == 0) {
  139. /* We could say 'Cache-control: no-cache' here if we start doing
  140. * http/1.1 */
  141. buf_add_string(buf, "Pragma: no-cache\r\n");
  142. }
  143. if (extra_headers) {
  144. buf_add_string(buf, extra_headers);
  145. }
  146. buf_add_string(buf, "\r\n");
  147. connection_buf_add_buf(TO_CONN(conn), buf);
  148. buf_free(buf);
  149. }
  150. /** As write_http_response_header_impl, but translates method into
  151. * encoding */
  152. static void
  153. write_http_response_headers(dir_connection_t *conn, ssize_t length,
  154. compress_method_t method,
  155. const char *extra_headers, long cache_lifetime)
  156. {
  157. write_http_response_header_impl(conn, length,
  158. "text/plain",
  159. compression_method_get_name(method),
  160. extra_headers,
  161. cache_lifetime);
  162. }
  163. /** As write_http_response_headers, but assumes extra_headers is NULL */
  164. static void
  165. write_http_response_header(dir_connection_t *conn, ssize_t length,
  166. compress_method_t method,
  167. long cache_lifetime)
  168. {
  169. write_http_response_headers(conn, length, method, NULL, cache_lifetime);
  170. }
  171. /** Array of compression methods to use (if supported) for serving
  172. * precompressed data, ordered from best to worst. */
  173. static compress_method_t srv_meth_pref_precompressed[] = {
  174. LZMA_METHOD,
  175. ZSTD_METHOD,
  176. ZLIB_METHOD,
  177. GZIP_METHOD,
  178. NO_METHOD
  179. };
  180. /** Array of compression methods to use (if supported) for serving
  181. * streamed data, ordered from best to worst. */
  182. static compress_method_t srv_meth_pref_streaming_compression[] = {
  183. ZSTD_METHOD,
  184. ZLIB_METHOD,
  185. GZIP_METHOD,
  186. NO_METHOD
  187. };
  188. /** Parse the compression methods listed in an Accept-Encoding header <b>h</b>,
  189. * and convert them to a bitfield where compression method x is supported if
  190. * and only if 1 &lt;&lt; x is set in the bitfield. */
  191. STATIC unsigned
  192. parse_accept_encoding_header(const char *h)
  193. {
  194. unsigned result = (1u << NO_METHOD);
  195. smartlist_t *methods = smartlist_new();
  196. smartlist_split_string(methods, h, ",",
  197. SPLIT_SKIP_SPACE|SPLIT_STRIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  198. SMARTLIST_FOREACH_BEGIN(methods, const char *, m) {
  199. compress_method_t method = compression_method_get_by_name(m);
  200. if (method != UNKNOWN_METHOD) {
  201. tor_assert(((unsigned)method) < 8*sizeof(unsigned));
  202. result |= (1u << method);
  203. }
  204. } SMARTLIST_FOREACH_END(m);
  205. SMARTLIST_FOREACH_BEGIN(methods, char *, m) {
  206. tor_free(m);
  207. } SMARTLIST_FOREACH_END(m);
  208. smartlist_free(methods);
  209. return result;
  210. }
  211. /** Decide whether a client would accept the consensus we have.
  212. *
  213. * Clients can say they only want a consensus if it's signed by more
  214. * than half the authorities in a list. They pass this list in
  215. * the url as "...consensus/<b>fpr</b>+<b>fpr</b>+<b>fpr</b>".
  216. *
  217. * <b>fpr</b> may be an abbreviated fingerprint, i.e. only a left substring
  218. * of the full authority identity digest. (Only strings of even length,
  219. * i.e. encodings of full bytes, are handled correctly. In the case
  220. * of an odd number of hex digits the last one is silently ignored.)
  221. *
  222. * Returns 1 if more than half of the requested authorities signed the
  223. * consensus, 0 otherwise.
  224. */
  225. static int
  226. client_likes_consensus(const struct consensus_cache_entry_t *ent,
  227. const char *want_url)
  228. {
  229. smartlist_t *voters = smartlist_new();
  230. int need_at_least;
  231. int have = 0;
  232. if (consensus_cache_entry_get_voter_id_digests(ent, voters) != 0) {
  233. smartlist_free(voters);
  234. return 1; // We don't know the voters; assume the client won't mind. */
  235. }
  236. smartlist_t *want_authorities = smartlist_new();
  237. dir_split_resource_into_fingerprints(want_url, want_authorities, NULL, 0);
  238. need_at_least = smartlist_len(want_authorities)/2+1;
  239. SMARTLIST_FOREACH_BEGIN(want_authorities, const char *, want_digest) {
  240. SMARTLIST_FOREACH_BEGIN(voters, const char *, digest) {
  241. if (!strcasecmpstart(digest, want_digest)) {
  242. have++;
  243. break;
  244. };
  245. } SMARTLIST_FOREACH_END(digest);
  246. /* early exit, if we already have enough */
  247. if (have >= need_at_least)
  248. break;
  249. } SMARTLIST_FOREACH_END(want_digest);
  250. SMARTLIST_FOREACH(want_authorities, char *, d, tor_free(d));
  251. smartlist_free(want_authorities);
  252. SMARTLIST_FOREACH(voters, char *, cp, tor_free(cp));
  253. smartlist_free(voters);
  254. return (have >= need_at_least);
  255. }
  256. /** Return the compression level we should use for sending a compressed
  257. * response of size <b>n_bytes</b>. */
  258. STATIC compression_level_t
  259. choose_compression_level(ssize_t n_bytes)
  260. {
  261. if (! have_been_under_memory_pressure()) {
  262. return HIGH_COMPRESSION; /* we have plenty of RAM. */
  263. } else if (n_bytes < 0) {
  264. return HIGH_COMPRESSION; /* unknown; might be big. */
  265. } else if (n_bytes < 1024) {
  266. return LOW_COMPRESSION;
  267. } else if (n_bytes < 2048) {
  268. return MEDIUM_COMPRESSION;
  269. } else {
  270. return HIGH_COMPRESSION;
  271. }
  272. }
  273. /** Information passed to handle a GET request. */
  274. typedef struct get_handler_args_t {
  275. /** Bitmask of compression methods that the client said (or implied) it
  276. * supported. */
  277. unsigned compression_supported;
  278. /** If nonzero, the time included an if-modified-since header with this
  279. * value. */
  280. time_t if_modified_since;
  281. /** String containing the requested URL or resource. */
  282. const char *url;
  283. /** String containing the HTTP headers */
  284. const char *headers;
  285. } get_handler_args_t;
  286. /** Entry for handling an HTTP GET request.
  287. *
  288. * This entry matches a request if "string" is equal to the requested
  289. * resource, or if "is_prefix" is true and "string" is a prefix of the
  290. * requested resource.
  291. *
  292. * The 'handler' function is called to handle the request. It receives
  293. * an arguments structure, and must return 0 on success or -1 if we should
  294. * close the connection.
  295. **/
  296. typedef struct url_table_ent_s {
  297. const char *string;
  298. int is_prefix;
  299. int (*handler)(dir_connection_t *conn, const get_handler_args_t *args);
  300. } url_table_ent_t;
  301. static int handle_get_frontpage(dir_connection_t *conn,
  302. const get_handler_args_t *args);
  303. static int handle_get_current_consensus(dir_connection_t *conn,
  304. const get_handler_args_t *args);
  305. static int handle_get_status_vote(dir_connection_t *conn,
  306. const get_handler_args_t *args);
  307. static int handle_get_microdesc(dir_connection_t *conn,
  308. const get_handler_args_t *args);
  309. static int handle_get_descriptor(dir_connection_t *conn,
  310. const get_handler_args_t *args);
  311. static int handle_get_keys(dir_connection_t *conn,
  312. const get_handler_args_t *args);
  313. static int handle_get_hs_descriptor_v2(dir_connection_t *conn,
  314. const get_handler_args_t *args);
  315. static int handle_get_robots(dir_connection_t *conn,
  316. const get_handler_args_t *args);
  317. static int handle_get_networkstatus_bridges(dir_connection_t *conn,
  318. const get_handler_args_t *args);
  319. /** Table for handling GET requests. */
  320. static const url_table_ent_t url_table[] = {
  321. { "/tor/", 0, handle_get_frontpage },
  322. { "/tor/status-vote/current/consensus", 1, handle_get_current_consensus },
  323. { "/tor/status-vote/current/", 1, handle_get_status_vote },
  324. { "/tor/status-vote/next/", 1, handle_get_status_vote },
  325. { "/tor/micro/d/", 1, handle_get_microdesc },
  326. { "/tor/server/", 1, handle_get_descriptor },
  327. { "/tor/extra/", 1, handle_get_descriptor },
  328. { "/tor/keys/", 1, handle_get_keys },
  329. { "/tor/rendezvous2/", 1, handle_get_hs_descriptor_v2 },
  330. { "/tor/hs/3/", 1, handle_get_hs_descriptor_v3 },
  331. { "/tor/robots.txt", 0, handle_get_robots },
  332. { "/tor/networkstatus-bridges", 0, handle_get_networkstatus_bridges },
  333. { NULL, 0, NULL },
  334. };
  335. /** Helper function: called when a dirserver gets a complete HTTP GET
  336. * request. Look for a request for a directory or for a rendezvous
  337. * service descriptor. On finding one, write a response into
  338. * conn-\>outbuf. If the request is unrecognized, send a 404.
  339. * Return 0 if we handled this successfully, or -1 if we need to close
  340. * the connection. */
  341. MOCK_IMPL(STATIC int,
  342. directory_handle_command_get,(dir_connection_t *conn, const char *headers,
  343. const char *req_body, size_t req_body_len))
  344. {
  345. char *url, *url_mem, *header;
  346. time_t if_modified_since = 0;
  347. int zlib_compressed_in_url;
  348. unsigned compression_methods_supported;
  349. /* We ignore the body of a GET request. */
  350. (void)req_body;
  351. (void)req_body_len;
  352. log_debug(LD_DIRSERV,"Received GET command.");
  353. conn->base_.state = DIR_CONN_STATE_SERVER_WRITING;
  354. if (parse_http_url(headers, &url) < 0) {
  355. write_short_http_response(conn, 400, "Bad request");
  356. return 0;
  357. }
  358. if ((header = http_get_header(headers, "If-Modified-Since: "))) {
  359. struct tm tm;
  360. if (parse_http_time(header, &tm) == 0) {
  361. if (tor_timegm(&tm, &if_modified_since)<0) {
  362. if_modified_since = 0;
  363. } else {
  364. log_debug(LD_DIRSERV, "If-Modified-Since is '%s'.", escaped(header));
  365. }
  366. }
  367. /* The correct behavior on a malformed If-Modified-Since header is to
  368. * act as if no If-Modified-Since header had been given. */
  369. tor_free(header);
  370. }
  371. log_debug(LD_DIRSERV,"rewritten url as '%s'.", escaped(url));
  372. url_mem = url;
  373. {
  374. size_t url_len = strlen(url);
  375. zlib_compressed_in_url = url_len > 2 && !strcmp(url+url_len-2, ".z");
  376. if (zlib_compressed_in_url) {
  377. url[url_len-2] = '\0';
  378. }
  379. }
  380. if ((header = http_get_header(headers, "Accept-Encoding: "))) {
  381. compression_methods_supported = parse_accept_encoding_header(header);
  382. tor_free(header);
  383. } else {
  384. compression_methods_supported = (1u << NO_METHOD);
  385. }
  386. if (zlib_compressed_in_url) {
  387. compression_methods_supported |= (1u << ZLIB_METHOD);
  388. }
  389. /* Remove all methods that we don't both support. */
  390. compression_methods_supported &= tor_compress_get_supported_method_bitmask();
  391. get_handler_args_t args;
  392. args.url = url;
  393. args.headers = headers;
  394. args.if_modified_since = if_modified_since;
  395. args.compression_supported = compression_methods_supported;
  396. int i, result = -1;
  397. for (i = 0; url_table[i].string; ++i) {
  398. int match;
  399. if (url_table[i].is_prefix) {
  400. match = !strcmpstart(url, url_table[i].string);
  401. } else {
  402. match = !strcmp(url, url_table[i].string);
  403. }
  404. if (match) {
  405. result = url_table[i].handler(conn, &args);
  406. goto done;
  407. }
  408. }
  409. /* we didn't recognize the url */
  410. write_short_http_response(conn, 404, "Not found");
  411. result = 0;
  412. done:
  413. tor_free(url_mem);
  414. return result;
  415. }
  416. /** Helper function for GET / or GET /tor/
  417. */
  418. static int
  419. handle_get_frontpage(dir_connection_t *conn, const get_handler_args_t *args)
  420. {
  421. (void) args; /* unused */
  422. const char *frontpage = get_dirportfrontpage();
  423. if (frontpage) {
  424. size_t dlen;
  425. dlen = strlen(frontpage);
  426. /* Let's return a disclaimer page (users shouldn't use V1 anymore,
  427. and caches don't fetch '/', so this is safe). */
  428. /* [We don't check for write_bucket_low here, since we want to serve
  429. * this page no matter what.] */
  430. write_http_response_header_impl(conn, dlen, "text/html", "identity",
  431. NULL, DIRPORTFRONTPAGE_CACHE_LIFETIME);
  432. connection_buf_add(frontpage, dlen, TO_CONN(conn));
  433. } else {
  434. write_short_http_response(conn, 404, "Not found");
  435. }
  436. return 0;
  437. }
  438. /** Warn that the cached consensus <b>consensus</b> of type
  439. * <b>flavor</b> too new or too old, based on <b>is_too_new</b>,
  440. * and will not be served to clients. Rate-limit the warning to avoid logging
  441. * an entry on every request.
  442. */
  443. static void
  444. warn_consensus_is_not_reasonably_live(
  445. const struct consensus_cache_entry_t *consensus,
  446. const char *flavor, time_t now, bool is_too_new)
  447. {
  448. #define NOT_REASONABLY_LIVE_WARNING_INTERVAL (60*60)
  449. static ratelim_t warned[2] = { RATELIM_INIT(
  450. NOT_REASONABLY_LIVE_WARNING_INTERVAL),
  451. RATELIM_INIT(
  452. NOT_REASONABLY_LIVE_WARNING_INTERVAL) };
  453. char timestamp[ISO_TIME_LEN+1];
  454. /* valid_after if is_too_new, valid_until if !is_too_new */
  455. time_t valid_time = 0;
  456. char *dupes = NULL;
  457. if (is_too_new) {
  458. if (consensus_cache_entry_get_valid_after(consensus, &valid_time))
  459. return;
  460. dupes = rate_limit_log(&warned[1], now);
  461. } else {
  462. if (consensus_cache_entry_get_valid_until(consensus, &valid_time))
  463. return;
  464. dupes = rate_limit_log(&warned[0], now);
  465. }
  466. if (dupes) {
  467. format_local_iso_time(timestamp, valid_time);
  468. log_warn(LD_DIRSERV, "Our %s%sconsensus is too %s, so we will not "
  469. "serve it to clients. It was valid %s %s local time and we "
  470. "continued to serve it for up to 24 hours %s.%s",
  471. flavor ? flavor : "",
  472. flavor ? " " : "",
  473. is_too_new ? "new" : "old",
  474. is_too_new ? "after" : "until",
  475. timestamp,
  476. is_too_new ? "before it was valid" : "after it expired",
  477. dupes);
  478. tor_free(dupes);
  479. }
  480. }
  481. /**
  482. * Parse a single hex-encoded sha3-256 digest from <b>hex</b> into
  483. * <b>digest</b>. Return 0 on success. On failure, report that the hash came
  484. * from <b>location</b>, report that we are taking <b>action</b> with it, and
  485. * return -1.
  486. */
  487. static int
  488. parse_one_diff_hash(uint8_t *digest, const char *hex, const char *location,
  489. const char *action)
  490. {
  491. if (base16_decode((char*)digest, DIGEST256_LEN, hex, strlen(hex)) ==
  492. DIGEST256_LEN) {
  493. return 0;
  494. } else {
  495. log_fn(LOG_PROTOCOL_WARN, LD_DIR,
  496. "%s contained bogus digest %s; %s.",
  497. location, escaped(hex), action);
  498. return -1;
  499. }
  500. }
  501. /** If there is an X-Or-Diff-From-Consensus header included in <b>headers</b>,
  502. * set <b>digest_out<b> to a new smartlist containing every 256-bit
  503. * hex-encoded digest listed in that header and return 0. Otherwise return
  504. * -1. */
  505. static int
  506. parse_or_diff_from_header(smartlist_t **digests_out, const char *headers)
  507. {
  508. char *hdr = http_get_header(headers, X_OR_DIFF_FROM_CONSENSUS_HEADER);
  509. if (hdr == NULL) {
  510. return -1;
  511. }
  512. smartlist_t *hex_digests = smartlist_new();
  513. *digests_out = smartlist_new();
  514. smartlist_split_string(hex_digests, hdr, " ",
  515. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
  516. SMARTLIST_FOREACH_BEGIN(hex_digests, const char *, hex) {
  517. uint8_t digest[DIGEST256_LEN];
  518. if (!parse_one_diff_hash(digest, hex, "X-Or-Diff-From-Consensus header",
  519. "ignoring")) {
  520. smartlist_add(*digests_out, tor_memdup(digest, sizeof(digest)));
  521. }
  522. } SMARTLIST_FOREACH_END(hex);
  523. SMARTLIST_FOREACH(hex_digests, char *, cp, tor_free(cp));
  524. smartlist_free(hex_digests);
  525. tor_free(hdr);
  526. return 0;
  527. }
  528. /** Fallback compression method. The fallback compression method is used in
  529. * case a client requests a non-compressed document. We only store compressed
  530. * documents, so we use this compression method to fetch the document and let
  531. * the spooling system do the streaming decompression.
  532. */
  533. #define FALLBACK_COMPRESS_METHOD ZLIB_METHOD
  534. /**
  535. * Try to find the best consensus diff possible in order to serve a client
  536. * request for a diff from one of the consensuses in <b>digests</b> to the
  537. * current consensus of flavor <b>flav</b>. The client supports the
  538. * compression methods listed in the <b>compression_methods</b> bitfield:
  539. * place the method chosen (if any) into <b>compression_used_out</b>.
  540. */
  541. static struct consensus_cache_entry_t *
  542. find_best_diff(const smartlist_t *digests, int flav,
  543. unsigned compression_methods,
  544. compress_method_t *compression_used_out)
  545. {
  546. struct consensus_cache_entry_t *result = NULL;
  547. SMARTLIST_FOREACH_BEGIN(digests, const uint8_t *, diff_from) {
  548. unsigned u;
  549. for (u = 0; u < ARRAY_LENGTH(srv_meth_pref_precompressed); ++u) {
  550. compress_method_t method = srv_meth_pref_precompressed[u];
  551. if (0 == (compression_methods & (1u<<method)))
  552. continue; // client doesn't like this one, or we don't have it.
  553. if (consdiffmgr_find_diff_from(&result, flav, DIGEST_SHA3_256,
  554. diff_from, DIGEST256_LEN,
  555. method) == CONSDIFF_AVAILABLE) {
  556. tor_assert_nonfatal(result);
  557. *compression_used_out = method;
  558. return result;
  559. }
  560. }
  561. } SMARTLIST_FOREACH_END(diff_from);
  562. SMARTLIST_FOREACH_BEGIN(digests, const uint8_t *, diff_from) {
  563. if (consdiffmgr_find_diff_from(&result, flav, DIGEST_SHA3_256, diff_from,
  564. DIGEST256_LEN, FALLBACK_COMPRESS_METHOD) == CONSDIFF_AVAILABLE) {
  565. tor_assert_nonfatal(result);
  566. *compression_used_out = FALLBACK_COMPRESS_METHOD;
  567. return result;
  568. }
  569. } SMARTLIST_FOREACH_END(diff_from);
  570. return NULL;
  571. }
  572. /** Lookup the cached consensus document by the flavor found in <b>flav</b>.
  573. * The preferred set of compression methods should be listed in the
  574. * <b>compression_methods</b> bitfield. The compression method chosen (if any)
  575. * is stored in <b>compression_used_out</b>. */
  576. static struct consensus_cache_entry_t *
  577. find_best_consensus(int flav,
  578. unsigned compression_methods,
  579. compress_method_t *compression_used_out)
  580. {
  581. struct consensus_cache_entry_t *result = NULL;
  582. unsigned u;
  583. for (u = 0; u < ARRAY_LENGTH(srv_meth_pref_precompressed); ++u) {
  584. compress_method_t method = srv_meth_pref_precompressed[u];
  585. if (0 == (compression_methods & (1u<<method)))
  586. continue;
  587. if (consdiffmgr_find_consensus(&result, flav,
  588. method) == CONSDIFF_AVAILABLE) {
  589. tor_assert_nonfatal(result);
  590. *compression_used_out = method;
  591. return result;
  592. }
  593. }
  594. if (consdiffmgr_find_consensus(&result, flav,
  595. FALLBACK_COMPRESS_METHOD) == CONSDIFF_AVAILABLE) {
  596. tor_assert_nonfatal(result);
  597. *compression_used_out = FALLBACK_COMPRESS_METHOD;
  598. return result;
  599. }
  600. return NULL;
  601. }
  602. /** Try to find the best supported compression method possible from a given
  603. * <b>compression_methods</b>. Return NO_METHOD if no mutually supported
  604. * compression method could be found. */
  605. static compress_method_t
  606. find_best_compression_method(unsigned compression_methods, int stream)
  607. {
  608. unsigned u;
  609. compress_method_t *methods;
  610. size_t length;
  611. if (stream) {
  612. methods = srv_meth_pref_streaming_compression;
  613. length = ARRAY_LENGTH(srv_meth_pref_streaming_compression);
  614. } else {
  615. methods = srv_meth_pref_precompressed;
  616. length = ARRAY_LENGTH(srv_meth_pref_precompressed);
  617. }
  618. for (u = 0; u < length; ++u) {
  619. compress_method_t method = methods[u];
  620. if (compression_methods & (1u<<method))
  621. return method;
  622. }
  623. return NO_METHOD;
  624. }
  625. /** Check if any of the digests in <b>digests</b> matches the latest consensus
  626. * flavor (given in <b>flavor</b>) that we have available. */
  627. static int
  628. digest_list_contains_best_consensus(consensus_flavor_t flavor,
  629. const smartlist_t *digests)
  630. {
  631. const networkstatus_t *ns = NULL;
  632. if (digests == NULL)
  633. return 0;
  634. ns = networkstatus_get_latest_consensus_by_flavor(flavor);
  635. if (ns == NULL)
  636. return 0;
  637. SMARTLIST_FOREACH_BEGIN(digests, const uint8_t *, digest) {
  638. if (tor_memeq(ns->digest_sha3_as_signed, digest, DIGEST256_LEN))
  639. return 1;
  640. } SMARTLIST_FOREACH_END(digest);
  641. return 0;
  642. }
  643. /** Encodes the results of parsing a consensus request to figure out what
  644. * consensus, and possibly what diffs, the user asked for. */
  645. typedef struct {
  646. /** name of the flavor to retrieve. */
  647. char *flavor;
  648. /** flavor to retrive, as enum. */
  649. consensus_flavor_t flav;
  650. /** plus-separated list of authority fingerprints; see
  651. * client_likes_consensus(). Aliases the URL in the request passed to
  652. * parse_consensus_request(). */
  653. const char *want_fps;
  654. /** Optionally, a smartlist of sha3 digests-as-signed of the consensuses
  655. * to return a diff from. */
  656. smartlist_t *diff_from_digests;
  657. /** If true, never send a full consensus. If there is no diff, send
  658. * a 404 instead. */
  659. int diff_only;
  660. } parsed_consensus_request_t;
  661. /** Remove all data held in <b>req</b>. Do not free <b>req</b> itself, since
  662. * it is stack-allocated. */
  663. static void
  664. parsed_consensus_request_clear(parsed_consensus_request_t *req)
  665. {
  666. if (!req)
  667. return;
  668. tor_free(req->flavor);
  669. if (req->diff_from_digests) {
  670. SMARTLIST_FOREACH(req->diff_from_digests, uint8_t *, d, tor_free(d));
  671. smartlist_free(req->diff_from_digests);
  672. }
  673. memset(req, 0, sizeof(parsed_consensus_request_t));
  674. }
  675. /**
  676. * Parse the URL and relevant headers of <b>args</b> for a current-consensus
  677. * request to learn what flavor of consensus we want, what keys it must be
  678. * signed with, and what diffs we would accept (or demand) instead. Return 0
  679. * on success and -1 on failure.
  680. */
  681. static int
  682. parse_consensus_request(parsed_consensus_request_t *out,
  683. const get_handler_args_t *args)
  684. {
  685. const char *url = args->url;
  686. memset(out, 0, sizeof(parsed_consensus_request_t));
  687. out->flav = FLAV_NS;
  688. const char CONSENSUS_URL_PREFIX[] = "/tor/status-vote/current/consensus/";
  689. const char CONSENSUS_FLAVORED_PREFIX[] =
  690. "/tor/status-vote/current/consensus-";
  691. /* figure out the flavor if any, and who we wanted to sign the thing */
  692. const char *after_flavor = NULL;
  693. if (!strcmpstart(url, CONSENSUS_FLAVORED_PREFIX)) {
  694. const char *f, *cp;
  695. f = url + strlen(CONSENSUS_FLAVORED_PREFIX);
  696. cp = strchr(f, '/');
  697. if (cp) {
  698. after_flavor = cp+1;
  699. out->flavor = tor_strndup(f, cp-f);
  700. } else {
  701. out->flavor = tor_strdup(f);
  702. }
  703. int flav = networkstatus_parse_flavor_name(out->flavor);
  704. if (flav < 0)
  705. flav = FLAV_NS;
  706. out->flav = flav;
  707. } else {
  708. if (!strcmpstart(url, CONSENSUS_URL_PREFIX))
  709. after_flavor = url+strlen(CONSENSUS_URL_PREFIX);
  710. }
  711. /* see whether we've been asked explicitly for a diff from an older
  712. * consensus. (The user might also have said that a diff would be okay,
  713. * via X-Or-Diff-From-Consensus */
  714. const char DIFF_COMPONENT[] = "diff/";
  715. char *diff_hash_in_url = NULL;
  716. if (after_flavor && !strcmpstart(after_flavor, DIFF_COMPONENT)) {
  717. after_flavor += strlen(DIFF_COMPONENT);
  718. const char *cp = strchr(after_flavor, '/');
  719. if (cp) {
  720. diff_hash_in_url = tor_strndup(after_flavor, cp-after_flavor);
  721. out->want_fps = cp+1;
  722. } else {
  723. diff_hash_in_url = tor_strdup(after_flavor);
  724. out->want_fps = NULL;
  725. }
  726. } else {
  727. out->want_fps = after_flavor;
  728. }
  729. if (diff_hash_in_url) {
  730. uint8_t diff_from[DIGEST256_LEN];
  731. out->diff_from_digests = smartlist_new();
  732. out->diff_only = 1;
  733. int ok = !parse_one_diff_hash(diff_from, diff_hash_in_url, "URL",
  734. "rejecting");
  735. tor_free(diff_hash_in_url);
  736. if (ok) {
  737. smartlist_add(out->diff_from_digests,
  738. tor_memdup(diff_from, DIGEST256_LEN));
  739. } else {
  740. return -1;
  741. }
  742. } else {
  743. parse_or_diff_from_header(&out->diff_from_digests, args->headers);
  744. }
  745. return 0;
  746. }
  747. /** Helper function for GET /tor/status-vote/current/consensus
  748. */
  749. static int
  750. handle_get_current_consensus(dir_connection_t *conn,
  751. const get_handler_args_t *args)
  752. {
  753. const compress_method_t compress_method =
  754. find_best_compression_method(args->compression_supported, 0);
  755. const time_t if_modified_since = args->if_modified_since;
  756. int clear_spool = 0;
  757. /* v3 network status fetch. */
  758. long lifetime = NETWORKSTATUS_CACHE_LIFETIME;
  759. time_t now = time(NULL);
  760. parsed_consensus_request_t req;
  761. if (parse_consensus_request(&req, args) < 0) {
  762. write_short_http_response(conn, 404, "Couldn't parse request");
  763. goto done;
  764. }
  765. if (digest_list_contains_best_consensus(req.flav,
  766. req.diff_from_digests)) {
  767. write_short_http_response(conn, 304, "Not modified");
  768. geoip_note_ns_response(GEOIP_REJECT_NOT_MODIFIED);
  769. goto done;
  770. }
  771. struct consensus_cache_entry_t *cached_consensus = NULL;
  772. compress_method_t compression_used = NO_METHOD;
  773. if (req.diff_from_digests) {
  774. cached_consensus = find_best_diff(req.diff_from_digests, req.flav,
  775. args->compression_supported,
  776. &compression_used);
  777. }
  778. if (req.diff_only && !cached_consensus) {
  779. write_short_http_response(conn, 404, "No such diff available");
  780. geoip_note_ns_response(GEOIP_REJECT_NOT_FOUND);
  781. goto done;
  782. }
  783. if (! cached_consensus) {
  784. cached_consensus = find_best_consensus(req.flav,
  785. args->compression_supported,
  786. &compression_used);
  787. }
  788. time_t valid_after, fresh_until, valid_until;
  789. int have_valid_after = 0, have_fresh_until = 0, have_valid_until = 0;
  790. if (cached_consensus) {
  791. have_valid_after =
  792. !consensus_cache_entry_get_valid_after(cached_consensus, &valid_after);
  793. have_fresh_until =
  794. !consensus_cache_entry_get_fresh_until(cached_consensus, &fresh_until);
  795. have_valid_until =
  796. !consensus_cache_entry_get_valid_until(cached_consensus, &valid_until);
  797. }
  798. if (cached_consensus && have_valid_after &&
  799. !networkstatus_valid_after_is_reasonably_live(valid_after, now)) {
  800. write_short_http_response(conn, 404, "Consensus is too new");
  801. warn_consensus_is_not_reasonably_live(cached_consensus, req.flavor, now,
  802. 1);
  803. geoip_note_ns_response(GEOIP_REJECT_NOT_FOUND);
  804. goto done;
  805. } else if (
  806. cached_consensus && have_valid_until &&
  807. !networkstatus_valid_until_is_reasonably_live(valid_until, now)) {
  808. write_short_http_response(conn, 404, "Consensus is too old");
  809. warn_consensus_is_not_reasonably_live(cached_consensus, req.flavor, now,
  810. 0);
  811. geoip_note_ns_response(GEOIP_REJECT_NOT_FOUND);
  812. goto done;
  813. }
  814. if (cached_consensus && req.want_fps &&
  815. !client_likes_consensus(cached_consensus, req.want_fps)) {
  816. write_short_http_response(conn, 404, "Consensus not signed by sufficient "
  817. "number of requested authorities");
  818. geoip_note_ns_response(GEOIP_REJECT_NOT_ENOUGH_SIGS);
  819. goto done;
  820. }
  821. conn->spool = smartlist_new();
  822. clear_spool = 1;
  823. {
  824. spooled_resource_t *spooled;
  825. if (cached_consensus) {
  826. spooled = spooled_resource_new_from_cache_entry(cached_consensus);
  827. smartlist_add(conn->spool, spooled);
  828. }
  829. }
  830. lifetime = (have_fresh_until && fresh_until > now) ? fresh_until - now : 0;
  831. size_t size_guess = 0;
  832. int n_expired = 0;
  833. dirserv_spool_remove_missing_and_guess_size(conn, if_modified_since,
  834. compress_method != NO_METHOD,
  835. &size_guess,
  836. &n_expired);
  837. if (!smartlist_len(conn->spool) && !n_expired) {
  838. write_short_http_response(conn, 404, "Not found");
  839. geoip_note_ns_response(GEOIP_REJECT_NOT_FOUND);
  840. goto done;
  841. } else if (!smartlist_len(conn->spool)) {
  842. write_short_http_response(conn, 304, "Not modified");
  843. geoip_note_ns_response(GEOIP_REJECT_NOT_MODIFIED);
  844. goto done;
  845. }
  846. if (global_write_bucket_low(TO_CONN(conn), size_guess, 2)) {
  847. log_debug(LD_DIRSERV,
  848. "Client asked for network status lists, but we've been "
  849. "writing too many bytes lately. Sending 503 Dir busy.");
  850. write_short_http_response(conn, 503, "Directory busy, try again later");
  851. geoip_note_ns_response(GEOIP_REJECT_BUSY);
  852. goto done;
  853. }
  854. tor_addr_t addr;
  855. if (tor_addr_parse(&addr, (TO_CONN(conn))->address) >= 0) {
  856. geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS,
  857. &addr, NULL,
  858. time(NULL));
  859. geoip_note_ns_response(GEOIP_SUCCESS);
  860. /* Note that a request for a network status has started, so that we
  861. * can measure the download time later on. */
  862. if (conn->dirreq_id)
  863. geoip_start_dirreq(conn->dirreq_id, size_guess, DIRREQ_TUNNELED);
  864. else
  865. geoip_start_dirreq(TO_CONN(conn)->global_identifier, size_guess,
  866. DIRREQ_DIRECT);
  867. }
  868. /* Use this header to tell caches that the response depends on the
  869. * X-Or-Diff-From-Consensus header (or lack thereof). */
  870. const char vary_header[] = "Vary: X-Or-Diff-From-Consensus\r\n";
  871. clear_spool = 0;
  872. // The compress_method might have been NO_METHOD, but we store the data
  873. // compressed. Decompress them using `compression_used`. See fallback code in
  874. // find_best_consensus() and find_best_diff().
  875. write_http_response_headers(conn, -1,
  876. compress_method == NO_METHOD ?
  877. NO_METHOD : compression_used,
  878. vary_header,
  879. smartlist_len(conn->spool) == 1 ? lifetime : 0);
  880. if (compress_method == NO_METHOD && smartlist_len(conn->spool))
  881. conn->compress_state = tor_compress_new(0, compression_used,
  882. HIGH_COMPRESSION);
  883. /* Prime the connection with some data. */
  884. const int initial_flush_result = connection_dirserv_flushed_some(conn);
  885. tor_assert_nonfatal(initial_flush_result == 0);
  886. goto done;
  887. done:
  888. parsed_consensus_request_clear(&req);
  889. if (clear_spool) {
  890. dir_conn_clear_spool(conn);
  891. }
  892. return 0;
  893. }
  894. /** Helper function for GET /tor/status-vote/{current,next}/...
  895. */
  896. static int
  897. handle_get_status_vote(dir_connection_t *conn, const get_handler_args_t *args)
  898. {
  899. const char *url = args->url;
  900. {
  901. ssize_t body_len = 0;
  902. ssize_t estimated_len = 0;
  903. int lifetime = 60; /* XXXX?? should actually use vote intervals. */
  904. /* This smartlist holds strings that we can compress on the fly. */
  905. smartlist_t *items = smartlist_new();
  906. /* This smartlist holds cached_dir_t objects that have a precompressed
  907. * deflated version. */
  908. smartlist_t *dir_items = smartlist_new();
  909. dirvote_dirreq_get_status_vote(url, items, dir_items);
  910. if (!smartlist_len(dir_items) && !smartlist_len(items)) {
  911. write_short_http_response(conn, 404, "Not found");
  912. goto vote_done;
  913. }
  914. /* We're sending items from at most one kind of source */
  915. tor_assert_nonfatal(smartlist_len(items) == 0 ||
  916. smartlist_len(dir_items) == 0);
  917. int streaming;
  918. unsigned mask;
  919. if (smartlist_len(items)) {
  920. /* We're taking strings and compressing them on the fly. */
  921. streaming = 1;
  922. mask = ~0u;
  923. } else {
  924. /* We're taking cached_dir_t objects. We only have them uncompressed
  925. * or deflated. */
  926. streaming = 0;
  927. mask = (1u<<NO_METHOD) | (1u<<ZLIB_METHOD);
  928. }
  929. const compress_method_t compress_method = find_best_compression_method(
  930. args->compression_supported&mask, streaming);
  931. SMARTLIST_FOREACH(dir_items, cached_dir_t *, d,
  932. body_len += compress_method != NO_METHOD ?
  933. d->dir_compressed_len : d->dir_len);
  934. estimated_len += body_len;
  935. SMARTLIST_FOREACH(items, const char *, item, {
  936. size_t ln = strlen(item);
  937. if (compress_method != NO_METHOD) {
  938. estimated_len += ln/2;
  939. } else {
  940. body_len += ln; estimated_len += ln;
  941. }
  942. });
  943. if (global_write_bucket_low(TO_CONN(conn), estimated_len, 2)) {
  944. write_short_http_response(conn, 503, "Directory busy, try again later");
  945. goto vote_done;
  946. }
  947. write_http_response_header(conn, body_len ? body_len : -1,
  948. compress_method,
  949. lifetime);
  950. if (smartlist_len(items)) {
  951. if (compress_method != NO_METHOD) {
  952. conn->compress_state = tor_compress_new(1, compress_method,
  953. choose_compression_level(estimated_len));
  954. SMARTLIST_FOREACH(items, const char *, c,
  955. connection_buf_add_compress(c, strlen(c), conn, 0));
  956. connection_buf_add_compress("", 0, conn, 1);
  957. } else {
  958. SMARTLIST_FOREACH(items, const char *, c,
  959. connection_buf_add(c, strlen(c), TO_CONN(conn)));
  960. }
  961. } else {
  962. SMARTLIST_FOREACH(dir_items, cached_dir_t *, d,
  963. connection_buf_add(compress_method != NO_METHOD ?
  964. d->dir_compressed : d->dir,
  965. compress_method != NO_METHOD ?
  966. d->dir_compressed_len : d->dir_len,
  967. TO_CONN(conn)));
  968. }
  969. vote_done:
  970. smartlist_free(items);
  971. smartlist_free(dir_items);
  972. goto done;
  973. }
  974. done:
  975. return 0;
  976. }
  977. /** Helper function for GET /tor/micro/d/...
  978. */
  979. static int
  980. handle_get_microdesc(dir_connection_t *conn, const get_handler_args_t *args)
  981. {
  982. const char *url = args->url;
  983. const compress_method_t compress_method =
  984. find_best_compression_method(args->compression_supported, 1);
  985. int clear_spool = 1;
  986. {
  987. conn->spool = smartlist_new();
  988. dir_split_resource_into_spoolable(url+strlen("/tor/micro/d/"),
  989. DIR_SPOOL_MICRODESC,
  990. conn->spool, NULL,
  991. DSR_DIGEST256|DSR_BASE64|DSR_SORT_UNIQ);
  992. size_t size_guess = 0;
  993. dirserv_spool_remove_missing_and_guess_size(conn, 0,
  994. compress_method != NO_METHOD,
  995. &size_guess, NULL);
  996. if (smartlist_len(conn->spool) == 0) {
  997. write_short_http_response(conn, 404, "Not found");
  998. goto done;
  999. }
  1000. if (global_write_bucket_low(TO_CONN(conn), size_guess, 2)) {
  1001. log_info(LD_DIRSERV,
  1002. "Client asked for server descriptors, but we've been "
  1003. "writing too many bytes lately. Sending 503 Dir busy.");
  1004. write_short_http_response(conn, 503, "Directory busy, try again later");
  1005. goto done;
  1006. }
  1007. clear_spool = 0;
  1008. write_http_response_header(conn, -1,
  1009. compress_method,
  1010. MICRODESC_CACHE_LIFETIME);
  1011. if (compress_method != NO_METHOD)
  1012. conn->compress_state = tor_compress_new(1, compress_method,
  1013. choose_compression_level(size_guess));
  1014. const int initial_flush_result = connection_dirserv_flushed_some(conn);
  1015. tor_assert_nonfatal(initial_flush_result == 0);
  1016. goto done;
  1017. }
  1018. done:
  1019. if (clear_spool) {
  1020. dir_conn_clear_spool(conn);
  1021. }
  1022. return 0;
  1023. }
  1024. /** Helper function for GET /tor/{server,extra}/...
  1025. */
  1026. static int
  1027. handle_get_descriptor(dir_connection_t *conn, const get_handler_args_t *args)
  1028. {
  1029. const char *url = args->url;
  1030. const compress_method_t compress_method =
  1031. find_best_compression_method(args->compression_supported, 1);
  1032. const or_options_t *options = get_options();
  1033. int clear_spool = 1;
  1034. if (!strcmpstart(url,"/tor/server/") ||
  1035. (!options->BridgeAuthoritativeDir &&
  1036. !options->BridgeRelay && !strcmpstart(url,"/tor/extra/"))) {
  1037. int res;
  1038. const char *msg = NULL;
  1039. int cache_lifetime = 0;
  1040. int is_extra = !strcmpstart(url,"/tor/extra/");
  1041. url += is_extra ? strlen("/tor/extra/") : strlen("/tor/server/");
  1042. dir_spool_source_t source;
  1043. time_t publish_cutoff = 0;
  1044. if (!strcmpstart(url, "d/")) {
  1045. source =
  1046. is_extra ? DIR_SPOOL_EXTRA_BY_DIGEST : DIR_SPOOL_SERVER_BY_DIGEST;
  1047. } else {
  1048. source =
  1049. is_extra ? DIR_SPOOL_EXTRA_BY_FP : DIR_SPOOL_SERVER_BY_FP;
  1050. /* We only want to apply a publish cutoff when we're requesting
  1051. * resources by fingerprint. */
  1052. publish_cutoff = time(NULL) - ROUTER_MAX_AGE_TO_PUBLISH;
  1053. }
  1054. conn->spool = smartlist_new();
  1055. res = dirserv_get_routerdesc_spool(conn->spool, url,
  1056. source,
  1057. connection_dir_is_encrypted(conn),
  1058. &msg);
  1059. if (!strcmpstart(url, "all")) {
  1060. cache_lifetime = FULL_DIR_CACHE_LIFETIME;
  1061. } else if (smartlist_len(conn->spool) == 1) {
  1062. cache_lifetime = ROUTERDESC_BY_DIGEST_CACHE_LIFETIME;
  1063. }
  1064. size_t size_guess = 0;
  1065. int n_expired = 0;
  1066. dirserv_spool_remove_missing_and_guess_size(conn, publish_cutoff,
  1067. compress_method != NO_METHOD,
  1068. &size_guess, &n_expired);
  1069. /* If we are the bridge authority and the descriptor is a bridge
  1070. * descriptor, remember that we served this descriptor for desc stats. */
  1071. /* XXXX it's a bit of a kludge to have this here. */
  1072. if (get_options()->BridgeAuthoritativeDir &&
  1073. source == DIR_SPOOL_SERVER_BY_FP) {
  1074. SMARTLIST_FOREACH_BEGIN(conn->spool, spooled_resource_t *, spooled) {
  1075. const routerinfo_t *router =
  1076. router_get_by_id_digest((const char *)spooled->digest);
  1077. /* router can be NULL here when the bridge auth is asked for its own
  1078. * descriptor. */
  1079. if (router && router->purpose == ROUTER_PURPOSE_BRIDGE)
  1080. rep_hist_note_desc_served(router->cache_info.identity_digest);
  1081. } SMARTLIST_FOREACH_END(spooled);
  1082. }
  1083. if (res < 0 || size_guess == 0 || smartlist_len(conn->spool) == 0) {
  1084. if (msg == NULL)
  1085. msg = "Not found";
  1086. write_short_http_response(conn, 404, msg);
  1087. } else {
  1088. if (global_write_bucket_low(TO_CONN(conn), size_guess, 2)) {
  1089. log_info(LD_DIRSERV,
  1090. "Client asked for server descriptors, but we've been "
  1091. "writing too many bytes lately. Sending 503 Dir busy.");
  1092. write_short_http_response(conn, 503,
  1093. "Directory busy, try again later");
  1094. dir_conn_clear_spool(conn);
  1095. goto done;
  1096. }
  1097. write_http_response_header(conn, -1, compress_method, cache_lifetime);
  1098. if (compress_method != NO_METHOD)
  1099. conn->compress_state = tor_compress_new(1, compress_method,
  1100. choose_compression_level(size_guess));
  1101. clear_spool = 0;
  1102. /* Prime the connection with some data. */
  1103. int initial_flush_result = connection_dirserv_flushed_some(conn);
  1104. tor_assert_nonfatal(initial_flush_result == 0);
  1105. }
  1106. goto done;
  1107. }
  1108. done:
  1109. if (clear_spool)
  1110. dir_conn_clear_spool(conn);
  1111. return 0;
  1112. }
  1113. /** Helper function for GET /tor/keys/...
  1114. */
  1115. static int
  1116. handle_get_keys(dir_connection_t *conn, const get_handler_args_t *args)
  1117. {
  1118. const char *url = args->url;
  1119. const compress_method_t compress_method =
  1120. find_best_compression_method(args->compression_supported, 1);
  1121. const time_t if_modified_since = args->if_modified_since;
  1122. {
  1123. smartlist_t *certs = smartlist_new();
  1124. ssize_t len = -1;
  1125. if (!strcmp(url, "/tor/keys/all")) {
  1126. authority_cert_get_all(certs);
  1127. } else if (!strcmp(url, "/tor/keys/authority")) {
  1128. authority_cert_t *cert = get_my_v3_authority_cert();
  1129. if (cert)
  1130. smartlist_add(certs, cert);
  1131. } else if (!strcmpstart(url, "/tor/keys/fp/")) {
  1132. smartlist_t *fps = smartlist_new();
  1133. dir_split_resource_into_fingerprints(url+strlen("/tor/keys/fp/"),
  1134. fps, NULL,
  1135. DSR_HEX|DSR_SORT_UNIQ);
  1136. SMARTLIST_FOREACH(fps, char *, d, {
  1137. authority_cert_t *c = authority_cert_get_newest_by_id(d);
  1138. if (c) smartlist_add(certs, c);
  1139. tor_free(d);
  1140. });
  1141. smartlist_free(fps);
  1142. } else if (!strcmpstart(url, "/tor/keys/sk/")) {
  1143. smartlist_t *fps = smartlist_new();
  1144. dir_split_resource_into_fingerprints(url+strlen("/tor/keys/sk/"),
  1145. fps, NULL,
  1146. DSR_HEX|DSR_SORT_UNIQ);
  1147. SMARTLIST_FOREACH(fps, char *, d, {
  1148. authority_cert_t *c = authority_cert_get_by_sk_digest(d);
  1149. if (c) smartlist_add(certs, c);
  1150. tor_free(d);
  1151. });
  1152. smartlist_free(fps);
  1153. } else if (!strcmpstart(url, "/tor/keys/fp-sk/")) {
  1154. smartlist_t *fp_sks = smartlist_new();
  1155. dir_split_resource_into_fingerprint_pairs(url+strlen("/tor/keys/fp-sk/"),
  1156. fp_sks);
  1157. SMARTLIST_FOREACH(fp_sks, fp_pair_t *, pair, {
  1158. authority_cert_t *c = authority_cert_get_by_digests(pair->first,
  1159. pair->second);
  1160. if (c) smartlist_add(certs, c);
  1161. tor_free(pair);
  1162. });
  1163. smartlist_free(fp_sks);
  1164. } else {
  1165. write_short_http_response(conn, 400, "Bad request");
  1166. goto keys_done;
  1167. }
  1168. if (!smartlist_len(certs)) {
  1169. write_short_http_response(conn, 404, "Not found");
  1170. goto keys_done;
  1171. }
  1172. SMARTLIST_FOREACH(certs, authority_cert_t *, c,
  1173. if (c->cache_info.published_on < if_modified_since)
  1174. SMARTLIST_DEL_CURRENT(certs, c));
  1175. if (!smartlist_len(certs)) {
  1176. write_short_http_response(conn, 304, "Not modified");
  1177. goto keys_done;
  1178. }
  1179. len = 0;
  1180. SMARTLIST_FOREACH(certs, authority_cert_t *, c,
  1181. len += c->cache_info.signed_descriptor_len);
  1182. if (global_write_bucket_low(TO_CONN(conn),
  1183. compress_method != NO_METHOD ? len/2 : len,
  1184. 2)) {
  1185. write_short_http_response(conn, 503, "Directory busy, try again later");
  1186. goto keys_done;
  1187. }
  1188. write_http_response_header(conn,
  1189. compress_method != NO_METHOD ? -1 : len,
  1190. compress_method,
  1191. 60*60);
  1192. if (compress_method != NO_METHOD) {
  1193. conn->compress_state = tor_compress_new(1, compress_method,
  1194. choose_compression_level(len));
  1195. SMARTLIST_FOREACH(certs, authority_cert_t *, c,
  1196. connection_buf_add_compress(
  1197. c->cache_info.signed_descriptor_body,
  1198. c->cache_info.signed_descriptor_len,
  1199. conn, 0));
  1200. connection_buf_add_compress("", 0, conn, 1);
  1201. } else {
  1202. SMARTLIST_FOREACH(certs, authority_cert_t *, c,
  1203. connection_buf_add(c->cache_info.signed_descriptor_body,
  1204. c->cache_info.signed_descriptor_len,
  1205. TO_CONN(conn)));
  1206. }
  1207. keys_done:
  1208. smartlist_free(certs);
  1209. goto done;
  1210. }
  1211. done:
  1212. return 0;
  1213. }
  1214. /** Helper function for GET /tor/rendezvous2/
  1215. */
  1216. static int
  1217. handle_get_hs_descriptor_v2(dir_connection_t *conn,
  1218. const get_handler_args_t *args)
  1219. {
  1220. const char *url = args->url;
  1221. if (connection_dir_is_encrypted(conn)) {
  1222. /* Handle v2 rendezvous descriptor fetch request. */
  1223. const char *descp;
  1224. const char *query = url + strlen("/tor/rendezvous2/");
  1225. if (rend_valid_descriptor_id(query)) {
  1226. log_info(LD_REND, "Got a v2 rendezvous descriptor request for ID '%s'",
  1227. safe_str(escaped(query)));
  1228. switch (rend_cache_lookup_v2_desc_as_dir(query, &descp)) {
  1229. case 1: /* valid */
  1230. write_http_response_header(conn, strlen(descp), NO_METHOD, 0);
  1231. connection_buf_add(descp, strlen(descp), TO_CONN(conn));
  1232. break;
  1233. case 0: /* well-formed but not present */
  1234. write_short_http_response(conn, 404, "Not found");
  1235. break;
  1236. case -1: /* not well-formed */
  1237. write_short_http_response(conn, 400, "Bad request");
  1238. break;
  1239. }
  1240. } else { /* not well-formed */
  1241. write_short_http_response(conn, 400, "Bad request");
  1242. }
  1243. goto done;
  1244. } else {
  1245. /* Not encrypted! */
  1246. write_short_http_response(conn, 404, "Not found");
  1247. }
  1248. done:
  1249. return 0;
  1250. }
  1251. /** Helper function for GET /tor/hs/3/<z>. Only for version 3.
  1252. */
  1253. STATIC int
  1254. handle_get_hs_descriptor_v3(dir_connection_t *conn,
  1255. const get_handler_args_t *args)
  1256. {
  1257. int retval;
  1258. const char *desc_str = NULL;
  1259. const char *pubkey_str = NULL;
  1260. const char *url = args->url;
  1261. /* Reject unencrypted dir connections */
  1262. if (!connection_dir_is_encrypted(conn)) {
  1263. write_short_http_response(conn, 404, "Not found");
  1264. goto done;
  1265. }
  1266. /* After the path prefix follows the base64 encoded blinded pubkey which we
  1267. * use to get the descriptor from the cache. Skip the prefix and get the
  1268. * pubkey. */
  1269. tor_assert(!strcmpstart(url, "/tor/hs/3/"));
  1270. pubkey_str = url + strlen("/tor/hs/3/");
  1271. retval = hs_cache_lookup_as_dir(HS_VERSION_THREE,
  1272. pubkey_str, &desc_str);
  1273. if (retval <= 0 || desc_str == NULL) {
  1274. write_short_http_response(conn, 404, "Not found");
  1275. goto done;
  1276. }
  1277. /* Found requested descriptor! Pass it to this nice client. */
  1278. write_http_response_header(conn, strlen(desc_str), NO_METHOD, 0);
  1279. connection_buf_add(desc_str, strlen(desc_str), TO_CONN(conn));
  1280. done:
  1281. return 0;
  1282. }
  1283. /** Helper function for GET /tor/networkstatus-bridges
  1284. */
  1285. static int
  1286. handle_get_networkstatus_bridges(dir_connection_t *conn,
  1287. const get_handler_args_t *args)
  1288. {
  1289. const char *headers = args->headers;
  1290. const or_options_t *options = get_options();
  1291. if (options->BridgeAuthoritativeDir &&
  1292. options->BridgePassword_AuthDigest_ &&
  1293. connection_dir_is_encrypted(conn)) {
  1294. char *status;
  1295. char digest[DIGEST256_LEN];
  1296. char *header = http_get_header(headers, "Authorization: Basic ");
  1297. if (header)
  1298. crypto_digest256(digest, header, strlen(header), DIGEST_SHA256);
  1299. /* now make sure the password is there and right */
  1300. if (!header ||
  1301. tor_memneq(digest,
  1302. options->BridgePassword_AuthDigest_, DIGEST256_LEN)) {
  1303. write_short_http_response(conn, 404, "Not found");
  1304. tor_free(header);
  1305. goto done;
  1306. }
  1307. tor_free(header);
  1308. /* all happy now. send an answer. */
  1309. status = networkstatus_getinfo_by_purpose("bridge", time(NULL));
  1310. size_t dlen = strlen(status);
  1311. write_http_response_header(conn, dlen, NO_METHOD, 0);
  1312. connection_buf_add(status, dlen, TO_CONN(conn));
  1313. tor_free(status);
  1314. goto done;
  1315. }
  1316. done:
  1317. return 0;
  1318. }
  1319. /** Helper function for GET robots.txt or /tor/robots.txt */
  1320. static int
  1321. handle_get_robots(dir_connection_t *conn, const get_handler_args_t *args)
  1322. {
  1323. (void)args;
  1324. {
  1325. const char robots[] = "User-agent: *\r\nDisallow: /\r\n";
  1326. size_t len = strlen(robots);
  1327. write_http_response_header(conn, len, NO_METHOD, ROBOTS_CACHE_LIFETIME);
  1328. connection_buf_add(robots, len, TO_CONN(conn));
  1329. }
  1330. return 0;
  1331. }
  1332. /* Given the <b>url</b> from a POST request, try to extract the version number
  1333. * using the provided <b>prefix</b>. The version should be after the prefix and
  1334. * ending with the separator "/". For instance:
  1335. * /tor/hs/3/publish
  1336. *
  1337. * On success, <b>end_pos</b> points to the position right after the version
  1338. * was found. On error, it is set to NULL.
  1339. *
  1340. * Return version on success else negative value. */
  1341. STATIC int
  1342. parse_hs_version_from_post(const char *url, const char *prefix,
  1343. const char **end_pos)
  1344. {
  1345. int ok;
  1346. unsigned long version;
  1347. const char *start;
  1348. char *end = NULL;
  1349. tor_assert(url);
  1350. tor_assert(prefix);
  1351. tor_assert(end_pos);
  1352. /* Check if the prefix does start the url. */
  1353. if (strcmpstart(url, prefix)) {
  1354. goto err;
  1355. }
  1356. /* Move pointer to the end of the prefix string. */
  1357. start = url + strlen(prefix);
  1358. /* Try this to be the HS version and if we are still at the separator, next
  1359. * will be move to the right value. */
  1360. version = tor_parse_long(start, 10, 0, INT_MAX, &ok, &end);
  1361. if (!ok) {
  1362. goto err;
  1363. }
  1364. *end_pos = end;
  1365. return (int) version;
  1366. err:
  1367. *end_pos = NULL;
  1368. return -1;
  1369. }
  1370. /* Handle the POST request for a hidden service descripror. The request is in
  1371. * <b>url</b>, the body of the request is in <b>body</b>. Return 200 on success
  1372. * else return 400 indicating a bad request. */
  1373. STATIC int
  1374. handle_post_hs_descriptor(const char *url, const char *body)
  1375. {
  1376. int version;
  1377. const char *end_pos;
  1378. tor_assert(url);
  1379. tor_assert(body);
  1380. version = parse_hs_version_from_post(url, "/tor/hs/", &end_pos);
  1381. if (version < 0) {
  1382. goto err;
  1383. }
  1384. /* We have a valid version number, now make sure it's a publish request. Use
  1385. * the end position just after the version and check for the command. */
  1386. if (strcmpstart(end_pos, "/publish")) {
  1387. goto err;
  1388. }
  1389. switch (version) {
  1390. case HS_VERSION_THREE:
  1391. if (hs_cache_store_as_dir(body) < 0) {
  1392. goto err;
  1393. }
  1394. log_info(LD_REND, "Publish request for HS descriptor handled "
  1395. "successfully.");
  1396. break;
  1397. default:
  1398. /* Unsupported version, return a bad request. */
  1399. goto err;
  1400. }
  1401. return 200;
  1402. err:
  1403. /* Bad request. */
  1404. return 400;
  1405. }
  1406. /** Helper function: called when a dirserver gets a complete HTTP POST
  1407. * request. Look for an uploaded server descriptor or rendezvous
  1408. * service descriptor. On finding one, process it and write a
  1409. * response into conn-\>outbuf. If the request is unrecognized, send a
  1410. * 400. Always return 0. */
  1411. MOCK_IMPL(STATIC int,
  1412. directory_handle_command_post,(dir_connection_t *conn, const char *headers,
  1413. const char *body, size_t body_len))
  1414. {
  1415. char *url = NULL;
  1416. const or_options_t *options = get_options();
  1417. log_debug(LD_DIRSERV,"Received POST command.");
  1418. conn->base_.state = DIR_CONN_STATE_SERVER_WRITING;
  1419. if (!public_server_mode(options)) {
  1420. log_info(LD_DIR, "Rejected dir post request from %s "
  1421. "since we're not a public relay.", conn->base_.address);
  1422. write_short_http_response(conn, 503, "Not acting as a public relay");
  1423. goto done;
  1424. }
  1425. if (parse_http_url(headers, &url) < 0) {
  1426. write_short_http_response(conn, 400, "Bad request");
  1427. return 0;
  1428. }
  1429. log_debug(LD_DIRSERV,"rewritten url as '%s'.", escaped(url));
  1430. /* Handle v2 rendezvous service publish request. */
  1431. if (connection_dir_is_encrypted(conn) &&
  1432. !strcmpstart(url,"/tor/rendezvous2/publish")) {
  1433. if (rend_cache_store_v2_desc_as_dir(body) < 0) {
  1434. log_warn(LD_REND, "Rejected v2 rend descriptor (body size %d) from %s.",
  1435. (int)body_len, conn->base_.address);
  1436. write_short_http_response(conn, 400,
  1437. "Invalid v2 service descriptor rejected");
  1438. } else {
  1439. write_short_http_response(conn, 200, "Service descriptor (v2) stored");
  1440. log_info(LD_REND, "Handled v2 rendezvous descriptor post: accepted");
  1441. }
  1442. goto done;
  1443. }
  1444. /* Handle HS descriptor publish request. */
  1445. /* XXX: This should be disabled with a consensus param until we want to
  1446. * the prop224 be deployed and thus use. */
  1447. if (connection_dir_is_encrypted(conn) && !strcmpstart(url, "/tor/hs/")) {
  1448. const char *msg = "HS descriptor stored successfully.";
  1449. /* We most probably have a publish request for an HS descriptor. */
  1450. int code = handle_post_hs_descriptor(url, body);
  1451. if (code != 200) {
  1452. msg = "Invalid HS descriptor. Rejected.";
  1453. }
  1454. write_short_http_response(conn, code, msg);
  1455. goto done;
  1456. }
  1457. if (!authdir_mode(options)) {
  1458. /* we just provide cached directories; we don't want to
  1459. * receive anything. */
  1460. write_short_http_response(conn, 400, "Nonauthoritative directory does not "
  1461. "accept posted server descriptors");
  1462. goto done;
  1463. }
  1464. if (authdir_mode(options) &&
  1465. !strcmp(url,"/tor/")) { /* server descriptor post */
  1466. const char *msg = "[None]";
  1467. uint8_t purpose = authdir_mode_bridge(options) ?
  1468. ROUTER_PURPOSE_BRIDGE : ROUTER_PURPOSE_GENERAL;
  1469. was_router_added_t r = dirserv_add_multiple_descriptors(body, body_len,
  1470. purpose, conn->base_.address, &msg);
  1471. tor_assert(msg);
  1472. if (r == ROUTER_ADDED_SUCCESSFULLY) {
  1473. write_short_http_response(conn, 200, msg);
  1474. } else if (WRA_WAS_OUTDATED(r)) {
  1475. write_http_response_header_impl(conn, -1, NULL, NULL,
  1476. "X-Descriptor-Not-New: Yes\r\n", -1);
  1477. } else {
  1478. log_info(LD_DIRSERV,
  1479. "Rejected router descriptor or extra-info from %s "
  1480. "(\"%s\").",
  1481. conn->base_.address, msg);
  1482. write_short_http_response(conn, 400, msg);
  1483. }
  1484. goto done;
  1485. }
  1486. if (authdir_mode_v3(options) &&
  1487. !strcmp(url,"/tor/post/vote")) { /* v3 networkstatus vote */
  1488. const char *msg = "OK";
  1489. int status;
  1490. if (dirvote_add_vote(body, &msg, &status)) {
  1491. write_short_http_response(conn, status, "Vote stored");
  1492. } else {
  1493. tor_assert(msg);
  1494. log_warn(LD_DIRSERV, "Rejected vote from %s (\"%s\").",
  1495. conn->base_.address, msg);
  1496. write_short_http_response(conn, status, msg);
  1497. }
  1498. goto done;
  1499. }
  1500. if (authdir_mode_v3(options) &&
  1501. !strcmp(url,"/tor/post/consensus-signature")) { /* sigs on consensus. */
  1502. const char *msg = NULL;
  1503. if (dirvote_add_signatures(body, conn->base_.address, &msg)>=0) {
  1504. write_short_http_response(conn, 200, msg?msg:"Signatures stored");
  1505. } else {
  1506. log_warn(LD_DIR, "Unable to store signatures posted by %s: %s",
  1507. conn->base_.address, msg?msg:"???");
  1508. write_short_http_response(conn, 400,
  1509. msg?msg:"Unable to store signatures");
  1510. }
  1511. goto done;
  1512. }
  1513. /* we didn't recognize the url */
  1514. write_short_http_response(conn, 404, "Not found");
  1515. done:
  1516. tor_free(url);
  1517. return 0;
  1518. }
  1519. /** If <b>headers</b> indicates that a proxy was involved, then rewrite
  1520. * <b>conn</b>-\>address to describe our best guess of the address that
  1521. * originated this HTTP request. */
  1522. static void
  1523. http_set_address_origin(const char *headers, connection_t *conn)
  1524. {
  1525. char *fwd;
  1526. fwd = http_get_header(headers, "Forwarded-For: ");
  1527. if (!fwd)
  1528. fwd = http_get_header(headers, "X-Forwarded-For: ");
  1529. if (fwd) {
  1530. tor_addr_t toraddr;
  1531. if (tor_addr_parse(&toraddr,fwd) == -1 ||
  1532. tor_addr_is_internal(&toraddr,0)) {
  1533. log_debug(LD_DIR, "Ignoring local/internal IP %s", escaped(fwd));
  1534. tor_free(fwd);
  1535. return;
  1536. }
  1537. tor_free(conn->address);
  1538. conn->address = tor_strdup(fwd);
  1539. tor_free(fwd);
  1540. }
  1541. }
  1542. /** Called when a dirserver receives data on a directory connection;
  1543. * looks for an HTTP request. If the request is complete, remove it
  1544. * from the inbuf, try to process it; otherwise, leave it on the
  1545. * buffer. Return a 0 on success, or -1 on error.
  1546. */
  1547. int
  1548. directory_handle_command(dir_connection_t *conn)
  1549. {
  1550. char *headers=NULL, *body=NULL;
  1551. size_t body_len=0;
  1552. int r;
  1553. tor_assert(conn);
  1554. tor_assert(conn->base_.type == CONN_TYPE_DIR);
  1555. switch (connection_fetch_from_buf_http(TO_CONN(conn),
  1556. &headers, MAX_HEADERS_SIZE,
  1557. &body, &body_len, MAX_DIR_UL_SIZE, 0)) {
  1558. case -1: /* overflow */
  1559. log_warn(LD_DIRSERV,
  1560. "Request too large from address '%s' to DirPort. Closing.",
  1561. safe_str(conn->base_.address));
  1562. return -1;
  1563. case 0:
  1564. log_debug(LD_DIRSERV,"command not all here yet.");
  1565. return 0;
  1566. /* case 1, fall through */
  1567. }
  1568. http_set_address_origin(headers, TO_CONN(conn));
  1569. // we should escape headers here as well,
  1570. // but we can't call escaped() twice, as it uses the same buffer
  1571. //log_debug(LD_DIRSERV,"headers %s, body %s.", headers, escaped(body));
  1572. if (!strncasecmp(headers,"GET",3))
  1573. r = directory_handle_command_get(conn, headers, body, body_len);
  1574. else if (!strncasecmp(headers,"POST",4))
  1575. r = directory_handle_command_post(conn, headers, body, body_len);
  1576. else {
  1577. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  1578. "Got headers %s with unknown command. Closing.",
  1579. escaped(headers));
  1580. r = -1;
  1581. }
  1582. tor_free(headers); tor_free(body);
  1583. return r;
  1584. }