rendservice.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. /* Copyright 2004-2007 Roger Dingledine, Nick Mathewson. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. const char rendservice_c_id[] =
  5. "$Id$";
  6. /**
  7. * \file rendservice.c
  8. * \brief The hidden-service side of rendezvous functionality.
  9. **/
  10. #include "or.h"
  11. static origin_circuit_t *find_intro_circuit(routerinfo_t *router,
  12. const char *pk_digest);
  13. /** Represents the mapping from a virtual port of a rendezvous service to
  14. * a real port on some IP.
  15. */
  16. typedef struct rend_service_port_config_t {
  17. uint16_t virtual_port;
  18. uint16_t real_port;
  19. uint32_t real_addr;
  20. } rend_service_port_config_t;
  21. /** Try to maintain this many intro points per service if possible. */
  22. #define NUM_INTRO_POINTS 3
  23. /** If we can't build our intro circuits, don't retry for this long. */
  24. #define INTRO_CIRC_RETRY_PERIOD (60*5)
  25. /** Don't try to build more than this many circuits before giving up
  26. * for a while.*/
  27. #define MAX_INTRO_CIRCS_PER_PERIOD 10
  28. /** How many times will a hidden service operator attempt to connect to
  29. * a requested rendezvous point before giving up? */
  30. #define MAX_REND_FAILURES 30
  31. /** How many seconds should we spend trying to connect to a requested
  32. * rendezvous point before giving up? */
  33. #define MAX_REND_TIMEOUT 30
  34. /** Represents a single hidden service running at this OP. */
  35. typedef struct rend_service_t {
  36. /** Fields specified in config file */
  37. char *directory; /**< where in the filesystem it stores it */
  38. smartlist_t *ports; /**< List of rend_service_port_config_t */
  39. char *intro_prefer_nodes; /**< comma-separated list of nicknames */
  40. char *intro_exclude_nodes; /**< comma-separated list of nicknames */
  41. /* Other fields */
  42. crypto_pk_env_t *private_key;
  43. char service_id[REND_SERVICE_ID_LEN+1];
  44. char pk_digest[DIGEST_LEN];
  45. smartlist_t *intro_nodes; /**< list of hexdigests for intro points we have,
  46. * or are trying to establish. */
  47. time_t intro_period_started;
  48. int n_intro_circuits_launched; /**< count of intro circuits we have
  49. * established in this period. */
  50. rend_service_descriptor_t *desc;
  51. time_t desc_is_dirty;
  52. time_t next_upload_time;
  53. } rend_service_t;
  54. /** A list of rend_service_t's for services run on this OP.
  55. */
  56. static smartlist_t *rend_service_list = NULL;
  57. /** Return the number of rendezvous services we have configured. */
  58. int
  59. num_rend_services(void)
  60. {
  61. if (!rend_service_list)
  62. return 0;
  63. return smartlist_len(rend_service_list);
  64. }
  65. /** Release the storage held by <b>service</b>.
  66. */
  67. static void
  68. rend_service_free(rend_service_t *service)
  69. {
  70. if (!service) return;
  71. tor_free(service->directory);
  72. SMARTLIST_FOREACH(service->ports, void*, p, tor_free(p));
  73. smartlist_free(service->ports);
  74. if (service->private_key)
  75. crypto_free_pk_env(service->private_key);
  76. tor_free(service->intro_prefer_nodes);
  77. tor_free(service->intro_exclude_nodes);
  78. SMARTLIST_FOREACH(service->intro_nodes, void*, p, tor_free(p));
  79. smartlist_free(service->intro_nodes);
  80. if (service->desc)
  81. rend_service_descriptor_free(service->desc);
  82. tor_free(service);
  83. }
  84. /** Release all the storage held in rend_service_list.
  85. */
  86. void
  87. rend_service_free_all(void)
  88. {
  89. if (!rend_service_list) {
  90. return;
  91. }
  92. SMARTLIST_FOREACH(rend_service_list, rend_service_t*, ptr,
  93. rend_service_free(ptr));
  94. smartlist_free(rend_service_list);
  95. rend_service_list = NULL;
  96. }
  97. /** Validate <b>service</b> and add it to rend_service_list if possible.
  98. */
  99. static void
  100. add_service(rend_service_t *service)
  101. {
  102. int i;
  103. rend_service_port_config_t *p;
  104. struct in_addr addr;
  105. if (!service->intro_prefer_nodes)
  106. service->intro_prefer_nodes = tor_strdup("");
  107. if (!service->intro_exclude_nodes)
  108. service->intro_exclude_nodes = tor_strdup("");
  109. if (!smartlist_len(service->ports)) {
  110. log_warn(LD_CONFIG, "Hidden service with no ports configured; ignoring.");
  111. rend_service_free(service);
  112. } else {
  113. smartlist_set_capacity(service->ports, -1);
  114. smartlist_add(rend_service_list, service);
  115. log_debug(LD_REND,"Configuring service with directory \"%s\"",
  116. service->directory);
  117. for (i = 0; i < smartlist_len(service->ports); ++i) {
  118. char addrbuf[INET_NTOA_BUF_LEN];
  119. p = smartlist_get(service->ports, i);
  120. addr.s_addr = htonl(p->real_addr);
  121. tor_inet_ntoa(&addr, addrbuf, sizeof(addrbuf));
  122. log_debug(LD_REND,"Service maps port %d to %s:%d",
  123. p->virtual_port, addrbuf, p->real_port);
  124. }
  125. }
  126. }
  127. /** Parses a real-port to virtual-port mapping and returns a new
  128. * rend_service_port_config_t.
  129. *
  130. * The format is: VirtualPort (IP|RealPort|IP:RealPort)?
  131. *
  132. * IP defaults to 127.0.0.1; RealPort defaults to VirtualPort.
  133. */
  134. static rend_service_port_config_t *
  135. parse_port_config(const char *string)
  136. {
  137. smartlist_t *sl;
  138. int virtport;
  139. int realport;
  140. uint16_t p;
  141. uint32_t addr;
  142. const char *addrport;
  143. rend_service_port_config_t *result = NULL;
  144. sl = smartlist_create();
  145. smartlist_split_string(sl, string, " ",
  146. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  147. if (smartlist_len(sl) < 1 || smartlist_len(sl) > 2) {
  148. log_warn(LD_CONFIG, "Bad syntax in hidden service port configuration.");
  149. goto err;
  150. }
  151. virtport = atoi(smartlist_get(sl,0));
  152. if (virtport < 1 || virtport > 65535) {
  153. log_warn(LD_CONFIG, "Missing or invalid port in hidden service port "
  154. "configuration.");
  155. goto err;
  156. }
  157. if (smartlist_len(sl) == 1) {
  158. /* No addr:port part; use default. */
  159. realport = virtport;
  160. addr = 0x7F000001u; /* 127.0.0.1 */
  161. } else {
  162. addrport = smartlist_get(sl,1);
  163. if (strchr(addrport, ':') || strchr(addrport, '.')) {
  164. if (parse_addr_port(LOG_WARN, addrport, NULL, &addr, &p)<0) {
  165. log_warn(LD_CONFIG,"Unparseable address in hidden service port "
  166. "configuration.");
  167. goto err;
  168. }
  169. realport = p?p:virtport;
  170. } else {
  171. /* No addr:port, no addr -- must be port. */
  172. realport = atoi(addrport);
  173. if (realport < 1 || realport > 65535)
  174. goto err;
  175. addr = 0x7F000001u; /* Default to 127.0.0.1 */
  176. }
  177. }
  178. result = tor_malloc(sizeof(rend_service_port_config_t));
  179. result->virtual_port = virtport;
  180. result->real_port = realport;
  181. result->real_addr = addr;
  182. err:
  183. SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
  184. smartlist_free(sl);
  185. return result;
  186. }
  187. /** Set up rend_service_list, based on the values of HiddenServiceDir and
  188. * HiddenServicePort in <b>options</b>. Return 0 on success and -1 on
  189. * failure. (If <b>validate_only</b> is set, parse, warn and return as
  190. * normal, but don't actually change the configured services.)
  191. */
  192. int
  193. rend_config_services(or_options_t *options, int validate_only)
  194. {
  195. config_line_t *line;
  196. rend_service_t *service = NULL;
  197. rend_service_port_config_t *portcfg;
  198. if (!validate_only) {
  199. rend_service_free_all();
  200. rend_service_list = smartlist_create();
  201. }
  202. for (line = options->RendConfigLines; line; line = line->next) {
  203. if (!strcasecmp(line->key, "HiddenServiceDir")) {
  204. if (service) {
  205. if (validate_only)
  206. rend_service_free(service);
  207. else
  208. add_service(service);
  209. }
  210. service = tor_malloc_zero(sizeof(rend_service_t));
  211. service->directory = tor_strdup(line->value);
  212. service->ports = smartlist_create();
  213. service->intro_nodes = smartlist_create();
  214. service->intro_period_started = time(NULL);
  215. continue;
  216. }
  217. if (!service) {
  218. log_warn(LD_CONFIG, "%s with no preceding HiddenServiceDir directive",
  219. line->key);
  220. rend_service_free(service);
  221. return -1;
  222. }
  223. if (!strcasecmp(line->key, "HiddenServicePort")) {
  224. portcfg = parse_port_config(line->value);
  225. if (!portcfg) {
  226. rend_service_free(service);
  227. return -1;
  228. }
  229. smartlist_add(service->ports, portcfg);
  230. } else if (!strcasecmp(line->key, "HiddenServiceNodes")) {
  231. if (service->intro_prefer_nodes) {
  232. log_warn(LD_CONFIG,
  233. "Got multiple HiddenServiceNodes lines for a single "
  234. "service.");
  235. return -1;
  236. }
  237. service->intro_prefer_nodes = tor_strdup(line->value);
  238. } else {
  239. tor_assert(!strcasecmp(line->key, "HiddenServiceExcludeNodes"));
  240. if (service->intro_exclude_nodes) {
  241. log_warn(LD_CONFIG,
  242. "Got multiple HiddenServiceExcludedNodes lines for "
  243. "a single service.");
  244. return -1;
  245. }
  246. service->intro_exclude_nodes = tor_strdup(line->value);
  247. }
  248. }
  249. if (service) {
  250. if (validate_only)
  251. rend_service_free(service);
  252. else
  253. add_service(service);
  254. }
  255. return 0;
  256. }
  257. /** Replace the old value of <b>service</b>-\>desc with one that reflects
  258. * the other fields in service.
  259. */
  260. static void
  261. rend_service_update_descriptor(rend_service_t *service)
  262. {
  263. rend_service_descriptor_t *d;
  264. origin_circuit_t *circ;
  265. int i,n;
  266. routerinfo_t *router;
  267. if (service->desc) {
  268. rend_service_descriptor_free(service->desc);
  269. service->desc = NULL;
  270. }
  271. d = service->desc = tor_malloc(sizeof(rend_service_descriptor_t));
  272. d->pk = crypto_pk_dup_key(service->private_key);
  273. d->timestamp = time(NULL);
  274. d->version = 1;
  275. n = smartlist_len(service->intro_nodes);
  276. d->n_intro_points = 0;
  277. d->intro_points = tor_malloc_zero(sizeof(char*)*n);
  278. d->intro_point_extend_info = tor_malloc_zero(sizeof(extend_info_t*)*n);
  279. /* We support intro protocol 2 and protocol 0. */
  280. d->protocols = (1<<2) | (1<<0);
  281. for (i=0; i < n; ++i) {
  282. const char *name = smartlist_get(service->intro_nodes, i);
  283. router = router_get_by_nickname(name, 1);
  284. if (!router) {
  285. log_info(LD_REND,"Router '%s' not found for intro point %d. Skipping.",
  286. safe_str(name), i);
  287. continue;
  288. }
  289. circ = find_intro_circuit(router, service->pk_digest);
  290. if (circ && circ->_base.purpose == CIRCUIT_PURPOSE_S_INTRO) {
  291. /* We have an entirely established intro circuit. */
  292. d->intro_points[d->n_intro_points] = tor_strdup(name);
  293. d->intro_point_extend_info[d->n_intro_points] =
  294. extend_info_from_router(router);
  295. d->n_intro_points++;
  296. }
  297. }
  298. }
  299. /** Load and/or generate private keys for all hidden services. Return 0 on
  300. * success, -1 on failure.
  301. */
  302. int
  303. rend_service_load_keys(void)
  304. {
  305. int i;
  306. rend_service_t *s;
  307. char fname[512];
  308. char buf[128];
  309. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  310. s = smartlist_get(rend_service_list,i);
  311. if (s->private_key)
  312. continue;
  313. log_info(LD_REND, "Loading hidden-service keys from \"%s\"",
  314. s->directory);
  315. /* Check/create directory */
  316. if (check_private_dir(s->directory, CPD_CREATE) < 0)
  317. return -1;
  318. /* Load key */
  319. if (strlcpy(fname,s->directory,sizeof(fname)) >= sizeof(fname) ||
  320. strlcat(fname,PATH_SEPARATOR"private_key",sizeof(fname))
  321. >= sizeof(fname)) {
  322. log_warn(LD_CONFIG, "Directory name too long to store key file: \"%s\".",
  323. s->directory);
  324. return -1;
  325. }
  326. s->private_key = init_key_from_file(fname, 1, LOG_ERR);
  327. if (!s->private_key)
  328. return -1;
  329. /* Create service file */
  330. if (rend_get_service_id(s->private_key, s->service_id)<0) {
  331. log_warn(LD_BUG, "Internal error: couldn't encode service ID.");
  332. return -1;
  333. }
  334. if (crypto_pk_get_digest(s->private_key, s->pk_digest)<0) {
  335. log_warn(LD_BUG, "Couldn't compute hash of public key.");
  336. return -1;
  337. }
  338. if (strlcpy(fname,s->directory,sizeof(fname)) >= sizeof(fname) ||
  339. strlcat(fname,PATH_SEPARATOR"hostname",sizeof(fname))
  340. >= sizeof(fname)) {
  341. log_warn(LD_CONFIG, "Directory name too long to store hostname file:"
  342. " \"%s\".", s->directory);
  343. return -1;
  344. }
  345. tor_snprintf(buf, sizeof(buf),"%s.onion\n", s->service_id);
  346. if (write_str_to_file(fname,buf,0)<0)
  347. return -1;
  348. }
  349. return 0;
  350. }
  351. /** Return the service whose public key has a digest of <b>digest</b>. Return
  352. * NULL if no such service exists.
  353. */
  354. static rend_service_t *
  355. rend_service_get_by_pk_digest(const char* digest)
  356. {
  357. SMARTLIST_FOREACH(rend_service_list, rend_service_t*, s,
  358. if (!memcmp(s->pk_digest,digest,DIGEST_LEN)) return s);
  359. return NULL;
  360. }
  361. /** Return 1 if any virtual port in <b>service</b> wants a circuit
  362. * to have good uptime. Else return 0.
  363. */
  364. static int
  365. rend_service_requires_uptime(rend_service_t *service)
  366. {
  367. int i;
  368. rend_service_port_config_t *p;
  369. for (i=0; i < smartlist_len(service->ports); ++i) {
  370. p = smartlist_get(service->ports, i);
  371. if (smartlist_string_num_isin(get_options()->LongLivedPorts,
  372. p->virtual_port))
  373. return 1;
  374. }
  375. return 0;
  376. }
  377. /******
  378. * Handle cells
  379. ******/
  380. /** Respond to an INTRODUCE2 cell by launching a circuit to the chosen
  381. * rendezvous point.
  382. */
  383. int
  384. rend_service_introduce(origin_circuit_t *circuit, const char *request,
  385. size_t request_len)
  386. {
  387. char *ptr, *r_cookie;
  388. extend_info_t *extend_info = NULL;
  389. char buf[RELAY_PAYLOAD_SIZE];
  390. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN]; /* Holds KH, Df, Db, Kf, Kb */
  391. rend_service_t *service;
  392. int r, i;
  393. size_t len, keylen;
  394. crypto_dh_env_t *dh = NULL;
  395. origin_circuit_t *launched = NULL;
  396. crypt_path_t *cpath = NULL;
  397. char serviceid[REND_SERVICE_ID_LEN+1];
  398. char hexcookie[9];
  399. int circ_needs_uptime;
  400. int reason = END_CIRC_REASON_TORPROTOCOL;
  401. base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
  402. circuit->rend_pk_digest,10);
  403. log_info(LD_REND, "Received INTRODUCE2 cell for service %s on circ %d.",
  404. escaped(serviceid), circuit->_base.n_circ_id);
  405. if (circuit->_base.purpose != CIRCUIT_PURPOSE_S_INTRO) {
  406. log_warn(LD_PROTOCOL,
  407. "Got an INTRODUCE2 over a non-introduction circuit %d.",
  408. circuit->_base.n_circ_id);
  409. return -1;
  410. }
  411. /* min key length plus digest length plus nickname length */
  412. if (request_len < DIGEST_LEN+REND_COOKIE_LEN+(MAX_NICKNAME_LEN+1)+
  413. DH_KEY_LEN+42) {
  414. log_warn(LD_PROTOCOL, "Got a truncated INTRODUCE2 cell on circ %d.",
  415. circuit->_base.n_circ_id);
  416. return -1;
  417. }
  418. /* first DIGEST_LEN bytes of request is service pk digest */
  419. service = rend_service_get_by_pk_digest(request);
  420. if (!service) {
  421. log_warn(LD_REND, "Got an INTRODUCE2 cell for an unrecognized service %s.",
  422. escaped(serviceid));
  423. return -1;
  424. }
  425. if (memcmp(circuit->rend_pk_digest, request, DIGEST_LEN)) {
  426. base32_encode(serviceid, REND_SERVICE_ID_LEN+1, request, 10);
  427. log_warn(LD_REND, "Got an INTRODUCE2 cell for the wrong service (%s).",
  428. escaped(serviceid));
  429. return -1;
  430. }
  431. keylen = crypto_pk_keysize(service->private_key);
  432. if (request_len < keylen+DIGEST_LEN) {
  433. log_warn(LD_PROTOCOL,
  434. "PK-encrypted portion of INTRODUCE2 cell was truncated.");
  435. return -1;
  436. }
  437. /* Next N bytes is encrypted with service key */
  438. note_crypto_pk_op(REND_SERVER);
  439. r = crypto_pk_private_hybrid_decrypt(
  440. service->private_key,buf,request+DIGEST_LEN,request_len-DIGEST_LEN,
  441. PK_PKCS1_OAEP_PADDING,1);
  442. if (r<0) {
  443. log_warn(LD_PROTOCOL, "Couldn't decrypt INTRODUCE2 cell.");
  444. return -1;
  445. }
  446. len = r;
  447. if (*buf == 2) {
  448. /* Version 2 INTRODUCE2 cell. */
  449. int klen;
  450. extend_info = tor_malloc_zero(sizeof(extend_info_t));
  451. extend_info->addr = ntohl(get_uint32(buf+1));
  452. extend_info->port = ntohs(get_uint16(buf+5));
  453. memcpy(extend_info->identity_digest, buf+7, DIGEST_LEN);
  454. extend_info->nickname[0] = '$';
  455. base16_encode(extend_info->nickname+1, sizeof(extend_info->nickname)-1,
  456. extend_info->identity_digest, DIGEST_LEN);
  457. klen = ntohs(get_uint16(buf+7+DIGEST_LEN));
  458. if ((int)len != 7+DIGEST_LEN+2+klen+20+128) {
  459. log_warn(LD_PROTOCOL, "Bad length %u for version 2 INTRODUCE2 cell.",
  460. (int)len);
  461. reason = END_CIRC_REASON_TORPROTOCOL;
  462. goto err;
  463. }
  464. extend_info->onion_key = crypto_pk_asn1_decode(buf+7+DIGEST_LEN+2, klen);
  465. if (!extend_info->onion_key) {
  466. log_warn(LD_PROTOCOL,
  467. "Error decoding onion key in version 2 INTRODUCE2 cell.");
  468. reason = END_CIRC_REASON_TORPROTOCOL;
  469. goto err;
  470. }
  471. ptr = buf+7+DIGEST_LEN+2+klen;
  472. len -= 7+DIGEST_LEN+2+klen;
  473. } else {
  474. char *rp_nickname;
  475. size_t nickname_field_len;
  476. routerinfo_t *router;
  477. int version;
  478. if (*buf == 1) {
  479. rp_nickname = buf+1;
  480. nickname_field_len = MAX_HEX_NICKNAME_LEN+1;
  481. version = 1;
  482. } else {
  483. nickname_field_len = MAX_NICKNAME_LEN+1;
  484. rp_nickname = buf;
  485. version = 0;
  486. }
  487. ptr=memchr(rp_nickname,0,nickname_field_len);
  488. if (!ptr || ptr == rp_nickname) {
  489. log_warn(LD_PROTOCOL,
  490. "Couldn't find a nul-padded nickname in INTRODUCE2 cell.");
  491. return -1;
  492. }
  493. if ((version == 0 && !is_legal_nickname(rp_nickname)) ||
  494. (version == 1 && !is_legal_nickname_or_hexdigest(rp_nickname))) {
  495. log_warn(LD_PROTOCOL, "Bad nickname in INTRODUCE2 cell.");
  496. return -1;
  497. }
  498. /* Okay, now we know that a nickname is at the start of the buffer. */
  499. ptr = rp_nickname+nickname_field_len;
  500. len -= nickname_field_len;
  501. len -= rp_nickname - buf; /* also remove header space used by version, if
  502. * any */
  503. router = router_get_by_nickname(rp_nickname, 0);
  504. if (!router) {
  505. log_info(LD_REND, "Couldn't find router %s named in introduce2 cell.",
  506. escaped_safe_str(rp_nickname));
  507. /* XXXX Add a no-such-router reason? */
  508. reason = END_CIRC_REASON_TORPROTOCOL;
  509. goto err;
  510. }
  511. extend_info = extend_info_from_router(router);
  512. }
  513. if (len != REND_COOKIE_LEN+DH_KEY_LEN) {
  514. log_warn(LD_PROTOCOL, "Bad length %u for INTRODUCE2 cell.", (int)len);
  515. reason = END_CIRC_REASON_TORPROTOCOL;
  516. return -1;
  517. }
  518. r_cookie = ptr;
  519. base16_encode(hexcookie,9,r_cookie,4);
  520. /* Try DH handshake... */
  521. dh = crypto_dh_new();
  522. if (!dh || crypto_dh_generate_public(dh)<0) {
  523. log_warn(LD_BUG,"Internal error: couldn't build DH state "
  524. "or generate public key.");
  525. reason = END_CIRC_REASON_INTERNAL;
  526. goto err;
  527. }
  528. if (crypto_dh_compute_secret(dh, ptr+REND_COOKIE_LEN, DH_KEY_LEN, keys,
  529. DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  530. log_warn(LD_BUG, "Internal error: couldn't complete DH handshake");
  531. reason = END_CIRC_REASON_INTERNAL;
  532. goto err;
  533. }
  534. circ_needs_uptime = rend_service_requires_uptime(service);
  535. /* help predict this next time */
  536. rep_hist_note_used_internal(time(NULL), circ_needs_uptime, 1);
  537. /* Launch a circuit to alice's chosen rendezvous point.
  538. */
  539. for (i=0;i<MAX_REND_FAILURES;i++) {
  540. launched = circuit_launch_by_extend_info(
  541. CIRCUIT_PURPOSE_S_CONNECT_REND, 0, extend_info,
  542. circ_needs_uptime, 1, 1);
  543. if (launched)
  544. break;
  545. }
  546. if (!launched) { /* give up */
  547. log_warn(LD_REND, "Giving up launching first hop of circuit to rendezvous "
  548. "point %s for service %s.",
  549. escaped_safe_str(extend_info->nickname), serviceid);
  550. reason = END_CIRC_REASON_CONNECTFAILED;
  551. goto err;
  552. }
  553. log_info(LD_REND,
  554. "Accepted intro; launching circuit to %s "
  555. "(cookie %s) for service %s.",
  556. escaped_safe_str(extend_info->nickname), hexcookie, serviceid);
  557. tor_assert(launched->build_state);
  558. /* Fill in the circuit's state. */
  559. memcpy(launched->rend_pk_digest, circuit->rend_pk_digest,
  560. DIGEST_LEN);
  561. memcpy(launched->rend_cookie, r_cookie, REND_COOKIE_LEN);
  562. strlcpy(launched->rend_query, service->service_id,
  563. sizeof(launched->rend_query));
  564. launched->build_state->pending_final_cpath = cpath =
  565. tor_malloc_zero(sizeof(crypt_path_t));
  566. cpath->magic = CRYPT_PATH_MAGIC;
  567. launched->build_state->expiry_time = time(NULL) + MAX_REND_TIMEOUT;
  568. cpath->dh_handshake_state = dh;
  569. dh = NULL;
  570. if (circuit_init_cpath_crypto(cpath,keys+DIGEST_LEN,1)<0)
  571. goto err;
  572. memcpy(cpath->handshake_digest, keys, DIGEST_LEN);
  573. if (extend_info) extend_info_free(extend_info);
  574. return 0;
  575. err:
  576. if (dh) crypto_dh_free(dh);
  577. if (launched)
  578. circuit_mark_for_close(TO_CIRCUIT(launched), reason);
  579. if (extend_info) extend_info_free(extend_info);
  580. return -1;
  581. }
  582. /** Called when we fail building a rendezvous circuit at some point other
  583. * than the last hop: launches a new circuit to the same rendezvous point.
  584. */
  585. void
  586. rend_service_relaunch_rendezvous(origin_circuit_t *oldcirc)
  587. {
  588. origin_circuit_t *newcirc;
  589. cpath_build_state_t *newstate, *oldstate;
  590. tor_assert(oldcirc->_base.purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  591. if (!oldcirc->build_state ||
  592. oldcirc->build_state->failure_count > MAX_REND_FAILURES ||
  593. oldcirc->build_state->expiry_time < time(NULL)) {
  594. log_info(LD_REND,
  595. "Attempt to build circuit to %s for rendezvous has failed "
  596. "too many times or expired; giving up.",
  597. oldcirc->build_state ?
  598. oldcirc->build_state->chosen_exit->nickname : "*unknown*");
  599. return;
  600. }
  601. oldstate = oldcirc->build_state;
  602. tor_assert(oldstate);
  603. if (oldstate->pending_final_cpath == NULL) {
  604. log_info(LD_REND,"Skipping relaunch of circ that failed on its first hop. "
  605. "Initiator will retry.");
  606. return;
  607. }
  608. log_info(LD_REND,"Reattempting rendezvous circuit to '%s'",
  609. oldstate->chosen_exit->nickname);
  610. newcirc = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_CONNECT_REND, 0,
  611. oldstate->chosen_exit, 0, 1, 1);
  612. if (!newcirc) {
  613. log_warn(LD_REND,"Couldn't relaunch rendezvous circuit to '%s'.",
  614. oldstate->chosen_exit->nickname);
  615. return;
  616. }
  617. newstate = newcirc->build_state;
  618. tor_assert(newstate);
  619. newstate->failure_count = oldstate->failure_count+1;
  620. newstate->expiry_time = oldstate->expiry_time;
  621. newstate->pending_final_cpath = oldstate->pending_final_cpath;
  622. oldstate->pending_final_cpath = NULL;
  623. memcpy(newcirc->rend_query, oldcirc->rend_query, REND_SERVICE_ID_LEN+1);
  624. memcpy(newcirc->rend_pk_digest, oldcirc->rend_pk_digest,
  625. DIGEST_LEN);
  626. memcpy(newcirc->rend_cookie, oldcirc->rend_cookie,
  627. REND_COOKIE_LEN);
  628. }
  629. /** Launch a circuit to serve as an introduction point for the service
  630. * <b>service</b> at the introduction point <b>nickname</b>
  631. */
  632. static int
  633. rend_service_launch_establish_intro(rend_service_t *service,
  634. const char *nickname)
  635. {
  636. origin_circuit_t *launched;
  637. log_info(LD_REND,
  638. "Launching circuit to introduction point %s for service %s",
  639. escaped_safe_str(nickname), service->service_id);
  640. rep_hist_note_used_internal(time(NULL), 1, 0);
  641. ++service->n_intro_circuits_launched;
  642. launched = circuit_launch_by_nickname(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO, 0,
  643. nickname, 1, 0, 1);
  644. if (!launched) {
  645. log_info(LD_REND,
  646. "Can't launch circuit to establish introduction at %s.",
  647. escaped_safe_str(nickname));
  648. return -1;
  649. }
  650. strlcpy(launched->rend_query, service->service_id,
  651. sizeof(launched->rend_query));
  652. memcpy(launched->rend_pk_digest, service->pk_digest, DIGEST_LEN);
  653. if (launched->_base.state == CIRCUIT_STATE_OPEN)
  654. rend_service_intro_has_opened(launched);
  655. return 0;
  656. }
  657. /** Called when we're done building a circuit to an introduction point:
  658. * sends a RELAY_ESTABLISH_INTRO cell.
  659. */
  660. void
  661. rend_service_intro_has_opened(origin_circuit_t *circuit)
  662. {
  663. rend_service_t *service;
  664. size_t len;
  665. int r;
  666. char buf[RELAY_PAYLOAD_SIZE];
  667. char auth[DIGEST_LEN + 9];
  668. char serviceid[REND_SERVICE_ID_LEN+1];
  669. int reason = END_CIRC_REASON_TORPROTOCOL;
  670. tor_assert(circuit->_base.purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO);
  671. tor_assert(circuit->cpath);
  672. base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
  673. circuit->rend_pk_digest,10);
  674. service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
  675. if (!service) {
  676. log_warn(LD_REND, "Unrecognized service ID %s on introduction circuit %d.",
  677. serviceid, circuit->_base.n_circ_id);
  678. reason = END_CIRC_REASON_NOSUCHSERVICE;
  679. goto err;
  680. }
  681. log_info(LD_REND,
  682. "Established circuit %d as introduction point for service %s",
  683. circuit->_base.n_circ_id, serviceid);
  684. /* Build the payload for a RELAY_ESTABLISH_INTRO cell. */
  685. len = crypto_pk_asn1_encode(service->private_key, buf+2,
  686. RELAY_PAYLOAD_SIZE-2);
  687. set_uint16(buf, htons((uint16_t)len));
  688. len += 2;
  689. memcpy(auth, circuit->cpath->prev->handshake_digest, DIGEST_LEN);
  690. memcpy(auth+DIGEST_LEN, "INTRODUCE", 9);
  691. if (crypto_digest(buf+len, auth, DIGEST_LEN+9))
  692. goto err;
  693. len += 20;
  694. note_crypto_pk_op(REND_SERVER);
  695. r = crypto_pk_private_sign_digest(service->private_key, buf+len, buf, len);
  696. if (r<0) {
  697. log_warn(LD_BUG, "Internal error: couldn't sign introduction request.");
  698. reason = END_CIRC_REASON_INTERNAL;
  699. goto err;
  700. }
  701. len += r;
  702. if (relay_send_command_from_edge(0, TO_CIRCUIT(circuit),
  703. RELAY_COMMAND_ESTABLISH_INTRO,
  704. buf, len, circuit->cpath->prev)<0) {
  705. log_info(LD_GENERAL,
  706. "Couldn't send introduction request for service %s on circuit %d",
  707. serviceid, circuit->_base.n_circ_id);
  708. reason = END_CIRC_REASON_INTERNAL;
  709. goto err;
  710. }
  711. return;
  712. err:
  713. circuit_mark_for_close(TO_CIRCUIT(circuit), reason);
  714. }
  715. /** Called when we get an INTRO_ESTABLISHED cell; mark the circuit as a
  716. * live introduction point, and note that the service descriptor is
  717. * now out-of-date.*/
  718. int
  719. rend_service_intro_established(origin_circuit_t *circuit, const char *request,
  720. size_t request_len)
  721. {
  722. rend_service_t *service;
  723. char serviceid[REND_SERVICE_ID_LEN+1];
  724. (void) request;
  725. (void) request_len;
  726. if (circuit->_base.purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO) {
  727. log_warn(LD_PROTOCOL,
  728. "received INTRO_ESTABLISHED cell on non-intro circuit.");
  729. goto err;
  730. }
  731. service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
  732. if (!service) {
  733. log_warn(LD_REND, "Unknown service on introduction circuit %d.",
  734. circuit->_base.n_circ_id);
  735. goto err;
  736. }
  737. service->desc_is_dirty = time(NULL);
  738. circuit->_base.purpose = CIRCUIT_PURPOSE_S_INTRO;
  739. base32_encode(serviceid, REND_SERVICE_ID_LEN + 1,
  740. circuit->rend_pk_digest, 10);
  741. log_info(LD_REND,
  742. "Received INTRO_ESTABLISHED cell on circuit %d for service %s",
  743. circuit->_base.n_circ_id, serviceid);
  744. return 0;
  745. err:
  746. circuit_mark_for_close(TO_CIRCUIT(circuit), END_CIRC_REASON_TORPROTOCOL);
  747. return -1;
  748. }
  749. /** Called once a circuit to a rendezvous point is established: sends a
  750. * RELAY_COMMAND_RENDEZVOUS1 cell.
  751. */
  752. void
  753. rend_service_rendezvous_has_opened(origin_circuit_t *circuit)
  754. {
  755. rend_service_t *service;
  756. char buf[RELAY_PAYLOAD_SIZE];
  757. crypt_path_t *hop;
  758. char serviceid[REND_SERVICE_ID_LEN+1];
  759. char hexcookie[9];
  760. int reason;
  761. tor_assert(circuit->_base.purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  762. tor_assert(circuit->cpath);
  763. tor_assert(circuit->build_state);
  764. hop = circuit->build_state->pending_final_cpath;
  765. tor_assert(hop);
  766. base16_encode(hexcookie,9,circuit->rend_cookie,4);
  767. base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
  768. circuit->rend_pk_digest,10);
  769. log_info(LD_REND,
  770. "Done building circuit %d to rendezvous with "
  771. "cookie %s for service %s",
  772. circuit->_base.n_circ_id, hexcookie, serviceid);
  773. service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
  774. if (!service) {
  775. log_warn(LD_GENERAL, "Internal error: unrecognized service ID on "
  776. "introduction circuit.");
  777. reason = END_CIRC_REASON_INTERNAL;
  778. goto err;
  779. }
  780. /* All we need to do is send a RELAY_RENDEZVOUS1 cell... */
  781. memcpy(buf, circuit->rend_cookie, REND_COOKIE_LEN);
  782. if (crypto_dh_get_public(hop->dh_handshake_state,
  783. buf+REND_COOKIE_LEN, DH_KEY_LEN)<0) {
  784. log_warn(LD_GENERAL,"Couldn't get DH public key.");
  785. reason = END_CIRC_REASON_INTERNAL;
  786. goto err;
  787. }
  788. memcpy(buf+REND_COOKIE_LEN+DH_KEY_LEN, hop->handshake_digest,
  789. DIGEST_LEN);
  790. /* Send the cell */
  791. if (relay_send_command_from_edge(0, TO_CIRCUIT(circuit),
  792. RELAY_COMMAND_RENDEZVOUS1,
  793. buf, REND_COOKIE_LEN+DH_KEY_LEN+DIGEST_LEN,
  794. circuit->cpath->prev)<0) {
  795. log_warn(LD_GENERAL, "Couldn't send RENDEZVOUS1 cell.");
  796. reason = END_CIRC_REASON_INTERNAL;
  797. goto err;
  798. }
  799. crypto_dh_free(hop->dh_handshake_state);
  800. hop->dh_handshake_state = NULL;
  801. /* Append the cpath entry. */
  802. hop->state = CPATH_STATE_OPEN;
  803. /* set the windows to default. these are the windows
  804. * that bob thinks alice has.
  805. */
  806. hop->package_window = CIRCWINDOW_START;
  807. hop->deliver_window = CIRCWINDOW_START;
  808. onion_append_to_cpath(&circuit->cpath, hop);
  809. circuit->build_state->pending_final_cpath = NULL; /* prevent double-free */
  810. /* Change the circuit purpose. */
  811. circuit->_base.purpose = CIRCUIT_PURPOSE_S_REND_JOINED;
  812. return;
  813. err:
  814. circuit_mark_for_close(TO_CIRCUIT(circuit), reason);
  815. }
  816. /*
  817. * Manage introduction points
  818. */
  819. /** Return the (possibly non-open) introduction circuit ending at
  820. * <b>router</b> for the service whose public key is <b>pk_digest</b>. Return
  821. * NULL if no such service is found.
  822. */
  823. static origin_circuit_t *
  824. find_intro_circuit(routerinfo_t *router, const char *pk_digest)
  825. {
  826. origin_circuit_t *circ = NULL;
  827. tor_assert(router);
  828. while ((circ = circuit_get_next_by_pk_and_purpose(circ,pk_digest,
  829. CIRCUIT_PURPOSE_S_INTRO))) {
  830. if (!strcasecmp(circ->build_state->chosen_exit->nickname,
  831. router->nickname)) {
  832. return circ;
  833. }
  834. }
  835. circ = NULL;
  836. while ((circ = circuit_get_next_by_pk_and_purpose(circ,pk_digest,
  837. CIRCUIT_PURPOSE_S_ESTABLISH_INTRO))) {
  838. if (!strcasecmp(circ->build_state->chosen_exit->nickname,
  839. router->nickname)) {
  840. return circ;
  841. }
  842. }
  843. return NULL;
  844. }
  845. /** Encode and sign an up-to-date service descriptor for <b>service</b>,
  846. * and upload it to all the dirservers.
  847. */
  848. static void
  849. upload_service_descriptor(rend_service_t *service)
  850. {
  851. char *desc;
  852. size_t desc_len;
  853. char serviceid[REND_SERVICE_ID_LEN+1];
  854. /* Update the descriptor. */
  855. rend_service_update_descriptor(service);
  856. if (rend_encode_service_descriptor(service->desc,
  857. service->private_key,
  858. &desc, &desc_len)<0) {
  859. log_warn(LD_BUG, "Internal error: couldn't encode service descriptor; "
  860. "not uploading.");
  861. return;
  862. }
  863. /* Post it to the dirservers */
  864. rend_get_service_id(service->desc->pk, serviceid);
  865. log_info(LD_REND, "Sending publish request for hidden service %s",
  866. serviceid);
  867. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_RENDDESC,
  868. ROUTER_PURPOSE_GENERAL,
  869. HIDSERV_AUTHORITY, desc, desc_len, 0);
  870. tor_free(desc);
  871. service->desc_is_dirty = 0;
  872. }
  873. /** For every service, check how many intro points it currently has, and:
  874. * - Pick new intro points as necessary.
  875. * - Launch circuits to any new intro points.
  876. */
  877. void
  878. rend_services_introduce(void)
  879. {
  880. int i,j,r;
  881. routerinfo_t *router;
  882. rend_service_t *service;
  883. char *intro;
  884. int changed, prev_intro_nodes;
  885. smartlist_t *intro_routers, *exclude_routers;
  886. time_t now;
  887. intro_routers = smartlist_create();
  888. exclude_routers = smartlist_create();
  889. now = time(NULL);
  890. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  891. smartlist_clear(intro_routers);
  892. service = smartlist_get(rend_service_list, i);
  893. tor_assert(service);
  894. changed = 0;
  895. if (now > service->intro_period_started+INTRO_CIRC_RETRY_PERIOD) {
  896. /* One period has elapsed; we can try building circuits again. */
  897. service->intro_period_started = now;
  898. service->n_intro_circuits_launched = 0;
  899. } else if (service->n_intro_circuits_launched >=
  900. MAX_INTRO_CIRCS_PER_PERIOD) {
  901. /* We have failed too many times in this period; wait for the next
  902. * one before we try again. */
  903. continue;
  904. }
  905. /* Find out which introduction points we have in progress for this
  906. service. */
  907. for (j=0; j < smartlist_len(service->intro_nodes); ++j) {
  908. intro = smartlist_get(service->intro_nodes, j);
  909. router = router_get_by_nickname(intro, 0);
  910. if (!router || !find_intro_circuit(router,service->pk_digest)) {
  911. log_info(LD_REND,"Giving up on %s as intro point for %s.",
  912. intro, service->service_id);
  913. tor_free(intro);
  914. smartlist_del(service->intro_nodes,j--);
  915. changed = 1;
  916. service->desc_is_dirty = now;
  917. }
  918. smartlist_add(intro_routers, router);
  919. }
  920. /* We have enough intro points, and the intro points we thought we had were
  921. * all connected.
  922. */
  923. if (!changed && smartlist_len(service->intro_nodes) >= NUM_INTRO_POINTS) {
  924. /* We have all our intro points! Start a fresh period and reset the
  925. * circuit count. */
  926. service->intro_period_started = now;
  927. service->n_intro_circuits_launched = 0;
  928. continue;
  929. }
  930. /* Remember how many introduction circuits we started with. */
  931. prev_intro_nodes = smartlist_len(service->intro_nodes);
  932. smartlist_add_all(exclude_routers, intro_routers);
  933. /* The directory is now here. Pick three ORs as intro points. */
  934. for (j=prev_intro_nodes; j < NUM_INTRO_POINTS; ++j) {
  935. char *hex_digest;
  936. router = router_choose_random_node(service->intro_prefer_nodes,
  937. service->intro_exclude_nodes, exclude_routers, 1, 0, 0,
  938. get_options()->_AllowInvalid & ALLOW_INVALID_INTRODUCTION,
  939. 0, 0);
  940. if (!router) {
  941. log_warn(LD_REND,
  942. "Could only establish %d introduction points for %s.",
  943. smartlist_len(service->intro_nodes), service->service_id);
  944. break;
  945. }
  946. changed = 1;
  947. hex_digest = tor_malloc_zero(HEX_DIGEST_LEN+2);
  948. hex_digest[0] = '$';
  949. base16_encode(hex_digest+1, HEX_DIGEST_LEN+1,
  950. router->cache_info.identity_digest,
  951. DIGEST_LEN);
  952. smartlist_add(intro_routers, router);
  953. smartlist_add(exclude_routers, router);
  954. smartlist_add(service->intro_nodes, hex_digest);
  955. log_info(LD_REND, "Picked router %s as an intro point for %s.",
  956. router->nickname, service->service_id);
  957. }
  958. /* Reset exclude_routers, for the next time around the loop. */
  959. smartlist_clear(exclude_routers);
  960. /* If there's no need to launch new circuits, stop here. */
  961. if (!changed)
  962. continue;
  963. /* Establish new introduction points. */
  964. for (j=prev_intro_nodes; j < smartlist_len(service->intro_nodes); ++j) {
  965. intro = smartlist_get(service->intro_nodes, j);
  966. r = rend_service_launch_establish_intro(service, intro);
  967. if (r<0) {
  968. log_warn(LD_REND, "Error launching circuit to node %s for service %s.",
  969. intro, service->service_id);
  970. }
  971. }
  972. }
  973. smartlist_free(intro_routers);
  974. smartlist_free(exclude_routers);
  975. }
  976. /** Regenerate and upload rendezvous service descriptors for all
  977. * services, if necessary. If the descriptor has been dirty enough
  978. * for long enough, definitely upload; else only upload when the
  979. * periodic timeout has expired.
  980. *
  981. * For the first upload, pick a random time between now and two periods
  982. * from now, and pick it independently for each service.
  983. */
  984. void
  985. rend_consider_services_upload(time_t now)
  986. {
  987. int i;
  988. rend_service_t *service;
  989. int rendpostperiod = get_options()->RendPostPeriod;
  990. if (!get_options()->PublishHidServDescriptors)
  991. return;
  992. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  993. service = smartlist_get(rend_service_list, i);
  994. if (!service->next_upload_time) { /* never been uploaded yet */
  995. service->next_upload_time =
  996. now + crypto_rand_int(2*rendpostperiod);
  997. }
  998. if (service->next_upload_time < now ||
  999. (service->desc_is_dirty &&
  1000. service->desc_is_dirty < now-30)) {
  1001. /* if it's time, or if the directory servers have a wrong service
  1002. * descriptor and ours has been stable for 30 seconds, upload a
  1003. * new one of each format. */
  1004. upload_service_descriptor(service);
  1005. service->next_upload_time = now + rendpostperiod;
  1006. }
  1007. }
  1008. }
  1009. /** Log the status of introduction points for all rendezvous services
  1010. * at log severity <b>severity</b>.
  1011. */
  1012. void
  1013. rend_service_dump_stats(int severity)
  1014. {
  1015. int i,j;
  1016. routerinfo_t *router;
  1017. rend_service_t *service;
  1018. const char *nickname, *safe_name;
  1019. char nn_buf[MAX_VERBOSE_NICKNAME_LEN];
  1020. origin_circuit_t *circ;
  1021. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  1022. service = smartlist_get(rend_service_list, i);
  1023. log(severity, LD_GENERAL, "Service configured in \"%s\":",
  1024. service->directory);
  1025. for (j=0; j < smartlist_len(service->intro_nodes); ++j) {
  1026. nickname = smartlist_get(service->intro_nodes, j);
  1027. router = router_get_by_nickname(nickname,1);
  1028. if (router) {
  1029. router_get_verbose_nickname(nn_buf, router);
  1030. nickname = nn_buf;
  1031. }
  1032. safe_name = safe_str(nickname);
  1033. if (!router) {
  1034. log(severity, LD_GENERAL,
  1035. " Intro point %d at %s: unrecognized router", j, safe_name);
  1036. continue;
  1037. }
  1038. circ = find_intro_circuit(router, service->pk_digest);
  1039. if (!circ) {
  1040. log(severity, LD_GENERAL, " Intro point %d at %s: no circuit",
  1041. j, safe_name);
  1042. continue;
  1043. }
  1044. log(severity, LD_GENERAL, " Intro point %d at %s: circuit is %s",
  1045. j, safe_name, circuit_state_to_string(circ->_base.state));
  1046. }
  1047. }
  1048. }
  1049. /** Given <b>conn</b>, a rendezvous exit stream, look up the hidden service for
  1050. * 'circ', and look up the port and address based on conn-\>port.
  1051. * Assign the actual conn-\>addr and conn-\>port. Return -1 if failure,
  1052. * or 0 for success.
  1053. */
  1054. int
  1055. rend_service_set_connection_addr_port(edge_connection_t *conn,
  1056. origin_circuit_t *circ)
  1057. {
  1058. rend_service_t *service;
  1059. char serviceid[REND_SERVICE_ID_LEN+1];
  1060. smartlist_t *matching_ports;
  1061. rend_service_port_config_t *chosen_port;
  1062. tor_assert(circ->_base.purpose == CIRCUIT_PURPOSE_S_REND_JOINED);
  1063. log_debug(LD_REND,"beginning to hunt for addr/port");
  1064. base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
  1065. circ->rend_pk_digest,10);
  1066. service = rend_service_get_by_pk_digest(circ->rend_pk_digest);
  1067. if (!service) {
  1068. log_warn(LD_REND, "Couldn't find any service associated with pk %s on "
  1069. "rendezvous circuit %d; closing.",
  1070. serviceid, circ->_base.n_circ_id);
  1071. return -1;
  1072. }
  1073. matching_ports = smartlist_create();
  1074. SMARTLIST_FOREACH(service->ports, rend_service_port_config_t *, p,
  1075. {
  1076. if (conn->_base.port == p->virtual_port) {
  1077. smartlist_add(matching_ports, p);
  1078. }
  1079. });
  1080. chosen_port = smartlist_choose(matching_ports);
  1081. smartlist_free(matching_ports);
  1082. if (chosen_port) {
  1083. conn->_base.addr = chosen_port->real_addr;
  1084. conn->_base.port = chosen_port->real_port;
  1085. return 0;
  1086. }
  1087. log_info(LD_REND, "No virtual port mapping exists for port %d on service %s",
  1088. conn->_base.port,serviceid);
  1089. return -1;
  1090. }