directory.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /* Copyright 2001,2002,2003 Roger Dingledine. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. /*****
  6. * directory.c: Implement directory HTTP protocol.
  7. *****/
  8. static void directory_send_command(connection_t *conn, int purpose,
  9. const char *payload, int payload_len);
  10. static int directory_handle_command(connection_t *conn);
  11. /********* START VARIABLES **********/
  12. extern or_options_t options; /* command-line and config-file options */
  13. /* URL for publishing rendezvous descriptors. */
  14. char rend_publish_string[] = "/rendezvous/publish";
  15. /* Prefix for downloading rendezvous descriptors. */
  16. char rend_fetch_url[] = "/rendezvous/";
  17. #define MAX_HEADERS_SIZE 10000
  18. #define MAX_BODY_SIZE 500000
  19. /********* END VARIABLES ************/
  20. /* Launch a new connection to the directory server 'router' to upload
  21. * or download a service or rendezvous descriptor. 'purpose' determines what
  22. * kind of directory connection we're launching, and must be one of
  23. * DIR_PURPOSE_{FETCH|UPLOAD}_{DIR|RENDDESC}.
  24. *
  25. * When uploading, 'payload' and 'payload_len' determine the content
  26. * of the HTTP post. When fetching a rendezvous descriptor, 'payload'
  27. * and 'payload_len' are the service ID we want to fetch.
  28. */
  29. void directory_initiate_command(routerinfo_t *router, int purpose,
  30. const char *payload, int payload_len) {
  31. connection_t *conn;
  32. switch (purpose)
  33. {
  34. case DIR_PURPOSE_FETCH_DIR:
  35. log_fn(LOG_DEBUG,"initiating directory fetch");
  36. break;
  37. case DIR_PURPOSE_FETCH_RENDDESC:
  38. log_fn(LOG_DEBUG,"initiating hidden-service descriptor fetch");
  39. break;
  40. case DIR_PURPOSE_UPLOAD_DIR:
  41. log_fn(LOG_DEBUG,"initiating server descriptor upload");
  42. break;
  43. case DIR_PURPOSE_UPLOAD_RENDDESC:
  44. log_fn(LOG_DEBUG,"initiating hidden-service descriptor upload");
  45. break;
  46. default:
  47. log_fn(LOG_ERR, "Unrecognized directory connection purpose.");
  48. tor_assert(0);
  49. }
  50. if (!router) { /* i guess they didn't have one in mind for me to use */
  51. log_fn(LOG_WARN,"No running dirservers known. Not trying. (purpose %d)", purpose);
  52. return;
  53. }
  54. conn = connection_new(CONN_TYPE_DIR);
  55. /* set up conn so it's got all the data we need to remember */
  56. conn->addr = router->addr;
  57. conn->port = router->dir_port;
  58. conn->address = tor_strdup(router->address);
  59. conn->nickname = tor_strdup(router->nickname);
  60. tor_assert(router->identity_pkey);
  61. conn->identity_pkey = crypto_pk_dup_key(router->identity_pkey);
  62. conn->purpose = purpose;
  63. /* give it an initial state */
  64. conn->state = DIR_CONN_STATE_CONNECTING;
  65. if(purpose == DIR_PURPOSE_FETCH_DIR ||
  66. purpose == DIR_PURPOSE_UPLOAD_DIR) {
  67. /* then we want to connect directly */
  68. switch(connection_connect(conn, conn->address, conn->addr, conn->port)) {
  69. case -1:
  70. router_mark_as_down(conn->nickname); /* don't try him again */
  71. connection_free(conn);
  72. return;
  73. case 1:
  74. conn->state = DIR_CONN_STATE_CLIENT_SENDING; /* start flushing conn */
  75. /* fall through */
  76. case 0:
  77. /* queue the command on the outbuf */
  78. directory_send_command(conn, purpose, payload, payload_len);
  79. connection_watch_events(conn, POLLIN | POLLOUT | POLLERR);
  80. /* writable indicates finish, readable indicates broken link,
  81. error indicates broken link in windowsland. */
  82. }
  83. } else { /* we want to connect via tor */
  84. /* make an AP connection
  85. * populate it and add it at the right state
  86. * socketpair and hook up both sides
  87. */
  88. conn->s = connection_ap_make_bridge(conn->address, conn->port);
  89. if(conn->s < 0) {
  90. log_fn(LOG_WARN,"Making AP bridge to dirserver failed.");
  91. connection_mark_for_close(conn, 0);
  92. return;
  93. }
  94. conn->state = DIR_CONN_STATE_CLIENT_SENDING;
  95. connection_add(conn);
  96. /* queue the command on the outbuf */
  97. directory_send_command(conn, purpose, payload, payload_len);
  98. connection_watch_events(conn, POLLIN | POLLOUT | POLLERR);
  99. }
  100. }
  101. /* Queue an appropriate HTTP command on conn->outbuf. The args
  102. * 'purpose', 'payload', and 'payload_len' are as in
  103. * directory_initiate_command.
  104. */
  105. static void directory_send_command(connection_t *conn, int purpose,
  106. const char *payload, int payload_len) {
  107. char fetchstring[] = "GET / HTTP/1.0\r\n\r\n";
  108. char tmp[8192];
  109. tor_assert(conn && conn->type == CONN_TYPE_DIR);
  110. switch(purpose) {
  111. case DIR_PURPOSE_FETCH_DIR:
  112. tor_assert(payload == NULL);
  113. connection_write_to_buf(fetchstring, strlen(fetchstring), conn);
  114. break;
  115. case DIR_PURPOSE_UPLOAD_DIR:
  116. tor_assert(payload);
  117. snprintf(tmp, sizeof(tmp), "POST / HTTP/1.0\r\nContent-Length: %d\r\n\r\n",
  118. payload_len);
  119. connection_write_to_buf(tmp, strlen(tmp), conn);
  120. connection_write_to_buf(payload, payload_len, conn);
  121. break;
  122. case DIR_PURPOSE_FETCH_RENDDESC:
  123. tor_assert(payload);
  124. /* this must be true or we wouldn't be doing the lookup */
  125. tor_assert(payload_len <= REND_SERVICE_ID_LEN);
  126. /* This breaks the function abstraction. */
  127. memcpy(conn->rend_query, payload, payload_len);
  128. conn->rend_query[payload_len] = 0;
  129. snprintf(tmp, sizeof(tmp), "GET %s%s HTTP/1.0\r\n\r\n", rend_fetch_url, payload);
  130. connection_write_to_buf(tmp, strlen(tmp), conn);
  131. break;
  132. case DIR_PURPOSE_UPLOAD_RENDDESC:
  133. tor_assert(payload);
  134. snprintf(tmp, sizeof(tmp),
  135. "POST %s HTTP/1.0\r\nContent-Length: %d\r\n\r\n", rend_publish_string, payload_len);
  136. connection_write_to_buf(tmp, strlen(tmp), conn);
  137. /* could include nuls, need to write it separately */
  138. connection_write_to_buf(payload, payload_len, conn);
  139. break;
  140. }
  141. }
  142. /* Parse an HTTP request string 'headers' of the form "%s %s HTTP/1..."
  143. * If it's well-formed, point *url to the second %s,
  144. * null-terminate it (this modifies headers!) and return 0.
  145. * Otherwise, return -1.
  146. */
  147. int parse_http_url(char *headers, char **url) {
  148. char *s, *tmp;
  149. s = (char *)eat_whitespace_no_nl(headers);
  150. if (!*s) return -1;
  151. s = (char *)find_whitespace(s); /* get past GET/POST */
  152. if (!*s) return -1;
  153. s = (char *)eat_whitespace_no_nl(s);
  154. if (!*s) return -1;
  155. tmp = s; /* this is it, assuming it's valid */
  156. s = (char *)find_whitespace(s);
  157. if (!*s) return -1;
  158. *s = 0;
  159. *url = tmp;
  160. return 0;
  161. }
  162. /* Parse an HTTP response string 'headers' of the form "HTTP/1.%d %d%s\r\n...".
  163. * If it's well-formed, assign *code, point *message to the first
  164. * non-space character after code if there is one and message is non-NULL
  165. * (else leave it alone), and return 0.
  166. * Otherwise, return -1.
  167. */
  168. int parse_http_response(char *headers, int *code, char **message) {
  169. int n1, n2;
  170. tor_assert(headers && code);
  171. while(isspace((int)*headers)) headers++; /* tolerate leading whitespace */
  172. if(sscanf(headers, "HTTP/1.%d %d", &n1, &n2) < 2 ||
  173. (n1 != 0 && n1 != 1) ||
  174. (n2 < 100 || n2 >= 600)) {
  175. log_fn(LOG_WARN,"Failed to parse header '%s'",headers);
  176. return -1;
  177. }
  178. *code = n2;
  179. if(message) {
  180. /* XXX should set *message correctly */
  181. }
  182. return 0;
  183. }
  184. /* Read handler for directory connections. (That's connections *to*
  185. * directory servers and connections *at* directory servers.)
  186. */
  187. int connection_dir_process_inbuf(connection_t *conn) {
  188. char *body;
  189. char *headers;
  190. int body_len=0;
  191. int status_code;
  192. tor_assert(conn && conn->type == CONN_TYPE_DIR);
  193. /* Directory clients write, then read data until they receive EOF;
  194. * directory servers read data until they get an HTTP command, then
  195. * write their response (when it's finished flushing, they mark for
  196. * close).
  197. */
  198. if(conn->inbuf_reached_eof) {
  199. if(conn->state != DIR_CONN_STATE_CLIENT_READING) {
  200. log_fn(LOG_INFO,"conn reached eof, not reading. Closing.");
  201. connection_close_immediate(conn); /* it was an error; give up on flushing */
  202. connection_mark_for_close(conn,0);
  203. return -1;
  204. }
  205. switch(fetch_from_buf_http(conn->inbuf,
  206. &headers, MAX_HEADERS_SIZE,
  207. &body, &body_len, MAX_DIR_SIZE)) {
  208. case -1: /* overflow */
  209. log_fn(LOG_WARN,"'fetch' response too large. Failing.");
  210. connection_mark_for_close(conn,0);
  211. return -1;
  212. case 0:
  213. log_fn(LOG_INFO,"'fetch' response not all here, but we're at eof. Closing.");
  214. connection_mark_for_close(conn,0);
  215. return -1;
  216. /* case 1, fall through */
  217. }
  218. if(parse_http_response(headers, &status_code, NULL) < 0) {
  219. log_fn(LOG_WARN,"Unparseable headers. Closing.");
  220. free(body); free(headers);
  221. connection_mark_for_close(conn,0);
  222. return -1;
  223. }
  224. if(conn->purpose == DIR_PURPOSE_FETCH_DIR) {
  225. /* fetch/process the directory to learn about new routers. */
  226. log_fn(LOG_INFO,"Received directory (size %d):\n%s", body_len, body);
  227. if(status_code == 503 || body_len == 0) {
  228. log_fn(LOG_INFO,"Empty directory. Ignoring.");
  229. free(body); free(headers);
  230. connection_mark_for_close(conn,0);
  231. return 0;
  232. }
  233. if(status_code != 200) {
  234. log_fn(LOG_WARN,"Received http status code %d from dirserver. Failing.",
  235. status_code);
  236. free(body); free(headers);
  237. connection_mark_for_close(conn,0);
  238. return -1;
  239. }
  240. if(router_set_routerlist_from_directory(body, conn->identity_pkey) < 0){
  241. log_fn(LOG_INFO,"...but parsing failed. Ignoring.");
  242. } else {
  243. log_fn(LOG_INFO,"updated routers.");
  244. }
  245. directory_has_arrived(); /* do things we've been waiting to do */
  246. }
  247. if(conn->purpose == DIR_PURPOSE_UPLOAD_DIR) {
  248. switch(status_code) {
  249. case 200:
  250. log_fn(LOG_INFO,"eof (status 200) after uploading server descriptor: finished.");
  251. break;
  252. case 400:
  253. log_fn(LOG_WARN,"http status 400 (bad request) response from dirserver. Malformed server descriptor?");
  254. break;
  255. case 403:
  256. log_fn(LOG_WARN,"http status 403 (unapproved server) response from dirserver. Is your clock skewed? Have you mailed us your identity fingerprint? Are you using the right key? See README.");
  257. break;
  258. default:
  259. log_fn(LOG_WARN,"http status %d response unrecognized.", status_code);
  260. break;
  261. }
  262. }
  263. if(conn->purpose == DIR_PURPOSE_FETCH_RENDDESC) {
  264. log_fn(LOG_INFO,"Received rendezvous descriptor (size %d, status code %d)",
  265. body_len, status_code);
  266. switch(status_code) {
  267. case 200:
  268. if(rend_cache_store(body, body_len) < 0) {
  269. log_fn(LOG_WARN,"Failed to store rendezvous descriptor.");
  270. /* alice's ap_stream will notice when connection_mark_for_close
  271. * cleans it up */
  272. } else {
  273. /* success. notify pending connections about this. */
  274. rend_client_desc_fetched(conn->rend_query, 1);
  275. conn->purpose = DIR_PURPOSE_HAS_FETCHED_RENDDESC;
  276. }
  277. break;
  278. case 404:
  279. /* not there. pending connections will be notified when
  280. * connection_mark_for_close cleans it up. */
  281. break;
  282. case 400:
  283. log_fn(LOG_WARN,"http status 400 (bad request). Dirserver didn't like our rendezvous query?");
  284. break;
  285. }
  286. }
  287. if(conn->purpose == DIR_PURPOSE_UPLOAD_RENDDESC) {
  288. switch(status_code) {
  289. case 200:
  290. log_fn(LOG_INFO,"eof (status 200) after uploading rendezvous descriptor: finished.");
  291. break;
  292. case 400:
  293. log_fn(LOG_WARN,"http status 400 (bad request) response from dirserver. Malformed rendezvous descriptor?");
  294. break;
  295. default:
  296. log_fn(LOG_WARN,"http status %d response unrecognized.", status_code);
  297. break;
  298. }
  299. }
  300. free(body); free(headers);
  301. connection_mark_for_close(conn,0);
  302. return 0;
  303. } /* endif 'reached eof' */
  304. /* If we're on the dirserver side, look for a command. */
  305. if(conn->state == DIR_CONN_STATE_SERVER_COMMAND_WAIT) {
  306. if (directory_handle_command(conn) < 0) {
  307. connection_mark_for_close(conn,0);
  308. return -1;
  309. }
  310. return 0;
  311. }
  312. /* XXX for READ states, might want to make sure inbuf isn't too big */
  313. log_fn(LOG_DEBUG,"Got data, not eof. Leaving on inbuf.");
  314. return 0;
  315. }
  316. static char answer200[] = "HTTP/1.0 200 OK\r\n\r\n";
  317. static char answer400[] = "HTTP/1.0 400 Bad request\r\n\r\n";
  318. static char answer403[] = "HTTP/1.0 403 Unapproved server\r\n\r\n";
  319. static char answer404[] = "HTTP/1.0 404 Not found\r\n\r\n";
  320. static char answer503[] = "HTTP/1.0 503 Directory unavailable\r\n\r\n";
  321. /* Helper function: called when a dirserver gets a complete HTTP GET
  322. * request. Look for a request for a directory or for a rendezvous
  323. * service descriptor. On finding one, write a response into
  324. * conn->outbuf. If the request is unrecognized, send a 404.
  325. * Always return 0. */
  326. static int directory_handle_command_get(connection_t *conn,
  327. char *headers, char *body,
  328. int body_len) {
  329. size_t dlen;
  330. const char *cp;
  331. char *url;
  332. char tmp[8192];
  333. log_fn(LOG_DEBUG,"Received GET command.");
  334. conn->state = DIR_CONN_STATE_SERVER_WRITING;
  335. if (parse_http_url(headers, &url) < 0) {
  336. connection_write_to_buf(answer400, strlen(answer400), conn);
  337. return 0;
  338. }
  339. if(!strcmp(url,"/")) { /* directory fetch */
  340. dlen = dirserv_get_directory(&cp);
  341. if(dlen == 0) {
  342. log_fn(LOG_WARN,"My directory is empty. Closing.");
  343. connection_write_to_buf(answer503, strlen(answer503), conn);
  344. return 0;
  345. }
  346. log_fn(LOG_DEBUG,"Dumping directory to client.");
  347. snprintf(tmp, sizeof(tmp), "HTTP/1.0 200 OK\r\nContent-Length: %d\r\nContent-Type: text/plain\r\n\r\n",
  348. (int)dlen);
  349. connection_write_to_buf(tmp, strlen(tmp), conn);
  350. connection_write_to_buf(cp, strlen(cp), conn);
  351. return 0;
  352. }
  353. if(!strncmp(url,rend_fetch_url,strlen(rend_fetch_url))) {
  354. /* rendezvous descriptor fetch */
  355. const char *descp;
  356. int desc_len;
  357. switch(rend_cache_lookup_desc(url+strlen(rend_fetch_url), &descp, &desc_len)) {
  358. case 1: /* valid */
  359. snprintf(tmp, sizeof(tmp), "HTTP/1.0 200 OK\r\nContent-Length: %d\r\nContent-Type: application/octet-stream\r\n\r\n",
  360. desc_len); /* can't include descp here, because it's got nuls */
  361. connection_write_to_buf(tmp, strlen(tmp), conn);
  362. connection_write_to_buf(descp, desc_len, conn);
  363. break;
  364. case 0: /* well-formed but not present */
  365. connection_write_to_buf(answer404, strlen(answer404), conn);
  366. break;
  367. case -1: /* not well-formed */
  368. connection_write_to_buf(answer400, strlen(answer400), conn);
  369. break;
  370. }
  371. return 0;
  372. }
  373. /* we didn't recognize the url */
  374. connection_write_to_buf(answer404, strlen(answer404), conn);
  375. return 0;
  376. }
  377. /* Helper function: called when a dirserver gets a complete HTTP POST
  378. * request. Look for an uploaded server descriptor or rendezvous
  379. * service descriptor. On finding one, process it and write a
  380. * response into conn->outbuf. If the request is unrecognized, send a
  381. * 404. Always return 0. */
  382. static int directory_handle_command_post(connection_t *conn,
  383. char *headers, char *body,
  384. int body_len) {
  385. const char *cp;
  386. char *url;
  387. log_fn(LOG_DEBUG,"Received POST command.");
  388. conn->state = DIR_CONN_STATE_SERVER_WRITING;
  389. if (parse_http_url(headers, &url) < 0) {
  390. connection_write_to_buf(answer400, strlen(answer400), conn);
  391. return 0;
  392. }
  393. log_fn(LOG_INFO,"url '%s' posted to us.", url);
  394. if(!strcmp(url,"/")) { /* server descriptor post */
  395. cp = body;
  396. switch(dirserv_add_descriptor(&cp)) {
  397. case -1:
  398. /* malformed descriptor, or something wrong */
  399. connection_write_to_buf(answer400, strlen(answer400), conn);
  400. break;
  401. case 0:
  402. /* descriptor was well-formed but server has not been approved */
  403. connection_write_to_buf(answer403, strlen(answer403), conn);
  404. break;
  405. case 1:
  406. dirserv_get_directory(&cp); /* rebuild and write to disk */
  407. connection_write_to_buf(answer200, strlen(answer200), conn);
  408. break;
  409. }
  410. return 0;
  411. }
  412. if(!strncmp(url,rend_publish_string,strlen(rend_publish_string))) {
  413. /* rendezvous descriptor post */
  414. if(rend_cache_store(body, body_len) < 0)
  415. connection_write_to_buf(answer400, strlen(answer400), conn);
  416. else
  417. connection_write_to_buf(answer200, strlen(answer200), conn);
  418. return 0;
  419. }
  420. /* we didn't recognize the url */
  421. connection_write_to_buf(answer404, strlen(answer404), conn);
  422. return 0;
  423. }
  424. /* Called when a dirserver receives data on a directory connection;
  425. * looks for an HTTP request. If the request is complete, remove it
  426. * from the inbuf, try to process it; otherwise, leave it on the
  427. * buffer. Return a 0 on success, or -1 on error.
  428. */
  429. static int directory_handle_command(connection_t *conn) {
  430. char *headers=NULL, *body=NULL;
  431. int body_len=0;
  432. int r;
  433. tor_assert(conn && conn->type == CONN_TYPE_DIR);
  434. switch(fetch_from_buf_http(conn->inbuf,
  435. &headers, MAX_HEADERS_SIZE,
  436. &body, &body_len, MAX_BODY_SIZE)) {
  437. case -1: /* overflow */
  438. log_fn(LOG_WARN,"input too large. Failing.");
  439. return -1;
  440. case 0:
  441. log_fn(LOG_DEBUG,"command not all here yet.");
  442. return 0;
  443. /* case 1, fall through */
  444. }
  445. log_fn(LOG_DEBUG,"headers '%s', body '%s'.", headers, body);
  446. if(!strncasecmp(headers,"GET",3))
  447. r = directory_handle_command_get(conn, headers, body, body_len);
  448. else if (!strncasecmp(headers,"POST",4))
  449. r = directory_handle_command_post(conn, headers, body, body_len);
  450. else {
  451. log_fn(LOG_WARN,"Got headers '%s' with unknown command. Closing.", headers);
  452. r = -1;
  453. }
  454. tor_free(headers); tor_free(body);
  455. return r;
  456. }
  457. /* Write handler for directory connections; called when all data has
  458. * been flushed. Handle a completed connection: close the connection
  459. * or wait for a response as appropriate.
  460. */
  461. int connection_dir_finished_flushing(connection_t *conn) {
  462. int e, len=sizeof(e);
  463. tor_assert(conn && conn->type == CONN_TYPE_DIR);
  464. switch(conn->state) {
  465. case DIR_CONN_STATE_CONNECTING:
  466. if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) { /* not yet */
  467. if(!ERRNO_IS_CONN_EINPROGRESS(tor_socket_errno(conn->s))) {
  468. log_fn(LOG_DEBUG,"in-progress connect failed. Removing.");
  469. router_mark_as_down(conn->nickname); /* don't try him again */
  470. connection_mark_for_close(conn,0);
  471. return -1;
  472. } else {
  473. return 0; /* no change, see if next time is better */
  474. }
  475. }
  476. /* the connect has finished. */
  477. log_fn(LOG_INFO,"Dir connection to router %s:%u established.",
  478. conn->address,conn->port);
  479. conn->state = DIR_CONN_STATE_CLIENT_SENDING; /* start flushing conn */
  480. return 0;
  481. case DIR_CONN_STATE_CLIENT_SENDING:
  482. log_fn(LOG_DEBUG,"client finished sending command.");
  483. conn->state = DIR_CONN_STATE_CLIENT_READING;
  484. connection_stop_writing(conn);
  485. return 0;
  486. case DIR_CONN_STATE_SERVER_WRITING:
  487. log_fn(LOG_INFO,"Finished writing server response. Closing.");
  488. connection_mark_for_close(conn,0);
  489. return 0;
  490. default:
  491. log_fn(LOG_WARN,"BUG: called in unexpected state %d.", conn->state);
  492. return -1;
  493. }
  494. return 0;
  495. }
  496. /*
  497. Local Variables:
  498. mode:c
  499. indent-tabs-mode:nil
  500. c-basic-offset:2
  501. End:
  502. */