rendservice.c 47 KB

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