rendservice.c 48 KB

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