directory.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. /* Copyright 2001-2004 Roger Dingledine.
  2. * Copyright 2004 Roger Dingledine, Nick Mathewson. */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. const char directory_c_id[] = "$Id$";
  6. #include "or.h"
  7. /**
  8. * \file directory.c
  9. * \brief Implement directory HTTP protocol.
  10. **/
  11. /* In-points to directory.c:
  12. *
  13. * - directory_post_to_dirservers(), called from
  14. * router_upload_dir_desc_to_dirservers() in router.c
  15. * upload_service_descriptor() in rendservice.c
  16. * - directory_get_from_dirserver(), called from
  17. * rend_client_refetch_renddesc() in rendclient.c
  18. * run_scheduled_events() in main.c
  19. * do_hup() in main.c
  20. * - connection_dir_process_inbuf(), called from
  21. * connection_process_inbuf() in connection.c
  22. * - connection_dir_finished_flushing(), called from
  23. * connection_finished_flushing() in connection.c
  24. * - connection_dir_finished_connecting(), called from
  25. * connection_finished_connecting() in connection.c
  26. */
  27. static void
  28. directory_initiate_command_router(routerinfo_t *router, uint8_t purpose,
  29. const char *resource,
  30. const char *payload, size_t payload_len);
  31. static void
  32. directory_initiate_command_trusted_dir(trusted_dir_server_t *dirserv,
  33. uint8_t purpose, const char *resource,
  34. const char *payload, size_t payload_len);
  35. static void
  36. directory_initiate_command(const char *address, uint32_t addr, uint16_t port,
  37. const char *platform,
  38. const char *digest, uint8_t purpose,
  39. const char *resource,
  40. const char *payload, size_t payload_len);
  41. static void
  42. directory_send_command(connection_t *conn, const char *platform,
  43. int purpose, const char *resource,
  44. const char *payload, size_t payload_len);
  45. static int directory_handle_command(connection_t *conn);
  46. static int body_is_plausible(const char *body, size_t body_len, int purpose);
  47. /********* START VARIABLES **********/
  48. static addr_policy_t *dir_policy = NULL;
  49. #if 0 /* commented out for now, since for now what clients send is
  50. different from what servers want to receive */
  51. /** URL for publishing rendezvous descriptors. */
  52. char rend_publish_string[] = "/tor/rendezvous/publish";
  53. /** Prefix for downloading rendezvous descriptors. */
  54. char rend_fetch_url[] = "/tor/rendezvous/";
  55. #endif
  56. #define MAX_HEADERS_SIZE 50000
  57. #define MAX_BODY_SIZE 500000
  58. #define ALLOW_DIRECTORY_TIME_SKEW 30*60
  59. /********* END VARIABLES ************/
  60. /** Parse get_options()->DirPolicy, and put the processed version in
  61. * &dir_policy. Ignore port specifiers.
  62. */
  63. void
  64. parse_dir_policy(void)
  65. {
  66. addr_policy_t *n;
  67. if (dir_policy) {
  68. addr_policy_free(dir_policy);
  69. dir_policy = NULL;
  70. }
  71. config_parse_addr_policy(get_options()->DirPolicy, &dir_policy);
  72. /* ports aren't used. */
  73. for (n=dir_policy; n; n = n->next) {
  74. n->prt_min = 1;
  75. n->prt_max = 65535;
  76. }
  77. }
  78. void
  79. free_dir_policy(void)
  80. {
  81. addr_policy_free(dir_policy);
  82. dir_policy = NULL;
  83. }
  84. /** Return 1 if <b>addr</b> is permitted to connect to our dir port,
  85. * based on <b>dir_policy</b>. Else return 0.
  86. */
  87. int dir_policy_permits_address(uint32_t addr)
  88. {
  89. int a;
  90. if (!dir_policy) /* 'no dir policy' means 'accept' */
  91. return 1;
  92. a = router_compare_addr_to_addr_policy(addr, 1, dir_policy);
  93. if (a==-1)
  94. return 0;
  95. else if (a==0)
  96. return 1;
  97. tor_assert(a==1);
  98. log_fn(LOG_WARN, "Bug: got unexpected 'maybe' answer from dir policy");
  99. return 0;
  100. }
  101. /** Start a connection to every known directory server, using
  102. * connection purpose 'purpose' and uploading the payload 'payload'
  103. * (length 'payload_len'). The purpose should be one of
  104. * 'DIR_PURPOSE_UPLOAD_DIR' or 'DIR_PURPOSE_UPLOAD_RENDDESC'.
  105. */
  106. void
  107. directory_post_to_dirservers(uint8_t purpose, const char *payload,
  108. size_t payload_len)
  109. {
  110. smartlist_t *dirservers;
  111. router_get_trusted_dir_servers(&dirservers);
  112. tor_assert(dirservers);
  113. /* This tries dirservers which we believe to be down, but ultimately, that's
  114. * harmless, and we may as well err on the side of getting things uploaded.
  115. */
  116. SMARTLIST_FOREACH(dirservers, trusted_dir_server_t *, ds,
  117. {
  118. /* Pay attention to fascistfirewall when we're uploading a
  119. * router descriptor, but not when uploading a service
  120. * descriptor -- those use Tor. */
  121. if (get_options()->FascistFirewall && purpose == DIR_PURPOSE_UPLOAD_DIR &&
  122. !get_options()->HttpProxy) {
  123. if (!smartlist_string_num_isin(get_options()->FirewallPorts, ds->dir_port))
  124. continue;
  125. }
  126. directory_initiate_command_trusted_dir(ds, purpose, NULL,
  127. payload, payload_len);
  128. });
  129. }
  130. /** Start a connection to a random running directory server, using
  131. * connection purpose 'purpose' requesting 'resource'. The purpose
  132. * should be one of 'DIR_PURPOSE_FETCH_DIR',
  133. * 'DIR_PURPOSE_FETCH_RENDDESC', 'DIR_PURPOSE_FETCH_RUNNING_LIST.'
  134. * If <b>retry_if_no_servers</b>, then if all the possible servers seem
  135. * down, mark them up and try again.
  136. */
  137. void
  138. directory_get_from_dirserver(uint8_t purpose, const char *resource,
  139. int retry_if_no_servers)
  140. {
  141. routerinfo_t *r = NULL;
  142. trusted_dir_server_t *ds = NULL;
  143. int fascistfirewall = get_options()->FascistFirewall;
  144. int directconn = purpose == DIR_PURPOSE_FETCH_DIR ||
  145. purpose == DIR_PURPOSE_FETCH_RUNNING_LIST;
  146. int fetch_fresh_first = advertised_server_mode();
  147. if (directconn) {
  148. if (fetch_fresh_first) {
  149. /* only ask authdirservers, and don't ask myself */
  150. ds = router_pick_trusteddirserver(1, fascistfirewall,
  151. retry_if_no_servers);
  152. }
  153. if (!ds) {
  154. /* anybody with a non-zero dirport will do */
  155. r = router_pick_directory_server(1, fascistfirewall,
  156. purpose==DIR_PURPOSE_FETCH_RUNNING_LIST,
  157. retry_if_no_servers);
  158. if (!r) {
  159. log_fn(LOG_INFO, "No router found for %s; falling back to dirserver list",
  160. purpose == DIR_PURPOSE_FETCH_RUNNING_LIST
  161. ? "status list" : "directory");
  162. ds = router_pick_trusteddirserver(1, fascistfirewall,
  163. retry_if_no_servers);
  164. }
  165. }
  166. } else { // (purpose == DIR_PURPOSE_FETCH_RENDDESC)
  167. /* only ask authdirservers, any of them will do */
  168. /* Never use fascistfirewall; we're going via Tor. */
  169. ds = router_pick_trusteddirserver(0, 0, retry_if_no_servers);
  170. }
  171. if (r)
  172. directory_initiate_command_router(r, purpose, resource, NULL, 0);
  173. else if (ds)
  174. directory_initiate_command_trusted_dir(ds, purpose, resource, NULL, 0);
  175. else {
  176. log_fn(LOG_NOTICE,"No running dirservers known. Not trying. (purpose %d)",
  177. purpose);
  178. if (directconn) {
  179. /* remember we tried them all and failed. */
  180. directory_all_unreachable(time(NULL));
  181. }
  182. }
  183. }
  184. /** Launch a new connection to the directory server <b>router</b> to upload or
  185. * download a service or rendezvous descriptor. <b>purpose</b> determines what
  186. * kind of directory connection we're launching, and must be one of
  187. * DIR_PURPOSE_{FETCH|UPLOAD}_{DIR|RENDDESC}.
  188. *
  189. * When uploading, <b>payload</b> and <b>payload_len</b> determine the content
  190. * of the HTTP post. Otherwise, <b>payload</b> should be NULL.
  191. *
  192. * When fetching a rendezvous descriptor, <b>resource</b> is the service ID we
  193. * want to fetch.
  194. */
  195. static void
  196. directory_initiate_command_router(routerinfo_t *router, uint8_t purpose,
  197. const char *resource,
  198. const char *payload, size_t payload_len)
  199. {
  200. directory_initiate_command(router->address, router->addr, router->dir_port,
  201. router->platform, router->identity_digest,
  202. purpose, resource, payload, payload_len);
  203. }
  204. /** As directory_initiate_command_router, but send the command to a trusted
  205. * directory server <b>dirserv</b>. **/
  206. static void
  207. directory_initiate_command_trusted_dir(trusted_dir_server_t *dirserv,
  208. uint8_t purpose, const char *resource,
  209. const char *payload, size_t payload_len)
  210. {
  211. directory_initiate_command(dirserv->address, dirserv->addr,dirserv->dir_port,
  212. NULL, dirserv->digest, purpose, resource, payload, payload_len);
  213. }
  214. /** Called when we are unable to complete our connection to a
  215. * directory server: Mark the router as down and try again if possible.
  216. */
  217. void
  218. connection_dir_connect_failed(connection_t *conn)
  219. {
  220. router_mark_as_down(conn->identity_digest); /* don't try him again */
  221. if (conn->purpose == DIR_PURPOSE_FETCH_DIR ||
  222. conn->purpose == DIR_PURPOSE_FETCH_RUNNING_LIST) {
  223. log_fn(LOG_INFO, "Giving up on directory server at '%s'; retrying",
  224. conn->address);
  225. directory_get_from_dirserver(conn->purpose, NULL,
  226. 0 /* don't retry_if_no_servers */);
  227. }
  228. }
  229. /** Helper for directory_initiate_command_(router|trusted_dir): send the
  230. * command to a server whose address is <b>address</b>, whose IP is
  231. * <b>addr</b>, whose directory port is <b>dir_port</b>, whose tor version is
  232. * <b>platform</b>, and whose identity key digest is <b>digest</b>. The
  233. * <b>platform</b> argument is optional; the others are required. */
  234. static void
  235. directory_initiate_command(const char *address, uint32_t addr,
  236. uint16_t dir_port, const char *platform,
  237. const char *digest, uint8_t purpose,
  238. const char *resource,
  239. const char *payload, size_t payload_len)
  240. {
  241. connection_t *conn;
  242. tor_assert(address);
  243. tor_assert(addr);
  244. tor_assert(dir_port);
  245. tor_assert(digest);
  246. switch (purpose) {
  247. case DIR_PURPOSE_FETCH_DIR:
  248. log_fn(LOG_DEBUG,"initiating directory fetch");
  249. break;
  250. case DIR_PURPOSE_FETCH_RENDDESC:
  251. log_fn(LOG_DEBUG,"initiating hidden-service descriptor fetch");
  252. break;
  253. case DIR_PURPOSE_UPLOAD_DIR:
  254. log_fn(LOG_DEBUG,"initiating server descriptor upload");
  255. break;
  256. case DIR_PURPOSE_UPLOAD_RENDDESC:
  257. log_fn(LOG_DEBUG,"initiating hidden-service descriptor upload");
  258. break;
  259. case DIR_PURPOSE_FETCH_RUNNING_LIST:
  260. log_fn(LOG_DEBUG,"initiating running-routers fetch");
  261. break;
  262. default:
  263. log_fn(LOG_ERR, "Unrecognized directory connection purpose.");
  264. tor_assert(0);
  265. }
  266. conn = connection_new(CONN_TYPE_DIR);
  267. /* set up conn so it's got all the data we need to remember */
  268. conn->addr = addr;
  269. conn->port = dir_port;
  270. if (get_options()->HttpProxy) {
  271. addr = get_options()->HttpProxyAddr;
  272. dir_port = get_options()->HttpProxyPort;
  273. }
  274. conn->address = tor_strdup(address);
  275. /* conn->nickname = tor_strdup(router->nickname); */
  276. /* tor_assert(router->identity_pkey); */
  277. /* conn->identity_pkey = crypto_pk_dup_key(router->identity_pkey); */
  278. /* crypto_pk_get_digest(conn->identity_pkey, conn->identity_digest); */
  279. memcpy(conn->identity_digest, digest, DIGEST_LEN);
  280. conn->purpose = purpose;
  281. /* give it an initial state */
  282. conn->state = DIR_CONN_STATE_CONNECTING;
  283. if (purpose == DIR_PURPOSE_FETCH_DIR ||
  284. purpose == DIR_PURPOSE_UPLOAD_DIR ||
  285. purpose == DIR_PURPOSE_FETCH_RUNNING_LIST) {
  286. /* then we want to connect directly */
  287. switch (connection_connect(conn, conn->address, addr, dir_port)) {
  288. case -1:
  289. connection_dir_connect_failed(conn);
  290. connection_free(conn);
  291. return;
  292. case 1:
  293. conn->state = DIR_CONN_STATE_CLIENT_SENDING; /* start flushing conn */
  294. /* fall through */
  295. case 0:
  296. /* queue the command on the outbuf */
  297. directory_send_command(conn, platform, purpose, resource,
  298. payload, payload_len);
  299. connection_watch_events(conn, EV_READ | EV_WRITE);
  300. /* writable indicates finish, readable indicates broken link,
  301. error indicates broken link in windowsland. */
  302. }
  303. } else { /* we want to connect via tor */
  304. /* make an AP connection
  305. * populate it and add it at the right state
  306. * socketpair and hook up both sides
  307. */
  308. conn->s = connection_ap_make_bridge(conn->address, conn->port);
  309. if (conn->s < 0) {
  310. log_fn(LOG_WARN,"Making AP bridge to dirserver failed.");
  311. connection_mark_for_close(conn);
  312. return;
  313. }
  314. conn->state = DIR_CONN_STATE_CLIENT_SENDING;
  315. connection_add(conn);
  316. /* queue the command on the outbuf */
  317. directory_send_command(conn, platform, purpose, resource,
  318. payload, payload_len);
  319. connection_watch_events(conn, EV_READ | EV_WRITE);
  320. }
  321. }
  322. /** Queue an appropriate HTTP command on conn-\>outbuf. The other args
  323. * are as in directory_initiate_command.
  324. */
  325. static void
  326. directory_send_command(connection_t *conn, const char *platform,
  327. int purpose, const char *resource,
  328. const char *payload, size_t payload_len) {
  329. char tmp[8192];
  330. char proxystring[128];
  331. char hoststring[128];
  332. char url[128];
  333. int use_newer = 0;
  334. const char *httpcommand = NULL;
  335. tor_assert(conn);
  336. tor_assert(conn->type == CONN_TYPE_DIR);
  337. /* If we don't know the platform, assume it's up-to-date. */
  338. use_newer = platform ? tor_version_as_new_as(platform, "0.0.9pre1"):1;
  339. if (conn->port == 80) {
  340. strlcpy(hoststring, conn->address, sizeof(hoststring));
  341. } else {
  342. tor_snprintf(hoststring, sizeof(hoststring),"%s:%d",conn->address, conn->port);
  343. }
  344. if (get_options()->HttpProxy) {
  345. tor_snprintf(proxystring, sizeof(proxystring),"http://%s", hoststring);
  346. } else {
  347. proxystring[0] = 0;
  348. }
  349. switch (purpose) {
  350. case DIR_PURPOSE_FETCH_DIR:
  351. tor_assert(!resource);
  352. tor_assert(!payload);
  353. log_fn(LOG_DEBUG, "Asking for %scompressed directory from server running %s",
  354. use_newer?"":"un", platform?platform:"<unknown version>");
  355. httpcommand = "GET";
  356. strlcpy(url, use_newer ? "/tor/dir.z" : "/", sizeof(url));
  357. break;
  358. case DIR_PURPOSE_FETCH_RUNNING_LIST:
  359. tor_assert(!resource);
  360. tor_assert(!payload);
  361. httpcommand = "GET";
  362. strlcpy(url, use_newer ? "/tor/running-routers" : "/running-routers", sizeof(url));
  363. break;
  364. case DIR_PURPOSE_UPLOAD_DIR:
  365. tor_assert(!resource);
  366. tor_assert(payload);
  367. httpcommand = "POST";
  368. strlcpy(url, use_newer ? "/tor/" : "/", sizeof(url));
  369. break;
  370. case DIR_PURPOSE_FETCH_RENDDESC:
  371. tor_assert(resource);
  372. tor_assert(!payload);
  373. /* this must be true or we wouldn't be doing the lookup */
  374. tor_assert(strlen(resource) <= REND_SERVICE_ID_LEN);
  375. /* This breaks the function abstraction. */
  376. strlcpy(conn->rend_query, resource, sizeof(conn->rend_query));
  377. httpcommand = "GET";
  378. tor_snprintf(url, sizeof(url), "%s/rendezvous/%s", use_newer ? "/tor" : "", resource);
  379. break;
  380. case DIR_PURPOSE_UPLOAD_RENDDESC:
  381. tor_assert(!resource);
  382. tor_assert(payload);
  383. httpcommand = "POST";
  384. tor_snprintf(url, sizeof(url), "%s/rendezvous/publish", use_newer ? "/tor" : "");
  385. break;
  386. }
  387. tor_snprintf(tmp, sizeof(tmp), "%s %s%s HTTP/1.0\r\nContent-Length: %lu\r\nHost: %s\r\n\r\n",
  388. httpcommand,
  389. proxystring,
  390. url,
  391. payload ? (unsigned long)payload_len : 0,
  392. hoststring);
  393. connection_write_to_buf(tmp, strlen(tmp), conn);
  394. if (payload) {
  395. /* then send the payload afterwards too */
  396. connection_write_to_buf(payload, payload_len, conn);
  397. }
  398. }
  399. /** Parse an HTTP request string <b>headers</b> of the form
  400. * "\%s [http[s]://]\%s HTTP/1..."
  401. * If it's well-formed, strdup the second \%s into *<b>url</b>, and
  402. * null-terminate it. If the url doesn't start with "/tor/", rewrite it
  403. * so it does. Return 0.
  404. * Otherwise, return -1.
  405. */
  406. static int
  407. parse_http_url(char *headers, char **url)
  408. {
  409. char *s, *start, *tmp;
  410. s = (char *)eat_whitespace_no_nl(headers);
  411. if (!*s) return -1;
  412. s = (char *)find_whitespace(s); /* get past GET/POST */
  413. if (!*s) return -1;
  414. s = (char *)eat_whitespace_no_nl(s);
  415. if (!*s) return -1;
  416. start = s; /* this is it, assuming it's valid */
  417. s = (char *)find_whitespace(start);
  418. if (!*s) return -1;
  419. /* tolerate the http[s] proxy style of putting the hostname in the url */
  420. if (s-start >= 4 && !strcmpstart(start,"http")) {
  421. tmp = start + 4;
  422. if (*tmp == 's')
  423. tmp++;
  424. if (s-tmp >= 3 && !strcmpstart(tmp,"://")) {
  425. tmp = strchr(tmp+3, '/');
  426. if (tmp && tmp < s) {
  427. log_fn(LOG_DEBUG,"Skipping over 'http[s]://hostname' string");
  428. start = tmp;
  429. }
  430. }
  431. }
  432. if (s-start < 5 || strcmpstart(start,"/tor/")) { /* need to rewrite it */
  433. *url = tor_malloc(s - start + 5);
  434. strlcpy(*url,"/tor", s-start+5);
  435. strlcat((*url)+4, start, s-start+1);
  436. } else {
  437. *url = tor_strndup(start, s-start);
  438. }
  439. return 0;
  440. }
  441. /** Parse an HTTP response string <b>headers</b> of the form
  442. * "HTTP/1.\%d \%d\%s\r\n...".
  443. * If it's well-formed, assign *<b>code</b> and return 0.
  444. * If <b>date</b> is provided, set *date to the Date header in the
  445. * http headers, or 0 if no such header is found. If <b>compression</b>
  446. * is provided, set *<b>compression</b> to the compression method given
  447. * in the Content-Encoding header, or 0 if no such header is found, or -1
  448. * if the value of the header is not recognized.
  449. * Otherwise, return -1.
  450. */
  451. static int
  452. parse_http_response(const char *headers, int *code, time_t *date,
  453. int *compression)
  454. {
  455. int n1, n2;
  456. char datestr[RFC1123_TIME_LEN+1];
  457. smartlist_t *parsed_headers;
  458. tor_assert(headers);
  459. tor_assert(code);
  460. while (TOR_ISSPACE(*headers)) headers++; /* tolerate leading whitespace */
  461. if (sscanf(headers, "HTTP/1.%d %d", &n1, &n2) < 2 ||
  462. (n1 != 0 && n1 != 1) ||
  463. (n2 < 100 || n2 >= 600)) {
  464. log_fn(LOG_WARN,"Failed to parse header '%s'",headers);
  465. return -1;
  466. }
  467. *code = n2;
  468. parsed_headers = smartlist_create();
  469. smartlist_split_string(parsed_headers, headers, "\n",
  470. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
  471. if (date) {
  472. *date = 0;
  473. SMARTLIST_FOREACH(parsed_headers, const char *, s,
  474. if (!strcmpstart(s, "Date: ")) {
  475. strlcpy(datestr, s+6, sizeof(datestr));
  476. /* This will do nothing on failure, so we don't need to check
  477. the result. We shouldn't warn, since there are many other valid
  478. date formats besides the one we use. */
  479. parse_rfc1123_time(datestr, date);
  480. break;
  481. });
  482. }
  483. if (compression) {
  484. const char *enc = NULL;
  485. SMARTLIST_FOREACH(parsed_headers, const char *, s,
  486. if (!strcmpstart(s, "Content-Encoding: ")) {
  487. enc = s+18; break;
  488. });
  489. if (!enc || !strcmp(enc, "identity")) {
  490. *compression = 0;
  491. } else if (!strcmp(enc, "deflate") || !strcmp(enc, "x-deflate")) {
  492. *compression = ZLIB_METHOD;
  493. } else if (!strcmp(enc, "gzip") || !strcmp(enc, "x-gzip")) {
  494. *compression = GZIP_METHOD;
  495. } else {
  496. log_fn(LOG_INFO, "Unrecognized content encoding: '%s'. Trying to deal.", enc);
  497. *compression = -1;
  498. }
  499. }
  500. SMARTLIST_FOREACH(parsed_headers, char *, s, tor_free(s));
  501. smartlist_free(parsed_headers);
  502. return 0;
  503. }
  504. /** Return true iff <b>body</b> doesn't start with a plausible router or
  505. * running-list or directory opening. This is a sign of possible compression.
  506. **/
  507. static int
  508. body_is_plausible(const char *body, size_t len, int purpose)
  509. {
  510. int i;
  511. if (len == 0)
  512. return 1; /* empty bodies don't need decompression */
  513. if (len < 32)
  514. return 0;
  515. if (purpose != DIR_PURPOSE_FETCH_RENDDESC) {
  516. if (!strcmpstart(body,"router") ||
  517. !strcmpstart(body,"signed-directory") ||
  518. !strcmpstart(body,"network-status") ||
  519. !strcmpstart(body,"running-routers"))
  520. return 1;
  521. for (i=0;i<32;++i) {
  522. if (!TOR_ISPRINT(body[i]) && !TOR_ISSPACE(body[i]))
  523. return 0;
  524. }
  525. return 1;
  526. } else {
  527. return 1;
  528. }
  529. }
  530. /** We are a client, and we've finished reading the server's
  531. * response. Parse and it and act appropriately.
  532. *
  533. * Return -1 if an error has occurred, or 0 normally. The caller
  534. * will take care of marking the connection for close.
  535. */
  536. static int
  537. connection_dir_client_reached_eof(connection_t *conn)
  538. {
  539. char *body;
  540. char *headers;
  541. size_t body_len=0;
  542. int status_code;
  543. time_t now, date_header=0;
  544. int delta;
  545. int compression;
  546. int plausible;
  547. int skewed=0;
  548. switch (fetch_from_buf_http(conn->inbuf,
  549. &headers, MAX_HEADERS_SIZE,
  550. &body, &body_len, MAX_DIR_SIZE)) {
  551. case -1: /* overflow */
  552. log_fn(LOG_WARN,"'fetch' response too large (server '%s'). Failing.", conn->address);
  553. return -1;
  554. case 0:
  555. log_fn(LOG_INFO,"'fetch' response not all here, but we're at eof. Closing.");
  556. return -1;
  557. /* case 1, fall through */
  558. }
  559. if (parse_http_response(headers, &status_code, &date_header,
  560. &compression) < 0) {
  561. log_fn(LOG_WARN,"Unparseable headers (server '%s'). Closing.", conn->address);
  562. tor_free(body); tor_free(headers);
  563. return -1;
  564. }
  565. if (date_header > 0) {
  566. now = time(NULL);
  567. delta = now-date_header;
  568. if (abs(delta)>ALLOW_DIRECTORY_TIME_SKEW) {
  569. routerinfo_t *router = router_get_by_digest(conn->identity_digest);
  570. log_fn((router && router->is_verified) ? LOG_WARN : LOG_INFO,
  571. "Received directory with skewed time (server '%s'): we are %d minutes %s, or the directory is %d minutes %s.",
  572. conn->address,
  573. abs(delta)/60, delta>0 ? "ahead" : "behind",
  574. abs(delta)/60, delta>0 ? "behind" : "ahead");
  575. skewed = 1; /* don't check the recommended-versions line */
  576. } else {
  577. log_fn(LOG_INFO, "Time on received directory is within tolerance; we are %d seconds skewed. (That's okay.)", delta);
  578. }
  579. }
  580. plausible = body_is_plausible(body, body_len, conn->purpose);
  581. if (compression || !plausible) {
  582. char *new_body = NULL;
  583. size_t new_len = 0;
  584. int guessed = detect_compression_method(body, body_len);
  585. if (compression <= 0 || guessed != compression) {
  586. /* Tell the user if we don't believe what we're told about compression.*/
  587. const char *description1, *description2;
  588. if (compression == ZLIB_METHOD)
  589. description1 = "as deflated";
  590. else if (compression == GZIP_METHOD)
  591. description1 = "as gzipped";
  592. else if (compression == 0)
  593. description1 = "as uncompressed";
  594. else
  595. description1 = "with an unknown Content-Encoding";
  596. if (guessed == ZLIB_METHOD)
  597. description2 = "deflated";
  598. else if (guessed == GZIP_METHOD)
  599. description2 = "gzipped";
  600. else if (!plausible)
  601. description2 = "confusing binary junk";
  602. else
  603. description2 = "uncompressed";
  604. log_fn(LOG_INFO, "HTTP body from server '%s' was labeled %s,"
  605. "but it seems to be %s.%s",
  606. conn->address, description1, description2,
  607. (compression>0 && guessed>0)?" Trying both.":"");
  608. }
  609. /* Try declared compression first if we can. */
  610. if (compression > 0)
  611. tor_gzip_uncompress(&new_body, &new_len, body, body_len, compression);
  612. /* Okay, if that didn't work, and we think that it was compressed
  613. * differently, try that. */
  614. if (!new_body && guessed > 0 && compression != guessed)
  615. tor_gzip_uncompress(&new_body, &new_len, body, body_len, guessed);
  616. /* If we're pretty sure that we have a compressed directory, and
  617. * we didn't manage to uncompress it, then warn and bail. */
  618. if (!plausible && !new_body) {
  619. log_fn(LOG_WARN, "Unable to decompress HTTP body (server '%s').", conn->address);
  620. tor_free(body); tor_free(headers);
  621. return -1;
  622. }
  623. if (new_body) {
  624. tor_free(body);
  625. body = new_body;
  626. body_len = new_len;
  627. }
  628. }
  629. if (conn->purpose == DIR_PURPOSE_FETCH_DIR) {
  630. /* fetch/process the directory to learn about new routers. */
  631. log_fn(LOG_INFO,"Received directory (size %d) from server '%s'",
  632. (int)body_len, conn->address);
  633. if (status_code == 503 || body_len == 0) {
  634. log_fn(LOG_INFO,"Empty directory. Ignoring.");
  635. tor_free(body); tor_free(headers);
  636. return 0;
  637. }
  638. if (status_code != 200) {
  639. log_fn(LOG_WARN,"Received http status code %d from server '%s'. Failing.",
  640. status_code, conn->address);
  641. tor_free(body); tor_free(headers);
  642. return -1;
  643. }
  644. if (router_load_routerlist_from_directory(body, NULL, !skewed, 0) < 0) {
  645. log_fn(LOG_NOTICE,"I failed to parse the directory I fetched from %s:%d. Ignoring.", conn->address, conn->port);
  646. } else {
  647. log_fn(LOG_INFO,"updated routers.");
  648. }
  649. directory_has_arrived(time(NULL)); /* do things we've been waiting to do */
  650. }
  651. if (conn->purpose == DIR_PURPOSE_FETCH_RUNNING_LIST) {
  652. running_routers_t *rrs;
  653. routerlist_t *rl;
  654. /* just update our list of running routers, if this list is new info */
  655. log_fn(LOG_INFO,"Received running-routers list (size %d)", (int)body_len);
  656. if (status_code != 200) {
  657. log_fn(LOG_WARN,"Received http status code %d from server '%s'. Failing.",
  658. status_code, conn->address);
  659. tor_free(body); tor_free(headers);
  660. return -1;
  661. }
  662. if (!(rrs = router_parse_runningrouters(body, 1))) {
  663. log_fn(LOG_WARN, "Can't parse runningrouters list (server '%s')", conn->address);
  664. tor_free(body); tor_free(headers);
  665. return -1;
  666. }
  667. router_get_routerlist(&rl);
  668. if (rl)
  669. routerlist_update_from_runningrouters(rl,rrs);
  670. running_routers_free(rrs);
  671. }
  672. if (conn->purpose == DIR_PURPOSE_UPLOAD_DIR) {
  673. switch (status_code) {
  674. case 200:
  675. log_fn(LOG_INFO,"eof (status 200) after uploading server descriptor: finished.");
  676. break;
  677. case 400:
  678. log_fn(LOG_WARN,"http status 400 (bad request) response from dirserver '%s'. Malformed server descriptor?", conn->address);
  679. break;
  680. case 403:
  681. log_fn(LOG_WARN,"http status 403 (unapproved server) response from dirserver '%s'. Is your clock skewed? Have you mailed us your key fingerprint? Are you using the right key? Are you using a private IP address? See http://tor.eff.org/doc/tor-doc.html#server.", conn->address);
  682. break;
  683. default:
  684. log_fn(LOG_WARN,"http status %d response unrecognized (server '%s').", status_code, conn->address);
  685. break;
  686. }
  687. }
  688. if (conn->purpose == DIR_PURPOSE_FETCH_RENDDESC) {
  689. log_fn(LOG_INFO,"Received rendezvous descriptor (size %d, status code %d)",
  690. (int)body_len, status_code);
  691. switch (status_code) {
  692. case 200:
  693. if (rend_cache_store(body, body_len) < 0) {
  694. log_fn(LOG_WARN,"Failed to store rendezvous descriptor.");
  695. /* alice's ap_stream will notice when connection_mark_for_close
  696. * cleans it up */
  697. } else {
  698. /* success. notify pending connections about this. */
  699. conn->purpose = DIR_PURPOSE_HAS_FETCHED_RENDDESC;
  700. rend_client_desc_here(conn->rend_query);
  701. }
  702. break;
  703. case 404:
  704. /* not there. pending connections will be notified when
  705. * connection_mark_for_close cleans it up. */
  706. break;
  707. case 400:
  708. log_fn(LOG_WARN,"http status 400 (bad request). Dirserver didn't like our rendezvous query?");
  709. break;
  710. }
  711. }
  712. if (conn->purpose == DIR_PURPOSE_UPLOAD_RENDDESC) {
  713. switch (status_code) {
  714. case 200:
  715. log_fn(LOG_INFO,"eof (status 200) after uploading rendezvous descriptor: finished.");
  716. break;
  717. case 400:
  718. log_fn(LOG_WARN,"http status 400 (bad request) response from dirserver. Malformed rendezvous descriptor?");
  719. break;
  720. default:
  721. log_fn(LOG_WARN,"http status %d response unrecognized.", status_code);
  722. break;
  723. }
  724. }
  725. tor_free(body); tor_free(headers);
  726. return 0;
  727. }
  728. int connection_dir_reached_eof(connection_t *conn) {
  729. int retval;
  730. if (conn->state != DIR_CONN_STATE_CLIENT_READING) {
  731. log_fn(LOG_INFO,"conn reached eof, not reading. Closing.");
  732. connection_close_immediate(conn); /* it was an error; give up on flushing */
  733. connection_mark_for_close(conn);
  734. return -1;
  735. }
  736. retval = connection_dir_client_reached_eof(conn);
  737. connection_mark_for_close(conn);
  738. return retval;
  739. }
  740. /** Read handler for directory connections. (That's connections <em>to</em>
  741. * directory servers and connections <em>at</em> directory servers.)
  742. */
  743. int connection_dir_process_inbuf(connection_t *conn) {
  744. tor_assert(conn);
  745. tor_assert(conn->type == CONN_TYPE_DIR);
  746. /* Directory clients write, then read data until they receive EOF;
  747. * directory servers read data until they get an HTTP command, then
  748. * write their response (when it's finished flushing, they mark for
  749. * close).
  750. */
  751. /* If we're on the dirserver side, look for a command. */
  752. if (conn->state == DIR_CONN_STATE_SERVER_COMMAND_WAIT) {
  753. if (directory_handle_command(conn) < 0) {
  754. connection_mark_for_close(conn);
  755. return -1;
  756. }
  757. return 0;
  758. }
  759. /* XXX for READ states, might want to make sure inbuf isn't too big */
  760. log_fn(LOG_DEBUG,"Got data, not eof. Leaving on inbuf.");
  761. return 0;
  762. }
  763. /** Create an http response for the client <b>conn</b> out of
  764. * <b>status</b> and <b>reason_phrase</b>. Write it to <b>conn</b>.
  765. */
  766. static void
  767. write_http_status_line(connection_t *conn, int status,
  768. const char *reason_phrase)
  769. {
  770. char buf[256];
  771. if (tor_snprintf(buf, sizeof(buf), "HTTP/1.0 %d %s\r\n\r\n",
  772. status, reason_phrase) < 0) {
  773. log_fn(LOG_WARN,"Bug: status line too long.");
  774. return;
  775. }
  776. connection_write_to_buf(buf, strlen(buf), conn);
  777. }
  778. /** Helper function: called when a dirserver gets a complete HTTP GET
  779. * request. Look for a request for a directory or for a rendezvous
  780. * service descriptor. On finding one, write a response into
  781. * conn-\>outbuf. If the request is unrecognized, send a 400.
  782. * Always return 0. */
  783. static int
  784. directory_handle_command_get(connection_t *conn, char *headers,
  785. char *body, size_t body_len)
  786. {
  787. size_t dlen;
  788. const char *cp;
  789. char *url;
  790. char tmp[8192];
  791. char date[RFC1123_TIME_LEN+1];
  792. log_fn(LOG_DEBUG,"Received GET command.");
  793. conn->state = DIR_CONN_STATE_SERVER_WRITING;
  794. if (parse_http_url(headers, &url) < 0) {
  795. write_http_status_line(conn, 400, "Bad request");
  796. return 0;
  797. }
  798. log_fn(LOG_INFO,"rewritten url as '%s'.", url);
  799. if (!strcmp(url,"/tor/") || !strcmp(url,"/tor/dir.z")) { /* directory fetch */
  800. int deflated = !strcmp(url,"/tor/dir.z");
  801. dlen = dirserv_get_directory(&cp, deflated);
  802. tor_free(url);
  803. if (dlen == 0) {
  804. log_fn(LOG_NOTICE,"Client asked for the mirrored directory, but we don't have a good one yet. Sending 503 Dir not available.");
  805. write_http_status_line(conn, 503, "Directory unavailable");
  806. /* try to get a new one now */
  807. if (!connection_get_by_type_purpose(CONN_TYPE_DIR, DIR_PURPOSE_FETCH_DIR))
  808. directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL, 1);
  809. return 0;
  810. }
  811. log_fn(LOG_DEBUG,"Dumping %sdirectory to client.",
  812. deflated?"deflated ":"");
  813. format_rfc1123_time(date, time(NULL));
  814. tor_snprintf(tmp, sizeof(tmp), "HTTP/1.0 200 OK\r\nDate: %s\r\nContent-Length: %d\r\nContent-Type: text/plain\r\nContent-Encoding: %s\r\n\r\n",
  815. date,
  816. (int)dlen,
  817. deflated?"deflate":"identity");
  818. connection_write_to_buf(tmp, strlen(tmp), conn);
  819. connection_write_to_buf(cp, dlen, conn);
  820. return 0;
  821. }
  822. if (!strcmp(url,"/tor/running-routers") ||
  823. !strcmp(url,"/tor/running-routers.z")) { /* running-routers fetch */
  824. int deflated = !strcmp(url,"/tor/dir.z");
  825. tor_free(url);
  826. dlen = dirserv_get_runningrouters(&cp, deflated);
  827. if (!dlen) { /* we failed to create/cache cp */
  828. write_http_status_line(conn, 503, "Directory unavailable");
  829. /* try to get a new one now */
  830. if (!connection_get_by_type_purpose(CONN_TYPE_DIR,
  831. DIR_PURPOSE_FETCH_RUNNING_LIST))
  832. directory_get_from_dirserver(DIR_PURPOSE_FETCH_RUNNING_LIST, NULL, 1);
  833. return 0;
  834. }
  835. format_rfc1123_time(date, time(NULL));
  836. tor_snprintf(tmp, sizeof(tmp), "HTTP/1.0 200 OK\r\nDate: %s\r\nContent-Length: %d\r\nContent-Type: text/plain\r\nContent-Encoding: %s\r\n\r\n",
  837. date,
  838. (int)dlen,
  839. deflated?"deflate":"identity");
  840. connection_write_to_buf(tmp, strlen(tmp), conn);
  841. connection_write_to_buf(cp, strlen(cp), conn);
  842. return 0;
  843. }
  844. if (!strcmpstart(url,"/tor/rendezvous/")) {
  845. /* rendezvous descriptor fetch */
  846. const char *descp;
  847. size_t desc_len;
  848. if (!authdir_mode(get_options())) {
  849. /* We don't hand out rend descs. In fact, it could be a security
  850. * risk, since rend_cache_lookup_desc() below would provide it
  851. * if we're gone to the site recently, and 404 if we haven't.
  852. *
  853. * Reject. */
  854. write_http_status_line(conn, 400, "Nonauthoritative directory does not not store rendezvous descriptors.");
  855. tor_free(url);
  856. return 0;
  857. }
  858. switch (rend_cache_lookup_desc(url+strlen("/tor/rendezvous/"), &descp, &desc_len)) {
  859. case 1: /* valid */
  860. format_rfc1123_time(date, time(NULL));
  861. tor_snprintf(tmp, sizeof(tmp), "HTTP/1.0 200 OK\r\nDate: %s\r\nContent-Length: %d\r\nContent-Type: application/octet-stream\r\n\r\n",
  862. date,
  863. (int)desc_len); /* can't include descp here, because it's got nuls */
  864. connection_write_to_buf(tmp, strlen(tmp), conn);
  865. connection_write_to_buf(descp, desc_len, conn);
  866. break;
  867. case 0: /* well-formed but not present */
  868. write_http_status_line(conn, 404, "Not found");
  869. break;
  870. case -1: /* not well-formed */
  871. write_http_status_line(conn, 400, "Bad request");
  872. break;
  873. }
  874. tor_free(url);
  875. return 0;
  876. }
  877. /* we didn't recognize the url */
  878. write_http_status_line(conn, 404, "Not found");
  879. tor_free(url);
  880. return 0;
  881. }
  882. /** Helper function: called when a dirserver gets a complete HTTP POST
  883. * request. Look for an uploaded server descriptor or rendezvous
  884. * service descriptor. On finding one, process it and write a
  885. * response into conn-\>outbuf. If the request is unrecognized, send a
  886. * 400. Always return 0. */
  887. static int
  888. directory_handle_command_post(connection_t *conn, char *headers,
  889. char *body, size_t body_len)
  890. {
  891. const char *cp;
  892. char *url;
  893. log_fn(LOG_DEBUG,"Received POST command.");
  894. conn->state = DIR_CONN_STATE_SERVER_WRITING;
  895. if (!authdir_mode(get_options())) {
  896. /* we just provide cached directories; we don't want to
  897. * receive anything. */
  898. write_http_status_line(conn, 400, "Nonauthoritative directory does not not store server descriptors.");
  899. return 0;
  900. }
  901. if (parse_http_url(headers, &url) < 0) {
  902. write_http_status_line(conn, 400, "Bad request");
  903. return 0;
  904. }
  905. log_fn(LOG_INFO,"rewritten url as '%s'.", url);
  906. if (!strcmp(url,"/tor/")) { /* server descriptor post */
  907. const char *msg;
  908. cp = body;
  909. switch (dirserv_add_descriptor(&cp, &msg)) {
  910. case -2:
  911. case -1:
  912. /* malformed descriptor, or something wrong */
  913. write_http_status_line(conn, 400, msg?msg:"Malformed or unacceptable server descriptor");
  914. break;
  915. case 0:
  916. /* descriptor was well-formed but server has not been approved */
  917. write_http_status_line(conn, 200, msg?msg:"Unverified server descriptor accepted");
  918. break;
  919. case 1:
  920. dirserv_get_directory(&cp, 0); /* rebuild and write to disk */
  921. write_http_status_line(conn, 200, msg?msg:"Verified server descriptor accepted");
  922. break;
  923. }
  924. tor_free(url);
  925. return 0;
  926. }
  927. if (!strcmpstart(url,"/tor/rendezvous/publish")) {
  928. /* rendezvous descriptor post */
  929. if (rend_cache_store(body, body_len) < 0)
  930. write_http_status_line(conn, 400, "Invalid service descriptor rejected");
  931. else
  932. write_http_status_line(conn, 200, "Service descriptor stored");
  933. tor_free(url);
  934. return 0;
  935. }
  936. /* we didn't recognize the url */
  937. write_http_status_line(conn, 404, "Not found");
  938. tor_free(url);
  939. return 0;
  940. }
  941. /** Called when a dirserver receives data on a directory connection;
  942. * looks for an HTTP request. If the request is complete, remove it
  943. * from the inbuf, try to process it; otherwise, leave it on the
  944. * buffer. Return a 0 on success, or -1 on error.
  945. */
  946. static int directory_handle_command(connection_t *conn) {
  947. char *headers=NULL, *body=NULL;
  948. size_t body_len=0;
  949. int r;
  950. tor_assert(conn);
  951. tor_assert(conn->type == CONN_TYPE_DIR);
  952. switch (fetch_from_buf_http(conn->inbuf,
  953. &headers, MAX_HEADERS_SIZE,
  954. &body, &body_len, MAX_BODY_SIZE)) {
  955. case -1: /* overflow */
  956. log_fn(LOG_WARN,"Invalid input. Closing.");
  957. return -1;
  958. case 0:
  959. log_fn(LOG_DEBUG,"command not all here yet.");
  960. return 0;
  961. /* case 1, fall through */
  962. }
  963. log_fn(LOG_DEBUG,"headers '%s', body '%s'.", headers, body);
  964. if (!strncasecmp(headers,"GET",3))
  965. r = directory_handle_command_get(conn, headers, body, body_len);
  966. else if (!strncasecmp(headers,"POST",4))
  967. r = directory_handle_command_post(conn, headers, body, body_len);
  968. else {
  969. log_fn(LOG_WARN,"Got headers '%s' with unknown command. Closing.", headers);
  970. r = -1;
  971. }
  972. tor_free(headers); tor_free(body);
  973. return r;
  974. }
  975. /** Write handler for directory connections; called when all data has
  976. * been flushed. Close the connection or wait for a response as
  977. * appropriate.
  978. */
  979. int connection_dir_finished_flushing(connection_t *conn) {
  980. tor_assert(conn);
  981. tor_assert(conn->type == CONN_TYPE_DIR);
  982. switch (conn->state) {
  983. case DIR_CONN_STATE_CLIENT_SENDING:
  984. log_fn(LOG_DEBUG,"client finished sending command.");
  985. conn->state = DIR_CONN_STATE_CLIENT_READING;
  986. connection_stop_writing(conn);
  987. return 0;
  988. case DIR_CONN_STATE_SERVER_WRITING:
  989. log_fn(LOG_INFO,"Finished writing server response. Closing.");
  990. connection_mark_for_close(conn);
  991. return 0;
  992. default:
  993. log_fn(LOG_WARN,"Bug: called in unexpected state %d.", conn->state);
  994. #ifdef TOR_FRAGILE
  995. tor_assert(0);
  996. #endif
  997. return -1;
  998. }
  999. return 0;
  1000. }
  1001. /** Connected handler for directory connections: begin sending data to the
  1002. * server */
  1003. int connection_dir_finished_connecting(connection_t *conn)
  1004. {
  1005. tor_assert(conn);
  1006. tor_assert(conn->type == CONN_TYPE_DIR);
  1007. tor_assert(conn->state == DIR_CONN_STATE_CONNECTING);
  1008. log_fn(LOG_INFO,"Dir connection to router %s:%u established.",
  1009. conn->address,conn->port);
  1010. conn->state = DIR_CONN_STATE_CLIENT_SENDING; /* start flushing conn */
  1011. return 0;
  1012. }