Merge remote-tracking branch 'public/bug23985_029' into maint-0.2.9
[tor.git] / src / or / rendcache.c
blobaa69d735fe096ea6346d56736ca21fa7d7a38a97
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 0 on success, or -1 if we couldn't parse any of them.
627 * We should only call this function for public (e.g. non bridge) relays.
630 rend_cache_store_v2_desc_as_dir(const char *desc)
632 const or_options_t *options = get_options();
633 rend_service_descriptor_t *parsed;
634 char desc_id[DIGEST_LEN];
635 char *intro_content;
636 size_t intro_size;
637 size_t encoded_size;
638 char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
639 int number_parsed = 0, number_stored = 0;
640 const char *current_desc = desc;
641 const char *next_desc;
642 rend_cache_entry_t *e;
643 time_t now = time(NULL);
644 tor_assert(rend_cache_v2_dir);
645 tor_assert(desc);
646 while (rend_parse_v2_service_descriptor(&parsed, desc_id, &intro_content,
647 &intro_size, &encoded_size,
648 &next_desc, current_desc, 1) >= 0) {
649 number_parsed++;
650 /* We don't care about the introduction points. */
651 tor_free(intro_content);
652 /* For pretty log statements. */
653 base32_encode(desc_id_base32, sizeof(desc_id_base32),
654 desc_id, DIGEST_LEN);
655 /* Is descriptor too old? */
656 if (parsed->timestamp < now - REND_CACHE_MAX_AGE-REND_CACHE_MAX_SKEW) {
657 log_info(LD_REND, "Service descriptor with desc ID %s is too old.",
658 safe_str(desc_id_base32));
659 goto skip;
661 /* Is descriptor too far in the future? */
662 if (parsed->timestamp > now + REND_CACHE_MAX_SKEW) {
663 log_info(LD_REND, "Service descriptor with desc ID %s is too far in the "
664 "future.",
665 safe_str(desc_id_base32));
666 goto skip;
668 /* Do we already have a newer descriptor? */
669 e = digestmap_get(rend_cache_v2_dir, desc_id);
670 if (e && e->parsed->timestamp > parsed->timestamp) {
671 log_info(LD_REND, "We already have a newer service descriptor with the "
672 "same desc ID %s and version.",
673 safe_str(desc_id_base32));
674 goto skip;
676 /* Do we already have this descriptor? */
677 if (e && !strcmp(desc, e->desc)) {
678 log_info(LD_REND, "We already have this service descriptor with desc "
679 "ID %s.", safe_str(desc_id_base32));
680 goto skip;
682 /* Store received descriptor. */
683 if (!e) {
684 e = tor_malloc_zero(sizeof(rend_cache_entry_t));
685 digestmap_set(rend_cache_v2_dir, desc_id, e);
686 /* Treat something just uploaded as having been served a little
687 * while ago, so that flooding with new descriptors doesn't help
688 * too much.
690 e->last_served = approx_time() - 3600;
691 } else {
692 rend_cache_decrement_allocation(rend_cache_entry_allocation(e));
693 rend_service_descriptor_free(e->parsed);
694 tor_free(e->desc);
696 e->parsed = parsed;
697 e->desc = tor_strndup(current_desc, encoded_size);
698 e->len = encoded_size;
699 rend_cache_increment_allocation(rend_cache_entry_allocation(e));
700 log_info(LD_REND, "Successfully stored service descriptor with desc ID "
701 "'%s' and len %d.",
702 safe_str(desc_id_base32), (int)encoded_size);
703 /* Statistics: Note down this potentially new HS. */
704 if (options->HiddenServiceStatistics) {
705 rep_hist_stored_maybe_new_hs(e->parsed->pk);
708 number_stored++;
709 goto advance;
710 skip:
711 rend_service_descriptor_free(parsed);
712 advance:
713 /* advance to next descriptor, if available. */
714 current_desc = next_desc;
715 /* check if there is a next descriptor. */
716 if (!current_desc ||
717 strcmpstart(current_desc, "rendezvous-service-descriptor "))
718 break;
720 if (!number_parsed) {
721 log_info(LD_REND, "Could not parse any descriptor.");
722 return -1;
724 log_info(LD_REND, "Parsed %d and added %d descriptor%s.",
725 number_parsed, number_stored, number_stored != 1 ? "s" : "");
726 return 0;
729 /** Parse the v2 service descriptor in <b>desc</b> and store it to the
730 * local service rend cache. Don't attempt to decrypt the included list of
731 * introduction points.
733 * If we have a newer descriptor with the same ID, ignore this one.
734 * If we have an older descriptor with the same ID, replace it.
736 * Return 0 on success, or -1 if we couldn't understand the descriptor.
739 rend_cache_store_v2_desc_as_service(const char *desc)
741 rend_service_descriptor_t *parsed = NULL;
742 char desc_id[DIGEST_LEN];
743 char *intro_content = NULL;
744 size_t intro_size;
745 size_t encoded_size;
746 const char *next_desc;
747 char service_id[REND_SERVICE_ID_LEN_BASE32+1];
748 rend_cache_entry_t *e;
749 int retval = -1;
750 tor_assert(rend_cache_local_service);
751 tor_assert(desc);
753 /* Parse the descriptor. */
754 if (rend_parse_v2_service_descriptor(&parsed, desc_id, &intro_content,
755 &intro_size, &encoded_size,
756 &next_desc, desc, 0) < 0) {
757 log_warn(LD_REND, "Could not parse descriptor.");
758 goto err;
760 /* Compute service ID from public key. */
761 if (rend_get_service_id(parsed->pk, service_id)<0) {
762 log_warn(LD_REND, "Couldn't compute service ID.");
763 goto err;
766 /* Do we already have a newer descriptor? Allow new descriptors with a
767 rounded timestamp equal to or newer than the current descriptor */
768 e = (rend_cache_entry_t*) strmap_get_lc(rend_cache_local_service,
769 service_id);
770 if (e && e->parsed->timestamp > parsed->timestamp) {
771 log_info(LD_REND, "We already have a newer service descriptor for "
772 "service ID %s.", safe_str_client(service_id));
773 goto okay;
775 /* We don't care about the introduction points. */
776 tor_free(intro_content);
777 if (!e) {
778 e = tor_malloc_zero(sizeof(rend_cache_entry_t));
779 strmap_set_lc(rend_cache_local_service, service_id, e);
780 } else {
781 rend_cache_decrement_allocation(rend_cache_entry_allocation(e));
782 rend_service_descriptor_free(e->parsed);
783 tor_free(e->desc);
785 e->parsed = parsed;
786 e->desc = tor_malloc_zero(encoded_size + 1);
787 strlcpy(e->desc, desc, encoded_size + 1);
788 e->len = encoded_size;
789 rend_cache_increment_allocation(rend_cache_entry_allocation(e));
790 log_debug(LD_REND,"Successfully stored rend desc '%s', len %d.",
791 safe_str_client(service_id), (int)encoded_size);
792 return 0;
794 okay:
795 retval = 0;
797 err:
798 rend_service_descriptor_free(parsed);
799 tor_free(intro_content);
800 return retval;
803 /** Parse the v2 service descriptor in <b>desc</b>, decrypt the included list
804 * of introduction points with <b>descriptor_cookie</b> (which may also be
805 * <b>NULL</b> if decryption is not necessary), and store the descriptor to
806 * the local cache under its version and service id.
808 * If we have a newer v2 descriptor with the same ID, ignore this one.
809 * If we have an older descriptor with the same ID, replace it.
810 * If the descriptor's service ID does not match
811 * <b>rend_query</b>-\>onion_address, reject it.
813 * If the descriptor's descriptor ID doesn't match <b>desc_id_base32</b>,
814 * reject it.
816 * Return 0 on success, or -1 if we rejected the descriptor.
817 * If entry is not NULL, set it with the cache entry pointer of the descriptor.
820 rend_cache_store_v2_desc_as_client(const char *desc,
821 const char *desc_id_base32,
822 const rend_data_t *rend_query,
823 rend_cache_entry_t **entry)
825 /*XXXX this seems to have a bit of duplicate code with
826 * rend_cache_store_v2_desc_as_dir(). Fix that. */
827 /* Though having similar elements, both functions were separated on
828 * purpose:
829 * - dirs don't care about encoded/encrypted introduction points, clients
830 * do.
831 * - dirs store descriptors in a separate cache by descriptor ID, whereas
832 * clients store them by service ID; both caches are different data
833 * structures and have different access methods.
834 * - dirs store a descriptor only if they are responsible for its ID,
835 * clients do so in every way (because they have requested it before).
836 * - dirs can process multiple concatenated descriptors which is required
837 * for replication, whereas clients only accept a single descriptor.
838 * Thus, combining both methods would result in a lot of if statements
839 * which probably would not improve, but worsen code readability. -KL */
840 rend_service_descriptor_t *parsed = NULL;
841 char desc_id[DIGEST_LEN];
842 char *intro_content = NULL;
843 size_t intro_size;
844 size_t encoded_size;
845 const char *next_desc;
846 time_t now = time(NULL);
847 char key[REND_SERVICE_ID_LEN_BASE32+2];
848 char service_id[REND_SERVICE_ID_LEN_BASE32+1];
849 char want_desc_id[DIGEST_LEN];
850 rend_cache_entry_t *e;
851 int retval = -1;
852 tor_assert(rend_cache);
853 tor_assert(desc);
854 tor_assert(desc_id_base32);
855 memset(want_desc_id, 0, sizeof(want_desc_id));
856 if (entry) {
857 *entry = NULL;
859 if (base32_decode(want_desc_id, sizeof(want_desc_id),
860 desc_id_base32, strlen(desc_id_base32)) != 0) {
861 log_warn(LD_BUG, "Couldn't decode base32 %s for descriptor id.",
862 escaped_safe_str_client(desc_id_base32));
863 goto err;
865 /* Parse the descriptor. */
866 if (rend_parse_v2_service_descriptor(&parsed, desc_id, &intro_content,
867 &intro_size, &encoded_size,
868 &next_desc, desc, 0) < 0) {
869 log_warn(LD_REND, "Could not parse descriptor.");
870 goto err;
872 /* Compute service ID from public key. */
873 if (rend_get_service_id(parsed->pk, service_id)<0) {
874 log_warn(LD_REND, "Couldn't compute service ID.");
875 goto err;
877 if (rend_query->onion_address[0] != '\0' &&
878 strcmp(rend_query->onion_address, service_id)) {
879 log_warn(LD_REND, "Received service descriptor for service ID %s; "
880 "expected descriptor for service ID %s.",
881 service_id, safe_str(rend_query->onion_address));
882 goto err;
884 if (tor_memneq(desc_id, want_desc_id, DIGEST_LEN)) {
885 log_warn(LD_REND, "Received service descriptor for %s with incorrect "
886 "descriptor ID.", service_id);
887 goto err;
890 /* Decode/decrypt introduction points. */
891 if (intro_content && intro_size > 0) {
892 int n_intro_points;
893 if (rend_query->auth_type != REND_NO_AUTH &&
894 !tor_mem_is_zero(rend_query->descriptor_cookie,
895 sizeof(rend_query->descriptor_cookie))) {
896 char *ipos_decrypted = NULL;
897 size_t ipos_decrypted_size;
898 if (rend_decrypt_introduction_points(&ipos_decrypted,
899 &ipos_decrypted_size,
900 rend_query->descriptor_cookie,
901 intro_content,
902 intro_size) < 0) {
903 log_warn(LD_REND, "Failed to decrypt introduction points. We are "
904 "probably unable to parse the encoded introduction points.");
905 } else {
906 /* Replace encrypted with decrypted introduction points. */
907 log_info(LD_REND, "Successfully decrypted introduction points.");
908 tor_free(intro_content);
909 intro_content = ipos_decrypted;
910 intro_size = ipos_decrypted_size;
913 n_intro_points = rend_parse_introduction_points(parsed, intro_content,
914 intro_size);
915 if (n_intro_points <= 0) {
916 log_warn(LD_REND, "Failed to parse introduction points. Either the "
917 "service has published a corrupt descriptor or you have "
918 "provided invalid authorization data, or (maybe!) the "
919 "server is deliberately serving broken data in an attempt "
920 "to crash you with bug 21018.");
921 goto err;
922 } else if (n_intro_points > MAX_INTRO_POINTS) {
923 log_warn(LD_REND, "Found too many introduction points on a hidden "
924 "service descriptor for %s. This is probably a (misguided) "
925 "attempt to improve reliability, but it could also be an "
926 "attempt to do a guard enumeration attack. Rejecting.",
927 safe_str_client(service_id));
929 goto err;
931 } else {
932 log_info(LD_REND, "Descriptor does not contain any introduction points.");
933 parsed->intro_nodes = smartlist_new();
935 /* We don't need the encoded/encrypted introduction points any longer. */
936 tor_free(intro_content);
937 /* Is descriptor too old? */
938 if (parsed->timestamp < now - REND_CACHE_MAX_AGE-REND_CACHE_MAX_SKEW) {
939 log_warn(LD_REND, "Service descriptor with service ID %s is too old.",
940 safe_str_client(service_id));
941 goto err;
943 /* Is descriptor too far in the future? */
944 if (parsed->timestamp > now + REND_CACHE_MAX_SKEW) {
945 log_warn(LD_REND, "Service descriptor with service ID %s is too far in "
946 "the future.", safe_str_client(service_id));
947 goto err;
949 /* Do we have the same exact copy already in our cache? */
950 tor_snprintf(key, sizeof(key), "2%s", service_id);
951 e = (rend_cache_entry_t*) strmap_get_lc(rend_cache, key);
952 if (e && !strcmp(desc, e->desc)) {
953 log_info(LD_REND,"We already have this service descriptor %s.",
954 safe_str_client(service_id));
955 goto okay;
957 /* Verify that we are not replacing an older descriptor. It's important to
958 * avoid an evil HSDir serving old descriptor. We validate if the
959 * timestamp is greater than and not equal because it's a rounded down
960 * timestamp to the hour so if the descriptor changed in the same hour,
961 * the rend cache failure will tell us if we have a new descriptor. */
962 if (e && e->parsed->timestamp > parsed->timestamp) {
963 log_info(LD_REND, "We already have a new enough service descriptor for "
964 "service ID %s with the same desc ID and version.",
965 safe_str_client(service_id));
966 goto okay;
968 /* Lookup our failure cache for intro point that might be unusable. */
969 validate_intro_point_failure(parsed, service_id);
970 /* It's now possible that our intro point list is empty, which means that
971 * this descriptor is useless to us because intro points have all failed
972 * somehow before. Discard the descriptor. */
973 if (smartlist_len(parsed->intro_nodes) == 0) {
974 log_info(LD_REND, "Service descriptor with service ID %s has no "
975 "usable intro points. Discarding it.",
976 safe_str_client(service_id));
977 goto err;
979 /* Now either purge the current one and replace its content or create a
980 * new one and add it to the rend cache. */
981 if (!e) {
982 e = tor_malloc_zero(sizeof(rend_cache_entry_t));
983 strmap_set_lc(rend_cache, key, e);
984 } else {
985 rend_cache_decrement_allocation(rend_cache_entry_allocation(e));
986 rend_cache_failure_remove(e->parsed);
987 rend_service_descriptor_free(e->parsed);
988 tor_free(e->desc);
990 e->parsed = parsed;
991 e->desc = tor_malloc_zero(encoded_size + 1);
992 strlcpy(e->desc, desc, encoded_size + 1);
993 e->len = encoded_size;
994 rend_cache_increment_allocation(rend_cache_entry_allocation(e));
995 log_debug(LD_REND,"Successfully stored rend desc '%s', len %d.",
996 safe_str_client(service_id), (int)encoded_size);
997 if (entry) {
998 *entry = e;
1000 return 0;
1002 okay:
1003 if (entry) {
1004 *entry = e;
1006 retval = 0;
1008 err:
1009 rend_service_descriptor_free(parsed);
1010 tor_free(intro_content);
1011 return retval;