rendservice.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /* Copyright 2004 Roger Dingledine */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /* This module implements the hidden-service side of rendezvous functionality.
  5. */
  6. #include "or.h"
  7. /* Represents the mapping from a virtual port of a rendezvous service to
  8. * a real port on some IP.
  9. */
  10. typedef struct rend_service_port_config_t {
  11. uint16_t virtual_port;
  12. uint16_t real_port;
  13. uint32_t real_address;
  14. } rend_service_port_config_t;
  15. /* Try to maintain this many intro points per service if possible.
  16. */
  17. #define NUM_INTRO_POINTS 3
  18. /* Represents a single hidden service running at this OP.
  19. */
  20. typedef struct rend_service_t {
  21. /* Fields specified in config file */
  22. char *directory; /* where in the filesystem it stores it */
  23. smartlist_t *ports;
  24. char *intro_prefer_nodes;
  25. char *intro_exclude_nodes;
  26. /* Other fields */
  27. crypto_pk_env_t *private_key;
  28. char service_id[REND_SERVICE_ID_LEN+1];
  29. char pk_digest[DIGEST_LEN];
  30. smartlist_t *intro_nodes; /* list of nicknames */
  31. rend_service_descriptor_t *desc;
  32. } rend_service_t;
  33. /* A list of rend_service_t's for services run on this OP.
  34. */
  35. static smartlist_t *rend_service_list = NULL;
  36. /* Release the storage held by 'service'.
  37. */
  38. static void rend_service_free(rend_service_t *service)
  39. {
  40. if (!service) return;
  41. tor_free(service->directory);
  42. SMARTLIST_FOREACH(service->ports, void*, p, tor_free(p));
  43. smartlist_free(service->ports);
  44. if (service->private_key)
  45. crypto_free_pk_env(service->private_key);
  46. tor_free(service->intro_prefer_nodes);
  47. tor_free(service->intro_exclude_nodes);
  48. SMARTLIST_FOREACH(service->intro_nodes, void*, p, tor_free(p));
  49. smartlist_free(service->intro_nodes);
  50. if (service->desc)
  51. rend_service_descriptor_free(service->desc);
  52. tor_free(service);
  53. }
  54. /* Release all the storage held in rend_service_list, and allocate a new,
  55. * empty rend_service_list.
  56. */
  57. static void rend_service_free_all(void)
  58. {
  59. if (!rend_service_list) {
  60. rend_service_list = smartlist_create();
  61. return;
  62. }
  63. SMARTLIST_FOREACH(rend_service_list, rend_service_t*, ptr,
  64. rend_service_free(ptr));
  65. smartlist_free(rend_service_list);
  66. rend_service_list = smartlist_create();
  67. }
  68. /* Validate 'service' and add it to rend_service_list if possible.
  69. */
  70. static void add_service(rend_service_t *service)
  71. {
  72. int i;
  73. rend_service_port_config_t *p;
  74. struct in_addr addr;
  75. if (!service->intro_prefer_nodes)
  76. service->intro_prefer_nodes = tor_strdup("");
  77. if (!service->intro_exclude_nodes)
  78. service->intro_exclude_nodes = tor_strdup("");
  79. if (!smartlist_len(service->ports)) {
  80. log_fn(LOG_WARN, "Hidden service with no ports configured; ignoring.");
  81. rend_service_free(service);
  82. } else {
  83. smartlist_set_capacity(service->ports, -1);
  84. smartlist_add(rend_service_list, service);
  85. log_fn(LOG_DEBUG,"Configuring service with directory %s",service->directory);
  86. for (i = 0; i < smartlist_len(service->ports); ++i) {
  87. p = smartlist_get(service->ports, i);
  88. addr.s_addr = htonl(p->real_address);
  89. log_fn(LOG_DEBUG,"Service maps port %d to %s:%d",
  90. p->virtual_port, inet_ntoa(addr), p->real_port);
  91. }
  92. }
  93. }
  94. /* Parses a real-port to virtual-port mapping and returns a new
  95. * rend_service_port_config_t.
  96. *
  97. * The format is: VirtualPort (IP|RealPort|IP:RealPort)?
  98. * IP defaults to 127.0.0.1; RealPort defaults to VirtualPort.
  99. */
  100. static rend_service_port_config_t *parse_port_config(const char *string)
  101. {
  102. int virtport, realport, r;
  103. struct in_addr addr;
  104. char *endptr, *colon, *addrstring;
  105. rend_service_port_config_t *result;
  106. virtport = (int) strtol(string, &endptr, 10);
  107. if (endptr == string) {
  108. log_fn(LOG_WARN, "Missing port in hidden service port configuration");
  109. return NULL;
  110. }
  111. if (virtport < 1 || virtport > 65535) {
  112. log_fn(LOG_WARN, "Port out of range in hidden service port configuration");
  113. return NULL;
  114. }
  115. string = endptr + strspn(endptr, " \t");
  116. if (!*string) {
  117. /* No addr:port part; use default. */
  118. realport = virtport;
  119. addr.s_addr = htonl(0x7F000001u); /* 127.0.0.1 */
  120. } else {
  121. colon = strchr(string, ':');
  122. if (colon) {
  123. /* Try to parse addr:port. */
  124. addrstring = tor_strndup(string, colon-string);
  125. r = tor_inet_aton(addrstring, &addr);
  126. tor_free(addrstring);
  127. if (!r) {
  128. log_fn(LOG_WARN,"Unparseable address in hidden service port configuration");
  129. return NULL;
  130. }
  131. realport = strtol(colon+1, &endptr, 10);
  132. if (*endptr) {
  133. log_fn(LOG_WARN,"Unparseable or missing port in hidden service port configuration.");
  134. return NULL;
  135. }
  136. } else if (strchr(string, '.') && tor_inet_aton(string, &addr)) {
  137. /* We have addr; use deafult port. */
  138. realport = virtport;
  139. } else {
  140. /* No addr:port, no addr -- must be port. */
  141. realport = strtol(string, &endptr, 10);
  142. if (*endptr) {
  143. log_fn(LOG_WARN, "Unparseable of missing port in hidden service port configuration.");
  144. return NULL;
  145. }
  146. addr.s_addr = htonl(0x7F000001u); /* Default to 127.0.0.1 */
  147. }
  148. }
  149. if (realport < 1 || realport > 65535) {
  150. log_fn(LOG_WARN, "Port out of range in hidden service port configuration.");
  151. return NULL;
  152. }
  153. result = tor_malloc(sizeof(rend_service_port_config_t));
  154. result->virtual_port = virtport;
  155. result->real_port = realport;
  156. result->real_address = (uint32_t) ntohl(addr.s_addr);
  157. return result;
  158. }
  159. /* Set up rend_service_list, based on the values of HiddenServiceDir and
  160. * HiddenServicePort in 'options'. Return 0 on success and -1 on
  161. * failure.
  162. */
  163. int rend_config_services(or_options_t *options)
  164. {
  165. struct config_line_t *line;
  166. rend_service_t *service = NULL;
  167. rend_service_port_config_t *portcfg;
  168. rend_service_free_all();
  169. for (line = options->RendConfigLines; line; line = line->next) {
  170. if (!strcasecmp(line->key, "HiddenServiceDir")) {
  171. if (service)
  172. add_service(service);
  173. service = tor_malloc_zero(sizeof(rend_service_t));
  174. service->directory = tor_strdup(line->value);
  175. service->ports = smartlist_create();
  176. service->intro_nodes = smartlist_create();
  177. continue;
  178. }
  179. if (!service) {
  180. log_fn(LOG_WARN, "HiddenServicePort with no preceeding HiddenServiceDir directive");
  181. rend_service_free(service);
  182. return -1;
  183. }
  184. if (!strcasecmp(line->key, "HiddenServicePort")) {
  185. portcfg = parse_port_config(line->value);
  186. if (!portcfg) {
  187. rend_service_free(service);
  188. return -1;
  189. }
  190. smartlist_add(service->ports, portcfg);
  191. } else if (!strcasecmp(line->key, "HiddenServiceNodes")) {
  192. if (service->intro_prefer_nodes) {
  193. log_fn(LOG_WARN, "Got multiple HiddenServiceNodes lines for a single service");
  194. return -1;
  195. }
  196. service->intro_prefer_nodes = tor_strdup(line->value);
  197. } else {
  198. assert(!strcasecmp(line->key, "HiddenServiceExcludeNodes"));
  199. if (service->intro_exclude_nodes) {
  200. log_fn(LOG_WARN, "Got multiple HiddenServiceExcludedNodes lines for a single service");
  201. return -1;
  202. }
  203. service->intro_exclude_nodes = tor_strdup(line->value);
  204. }
  205. }
  206. if (service)
  207. add_service(service);
  208. return 0;
  209. }
  210. /* Replace the old value of service->desc with one that reflects
  211. * the other fields in service.
  212. */
  213. static void rend_service_update_descriptor(rend_service_t *service)
  214. {
  215. rend_service_descriptor_t *d;
  216. int i,n;
  217. if (service->desc) {
  218. rend_service_descriptor_free(service->desc);
  219. service->desc = NULL;
  220. }
  221. d = service->desc = tor_malloc(sizeof(rend_service_descriptor_t));
  222. d->pk = crypto_pk_dup_key(service->private_key);
  223. d->timestamp = time(NULL);
  224. n = d->n_intro_points = smartlist_len(service->intro_nodes);
  225. d->intro_points = tor_malloc(sizeof(char*)*n);
  226. for (i=0; i < n; ++i) {
  227. d->intro_points[i] = tor_strdup(smartlist_get(service->intro_nodes, i));
  228. }
  229. }
  230. /* Load and/or generate private keys for all hidden services. Return 0 on
  231. * success, -1 on failure.
  232. */
  233. int rend_service_init_keys(void)
  234. {
  235. int i;
  236. rend_service_t *s;
  237. char fname[512];
  238. char buf[128];
  239. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  240. s = smartlist_get(rend_service_list,i);
  241. if (s->private_key)
  242. continue;
  243. log_fn(LOG_INFO, "Loading hidden-service keys from '%s'", s->directory);
  244. /* Check/create directory */
  245. if (check_private_dir(s->directory, 1) < 0)
  246. return -1;
  247. /* Load key */
  248. if (strlcpy(fname,s->directory,sizeof(fname)) >= sizeof(fname) ||
  249. strlcat(fname,"/private_key",sizeof(fname)) >= sizeof(fname)) {
  250. log_fn(LOG_WARN, "Directory name too long: '%s'", s->directory);
  251. return -1;
  252. }
  253. s->private_key = init_key_from_file(fname);
  254. if (!s->private_key)
  255. return -1;
  256. /* Create service file */
  257. if (rend_get_service_id(s->private_key, s->service_id)<0) {
  258. log_fn(LOG_WARN, "Couldn't encode service ID");
  259. return -1;
  260. }
  261. if (crypto_pk_get_digest(s->private_key, s->pk_digest)<0) {
  262. log_fn(LOG_WARN, "Couldn't compute hash of public key");
  263. return -1;
  264. }
  265. if (strlcpy(fname,s->directory,sizeof(fname)) >= sizeof(fname) ||
  266. strlcat(fname,"/hostname",sizeof(fname)) >= sizeof(fname)) {
  267. log_fn(LOG_WARN, "Directory name too long: '%s'", s->directory);
  268. return -1;
  269. }
  270. sprintf(buf, "%s.onion\n", s->service_id);
  271. if (write_str_to_file(fname,buf)<0)
  272. return -1;
  273. }
  274. return 0;
  275. }
  276. /* Return the service whose public key has a digest of 'digest'. Return
  277. * NULL if no such service exists.
  278. */
  279. static rend_service_t *
  280. rend_service_get_by_pk_digest(const char* digest)
  281. {
  282. SMARTLIST_FOREACH(rend_service_list, rend_service_t*, s,
  283. if (!memcmp(s->pk_digest,digest,DIGEST_LEN)) return s);
  284. return NULL;
  285. }
  286. /******
  287. * Handle cells
  288. ******/
  289. /* Respond to an INTRODUCE2 cell by launching a circuit to the chosen
  290. * rendezvous points.
  291. */
  292. int
  293. rend_service_introduce(circuit_t *circuit, const char *request, int request_len)
  294. {
  295. char *ptr, *rp_nickname, *r_cookie;
  296. char buf[RELAY_PAYLOAD_SIZE];
  297. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN]; /* Holds KH, Df, Db, Kf, Kb */
  298. rend_service_t *service;
  299. int len, keylen;
  300. crypto_dh_env_t *dh = NULL;
  301. circuit_t *launched = NULL;
  302. crypt_path_t *cpath = NULL;
  303. char hexid[9];
  304. char hexcookie[9];
  305. hex_encode(circuit->rend_pk_digest, 4, hexid);
  306. log_fn(LOG_INFO, "Received INTRODUCE2 cell for service %s on circ %d",
  307. hexid, circuit->n_circ_id);
  308. if (circuit->purpose != CIRCUIT_PURPOSE_S_INTRO) {
  309. log_fn(LOG_WARN, "Got an INTRODUCE2 over a non-introduction circuit %d",
  310. circuit->n_circ_id);
  311. return -1;
  312. }
  313. /* min key length plus digest length plus nickname length */
  314. if (request_len < DIGEST_LEN+REND_COOKIE_LEN+(MAX_NICKNAME_LEN+1)+
  315. DH_KEY_LEN+42){
  316. log_fn(LOG_WARN, "Got a truncated INTRODUCE2 cell on circ %d",
  317. circuit->n_circ_id);
  318. return -1;
  319. }
  320. /* first DIGEST_LEN bytes of request is service pk digest */
  321. service = rend_service_get_by_pk_digest(request);
  322. if (!service) {
  323. log_fn(LOG_WARN, "Got an INTRODUCE2 cell for an unrecognized service %s",
  324. hexid);
  325. return -1;
  326. }
  327. if (memcmp(circuit->rend_pk_digest, request, DIGEST_LEN)) {
  328. hex_encode(request, 4, hexid);
  329. log_fn(LOG_WARN, "Got an INTRODUCE2 cell for the wrong service (%s)",
  330. hexid);
  331. return -1;
  332. }
  333. keylen = crypto_pk_keysize(service->private_key);
  334. if (request_len < keylen+DIGEST_LEN) {
  335. log_fn(LOG_WARN, "PK-encrypted portion of INTRODUCE2 cell was truncated");
  336. return -1;
  337. }
  338. /* Next N bytes is encrypted with service key */
  339. len = crypto_pk_private_hybrid_decrypt(
  340. service->private_key,request+DIGEST_LEN,request_len-DIGEST_LEN,buf,
  341. PK_PKCS1_OAEP_PADDING);
  342. if (len<0) {
  343. log_fn(LOG_WARN, "Couldn't decrypt INTRODUCE2 cell");
  344. return -1;
  345. }
  346. ptr=memchr(buf,0,MAX_NICKNAME_LEN+1);
  347. if (!ptr || ptr == buf) {
  348. log_fn(LOG_WARN, "Couldn't find a null-padded nickname in INTRODUCE2 cell");
  349. return -1;
  350. }
  351. if (strspn(buf,LEGAL_NICKNAME_CHARACTERS) != ptr-buf) {
  352. log_fn(LOG_WARN, "Nickname in INTRODUCE2 cell contains illegal character.");
  353. return -1;
  354. }
  355. /* Okay, now we know that the nickname is at the start of the buffer. */
  356. rp_nickname = buf;
  357. ptr = buf+(MAX_NICKNAME_LEN+1);
  358. len -= (MAX_NICKNAME_LEN+1);
  359. if (len != REND_COOKIE_LEN+DH_KEY_LEN) {
  360. log_fn(LOG_WARN, "Bad length for INTRODUCE2 cell.");
  361. return -1;
  362. }
  363. r_cookie = ptr;
  364. hex_encode(r_cookie,4,hexcookie);
  365. /* Try DH handshake... */
  366. dh = crypto_dh_new();
  367. if (!dh || crypto_dh_generate_public(dh)<0) {
  368. log_fn(LOG_WARN, "Couldn't build DH state or generate public key");
  369. goto err;
  370. }
  371. if (crypto_dh_compute_secret(dh, ptr+REND_COOKIE_LEN, DH_KEY_LEN, keys,
  372. DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  373. log_fn(LOG_WARN, "Couldn't complete DH handshake");
  374. goto err;
  375. }
  376. /* Launch a circuit to alice's chosen rendezvous point.
  377. */
  378. launched = circuit_launch_new(CIRCUIT_PURPOSE_S_CONNECT_REND, rp_nickname);
  379. log_fn(LOG_INFO,
  380. "Accepted intro; launching circuit to '%s' (cookie %s) for service %s",
  381. rp_nickname, hexcookie, hexid);
  382. if (!launched) {
  383. log_fn(LOG_WARN,
  384. "Can't launch circuit to rendezvous point '%s' for service %s",
  385. rp_nickname, hexid);
  386. return -1;
  387. }
  388. assert(launched->build_state);
  389. /* Fill in the circuit's state. */
  390. memcpy(launched->rend_pk_digest, circuit->rend_pk_digest,
  391. DIGEST_LEN);
  392. memcpy(launched->rend_cookie, r_cookie, REND_COOKIE_LEN);
  393. strcpy(launched->rend_query, service->service_id);
  394. launched->build_state->pending_final_cpath = cpath =
  395. tor_malloc_zero(sizeof(crypt_path_t));
  396. cpath->handshake_state = dh;
  397. dh = NULL;
  398. if (circuit_init_cpath_crypto(cpath,keys+DIGEST_LEN,1)<0)
  399. goto err;
  400. memcpy(cpath->handshake_digest, keys, DIGEST_LEN);
  401. return 0;
  402. err:
  403. if (dh) crypto_dh_free(dh);
  404. if (launched) circuit_mark_for_close(launched);
  405. return -1;
  406. }
  407. /* Launch a circuit to serve as an introduction point.
  408. */
  409. static int
  410. rend_service_launch_establish_intro(rend_service_t *service, char *nickname)
  411. {
  412. circuit_t *launched;
  413. char hexid[9];
  414. assert(service && nickname);
  415. hex_encode(service->pk_digest, 4, hexid);
  416. log_fn(LOG_INFO, "Launching circuit to introduction point %s for service %s",
  417. nickname, hexid);
  418. launched = circuit_launch_new(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO, nickname);
  419. if (!launched) {
  420. log_fn(LOG_WARN, "Can't launch circuit to establish introduction at '%s'",
  421. nickname);
  422. return -1;
  423. }
  424. strcpy(launched->rend_query, service->service_id);
  425. memcpy(launched->rend_pk_digest, service->pk_digest, DIGEST_LEN);
  426. return 0;
  427. }
  428. /* Called when we're done building a circuit to an introduction point:
  429. * sends a RELAY_ESTABLISH_INTRO cell.
  430. */
  431. void
  432. rend_service_intro_is_ready(circuit_t *circuit)
  433. {
  434. rend_service_t *service;
  435. int len, r;
  436. char buf[RELAY_PAYLOAD_SIZE];
  437. char auth[DIGEST_LEN + 9];
  438. char hexid[9];
  439. assert(circuit->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO);
  440. assert(CIRCUIT_IS_ORIGIN(circuit) && circuit->cpath);
  441. hex_encode(circuit->rend_pk_digest, 4, hexid);
  442. service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
  443. if (!service) {
  444. log_fn(LOG_WARN, "Unrecognized service ID %s on introduction circuit %d",
  445. hexid, circuit->n_circ_id);
  446. goto err;
  447. }
  448. log_fn(LOG_INFO,
  449. "Established circuit %d as introduction point for service %s",
  450. circuit->n_circ_id, hexid);
  451. /* Build the payload for a RELAY_ESTABLISH_INTRO cell. */
  452. len = crypto_pk_asn1_encode(service->private_key, buf+2,
  453. RELAY_PAYLOAD_SIZE-2);
  454. set_uint16(buf, htons(len));
  455. len += 2;
  456. memcpy(auth, circuit->cpath->prev->handshake_digest, DIGEST_LEN);
  457. memcpy(auth+DIGEST_LEN, "INTRODUCE", 9);
  458. if (crypto_digest(auth, DIGEST_LEN+9, buf+len))
  459. goto err;
  460. len += 20;
  461. r = crypto_pk_private_sign_digest(service->private_key, buf, len, buf+len);
  462. if (r<0) {
  463. log_fn(LOG_WARN, "Couldn't sign introduction request");
  464. goto err;
  465. }
  466. len += r;
  467. if (connection_edge_send_command(NULL, circuit,RELAY_COMMAND_ESTABLISH_INTRO,
  468. buf, len, circuit->cpath->prev)<0) {
  469. log_fn(LOG_WARN,
  470. "Couldn't send introduction request for service %s on circuit %d",
  471. hexid, circuit->n_circ_id);
  472. goto err;
  473. }
  474. return;
  475. err:
  476. circuit_mark_for_close(circuit);
  477. }
  478. /* Handle an intro_established cell. */
  479. int
  480. rend_service_intro_established(circuit_t *circuit, const char *request, int request_len)
  481. {
  482. if (circuit->purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO) {
  483. log_fn(LOG_WARN, "received INTRO_ESTABLISHED cell on non-intro circuit");
  484. goto err;
  485. }
  486. circuit->purpose = CIRCUIT_PURPOSE_S_INTRO;
  487. return 0;
  488. err:
  489. circuit_mark_for_close(circuit);
  490. return -1;
  491. }
  492. /* Called once a circuit to a rendezvous point is ready: sends a
  493. * RELAY_COMMAND_RENDEZVOUS1 cell.
  494. */
  495. void
  496. rend_service_rendezvous_is_ready(circuit_t *circuit)
  497. {
  498. rend_service_t *service;
  499. char buf[RELAY_PAYLOAD_SIZE];
  500. crypt_path_t *hop;
  501. char hexid[9];
  502. char hexcookie[9];
  503. assert(circuit->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  504. assert(circuit->cpath);
  505. assert(circuit->build_state);
  506. hop = circuit->build_state->pending_final_cpath;
  507. assert(hop);
  508. hex_encode(circuit->rend_pk_digest, 4, hexid);
  509. hex_encode(circuit->rend_cookie, 4, hexcookie);
  510. log_fn(LOG_INFO,
  511. "Done building circuit %d to rendezvous with cookie %s for service %s",
  512. circuit->n_circ_id, hexcookie, hexid);
  513. service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
  514. if (!service) {
  515. log_fn(LOG_WARN, "Internal error: unrecognized service ID on introduction circuit");
  516. goto err;
  517. }
  518. /* All we need to do is send a RELAY_RENDEZVOUS1 cell... */
  519. memcpy(buf, circuit->rend_cookie, REND_COOKIE_LEN);
  520. if (crypto_dh_get_public(hop->handshake_state,
  521. buf+REND_COOKIE_LEN, DH_KEY_LEN)<0) {
  522. log_fn(LOG_WARN,"Couldn't get DH public key");
  523. goto err;
  524. }
  525. memcpy(buf+REND_COOKIE_LEN+DH_KEY_LEN, hop->handshake_digest,
  526. DIGEST_LEN);
  527. /* Send the cell */
  528. if (connection_edge_send_command(NULL, circuit, RELAY_COMMAND_RENDEZVOUS1,
  529. buf, REND_COOKIE_LEN+DH_KEY_LEN+DIGEST_LEN,
  530. circuit->cpath->prev)<0) {
  531. log_fn(LOG_WARN, "Couldn't send RENDEZVOUS1 cell");
  532. goto err;
  533. }
  534. crypto_dh_free(hop->handshake_state);
  535. hop->handshake_state = NULL;
  536. /* Append the cpath entry. */
  537. hop->state = CPATH_STATE_OPEN;
  538. /* set the windows to default. these are the windows
  539. * that bob thinks alice has.
  540. */
  541. hop->package_window = CIRCWINDOW_START;
  542. hop->deliver_window = CIRCWINDOW_START;
  543. onion_append_to_cpath(&circuit->cpath, hop);
  544. circuit->build_state->pending_final_cpath = NULL; /* prevent double-free */
  545. /* Change the circuit purpose. */
  546. circuit->purpose = CIRCUIT_PURPOSE_S_REND_JOINED;
  547. return;
  548. err:
  549. circuit_mark_for_close(circuit);
  550. }
  551. /******
  552. * Manage introduction points
  553. ******/
  554. /* Return the introduction circuit ending at 'router' for the service
  555. * whose public key is 'pk_digest'. Return NULL if no such service is
  556. * found.
  557. */
  558. static circuit_t *
  559. find_intro_circuit(routerinfo_t *router, const char *pk_digest)
  560. {
  561. circuit_t *circ = NULL;
  562. while ((circ = circuit_get_next_by_pk_and_purpose(circ,pk_digest,
  563. CIRCUIT_PURPOSE_S_INTRO))) {
  564. assert(circ->cpath);
  565. if (circ->cpath->prev->addr == router->addr &&
  566. circ->cpath->prev->port == router->or_port) {
  567. return circ;
  568. }
  569. }
  570. circ = NULL;
  571. while ((circ = circuit_get_next_by_pk_and_purpose(circ,pk_digest,
  572. CIRCUIT_PURPOSE_S_ESTABLISH_INTRO))) {
  573. assert(circ->cpath);
  574. if (circ->cpath->prev->addr == router->addr &&
  575. circ->cpath->prev->port == router->or_port) {
  576. return circ;
  577. }
  578. }
  579. return NULL;
  580. }
  581. /* XXXX Make this longer once directories remember service descriptors across
  582. * restarts.*/
  583. #define MAX_SERVICE_PUBLICATION_INTERVAL (15*60)
  584. /* For every service, check how many intro points it currently has, and:
  585. * - Pick new intro points as necessary.
  586. * - Launch circuits to any new intro points.
  587. * - Upload a fresh service descriptor if anything has changed.
  588. */
  589. int rend_services_init(void) {
  590. int i,j,r;
  591. routerinfo_t *router;
  592. routerlist_t *rl;
  593. rend_service_t *service;
  594. char *desc, *intro;
  595. int changed, prev_intro_nodes, desc_len;
  596. smartlist_t *intro_routers, *exclude_routers;
  597. int n_old_routers;
  598. time_t now;
  599. router_get_routerlist(&rl);
  600. intro_routers = smartlist_create();
  601. exclude_routers = smartlist_create();
  602. router_add_nonrendezvous_to_list(exclude_routers);
  603. n_old_routers = smartlist_len(exclude_routers);
  604. for (i=0; i< smartlist_len(rend_service_list); ++i) {
  605. smartlist_clear(intro_routers);
  606. service = smartlist_get(rend_service_list, i);
  607. assert(service);
  608. changed = 0;
  609. /* Find out which introduction points we really have for this service. */
  610. for (j=0;j< smartlist_len(service->intro_nodes); ++j) {
  611. router = router_get_by_nickname(smartlist_get(service->intro_nodes,j));
  612. if (!router || !find_intro_circuit(router,service->pk_digest)) {
  613. smartlist_del(service->intro_nodes,j--);
  614. changed = 1;
  615. }
  616. smartlist_add(intro_routers, router);
  617. }
  618. /* We have enough intro points, and the intro points we thought we had were
  619. * all connected.
  620. */
  621. if (!changed && smartlist_len(service->intro_nodes) >= NUM_INTRO_POINTS)
  622. continue;
  623. /* Remember how many introduction circuits we started with. */
  624. prev_intro_nodes = smartlist_len(service->intro_nodes);
  625. smartlist_add_all(exclude_routers, intro_routers);
  626. /* The directory is now here. Pick three ORs as intro points. */
  627. for (j=prev_intro_nodes; j < NUM_INTRO_POINTS; ++j) {
  628. router = router_choose_random_node(rl,
  629. service->intro_prefer_nodes,
  630. service->intro_exclude_nodes,
  631. exclude_routers);
  632. if (!router) {
  633. log_fn(LOG_WARN, "Could only establish %d introduction points",
  634. smartlist_len(service->intro_nodes));
  635. break;
  636. }
  637. changed = 1;
  638. smartlist_add(intro_routers, router);
  639. smartlist_add(exclude_routers, router);
  640. smartlist_add(service->intro_nodes, tor_strdup(router->nickname));
  641. log_fn(LOG_INFO,"Picked router %s as an intro point.", router->nickname);
  642. }
  643. /* Reset exclude_routers to include obsolete routers only for the next
  644. * time around the loop. */
  645. smartlist_truncate(exclude_routers, n_old_routers);
  646. /* If there's no need to republish, stop here. */
  647. now = time(NULL);
  648. if (!changed && service->desc &&
  649. service->desc->timestamp+MAX_SERVICE_PUBLICATION_INTERVAL >= now)
  650. continue;
  651. /* Update the descriptor. */
  652. rend_service_update_descriptor(service);
  653. if (rend_encode_service_descriptor(service->desc,
  654. service->private_key,
  655. &desc, &desc_len)<0) {
  656. log_fn(LOG_WARN, "Couldn't encode service descriptor; not uploading");
  657. continue;
  658. }
  659. /* Post it to the dirservers */
  660. router_post_to_dirservers(DIR_PURPOSE_UPLOAD_RENDDESC, desc, desc_len);
  661. tor_free(desc);
  662. /* Establish new introduction points. */
  663. for (j=prev_intro_nodes; j < smartlist_len(service->intro_nodes); ++j) {
  664. intro = smartlist_get(service->intro_nodes, j);
  665. r = rend_service_launch_establish_intro(service, intro);
  666. if (r<0) {
  667. log_fn(LOG_WARN, "Error launching circuit to node %s", intro);
  668. }
  669. }
  670. }
  671. smartlist_free(intro_routers);
  672. smartlist_free(exclude_routers);
  673. return 0;
  674. }
  675. /* This is a beginning rendezvous stream. Look up conn->port,
  676. * and assign the actual conn->addr and conn->port. Return -1
  677. * if failure, or 0 for success.
  678. */
  679. int
  680. rend_service_set_connection_addr_port(connection_t *conn, circuit_t *circ)
  681. {
  682. rend_service_t *service;
  683. int i;
  684. rend_service_port_config_t *p;
  685. char hexid[9];
  686. assert(circ->purpose == CIRCUIT_PURPOSE_S_REND_JOINED);
  687. hex_encode(circ->rend_pk_digest, 4, hexid);
  688. service = rend_service_get_by_pk_digest(circ->rend_pk_digest);
  689. if (!service) {
  690. log_fn(LOG_WARN, "Couldn't find any service associated with pk %s on rendezvous circuit %d; closing",
  691. hexid, circ->n_circ_id);
  692. circuit_mark_for_close(circ);
  693. connection_mark_for_close(conn, 0/*XXX*/);
  694. }
  695. for (i = 0; i < smartlist_len(service->ports); ++i) {
  696. p = smartlist_get(service->ports, i);
  697. if (conn->port == p->virtual_port) {
  698. conn->addr = p->real_address;
  699. conn->port = p->real_port;
  700. return 0;
  701. }
  702. }
  703. log_fn(LOG_WARN, "No virtual port mapping exists for port %d on service %s",
  704. conn->port, hexid);
  705. connection_mark_for_close(conn, 0/*XXX*/);
  706. return -1;
  707. }
  708. /*
  709. Local Variables:
  710. mode:c
  711. indent-tabs-mode:nil
  712. c-basic-offset:2
  713. End:
  714. */