rendservice.c 46 KB

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