rendclient.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  2. * Copyright (c) 2007-2011, The Tor Project, Inc. */
  3. /* See LICENSE for licensing information */
  4. /**
  5. * \file rendclient.c
  6. * \brief Client code to access location-hidden services.
  7. **/
  8. #include "or.h"
  9. #include "circuitbuild.h"
  10. #include "circuitlist.h"
  11. #include "circuituse.h"
  12. #include "config.h"
  13. #include "connection.h"
  14. #include "connection_edge.h"
  15. #include "directory.h"
  16. #include "main.h"
  17. #include "nodelist.h"
  18. #include "relay.h"
  19. #include "rendclient.h"
  20. #include "rendcommon.h"
  21. #include "rephist.h"
  22. #include "routerlist.h"
  23. static extend_info_t *rend_client_get_random_intro_impl(
  24. const rend_cache_entry_t *rend_query,
  25. const int strict, const int warnings);
  26. /** Called when we've established a circuit to an introduction point:
  27. * send the introduction request. */
  28. void
  29. rend_client_introcirc_has_opened(origin_circuit_t *circ)
  30. {
  31. tor_assert(circ->_base.purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  32. tor_assert(circ->cpath);
  33. log_info(LD_REND,"introcirc is open");
  34. connection_ap_attach_pending();
  35. }
  36. /** Send the establish-rendezvous cell along a rendezvous circuit. if
  37. * it fails, mark the circ for close and return -1. else return 0.
  38. */
  39. static int
  40. rend_client_send_establish_rendezvous(origin_circuit_t *circ)
  41. {
  42. tor_assert(circ->_base.purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  43. tor_assert(circ->rend_data);
  44. log_info(LD_REND, "Sending an ESTABLISH_RENDEZVOUS cell");
  45. if (crypto_rand(circ->rend_data->rend_cookie, REND_COOKIE_LEN) < 0) {
  46. log_warn(LD_BUG, "Internal error: Couldn't produce random cookie.");
  47. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  48. return -1;
  49. }
  50. if (relay_send_command_from_edge(0, TO_CIRCUIT(circ),
  51. RELAY_COMMAND_ESTABLISH_RENDEZVOUS,
  52. circ->rend_data->rend_cookie,
  53. REND_COOKIE_LEN,
  54. circ->cpath->prev)<0) {
  55. /* circ is already marked for close */
  56. log_warn(LD_GENERAL, "Couldn't send ESTABLISH_RENDEZVOUS cell");
  57. return -1;
  58. }
  59. return 0;
  60. }
  61. /** Extend the introduction circuit <b>circ</b> to another valid
  62. * introduction point for the hidden service it is trying to connect
  63. * to, or mark it and launch a new circuit if we can't extend it.
  64. * Return 0 on success. Return -1 and mark the introduction
  65. * circuit on failure.
  66. *
  67. * On failure, the caller is responsible for marking the associated
  68. * rendezvous circuit for close. */
  69. static int
  70. rend_client_reextend_intro_circuit(origin_circuit_t *circ)
  71. {
  72. extend_info_t *extend_info;
  73. int result;
  74. extend_info = rend_client_get_random_intro(circ->rend_data);
  75. if (!extend_info) {
  76. log_warn(LD_REND,
  77. "No usable introduction points left for %s. Closing.",
  78. safe_str_client(circ->rend_data->onion_address));
  79. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  80. return -1;
  81. }
  82. if (circ->remaining_relay_early_cells) {
  83. log_info(LD_REND,
  84. "Re-extending circ %d, this time to %s.",
  85. circ->_base.n_circ_id, extend_info->nickname);
  86. result = circuit_extend_to_new_exit(circ, extend_info);
  87. } else {
  88. log_info(LD_REND,
  89. "Building a new introduction circuit, this time to %s.",
  90. extend_info->nickname);
  91. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_FINISHED);
  92. if (!circuit_launch_by_extend_info(CIRCUIT_PURPOSE_C_INTRODUCING,
  93. extend_info,
  94. CIRCLAUNCH_IS_INTERNAL)) {
  95. log_warn(LD_REND, "Building introduction circuit failed.");
  96. result = -1;
  97. } else {
  98. result = 0;
  99. }
  100. }
  101. extend_info_free(extend_info);
  102. return result;
  103. }
  104. /** Called when we're trying to connect an ap conn; sends an INTRODUCE1 cell
  105. * down introcirc if possible.
  106. */
  107. int
  108. rend_client_send_introduction(origin_circuit_t *introcirc,
  109. origin_circuit_t *rendcirc)
  110. {
  111. size_t payload_len;
  112. int r, v3_shift = 0;
  113. char payload[RELAY_PAYLOAD_SIZE];
  114. char tmp[RELAY_PAYLOAD_SIZE];
  115. rend_cache_entry_t *entry;
  116. crypt_path_t *cpath;
  117. off_t dh_offset;
  118. crypto_pk_env_t *intro_key = NULL;
  119. tor_assert(introcirc->_base.purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  120. tor_assert(rendcirc->_base.purpose == CIRCUIT_PURPOSE_C_REND_READY);
  121. tor_assert(introcirc->rend_data);
  122. tor_assert(rendcirc->rend_data);
  123. tor_assert(!rend_cmp_service_ids(introcirc->rend_data->onion_address,
  124. rendcirc->rend_data->onion_address));
  125. if (rend_cache_lookup_entry(introcirc->rend_data->onion_address, -1,
  126. &entry) < 1) {
  127. log_info(LD_REND,
  128. "query %s didn't have valid rend desc in cache. "
  129. "Refetching descriptor.",
  130. safe_str_client(introcirc->rend_data->onion_address));
  131. rend_client_refetch_v2_renddesc(introcirc->rend_data);
  132. {
  133. connection_t *conn;
  134. while ((conn = connection_get_by_type_state_rendquery(CONN_TYPE_AP,
  135. AP_CONN_STATE_CIRCUIT_WAIT,
  136. introcirc->rend_data->onion_address))) {
  137. conn->state = AP_CONN_STATE_RENDDESC_WAIT;
  138. }
  139. }
  140. return -1;
  141. }
  142. /* first 20 bytes of payload are the hash of Bob's pk */
  143. intro_key = NULL;
  144. SMARTLIST_FOREACH(entry->parsed->intro_nodes, rend_intro_point_t *,
  145. intro, {
  146. if (tor_memeq(introcirc->build_state->chosen_exit->identity_digest,
  147. intro->extend_info->identity_digest, DIGEST_LEN)) {
  148. intro_key = intro->intro_key;
  149. break;
  150. }
  151. });
  152. if (!intro_key) {
  153. log_info(LD_REND, "Could not find intro key for %s at %s; we "
  154. "have a v2 rend desc with %d intro points. "
  155. "Trying a different intro point...",
  156. safe_str_client(introcirc->rend_data->onion_address),
  157. introcirc->build_state->chosen_exit->nickname,
  158. smartlist_len(entry->parsed->intro_nodes));
  159. if (rend_client_reextend_intro_circuit(introcirc)) {
  160. goto perm_err;
  161. } else {
  162. return -1;
  163. }
  164. }
  165. if (crypto_pk_get_digest(intro_key, payload)<0) {
  166. log_warn(LD_BUG, "Internal error: couldn't hash public key.");
  167. goto perm_err;
  168. }
  169. /* Initialize the pending_final_cpath and start the DH handshake. */
  170. cpath = rendcirc->build_state->pending_final_cpath;
  171. if (!cpath) {
  172. cpath = rendcirc->build_state->pending_final_cpath =
  173. tor_malloc_zero(sizeof(crypt_path_t));
  174. cpath->magic = CRYPT_PATH_MAGIC;
  175. if (!(cpath->dh_handshake_state = crypto_dh_new(DH_TYPE_REND))) {
  176. log_warn(LD_BUG, "Internal error: couldn't allocate DH.");
  177. goto perm_err;
  178. }
  179. if (crypto_dh_generate_public(cpath->dh_handshake_state)<0) {
  180. log_warn(LD_BUG, "Internal error: couldn't generate g^x.");
  181. goto perm_err;
  182. }
  183. }
  184. /* If version is 3, write (optional) auth data and timestamp. */
  185. if (entry->parsed->protocols & (1<<3)) {
  186. tmp[0] = 3; /* version 3 of the cell format */
  187. tmp[1] = (uint8_t)introcirc->rend_data->auth_type; /* auth type, if any */
  188. v3_shift = 1;
  189. if (introcirc->rend_data->auth_type != REND_NO_AUTH) {
  190. set_uint16(tmp+2, htons(REND_DESC_COOKIE_LEN));
  191. memcpy(tmp+4, introcirc->rend_data->descriptor_cookie,
  192. REND_DESC_COOKIE_LEN);
  193. v3_shift += 2+REND_DESC_COOKIE_LEN;
  194. }
  195. set_uint32(tmp+v3_shift+1, htonl((uint32_t)time(NULL)));
  196. v3_shift += 4;
  197. } /* if version 2 only write version number */
  198. else if (entry->parsed->protocols & (1<<2)) {
  199. tmp[0] = 2; /* version 2 of the cell format */
  200. }
  201. /* write the remaining items into tmp */
  202. if (entry->parsed->protocols & (1<<3) || entry->parsed->protocols & (1<<2)) {
  203. /* version 2 format */
  204. extend_info_t *extend_info = rendcirc->build_state->chosen_exit;
  205. int klen;
  206. /* nul pads */
  207. set_uint32(tmp+v3_shift+1, tor_addr_to_ipv4h(&extend_info->addr));
  208. set_uint16(tmp+v3_shift+5, htons(extend_info->port));
  209. memcpy(tmp+v3_shift+7, extend_info->identity_digest, DIGEST_LEN);
  210. klen = crypto_pk_asn1_encode(extend_info->onion_key,
  211. tmp+v3_shift+7+DIGEST_LEN+2,
  212. sizeof(tmp)-(v3_shift+7+DIGEST_LEN+2));
  213. set_uint16(tmp+v3_shift+7+DIGEST_LEN, htons(klen));
  214. memcpy(tmp+v3_shift+7+DIGEST_LEN+2+klen, rendcirc->rend_data->rend_cookie,
  215. REND_COOKIE_LEN);
  216. dh_offset = v3_shift+7+DIGEST_LEN+2+klen+REND_COOKIE_LEN;
  217. } else {
  218. /* Version 0. */
  219. strncpy(tmp, rendcirc->build_state->chosen_exit->nickname,
  220. (MAX_NICKNAME_LEN+1)); /* nul pads */
  221. memcpy(tmp+MAX_NICKNAME_LEN+1, rendcirc->rend_data->rend_cookie,
  222. REND_COOKIE_LEN);
  223. dh_offset = MAX_NICKNAME_LEN+1+REND_COOKIE_LEN;
  224. }
  225. if (crypto_dh_get_public(cpath->dh_handshake_state, tmp+dh_offset,
  226. DH_KEY_LEN)<0) {
  227. log_warn(LD_BUG, "Internal error: couldn't extract g^x.");
  228. goto perm_err;
  229. }
  230. note_crypto_pk_op(REND_CLIENT);
  231. /*XXX maybe give crypto_pk_public_hybrid_encrypt a max_len arg,
  232. * to avoid buffer overflows? */
  233. r = crypto_pk_public_hybrid_encrypt(intro_key, payload+DIGEST_LEN,
  234. sizeof(payload)-DIGEST_LEN,
  235. tmp,
  236. (int)(dh_offset+DH_KEY_LEN),
  237. PK_PKCS1_OAEP_PADDING, 0);
  238. if (r<0) {
  239. log_warn(LD_BUG,"Internal error: hybrid pk encrypt failed.");
  240. goto perm_err;
  241. }
  242. payload_len = DIGEST_LEN + r;
  243. tor_assert(payload_len <= RELAY_PAYLOAD_SIZE); /* we overran something */
  244. log_info(LD_REND, "Sending an INTRODUCE1 cell");
  245. if (relay_send_command_from_edge(0, TO_CIRCUIT(introcirc),
  246. RELAY_COMMAND_INTRODUCE1,
  247. payload, payload_len,
  248. introcirc->cpath->prev)<0) {
  249. /* introcirc is already marked for close. leave rendcirc alone. */
  250. log_warn(LD_BUG, "Couldn't send INTRODUCE1 cell");
  251. return -2;
  252. }
  253. /* Now, we wait for an ACK or NAK on this circuit. */
  254. introcirc->_base.purpose = CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT;
  255. return 0;
  256. perm_err:
  257. if (!introcirc->_base.marked_for_close)
  258. circuit_mark_for_close(TO_CIRCUIT(introcirc), END_CIRC_REASON_INTERNAL);
  259. circuit_mark_for_close(TO_CIRCUIT(rendcirc), END_CIRC_REASON_INTERNAL);
  260. return -2;
  261. }
  262. /** Called when a rendezvous circuit is open; sends a establish
  263. * rendezvous circuit as appropriate. */
  264. void
  265. rend_client_rendcirc_has_opened(origin_circuit_t *circ)
  266. {
  267. tor_assert(circ->_base.purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  268. log_info(LD_REND,"rendcirc is open");
  269. /* generate a rendezvous cookie, store it in circ */
  270. if (rend_client_send_establish_rendezvous(circ) < 0) {
  271. return;
  272. }
  273. }
  274. /** Called when get an ACK or a NAK for a REND_INTRODUCE1 cell.
  275. */
  276. int
  277. rend_client_introduction_acked(origin_circuit_t *circ,
  278. const uint8_t *request, size_t request_len)
  279. {
  280. origin_circuit_t *rendcirc;
  281. (void) request; // XXXX Use this.
  282. if (circ->_base.purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  283. log_warn(LD_PROTOCOL,
  284. "Received REND_INTRODUCE_ACK on unexpected circuit %d.",
  285. circ->_base.n_circ_id);
  286. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  287. return -1;
  288. }
  289. tor_assert(circ->build_state->chosen_exit);
  290. tor_assert(circ->rend_data);
  291. if (request_len == 0) {
  292. /* It's an ACK; the introduction point relayed our introduction request. */
  293. /* Locate the rend circ which is waiting to hear about this ack,
  294. * and tell it.
  295. */
  296. log_info(LD_REND,"Received ack. Telling rend circ...");
  297. rendcirc = circuit_get_by_rend_query_and_purpose(
  298. circ->rend_data->onion_address, CIRCUIT_PURPOSE_C_REND_READY);
  299. if (rendcirc) { /* remember the ack */
  300. rendcirc->_base.purpose = CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED;
  301. } else {
  302. log_info(LD_REND,"...Found no rend circ. Dropping on the floor.");
  303. }
  304. /* close the circuit: we won't need it anymore. */
  305. circ->_base.purpose = CIRCUIT_PURPOSE_C_INTRODUCE_ACKED;
  306. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_FINISHED);
  307. } else {
  308. /* It's a NAK; the introduction point didn't relay our request. */
  309. circ->_base.purpose = CIRCUIT_PURPOSE_C_INTRODUCING;
  310. /* Remove this intro point from the set of viable introduction
  311. * points. If any remain, extend to a new one and try again.
  312. * If none remain, refetch the service descriptor.
  313. */
  314. log_info(LD_REND, "Got nack for %s from %s...",
  315. safe_str_client(circ->rend_data->onion_address),
  316. circ->build_state->chosen_exit->nickname);
  317. if (rend_client_remove_intro_point(circ->build_state->chosen_exit,
  318. circ->rend_data) > 0) {
  319. /* There are introduction points left. Re-extend the circuit to
  320. * another intro point and try again. */
  321. int result = rend_client_reextend_intro_circuit(circ);
  322. /* XXXX If that call failed, should we close the rend circuit,
  323. * too? */
  324. return result;
  325. }
  326. }
  327. return 0;
  328. }
  329. /** The period for which a hidden service directory cannot be queried for
  330. * the same descriptor ID again. */
  331. #define REND_HID_SERV_DIR_REQUERY_PERIOD (15 * 60)
  332. /** Contains the last request times to hidden service directories for
  333. * certain queries; keys are strings consisting of base32-encoded
  334. * hidden service directory identities and base32-encoded descriptor IDs;
  335. * values are pointers to timestamps of the last requests. */
  336. static strmap_t *last_hid_serv_requests = NULL;
  337. /** Look up the last request time to hidden service directory <b>hs_dir</b>
  338. * for descriptor ID <b>desc_id_base32</b>. If <b>set</b> is non-zero,
  339. * assign the current time <b>now</b> and return that. Otherwise, return
  340. * the most recent request time, or 0 if no such request has been sent
  341. * before. */
  342. static time_t
  343. lookup_last_hid_serv_request(routerstatus_t *hs_dir,
  344. const char *desc_id_base32, time_t now, int set)
  345. {
  346. char hsdir_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  347. char hsdir_desc_comb_id[2 * REND_DESC_ID_V2_LEN_BASE32 + 1];
  348. time_t *last_request_ptr;
  349. base32_encode(hsdir_id_base32, sizeof(hsdir_id_base32),
  350. hs_dir->identity_digest, DIGEST_LEN);
  351. tor_snprintf(hsdir_desc_comb_id, sizeof(hsdir_desc_comb_id), "%s%s",
  352. hsdir_id_base32, desc_id_base32);
  353. if (set) {
  354. time_t *oldptr;
  355. last_request_ptr = tor_malloc_zero(sizeof(time_t));
  356. *last_request_ptr = now;
  357. oldptr = strmap_set(last_hid_serv_requests, hsdir_desc_comb_id,
  358. last_request_ptr);
  359. tor_free(oldptr);
  360. } else
  361. last_request_ptr = strmap_get_lc(last_hid_serv_requests,
  362. hsdir_desc_comb_id);
  363. return (last_request_ptr) ? *last_request_ptr : 0;
  364. }
  365. /** Clean the history of request times to hidden service directories, so that
  366. * it does not contain requests older than REND_HID_SERV_DIR_REQUERY_PERIOD
  367. * seconds any more. */
  368. static void
  369. directory_clean_last_hid_serv_requests(time_t now)
  370. {
  371. strmap_iter_t *iter;
  372. time_t cutoff = now - REND_HID_SERV_DIR_REQUERY_PERIOD;
  373. if (!last_hid_serv_requests)
  374. last_hid_serv_requests = strmap_new();
  375. for (iter = strmap_iter_init(last_hid_serv_requests);
  376. !strmap_iter_done(iter); ) {
  377. const char *key;
  378. void *val;
  379. time_t *ent;
  380. strmap_iter_get(iter, &key, &val);
  381. ent = (time_t *) val;
  382. if (*ent < cutoff) {
  383. iter = strmap_iter_next_rmv(last_hid_serv_requests, iter);
  384. tor_free(ent);
  385. } else {
  386. iter = strmap_iter_next(last_hid_serv_requests, iter);
  387. }
  388. }
  389. }
  390. /** Determine the responsible hidden service directories for <b>desc_id</b>
  391. * and fetch the descriptor belonging to that ID from one of them. Only
  392. * send a request to hidden service directories that we did not try within
  393. * the last REND_HID_SERV_DIR_REQUERY_PERIOD seconds; on success, return 1,
  394. * in the case that no hidden service directory is left to ask for the
  395. * descriptor, return 0, and in case of a failure -1. <b>query</b> is only
  396. * passed for pretty log statements. */
  397. static int
  398. directory_get_from_hs_dir(const char *desc_id, const rend_data_t *rend_query)
  399. {
  400. smartlist_t *responsible_dirs = smartlist_create();
  401. routerstatus_t *hs_dir;
  402. char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  403. time_t now = time(NULL);
  404. char descriptor_cookie_base64[3*REND_DESC_COOKIE_LEN_BASE64];
  405. tor_assert(desc_id);
  406. tor_assert(rend_query);
  407. /* Determine responsible dirs. Even if we can't get all we want,
  408. * work with the ones we have. If it's empty, we'll notice below. */
  409. hid_serv_get_responsible_directories(responsible_dirs, desc_id);
  410. base32_encode(desc_id_base32, sizeof(desc_id_base32),
  411. desc_id, DIGEST_LEN);
  412. /* Only select those hidden service directories to which we did not send
  413. * a request recently and for which we have a router descriptor here. */
  414. /* Clean request history first. */
  415. directory_clean_last_hid_serv_requests(now);
  416. SMARTLIST_FOREACH(responsible_dirs, routerstatus_t *, dir, {
  417. if (lookup_last_hid_serv_request(dir, desc_id_base32, 0, 0) +
  418. REND_HID_SERV_DIR_REQUERY_PERIOD >= now ||
  419. !router_get_by_id_digest(dir->identity_digest))
  420. SMARTLIST_DEL_CURRENT(responsible_dirs, dir);
  421. });
  422. hs_dir = smartlist_choose(responsible_dirs);
  423. smartlist_free(responsible_dirs);
  424. if (!hs_dir) {
  425. log_info(LD_REND, "Could not pick one of the responsible hidden "
  426. "service directories, because we requested them all "
  427. "recently without success.");
  428. return 0;
  429. }
  430. /* Remember, that we are requesting a descriptor from this hidden service
  431. * directory now. */
  432. lookup_last_hid_serv_request(hs_dir, desc_id_base32, now, 1);
  433. /* Encode descriptor cookie for logging purposes. */
  434. if (rend_query->auth_type != REND_NO_AUTH) {
  435. if (base64_encode(descriptor_cookie_base64,
  436. sizeof(descriptor_cookie_base64),
  437. rend_query->descriptor_cookie, REND_DESC_COOKIE_LEN)<0) {
  438. log_warn(LD_BUG, "Could not base64-encode descriptor cookie.");
  439. return 0;
  440. }
  441. /* Remove == signs and newline. */
  442. descriptor_cookie_base64[strlen(descriptor_cookie_base64)-3] = '\0';
  443. } else {
  444. strlcpy(descriptor_cookie_base64, "(none)",
  445. sizeof(descriptor_cookie_base64));
  446. }
  447. /* Send fetch request. (Pass query and possibly descriptor cookie so that
  448. * they can be written to the directory connection and be referred to when
  449. * the response arrives. */
  450. directory_initiate_command_routerstatus_rend(hs_dir,
  451. DIR_PURPOSE_FETCH_RENDDESC_V2,
  452. ROUTER_PURPOSE_GENERAL,
  453. 1, desc_id_base32, NULL, 0, 0,
  454. rend_query);
  455. log_info(LD_REND, "Sending fetch request for v2 descriptor for "
  456. "service '%s' with descriptor ID '%s', auth type %d, "
  457. "and descriptor cookie '%s' to hidden service "
  458. "directory '%s' on port %d.",
  459. rend_query->onion_address, desc_id_base32,
  460. rend_query->auth_type,
  461. (rend_query->auth_type == REND_NO_AUTH ? "[none]" :
  462. escaped_safe_str_client(descriptor_cookie_base64)),
  463. hs_dir->nickname, hs_dir->dir_port);
  464. return 1;
  465. }
  466. /** Unless we already have a descriptor for <b>rend_query</b> with at least
  467. * one (possibly) working introduction point in it, start a connection to a
  468. * hidden service directory to fetch a v2 rendezvous service descriptor. */
  469. void
  470. rend_client_refetch_v2_renddesc(const rend_data_t *rend_query)
  471. {
  472. char descriptor_id[DIGEST_LEN];
  473. int replicas_left_to_try[REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS];
  474. int i, tries_left;
  475. rend_cache_entry_t *e = NULL;
  476. tor_assert(rend_query);
  477. /* Are we configured to fetch descriptors? */
  478. if (!get_options()->FetchHidServDescriptors) {
  479. log_warn(LD_REND, "We received an onion address for a v2 rendezvous "
  480. "service descriptor, but are not fetching service descriptors.");
  481. return;
  482. }
  483. /* Before fetching, check if we already have the descriptor here. */
  484. if (rend_cache_lookup_entry(rend_query->onion_address, -1, &e) > 0) {
  485. log_info(LD_REND, "We would fetch a v2 rendezvous descriptor, but we "
  486. "already have that descriptor here. Not fetching.");
  487. return;
  488. }
  489. log_debug(LD_REND, "Fetching v2 rendezvous descriptor for service %s",
  490. safe_str_client(rend_query->onion_address));
  491. /* Randomly iterate over the replicas until a descriptor can be fetched
  492. * from one of the consecutive nodes, or no options are left. */
  493. tries_left = REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS;
  494. for (i = 0; i < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; i++)
  495. replicas_left_to_try[i] = i;
  496. while (tries_left > 0) {
  497. int rand = crypto_rand_int(tries_left);
  498. int chosen_replica = replicas_left_to_try[rand];
  499. replicas_left_to_try[rand] = replicas_left_to_try[--tries_left];
  500. if (rend_compute_v2_desc_id(descriptor_id, rend_query->onion_address,
  501. rend_query->auth_type == REND_STEALTH_AUTH ?
  502. rend_query->descriptor_cookie : NULL,
  503. time(NULL), chosen_replica) < 0) {
  504. log_warn(LD_REND, "Internal error: Computing v2 rendezvous "
  505. "descriptor ID did not succeed.");
  506. return;
  507. }
  508. if (directory_get_from_hs_dir(descriptor_id, rend_query) != 0)
  509. return; /* either success or failure, but we're done */
  510. }
  511. /* If we come here, there are no hidden service directories left. */
  512. log_info(LD_REND, "Could not pick one of the responsible hidden "
  513. "service directories to fetch descriptors, because "
  514. "we already tried them all unsuccessfully.");
  515. /* Close pending connections. */
  516. rend_client_desc_trynow(rend_query->onion_address);
  517. return;
  518. }
  519. /** Cancel all rendezvous descriptor fetches currently in progress.
  520. */
  521. void
  522. rend_client_cancel_descriptor_fetches(void)
  523. {
  524. smartlist_t *connection_array = get_connection_array();
  525. SMARTLIST_FOREACH_BEGIN(connection_array, connection_t *, conn) {
  526. if (conn->type == CONN_TYPE_DIR &&
  527. (conn->purpose == DIR_PURPOSE_FETCH_RENDDESC ||
  528. conn->purpose == DIR_PURPOSE_FETCH_RENDDESC_V2)) {
  529. /* It's a rendezvous descriptor fetch in progress -- cancel it
  530. * by marking the connection for close.
  531. *
  532. * Even if this connection has already reached EOF, this is
  533. * enough to make sure that if the descriptor hasn't been
  534. * processed yet, it won't be. See the end of
  535. * connection_handle_read; connection_reached_eof (indirectly)
  536. * processes whatever response the connection received. */
  537. const rend_data_t *rd = (TO_DIR_CONN(conn))->rend_data;
  538. if (!rd) {
  539. log_warn(LD_BUG | LD_REND,
  540. "Marking for close dir conn fetching rendezvous "
  541. "descriptor for unknown service!");
  542. } else {
  543. log_debug(LD_REND, "Marking for close dir conn fetching "
  544. "rendezvous descriptor for service %s",
  545. safe_str(rd->onion_address));
  546. }
  547. connection_mark_for_close(conn);
  548. }
  549. } SMARTLIST_FOREACH_END(conn);
  550. }
  551. /** Remove failed_intro from ent. If ent now has no intro points, or
  552. * service is unrecognized, then launch a new renddesc fetch.
  553. *
  554. * Return -1 if error, 0 if no intro points remain or service
  555. * unrecognized, 1 if recognized and some intro points remain.
  556. */
  557. int
  558. rend_client_remove_intro_point(extend_info_t *failed_intro,
  559. const rend_data_t *rend_query)
  560. {
  561. int i, r;
  562. rend_cache_entry_t *ent;
  563. connection_t *conn;
  564. r = rend_cache_lookup_entry(rend_query->onion_address, -1, &ent);
  565. if (r<0) {
  566. log_warn(LD_BUG, "Malformed service ID %s.",
  567. escaped_safe_str_client(rend_query->onion_address));
  568. return -1;
  569. }
  570. if (r==0) {
  571. log_info(LD_REND, "Unknown service %s. Re-fetching descriptor.",
  572. escaped_safe_str_client(rend_query->onion_address));
  573. rend_client_refetch_v2_renddesc(rend_query);
  574. return 0;
  575. }
  576. for (i = 0; i < smartlist_len(ent->parsed->intro_nodes); i++) {
  577. rend_intro_point_t *intro = smartlist_get(ent->parsed->intro_nodes, i);
  578. if (tor_memeq(failed_intro->identity_digest,
  579. intro->extend_info->identity_digest, DIGEST_LEN)) {
  580. rend_intro_point_free(intro);
  581. smartlist_del(ent->parsed->intro_nodes, i);
  582. break;
  583. }
  584. }
  585. if (! rend_client_any_intro_points_usable(ent)) {
  586. log_info(LD_REND,
  587. "No more intro points remain for %s. Re-fetching descriptor.",
  588. escaped_safe_str_client(rend_query->onion_address));
  589. rend_client_refetch_v2_renddesc(rend_query);
  590. /* move all pending streams back to renddesc_wait */
  591. while ((conn = connection_get_by_type_state_rendquery(CONN_TYPE_AP,
  592. AP_CONN_STATE_CIRCUIT_WAIT,
  593. rend_query->onion_address))) {
  594. conn->state = AP_CONN_STATE_RENDDESC_WAIT;
  595. }
  596. return 0;
  597. }
  598. log_info(LD_REND,"%d options left for %s.",
  599. smartlist_len(ent->parsed->intro_nodes),
  600. escaped_safe_str_client(rend_query->onion_address));
  601. return 1;
  602. }
  603. /** Called when we receive a RENDEZVOUS_ESTABLISHED cell; changes the state of
  604. * the circuit to C_REND_READY.
  605. */
  606. int
  607. rend_client_rendezvous_acked(origin_circuit_t *circ, const uint8_t *request,
  608. size_t request_len)
  609. {
  610. (void) request;
  611. (void) request_len;
  612. /* we just got an ack for our establish-rendezvous. switch purposes. */
  613. if (circ->_base.purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
  614. log_warn(LD_PROTOCOL,"Got a rendezvous ack when we weren't expecting one. "
  615. "Closing circ.");
  616. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  617. return -1;
  618. }
  619. log_info(LD_REND,"Got rendezvous ack. This circuit is now ready for "
  620. "rendezvous.");
  621. circ->_base.purpose = CIRCUIT_PURPOSE_C_REND_READY;
  622. /* XXXX023 This is a pretty brute-force approach. It'd be better to
  623. * attach only the connections that are waiting on this circuit, rather
  624. * than trying to attach them all. See comments bug 743. */
  625. /* If we already have the introduction circuit built, make sure we send
  626. * the INTRODUCE cell _now_ */
  627. connection_ap_attach_pending();
  628. return 0;
  629. }
  630. /** Bob sent us a rendezvous cell; join the circuits. */
  631. int
  632. rend_client_receive_rendezvous(origin_circuit_t *circ, const uint8_t *request,
  633. size_t request_len)
  634. {
  635. crypt_path_t *hop;
  636. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
  637. if ((circ->_base.purpose != CIRCUIT_PURPOSE_C_REND_READY &&
  638. circ->_base.purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED)
  639. || !circ->build_state->pending_final_cpath) {
  640. log_warn(LD_PROTOCOL,"Got rendezvous2 cell from hidden service, but not "
  641. "expecting it. Closing.");
  642. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  643. return -1;
  644. }
  645. if (request_len != DH_KEY_LEN+DIGEST_LEN) {
  646. log_warn(LD_PROTOCOL,"Incorrect length (%d) on RENDEZVOUS2 cell.",
  647. (int)request_len);
  648. goto err;
  649. }
  650. log_info(LD_REND,"Got RENDEZVOUS2 cell from hidden service.");
  651. /* first DH_KEY_LEN bytes are g^y from bob. Finish the dh handshake...*/
  652. tor_assert(circ->build_state);
  653. tor_assert(circ->build_state->pending_final_cpath);
  654. hop = circ->build_state->pending_final_cpath;
  655. tor_assert(hop->dh_handshake_state);
  656. if (crypto_dh_compute_secret(LOG_PROTOCOL_WARN,
  657. hop->dh_handshake_state, (char*)request,
  658. DH_KEY_LEN,
  659. keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  660. log_warn(LD_GENERAL, "Couldn't complete DH handshake.");
  661. goto err;
  662. }
  663. /* ... and set up cpath. */
  664. if (circuit_init_cpath_crypto(hop, keys+DIGEST_LEN, 0)<0)
  665. goto err;
  666. /* Check whether the digest is right... */
  667. if (tor_memneq(keys, request+DH_KEY_LEN, DIGEST_LEN)) {
  668. log_warn(LD_PROTOCOL, "Incorrect digest of key material.");
  669. goto err;
  670. }
  671. crypto_dh_free(hop->dh_handshake_state);
  672. hop->dh_handshake_state = NULL;
  673. /* All is well. Extend the circuit. */
  674. circ->_base.purpose = CIRCUIT_PURPOSE_C_REND_JOINED;
  675. hop->state = CPATH_STATE_OPEN;
  676. /* set the windows to default. these are the windows
  677. * that alice thinks bob has.
  678. */
  679. hop->package_window = circuit_initial_package_window();
  680. hop->deliver_window = CIRCWINDOW_START;
  681. onion_append_to_cpath(&circ->cpath, hop);
  682. circ->build_state->pending_final_cpath = NULL; /* prevent double-free */
  683. /* XXXX023 This is a pretty brute-force approach. It'd be better to
  684. * attach only the connections that are waiting on this circuit, rather
  685. * than trying to attach them all. See comments bug 743. */
  686. connection_ap_attach_pending();
  687. memset(keys, 0, sizeof(keys));
  688. return 0;
  689. err:
  690. memset(keys, 0, sizeof(keys));
  691. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  692. return -1;
  693. }
  694. /** Find all the apconns in state AP_CONN_STATE_RENDDESC_WAIT that are
  695. * waiting on <b>query</b>. If there's a working cache entry here with at
  696. * least one intro point, move them to the next state. */
  697. void
  698. rend_client_desc_trynow(const char *query)
  699. {
  700. edge_connection_t *conn;
  701. rend_cache_entry_t *entry;
  702. time_t now = time(NULL);
  703. smartlist_t *conns = get_connection_array();
  704. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, _conn) {
  705. if (_conn->type != CONN_TYPE_AP ||
  706. _conn->state != AP_CONN_STATE_RENDDESC_WAIT ||
  707. _conn->marked_for_close)
  708. continue;
  709. conn = TO_EDGE_CONN(_conn);
  710. if (!conn->rend_data)
  711. continue;
  712. if (rend_cmp_service_ids(query, conn->rend_data->onion_address))
  713. continue;
  714. assert_connection_ok(TO_CONN(conn), now);
  715. if (rend_cache_lookup_entry(conn->rend_data->onion_address, -1,
  716. &entry) == 1 &&
  717. rend_client_any_intro_points_usable(entry)) {
  718. /* either this fetch worked, or it failed but there was a
  719. * valid entry from before which we should reuse */
  720. log_info(LD_REND,"Rend desc is usable. Launching circuits.");
  721. conn->_base.state = AP_CONN_STATE_CIRCUIT_WAIT;
  722. /* restart their timeout values, so they get a fair shake at
  723. * connecting to the hidden service. */
  724. conn->_base.timestamp_created = now;
  725. conn->_base.timestamp_lastread = now;
  726. conn->_base.timestamp_lastwritten = now;
  727. if (connection_ap_handshake_attach_circuit(conn) < 0) {
  728. /* it will never work */
  729. log_warn(LD_REND,"Rendezvous attempt failed. Closing.");
  730. if (!conn->_base.marked_for_close)
  731. connection_mark_unattached_ap(conn, END_STREAM_REASON_CANT_ATTACH);
  732. }
  733. } else { /* 404, or fetch didn't get that far */
  734. log_notice(LD_REND,"Closing stream for '%s.onion': hidden service is "
  735. "unavailable (try again later).",
  736. safe_str_client(query));
  737. connection_mark_unattached_ap(conn, END_STREAM_REASON_RESOLVEFAILED);
  738. }
  739. } SMARTLIST_FOREACH_END(_conn);
  740. }
  741. /** Return a newly allocated extend_info_t* for a randomly chosen introduction
  742. * point for the named hidden service. Return NULL if all introduction points
  743. * have been tried and failed.
  744. */
  745. extend_info_t *
  746. rend_client_get_random_intro(const rend_data_t *rend_query)
  747. {
  748. extend_info_t *result;
  749. rend_cache_entry_t *entry;
  750. if (rend_cache_lookup_entry(rend_query->onion_address, -1, &entry) < 1) {
  751. log_warn(LD_REND,
  752. "Query '%s' didn't have valid rend desc in cache. Failing.",
  753. safe_str_client(rend_query->onion_address));
  754. return NULL;
  755. }
  756. /* See if we can get a node that complies with ExcludeNodes */
  757. if ((result = rend_client_get_random_intro_impl(entry, 1, 1)))
  758. return result;
  759. /* If not, and StrictNodes is not set, see if we can return any old node
  760. */
  761. if (!get_options()->StrictNodes)
  762. return rend_client_get_random_intro_impl(entry, 0, 1);
  763. return NULL;
  764. }
  765. /** As rend_client_get_random_intro, except assume that StrictNodes is set
  766. * iff <b>strict</b> is true. If <b>warnings</b> is false, don't complain
  767. * to the user when we're out of nodes, even if StrictNodes is true.
  768. */
  769. static extend_info_t *
  770. rend_client_get_random_intro_impl(const rend_cache_entry_t *entry,
  771. const int strict,
  772. const int warnings)
  773. {
  774. int i;
  775. rend_intro_point_t *intro;
  776. or_options_t *options = get_options();
  777. smartlist_t *usable_nodes;
  778. int n_excluded = 0;
  779. /* We'll keep a separate list of the usable nodes. If this becomes empty,
  780. * no nodes are usable. */
  781. usable_nodes = smartlist_create();
  782. smartlist_add_all(usable_nodes, entry->parsed->intro_nodes);
  783. again:
  784. if (smartlist_len(usable_nodes) == 0) {
  785. if (n_excluded && get_options()->StrictNodes && warnings) {
  786. /* We only want to warn if StrictNodes is really set. Otherwise
  787. * we're just about to retry anyways.
  788. */
  789. log_warn(LD_REND, "All introduction points for hidden service are "
  790. "at excluded relays, and StrictNodes is set. Skipping.");
  791. }
  792. smartlist_free(usable_nodes);
  793. return NULL;
  794. }
  795. i = crypto_rand_int(smartlist_len(usable_nodes));
  796. intro = smartlist_get(usable_nodes, i);
  797. /* Do we need to look up the router or is the extend info complete? */
  798. if (!intro->extend_info->onion_key) {
  799. const node_t *node;
  800. if (tor_digest_is_zero(intro->extend_info->identity_digest))
  801. node = node_get_by_hex_id(intro->extend_info->nickname);
  802. else
  803. node = node_get_by_id(intro->extend_info->identity_digest);
  804. if (!node) {
  805. log_info(LD_REND, "Unknown router with nickname '%s'; trying another.",
  806. intro->extend_info->nickname);
  807. smartlist_del(usable_nodes, i);
  808. goto again;
  809. }
  810. extend_info_free(intro->extend_info);
  811. intro->extend_info = extend_info_from_node(node);
  812. }
  813. /* Check if we should refuse to talk to this router. */
  814. if (options->ExcludeNodes && strict &&
  815. routerset_contains_extendinfo(options->ExcludeNodes,
  816. intro->extend_info)) {
  817. n_excluded++;
  818. smartlist_del(usable_nodes, i);
  819. goto again;
  820. }
  821. smartlist_free(usable_nodes);
  822. return extend_info_dup(intro->extend_info);
  823. }
  824. /** Return true iff any introduction points still listed in <b>entry</b> are
  825. * usable. */
  826. int
  827. rend_client_any_intro_points_usable(const rend_cache_entry_t *entry)
  828. {
  829. return rend_client_get_random_intro_impl(
  830. entry, get_options()->StrictNodes, 0) != NULL;
  831. }
  832. /** Client-side authorizations for hidden services; map of onion address to
  833. * rend_service_authorization_t*. */
  834. static strmap_t *auth_hid_servs = NULL;
  835. /** Look up the client-side authorization for the hidden service with
  836. * <b>onion_address</b>. Return NULL if no authorization is available for
  837. * that address. */
  838. rend_service_authorization_t*
  839. rend_client_lookup_service_authorization(const char *onion_address)
  840. {
  841. tor_assert(onion_address);
  842. if (!auth_hid_servs) return NULL;
  843. return strmap_get(auth_hid_servs, onion_address);
  844. }
  845. /** Helper: Free storage held by rend_service_authorization_t. */
  846. static void
  847. rend_service_authorization_free(rend_service_authorization_t *auth)
  848. {
  849. tor_free(auth);
  850. }
  851. /** Helper for strmap_free. */
  852. static void
  853. rend_service_authorization_strmap_item_free(void *service_auth)
  854. {
  855. rend_service_authorization_free(service_auth);
  856. }
  857. /** Release all the storage held in auth_hid_servs.
  858. */
  859. void
  860. rend_service_authorization_free_all(void)
  861. {
  862. if (!auth_hid_servs) {
  863. return;
  864. }
  865. strmap_free(auth_hid_servs, rend_service_authorization_strmap_item_free);
  866. auth_hid_servs = NULL;
  867. }
  868. /** Parse <b>config_line</b> as a client-side authorization for a hidden
  869. * service and add it to the local map of hidden service authorizations.
  870. * Return 0 for success and -1 for failure. */
  871. int
  872. rend_parse_service_authorization(or_options_t *options, int validate_only)
  873. {
  874. config_line_t *line;
  875. int res = -1;
  876. strmap_t *parsed = strmap_new();
  877. smartlist_t *sl = smartlist_create();
  878. rend_service_authorization_t *auth = NULL;
  879. for (line = options->HidServAuth; line; line = line->next) {
  880. char *onion_address, *descriptor_cookie;
  881. char descriptor_cookie_tmp[REND_DESC_COOKIE_LEN+2];
  882. char descriptor_cookie_base64ext[REND_DESC_COOKIE_LEN_BASE64+2+1];
  883. int auth_type_val = 0;
  884. auth = NULL;
  885. SMARTLIST_FOREACH(sl, char *, c, tor_free(c););
  886. smartlist_clear(sl);
  887. smartlist_split_string(sl, line->value, " ",
  888. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 3);
  889. if (smartlist_len(sl) < 2) {
  890. log_warn(LD_CONFIG, "Configuration line does not consist of "
  891. "\"onion-address authorization-cookie [service-name]\": "
  892. "'%s'", line->value);
  893. goto err;
  894. }
  895. auth = tor_malloc_zero(sizeof(rend_service_authorization_t));
  896. /* Parse onion address. */
  897. onion_address = smartlist_get(sl, 0);
  898. if (strlen(onion_address) != REND_SERVICE_ADDRESS_LEN ||
  899. strcmpend(onion_address, ".onion")) {
  900. log_warn(LD_CONFIG, "Onion address has wrong format: '%s'",
  901. onion_address);
  902. goto err;
  903. }
  904. strlcpy(auth->onion_address, onion_address, REND_SERVICE_ID_LEN_BASE32+1);
  905. if (!rend_valid_service_id(auth->onion_address)) {
  906. log_warn(LD_CONFIG, "Onion address has wrong format: '%s'",
  907. onion_address);
  908. goto err;
  909. }
  910. /* Parse descriptor cookie. */
  911. descriptor_cookie = smartlist_get(sl, 1);
  912. if (strlen(descriptor_cookie) != REND_DESC_COOKIE_LEN_BASE64) {
  913. log_warn(LD_CONFIG, "Authorization cookie has wrong length: '%s'",
  914. descriptor_cookie);
  915. goto err;
  916. }
  917. /* Add trailing zero bytes (AA) to make base64-decoding happy. */
  918. tor_snprintf(descriptor_cookie_base64ext,
  919. REND_DESC_COOKIE_LEN_BASE64+2+1,
  920. "%sAA", descriptor_cookie);
  921. if (base64_decode(descriptor_cookie_tmp, sizeof(descriptor_cookie_tmp),
  922. descriptor_cookie_base64ext,
  923. strlen(descriptor_cookie_base64ext)) < 0) {
  924. log_warn(LD_CONFIG, "Decoding authorization cookie failed: '%s'",
  925. descriptor_cookie);
  926. goto err;
  927. }
  928. auth_type_val = (descriptor_cookie_tmp[16] >> 4) + 1;
  929. if (auth_type_val < 1 || auth_type_val > 2) {
  930. log_warn(LD_CONFIG, "Authorization cookie has unknown authorization "
  931. "type encoded.");
  932. goto err;
  933. }
  934. auth->auth_type = auth_type_val == 1 ? REND_BASIC_AUTH : REND_STEALTH_AUTH;
  935. memcpy(auth->descriptor_cookie, descriptor_cookie_tmp,
  936. REND_DESC_COOKIE_LEN);
  937. if (strmap_get(parsed, auth->onion_address)) {
  938. log_warn(LD_CONFIG, "Duplicate authorization for the same hidden "
  939. "service.");
  940. goto err;
  941. }
  942. strmap_set(parsed, auth->onion_address, auth);
  943. auth = NULL;
  944. }
  945. res = 0;
  946. goto done;
  947. err:
  948. res = -1;
  949. done:
  950. rend_service_authorization_free(auth);
  951. SMARTLIST_FOREACH(sl, char *, c, tor_free(c););
  952. smartlist_free(sl);
  953. if (!validate_only && res == 0) {
  954. rend_service_authorization_free_all();
  955. auth_hid_servs = parsed;
  956. } else {
  957. strmap_free(parsed, rend_service_authorization_strmap_item_free);
  958. }
  959. return res;
  960. }