1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2009, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
7 * \brief The hidden-service side of rendezvous functionality.
12 static origin_circuit_t
*find_intro_circuit(rend_intro_point_t
*intro
,
13 const char *pk_digest
);
15 /** Represents the mapping from a virtual port of a rendezvous service to
16 * a real port on some IP.
18 typedef struct rend_service_port_config_t
{
19 uint16_t virtual_port
;
22 } rend_service_port_config_t
;
24 /** Try to maintain this many intro points per service if possible. */
25 #define NUM_INTRO_POINTS 3
27 /** If we can't build our intro circuits, don't retry for this long. */
28 #define INTRO_CIRC_RETRY_PERIOD (60*5)
29 /** Don't try to build more than this many circuits before giving up
31 #define MAX_INTRO_CIRCS_PER_PERIOD 10
32 /** How many times will a hidden service operator attempt to connect to
33 * a requested rendezvous point before giving up? */
34 #define MAX_REND_FAILURES 30
35 /** How many seconds should we spend trying to connect to a requested
36 * rendezvous point before giving up? */
37 #define MAX_REND_TIMEOUT 30
39 /** Represents a single hidden service running at this OP. */
40 typedef struct rend_service_t
{
41 /* Fields specified in config file */
42 char *directory
; /**< where in the filesystem it stores it */
43 smartlist_t
*ports
; /**< List of rend_service_port_config_t */
44 rend_auth_type_t auth_type
; /**< Client authorization type or 0 if no client
45 * authorization is performed. */
46 smartlist_t
*clients
; /**< List of rend_authorized_client_t's of
47 * clients that may access our service. Can be NULL
48 * if no client authorization is performed. */
50 crypto_pk_env_t
*private_key
; /**< Permanent hidden-service key. */
51 char service_id
[REND_SERVICE_ID_LEN_BASE32
+1]; /**< Onion address without
53 char pk_digest
[DIGEST_LEN
]; /**< Hash of permanent hidden-service key. */
54 smartlist_t
*intro_nodes
; /**< List of rend_intro_point_t's we have,
55 * or are trying to establish. */
56 time_t intro_period_started
; /**< Start of the current period to build
57 * introduction points. */
58 int n_intro_circuits_launched
; /**< Count of intro circuits we have
59 * established in this period. */
60 rend_service_descriptor_t
*desc
; /**< Current hidden service descriptor. */
61 time_t desc_is_dirty
; /**< Time at which changes to the hidden service
62 * descriptor content occurred, or 0 if it's
64 time_t next_upload_time
; /**< Scheduled next hidden service descriptor
66 /** Map from digests of Diffie-Hellman values INTRODUCE2 to time_t of when
67 * they were received; used to prevent replays. */
68 digestmap_t
*accepted_intros
;
69 /** Time at which we last removed expired values from accepted_intros. */
70 time_t last_cleaned_accepted_intros
;
73 /** A list of rend_service_t's for services run on this OP.
75 static smartlist_t
*rend_service_list
= NULL
;
77 /** Return the number of rendezvous services we have configured. */
79 num_rend_services(void)
81 if (!rend_service_list
)
83 return smartlist_len(rend_service_list
);
86 /** Helper: free storage held by a single service authorized client entry. */
88 rend_authorized_client_free(rend_authorized_client_t
*client
)
91 if (client
->client_key
)
92 crypto_free_pk_env(client
->client_key
);
93 tor_free(client
->client_name
);
97 /** Helper for strmap_free. */
99 rend_authorized_client_strmap_item_free(void *authorized_client
)
101 rend_authorized_client_free(authorized_client
);
104 /** Release the storage held by <b>service</b>.
107 rend_service_free(rend_service_t
*service
)
109 if (!service
) return;
110 tor_free(service
->directory
);
111 SMARTLIST_FOREACH(service
->ports
, void*, p
, tor_free(p
));
112 smartlist_free(service
->ports
);
113 if (service
->private_key
)
114 crypto_free_pk_env(service
->private_key
);
115 if (service
->intro_nodes
) {
116 SMARTLIST_FOREACH(service
->intro_nodes
, rend_intro_point_t
*, intro
,
117 rend_intro_point_free(intro
););
118 smartlist_free(service
->intro_nodes
);
121 rend_service_descriptor_free(service
->desc
);
122 if (service
->clients
) {
123 SMARTLIST_FOREACH(service
->clients
, rend_authorized_client_t
*, c
,
124 rend_authorized_client_free(c
););
125 smartlist_free(service
->clients
);
127 if (service
->accepted_intros
)
128 digestmap_free(service
->accepted_intros
, _tor_free
);
132 /** Release all the storage held in rend_service_list.
135 rend_service_free_all(void)
137 if (!rend_service_list
) {
140 SMARTLIST_FOREACH(rend_service_list
, rend_service_t
*, ptr
,
141 rend_service_free(ptr
));
142 smartlist_free(rend_service_list
);
143 rend_service_list
= NULL
;
146 /** Validate <b>service</b> and add it to rend_service_list if possible.
149 rend_add_service(rend_service_t
*service
)
152 rend_service_port_config_t
*p
;
154 service
->intro_nodes
= smartlist_create();
156 if (service
->auth_type
!= REND_NO_AUTH
&&
157 smartlist_len(service
->clients
) == 0) {
158 log_warn(LD_CONFIG
, "Hidden service with client authorization but no "
159 "clients; ignoring.");
160 rend_service_free(service
);
164 if (!smartlist_len(service
->ports
)) {
165 log_warn(LD_CONFIG
, "Hidden service with no ports configured; ignoring.");
166 rend_service_free(service
);
168 smartlist_add(rend_service_list
, service
);
169 log_debug(LD_REND
,"Configuring service with directory \"%s\"",
171 for (i
= 0; i
< smartlist_len(service
->ports
); ++i
) {
172 p
= smartlist_get(service
->ports
, i
);
173 log_debug(LD_REND
,"Service maps port %d to %s:%d",
174 p
->virtual_port
, fmt_addr(&p
->real_addr
), p
->real_port
);
179 /** Parses a real-port to virtual-port mapping and returns a new
180 * rend_service_port_config_t.
182 * The format is: VirtualPort (IP|RealPort|IP:RealPort)?
184 * IP defaults to 127.0.0.1; RealPort defaults to VirtualPort.
186 static rend_service_port_config_t
*
187 parse_port_config(const char *string
)
194 const char *addrport
;
195 rend_service_port_config_t
*result
= NULL
;
197 sl
= smartlist_create();
198 smartlist_split_string(sl
, string
, " ",
199 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
200 if (smartlist_len(sl
) < 1 || smartlist_len(sl
) > 2) {
201 log_warn(LD_CONFIG
, "Bad syntax in hidden service port configuration.");
205 virtport
= (int)tor_parse_long(smartlist_get(sl
,0), 10, 1, 65535, NULL
,NULL
);
207 log_warn(LD_CONFIG
, "Missing or invalid port %s in hidden service port "
208 "configuration", escaped(smartlist_get(sl
,0)));
212 if (smartlist_len(sl
) == 1) {
213 /* No addr:port part; use default. */
215 tor_addr_from_ipv4h(&addr
, 0x7F000001u
); /* 127.0.0.1 */
217 addrport
= smartlist_get(sl
,1);
218 if (strchr(addrport
, ':') || strchr(addrport
, '.')) {
219 if (tor_addr_port_parse(addrport
, &addr
, &p
)<0) {
220 log_warn(LD_CONFIG
,"Unparseable address in hidden service port "
224 realport
= p
?p
:virtport
;
226 /* No addr:port, no addr -- must be port. */
227 realport
= (int)tor_parse_long(addrport
, 10, 1, 65535, NULL
, NULL
);
229 log_warn(LD_CONFIG
,"Unparseable or out-of-range port %s in hidden "
230 "service port configuration.", escaped(addrport
));
233 tor_addr_from_ipv4h(&addr
, 0x7F000001u
); /* Default to 127.0.0.1 */
237 result
= tor_malloc(sizeof(rend_service_port_config_t
));
238 result
->virtual_port
= virtport
;
239 result
->real_port
= realport
;
240 tor_addr_copy(&result
->real_addr
, &addr
);
242 SMARTLIST_FOREACH(sl
, char *, c
, tor_free(c
));
247 /** Set up rend_service_list, based on the values of HiddenServiceDir and
248 * HiddenServicePort in <b>options</b>. Return 0 on success and -1 on
249 * failure. (If <b>validate_only</b> is set, parse, warn and return as
250 * normal, but don't actually change the configured services.)
253 rend_config_services(or_options_t
*options
, int validate_only
)
256 rend_service_t
*service
= NULL
;
257 rend_service_port_config_t
*portcfg
;
258 smartlist_t
*old_service_list
= NULL
;
260 if (!validate_only
) {
261 old_service_list
= rend_service_list
;
262 rend_service_list
= smartlist_create();
265 for (line
= options
->RendConfigLines
; line
; line
= line
->next
) {
266 if (!strcasecmp(line
->key
, "HiddenServiceDir")) {
269 rend_service_free(service
);
271 rend_add_service(service
);
273 service
= tor_malloc_zero(sizeof(rend_service_t
));
274 service
->directory
= tor_strdup(line
->value
);
275 service
->ports
= smartlist_create();
276 service
->intro_period_started
= time(NULL
);
280 log_warn(LD_CONFIG
, "%s with no preceding HiddenServiceDir directive",
282 rend_service_free(service
);
285 if (!strcasecmp(line
->key
, "HiddenServicePort")) {
286 portcfg
= parse_port_config(line
->value
);
288 rend_service_free(service
);
291 smartlist_add(service
->ports
, portcfg
);
292 } else if (!strcasecmp(line
->key
, "HiddenServiceAuthorizeClient")) {
293 /* Parse auth type and comma-separated list of client names and add a
294 * rend_authorized_client_t for each client to the service's list
295 * of authorized clients. */
296 smartlist_t
*type_names_split
, *clients
;
297 const char *authname
;
299 if (service
->auth_type
!= REND_NO_AUTH
) {
300 log_warn(LD_CONFIG
, "Got multiple HiddenServiceAuthorizeClient "
301 "lines for a single service.");
302 rend_service_free(service
);
305 type_names_split
= smartlist_create();
306 smartlist_split_string(type_names_split
, line
->value
, " ", 0, 2);
307 if (smartlist_len(type_names_split
) < 1) {
308 log_warn(LD_BUG
, "HiddenServiceAuthorizeClient has no value. This "
309 "should have been prevented when parsing the "
311 smartlist_free(type_names_split
);
312 rend_service_free(service
);
315 authname
= smartlist_get(type_names_split
, 0);
316 if (!strcasecmp(authname
, "basic")) {
317 service
->auth_type
= REND_BASIC_AUTH
;
318 } else if (!strcasecmp(authname
, "stealth")) {
319 service
->auth_type
= REND_STEALTH_AUTH
;
321 log_warn(LD_CONFIG
, "HiddenServiceAuthorizeClient contains "
322 "unrecognized auth-type '%s'. Only 'basic' or 'stealth' "
324 (char *) smartlist_get(type_names_split
, 0));
325 SMARTLIST_FOREACH(type_names_split
, char *, cp
, tor_free(cp
));
326 smartlist_free(type_names_split
);
327 rend_service_free(service
);
330 service
->clients
= smartlist_create();
331 if (smartlist_len(type_names_split
) < 2) {
332 log_warn(LD_CONFIG
, "HiddenServiceAuthorizeClient contains "
333 "auth-type '%s', but no client names.",
334 service
->auth_type
== REND_BASIC_AUTH
? "basic" : "stealth");
335 SMARTLIST_FOREACH(type_names_split
, char *, cp
, tor_free(cp
));
336 smartlist_free(type_names_split
);
339 clients
= smartlist_create();
340 smartlist_split_string(clients
, smartlist_get(type_names_split
, 1),
341 ",", SPLIT_SKIP_SPACE
, 0);
342 SMARTLIST_FOREACH(type_names_split
, char *, cp
, tor_free(cp
));
343 smartlist_free(type_names_split
);
344 /* Remove duplicate client names. */
345 num_clients
= smartlist_len(clients
);
346 smartlist_sort_strings(clients
);
347 smartlist_uniq_strings(clients
);
348 if (smartlist_len(clients
) < num_clients
) {
349 log_info(LD_CONFIG
, "HiddenServiceAuthorizeClient contains %d "
350 "duplicate client name(s); removing.",
351 num_clients
- smartlist_len(clients
));
352 num_clients
= smartlist_len(clients
);
354 SMARTLIST_FOREACH_BEGIN(clients
, const char *, client_name
)
356 rend_authorized_client_t
*client
;
357 size_t len
= strlen(client_name
);
358 if (len
< 1 || len
> REND_CLIENTNAME_MAX_LEN
) {
359 log_warn(LD_CONFIG
, "HiddenServiceAuthorizeClient contains an "
360 "illegal client name: '%s'. Length must be "
361 "between 1 and %d characters.",
362 client_name
, REND_CLIENTNAME_MAX_LEN
);
363 SMARTLIST_FOREACH(clients
, char *, cp
, tor_free(cp
));
364 smartlist_free(clients
);
365 rend_service_free(service
);
368 if (strspn(client_name
, REND_LEGAL_CLIENTNAME_CHARACTERS
) != len
) {
369 log_warn(LD_CONFIG
, "HiddenServiceAuthorizeClient contains an "
370 "illegal client name: '%s'. Valid "
371 "characters are [A-Za-z0-9+-_].",
373 SMARTLIST_FOREACH(clients
, char *, cp
, tor_free(cp
));
374 smartlist_free(clients
);
375 rend_service_free(service
);
378 client
= tor_malloc_zero(sizeof(rend_authorized_client_t
));
379 client
->client_name
= tor_strdup(client_name
);
380 smartlist_add(service
->clients
, client
);
381 log_debug(LD_REND
, "Adding client name '%s'", client_name
);
383 SMARTLIST_FOREACH_END(client_name
);
384 SMARTLIST_FOREACH(clients
, char *, cp
, tor_free(cp
));
385 smartlist_free(clients
);
386 /* Ensure maximum number of clients. */
387 if ((service
->auth_type
== REND_BASIC_AUTH
&&
388 smartlist_len(service
->clients
) > 512) ||
389 (service
->auth_type
== REND_STEALTH_AUTH
&&
390 smartlist_len(service
->clients
) > 16)) {
391 log_warn(LD_CONFIG
, "HiddenServiceAuthorizeClient contains %d "
392 "client authorization entries, but only a "
393 "maximum of %d entries is allowed for "
394 "authorization type '%s'.",
395 smartlist_len(service
->clients
),
396 service
->auth_type
== REND_BASIC_AUTH
? 512 : 16,
397 service
->auth_type
== REND_BASIC_AUTH
? "basic" : "stealth");
398 rend_service_free(service
);
402 tor_assert(!strcasecmp(line
->key
, "HiddenServiceVersion"));
403 if (strcmp(line
->value
, "2")) {
405 "The only supported HiddenServiceVersion is 2.");
406 rend_service_free(service
);
413 rend_service_free(service
);
415 rend_add_service(service
);
418 /* If this is a reload and there were hidden services configured before,
419 * keep the introduction points that are still needed and close the
421 if (old_service_list
&& !validate_only
) {
422 smartlist_t
*surviving_services
= smartlist_create();
425 /* Copy introduction points to new services. */
426 /* XXXX This is O(n^2), but it's only called on reconfigure, so it's
428 SMARTLIST_FOREACH(rend_service_list
, rend_service_t
*, new, {
429 SMARTLIST_FOREACH(old_service_list
, rend_service_t
*, old
, {
430 if (!strcmp(old
->directory
, new->directory
)) {
431 smartlist_add_all(new->intro_nodes
, old
->intro_nodes
);
432 smartlist_clear(old
->intro_nodes
);
433 smartlist_add(surviving_services
, old
);
439 /* Close introduction circuits of services we don't serve anymore. */
440 /* XXXX it would be nicer if we had a nicer abstraction to use here,
441 * so we could just iterate over the list of services to close, but
442 * once again, this isn't critical-path code. */
443 for (circ
= _circuit_get_global_list(); circ
; circ
= circ
->next
) {
444 if (!circ
->marked_for_close
&&
445 circ
->state
== CIRCUIT_STATE_OPEN
&&
446 (circ
->purpose
== CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
||
447 circ
->purpose
== CIRCUIT_PURPOSE_S_INTRO
)) {
448 origin_circuit_t
*oc
= TO_ORIGIN_CIRCUIT(circ
);
450 tor_assert(oc
->rend_data
);
451 SMARTLIST_FOREACH(surviving_services
, rend_service_t
*, ptr
, {
452 if (!memcmp(ptr
->pk_digest
, oc
->rend_data
->rend_pk_digest
,
460 log_info(LD_REND
, "Closing intro point %s for service %s.",
461 safe_str(oc
->build_state
->chosen_exit
->nickname
),
462 oc
->rend_data
->onion_address
);
463 circuit_mark_for_close(circ
, END_CIRC_REASON_FINISHED
);
464 /* XXXX Is there another reason we should use here? */
467 smartlist_free(surviving_services
);
468 SMARTLIST_FOREACH(old_service_list
, rend_service_t
*, ptr
,
469 rend_service_free(ptr
));
470 smartlist_free(old_service_list
);
476 /** Replace the old value of <b>service</b>-\>desc with one that reflects
477 * the other fields in service.
480 rend_service_update_descriptor(rend_service_t
*service
)
482 rend_service_descriptor_t
*d
;
483 origin_circuit_t
*circ
;
486 rend_service_descriptor_free(service
->desc
);
487 service
->desc
= NULL
;
489 d
= service
->desc
= tor_malloc_zero(sizeof(rend_service_descriptor_t
));
490 d
->pk
= crypto_pk_dup_key(service
->private_key
);
491 d
->timestamp
= time(NULL
);
492 d
->intro_nodes
= smartlist_create();
493 /* Support intro protocols 2 and 3. */
494 d
->protocols
= (1 << 2) + (1 << 3);
496 for (i
= 0; i
< smartlist_len(service
->intro_nodes
); ++i
) {
497 rend_intro_point_t
*intro_svc
= smartlist_get(service
->intro_nodes
, i
);
498 rend_intro_point_t
*intro_desc
;
499 circ
= find_intro_circuit(intro_svc
, service
->pk_digest
);
500 if (!circ
|| circ
->_base
.purpose
!= CIRCUIT_PURPOSE_S_INTRO
)
503 /* We have an entirely established intro circuit. */
504 intro_desc
= tor_malloc_zero(sizeof(rend_intro_point_t
));
505 intro_desc
->extend_info
= extend_info_dup(intro_svc
->extend_info
);
506 if (intro_svc
->intro_key
)
507 intro_desc
->intro_key
= crypto_pk_dup_key(intro_svc
->intro_key
);
508 smartlist_add(d
->intro_nodes
, intro_desc
);
512 /** Load and/or generate private keys for all hidden services, possibly
513 * including keys for client authorization. Return 0 on success, -1 on
517 rend_service_load_keys(void)
523 SMARTLIST_FOREACH_BEGIN(rend_service_list
, rend_service_t
*, s
) {
526 log_info(LD_REND
, "Loading hidden-service keys from \"%s\"",
529 /* Check/create directory */
530 if (check_private_dir(s
->directory
, CPD_CREATE
) < 0)
534 if (strlcpy(fname
,s
->directory
,sizeof(fname
)) >= sizeof(fname
) ||
535 strlcat(fname
,PATH_SEPARATOR
"private_key",sizeof(fname
))
537 log_warn(LD_CONFIG
, "Directory name too long to store key file: \"%s\".",
541 s
->private_key
= init_key_from_file(fname
, 1, LOG_ERR
);
545 /* Create service file */
546 if (rend_get_service_id(s
->private_key
, s
->service_id
)<0) {
547 log_warn(LD_BUG
, "Internal error: couldn't encode service ID.");
550 if (crypto_pk_get_digest(s
->private_key
, s
->pk_digest
)<0) {
551 log_warn(LD_BUG
, "Couldn't compute hash of public key.");
554 if (strlcpy(fname
,s
->directory
,sizeof(fname
)) >= sizeof(fname
) ||
555 strlcat(fname
,PATH_SEPARATOR
"hostname",sizeof(fname
))
557 log_warn(LD_CONFIG
, "Directory name too long to store hostname file:"
558 " \"%s\".", s
->directory
);
561 tor_snprintf(buf
, sizeof(buf
),"%s.onion\n", s
->service_id
);
562 if (write_str_to_file(fname
,buf
,0)<0) {
563 log_warn(LD_CONFIG
, "Could not write onion address to hostname file.");
567 /* If client authorization is configured, load or generate keys. */
568 if (s
->auth_type
!= REND_NO_AUTH
) {
569 char *client_keys_str
= NULL
;
570 strmap_t
*parsed_clients
= strmap_new();
573 open_file_t
*open_cfile
= NULL
, *open_hfile
= NULL
;
575 /* Load client keys and descriptor cookies, if available. */
576 if (tor_snprintf(cfname
, sizeof(cfname
), "%s"PATH_SEPARATOR
"client_keys",
578 log_warn(LD_CONFIG
, "Directory name too long to store client keys "
579 "file: \"%s\".", s
->directory
);
582 client_keys_str
= read_file_to_str(cfname
, RFTS_IGNORE_MISSING
, NULL
);
583 if (client_keys_str
) {
584 if (rend_parse_client_keys(parsed_clients
, client_keys_str
) < 0) {
585 log_warn(LD_CONFIG
, "Previously stored client_keys file could not "
589 log_info(LD_CONFIG
, "Parsed %d previously stored client entries.",
590 strmap_size(parsed_clients
));
591 tor_free(client_keys_str
);
595 /* Prepare client_keys and hostname files. */
596 if (!(cfile
= start_writing_to_stdio_file(cfname
, OPEN_FLAGS_REPLACE
,
597 0600, &open_cfile
))) {
598 log_warn(LD_CONFIG
, "Could not open client_keys file %s",
602 if (!(hfile
= start_writing_to_stdio_file(fname
, OPEN_FLAGS_REPLACE
,
603 0600, &open_hfile
))) {
604 log_warn(LD_CONFIG
, "Could not open hostname file %s", escaped(fname
));
608 /* Either use loaded keys for configured clients or generate new
609 * ones if a client is new. */
610 SMARTLIST_FOREACH_BEGIN(s
->clients
, rend_authorized_client_t
*, client
)
612 char desc_cook_out
[3*REND_DESC_COOKIE_LEN_BASE64
+1];
613 char service_id
[16+1];
614 rend_authorized_client_t
*parsed
=
615 strmap_get(parsed_clients
, client
->client_name
);
618 /* Copy descriptor cookie from parsed entry or create new one. */
620 memcpy(client
->descriptor_cookie
, parsed
->descriptor_cookie
,
621 REND_DESC_COOKIE_LEN
);
623 crypto_rand(client
->descriptor_cookie
, REND_DESC_COOKIE_LEN
);
625 if (base64_encode(desc_cook_out
, 3*REND_DESC_COOKIE_LEN_BASE64
+1,
626 client
->descriptor_cookie
,
627 REND_DESC_COOKIE_LEN
) < 0) {
628 log_warn(LD_BUG
, "Could not base64-encode descriptor cookie.");
629 strmap_free(parsed_clients
, rend_authorized_client_strmap_item_free
);
632 /* Copy client key from parsed entry or create new one if required. */
633 if (parsed
&& parsed
->client_key
) {
634 client
->client_key
= crypto_pk_dup_key(parsed
->client_key
);
635 } else if (s
->auth_type
== REND_STEALTH_AUTH
) {
636 /* Create private key for client. */
637 crypto_pk_env_t
*prkey
= NULL
;
638 if (!(prkey
= crypto_new_pk_env())) {
639 log_warn(LD_BUG
,"Error constructing client key");
642 if (crypto_pk_generate_key(prkey
)) {
643 log_warn(LD_BUG
,"Error generating client key");
644 crypto_free_pk_env(prkey
);
647 if (crypto_pk_check_key(prkey
) <= 0) {
648 log_warn(LD_BUG
,"Generated client key seems invalid");
649 crypto_free_pk_env(prkey
);
652 client
->client_key
= prkey
;
654 /* Add entry to client_keys file. */
655 desc_cook_out
[strlen(desc_cook_out
)-1] = '\0'; /* Remove newline. */
656 written
= tor_snprintf(buf
, sizeof(buf
),
657 "client-name %s\ndescriptor-cookie %s\n",
658 client
->client_name
, desc_cook_out
);
660 log_warn(LD_BUG
, "Could not write client entry.");
663 if (client
->client_key
) {
664 char *client_key_out
= NULL
;
665 crypto_pk_write_private_key_to_string(client
->client_key
,
666 &client_key_out
, &len
);
667 if (rend_get_service_id(client
->client_key
, service_id
)<0) {
668 log_warn(LD_BUG
, "Internal error: couldn't encode service ID.");
669 tor_free(client_key_out
);
672 written
= tor_snprintf(buf
+ written
, sizeof(buf
) - written
,
673 "client-key\n%s", client_key_out
);
674 tor_free(client_key_out
);
676 log_warn(LD_BUG
, "Could not write client entry.");
681 if (fputs(buf
, cfile
) < 0) {
682 log_warn(LD_FS
, "Could not append client entry to file: %s",
687 /* Add line to hostname file. */
688 if (s
->auth_type
== REND_BASIC_AUTH
) {
689 /* Remove == signs (newline has been removed above). */
690 desc_cook_out
[strlen(desc_cook_out
)-2] = '\0';
691 tor_snprintf(buf
, sizeof(buf
),"%s.onion %s # client: %s\n",
692 s
->service_id
, desc_cook_out
, client
->client_name
);
694 char extended_desc_cookie
[REND_DESC_COOKIE_LEN
+1];
695 memcpy(extended_desc_cookie
, client
->descriptor_cookie
,
696 REND_DESC_COOKIE_LEN
);
697 extended_desc_cookie
[REND_DESC_COOKIE_LEN
] =
698 ((int)s
->auth_type
- 1) << 4;
699 if (base64_encode(desc_cook_out
, 3*REND_DESC_COOKIE_LEN_BASE64
+1,
700 extended_desc_cookie
,
701 REND_DESC_COOKIE_LEN
+1) < 0) {
702 log_warn(LD_BUG
, "Could not base64-encode descriptor cookie.");
705 desc_cook_out
[strlen(desc_cook_out
)-3] = '\0'; /* Remove A= and
707 tor_snprintf(buf
, sizeof(buf
),"%s.onion %s # client: %s\n",
708 service_id
, desc_cook_out
, client
->client_name
);
711 if (fputs(buf
, hfile
)<0) {
712 log_warn(LD_FS
, "Could not append host entry to file: %s",
717 SMARTLIST_FOREACH_END(client
);
723 tor_free(client_keys_str
);
724 strmap_free(parsed_clients
, rend_authorized_client_strmap_item_free
);
727 abort_writing_to_file(open_cfile
);
729 abort_writing_to_file(open_hfile
);
732 finish_writing_to_file(open_cfile
);
733 finish_writing_to_file(open_hfile
);
736 } SMARTLIST_FOREACH_END(s
);
740 /** Return the service whose public key has a digest of <b>digest</b>, or
741 * NULL if no such service exists.
743 static rend_service_t
*
744 rend_service_get_by_pk_digest(const char* digest
)
746 SMARTLIST_FOREACH(rend_service_list
, rend_service_t
*, s
,
747 if (!memcmp(s
->pk_digest
,digest
,DIGEST_LEN
))
752 /** Return 1 if any virtual port in <b>service</b> wants a circuit
753 * to have good uptime. Else return 0.
756 rend_service_requires_uptime(rend_service_t
*service
)
759 rend_service_port_config_t
*p
;
761 for (i
=0; i
< smartlist_len(service
->ports
); ++i
) {
762 p
= smartlist_get(service
->ports
, i
);
763 if (smartlist_string_num_isin(get_options()->LongLivedPorts
,
770 /** Check client authorization of a given <b>descriptor_cookie</b> for
771 * <b>service</b>. Return 1 for success and 0 for failure. */
773 rend_check_authorization(rend_service_t
*service
,
774 const char *descriptor_cookie
)
776 rend_authorized_client_t
*auth_client
= NULL
;
778 tor_assert(descriptor_cookie
);
779 if (!service
->clients
) {
780 log_warn(LD_BUG
, "Can't check authorization for a service that has no "
781 "authorized clients configured.");
785 /* Look up client authorization by descriptor cookie. */
786 SMARTLIST_FOREACH(service
->clients
, rend_authorized_client_t
*, client
, {
787 if (!memcmp(client
->descriptor_cookie
, descriptor_cookie
,
788 REND_DESC_COOKIE_LEN
)) {
789 auth_client
= client
;
794 char descriptor_cookie_base64
[3*REND_DESC_COOKIE_LEN_BASE64
];
795 base64_encode(descriptor_cookie_base64
, sizeof(descriptor_cookie_base64
),
796 descriptor_cookie
, REND_DESC_COOKIE_LEN
);
797 log_info(LD_REND
, "No authorization found for descriptor cookie '%s'! "
799 descriptor_cookie_base64
);
803 /* Allow the request. */
804 log_debug(LD_REND
, "Client %s authorized for service %s.",
805 auth_client
->client_name
, service
->service_id
);
809 /** Remove elements from <b>service</b>'s replay cache that are old enough to
810 * be noticed by timestamp checking. */
812 clean_accepted_intros(rend_service_t
*service
, time_t now
)
814 const time_t cutoff
= now
- REND_REPLAY_TIME_INTERVAL
;
816 service
->last_cleaned_accepted_intros
= now
;
817 if (!service
->accepted_intros
)
820 DIGESTMAP_FOREACH_MODIFY(service
->accepted_intros
, digest
, time_t *, t
) {
823 MAP_DEL_CURRENT(digest
);
825 } DIGESTMAP_FOREACH_END
;
832 /** Respond to an INTRODUCE2 cell by launching a circuit to the chosen
836 rend_service_introduce(origin_circuit_t
*circuit
, const char *request
,
839 char *ptr
, *r_cookie
;
840 extend_info_t
*extend_info
= NULL
;
841 char buf
[RELAY_PAYLOAD_SIZE
];
842 char keys
[DIGEST_LEN
+CPATH_KEY_MATERIAL_LEN
]; /* Holds KH, Df, Db, Kf, Kb */
843 rend_service_t
*service
;
844 int r
, i
, v3_shift
= 0;
846 crypto_dh_env_t
*dh
= NULL
;
847 origin_circuit_t
*launched
= NULL
;
848 crypt_path_t
*cpath
= NULL
;
849 char serviceid
[REND_SERVICE_ID_LEN_BASE32
+1];
851 int circ_needs_uptime
;
852 int reason
= END_CIRC_REASON_TORPROTOCOL
;
853 crypto_pk_env_t
*intro_key
;
854 char intro_key_digest
[DIGEST_LEN
];
857 char auth_data
[REND_DESC_COOKIE_LEN
];
858 crypto_digest_env_t
*digest
= NULL
;
859 time_t now
= time(NULL
);
860 char diffie_hellman_hash
[DIGEST_LEN
];
862 tor_assert(circuit
->rend_data
);
864 base32_encode(serviceid
, REND_SERVICE_ID_LEN_BASE32
+1,
865 circuit
->rend_data
->rend_pk_digest
, REND_SERVICE_ID_LEN
);
866 log_info(LD_REND
, "Received INTRODUCE2 cell for service %s on circ %d.",
867 escaped(serviceid
), circuit
->_base
.n_circ_id
);
869 if (circuit
->_base
.purpose
!= CIRCUIT_PURPOSE_S_INTRO
) {
870 log_warn(LD_PROTOCOL
,
871 "Got an INTRODUCE2 over a non-introduction circuit %d.",
872 circuit
->_base
.n_circ_id
);
876 /* min key length plus digest length plus nickname length */
877 if (request_len
< DIGEST_LEN
+REND_COOKIE_LEN
+(MAX_NICKNAME_LEN
+1)+
879 log_warn(LD_PROTOCOL
, "Got a truncated INTRODUCE2 cell on circ %d.",
880 circuit
->_base
.n_circ_id
);
884 /* look up service depending on circuit. */
885 service
= rend_service_get_by_pk_digest(
886 circuit
->rend_data
->rend_pk_digest
);
888 log_warn(LD_REND
, "Got an INTRODUCE2 cell for an unrecognized service %s.",
893 /* use intro key instead of service key. */
894 intro_key
= circuit
->intro_key
;
896 /* first DIGEST_LEN bytes of request is intro or service pk digest */
897 crypto_pk_get_digest(intro_key
, intro_key_digest
);
898 if (memcmp(intro_key_digest
, request
, DIGEST_LEN
)) {
899 base32_encode(serviceid
, REND_SERVICE_ID_LEN_BASE32
+1,
900 request
, REND_SERVICE_ID_LEN
);
901 log_warn(LD_REND
, "Got an INTRODUCE2 cell for the wrong service (%s).",
906 keylen
= crypto_pk_keysize(intro_key
);
907 if (request_len
< keylen
+DIGEST_LEN
) {
908 log_warn(LD_PROTOCOL
,
909 "PK-encrypted portion of INTRODUCE2 cell was truncated.");
912 /* Next N bytes is encrypted with service key */
913 note_crypto_pk_op(REND_SERVER
);
914 r
= crypto_pk_private_hybrid_decrypt(
915 intro_key
,buf
,request
+DIGEST_LEN
,request_len
-DIGEST_LEN
,
916 PK_PKCS1_OAEP_PADDING
,1);
918 log_warn(LD_PROTOCOL
, "Couldn't decrypt INTRODUCE2 cell.");
923 /* Version 3 INTRODUCE2 cell. */
924 time_t ts
= 0, now
= time(NULL
);
928 case REND_BASIC_AUTH
:
930 case REND_STEALTH_AUTH
:
931 auth_len
= ntohs(get_uint16(buf
+2));
932 if (auth_len
!= REND_DESC_COOKIE_LEN
) {
933 log_info(LD_REND
, "Wrong auth data size %d, should be %d.",
934 (int)auth_len
, REND_DESC_COOKIE_LEN
);
937 memcpy(auth_data
, buf
+4, sizeof(auth_data
));
938 v3_shift
+= 2+REND_DESC_COOKIE_LEN
;
943 log_info(LD_REND
, "Unknown authorization type '%d'", auth_type
);
946 /* Check timestamp. */
947 memcpy((char*)&ts
, buf
+1+v3_shift
, sizeof(uint32_t));
949 ts
= ntohl((uint32_t)ts
);
950 if ((now
- ts
) < -1 * REND_REPLAY_TIME_INTERVAL
/ 2 ||
951 (now
- ts
) > REND_REPLAY_TIME_INTERVAL
/ 2) {
952 log_warn(LD_REND
, "INTRODUCE2 cell is too %s. Discarding.",
953 (now
- ts
) < 0 ? "old" : "new");
957 if (*buf
== 2 || *buf
== 3) {
958 /* Version 2 INTRODUCE2 cell. */
960 extend_info
= tor_malloc_zero(sizeof(extend_info_t
));
961 tor_addr_from_ipv4n(&extend_info
->addr
, get_uint32(buf
+v3_shift
+1));
962 extend_info
->port
= ntohs(get_uint16(buf
+v3_shift
+5));
963 memcpy(extend_info
->identity_digest
, buf
+v3_shift
+7,
965 extend_info
->nickname
[0] = '$';
966 base16_encode(extend_info
->nickname
+1, sizeof(extend_info
->nickname
)-1,
967 extend_info
->identity_digest
, DIGEST_LEN
);
969 klen
= ntohs(get_uint16(buf
+v3_shift
+7+DIGEST_LEN
));
970 if ((int)len
!= v3_shift
+7+DIGEST_LEN
+2+klen
+20+128) {
971 log_warn(LD_PROTOCOL
, "Bad length %u for version %d INTRODUCE2 cell.",
973 reason
= END_CIRC_REASON_TORPROTOCOL
;
976 extend_info
->onion_key
=
977 crypto_pk_asn1_decode(buf
+v3_shift
+7+DIGEST_LEN
+2, klen
);
978 if (!extend_info
->onion_key
) {
979 log_warn(LD_PROTOCOL
, "Error decoding onion key in version %d "
980 "INTRODUCE2 cell.", *buf
);
981 reason
= END_CIRC_REASON_TORPROTOCOL
;
984 ptr
= buf
+v3_shift
+7+DIGEST_LEN
+2+klen
;
985 len
-= v3_shift
+7+DIGEST_LEN
+2+klen
;
988 size_t nickname_field_len
;
989 routerinfo_t
*router
;
993 nickname_field_len
= MAX_HEX_NICKNAME_LEN
+1;
996 nickname_field_len
= MAX_NICKNAME_LEN
+1;
1000 ptr
=memchr(rp_nickname
,0,nickname_field_len
);
1001 if (!ptr
|| ptr
== rp_nickname
) {
1002 log_warn(LD_PROTOCOL
,
1003 "Couldn't find a nul-padded nickname in INTRODUCE2 cell.");
1006 if ((version
== 0 && !is_legal_nickname(rp_nickname
)) ||
1007 (version
== 1 && !is_legal_nickname_or_hexdigest(rp_nickname
))) {
1008 log_warn(LD_PROTOCOL
, "Bad nickname in INTRODUCE2 cell.");
1011 /* Okay, now we know that a nickname is at the start of the buffer. */
1012 ptr
= rp_nickname
+nickname_field_len
;
1013 len
-= nickname_field_len
;
1014 len
-= rp_nickname
- buf
; /* also remove header space used by version, if
1016 router
= router_get_by_nickname(rp_nickname
, 0);
1018 log_info(LD_REND
, "Couldn't find router %s named in introduce2 cell.",
1019 escaped_safe_str(rp_nickname
));
1020 /* XXXX Add a no-such-router reason? */
1021 reason
= END_CIRC_REASON_TORPROTOCOL
;
1025 extend_info
= extend_info_from_router(router
);
1028 if (len
!= REND_COOKIE_LEN
+DH_KEY_LEN
) {
1029 log_warn(LD_PROTOCOL
, "Bad length %u for INTRODUCE2 cell.", (int)len
);
1030 reason
= END_CIRC_REASON_TORPROTOCOL
;
1035 base16_encode(hexcookie
,9,r_cookie
,4);
1037 /* Determine hash of Diffie-Hellman, part 1 to detect replays. */
1038 digest
= crypto_new_digest_env();
1039 crypto_digest_add_bytes(digest
, ptr
+REND_COOKIE_LEN
, DH_KEY_LEN
);
1040 crypto_digest_get_digest(digest
, diffie_hellman_hash
, DIGEST_LEN
);
1041 crypto_free_digest_env(digest
);
1043 /* Check whether there is a past request with the same Diffie-Hellman,
1045 if (!service
->accepted_intros
)
1046 service
->accepted_intros
= digestmap_new();
1048 access_time
= digestmap_get(service
->accepted_intros
, diffie_hellman_hash
);
1049 if (access_time
!= NULL
) {
1050 log_warn(LD_REND
, "Possible replay detected! We received an "
1051 "INTRODUCE2 cell with same first part of "
1052 "Diffie-Hellman handshake %d seconds ago. Dropping "
1054 (int) (now
- *access_time
));
1058 /* Add request to access history, including time and hash of Diffie-Hellman,
1059 * part 1, and possibly remove requests from the history that are older than
1061 access_time
= tor_malloc(sizeof(time_t));
1063 digestmap_set(service
->accepted_intros
, diffie_hellman_hash
, access_time
);
1064 if (service
->last_cleaned_accepted_intros
+ REND_REPLAY_TIME_INTERVAL
< now
)
1065 clean_accepted_intros(service
, now
);
1067 /* If the service performs client authorization, check included auth data. */
1068 if (service
->clients
) {
1070 if (rend_check_authorization(service
, auth_data
)) {
1071 log_info(LD_REND
, "Authorization data in INTRODUCE2 cell are valid.");
1073 log_info(LD_REND
, "The authorization data that are contained in "
1074 "the INTRODUCE2 cell are invalid. Dropping cell.");
1075 reason
= END_CIRC_REASON_CONNECTFAILED
;
1079 log_info(LD_REND
, "INTRODUCE2 cell does not contain authentication "
1080 "data, but we require client authorization. Dropping cell.");
1081 reason
= END_CIRC_REASON_CONNECTFAILED
;
1086 /* Try DH handshake... */
1087 dh
= crypto_dh_new();
1088 if (!dh
|| crypto_dh_generate_public(dh
)<0) {
1089 log_warn(LD_BUG
,"Internal error: couldn't build DH state "
1090 "or generate public key.");
1091 reason
= END_CIRC_REASON_INTERNAL
;
1094 if (crypto_dh_compute_secret(dh
, ptr
+REND_COOKIE_LEN
, DH_KEY_LEN
, keys
,
1095 DIGEST_LEN
+CPATH_KEY_MATERIAL_LEN
)<0) {
1096 log_warn(LD_BUG
, "Internal error: couldn't complete DH handshake");
1097 reason
= END_CIRC_REASON_INTERNAL
;
1101 circ_needs_uptime
= rend_service_requires_uptime(service
);
1103 /* help predict this next time */
1104 rep_hist_note_used_internal(time(NULL
), circ_needs_uptime
, 1);
1106 /* Launch a circuit to alice's chosen rendezvous point.
1108 for (i
=0;i
<MAX_REND_FAILURES
;i
++) {
1109 int flags
= CIRCLAUNCH_NEED_CAPACITY
| CIRCLAUNCH_IS_INTERNAL
;
1110 if (circ_needs_uptime
) flags
|= CIRCLAUNCH_NEED_UPTIME
;
1111 launched
= circuit_launch_by_extend_info(
1112 CIRCUIT_PURPOSE_S_CONNECT_REND
, extend_info
, flags
);
1117 if (!launched
) { /* give up */
1118 log_warn(LD_REND
, "Giving up launching first hop of circuit to rendezvous "
1119 "point %s for service %s.",
1120 escaped_safe_str(extend_info
->nickname
), serviceid
);
1121 reason
= END_CIRC_REASON_CONNECTFAILED
;
1125 "Accepted intro; launching circuit to %s "
1126 "(cookie %s) for service %s.",
1127 escaped_safe_str(extend_info
->nickname
), hexcookie
, serviceid
);
1128 tor_assert(launched
->build_state
);
1129 /* Fill in the circuit's state. */
1130 launched
->rend_data
= tor_malloc_zero(sizeof(rend_data_t
));
1131 memcpy(launched
->rend_data
->rend_pk_digest
,
1132 circuit
->rend_data
->rend_pk_digest
,
1134 memcpy(launched
->rend_data
->rend_cookie
, r_cookie
, REND_COOKIE_LEN
);
1135 strlcpy(launched
->rend_data
->onion_address
, service
->service_id
,
1136 sizeof(launched
->rend_data
->onion_address
));
1137 launched
->build_state
->pending_final_cpath
= cpath
=
1138 tor_malloc_zero(sizeof(crypt_path_t
));
1139 cpath
->magic
= CRYPT_PATH_MAGIC
;
1140 launched
->build_state
->expiry_time
= time(NULL
) + MAX_REND_TIMEOUT
;
1142 cpath
->dh_handshake_state
= dh
;
1144 if (circuit_init_cpath_crypto(cpath
,keys
+DIGEST_LEN
,1)<0)
1146 memcpy(cpath
->handshake_digest
, keys
, DIGEST_LEN
);
1147 if (extend_info
) extend_info_free(extend_info
);
1151 if (dh
) crypto_dh_free(dh
);
1153 circuit_mark_for_close(TO_CIRCUIT(launched
), reason
);
1154 if (extend_info
) extend_info_free(extend_info
);
1158 /** Called when we fail building a rendezvous circuit at some point other
1159 * than the last hop: launches a new circuit to the same rendezvous point.
1162 rend_service_relaunch_rendezvous(origin_circuit_t
*oldcirc
)
1164 origin_circuit_t
*newcirc
;
1165 cpath_build_state_t
*newstate
, *oldstate
;
1167 tor_assert(oldcirc
->_base
.purpose
== CIRCUIT_PURPOSE_S_CONNECT_REND
);
1169 if (!oldcirc
->build_state
||
1170 oldcirc
->build_state
->failure_count
> MAX_REND_FAILURES
||
1171 oldcirc
->build_state
->expiry_time
< time(NULL
)) {
1173 "Attempt to build circuit to %s for rendezvous has failed "
1174 "too many times or expired; giving up.",
1175 oldcirc
->build_state
?
1176 oldcirc
->build_state
->chosen_exit
->nickname
: "*unknown*");
1180 oldstate
= oldcirc
->build_state
;
1181 tor_assert(oldstate
);
1183 if (oldstate
->pending_final_cpath
== NULL
) {
1184 log_info(LD_REND
,"Skipping relaunch of circ that failed on its first hop. "
1185 "Initiator will retry.");
1189 log_info(LD_REND
,"Reattempting rendezvous circuit to '%s'",
1190 oldstate
->chosen_exit
->nickname
);
1192 newcirc
= circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_CONNECT_REND
,
1193 oldstate
->chosen_exit
,
1194 CIRCLAUNCH_NEED_CAPACITY
|CIRCLAUNCH_IS_INTERNAL
);
1197 log_warn(LD_REND
,"Couldn't relaunch rendezvous circuit to '%s'.",
1198 oldstate
->chosen_exit
->nickname
);
1201 newstate
= newcirc
->build_state
;
1202 tor_assert(newstate
);
1203 newstate
->failure_count
= oldstate
->failure_count
+1;
1204 newstate
->expiry_time
= oldstate
->expiry_time
;
1205 newstate
->pending_final_cpath
= oldstate
->pending_final_cpath
;
1206 oldstate
->pending_final_cpath
= NULL
;
1208 newcirc
->rend_data
= rend_data_dup(oldcirc
->rend_data
);
1211 /** Launch a circuit to serve as an introduction point for the service
1212 * <b>service</b> at the introduction point <b>nickname</b>
1215 rend_service_launch_establish_intro(rend_service_t
*service
,
1216 rend_intro_point_t
*intro
)
1218 origin_circuit_t
*launched
;
1221 "Launching circuit to introduction point %s for service %s",
1222 escaped_safe_str(intro
->extend_info
->nickname
),
1223 service
->service_id
);
1225 rep_hist_note_used_internal(time(NULL
), 1, 0);
1227 ++service
->n_intro_circuits_launched
;
1228 launched
= circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
,
1230 CIRCLAUNCH_NEED_UPTIME
|CIRCLAUNCH_IS_INTERNAL
);
1234 "Can't launch circuit to establish introduction at %s.",
1235 escaped_safe_str(intro
->extend_info
->nickname
));
1239 if (memcmp(intro
->extend_info
->identity_digest
,
1240 launched
->build_state
->chosen_exit
->identity_digest
, DIGEST_LEN
)) {
1241 char cann
[HEX_DIGEST_LEN
+1], orig
[HEX_DIGEST_LEN
+1];
1242 base16_encode(cann
, sizeof(cann
),
1243 launched
->build_state
->chosen_exit
->identity_digest
,
1245 base16_encode(orig
, sizeof(orig
),
1246 intro
->extend_info
->identity_digest
, DIGEST_LEN
);
1247 log_info(LD_REND
, "The intro circuit we just cannibalized ends at $%s, "
1248 "but we requested an intro circuit to $%s. Updating "
1249 "our service.", cann
, orig
);
1250 extend_info_free(intro
->extend_info
);
1251 intro
->extend_info
= extend_info_dup(launched
->build_state
->chosen_exit
);
1254 launched
->rend_data
= tor_malloc_zero(sizeof(rend_data_t
));
1255 strlcpy(launched
->rend_data
->onion_address
, service
->service_id
,
1256 sizeof(launched
->rend_data
->onion_address
));
1257 memcpy(launched
->rend_data
->rend_pk_digest
, service
->pk_digest
, DIGEST_LEN
);
1258 launched
->intro_key
= crypto_pk_dup_key(intro
->intro_key
);
1259 if (launched
->_base
.state
== CIRCUIT_STATE_OPEN
)
1260 rend_service_intro_has_opened(launched
);
1264 /** Return the number of introduction points that are or have been
1265 * established for the given service address in <b>query</b>. */
1267 count_established_intro_points(const char *query
)
1271 for (circ
= _circuit_get_global_list(); circ
; circ
= circ
->next
) {
1272 if (!circ
->marked_for_close
&&
1273 circ
->state
== CIRCUIT_STATE_OPEN
&&
1274 (circ
->purpose
== CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
||
1275 circ
->purpose
== CIRCUIT_PURPOSE_S_INTRO
)) {
1276 origin_circuit_t
*oc
= TO_ORIGIN_CIRCUIT(circ
);
1277 if (oc
->rend_data
&&
1278 !rend_cmp_service_ids(query
, oc
->rend_data
->onion_address
))
1285 /** Called when we're done building a circuit to an introduction point:
1286 * sends a RELAY_ESTABLISH_INTRO cell.
1289 rend_service_intro_has_opened(origin_circuit_t
*circuit
)
1291 rend_service_t
*service
;
1294 char buf
[RELAY_PAYLOAD_SIZE
];
1295 char auth
[DIGEST_LEN
+ 9];
1296 char serviceid
[REND_SERVICE_ID_LEN_BASE32
+1];
1297 int reason
= END_CIRC_REASON_TORPROTOCOL
;
1298 crypto_pk_env_t
*intro_key
;
1300 tor_assert(circuit
->_base
.purpose
== CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
);
1301 tor_assert(circuit
->cpath
);
1302 tor_assert(circuit
->rend_data
);
1304 base32_encode(serviceid
, REND_SERVICE_ID_LEN_BASE32
+1,
1305 circuit
->rend_data
->rend_pk_digest
, REND_SERVICE_ID_LEN
);
1307 service
= rend_service_get_by_pk_digest(
1308 circuit
->rend_data
->rend_pk_digest
);
1310 log_warn(LD_REND
, "Unrecognized service ID %s on introduction circuit %d.",
1311 serviceid
, circuit
->_base
.n_circ_id
);
1312 reason
= END_CIRC_REASON_NOSUCHSERVICE
;
1316 /* If we already have enough introduction circuits for this service,
1317 * redefine this one as a general circuit. */
1318 if (count_established_intro_points(serviceid
) > NUM_INTRO_POINTS
) {
1319 log_info(LD_CIRC
|LD_REND
, "We have just finished an introduction "
1320 "circuit, but we already have enough. Redefining purpose to "
1322 TO_CIRCUIT(circuit
)->purpose
= CIRCUIT_PURPOSE_C_GENERAL
;
1323 circuit_has_opened(circuit
);
1328 "Established circuit %d as introduction point for service %s",
1329 circuit
->_base
.n_circ_id
, serviceid
);
1331 /* Use the intro key instead of the service key in ESTABLISH_INTRO. */
1332 intro_key
= circuit
->intro_key
;
1333 /* Build the payload for a RELAY_ESTABLISH_INTRO cell. */
1334 r
= crypto_pk_asn1_encode(intro_key
, buf
+2,
1335 RELAY_PAYLOAD_SIZE
-2);
1337 log_warn(LD_BUG
, "Internal error; failed to establish intro point.");
1338 reason
= END_CIRC_REASON_INTERNAL
;
1342 set_uint16(buf
, htons((uint16_t)len
));
1344 memcpy(auth
, circuit
->cpath
->prev
->handshake_digest
, DIGEST_LEN
);
1345 memcpy(auth
+DIGEST_LEN
, "INTRODUCE", 9);
1346 if (crypto_digest(buf
+len
, auth
, DIGEST_LEN
+9))
1349 note_crypto_pk_op(REND_SERVER
);
1350 r
= crypto_pk_private_sign_digest(intro_key
, buf
+len
, buf
, len
);
1352 log_warn(LD_BUG
, "Internal error: couldn't sign introduction request.");
1353 reason
= END_CIRC_REASON_INTERNAL
;
1358 if (relay_send_command_from_edge(0, TO_CIRCUIT(circuit
),
1359 RELAY_COMMAND_ESTABLISH_INTRO
,
1360 buf
, len
, circuit
->cpath
->prev
)<0) {
1361 log_info(LD_GENERAL
,
1362 "Couldn't send introduction request for service %s on circuit %d",
1363 serviceid
, circuit
->_base
.n_circ_id
);
1364 reason
= END_CIRC_REASON_INTERNAL
;
1370 circuit_mark_for_close(TO_CIRCUIT(circuit
), reason
);
1373 /** Called when we get an INTRO_ESTABLISHED cell; mark the circuit as a
1374 * live introduction point, and note that the service descriptor is
1375 * now out-of-date.*/
1377 rend_service_intro_established(origin_circuit_t
*circuit
, const char *request
,
1380 rend_service_t
*service
;
1381 char serviceid
[REND_SERVICE_ID_LEN_BASE32
+1];
1385 if (circuit
->_base
.purpose
!= CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
) {
1386 log_warn(LD_PROTOCOL
,
1387 "received INTRO_ESTABLISHED cell on non-intro circuit.");
1390 tor_assert(circuit
->rend_data
);
1391 service
= rend_service_get_by_pk_digest(
1392 circuit
->rend_data
->rend_pk_digest
);
1394 log_warn(LD_REND
, "Unknown service on introduction circuit %d.",
1395 circuit
->_base
.n_circ_id
);
1398 service
->desc_is_dirty
= time(NULL
);
1399 circuit
->_base
.purpose
= CIRCUIT_PURPOSE_S_INTRO
;
1401 base32_encode(serviceid
, REND_SERVICE_ID_LEN_BASE32
+ 1,
1402 circuit
->rend_data
->rend_pk_digest
, REND_SERVICE_ID_LEN
);
1404 "Received INTRO_ESTABLISHED cell on circuit %d for service %s",
1405 circuit
->_base
.n_circ_id
, serviceid
);
1409 circuit_mark_for_close(TO_CIRCUIT(circuit
), END_CIRC_REASON_TORPROTOCOL
);
1413 /** Called once a circuit to a rendezvous point is established: sends a
1414 * RELAY_COMMAND_RENDEZVOUS1 cell.
1417 rend_service_rendezvous_has_opened(origin_circuit_t
*circuit
)
1419 rend_service_t
*service
;
1420 char buf
[RELAY_PAYLOAD_SIZE
];
1422 char serviceid
[REND_SERVICE_ID_LEN_BASE32
+1];
1426 tor_assert(circuit
->_base
.purpose
== CIRCUIT_PURPOSE_S_CONNECT_REND
);
1427 tor_assert(circuit
->cpath
);
1428 tor_assert(circuit
->build_state
);
1429 tor_assert(circuit
->rend_data
);
1430 hop
= circuit
->build_state
->pending_final_cpath
;
1433 base16_encode(hexcookie
,9,circuit
->rend_data
->rend_cookie
,4);
1434 base32_encode(serviceid
, REND_SERVICE_ID_LEN_BASE32
+1,
1435 circuit
->rend_data
->rend_pk_digest
, REND_SERVICE_ID_LEN
);
1438 "Done building circuit %d to rendezvous with "
1439 "cookie %s for service %s",
1440 circuit
->_base
.n_circ_id
, hexcookie
, serviceid
);
1442 service
= rend_service_get_by_pk_digest(
1443 circuit
->rend_data
->rend_pk_digest
);
1445 log_warn(LD_GENERAL
, "Internal error: unrecognized service ID on "
1446 "introduction circuit.");
1447 reason
= END_CIRC_REASON_INTERNAL
;
1451 /* All we need to do is send a RELAY_RENDEZVOUS1 cell... */
1452 memcpy(buf
, circuit
->rend_data
->rend_cookie
, REND_COOKIE_LEN
);
1453 if (crypto_dh_get_public(hop
->dh_handshake_state
,
1454 buf
+REND_COOKIE_LEN
, DH_KEY_LEN
)<0) {
1455 log_warn(LD_GENERAL
,"Couldn't get DH public key.");
1456 reason
= END_CIRC_REASON_INTERNAL
;
1459 memcpy(buf
+REND_COOKIE_LEN
+DH_KEY_LEN
, hop
->handshake_digest
,
1463 if (relay_send_command_from_edge(0, TO_CIRCUIT(circuit
),
1464 RELAY_COMMAND_RENDEZVOUS1
,
1465 buf
, REND_COOKIE_LEN
+DH_KEY_LEN
+DIGEST_LEN
,
1466 circuit
->cpath
->prev
)<0) {
1467 log_warn(LD_GENERAL
, "Couldn't send RENDEZVOUS1 cell.");
1468 reason
= END_CIRC_REASON_INTERNAL
;
1472 crypto_dh_free(hop
->dh_handshake_state
);
1473 hop
->dh_handshake_state
= NULL
;
1475 /* Append the cpath entry. */
1476 hop
->state
= CPATH_STATE_OPEN
;
1477 /* set the windows to default. these are the windows
1478 * that bob thinks alice has.
1480 hop
->package_window
= circuit_initial_package_window();;
1481 hop
->deliver_window
= CIRCWINDOW_START
;
1483 onion_append_to_cpath(&circuit
->cpath
, hop
);
1484 circuit
->build_state
->pending_final_cpath
= NULL
; /* prevent double-free */
1486 /* Change the circuit purpose. */
1487 circuit
->_base
.purpose
= CIRCUIT_PURPOSE_S_REND_JOINED
;
1491 circuit_mark_for_close(TO_CIRCUIT(circuit
), reason
);
1495 * Manage introduction points
1498 /** Return the (possibly non-open) introduction circuit ending at
1499 * <b>intro</b> for the service whose public key is <b>pk_digest</b>.
1500 * (<b>desc_version</b> is ignored). Return NULL if no such service is
1503 static origin_circuit_t
*
1504 find_intro_circuit(rend_intro_point_t
*intro
, const char *pk_digest
)
1506 origin_circuit_t
*circ
= NULL
;
1509 while ((circ
= circuit_get_next_by_pk_and_purpose(circ
,pk_digest
,
1510 CIRCUIT_PURPOSE_S_INTRO
))) {
1511 if (!memcmp(circ
->build_state
->chosen_exit
->identity_digest
,
1512 intro
->extend_info
->identity_digest
, DIGEST_LEN
) &&
1519 while ((circ
= circuit_get_next_by_pk_and_purpose(circ
,pk_digest
,
1520 CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
))) {
1521 if (!memcmp(circ
->build_state
->chosen_exit
->identity_digest
,
1522 intro
->extend_info
->identity_digest
, DIGEST_LEN
) &&
1530 /** Determine the responsible hidden service directories for the
1531 * rend_encoded_v2_service_descriptor_t's in <b>descs</b> and upload them;
1532 * <b>service_id</b> and <b>seconds_valid</b> are only passed for logging
1535 directory_post_to_hs_dir(rend_service_descriptor_t
*renddesc
,
1536 smartlist_t
*descs
, const char *service_id
,
1539 int i
, j
, failed_upload
= 0;
1540 smartlist_t
*responsible_dirs
= smartlist_create();
1541 smartlist_t
*successful_uploads
= smartlist_create();
1542 routerstatus_t
*hs_dir
;
1543 for (i
= 0; i
< smartlist_len(descs
); i
++) {
1544 rend_encoded_v2_service_descriptor_t
*desc
= smartlist_get(descs
, i
);
1545 /* Determine responsible dirs. */
1546 if (hid_serv_get_responsible_directories(responsible_dirs
,
1547 desc
->desc_id
) < 0) {
1548 log_warn(LD_REND
, "Could not determine the responsible hidden service "
1549 "directories to post descriptors to.");
1550 smartlist_free(responsible_dirs
);
1551 smartlist_free(successful_uploads
);
1554 for (j
= 0; j
< smartlist_len(responsible_dirs
); j
++) {
1555 char desc_id_base32
[REND_DESC_ID_V2_LEN_BASE32
+ 1];
1556 hs_dir
= smartlist_get(responsible_dirs
, j
);
1557 if (smartlist_digest_isin(renddesc
->successful_uploads
,
1558 hs_dir
->identity_digest
))
1559 /* Don't upload descriptor if we succeeded in doing so last time. */
1561 if (!router_get_by_digest(hs_dir
->identity_digest
)) {
1562 log_info(LD_REND
, "Not sending publish request for v2 descriptor to "
1563 "hidden service directory '%s'; we don't have its "
1564 "router descriptor. Queuing for later upload.",
1569 /* Send publish request. */
1570 directory_initiate_command_routerstatus(hs_dir
,
1571 DIR_PURPOSE_UPLOAD_RENDDESC_V2
,
1572 ROUTER_PURPOSE_GENERAL
,
1573 1, NULL
, desc
->desc_str
,
1574 strlen(desc
->desc_str
), 0);
1575 base32_encode(desc_id_base32
, sizeof(desc_id_base32
),
1576 desc
->desc_id
, DIGEST_LEN
);
1577 log_info(LD_REND
, "Sending publish request for v2 descriptor for "
1578 "service '%s' with descriptor ID '%s' with validity "
1579 "of %d seconds to hidden service directory '%s' on "
1581 safe_str(service_id
),
1582 safe_str(desc_id_base32
),
1586 /* Remember successful upload to this router for next time. */
1587 if (!smartlist_digest_isin(successful_uploads
, hs_dir
->identity_digest
))
1588 smartlist_add(successful_uploads
, hs_dir
->identity_digest
);
1590 smartlist_clear(responsible_dirs
);
1592 if (!failed_upload
) {
1593 if (renddesc
->successful_uploads
) {
1594 SMARTLIST_FOREACH(renddesc
->successful_uploads
, char *, c
, tor_free(c
););
1595 smartlist_free(renddesc
->successful_uploads
);
1596 renddesc
->successful_uploads
= NULL
;
1598 renddesc
->all_uploads_performed
= 1;
1600 /* Remember which routers worked this time, so that we don't upload the
1601 * descriptor to them again. */
1602 if (!renddesc
->successful_uploads
)
1603 renddesc
->successful_uploads
= smartlist_create();
1604 SMARTLIST_FOREACH(successful_uploads
, const char *, c
, {
1605 if (!smartlist_digest_isin(renddesc
->successful_uploads
, c
)) {
1606 char *hsdir_id
= tor_memdup(c
, DIGEST_LEN
);
1607 smartlist_add(renddesc
->successful_uploads
, hsdir_id
);
1611 smartlist_free(responsible_dirs
);
1612 smartlist_free(successful_uploads
);
1615 /** Encode and sign an up-to-date service descriptor for <b>service</b>,
1616 * and upload it/them to the responsible hidden service directories.
1619 upload_service_descriptor(rend_service_t
*service
)
1621 time_t now
= time(NULL
);
1623 char serviceid
[REND_SERVICE_ID_LEN_BASE32
+1];
1626 rendpostperiod
= get_options()->RendPostPeriod
;
1628 /* Upload descriptor? */
1629 if (get_options()->PublishHidServDescriptors
) {
1630 networkstatus_t
*c
= networkstatus_get_latest_consensus();
1631 if (c
&& smartlist_len(c
->routerstatus_list
) > 0) {
1632 int seconds_valid
, i
, j
, num_descs
;
1633 smartlist_t
*descs
= smartlist_create();
1634 smartlist_t
*client_cookies
= smartlist_create();
1635 /* Either upload a single descriptor (including replicas) or one
1636 * descriptor for each authorized client in case of authorization
1637 * type 'stealth'. */
1638 num_descs
= service
->auth_type
== REND_STEALTH_AUTH
?
1639 smartlist_len(service
->clients
) : 1;
1640 for (j
= 0; j
< num_descs
; j
++) {
1641 crypto_pk_env_t
*client_key
= NULL
;
1642 rend_authorized_client_t
*client
= NULL
;
1643 smartlist_clear(client_cookies
);
1644 switch (service
->auth_type
) {
1646 /* Do nothing here. */
1648 case REND_BASIC_AUTH
:
1649 SMARTLIST_FOREACH(service
->clients
, rend_authorized_client_t
*,
1650 cl
, smartlist_add(client_cookies
, cl
->descriptor_cookie
));
1652 case REND_STEALTH_AUTH
:
1653 client
= smartlist_get(service
->clients
, j
);
1654 client_key
= client
->client_key
;
1655 smartlist_add(client_cookies
, client
->descriptor_cookie
);
1658 /* Encode the current descriptor. */
1659 seconds_valid
= rend_encode_v2_descriptors(descs
, service
->desc
,
1664 if (seconds_valid
< 0) {
1665 log_warn(LD_BUG
, "Internal error: couldn't encode service "
1666 "descriptor; not uploading.");
1667 smartlist_free(descs
);
1668 smartlist_free(client_cookies
);
1671 /* Post the current descriptors to the hidden service directories. */
1672 rend_get_service_id(service
->desc
->pk
, serviceid
);
1673 log_info(LD_REND
, "Sending publish request for hidden service %s",
1675 directory_post_to_hs_dir(service
->desc
, descs
, serviceid
,
1677 /* Free memory for descriptors. */
1678 for (i
= 0; i
< smartlist_len(descs
); i
++)
1679 rend_encoded_v2_service_descriptor_free(smartlist_get(descs
, i
));
1680 smartlist_clear(descs
);
1681 /* Update next upload time. */
1682 if (seconds_valid
- REND_TIME_PERIOD_OVERLAPPING_V2_DESCS
1684 service
->next_upload_time
= now
+ rendpostperiod
;
1685 else if (seconds_valid
< REND_TIME_PERIOD_OVERLAPPING_V2_DESCS
)
1686 service
->next_upload_time
= now
+ seconds_valid
+ 1;
1688 service
->next_upload_time
= now
+ seconds_valid
-
1689 REND_TIME_PERIOD_OVERLAPPING_V2_DESCS
+ 1;
1690 /* Post also the next descriptors, if necessary. */
1691 if (seconds_valid
< REND_TIME_PERIOD_OVERLAPPING_V2_DESCS
) {
1692 seconds_valid
= rend_encode_v2_descriptors(descs
, service
->desc
,
1697 if (seconds_valid
< 0) {
1698 log_warn(LD_BUG
, "Internal error: couldn't encode service "
1699 "descriptor; not uploading.");
1700 smartlist_free(descs
);
1701 smartlist_free(client_cookies
);
1704 directory_post_to_hs_dir(service
->desc
, descs
, serviceid
,
1706 /* Free memory for descriptors. */
1707 for (i
= 0; i
< smartlist_len(descs
); i
++)
1708 rend_encoded_v2_service_descriptor_free(smartlist_get(descs
, i
));
1709 smartlist_clear(descs
);
1712 smartlist_free(descs
);
1713 smartlist_free(client_cookies
);
1715 log_info(LD_REND
, "Successfully uploaded v2 rend descriptors!");
1719 /* If not uploaded, try again in one minute. */
1721 service
->next_upload_time
= now
+ 60;
1723 /* Unmark dirty flag of this service. */
1724 service
->desc_is_dirty
= 0;
1727 /** For every service, check how many intro points it currently has, and:
1728 * - Pick new intro points as necessary.
1729 * - Launch circuits to any new intro points.
1732 rend_services_introduce(void)
1735 routerinfo_t
*router
;
1736 rend_service_t
*service
;
1737 rend_intro_point_t
*intro
;
1738 int changed
, prev_intro_nodes
;
1739 smartlist_t
*intro_routers
;
1741 or_options_t
*options
= get_options();
1743 intro_routers
= smartlist_create();
1746 for (i
=0; i
< smartlist_len(rend_service_list
); ++i
) {
1747 smartlist_clear(intro_routers
);
1748 service
= smartlist_get(rend_service_list
, i
);
1750 tor_assert(service
);
1752 if (now
> service
->intro_period_started
+INTRO_CIRC_RETRY_PERIOD
) {
1753 /* One period has elapsed; we can try building circuits again. */
1754 service
->intro_period_started
= now
;
1755 service
->n_intro_circuits_launched
= 0;
1756 } else if (service
->n_intro_circuits_launched
>=
1757 MAX_INTRO_CIRCS_PER_PERIOD
) {
1758 /* We have failed too many times in this period; wait for the next
1759 * one before we try again. */
1763 /* Find out which introduction points we have in progress for this
1765 for (j
=0; j
< smartlist_len(service
->intro_nodes
); ++j
) {
1766 intro
= smartlist_get(service
->intro_nodes
, j
);
1767 router
= router_get_by_digest(intro
->extend_info
->identity_digest
);
1768 if (!router
|| !find_intro_circuit(intro
, service
->pk_digest
)) {
1769 log_info(LD_REND
,"Giving up on %s as intro point for %s.",
1770 intro
->extend_info
->nickname
, service
->service_id
);
1771 if (service
->desc
) {
1772 SMARTLIST_FOREACH(service
->desc
->intro_nodes
, rend_intro_point_t
*,
1774 if (!memcmp(dintro
->extend_info
->identity_digest
,
1775 intro
->extend_info
->identity_digest
, DIGEST_LEN
)) {
1776 log_info(LD_REND
, "The intro point we are giving up on was "
1777 "included in the last published descriptor. "
1778 "Marking current descriptor as dirty.");
1779 service
->desc_is_dirty
= now
;
1783 rend_intro_point_free(intro
);
1784 smartlist_del(service
->intro_nodes
,j
--);
1788 smartlist_add(intro_routers
, router
);
1791 /* We have enough intro points, and the intro points we thought we had were
1794 if (!changed
&& smartlist_len(service
->intro_nodes
) >= NUM_INTRO_POINTS
) {
1795 /* We have all our intro points! Start a fresh period and reset the
1797 service
->intro_period_started
= now
;
1798 service
->n_intro_circuits_launched
= 0;
1802 /* Remember how many introduction circuits we started with. */
1803 prev_intro_nodes
= smartlist_len(service
->intro_nodes
);
1804 /* We have enough directory information to start establishing our
1805 * intro points. We want to end up with three intro points, but if
1806 * we're just starting, we launch five and pick the first three that
1809 * The ones after the first three will be converted to 'general'
1810 * internal circuits in rend_service_intro_has_opened(), and then
1811 * we'll drop them from the list of intro points next time we
1812 * go through the above "find out which introduction points we have
1813 * in progress" loop. */
1814 #define NUM_INTRO_POINTS_INIT (NUM_INTRO_POINTS + 2)
1815 for (j
=prev_intro_nodes
; j
< (prev_intro_nodes
== 0 ?
1816 NUM_INTRO_POINTS_INIT
: NUM_INTRO_POINTS
); ++j
) {
1817 router_crn_flags_t flags
= CRN_NEED_UPTIME
;
1818 if (get_options()->_AllowInvalid
& ALLOW_INVALID_INTRODUCTION
)
1819 flags
|= CRN_ALLOW_INVALID
;
1820 router
= router_choose_random_node(NULL
, intro_routers
,
1821 options
->ExcludeNodes
, flags
);
1824 "Could only establish %d introduction points for %s.",
1825 smartlist_len(service
->intro_nodes
), service
->service_id
);
1829 smartlist_add(intro_routers
, router
);
1830 intro
= tor_malloc_zero(sizeof(rend_intro_point_t
));
1831 intro
->extend_info
= extend_info_from_router(router
);
1832 intro
->intro_key
= crypto_new_pk_env();
1833 tor_assert(!crypto_pk_generate_key(intro
->intro_key
));
1834 smartlist_add(service
->intro_nodes
, intro
);
1835 log_info(LD_REND
, "Picked router %s as an intro point for %s.",
1836 router
->nickname
, service
->service_id
);
1839 /* If there's no need to launch new circuits, stop here. */
1843 /* Establish new introduction points. */
1844 for (j
=prev_intro_nodes
; j
< smartlist_len(service
->intro_nodes
); ++j
) {
1845 intro
= smartlist_get(service
->intro_nodes
, j
);
1846 r
= rend_service_launch_establish_intro(service
, intro
);
1848 log_warn(LD_REND
, "Error launching circuit to node %s for service %s.",
1849 intro
->extend_info
->nickname
, service
->service_id
);
1853 smartlist_free(intro_routers
);
1856 /** Regenerate and upload rendezvous service descriptors for all
1857 * services, if necessary. If the descriptor has been dirty enough
1858 * for long enough, definitely upload; else only upload when the
1859 * periodic timeout has expired.
1861 * For the first upload, pick a random time between now and two periods
1862 * from now, and pick it independently for each service.
1865 rend_consider_services_upload(time_t now
)
1868 rend_service_t
*service
;
1869 int rendpostperiod
= get_options()->RendPostPeriod
;
1871 if (!get_options()->PublishHidServDescriptors
)
1874 for (i
=0; i
< smartlist_len(rend_service_list
); ++i
) {
1875 service
= smartlist_get(rend_service_list
, i
);
1876 if (!service
->next_upload_time
) { /* never been uploaded yet */
1877 /* The fixed lower bound of 30 seconds ensures that the descriptor
1878 * is stable before being published. See comment below. */
1879 service
->next_upload_time
=
1880 now
+ 30 + crypto_rand_int(2*rendpostperiod
);
1882 if (service
->next_upload_time
< now
||
1883 (service
->desc_is_dirty
&&
1884 service
->desc_is_dirty
< now
-30)) {
1885 /* if it's time, or if the directory servers have a wrong service
1886 * descriptor and ours has been stable for 30 seconds, upload a
1887 * new one of each format. */
1888 rend_service_update_descriptor(service
);
1889 upload_service_descriptor(service
);
1894 /** True if the list of available router descriptors might have changed so
1895 * that we should have a look whether we can republish previously failed
1896 * rendezvous service descriptors. */
1897 static int consider_republishing_rend_descriptors
= 1;
1899 /** Called when our internal view of the directory has changed, so that we
1900 * might have router descriptors of hidden service directories available that
1901 * we did not have before. */
1903 rend_hsdir_routers_changed(void)
1905 consider_republishing_rend_descriptors
= 1;
1908 /** Consider republication of v2 rendezvous service descriptors that failed
1909 * previously, but without regenerating descriptor contents.
1912 rend_consider_descriptor_republication(void)
1915 rend_service_t
*service
;
1917 if (!consider_republishing_rend_descriptors
)
1919 consider_republishing_rend_descriptors
= 0;
1921 if (!get_options()->PublishHidServDescriptors
)
1924 for (i
=0; i
< smartlist_len(rend_service_list
); ++i
) {
1925 service
= smartlist_get(rend_service_list
, i
);
1926 if (service
->desc
&& !service
->desc
->all_uploads_performed
) {
1927 /* If we failed in uploading a descriptor last time, try again *without*
1928 * updating the descriptor's contents. */
1929 upload_service_descriptor(service
);
1934 /** Log the status of introduction points for all rendezvous services
1935 * at log severity <b>severity</b>.
1938 rend_service_dump_stats(int severity
)
1941 rend_service_t
*service
;
1942 rend_intro_point_t
*intro
;
1943 const char *safe_name
;
1944 origin_circuit_t
*circ
;
1946 for (i
=0; i
< smartlist_len(rend_service_list
); ++i
) {
1947 service
= smartlist_get(rend_service_list
, i
);
1948 log(severity
, LD_GENERAL
, "Service configured in \"%s\":",
1949 service
->directory
);
1950 for (j
=0; j
< smartlist_len(service
->intro_nodes
); ++j
) {
1951 intro
= smartlist_get(service
->intro_nodes
, j
);
1952 safe_name
= safe_str(intro
->extend_info
->nickname
);
1954 circ
= find_intro_circuit(intro
, service
->pk_digest
);
1956 log(severity
, LD_GENERAL
, " Intro point %d at %s: no circuit",
1960 log(severity
, LD_GENERAL
, " Intro point %d at %s: circuit is %s",
1961 j
, safe_name
, circuit_state_to_string(circ
->_base
.state
));
1966 /** Given <b>conn</b>, a rendezvous exit stream, look up the hidden service for
1967 * 'circ', and look up the port and address based on conn-\>port.
1968 * Assign the actual conn-\>addr and conn-\>port. Return -1 if failure,
1972 rend_service_set_connection_addr_port(edge_connection_t
*conn
,
1973 origin_circuit_t
*circ
)
1975 rend_service_t
*service
;
1976 char serviceid
[REND_SERVICE_ID_LEN_BASE32
+1];
1977 smartlist_t
*matching_ports
;
1978 rend_service_port_config_t
*chosen_port
;
1980 tor_assert(circ
->_base
.purpose
== CIRCUIT_PURPOSE_S_REND_JOINED
);
1981 tor_assert(circ
->rend_data
);
1982 log_debug(LD_REND
,"beginning to hunt for addr/port");
1983 base32_encode(serviceid
, REND_SERVICE_ID_LEN_BASE32
+1,
1984 circ
->rend_data
->rend_pk_digest
, REND_SERVICE_ID_LEN
);
1985 service
= rend_service_get_by_pk_digest(
1986 circ
->rend_data
->rend_pk_digest
);
1988 log_warn(LD_REND
, "Couldn't find any service associated with pk %s on "
1989 "rendezvous circuit %d; closing.",
1990 serviceid
, circ
->_base
.n_circ_id
);
1993 matching_ports
= smartlist_create();
1994 SMARTLIST_FOREACH(service
->ports
, rend_service_port_config_t
*, p
,
1996 if (conn
->_base
.port
== p
->virtual_port
) {
1997 smartlist_add(matching_ports
, p
);
2000 chosen_port
= smartlist_choose(matching_ports
);
2001 smartlist_free(matching_ports
);
2003 tor_addr_copy(&conn
->_base
.addr
, &chosen_port
->real_addr
);
2004 conn
->_base
.port
= chosen_port
->real_port
;
2007 log_info(LD_REND
, "No virtual port mapping exists for port %d on service %s",
2008 conn
->_base
.port
,serviceid
);