2007-12-12 22:09:01 +01:00
|
|
|
/* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2017-03-15 21:13:17 +01:00
|
|
|
* Copyright (c) 2007-2017, The Tor Project, Inc. */
|
2004-03-31 23:54:56 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/**
|
|
|
|
* \file rendservice.c
|
|
|
|
* \brief The hidden-service side of rendezvous functionality.
|
|
|
|
**/
|
2004-03-31 23:54:56 +02:00
|
|
|
|
2012-07-15 11:36:36 +02:00
|
|
|
#define RENDSERVICE_PRIVATE
|
|
|
|
|
2004-03-31 23:54:56 +02:00
|
|
|
#include "or.h"
|
2013-10-31 19:33:34 +01:00
|
|
|
#include "circpathbias.h"
|
2010-07-22 01:21:00 +02:00
|
|
|
#include "circuitbuild.h"
|
2010-07-22 09:46:23 +02:00
|
|
|
#include "circuitlist.h"
|
2010-07-22 10:03:40 +02:00
|
|
|
#include "circuituse.h"
|
2010-07-22 10:22:51 +02:00
|
|
|
#include "config.h"
|
2015-03-22 14:31:53 +01:00
|
|
|
#include "control.h"
|
2010-07-22 11:54:50 +02:00
|
|
|
#include "directory.h"
|
2016-05-31 20:51:30 +02:00
|
|
|
#include "hs_common.h"
|
2014-11-20 17:51:36 +01:00
|
|
|
#include "main.h"
|
2010-07-23 20:18:55 +02:00
|
|
|
#include "networkstatus.h"
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
#include "nodelist.h"
|
2016-07-14 06:04:02 +02:00
|
|
|
#include "policies.h"
|
2010-07-22 00:13:51 +02:00
|
|
|
#include "rendclient.h"
|
2010-07-21 17:52:54 +02:00
|
|
|
#include "rendcommon.h"
|
2010-07-22 00:30:17 +02:00
|
|
|
#include "rendservice.h"
|
2010-07-21 16:17:10 +02:00
|
|
|
#include "router.h"
|
2010-07-23 21:53:11 +02:00
|
|
|
#include "relay.h"
|
2010-07-23 22:57:20 +02:00
|
|
|
#include "rephist.h"
|
2012-07-11 04:18:47 +02:00
|
|
|
#include "replaycache.h"
|
2010-07-21 17:08:11 +02:00
|
|
|
#include "routerlist.h"
|
2010-07-23 23:23:43 +02:00
|
|
|
#include "routerparse.h"
|
2012-09-13 18:46:39 +02:00
|
|
|
#include "routerset.h"
|
2004-03-31 23:54:56 +02:00
|
|
|
|
2015-06-15 23:11:57 +02:00
|
|
|
struct rend_service_t;
|
2007-12-21 10:28:22 +01:00
|
|
|
static origin_circuit_t *find_intro_circuit(rend_intro_point_t *intro,
|
2009-05-03 01:04:25 +02:00
|
|
|
const char *pk_digest);
|
2011-10-06 08:52:14 +02:00
|
|
|
static rend_intro_point_t *find_intro_point(origin_circuit_t *circ);
|
2015-06-15 23:11:57 +02:00
|
|
|
static rend_intro_point_t *find_expiring_intro_point(
|
|
|
|
struct rend_service_t *service, origin_circuit_t *circ);
|
2004-04-13 19:16:47 +02:00
|
|
|
|
2012-07-15 11:36:36 +02:00
|
|
|
static extend_info_t *find_rp_for_intro(
|
|
|
|
const rend_intro_cell_t *intro,
|
2015-09-01 15:30:48 +02:00
|
|
|
char **err_msg_out);
|
2012-07-15 11:36:36 +02:00
|
|
|
|
2011-12-13 12:36:48 +01:00
|
|
|
static int intro_point_accepted_intro_count(rend_intro_point_t *intro);
|
2011-10-16 00:40:28 +02:00
|
|
|
static int intro_point_should_expire_now(rend_intro_point_t *intro,
|
|
|
|
time_t now);
|
2015-04-25 10:23:15 +02:00
|
|
|
static int rend_service_derive_key_digests(struct rend_service_t *s);
|
2012-06-18 18:43:20 +02:00
|
|
|
static int rend_service_load_keys(struct rend_service_t *s);
|
2012-06-18 19:05:28 +02:00
|
|
|
static int rend_service_load_auth_keys(struct rend_service_t *s,
|
|
|
|
const char *hfname);
|
2015-04-25 10:23:15 +02:00
|
|
|
static struct rend_service_t *rend_service_get_by_pk_digest(
|
|
|
|
const char* digest);
|
|
|
|
static struct rend_service_t *rend_service_get_by_service_id(const char *id);
|
|
|
|
static const char *rend_service_escaped_dir(
|
|
|
|
const struct rend_service_t *s);
|
2004-04-13 19:16:47 +02:00
|
|
|
|
2012-07-15 11:36:36 +02:00
|
|
|
static ssize_t rend_service_parse_intro_for_v0_or_v1(
|
|
|
|
rend_intro_cell_t *intro,
|
|
|
|
const uint8_t *buf,
|
|
|
|
size_t plaintext_len,
|
|
|
|
char **err_msg_out);
|
|
|
|
static ssize_t rend_service_parse_intro_for_v2(
|
|
|
|
rend_intro_cell_t *intro,
|
|
|
|
const uint8_t *buf,
|
|
|
|
size_t plaintext_len,
|
|
|
|
char **err_msg_out);
|
|
|
|
static ssize_t rend_service_parse_intro_for_v3(
|
|
|
|
rend_intro_cell_t *intro,
|
|
|
|
const uint8_t *buf,
|
|
|
|
size_t plaintext_len,
|
|
|
|
char **err_msg_out);
|
|
|
|
|
2016-11-02 04:55:14 +01:00
|
|
|
static int rend_service_check_private_dir(const or_options_t *options,
|
|
|
|
const rend_service_t *s,
|
|
|
|
int create);
|
2016-11-18 01:46:01 +01:00
|
|
|
static int rend_service_check_private_dir_impl(const or_options_t *options,
|
|
|
|
const rend_service_t *s,
|
|
|
|
int create);
|
2016-11-18 01:50:34 +01:00
|
|
|
static const smartlist_t* rend_get_service_list(
|
|
|
|
const smartlist_t* substitute_service_list);
|
|
|
|
static smartlist_t* rend_get_service_list_mutable(
|
|
|
|
smartlist_t* substitute_service_list);
|
2017-03-04 12:42:59 +01:00
|
|
|
static int rend_max_intro_circs_per_period(unsigned int n_intro_points_wanted);
|
2016-10-31 19:53:30 +01:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Represents the mapping from a virtual port of a rendezvous service to
|
2004-03-31 23:54:56 +02:00
|
|
|
* a real port on some IP.
|
|
|
|
*/
|
2015-04-25 10:23:15 +02:00
|
|
|
struct rend_service_port_config_s {
|
2015-01-22 03:22:33 +01:00
|
|
|
/* The incoming HS virtual port we're mapping */
|
2004-03-31 23:54:56 +02:00
|
|
|
uint16_t virtual_port;
|
2015-01-22 03:22:33 +01:00
|
|
|
/* Is this an AF_UNIX port? */
|
|
|
|
unsigned int is_unix_addr:1;
|
|
|
|
/* The outgoing TCP port to use, if !is_unix_addr */
|
2004-03-31 23:54:56 +02:00
|
|
|
uint16_t real_port;
|
2015-01-22 03:22:33 +01:00
|
|
|
/* The outgoing IPv4 or IPv6 address to use, if !is_unix_addr */
|
2008-08-05 22:08:19 +02:00
|
|
|
tor_addr_t real_addr;
|
2015-01-22 03:22:33 +01:00
|
|
|
/* The socket path to connect to, if is_unix_addr */
|
|
|
|
char unix_addr[FLEXIBLE_ARRAY_MEMBER];
|
2015-04-25 10:23:15 +02:00
|
|
|
};
|
2004-03-31 23:54:56 +02:00
|
|
|
|
2011-10-05 02:34:42 +02:00
|
|
|
/** Try to maintain this many intro points per service by default. */
|
|
|
|
#define NUM_INTRO_POINTS_DEFAULT 3
|
2015-05-11 21:48:04 +02:00
|
|
|
/** Maximum number of intro points per service. */
|
|
|
|
#define NUM_INTRO_POINTS_MAX 10
|
2015-05-29 23:45:45 +02:00
|
|
|
/** Number of extra intro points we launch if our set of intro nodes is
|
|
|
|
* empty. See proposal 155, section 4. */
|
|
|
|
#define NUM_INTRO_POINTS_EXTRA 2
|
2004-04-03 01:01:00 +02:00
|
|
|
|
2004-05-18 03:53:53 +02:00
|
|
|
/** If we can't build our intro circuits, don't retry for this long. */
|
2006-03-13 00:31:16 +01:00
|
|
|
#define INTRO_CIRC_RETRY_PERIOD (60*5)
|
2004-10-30 07:04:52 +02:00
|
|
|
/** How many times will a hidden service operator attempt to connect to
|
|
|
|
* a requested rendezvous point before giving up? */
|
2015-04-02 16:58:33 +02:00
|
|
|
#define MAX_REND_FAILURES 1
|
2005-03-19 05:38:59 +01:00
|
|
|
/** How many seconds should we spend trying to connect to a requested
|
|
|
|
* rendezvous point before giving up? */
|
|
|
|
#define MAX_REND_TIMEOUT 30
|
2004-05-18 03:53:53 +02:00
|
|
|
|
2016-09-07 05:29:07 +02:00
|
|
|
/* Hidden service directory file names:
|
|
|
|
* new file names should be added to rend_service_add_filenames_to_list()
|
|
|
|
* for sandboxing purposes. */
|
|
|
|
static const char *private_key_fname = "private_key";
|
|
|
|
static const char *hostname_fname = "hostname";
|
|
|
|
static const char *client_keys_fname = "client_keys";
|
|
|
|
static const char *sos_poison_fname = "onion_service_non_anonymous";
|
2004-03-31 23:54:56 +02:00
|
|
|
|
2016-11-18 01:50:34 +01:00
|
|
|
/** A list of rend_service_t's for services run on this OP.
|
|
|
|
*/
|
|
|
|
static smartlist_t *rend_service_list = NULL;
|
|
|
|
|
|
|
|
/* Like rend_get_service_list_mutable, but returns a read-only list. */
|
|
|
|
static const smartlist_t*
|
|
|
|
rend_get_service_list(const smartlist_t* substitute_service_list)
|
|
|
|
{
|
|
|
|
/* It is safe to cast away the const here, because
|
|
|
|
* rend_get_service_list_mutable does not actually modify the list */
|
|
|
|
return rend_get_service_list_mutable((smartlist_t*)substitute_service_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return a mutable list of hidden services.
|
|
|
|
* If substitute_service_list is not NULL, return it.
|
|
|
|
* Otherwise, check if the global rend_service_list is non-NULL, and if so,
|
|
|
|
* return it.
|
2016-12-02 22:23:01 +01:00
|
|
|
* Otherwise, log a BUG message and return NULL.
|
2016-11-18 01:50:34 +01:00
|
|
|
* */
|
|
|
|
static smartlist_t*
|
|
|
|
rend_get_service_list_mutable(smartlist_t* substitute_service_list)
|
|
|
|
{
|
|
|
|
if (substitute_service_list) {
|
|
|
|
return substitute_service_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If no special service list is provided, then just use the global one. */
|
|
|
|
|
|
|
|
if (BUG(!rend_service_list)) {
|
|
|
|
/* No global HS list, which is a programmer error. */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rend_service_list;
|
|
|
|
}
|
|
|
|
|
2016-11-01 20:40:41 +01:00
|
|
|
/** Tells if onion service <b>s</b> is ephemeral.
|
2016-11-18 01:50:34 +01:00
|
|
|
*/
|
2016-11-01 20:40:41 +01:00
|
|
|
static unsigned int
|
|
|
|
rend_service_is_ephemeral(const struct rend_service_t *s)
|
|
|
|
{
|
|
|
|
return (s->directory == NULL);
|
|
|
|
}
|
|
|
|
|
2015-04-25 10:23:15 +02:00
|
|
|
/** Returns a escaped string representation of the service, <b>s</b>.
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
rend_service_escaped_dir(const struct rend_service_t *s)
|
|
|
|
{
|
2016-11-01 20:40:41 +01:00
|
|
|
return rend_service_is_ephemeral(s) ? "[EPHEMERAL]" : escaped(s->directory);
|
2015-04-25 10:23:15 +02:00
|
|
|
}
|
|
|
|
|
2005-08-13 02:22:07 +02:00
|
|
|
/** Return the number of rendezvous services we have configured. */
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
|
|
|
num_rend_services(void)
|
|
|
|
{
|
2005-08-13 02:22:07 +02:00
|
|
|
if (!rend_service_list)
|
|
|
|
return 0;
|
|
|
|
return smartlist_len(rend_service_list);
|
|
|
|
}
|
|
|
|
|
2008-08-08 16:36:11 +02:00
|
|
|
/** Helper: free storage held by a single service authorized client entry. */
|
2015-04-14 05:35:40 +02:00
|
|
|
void
|
2008-08-08 16:36:11 +02:00
|
|
|
rend_authorized_client_free(rend_authorized_client_t *client)
|
|
|
|
{
|
2009-09-28 16:37:01 +02:00
|
|
|
if (!client)
|
|
|
|
return;
|
2008-08-08 16:36:11 +02:00
|
|
|
if (client->client_key)
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_free(client->client_key);
|
2015-04-14 05:35:40 +02:00
|
|
|
if (client->client_name)
|
|
|
|
memwipe(client->client_name, 0, strlen(client->client_name));
|
2008-08-08 16:36:11 +02:00
|
|
|
tor_free(client->client_name);
|
2012-11-08 23:00:36 +01:00
|
|
|
memwipe(client->descriptor_cookie, 0, sizeof(client->descriptor_cookie));
|
2008-08-08 16:36:11 +02:00
|
|
|
tor_free(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Helper for strmap_free. */
|
|
|
|
static void
|
|
|
|
rend_authorized_client_strmap_item_free(void *authorized_client)
|
|
|
|
{
|
|
|
|
rend_authorized_client_free(authorized_client);
|
|
|
|
}
|
|
|
|
|
2004-05-10 06:34:48 +02:00
|
|
|
/** Release the storage held by <b>service</b>.
|
2004-04-06 05:44:36 +02:00
|
|
|
*/
|
2016-07-14 06:07:48 +02:00
|
|
|
STATIC void
|
2005-06-11 20:52:12 +02:00
|
|
|
rend_service_free(rend_service_t *service)
|
2004-03-31 23:54:56 +02:00
|
|
|
{
|
2009-09-28 16:37:01 +02:00
|
|
|
if (!service)
|
|
|
|
return;
|
|
|
|
|
2004-04-06 05:44:36 +02:00
|
|
|
tor_free(service->directory);
|
2016-07-14 06:07:48 +02:00
|
|
|
if (service->ports) {
|
|
|
|
SMARTLIST_FOREACH(service->ports, rend_service_port_config_t*, p,
|
|
|
|
rend_service_port_config_free(p));
|
|
|
|
smartlist_free(service->ports);
|
|
|
|
}
|
2004-04-06 05:44:36 +02:00
|
|
|
if (service->private_key)
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_free(service->private_key);
|
2007-12-21 10:28:22 +01:00
|
|
|
if (service->intro_nodes) {
|
|
|
|
SMARTLIST_FOREACH(service->intro_nodes, rend_intro_point_t *, intro,
|
|
|
|
rend_intro_point_free(intro););
|
|
|
|
smartlist_free(service->intro_nodes);
|
|
|
|
}
|
2015-05-29 23:45:45 +02:00
|
|
|
if (service->expiring_nodes) {
|
|
|
|
SMARTLIST_FOREACH(service->expiring_nodes, rend_intro_point_t *, intro,
|
|
|
|
rend_intro_point_free(intro););
|
|
|
|
smartlist_free(service->expiring_nodes);
|
|
|
|
}
|
2009-12-12 08:07:59 +01:00
|
|
|
|
|
|
|
rend_service_descriptor_free(service->desc);
|
2008-08-08 16:36:11 +02:00
|
|
|
if (service->clients) {
|
|
|
|
SMARTLIST_FOREACH(service->clients, rend_authorized_client_t *, c,
|
|
|
|
rend_authorized_client_free(c););
|
|
|
|
smartlist_free(service->clients);
|
|
|
|
}
|
2012-07-11 04:18:47 +02:00
|
|
|
if (service->accepted_intro_dh_parts) {
|
|
|
|
replaycache_free(service->accepted_intro_dh_parts);
|
|
|
|
}
|
2004-04-06 05:44:36 +02:00
|
|
|
tor_free(service);
|
2004-03-31 23:54:56 +02:00
|
|
|
}
|
|
|
|
|
2005-02-27 23:08:01 +01:00
|
|
|
/** Release all the storage held in rend_service_list.
|
2004-04-06 05:44:36 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
rend_service_free_all(void)
|
2004-03-31 23:54:56 +02:00
|
|
|
{
|
2009-09-28 16:37:01 +02:00
|
|
|
if (!rend_service_list)
|
2004-03-31 23:54:56 +02:00
|
|
|
return;
|
2009-09-28 16:37:01 +02:00
|
|
|
|
2004-04-03 02:58:54 +02:00
|
|
|
SMARTLIST_FOREACH(rend_service_list, rend_service_t*, ptr,
|
|
|
|
rend_service_free(ptr));
|
2004-03-31 23:54:56 +02:00
|
|
|
smartlist_free(rend_service_list);
|
2005-02-11 00:18:39 +01:00
|
|
|
rend_service_list = NULL;
|
2004-03-31 23:54:56 +02:00
|
|
|
}
|
|
|
|
|
2016-11-18 04:32:13 +01:00
|
|
|
/** Validate <b>service</b> and add it to <b>service_list</b>, or to
|
|
|
|
* the global rend_service_list if <b>service_list</b> is NULL.
|
2015-05-28 19:23:09 +02:00
|
|
|
* Return 0 on success. On failure, free <b>service</b> and return -1.
|
2016-11-04 06:04:05 +01:00
|
|
|
* Takes ownership of <b>service</b>.
|
2004-04-06 05:44:36 +02:00
|
|
|
*/
|
2015-04-25 10:23:15 +02:00
|
|
|
static int
|
2016-11-18 04:32:13 +01:00
|
|
|
rend_add_service(smartlist_t *service_list, rend_service_t *service)
|
2004-03-31 23:54:56 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
rend_service_port_config_t *p;
|
|
|
|
|
2016-12-02 22:25:50 +01:00
|
|
|
tor_assert(service);
|
|
|
|
|
2016-11-18 01:50:34 +01:00
|
|
|
smartlist_t *s_list = rend_get_service_list_mutable(service_list);
|
2016-12-02 22:25:50 +01:00
|
|
|
/* We must have a service list, even if it's a temporary one, so we can
|
|
|
|
* check for duplicate services */
|
2016-11-18 01:50:34 +01:00
|
|
|
if (BUG(!s_list)) {
|
|
|
|
return -1;
|
2016-11-18 04:32:13 +01:00
|
|
|
}
|
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
service->intro_nodes = smartlist_new();
|
2015-05-29 23:45:45 +02:00
|
|
|
service->expiring_nodes = smartlist_new();
|
2004-04-03 01:01:00 +02:00
|
|
|
|
2015-05-20 19:41:27 +02:00
|
|
|
if (service->max_streams_per_circuit < 0) {
|
|
|
|
log_warn(LD_CONFIG, "Hidden service (%s) configured with negative max "
|
2016-11-04 06:37:57 +01:00
|
|
|
"streams per circuit.",
|
2015-05-20 19:41:27 +02:00
|
|
|
rend_service_escaped_dir(service));
|
|
|
|
rend_service_free(service);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (service->max_streams_close_circuit < 0 ||
|
|
|
|
service->max_streams_close_circuit > 1) {
|
|
|
|
log_warn(LD_CONFIG, "Hidden service (%s) configured with invalid "
|
2016-11-04 06:37:57 +01:00
|
|
|
"max streams handling.",
|
2015-05-20 19:41:27 +02:00
|
|
|
rend_service_escaped_dir(service));
|
|
|
|
rend_service_free(service);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-08-09 17:13:28 +02:00
|
|
|
if (service->auth_type != REND_NO_AUTH &&
|
2016-11-18 04:32:13 +01:00
|
|
|
(!service->clients ||
|
|
|
|
smartlist_len(service->clients) == 0)) {
|
2011-11-08 02:02:50 +01:00
|
|
|
log_warn(LD_CONFIG, "Hidden service (%s) with client authorization but no "
|
2016-11-04 06:37:57 +01:00
|
|
|
"clients.",
|
2015-04-25 10:23:15 +02:00
|
|
|
rend_service_escaped_dir(service));
|
2008-08-08 16:36:11 +02:00
|
|
|
rend_service_free(service);
|
2015-04-25 10:23:15 +02:00
|
|
|
return -1;
|
2008-08-08 16:36:11 +02:00
|
|
|
}
|
|
|
|
|
2016-11-18 04:32:13 +01:00
|
|
|
if (!service->ports || !smartlist_len(service->ports)) {
|
2016-11-04 06:37:57 +01:00
|
|
|
log_warn(LD_CONFIG, "Hidden service (%s) with no ports configured.",
|
2015-04-25 10:23:15 +02:00
|
|
|
rend_service_escaped_dir(service));
|
2004-03-31 23:54:56 +02:00
|
|
|
rend_service_free(service);
|
2015-04-25 10:23:15 +02:00
|
|
|
return -1;
|
2004-03-31 23:54:56 +02:00
|
|
|
} else {
|
2011-05-29 00:54:59 +02:00
|
|
|
int dupe = 0;
|
|
|
|
/* XXX This duplicate check has two problems:
|
|
|
|
*
|
|
|
|
* a) It's O(n^2), but the same comment from the bottom of
|
|
|
|
* rend_config_services() should apply.
|
|
|
|
*
|
|
|
|
* b) We only compare directory paths as strings, so we can't
|
|
|
|
* detect two distinct paths that specify the same directory
|
|
|
|
* (which can arise from symlinks, case-insensitivity, bind
|
|
|
|
* mounts, etc.).
|
|
|
|
*
|
|
|
|
* It also can't detect that two separate Tor instances are trying
|
|
|
|
* to use the same HiddenServiceDir; for that, we would need a
|
|
|
|
* lock file. But this is enough to detect a simple mistake that
|
|
|
|
* at least one person has actually made.
|
|
|
|
*/
|
2016-12-02 22:25:50 +01:00
|
|
|
tor_assert(s_list);
|
2016-11-04 00:12:18 +01:00
|
|
|
if (!rend_service_is_ephemeral(service)) {
|
|
|
|
/* Skip dupe for ephemeral services. */
|
2016-11-18 04:32:13 +01:00
|
|
|
SMARTLIST_FOREACH(s_list, rend_service_t*, ptr,
|
2015-04-25 10:23:15 +02:00
|
|
|
dupe = dupe ||
|
|
|
|
!strcmp(ptr->directory, service->directory));
|
|
|
|
if (dupe) {
|
|
|
|
log_warn(LD_REND, "Another hidden service is already configured for "
|
2016-11-04 06:37:57 +01:00
|
|
|
"directory %s.",
|
2015-04-25 10:23:15 +02:00
|
|
|
rend_service_escaped_dir(service));
|
|
|
|
rend_service_free(service);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-05-29 00:54:59 +02:00
|
|
|
}
|
2016-11-01 20:55:03 +01:00
|
|
|
log_debug(LD_REND,"Configuring service with directory %s",
|
|
|
|
rend_service_escaped_dir(service));
|
2004-04-03 02:58:54 +02:00
|
|
|
for (i = 0; i < smartlist_len(service->ports); ++i) {
|
|
|
|
p = smartlist_get(service->ports, i);
|
2015-01-22 03:22:33 +01:00
|
|
|
if (!(p->is_unix_addr)) {
|
|
|
|
log_debug(LD_REND,
|
|
|
|
"Service maps port %d to %s",
|
|
|
|
p->virtual_port,
|
|
|
|
fmt_addrport(&p->real_addr, p->real_port));
|
|
|
|
} else {
|
|
|
|
#ifdef HAVE_SYS_UN_H
|
|
|
|
log_debug(LD_REND,
|
|
|
|
"Service maps port %d to socket at \"%s\"",
|
|
|
|
p->virtual_port, p->unix_addr);
|
|
|
|
#else
|
2016-11-04 06:37:57 +01:00
|
|
|
log_warn(LD_BUG,
|
|
|
|
"Service maps port %d to an AF_UNIX socket, but we "
|
|
|
|
"have no AF_UNIX support on this platform. This is "
|
|
|
|
"probably a bug.",
|
|
|
|
p->virtual_port);
|
2016-12-02 22:24:47 +01:00
|
|
|
rend_service_free(service);
|
2016-11-04 06:37:57 +01:00
|
|
|
return -1;
|
2015-01-22 03:22:33 +01:00
|
|
|
#endif /* defined(HAVE_SYS_UN_H) */
|
|
|
|
}
|
2004-03-31 23:54:56 +02:00
|
|
|
}
|
2016-12-02 22:25:50 +01:00
|
|
|
/* The service passed all the checks */
|
|
|
|
tor_assert(s_list);
|
2016-11-04 06:37:57 +01:00
|
|
|
smartlist_add(s_list, service);
|
2015-04-25 10:23:15 +02:00
|
|
|
return 0;
|
2004-03-31 23:54:56 +02:00
|
|
|
}
|
2015-04-25 10:23:15 +02:00
|
|
|
/* NOTREACHED */
|
2004-03-31 23:54:56 +02:00
|
|
|
}
|
|
|
|
|
2015-01-23 15:45:12 +01:00
|
|
|
/** Return a new rend_service_port_config_t with its path set to
|
|
|
|
* <b>socket_path</b> or empty if <b>socket_path</b> is NULL */
|
|
|
|
static rend_service_port_config_t *
|
|
|
|
rend_service_port_config_new(const char *socket_path)
|
|
|
|
{
|
|
|
|
if (!socket_path)
|
2015-02-09 04:48:16 +01:00
|
|
|
return tor_malloc_zero(sizeof(rend_service_port_config_t) + 1);
|
2015-01-23 15:45:12 +01:00
|
|
|
|
|
|
|
const size_t pathlen = strlen(socket_path) + 1;
|
|
|
|
rend_service_port_config_t *conf =
|
|
|
|
tor_malloc_zero(sizeof(rend_service_port_config_t) + pathlen);
|
|
|
|
memcpy(conf->unix_addr, socket_path, pathlen);
|
|
|
|
conf->is_unix_addr = 1;
|
|
|
|
return conf;
|
|
|
|
}
|
|
|
|
|
2016-11-10 23:10:31 +01:00
|
|
|
/** Parses a virtual-port to real-port/socket mapping separated by
|
|
|
|
* the provided separator and returns a new rend_service_port_config_t,
|
|
|
|
* or NULL and an optional error string on failure.
|
2004-04-06 05:44:36 +02:00
|
|
|
*
|
2015-04-25 10:23:15 +02:00
|
|
|
* The format is: VirtualPort SEP (IP|RealPort|IP:RealPort|'socket':path)?
|
2004-05-10 06:34:48 +02:00
|
|
|
*
|
|
|
|
* IP defaults to 127.0.0.1; RealPort defaults to VirtualPort.
|
2004-03-31 23:54:56 +02:00
|
|
|
*/
|
2015-04-25 10:23:15 +02:00
|
|
|
rend_service_port_config_t *
|
|
|
|
rend_service_parse_port_config(const char *string, const char *sep,
|
|
|
|
char **err_msg_out)
|
2004-03-31 23:54:56 +02:00
|
|
|
{
|
2005-08-06 23:35:04 +02:00
|
|
|
smartlist_t *sl;
|
2004-10-12 17:52:09 +02:00
|
|
|
int virtport;
|
2015-01-30 20:46:18 +01:00
|
|
|
int realport = 0;
|
2004-10-13 21:53:34 +02:00
|
|
|
uint16_t p;
|
2008-08-05 22:08:19 +02:00
|
|
|
tor_addr_t addr;
|
2005-08-06 23:35:04 +02:00
|
|
|
rend_service_port_config_t *result = NULL;
|
2015-01-23 15:45:12 +01:00
|
|
|
unsigned int is_unix_addr = 0;
|
2016-10-03 22:32:00 +02:00
|
|
|
const char *socket_path = NULL;
|
2015-04-25 10:23:15 +02:00
|
|
|
char *err_msg = NULL;
|
2016-10-03 22:32:00 +02:00
|
|
|
char *addrport = NULL;
|
2005-08-06 23:35:04 +02:00
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
sl = smartlist_new();
|
2015-04-25 10:23:15 +02:00
|
|
|
smartlist_split_string(sl, string, sep,
|
2016-10-03 22:32:00 +02:00
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 2);
|
|
|
|
if (smartlist_len(sl) < 1 || BUG(smartlist_len(sl) > 2)) {
|
2016-11-10 22:29:48 +01:00
|
|
|
err_msg = tor_strdup("Bad syntax in hidden service port configuration.");
|
2005-08-06 23:35:04 +02:00
|
|
|
goto err;
|
2004-03-31 23:54:56 +02:00
|
|
|
}
|
2008-02-06 01:54:47 +01:00
|
|
|
virtport = (int)tor_parse_long(smartlist_get(sl,0), 10, 1, 65535, NULL,NULL);
|
|
|
|
if (!virtport) {
|
2016-11-10 22:29:48 +01:00
|
|
|
tor_asprintf(&err_msg, "Missing or invalid port %s in hidden service "
|
2015-04-25 10:23:15 +02:00
|
|
|
"port configuration", escaped(smartlist_get(sl,0)));
|
|
|
|
|
2005-08-06 23:35:04 +02:00
|
|
|
goto err;
|
2004-03-31 23:54:56 +02:00
|
|
|
}
|
2005-08-06 23:35:04 +02:00
|
|
|
if (smartlist_len(sl) == 1) {
|
2004-03-31 23:54:56 +02:00
|
|
|
/* No addr:port part; use default. */
|
|
|
|
realport = virtport;
|
2008-08-05 22:08:19 +02:00
|
|
|
tor_addr_from_ipv4h(&addr, 0x7F000001u); /* 127.0.0.1 */
|
2004-10-12 17:52:09 +02:00
|
|
|
} else {
|
2015-01-29 00:01:53 +01:00
|
|
|
int ret;
|
|
|
|
|
2016-10-03 22:32:00 +02:00
|
|
|
const char *addrport_element = smartlist_get(sl,1);
|
|
|
|
const char *rest = NULL;
|
|
|
|
int is_unix;
|
|
|
|
ret = port_cfg_line_extract_addrport(addrport_element, &addrport,
|
|
|
|
&is_unix, &rest);
|
|
|
|
if (ret < 0) {
|
2016-10-14 16:40:31 +02:00
|
|
|
tor_asprintf(&err_msg, "Couldn't process address <%s> from hidden "
|
|
|
|
"service configuration", addrport_element);
|
2015-01-29 00:01:53 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2016-10-03 22:32:00 +02:00
|
|
|
if (is_unix) {
|
|
|
|
socket_path = addrport;
|
2015-01-29 00:01:53 +01:00
|
|
|
is_unix_addr = 1;
|
2015-01-23 15:45:12 +01:00
|
|
|
} else if (strchr(addrport, ':') || strchr(addrport, '.')) {
|
|
|
|
/* else try it as an IP:port pair if it has a : or . in it */
|
2011-10-11 17:21:31 +02:00
|
|
|
if (tor_addr_port_lookup(addrport, &addr, &p)<0) {
|
2016-11-10 22:29:48 +01:00
|
|
|
err_msg = tor_strdup("Unparseable address in hidden service port "
|
|
|
|
"configuration.");
|
2005-08-06 23:35:04 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
realport = p?p:virtport;
|
|
|
|
} else {
|
|
|
|
/* No addr:port, no addr -- must be port. */
|
2008-02-06 01:54:47 +01:00
|
|
|
realport = (int)tor_parse_long(addrport, 10, 1, 65535, NULL, NULL);
|
|
|
|
if (!realport) {
|
2016-11-10 22:29:48 +01:00
|
|
|
tor_asprintf(&err_msg, "Unparseable or out-of-range port %s in "
|
|
|
|
"hidden service port configuration.",
|
|
|
|
escaped(addrport));
|
2005-08-06 23:35:04 +02:00
|
|
|
goto err;
|
2008-02-06 01:54:47 +01:00
|
|
|
}
|
2008-08-05 22:08:19 +02:00
|
|
|
tor_addr_from_ipv4h(&addr, 0x7F000001u); /* Default to 127.0.0.1 */
|
2004-10-12 21:01:53 +02:00
|
|
|
}
|
2004-03-31 23:54:56 +02:00
|
|
|
}
|
|
|
|
|
2015-01-23 15:45:12 +01:00
|
|
|
/* Allow room for unix_addr */
|
|
|
|
result = rend_service_port_config_new(socket_path);
|
2004-03-31 23:54:56 +02:00
|
|
|
result->virtual_port = virtport;
|
2015-01-23 15:45:12 +01:00
|
|
|
result->is_unix_addr = is_unix_addr;
|
|
|
|
if (!is_unix_addr) {
|
|
|
|
result->real_port = realport;
|
|
|
|
tor_addr_copy(&result->real_addr, &addr);
|
|
|
|
result->unix_addr[0] = '\0';
|
|
|
|
}
|
|
|
|
|
2005-08-06 23:35:04 +02:00
|
|
|
err:
|
2016-10-03 22:32:00 +02:00
|
|
|
tor_free(addrport);
|
2016-11-10 22:29:48 +01:00
|
|
|
if (err_msg_out != NULL) {
|
|
|
|
*err_msg_out = err_msg;
|
|
|
|
} else {
|
|
|
|
tor_free(err_msg);
|
|
|
|
}
|
2005-08-06 23:35:04 +02:00
|
|
|
SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
|
|
|
|
smartlist_free(sl);
|
2015-01-23 15:45:12 +01:00
|
|
|
|
2004-03-31 23:54:56 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-04-25 10:23:15 +02:00
|
|
|
/** Release all storage held in a rend_service_port_config_t. */
|
|
|
|
void
|
|
|
|
rend_service_port_config_free(rend_service_port_config_t *p)
|
|
|
|
{
|
|
|
|
tor_free(p);
|
|
|
|
}
|
|
|
|
|
2016-11-18 04:32:13 +01:00
|
|
|
/* Check the directory for <b>service</b>, and add the service to
|
|
|
|
* <b>service_list</b>, or to the global list if <b>service_list</b> is NULL.
|
|
|
|
* Only add the service to the list if <b>validate_only</b> is false.
|
2016-11-04 06:04:05 +01:00
|
|
|
* If <b>validate_only</b> is true, free the service.
|
|
|
|
* If <b>service</b> is NULL, ignore it, and return 0.
|
|
|
|
* Returns 0 on success, and -1 on failure.
|
2016-11-04 06:28:33 +01:00
|
|
|
* Takes ownership of <b>service</b>, either freeing it, or adding it to the
|
|
|
|
* global service list.
|
2016-11-04 06:04:05 +01:00
|
|
|
*/
|
2016-11-18 04:32:13 +01:00
|
|
|
STATIC int
|
|
|
|
rend_service_check_dir_and_add(smartlist_t *service_list,
|
|
|
|
const or_options_t *options,
|
2016-11-04 06:04:05 +01:00
|
|
|
rend_service_t *service,
|
|
|
|
int validate_only)
|
|
|
|
{
|
2016-11-04 06:28:33 +01:00
|
|
|
if (!service) {
|
|
|
|
/* It is ok for a service to be NULL, this means there are no services */
|
|
|
|
return 0;
|
|
|
|
}
|
2016-11-04 06:04:05 +01:00
|
|
|
|
2016-11-04 06:28:33 +01:00
|
|
|
if (rend_service_check_private_dir(options, service, !validate_only)
|
|
|
|
< 0) {
|
|
|
|
rend_service_free(service);
|
|
|
|
return -1;
|
2016-11-04 06:04:05 +01:00
|
|
|
}
|
|
|
|
|
2016-12-02 22:25:50 +01:00
|
|
|
smartlist_t *s_list = rend_get_service_list_mutable(service_list);
|
|
|
|
/* We must have a service list, even if it's a temporary one, so we can
|
|
|
|
* check for duplicate services */
|
|
|
|
if (BUG(!s_list)) {
|
|
|
|
return -1;
|
2016-11-04 06:28:33 +01:00
|
|
|
}
|
2016-12-02 22:25:50 +01:00
|
|
|
return rend_add_service(s_list, service);
|
2016-11-04 06:04:05 +01:00
|
|
|
}
|
|
|
|
|
2016-12-21 19:35:22 +01:00
|
|
|
/* If this is a reload and there were hidden services configured before,
|
|
|
|
* keep the introduction points that are still needed and close the
|
|
|
|
* other ones. */
|
2016-12-21 21:00:02 +01:00
|
|
|
STATIC void
|
2016-12-21 19:35:22 +01:00
|
|
|
prune_services_on_reload(smartlist_t *old_service_list,
|
|
|
|
smartlist_t *new_service_list)
|
|
|
|
{
|
|
|
|
origin_circuit_t *ocirc = NULL;
|
|
|
|
smartlist_t *surviving_services = NULL;
|
|
|
|
|
|
|
|
tor_assert(old_service_list);
|
|
|
|
tor_assert(new_service_list);
|
|
|
|
|
|
|
|
/* This contains all _existing_ services that survives the relaod that is
|
|
|
|
* that haven't been removed from the configuration. The difference between
|
|
|
|
* this list and the new service list is that the new list can possibly
|
|
|
|
* contain newly configured service that have no introduction points opened
|
|
|
|
* yet nor key material loaded or generated. */
|
|
|
|
surviving_services = smartlist_new();
|
|
|
|
|
|
|
|
/* Preserve the existing ephemeral services.
|
|
|
|
*
|
|
|
|
* This is the ephemeral service equivalent of the "Copy introduction
|
|
|
|
* points to new services" block, except there's no copy required since
|
|
|
|
* the service structure isn't regenerated.
|
|
|
|
*
|
|
|
|
* After this is done, all ephemeral services will be:
|
|
|
|
* * Removed from old_service_list, so the equivalent non-ephemeral code
|
|
|
|
* will not attempt to preserve them.
|
|
|
|
* * Added to the new_service_list (that previously only had the
|
|
|
|
* services listed in the configuration).
|
|
|
|
* * Added to surviving_services, which is the list of services that
|
|
|
|
* will NOT have their intro point closed.
|
|
|
|
*/
|
|
|
|
SMARTLIST_FOREACH_BEGIN(old_service_list, rend_service_t *, old) {
|
|
|
|
if (rend_service_is_ephemeral(old)) {
|
|
|
|
SMARTLIST_DEL_CURRENT(old_service_list, old);
|
|
|
|
smartlist_add(surviving_services, old);
|
|
|
|
smartlist_add(new_service_list, old);
|
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(old);
|
|
|
|
|
|
|
|
/* Copy introduction points to new services. This is O(n^2), but it's only
|
|
|
|
* called on reconfigure, so it's ok performance wise. */
|
|
|
|
SMARTLIST_FOREACH_BEGIN(new_service_list, rend_service_t *, new) {
|
|
|
|
SMARTLIST_FOREACH_BEGIN(old_service_list, rend_service_t *, old) {
|
|
|
|
/* Skip ephemeral services as we only want to copy introduction points
|
|
|
|
* from current services to newly configured one that already exists.
|
|
|
|
* The same directory means it's the same service. */
|
|
|
|
if (rend_service_is_ephemeral(new) || rend_service_is_ephemeral(old) ||
|
|
|
|
strcmp(old->directory, new->directory)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
smartlist_add_all(new->intro_nodes, old->intro_nodes);
|
|
|
|
smartlist_clear(old->intro_nodes);
|
|
|
|
smartlist_add_all(new->expiring_nodes, old->expiring_nodes);
|
|
|
|
smartlist_clear(old->expiring_nodes);
|
|
|
|
/* This regular service will survive the closing IPs step after. */
|
|
|
|
smartlist_add(surviving_services, old);
|
|
|
|
break;
|
|
|
|
} SMARTLIST_FOREACH_END(old);
|
|
|
|
} SMARTLIST_FOREACH_END(new);
|
|
|
|
|
|
|
|
/* For every service introduction circuit we can find, see if we have a
|
|
|
|
* matching surviving configured service. If not, close the circuit. */
|
|
|
|
while ((ocirc = circuit_get_next_service_intro_circ(ocirc))) {
|
|
|
|
int keep_it = 0;
|
|
|
|
tor_assert(ocirc->rend_data);
|
|
|
|
SMARTLIST_FOREACH_BEGIN(surviving_services, const rend_service_t *, s) {
|
|
|
|
if (rend_circuit_pk_digest_eq(ocirc, (uint8_t *) s->pk_digest)) {
|
|
|
|
/* Keep this circuit as we have a matching configured service. */
|
|
|
|
keep_it = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(s);
|
|
|
|
if (keep_it) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
log_info(LD_REND, "Closing intro point %s for service %s.",
|
|
|
|
safe_str_client(extend_info_describe(
|
|
|
|
ocirc->build_state->chosen_exit)),
|
|
|
|
safe_str_client(rend_data_get_address(ocirc->rend_data)));
|
|
|
|
/* Reason is FINISHED because service has been removed and thus the
|
|
|
|
* circuit is considered old/uneeded. */
|
|
|
|
circuit_mark_for_close(TO_CIRCUIT(ocirc), END_CIRC_REASON_FINISHED);
|
|
|
|
}
|
|
|
|
smartlist_free(surviving_services);
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Set up rend_service_list, based on the values of HiddenServiceDir and
|
2004-05-10 06:34:48 +02:00
|
|
|
* HiddenServicePort in <b>options</b>. Return 0 on success and -1 on
|
2004-11-09 00:12:40 +01:00
|
|
|
* failure. (If <b>validate_only</b> is set, parse, warn and return as
|
|
|
|
* normal, but don't actually change the configured services.)
|
2004-03-31 23:54:56 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2011-06-14 19:01:38 +02:00
|
|
|
rend_config_services(const or_options_t *options, int validate_only)
|
2004-03-31 23:54:56 +02:00
|
|
|
{
|
2005-07-22 23:12:10 +02:00
|
|
|
config_line_t *line;
|
2004-03-31 23:54:56 +02:00
|
|
|
rend_service_t *service = NULL;
|
|
|
|
rend_service_port_config_t *portcfg;
|
2008-12-10 19:16:58 +01:00
|
|
|
smartlist_t *old_service_list = NULL;
|
2016-12-02 22:25:50 +01:00
|
|
|
smartlist_t *temp_service_list = NULL;
|
2014-08-30 23:14:51 +02:00
|
|
|
int ok = 0;
|
2016-12-15 14:12:59 +01:00
|
|
|
int rv = -1;
|
2004-11-09 00:12:40 +01:00
|
|
|
|
2016-12-02 22:25:50 +01:00
|
|
|
/* Use a temporary service list, so that we can check the new services'
|
|
|
|
* consistency with each other */
|
|
|
|
temp_service_list = smartlist_new();
|
2004-03-31 23:54:56 +02:00
|
|
|
|
|
|
|
for (line = options->RendConfigLines; line; line = line->next) {
|
|
|
|
if (!strcasecmp(line->key, "HiddenServiceDir")) {
|
2016-11-04 06:04:05 +01:00
|
|
|
/* register the service we just finished parsing
|
|
|
|
* this code registers every service except the last one parsed,
|
|
|
|
* which is registered below the loop */
|
2016-12-02 22:25:50 +01:00
|
|
|
if (rend_service_check_dir_and_add(temp_service_list, options, service,
|
2016-11-18 04:32:13 +01:00
|
|
|
validate_only) < 0) {
|
2016-12-15 14:12:59 +01:00
|
|
|
service = NULL;
|
|
|
|
goto free_and_return;
|
2015-05-11 22:16:41 +02:00
|
|
|
}
|
|
|
|
service = tor_malloc_zero(sizeof(rend_service_t));
|
|
|
|
service->directory = tor_strdup(line->value);
|
|
|
|
service->ports = smartlist_new();
|
|
|
|
service->intro_period_started = time(NULL);
|
|
|
|
service->n_intro_points_wanted = NUM_INTRO_POINTS_DEFAULT;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!service) {
|
|
|
|
log_warn(LD_CONFIG, "%s with no preceding HiddenServiceDir directive",
|
|
|
|
line->key);
|
2016-12-15 14:12:59 +01:00
|
|
|
goto free_and_return;
|
2015-05-11 22:16:41 +02:00
|
|
|
}
|
|
|
|
if (!strcasecmp(line->key, "HiddenServicePort")) {
|
|
|
|
char *err_msg = NULL;
|
|
|
|
portcfg = rend_service_parse_port_config(line->value, " ", &err_msg);
|
|
|
|
if (!portcfg) {
|
|
|
|
if (err_msg)
|
|
|
|
log_warn(LD_CONFIG, "%s", err_msg);
|
|
|
|
tor_free(err_msg);
|
2016-12-15 14:12:59 +01:00
|
|
|
goto free_and_return;
|
2015-05-11 22:16:41 +02:00
|
|
|
}
|
|
|
|
tor_assert(!err_msg);
|
|
|
|
smartlist_add(service->ports, portcfg);
|
|
|
|
} else if (!strcasecmp(line->key, "HiddenServiceAllowUnknownPorts")) {
|
|
|
|
service->allow_unknown_ports = (int)tor_parse_long(line->value,
|
|
|
|
10, 0, 1, &ok, NULL);
|
|
|
|
if (!ok) {
|
|
|
|
log_warn(LD_CONFIG,
|
|
|
|
"HiddenServiceAllowUnknownPorts should be 0 or 1, not %s",
|
|
|
|
line->value);
|
2016-12-15 14:12:59 +01:00
|
|
|
goto free_and_return;
|
2015-05-11 22:16:41 +02:00
|
|
|
}
|
|
|
|
log_info(LD_CONFIG,
|
|
|
|
"HiddenServiceAllowUnknownPorts=%d for %s",
|
2016-11-01 20:55:03 +01:00
|
|
|
(int)service->allow_unknown_ports,
|
|
|
|
rend_service_escaped_dir(service));
|
2015-05-11 22:16:41 +02:00
|
|
|
} else if (!strcasecmp(line->key,
|
|
|
|
"HiddenServiceDirGroupReadable")) {
|
|
|
|
service->dir_group_readable = (int)tor_parse_long(line->value,
|
|
|
|
10, 0, 1, &ok, NULL);
|
|
|
|
if (!ok) {
|
|
|
|
log_warn(LD_CONFIG,
|
|
|
|
"HiddenServiceDirGroupReadable should be 0 or 1, not %s",
|
|
|
|
line->value);
|
2016-12-15 14:12:59 +01:00
|
|
|
goto free_and_return;
|
2015-05-11 22:16:41 +02:00
|
|
|
}
|
|
|
|
log_info(LD_CONFIG,
|
|
|
|
"HiddenServiceDirGroupReadable=%d for %s",
|
2016-11-03 19:29:18 +01:00
|
|
|
service->dir_group_readable,
|
|
|
|
rend_service_escaped_dir(service));
|
2015-05-20 19:33:59 +02:00
|
|
|
} else if (!strcasecmp(line->key, "HiddenServiceMaxStreams")) {
|
|
|
|
service->max_streams_per_circuit = (int)tor_parse_long(line->value,
|
|
|
|
10, 0, 65535, &ok, NULL);
|
|
|
|
if (!ok) {
|
|
|
|
log_warn(LD_CONFIG,
|
|
|
|
"HiddenServiceMaxStreams should be between 0 and %d, not %s",
|
|
|
|
65535, line->value);
|
2016-12-15 14:12:59 +01:00
|
|
|
goto free_and_return;
|
2015-05-20 19:33:59 +02:00
|
|
|
}
|
|
|
|
log_info(LD_CONFIG,
|
|
|
|
"HiddenServiceMaxStreams=%d for %s",
|
2016-11-03 19:29:18 +01:00
|
|
|
service->max_streams_per_circuit,
|
|
|
|
rend_service_escaped_dir(service));
|
2015-05-20 19:33:59 +02:00
|
|
|
} else if (!strcasecmp(line->key, "HiddenServiceMaxStreamsCloseCircuit")) {
|
|
|
|
service->max_streams_close_circuit = (int)tor_parse_long(line->value,
|
|
|
|
10, 0, 1, &ok, NULL);
|
|
|
|
if (!ok) {
|
|
|
|
log_warn(LD_CONFIG,
|
2015-06-01 14:59:14 +02:00
|
|
|
"HiddenServiceMaxStreamsCloseCircuit should be 0 or 1, "
|
|
|
|
"not %s",
|
2015-05-20 19:33:59 +02:00
|
|
|
line->value);
|
2016-12-15 14:12:59 +01:00
|
|
|
goto free_and_return;
|
2015-05-20 19:33:59 +02:00
|
|
|
}
|
|
|
|
log_info(LD_CONFIG,
|
|
|
|
"HiddenServiceMaxStreamsCloseCircuit=%d for %s",
|
2016-11-01 20:55:03 +01:00
|
|
|
(int)service->max_streams_close_circuit,
|
|
|
|
rend_service_escaped_dir(service));
|
2015-05-11 21:48:04 +02:00
|
|
|
} else if (!strcasecmp(line->key, "HiddenServiceNumIntroductionPoints")) {
|
|
|
|
service->n_intro_points_wanted =
|
|
|
|
(unsigned int) tor_parse_long(line->value, 10,
|
2017-01-17 20:53:06 +01:00
|
|
|
0, NUM_INTRO_POINTS_MAX, &ok, NULL);
|
2015-05-11 21:48:04 +02:00
|
|
|
if (!ok) {
|
|
|
|
log_warn(LD_CONFIG,
|
|
|
|
"HiddenServiceNumIntroductionPoints "
|
|
|
|
"should be between %d and %d, not %s",
|
2017-01-17 20:53:06 +01:00
|
|
|
0, NUM_INTRO_POINTS_MAX, line->value);
|
2016-12-15 14:12:59 +01:00
|
|
|
goto free_and_return;
|
2015-05-11 21:48:04 +02:00
|
|
|
}
|
|
|
|
log_info(LD_CONFIG, "HiddenServiceNumIntroductionPoints=%d for %s",
|
2016-11-03 19:29:18 +01:00
|
|
|
service->n_intro_points_wanted,
|
|
|
|
rend_service_escaped_dir(service));
|
2015-05-11 22:16:41 +02:00
|
|
|
} else if (!strcasecmp(line->key, "HiddenServiceAuthorizeClient")) {
|
|
|
|
/* Parse auth type and comma-separated list of client names and add a
|
|
|
|
* rend_authorized_client_t for each client to the service's list
|
|
|
|
* of authorized clients. */
|
|
|
|
smartlist_t *type_names_split, *clients;
|
|
|
|
const char *authname;
|
|
|
|
int num_clients;
|
|
|
|
if (service->auth_type != REND_NO_AUTH) {
|
|
|
|
log_warn(LD_CONFIG, "Got multiple HiddenServiceAuthorizeClient "
|
|
|
|
"lines for a single service.");
|
2016-12-15 14:12:59 +01:00
|
|
|
goto free_and_return;
|
2015-05-11 22:16:41 +02:00
|
|
|
}
|
|
|
|
type_names_split = smartlist_new();
|
|
|
|
smartlist_split_string(type_names_split, line->value, " ", 0, 2);
|
|
|
|
if (smartlist_len(type_names_split) < 1) {
|
|
|
|
log_warn(LD_BUG, "HiddenServiceAuthorizeClient has no value. This "
|
|
|
|
"should have been prevented when parsing the "
|
|
|
|
"configuration.");
|
2016-12-15 14:12:59 +01:00
|
|
|
goto free_and_return;
|
2015-05-11 22:16:41 +02:00
|
|
|
}
|
|
|
|
authname = smartlist_get(type_names_split, 0);
|
|
|
|
if (!strcasecmp(authname, "basic")) {
|
|
|
|
service->auth_type = REND_BASIC_AUTH;
|
|
|
|
} else if (!strcasecmp(authname, "stealth")) {
|
|
|
|
service->auth_type = REND_STEALTH_AUTH;
|
|
|
|
} else {
|
|
|
|
log_warn(LD_CONFIG, "HiddenServiceAuthorizeClient contains "
|
|
|
|
"unrecognized auth-type '%s'. Only 'basic' or 'stealth' "
|
|
|
|
"are recognized.",
|
|
|
|
(char *) smartlist_get(type_names_split, 0));
|
|
|
|
SMARTLIST_FOREACH(type_names_split, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(type_names_split);
|
2016-12-15 14:12:59 +01:00
|
|
|
goto free_and_return;
|
2015-05-11 22:16:41 +02:00
|
|
|
}
|
|
|
|
service->clients = smartlist_new();
|
|
|
|
if (smartlist_len(type_names_split) < 2) {
|
|
|
|
log_warn(LD_CONFIG, "HiddenServiceAuthorizeClient contains "
|
|
|
|
"auth-type '%s', but no client names.",
|
|
|
|
service->auth_type == REND_BASIC_AUTH ? "basic" : "stealth");
|
|
|
|
SMARTLIST_FOREACH(type_names_split, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(type_names_split);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
clients = smartlist_new();
|
|
|
|
smartlist_split_string(clients, smartlist_get(type_names_split, 1),
|
|
|
|
",", SPLIT_SKIP_SPACE, 0);
|
|
|
|
SMARTLIST_FOREACH(type_names_split, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(type_names_split);
|
|
|
|
/* Remove duplicate client names. */
|
|
|
|
num_clients = smartlist_len(clients);
|
|
|
|
smartlist_sort_strings(clients);
|
|
|
|
smartlist_uniq_strings(clients);
|
|
|
|
if (smartlist_len(clients) < num_clients) {
|
2008-08-09 17:13:28 +02:00
|
|
|
log_info(LD_CONFIG, "HiddenServiceAuthorizeClient contains %d "
|
|
|
|
"duplicate client name(s); removing.",
|
|
|
|
num_clients - smartlist_len(clients));
|
|
|
|
num_clients = smartlist_len(clients);
|
|
|
|
}
|
2008-08-08 16:36:11 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(clients, const char *, client_name)
|
|
|
|
{
|
|
|
|
rend_authorized_client_t *client;
|
2016-02-20 00:32:25 +01:00
|
|
|
if (!rend_valid_client_name(client_name)) {
|
2008-08-08 16:36:11 +02:00
|
|
|
log_warn(LD_CONFIG, "HiddenServiceAuthorizeClient contains an "
|
2016-02-20 00:32:25 +01:00
|
|
|
"illegal client name: '%s'. Names must be "
|
|
|
|
"between 1 and %d characters and contain "
|
|
|
|
"only [A-Za-z0-9+_-].",
|
2008-08-09 17:13:28 +02:00
|
|
|
client_name, REND_CLIENTNAME_MAX_LEN);
|
2008-08-08 16:36:11 +02:00
|
|
|
SMARTLIST_FOREACH(clients, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(clients);
|
2016-12-15 14:12:59 +01:00
|
|
|
goto free_and_return;
|
2008-08-08 16:36:11 +02:00
|
|
|
}
|
|
|
|
client = tor_malloc_zero(sizeof(rend_authorized_client_t));
|
|
|
|
client->client_name = tor_strdup(client_name);
|
|
|
|
smartlist_add(service->clients, client);
|
|
|
|
log_debug(LD_REND, "Adding client name '%s'", client_name);
|
|
|
|
}
|
|
|
|
SMARTLIST_FOREACH_END(client_name);
|
|
|
|
SMARTLIST_FOREACH(clients, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(clients);
|
|
|
|
/* Ensure maximum number of clients. */
|
|
|
|
if ((service->auth_type == REND_BASIC_AUTH &&
|
|
|
|
smartlist_len(service->clients) > 512) ||
|
|
|
|
(service->auth_type == REND_STEALTH_AUTH &&
|
|
|
|
smartlist_len(service->clients) > 16)) {
|
|
|
|
log_warn(LD_CONFIG, "HiddenServiceAuthorizeClient contains %d "
|
|
|
|
"client authorization entries, but only a "
|
|
|
|
"maximum of %d entries is allowed for "
|
2008-08-09 17:13:28 +02:00
|
|
|
"authorization type '%s'.",
|
2008-08-08 16:36:11 +02:00
|
|
|
smartlist_len(service->clients),
|
|
|
|
service->auth_type == REND_BASIC_AUTH ? 512 : 16,
|
2008-09-24 16:44:29 +02:00
|
|
|
service->auth_type == REND_BASIC_AUTH ? "basic" : "stealth");
|
2016-12-15 14:12:59 +01:00
|
|
|
goto free_and_return;
|
2008-08-08 16:36:11 +02:00
|
|
|
}
|
2007-10-31 21:48:06 +01:00
|
|
|
} else {
|
|
|
|
tor_assert(!strcasecmp(line->key, "HiddenServiceVersion"));
|
2009-05-02 23:31:58 +02:00
|
|
|
if (strcmp(line->value, "2")) {
|
|
|
|
log_warn(LD_CONFIG,
|
|
|
|
"The only supported HiddenServiceVersion is 2.");
|
2016-12-15 14:12:59 +01:00
|
|
|
goto free_and_return;
|
2007-10-31 21:48:06 +01:00
|
|
|
}
|
2004-03-31 23:54:56 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-04 06:04:05 +01:00
|
|
|
/* register the final service after we have finished parsing all services
|
|
|
|
* this code only registers the last service, other services are registered
|
2016-11-18 01:46:01 +01:00
|
|
|
* within the loop. It is ok for this service to be NULL, it is ignored. */
|
2016-12-02 22:25:50 +01:00
|
|
|
if (rend_service_check_dir_and_add(temp_service_list, options, service,
|
2016-11-18 04:32:13 +01:00
|
|
|
validate_only) < 0) {
|
2016-12-15 14:12:59 +01:00
|
|
|
service = NULL;
|
|
|
|
goto free_and_return;
|
2004-11-09 00:12:40 +01:00
|
|
|
}
|
2016-12-15 14:12:59 +01:00
|
|
|
service = NULL;
|
2004-03-31 23:54:56 +02:00
|
|
|
|
2016-12-02 22:25:50 +01:00
|
|
|
/* Free the newly added services if validating */
|
|
|
|
if (validate_only) {
|
2016-12-15 14:12:59 +01:00
|
|
|
rv = 0;
|
|
|
|
goto free_and_return;
|
2016-12-02 22:25:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, use the newly added services as the new service list
|
|
|
|
* Since we have now replaced the global service list, from this point on we
|
|
|
|
* must succeed, or die trying. */
|
|
|
|
old_service_list = rend_service_list;
|
|
|
|
rend_service_list = temp_service_list;
|
|
|
|
temp_service_list = NULL;
|
|
|
|
|
2008-12-10 19:16:58 +01:00
|
|
|
/* If this is a reload and there were hidden services configured before,
|
|
|
|
* keep the introduction points that are still needed and close the
|
|
|
|
* other ones. */
|
|
|
|
if (old_service_list && !validate_only) {
|
2016-12-21 19:35:22 +01:00
|
|
|
prune_services_on_reload(old_service_list, rend_service_list);
|
|
|
|
/* Every remaining service in the old list have been removed from the
|
|
|
|
* configuration so clean them up safely. */
|
|
|
|
SMARTLIST_FOREACH(old_service_list, rend_service_t *, s,
|
|
|
|
rend_service_free(s));
|
2008-12-10 19:16:58 +01:00
|
|
|
smartlist_free(old_service_list);
|
|
|
|
}
|
|
|
|
|
2004-03-31 23:54:56 +02:00
|
|
|
return 0;
|
2016-12-15 14:12:59 +01:00
|
|
|
free_and_return:
|
|
|
|
rend_service_free(service);
|
|
|
|
SMARTLIST_FOREACH(temp_service_list, rend_service_t *, ptr,
|
|
|
|
rend_service_free(ptr));
|
|
|
|
smartlist_free(temp_service_list);
|
|
|
|
return rv;
|
2004-03-31 23:54:56 +02:00
|
|
|
}
|
|
|
|
|
2015-04-14 05:09:09 +02:00
|
|
|
/** Add the ephemeral service <b>pk</b>/<b>ports</b> if possible, using
|
|
|
|
* client authorization <b>auth_type</b> and an optional list of
|
|
|
|
* rend_authorized_client_t in <b>auth_clients</b>, with
|
2015-05-20 19:41:27 +02:00
|
|
|
* <b>max_streams_per_circuit</b> streams allowed per rendezvous circuit,
|
|
|
|
* and circuit closure on max streams being exceeded set by
|
|
|
|
* <b>max_streams_close_circuit</b>.
|
2015-04-25 10:23:15 +02:00
|
|
|
*
|
2015-04-14 05:09:09 +02:00
|
|
|
* Ownership of pk, ports, and auth_clients is passed to this routine.
|
|
|
|
* Regardless of success/failure, callers should not touch these values
|
|
|
|
* after calling this routine, and may assume that correct cleanup has
|
|
|
|
* been done on failure.
|
2015-04-25 10:23:15 +02:00
|
|
|
*
|
|
|
|
* Return an appropriate rend_service_add_ephemeral_status_t.
|
|
|
|
*/
|
|
|
|
rend_service_add_ephemeral_status_t
|
|
|
|
rend_service_add_ephemeral(crypto_pk_t *pk,
|
|
|
|
smartlist_t *ports,
|
2015-05-20 19:41:27 +02:00
|
|
|
int max_streams_per_circuit,
|
|
|
|
int max_streams_close_circuit,
|
2015-04-14 05:09:09 +02:00
|
|
|
rend_auth_type_t auth_type,
|
|
|
|
smartlist_t *auth_clients,
|
2015-04-25 10:23:15 +02:00
|
|
|
char **service_id_out)
|
|
|
|
{
|
|
|
|
*service_id_out = NULL;
|
|
|
|
/* Allocate the service structure, and initialize the key, and key derived
|
|
|
|
* parameters.
|
|
|
|
*/
|
|
|
|
rend_service_t *s = tor_malloc_zero(sizeof(rend_service_t));
|
|
|
|
s->directory = NULL; /* This indicates the service is ephemeral. */
|
|
|
|
s->private_key = pk;
|
2015-04-14 05:09:09 +02:00
|
|
|
s->auth_type = auth_type;
|
|
|
|
s->clients = auth_clients;
|
2015-04-25 10:23:15 +02:00
|
|
|
s->ports = ports;
|
|
|
|
s->intro_period_started = time(NULL);
|
|
|
|
s->n_intro_points_wanted = NUM_INTRO_POINTS_DEFAULT;
|
2015-05-20 19:41:27 +02:00
|
|
|
s->max_streams_per_circuit = max_streams_per_circuit;
|
|
|
|
s->max_streams_close_circuit = max_streams_close_circuit;
|
2015-04-25 10:23:15 +02:00
|
|
|
if (rend_service_derive_key_digests(s) < 0) {
|
|
|
|
rend_service_free(s);
|
|
|
|
return RSAE_BADPRIVKEY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!s->ports || smartlist_len(s->ports) == 0) {
|
|
|
|
log_warn(LD_CONFIG, "At least one VIRTPORT/TARGET must be specified.");
|
|
|
|
rend_service_free(s);
|
|
|
|
return RSAE_BADVIRTPORT;
|
|
|
|
}
|
2015-04-14 05:09:09 +02:00
|
|
|
if (s->auth_type != REND_NO_AUTH &&
|
|
|
|
(!s->clients || smartlist_len(s->clients) == 0)) {
|
|
|
|
log_warn(LD_CONFIG, "At least one authorized client must be specified.");
|
|
|
|
rend_service_free(s);
|
|
|
|
return RSAE_BADAUTH;
|
|
|
|
}
|
2015-04-25 10:23:15 +02:00
|
|
|
|
|
|
|
/* Enforcing pk/id uniqueness should be done by rend_service_load_keys(), but
|
|
|
|
* it's not, see #14828.
|
|
|
|
*/
|
|
|
|
if (rend_service_get_by_pk_digest(s->pk_digest)) {
|
|
|
|
log_warn(LD_CONFIG, "Onion Service private key collides with an "
|
|
|
|
"existing service.");
|
|
|
|
rend_service_free(s);
|
|
|
|
return RSAE_ADDREXISTS;
|
|
|
|
}
|
|
|
|
if (rend_service_get_by_service_id(s->service_id)) {
|
|
|
|
log_warn(LD_CONFIG, "Onion Service id collides with an existing service.");
|
|
|
|
rend_service_free(s);
|
|
|
|
return RSAE_ADDREXISTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the service. */
|
2016-11-18 04:32:13 +01:00
|
|
|
if (rend_add_service(NULL, s)) {
|
2015-04-25 10:23:15 +02:00
|
|
|
return RSAE_INTERNAL;
|
|
|
|
}
|
|
|
|
*service_id_out = tor_strdup(s->service_id);
|
|
|
|
|
|
|
|
log_debug(LD_CONFIG, "Added ephemeral Onion Service: %s", s->service_id);
|
|
|
|
return RSAE_OKAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Remove the ephemeral service <b>service_id</b> if possible. Returns 0 on
|
|
|
|
* success, and -1 on failure.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
rend_service_del_ephemeral(const char *service_id)
|
|
|
|
{
|
|
|
|
rend_service_t *s;
|
|
|
|
if (!rend_valid_service_id(service_id)) {
|
|
|
|
log_warn(LD_CONFIG, "Requested malformed Onion Service id for removal.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((s = rend_service_get_by_service_id(service_id)) == NULL) {
|
|
|
|
log_warn(LD_CONFIG, "Requested non-existent Onion Service id for "
|
|
|
|
"removal.");
|
|
|
|
return -1;
|
|
|
|
}
|
2016-11-01 20:40:41 +01:00
|
|
|
if (!rend_service_is_ephemeral(s)) {
|
2015-04-25 10:23:15 +02:00
|
|
|
log_warn(LD_CONFIG, "Requested non-ephemeral Onion Service for removal.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Kill the intro point circuit for the Onion Service, and remove it from
|
|
|
|
* the list. Closing existing connections is the application's problem.
|
|
|
|
*
|
|
|
|
* XXX: As with the comment in rend_config_services(), a nice abstraction
|
|
|
|
* would be ideal here, but for now just duplicate the code.
|
|
|
|
*/
|
|
|
|
SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
|
|
|
|
if (!circ->marked_for_close &&
|
|
|
|
(circ->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO ||
|
|
|
|
circ->purpose == CIRCUIT_PURPOSE_S_INTRO)) {
|
|
|
|
origin_circuit_t *oc = TO_ORIGIN_CIRCUIT(circ);
|
|
|
|
tor_assert(oc->rend_data);
|
2016-05-31 20:51:30 +02:00
|
|
|
if (!rend_circuit_pk_digest_eq(oc, (uint8_t *) s->pk_digest)) {
|
2015-04-25 10:23:15 +02:00
|
|
|
continue;
|
2016-05-31 20:51:30 +02:00
|
|
|
}
|
2015-04-25 10:23:15 +02:00
|
|
|
log_debug(LD_REND, "Closing intro point %s for service %s.",
|
|
|
|
safe_str_client(extend_info_describe(
|
|
|
|
oc->build_state->chosen_exit)),
|
2016-05-31 20:51:30 +02:00
|
|
|
rend_data_get_address(oc->rend_data));
|
2015-04-25 10:23:15 +02:00
|
|
|
circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
|
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(circ);
|
|
|
|
smartlist_remove(rend_service_list, s);
|
|
|
|
rend_service_free(s);
|
|
|
|
|
|
|
|
log_debug(LD_CONFIG, "Removed ephemeral Onion Service: %s", service_id);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-04 12:42:59 +01:00
|
|
|
/* There can be 1 second's delay due to second_elapsed_callback, and perhaps
|
|
|
|
* another few seconds due to blocking calls. */
|
|
|
|
#define INTRO_CIRC_RETRY_PERIOD_SLOP 10
|
|
|
|
|
|
|
|
/** Log information about the intro point creation rate and current intro
|
|
|
|
* points for service, upgrading the log level from min_severity to warn if
|
|
|
|
* we have stopped launching new intro point circuits. */
|
|
|
|
static void
|
|
|
|
rend_log_intro_limit(const rend_service_t *service, int min_severity)
|
|
|
|
{
|
|
|
|
int exceeded_limit = (service->n_intro_circuits_launched >=
|
|
|
|
rend_max_intro_circs_per_period(
|
|
|
|
service->n_intro_points_wanted));
|
|
|
|
int severity = min_severity;
|
|
|
|
/* We stopped creating circuits */
|
|
|
|
if (exceeded_limit) {
|
|
|
|
severity = LOG_WARN;
|
|
|
|
}
|
|
|
|
time_t intro_period_elapsed = time(NULL) - service->intro_period_started;
|
|
|
|
tor_assert_nonfatal(intro_period_elapsed >= 0);
|
|
|
|
/* We delayed resuming circuits longer than expected */
|
|
|
|
int exceeded_elapsed = (intro_period_elapsed > INTRO_CIRC_RETRY_PERIOD +
|
|
|
|
INTRO_CIRC_RETRY_PERIOD_SLOP);
|
|
|
|
if (exceeded_elapsed) {
|
|
|
|
severity = LOG_WARN;
|
|
|
|
}
|
|
|
|
log_fn(severity, LD_REND, "Hidden service %s %s %d intro points in the last "
|
|
|
|
"%d seconds%s. Intro circuit launches are limited to %d per %d "
|
|
|
|
"seconds.",
|
|
|
|
service->service_id,
|
|
|
|
exceeded_limit ? "exceeded launch limit with" : "launched",
|
|
|
|
service->n_intro_circuits_launched,
|
|
|
|
(int)intro_period_elapsed,
|
|
|
|
exceeded_elapsed ? " (delayed)" : "",
|
|
|
|
rend_max_intro_circs_per_period(service->n_intro_points_wanted),
|
|
|
|
INTRO_CIRC_RETRY_PERIOD);
|
|
|
|
rend_service_dump_stats(severity);
|
|
|
|
}
|
|
|
|
|
2004-05-10 06:34:48 +02:00
|
|
|
/** Replace the old value of <b>service</b>-\>desc with one that reflects
|
2004-04-03 02:58:54 +02:00
|
|
|
* the other fields in service.
|
2004-04-03 01:01:00 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
|
|
|
rend_service_update_descriptor(rend_service_t *service)
|
2004-04-03 01:01:00 +02:00
|
|
|
{
|
|
|
|
rend_service_descriptor_t *d;
|
2007-12-21 10:28:22 +01:00
|
|
|
int i;
|
2009-12-12 08:07:59 +01:00
|
|
|
|
|
|
|
rend_service_descriptor_free(service->desc);
|
|
|
|
service->desc = NULL;
|
|
|
|
|
2007-10-28 20:48:16 +01:00
|
|
|
d = service->desc = tor_malloc_zero(sizeof(rend_service_descriptor_t));
|
2004-04-03 01:01:00 +02:00
|
|
|
d->pk = crypto_pk_dup_key(service->private_key);
|
|
|
|
d->timestamp = time(NULL);
|
2013-09-18 17:09:34 +02:00
|
|
|
d->timestamp -= d->timestamp % 3600; /* Round down to nearest hour */
|
2012-01-18 21:53:30 +01:00
|
|
|
d->intro_nodes = smartlist_new();
|
2008-08-19 17:41:28 +02:00
|
|
|
/* Support intro protocols 2 and 3. */
|
|
|
|
d->protocols = (1 << 2) + (1 << 3);
|
2007-10-28 20:48:16 +01:00
|
|
|
|
2007-12-21 10:28:22 +01:00
|
|
|
for (i = 0; i < smartlist_len(service->intro_nodes); ++i) {
|
|
|
|
rend_intro_point_t *intro_svc = smartlist_get(service->intro_nodes, i);
|
|
|
|
rend_intro_point_t *intro_desc;
|
2011-10-13 17:48:31 +02:00
|
|
|
|
|
|
|
/* This intro point won't be listed in the descriptor... */
|
|
|
|
intro_svc->listed_in_last_desc = 0;
|
|
|
|
|
2017-03-02 05:55:33 +01:00
|
|
|
/* circuit_established is set in rend_service_intro_established(), and
|
|
|
|
* checked every second in rend_consider_services_intro_points(), so it's
|
|
|
|
* safe to use it here */
|
|
|
|
if (!intro_svc->circuit_established) {
|
2004-09-20 05:07:11 +02:00
|
|
|
continue;
|
2011-10-16 00:40:28 +02:00
|
|
|
}
|
2007-12-21 10:28:22 +01:00
|
|
|
|
2011-10-13 17:48:31 +02:00
|
|
|
/* ...unless this intro point is listed in the descriptor. */
|
|
|
|
intro_svc->listed_in_last_desc = 1;
|
2007-12-21 10:28:22 +01:00
|
|
|
|
2011-10-06 00:36:47 +02:00
|
|
|
/* We have an entirely established intro circuit. Publish it in
|
|
|
|
* our descriptor. */
|
2007-12-21 10:28:22 +01:00
|
|
|
intro_desc = tor_malloc_zero(sizeof(rend_intro_point_t));
|
|
|
|
intro_desc->extend_info = extend_info_dup(intro_svc->extend_info);
|
|
|
|
if (intro_svc->intro_key)
|
|
|
|
intro_desc->intro_key = crypto_pk_dup_key(intro_svc->intro_key);
|
|
|
|
smartlist_add(d->intro_nodes, intro_desc);
|
2011-10-06 00:43:02 +02:00
|
|
|
|
|
|
|
if (intro_svc->time_published == -1) {
|
|
|
|
/* We are publishing this intro point in a descriptor for the
|
|
|
|
* first time -- note the current time in the service's copy of
|
|
|
|
* the intro point. */
|
|
|
|
intro_svc->time_published = time(NULL);
|
|
|
|
}
|
2004-04-03 01:01:00 +02:00
|
|
|
}
|
2017-03-02 06:38:21 +01:00
|
|
|
|
|
|
|
/* Check that we have the right number of intro points */
|
|
|
|
unsigned int have_intro = (unsigned int)smartlist_len(d->intro_nodes);
|
|
|
|
if (have_intro != service->n_intro_points_wanted) {
|
|
|
|
int severity;
|
|
|
|
/* Getting less than we wanted or more than we're allowed is serious */
|
|
|
|
if (have_intro < service->n_intro_points_wanted ||
|
|
|
|
have_intro > NUM_INTRO_POINTS_MAX) {
|
|
|
|
severity = LOG_WARN;
|
|
|
|
} else {
|
|
|
|
/* Getting more than we wanted is weird, but less of a problem */
|
|
|
|
severity = LOG_NOTICE;
|
|
|
|
}
|
|
|
|
log_fn(severity, LD_REND, "Hidden service %s wanted %d intro points, but "
|
|
|
|
"descriptor was updated with %d instead.",
|
|
|
|
service->service_id,
|
|
|
|
service->n_intro_points_wanted, have_intro);
|
2017-03-04 12:44:21 +01:00
|
|
|
/* Now log an informative message about how we might have got here. */
|
|
|
|
rend_log_intro_limit(service, severity);
|
2017-03-02 06:38:21 +01:00
|
|
|
}
|
2004-04-03 01:01:00 +02:00
|
|
|
}
|
|
|
|
|
2016-09-07 05:29:07 +02:00
|
|
|
/* Allocate and return a string containing the path to file_name in
|
|
|
|
* service->directory. Asserts that service has a directory.
|
|
|
|
* This function will never return NULL.
|
|
|
|
* The caller must free this path. */
|
2016-07-14 06:04:02 +02:00
|
|
|
static char *
|
2016-09-07 05:29:07 +02:00
|
|
|
rend_service_path(const rend_service_t *service, const char *file_name)
|
2016-07-14 06:04:02 +02:00
|
|
|
{
|
2016-09-07 05:29:07 +02:00
|
|
|
char *file_path = NULL;
|
2016-07-14 06:04:02 +02:00
|
|
|
|
|
|
|
tor_assert(service->directory);
|
|
|
|
|
2016-09-07 05:29:07 +02:00
|
|
|
/* Can never fail: asserts rather than leaving file_path NULL. */
|
|
|
|
tor_asprintf(&file_path, "%s%s%s",
|
|
|
|
service->directory, PATH_SEPARATOR, file_name);
|
|
|
|
|
|
|
|
return file_path;
|
|
|
|
}
|
2016-07-14 06:04:02 +02:00
|
|
|
|
2016-09-07 05:29:07 +02:00
|
|
|
/* Allocate and return a string containing the path to the single onion
|
|
|
|
* service poison file in service->directory. Asserts that service has a
|
|
|
|
* directory.
|
|
|
|
* The caller must free this path. */
|
|
|
|
STATIC char *
|
|
|
|
rend_service_sos_poison_path(const rend_service_t *service)
|
|
|
|
{
|
|
|
|
return rend_service_path(service, sos_poison_fname);
|
2016-07-14 06:04:02 +02:00
|
|
|
}
|
|
|
|
|
2016-10-24 16:31:16 +02:00
|
|
|
/** Return True if hidden services <b>service</b> has been poisoned by single
|
2016-07-14 06:04:02 +02:00
|
|
|
* onion mode. */
|
|
|
|
static int
|
|
|
|
service_is_single_onion_poisoned(const rend_service_t *service)
|
|
|
|
{
|
|
|
|
char *poison_fname = NULL;
|
|
|
|
file_status_t fstatus;
|
|
|
|
|
2016-11-18 01:46:01 +01:00
|
|
|
/* Passing a NULL service is a bug */
|
|
|
|
if (BUG(!service)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-01 20:40:41 +01:00
|
|
|
if (rend_service_is_ephemeral(service)) {
|
2016-07-14 06:04:02 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-07 05:29:07 +02:00
|
|
|
poison_fname = rend_service_sos_poison_path(service);
|
2016-07-14 06:04:02 +02:00
|
|
|
|
|
|
|
fstatus = file_status(poison_fname);
|
|
|
|
tor_free(poison_fname);
|
|
|
|
|
2016-11-02 04:11:26 +01:00
|
|
|
/* If this fname is occupied, the hidden service has been poisoned.
|
|
|
|
* fstatus can be FN_ERROR if the service directory does not exist, in that
|
|
|
|
* case, there is obviously no private key. */
|
2016-07-14 06:04:02 +02:00
|
|
|
if (fstatus == FN_FILE || fstatus == FN_EMPTY) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return 1 if the private key file for service exists and has a non-zero size,
|
|
|
|
* and 0 otherwise. */
|
|
|
|
static int
|
|
|
|
rend_service_private_key_exists(const rend_service_t *service)
|
|
|
|
{
|
|
|
|
char *private_key_path = rend_service_path(service, private_key_fname);
|
|
|
|
const file_status_t private_key_status = file_status(private_key_path);
|
|
|
|
tor_free(private_key_path);
|
2016-11-02 04:11:26 +01:00
|
|
|
/* Only non-empty regular private key files could have been used before.
|
|
|
|
* fstatus can be FN_ERROR if the service directory does not exist, in that
|
|
|
|
* case, there is obviously no private key. */
|
2016-07-14 06:04:02 +02:00
|
|
|
return private_key_status == FN_FILE;
|
|
|
|
}
|
|
|
|
|
2016-11-18 01:46:01 +01:00
|
|
|
/** Check the single onion service poison state of the directory for s:
|
|
|
|
* - If the service is poisoned, and we are in Single Onion Mode,
|
2016-09-13 09:20:46 +02:00
|
|
|
* return 0,
|
2016-11-18 01:46:01 +01:00
|
|
|
* - If the service is not poisoned, and we are not in Single Onion Mode,
|
2016-07-14 06:04:02 +02:00
|
|
|
* return 0,
|
2016-11-18 01:46:01 +01:00
|
|
|
* - Otherwise, the poison state is invalid: the service was created in one
|
|
|
|
* mode, and is being used in the other, return -1.
|
|
|
|
* Hidden service directories without keys are always considered consistent.
|
|
|
|
* They will be poisoned after their directory is created (if needed). */
|
|
|
|
STATIC int
|
|
|
|
rend_service_verify_single_onion_poison(const rend_service_t* s,
|
|
|
|
const or_options_t* options)
|
2016-07-14 06:04:02 +02:00
|
|
|
{
|
2016-11-18 01:46:01 +01:00
|
|
|
/* Passing a NULL service is a bug */
|
|
|
|
if (BUG(!s)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-07-14 06:04:02 +02:00
|
|
|
|
2016-11-18 01:46:01 +01:00
|
|
|
/* Ephemeral services are checked at ADD_ONION time */
|
2016-12-03 06:10:28 +01:00
|
|
|
if (BUG(rend_service_is_ephemeral(s))) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Service is expected to have a directory */
|
|
|
|
if (BUG(!s->directory)) {
|
|
|
|
return -1;
|
2016-07-14 06:04:02 +02:00
|
|
|
}
|
|
|
|
|
2016-11-18 01:46:01 +01:00
|
|
|
/* Services without keys are always ok - their keys will only ever be used
|
|
|
|
* in the current mode */
|
|
|
|
if (!rend_service_private_key_exists(s)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-07-14 06:04:02 +02:00
|
|
|
|
2016-11-18 01:46:01 +01:00
|
|
|
/* The key has been used before in a different mode */
|
|
|
|
if (service_is_single_onion_poisoned(s) !=
|
|
|
|
rend_service_non_anonymous_mode_enabled(options)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-07-14 06:04:02 +02:00
|
|
|
|
2016-11-18 01:46:01 +01:00
|
|
|
/* The key exists and is consistent with the current mode */
|
|
|
|
return 0;
|
2016-07-14 06:04:02 +02:00
|
|
|
}
|
|
|
|
|
2016-11-18 01:46:01 +01:00
|
|
|
/*** Helper for rend_service_poison_new_single_onion_dir(). Add a file to
|
|
|
|
* the hidden service directory for s that marks it as a single onion service.
|
|
|
|
* Tor must be in single onion mode before calling this function, and the
|
|
|
|
* service directory must already have been created.
|
2016-09-13 09:20:46 +02:00
|
|
|
* Returns 0 when a directory is successfully poisoned, or if it is already
|
|
|
|
* poisoned. Returns -1 on a failure to read the directory or write the poison
|
|
|
|
* file, or if there is an existing private key file in the directory. (The
|
|
|
|
* service should have been poisoned when the key was created.) */
|
2016-07-14 06:04:02 +02:00
|
|
|
static int
|
2016-11-18 01:46:01 +01:00
|
|
|
poison_new_single_onion_hidden_service_dir_impl(const rend_service_t *service,
|
|
|
|
const or_options_t* options)
|
2016-07-14 06:04:02 +02:00
|
|
|
{
|
2016-11-18 01:46:01 +01:00
|
|
|
/* Passing a NULL service is a bug */
|
|
|
|
if (BUG(!service)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-07-14 06:04:02 +02:00
|
|
|
/* We must only poison directories if we're in Single Onion mode */
|
2016-11-18 01:46:01 +01:00
|
|
|
tor_assert(rend_service_non_anonymous_mode_enabled(options));
|
2016-07-14 06:04:02 +02:00
|
|
|
|
|
|
|
int fd;
|
|
|
|
int retval = -1;
|
|
|
|
char *poison_fname = NULL;
|
|
|
|
|
2016-11-01 20:40:41 +01:00
|
|
|
if (rend_service_is_ephemeral(service)) {
|
2016-09-13 09:20:46 +02:00
|
|
|
log_info(LD_REND, "Ephemeral HS started in non-anonymous mode.");
|
2016-07-14 06:04:02 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure we're only poisoning new hidden service directories */
|
|
|
|
if (rend_service_private_key_exists(service)) {
|
|
|
|
log_warn(LD_BUG, "Tried to single onion poison a service directory after "
|
|
|
|
"the private key was created.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-11-18 01:46:01 +01:00
|
|
|
/* Make sure the directory was created before calling this function. */
|
|
|
|
if (BUG(rend_service_check_private_dir_impl(options, service, 0) < 0))
|
2016-10-31 19:53:30 +01:00
|
|
|
return -1;
|
|
|
|
|
2016-09-07 05:29:07 +02:00
|
|
|
poison_fname = rend_service_sos_poison_path(service);
|
2016-07-14 06:04:02 +02:00
|
|
|
|
|
|
|
switch (file_status(poison_fname)) {
|
|
|
|
case FN_DIR:
|
|
|
|
case FN_ERROR:
|
|
|
|
log_warn(LD_FS, "Can't read single onion poison file \"%s\"",
|
|
|
|
poison_fname);
|
|
|
|
goto done;
|
|
|
|
case FN_FILE: /* single onion poison file already exists. NOP. */
|
|
|
|
case FN_EMPTY: /* single onion poison file already exists. NOP. */
|
|
|
|
log_debug(LD_FS, "Tried to re-poison a single onion poisoned file \"%s\"",
|
|
|
|
poison_fname);
|
|
|
|
break;
|
|
|
|
case FN_NOENT:
|
|
|
|
fd = tor_open_cloexec(poison_fname, O_RDWR|O_CREAT|O_TRUNC, 0600);
|
|
|
|
if (fd < 0) {
|
|
|
|
log_warn(LD_FS, "Could not create single onion poison file %s",
|
|
|
|
poison_fname);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
tor_assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = 0;
|
|
|
|
|
|
|
|
done:
|
|
|
|
tor_free(poison_fname);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2016-12-03 06:10:28 +01:00
|
|
|
/** We just got launched in Single Onion Mode. That's a non-anonymous mode for
|
2016-11-18 01:46:01 +01:00
|
|
|
* hidden services. If s is new, we should mark its hidden service
|
|
|
|
* directory appropriately so that it is never launched as a location-private
|
|
|
|
* hidden service. (New directories don't have private key files.)
|
2016-07-14 06:04:02 +02:00
|
|
|
* Return 0 on success, -1 on fail. */
|
2016-11-18 01:46:01 +01:00
|
|
|
STATIC int
|
|
|
|
rend_service_poison_new_single_onion_dir(const rend_service_t *s,
|
|
|
|
const or_options_t* options)
|
2016-07-14 06:04:02 +02:00
|
|
|
{
|
2016-11-18 01:46:01 +01:00
|
|
|
/* Passing a NULL service is a bug */
|
|
|
|
if (BUG(!s)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-07-14 06:04:02 +02:00
|
|
|
/* We must only poison directories if we're in Single Onion mode */
|
2016-11-18 01:46:01 +01:00
|
|
|
tor_assert(rend_service_non_anonymous_mode_enabled(options));
|
2016-07-14 06:04:02 +02:00
|
|
|
|
2016-12-03 06:10:28 +01:00
|
|
|
/* Ephemeral services aren't allowed in non-anonymous mode */
|
|
|
|
if (BUG(rend_service_is_ephemeral(s))) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Service is expected to have a directory */
|
|
|
|
if (BUG(!s->directory)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-11-18 01:46:01 +01:00
|
|
|
if (!rend_service_private_key_exists(s)) {
|
|
|
|
if (poison_new_single_onion_hidden_service_dir_impl(s, options)
|
|
|
|
< 0) {
|
|
|
|
return -1;
|
2016-07-14 06:04:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-08-08 16:36:11 +02:00
|
|
|
/** Load and/or generate private keys for all hidden services, possibly
|
2016-09-07 05:24:43 +02:00
|
|
|
* including keys for client authorization.
|
|
|
|
* If a <b>service_list</b> is provided, treat it as the list of hidden
|
|
|
|
* services (used in unittests). Otherwise, require that rend_service_list is
|
|
|
|
* not NULL.
|
|
|
|
* Return 0 on success, -1 on failure. */
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2016-09-07 05:24:43 +02:00
|
|
|
rend_service_load_all_keys(const smartlist_t *service_list)
|
2004-03-31 23:54:56 +02:00
|
|
|
{
|
2016-11-18 01:50:34 +01:00
|
|
|
/* Use service_list for unit tests */
|
|
|
|
const smartlist_t *s_list = rend_get_service_list(service_list);
|
|
|
|
if (BUG(!s_list)) {
|
|
|
|
return -1;
|
2016-09-07 05:24:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH_BEGIN(s_list, rend_service_t *, s) {
|
2004-03-31 23:54:56 +02:00
|
|
|
if (s->private_key)
|
|
|
|
continue;
|
2016-11-01 20:55:03 +01:00
|
|
|
log_info(LD_REND, "Loading hidden-service keys from %s",
|
|
|
|
rend_service_escaped_dir(s));
|
2004-04-03 02:58:54 +02:00
|
|
|
|
2012-06-18 18:43:20 +02:00
|
|
|
if (rend_service_load_keys(s) < 0)
|
2004-03-31 23:54:56 +02:00
|
|
|
return -1;
|
2012-06-18 18:43:20 +02:00
|
|
|
} SMARTLIST_FOREACH_END(s);
|
2004-03-31 23:54:56 +02:00
|
|
|
|
2012-06-18 18:43:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-23 02:39:10 +02:00
|
|
|
/** Add to <b>lst</b> every filename used by <b>s</b>. */
|
|
|
|
static void
|
|
|
|
rend_service_add_filenames_to_list(smartlist_t *lst, const rend_service_t *s)
|
|
|
|
{
|
|
|
|
tor_assert(lst);
|
|
|
|
tor_assert(s);
|
2015-04-25 10:23:15 +02:00
|
|
|
tor_assert(s->directory);
|
2016-09-07 05:29:07 +02:00
|
|
|
smartlist_add(lst, rend_service_path(s, private_key_fname));
|
|
|
|
smartlist_add(lst, rend_service_path(s, hostname_fname));
|
|
|
|
smartlist_add(lst, rend_service_path(s, client_keys_fname));
|
|
|
|
smartlist_add(lst, rend_service_sos_poison_path(s));
|
2014-05-23 02:39:10 +02:00
|
|
|
}
|
|
|
|
|
2014-05-23 17:46:44 +02:00
|
|
|
/** Add to <b>open_lst</b> every filename used by a configured hidden service,
|
|
|
|
* and to <b>stat_lst</b> every directory used by a configured hidden
|
|
|
|
* service */
|
2014-05-23 02:39:10 +02:00
|
|
|
void
|
2014-05-23 17:46:44 +02:00
|
|
|
rend_services_add_filenames_to_lists(smartlist_t *open_lst,
|
|
|
|
smartlist_t *stat_lst)
|
2014-05-23 02:39:10 +02:00
|
|
|
{
|
|
|
|
if (!rend_service_list)
|
|
|
|
return;
|
|
|
|
SMARTLIST_FOREACH_BEGIN(rend_service_list, rend_service_t *, s) {
|
2016-11-01 20:40:41 +01:00
|
|
|
if (!rend_service_is_ephemeral(s)) {
|
2015-04-25 10:23:15 +02:00
|
|
|
rend_service_add_filenames_to_list(open_lst, s);
|
2016-10-27 11:26:06 +02:00
|
|
|
smartlist_add_strdup(stat_lst, s->directory);
|
2015-04-25 10:23:15 +02:00
|
|
|
}
|
2014-05-23 02:39:10 +02:00
|
|
|
} SMARTLIST_FOREACH_END(s);
|
|
|
|
}
|
|
|
|
|
2015-04-25 10:23:15 +02:00
|
|
|
/** Derive all rend_service_t internal material based on the service's key.
|
|
|
|
* Returns 0 on sucess, -1 on failure.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
rend_service_derive_key_digests(struct rend_service_t *s)
|
|
|
|
{
|
|
|
|
if (rend_get_service_id(s->private_key, s->service_id)<0) {
|
|
|
|
log_warn(LD_BUG, "Internal error: couldn't encode service ID.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (crypto_pk_get_digest(s->private_key, s->pk_digest)<0) {
|
|
|
|
log_warn(LD_BUG, "Couldn't compute hash of public key.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-18 01:46:01 +01:00
|
|
|
/* Implements the directory check from rend_service_check_private_dir,
|
|
|
|
* without doing the single onion poison checks. */
|
|
|
|
static int
|
|
|
|
rend_service_check_private_dir_impl(const or_options_t *options,
|
|
|
|
const rend_service_t *s,
|
|
|
|
int create)
|
|
|
|
{
|
|
|
|
cpd_check_t check_opts = CPD_NONE;
|
|
|
|
if (create) {
|
|
|
|
check_opts |= CPD_CREATE;
|
|
|
|
} else {
|
|
|
|
check_opts |= CPD_CHECK_MODE_ONLY;
|
|
|
|
check_opts |= CPD_CHECK;
|
|
|
|
}
|
|
|
|
if (s->dir_group_readable) {
|
|
|
|
check_opts |= CPD_GROUP_READ;
|
|
|
|
}
|
|
|
|
/* Check/create directory */
|
|
|
|
if (check_private_dir(s->directory, check_opts, options->User) < 0) {
|
|
|
|
log_warn(LD_REND, "Checking service directory %s failed.", s->directory);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-02 04:55:14 +01:00
|
|
|
/** Make sure that the directory for <b>s</b> is private, using the config in
|
|
|
|
* <b>options</b>.
|
2016-11-02 04:10:52 +01:00
|
|
|
* If <b>create</b> is true:
|
|
|
|
* - if the directory exists, change permissions if needed,
|
|
|
|
* - if the directory does not exist, create it with the correct permissions.
|
|
|
|
* If <b>create</b> is false:
|
|
|
|
* - if the directory exists, check permissions,
|
|
|
|
* - if the directory does not exist, check if we think we can create it.
|
2016-11-02 01:16:00 +01:00
|
|
|
* Return 0 on success, -1 on failure. */
|
2012-06-18 18:43:20 +02:00
|
|
|
static int
|
2016-11-02 04:55:14 +01:00
|
|
|
rend_service_check_private_dir(const or_options_t *options,
|
|
|
|
const rend_service_t *s,
|
|
|
|
int create)
|
2004-03-31 23:54:56 +02:00
|
|
|
{
|
2016-11-18 01:46:01 +01:00
|
|
|
/* Passing a NULL service is a bug */
|
|
|
|
if (BUG(!s)) {
|
|
|
|
return -1;
|
2014-08-28 20:10:21 +02:00
|
|
|
}
|
2016-11-18 01:46:01 +01:00
|
|
|
|
2012-06-18 18:45:55 +02:00
|
|
|
/* Check/create directory */
|
2016-11-18 01:46:01 +01:00
|
|
|
if (rend_service_check_private_dir_impl(options, s, create) < 0) {
|
2016-10-31 19:53:30 +01:00
|
|
|
return -1;
|
2014-08-28 20:10:21 +02:00
|
|
|
}
|
2016-11-18 01:46:01 +01:00
|
|
|
|
|
|
|
/* Check if the hidden service key exists, and was created in a different
|
|
|
|
* single onion service mode, and refuse to launch if it has.
|
|
|
|
* This is safe to call even when create is false, as it ignores missing
|
|
|
|
* keys and directories: they are always valid.
|
|
|
|
*/
|
|
|
|
if (rend_service_verify_single_onion_poison(s, options) < 0) {
|
|
|
|
/* We can't use s->service_id here, as the key may not have been loaded */
|
|
|
|
log_warn(LD_GENERAL, "We are configured with "
|
|
|
|
"HiddenServiceNonAnonymousMode %d, but the hidden "
|
|
|
|
"service key in directory %s was created in %s mode. "
|
|
|
|
"This is not allowed.",
|
|
|
|
rend_service_non_anonymous_mode_enabled(options) ? 1 : 0,
|
|
|
|
rend_service_escaped_dir(s),
|
|
|
|
rend_service_non_anonymous_mode_enabled(options) ?
|
|
|
|
"an anonymous" : "a non-anonymous"
|
|
|
|
);
|
2016-10-31 19:53:30 +01:00
|
|
|
return -1;
|
2014-08-28 20:10:21 +02:00
|
|
|
}
|
2016-11-18 01:46:01 +01:00
|
|
|
|
|
|
|
/* Poison new single onion directories immediately after they are created,
|
|
|
|
* so that we never accidentally launch non-anonymous hidden services
|
|
|
|
* thinking they are anonymous. Any keys created later will end up with the
|
|
|
|
* correct poisoning state.
|
|
|
|
*/
|
|
|
|
if (create && rend_service_non_anonymous_mode_enabled(options)) {
|
|
|
|
static int logged_warning = 0;
|
|
|
|
|
|
|
|
if (rend_service_poison_new_single_onion_dir(s, options) < 0) {
|
|
|
|
log_warn(LD_GENERAL,"Failed to mark new hidden services as non-anonymous"
|
|
|
|
".");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!logged_warning) {
|
|
|
|
/* The keys for these services are linked to the server IP address */
|
|
|
|
log_notice(LD_REND, "The configured onion service directories have been "
|
|
|
|
"used in single onion mode. They can not be used for "
|
|
|
|
"anonymous hidden services.");
|
|
|
|
logged_warning = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-31 19:53:30 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-18 18:43:20 +02:00
|
|
|
/** Load and/or generate private keys for the hidden service <b>s</b>,
|
|
|
|
* possibly including keys for client authorization. Return 0 on success, -1
|
|
|
|
* on failure. */
|
|
|
|
static int
|
|
|
|
rend_service_load_keys(rend_service_t *s)
|
2004-03-31 23:54:56 +02:00
|
|
|
{
|
2016-09-07 05:29:07 +02:00
|
|
|
char *fname = NULL;
|
2012-06-18 18:59:29 +02:00
|
|
|
char buf[128];
|
2004-03-31 23:54:56 +02:00
|
|
|
|
2016-11-18 01:46:01 +01:00
|
|
|
/* Make sure the directory was created and single onion poisoning was
|
|
|
|
* checked before calling this function */
|
2016-11-04 05:46:24 +01:00
|
|
|
if (BUG(rend_service_check_private_dir(get_options(), s, 0) < 0))
|
2016-09-07 05:29:07 +02:00
|
|
|
goto err;
|
2012-06-18 18:45:55 +02:00
|
|
|
|
|
|
|
/* Load key */
|
2016-09-07 05:29:07 +02:00
|
|
|
fname = rend_service_path(s, private_key_fname);
|
2014-12-11 06:30:14 +01:00
|
|
|
s->private_key = init_key_from_file(fname, 1, LOG_ERR, 0);
|
2016-09-07 05:29:07 +02:00
|
|
|
|
2012-06-18 18:45:55 +02:00
|
|
|
if (!s->private_key)
|
2016-09-07 05:29:07 +02:00
|
|
|
goto err;
|
2012-06-18 18:45:55 +02:00
|
|
|
|
2015-04-25 10:23:15 +02:00
|
|
|
if (rend_service_derive_key_digests(s) < 0)
|
2016-09-07 05:29:07 +02:00
|
|
|
goto err;
|
2015-04-25 10:23:15 +02:00
|
|
|
|
2016-09-07 05:29:07 +02:00
|
|
|
tor_free(fname);
|
2015-04-25 10:23:15 +02:00
|
|
|
/* Create service file */
|
2016-09-07 05:29:07 +02:00
|
|
|
fname = rend_service_path(s, hostname_fname);
|
2012-06-18 19:05:28 +02:00
|
|
|
|
2012-06-18 18:45:55 +02:00
|
|
|
tor_snprintf(buf, sizeof(buf),"%s.onion\n", s->service_id);
|
|
|
|
if (write_str_to_file(fname,buf,0)<0) {
|
|
|
|
log_warn(LD_CONFIG, "Could not write onion address to hostname file.");
|
2016-09-07 05:29:07 +02:00
|
|
|
goto err;
|
2012-06-18 18:45:55 +02:00
|
|
|
}
|
2014-08-28 20:10:21 +02:00
|
|
|
#ifndef _WIN32
|
2014-08-30 23:14:51 +02:00
|
|
|
if (s->dir_group_readable) {
|
2014-08-29 07:58:53 +02:00
|
|
|
/* Also verify hostname file created with group read. */
|
2014-09-02 19:59:31 +02:00
|
|
|
if (chmod(fname, 0640))
|
|
|
|
log_warn(LD_FS,"Unable to make hidden hostname file %s group-readable.",
|
|
|
|
fname);
|
2014-08-28 20:10:21 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-06-18 18:45:55 +02:00
|
|
|
/* If client authorization is configured, load or generate keys. */
|
|
|
|
if (s->auth_type != REND_NO_AUTH) {
|
2016-09-07 05:29:07 +02:00
|
|
|
if (rend_service_load_auth_keys(s, fname) < 0) {
|
|
|
|
goto err;
|
|
|
|
}
|
2012-06-18 18:59:29 +02:00
|
|
|
}
|
|
|
|
|
2016-09-07 05:29:07 +02:00
|
|
|
int r = 0;
|
|
|
|
goto done;
|
|
|
|
err:
|
|
|
|
r = -1;
|
|
|
|
done:
|
|
|
|
memwipe(buf, 0, sizeof(buf));
|
|
|
|
tor_free(fname);
|
|
|
|
return r;
|
2012-06-18 18:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Load and/or generate client authorization keys for the hidden service
|
2012-06-18 19:05:28 +02:00
|
|
|
* <b>s</b>, which stores its hostname in <b>hfname</b>. Return 0 on success,
|
|
|
|
* -1 on failure. */
|
2012-06-18 18:59:29 +02:00
|
|
|
static int
|
2012-06-18 19:05:28 +02:00
|
|
|
rend_service_load_auth_keys(rend_service_t *s, const char *hfname)
|
2012-06-18 18:59:29 +02:00
|
|
|
{
|
|
|
|
int r = 0;
|
2016-09-07 05:29:07 +02:00
|
|
|
char *cfname = NULL;
|
2012-06-18 18:59:29 +02:00
|
|
|
char *client_keys_str = NULL;
|
|
|
|
strmap_t *parsed_clients = strmap_new();
|
|
|
|
FILE *cfile, *hfile;
|
|
|
|
open_file_t *open_cfile = NULL, *open_hfile = NULL;
|
|
|
|
char desc_cook_out[3*REND_DESC_COOKIE_LEN_BASE64+1];
|
|
|
|
char service_id[16+1];
|
|
|
|
char buf[1500];
|
2012-06-18 18:45:55 +02:00
|
|
|
|
2012-06-18 19:01:15 +02:00
|
|
|
/* Load client keys and descriptor cookies, if available. */
|
2016-09-07 05:29:07 +02:00
|
|
|
cfname = rend_service_path(s, client_keys_fname);
|
2012-06-18 19:01:15 +02:00
|
|
|
client_keys_str = read_file_to_str(cfname, RFTS_IGNORE_MISSING, NULL);
|
|
|
|
if (client_keys_str) {
|
|
|
|
if (rend_parse_client_keys(parsed_clients, client_keys_str) < 0) {
|
|
|
|
log_warn(LD_CONFIG, "Previously stored client_keys file could not "
|
|
|
|
"be parsed.");
|
2012-06-18 18:45:55 +02:00
|
|
|
goto err;
|
2012-06-18 19:01:15 +02:00
|
|
|
} else {
|
|
|
|
log_info(LD_CONFIG, "Parsed %d previously stored client entries.",
|
|
|
|
strmap_size(parsed_clients));
|
2004-03-31 23:54:56 +02:00
|
|
|
}
|
2012-06-18 19:01:15 +02:00
|
|
|
}
|
2004-03-31 23:54:56 +02:00
|
|
|
|
2012-06-18 19:01:15 +02:00
|
|
|
/* Prepare client_keys and hostname files. */
|
|
|
|
if (!(cfile = start_writing_to_stdio_file(cfname,
|
|
|
|
OPEN_FLAGS_REPLACE | O_TEXT,
|
|
|
|
0600, &open_cfile))) {
|
|
|
|
log_warn(LD_CONFIG, "Could not open client_keys file %s",
|
|
|
|
escaped(cfname));
|
|
|
|
goto err;
|
|
|
|
}
|
2012-06-18 19:05:28 +02:00
|
|
|
|
|
|
|
if (!(hfile = start_writing_to_stdio_file(hfname,
|
2012-06-18 19:01:15 +02:00
|
|
|
OPEN_FLAGS_REPLACE | O_TEXT,
|
|
|
|
0600, &open_hfile))) {
|
2012-06-18 19:05:28 +02:00
|
|
|
log_warn(LD_CONFIG, "Could not open hostname file %s", escaped(hfname));
|
2012-06-18 19:01:15 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Either use loaded keys for configured clients or generate new
|
|
|
|
* ones if a client is new. */
|
|
|
|
SMARTLIST_FOREACH_BEGIN(s->clients, rend_authorized_client_t *, client) {
|
|
|
|
rend_authorized_client_t *parsed =
|
|
|
|
strmap_get(parsed_clients, client->client_name);
|
|
|
|
int written;
|
|
|
|
size_t len;
|
|
|
|
/* Copy descriptor cookie from parsed entry or create new one. */
|
|
|
|
if (parsed) {
|
|
|
|
memcpy(client->descriptor_cookie, parsed->descriptor_cookie,
|
|
|
|
REND_DESC_COOKIE_LEN);
|
|
|
|
} else {
|
2016-05-09 19:43:14 +02:00
|
|
|
crypto_rand((char *) client->descriptor_cookie, REND_DESC_COOKIE_LEN);
|
2004-03-31 23:54:56 +02:00
|
|
|
}
|
2015-04-14 05:35:06 +02:00
|
|
|
/* For compatibility with older tor clients, this does not
|
|
|
|
* truncate the padding characters, unlike rend_auth_encode_cookie. */
|
2012-06-18 19:01:15 +02:00
|
|
|
if (base64_encode(desc_cook_out, 3*REND_DESC_COOKIE_LEN_BASE64+1,
|
2016-05-09 19:43:14 +02:00
|
|
|
(char *) client->descriptor_cookie,
|
2015-04-10 13:25:08 +02:00
|
|
|
REND_DESC_COOKIE_LEN, 0) < 0) {
|
2012-06-18 19:01:15 +02:00
|
|
|
log_warn(LD_BUG, "Could not base64-encode descriptor cookie.");
|
2012-06-18 19:05:28 +02:00
|
|
|
goto err;
|
2004-04-01 05:34:05 +02:00
|
|
|
}
|
2012-06-18 19:01:15 +02:00
|
|
|
/* Copy client key from parsed entry or create new one if required. */
|
|
|
|
if (parsed && parsed->client_key) {
|
|
|
|
client->client_key = crypto_pk_dup_key(parsed->client_key);
|
|
|
|
} else if (s->auth_type == REND_STEALTH_AUTH) {
|
|
|
|
/* Create private key for client. */
|
|
|
|
crypto_pk_t *prkey = NULL;
|
|
|
|
if (!(prkey = crypto_pk_new())) {
|
|
|
|
log_warn(LD_BUG,"Error constructing client key");
|
|
|
|
goto err;
|
2008-08-08 16:36:11 +02:00
|
|
|
}
|
2012-06-18 19:01:15 +02:00
|
|
|
if (crypto_pk_generate_key(prkey)) {
|
|
|
|
log_warn(LD_BUG,"Error generating client key");
|
|
|
|
crypto_pk_free(prkey);
|
|
|
|
goto err;
|
2012-06-18 18:45:55 +02:00
|
|
|
}
|
2012-06-18 19:01:15 +02:00
|
|
|
if (crypto_pk_check_key(prkey) <= 0) {
|
|
|
|
log_warn(LD_BUG,"Generated client key seems invalid");
|
|
|
|
crypto_pk_free(prkey);
|
|
|
|
goto err;
|
2008-08-08 16:36:11 +02:00
|
|
|
}
|
2012-06-18 19:01:15 +02:00
|
|
|
client->client_key = prkey;
|
2004-03-31 23:54:56 +02:00
|
|
|
}
|
2012-06-18 19:01:15 +02:00
|
|
|
/* Add entry to client_keys file. */
|
|
|
|
written = tor_snprintf(buf, sizeof(buf),
|
|
|
|
"client-name %s\ndescriptor-cookie %s\n",
|
|
|
|
client->client_name, desc_cook_out);
|
|
|
|
if (written < 0) {
|
|
|
|
log_warn(LD_BUG, "Could not write client entry.");
|
|
|
|
goto err;
|
2008-08-08 16:36:11 +02:00
|
|
|
}
|
2012-06-18 19:01:15 +02:00
|
|
|
if (client->client_key) {
|
|
|
|
char *client_key_out = NULL;
|
|
|
|
if (crypto_pk_write_private_key_to_string(client->client_key,
|
|
|
|
&client_key_out, &len) != 0) {
|
|
|
|
log_warn(LD_BUG, "Internal error: "
|
|
|
|
"crypto_pk_write_private_key_to_string() failed.");
|
2008-08-08 16:36:11 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2012-06-18 19:01:15 +02:00
|
|
|
if (rend_get_service_id(client->client_key, service_id)<0) {
|
|
|
|
log_warn(LD_BUG, "Internal error: couldn't encode service ID.");
|
|
|
|
/*
|
|
|
|
* len is string length, not buffer length, but last byte is NUL
|
|
|
|
* anyway.
|
|
|
|
*/
|
2012-11-08 23:00:36 +01:00
|
|
|
memwipe(client_key_out, 0, len);
|
2012-06-18 18:45:55 +02:00
|
|
|
tor_free(client_key_out);
|
2008-08-08 16:36:11 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2012-06-18 19:01:15 +02:00
|
|
|
written = tor_snprintf(buf + written, sizeof(buf) - written,
|
|
|
|
"client-key\n%s", client_key_out);
|
2012-11-08 23:00:36 +01:00
|
|
|
memwipe(client_key_out, 0, len);
|
2012-06-18 19:01:15 +02:00
|
|
|
tor_free(client_key_out);
|
|
|
|
if (written < 0) {
|
|
|
|
log_warn(LD_BUG, "Could not write client entry.");
|
2008-08-08 16:36:11 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2015-04-14 05:35:06 +02:00
|
|
|
} else {
|
|
|
|
strlcpy(service_id, s->service_id, sizeof(service_id));
|
2012-06-18 19:01:15 +02:00
|
|
|
}
|
2008-08-08 16:36:11 +02:00
|
|
|
|
2012-06-18 19:01:15 +02:00
|
|
|
if (fputs(buf, cfile) < 0) {
|
|
|
|
log_warn(LD_FS, "Could not append client entry to file: %s",
|
|
|
|
strerror(errno));
|
|
|
|
goto err;
|
|
|
|
}
|
2008-08-08 16:36:11 +02:00
|
|
|
|
2015-04-14 05:35:06 +02:00
|
|
|
/* Add line to hostname file. This is not the same encoding as in
|
|
|
|
* client_keys. */
|
|
|
|
char *encoded_cookie = rend_auth_encode_cookie(client->descriptor_cookie,
|
|
|
|
s->auth_type);
|
|
|
|
if (!encoded_cookie) {
|
|
|
|
log_warn(LD_BUG, "Could not base64-encode descriptor cookie.");
|
|
|
|
goto err;
|
2008-08-08 16:36:11 +02:00
|
|
|
}
|
2015-04-14 05:35:06 +02:00
|
|
|
tor_snprintf(buf, sizeof(buf), "%s.onion %s # client: %s\n",
|
|
|
|
service_id, encoded_cookie, client->client_name);
|
|
|
|
memwipe(encoded_cookie, 0, strlen(encoded_cookie));
|
|
|
|
tor_free(encoded_cookie);
|
2008-08-08 16:36:11 +02:00
|
|
|
|
2012-06-18 19:01:15 +02:00
|
|
|
if (fputs(buf, hfile)<0) {
|
|
|
|
log_warn(LD_FS, "Could not append host entry to file: %s",
|
|
|
|
strerror(errno));
|
|
|
|
goto err;
|
2008-08-08 16:36:11 +02:00
|
|
|
}
|
2012-06-18 19:05:28 +02:00
|
|
|
} SMARTLIST_FOREACH_END(client);
|
|
|
|
|
|
|
|
finish_writing_to_file(open_cfile);
|
|
|
|
finish_writing_to_file(open_hfile);
|
2012-06-18 19:01:15 +02:00
|
|
|
|
|
|
|
goto done;
|
|
|
|
err:
|
|
|
|
r = -1;
|
2012-06-18 19:05:28 +02:00
|
|
|
if (open_cfile)
|
|
|
|
abort_writing_to_file(open_cfile);
|
|
|
|
if (open_hfile)
|
|
|
|
abort_writing_to_file(open_hfile);
|
2012-06-18 19:01:15 +02:00
|
|
|
done:
|
2012-06-29 06:32:27 +02:00
|
|
|
if (client_keys_str) {
|
2015-02-17 19:53:33 +01:00
|
|
|
memwipe(client_keys_str, 0, strlen(client_keys_str));
|
2012-06-29 06:32:27 +02:00
|
|
|
tor_free(client_keys_str);
|
|
|
|
}
|
2012-06-18 19:01:15 +02:00
|
|
|
strmap_free(parsed_clients, rend_authorized_client_strmap_item_free);
|
2012-06-16 04:54:54 +02:00
|
|
|
|
2016-09-07 05:29:07 +02:00
|
|
|
if (cfname) {
|
2016-09-14 16:58:00 +02:00
|
|
|
memwipe(cfname, 0, strlen(cfname));
|
2016-09-07 05:29:07 +02:00
|
|
|
tor_free(cfname);
|
|
|
|
}
|
2012-06-18 19:05:28 +02:00
|
|
|
|
|
|
|
/* Clear stack buffers that held key-derived material. */
|
2012-11-08 23:00:36 +01:00
|
|
|
memwipe(buf, 0, sizeof(buf));
|
|
|
|
memwipe(desc_cook_out, 0, sizeof(desc_cook_out));
|
|
|
|
memwipe(service_id, 0, sizeof(service_id));
|
2012-06-16 04:54:54 +02:00
|
|
|
|
2008-08-08 16:36:11 +02:00
|
|
|
return r;
|
2004-03-31 23:54:56 +02:00
|
|
|
}
|
2004-04-01 05:23:28 +02:00
|
|
|
|
2009-05-03 01:04:25 +02:00
|
|
|
/** Return the service whose public key has a digest of <b>digest</b>, or
|
|
|
|
* NULL if no such service exists.
|
2004-04-06 05:44:36 +02:00
|
|
|
*/
|
2004-04-02 23:56:52 +02:00
|
|
|
static rend_service_t *
|
2009-05-03 01:04:25 +02:00
|
|
|
rend_service_get_by_pk_digest(const char* digest)
|
2004-04-01 05:34:05 +02:00
|
|
|
{
|
2004-04-03 02:58:54 +02:00
|
|
|
SMARTLIST_FOREACH(rend_service_list, rend_service_t*, s,
|
2011-05-11 22:23:42 +02:00
|
|
|
if (tor_memeq(s->pk_digest,digest,DIGEST_LEN))
|
2009-05-02 23:31:58 +02:00
|
|
|
return s);
|
2004-04-01 05:34:05 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-04-25 10:23:15 +02:00
|
|
|
/** Return the service whose service id is <b>id</b>, or NULL if no such
|
|
|
|
* service exists.
|
|
|
|
*/
|
|
|
|
static struct rend_service_t *
|
|
|
|
rend_service_get_by_service_id(const char *id)
|
|
|
|
{
|
|
|
|
tor_assert(strlen(id) == REND_SERVICE_ID_LEN_BASE32);
|
|
|
|
SMARTLIST_FOREACH(rend_service_list, rend_service_t*, s, {
|
|
|
|
if (tor_memeq(s->service_id, id, REND_SERVICE_ID_LEN_BASE32))
|
|
|
|
return s;
|
|
|
|
});
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-01-14 05:35:53 +01:00
|
|
|
/** Return 1 if any virtual port in <b>service</b> wants a circuit
|
|
|
|
* to have good uptime. Else return 0.
|
|
|
|
*/
|
|
|
|
static int
|
2005-09-30 03:09:52 +02:00
|
|
|
rend_service_requires_uptime(rend_service_t *service)
|
|
|
|
{
|
2005-01-14 05:35:53 +01:00
|
|
|
int i;
|
|
|
|
rend_service_port_config_t *p;
|
|
|
|
|
|
|
|
for (i=0; i < smartlist_len(service->ports); ++i) {
|
|
|
|
p = smartlist_get(service->ports, i);
|
2012-04-11 18:50:50 +02:00
|
|
|
if (smartlist_contains_int_as_string(get_options()->LongLivedPorts,
|
2005-12-14 21:40:40 +01:00
|
|
|
p->virtual_port))
|
2005-01-14 05:35:53 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-26 06:52:35 +02:00
|
|
|
/** Check client authorization of a given <b>descriptor_cookie</b> of
|
|
|
|
* length <b>cookie_len</b> for <b>service</b>. Return 1 for success
|
|
|
|
* and 0 for failure. */
|
2008-09-24 16:44:29 +02:00
|
|
|
static int
|
|
|
|
rend_check_authorization(rend_service_t *service,
|
2015-04-26 06:52:35 +02:00
|
|
|
const char *descriptor_cookie,
|
|
|
|
size_t cookie_len)
|
2008-09-24 16:44:29 +02:00
|
|
|
{
|
|
|
|
rend_authorized_client_t *auth_client = NULL;
|
|
|
|
tor_assert(service);
|
|
|
|
tor_assert(descriptor_cookie);
|
|
|
|
if (!service->clients) {
|
|
|
|
log_warn(LD_BUG, "Can't check authorization for a service that has no "
|
|
|
|
"authorized clients configured.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-26 06:52:35 +02:00
|
|
|
if (cookie_len != REND_DESC_COOKIE_LEN) {
|
|
|
|
log_info(LD_REND, "Descriptor cookie is %lu bytes, but we expected "
|
|
|
|
"%lu bytes. Dropping cell.",
|
|
|
|
(unsigned long)cookie_len, (unsigned long)REND_DESC_COOKIE_LEN);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-24 16:44:29 +02:00
|
|
|
/* Look up client authorization by descriptor cookie. */
|
|
|
|
SMARTLIST_FOREACH(service->clients, rend_authorized_client_t *, client, {
|
2011-05-10 22:23:43 +02:00
|
|
|
if (tor_memeq(client->descriptor_cookie, descriptor_cookie,
|
2008-09-24 16:44:29 +02:00
|
|
|
REND_DESC_COOKIE_LEN)) {
|
|
|
|
auth_client = client;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!auth_client) {
|
|
|
|
char descriptor_cookie_base64[3*REND_DESC_COOKIE_LEN_BASE64];
|
|
|
|
base64_encode(descriptor_cookie_base64, sizeof(descriptor_cookie_base64),
|
2015-04-10 13:25:08 +02:00
|
|
|
descriptor_cookie, REND_DESC_COOKIE_LEN, 0);
|
2008-09-24 16:44:29 +02:00
|
|
|
log_info(LD_REND, "No authorization found for descriptor cookie '%s'! "
|
|
|
|
"Dropping cell!",
|
|
|
|
descriptor_cookie_base64);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allow the request. */
|
2015-01-28 15:42:28 +01:00
|
|
|
log_info(LD_REND, "Client %s authorized for service %s.",
|
|
|
|
auth_client->client_name, service->service_id);
|
2008-09-24 16:44:29 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-07-14 06:04:02 +02:00
|
|
|
/* Can this service make a direct connection to ei?
|
|
|
|
* It must be a single onion service, and the firewall rules must allow ei. */
|
|
|
|
static int
|
|
|
|
rend_service_use_direct_connection(const or_options_t* options,
|
|
|
|
const extend_info_t* ei)
|
|
|
|
{
|
2016-08-18 05:19:22 +02:00
|
|
|
/* We'll connect directly all reachable addresses, whether preferred or not.
|
|
|
|
* The prefer_ipv6 argument to fascist_firewall_allows_address_addr is
|
2016-07-14 06:04:02 +02:00
|
|
|
* ignored, because pref_only is 0. */
|
2016-08-18 05:19:22 +02:00
|
|
|
return (rend_service_allow_non_anonymous_connection(options) &&
|
2016-07-14 06:04:02 +02:00
|
|
|
fascist_firewall_allows_address_addr(&ei->addr, ei->port,
|
|
|
|
FIREWALL_OR_CONNECTION, 0, 0));
|
|
|
|
}
|
|
|
|
|
2016-08-18 05:19:22 +02:00
|
|
|
/* Like rend_service_use_direct_connection, but to a node. */
|
|
|
|
static int
|
|
|
|
rend_service_use_direct_connection_node(const or_options_t* options,
|
|
|
|
const node_t* node)
|
|
|
|
{
|
|
|
|
/* We'll connect directly all reachable addresses, whether preferred or not.
|
|
|
|
*/
|
|
|
|
return (rend_service_allow_non_anonymous_connection(options) &&
|
|
|
|
fascist_firewall_allows_node(node, FIREWALL_OR_CONNECTION, 0));
|
|
|
|
}
|
|
|
|
|
2004-04-01 05:34:05 +02:00
|
|
|
/******
|
|
|
|
* Handle cells
|
|
|
|
******/
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Respond to an INTRODUCE2 cell by launching a circuit to the chosen
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
* rendezvous point.
|
2004-04-02 00:21:01 +02:00
|
|
|
*/
|
2004-04-01 05:34:05 +02:00
|
|
|
int
|
2015-07-14 08:22:23 +02:00
|
|
|
rend_service_receive_introduction(origin_circuit_t *circuit,
|
|
|
|
const uint8_t *request,
|
|
|
|
size_t request_len)
|
2004-04-01 05:34:05 +02:00
|
|
|
{
|
2012-07-15 11:36:36 +02:00
|
|
|
/* Global status stuff */
|
|
|
|
int status = 0, result;
|
|
|
|
const or_options_t *options = get_options();
|
|
|
|
char *err_msg = NULL;
|
2016-04-27 02:46:44 +02:00
|
|
|
int err_msg_severity = LOG_WARN;
|
2016-05-31 20:51:30 +02:00
|
|
|
const char *stage_descr = NULL, *rend_pk_digest;
|
2012-07-15 11:36:36 +02:00
|
|
|
int reason = END_CIRC_REASON_TORPROTOCOL;
|
|
|
|
/* Service/circuit/key stuff we can learn before parsing */
|
|
|
|
char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
|
|
|
|
rend_service_t *service = NULL;
|
|
|
|
rend_intro_point_t *intro_point = NULL;
|
|
|
|
crypto_pk_t *intro_key = NULL;
|
|
|
|
/* Parsed cell */
|
|
|
|
rend_intro_cell_t *parsed_req = NULL;
|
|
|
|
/* Rendezvous point */
|
|
|
|
extend_info_t *rp = NULL;
|
|
|
|
/* XXX not handled yet */
|
2004-04-01 22:05:57 +02:00
|
|
|
char buf[RELAY_PAYLOAD_SIZE];
|
2004-04-05 23:15:14 +02:00
|
|
|
char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN]; /* Holds KH, Df, Db, Kf, Kb */
|
2012-07-15 11:36:36 +02:00
|
|
|
int i;
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_dh_t *dh = NULL;
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t *launched = NULL;
|
2004-04-02 00:21:01 +02:00
|
|
|
crypt_path_t *cpath = NULL;
|
2004-04-03 02:58:54 +02:00
|
|
|
char hexcookie[9];
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
int circ_needs_uptime;
|
2008-09-24 16:44:29 +02:00
|
|
|
time_t now = time(NULL);
|
2012-07-11 04:18:47 +02:00
|
|
|
time_t elapsed;
|
|
|
|
int replay;
|
2009-10-18 00:52:18 +02:00
|
|
|
|
2012-07-15 11:36:36 +02:00
|
|
|
/* Do some initial validation and logging before we parse the cell */
|
2012-10-12 18:22:13 +02:00
|
|
|
if (circuit->base_.purpose != CIRCUIT_PURPOSE_S_INTRO) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_PROTOCOL,
|
2013-03-10 13:32:58 +01:00
|
|
|
"Got an INTRODUCE2 over a non-introduction circuit %u.",
|
|
|
|
(unsigned) circuit->base_.n_circ_id);
|
2012-06-16 05:19:02 +02:00
|
|
|
goto err;
|
2004-04-01 05:34:05 +02:00
|
|
|
}
|
|
|
|
|
2016-07-14 06:04:02 +02:00
|
|
|
assert_circ_anonymity_ok(circuit, options);
|
2008-09-24 16:44:29 +02:00
|
|
|
tor_assert(circuit->rend_data);
|
2016-05-31 20:51:30 +02:00
|
|
|
/* XXX: This is version 2 specific (only one supported). */
|
|
|
|
rend_pk_digest = (char *) rend_data_get_pk_digest(circuit->rend_data, NULL);
|
2004-04-03 02:58:54 +02:00
|
|
|
|
2012-07-15 11:36:36 +02:00
|
|
|
/* We'll use this in a bazillion log messages */
|
2007-11-29 16:25:04 +01:00
|
|
|
base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
|
2016-05-31 20:51:30 +02:00
|
|
|
rend_pk_digest, REND_SERVICE_ID_LEN);
|
2004-04-01 05:34:05 +02:00
|
|
|
|
2007-10-31 21:48:06 +01:00
|
|
|
/* look up service depending on circuit. */
|
2016-05-31 20:51:30 +02:00
|
|
|
service = rend_service_get_by_pk_digest(rend_pk_digest);
|
2004-04-01 05:34:05 +02:00
|
|
|
if (!service) {
|
2012-07-15 11:36:36 +02:00
|
|
|
log_warn(LD_BUG,
|
|
|
|
"Internal error: Got an INTRODUCE2 cell on an intro "
|
2011-10-06 00:55:23 +02:00
|
|
|
"circ for an unrecognized service %s.",
|
2006-03-06 01:25:39 +01:00
|
|
|
escaped(serviceid));
|
2012-06-16 05:19:02 +02:00
|
|
|
goto err;
|
2004-04-01 05:34:05 +02:00
|
|
|
}
|
2007-10-31 21:48:06 +01:00
|
|
|
|
2012-07-15 11:36:36 +02:00
|
|
|
intro_point = find_intro_point(circuit);
|
|
|
|
if (intro_point == NULL) {
|
2015-06-15 23:11:57 +02:00
|
|
|
intro_point = find_expiring_intro_point(service, circuit);
|
|
|
|
if (intro_point == NULL) {
|
|
|
|
log_warn(LD_BUG,
|
|
|
|
"Internal error: Got an INTRODUCE2 cell on an "
|
|
|
|
"intro circ (for service %s) with no corresponding "
|
|
|
|
"rend_intro_point_t.",
|
|
|
|
escaped(serviceid));
|
|
|
|
goto err;
|
|
|
|
}
|
2004-04-01 05:34:05 +02:00
|
|
|
}
|
|
|
|
|
2013-03-10 13:32:58 +01:00
|
|
|
log_info(LD_REND, "Received INTRODUCE2 cell for service %s on circ %u.",
|
|
|
|
escaped(serviceid), (unsigned)circuit->base_.n_circ_id);
|
2011-05-11 03:40:10 +02:00
|
|
|
|
2012-07-15 11:36:36 +02:00
|
|
|
/* use intro key instead of service key. */
|
|
|
|
intro_key = circuit->intro_key;
|
|
|
|
|
|
|
|
tor_free(err_msg);
|
|
|
|
stage_descr = NULL;
|
|
|
|
|
|
|
|
stage_descr = "early parsing";
|
|
|
|
/* Early parsing pass (get pk, ciphertext); type 2 is INTRODUCE2 */
|
|
|
|
parsed_req =
|
|
|
|
rend_service_begin_parse_intro(request, request_len, 2, &err_msg);
|
2012-08-21 16:15:52 +02:00
|
|
|
if (!parsed_req) {
|
|
|
|
goto log_error;
|
|
|
|
} else if (err_msg) {
|
2013-03-10 13:32:58 +01:00
|
|
|
log_info(LD_REND, "%s on circ %u.", err_msg,
|
|
|
|
(unsigned)circuit->base_.n_circ_id);
|
2012-07-15 11:36:36 +02:00
|
|
|
tor_free(err_msg);
|
|
|
|
}
|
2012-07-31 20:32:20 +02:00
|
|
|
|
2012-07-15 11:36:36 +02:00
|
|
|
/* make sure service replay caches are present */
|
2012-07-11 04:18:47 +02:00
|
|
|
if (!service->accepted_intro_dh_parts) {
|
|
|
|
service->accepted_intro_dh_parts =
|
|
|
|
replaycache_new(REND_REPLAY_TIME_INTERVAL,
|
|
|
|
REND_REPLAY_TIME_INTERVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!intro_point->accepted_intro_rsa_parts) {
|
|
|
|
intro_point->accepted_intro_rsa_parts = replaycache_new(0, 0);
|
|
|
|
}
|
2011-05-11 03:40:10 +02:00
|
|
|
|
2012-07-15 11:36:36 +02:00
|
|
|
/* check for replay of PK-encrypted portion. */
|
2012-07-11 04:18:47 +02:00
|
|
|
replay = replaycache_add_test_and_elapsed(
|
2012-07-15 11:36:36 +02:00
|
|
|
intro_point->accepted_intro_rsa_parts,
|
2013-08-04 07:36:32 +02:00
|
|
|
parsed_req->ciphertext, parsed_req->ciphertext_len,
|
2012-07-15 11:36:36 +02:00
|
|
|
&elapsed);
|
2011-05-11 03:40:10 +02:00
|
|
|
|
2012-07-11 04:18:47 +02:00
|
|
|
if (replay) {
|
2012-07-15 11:36:36 +02:00
|
|
|
log_warn(LD_REND,
|
|
|
|
"Possible replay detected! We received an "
|
|
|
|
"INTRODUCE2 cell with same PK-encrypted part %d "
|
|
|
|
"seconds ago. Dropping cell.",
|
|
|
|
(int)elapsed);
|
2012-08-21 16:15:52 +02:00
|
|
|
goto err;
|
2011-05-11 03:40:10 +02:00
|
|
|
}
|
2012-07-31 20:32:20 +02:00
|
|
|
|
2012-07-15 11:36:36 +02:00
|
|
|
stage_descr = "decryption";
|
|
|
|
/* Now try to decrypt it */
|
|
|
|
result = rend_service_decrypt_intro(parsed_req, intro_key, &err_msg);
|
2012-08-21 16:15:52 +02:00
|
|
|
if (result < 0) {
|
|
|
|
goto log_error;
|
|
|
|
} else if (err_msg) {
|
2013-03-10 13:32:58 +01:00
|
|
|
log_info(LD_REND, "%s on circ %u.", err_msg,
|
|
|
|
(unsigned)circuit->base_.n_circ_id);
|
2012-07-15 11:36:36 +02:00
|
|
|
tor_free(err_msg);
|
2004-04-01 05:34:05 +02:00
|
|
|
}
|
2005-06-29 23:46:55 +02:00
|
|
|
|
2012-07-15 11:36:36 +02:00
|
|
|
stage_descr = "late parsing";
|
|
|
|
/* Parse the plaintext */
|
|
|
|
result = rend_service_parse_intro_plaintext(parsed_req, &err_msg);
|
2012-08-21 16:15:52 +02:00
|
|
|
if (result < 0) {
|
|
|
|
goto log_error;
|
|
|
|
} else if (err_msg) {
|
2013-03-10 13:32:58 +01:00
|
|
|
log_info(LD_REND, "%s on circ %u.", err_msg,
|
|
|
|
(unsigned)circuit->base_.n_circ_id);
|
2012-07-15 11:36:36 +02:00
|
|
|
tor_free(err_msg);
|
2004-04-01 05:34:05 +02:00
|
|
|
}
|
2005-06-29 23:46:55 +02:00
|
|
|
|
2012-07-15 11:36:36 +02:00
|
|
|
stage_descr = "late validation";
|
|
|
|
/* Validate the parsed plaintext parts */
|
|
|
|
result = rend_service_validate_intro_late(parsed_req, &err_msg);
|
2012-08-21 16:15:52 +02:00
|
|
|
if (result < 0) {
|
|
|
|
goto log_error;
|
|
|
|
} else if (err_msg) {
|
2013-03-10 13:32:58 +01:00
|
|
|
log_info(LD_REND, "%s on circ %u.", err_msg,
|
|
|
|
(unsigned)circuit->base_.n_circ_id);
|
2012-07-15 11:36:36 +02:00
|
|
|
tor_free(err_msg);
|
2004-04-01 05:34:05 +02:00
|
|
|
}
|
2012-07-15 11:36:36 +02:00
|
|
|
stage_descr = NULL;
|
|
|
|
|
|
|
|
/* Increment INTRODUCE2 counter */
|
|
|
|
++(intro_point->accepted_introduce2_count);
|
|
|
|
|
|
|
|
/* Find the rendezvous point */
|
2015-09-01 15:30:48 +02:00
|
|
|
rp = find_rp_for_intro(parsed_req, &err_msg);
|
2016-04-27 02:46:44 +02:00
|
|
|
if (!rp) {
|
|
|
|
err_msg_severity = LOG_PROTOCOL_WARN;
|
2012-08-21 16:15:52 +02:00
|
|
|
goto log_error;
|
2016-04-27 02:46:44 +02:00
|
|
|
}
|
2005-06-29 23:46:55 +02:00
|
|
|
|
2009-10-18 00:52:18 +02:00
|
|
|
/* Check if we'd refuse to talk to this router */
|
2011-07-07 17:52:13 +02:00
|
|
|
if (options->StrictNodes &&
|
2012-07-15 11:36:36 +02:00
|
|
|
routerset_contains_extendinfo(options->ExcludeNodes, rp)) {
|
2009-10-18 00:52:18 +02:00
|
|
|
log_warn(LD_REND, "Client asked to rendezvous at a relay that we "
|
|
|
|
"exclude, and StrictNodes is set. Refusing service.");
|
|
|
|
reason = END_CIRC_REASON_INTERNAL; /* XXX might leak why we refused */
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2012-07-15 11:36:36 +02:00
|
|
|
base16_encode(hexcookie, 9, (const char *)(parsed_req->rc), 4);
|
2004-04-01 05:34:05 +02:00
|
|
|
|
2008-09-24 19:33:07 +02:00
|
|
|
/* Check whether there is a past request with the same Diffie-Hellman,
|
|
|
|
* part 1. */
|
2012-07-11 04:18:47 +02:00
|
|
|
replay = replaycache_add_test_and_elapsed(
|
|
|
|
service->accepted_intro_dh_parts,
|
2012-07-15 11:36:36 +02:00
|
|
|
parsed_req->dh, DH_KEY_LEN,
|
2012-07-11 04:18:47 +02:00
|
|
|
&elapsed);
|
|
|
|
|
|
|
|
if (replay) {
|
2011-09-11 02:54:26 +02:00
|
|
|
/* A Tor client will send a new INTRODUCE1 cell with the same rend
|
|
|
|
* cookie and DH public key as its previous one if its intro circ
|
|
|
|
* times out while in state CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT .
|
|
|
|
* If we received the first INTRODUCE1 cell (the intro-point relay
|
|
|
|
* converts it into an INTRODUCE2 cell), we are already trying to
|
|
|
|
* connect to that rend point (and may have already succeeded);
|
|
|
|
* drop this cell. */
|
|
|
|
log_info(LD_REND, "We received an "
|
2008-09-24 16:44:29 +02:00
|
|
|
"INTRODUCE2 cell with same first part of "
|
|
|
|
"Diffie-Hellman handshake %d seconds ago. Dropping "
|
|
|
|
"cell.",
|
2012-07-11 04:18:47 +02:00
|
|
|
(int) elapsed);
|
2008-09-24 16:44:29 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the service performs client authorization, check included auth data. */
|
|
|
|
if (service->clients) {
|
2013-02-11 21:42:57 +01:00
|
|
|
if (parsed_req->version == 3 && parsed_req->u.v3.auth_len > 0) {
|
|
|
|
if (rend_check_authorization(service,
|
2015-04-26 06:52:35 +02:00
|
|
|
(const char*)parsed_req->u.v3.auth_data,
|
|
|
|
parsed_req->u.v3.auth_len)) {
|
2008-09-24 16:44:29 +02:00
|
|
|
log_info(LD_REND, "Authorization data in INTRODUCE2 cell are valid.");
|
|
|
|
} else {
|
|
|
|
log_info(LD_REND, "The authorization data that are contained in "
|
|
|
|
"the INTRODUCE2 cell are invalid. Dropping cell.");
|
|
|
|
reason = END_CIRC_REASON_CONNECTFAILED;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log_info(LD_REND, "INTRODUCE2 cell does not contain authentication "
|
|
|
|
"data, but we require client authorization. Dropping cell.");
|
|
|
|
reason = END_CIRC_REASON_CONNECTFAILED;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-01 22:05:57 +02:00
|
|
|
/* Try DH handshake... */
|
2011-01-24 22:03:14 +01:00
|
|
|
dh = crypto_dh_new(DH_TYPE_REND);
|
2004-04-01 22:05:57 +02:00
|
|
|
if (!dh || crypto_dh_generate_public(dh)<0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG,"Internal error: couldn't build DH state "
|
|
|
|
"or generate public key.");
|
2006-10-09 17:47:50 +02:00
|
|
|
reason = END_CIRC_REASON_INTERNAL;
|
2004-04-01 22:05:57 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2012-07-15 11:36:36 +02:00
|
|
|
if (crypto_dh_compute_secret(LOG_PROTOCOL_WARN, dh,
|
|
|
|
(char *)(parsed_req->dh),
|
2009-10-26 07:47:05 +01:00
|
|
|
DH_KEY_LEN, keys,
|
2004-04-05 23:15:14 +02:00
|
|
|
DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG, "Internal error: couldn't complete DH handshake");
|
2006-10-09 17:47:50 +02:00
|
|
|
reason = END_CIRC_REASON_INTERNAL;
|
2004-04-01 22:05:57 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
circ_needs_uptime = rend_service_requires_uptime(service);
|
|
|
|
|
|
|
|
/* help predict this next time */
|
2009-09-03 10:17:08 +02:00
|
|
|
rep_hist_note_used_internal(now, circ_needs_uptime, 1);
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
|
2016-01-26 03:48:31 +01:00
|
|
|
/* Launch a circuit to the client's chosen rendezvous point.
|
2004-04-01 05:34:05 +02:00
|
|
|
*/
|
2004-11-28 10:05:49 +01:00
|
|
|
for (i=0;i<MAX_REND_FAILURES;i++) {
|
2007-12-23 20:15:22 +01:00
|
|
|
int flags = CIRCLAUNCH_NEED_CAPACITY | CIRCLAUNCH_IS_INTERNAL;
|
|
|
|
if (circ_needs_uptime) flags |= CIRCLAUNCH_NEED_UPTIME;
|
2016-07-14 06:04:02 +02:00
|
|
|
/* A Single Onion Service only uses a direct connection if its
|
|
|
|
* firewall rules permit direct connections to the address. */
|
|
|
|
if (rend_service_use_direct_connection(options, rp)) {
|
|
|
|
flags = flags | CIRCLAUNCH_ONEHOP_TUNNEL;
|
|
|
|
}
|
2005-06-29 23:46:55 +02:00
|
|
|
launched = circuit_launch_by_extend_info(
|
2012-07-15 11:36:36 +02:00
|
|
|
CIRCUIT_PURPOSE_S_CONNECT_REND, rp, flags);
|
2005-06-29 23:46:55 +02:00
|
|
|
|
2004-10-30 07:04:52 +02:00
|
|
|
if (launched)
|
|
|
|
break;
|
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!launched) { /* give up */
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_REND, "Giving up launching first hop of circuit to rendezvous "
|
2006-12-07 21:14:29 +01:00
|
|
|
"point %s for service %s.",
|
2012-07-15 11:36:36 +02:00
|
|
|
safe_str_client(extend_info_describe(rp)),
|
2009-09-28 15:08:32 +02:00
|
|
|
serviceid);
|
2006-10-09 17:47:50 +02:00
|
|
|
reason = END_CIRC_REASON_CONNECTFAILED;
|
2004-10-25 01:09:48 +02:00
|
|
|
goto err;
|
2004-04-01 22:05:57 +02:00
|
|
|
}
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_REND,
|
2006-12-07 21:14:29 +01:00
|
|
|
"Accepted intro; launching circuit to %s "
|
2006-02-13 11:33:00 +01:00
|
|
|
"(cookie %s) for service %s.",
|
2012-07-15 11:36:36 +02:00
|
|
|
safe_str_client(extend_info_describe(rp)),
|
2009-09-28 15:08:32 +02:00
|
|
|
hexcookie, serviceid);
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(launched->build_state);
|
2004-04-01 22:05:57 +02:00
|
|
|
/* Fill in the circuit's state. */
|
2015-04-27 21:08:31 +02:00
|
|
|
|
|
|
|
launched->rend_data =
|
2016-05-31 20:51:30 +02:00
|
|
|
rend_data_service_create(service->service_id, rend_pk_digest,
|
2015-04-27 21:08:31 +02:00
|
|
|
parsed_req->rc, service->auth_type);
|
2012-01-07 07:44:20 +01:00
|
|
|
|
|
|
|
launched->build_state->service_pending_final_cpath_ref =
|
|
|
|
tor_malloc_zero(sizeof(crypt_path_reference_t));
|
|
|
|
launched->build_state->service_pending_final_cpath_ref->refcount = 1;
|
|
|
|
|
|
|
|
launched->build_state->service_pending_final_cpath_ref->cpath = cpath =
|
2004-04-02 00:21:01 +02:00
|
|
|
tor_malloc_zero(sizeof(crypt_path_t));
|
2005-03-23 07:21:48 +01:00
|
|
|
cpath->magic = CRYPT_PATH_MAGIC;
|
2009-09-03 10:17:08 +02:00
|
|
|
launched->build_state->expiry_time = now + MAX_REND_TIMEOUT;
|
2004-04-02 00:21:01 +02:00
|
|
|
|
2012-12-05 03:27:07 +01:00
|
|
|
cpath->rend_dh_handshake_state = dh;
|
2004-04-01 22:05:57 +02:00
|
|
|
dh = NULL;
|
2004-04-06 05:44:36 +02:00
|
|
|
if (circuit_init_cpath_crypto(cpath,keys+DIGEST_LEN,1)<0)
|
2004-04-02 00:21:01 +02:00
|
|
|
goto err;
|
2012-12-05 03:39:54 +01:00
|
|
|
memcpy(cpath->rend_circ_nonce, keys, DIGEST_LEN);
|
2012-12-10 08:47:04 +01:00
|
|
|
|
2012-06-16 05:19:02 +02:00
|
|
|
goto done;
|
|
|
|
|
2012-08-21 16:15:52 +02:00
|
|
|
log_error:
|
2012-07-15 11:36:36 +02:00
|
|
|
if (!err_msg) {
|
|
|
|
if (stage_descr) {
|
|
|
|
tor_asprintf(&err_msg,
|
|
|
|
"unknown %s error for INTRODUCE2", stage_descr);
|
2012-08-21 16:15:52 +02:00
|
|
|
} else {
|
2012-07-15 11:36:36 +02:00
|
|
|
err_msg = tor_strdup("unknown error for INTRODUCE2");
|
2012-08-21 16:15:52 +02:00
|
|
|
}
|
2012-07-15 11:36:36 +02:00
|
|
|
}
|
2012-08-21 16:15:52 +02:00
|
|
|
|
2016-04-27 02:46:44 +02:00
|
|
|
log_fn(err_msg_severity, LD_REND, "%s on circ %u", err_msg,
|
2013-03-10 13:32:58 +01:00
|
|
|
(unsigned)circuit->base_.n_circ_id);
|
2012-08-21 16:15:52 +02:00
|
|
|
err:
|
|
|
|
status = -1;
|
2004-04-01 22:05:57 +02:00
|
|
|
if (dh) crypto_dh_free(dh);
|
2012-07-15 11:36:36 +02:00
|
|
|
if (launched) {
|
2006-10-09 17:47:50 +02:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(launched), reason);
|
2012-07-15 11:36:36 +02:00
|
|
|
}
|
|
|
|
tor_free(err_msg);
|
|
|
|
|
2012-06-16 05:19:02 +02:00
|
|
|
done:
|
2012-11-08 22:48:04 +01:00
|
|
|
memwipe(keys, 0, sizeof(keys));
|
|
|
|
memwipe(buf, 0, sizeof(buf));
|
|
|
|
memwipe(serviceid, 0, sizeof(serviceid));
|
|
|
|
memwipe(hexcookie, 0, sizeof(hexcookie));
|
2012-06-16 05:19:02 +02:00
|
|
|
|
2012-07-15 11:36:36 +02:00
|
|
|
/* Free the parsed cell */
|
2014-08-21 16:34:29 +02:00
|
|
|
rend_service_free_intro(parsed_req);
|
2012-07-15 11:36:36 +02:00
|
|
|
|
2015-09-01 15:30:48 +02:00
|
|
|
/* Free rp */
|
|
|
|
extend_info_free(rp);
|
2012-07-15 11:36:36 +02:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Given a parsed and decrypted INTRODUCE2, find the rendezvous point or
|
2015-09-01 15:30:48 +02:00
|
|
|
* return NULL and an error string if we can't. Return a newly allocated
|
2015-09-02 14:51:24 +02:00
|
|
|
* extend_info_t* for the rendezvous point. */
|
2012-07-15 11:36:36 +02:00
|
|
|
static extend_info_t *
|
|
|
|
find_rp_for_intro(const rend_intro_cell_t *intro,
|
2015-09-01 15:30:48 +02:00
|
|
|
char **err_msg_out)
|
2012-07-15 11:36:36 +02:00
|
|
|
{
|
|
|
|
extend_info_t *rp = NULL;
|
|
|
|
char *err_msg = NULL;
|
|
|
|
const char *rp_nickname = NULL;
|
|
|
|
const node_t *node = NULL;
|
|
|
|
|
2015-09-01 15:30:48 +02:00
|
|
|
if (!intro) {
|
2012-07-15 11:36:36 +02:00
|
|
|
if (err_msg_out)
|
|
|
|
err_msg = tor_strdup("Bad parameters to find_rp_for_intro()");
|
|
|
|
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (intro->version == 0 || intro->version == 1) {
|
2014-12-30 18:07:28 +01:00
|
|
|
rp_nickname = (const char *)(intro->u.v0_v1.rp);
|
2012-07-15 11:36:36 +02:00
|
|
|
|
|
|
|
node = node_get_by_nickname(rp_nickname, 0);
|
|
|
|
if (!node) {
|
|
|
|
if (err_msg_out) {
|
|
|
|
tor_asprintf(&err_msg,
|
|
|
|
"Couldn't find router %s named in INTRODUCE2 cell",
|
|
|
|
escaped_safe_str_client(rp_nickname));
|
|
|
|
}
|
|
|
|
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-07-14 06:04:02 +02:00
|
|
|
/* Are we in single onion mode? */
|
|
|
|
const int allow_direct = rend_service_allow_non_anonymous_connection(
|
|
|
|
get_options());
|
|
|
|
rp = extend_info_from_node(node, allow_direct);
|
2012-07-15 11:36:36 +02:00
|
|
|
if (!rp) {
|
|
|
|
if (err_msg_out) {
|
|
|
|
tor_asprintf(&err_msg,
|
2016-04-27 02:46:44 +02:00
|
|
|
"Couldn't build extend_info_t for router %s named "
|
2012-07-15 11:36:36 +02:00
|
|
|
"in INTRODUCE2 cell",
|
|
|
|
escaped_safe_str_client(rp_nickname));
|
|
|
|
}
|
|
|
|
|
|
|
|
goto err;
|
|
|
|
}
|
2012-07-31 20:32:20 +02:00
|
|
|
} else if (intro->version == 2) {
|
2015-09-01 15:30:48 +02:00
|
|
|
rp = extend_info_dup(intro->u.v2.extend_info);
|
2012-07-31 20:32:20 +02:00
|
|
|
} else if (intro->version == 3) {
|
2015-09-01 15:30:48 +02:00
|
|
|
rp = extend_info_dup(intro->u.v3.extend_info);
|
2012-07-31 20:32:20 +02:00
|
|
|
} else {
|
2012-07-15 11:36:36 +02:00
|
|
|
if (err_msg_out) {
|
|
|
|
tor_asprintf(&err_msg,
|
|
|
|
"Unknown version %d in INTRODUCE2 cell",
|
|
|
|
(int)(intro->version));
|
|
|
|
}
|
|
|
|
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-07-14 06:04:02 +02:00
|
|
|
/* rp is always set here: extend_info_dup guarantees a non-NULL result, and
|
|
|
|
* the other cases goto err. */
|
|
|
|
tor_assert(rp);
|
|
|
|
|
2015-12-08 19:42:27 +01:00
|
|
|
/* Make sure the RP we are being asked to connect to is _not_ a private
|
|
|
|
* address unless it's allowed. Let's avoid to build a circuit to our
|
|
|
|
* second middle node and fail right after when extending to the RP. */
|
|
|
|
if (!extend_info_addr_is_allowed(&rp->addr)) {
|
|
|
|
if (err_msg_out) {
|
|
|
|
tor_asprintf(&err_msg,
|
|
|
|
"Relay IP in INTRODUCE2 cell is private address.");
|
|
|
|
}
|
|
|
|
extend_info_free(rp);
|
|
|
|
rp = NULL;
|
|
|
|
goto err;
|
|
|
|
}
|
2012-07-15 11:36:36 +02:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
err:
|
2016-04-27 02:46:44 +02:00
|
|
|
if (err_msg_out)
|
|
|
|
*err_msg_out = err_msg;
|
|
|
|
else
|
|
|
|
tor_free(err_msg);
|
2012-07-15 11:36:36 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
return rp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Free a parsed INTRODUCE1 or INTRODUCE2 cell that was allocated by
|
|
|
|
* rend_service_parse_intro().
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
rend_service_free_intro(rend_intro_cell_t *request)
|
|
|
|
{
|
|
|
|
if (!request) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free ciphertext */
|
|
|
|
tor_free(request->ciphertext);
|
|
|
|
request->ciphertext_len = 0;
|
|
|
|
|
|
|
|
/* Have plaintext? */
|
|
|
|
if (request->plaintext) {
|
|
|
|
/* Zero it out just to be safe */
|
2012-11-08 23:00:36 +01:00
|
|
|
memwipe(request->plaintext, 0, request->plaintext_len);
|
2012-07-15 11:36:36 +02:00
|
|
|
tor_free(request->plaintext);
|
|
|
|
request->plaintext_len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Have parsed plaintext? */
|
|
|
|
if (request->parsed) {
|
|
|
|
switch (request->version) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
/*
|
|
|
|
* Nothing more to do; these formats have no further pointers
|
|
|
|
* in them.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
extend_info_free(request->u.v2.extend_info);
|
|
|
|
request->u.v2.extend_info = NULL;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
if (request->u.v3.auth_data) {
|
2012-11-08 23:00:36 +01:00
|
|
|
memwipe(request->u.v3.auth_data, 0, request->u.v3.auth_len);
|
2012-07-15 11:36:36 +02:00
|
|
|
tor_free(request->u.v3.auth_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
extend_info_free(request->u.v3.extend_info);
|
|
|
|
request->u.v3.extend_info = NULL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
log_info(LD_BUG,
|
|
|
|
"rend_service_free_intro() saw unknown protocol "
|
|
|
|
"version %d.",
|
|
|
|
request->version);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Zero it out to make sure sensitive stuff doesn't hang around in memory */
|
2012-11-08 23:00:36 +01:00
|
|
|
memwipe(request, 0, sizeof(*request));
|
2012-07-15 11:36:36 +02:00
|
|
|
|
|
|
|
tor_free(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Parse an INTRODUCE1 or INTRODUCE2 cell into a newly allocated
|
|
|
|
* rend_intro_cell_t structure. Free it with rend_service_free_intro()
|
|
|
|
* when finished. The type parameter should be 1 or 2 to indicate whether
|
|
|
|
* this is INTRODUCE1 or INTRODUCE2. This parses only the non-encrypted
|
|
|
|
* parts; after this, call rend_service_decrypt_intro() with a key, then
|
|
|
|
* rend_service_parse_intro_plaintext() to finish parsing. The optional
|
|
|
|
* err_msg_out parameter is set to a string suitable for log output
|
|
|
|
* if parsing fails. This function does some validation, but only
|
|
|
|
* that which depends solely on the contents of the cell and the
|
|
|
|
* key; it can be unit-tested. Further validation is done in
|
|
|
|
* rend_service_validate_intro().
|
|
|
|
*/
|
|
|
|
|
|
|
|
rend_intro_cell_t *
|
|
|
|
rend_service_begin_parse_intro(const uint8_t *request,
|
|
|
|
size_t request_len,
|
|
|
|
uint8_t type,
|
|
|
|
char **err_msg_out)
|
|
|
|
{
|
|
|
|
rend_intro_cell_t *rv = NULL;
|
|
|
|
char *err_msg = NULL;
|
|
|
|
|
|
|
|
if (!request || request_len <= 0) goto err;
|
|
|
|
if (!(type == 1 || type == 2)) goto err;
|
|
|
|
|
|
|
|
/* First, check that the cell is long enough to be a sensible INTRODUCE */
|
|
|
|
|
|
|
|
/* min key length plus digest length plus nickname length */
|
|
|
|
if (request_len <
|
|
|
|
(DIGEST_LEN + REND_COOKIE_LEN + (MAX_NICKNAME_LEN + 1) +
|
|
|
|
DH_KEY_LEN + 42)) {
|
|
|
|
if (err_msg_out) {
|
|
|
|
tor_asprintf(&err_msg,
|
|
|
|
"got a truncated INTRODUCE%d cell",
|
|
|
|
(int)type);
|
|
|
|
}
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a new parsed cell structure */
|
|
|
|
rv = tor_malloc_zero(sizeof(*rv));
|
|
|
|
|
|
|
|
/* Set the type */
|
|
|
|
rv->type = type;
|
|
|
|
|
|
|
|
/* Copy in the ID */
|
|
|
|
memcpy(rv->pk, request, DIGEST_LEN);
|
|
|
|
|
|
|
|
/* Copy in the ciphertext */
|
|
|
|
rv->ciphertext = tor_malloc(request_len - DIGEST_LEN);
|
|
|
|
memcpy(rv->ciphertext, request + DIGEST_LEN, request_len - DIGEST_LEN);
|
|
|
|
rv->ciphertext_len = request_len - DIGEST_LEN;
|
|
|
|
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
err:
|
2014-08-21 16:34:29 +02:00
|
|
|
rend_service_free_intro(rv);
|
2012-07-15 11:36:36 +02:00
|
|
|
rv = NULL;
|
2014-08-21 16:49:01 +02:00
|
|
|
|
2012-07-15 11:36:36 +02:00
|
|
|
if (err_msg_out && !err_msg) {
|
|
|
|
tor_asprintf(&err_msg,
|
|
|
|
"unknown INTRODUCE%d error",
|
|
|
|
(int)type);
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (err_msg_out) *err_msg_out = err_msg;
|
|
|
|
else tor_free(err_msg);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Parse the version-specific parts of a v0 or v1 INTRODUCE1 or INTRODUCE2
|
|
|
|
* cell
|
|
|
|
*/
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
rend_service_parse_intro_for_v0_or_v1(
|
|
|
|
rend_intro_cell_t *intro,
|
|
|
|
const uint8_t *buf,
|
|
|
|
size_t plaintext_len,
|
|
|
|
char **err_msg_out)
|
|
|
|
{
|
|
|
|
const char *rp_nickname, *endptr;
|
|
|
|
size_t nickname_field_len, ver_specific_len;
|
|
|
|
|
|
|
|
if (intro->version == 1) {
|
|
|
|
ver_specific_len = MAX_HEX_NICKNAME_LEN + 2;
|
|
|
|
rp_nickname = ((const char *)buf) + 1;
|
|
|
|
nickname_field_len = MAX_HEX_NICKNAME_LEN + 1;
|
|
|
|
} else if (intro->version == 0) {
|
|
|
|
ver_specific_len = MAX_NICKNAME_LEN + 1;
|
|
|
|
rp_nickname = (const char *)buf;
|
|
|
|
nickname_field_len = MAX_NICKNAME_LEN + 1;
|
|
|
|
} else {
|
|
|
|
if (err_msg_out)
|
|
|
|
tor_asprintf(err_msg_out,
|
|
|
|
"rend_service_parse_intro_for_v0_or_v1() called with "
|
|
|
|
"bad version %d on INTRODUCE%d cell (this is a bug)",
|
|
|
|
intro->version,
|
|
|
|
(int)(intro->type));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (plaintext_len < ver_specific_len) {
|
|
|
|
if (err_msg_out)
|
|
|
|
tor_asprintf(err_msg_out,
|
|
|
|
"short plaintext of encrypted part in v1 INTRODUCE%d "
|
|
|
|
"cell (%lu bytes, needed %lu)",
|
|
|
|
(int)(intro->type),
|
|
|
|
(unsigned long)plaintext_len,
|
|
|
|
(unsigned long)ver_specific_len);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
endptr = memchr(rp_nickname, 0, nickname_field_len);
|
|
|
|
if (!endptr || endptr == rp_nickname) {
|
|
|
|
if (err_msg_out) {
|
|
|
|
tor_asprintf(err_msg_out,
|
|
|
|
"couldn't find a nul-padded nickname in "
|
|
|
|
"INTRODUCE%d cell",
|
|
|
|
(int)(intro->type));
|
|
|
|
}
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((intro->version == 0 &&
|
|
|
|
!is_legal_nickname(rp_nickname)) ||
|
|
|
|
(intro->version == 1 &&
|
|
|
|
!is_legal_nickname_or_hexdigest(rp_nickname))) {
|
|
|
|
if (err_msg_out) {
|
|
|
|
tor_asprintf(err_msg_out,
|
|
|
|
"bad nickname in INTRODUCE%d cell",
|
|
|
|
(int)(intro->type));
|
|
|
|
}
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2014-12-30 18:07:28 +01:00
|
|
|
memcpy(intro->u.v0_v1.rp, rp_nickname, endptr - rp_nickname + 1);
|
2012-07-15 11:36:36 +02:00
|
|
|
|
|
|
|
return ver_specific_len;
|
|
|
|
|
|
|
|
err:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Parse the version-specific parts of a v2 INTRODUCE1 or INTRODUCE2 cell
|
|
|
|
*/
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
rend_service_parse_intro_for_v2(
|
|
|
|
rend_intro_cell_t *intro,
|
|
|
|
const uint8_t *buf,
|
|
|
|
size_t plaintext_len,
|
|
|
|
char **err_msg_out)
|
|
|
|
{
|
|
|
|
unsigned int klen;
|
|
|
|
extend_info_t *extend_info = NULL;
|
|
|
|
ssize_t ver_specific_len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We accept version 3 too so that the v3 parser can call this with
|
2014-09-13 23:10:04 +02:00
|
|
|
* an adjusted buffer for the latter part of a v3 cell, which is
|
2012-07-15 11:36:36 +02:00
|
|
|
* identical to a v2 cell.
|
|
|
|
*/
|
|
|
|
if (!(intro->version == 2 ||
|
|
|
|
intro->version == 3)) {
|
|
|
|
if (err_msg_out)
|
|
|
|
tor_asprintf(err_msg_out,
|
|
|
|
"rend_service_parse_intro_for_v2() called with "
|
|
|
|
"bad version %d on INTRODUCE%d cell (this is a bug)",
|
|
|
|
intro->version,
|
|
|
|
(int)(intro->type));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 7 == version, IP and port, DIGEST_LEN == id, 2 == key length */
|
|
|
|
if (plaintext_len < 7 + DIGEST_LEN + 2) {
|
|
|
|
if (err_msg_out) {
|
|
|
|
tor_asprintf(err_msg_out,
|
|
|
|
"truncated plaintext of encrypted parted of "
|
|
|
|
"version %d INTRODUCE%d cell",
|
|
|
|
intro->version,
|
|
|
|
(int)(intro->type));
|
|
|
|
}
|
|
|
|
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
extend_info = tor_malloc_zero(sizeof(extend_info_t));
|
|
|
|
tor_addr_from_ipv4n(&extend_info->addr, get_uint32(buf + 1));
|
|
|
|
extend_info->port = ntohs(get_uint16(buf + 5));
|
|
|
|
memcpy(extend_info->identity_digest, buf + 7, DIGEST_LEN);
|
|
|
|
extend_info->nickname[0] = '$';
|
|
|
|
base16_encode(extend_info->nickname + 1, sizeof(extend_info->nickname) - 1,
|
|
|
|
extend_info->identity_digest, DIGEST_LEN);
|
|
|
|
klen = ntohs(get_uint16(buf + 7 + DIGEST_LEN));
|
|
|
|
|
|
|
|
/* 7 == version, IP and port, DIGEST_LEN == id, 2 == key length */
|
|
|
|
if (plaintext_len < 7 + DIGEST_LEN + 2 + klen) {
|
|
|
|
if (err_msg_out) {
|
|
|
|
tor_asprintf(err_msg_out,
|
|
|
|
"truncated plaintext of encrypted parted of "
|
|
|
|
"version %d INTRODUCE%d cell",
|
|
|
|
intro->version,
|
|
|
|
(int)(intro->type));
|
|
|
|
}
|
|
|
|
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
extend_info->onion_key =
|
|
|
|
crypto_pk_asn1_decode((const char *)(buf + 7 + DIGEST_LEN + 2), klen);
|
|
|
|
if (!extend_info->onion_key) {
|
|
|
|
if (err_msg_out) {
|
|
|
|
tor_asprintf(err_msg_out,
|
|
|
|
"error decoding onion key in version %d "
|
|
|
|
"INTRODUCE%d cell",
|
|
|
|
intro->version,
|
|
|
|
(intro->type));
|
|
|
|
}
|
|
|
|
|
|
|
|
goto err;
|
|
|
|
}
|
2015-03-30 23:53:39 +02:00
|
|
|
if (128 != crypto_pk_keysize(extend_info->onion_key)) {
|
|
|
|
if (err_msg_out) {
|
|
|
|
tor_asprintf(err_msg_out,
|
|
|
|
"invalid onion key size in version %d INTRODUCE%d cell",
|
|
|
|
intro->version,
|
|
|
|
(intro->type));
|
|
|
|
}
|
|
|
|
|
|
|
|
goto err;
|
|
|
|
}
|
2012-07-15 11:36:36 +02:00
|
|
|
|
|
|
|
ver_specific_len = 7+DIGEST_LEN+2+klen;
|
|
|
|
|
|
|
|
if (intro->version == 2) intro->u.v2.extend_info = extend_info;
|
|
|
|
else intro->u.v3.extend_info = extend_info;
|
|
|
|
|
|
|
|
return ver_specific_len;
|
|
|
|
|
|
|
|
err:
|
|
|
|
extend_info_free(extend_info);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Parse the version-specific parts of a v3 INTRODUCE1 or INTRODUCE2 cell
|
|
|
|
*/
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
rend_service_parse_intro_for_v3(
|
|
|
|
rend_intro_cell_t *intro,
|
|
|
|
const uint8_t *buf,
|
|
|
|
size_t plaintext_len,
|
|
|
|
char **err_msg_out)
|
|
|
|
{
|
|
|
|
ssize_t adjust, v2_ver_specific_len, ts_offset;
|
|
|
|
|
|
|
|
/* This should only be called on v3 cells */
|
|
|
|
if (intro->version != 3) {
|
|
|
|
if (err_msg_out)
|
|
|
|
tor_asprintf(err_msg_out,
|
|
|
|
"rend_service_parse_intro_for_v3() called with "
|
|
|
|
"bad version %d on INTRODUCE%d cell (this is a bug)",
|
|
|
|
intro->version,
|
|
|
|
(int)(intro->type));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that we have at least enough to get auth_len:
|
|
|
|
*
|
|
|
|
* 1 octet for version, 1 for auth_type, 2 for auth_len
|
|
|
|
*/
|
|
|
|
if (plaintext_len < 4) {
|
|
|
|
if (err_msg_out) {
|
|
|
|
tor_asprintf(err_msg_out,
|
|
|
|
"truncated plaintext of encrypted parted of "
|
|
|
|
"version %d INTRODUCE%d cell",
|
|
|
|
intro->version,
|
|
|
|
(int)(intro->type));
|
|
|
|
}
|
|
|
|
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The rend_client_send_introduction() function over in rendclient.c is
|
|
|
|
* broken (i.e., fails to match the spec) in such a way that we can't
|
|
|
|
* change it without breaking the protocol. Specifically, it doesn't
|
|
|
|
* emit auth_len when auth-type is REND_NO_AUTH, so everything is off
|
|
|
|
* by two bytes after that. Calculate ts_offset and do everything from
|
|
|
|
* the timestamp on relative to that to handle this dain bramage.
|
|
|
|
*/
|
|
|
|
|
|
|
|
intro->u.v3.auth_type = buf[1];
|
|
|
|
if (intro->u.v3.auth_type != REND_NO_AUTH) {
|
|
|
|
intro->u.v3.auth_len = ntohs(get_uint16(buf + 2));
|
|
|
|
ts_offset = 4 + intro->u.v3.auth_len;
|
2012-07-31 20:32:20 +02:00
|
|
|
} else {
|
2012-07-15 11:36:36 +02:00
|
|
|
intro->u.v3.auth_len = 0;
|
|
|
|
ts_offset = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that auth len makes sense for this auth type */
|
|
|
|
if (intro->u.v3.auth_type == REND_BASIC_AUTH ||
|
|
|
|
intro->u.v3.auth_type == REND_STEALTH_AUTH) {
|
|
|
|
if (intro->u.v3.auth_len != REND_DESC_COOKIE_LEN) {
|
|
|
|
if (err_msg_out) {
|
|
|
|
tor_asprintf(err_msg_out,
|
|
|
|
"wrong auth data size %d for INTRODUCE%d cell, "
|
|
|
|
"should be %d",
|
|
|
|
(int)(intro->u.v3.auth_len),
|
|
|
|
(int)(intro->type),
|
|
|
|
REND_DESC_COOKIE_LEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-05 17:40:33 +02:00
|
|
|
/* Check that we actually have everything up through the timestamp */
|
|
|
|
if (plaintext_len < (size_t)(ts_offset)+4) {
|
2012-07-15 11:36:36 +02:00
|
|
|
if (err_msg_out) {
|
|
|
|
tor_asprintf(err_msg_out,
|
|
|
|
"truncated plaintext of encrypted parted of "
|
|
|
|
"version %d INTRODUCE%d cell",
|
|
|
|
intro->version,
|
|
|
|
(int)(intro->type));
|
|
|
|
}
|
|
|
|
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (intro->u.v3.auth_type != REND_NO_AUTH &&
|
|
|
|
intro->u.v3.auth_len > 0) {
|
|
|
|
/* Okay, we can go ahead and copy auth_data */
|
|
|
|
intro->u.v3.auth_data = tor_malloc(intro->u.v3.auth_len);
|
|
|
|
/*
|
|
|
|
* We know we had an auth_len field in this case, so 4 is
|
|
|
|
* always right.
|
|
|
|
*/
|
|
|
|
memcpy(intro->u.v3.auth_data, buf + 4, intro->u.v3.auth_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* From here on, the format is as in v2, so we call the v2 parser with
|
|
|
|
* adjusted buffer and length. We are 4 + ts_offset octets in, but the
|
|
|
|
* v2 parser expects to skip over a version byte at the start, so we
|
|
|
|
* adjust by 3 + ts_offset.
|
|
|
|
*/
|
|
|
|
adjust = 3 + ts_offset;
|
|
|
|
|
|
|
|
v2_ver_specific_len =
|
|
|
|
rend_service_parse_intro_for_v2(intro,
|
|
|
|
buf + adjust, plaintext_len - adjust,
|
|
|
|
err_msg_out);
|
|
|
|
|
|
|
|
/* Success in v2 parser */
|
|
|
|
if (v2_ver_specific_len >= 0) return v2_ver_specific_len + adjust;
|
|
|
|
/* Failure in v2 parser; it will have provided an err_msg */
|
|
|
|
else return v2_ver_specific_len;
|
|
|
|
|
|
|
|
err:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Table of parser functions for version-specific parts of an INTRODUCE2
|
|
|
|
* cell.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
(*intro_version_handlers[])(
|
|
|
|
rend_intro_cell_t *,
|
|
|
|
const uint8_t *,
|
|
|
|
size_t,
|
|
|
|
char **) =
|
|
|
|
{ rend_service_parse_intro_for_v0_or_v1,
|
|
|
|
rend_service_parse_intro_for_v0_or_v1,
|
|
|
|
rend_service_parse_intro_for_v2,
|
|
|
|
rend_service_parse_intro_for_v3 };
|
|
|
|
|
|
|
|
/** Decrypt the encrypted part of an INTRODUCE1 or INTRODUCE2 cell,
|
|
|
|
* return 0 if successful, or < 0 and write an error message to
|
|
|
|
* *err_msg_out if provided.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
rend_service_decrypt_intro(
|
|
|
|
rend_intro_cell_t *intro,
|
|
|
|
crypto_pk_t *key,
|
|
|
|
char **err_msg_out)
|
|
|
|
{
|
|
|
|
char *err_msg = NULL;
|
|
|
|
uint8_t key_digest[DIGEST_LEN];
|
|
|
|
char service_id[REND_SERVICE_ID_LEN_BASE32+1];
|
|
|
|
ssize_t key_len;
|
|
|
|
uint8_t buf[RELAY_PAYLOAD_SIZE];
|
2014-08-21 16:49:01 +02:00
|
|
|
int result, status = -1;
|
2012-07-15 11:36:36 +02:00
|
|
|
|
|
|
|
if (!intro || !key) {
|
|
|
|
if (err_msg_out) {
|
|
|
|
err_msg =
|
|
|
|
tor_strdup("rend_service_decrypt_intro() called with bad "
|
|
|
|
"parameters");
|
|
|
|
}
|
|
|
|
|
|
|
|
status = -2;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure we have ciphertext */
|
|
|
|
if (!(intro->ciphertext) || intro->ciphertext_len <= 0) {
|
|
|
|
if (err_msg_out) {
|
|
|
|
tor_asprintf(&err_msg,
|
|
|
|
"rend_intro_cell_t was missing ciphertext for "
|
|
|
|
"INTRODUCE%d cell",
|
|
|
|
(int)(intro->type));
|
|
|
|
}
|
|
|
|
status = -3;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that this cell actually matches this service key */
|
|
|
|
|
|
|
|
/* first DIGEST_LEN bytes of request is intro or service pk digest */
|
|
|
|
crypto_pk_get_digest(key, (char *)key_digest);
|
|
|
|
if (tor_memneq(key_digest, intro->pk, DIGEST_LEN)) {
|
|
|
|
if (err_msg_out) {
|
|
|
|
base32_encode(service_id, REND_SERVICE_ID_LEN_BASE32 + 1,
|
|
|
|
(char*)(intro->pk), REND_SERVICE_ID_LEN);
|
|
|
|
tor_asprintf(&err_msg,
|
|
|
|
"got an INTRODUCE%d cell for the wrong service (%s)",
|
|
|
|
(int)(intro->type),
|
|
|
|
escaped(service_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
status = -4;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure the encrypted part is long enough to decrypt */
|
|
|
|
|
|
|
|
key_len = crypto_pk_keysize(key);
|
|
|
|
if (intro->ciphertext_len < key_len) {
|
|
|
|
if (err_msg_out) {
|
|
|
|
tor_asprintf(&err_msg,
|
|
|
|
"got an INTRODUCE%d cell with a truncated PK-encrypted "
|
|
|
|
"part",
|
|
|
|
(int)(intro->type));
|
|
|
|
}
|
|
|
|
|
|
|
|
status = -5;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decrypt the encrypted part */
|
|
|
|
|
|
|
|
note_crypto_pk_op(REND_SERVER);
|
|
|
|
result =
|
|
|
|
crypto_pk_private_hybrid_decrypt(
|
|
|
|
key, (char *)buf, sizeof(buf),
|
|
|
|
(const char *)(intro->ciphertext), intro->ciphertext_len,
|
|
|
|
PK_PKCS1_OAEP_PADDING, 1);
|
|
|
|
if (result < 0) {
|
|
|
|
if (err_msg_out) {
|
|
|
|
tor_asprintf(&err_msg,
|
|
|
|
"couldn't decrypt INTRODUCE%d cell",
|
|
|
|
(int)(intro->type));
|
|
|
|
}
|
|
|
|
status = -6;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
intro->plaintext_len = result;
|
|
|
|
intro->plaintext = tor_malloc(intro->plaintext_len);
|
|
|
|
memcpy(intro->plaintext, buf, intro->plaintext_len);
|
|
|
|
|
2014-08-21 16:49:01 +02:00
|
|
|
status = 0;
|
|
|
|
|
2012-07-15 11:36:36 +02:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (err_msg_out && !err_msg) {
|
|
|
|
tor_asprintf(&err_msg,
|
|
|
|
"unknown INTRODUCE%d error decrypting encrypted part",
|
2014-04-19 03:24:16 +02:00
|
|
|
intro ? (int)(intro->type) : -1);
|
2012-07-15 11:36:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (err_msg_out) *err_msg_out = err_msg;
|
|
|
|
else tor_free(err_msg);
|
|
|
|
|
|
|
|
/* clean up potentially sensitive material */
|
2012-11-08 23:00:36 +01:00
|
|
|
memwipe(buf, 0, sizeof(buf));
|
|
|
|
memwipe(key_digest, 0, sizeof(key_digest));
|
|
|
|
memwipe(service_id, 0, sizeof(service_id));
|
2012-07-15 11:36:36 +02:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Parse the plaintext of the encrypted part of an INTRODUCE1 or
|
|
|
|
* INTRODUCE2 cell, return 0 if successful, or < 0 and write an error
|
|
|
|
* message to *err_msg_out if provided.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
rend_service_parse_intro_plaintext(
|
|
|
|
rend_intro_cell_t *intro,
|
|
|
|
char **err_msg_out)
|
|
|
|
{
|
|
|
|
char *err_msg = NULL;
|
|
|
|
ssize_t ver_specific_len, ver_invariant_len;
|
|
|
|
uint8_t version;
|
2014-08-21 16:49:01 +02:00
|
|
|
int status = -1;
|
2012-07-15 11:36:36 +02:00
|
|
|
|
|
|
|
if (!intro) {
|
|
|
|
if (err_msg_out) {
|
|
|
|
err_msg =
|
|
|
|
tor_strdup("rend_service_parse_intro_plaintext() called with NULL "
|
|
|
|
"rend_intro_cell_t");
|
|
|
|
}
|
|
|
|
|
|
|
|
status = -2;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that we have plaintext */
|
|
|
|
if (!(intro->plaintext) || intro->plaintext_len <= 0) {
|
|
|
|
if (err_msg_out) {
|
|
|
|
err_msg = tor_strdup("rend_intro_cell_t was missing plaintext");
|
|
|
|
}
|
|
|
|
status = -3;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In all formats except v0, the first byte is a version number */
|
|
|
|
version = intro->plaintext[0];
|
|
|
|
|
|
|
|
/* v0 has no version byte (stupid...), so handle it as a fallback */
|
|
|
|
if (version > 3) version = 0;
|
|
|
|
|
|
|
|
/* Copy the version into the parsed cell structure */
|
|
|
|
intro->version = version;
|
|
|
|
|
|
|
|
/* Call the version-specific parser from the table */
|
|
|
|
ver_specific_len =
|
|
|
|
intro_version_handlers[version](intro,
|
|
|
|
intro->plaintext, intro->plaintext_len,
|
|
|
|
&err_msg);
|
|
|
|
if (ver_specific_len < 0) {
|
|
|
|
status = -4;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** The rendezvous cookie and Diffie-Hellman stuff are version-invariant
|
|
|
|
* and at the end of the plaintext of the encrypted part of the cell.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ver_invariant_len = intro->plaintext_len - ver_specific_len;
|
|
|
|
if (ver_invariant_len < REND_COOKIE_LEN + DH_KEY_LEN) {
|
|
|
|
tor_asprintf(&err_msg,
|
|
|
|
"decrypted plaintext of INTRODUCE%d cell was truncated (%ld bytes)",
|
|
|
|
(int)(intro->type),
|
|
|
|
(long)(intro->plaintext_len));
|
|
|
|
status = -5;
|
|
|
|
goto err;
|
|
|
|
} else if (ver_invariant_len > REND_COOKIE_LEN + DH_KEY_LEN) {
|
|
|
|
tor_asprintf(&err_msg,
|
|
|
|
"decrypted plaintext of INTRODUCE%d cell was too long (%ld bytes)",
|
|
|
|
(int)(intro->type),
|
|
|
|
(long)(intro->plaintext_len));
|
|
|
|
status = -6;
|
2014-08-21 16:49:01 +02:00
|
|
|
goto err;
|
2012-07-15 11:36:36 +02:00
|
|
|
} else {
|
|
|
|
memcpy(intro->rc,
|
|
|
|
intro->plaintext + ver_specific_len,
|
|
|
|
REND_COOKIE_LEN);
|
|
|
|
memcpy(intro->dh,
|
|
|
|
intro->plaintext + ver_specific_len + REND_COOKIE_LEN,
|
|
|
|
DH_KEY_LEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Flag it as being fully parsed */
|
|
|
|
intro->parsed = 1;
|
|
|
|
|
2014-08-21 16:49:01 +02:00
|
|
|
status = 0;
|
2012-07-15 11:36:36 +02:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (err_msg_out && !err_msg) {
|
|
|
|
tor_asprintf(&err_msg,
|
|
|
|
"unknown INTRODUCE%d error parsing encrypted part",
|
2014-04-19 03:24:16 +02:00
|
|
|
intro ? (int)(intro->type) : -1);
|
2012-07-15 11:36:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (err_msg_out) *err_msg_out = err_msg;
|
|
|
|
else tor_free(err_msg);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Do validity checks on a parsed intro cell after decryption; some of
|
|
|
|
* these are not done in rend_service_parse_intro_plaintext() itself because
|
|
|
|
* they depend on a lot of other state and would make it hard to unit test.
|
|
|
|
* Returns >= 0 if successful or < 0 if the intro cell is invalid, and
|
|
|
|
* optionally writes out an error message for logging. If an err_msg
|
|
|
|
* pointer is provided, it is the caller's responsibility to free any
|
|
|
|
* provided message.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
rend_service_validate_intro_late(const rend_intro_cell_t *intro,
|
|
|
|
char **err_msg_out)
|
|
|
|
{
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
if (!intro) {
|
|
|
|
if (err_msg_out)
|
|
|
|
*err_msg_out =
|
|
|
|
tor_strdup("NULL intro cell passed to "
|
|
|
|
"rend_service_validate_intro_late()");
|
|
|
|
|
|
|
|
status = -1;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (intro->version == 3 && intro->parsed) {
|
|
|
|
if (!(intro->u.v3.auth_type == REND_NO_AUTH ||
|
|
|
|
intro->u.v3.auth_type == REND_BASIC_AUTH ||
|
|
|
|
intro->u.v3.auth_type == REND_STEALTH_AUTH)) {
|
|
|
|
/* This is an informative message, not an error, as in the old code */
|
|
|
|
if (err_msg_out)
|
|
|
|
tor_asprintf(err_msg_out,
|
|
|
|
"unknown authorization type %d",
|
|
|
|
intro->u.v3.auth_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err:
|
2012-06-16 05:19:02 +02:00
|
|
|
return status;
|
2004-04-01 05:34:05 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Called when we fail building a rendezvous circuit at some point other
|
2004-05-05 23:32:43 +02:00
|
|
|
* than the last hop: launches a new circuit to the same rendezvous point.
|
|
|
|
*/
|
2004-04-14 23:40:50 +02:00
|
|
|
void
|
2006-07-23 09:37:35 +02:00
|
|
|
rend_service_relaunch_rendezvous(origin_circuit_t *oldcirc)
|
2004-04-14 23:40:50 +02:00
|
|
|
{
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t *newcirc;
|
2004-04-14 23:40:50 +02:00
|
|
|
cpath_build_state_t *newstate, *oldstate;
|
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
tor_assert(oldcirc->base_.purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
|
2004-04-14 23:40:50 +02:00
|
|
|
|
2011-12-24 13:47:30 +01:00
|
|
|
/* Don't relaunch the same rend circ twice. */
|
|
|
|
if (oldcirc->hs_service_side_rend_circ_has_been_relaunched) {
|
|
|
|
log_info(LD_REND, "Rendezvous circuit to %s has already been relaunched; "
|
|
|
|
"not relaunching it again.",
|
|
|
|
oldcirc->build_state ?
|
|
|
|
safe_str(extend_info_describe(oldcirc->build_state->chosen_exit))
|
|
|
|
: "*unknown*");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
oldcirc->hs_service_side_rend_circ_has_been_relaunched = 1;
|
|
|
|
|
2004-04-14 23:40:50 +02:00
|
|
|
if (!oldcirc->build_state ||
|
2005-03-19 05:38:59 +01:00
|
|
|
oldcirc->build_state->failure_count > MAX_REND_FAILURES ||
|
|
|
|
oldcirc->build_state->expiry_time < time(NULL)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_REND,
|
|
|
|
"Attempt to build circuit to %s for rendezvous has failed "
|
|
|
|
"too many times or expired; giving up.",
|
|
|
|
oldcirc->build_state ?
|
2011-05-16 03:58:46 +02:00
|
|
|
safe_str(extend_info_describe(oldcirc->build_state->chosen_exit))
|
|
|
|
: "*unknown*");
|
2004-04-14 23:40:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-10-30 07:04:52 +02:00
|
|
|
oldstate = oldcirc->build_state;
|
|
|
|
tor_assert(oldstate);
|
|
|
|
|
2012-01-07 07:44:20 +01:00
|
|
|
if (oldstate->service_pending_final_cpath_ref == NULL) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_REND,"Skipping relaunch of circ that failed on its first hop. "
|
|
|
|
"Initiator will retry.");
|
2004-10-30 07:04:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-03-06 01:25:39 +01:00
|
|
|
log_info(LD_REND,"Reattempting rendezvous circuit to '%s'",
|
2011-05-16 03:58:46 +02:00
|
|
|
safe_str(extend_info_describe(oldstate->chosen_exit)));
|
2004-04-14 23:40:50 +02:00
|
|
|
|
2016-07-14 06:04:02 +02:00
|
|
|
/* You'd think Single Onion Services would want to retry the rendezvous
|
|
|
|
* using a direct connection. But if it's blocked by a firewall, or the
|
|
|
|
* service is IPv6-only, or the rend point avoiding becoming a one-hop
|
|
|
|
* proxy, we need a 3-hop connection. */
|
2007-12-23 20:15:22 +01:00
|
|
|
newcirc = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_CONNECT_REND,
|
|
|
|
oldstate->chosen_exit,
|
|
|
|
CIRCLAUNCH_NEED_CAPACITY|CIRCLAUNCH_IS_INTERNAL);
|
|
|
|
|
2004-04-14 23:40:50 +02:00
|
|
|
if (!newcirc) {
|
2006-03-06 01:25:39 +01:00
|
|
|
log_warn(LD_REND,"Couldn't relaunch rendezvous circuit to '%s'.",
|
2011-05-16 03:58:46 +02:00
|
|
|
safe_str(extend_info_describe(oldstate->chosen_exit)));
|
2004-04-14 23:40:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
newstate = newcirc->build_state;
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(newstate);
|
2004-04-14 23:40:50 +02:00
|
|
|
newstate->failure_count = oldstate->failure_count+1;
|
2005-03-19 05:38:59 +01:00
|
|
|
newstate->expiry_time = oldstate->expiry_time;
|
2012-01-07 07:44:20 +01:00
|
|
|
newstate->service_pending_final_cpath_ref =
|
|
|
|
oldstate->service_pending_final_cpath_ref;
|
|
|
|
++(newstate->service_pending_final_cpath_ref->refcount);
|
2004-04-14 23:40:50 +02:00
|
|
|
|
2008-09-24 16:44:29 +02:00
|
|
|
newcirc->rend_data = rend_data_dup(oldcirc->rend_data);
|
2004-04-14 23:40:50 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Launch a circuit to serve as an introduction point for the service
|
2004-05-10 06:34:48 +02:00
|
|
|
* <b>service</b> at the introduction point <b>nickname</b>
|
2004-04-02 00:21:01 +02:00
|
|
|
*/
|
|
|
|
static int
|
2005-12-14 21:40:40 +01:00
|
|
|
rend_service_launch_establish_intro(rend_service_t *service,
|
2007-12-21 10:28:22 +01:00
|
|
|
rend_intro_point_t *intro)
|
2004-04-02 00:21:01 +02:00
|
|
|
{
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t *launched;
|
2016-07-14 06:04:02 +02:00
|
|
|
int flags = CIRCLAUNCH_NEED_UPTIME|CIRCLAUNCH_IS_INTERNAL;
|
2016-08-18 05:19:22 +02:00
|
|
|
const or_options_t *options = get_options();
|
|
|
|
extend_info_t *launch_ei = intro->extend_info;
|
|
|
|
extend_info_t *direct_ei = NULL;
|
|
|
|
|
|
|
|
/* Are we in single onion mode? */
|
|
|
|
if (rend_service_allow_non_anonymous_connection(options)) {
|
|
|
|
/* Do we have a descriptor for the node?
|
|
|
|
* We've either just chosen it from the consensus, or we've just reviewed
|
|
|
|
* our intro points to see which ones are still valid, and deleted the ones
|
|
|
|
* that aren't in the consensus any more. */
|
|
|
|
const node_t *node = node_get_by_id(launch_ei->identity_digest);
|
|
|
|
if (BUG(!node)) {
|
|
|
|
/* The service has kept an intro point after it went missing from the
|
|
|
|
* consensus. If we did anything else here, it would be a consensus
|
|
|
|
* distinguisher. Which are less of an issue for single onion services,
|
|
|
|
* but still a bug. */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Can we connect to the node directly? If so, replace launch_ei
|
|
|
|
* (a multi-hop extend_info) with one suitable for direct connection. */
|
|
|
|
if (rend_service_use_direct_connection_node(options, node)) {
|
|
|
|
direct_ei = extend_info_from_node(node, 1);
|
|
|
|
if (BUG(!direct_ei)) {
|
|
|
|
/* rend_service_use_direct_connection_node and extend_info_from_node
|
|
|
|
* disagree about which addresses on this node are permitted. This
|
|
|
|
* should never happen. Avoiding the connection is a safe response. */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
flags = flags | CIRCLAUNCH_ONEHOP_TUNNEL;
|
|
|
|
launch_ei = direct_ei;
|
|
|
|
}
|
2016-07-14 06:04:02 +02:00
|
|
|
}
|
2016-08-18 05:19:22 +02:00
|
|
|
/* launch_ei is either intro->extend_info, or has been replaced with a valid
|
|
|
|
* extend_info for single onion service direct connection. */
|
|
|
|
tor_assert(launch_ei);
|
|
|
|
/* We must have the same intro when making a direct connection. */
|
|
|
|
tor_assert(tor_memeq(intro->extend_info->identity_digest,
|
|
|
|
launch_ei->identity_digest,
|
|
|
|
DIGEST_LEN));
|
2004-04-02 00:21:01 +02:00
|
|
|
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_REND,
|
2016-08-18 05:19:22 +02:00
|
|
|
"Launching circuit to introduction point %s%s%s for service %s",
|
2011-05-16 03:58:46 +02:00
|
|
|
safe_str_client(extend_info_describe(intro->extend_info)),
|
2016-08-18 05:19:22 +02:00
|
|
|
direct_ei ? " via direct address " : "",
|
|
|
|
direct_ei ? safe_str_client(extend_info_describe(direct_ei)) : "",
|
2007-12-21 10:28:22 +01:00
|
|
|
service->service_id);
|
2004-04-03 02:58:54 +02:00
|
|
|
|
2005-11-25 09:08:56 +01:00
|
|
|
rep_hist_note_used_internal(time(NULL), 1, 0);
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
|
2004-05-18 03:53:53 +02:00
|
|
|
++service->n_intro_circuits_launched;
|
2007-12-21 10:28:22 +01:00
|
|
|
launched = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
|
2016-08-18 05:19:22 +02:00
|
|
|
launch_ei, flags);
|
2007-12-23 20:15:22 +01:00
|
|
|
|
2004-04-02 00:21:01 +02:00
|
|
|
if (!launched) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_REND,
|
2016-08-18 05:19:22 +02:00
|
|
|
"Can't launch circuit to establish introduction at %s%s%s.",
|
|
|
|
safe_str_client(extend_info_describe(intro->extend_info)),
|
|
|
|
direct_ei ? " via direct address " : "",
|
|
|
|
direct_ei ? safe_str_client(extend_info_describe(direct_ei)) : ""
|
|
|
|
);
|
|
|
|
extend_info_free(direct_ei);
|
2004-04-02 00:21:01 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2016-08-18 05:19:22 +02:00
|
|
|
/* We must have the same exit node even if cannibalized or direct connection.
|
|
|
|
*/
|
2015-06-16 16:29:29 +02:00
|
|
|
tor_assert(tor_memeq(intro->extend_info->identity_digest,
|
|
|
|
launched->build_state->chosen_exit->identity_digest,
|
|
|
|
DIGEST_LEN));
|
2008-06-17 19:10:03 +02:00
|
|
|
|
2015-04-27 21:08:31 +02:00
|
|
|
launched->rend_data = rend_data_service_create(service->service_id,
|
|
|
|
service->pk_digest, NULL,
|
|
|
|
service->auth_type);
|
2009-05-02 23:31:58 +02:00
|
|
|
launched->intro_key = crypto_pk_dup_key(intro->intro_key);
|
2012-10-12 18:22:13 +02:00
|
|
|
if (launched->base_.state == CIRCUIT_STATE_OPEN)
|
2005-08-12 03:26:21 +02:00
|
|
|
rend_service_intro_has_opened(launched);
|
2016-08-18 05:19:22 +02:00
|
|
|
extend_info_free(direct_ei);
|
2004-04-02 00:21:01 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-15 23:11:57 +02:00
|
|
|
/** Return the number of introduction points that are established for the
|
|
|
|
* given service. */
|
2015-06-01 19:17:37 +02:00
|
|
|
static unsigned int
|
|
|
|
count_established_intro_points(const rend_service_t *service)
|
2008-10-15 22:59:48 +02:00
|
|
|
{
|
2015-06-01 19:17:37 +02:00
|
|
|
unsigned int num = 0;
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(service->intro_nodes, rend_intro_point_t *, intro,
|
|
|
|
num += intro->circuit_established
|
|
|
|
);
|
|
|
|
return num;
|
2008-10-15 22:59:48 +02:00
|
|
|
}
|
|
|
|
|
2015-06-15 23:11:57 +02:00
|
|
|
/** Return the number of introduction points that are or are being
|
|
|
|
* established for the given service. This function iterates over all
|
|
|
|
* circuit and count those that are linked to the service and are waiting
|
|
|
|
* for the intro point to respond. */
|
|
|
|
static unsigned int
|
|
|
|
count_intro_point_circuits(const rend_service_t *service)
|
|
|
|
{
|
|
|
|
unsigned int num_ipos = 0;
|
|
|
|
SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
|
|
|
|
if (!circ->marked_for_close &&
|
|
|
|
circ->state == CIRCUIT_STATE_OPEN &&
|
|
|
|
(circ->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO ||
|
|
|
|
circ->purpose == CIRCUIT_PURPOSE_S_INTRO)) {
|
|
|
|
origin_circuit_t *oc = TO_ORIGIN_CIRCUIT(circ);
|
|
|
|
if (oc->rend_data &&
|
2016-05-31 20:51:30 +02:00
|
|
|
rend_circuit_pk_digest_eq(oc, (uint8_t *) service->pk_digest)) {
|
2015-06-15 23:11:57 +02:00
|
|
|
num_ipos++;
|
2016-05-31 20:51:30 +02:00
|
|
|
}
|
2015-06-15 23:11:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
SMARTLIST_FOREACH_END(circ);
|
|
|
|
return num_ipos;
|
|
|
|
}
|
|
|
|
|
2016-09-05 17:36:12 +02:00
|
|
|
/* Given a buffer of at least RELAY_PAYLOAD_SIZE bytes in <b>cell_body_out</b>,
|
|
|
|
write the body of a legacy ESTABLISH_INTRO cell in it. Use <b>intro_key</b>
|
|
|
|
as the intro point auth key, and <b>rend_circ_nonce</b> as the circuit
|
|
|
|
crypto material. On success, fill <b>cell_body_out</b> and return the number
|
|
|
|
of bytes written. On fail, return -1.
|
|
|
|
*/
|
|
|
|
STATIC ssize_t
|
2017-02-24 15:48:14 +01:00
|
|
|
encode_establish_intro_cell_legacy(char *cell_body_out,
|
2017-02-24 17:37:33 +01:00
|
|
|
size_t cell_body_out_len,
|
|
|
|
crypto_pk_t *intro_key, char *rend_circ_nonce)
|
2016-09-02 10:42:34 +02:00
|
|
|
{
|
2016-09-05 17:36:12 +02:00
|
|
|
int retval = -1;
|
|
|
|
int r;
|
|
|
|
int len = 0;
|
|
|
|
char auth[DIGEST_LEN + 9];
|
|
|
|
|
|
|
|
tor_assert(intro_key);
|
|
|
|
tor_assert(rend_circ_nonce);
|
|
|
|
|
2016-09-02 10:42:34 +02:00
|
|
|
/* Build the payload for a RELAY_ESTABLISH_INTRO cell. */
|
2016-09-05 17:36:12 +02:00
|
|
|
r = crypto_pk_asn1_encode(intro_key, cell_body_out+2,
|
2016-09-02 10:42:34 +02:00
|
|
|
RELAY_PAYLOAD_SIZE-2);
|
|
|
|
if (r < 0) {
|
|
|
|
log_warn(LD_BUG, "Internal error; failed to establish intro point.");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
len = r;
|
2016-09-05 17:36:12 +02:00
|
|
|
set_uint16(cell_body_out, htons((uint16_t)len));
|
2016-09-02 10:42:34 +02:00
|
|
|
len += 2;
|
2016-09-05 17:36:12 +02:00
|
|
|
memcpy(auth, rend_circ_nonce, DIGEST_LEN);
|
2016-09-02 10:42:34 +02:00
|
|
|
memcpy(auth+DIGEST_LEN, "INTRODUCE", 9);
|
2016-09-05 17:36:12 +02:00
|
|
|
if (crypto_digest(cell_body_out+len, auth, DIGEST_LEN+9))
|
2016-09-02 10:42:34 +02:00
|
|
|
goto err;
|
|
|
|
len += 20;
|
|
|
|
note_crypto_pk_op(REND_SERVER);
|
2016-09-05 17:36:12 +02:00
|
|
|
r = crypto_pk_private_sign_digest(intro_key, cell_body_out+len,
|
2017-02-24 15:48:14 +01:00
|
|
|
cell_body_out_len - len,
|
2016-09-05 17:36:12 +02:00
|
|
|
cell_body_out, len);
|
2016-09-02 10:42:34 +02:00
|
|
|
if (r<0) {
|
|
|
|
log_warn(LD_BUG, "Internal error: couldn't sign introduction request.");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
len += r;
|
2016-09-05 17:36:12 +02:00
|
|
|
|
|
|
|
retval = len;
|
|
|
|
|
|
|
|
err:
|
|
|
|
memwipe(auth, 0, sizeof(auth));
|
|
|
|
|
|
|
|
return retval;
|
2016-09-02 10:42:34 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Called when we're done building a circuit to an introduction point:
|
2004-04-02 00:21:01 +02:00
|
|
|
* sends a RELAY_ESTABLISH_INTRO cell.
|
|
|
|
*/
|
|
|
|
void
|
2006-07-23 09:37:35 +02:00
|
|
|
rend_service_intro_has_opened(origin_circuit_t *circuit)
|
2004-04-02 00:21:01 +02:00
|
|
|
{
|
|
|
|
rend_service_t *service;
|
|
|
|
char buf[RELAY_PAYLOAD_SIZE];
|
2007-11-29 16:25:04 +01:00
|
|
|
char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
|
2017-02-01 17:22:17 +01:00
|
|
|
unsigned int expiring_nodes_len, num_ip_circuits, valid_ip_circuits = 0;
|
2006-10-09 17:47:50 +02:00
|
|
|
int reason = END_CIRC_REASON_TORPROTOCOL;
|
2016-05-31 20:51:30 +02:00
|
|
|
const char *rend_pk_digest;
|
2004-04-02 00:21:01 +02:00
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
tor_assert(circuit->base_.purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO);
|
2016-07-14 06:04:02 +02:00
|
|
|
assert_circ_anonymity_ok(circuit, get_options());
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(circuit->cpath);
|
2008-09-24 16:44:29 +02:00
|
|
|
tor_assert(circuit->rend_data);
|
2016-05-31 20:51:30 +02:00
|
|
|
/* XXX: This is version 2 specific (only on supported). */
|
|
|
|
rend_pk_digest = (char *) rend_data_get_pk_digest(circuit->rend_data, NULL);
|
2004-04-03 02:58:54 +02:00
|
|
|
|
2007-11-29 16:25:04 +01:00
|
|
|
base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
|
2016-05-31 20:51:30 +02:00
|
|
|
rend_pk_digest, REND_SERVICE_ID_LEN);
|
2004-04-12 07:12:50 +02:00
|
|
|
|
2016-05-31 20:51:30 +02:00
|
|
|
service = rend_service_get_by_pk_digest(rend_pk_digest);
|
2004-04-02 00:21:01 +02:00
|
|
|
if (!service) {
|
2013-03-10 13:32:58 +01:00
|
|
|
log_warn(LD_REND, "Unrecognized service ID %s on introduction circuit %u.",
|
2016-03-21 13:22:14 +01:00
|
|
|
safe_str_client(serviceid), (unsigned)circuit->base_.n_circ_id);
|
2006-10-17 17:20:00 +02:00
|
|
|
reason = END_CIRC_REASON_NOSUCHSERVICE;
|
2004-04-02 00:21:01 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-02-01 17:22:17 +01:00
|
|
|
/* Take the current amount of expiring nodes and the current amount of IP
|
|
|
|
* circuits and compute how many valid IP circuits we have. */
|
|
|
|
expiring_nodes_len = (unsigned int) smartlist_len(service->expiring_nodes);
|
|
|
|
num_ip_circuits = count_intro_point_circuits(service);
|
|
|
|
/* Let's avoid an underflow. The valid_ip_circuits is initialized to 0 in
|
|
|
|
* case this condition turns out false because it means that all circuits
|
|
|
|
* are expiring so we need to keep this circuit. */
|
|
|
|
if (num_ip_circuits > expiring_nodes_len) {
|
|
|
|
valid_ip_circuits = num_ip_circuits - expiring_nodes_len;
|
|
|
|
}
|
|
|
|
|
2008-10-15 22:59:48 +02:00
|
|
|
/* If we already have enough introduction circuits for this service,
|
2015-05-29 23:45:45 +02:00
|
|
|
* redefine this one as a general circuit or close it, depending.
|
2017-02-01 17:22:17 +01:00
|
|
|
* Substract the amount of expiring nodes here because the circuits are
|
2015-05-29 23:45:45 +02:00
|
|
|
* still opened. */
|
2017-02-01 17:22:17 +01:00
|
|
|
if (valid_ip_circuits > service->n_intro_points_wanted) {
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2015-06-01 18:08:13 +02:00
|
|
|
/* Remove the intro point associated with this circuit, it's being
|
|
|
|
* repurposed or closed thus cleanup memory. */
|
|
|
|
rend_intro_point_t *intro = find_intro_point(circuit);
|
|
|
|
if (intro != NULL) {
|
|
|
|
smartlist_remove(service->intro_nodes, intro);
|
|
|
|
rend_intro_point_free(intro);
|
|
|
|
}
|
|
|
|
|
2011-04-04 02:06:31 +02:00
|
|
|
if (options->ExcludeNodes) {
|
|
|
|
/* XXXX in some future version, we can test whether the transition is
|
|
|
|
allowed or not given the actual nodes in the circuit. But for now,
|
|
|
|
this case, we might as well close the thing. */
|
|
|
|
log_info(LD_CIRC|LD_REND, "We have just finished an introduction "
|
|
|
|
"circuit, but we already have enough. Closing it.");
|
2012-06-16 05:43:33 +02:00
|
|
|
reason = END_CIRC_REASON_NONE;
|
|
|
|
goto err;
|
2011-04-04 02:06:31 +02:00
|
|
|
} else {
|
|
|
|
tor_assert(circuit->build_state->is_internal);
|
|
|
|
log_info(LD_CIRC|LD_REND, "We have just finished an introduction "
|
|
|
|
"circuit, but we already have enough. Redefining purpose to "
|
|
|
|
"general; leaving as internal.");
|
2011-10-18 16:08:02 +02:00
|
|
|
|
2011-11-24 15:52:38 +01:00
|
|
|
circuit_change_purpose(TO_CIRCUIT(circuit), CIRCUIT_PURPOSE_C_GENERAL);
|
2011-10-18 16:08:02 +02:00
|
|
|
|
|
|
|
{
|
2016-05-31 20:51:30 +02:00
|
|
|
rend_data_free(circuit->rend_data);
|
2011-10-18 16:08:02 +02:00
|
|
|
circuit->rend_data = NULL;
|
|
|
|
}
|
|
|
|
{
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_t *intro_key = circuit->intro_key;
|
2011-10-18 16:08:02 +02:00
|
|
|
circuit->intro_key = NULL;
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_free(intro_key);
|
2011-10-18 16:08:02 +02:00
|
|
|
}
|
|
|
|
|
2011-04-04 02:06:31 +02:00
|
|
|
circuit_has_opened(circuit);
|
2012-06-16 05:43:33 +02:00
|
|
|
goto done;
|
2011-04-04 02:06:31 +02:00
|
|
|
}
|
2008-10-15 22:59:48 +02:00
|
|
|
}
|
|
|
|
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_REND,
|
2013-03-10 13:32:58 +01:00
|
|
|
"Established circuit %u as introduction point for service %s",
|
|
|
|
(unsigned)circuit->base_.n_circ_id, serviceid);
|
2016-07-14 06:04:02 +02:00
|
|
|
circuit_log_path(LOG_INFO, LD_REND, circuit);
|
2004-04-03 02:58:54 +02:00
|
|
|
|
2016-09-05 17:36:12 +02:00
|
|
|
/* Send the ESTABLISH_INTRO cell */
|
|
|
|
{
|
|
|
|
ssize_t len;
|
2017-02-24 15:48:14 +01:00
|
|
|
len = encode_establish_intro_cell_legacy(buf, sizeof(buf),
|
|
|
|
circuit->intro_key,
|
|
|
|
circuit->cpath->prev->rend_circ_nonce);
|
2016-09-05 17:36:12 +02:00
|
|
|
if (len < 0) {
|
|
|
|
reason = END_CIRC_REASON_INTERNAL;
|
|
|
|
goto err;
|
|
|
|
}
|
2004-04-02 00:21:01 +02:00
|
|
|
|
2016-09-05 17:36:12 +02:00
|
|
|
if (relay_send_command_from_edge(0, TO_CIRCUIT(circuit),
|
|
|
|
RELAY_COMMAND_ESTABLISH_INTRO,
|
|
|
|
buf, len, circuit->cpath->prev)<0) {
|
|
|
|
log_info(LD_GENERAL,
|
2013-03-10 13:32:58 +01:00
|
|
|
"Couldn't send introduction request for service %s on circuit %u",
|
|
|
|
serviceid, (unsigned)circuit->base_.n_circ_id);
|
2016-09-05 17:36:12 +02:00
|
|
|
goto done;
|
|
|
|
}
|
2004-04-02 00:21:01 +02:00
|
|
|
}
|
|
|
|
|
2013-01-19 04:37:16 +01:00
|
|
|
/* We've attempted to use this circuit */
|
|
|
|
pathbias_count_use_attempt(circuit);
|
|
|
|
|
2012-06-16 05:43:33 +02:00
|
|
|
goto done;
|
|
|
|
|
2004-04-02 00:21:01 +02:00
|
|
|
err:
|
2006-10-09 17:47:50 +02:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circuit), reason);
|
2012-06-16 05:43:33 +02:00
|
|
|
done:
|
2012-11-08 23:00:36 +01:00
|
|
|
memwipe(buf, 0, sizeof(buf));
|
|
|
|
memwipe(serviceid, 0, sizeof(serviceid));
|
2012-06-16 05:43:33 +02:00
|
|
|
|
|
|
|
return;
|
2004-04-02 00:21:01 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Called when we get an INTRO_ESTABLISHED cell; mark the circuit as a
|
2004-05-05 23:32:43 +02:00
|
|
|
* live introduction point, and note that the service descriptor is
|
2009-10-18 00:52:18 +02:00
|
|
|
* now out-of-date. */
|
2004-04-03 06:55:22 +02:00
|
|
|
int
|
2010-12-14 01:34:01 +01:00
|
|
|
rend_service_intro_established(origin_circuit_t *circuit,
|
|
|
|
const uint8_t *request,
|
2005-12-14 21:40:40 +01:00
|
|
|
size_t request_len)
|
2004-04-03 06:55:22 +02:00
|
|
|
{
|
2004-04-13 19:16:47 +02:00
|
|
|
rend_service_t *service;
|
2015-06-01 19:17:37 +02:00
|
|
|
rend_intro_point_t *intro;
|
2007-11-29 16:25:04 +01:00
|
|
|
char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
|
2006-06-05 00:42:13 +02:00
|
|
|
(void) request;
|
|
|
|
(void) request_len;
|
2016-05-31 20:51:30 +02:00
|
|
|
tor_assert(circuit->rend_data);
|
|
|
|
/* XXX: This is version 2 specific (only supported one for now). */
|
|
|
|
const char *rend_pk_digest =
|
|
|
|
(char *) rend_data_get_pk_digest(circuit->rend_data, NULL);
|
2004-04-13 19:16:47 +02:00
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
if (circuit->base_.purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_PROTOCOL,
|
|
|
|
"received INTRO_ESTABLISHED cell on non-intro circuit.");
|
2004-04-03 06:55:22 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2016-05-31 20:51:30 +02:00
|
|
|
service = rend_service_get_by_pk_digest(rend_pk_digest);
|
2004-04-13 19:16:47 +02:00
|
|
|
if (!service) {
|
2013-03-10 13:32:58 +01:00
|
|
|
log_warn(LD_REND, "Unknown service on introduction circuit %u.",
|
|
|
|
(unsigned)circuit->base_.n_circ_id);
|
2004-04-13 19:16:47 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2015-06-01 19:17:37 +02:00
|
|
|
/* We've just successfully established a intro circuit to one of our
|
|
|
|
* introduction point, account for it. */
|
|
|
|
intro = find_intro_point(circuit);
|
|
|
|
if (intro == NULL) {
|
|
|
|
log_warn(LD_REND,
|
|
|
|
"Introduction circuit established without a rend_intro_point_t "
|
|
|
|
"object for service %s on circuit %u",
|
|
|
|
safe_str_client(serviceid), (unsigned)circuit->base_.n_circ_id);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
intro->circuit_established = 1;
|
|
|
|
/* We might not have every introduction point ready but at this point we
|
|
|
|
* know that the descriptor needs to be uploaded. */
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
service->desc_is_dirty = time(NULL);
|
2011-11-24 15:52:38 +01:00
|
|
|
circuit_change_purpose(TO_CIRCUIT(circuit), CIRCUIT_PURPOSE_S_INTRO);
|
2004-04-03 06:55:22 +02:00
|
|
|
|
2007-11-29 16:25:04 +01:00
|
|
|
base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32 + 1,
|
2016-05-31 20:51:30 +02:00
|
|
|
rend_pk_digest, REND_SERVICE_ID_LEN);
|
2007-08-11 16:13:25 +02:00
|
|
|
log_info(LD_REND,
|
2013-03-10 13:32:58 +01:00
|
|
|
"Received INTRO_ESTABLISHED cell on circuit %u for service %s",
|
|
|
|
(unsigned)circuit->base_.n_circ_id, serviceid);
|
2007-08-11 16:13:25 +02:00
|
|
|
|
2013-01-19 04:37:16 +01:00
|
|
|
/* Getting a valid INTRODUCE_ESTABLISHED means we've successfully
|
|
|
|
* used the circ */
|
|
|
|
pathbias_mark_use_success(circuit);
|
|
|
|
|
2004-04-03 06:55:22 +02:00
|
|
|
return 0;
|
|
|
|
err:
|
2006-10-09 17:47:27 +02:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circuit), END_CIRC_REASON_TORPROTOCOL);
|
2004-04-03 06:55:22 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Called once a circuit to a rendezvous point is established: sends a
|
2004-04-02 00:21:01 +02:00
|
|
|
* RELAY_COMMAND_RENDEZVOUS1 cell.
|
|
|
|
*/
|
|
|
|
void
|
2006-07-23 09:37:35 +02:00
|
|
|
rend_service_rendezvous_has_opened(origin_circuit_t *circuit)
|
2004-04-02 00:21:01 +02:00
|
|
|
{
|
|
|
|
rend_service_t *service;
|
|
|
|
char buf[RELAY_PAYLOAD_SIZE];
|
|
|
|
crypt_path_t *hop;
|
2007-11-29 16:25:04 +01:00
|
|
|
char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
|
2004-04-03 02:58:54 +02:00
|
|
|
char hexcookie[9];
|
2006-10-09 17:47:50 +02:00
|
|
|
int reason;
|
2016-05-31 20:51:30 +02:00
|
|
|
const char *rend_cookie, *rend_pk_digest;
|
2004-04-02 00:21:01 +02:00
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
tor_assert(circuit->base_.purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(circuit->cpath);
|
|
|
|
tor_assert(circuit->build_state);
|
2016-07-14 06:04:02 +02:00
|
|
|
assert_circ_anonymity_ok(circuit, get_options());
|
2008-09-24 16:44:29 +02:00
|
|
|
tor_assert(circuit->rend_data);
|
2012-11-19 19:45:47 +01:00
|
|
|
|
2016-05-31 20:51:30 +02:00
|
|
|
/* XXX: This is version 2 specific (only one supported). */
|
|
|
|
rend_pk_digest = (char *) rend_data_get_pk_digest(circuit->rend_data,
|
|
|
|
NULL);
|
|
|
|
rend_cookie = circuit->rend_data->rend_cookie;
|
|
|
|
|
2012-11-19 19:45:47 +01:00
|
|
|
/* Declare the circuit dirty to avoid reuse, and for path-bias */
|
2012-12-10 08:47:04 +01:00
|
|
|
if (!circuit->base_.timestamp_dirty)
|
2012-12-08 02:47:23 +01:00
|
|
|
circuit->base_.timestamp_dirty = time(NULL);
|
2012-11-19 19:45:47 +01:00
|
|
|
|
2013-01-19 04:37:16 +01:00
|
|
|
/* This may be redundant */
|
|
|
|
pathbias_count_use_attempt(circuit);
|
|
|
|
|
2012-01-07 07:44:20 +01:00
|
|
|
hop = circuit->build_state->service_pending_final_cpath_ref->cpath;
|
2004-04-02 00:21:01 +02:00
|
|
|
|
2016-05-31 20:51:30 +02:00
|
|
|
base16_encode(hexcookie,9, rend_cookie,4);
|
2007-11-29 16:25:04 +01:00
|
|
|
base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
|
2016-05-31 20:51:30 +02:00
|
|
|
rend_pk_digest, REND_SERVICE_ID_LEN);
|
2004-04-03 02:58:54 +02:00
|
|
|
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_REND,
|
2013-03-10 13:32:58 +01:00
|
|
|
"Done building circuit %u to rendezvous with "
|
2006-02-13 11:33:00 +01:00
|
|
|
"cookie %s for service %s",
|
2013-03-10 13:32:58 +01:00
|
|
|
(unsigned)circuit->base_.n_circ_id, hexcookie, serviceid);
|
2016-07-14 06:04:02 +02:00
|
|
|
circuit_log_path(LOG_INFO, LD_REND, circuit);
|
2004-04-03 02:58:54 +02:00
|
|
|
|
2011-12-24 13:47:30 +01:00
|
|
|
/* Clear the 'in-progress HS circ has timed out' flag for
|
|
|
|
* consistency with what happens on the client side; this line has
|
|
|
|
* no effect on Tor's behaviour. */
|
|
|
|
circuit->hs_circ_has_timed_out = 0;
|
|
|
|
|
2012-01-07 07:44:20 +01:00
|
|
|
/* If hop is NULL, another rend circ has already connected to this
|
|
|
|
* rend point. Close this circ. */
|
|
|
|
if (hop == NULL) {
|
|
|
|
log_info(LD_REND, "Another rend circ has already reached this rend point; "
|
|
|
|
"closing this rend circ.");
|
|
|
|
reason = END_CIRC_REASON_NONE;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove our final cpath element from the reference, so that no
|
|
|
|
* other circuit will try to use it. Store it in
|
|
|
|
* pending_final_cpath for now to ensure that it will be freed if
|
|
|
|
* our rendezvous attempt fails. */
|
|
|
|
circuit->build_state->pending_final_cpath = hop;
|
|
|
|
circuit->build_state->service_pending_final_cpath_ref->cpath = NULL;
|
|
|
|
|
2016-05-31 20:51:30 +02:00
|
|
|
service = rend_service_get_by_pk_digest(rend_pk_digest);
|
2004-04-02 00:21:01 +02:00
|
|
|
if (!service) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_GENERAL, "Internal error: unrecognized service ID on "
|
2012-01-09 18:21:04 +01:00
|
|
|
"rendezvous circuit.");
|
2006-10-09 17:47:50 +02:00
|
|
|
reason = END_CIRC_REASON_INTERNAL;
|
2004-04-02 00:21:01 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All we need to do is send a RELAY_RENDEZVOUS1 cell... */
|
2016-05-31 20:51:30 +02:00
|
|
|
memcpy(buf, rend_cookie, REND_COOKIE_LEN);
|
2012-12-05 03:27:07 +01:00
|
|
|
if (crypto_dh_get_public(hop->rend_dh_handshake_state,
|
2004-04-02 00:21:01 +02:00
|
|
|
buf+REND_COOKIE_LEN, DH_KEY_LEN)<0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_GENERAL,"Couldn't get DH public key.");
|
2006-10-09 17:47:50 +02:00
|
|
|
reason = END_CIRC_REASON_INTERNAL;
|
2004-04-02 00:21:01 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2012-12-05 03:39:54 +01:00
|
|
|
memcpy(buf+REND_COOKIE_LEN+DH_KEY_LEN, hop->rend_circ_nonce,
|
2004-04-03 04:40:30 +02:00
|
|
|
DIGEST_LEN);
|
2004-04-02 00:21:01 +02:00
|
|
|
|
|
|
|
/* Send the cell */
|
2007-03-24 16:57:51 +01:00
|
|
|
if (relay_send_command_from_edge(0, TO_CIRCUIT(circuit),
|
2006-07-23 09:37:35 +02:00
|
|
|
RELAY_COMMAND_RENDEZVOUS1,
|
2004-04-05 23:39:47 +02:00
|
|
|
buf, REND_COOKIE_LEN+DH_KEY_LEN+DIGEST_LEN,
|
2004-04-02 00:21:01 +02:00
|
|
|
circuit->cpath->prev)<0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_GENERAL, "Couldn't send RENDEZVOUS1 cell.");
|
2016-10-16 22:08:26 +02:00
|
|
|
goto done;
|
2004-04-02 00:21:01 +02:00
|
|
|
}
|
|
|
|
|
2012-12-05 03:27:07 +01:00
|
|
|
crypto_dh_free(hop->rend_dh_handshake_state);
|
|
|
|
hop->rend_dh_handshake_state = NULL;
|
2004-04-06 22:23:58 +02:00
|
|
|
|
2004-04-02 00:21:01 +02:00
|
|
|
/* Append the cpath entry. */
|
2004-04-06 01:40:59 +02:00
|
|
|
hop->state = CPATH_STATE_OPEN;
|
2004-04-06 23:25:11 +02:00
|
|
|
/* set the windows to default. these are the windows
|
2016-01-26 03:48:31 +01:00
|
|
|
* that the service thinks the client has.
|
2004-04-06 23:25:11 +02:00
|
|
|
*/
|
2009-09-17 07:58:39 +02:00
|
|
|
hop->package_window = circuit_initial_package_window();
|
2004-04-06 23:25:11 +02:00
|
|
|
hop->deliver_window = CIRCWINDOW_START;
|
|
|
|
|
2004-04-02 00:21:01 +02:00
|
|
|
onion_append_to_cpath(&circuit->cpath, hop);
|
|
|
|
circuit->build_state->pending_final_cpath = NULL; /* prevent double-free */
|
|
|
|
|
|
|
|
/* Change the circuit purpose. */
|
2011-11-24 15:52:38 +01:00
|
|
|
circuit_change_purpose(TO_CIRCUIT(circuit), CIRCUIT_PURPOSE_S_REND_JOINED);
|
2004-04-02 00:21:01 +02:00
|
|
|
|
2012-06-16 05:54:45 +02:00
|
|
|
goto done;
|
|
|
|
|
2004-04-02 00:21:01 +02:00
|
|
|
err:
|
2006-10-09 17:47:50 +02:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circuit), reason);
|
2012-06-16 05:54:45 +02:00
|
|
|
done:
|
2012-11-08 23:00:36 +01:00
|
|
|
memwipe(buf, 0, sizeof(buf));
|
|
|
|
memwipe(serviceid, 0, sizeof(serviceid));
|
|
|
|
memwipe(hexcookie, 0, sizeof(hexcookie));
|
2012-06-16 05:54:45 +02:00
|
|
|
|
|
|
|
return;
|
2004-04-02 00:21:01 +02:00
|
|
|
}
|
|
|
|
|
2004-05-10 06:34:48 +02:00
|
|
|
/*
|
2004-04-01 05:34:05 +02:00
|
|
|
* Manage introduction points
|
2004-05-10 06:34:48 +02:00
|
|
|
*/
|
2004-04-01 05:23:28 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Return the (possibly non-open) introduction circuit ending at
|
2009-05-03 00:09:18 +02:00
|
|
|
* <b>intro</b> for the service whose public key is <b>pk_digest</b>.
|
|
|
|
* (<b>desc_version</b> is ignored). Return NULL if no such service is
|
|
|
|
* found.
|
2004-04-06 05:44:36 +02:00
|
|
|
*/
|
2006-07-23 09:37:35 +02:00
|
|
|
static origin_circuit_t *
|
2009-05-03 01:04:25 +02:00
|
|
|
find_intro_circuit(rend_intro_point_t *intro, const char *pk_digest)
|
2004-04-03 06:55:22 +02:00
|
|
|
{
|
2006-07-26 21:05:41 +02:00
|
|
|
origin_circuit_t *circ = NULL;
|
2004-04-03 06:55:22 +02:00
|
|
|
|
2007-12-21 10:28:22 +01:00
|
|
|
tor_assert(intro);
|
2016-05-31 20:51:30 +02:00
|
|
|
while ((circ = circuit_get_next_by_pk_and_purpose(circ,
|
|
|
|
(uint8_t *) pk_digest, CIRCUIT_PURPOSE_S_INTRO))) {
|
2011-05-10 22:23:43 +02:00
|
|
|
if (tor_memeq(circ->build_state->chosen_exit->identity_digest,
|
2008-06-12 00:46:31 +02:00
|
|
|
intro->extend_info->identity_digest, DIGEST_LEN) &&
|
2009-05-03 00:09:18 +02:00
|
|
|
circ->rend_data) {
|
2006-07-26 21:05:41 +02:00
|
|
|
return circ;
|
2004-04-03 06:55:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
circ = NULL;
|
2016-05-31 20:51:30 +02:00
|
|
|
while ((circ = circuit_get_next_by_pk_and_purpose(circ,
|
|
|
|
(uint8_t *) pk_digest,
|
|
|
|
CIRCUIT_PURPOSE_S_ESTABLISH_INTRO))) {
|
2011-05-10 22:23:43 +02:00
|
|
|
if (tor_memeq(circ->build_state->chosen_exit->identity_digest,
|
2008-06-12 00:46:31 +02:00
|
|
|
intro->extend_info->identity_digest, DIGEST_LEN) &&
|
2009-05-03 00:09:18 +02:00
|
|
|
circ->rend_data) {
|
2006-07-26 21:05:41 +02:00
|
|
|
return circ;
|
2004-04-03 06:55:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-15 23:11:57 +02:00
|
|
|
/** Return the corresponding introdution point using the circuit <b>circ</b>
|
|
|
|
* found in the <b>service</b>. NULL is returned if not found. */
|
|
|
|
static rend_intro_point_t *
|
|
|
|
find_expiring_intro_point(rend_service_t *service, origin_circuit_t *circ)
|
|
|
|
{
|
|
|
|
tor_assert(service);
|
|
|
|
tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO ||
|
|
|
|
TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_INTRO);
|
|
|
|
|
2015-11-23 15:02:54 +01:00
|
|
|
SMARTLIST_FOREACH(service->expiring_nodes, rend_intro_point_t *,
|
|
|
|
intro_point,
|
2015-06-15 23:11:57 +02:00
|
|
|
if (crypto_pk_eq_keys(intro_point->intro_key, circ->intro_key)) {
|
|
|
|
return intro_point;
|
|
|
|
});
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-10-06 08:52:14 +02:00
|
|
|
/** Return a pointer to the rend_intro_point_t corresponding to the
|
|
|
|
* service-side introduction circuit <b>circ</b>. */
|
|
|
|
static rend_intro_point_t *
|
|
|
|
find_intro_point(origin_circuit_t *circ)
|
|
|
|
{
|
|
|
|
const char *serviceid;
|
|
|
|
rend_service_t *service = NULL;
|
|
|
|
|
|
|
|
tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO ||
|
|
|
|
TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_INTRO);
|
|
|
|
tor_assert(circ->rend_data);
|
2016-05-31 20:51:30 +02:00
|
|
|
serviceid = rend_data_get_address(circ->rend_data);
|
2011-10-06 08:52:14 +02:00
|
|
|
|
|
|
|
SMARTLIST_FOREACH(rend_service_list, rend_service_t *, s,
|
|
|
|
if (tor_memeq(s->service_id, serviceid, REND_SERVICE_ID_LEN_BASE32)) {
|
|
|
|
service = s;
|
|
|
|
break;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (service == NULL) return NULL;
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(service->intro_nodes, rend_intro_point_t *, intro_point,
|
2012-09-15 12:52:13 +02:00
|
|
|
if (crypto_pk_eq_keys(intro_point->intro_key, circ->intro_key)) {
|
2011-10-06 08:52:14 +02:00
|
|
|
return intro_point;
|
|
|
|
});
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-03-22 14:31:53 +01:00
|
|
|
/** Upload the rend_encoded_v2_service_descriptor_t's in <b>descs</b>
|
|
|
|
* associated with the rend_service_descriptor_t <b>renddesc</b> to
|
|
|
|
* the responsible hidden service directories OR the hidden service
|
|
|
|
* directories specified by <b>hs_dirs</b>; <b>service_id</b> and
|
|
|
|
* <b>seconds_valid</b> are only passed for logging purposes.
|
|
|
|
*/
|
|
|
|
void
|
2008-09-09 10:41:58 +02:00
|
|
|
directory_post_to_hs_dir(rend_service_descriptor_t *renddesc,
|
2015-03-22 14:31:53 +01:00
|
|
|
smartlist_t *descs, smartlist_t *hs_dirs,
|
|
|
|
const char *service_id, int seconds_valid)
|
2008-01-26 01:42:13 +01:00
|
|
|
{
|
2008-09-09 10:41:58 +02:00
|
|
|
int i, j, failed_upload = 0;
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *responsible_dirs = smartlist_new();
|
|
|
|
smartlist_t *successful_uploads = smartlist_new();
|
2008-01-26 01:42:13 +01:00
|
|
|
routerstatus_t *hs_dir;
|
|
|
|
for (i = 0; i < smartlist_len(descs); i++) {
|
|
|
|
rend_encoded_v2_service_descriptor_t *desc = smartlist_get(descs, i);
|
2015-03-22 14:31:53 +01:00
|
|
|
/** If any HSDirs are specified, they should be used instead of
|
|
|
|
* the responsible directories */
|
|
|
|
if (hs_dirs && smartlist_len(hs_dirs) > 0) {
|
|
|
|
smartlist_add_all(responsible_dirs, hs_dirs);
|
|
|
|
} else {
|
|
|
|
/* Determine responsible dirs. */
|
|
|
|
if (hid_serv_get_responsible_directories(responsible_dirs,
|
|
|
|
desc->desc_id) < 0) {
|
|
|
|
log_warn(LD_REND, "Could not determine the responsible hidden service "
|
|
|
|
"directories to post descriptors to.");
|
|
|
|
control_event_hs_descriptor_upload(service_id,
|
|
|
|
"UNKNOWN",
|
|
|
|
"UNKNOWN");
|
|
|
|
goto done;
|
|
|
|
}
|
2008-01-26 01:42:13 +01:00
|
|
|
}
|
|
|
|
for (j = 0; j < smartlist_len(responsible_dirs); j++) {
|
|
|
|
char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
|
2009-10-27 07:09:10 +01:00
|
|
|
char *hs_dir_ip;
|
2011-07-16 00:53:29 +02:00
|
|
|
const node_t *node;
|
2015-05-29 23:45:45 +02:00
|
|
|
rend_data_t *rend_data;
|
2008-01-26 01:42:13 +01:00
|
|
|
hs_dir = smartlist_get(responsible_dirs, j);
|
2012-04-11 18:50:50 +02:00
|
|
|
if (smartlist_contains_digest(renddesc->successful_uploads,
|
2008-09-09 10:41:58 +02:00
|
|
|
hs_dir->identity_digest))
|
|
|
|
/* Don't upload descriptor if we succeeded in doing so last time. */
|
|
|
|
continue;
|
2011-07-16 00:53:29 +02:00
|
|
|
node = node_get_by_id(hs_dir->identity_digest);
|
|
|
|
if (!node || !node_has_descriptor(node)) {
|
2012-06-15 21:25:46 +02:00
|
|
|
log_info(LD_REND, "Not launching upload for for v2 descriptor to "
|
2011-05-16 03:58:46 +02:00
|
|
|
"hidden service directory %s; we don't have its "
|
2009-05-27 23:55:51 +02:00
|
|
|
"router descriptor. Queuing for later upload.",
|
2011-05-16 03:58:46 +02:00
|
|
|
safe_str_client(routerstatus_describe(hs_dir)));
|
2008-09-09 10:41:58 +02:00
|
|
|
failed_upload = -1;
|
|
|
|
continue;
|
|
|
|
}
|
2008-01-26 01:42:13 +01:00
|
|
|
/* Send publish request. */
|
2015-05-29 23:45:45 +02:00
|
|
|
|
|
|
|
/* We need the service ID to identify which service did the upload
|
|
|
|
* request. Lookup is made in rend_service_desc_has_uploaded(). */
|
|
|
|
rend_data = rend_data_client_create(service_id, desc->desc_id, NULL,
|
|
|
|
REND_NO_AUTH);
|
|
|
|
directory_initiate_command_routerstatus_rend(hs_dir,
|
2015-07-16 17:11:21 +02:00
|
|
|
DIR_PURPOSE_UPLOAD_RENDDESC_V2,
|
2015-05-29 23:45:45 +02:00
|
|
|
ROUTER_PURPOSE_GENERAL,
|
|
|
|
DIRIND_ANONYMOUS, NULL,
|
|
|
|
desc->desc_str,
|
|
|
|
strlen(desc->desc_str),
|
2016-11-21 23:23:25 +01:00
|
|
|
0, rend_data, NULL);
|
2015-05-29 23:45:45 +02:00
|
|
|
rend_data_free(rend_data);
|
2008-01-26 01:42:13 +01:00
|
|
|
base32_encode(desc_id_base32, sizeof(desc_id_base32),
|
|
|
|
desc->desc_id, DIGEST_LEN);
|
2009-10-27 07:09:10 +01:00
|
|
|
hs_dir_ip = tor_dup_ip(hs_dir->addr);
|
2012-06-15 21:25:46 +02:00
|
|
|
log_info(LD_REND, "Launching upload for v2 descriptor for "
|
2008-01-26 01:42:13 +01:00
|
|
|
"service '%s' with descriptor ID '%s' with validity "
|
|
|
|
"of %d seconds to hidden service directory '%s' on "
|
2009-10-27 07:09:10 +01:00
|
|
|
"%s:%d.",
|
2009-12-15 23:23:36 +01:00
|
|
|
safe_str_client(service_id),
|
|
|
|
safe_str_client(desc_id_base32),
|
2008-01-26 01:42:13 +01:00
|
|
|
seconds_valid,
|
|
|
|
hs_dir->nickname,
|
2009-10-27 07:09:10 +01:00
|
|
|
hs_dir_ip,
|
|
|
|
hs_dir->or_port);
|
2015-03-22 14:31:53 +01:00
|
|
|
control_event_hs_descriptor_upload(service_id,
|
|
|
|
hs_dir->identity_digest,
|
|
|
|
desc_id_base32);
|
2009-10-27 07:09:10 +01:00
|
|
|
tor_free(hs_dir_ip);
|
2008-09-09 10:41:58 +02:00
|
|
|
/* Remember successful upload to this router for next time. */
|
2012-04-11 18:59:57 +02:00
|
|
|
if (!smartlist_contains_digest(successful_uploads,
|
|
|
|
hs_dir->identity_digest))
|
2008-09-09 10:41:58 +02:00
|
|
|
smartlist_add(successful_uploads, hs_dir->identity_digest);
|
2008-01-26 01:42:13 +01:00
|
|
|
}
|
|
|
|
smartlist_clear(responsible_dirs);
|
|
|
|
}
|
2008-09-09 10:41:58 +02:00
|
|
|
if (!failed_upload) {
|
|
|
|
if (renddesc->successful_uploads) {
|
|
|
|
SMARTLIST_FOREACH(renddesc->successful_uploads, char *, c, tor_free(c););
|
|
|
|
smartlist_free(renddesc->successful_uploads);
|
2009-03-18 15:35:24 +01:00
|
|
|
renddesc->successful_uploads = NULL;
|
2008-09-09 10:41:58 +02:00
|
|
|
}
|
2008-09-09 11:21:21 +02:00
|
|
|
renddesc->all_uploads_performed = 1;
|
2008-09-09 10:41:58 +02:00
|
|
|
} else {
|
|
|
|
/* Remember which routers worked this time, so that we don't upload the
|
|
|
|
* descriptor to them again. */
|
|
|
|
if (!renddesc->successful_uploads)
|
2012-01-18 21:53:30 +01:00
|
|
|
renddesc->successful_uploads = smartlist_new();
|
2009-03-18 15:35:24 +01:00
|
|
|
SMARTLIST_FOREACH(successful_uploads, const char *, c, {
|
2012-04-11 18:50:50 +02:00
|
|
|
if (!smartlist_contains_digest(renddesc->successful_uploads, c)) {
|
2009-03-18 15:35:24 +01:00
|
|
|
char *hsdir_id = tor_memdup(c, DIGEST_LEN);
|
2008-09-09 10:41:58 +02:00
|
|
|
smartlist_add(renddesc->successful_uploads, hsdir_id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-03-22 14:31:53 +01:00
|
|
|
done:
|
2008-01-26 01:42:13 +01:00
|
|
|
smartlist_free(responsible_dirs);
|
2008-09-09 10:41:58 +02:00
|
|
|
smartlist_free(successful_uploads);
|
2008-01-26 01:42:13 +01:00
|
|
|
}
|
|
|
|
|
2009-05-02 23:31:58 +02:00
|
|
|
/** Encode and sign an up-to-date service descriptor for <b>service</b>,
|
|
|
|
* and upload it/them to the responsible hidden service directories.
|
2004-05-05 23:32:43 +02:00
|
|
|
*/
|
2004-04-13 19:16:47 +02:00
|
|
|
static void
|
2007-09-18 23:17:45 +02:00
|
|
|
upload_service_descriptor(rend_service_t *service)
|
2004-04-13 19:16:47 +02:00
|
|
|
{
|
2007-10-31 21:48:06 +01:00
|
|
|
time_t now = time(NULL);
|
|
|
|
int rendpostperiod;
|
2007-11-29 16:25:04 +01:00
|
|
|
char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
|
2007-10-31 21:48:06 +01:00
|
|
|
int uploaded = 0;
|
2004-04-13 19:16:47 +02:00
|
|
|
|
2007-10-31 21:48:06 +01:00
|
|
|
rendpostperiod = get_options()->RendPostPeriod;
|
|
|
|
|
2015-07-29 14:22:32 +02:00
|
|
|
networkstatus_t *c = networkstatus_get_latest_consensus();
|
|
|
|
if (c && smartlist_len(c->routerstatus_list) > 0) {
|
|
|
|
int seconds_valid, i, j, num_descs;
|
|
|
|
smartlist_t *descs = smartlist_new();
|
|
|
|
smartlist_t *client_cookies = smartlist_new();
|
|
|
|
/* Either upload a single descriptor (including replicas) or one
|
|
|
|
* descriptor for each authorized client in case of authorization
|
|
|
|
* type 'stealth'. */
|
|
|
|
num_descs = service->auth_type == REND_STEALTH_AUTH ?
|
|
|
|
smartlist_len(service->clients) : 1;
|
|
|
|
for (j = 0; j < num_descs; j++) {
|
|
|
|
crypto_pk_t *client_key = NULL;
|
|
|
|
rend_authorized_client_t *client = NULL;
|
|
|
|
smartlist_clear(client_cookies);
|
|
|
|
switch (service->auth_type) {
|
|
|
|
case REND_NO_AUTH:
|
|
|
|
/* Do nothing here. */
|
|
|
|
break;
|
|
|
|
case REND_BASIC_AUTH:
|
|
|
|
SMARTLIST_FOREACH(service->clients, rend_authorized_client_t *,
|
|
|
|
cl, smartlist_add(client_cookies, cl->descriptor_cookie));
|
|
|
|
break;
|
|
|
|
case REND_STEALTH_AUTH:
|
|
|
|
client = smartlist_get(service->clients, j);
|
|
|
|
client_key = client->client_key;
|
|
|
|
smartlist_add(client_cookies, client->descriptor_cookie);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Encode the current descriptor. */
|
|
|
|
seconds_valid = rend_encode_v2_descriptors(descs, service->desc,
|
|
|
|
now, 0,
|
|
|
|
service->auth_type,
|
|
|
|
client_key,
|
|
|
|
client_cookies);
|
|
|
|
if (seconds_valid < 0) {
|
|
|
|
log_warn(LD_BUG, "Internal error: couldn't encode service "
|
|
|
|
"descriptor; not uploading.");
|
|
|
|
smartlist_free(descs);
|
|
|
|
smartlist_free(client_cookies);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rend_get_service_id(service->desc->pk, serviceid);
|
2015-07-29 15:20:51 +02:00
|
|
|
if (get_options()->PublishHidServDescriptors) {
|
|
|
|
/* Post the current descriptors to the hidden service directories. */
|
|
|
|
log_info(LD_REND, "Launching upload for hidden service %s",
|
|
|
|
serviceid);
|
|
|
|
directory_post_to_hs_dir(service->desc, descs, NULL, serviceid,
|
|
|
|
seconds_valid);
|
|
|
|
}
|
2015-07-29 14:22:32 +02:00
|
|
|
/* Free memory for descriptors. */
|
|
|
|
for (i = 0; i < smartlist_len(descs); i++)
|
|
|
|
rend_encoded_v2_service_descriptor_free(smartlist_get(descs, i));
|
|
|
|
smartlist_clear(descs);
|
|
|
|
/* Update next upload time. */
|
|
|
|
if (seconds_valid - REND_TIME_PERIOD_OVERLAPPING_V2_DESCS
|
|
|
|
> rendpostperiod)
|
|
|
|
service->next_upload_time = now + rendpostperiod;
|
|
|
|
else if (seconds_valid < REND_TIME_PERIOD_OVERLAPPING_V2_DESCS)
|
|
|
|
service->next_upload_time = now + seconds_valid + 1;
|
|
|
|
else
|
|
|
|
service->next_upload_time = now + seconds_valid -
|
|
|
|
REND_TIME_PERIOD_OVERLAPPING_V2_DESCS + 1;
|
|
|
|
/* Post also the next descriptors, if necessary. */
|
|
|
|
if (seconds_valid < REND_TIME_PERIOD_OVERLAPPING_V2_DESCS) {
|
2007-12-15 21:28:09 +01:00
|
|
|
seconds_valid = rend_encode_v2_descriptors(descs, service->desc,
|
2015-07-29 14:22:32 +02:00
|
|
|
now, 1,
|
2008-08-19 17:41:28 +02:00
|
|
|
service->auth_type,
|
|
|
|
client_key,
|
|
|
|
client_cookies);
|
2007-10-31 21:48:06 +01:00
|
|
|
if (seconds_valid < 0) {
|
|
|
|
log_warn(LD_BUG, "Internal error: couldn't encode service "
|
|
|
|
"descriptor; not uploading.");
|
2007-12-15 21:50:56 +01:00
|
|
|
smartlist_free(descs);
|
2008-08-19 17:41:28 +02:00
|
|
|
smartlist_free(client_cookies);
|
2007-10-31 21:48:06 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-07-29 15:20:51 +02:00
|
|
|
if (get_options()->PublishHidServDescriptors) {
|
|
|
|
directory_post_to_hs_dir(service->desc, descs, NULL, serviceid,
|
|
|
|
seconds_valid);
|
|
|
|
}
|
2007-10-31 21:48:06 +01:00
|
|
|
/* Free memory for descriptors. */
|
2007-12-15 21:50:56 +01:00
|
|
|
for (i = 0; i < smartlist_len(descs); i++)
|
2007-12-15 21:28:09 +01:00
|
|
|
rend_encoded_v2_service_descriptor_free(smartlist_get(descs, i));
|
2008-08-19 17:41:28 +02:00
|
|
|
smartlist_clear(descs);
|
2007-10-31 21:48:06 +01:00
|
|
|
}
|
|
|
|
}
|
2015-07-29 14:22:32 +02:00
|
|
|
smartlist_free(descs);
|
|
|
|
smartlist_free(client_cookies);
|
|
|
|
uploaded = 1;
|
2015-07-29 15:20:51 +02:00
|
|
|
if (get_options()->PublishHidServDescriptors) {
|
2007-10-31 21:48:06 +01:00
|
|
|
log_info(LD_REND, "Successfully uploaded v2 rend descriptors!");
|
2015-07-29 15:20:51 +02:00
|
|
|
} else {
|
|
|
|
log_info(LD_REND, "Successfully stored created v2 rend descriptors!");
|
2007-10-31 21:48:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If not uploaded, try again in one minute. */
|
|
|
|
if (!uploaded)
|
|
|
|
service->next_upload_time = now + 60;
|
2004-04-13 19:16:47 +02:00
|
|
|
|
2007-10-31 21:48:06 +01:00
|
|
|
/* Unmark dirty flag of this service. */
|
2004-04-13 19:16:47 +02:00
|
|
|
service->desc_is_dirty = 0;
|
|
|
|
}
|
|
|
|
|
2012-01-07 13:41:46 +01:00
|
|
|
/** Return the number of INTRODUCE2 cells this hidden service has received
|
|
|
|
* from this intro point. */
|
2011-12-13 12:36:48 +01:00
|
|
|
static int
|
|
|
|
intro_point_accepted_intro_count(rend_intro_point_t *intro)
|
|
|
|
{
|
2012-07-11 04:18:47 +02:00
|
|
|
return intro->accepted_introduce2_count;
|
2011-12-13 12:36:48 +01:00
|
|
|
}
|
|
|
|
|
2011-10-16 00:40:28 +02:00
|
|
|
/** Return non-zero iff <b>intro</b> should 'expire' now (i.e. we
|
|
|
|
* should stop publishing it in new descriptors and eventually close
|
2011-10-29 03:35:55 +02:00
|
|
|
* it). */
|
2011-10-16 00:40:28 +02:00
|
|
|
static int
|
|
|
|
intro_point_should_expire_now(rend_intro_point_t *intro,
|
|
|
|
time_t now)
|
|
|
|
{
|
2011-10-29 03:35:55 +02:00
|
|
|
tor_assert(intro != NULL);
|
|
|
|
|
|
|
|
if (intro->time_published == -1) {
|
|
|
|
/* Don't expire an intro point if we haven't even published it yet. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-13 12:36:48 +01:00
|
|
|
if (intro_point_accepted_intro_count(intro) >=
|
2015-04-20 21:51:06 +02:00
|
|
|
intro->max_introductions) {
|
2011-10-29 03:35:55 +02:00
|
|
|
/* This intro point has been used too many times. Expire it now. */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (intro->time_to_expire == -1) {
|
|
|
|
/* This intro point has been published, but we haven't picked an
|
|
|
|
* expiration time for it. Pick one now. */
|
|
|
|
int intro_point_lifetime_seconds =
|
2015-04-21 16:17:12 +02:00
|
|
|
crypto_rand_int_range(INTRO_POINT_LIFETIME_MIN_SECONDS,
|
|
|
|
INTRO_POINT_LIFETIME_MAX_SECONDS);
|
2011-10-29 03:35:55 +02:00
|
|
|
|
|
|
|
/* Start the expiration timer now, rather than when the intro
|
|
|
|
* point was first published. There shouldn't be much of a time
|
|
|
|
* difference. */
|
|
|
|
intro->time_to_expire = now + intro_point_lifetime_seconds;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This intro point has a time to expire set already. Use it. */
|
|
|
|
return (now >= intro->time_to_expire);
|
2011-10-16 00:40:28 +02:00
|
|
|
}
|
|
|
|
|
2015-05-29 23:45:45 +02:00
|
|
|
/** Iterate over intro points in the given service and remove the invalid
|
|
|
|
* ones. For an intro point object to be considered invalid, the circuit
|
2015-06-01 18:08:13 +02:00
|
|
|
* _and_ node need to have disappeared.
|
2015-05-29 23:45:45 +02:00
|
|
|
*
|
|
|
|
* If the intro point should expire, it's placed into the expiring_nodes
|
|
|
|
* list of the service and removed from the active intro nodes list.
|
|
|
|
*
|
2015-06-01 18:08:13 +02:00
|
|
|
* If <b>exclude_nodes</b> is not NULL, add the valid nodes to it.
|
|
|
|
*
|
|
|
|
* If <b>retry_nodes</b> is not NULL, add the valid node to it if the
|
|
|
|
* circuit disappeared but the node is still in the consensus. */
|
2015-05-29 23:45:45 +02:00
|
|
|
static void
|
|
|
|
remove_invalid_intro_points(rend_service_t *service,
|
2015-06-01 18:08:13 +02:00
|
|
|
smartlist_t *exclude_nodes,
|
|
|
|
smartlist_t *retry_nodes, time_t now)
|
2015-05-29 23:45:45 +02:00
|
|
|
{
|
|
|
|
tor_assert(service);
|
|
|
|
|
2017-02-01 17:30:46 +01:00
|
|
|
/* Remove any expired nodes that doesn't have a circuit. */
|
|
|
|
SMARTLIST_FOREACH_BEGIN(service->expiring_nodes, rend_intro_point_t *,
|
|
|
|
intro) {
|
|
|
|
origin_circuit_t *intro_circ =
|
|
|
|
find_intro_circuit(intro, service->pk_digest);
|
|
|
|
if (intro_circ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* No more circuit, cleanup the into point object. */
|
|
|
|
SMARTLIST_DEL_CURRENT(service->expiring_nodes, intro);
|
|
|
|
rend_intro_point_free(intro);
|
|
|
|
} SMARTLIST_FOREACH_END(intro);
|
|
|
|
|
2015-05-29 23:45:45 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(service->intro_nodes, rend_intro_point_t *,
|
|
|
|
intro) {
|
|
|
|
/* Find the introduction point node object. */
|
|
|
|
const node_t *node =
|
|
|
|
node_get_by_id(intro->extend_info->identity_digest);
|
|
|
|
/* Find the intro circuit, this might be NULL. */
|
|
|
|
origin_circuit_t *intro_circ =
|
|
|
|
find_intro_circuit(intro, service->pk_digest);
|
|
|
|
|
2015-06-01 18:08:13 +02:00
|
|
|
/* Add the valid node to the exclusion list so we don't try to establish
|
|
|
|
* an introduction point to it again. */
|
|
|
|
if (node && exclude_nodes) {
|
|
|
|
smartlist_add(exclude_nodes, (void*) node);
|
|
|
|
}
|
|
|
|
|
2015-05-29 23:45:45 +02:00
|
|
|
/* First, make sure we still have a valid circuit for this intro point.
|
|
|
|
* If we dont, we'll give up on it and make a new one. */
|
|
|
|
if (intro_circ == NULL) {
|
2015-06-29 17:12:25 +02:00
|
|
|
log_info(LD_REND, "Attempting to retry on %s as intro point for %s"
|
2015-05-29 23:45:45 +02:00
|
|
|
" (circuit disappeared).",
|
|
|
|
safe_str_client(extend_info_describe(intro->extend_info)),
|
|
|
|
safe_str_client(service->service_id));
|
2015-06-01 19:17:37 +02:00
|
|
|
/* We've lost the circuit for this intro point, flag it so it can be
|
|
|
|
* accounted for when considiring uploading a descriptor. */
|
|
|
|
intro->circuit_established = 0;
|
|
|
|
|
2015-06-01 18:08:13 +02:00
|
|
|
/* Node is gone or we've reached our maximum circuit creationg retry
|
|
|
|
* count, clean up everything, we'll find a new one. */
|
|
|
|
if (node == NULL ||
|
|
|
|
intro->circuit_retries >= MAX_INTRO_POINT_CIRCUIT_RETRIES) {
|
|
|
|
rend_intro_point_free(intro);
|
|
|
|
SMARTLIST_DEL_CURRENT(service->intro_nodes, intro);
|
|
|
|
/* We've just killed the intro point, nothing left to do. */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The intro point is still alive so let's try to use it again because
|
|
|
|
* we have a published descriptor containing it. Keep the intro point
|
|
|
|
* in the intro_nodes list because it's still valid, we are rebuilding
|
|
|
|
* a circuit to it. */
|
|
|
|
if (retry_nodes) {
|
|
|
|
smartlist_add(retry_nodes, intro);
|
|
|
|
}
|
2015-05-29 23:45:45 +02:00
|
|
|
}
|
|
|
|
/* else, the circuit is valid so in both cases, node being alive or not,
|
|
|
|
* we leave the circuit and intro point object as is. Closing the
|
|
|
|
* circuit here would leak new consensus timing and freeing the intro
|
|
|
|
* point object would make the intro circuit unusable. */
|
|
|
|
|
|
|
|
/* Now, check if intro point should expire. If it does, queue it so
|
|
|
|
* it can be cleaned up once it has been replaced properly. */
|
2015-06-01 18:08:13 +02:00
|
|
|
if (intro_point_should_expire_now(intro, now)) {
|
2015-05-29 23:45:45 +02:00
|
|
|
log_info(LD_REND, "Expiring %s as intro point for %s.",
|
|
|
|
safe_str_client(extend_info_describe(intro->extend_info)),
|
|
|
|
safe_str_client(service->service_id));
|
|
|
|
smartlist_add(service->expiring_nodes, intro);
|
|
|
|
SMARTLIST_DEL_CURRENT(service->intro_nodes, intro);
|
2015-06-01 19:17:37 +02:00
|
|
|
/* Intro point is expired, we need a new one thus don't consider it
|
|
|
|
* anymore has a valid established intro point. */
|
|
|
|
intro->circuit_established = 0;
|
2015-05-29 23:45:45 +02:00
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(intro);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** A new descriptor has been successfully uploaded for the given
|
|
|
|
* <b>rend_data</b>. Remove and free the expiring nodes from the associated
|
|
|
|
* service. */
|
|
|
|
void
|
|
|
|
rend_service_desc_has_uploaded(const rend_data_t *rend_data)
|
|
|
|
{
|
|
|
|
rend_service_t *service;
|
2016-05-31 20:51:30 +02:00
|
|
|
const char *onion_address;
|
2015-05-29 23:45:45 +02:00
|
|
|
|
|
|
|
tor_assert(rend_data);
|
|
|
|
|
2016-05-31 20:51:30 +02:00
|
|
|
onion_address = rend_data_get_address(rend_data);
|
|
|
|
|
|
|
|
service = rend_service_get_by_service_id(onion_address);
|
2015-05-29 23:45:45 +02:00
|
|
|
if (service == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH_BEGIN(service->expiring_nodes, rend_intro_point_t *,
|
|
|
|
intro) {
|
|
|
|
origin_circuit_t *intro_circ =
|
|
|
|
find_intro_circuit(intro, service->pk_digest);
|
|
|
|
if (intro_circ != NULL) {
|
|
|
|
circuit_mark_for_close(TO_CIRCUIT(intro_circ),
|
|
|
|
END_CIRC_REASON_FINISHED);
|
|
|
|
}
|
|
|
|
SMARTLIST_DEL_CURRENT(service->expiring_nodes, intro);
|
|
|
|
rend_intro_point_free(intro);
|
|
|
|
} SMARTLIST_FOREACH_END(intro);
|
|
|
|
}
|
|
|
|
|
2017-03-02 05:14:45 +01:00
|
|
|
/** Don't try to build more than this many circuits before giving up
|
|
|
|
* for a while. Dynamically calculated based on the configured number of
|
|
|
|
* introduction points for the service, n_intro_points_wanted. */
|
|
|
|
static int
|
|
|
|
rend_max_intro_circs_per_period(unsigned int n_intro_points_wanted)
|
|
|
|
{
|
|
|
|
/* Allow all but one of the initial connections to fail and be
|
|
|
|
* retried. (If all fail, we *want* to wait, because something is broken.) */
|
|
|
|
tor_assert(n_intro_points_wanted <= NUM_INTRO_POINTS_MAX);
|
|
|
|
return (int)(2*n_intro_points_wanted + NUM_INTRO_POINTS_EXTRA);
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** For every service, check how many intro points it currently has, and:
|
2015-05-29 23:45:45 +02:00
|
|
|
* - Invalidate introdution points based on specific criteria, see
|
|
|
|
* remove_invalid_intro_points comments.
|
2004-04-03 01:01:00 +02:00
|
|
|
* - Pick new intro points as necessary.
|
|
|
|
* - Launch circuits to any new intro points.
|
2015-05-29 23:45:45 +02:00
|
|
|
*
|
|
|
|
* This is called once a second by the main loop.
|
2004-04-03 01:01:00 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2015-07-14 08:22:23 +02:00
|
|
|
rend_consider_services_intro_points(void)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2015-05-29 23:45:45 +02:00
|
|
|
int i;
|
2004-05-18 03:53:53 +02:00
|
|
|
time_t now;
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2016-08-18 05:19:22 +02:00
|
|
|
/* Are we in single onion mode? */
|
|
|
|
const int allow_direct = rend_service_allow_non_anonymous_connection(
|
|
|
|
get_options());
|
2015-05-29 23:45:45 +02:00
|
|
|
/* List of nodes we need to _exclude_ when choosing a new node to
|
|
|
|
* establish an intro point to. */
|
2014-11-28 05:24:03 +01:00
|
|
|
smartlist_t *exclude_nodes;
|
2015-06-01 18:08:13 +02:00
|
|
|
/* List of nodes we need to retry to build a circuit on them because the
|
|
|
|
* node is valid but circuit died. */
|
|
|
|
smartlist_t *retry_nodes;
|
2004-04-01 05:23:28 +02:00
|
|
|
|
2014-11-20 18:03:46 +01:00
|
|
|
if (!have_completed_a_circuit())
|
2014-11-20 17:51:36 +01:00
|
|
|
return;
|
|
|
|
|
2014-11-28 05:24:03 +01:00
|
|
|
exclude_nodes = smartlist_new();
|
2015-06-01 18:08:13 +02:00
|
|
|
retry_nodes = smartlist_new();
|
2004-05-18 03:53:53 +02:00
|
|
|
now = time(NULL);
|
2004-04-01 05:23:28 +02:00
|
|
|
|
2015-05-29 23:45:45 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(rend_service_list, rend_service_t *, service) {
|
2015-06-01 18:08:13 +02:00
|
|
|
int r;
|
2015-05-29 23:45:45 +02:00
|
|
|
/* Number of intro points we want to open and add to the intro nodes
|
|
|
|
* list of the service. */
|
|
|
|
unsigned int n_intro_points_to_open;
|
|
|
|
/* Have an unsigned len so we can use it to compare values else gcc is
|
|
|
|
* not happy with unmatching signed comparaison. */
|
|
|
|
unsigned int intro_nodes_len;
|
|
|
|
/* Different service are allowed to have the same introduction point as
|
|
|
|
* long as they are on different circuit thus why we clear this list. */
|
2014-11-04 06:19:31 +01:00
|
|
|
smartlist_clear(exclude_nodes);
|
2015-06-01 18:08:13 +02:00
|
|
|
smartlist_clear(retry_nodes);
|
2004-04-03 02:58:54 +02:00
|
|
|
|
2017-03-02 05:33:09 +01:00
|
|
|
/* Cleanup the invalid intro points and save the node objects, if any,
|
|
|
|
* in the exclude_nodes and retry_nodes lists. */
|
|
|
|
remove_invalid_intro_points(service, exclude_nodes, retry_nodes, now);
|
|
|
|
|
2015-05-29 23:45:45 +02:00
|
|
|
/* This retry period is important here so we don't stress circuit
|
|
|
|
* creation. */
|
2017-03-04 12:46:19 +01:00
|
|
|
|
2015-05-29 23:45:45 +02:00
|
|
|
if (now > service->intro_period_started + INTRO_CIRC_RETRY_PERIOD) {
|
2017-03-04 12:46:19 +01:00
|
|
|
/* One period has elapsed:
|
|
|
|
* - if we stopped, we can try building circuits again,
|
|
|
|
* - if we haven't, we reset the circuit creation counts. */
|
|
|
|
rend_log_intro_limit(service, LOG_INFO);
|
2004-05-18 03:53:53 +02:00
|
|
|
service->intro_period_started = now;
|
|
|
|
service->n_intro_circuits_launched = 0;
|
2004-11-23 00:28:26 +01:00
|
|
|
} else if (service->n_intro_circuits_launched >=
|
2017-03-02 05:14:45 +01:00
|
|
|
rend_max_intro_circs_per_period(
|
|
|
|
service->n_intro_points_wanted)) {
|
2004-05-18 03:53:53 +02:00
|
|
|
/* We have failed too many times in this period; wait for the next
|
2017-03-02 05:33:09 +01:00
|
|
|
* one before we try to initiate any more connections. */
|
2017-03-04 12:46:19 +01:00
|
|
|
rend_log_intro_limit(service, LOG_WARN);
|
2004-05-18 03:53:53 +02:00
|
|
|
continue;
|
|
|
|
}
|
2004-04-03 01:01:00 +02:00
|
|
|
|
2015-06-01 18:08:13 +02:00
|
|
|
/* Let's try to rebuild circuit on the nodes we want to retry on. */
|
|
|
|
SMARTLIST_FOREACH_BEGIN(retry_nodes, rend_intro_point_t *, intro) {
|
|
|
|
r = rend_service_launch_establish_intro(service, intro);
|
|
|
|
if (r < 0) {
|
|
|
|
log_warn(LD_REND, "Error launching circuit to node %s for service %s.",
|
|
|
|
safe_str_client(extend_info_describe(intro->extend_info)),
|
|
|
|
safe_str_client(service->service_id));
|
|
|
|
/* Unable to launch a circuit to that intro point, remove it from
|
|
|
|
* the valid list so we can create a new one. */
|
|
|
|
smartlist_remove(service->intro_nodes, intro);
|
|
|
|
rend_intro_point_free(intro);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
intro->circuit_retries++;
|
|
|
|
} SMARTLIST_FOREACH_END(intro);
|
|
|
|
|
|
|
|
/* Avoid mismatched signed comparaison below. */
|
2015-05-29 23:45:45 +02:00
|
|
|
intro_nodes_len = (unsigned int) smartlist_len(service->intro_nodes);
|
|
|
|
|
2017-02-01 17:07:09 +01:00
|
|
|
/* Quiescent state, we have more or the equal amount of wanted node for
|
|
|
|
* this service. Proceed to the next service. We can have more nodes
|
|
|
|
* because we launch extra preemptive circuits if our intro nodes list was
|
|
|
|
* originally empty for performance reasons. */
|
|
|
|
if (intro_nodes_len >= service->n_intro_points_wanted) {
|
2004-04-03 01:01:00 +02:00
|
|
|
continue;
|
2004-05-18 03:53:53 +02:00
|
|
|
}
|
2004-04-03 01:01:00 +02:00
|
|
|
|
2017-02-01 17:07:09 +01:00
|
|
|
/* Number of intro points we want to open which is the wanted amount minus
|
|
|
|
* the current amount of valid nodes. We know that this won't underflow
|
|
|
|
* because of the check above. */
|
2015-05-29 23:45:45 +02:00
|
|
|
n_intro_points_to_open = service->n_intro_points_wanted - intro_nodes_len;
|
|
|
|
if (intro_nodes_len == 0) {
|
|
|
|
/* We want to end up with n_intro_points_wanted intro points, but if
|
|
|
|
* we have no intro points at all (chances are they all cycled or we
|
|
|
|
* are starting up), we launch NUM_INTRO_POINTS_EXTRA extra circuits
|
|
|
|
* and use the first n_intro_points_wanted that complete. See proposal
|
|
|
|
* #155, section 4 for the rationale of this which is purely for
|
|
|
|
* performance.
|
|
|
|
*
|
|
|
|
* The ones after the first n_intro_points_to_open will be converted
|
|
|
|
* to 'general' internal circuits in rend_service_intro_has_opened(),
|
|
|
|
* and then we'll drop them from the list of intro points. */
|
|
|
|
n_intro_points_to_open += NUM_INTRO_POINTS_EXTRA;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < (int) n_intro_points_to_open; i++) {
|
|
|
|
const node_t *node;
|
|
|
|
rend_intro_point_t *intro;
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
router_crn_flags_t flags = CRN_NEED_UPTIME|CRN_NEED_DESC;
|
2012-10-12 18:22:13 +02:00
|
|
|
if (get_options()->AllowInvalid_ & ALLOW_INVALID_INTRODUCTION)
|
2008-07-24 11:22:34 +02:00
|
|
|
flags |= CRN_ALLOW_INVALID;
|
2016-08-18 05:19:22 +02:00
|
|
|
router_crn_flags_t direct_flags = flags;
|
|
|
|
direct_flags |= CRN_PREF_ADDR;
|
|
|
|
direct_flags |= CRN_DIRECT_CONN;
|
|
|
|
|
2014-11-04 06:19:31 +01:00
|
|
|
node = router_choose_random_node(exclude_nodes,
|
2016-08-18 05:19:22 +02:00
|
|
|
options->ExcludeNodes,
|
|
|
|
allow_direct ? direct_flags : flags);
|
|
|
|
/* If we are in single onion mode, retry node selection for a 3-hop
|
|
|
|
* path */
|
|
|
|
if (allow_direct && !node) {
|
|
|
|
log_info(LD_REND,
|
|
|
|
"Unable to find an intro point that we can connect to "
|
|
|
|
"directly for %s, falling back to a 3-hop path.",
|
|
|
|
safe_str_client(service->service_id));
|
|
|
|
node = router_choose_random_node(exclude_nodes,
|
|
|
|
options->ExcludeNodes, flags);
|
|
|
|
}
|
|
|
|
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
if (!node) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_REND,
|
2015-05-29 23:45:45 +02:00
|
|
|
"We only have %d introduction points established for %s; "
|
2011-10-05 02:34:42 +02:00
|
|
|
"wanted %u.",
|
2015-05-11 22:19:16 +02:00
|
|
|
smartlist_len(service->intro_nodes),
|
|
|
|
safe_str_client(service->service_id),
|
2011-10-05 02:34:42 +02:00
|
|
|
n_intro_points_to_open);
|
2004-04-03 01:01:00 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-09-05 07:44:02 +02:00
|
|
|
/* Add the choosen node to the exclusion list in order to avoid picking
|
|
|
|
* it again in the next iteration. */
|
2014-11-04 06:19:31 +01:00
|
|
|
smartlist_add(exclude_nodes, (void*)node);
|
2007-12-21 10:28:22 +01:00
|
|
|
intro = tor_malloc_zero(sizeof(rend_intro_point_t));
|
2016-08-18 05:19:22 +02:00
|
|
|
/* extend_info is for clients, so we want the multi-hop primary ORPort,
|
|
|
|
* even if we are a single onion service and intend to connect to it
|
|
|
|
* directly ourselves. */
|
2011-11-24 18:29:56 +01:00
|
|
|
intro->extend_info = extend_info_from_node(node, 0);
|
2017-02-03 16:14:25 +01:00
|
|
|
if (BUG(intro->extend_info == NULL)) {
|
|
|
|
break;
|
|
|
|
}
|
2012-01-18 21:53:30 +01:00
|
|
|
intro->intro_key = crypto_pk_new();
|
2015-03-13 14:41:49 +01:00
|
|
|
const int fail = crypto_pk_generate_key(intro->intro_key);
|
2015-03-12 18:59:46 +01:00
|
|
|
tor_assert(!fail);
|
2011-10-06 00:43:02 +02:00
|
|
|
intro->time_published = -1;
|
2011-10-29 03:35:55 +02:00
|
|
|
intro->time_to_expire = -1;
|
2015-04-20 21:51:06 +02:00
|
|
|
intro->max_introductions =
|
2015-04-21 16:17:12 +02:00
|
|
|
crypto_rand_int_range(INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS,
|
|
|
|
INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS);
|
2007-12-21 10:28:22 +01:00
|
|
|
smartlist_add(service->intro_nodes, intro);
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_REND, "Picked router %s as an intro point for %s.",
|
2011-05-30 21:41:46 +02:00
|
|
|
safe_str_client(node_describe(node)),
|
2011-05-16 03:58:46 +02:00
|
|
|
safe_str_client(service->service_id));
|
2015-05-29 23:45:45 +02:00
|
|
|
/* Establish new introduction circuit to our chosen intro point. */
|
2004-04-03 01:01:00 +02:00
|
|
|
r = rend_service_launch_establish_intro(service, intro);
|
2015-05-29 23:45:45 +02:00
|
|
|
if (r < 0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_REND, "Error launching circuit to node %s for service %s.",
|
2011-05-16 03:58:46 +02:00
|
|
|
safe_str_client(extend_info_describe(intro->extend_info)),
|
|
|
|
safe_str_client(service->service_id));
|
2015-05-29 23:45:45 +02:00
|
|
|
/* This funcion will be called again by the main loop so this intro
|
2015-06-29 17:12:25 +02:00
|
|
|
* point without a intro circuit will be retried on or removed after
|
|
|
|
* a maximum number of attempts. */
|
2004-04-03 01:01:00 +02:00
|
|
|
}
|
|
|
|
}
|
2015-05-29 23:45:45 +02:00
|
|
|
} SMARTLIST_FOREACH_END(service);
|
2014-11-04 06:19:31 +01:00
|
|
|
smartlist_free(exclude_nodes);
|
2015-06-01 18:08:13 +02:00
|
|
|
smartlist_free(retry_nodes);
|
2004-04-01 05:23:28 +02:00
|
|
|
}
|
|
|
|
|
2015-01-10 12:20:29 +01:00
|
|
|
#define MIN_REND_INITIAL_POST_DELAY (30)
|
|
|
|
#define MIN_REND_INITIAL_POST_DELAY_TESTING (5)
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Regenerate and upload rendezvous service descriptors for all
|
2004-11-15 10:05:54 +01:00
|
|
|
* services, if necessary. If the descriptor has been dirty enough
|
|
|
|
* for long enough, definitely upload; else only upload when the
|
|
|
|
* periodic timeout has expired.
|
|
|
|
*
|
|
|
|
* For the first upload, pick a random time between now and two periods
|
|
|
|
* from now, and pick it independently for each service.
|
2004-05-05 23:32:43 +02:00
|
|
|
*/
|
2004-04-13 19:16:47 +02:00
|
|
|
void
|
2005-06-11 20:52:12 +02:00
|
|
|
rend_consider_services_upload(time_t now)
|
|
|
|
{
|
2004-04-13 19:16:47 +02:00
|
|
|
int i;
|
|
|
|
rend_service_t *service;
|
2016-07-14 06:04:02 +02:00
|
|
|
const or_options_t *options = get_options();
|
|
|
|
int rendpostperiod = options->RendPostPeriod;
|
|
|
|
int rendinitialpostdelay = (options->TestingTorNetwork ?
|
2015-01-10 12:20:29 +01:00
|
|
|
MIN_REND_INITIAL_POST_DELAY_TESTING :
|
|
|
|
MIN_REND_INITIAL_POST_DELAY);
|
2004-04-13 19:16:47 +02:00
|
|
|
|
2005-01-14 05:35:53 +01:00
|
|
|
for (i=0; i < smartlist_len(rend_service_list); ++i) {
|
2004-04-13 19:16:47 +02:00
|
|
|
service = smartlist_get(rend_service_list, i);
|
2004-11-15 10:05:54 +01:00
|
|
|
if (!service->next_upload_time) { /* never been uploaded yet */
|
2015-01-10 12:20:29 +01:00
|
|
|
/* The fixed lower bound of rendinitialpostdelay seconds ensures that
|
|
|
|
* the descriptor is stable before being published. See comment below. */
|
2004-11-15 10:05:54 +01:00
|
|
|
service->next_upload_time =
|
2015-01-10 12:20:29 +01:00
|
|
|
now + rendinitialpostdelay + crypto_rand_int(2*rendpostperiod);
|
2016-07-14 06:04:02 +02:00
|
|
|
/* Single Onion Services prioritise availability over hiding their
|
|
|
|
* startup time, as their IP address is publicly discoverable anyway.
|
|
|
|
*/
|
|
|
|
if (rend_service_reveal_startup_time(options)) {
|
|
|
|
service->next_upload_time = now + rendinitialpostdelay;
|
|
|
|
}
|
2004-11-15 10:05:54 +01:00
|
|
|
}
|
2015-06-01 19:17:37 +02:00
|
|
|
/* Does every introduction points have been established? */
|
|
|
|
unsigned int intro_points_ready =
|
2015-07-16 17:11:21 +02:00
|
|
|
count_established_intro_points(service) >=
|
|
|
|
service->n_intro_points_wanted;
|
2015-06-15 23:11:57 +02:00
|
|
|
if (intro_points_ready &&
|
|
|
|
(service->next_upload_time < now ||
|
2004-11-15 10:05:54 +01:00
|
|
|
(service->desc_is_dirty &&
|
2015-06-15 23:11:57 +02:00
|
|
|
service->desc_is_dirty < now-rendinitialpostdelay))) {
|
2004-11-15 10:05:54 +01:00
|
|
|
/* if it's time, or if the directory servers have a wrong service
|
2015-01-10 12:20:29 +01:00
|
|
|
* descriptor and ours has been stable for rendinitialpostdelay seconds,
|
|
|
|
* upload a new one of each format. */
|
2008-09-09 10:41:58 +02:00
|
|
|
rend_service_update_descriptor(service);
|
|
|
|
upload_service_descriptor(service);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** True if the list of available router descriptors might have changed so
|
|
|
|
* that we should have a look whether we can republish previously failed
|
|
|
|
* rendezvous service descriptors. */
|
|
|
|
static int consider_republishing_rend_descriptors = 1;
|
|
|
|
|
|
|
|
/** Called when our internal view of the directory has changed, so that we
|
|
|
|
* might have router descriptors of hidden service directories available that
|
|
|
|
* we did not have before. */
|
|
|
|
void
|
|
|
|
rend_hsdir_routers_changed(void)
|
|
|
|
{
|
|
|
|
consider_republishing_rend_descriptors = 1;
|
|
|
|
}
|
|
|
|
|
2008-09-23 11:30:57 +02:00
|
|
|
/** Consider republication of v2 rendezvous service descriptors that failed
|
2008-09-09 10:41:58 +02:00
|
|
|
* previously, but without regenerating descriptor contents.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
rend_consider_descriptor_republication(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
rend_service_t *service;
|
|
|
|
|
|
|
|
if (!consider_republishing_rend_descriptors)
|
|
|
|
return;
|
|
|
|
consider_republishing_rend_descriptors = 0;
|
|
|
|
|
|
|
|
if (!get_options()->PublishHidServDescriptors)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i=0; i < smartlist_len(rend_service_list); ++i) {
|
|
|
|
service = smartlist_get(rend_service_list, i);
|
2009-05-02 23:31:58 +02:00
|
|
|
if (service->desc && !service->desc->all_uploads_performed) {
|
2008-09-09 10:41:58 +02:00
|
|
|
/* If we failed in uploading a descriptor last time, try again *without*
|
|
|
|
* updating the descriptor's contents. */
|
2007-09-18 23:17:45 +02:00
|
|
|
upload_service_descriptor(service);
|
2004-11-15 10:05:54 +01:00
|
|
|
}
|
2004-04-13 19:16:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Log the status of introduction points for all rendezvous services
|
2004-12-01 04:48:14 +01:00
|
|
|
* at log severity <b>severity</b>.
|
2004-05-05 23:32:43 +02:00
|
|
|
*/
|
2004-04-09 22:02:16 +02:00
|
|
|
void
|
|
|
|
rend_service_dump_stats(int severity)
|
|
|
|
{
|
|
|
|
int i,j;
|
|
|
|
rend_service_t *service;
|
2007-12-21 10:28:22 +01:00
|
|
|
rend_intro_point_t *intro;
|
|
|
|
const char *safe_name;
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t *circ;
|
2004-04-09 22:02:16 +02:00
|
|
|
|
|
|
|
for (i=0; i < smartlist_len(rend_service_list); ++i) {
|
|
|
|
service = smartlist_get(rend_service_list, i);
|
2016-11-01 20:55:03 +01:00
|
|
|
tor_log(severity, LD_GENERAL, "Service configured in %s:",
|
|
|
|
rend_service_escaped_dir(service));
|
2004-04-09 22:02:16 +02:00
|
|
|
for (j=0; j < smartlist_len(service->intro_nodes); ++j) {
|
2007-12-21 10:28:22 +01:00
|
|
|
intro = smartlist_get(service->intro_nodes, j);
|
2009-12-15 23:23:36 +01:00
|
|
|
safe_name = safe_str_client(intro->extend_info->nickname);
|
2006-12-07 18:04:44 +01:00
|
|
|
|
2009-05-03 01:04:25 +02:00
|
|
|
circ = find_intro_circuit(intro, service->pk_digest);
|
2004-04-09 22:02:16 +02:00
|
|
|
if (!circ) {
|
2013-02-01 21:43:37 +01:00
|
|
|
tor_log(severity, LD_GENERAL, " Intro point %d at %s: no circuit",
|
2006-12-07 18:04:44 +01:00
|
|
|
j, safe_name);
|
2004-04-09 22:02:16 +02:00
|
|
|
continue;
|
|
|
|
}
|
2013-02-01 21:43:37 +01:00
|
|
|
tor_log(severity, LD_GENERAL, " Intro point %d at %s: circuit is %s",
|
2012-10-12 18:22:13 +02:00
|
|
|
j, safe_name, circuit_state_to_string(circ->base_.state));
|
2004-04-09 22:02:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-28 19:11:21 +01:00
|
|
|
#ifdef HAVE_SYS_UN_H
|
|
|
|
|
|
|
|
/** Given <b>ports</b>, a smarlist containing rend_service_port_config_t,
|
|
|
|
* add the given <b>p</b>, a AF_UNIX port to the list. Return 0 on success
|
|
|
|
* else return -ENOSYS if AF_UNIX is not supported (see function in the
|
|
|
|
* #else statement below). */
|
|
|
|
static int
|
|
|
|
add_unix_port(smartlist_t *ports, rend_service_port_config_t *p)
|
|
|
|
{
|
|
|
|
tor_assert(ports);
|
|
|
|
tor_assert(p);
|
|
|
|
tor_assert(p->is_unix_addr);
|
|
|
|
|
|
|
|
smartlist_add(ports, p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Given <b>conn</b> set it to use the given port <b>p</b> values. Return 0
|
|
|
|
* on success else return -ENOSYS if AF_UNIX is not supported (see function
|
|
|
|
* in the #else statement below). */
|
|
|
|
static int
|
|
|
|
set_unix_port(edge_connection_t *conn, rend_service_port_config_t *p)
|
|
|
|
{
|
|
|
|
tor_assert(conn);
|
|
|
|
tor_assert(p);
|
|
|
|
tor_assert(p->is_unix_addr);
|
|
|
|
|
|
|
|
conn->base_.socket_family = AF_UNIX;
|
|
|
|
tor_addr_make_unspec(&conn->base_.addr);
|
|
|
|
conn->base_.port = 1;
|
|
|
|
conn->base_.address = tor_strdup(p->unix_addr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* defined(HAVE_SYS_UN_H) */
|
|
|
|
|
|
|
|
static int
|
|
|
|
set_unix_port(edge_connection_t *conn, rend_service_port_config_t *p)
|
|
|
|
{
|
2015-01-30 20:47:56 +01:00
|
|
|
(void) conn;
|
|
|
|
(void) p;
|
2015-01-28 19:11:21 +01:00
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
add_unix_port(smartlist_t *ports, rend_service_port_config_t *p)
|
|
|
|
{
|
2015-01-30 20:47:56 +01:00
|
|
|
(void) ports;
|
|
|
|
(void) p;
|
2015-01-28 19:11:21 +01:00
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_SYS_UN_H */
|
|
|
|
|
2004-05-10 09:27:29 +02:00
|
|
|
/** Given <b>conn</b>, a rendezvous exit stream, look up the hidden service for
|
2004-05-10 06:34:48 +02:00
|
|
|
* 'circ', and look up the port and address based on conn-\>port.
|
2015-01-03 18:34:52 +01:00
|
|
|
* Assign the actual conn-\>addr and conn-\>port. Return -2 on failure
|
|
|
|
* for which the circuit should be closed, -1 on other failure,
|
2004-05-05 23:32:43 +02:00
|
|
|
* or 0 for success.
|
2004-04-07 00:05:49 +02:00
|
|
|
*/
|
|
|
|
int
|
2006-07-26 21:07:26 +02:00
|
|
|
rend_service_set_connection_addr_port(edge_connection_t *conn,
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t *circ)
|
2004-04-07 00:05:49 +02:00
|
|
|
{
|
|
|
|
rend_service_t *service;
|
2007-11-29 16:25:04 +01:00
|
|
|
char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
|
2007-05-29 20:58:16 +02:00
|
|
|
smartlist_t *matching_ports;
|
|
|
|
rend_service_port_config_t *chosen_port;
|
2015-01-28 19:11:21 +01:00
|
|
|
unsigned int warn_once = 0;
|
2016-05-31 20:51:30 +02:00
|
|
|
const char *rend_pk_digest;
|
2004-04-07 00:05:49 +02:00
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
tor_assert(circ->base_.purpose == CIRCUIT_PURPOSE_S_REND_JOINED);
|
2008-09-24 16:44:29 +02:00
|
|
|
tor_assert(circ->rend_data);
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(LD_REND,"beginning to hunt for addr/port");
|
2016-05-31 20:51:30 +02:00
|
|
|
/* XXX: This is version 2 specific (only one supported). */
|
|
|
|
rend_pk_digest = (char *) rend_data_get_pk_digest(circ->rend_data, NULL);
|
2007-11-29 16:25:04 +01:00
|
|
|
base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
|
2016-05-31 20:51:30 +02:00
|
|
|
rend_pk_digest, REND_SERVICE_ID_LEN);
|
|
|
|
service = rend_service_get_by_pk_digest(rend_pk_digest);
|
2004-04-07 00:05:49 +02:00
|
|
|
if (!service) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_REND, "Couldn't find any service associated with pk %s on "
|
2013-03-10 13:32:58 +01:00
|
|
|
"rendezvous circuit %u; closing.",
|
|
|
|
serviceid, (unsigned)circ->base_.n_circ_id);
|
2015-01-03 18:34:52 +01:00
|
|
|
return -2;
|
2004-04-07 00:05:49 +02:00
|
|
|
}
|
2015-05-20 19:33:59 +02:00
|
|
|
if (service->max_streams_per_circuit > 0) {
|
|
|
|
/* Enforce the streams-per-circuit limit, and refuse to provide a
|
|
|
|
* mapping if this circuit will exceed the limit. */
|
|
|
|
#define MAX_STREAM_WARN_INTERVAL 600
|
|
|
|
static struct ratelim_t stream_ratelim =
|
|
|
|
RATELIM_INIT(MAX_STREAM_WARN_INTERVAL);
|
|
|
|
if (circ->rend_data->nr_streams >= service->max_streams_per_circuit) {
|
|
|
|
log_fn_ratelim(&stream_ratelim, LOG_WARN, LD_REND,
|
|
|
|
"Maximum streams per circuit limit reached on rendezvous "
|
|
|
|
"circuit %u; %s. Circuit has %d out of %d streams.",
|
|
|
|
(unsigned)circ->base_.n_circ_id,
|
|
|
|
service->max_streams_close_circuit ?
|
|
|
|
"closing circuit" :
|
|
|
|
"ignoring open stream request",
|
|
|
|
circ->rend_data->nr_streams,
|
|
|
|
service->max_streams_per_circuit);
|
|
|
|
return service->max_streams_close_circuit ? -2 : -1;
|
|
|
|
}
|
|
|
|
}
|
2012-01-18 21:53:30 +01:00
|
|
|
matching_ports = smartlist_new();
|
2007-05-29 20:58:16 +02:00
|
|
|
SMARTLIST_FOREACH(service->ports, rend_service_port_config_t *, p,
|
|
|
|
{
|
2015-01-28 19:11:21 +01:00
|
|
|
if (conn->base_.port != p->virtual_port) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!(p->is_unix_addr)) {
|
2007-05-29 20:58:16 +02:00
|
|
|
smartlist_add(matching_ports, p);
|
2015-01-28 19:11:21 +01:00
|
|
|
} else {
|
|
|
|
if (add_unix_port(matching_ports, p)) {
|
|
|
|
if (!warn_once) {
|
|
|
|
/* Unix port not supported so warn only once. */
|
2015-01-27 14:12:40 +01:00
|
|
|
log_warn(LD_REND,
|
2015-01-28 19:11:21 +01:00
|
|
|
"Saw AF_UNIX virtual port mapping for port %d on service "
|
|
|
|
"%s, which is unsupported on this platform. Ignoring it.",
|
|
|
|
conn->base_.port, serviceid);
|
2015-01-27 14:12:40 +01:00
|
|
|
}
|
2015-01-28 19:11:21 +01:00
|
|
|
warn_once++;
|
2015-01-22 03:22:33 +01:00
|
|
|
}
|
2004-04-07 00:05:49 +02:00
|
|
|
}
|
2007-05-29 20:58:16 +02:00
|
|
|
});
|
|
|
|
chosen_port = smartlist_choose(matching_ports);
|
|
|
|
smartlist_free(matching_ports);
|
|
|
|
if (chosen_port) {
|
2015-01-27 14:12:40 +01:00
|
|
|
if (!(chosen_port->is_unix_addr)) {
|
|
|
|
/* Get a non-AF_UNIX connection ready for connection_exit_connect() */
|
|
|
|
tor_addr_copy(&conn->base_.addr, &chosen_port->real_addr);
|
|
|
|
conn->base_.port = chosen_port->real_port;
|
|
|
|
} else {
|
2015-01-28 19:11:21 +01:00
|
|
|
if (set_unix_port(conn, chosen_port)) {
|
|
|
|
/* Simply impossible to end up here else we were able to add a Unix
|
|
|
|
* port without AF_UNIX support... ? */
|
|
|
|
tor_assert(0);
|
|
|
|
}
|
2015-01-27 14:12:40 +01:00
|
|
|
}
|
2007-05-29 20:58:16 +02:00
|
|
|
return 0;
|
2004-04-07 00:05:49 +02:00
|
|
|
}
|
2015-01-27 14:12:40 +01:00
|
|
|
|
|
|
|
log_info(LD_REND,
|
|
|
|
"No virtual port mapping exists for port %d on service %s",
|
|
|
|
conn->base_.port, serviceid);
|
|
|
|
|
2015-01-03 18:34:52 +01:00
|
|
|
if (service->allow_unknown_ports)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return -2;
|
2004-04-07 00:05:49 +02:00
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|
2016-09-13 09:28:03 +02:00
|
|
|
/* Are HiddenServiceSingleHopMode and HiddenServiceNonAnonymousMode consistent?
|
2016-09-13 09:20:46 +02:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
rend_service_non_anonymous_mode_consistent(const or_options_t *options)
|
|
|
|
{
|
|
|
|
/* !! is used to make these options boolean */
|
2016-09-13 09:28:03 +02:00
|
|
|
return (!! options->HiddenServiceSingleHopMode ==
|
|
|
|
!! options->HiddenServiceNonAnonymousMode);
|
2016-09-13 09:20:46 +02:00
|
|
|
}
|
|
|
|
|
2016-07-14 06:04:02 +02:00
|
|
|
/* Do the options allow onion services to make direct (non-anonymous)
|
|
|
|
* connections to introduction or rendezvous points?
|
2016-09-13 09:20:46 +02:00
|
|
|
* Must only be called after options_validate_single_onion() has successfully
|
|
|
|
* checked onion service option consistency.
|
2016-09-13 09:28:03 +02:00
|
|
|
* Returns true if tor is in HiddenServiceSingleHopMode. */
|
2016-08-17 09:47:24 +02:00
|
|
|
int
|
2016-07-14 06:04:02 +02:00
|
|
|
rend_service_allow_non_anonymous_connection(const or_options_t *options)
|
2016-08-17 09:47:24 +02:00
|
|
|
{
|
2016-09-13 09:20:46 +02:00
|
|
|
tor_assert(rend_service_non_anonymous_mode_consistent(options));
|
2016-09-13 09:28:03 +02:00
|
|
|
return options->HiddenServiceSingleHopMode ? 1 : 0;
|
2016-07-14 06:04:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the options allow us to reveal the exact startup time of the onion
|
|
|
|
* service?
|
|
|
|
* Single Onion Services prioritise availability over hiding their
|
|
|
|
* startup time, as their IP address is publicly discoverable anyway.
|
2016-09-13 09:20:46 +02:00
|
|
|
* Must only be called after options_validate_single_onion() has successfully
|
|
|
|
* checked onion service option consistency.
|
|
|
|
* Returns true if tor is in non-anonymous hidden service mode. */
|
2016-07-14 06:04:02 +02:00
|
|
|
int
|
|
|
|
rend_service_reveal_startup_time(const or_options_t *options)
|
|
|
|
{
|
2016-09-13 09:20:46 +02:00
|
|
|
tor_assert(rend_service_non_anonymous_mode_consistent(options));
|
|
|
|
return rend_service_non_anonymous_mode_enabled(options);
|
2016-07-14 06:04:02 +02:00
|
|
|
}
|
|
|
|
|
2016-09-13 09:28:03 +02:00
|
|
|
/* Is non-anonymous mode enabled using the HiddenServiceNonAnonymousMode
|
2016-09-13 09:20:46 +02:00
|
|
|
* config option?
|
|
|
|
* Must only be called after options_validate_single_onion() has successfully
|
|
|
|
* checked onion service option consistency.
|
|
|
|
*/
|
2016-07-14 06:04:02 +02:00
|
|
|
int
|
|
|
|
rend_service_non_anonymous_mode_enabled(const or_options_t *options)
|
|
|
|
{
|
2016-09-13 09:20:46 +02:00
|
|
|
tor_assert(rend_service_non_anonymous_mode_consistent(options));
|
2016-09-13 09:28:03 +02:00
|
|
|
return options->HiddenServiceNonAnonymousMode ? 1 : 0;
|
2016-08-17 09:47:24 +02:00
|
|
|
}
|
2016-12-14 21:41:08 +01:00
|
|
|
|