rip out hid_serv_acting_as_directory()
[tor.git] / src / or / rendcache.c
blobdad4b2b5ba50f748fd3616b5e632cea07e11fc97
1 /* Copyright (c) 2015-2016, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 /**
5 * \file rendcache.c
6 * \brief Hidden service descriptor cache.
7 **/
9 #define RENDCACHE_PRIVATE
10 #include "rendcache.h"
12 #include "config.h"
13 #include "rephist.h"
14 #include "routerlist.h"
15 #include "routerparse.h"
16 #include "rendcommon.h"
18 /** Map from service id (as generated by rend_get_service_id) to
19 * rend_cache_entry_t. */
20 STATIC strmap_t *rend_cache = NULL;
22 /** Map from service id to rend_cache_entry_t; only for hidden services. */
23 static strmap_t *rend_cache_local_service = NULL;
25 /** Map from descriptor id to rend_cache_entry_t; only for hidden service
26 * directories. */
27 STATIC digestmap_t *rend_cache_v2_dir = NULL;
29 /** (Client side only) Map from service id to rend_cache_failure_t. This
30 * cache is used to track intro point(IP) failures so we know when to keep
31 * or discard a new descriptor we just fetched. Here is a description of the
32 * cache behavior.
34 * Everytime tor discards an IP (ex: receives a NACK), we add an entry to
35 * this cache noting the identity digest of the IP and it's failure type for
36 * the service ID. The reason we indexed this cache by service ID is to
37 * differentiate errors that can occur only for a specific service like a
38 * NACK for instance. It applies for one but maybe not for the others.
40 * Once a service descriptor is fetched and considered valid, each IP is
41 * looked up in this cache and if present, it is discarded from the fetched
42 * descriptor. At the end, all IP(s) in the cache, for a specific service
43 * ID, that were NOT present in the descriptor are removed from this cache.
44 * Which means that if at least one IP was not in this cache, thus usuable,
45 * it's considered a new descriptor so we keep it. Else, if all IPs were in
46 * this cache, we discard the descriptor as it's considered unsuable.
48 * Once a descriptor is removed from the rend cache or expires, the entry
49 * in this cache is also removed for the service ID.
51 * This scheme allows us to not realy on the descriptor's timestamp (which
52 * is rounded down to the hour) to know if we have a newer descriptor. We
53 * only rely on the usability of intro points from an internal state. */
54 STATIC strmap_t *rend_cache_failure = NULL;
56 /** DOCDOC */
57 STATIC size_t rend_cache_total_allocation = 0;
59 /** Initializes the service descriptor cache.
61 void
62 rend_cache_init(void)
64 rend_cache = strmap_new();
65 rend_cache_v2_dir = digestmap_new();
66 rend_cache_local_service = strmap_new();
67 rend_cache_failure = strmap_new();
70 /** Return the approximate number of bytes needed to hold <b>e</b>. */
71 STATIC size_t
72 rend_cache_entry_allocation(const rend_cache_entry_t *e)
74 if (!e)
75 return 0;
77 /* This doesn't count intro_nodes or key size */
78 return sizeof(*e) + e->len + sizeof(*e->parsed);
81 /** DOCDOC */
82 size_t
83 rend_cache_get_total_allocation(void)
85 return rend_cache_total_allocation;
88 /** Decrement the total bytes attributed to the rendezvous cache by n. */
89 STATIC void
90 rend_cache_decrement_allocation(size_t n)
92 static int have_underflowed = 0;
94 if (rend_cache_total_allocation >= n) {
95 rend_cache_total_allocation -= n;
96 } else {
97 rend_cache_total_allocation = 0;
98 if (! have_underflowed) {
99 have_underflowed = 1;
100 log_warn(LD_BUG, "Underflow in rend_cache_decrement_allocation");
105 /** Increase the total bytes attributed to the rendezvous cache by n. */
106 STATIC void
107 rend_cache_increment_allocation(size_t n)
109 static int have_overflowed = 0;
110 if (rend_cache_total_allocation <= SIZE_MAX - n) {
111 rend_cache_total_allocation += n;
112 } else {
113 rend_cache_total_allocation = SIZE_MAX;
114 if (! have_overflowed) {
115 have_overflowed = 1;
116 log_warn(LD_BUG, "Overflow in rend_cache_increment_allocation");
121 /** Helper: free a rend cache failure intro object. */
122 STATIC void
123 rend_cache_failure_intro_entry_free(rend_cache_failure_intro_t *entry)
125 if (entry == NULL) {
126 return;
128 tor_free(entry);
131 static void
132 rend_cache_failure_intro_entry_free_(void *entry)
134 rend_cache_failure_intro_entry_free(entry);
137 /** Allocate a rend cache failure intro object and return it. <b>failure</b>
138 * is set into the object. This function can not fail. */
139 STATIC rend_cache_failure_intro_t *
140 rend_cache_failure_intro_entry_new(rend_intro_point_failure_t failure)
142 rend_cache_failure_intro_t *entry = tor_malloc(sizeof(*entry));
143 entry->failure_type = failure;
144 entry->created_ts = time(NULL);
145 return entry;
148 /** Helper: free a rend cache failure object. */
149 STATIC void
150 rend_cache_failure_entry_free(rend_cache_failure_t *entry)
152 if (entry == NULL) {
153 return;
156 /* Free and remove every intro failure object. */
157 digestmap_free(entry->intro_failures,
158 rend_cache_failure_intro_entry_free_);
160 tor_free(entry);
163 /** Helper: deallocate a rend_cache_failure_t. (Used with strmap_free(),
164 * which requires a function pointer whose argument is void*). */
165 STATIC void
166 rend_cache_failure_entry_free_(void *entry)
168 rend_cache_failure_entry_free(entry);
171 /** Allocate a rend cache failure object and return it. This function can
172 * not fail. */
173 STATIC rend_cache_failure_t *
174 rend_cache_failure_entry_new(void)
176 rend_cache_failure_t *entry = tor_malloc(sizeof(*entry));
177 entry->intro_failures = digestmap_new();
178 return entry;
181 /** Remove failure cache entry for the service ID in the given descriptor
182 * <b>desc</b>. */
183 STATIC void
184 rend_cache_failure_remove(rend_service_descriptor_t *desc)
186 char service_id[REND_SERVICE_ID_LEN_BASE32 + 1];
187 rend_cache_failure_t *entry;
189 if (desc == NULL) {
190 return;
192 if (rend_get_service_id(desc->pk, service_id) < 0) {
193 return;
195 entry = strmap_get_lc(rend_cache_failure, service_id);
196 if (entry != NULL) {
197 strmap_remove_lc(rend_cache_failure, service_id);
198 rend_cache_failure_entry_free(entry);
202 /** Helper: free storage held by a single service descriptor cache entry. */
203 STATIC void
204 rend_cache_entry_free(rend_cache_entry_t *e)
206 if (!e)
207 return;
208 rend_cache_decrement_allocation(rend_cache_entry_allocation(e));
209 /* We are about to remove a descriptor from the cache so remove the entry
210 * in the failure cache. */
211 rend_cache_failure_remove(e->parsed);
212 rend_service_descriptor_free(e->parsed);
213 tor_free(e->desc);
214 tor_free(e);
217 /** Helper: deallocate a rend_cache_entry_t. (Used with strmap_free(), which
218 * requires a function pointer whose argument is void*). */
219 static void
220 rend_cache_entry_free_(void *p)
222 rend_cache_entry_free(p);
225 /** Free all storage held by the service descriptor cache. */
226 void
227 rend_cache_free_all(void)
229 strmap_free(rend_cache, rend_cache_entry_free_);
230 digestmap_free(rend_cache_v2_dir, rend_cache_entry_free_);
231 strmap_free(rend_cache_local_service, rend_cache_entry_free_);
232 strmap_free(rend_cache_failure, rend_cache_failure_entry_free_);
233 rend_cache = NULL;
234 rend_cache_v2_dir = NULL;
235 rend_cache_local_service = NULL;
236 rend_cache_failure = NULL;
237 rend_cache_total_allocation = 0;
240 /** Remove all entries that re REND_CACHE_FAILURE_MAX_AGE old. This is
241 * called every second.
243 * We have to clean these regurlarly else if for whatever reasons an hidden
244 * service goes offline and a client tries to connect to it during that
245 * time, a failure entry is created and the client will be unable to connect
246 * for a while even though the service has return online. */
247 void
248 rend_cache_failure_clean(time_t now)
250 time_t cutoff = now - REND_CACHE_FAILURE_MAX_AGE;
251 STRMAP_FOREACH_MODIFY(rend_cache_failure, key,
252 rend_cache_failure_t *, ent) {
253 /* Free and remove every intro failure object that match the cutoff. */
254 DIGESTMAP_FOREACH_MODIFY(ent->intro_failures, ip_key,
255 rend_cache_failure_intro_t *, ip_ent) {
256 if (ip_ent->created_ts < cutoff) {
257 rend_cache_failure_intro_entry_free(ip_ent);
258 MAP_DEL_CURRENT(ip_key);
260 } DIGESTMAP_FOREACH_END;
261 /* If the entry is now empty of intro point failures, remove it. */
262 if (digestmap_isempty(ent->intro_failures)) {
263 rend_cache_failure_entry_free(ent);
264 MAP_DEL_CURRENT(key);
266 } STRMAP_FOREACH_END;
269 /** Removes all old entries from the client or service descriptor cache.
271 void
272 rend_cache_clean(time_t now, rend_cache_type_t cache_type)
274 strmap_iter_t *iter;
275 const char *key;
276 void *val;
277 rend_cache_entry_t *ent;
278 time_t cutoff = now - REND_CACHE_MAX_AGE - REND_CACHE_MAX_SKEW;
279 strmap_t *cache = NULL;
281 if (cache_type == REND_CACHE_TYPE_CLIENT) {
282 cache = rend_cache;
283 } else if (cache_type == REND_CACHE_TYPE_SERVICE) {
284 cache = rend_cache_local_service;
286 tor_assert(cache);
288 for (iter = strmap_iter_init(cache); !strmap_iter_done(iter); ) {
289 strmap_iter_get(iter, &key, &val);
290 ent = (rend_cache_entry_t*)val;
291 if (ent->parsed->timestamp < cutoff) {
292 iter = strmap_iter_next_rmv(cache, iter);
293 rend_cache_entry_free(ent);
294 } else {
295 iter = strmap_iter_next(cache, iter);
300 /** Remove ALL entries from the rendezvous service descriptor cache.
302 void
303 rend_cache_purge(void)
305 if (rend_cache) {
306 log_info(LD_REND, "Purging HS descriptor cache");
307 strmap_free(rend_cache, rend_cache_entry_free_);
309 rend_cache = strmap_new();
312 /** Remove ALL entries from the failure cache. This is also called when a
313 * NEWNYM signal is received. */
314 void
315 rend_cache_failure_purge(void)
317 if (rend_cache_failure) {
318 log_info(LD_REND, "Purging HS failure cache");
319 strmap_free(rend_cache_failure, rend_cache_failure_entry_free_);
321 rend_cache_failure = strmap_new();
324 /** Lookup the rend failure cache using a relay identity digest in
325 * <b>identity</b> which has DIGEST_LEN bytes and service ID <b>service_id</b>
326 * which is a null-terminated string. If found, the intro failure is set in
327 * <b>intro_entry</b> else it stays untouched. Return 1 iff found else 0. */
328 STATIC int
329 cache_failure_intro_lookup(const uint8_t *identity, const char *service_id,
330 rend_cache_failure_intro_t **intro_entry)
332 rend_cache_failure_t *elem;
333 rend_cache_failure_intro_t *intro_elem;
335 tor_assert(rend_cache_failure);
337 if (intro_entry) {
338 *intro_entry = NULL;
341 /* Lookup descriptor and return it. */
342 elem = strmap_get_lc(rend_cache_failure, service_id);
343 if (elem == NULL) {
344 goto not_found;
346 intro_elem = digestmap_get(elem->intro_failures, (char *) identity);
347 if (intro_elem == NULL) {
348 goto not_found;
350 if (intro_entry) {
351 *intro_entry = intro_elem;
353 return 1;
354 not_found:
355 return 0;
358 /** Allocate a new cache failure intro object and copy the content from
359 * <b>entry</b> to this newly allocated object. Return it. */
360 static rend_cache_failure_intro_t *
361 cache_failure_intro_dup(const rend_cache_failure_intro_t *entry)
363 rend_cache_failure_intro_t *ent_dup =
364 rend_cache_failure_intro_entry_new(entry->failure_type);
365 ent_dup->created_ts = entry->created_ts;
366 return ent_dup;
369 /** Add an intro point failure to the failure cache using the relay
370 * <b>identity</b> and service ID <b>service_id</b>. Record the
371 * <b>failure</b> in that object. */
372 STATIC void
373 cache_failure_intro_add(const uint8_t *identity, const char *service_id,
374 rend_intro_point_failure_t failure)
376 rend_cache_failure_t *fail_entry;
377 rend_cache_failure_intro_t *entry, *old_entry;
379 /* Make sure we have a failure object for this service ID and if not,
380 * create it with this new intro failure entry. */
381 fail_entry = strmap_get_lc(rend_cache_failure, service_id);
382 if (fail_entry == NULL) {
383 fail_entry = rend_cache_failure_entry_new();
384 /* Add failure entry to global rend failure cache. */
385 strmap_set_lc(rend_cache_failure, service_id, fail_entry);
387 entry = rend_cache_failure_intro_entry_new(failure);
388 old_entry = digestmap_set(fail_entry->intro_failures,
389 (char *) identity, entry);
390 /* This _should_ be NULL, but in case it isn't, free it. */
391 rend_cache_failure_intro_entry_free(old_entry);
394 /** Using a parsed descriptor <b>desc</b>, check if the introduction points
395 * are present in the failure cache and if so they are removed from the
396 * descriptor and kept into the failure cache. Then, each intro points that
397 * are NOT in the descriptor but in the failure cache for the given
398 * <b>service_id</b> are removed from the failure cache. */
399 STATIC void
400 validate_intro_point_failure(const rend_service_descriptor_t *desc,
401 const char *service_id)
403 rend_cache_failure_t *new_entry, *cur_entry;
404 /* New entry for the service ID that will be replacing the one in the
405 * failure cache since we have a new descriptor. In the case where all
406 * intro points are removed, we are assured that the new entry is the same
407 * as the current one. */
408 new_entry = tor_malloc(sizeof(*new_entry));
409 new_entry->intro_failures = digestmap_new();
411 tor_assert(desc);
413 SMARTLIST_FOREACH_BEGIN(desc->intro_nodes, rend_intro_point_t *, intro) {
414 int found;
415 rend_cache_failure_intro_t *entry;
416 const uint8_t *identity =
417 (uint8_t *) intro->extend_info->identity_digest;
419 found = cache_failure_intro_lookup(identity, service_id, &entry);
420 if (found) {
421 /* Dup here since it will be freed at the end when removing the
422 * original entry in the cache. */
423 rend_cache_failure_intro_t *ent_dup = cache_failure_intro_dup(entry);
424 /* This intro point is in our cache, discard it from the descriptor
425 * because chances are that it's unusable. */
426 SMARTLIST_DEL_CURRENT(desc->intro_nodes, intro);
427 /* Keep it for our new entry. */
428 digestmap_set(new_entry->intro_failures, (char *) identity, ent_dup);
429 /* Only free it when we're done looking at it. */
430 rend_intro_point_free(intro);
431 continue;
433 } SMARTLIST_FOREACH_END(intro);
435 /* Swap the failure entry in the cache and free the current one. */
436 cur_entry = strmap_get_lc(rend_cache_failure, service_id);
437 if (cur_entry != NULL) {
438 rend_cache_failure_entry_free(cur_entry);
440 strmap_set_lc(rend_cache_failure, service_id, new_entry);
443 /** Note down an intro failure in the rend failure cache using the type of
444 * failure in <b>failure</b> for the relay identity digest in
445 * <b>identity</b> and service ID <b>service_id</b>. If an entry already
446 * exists in the cache, the failure type is changed with <b>failure</b>. */
447 void
448 rend_cache_intro_failure_note(rend_intro_point_failure_t failure,
449 const uint8_t *identity,
450 const char *service_id)
452 int found;
453 rend_cache_failure_intro_t *entry;
455 found = cache_failure_intro_lookup(identity, service_id, &entry);
456 if (!found) {
457 cache_failure_intro_add(identity, service_id, failure);
458 } else {
459 /* Replace introduction point failure with this one. */
460 entry->failure_type = failure;
464 /** Remove all old v2 descriptors and those for which this hidden service
465 * directory is not responsible for any more.
467 * If at all possible, remove at least <b>force_remove</b> bytes of data.
469 void
470 rend_cache_clean_v2_descs_as_dir(time_t now, size_t force_remove)
472 digestmap_iter_t *iter;
473 time_t cutoff = now - REND_CACHE_MAX_AGE - REND_CACHE_MAX_SKEW;
474 const int LAST_SERVED_CUTOFF_STEP = 1800;
475 time_t last_served_cutoff = cutoff;
476 size_t bytes_removed = 0;
477 do {
478 for (iter = digestmap_iter_init(rend_cache_v2_dir);
479 !digestmap_iter_done(iter); ) {
480 const char *key;
481 void *val;
482 rend_cache_entry_t *ent;
483 digestmap_iter_get(iter, &key, &val);
484 ent = val;
485 if (ent->parsed->timestamp < cutoff ||
486 ent->last_served < last_served_cutoff) {
487 char key_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
488 base32_encode(key_base32, sizeof(key_base32), key, DIGEST_LEN);
489 log_info(LD_REND, "Removing descriptor with ID '%s' from cache",
490 safe_str_client(key_base32));
491 bytes_removed += rend_cache_entry_allocation(ent);
492 iter = digestmap_iter_next_rmv(rend_cache_v2_dir, iter);
493 rend_cache_entry_free(ent);
494 } else {
495 iter = digestmap_iter_next(rend_cache_v2_dir, iter);
499 /* In case we didn't remove enough bytes, advance the cutoff a little. */
500 last_served_cutoff += LAST_SERVED_CUTOFF_STEP;
501 if (last_served_cutoff > now)
502 break;
503 } while (bytes_removed < force_remove);
506 /** Lookup in the client cache the given service ID <b>query</b> for
507 * <b>version</b>.
509 * Return 0 if found and if <b>e</b> is non NULL, set it with the entry
510 * found. Else, a negative value is returned and <b>e</b> is untouched.
511 * -EINVAL means that <b>query</b> is not a valid service id.
512 * -ENOENT means that no entry in the cache was found. */
514 rend_cache_lookup_entry(const char *query, int version, rend_cache_entry_t **e)
516 int ret = 0;
517 char key[REND_SERVICE_ID_LEN_BASE32 + 2]; /* <version><query>\0 */
518 rend_cache_entry_t *entry = NULL;
519 static const int default_version = 2;
521 tor_assert(rend_cache);
522 tor_assert(query);
524 if (!rend_valid_service_id(query)) {
525 ret = -EINVAL;
526 goto end;
529 switch (version) {
530 case 0:
531 log_warn(LD_REND, "Cache lookup of a v0 renddesc is deprecated.");
532 break;
533 case 2:
534 /* Default is version 2. */
535 default:
536 tor_snprintf(key, sizeof(key), "%d%s", default_version, query);
537 entry = strmap_get_lc(rend_cache, key);
538 break;
540 if (!entry) {
541 ret = -ENOENT;
542 goto end;
544 tor_assert(entry->parsed && entry->parsed->intro_nodes);
546 if (e) {
547 *e = entry;
550 end:
551 return ret;
555 * Lookup the v2 service descriptor with the service ID <b>query</b> in the
556 * local service descriptor cache. Return 0 if found and if <b>e</b> is
557 * non NULL, set it with the entry found. Else, a negative value is returned
558 * and <b>e</b> is untouched.
559 * -EINVAL means that <b>query</b> is not a valid service id.
560 * -ENOENT means that no entry in the cache was found. */
562 rend_cache_lookup_v2_desc_as_service(const char *query, rend_cache_entry_t **e)
564 int ret = 0;
565 rend_cache_entry_t *entry = NULL;
567 tor_assert(rend_cache_local_service);
568 tor_assert(query);
570 if (!rend_valid_service_id(query)) {
571 ret = -EINVAL;
572 goto end;
575 /* Lookup descriptor and return. */
576 entry = strmap_get_lc(rend_cache_local_service, query);
577 if (!entry) {
578 ret = -ENOENT;
579 goto end;
582 if (e) {
583 *e = entry;
586 end:
587 return ret;
590 /** Lookup the v2 service descriptor with base32-encoded <b>desc_id</b> and
591 * copy the pointer to it to *<b>desc</b>. Return 1 on success, 0 on
592 * well-formed-but-not-found, and -1 on failure.
595 rend_cache_lookup_v2_desc_as_dir(const char *desc_id, const char **desc)
597 rend_cache_entry_t *e;
598 char desc_id_digest[DIGEST_LEN];
599 tor_assert(rend_cache_v2_dir);
600 if (base32_decode(desc_id_digest, DIGEST_LEN,
601 desc_id, REND_DESC_ID_V2_LEN_BASE32) < 0) {
602 log_fn(LOG_PROTOCOL_WARN, LD_REND,
603 "Rejecting v2 rendezvous descriptor request -- descriptor ID "
604 "contains illegal characters: %s",
605 safe_str(desc_id));
606 return -1;
608 /* Lookup descriptor and return. */
609 e = digestmap_get(rend_cache_v2_dir, desc_id_digest);
610 if (e) {
611 *desc = e->desc;
612 e->last_served = approx_time();
613 return 1;
615 return 0;
618 /** Parse the v2 service descriptor(s) in <b>desc</b> and store it/them to the
619 * local rend cache. Don't attempt to decrypt the included list of introduction
620 * points (as we don't have a descriptor cookie for it).
622 * If we have a newer descriptor with the same ID, ignore this one.
623 * If we have an older descriptor with the same ID, replace it.
625 * Return an appropriate rend_cache_store_status_t.
627 rend_cache_store_status_t
628 rend_cache_store_v2_desc_as_dir(const char *desc)
630 const or_options_t *options = get_options();
631 rend_service_descriptor_t *parsed;
632 char desc_id[DIGEST_LEN];
633 char *intro_content;
634 size_t intro_size;
635 size_t encoded_size;
636 char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
637 int number_parsed = 0, number_stored = 0;
638 const char *current_desc = desc;
639 const char *next_desc;
640 rend_cache_entry_t *e;
641 time_t now = time(NULL);
642 tor_assert(rend_cache_v2_dir);
643 tor_assert(desc);
644 while (rend_parse_v2_service_descriptor(&parsed, desc_id, &intro_content,
645 &intro_size, &encoded_size,
646 &next_desc, current_desc, 1) >= 0) {
647 number_parsed++;
648 /* We don't care about the introduction points. */
649 tor_free(intro_content);
650 /* For pretty log statements. */
651 base32_encode(desc_id_base32, sizeof(desc_id_base32),
652 desc_id, DIGEST_LEN);
653 /* Is descriptor too old? */
654 if (parsed->timestamp < now - REND_CACHE_MAX_AGE-REND_CACHE_MAX_SKEW) {
655 log_info(LD_REND, "Service descriptor with desc ID %s is too old.",
656 safe_str(desc_id_base32));
657 goto skip;
659 /* Is descriptor too far in the future? */
660 if (parsed->timestamp > now + REND_CACHE_MAX_SKEW) {
661 log_info(LD_REND, "Service descriptor with desc ID %s is too far in the "
662 "future.",
663 safe_str(desc_id_base32));
664 goto skip;
666 /* Do we already have a newer descriptor? */
667 e = digestmap_get(rend_cache_v2_dir, desc_id);
668 if (e && e->parsed->timestamp > parsed->timestamp) {
669 log_info(LD_REND, "We already have a newer service descriptor with the "
670 "same desc ID %s and version.",
671 safe_str(desc_id_base32));
672 goto skip;
674 /* Do we already have this descriptor? */
675 if (e && !strcmp(desc, e->desc)) {
676 log_info(LD_REND, "We already have this service descriptor with desc "
677 "ID %s.", safe_str(desc_id_base32));
678 goto skip;
680 /* Store received descriptor. */
681 if (!e) {
682 e = tor_malloc_zero(sizeof(rend_cache_entry_t));
683 digestmap_set(rend_cache_v2_dir, desc_id, e);
684 /* Treat something just uploaded as having been served a little
685 * while ago, so that flooding with new descriptors doesn't help
686 * too much.
688 e->last_served = approx_time() - 3600;
689 } else {
690 rend_cache_decrement_allocation(rend_cache_entry_allocation(e));
691 rend_service_descriptor_free(e->parsed);
692 tor_free(e->desc);
694 e->parsed = parsed;
695 e->desc = tor_strndup(current_desc, encoded_size);
696 e->len = encoded_size;
697 rend_cache_increment_allocation(rend_cache_entry_allocation(e));
698 log_info(LD_REND, "Successfully stored service descriptor with desc ID "
699 "'%s' and len %d.",
700 safe_str(desc_id_base32), (int)encoded_size);
701 /* Statistics: Note down this potentially new HS. */
702 if (options->HiddenServiceStatistics) {
703 rep_hist_stored_maybe_new_hs(e->parsed->pk);
706 number_stored++;
707 goto advance;
708 skip:
709 rend_service_descriptor_free(parsed);
710 advance:
711 /* advance to next descriptor, if available. */
712 current_desc = next_desc;
713 /* check if there is a next descriptor. */
714 if (!current_desc ||
715 strcmpstart(current_desc, "rendezvous-service-descriptor "))
716 break;
718 if (!number_parsed) {
719 log_info(LD_REND, "Could not parse any descriptor.");
720 return RCS_BADDESC;
722 log_info(LD_REND, "Parsed %d and added %d descriptor%s.",
723 number_parsed, number_stored, number_stored != 1 ? "s" : "");
724 return RCS_OKAY;
727 /** Parse the v2 service descriptor in <b>desc</b> and store it to the
728 * local service rend cache. Don't attempt to decrypt the included list of
729 * introduction points.
731 * If we have a newer descriptor with the same ID, ignore this one.
732 * If we have an older descriptor with the same ID, replace it.
734 * Return an appropriate rend_cache_store_status_t.
736 rend_cache_store_status_t
737 rend_cache_store_v2_desc_as_service(const char *desc)
739 rend_service_descriptor_t *parsed = NULL;
740 char desc_id[DIGEST_LEN];
741 char *intro_content = NULL;
742 size_t intro_size;
743 size_t encoded_size;
744 const char *next_desc;
745 char service_id[REND_SERVICE_ID_LEN_BASE32+1];
746 rend_cache_entry_t *e;
747 rend_cache_store_status_t retval = RCS_BADDESC;
748 tor_assert(rend_cache_local_service);
749 tor_assert(desc);
751 /* Parse the descriptor. */
752 if (rend_parse_v2_service_descriptor(&parsed, desc_id, &intro_content,
753 &intro_size, &encoded_size,
754 &next_desc, desc, 0) < 0) {
755 log_warn(LD_REND, "Could not parse descriptor.");
756 goto err;
758 /* Compute service ID from public key. */
759 if (rend_get_service_id(parsed->pk, service_id)<0) {
760 log_warn(LD_REND, "Couldn't compute service ID.");
761 goto err;
764 /* Do we already have a newer descriptor? Allow new descriptors with a
765 rounded timestamp equal to or newer than the current descriptor */
766 e = (rend_cache_entry_t*) strmap_get_lc(rend_cache_local_service,
767 service_id);
768 if (e && e->parsed->timestamp > parsed->timestamp) {
769 log_info(LD_REND, "We already have a newer service descriptor for "
770 "service ID %s.", safe_str_client(service_id));
771 goto okay;
773 /* We don't care about the introduction points. */
774 tor_free(intro_content);
775 if (!e) {
776 e = tor_malloc_zero(sizeof(rend_cache_entry_t));
777 strmap_set_lc(rend_cache_local_service, service_id, e);
778 } else {
779 rend_cache_decrement_allocation(rend_cache_entry_allocation(e));
780 rend_service_descriptor_free(e->parsed);
781 tor_free(e->desc);
783 e->parsed = parsed;
784 e->desc = tor_malloc_zero(encoded_size + 1);
785 strlcpy(e->desc, desc, encoded_size + 1);
786 e->len = encoded_size;
787 rend_cache_increment_allocation(rend_cache_entry_allocation(e));
788 log_debug(LD_REND,"Successfully stored rend desc '%s', len %d.",
789 safe_str_client(service_id), (int)encoded_size);
790 return RCS_OKAY;
792 okay:
793 retval = RCS_OKAY;
795 err:
796 rend_service_descriptor_free(parsed);
797 tor_free(intro_content);
798 return retval;
801 /** Parse the v2 service descriptor in <b>desc</b>, decrypt the included list
802 * of introduction points with <b>descriptor_cookie</b> (which may also be
803 * <b>NULL</b> if decryption is not necessary), and store the descriptor to
804 * the local cache under its version and service id.
806 * If we have a newer v2 descriptor with the same ID, ignore this one.
807 * If we have an older descriptor with the same ID, replace it.
808 * If the descriptor's service ID does not match
809 * <b>rend_query</b>-\>onion_address, reject it.
811 * If the descriptor's descriptor ID doesn't match <b>desc_id_base32</b>,
812 * reject it.
814 * Return an appropriate rend_cache_store_status_t. If entry is not NULL,
815 * set it with the cache entry pointer of the descriptor.
817 rend_cache_store_status_t
818 rend_cache_store_v2_desc_as_client(const char *desc,
819 const char *desc_id_base32,
820 const rend_data_t *rend_query,
821 rend_cache_entry_t **entry)
823 /*XXXX this seems to have a bit of duplicate code with
824 * rend_cache_store_v2_desc_as_dir(). Fix that. */
825 /* Though having similar elements, both functions were separated on
826 * purpose:
827 * - dirs don't care about encoded/encrypted introduction points, clients
828 * do.
829 * - dirs store descriptors in a separate cache by descriptor ID, whereas
830 * clients store them by service ID; both caches are different data
831 * structures and have different access methods.
832 * - dirs store a descriptor only if they are responsible for its ID,
833 * clients do so in every way (because they have requested it before).
834 * - dirs can process multiple concatenated descriptors which is required
835 * for replication, whereas clients only accept a single descriptor.
836 * Thus, combining both methods would result in a lot of if statements
837 * which probably would not improve, but worsen code readability. -KL */
838 rend_service_descriptor_t *parsed = NULL;
839 char desc_id[DIGEST_LEN];
840 char *intro_content = NULL;
841 size_t intro_size;
842 size_t encoded_size;
843 const char *next_desc;
844 time_t now = time(NULL);
845 char key[REND_SERVICE_ID_LEN_BASE32+2];
846 char service_id[REND_SERVICE_ID_LEN_BASE32+1];
847 char want_desc_id[DIGEST_LEN];
848 rend_cache_entry_t *e;
849 rend_cache_store_status_t retval = RCS_BADDESC;
850 tor_assert(rend_cache);
851 tor_assert(desc);
852 tor_assert(desc_id_base32);
853 memset(want_desc_id, 0, sizeof(want_desc_id));
854 if (entry) {
855 *entry = NULL;
857 if (base32_decode(want_desc_id, sizeof(want_desc_id),
858 desc_id_base32, strlen(desc_id_base32)) != 0) {
859 log_warn(LD_BUG, "Couldn't decode base32 %s for descriptor id.",
860 escaped_safe_str_client(desc_id_base32));
861 goto err;
863 /* Parse the descriptor. */
864 if (rend_parse_v2_service_descriptor(&parsed, desc_id, &intro_content,
865 &intro_size, &encoded_size,
866 &next_desc, desc, 0) < 0) {
867 log_warn(LD_REND, "Could not parse descriptor.");
868 goto err;
870 /* Compute service ID from public key. */
871 if (rend_get_service_id(parsed->pk, service_id)<0) {
872 log_warn(LD_REND, "Couldn't compute service ID.");
873 goto err;
875 if (rend_query->onion_address[0] != '\0' &&
876 strcmp(rend_query->onion_address, service_id)) {
877 log_warn(LD_REND, "Received service descriptor for service ID %s; "
878 "expected descriptor for service ID %s.",
879 service_id, safe_str(rend_query->onion_address));
880 goto err;
882 if (tor_memneq(desc_id, want_desc_id, DIGEST_LEN)) {
883 log_warn(LD_REND, "Received service descriptor for %s with incorrect "
884 "descriptor ID.", service_id);
885 goto err;
888 /* Decode/decrypt introduction points. */
889 if (intro_content && intro_size > 0) {
890 int n_intro_points;
891 if (rend_query->auth_type != REND_NO_AUTH &&
892 !tor_mem_is_zero(rend_query->descriptor_cookie,
893 sizeof(rend_query->descriptor_cookie))) {
894 char *ipos_decrypted = NULL;
895 size_t ipos_decrypted_size;
896 if (rend_decrypt_introduction_points(&ipos_decrypted,
897 &ipos_decrypted_size,
898 rend_query->descriptor_cookie,
899 intro_content,
900 intro_size) < 0) {
901 log_warn(LD_REND, "Failed to decrypt introduction points. We are "
902 "probably unable to parse the encoded introduction points.");
903 } else {
904 /* Replace encrypted with decrypted introduction points. */
905 log_info(LD_REND, "Successfully decrypted introduction points.");
906 tor_free(intro_content);
907 intro_content = ipos_decrypted;
908 intro_size = ipos_decrypted_size;
911 n_intro_points = rend_parse_introduction_points(parsed, intro_content,
912 intro_size);
913 if (n_intro_points <= 0) {
914 log_warn(LD_REND, "Failed to parse introduction points. Either the "
915 "service has published a corrupt descriptor or you have "
916 "provided invalid authorization data.");
917 goto err;
918 } else if (n_intro_points > MAX_INTRO_POINTS) {
919 log_warn(LD_REND, "Found too many introduction points on a hidden "
920 "service descriptor for %s. This is probably a (misguided) "
921 "attempt to improve reliability, but it could also be an "
922 "attempt to do a guard enumeration attack. Rejecting.",
923 safe_str_client(service_id));
925 goto err;
927 } else {
928 log_info(LD_REND, "Descriptor does not contain any introduction points.");
929 parsed->intro_nodes = smartlist_new();
931 /* We don't need the encoded/encrypted introduction points any longer. */
932 tor_free(intro_content);
933 /* Is descriptor too old? */
934 if (parsed->timestamp < now - REND_CACHE_MAX_AGE-REND_CACHE_MAX_SKEW) {
935 log_warn(LD_REND, "Service descriptor with service ID %s is too old.",
936 safe_str_client(service_id));
937 goto err;
939 /* Is descriptor too far in the future? */
940 if (parsed->timestamp > now + REND_CACHE_MAX_SKEW) {
941 log_warn(LD_REND, "Service descriptor with service ID %s is too far in "
942 "the future.", safe_str_client(service_id));
943 goto err;
945 /* Do we have the same exact copy already in our cache? */
946 tor_snprintf(key, sizeof(key), "2%s", service_id);
947 e = (rend_cache_entry_t*) strmap_get_lc(rend_cache, key);
948 if (e && !strcmp(desc, e->desc)) {
949 log_info(LD_REND,"We already have this service descriptor %s.",
950 safe_str_client(service_id));
951 goto okay;
953 /* Verify that we are not replacing an older descriptor. It's important to
954 * avoid an evil HSDir serving old descriptor. We validate if the
955 * timestamp is greater than and not equal because it's a rounded down
956 * timestamp to the hour so if the descriptor changed in the same hour,
957 * the rend cache failure will tells us if we have a new descriptor. */
958 if (e && e->parsed->timestamp > parsed->timestamp) {
959 log_info(LD_REND, "We already have a new enough service descriptor for "
960 "service ID %s with the same desc ID and version.",
961 safe_str_client(service_id));
962 goto okay;
964 /* Lookup our failure cache for intro point that might be unsuable. */
965 validate_intro_point_failure(parsed, service_id);
966 /* It's now possible that our intro point list is empty, this means that
967 * this descriptor is useless to us because intro points have all failed
968 * somehow before. Discard the descriptor. */
969 if (smartlist_len(parsed->intro_nodes) == 0) {
970 log_info(LD_REND, "Service descriptor with service ID %s, every "
971 "intro points are unusable. Discarding it.",
972 safe_str_client(service_id));
973 goto err;
975 /* Now either purge the current one and replace it's content or create a
976 * new one and add it to the rend cache. */
977 if (!e) {
978 e = tor_malloc_zero(sizeof(rend_cache_entry_t));
979 strmap_set_lc(rend_cache, key, e);
980 } else {
981 rend_cache_decrement_allocation(rend_cache_entry_allocation(e));
982 rend_cache_failure_remove(e->parsed);
983 rend_service_descriptor_free(e->parsed);
984 tor_free(e->desc);
986 e->parsed = parsed;
987 e->desc = tor_malloc_zero(encoded_size + 1);
988 strlcpy(e->desc, desc, encoded_size + 1);
989 e->len = encoded_size;
990 rend_cache_increment_allocation(rend_cache_entry_allocation(e));
991 log_debug(LD_REND,"Successfully stored rend desc '%s', len %d.",
992 safe_str_client(service_id), (int)encoded_size);
993 if (entry) {
994 *entry = e;
996 return RCS_OKAY;
998 okay:
999 if (entry) {
1000 *entry = e;
1002 retval = RCS_OKAY;
1004 err:
1005 rend_service_descriptor_free(parsed);
1006 tor_free(intro_content);
1007 return retval;