rendservice.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  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. int flags = CIRCLAUNCH_NEED_CAPACITY | CIRCLAUNCH_IS_INTERNAL;
  598. if (circ_needs_uptime) flags |= CIRCLAUNCH_NEED_UPTIME;
  599. launched = circuit_launch_by_extend_info(
  600. CIRCUIT_PURPOSE_S_CONNECT_REND, extend_info, flags);
  601. if (launched)
  602. break;
  603. }
  604. if (!launched) { /* give up */
  605. log_warn(LD_REND, "Giving up launching first hop of circuit to rendezvous "
  606. "point %s for service %s.",
  607. escaped_safe_str(extend_info->nickname), serviceid);
  608. reason = END_CIRC_REASON_CONNECTFAILED;
  609. goto err;
  610. }
  611. log_info(LD_REND,
  612. "Accepted intro; launching circuit to %s "
  613. "(cookie %s) for service %s.",
  614. escaped_safe_str(extend_info->nickname), hexcookie, serviceid);
  615. tor_assert(launched->build_state);
  616. /* Fill in the circuit's state. */
  617. memcpy(launched->rend_pk_digest, circuit->rend_pk_digest,
  618. DIGEST_LEN);
  619. memcpy(launched->rend_cookie, r_cookie, REND_COOKIE_LEN);
  620. strlcpy(launched->rend_query, service->service_id,
  621. sizeof(launched->rend_query));
  622. launched->rend_desc_version = service->descriptor_version;
  623. launched->build_state->pending_final_cpath = cpath =
  624. tor_malloc_zero(sizeof(crypt_path_t));
  625. cpath->magic = CRYPT_PATH_MAGIC;
  626. launched->build_state->expiry_time = time(NULL) + MAX_REND_TIMEOUT;
  627. cpath->dh_handshake_state = dh;
  628. dh = NULL;
  629. if (circuit_init_cpath_crypto(cpath,keys+DIGEST_LEN,1)<0)
  630. goto err;
  631. memcpy(cpath->handshake_digest, keys, DIGEST_LEN);
  632. if (extend_info) extend_info_free(extend_info);
  633. return 0;
  634. err:
  635. if (dh) crypto_dh_free(dh);
  636. if (launched)
  637. circuit_mark_for_close(TO_CIRCUIT(launched), reason);
  638. if (extend_info) extend_info_free(extend_info);
  639. return -1;
  640. }
  641. /** Called when we fail building a rendezvous circuit at some point other
  642. * than the last hop: launches a new circuit to the same rendezvous point.
  643. */
  644. void
  645. rend_service_relaunch_rendezvous(origin_circuit_t *oldcirc)
  646. {
  647. origin_circuit_t *newcirc;
  648. cpath_build_state_t *newstate, *oldstate;
  649. tor_assert(oldcirc->_base.purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  650. if (!oldcirc->build_state ||
  651. oldcirc->build_state->failure_count > MAX_REND_FAILURES ||
  652. oldcirc->build_state->expiry_time < time(NULL)) {
  653. log_info(LD_REND,
  654. "Attempt to build circuit to %s for rendezvous has failed "
  655. "too many times or expired; giving up.",
  656. oldcirc->build_state ?
  657. oldcirc->build_state->chosen_exit->nickname : "*unknown*");
  658. return;
  659. }
  660. oldstate = oldcirc->build_state;
  661. tor_assert(oldstate);
  662. if (oldstate->pending_final_cpath == NULL) {
  663. log_info(LD_REND,"Skipping relaunch of circ that failed on its first hop. "
  664. "Initiator will retry.");
  665. return;
  666. }
  667. log_info(LD_REND,"Reattempting rendezvous circuit to '%s'",
  668. oldstate->chosen_exit->nickname);
  669. newcirc = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_CONNECT_REND,
  670. oldstate->chosen_exit,
  671. CIRCLAUNCH_NEED_CAPACITY|CIRCLAUNCH_IS_INTERNAL);
  672. if (!newcirc) {
  673. log_warn(LD_REND,"Couldn't relaunch rendezvous circuit to '%s'.",
  674. oldstate->chosen_exit->nickname);
  675. return;
  676. }
  677. newstate = newcirc->build_state;
  678. tor_assert(newstate);
  679. newstate->failure_count = oldstate->failure_count+1;
  680. newstate->expiry_time = oldstate->expiry_time;
  681. newstate->pending_final_cpath = oldstate->pending_final_cpath;
  682. oldstate->pending_final_cpath = NULL;
  683. memcpy(newcirc->rend_query, oldcirc->rend_query,
  684. REND_SERVICE_ID_LEN_BASE32+1);
  685. memcpy(newcirc->rend_pk_digest, oldcirc->rend_pk_digest,
  686. DIGEST_LEN);
  687. memcpy(newcirc->rend_cookie, oldcirc->rend_cookie,
  688. REND_COOKIE_LEN);
  689. newcirc->rend_desc_version = oldcirc->rend_desc_version;
  690. }
  691. /** Launch a circuit to serve as an introduction point for the service
  692. * <b>service</b> at the introduction point <b>nickname</b>
  693. */
  694. static int
  695. rend_service_launch_establish_intro(rend_service_t *service,
  696. rend_intro_point_t *intro)
  697. {
  698. origin_circuit_t *launched;
  699. log_info(LD_REND,
  700. "Launching circuit to introduction point %s for service %s",
  701. escaped_safe_str(intro->extend_info->nickname),
  702. service->service_id);
  703. rep_hist_note_used_internal(time(NULL), 1, 0);
  704. ++service->n_intro_circuits_launched;
  705. launched = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  706. intro->extend_info,
  707. CIRCLAUNCH_NEED_UPTIME|CIRCLAUNCH_IS_INTERNAL);
  708. if (!launched) {
  709. log_info(LD_REND,
  710. "Can't launch circuit to establish introduction at %s.",
  711. escaped_safe_str(intro->extend_info->nickname));
  712. return -1;
  713. }
  714. strlcpy(launched->rend_query, service->service_id,
  715. sizeof(launched->rend_query));
  716. memcpy(launched->rend_pk_digest, service->pk_digest, DIGEST_LEN);
  717. launched->rend_desc_version = service->descriptor_version;
  718. if (service->descriptor_version == 2)
  719. launched->intro_key = crypto_pk_dup_key(intro->intro_key);
  720. if (launched->_base.state == CIRCUIT_STATE_OPEN)
  721. rend_service_intro_has_opened(launched);
  722. return 0;
  723. }
  724. /** Called when we're done building a circuit to an introduction point:
  725. * sends a RELAY_ESTABLISH_INTRO cell.
  726. */
  727. void
  728. rend_service_intro_has_opened(origin_circuit_t *circuit)
  729. {
  730. rend_service_t *service;
  731. size_t len;
  732. int r;
  733. char buf[RELAY_PAYLOAD_SIZE];
  734. char auth[DIGEST_LEN + 9];
  735. char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
  736. int reason = END_CIRC_REASON_TORPROTOCOL;
  737. crypto_pk_env_t *intro_key;
  738. tor_assert(circuit->_base.purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO);
  739. tor_assert(circuit->cpath);
  740. base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
  741. circuit->rend_pk_digest, REND_SERVICE_ID_LEN);
  742. service = rend_service_get_by_pk_digest_and_version(
  743. circuit->rend_pk_digest, circuit->rend_desc_version);
  744. if (!service) {
  745. log_warn(LD_REND, "Unrecognized service ID %s on introduction circuit %d.",
  746. serviceid, circuit->_base.n_circ_id);
  747. reason = END_CIRC_REASON_NOSUCHSERVICE;
  748. goto err;
  749. }
  750. log_info(LD_REND,
  751. "Established circuit %d as introduction point for service %s",
  752. circuit->_base.n_circ_id, serviceid);
  753. /* If the introduction point will not be used in an unversioned
  754. * descriptor, use the intro key instead of the service key in
  755. * ESTABLISH_INTRO. */
  756. if (service->descriptor_version == 0)
  757. intro_key = service->private_key;
  758. else
  759. intro_key = circuit->intro_key;
  760. /* Build the payload for a RELAY_ESTABLISH_INTRO cell. */
  761. len = crypto_pk_asn1_encode(intro_key, buf+2,
  762. RELAY_PAYLOAD_SIZE-2);
  763. set_uint16(buf, htons((uint16_t)len));
  764. len += 2;
  765. memcpy(auth, circuit->cpath->prev->handshake_digest, DIGEST_LEN);
  766. memcpy(auth+DIGEST_LEN, "INTRODUCE", 9);
  767. if (crypto_digest(buf+len, auth, DIGEST_LEN+9))
  768. goto err;
  769. len += 20;
  770. note_crypto_pk_op(REND_SERVER);
  771. r = crypto_pk_private_sign_digest(intro_key, buf+len, buf, len);
  772. if (r<0) {
  773. log_warn(LD_BUG, "Internal error: couldn't sign introduction request.");
  774. reason = END_CIRC_REASON_INTERNAL;
  775. goto err;
  776. }
  777. len += r;
  778. if (relay_send_command_from_edge(0, TO_CIRCUIT(circuit),
  779. RELAY_COMMAND_ESTABLISH_INTRO,
  780. buf, len, circuit->cpath->prev)<0) {
  781. log_info(LD_GENERAL,
  782. "Couldn't send introduction request for service %s on circuit %d",
  783. serviceid, circuit->_base.n_circ_id);
  784. reason = END_CIRC_REASON_INTERNAL;
  785. goto err;
  786. }
  787. return;
  788. err:
  789. circuit_mark_for_close(TO_CIRCUIT(circuit), reason);
  790. }
  791. /** Called when we get an INTRO_ESTABLISHED cell; mark the circuit as a
  792. * live introduction point, and note that the service descriptor is
  793. * now out-of-date.*/
  794. int
  795. rend_service_intro_established(origin_circuit_t *circuit, const char *request,
  796. size_t request_len)
  797. {
  798. rend_service_t *service;
  799. char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
  800. (void) request;
  801. (void) request_len;
  802. if (circuit->_base.purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO) {
  803. log_warn(LD_PROTOCOL,
  804. "received INTRO_ESTABLISHED cell on non-intro circuit.");
  805. goto err;
  806. }
  807. service = rend_service_get_by_pk_digest_and_version(
  808. circuit->rend_pk_digest, circuit->rend_desc_version);
  809. if (!service) {
  810. log_warn(LD_REND, "Unknown service on introduction circuit %d.",
  811. circuit->_base.n_circ_id);
  812. goto err;
  813. }
  814. service->desc_is_dirty = time(NULL);
  815. circuit->_base.purpose = CIRCUIT_PURPOSE_S_INTRO;
  816. base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32 + 1,
  817. circuit->rend_pk_digest, REND_SERVICE_ID_LEN);
  818. log_info(LD_REND,
  819. "Received INTRO_ESTABLISHED cell on circuit %d for service %s",
  820. circuit->_base.n_circ_id, serviceid);
  821. return 0;
  822. err:
  823. circuit_mark_for_close(TO_CIRCUIT(circuit), END_CIRC_REASON_TORPROTOCOL);
  824. return -1;
  825. }
  826. /** Called once a circuit to a rendezvous point is established: sends a
  827. * RELAY_COMMAND_RENDEZVOUS1 cell.
  828. */
  829. void
  830. rend_service_rendezvous_has_opened(origin_circuit_t *circuit)
  831. {
  832. rend_service_t *service;
  833. char buf[RELAY_PAYLOAD_SIZE];
  834. crypt_path_t *hop;
  835. char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
  836. char hexcookie[9];
  837. int reason;
  838. tor_assert(circuit->_base.purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  839. tor_assert(circuit->cpath);
  840. tor_assert(circuit->build_state);
  841. hop = circuit->build_state->pending_final_cpath;
  842. tor_assert(hop);
  843. base16_encode(hexcookie,9,circuit->rend_cookie,4);
  844. base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
  845. circuit->rend_pk_digest, REND_SERVICE_ID_LEN);
  846. log_info(LD_REND,
  847. "Done building circuit %d to rendezvous with "
  848. "cookie %s for service %s",
  849. circuit->_base.n_circ_id, hexcookie, serviceid);
  850. service = rend_service_get_by_pk_digest_and_version(
  851. circuit->rend_pk_digest, circuit->rend_desc_version);
  852. if (!service) {
  853. log_warn(LD_GENERAL, "Internal error: unrecognized service ID on "
  854. "introduction circuit.");
  855. reason = END_CIRC_REASON_INTERNAL;
  856. goto err;
  857. }
  858. /* All we need to do is send a RELAY_RENDEZVOUS1 cell... */
  859. memcpy(buf, circuit->rend_cookie, REND_COOKIE_LEN);
  860. if (crypto_dh_get_public(hop->dh_handshake_state,
  861. buf+REND_COOKIE_LEN, DH_KEY_LEN)<0) {
  862. log_warn(LD_GENERAL,"Couldn't get DH public key.");
  863. reason = END_CIRC_REASON_INTERNAL;
  864. goto err;
  865. }
  866. memcpy(buf+REND_COOKIE_LEN+DH_KEY_LEN, hop->handshake_digest,
  867. DIGEST_LEN);
  868. /* Send the cell */
  869. if (relay_send_command_from_edge(0, TO_CIRCUIT(circuit),
  870. RELAY_COMMAND_RENDEZVOUS1,
  871. buf, REND_COOKIE_LEN+DH_KEY_LEN+DIGEST_LEN,
  872. circuit->cpath->prev)<0) {
  873. log_warn(LD_GENERAL, "Couldn't send RENDEZVOUS1 cell.");
  874. reason = END_CIRC_REASON_INTERNAL;
  875. goto err;
  876. }
  877. crypto_dh_free(hop->dh_handshake_state);
  878. hop->dh_handshake_state = NULL;
  879. /* Append the cpath entry. */
  880. hop->state = CPATH_STATE_OPEN;
  881. /* set the windows to default. these are the windows
  882. * that bob thinks alice has.
  883. */
  884. hop->package_window = CIRCWINDOW_START;
  885. hop->deliver_window = CIRCWINDOW_START;
  886. onion_append_to_cpath(&circuit->cpath, hop);
  887. circuit->build_state->pending_final_cpath = NULL; /* prevent double-free */
  888. /* Change the circuit purpose. */
  889. circuit->_base.purpose = CIRCUIT_PURPOSE_S_REND_JOINED;
  890. return;
  891. err:
  892. circuit_mark_for_close(TO_CIRCUIT(circuit), reason);
  893. }
  894. /*
  895. * Manage introduction points
  896. */
  897. /** Return the (possibly non-open) introduction circuit ending at
  898. * <b>intro</b> for the service whose public key is <b>pk_digest</b> and
  899. * which publishes descriptor of version <b>desc_version</b>. Return
  900. * NULL if no such service is found.
  901. */
  902. static origin_circuit_t *
  903. find_intro_circuit(rend_intro_point_t *intro, const char *pk_digest,
  904. int desc_version)
  905. {
  906. origin_circuit_t *circ = NULL;
  907. tor_assert(intro);
  908. while ((circ = circuit_get_next_by_pk_and_purpose(circ,pk_digest,
  909. CIRCUIT_PURPOSE_S_INTRO))) {
  910. if (!strcasecmp(circ->build_state->chosen_exit->identity_digest,
  911. intro->extend_info->identity_digest) &&
  912. circ->rend_desc_version == desc_version) {
  913. return circ;
  914. }
  915. }
  916. circ = NULL;
  917. while ((circ = circuit_get_next_by_pk_and_purpose(circ,pk_digest,
  918. CIRCUIT_PURPOSE_S_ESTABLISH_INTRO))) {
  919. if (!strcasecmp(circ->build_state->chosen_exit->identity_digest,
  920. intro->extend_info->identity_digest) &&
  921. circ->rend_desc_version == desc_version) {
  922. return circ;
  923. }
  924. }
  925. return NULL;
  926. }
  927. /** Encode and sign up-to-date v0 and/or v2 service descriptors for
  928. * <b>service</b>, and upload it/them to all the dirservers/to the
  929. * responsible hidden service directories.
  930. */
  931. static void
  932. upload_service_descriptor(rend_service_t *service)
  933. {
  934. time_t now = time(NULL);
  935. int rendpostperiod;
  936. char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
  937. int uploaded = 0;
  938. /* Update the descriptor. */
  939. rend_service_update_descriptor(service);
  940. rendpostperiod = get_options()->RendPostPeriod;
  941. /* Upload unversioned (v0) descriptor? */
  942. if (service->descriptor_version == 0 &&
  943. get_options()->PublishHidServDescriptors) {
  944. char *desc;
  945. size_t desc_len;
  946. /* Encode the descriptor. */
  947. if (rend_encode_service_descriptor(service->desc,
  948. service->private_key,
  949. &desc, &desc_len)<0) {
  950. log_warn(LD_BUG, "Internal error: couldn't encode service descriptor; "
  951. "not uploading.");
  952. return;
  953. }
  954. /* Post it to the dirservers */
  955. rend_get_service_id(service->desc->pk, serviceid);
  956. log_info(LD_REND, "Sending publish request for hidden service %s",
  957. serviceid);
  958. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_RENDDESC,
  959. ROUTER_PURPOSE_GENERAL,
  960. HIDSERV_AUTHORITY, desc, desc_len, 0);
  961. tor_free(desc);
  962. service->next_upload_time = now + rendpostperiod;
  963. uploaded = 1;
  964. }
  965. /* Upload v2 descriptor? */
  966. if (service->descriptor_version == 2 &&
  967. get_options()->PublishHidServDescriptors) {
  968. if (hid_serv_have_enough_directories()) {
  969. int seconds_valid;
  970. smartlist_t *descs = smartlist_create();
  971. int i;
  972. /* Encode the current descriptor. */
  973. seconds_valid = rend_encode_v2_descriptors(descs, service->desc, now,
  974. NULL, 0);
  975. if (seconds_valid < 0) {
  976. log_warn(LD_BUG, "Internal error: couldn't encode service descriptor; "
  977. "not uploading.");
  978. smartlist_free(descs);
  979. return;
  980. }
  981. /* Post the current descriptors to the hidden service directories. */
  982. rend_get_service_id(service->desc->pk, serviceid);
  983. log_info(LD_REND, "Sending publish request for hidden service %s",
  984. serviceid);
  985. directory_post_to_hs_dir(descs, serviceid, seconds_valid);
  986. /* Free memory for descriptors. */
  987. for (i = 0; i < smartlist_len(descs); i++)
  988. rend_encoded_v2_service_descriptor_free(smartlist_get(descs, i));
  989. smartlist_clear(descs);
  990. /* Update next upload time. */
  991. if (seconds_valid - REND_TIME_PERIOD_OVERLAPPING_V2_DESCS
  992. > rendpostperiod)
  993. service->next_upload_time = now + rendpostperiod;
  994. else if (seconds_valid < REND_TIME_PERIOD_OVERLAPPING_V2_DESCS)
  995. service->next_upload_time = now + seconds_valid + 1;
  996. else
  997. service->next_upload_time = now + seconds_valid -
  998. REND_TIME_PERIOD_OVERLAPPING_V2_DESCS + 1;
  999. /* Post also the next descriptors, if necessary. */
  1000. if (seconds_valid < REND_TIME_PERIOD_OVERLAPPING_V2_DESCS) {
  1001. seconds_valid = rend_encode_v2_descriptors(descs, service->desc,
  1002. now, NULL, 1);
  1003. if (seconds_valid < 0) {
  1004. log_warn(LD_BUG, "Internal error: couldn't encode service "
  1005. "descriptor; not uploading.");
  1006. smartlist_free(descs);
  1007. return;
  1008. }
  1009. directory_post_to_hs_dir(descs, serviceid, seconds_valid);
  1010. /* Free memory for descriptors. */
  1011. for (i = 0; i < smartlist_len(descs); i++)
  1012. rend_encoded_v2_service_descriptor_free(smartlist_get(descs, i));
  1013. }
  1014. smartlist_free(descs);
  1015. uploaded = 1;
  1016. log_info(LD_REND, "Successfully uploaded v2 rend descriptors!");
  1017. }
  1018. }
  1019. /* If not uploaded, try again in one minute. */
  1020. if (!uploaded)
  1021. service->next_upload_time = now + 60;
  1022. /* Unmark dirty flag of this service. */
  1023. service->desc_is_dirty = 0;
  1024. }
  1025. /** For every service, check how many intro points it currently has, and:
  1026. * - Pick new intro points as necessary.
  1027. * - Launch circuits to any new intro points.
  1028. */
  1029. void
  1030. rend_services_introduce(void)
  1031. {
  1032. int i,j,r;
  1033. routerinfo_t *router;
  1034. rend_service_t *service;
  1035. rend_intro_point_t *intro;
  1036. int changed, prev_intro_nodes;
  1037. smartlist_t *intro_routers, *exclude_routers;
  1038. time_t now;
  1039. intro_routers = smartlist_create();
  1040. exclude_routers = smartlist_create();
  1041. now = time(NULL);
  1042. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  1043. smartlist_clear(intro_routers);
  1044. service = smartlist_get(rend_service_list, i);
  1045. tor_assert(service);
  1046. changed = 0;
  1047. if (now > service->intro_period_started+INTRO_CIRC_RETRY_PERIOD) {
  1048. /* One period has elapsed; we can try building circuits again. */
  1049. service->intro_period_started = now;
  1050. service->n_intro_circuits_launched = 0;
  1051. } else if (service->n_intro_circuits_launched >=
  1052. MAX_INTRO_CIRCS_PER_PERIOD) {
  1053. /* We have failed too many times in this period; wait for the next
  1054. * one before we try again. */
  1055. continue;
  1056. }
  1057. /* Find out which introduction points we have in progress for this
  1058. service. */
  1059. for (j=0; j < smartlist_len(service->intro_nodes); ++j) {
  1060. intro = smartlist_get(service->intro_nodes, j);
  1061. router = router_get_by_digest(intro->extend_info->identity_digest);
  1062. if (!router || !find_intro_circuit(intro, service->pk_digest,
  1063. service->descriptor_version)) {
  1064. log_info(LD_REND,"Giving up on %s as intro point for %s.",
  1065. intro->extend_info->nickname, service->service_id);
  1066. rend_intro_point_free(intro);
  1067. smartlist_del(service->intro_nodes,j--);
  1068. changed = 1;
  1069. service->desc_is_dirty = now;
  1070. }
  1071. smartlist_add(intro_routers, router);
  1072. }
  1073. /* We have enough intro points, and the intro points we thought we had were
  1074. * all connected.
  1075. */
  1076. if (!changed && smartlist_len(service->intro_nodes) >= NUM_INTRO_POINTS) {
  1077. /* We have all our intro points! Start a fresh period and reset the
  1078. * circuit count. */
  1079. service->intro_period_started = now;
  1080. service->n_intro_circuits_launched = 0;
  1081. continue;
  1082. }
  1083. /* Remember how many introduction circuits we started with. */
  1084. prev_intro_nodes = smartlist_len(service->intro_nodes);
  1085. smartlist_add_all(exclude_routers, intro_routers);
  1086. /* The directory is now here. Pick three ORs as intro points. */
  1087. for (j=prev_intro_nodes; j < NUM_INTRO_POINTS; ++j) {
  1088. router = router_choose_random_node(service->intro_prefer_nodes,
  1089. service->intro_exclude_nodes, exclude_routers, 1, 0, 0,
  1090. get_options()->_AllowInvalid & ALLOW_INVALID_INTRODUCTION,
  1091. 0, 0);
  1092. if (!router) {
  1093. log_warn(LD_REND,
  1094. "Could only establish %d introduction points for %s.",
  1095. smartlist_len(service->intro_nodes), service->service_id);
  1096. break;
  1097. }
  1098. changed = 1;
  1099. smartlist_add(intro_routers, router);
  1100. smartlist_add(exclude_routers, router);
  1101. intro = tor_malloc_zero(sizeof(rend_intro_point_t));
  1102. intro->extend_info = extend_info_from_router(router);
  1103. if (service->descriptor_version == 2) {
  1104. intro->intro_key = crypto_new_pk_env();
  1105. tor_assert(!crypto_pk_generate_key(intro->intro_key));
  1106. }
  1107. smartlist_add(service->intro_nodes, intro);
  1108. log_info(LD_REND, "Picked router %s as an intro point for %s.",
  1109. router->nickname, service->service_id);
  1110. }
  1111. /* Reset exclude_routers, for the next time around the loop. */
  1112. smartlist_clear(exclude_routers);
  1113. /* If there's no need to launch new circuits, stop here. */
  1114. if (!changed)
  1115. continue;
  1116. /* Establish new introduction points. */
  1117. for (j=prev_intro_nodes; j < smartlist_len(service->intro_nodes); ++j) {
  1118. intro = smartlist_get(service->intro_nodes, j);
  1119. r = rend_service_launch_establish_intro(service, intro);
  1120. if (r<0) {
  1121. log_warn(LD_REND, "Error launching circuit to node %s for service %s.",
  1122. intro->extend_info->nickname, service->service_id);
  1123. }
  1124. }
  1125. }
  1126. smartlist_free(intro_routers);
  1127. smartlist_free(exclude_routers);
  1128. }
  1129. /** Regenerate and upload rendezvous service descriptors for all
  1130. * services, if necessary. If the descriptor has been dirty enough
  1131. * for long enough, definitely upload; else only upload when the
  1132. * periodic timeout has expired.
  1133. *
  1134. * For the first upload, pick a random time between now and two periods
  1135. * from now, and pick it independently for each service.
  1136. */
  1137. void
  1138. rend_consider_services_upload(time_t now)
  1139. {
  1140. int i;
  1141. rend_service_t *service;
  1142. int rendpostperiod = get_options()->RendPostPeriod;
  1143. if (!get_options()->PublishHidServDescriptors)
  1144. return;
  1145. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  1146. service = smartlist_get(rend_service_list, i);
  1147. if (!service->next_upload_time) { /* never been uploaded yet */
  1148. service->next_upload_time =
  1149. now + crypto_rand_int(2*rendpostperiod);
  1150. }
  1151. if (service->next_upload_time < now ||
  1152. (service->desc_is_dirty &&
  1153. service->desc_is_dirty < now-30)) {
  1154. /* if it's time, or if the directory servers have a wrong service
  1155. * descriptor and ours has been stable for 30 seconds, upload a
  1156. * new one of each format. */
  1157. upload_service_descriptor(service);
  1158. }
  1159. }
  1160. }
  1161. /** Log the status of introduction points for all rendezvous services
  1162. * at log severity <b>severity</b>.
  1163. */
  1164. void
  1165. rend_service_dump_stats(int severity)
  1166. {
  1167. int i,j;
  1168. rend_service_t *service;
  1169. rend_intro_point_t *intro;
  1170. const char *safe_name;
  1171. origin_circuit_t *circ;
  1172. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  1173. service = smartlist_get(rend_service_list, i);
  1174. log(severity, LD_GENERAL, "Service configured in \"%s\":",
  1175. service->directory);
  1176. for (j=0; j < smartlist_len(service->intro_nodes); ++j) {
  1177. intro = smartlist_get(service->intro_nodes, j);
  1178. safe_name = safe_str(intro->extend_info->nickname);
  1179. circ = find_intro_circuit(intro, service->pk_digest,
  1180. service->descriptor_version);
  1181. if (!circ) {
  1182. log(severity, LD_GENERAL, " Intro point %d at %s: no circuit",
  1183. j, safe_name);
  1184. continue;
  1185. }
  1186. log(severity, LD_GENERAL, " Intro point %d at %s: circuit is %s",
  1187. j, safe_name, circuit_state_to_string(circ->_base.state));
  1188. }
  1189. }
  1190. }
  1191. /** Given <b>conn</b>, a rendezvous exit stream, look up the hidden service for
  1192. * 'circ', and look up the port and address based on conn-\>port.
  1193. * Assign the actual conn-\>addr and conn-\>port. Return -1 if failure,
  1194. * or 0 for success.
  1195. */
  1196. int
  1197. rend_service_set_connection_addr_port(edge_connection_t *conn,
  1198. origin_circuit_t *circ)
  1199. {
  1200. rend_service_t *service;
  1201. char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
  1202. smartlist_t *matching_ports;
  1203. rend_service_port_config_t *chosen_port;
  1204. tor_assert(circ->_base.purpose == CIRCUIT_PURPOSE_S_REND_JOINED);
  1205. log_debug(LD_REND,"beginning to hunt for addr/port");
  1206. base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
  1207. circ->rend_pk_digest, REND_SERVICE_ID_LEN);
  1208. service = rend_service_get_by_pk_digest_and_version(circ->rend_pk_digest,
  1209. circ->rend_desc_version);
  1210. if (!service) {
  1211. log_warn(LD_REND, "Couldn't find any service associated with pk %s on "
  1212. "rendezvous circuit %d; closing.",
  1213. serviceid, circ->_base.n_circ_id);
  1214. return -1;
  1215. }
  1216. matching_ports = smartlist_create();
  1217. SMARTLIST_FOREACH(service->ports, rend_service_port_config_t *, p,
  1218. {
  1219. if (conn->_base.port == p->virtual_port) {
  1220. smartlist_add(matching_ports, p);
  1221. }
  1222. });
  1223. chosen_port = smartlist_choose(matching_ports);
  1224. smartlist_free(matching_ports);
  1225. if (chosen_port) {
  1226. conn->_base.addr = chosen_port->real_addr;
  1227. conn->_base.port = chosen_port->real_port;
  1228. return 0;
  1229. }
  1230. log_info(LD_REND, "No virtual port mapping exists for port %d on service %s",
  1231. conn->_base.port,serviceid);
  1232. return -1;
  1233. }