rendservice.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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. d->protocols = (1<<2) | (1<<0); /* We support protocol 2 and protocol 0. */
  280. for (i=0; i < n; ++i) {
  281. router = router_get_by_nickname(smartlist_get(service->intro_nodes, i),1);
  282. if (!router) {
  283. log_info(LD_REND,"Router '%s' not found for intro point %d. Skipping.",
  284. safe_str((char*)smartlist_get(service->intro_nodes, i)), i);
  285. continue;
  286. }
  287. circ = find_intro_circuit(router, service->pk_digest);
  288. if (circ && circ->_base.purpose == CIRCUIT_PURPOSE_S_INTRO) {
  289. /* We have an entirely established intro circuit. */
  290. d->intro_points[d->n_intro_points] = tor_strdup(router->nickname);
  291. d->intro_point_extend_info[d->n_intro_points] =
  292. extend_info_from_router(router);
  293. d->n_intro_points++;
  294. }
  295. }
  296. }
  297. /** Load and/or generate private keys for all hidden services. Return 0 on
  298. * success, -1 on failure.
  299. */
  300. int
  301. rend_service_load_keys(void)
  302. {
  303. int i;
  304. rend_service_t *s;
  305. char fname[512];
  306. char buf[128];
  307. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  308. s = smartlist_get(rend_service_list,i);
  309. if (s->private_key)
  310. continue;
  311. log_info(LD_REND, "Loading hidden-service keys from \"%s\"",
  312. s->directory);
  313. /* Check/create directory */
  314. if (check_private_dir(s->directory, CPD_CREATE) < 0)
  315. return -1;
  316. /* Load key */
  317. if (strlcpy(fname,s->directory,sizeof(fname)) >= sizeof(fname) ||
  318. strlcat(fname,PATH_SEPARATOR"private_key",sizeof(fname))
  319. >= sizeof(fname)) {
  320. log_warn(LD_CONFIG, "Directory name too long to store key file: \"%s\".",
  321. s->directory);
  322. return -1;
  323. }
  324. s->private_key = init_key_from_file(fname, 1, LOG_ERR);
  325. if (!s->private_key)
  326. return -1;
  327. /* Create service file */
  328. if (rend_get_service_id(s->private_key, s->service_id)<0) {
  329. log_warn(LD_BUG, "Internal error: couldn't encode service ID.");
  330. return -1;
  331. }
  332. if (crypto_pk_get_digest(s->private_key, s->pk_digest)<0) {
  333. log_warn(LD_BUG, "Couldn't compute hash of public key.");
  334. return -1;
  335. }
  336. if (strlcpy(fname,s->directory,sizeof(fname)) >= sizeof(fname) ||
  337. strlcat(fname,PATH_SEPARATOR"hostname",sizeof(fname))
  338. >= sizeof(fname)) {
  339. log_warn(LD_CONFIG, "Directory name too long to store hostname file:"
  340. " \"%s\".", s->directory);
  341. return -1;
  342. }
  343. tor_snprintf(buf, sizeof(buf),"%s.onion\n", s->service_id);
  344. if (write_str_to_file(fname,buf,0)<0)
  345. return -1;
  346. }
  347. return 0;
  348. }
  349. /** Return the service whose public key has a digest of <b>digest</b>. Return
  350. * NULL if no such service exists.
  351. */
  352. static rend_service_t *
  353. rend_service_get_by_pk_digest(const char* digest)
  354. {
  355. SMARTLIST_FOREACH(rend_service_list, rend_service_t*, s,
  356. if (!memcmp(s->pk_digest,digest,DIGEST_LEN)) return s);
  357. return NULL;
  358. }
  359. /** Return 1 if any virtual port in <b>service</b> wants a circuit
  360. * to have good uptime. Else return 0.
  361. */
  362. static int
  363. rend_service_requires_uptime(rend_service_t *service)
  364. {
  365. int i;
  366. rend_service_port_config_t *p;
  367. for (i=0; i < smartlist_len(service->ports); ++i) {
  368. p = smartlist_get(service->ports, i);
  369. if (smartlist_string_num_isin(get_options()->LongLivedPorts,
  370. p->virtual_port))
  371. return 1;
  372. }
  373. return 0;
  374. }
  375. /******
  376. * Handle cells
  377. ******/
  378. /** Respond to an INTRODUCE2 cell by launching a circuit to the chosen
  379. * rendezvous point.
  380. */
  381. int
  382. rend_service_introduce(origin_circuit_t *circuit, const char *request,
  383. size_t request_len)
  384. {
  385. char *ptr, *r_cookie;
  386. extend_info_t *extend_info = NULL;
  387. char buf[RELAY_PAYLOAD_SIZE];
  388. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN]; /* Holds KH, Df, Db, Kf, Kb */
  389. rend_service_t *service;
  390. int r, i;
  391. size_t len, keylen;
  392. crypto_dh_env_t *dh = NULL;
  393. origin_circuit_t *launched = NULL;
  394. crypt_path_t *cpath = NULL;
  395. char serviceid[REND_SERVICE_ID_LEN+1];
  396. char hexcookie[9];
  397. int circ_needs_uptime;
  398. int reason = END_CIRC_REASON_TORPROTOCOL;
  399. base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
  400. circuit->rend_pk_digest,10);
  401. log_info(LD_REND, "Received INTRODUCE2 cell for service %s on circ %d.",
  402. escaped(serviceid), circuit->_base.n_circ_id);
  403. if (circuit->_base.purpose != CIRCUIT_PURPOSE_S_INTRO) {
  404. log_warn(LD_PROTOCOL,
  405. "Got an INTRODUCE2 over a non-introduction circuit %d.",
  406. circuit->_base.n_circ_id);
  407. return -1;
  408. }
  409. /* min key length plus digest length plus nickname length */
  410. if (request_len < DIGEST_LEN+REND_COOKIE_LEN+(MAX_NICKNAME_LEN+1)+
  411. DH_KEY_LEN+42) {
  412. log_warn(LD_PROTOCOL, "Got a truncated INTRODUCE2 cell on circ %d.",
  413. circuit->_base.n_circ_id);
  414. return -1;
  415. }
  416. /* first DIGEST_LEN bytes of request is service pk digest */
  417. service = rend_service_get_by_pk_digest(request);
  418. if (!service) {
  419. log_warn(LD_REND, "Got an INTRODUCE2 cell for an unrecognized service %s.",
  420. escaped(serviceid));
  421. return -1;
  422. }
  423. if (memcmp(circuit->rend_pk_digest, request, DIGEST_LEN)) {
  424. base32_encode(serviceid, REND_SERVICE_ID_LEN+1, request, 10);
  425. log_warn(LD_REND, "Got an INTRODUCE2 cell for the wrong service (%s).",
  426. escaped(serviceid));
  427. return -1;
  428. }
  429. keylen = crypto_pk_keysize(service->private_key);
  430. if (request_len < keylen+DIGEST_LEN) {
  431. log_warn(LD_PROTOCOL,
  432. "PK-encrypted portion of INTRODUCE2 cell was truncated.");
  433. return -1;
  434. }
  435. /* Next N bytes is encrypted with service key */
  436. note_crypto_pk_op(REND_SERVER);
  437. r = crypto_pk_private_hybrid_decrypt(
  438. service->private_key,buf,request+DIGEST_LEN,request_len-DIGEST_LEN,
  439. PK_PKCS1_OAEP_PADDING,1);
  440. if (r<0) {
  441. log_warn(LD_PROTOCOL, "Couldn't decrypt INTRODUCE2 cell.");
  442. return -1;
  443. }
  444. len = r;
  445. if (*buf == 2) {
  446. /* Version 2 INTRODUCE2 cell. */
  447. int klen;
  448. extend_info = tor_malloc_zero(sizeof(extend_info_t));
  449. extend_info->addr = ntohl(get_uint32(buf+1));
  450. extend_info->port = ntohs(get_uint16(buf+5));
  451. memcpy(extend_info->identity_digest, buf+7, DIGEST_LEN);
  452. extend_info->nickname[0] = '$';
  453. base16_encode(extend_info->nickname+1, sizeof(extend_info->nickname)-1,
  454. extend_info->identity_digest, DIGEST_LEN);
  455. klen = ntohs(get_uint16(buf+7+DIGEST_LEN));
  456. if ((int)len != 7+DIGEST_LEN+2+klen+20+128) {
  457. log_warn(LD_PROTOCOL, "Bad length %u for version 2 INTRODUCE2 cell.",
  458. (int)len);
  459. reason = END_CIRC_REASON_TORPROTOCOL;
  460. goto err;
  461. }
  462. extend_info->onion_key = crypto_pk_asn1_decode(buf+7+DIGEST_LEN+2, klen);
  463. if (!extend_info->onion_key) {
  464. log_warn(LD_PROTOCOL,
  465. "Error decoding onion key in version 2 INTRODUCE2 cell.");
  466. reason = END_CIRC_REASON_TORPROTOCOL;
  467. goto err;
  468. }
  469. ptr = buf+7+DIGEST_LEN+2+klen;
  470. len -= 7+DIGEST_LEN+2+klen;
  471. } else {
  472. char *rp_nickname;
  473. size_t nickname_field_len;
  474. routerinfo_t *router;
  475. int version;
  476. if (*buf == 1) {
  477. rp_nickname = buf+1;
  478. nickname_field_len = MAX_HEX_NICKNAME_LEN+1;
  479. version = 1;
  480. } else {
  481. nickname_field_len = MAX_NICKNAME_LEN+1;
  482. rp_nickname = buf;
  483. version = 0;
  484. }
  485. ptr=memchr(rp_nickname,0,nickname_field_len);
  486. if (!ptr || ptr == rp_nickname) {
  487. log_warn(LD_PROTOCOL,
  488. "Couldn't find a nul-padded nickname in INTRODUCE2 cell.");
  489. return -1;
  490. }
  491. if ((version == 0 && !is_legal_nickname(rp_nickname)) ||
  492. (version == 1 && !is_legal_nickname_or_hexdigest(rp_nickname))) {
  493. log_warn(LD_PROTOCOL, "Bad nickname in INTRODUCE2 cell.");
  494. return -1;
  495. }
  496. /* Okay, now we know that a nickname is at the start of the buffer. */
  497. ptr = rp_nickname+nickname_field_len;
  498. len -= nickname_field_len;
  499. len -= rp_nickname - buf; /* also remove header space used by version, if
  500. * any */
  501. router = router_get_by_nickname(rp_nickname, 0);
  502. if (!router) {
  503. log_info(LD_REND, "Couldn't find router %s named in rendezvous cell.",
  504. escaped_safe_str(rp_nickname));
  505. /* XXXX Add a no-such-router reason? */
  506. reason = END_CIRC_REASON_TORPROTOCOL;
  507. goto err;
  508. }
  509. extend_info = extend_info_from_router(router);
  510. }
  511. if (len != REND_COOKIE_LEN+DH_KEY_LEN) {
  512. log_warn(LD_PROTOCOL, "Bad length %u for INTRODUCE2 cell.", (int)len);
  513. reason = END_CIRC_REASON_TORPROTOCOL;
  514. return -1;
  515. }
  516. r_cookie = ptr;
  517. base16_encode(hexcookie,9,r_cookie,4);
  518. /* Try DH handshake... */
  519. dh = crypto_dh_new();
  520. if (!dh || crypto_dh_generate_public(dh)<0) {
  521. log_warn(LD_BUG,"Internal error: couldn't build DH state "
  522. "or generate public key.");
  523. reason = END_CIRC_REASON_INTERNAL;
  524. goto err;
  525. }
  526. if (crypto_dh_compute_secret(dh, ptr+REND_COOKIE_LEN, DH_KEY_LEN, keys,
  527. DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  528. log_warn(LD_BUG, "Internal error: couldn't complete DH handshake");
  529. reason = END_CIRC_REASON_INTERNAL;
  530. goto err;
  531. }
  532. circ_needs_uptime = rend_service_requires_uptime(service);
  533. /* help predict this next time */
  534. rep_hist_note_used_internal(time(NULL), circ_needs_uptime, 1);
  535. /* Launch a circuit to alice's chosen rendezvous point.
  536. */
  537. for (i=0;i<MAX_REND_FAILURES;i++) {
  538. launched = circuit_launch_by_extend_info(
  539. CIRCUIT_PURPOSE_S_CONNECT_REND, 0, extend_info,
  540. circ_needs_uptime, 1, 1);
  541. if (launched)
  542. break;
  543. }
  544. if (!launched) { /* give up */
  545. log_warn(LD_REND, "Giving up launching first hop of circuit to rendezvous "
  546. "point %s for service %s.",
  547. escaped_safe_str(extend_info->nickname), serviceid);
  548. reason = END_CIRC_REASON_CONNECTFAILED;
  549. goto err;
  550. }
  551. log_info(LD_REND,
  552. "Accepted intro; launching circuit to %s "
  553. "(cookie %s) for service %s.",
  554. escaped_safe_str(extend_info->nickname), hexcookie, serviceid);
  555. tor_assert(launched->build_state);
  556. /* Fill in the circuit's state. */
  557. memcpy(launched->rend_pk_digest, circuit->rend_pk_digest,
  558. DIGEST_LEN);
  559. memcpy(launched->rend_cookie, r_cookie, REND_COOKIE_LEN);
  560. strlcpy(launched->rend_query, service->service_id,
  561. sizeof(launched->rend_query));
  562. launched->build_state->pending_final_cpath = cpath =
  563. tor_malloc_zero(sizeof(crypt_path_t));
  564. cpath->magic = CRYPT_PATH_MAGIC;
  565. launched->build_state->expiry_time = time(NULL) + MAX_REND_TIMEOUT;
  566. cpath->dh_handshake_state = dh;
  567. dh = NULL;
  568. if (circuit_init_cpath_crypto(cpath,keys+DIGEST_LEN,1)<0)
  569. goto err;
  570. memcpy(cpath->handshake_digest, keys, DIGEST_LEN);
  571. if (extend_info) extend_info_free(extend_info);
  572. return 0;
  573. err:
  574. if (dh) crypto_dh_free(dh);
  575. if (launched)
  576. circuit_mark_for_close(TO_CIRCUIT(launched), reason);
  577. if (extend_info) extend_info_free(extend_info);
  578. return -1;
  579. }
  580. /** Called when we fail building a rendezvous circuit at some point other
  581. * than the last hop: launches a new circuit to the same rendezvous point.
  582. */
  583. void
  584. rend_service_relaunch_rendezvous(origin_circuit_t *oldcirc)
  585. {
  586. origin_circuit_t *newcirc;
  587. cpath_build_state_t *newstate, *oldstate;
  588. tor_assert(oldcirc->_base.purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  589. if (!oldcirc->build_state ||
  590. oldcirc->build_state->failure_count > MAX_REND_FAILURES ||
  591. oldcirc->build_state->expiry_time < time(NULL)) {
  592. log_info(LD_REND,
  593. "Attempt to build circuit to %s for rendezvous has failed "
  594. "too many times or expired; giving up.",
  595. oldcirc->build_state ?
  596. oldcirc->build_state->chosen_exit->nickname : "*unknown*");
  597. return;
  598. }
  599. oldstate = oldcirc->build_state;
  600. tor_assert(oldstate);
  601. if (oldstate->pending_final_cpath == NULL) {
  602. log_info(LD_REND,"Skipping relaunch of circ that failed on its first hop. "
  603. "Initiator will retry.");
  604. return;
  605. }
  606. log_info(LD_REND,"Reattempting rendezvous circuit to '%s'",
  607. oldstate->chosen_exit->nickname);
  608. newcirc = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_CONNECT_REND, 0,
  609. oldstate->chosen_exit, 0, 1, 1);
  610. if (!newcirc) {
  611. log_warn(LD_REND,"Couldn't relaunch rendezvous circuit to '%s'.",
  612. oldstate->chosen_exit->nickname);
  613. return;
  614. }
  615. newstate = newcirc->build_state;
  616. tor_assert(newstate);
  617. newstate->failure_count = oldstate->failure_count+1;
  618. newstate->expiry_time = oldstate->expiry_time;
  619. newstate->pending_final_cpath = oldstate->pending_final_cpath;
  620. oldstate->pending_final_cpath = NULL;
  621. memcpy(newcirc->rend_query, oldcirc->rend_query, REND_SERVICE_ID_LEN+1);
  622. memcpy(newcirc->rend_pk_digest, oldcirc->rend_pk_digest,
  623. DIGEST_LEN);
  624. memcpy(newcirc->rend_cookie, oldcirc->rend_cookie,
  625. REND_COOKIE_LEN);
  626. }
  627. /** Launch a circuit to serve as an introduction point for the service
  628. * <b>service</b> at the introduction point <b>nickname</b>
  629. */
  630. static int
  631. rend_service_launch_establish_intro(rend_service_t *service,
  632. const char *nickname)
  633. {
  634. origin_circuit_t *launched;
  635. log_info(LD_REND,
  636. "Launching circuit to introduction point %s for service %s",
  637. escaped_safe_str(nickname), service->service_id);
  638. rep_hist_note_used_internal(time(NULL), 1, 0);
  639. ++service->n_intro_circuits_launched;
  640. launched = circuit_launch_by_nickname(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO, 0,
  641. nickname, 1, 0, 1);
  642. if (!launched) {
  643. log_info(LD_REND,
  644. "Can't launch circuit to establish introduction at %s.",
  645. escaped_safe_str(nickname));
  646. return -1;
  647. }
  648. strlcpy(launched->rend_query, service->service_id,
  649. sizeof(launched->rend_query));
  650. memcpy(launched->rend_pk_digest, service->pk_digest, DIGEST_LEN);
  651. if (launched->_base.state == CIRCUIT_STATE_OPEN)
  652. rend_service_intro_has_opened(launched);
  653. return 0;
  654. }
  655. /** Called when we're done building a circuit to an introduction point:
  656. * sends a RELAY_ESTABLISH_INTRO cell.
  657. */
  658. void
  659. rend_service_intro_has_opened(origin_circuit_t *circuit)
  660. {
  661. rend_service_t *service;
  662. size_t len;
  663. int r;
  664. char buf[RELAY_PAYLOAD_SIZE];
  665. char auth[DIGEST_LEN + 9];
  666. char serviceid[REND_SERVICE_ID_LEN+1];
  667. int reason = END_CIRC_REASON_TORPROTOCOL;
  668. tor_assert(circuit->_base.purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO);
  669. tor_assert(circuit->cpath);
  670. base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
  671. circuit->rend_pk_digest,10);
  672. service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
  673. if (!service) {
  674. log_warn(LD_REND, "Unrecognized service ID %s on introduction circuit %d.",
  675. serviceid, circuit->_base.n_circ_id);
  676. reason = END_CIRC_REASON_NOSUCHSERVICE;
  677. goto err;
  678. }
  679. log_info(LD_REND,
  680. "Established circuit %d as introduction point for service %s",
  681. circuit->_base.n_circ_id, serviceid);
  682. /* Build the payload for a RELAY_ESTABLISH_INTRO cell. */
  683. len = crypto_pk_asn1_encode(service->private_key, buf+2,
  684. RELAY_PAYLOAD_SIZE-2);
  685. set_uint16(buf, htons((uint16_t)len));
  686. len += 2;
  687. memcpy(auth, circuit->cpath->prev->handshake_digest, DIGEST_LEN);
  688. memcpy(auth+DIGEST_LEN, "INTRODUCE", 9);
  689. if (crypto_digest(buf+len, auth, DIGEST_LEN+9))
  690. goto err;
  691. len += 20;
  692. note_crypto_pk_op(REND_SERVER);
  693. r = crypto_pk_private_sign_digest(service->private_key, buf+len, buf, len);
  694. if (r<0) {
  695. log_warn(LD_BUG, "Internal error: couldn't sign introduction request.");
  696. reason = END_CIRC_REASON_INTERNAL;
  697. goto err;
  698. }
  699. len += r;
  700. if (relay_send_command_from_edge(0, TO_CIRCUIT(circuit),
  701. RELAY_COMMAND_ESTABLISH_INTRO,
  702. buf, len, circuit->cpath->prev)<0) {
  703. log_info(LD_GENERAL,
  704. "Couldn't send introduction request for service %s on circuit %d",
  705. serviceid, circuit->_base.n_circ_id);
  706. reason = END_CIRC_REASON_INTERNAL;
  707. goto err;
  708. }
  709. return;
  710. err:
  711. circuit_mark_for_close(TO_CIRCUIT(circuit), reason);
  712. }
  713. /** Called when we get an INTRO_ESTABLISHED cell; mark the circuit as a
  714. * live introduction point, and note that the service descriptor is
  715. * now out-of-date.*/
  716. int
  717. rend_service_intro_established(origin_circuit_t *circuit, const char *request,
  718. size_t request_len)
  719. {
  720. rend_service_t *service;
  721. (void) request;
  722. (void) request_len;
  723. if (circuit->_base.purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO) {
  724. log_warn(LD_PROTOCOL,
  725. "received INTRO_ESTABLISHED cell on non-intro circuit.");
  726. goto err;
  727. }
  728. service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
  729. if (!service) {
  730. log_warn(LD_REND, "Unknown service on introduction circuit %d.",
  731. circuit->_base.n_circ_id);
  732. goto err;
  733. }
  734. service->desc_is_dirty = time(NULL);
  735. circuit->_base.purpose = CIRCUIT_PURPOSE_S_INTRO;
  736. return 0;
  737. err:
  738. circuit_mark_for_close(TO_CIRCUIT(circuit), END_CIRC_REASON_TORPROTOCOL);
  739. return -1;
  740. }
  741. /** Called once a circuit to a rendezvous point is established: sends a
  742. * RELAY_COMMAND_RENDEZVOUS1 cell.
  743. */
  744. void
  745. rend_service_rendezvous_has_opened(origin_circuit_t *circuit)
  746. {
  747. rend_service_t *service;
  748. char buf[RELAY_PAYLOAD_SIZE];
  749. crypt_path_t *hop;
  750. char serviceid[REND_SERVICE_ID_LEN+1];
  751. char hexcookie[9];
  752. int reason;
  753. tor_assert(circuit->_base.purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  754. tor_assert(circuit->cpath);
  755. tor_assert(circuit->build_state);
  756. hop = circuit->build_state->pending_final_cpath;
  757. tor_assert(hop);
  758. base16_encode(hexcookie,9,circuit->rend_cookie,4);
  759. base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
  760. circuit->rend_pk_digest,10);
  761. log_info(LD_REND,
  762. "Done building circuit %d to rendezvous with "
  763. "cookie %s for service %s",
  764. circuit->_base.n_circ_id, hexcookie, serviceid);
  765. service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
  766. if (!service) {
  767. log_warn(LD_GENERAL, "Internal error: unrecognized service ID on "
  768. "introduction circuit.");
  769. reason = END_CIRC_REASON_INTERNAL;
  770. goto err;
  771. }
  772. /* All we need to do is send a RELAY_RENDEZVOUS1 cell... */
  773. memcpy(buf, circuit->rend_cookie, REND_COOKIE_LEN);
  774. if (crypto_dh_get_public(hop->dh_handshake_state,
  775. buf+REND_COOKIE_LEN, DH_KEY_LEN)<0) {
  776. log_warn(LD_GENERAL,"Couldn't get DH public key.");
  777. reason = END_CIRC_REASON_INTERNAL;
  778. goto err;
  779. }
  780. memcpy(buf+REND_COOKIE_LEN+DH_KEY_LEN, hop->handshake_digest,
  781. DIGEST_LEN);
  782. /* Send the cell */
  783. if (relay_send_command_from_edge(0, TO_CIRCUIT(circuit),
  784. RELAY_COMMAND_RENDEZVOUS1,
  785. buf, REND_COOKIE_LEN+DH_KEY_LEN+DIGEST_LEN,
  786. circuit->cpath->prev)<0) {
  787. log_warn(LD_GENERAL, "Couldn't send RENDEZVOUS1 cell.");
  788. reason = END_CIRC_REASON_INTERNAL;
  789. goto err;
  790. }
  791. crypto_dh_free(hop->dh_handshake_state);
  792. hop->dh_handshake_state = NULL;
  793. /* Append the cpath entry. */
  794. hop->state = CPATH_STATE_OPEN;
  795. /* set the windows to default. these are the windows
  796. * that bob thinks alice has.
  797. */
  798. hop->package_window = CIRCWINDOW_START;
  799. hop->deliver_window = CIRCWINDOW_START;
  800. onion_append_to_cpath(&circuit->cpath, hop);
  801. circuit->build_state->pending_final_cpath = NULL; /* prevent double-free */
  802. /* Change the circuit purpose. */
  803. circuit->_base.purpose = CIRCUIT_PURPOSE_S_REND_JOINED;
  804. return;
  805. err:
  806. circuit_mark_for_close(TO_CIRCUIT(circuit), reason);
  807. }
  808. /*
  809. * Manage introduction points
  810. */
  811. /** Return the (possibly non-open) introduction circuit ending at
  812. * <b>router</b> for the service whose public key is <b>pk_digest</b>. Return
  813. * NULL if no such service is found.
  814. */
  815. static origin_circuit_t *
  816. find_intro_circuit(routerinfo_t *router, const char *pk_digest)
  817. {
  818. origin_circuit_t *circ = NULL;
  819. tor_assert(router);
  820. while ((circ = circuit_get_next_by_pk_and_purpose(circ,pk_digest,
  821. CIRCUIT_PURPOSE_S_INTRO))) {
  822. if (!strcasecmp(circ->build_state->chosen_exit->nickname,
  823. router->nickname)) {
  824. return circ;
  825. }
  826. }
  827. circ = NULL;
  828. while ((circ = circuit_get_next_by_pk_and_purpose(circ,pk_digest,
  829. CIRCUIT_PURPOSE_S_ESTABLISH_INTRO))) {
  830. if (!strcasecmp(circ->build_state->chosen_exit->nickname,
  831. router->nickname)) {
  832. return circ;
  833. }
  834. }
  835. return NULL;
  836. }
  837. /** Encode and sign an up-to-date service descriptor for <b>service</b>,
  838. * and upload it to all the dirservers.
  839. */
  840. static void
  841. upload_service_descriptor(rend_service_t *service, int version)
  842. {
  843. char *desc;
  844. size_t desc_len;
  845. /* Update the descriptor. */
  846. rend_service_update_descriptor(service);
  847. if (rend_encode_service_descriptor(service->desc,
  848. version,
  849. service->private_key,
  850. &desc, &desc_len)<0) {
  851. log_warn(LD_BUG, "Internal error: couldn't encode service descriptor; "
  852. "not uploading.");
  853. return;
  854. }
  855. /* Post it to the dirservers */
  856. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_RENDDESC,
  857. HIDSERV_AUTHORITY, desc, desc_len, 0);
  858. tor_free(desc);
  859. service->desc_is_dirty = 0;
  860. }
  861. /** For every service, check how many intro points it currently has, and:
  862. * - Pick new intro points as necessary.
  863. * - Launch circuits to any new intro points.
  864. */
  865. void
  866. rend_services_introduce(void)
  867. {
  868. int i,j,r;
  869. routerinfo_t *router;
  870. rend_service_t *service;
  871. char *intro;
  872. int changed, prev_intro_nodes;
  873. smartlist_t *intro_routers, *exclude_routers;
  874. time_t now;
  875. intro_routers = smartlist_create();
  876. exclude_routers = smartlist_create();
  877. now = time(NULL);
  878. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  879. smartlist_clear(intro_routers);
  880. service = smartlist_get(rend_service_list, i);
  881. tor_assert(service);
  882. changed = 0;
  883. if (now > service->intro_period_started+INTRO_CIRC_RETRY_PERIOD) {
  884. /* One period has elapsed; we can try building circuits again. */
  885. service->intro_period_started = now;
  886. service->n_intro_circuits_launched = 0;
  887. } else if (service->n_intro_circuits_launched >=
  888. MAX_INTRO_CIRCS_PER_PERIOD) {
  889. /* We have failed too many times in this period; wait for the next
  890. * one before we try again. */
  891. continue;
  892. }
  893. /* Find out which introduction points we have in progress for this
  894. service. */
  895. for (j=0; j < smartlist_len(service->intro_nodes); ++j) {
  896. intro = smartlist_get(service->intro_nodes, j);
  897. router = router_get_by_nickname(intro, 0);
  898. if (!router || !find_intro_circuit(router,service->pk_digest)) {
  899. log_info(LD_REND,"Giving up on %s as intro point for %s.",
  900. intro, service->service_id);
  901. tor_free(intro);
  902. smartlist_del(service->intro_nodes,j--);
  903. changed = 1;
  904. service->desc_is_dirty = now;
  905. }
  906. smartlist_add(intro_routers, router);
  907. }
  908. /* We have enough intro points, and the intro points we thought we had were
  909. * all connected.
  910. */
  911. if (!changed && smartlist_len(service->intro_nodes) >= NUM_INTRO_POINTS) {
  912. /* We have all our intro points! Start a fresh period and reset the
  913. * circuit count. */
  914. service->intro_period_started = now;
  915. service->n_intro_circuits_launched = 0;
  916. continue;
  917. }
  918. /* Remember how many introduction circuits we started with. */
  919. prev_intro_nodes = smartlist_len(service->intro_nodes);
  920. smartlist_add_all(exclude_routers, intro_routers);
  921. /* The directory is now here. Pick three ORs as intro points. */
  922. for (j=prev_intro_nodes; j < NUM_INTRO_POINTS; ++j) {
  923. char *hex_digest;
  924. router = router_choose_random_node(service->intro_prefer_nodes,
  925. service->intro_exclude_nodes, exclude_routers, 1, 0, 0,
  926. get_options()->_AllowInvalid & ALLOW_INVALID_INTRODUCTION,
  927. 0, 0);
  928. if (!router) {
  929. log_warn(LD_REND,
  930. "Could only establish %d introduction points for %s.",
  931. smartlist_len(service->intro_nodes), service->service_id);
  932. break;
  933. }
  934. changed = 1;
  935. hex_digest = tor_malloc_zero(HEX_DIGEST_LEN+2);
  936. hex_digest[0] = '$';
  937. base16_encode(hex_digest+1, HEX_DIGEST_LEN+1,
  938. router->cache_info.identity_digest,
  939. DIGEST_LEN);
  940. smartlist_add(intro_routers, router);
  941. smartlist_add(exclude_routers, router);
  942. smartlist_add(service->intro_nodes, hex_digest);
  943. log_info(LD_REND, "Picked router %s as an intro point for %s.",
  944. router->nickname, service->service_id);
  945. }
  946. /* Reset exclude_routers, for the next time around the loop. */
  947. smartlist_clear(exclude_routers);
  948. /* If there's no need to launch new circuits, stop here. */
  949. if (!changed)
  950. continue;
  951. /* Establish new introduction points. */
  952. for (j=prev_intro_nodes; j < smartlist_len(service->intro_nodes); ++j) {
  953. intro = smartlist_get(service->intro_nodes, j);
  954. r = rend_service_launch_establish_intro(service, intro);
  955. if (r<0) {
  956. log_warn(LD_REND, "Error launching circuit to node %s for service %s.",
  957. intro, service->service_id);
  958. }
  959. }
  960. }
  961. smartlist_free(intro_routers);
  962. smartlist_free(exclude_routers);
  963. }
  964. /** Regenerate and upload rendezvous service descriptors for all
  965. * services, if necessary. If the descriptor has been dirty enough
  966. * for long enough, definitely upload; else only upload when the
  967. * periodic timeout has expired.
  968. *
  969. * For the first upload, pick a random time between now and two periods
  970. * from now, and pick it independently for each service.
  971. */
  972. void
  973. rend_consider_services_upload(time_t now)
  974. {
  975. int i;
  976. rend_service_t *service;
  977. int rendpostperiod = get_options()->RendPostPeriod;
  978. if (!get_options()->PublishHidServDescriptors)
  979. return;
  980. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  981. service = smartlist_get(rend_service_list, i);
  982. if (!service->next_upload_time) { /* never been uploaded yet */
  983. service->next_upload_time =
  984. now + crypto_rand_int(2*rendpostperiod);
  985. }
  986. if (service->next_upload_time < now ||
  987. (service->desc_is_dirty &&
  988. service->desc_is_dirty < now-30)) {
  989. /* if it's time, or if the directory servers have a wrong service
  990. * descriptor and ours has been stable for 30 seconds, upload a
  991. * new one of each format. */
  992. upload_service_descriptor(service, 0);
  993. service->next_upload_time = now + rendpostperiod;
  994. }
  995. }
  996. }
  997. /** Log the status of introduction points for all rendezvous services
  998. * at log severity <b>severity</b>.
  999. */
  1000. void
  1001. rend_service_dump_stats(int severity)
  1002. {
  1003. int i,j;
  1004. routerinfo_t *router;
  1005. rend_service_t *service;
  1006. const char *nickname, *safe_name;
  1007. char nn_buf[MAX_VERBOSE_NICKNAME_LEN];
  1008. origin_circuit_t *circ;
  1009. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  1010. service = smartlist_get(rend_service_list, i);
  1011. log(severity, LD_GENERAL, "Service configured in \"%s\":",
  1012. service->directory);
  1013. for (j=0; j < smartlist_len(service->intro_nodes); ++j) {
  1014. nickname = smartlist_get(service->intro_nodes, j);
  1015. router = router_get_by_nickname(nickname,1);
  1016. if (router) {
  1017. router_get_verbose_nickname(nn_buf, router);
  1018. nickname = nn_buf;
  1019. }
  1020. safe_name = safe_str(nickname);
  1021. if (!router) {
  1022. log(severity, LD_GENERAL,
  1023. " Intro point %d at %s: unrecognized router", j, safe_name);
  1024. continue;
  1025. }
  1026. circ = find_intro_circuit(router, service->pk_digest);
  1027. if (!circ) {
  1028. log(severity, LD_GENERAL, " Intro point %d at %s: no circuit",
  1029. j, safe_name);
  1030. continue;
  1031. }
  1032. log(severity, LD_GENERAL, " Intro point %d at %s: circuit is %s",
  1033. j, safe_name, circuit_state_to_string(circ->_base.state));
  1034. }
  1035. }
  1036. }
  1037. /** Given <b>conn</b>, a rendezvous exit stream, look up the hidden service for
  1038. * 'circ', and look up the port and address based on conn-\>port.
  1039. * Assign the actual conn-\>addr and conn-\>port. Return -1 if failure,
  1040. * or 0 for success.
  1041. */
  1042. int
  1043. rend_service_set_connection_addr_port(edge_connection_t *conn,
  1044. origin_circuit_t *circ)
  1045. {
  1046. rend_service_t *service;
  1047. char serviceid[REND_SERVICE_ID_LEN+1];
  1048. smartlist_t *matching_ports;
  1049. rend_service_port_config_t *chosen_port;
  1050. tor_assert(circ->_base.purpose == CIRCUIT_PURPOSE_S_REND_JOINED);
  1051. log_debug(LD_REND,"beginning to hunt for addr/port");
  1052. base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
  1053. circ->rend_pk_digest,10);
  1054. service = rend_service_get_by_pk_digest(circ->rend_pk_digest);
  1055. if (!service) {
  1056. log_warn(LD_REND, "Couldn't find any service associated with pk %s on "
  1057. "rendezvous circuit %d; closing.",
  1058. serviceid, circ->_base.n_circ_id);
  1059. return -1;
  1060. }
  1061. matching_ports = smartlist_create();
  1062. SMARTLIST_FOREACH(service->ports, rend_service_port_config_t *, p,
  1063. {
  1064. if (conn->_base.port == p->virtual_port) {
  1065. smartlist_add(matching_ports, p);
  1066. }
  1067. });
  1068. chosen_port = smartlist_choose(matching_ports);
  1069. smartlist_free(matching_ports);
  1070. if (chosen_port) {
  1071. conn->_base.addr = chosen_port->real_addr;
  1072. conn->_base.port = chosen_port->real_port;
  1073. return 0;
  1074. }
  1075. log_info(LD_REND, "No virtual port mapping exists for port %d on service %s",
  1076. conn->_base.port,serviceid);
  1077. return -1;
  1078. }