2004-03-31 23:54:56 +02:00
|
|
|
/* Copyright 2004 Roger Dingledine */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
|
|
|
|
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
|
|
|
|
|
|
|
#include "or.h"
|
|
|
|
|
2004-04-13 19:16:47 +02:00
|
|
|
static circuit_t *find_intro_circuit(routerinfo_t *router, const char *pk_digest);
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
typedef struct rend_service_port_config_t {
|
|
|
|
uint16_t virtual_port;
|
|
|
|
uint16_t real_port;
|
|
|
|
uint32_t real_address;
|
|
|
|
} rend_service_port_config_t;
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Try to maintain this many intro points per service if possible. */
|
2004-04-03 01:01:00 +02:00
|
|
|
#define NUM_INTRO_POINTS 3
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Represents a single hidden service running at this OP. */
|
2004-03-31 23:54:56 +02:00
|
|
|
typedef struct rend_service_t {
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Fields specified in config file */
|
|
|
|
char *directory; /**< where in the filesystem it stores it */
|
|
|
|
smartlist_t *ports; /**< List of rend_service_port_config_t */
|
|
|
|
char *intro_prefer_nodes; /**< comma-separated list of nicknames */
|
|
|
|
char *intro_exclude_nodes; /**< comma-separated list of nicknames */
|
2004-03-31 23:54:56 +02:00
|
|
|
/* Other fields */
|
|
|
|
crypto_pk_env_t *private_key;
|
|
|
|
char service_id[REND_SERVICE_ID_LEN+1];
|
2004-04-03 05:07:25 +02:00
|
|
|
char pk_digest[DIGEST_LEN];
|
2004-05-09 18:47:25 +02:00
|
|
|
smartlist_t *intro_nodes; /**< list of nicknames for intro points we have,
|
2004-05-05 23:32:43 +02:00
|
|
|
* or are trying to establish. */
|
2004-04-03 01:01:00 +02:00
|
|
|
rend_service_descriptor_t *desc;
|
2004-04-13 19:16:47 +02:00
|
|
|
int desc_is_dirty;
|
2004-03-31 23:54:56 +02:00
|
|
|
} rend_service_t;
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** A list of rend_service_t's for services run on this OP.
|
2004-03-31 23:54:56 +02:00
|
|
|
*/
|
|
|
|
static smartlist_t *rend_service_list = NULL;
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Release the storage held by 'service'.
|
2004-04-06 05:44:36 +02:00
|
|
|
*/
|
|
|
|
static void rend_service_free(rend_service_t *service)
|
2004-03-31 23:54:56 +02:00
|
|
|
{
|
2004-04-06 05:44:36 +02:00
|
|
|
if (!service) return;
|
|
|
|
tor_free(service->directory);
|
|
|
|
SMARTLIST_FOREACH(service->ports, void*, p, tor_free(p));
|
|
|
|
smartlist_free(service->ports);
|
|
|
|
if (service->private_key)
|
|
|
|
crypto_free_pk_env(service->private_key);
|
|
|
|
tor_free(service->intro_prefer_nodes);
|
|
|
|
tor_free(service->intro_exclude_nodes);
|
|
|
|
SMARTLIST_FOREACH(service->intro_nodes, void*, p, tor_free(p));
|
|
|
|
smartlist_free(service->intro_nodes);
|
|
|
|
if (service->desc)
|
|
|
|
rend_service_descriptor_free(service->desc);
|
|
|
|
tor_free(service);
|
2004-03-31 23:54:56 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Release all the storage held in rend_service_list, and allocate a new,
|
2004-04-06 05:44:36 +02:00
|
|
|
* empty rend_service_list.
|
|
|
|
*/
|
2004-03-31 23:54:56 +02:00
|
|
|
static void rend_service_free_all(void)
|
|
|
|
{
|
|
|
|
if (!rend_service_list) {
|
|
|
|
rend_service_list = smartlist_create();
|
|
|
|
return;
|
|
|
|
}
|
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);
|
|
|
|
rend_service_list = smartlist_create();
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Validate 'service' and add it to rend_service_list if possible.
|
2004-04-06 05:44:36 +02:00
|
|
|
*/
|
2004-03-31 23:54:56 +02:00
|
|
|
static void add_service(rend_service_t *service)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
rend_service_port_config_t *p;
|
|
|
|
struct in_addr addr;
|
|
|
|
|
2004-04-03 01:01:00 +02:00
|
|
|
if (!service->intro_prefer_nodes)
|
|
|
|
service->intro_prefer_nodes = tor_strdup("");
|
|
|
|
if (!service->intro_exclude_nodes)
|
|
|
|
service->intro_exclude_nodes = tor_strdup("");
|
|
|
|
|
2004-04-03 02:58:54 +02:00
|
|
|
if (!smartlist_len(service->ports)) {
|
2004-03-31 23:54:56 +02:00
|
|
|
log_fn(LOG_WARN, "Hidden service with no ports configured; ignoring.");
|
|
|
|
rend_service_free(service);
|
|
|
|
} else {
|
2004-04-03 02:58:54 +02:00
|
|
|
smartlist_set_capacity(service->ports, -1);
|
2004-03-31 23:54:56 +02:00
|
|
|
smartlist_add(rend_service_list, service);
|
2004-04-08 03:59:31 +02:00
|
|
|
log_fn(LOG_DEBUG,"Configuring service with directory %s",service->directory);
|
2004-04-03 02:58:54 +02:00
|
|
|
for (i = 0; i < smartlist_len(service->ports); ++i) {
|
|
|
|
p = smartlist_get(service->ports, i);
|
2004-03-31 23:54:56 +02:00
|
|
|
addr.s_addr = htonl(p->real_address);
|
2004-04-08 03:59:31 +02:00
|
|
|
log_fn(LOG_DEBUG,"Service maps port %d to %s:%d",
|
2004-04-03 04:14:20 +02:00
|
|
|
p->virtual_port, inet_ntoa(addr), p->real_port);
|
2004-03-31 23:54:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Parses a real-port to virtual-port mapping and returns a new
|
2004-04-06 05:44:36 +02:00
|
|
|
* rend_service_port_config_t.
|
|
|
|
*
|
|
|
|
* The format is: VirtualPort (IP|RealPort|IP:RealPort)?
|
2004-03-31 23:54:56 +02:00
|
|
|
* IP defaults to 127.0.0.1; RealPort defaults to VirtualPort.
|
|
|
|
*/
|
|
|
|
static rend_service_port_config_t *parse_port_config(const char *string)
|
|
|
|
{
|
|
|
|
int virtport, realport, r;
|
|
|
|
struct in_addr addr;
|
|
|
|
char *endptr, *colon, *addrstring;
|
|
|
|
rend_service_port_config_t *result;
|
|
|
|
|
|
|
|
virtport = (int) strtol(string, &endptr, 10);
|
|
|
|
if (endptr == string) {
|
|
|
|
log_fn(LOG_WARN, "Missing port in hidden service port configuration");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (virtport < 1 || virtport > 65535) {
|
|
|
|
log_fn(LOG_WARN, "Port out of range in hidden service port configuration");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
string = endptr + strspn(endptr, " \t");
|
|
|
|
if (!*string) {
|
|
|
|
/* No addr:port part; use default. */
|
|
|
|
realport = virtport;
|
2004-04-06 05:44:36 +02:00
|
|
|
addr.s_addr = htonl(0x7F000001u); /* 127.0.0.1 */
|
2004-03-31 23:54:56 +02:00
|
|
|
} else {
|
|
|
|
colon = strchr(string, ':');
|
|
|
|
if (colon) {
|
|
|
|
/* Try to parse addr:port. */
|
|
|
|
addrstring = tor_strndup(string, colon-string);
|
|
|
|
r = tor_inet_aton(addrstring, &addr);
|
|
|
|
tor_free(addrstring);
|
|
|
|
if (!r) {
|
2004-04-03 04:14:20 +02:00
|
|
|
log_fn(LOG_WARN,"Unparseable address in hidden service port configuration");
|
|
|
|
return NULL;
|
2004-03-31 23:54:56 +02:00
|
|
|
}
|
|
|
|
realport = strtol(colon+1, &endptr, 10);
|
|
|
|
if (*endptr) {
|
2004-04-03 04:14:20 +02:00
|
|
|
log_fn(LOG_WARN,"Unparseable or missing port in hidden service port configuration.");
|
|
|
|
return NULL;
|
2004-03-31 23:54:56 +02:00
|
|
|
}
|
|
|
|
} else if (strchr(string, '.') && tor_inet_aton(string, &addr)) {
|
|
|
|
/* We have addr; use deafult port. */
|
|
|
|
realport = virtport;
|
|
|
|
} else {
|
|
|
|
/* No addr:port, no addr -- must be port. */
|
|
|
|
realport = strtol(string, &endptr, 10);
|
|
|
|
if (*endptr) {
|
2004-04-03 04:14:20 +02:00
|
|
|
log_fn(LOG_WARN, "Unparseable of missing port in hidden service port configuration.");
|
|
|
|
return NULL;
|
2004-03-31 23:54:56 +02:00
|
|
|
}
|
|
|
|
addr.s_addr = htonl(0x7F000001u); /* Default to 127.0.0.1 */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (realport < 1 || realport > 65535) {
|
|
|
|
log_fn(LOG_WARN, "Port out of range in hidden service port configuration.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = tor_malloc(sizeof(rend_service_port_config_t));
|
|
|
|
result->virtual_port = virtport;
|
|
|
|
result->real_port = realport;
|
|
|
|
result->real_address = (uint32_t) ntohl(addr.s_addr);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Set up rend_service_list, based on the values of HiddenServiceDir and
|
2004-03-31 23:54:56 +02:00
|
|
|
* HiddenServicePort in 'options'. Return 0 on success and -1 on
|
|
|
|
* failure.
|
|
|
|
*/
|
|
|
|
int rend_config_services(or_options_t *options)
|
|
|
|
{
|
|
|
|
struct config_line_t *line;
|
|
|
|
rend_service_t *service = NULL;
|
|
|
|
rend_service_port_config_t *portcfg;
|
|
|
|
rend_service_free_all();
|
|
|
|
|
|
|
|
for (line = options->RendConfigLines; line; line = line->next) {
|
|
|
|
if (!strcasecmp(line->key, "HiddenServiceDir")) {
|
|
|
|
if (service)
|
2004-04-03 04:14:20 +02:00
|
|
|
add_service(service);
|
2004-03-31 23:54:56 +02:00
|
|
|
service = tor_malloc_zero(sizeof(rend_service_t));
|
|
|
|
service->directory = tor_strdup(line->value);
|
|
|
|
service->ports = smartlist_create();
|
2004-04-03 01:01:00 +02:00
|
|
|
service->intro_nodes = smartlist_create();
|
2004-04-03 00:06:46 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!service) {
|
|
|
|
log_fn(LOG_WARN, "HiddenServicePort with no preceeding HiddenServiceDir directive");
|
|
|
|
rend_service_free(service);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!strcasecmp(line->key, "HiddenServicePort")) {
|
2004-03-31 23:54:56 +02:00
|
|
|
portcfg = parse_port_config(line->value);
|
|
|
|
if (!portcfg) {
|
2004-04-03 04:14:20 +02:00
|
|
|
rend_service_free(service);
|
|
|
|
return -1;
|
2004-03-31 23:54:56 +02:00
|
|
|
}
|
|
|
|
smartlist_add(service->ports, portcfg);
|
2004-04-03 00:06:46 +02:00
|
|
|
} else if (!strcasecmp(line->key, "HiddenServiceNodes")) {
|
2004-04-03 01:01:00 +02:00
|
|
|
if (service->intro_prefer_nodes) {
|
2004-04-03 00:06:46 +02:00
|
|
|
log_fn(LOG_WARN, "Got multiple HiddenServiceNodes lines for a single service");
|
|
|
|
return -1;
|
|
|
|
}
|
2004-04-03 01:01:00 +02:00
|
|
|
service->intro_prefer_nodes = tor_strdup(line->value);
|
2004-04-03 00:06:46 +02:00
|
|
|
} else {
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(!strcasecmp(line->key, "HiddenServiceExcludeNodes"));
|
2004-04-03 00:06:46 +02:00
|
|
|
if (service->intro_exclude_nodes) {
|
|
|
|
log_fn(LOG_WARN, "Got multiple HiddenServiceExcludedNodes lines for a single service");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
service->intro_exclude_nodes = tor_strdup(line->value);
|
2004-03-31 23:54:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (service)
|
|
|
|
add_service(service);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Replace the old value of service->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
|
|
|
*/
|
|
|
|
static void rend_service_update_descriptor(rend_service_t *service)
|
|
|
|
{
|
|
|
|
rend_service_descriptor_t *d;
|
2004-04-13 19:16:47 +02:00
|
|
|
circuit_t *circ;
|
2004-04-03 01:01:00 +02:00
|
|
|
int i,n;
|
2004-04-13 19:16:47 +02:00
|
|
|
routerinfo_t *router;
|
2004-04-03 01:01:00 +02:00
|
|
|
|
|
|
|
if (service->desc) {
|
|
|
|
rend_service_descriptor_free(service->desc);
|
|
|
|
service->desc = NULL;
|
|
|
|
}
|
|
|
|
d = service->desc = tor_malloc(sizeof(rend_service_descriptor_t));
|
|
|
|
d->pk = crypto_pk_dup_key(service->private_key);
|
|
|
|
d->timestamp = time(NULL);
|
2004-04-13 19:16:47 +02:00
|
|
|
n = smartlist_len(service->intro_nodes);
|
|
|
|
d->n_intro_points = 0;
|
2004-04-03 01:01:00 +02:00
|
|
|
d->intro_points = tor_malloc(sizeof(char*)*n);
|
|
|
|
for (i=0; i < n; ++i) {
|
2004-04-13 19:16:47 +02:00
|
|
|
router = router_get_by_nickname(smartlist_get(service->intro_nodes, i));
|
|
|
|
circ = find_intro_circuit(router, service->pk_digest);
|
2004-04-14 06:19:12 +02:00
|
|
|
if (circ && circ->purpose == CIRCUIT_PURPOSE_S_INTRO) {
|
2004-04-13 19:16:47 +02:00
|
|
|
/* We have an entirely established intro circuit. */
|
|
|
|
d->intro_points[d->n_intro_points++] = tor_strdup(router->nickname);
|
|
|
|
}
|
2004-04-03 01:01:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Load and/or generate private keys for all hidden services. Return 0 on
|
2004-03-31 23:54:56 +02:00
|
|
|
* success, -1 on failure.
|
|
|
|
*/
|
2004-04-13 19:16:47 +02:00
|
|
|
int rend_service_load_keys(void)
|
2004-03-31 23:54:56 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
rend_service_t *s;
|
|
|
|
char fname[512];
|
|
|
|
char buf[128];
|
|
|
|
|
2004-04-03 02:58:54 +02:00
|
|
|
for (i=0; i < smartlist_len(rend_service_list); ++i) {
|
|
|
|
s = smartlist_get(rend_service_list,i);
|
2004-03-31 23:54:56 +02:00
|
|
|
if (s->private_key)
|
|
|
|
continue;
|
2004-04-03 02:58:54 +02:00
|
|
|
log_fn(LOG_INFO, "Loading hidden-service keys from '%s'", s->directory);
|
|
|
|
|
2004-03-31 23:54:56 +02:00
|
|
|
/* Check/create directory */
|
|
|
|
if (check_private_dir(s->directory, 1) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Load key */
|
2004-04-06 05:44:36 +02:00
|
|
|
if (strlcpy(fname,s->directory,sizeof(fname)) >= sizeof(fname) ||
|
|
|
|
strlcat(fname,"/private_key",sizeof(fname)) >= sizeof(fname)) {
|
2004-03-31 23:54:56 +02:00
|
|
|
log_fn(LOG_WARN, "Directory name too long: '%s'", s->directory);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
s->private_key = init_key_from_file(fname);
|
|
|
|
if (!s->private_key)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Create service file */
|
|
|
|
if (rend_get_service_id(s->private_key, s->service_id)<0) {
|
|
|
|
log_fn(LOG_WARN, "Couldn't encode service ID");
|
|
|
|
return -1;
|
|
|
|
}
|
2004-04-01 05:34:05 +02:00
|
|
|
if (crypto_pk_get_digest(s->private_key, s->pk_digest)<0) {
|
|
|
|
log_fn(LOG_WARN, "Couldn't compute hash of public key");
|
|
|
|
return -1;
|
|
|
|
}
|
2004-04-06 05:44:36 +02:00
|
|
|
if (strlcpy(fname,s->directory,sizeof(fname)) >= sizeof(fname) ||
|
|
|
|
strlcat(fname,"/hostname",sizeof(fname)) >= sizeof(fname)) {
|
2004-03-31 23:54:56 +02:00
|
|
|
log_fn(LOG_WARN, "Directory name too long: '%s'", s->directory);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sprintf(buf, "%s.onion\n", s->service_id);
|
|
|
|
if (write_str_to_file(fname,buf)<0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2004-04-01 05:23:28 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Return the service whose public key has a digest of 'digest'. Return
|
2004-04-06 05:44:36 +02:00
|
|
|
* NULL if no such service exists.
|
|
|
|
*/
|
2004-04-02 23:56:52 +02:00
|
|
|
static rend_service_t *
|
2004-04-01 05:34:05 +02:00
|
|
|
rend_service_get_by_pk_digest(const char* digest)
|
|
|
|
{
|
2004-04-03 02:58:54 +02:00
|
|
|
SMARTLIST_FOREACH(rend_service_list, rend_service_t*, s,
|
2004-04-06 05:44:36 +02:00
|
|
|
if (!memcmp(s->pk_digest,digest,DIGEST_LEN)) return s);
|
2004-04-01 05:34:05 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******
|
|
|
|
* Handle cells
|
|
|
|
******/
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Respond to an INTRODUCE2 cell by launching a circuit to the chosen
|
2004-04-02 00:21:01 +02:00
|
|
|
* rendezvous points.
|
|
|
|
*/
|
2004-04-01 05:34:05 +02:00
|
|
|
int
|
2004-04-03 05:37:11 +02:00
|
|
|
rend_service_introduce(circuit_t *circuit, const char *request, int request_len)
|
2004-04-01 05:34:05 +02:00
|
|
|
{
|
|
|
|
char *ptr, *rp_nickname, *r_cookie;
|
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 */
|
2004-04-01 05:34:05 +02:00
|
|
|
rend_service_t *service;
|
|
|
|
int len, keylen;
|
2004-04-01 22:05:57 +02:00
|
|
|
crypto_dh_env_t *dh = NULL;
|
|
|
|
circuit_t *launched = NULL;
|
2004-04-02 00:21:01 +02:00
|
|
|
crypt_path_t *cpath = NULL;
|
2004-04-12 07:12:50 +02:00
|
|
|
char serviceid[REND_SERVICE_ID_LEN+1];
|
2004-04-03 02:58:54 +02:00
|
|
|
char hexcookie[9];
|
|
|
|
|
2004-04-12 07:12:50 +02:00
|
|
|
if (base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
|
|
|
|
circuit->rend_pk_digest,10)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2004-04-03 02:58:54 +02:00
|
|
|
log_fn(LOG_INFO, "Received INTRODUCE2 cell for service %s on circ %d",
|
2004-04-12 07:12:50 +02:00
|
|
|
serviceid, circuit->n_circ_id);
|
2004-04-01 05:34:05 +02:00
|
|
|
|
2004-04-03 06:55:22 +02:00
|
|
|
if (circuit->purpose != CIRCUIT_PURPOSE_S_INTRO) {
|
2004-04-03 02:58:54 +02:00
|
|
|
log_fn(LOG_WARN, "Got an INTRODUCE2 over a non-introduction circuit %d",
|
|
|
|
circuit->n_circ_id);
|
2004-04-01 05:34:05 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-04-05 19:28:48 +02:00
|
|
|
/* min key length plus digest length plus nickname length */
|
2004-04-06 05:44:36 +02:00
|
|
|
if (request_len < DIGEST_LEN+REND_COOKIE_LEN+(MAX_NICKNAME_LEN+1)+
|
|
|
|
DH_KEY_LEN+42){
|
2004-04-03 02:58:54 +02:00
|
|
|
log_fn(LOG_WARN, "Got a truncated INTRODUCE2 cell on circ %d",
|
|
|
|
circuit->n_circ_id);
|
2004-04-01 05:34:05 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-04-05 23:15:14 +02:00
|
|
|
/* first DIGEST_LEN bytes of request is service pk digest */
|
2004-04-01 05:34:05 +02:00
|
|
|
service = rend_service_get_by_pk_digest(request);
|
|
|
|
if (!service) {
|
2004-04-03 02:58:54 +02:00
|
|
|
log_fn(LOG_WARN, "Got an INTRODUCE2 cell for an unrecognized service %s",
|
2004-04-12 07:12:50 +02:00
|
|
|
serviceid);
|
2004-04-01 05:34:05 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2004-04-05 23:15:14 +02:00
|
|
|
if (memcmp(circuit->rend_pk_digest, request, DIGEST_LEN)) {
|
2004-04-12 07:12:50 +02:00
|
|
|
if (base32_encode(serviceid, REND_SERVICE_ID_LEN+1, request, 10)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2004-04-05 22:33:29 +02:00
|
|
|
log_fn(LOG_WARN, "Got an INTRODUCE2 cell for the wrong service (%s)",
|
2004-04-12 07:12:50 +02:00
|
|
|
serviceid);
|
2004-04-01 05:34:05 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
keylen = crypto_pk_keysize(service->private_key);
|
2004-04-05 23:15:14 +02:00
|
|
|
if (request_len < keylen+DIGEST_LEN) {
|
2004-04-01 05:34:05 +02:00
|
|
|
log_fn(LOG_WARN, "PK-encrypted portion of INTRODUCE2 cell was truncated");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Next N bytes is encrypted with service key */
|
|
|
|
len = crypto_pk_private_hybrid_decrypt(
|
2004-04-05 23:31:53 +02:00
|
|
|
service->private_key,request+DIGEST_LEN,request_len-DIGEST_LEN,buf,
|
2004-04-05 23:15:14 +02:00
|
|
|
PK_PKCS1_OAEP_PADDING);
|
2004-04-01 05:34:05 +02:00
|
|
|
if (len<0) {
|
|
|
|
log_fn(LOG_WARN, "Couldn't decrypt INTRODUCE2 cell");
|
|
|
|
return -1;
|
|
|
|
}
|
2004-04-05 19:28:48 +02:00
|
|
|
ptr=memchr(buf,0,MAX_NICKNAME_LEN+1);
|
2004-04-01 05:34:05 +02:00
|
|
|
if (!ptr || ptr == buf) {
|
2004-04-05 19:28:48 +02:00
|
|
|
log_fn(LOG_WARN, "Couldn't find a null-padded nickname in INTRODUCE2 cell");
|
2004-04-01 05:34:05 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2004-04-28 22:13:21 +02:00
|
|
|
if ((int)strspn(buf,LEGAL_NICKNAME_CHARACTERS) != ptr-buf) {
|
2004-04-01 05:34:05 +02:00
|
|
|
log_fn(LOG_WARN, "Nickname in INTRODUCE2 cell contains illegal character.");
|
|
|
|
return -1;
|
|
|
|
}
|
2004-04-01 22:05:57 +02:00
|
|
|
/* Okay, now we know that the nickname is at the start of the buffer. */
|
2004-04-01 05:34:05 +02:00
|
|
|
rp_nickname = buf;
|
2004-04-05 19:28:48 +02:00
|
|
|
ptr = buf+(MAX_NICKNAME_LEN+1);
|
|
|
|
len -= (MAX_NICKNAME_LEN+1);
|
2004-04-06 05:44:36 +02:00
|
|
|
if (len != REND_COOKIE_LEN+DH_KEY_LEN) {
|
2004-04-01 05:34:05 +02:00
|
|
|
log_fn(LOG_WARN, "Bad length for INTRODUCE2 cell.");
|
|
|
|
return -1;
|
|
|
|
}
|
2004-04-01 22:05:57 +02:00
|
|
|
r_cookie = ptr;
|
2004-04-03 02:58:54 +02:00
|
|
|
hex_encode(r_cookie,4,hexcookie);
|
2004-04-01 05:34:05 +02:00
|
|
|
|
2004-04-01 22:05:57 +02:00
|
|
|
/* Try DH handshake... */
|
|
|
|
dh = crypto_dh_new();
|
|
|
|
if (!dh || crypto_dh_generate_public(dh)<0) {
|
|
|
|
log_fn(LOG_WARN, "Couldn't build DH state or generate public key");
|
|
|
|
goto err;
|
|
|
|
}
|
2004-04-05 23:15:14 +02:00
|
|
|
if (crypto_dh_compute_secret(dh, ptr+REND_COOKIE_LEN, DH_KEY_LEN, keys,
|
|
|
|
DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
|
2004-04-01 22:05:57 +02:00
|
|
|
log_fn(LOG_WARN, "Couldn't complete DH handshake");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Launch a circuit to alice's chosen rendezvous point.
|
2004-04-01 05:34:05 +02:00
|
|
|
*/
|
2004-04-02 00:21:01 +02:00
|
|
|
launched = circuit_launch_new(CIRCUIT_PURPOSE_S_CONNECT_REND, rp_nickname);
|
2004-04-03 02:58:54 +02:00
|
|
|
log_fn(LOG_INFO,
|
|
|
|
"Accepted intro; launching circuit to '%s' (cookie %s) for service %s",
|
2004-04-12 07:12:50 +02:00
|
|
|
rp_nickname, hexcookie, serviceid);
|
2004-04-01 22:05:57 +02:00
|
|
|
if (!launched) {
|
2004-04-03 02:58:54 +02:00
|
|
|
log_fn(LOG_WARN,
|
|
|
|
"Can't launch circuit to rendezvous point '%s' for service %s",
|
2004-04-12 07:12:50 +02:00
|
|
|
rp_nickname, serviceid);
|
2004-04-01 22:05:57 +02:00
|
|
|
return -1;
|
|
|
|
}
|
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. */
|
2004-04-03 01:44:46 +02:00
|
|
|
memcpy(launched->rend_pk_digest, circuit->rend_pk_digest,
|
2004-04-03 04:40:30 +02:00
|
|
|
DIGEST_LEN);
|
2004-04-01 22:05:57 +02:00
|
|
|
memcpy(launched->rend_cookie, r_cookie, REND_COOKIE_LEN);
|
2004-04-08 04:10:43 +02:00
|
|
|
strcpy(launched->rend_query, service->service_id);
|
2004-04-02 00:21:01 +02:00
|
|
|
launched->build_state->pending_final_cpath = cpath =
|
|
|
|
tor_malloc_zero(sizeof(crypt_path_t));
|
|
|
|
|
|
|
|
cpath->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;
|
2004-04-06 05:44:36 +02:00
|
|
|
memcpy(cpath->handshake_digest, keys, DIGEST_LEN);
|
2004-04-01 05:34:05 +02:00
|
|
|
|
|
|
|
return 0;
|
2004-04-01 22:05:57 +02:00
|
|
|
err:
|
|
|
|
if (dh) crypto_dh_free(dh);
|
|
|
|
if (launched) circuit_mark_for_close(launched);
|
|
|
|
return -1;
|
2004-04-01 05:34:05 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** How many times will a hidden service operator attempt to connect to
|
|
|
|
* a requested rendezvous point before giving up? */
|
2004-04-14 23:40:50 +02:00
|
|
|
#define MAX_REND_FAILURES 3
|
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
|
|
|
|
rend_service_relaunch_rendezvous(circuit_t *oldcirc)
|
|
|
|
{
|
|
|
|
circuit_t *newcirc;
|
|
|
|
cpath_build_state_t *newstate, *oldstate;
|
|
|
|
|
|
|
|
/* XXXX assert type and build_state */
|
|
|
|
|
|
|
|
if (!oldcirc->build_state ||
|
|
|
|
oldcirc->build_state->failure_count > MAX_REND_FAILURES) {
|
|
|
|
log_fn(LOG_INFO,"Attempt to build circuit to %s for rendezvous has failed too many times; giving up.",
|
|
|
|
oldcirc->build_state->chosen_exit);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_fn(LOG_INFO,"Reattempting rendezvous circuit to %s",
|
|
|
|
oldcirc->build_state->chosen_exit);
|
|
|
|
|
|
|
|
newcirc = circuit_launch_new(CIRCUIT_PURPOSE_S_CONNECT_REND,
|
|
|
|
oldcirc->build_state->chosen_exit);
|
|
|
|
if (!newcirc) {
|
|
|
|
log_fn(LOG_WARN,"Couldn't relaunch rendezvous circuit to %s",
|
|
|
|
oldcirc->build_state->chosen_exit);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
oldstate = oldcirc->build_state;
|
|
|
|
newstate = newcirc->build_state;
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(newstate && oldstate);
|
2004-04-14 23:40:50 +02:00
|
|
|
newstate->failure_count = oldstate->failure_count+1;
|
|
|
|
newstate->pending_final_cpath = oldstate->pending_final_cpath;
|
|
|
|
oldstate->pending_final_cpath = NULL;
|
|
|
|
|
|
|
|
memcpy(newcirc->rend_query, oldcirc->rend_query, REND_SERVICE_ID_LEN+1);
|
|
|
|
memcpy(newcirc->rend_pk_digest, oldcirc->rend_pk_digest, DIGEST_LEN);
|
2004-04-30 18:35:48 +02:00
|
|
|
memcpy(newcirc->rend_cookie, oldcirc->rend_cookie, REND_COOKIE_LEN);
|
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-05 23:32:43 +02:00
|
|
|
* 'service' at the introduction point 'nickname'
|
2004-04-02 00:21:01 +02:00
|
|
|
*/
|
|
|
|
static int
|
2004-05-05 23:32:43 +02:00
|
|
|
rend_service_launch_establish_intro(rend_service_t *service, const char *nickname)
|
2004-04-02 00:21:01 +02:00
|
|
|
{
|
|
|
|
circuit_t *launched;
|
|
|
|
|
2004-04-03 02:58:54 +02:00
|
|
|
log_fn(LOG_INFO, "Launching circuit to introduction point %s for service %s",
|
2004-04-12 07:12:50 +02:00
|
|
|
nickname, service->service_id);
|
2004-04-03 02:58:54 +02:00
|
|
|
|
2004-04-02 00:21:01 +02:00
|
|
|
launched = circuit_launch_new(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO, nickname);
|
|
|
|
if (!launched) {
|
|
|
|
log_fn(LOG_WARN, "Can't launch circuit to establish introduction at '%s'",
|
|
|
|
nickname);
|
|
|
|
return -1;
|
|
|
|
}
|
2004-04-08 04:10:43 +02:00
|
|
|
strcpy(launched->rend_query, service->service_id);
|
2004-04-03 04:40:30 +02:00
|
|
|
memcpy(launched->rend_pk_digest, service->pk_digest, DIGEST_LEN);
|
2004-04-02 00:21:01 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
rend_service_intro_is_ready(circuit_t *circuit)
|
|
|
|
{
|
|
|
|
rend_service_t *service;
|
|
|
|
int len, r;
|
|
|
|
char buf[RELAY_PAYLOAD_SIZE];
|
2004-04-06 05:44:36 +02:00
|
|
|
char auth[DIGEST_LEN + 9];
|
2004-04-12 07:12:50 +02:00
|
|
|
char serviceid[REND_SERVICE_ID_LEN+1];
|
2004-04-02 00:21:01 +02:00
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(circuit->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO);
|
|
|
|
tor_assert(CIRCUIT_IS_ORIGIN(circuit) && circuit->cpath);
|
2004-04-03 02:58:54 +02:00
|
|
|
|
2004-04-12 07:12:50 +02:00
|
|
|
if (base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
|
|
|
|
circuit->rend_pk_digest,10)) {
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(0);
|
2004-04-12 07:12:50 +02:00
|
|
|
}
|
|
|
|
|
2004-04-03 01:44:46 +02:00
|
|
|
service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
|
2004-04-02 00:21:01 +02:00
|
|
|
if (!service) {
|
2004-04-03 02:58:54 +02:00
|
|
|
log_fn(LOG_WARN, "Unrecognized service ID %s on introduction circuit %d",
|
2004-04-12 07:12:50 +02:00
|
|
|
serviceid, circuit->n_circ_id);
|
2004-04-02 00:21:01 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2004-04-03 02:58:54 +02:00
|
|
|
log_fn(LOG_INFO,
|
|
|
|
"Established circuit %d as introduction point for service %s",
|
2004-04-12 07:12:50 +02:00
|
|
|
circuit->n_circ_id, serviceid);
|
2004-04-03 02:58:54 +02:00
|
|
|
|
2004-04-02 00:21:01 +02:00
|
|
|
/* Build the payload for a RELAY_ESTABLISH_INTRO cell. */
|
|
|
|
len = crypto_pk_asn1_encode(service->private_key, buf+2,
|
|
|
|
RELAY_PAYLOAD_SIZE-2);
|
2004-04-28 22:13:21 +02:00
|
|
|
set_uint16(buf, htons((uint16_t)len));
|
2004-04-02 00:21:01 +02:00
|
|
|
len += 2;
|
2004-04-03 04:40:30 +02:00
|
|
|
memcpy(auth, circuit->cpath->prev->handshake_digest, DIGEST_LEN);
|
|
|
|
memcpy(auth+DIGEST_LEN, "INTRODUCE", 9);
|
|
|
|
if (crypto_digest(auth, DIGEST_LEN+9, buf+len))
|
2004-04-02 00:21:01 +02:00
|
|
|
goto err;
|
|
|
|
len += 20;
|
|
|
|
r = crypto_pk_private_sign_digest(service->private_key, buf, len, buf+len);
|
|
|
|
if (r<0) {
|
|
|
|
log_fn(LOG_WARN, "Couldn't sign introduction request");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
len += r;
|
|
|
|
|
|
|
|
if (connection_edge_send_command(NULL, circuit,RELAY_COMMAND_ESTABLISH_INTRO,
|
|
|
|
buf, len, circuit->cpath->prev)<0) {
|
2004-04-03 02:58:54 +02:00
|
|
|
log_fn(LOG_WARN,
|
|
|
|
"Couldn't send introduction request for service %s on circuit %d",
|
2004-04-12 07:12:50 +02:00
|
|
|
serviceid, circuit->n_circ_id);
|
2004-04-02 00:21:01 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
err:
|
|
|
|
circuit_mark_for_close(circuit);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* now out-of-date.*/
|
2004-04-03 06:55:22 +02:00
|
|
|
int
|
|
|
|
rend_service_intro_established(circuit_t *circuit, const char *request, int request_len)
|
|
|
|
{
|
2004-04-13 19:16:47 +02:00
|
|
|
rend_service_t *service;
|
|
|
|
|
2004-04-03 06:55:22 +02:00
|
|
|
if (circuit->purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO) {
|
|
|
|
log_fn(LOG_WARN, "received INTRO_ESTABLISHED cell on non-intro circuit");
|
|
|
|
goto err;
|
|
|
|
}
|
2004-04-13 19:16:47 +02:00
|
|
|
service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
|
|
|
|
if (!service) {
|
|
|
|
log_fn(LOG_WARN, "Unknown service on introduction circuit %d",
|
|
|
|
circuit->n_circ_id);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
service->desc_is_dirty = 1;
|
2004-04-03 06:55:22 +02:00
|
|
|
circuit->purpose = CIRCUIT_PURPOSE_S_INTRO;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
circuit_mark_for_close(circuit);
|
|
|
|
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
|
|
|
|
rend_service_rendezvous_is_ready(circuit_t *circuit)
|
|
|
|
{
|
|
|
|
rend_service_t *service;
|
|
|
|
char buf[RELAY_PAYLOAD_SIZE];
|
|
|
|
crypt_path_t *hop;
|
2004-04-12 07:12:50 +02:00
|
|
|
char serviceid[REND_SERVICE_ID_LEN+1];
|
2004-04-03 02:58:54 +02:00
|
|
|
char hexcookie[9];
|
2004-04-02 00:21:01 +02:00
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(circuit->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
|
|
|
|
tor_assert(circuit->cpath);
|
|
|
|
tor_assert(circuit->build_state);
|
2004-04-02 00:21:01 +02:00
|
|
|
hop = circuit->build_state->pending_final_cpath;
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(hop);
|
2004-04-02 00:21:01 +02:00
|
|
|
|
2004-04-03 02:58:54 +02:00
|
|
|
hex_encode(circuit->rend_cookie, 4, hexcookie);
|
2004-04-12 07:12:50 +02:00
|
|
|
if (base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
|
|
|
|
circuit->rend_pk_digest,10)) {
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(0);
|
2004-04-12 07:12:50 +02:00
|
|
|
}
|
2004-04-03 02:58:54 +02:00
|
|
|
|
|
|
|
log_fn(LOG_INFO,
|
|
|
|
"Done building circuit %d to rendezvous with cookie %s for service %s",
|
2004-04-12 07:12:50 +02:00
|
|
|
circuit->n_circ_id, hexcookie, serviceid);
|
2004-04-03 02:58:54 +02:00
|
|
|
|
2004-04-03 01:44:46 +02:00
|
|
|
service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
|
2004-04-02 00:21:01 +02:00
|
|
|
if (!service) {
|
|
|
|
log_fn(LOG_WARN, "Internal error: unrecognized service ID on introduction circuit");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All we need to do is send a RELAY_RENDEZVOUS1 cell... */
|
|
|
|
memcpy(buf, circuit->rend_cookie, REND_COOKIE_LEN);
|
|
|
|
if (crypto_dh_get_public(hop->handshake_state,
|
|
|
|
buf+REND_COOKIE_LEN, DH_KEY_LEN)<0) {
|
|
|
|
log_fn(LOG_WARN,"Couldn't get DH public key");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
memcpy(buf+REND_COOKIE_LEN+DH_KEY_LEN, hop->handshake_digest,
|
2004-04-03 04:40:30 +02:00
|
|
|
DIGEST_LEN);
|
2004-04-02 00:21:01 +02:00
|
|
|
|
|
|
|
/* Send the cell */
|
|
|
|
if (connection_edge_send_command(NULL, circuit, 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) {
|
|
|
|
log_fn(LOG_WARN, "Couldn't send RENDEZVOUS1 cell");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2004-04-06 22:23:58 +02:00
|
|
|
crypto_dh_free(hop->handshake_state);
|
|
|
|
hop->handshake_state = NULL;
|
|
|
|
|
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
|
|
|
|
* that bob thinks alice has.
|
|
|
|
*/
|
|
|
|
hop->package_window = CIRCWINDOW_START;
|
|
|
|
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. */
|
|
|
|
circuit->purpose = CIRCUIT_PURPOSE_S_REND_JOINED;
|
|
|
|
|
|
|
|
return;
|
|
|
|
err:
|
|
|
|
circuit_mark_for_close(circuit);
|
|
|
|
}
|
|
|
|
|
2004-04-01 05:34:05 +02:00
|
|
|
/******
|
|
|
|
* Manage introduction points
|
|
|
|
******/
|
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
|
2004-04-13 03:15:06 +02:00
|
|
|
* 'router' for the service whose public key is 'pk_digest'. Return
|
|
|
|
* NULL if no such service is found.
|
2004-04-06 05:44:36 +02:00
|
|
|
*/
|
2004-04-03 06:55:22 +02:00
|
|
|
static circuit_t *
|
|
|
|
find_intro_circuit(routerinfo_t *router, const char *pk_digest)
|
|
|
|
{
|
|
|
|
circuit_t *circ = NULL;
|
|
|
|
|
|
|
|
while ((circ = circuit_get_next_by_pk_and_purpose(circ,pk_digest,
|
|
|
|
CIRCUIT_PURPOSE_S_INTRO))) {
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(circ->cpath);
|
2004-04-13 03:15:06 +02:00
|
|
|
if (circ->build_state->chosen_exit &&
|
|
|
|
!strcasecmp(circ->build_state->chosen_exit, router->nickname)) {
|
2004-04-03 06:55:22 +02:00
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
circ = NULL;
|
|
|
|
while ((circ = circuit_get_next_by_pk_and_purpose(circ,pk_digest,
|
|
|
|
CIRCUIT_PURPOSE_S_ESTABLISH_INTRO))) {
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(circ->cpath);
|
2004-04-13 03:15:06 +02:00
|
|
|
if (circ->build_state->chosen_exit &&
|
|
|
|
!strcasecmp(circ->build_state->chosen_exit, router->nickname)) {
|
2004-04-03 06:55:22 +02:00
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** If the directory servers don't have an up-to-date descriptor for
|
2004-05-05 23:32:43 +02:00
|
|
|
* 'service', Encode and sign the service descriptor for 'service',
|
|
|
|
* and upload it to all the dirservers.
|
|
|
|
*/
|
2004-04-13 19:16:47 +02:00
|
|
|
static void
|
|
|
|
upload_service_descriptor(rend_service_t *service)
|
|
|
|
{
|
|
|
|
char *desc;
|
|
|
|
int desc_len;
|
|
|
|
if (!service->desc_is_dirty)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Update the descriptor. */
|
|
|
|
rend_service_update_descriptor(service);
|
|
|
|
if (rend_encode_service_descriptor(service->desc,
|
|
|
|
service->private_key,
|
|
|
|
&desc, &desc_len)<0) {
|
|
|
|
log_fn(LOG_WARN, "Couldn't encode service descriptor; not uploading");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Post it to the dirservers */
|
|
|
|
router_post_to_dirservers(DIR_PURPOSE_UPLOAD_RENDDESC, desc, desc_len);
|
|
|
|
tor_free(desc);
|
|
|
|
|
|
|
|
service->desc_is_dirty = 0;
|
|
|
|
}
|
|
|
|
|
2004-04-08 00:06:54 +02:00
|
|
|
/* XXXX Make this longer once directories remember service descriptors across
|
|
|
|
* restarts.*/
|
|
|
|
#define MAX_SERVICE_PUBLICATION_INTERVAL (15*60)
|
2004-04-03 06:55:22 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** For every service, check how many intro points it currently has, and:
|
2004-04-03 01:01:00 +02:00
|
|
|
* - Pick new intro points as necessary.
|
|
|
|
* - Launch circuits to any new intro points.
|
|
|
|
*/
|
2004-04-13 19:16:47 +02:00
|
|
|
void rend_services_introduce(void) {
|
2004-04-02 00:21:01 +02:00
|
|
|
int i,j,r;
|
2004-04-01 05:23:28 +02:00
|
|
|
routerinfo_t *router;
|
|
|
|
routerlist_t *rl;
|
2004-04-02 00:21:01 +02:00
|
|
|
rend_service_t *service;
|
2004-04-13 19:16:47 +02:00
|
|
|
char *intro;
|
|
|
|
int changed, prev_intro_nodes;
|
2004-04-07 23:36:03 +02:00
|
|
|
smartlist_t *intro_routers, *exclude_routers;
|
2004-04-01 05:23:28 +02:00
|
|
|
|
|
|
|
router_get_routerlist(&rl);
|
2004-04-06 23:19:59 +02:00
|
|
|
intro_routers = smartlist_create();
|
2004-04-07 23:36:03 +02:00
|
|
|
exclude_routers = smartlist_create();
|
2004-04-01 05:23:28 +02:00
|
|
|
|
2004-04-03 02:58:54 +02:00
|
|
|
for (i=0; i< smartlist_len(rend_service_list); ++i) {
|
2004-04-06 23:19:59 +02:00
|
|
|
smartlist_clear(intro_routers);
|
2004-04-03 02:58:54 +02:00
|
|
|
service = smartlist_get(rend_service_list, i);
|
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(service);
|
2004-04-03 01:01:00 +02:00
|
|
|
changed = 0;
|
|
|
|
|
2004-04-13 19:16:47 +02:00
|
|
|
/* Find out which introduction points we have in progress for this service. */
|
2004-04-03 02:58:54 +02:00
|
|
|
for (j=0;j< smartlist_len(service->intro_nodes); ++j) {
|
2004-04-16 13:48:03 +02:00
|
|
|
intro = smartlist_get(service->intro_nodes, j);
|
|
|
|
router = router_get_by_nickname(intro);
|
2004-04-03 06:55:22 +02:00
|
|
|
if (!router || !find_intro_circuit(router,service->pk_digest)) {
|
2004-04-16 13:48:03 +02:00
|
|
|
log_fn(LOG_INFO,"Giving up on %s as intro point for %s.",
|
|
|
|
intro, service->service_id);
|
2004-04-03 06:55:22 +02:00
|
|
|
smartlist_del(service->intro_nodes,j--);
|
2004-04-13 19:20:41 +02:00
|
|
|
changed = service->desc_is_dirty = 1;
|
2004-04-03 01:01:00 +02:00
|
|
|
}
|
2004-04-06 23:19:59 +02:00
|
|
|
smartlist_add(intro_routers, router);
|
2004-04-03 01:01:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We have enough intro points, and the intro points we thought we had were
|
|
|
|
* all connected.
|
|
|
|
*/
|
2004-04-03 02:58:54 +02:00
|
|
|
if (!changed && smartlist_len(service->intro_nodes) >= NUM_INTRO_POINTS)
|
2004-04-03 01:01:00 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Remember how many introduction circuits we started with. */
|
2004-04-03 02:58:54 +02:00
|
|
|
prev_intro_nodes = smartlist_len(service->intro_nodes);
|
2004-04-01 05:23:28 +02:00
|
|
|
|
2004-04-07 23:36:03 +02:00
|
|
|
smartlist_add_all(exclude_routers, intro_routers);
|
2004-04-01 05:23:28 +02:00
|
|
|
/* The directory is now here. Pick three ORs as intro points. */
|
2004-04-03 01:01:00 +02:00
|
|
|
for (j=prev_intro_nodes; j < NUM_INTRO_POINTS; ++j) {
|
|
|
|
router = router_choose_random_node(rl,
|
|
|
|
service->intro_prefer_nodes,
|
|
|
|
service->intro_exclude_nodes,
|
2004-04-07 23:36:03 +02:00
|
|
|
exclude_routers);
|
2004-04-03 01:01:00 +02:00
|
|
|
if (!router) {
|
2004-04-16 13:48:03 +02:00
|
|
|
log_fn(LOG_WARN, "Could only establish %d introduction points for %s",
|
|
|
|
smartlist_len(service->intro_nodes), service->service_id);
|
2004-04-03 01:01:00 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
changed = 1;
|
2004-04-06 23:19:59 +02:00
|
|
|
smartlist_add(intro_routers, router);
|
2004-04-07 23:36:03 +02:00
|
|
|
smartlist_add(exclude_routers, router);
|
2004-04-03 01:01:00 +02:00
|
|
|
smartlist_add(service->intro_nodes, tor_strdup(router->nickname));
|
2004-04-16 13:48:03 +02:00
|
|
|
log_fn(LOG_INFO,"Picked router %s as an intro point for %s.", router->nickname,
|
|
|
|
service->service_id);
|
2004-04-01 05:23:28 +02:00
|
|
|
}
|
|
|
|
|
2004-04-25 23:32:04 +02:00
|
|
|
/* Reset exclude_routers, for the next time around the loop. */
|
|
|
|
smartlist_clear(exclude_routers);
|
2004-04-07 23:36:03 +02:00
|
|
|
|
2004-04-13 19:16:47 +02:00
|
|
|
/* If there's no need to launch new circuits, stop here. */
|
|
|
|
if (!changed)
|
2004-04-03 01:01:00 +02:00
|
|
|
continue;
|
2004-04-01 05:23:28 +02:00
|
|
|
|
2004-04-03 01:01:00 +02:00
|
|
|
/* Establish new introduction points. */
|
2004-04-03 02:58:54 +02:00
|
|
|
for (j=prev_intro_nodes; j < smartlist_len(service->intro_nodes); ++j) {
|
|
|
|
intro = smartlist_get(service->intro_nodes, j);
|
2004-04-03 01:01:00 +02:00
|
|
|
r = rend_service_launch_establish_intro(service, intro);
|
|
|
|
if (r<0) {
|
2004-04-16 13:48:03 +02:00
|
|
|
log_fn(LOG_WARN, "Error launching circuit to node %s for service %s",
|
|
|
|
intro, service->service_id);
|
2004-04-03 01:01:00 +02:00
|
|
|
}
|
|
|
|
}
|
2004-04-02 00:21:01 +02:00
|
|
|
}
|
2004-04-06 23:19:59 +02:00
|
|
|
smartlist_free(intro_routers);
|
2004-04-07 23:36:03 +02:00
|
|
|
smartlist_free(exclude_routers);
|
2004-04-01 05:23:28 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Regenerate and upload rendezvous service descriptors for all
|
2004-05-05 23:32:43 +02:00
|
|
|
* services. If 'force' is false, skip services where we've already
|
|
|
|
* uploaded an up-to-date copy; if 'force' is true, regenerate and
|
|
|
|
* upload everything.
|
|
|
|
*/
|
2004-04-13 19:16:47 +02:00
|
|
|
void
|
|
|
|
rend_services_upload(int force)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
rend_service_t *service;
|
|
|
|
|
|
|
|
for (i=0; i< smartlist_len(rend_service_list); ++i) {
|
|
|
|
service = smartlist_get(rend_service_list, i);
|
|
|
|
if (force)
|
|
|
|
service->desc_is_dirty = 1;
|
|
|
|
if (service->desc_is_dirty)
|
|
|
|
upload_service_descriptor(service);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Log the status of introduction points for all rendezvous services
|
2004-05-05 23:32:43 +02:00
|
|
|
* at log severity 'serverity'.
|
|
|
|
*/
|
2004-04-09 22:02:16 +02:00
|
|
|
void
|
|
|
|
rend_service_dump_stats(int severity)
|
|
|
|
{
|
|
|
|
int i,j;
|
|
|
|
routerinfo_t *router;
|
|
|
|
rend_service_t *service;
|
|
|
|
char *nickname;
|
|
|
|
circuit_t *circ;
|
|
|
|
|
|
|
|
for (i=0; i < smartlist_len(rend_service_list); ++i) {
|
|
|
|
service = smartlist_get(rend_service_list, i);
|
|
|
|
log(severity, "Service configured in %s:", service->directory);
|
|
|
|
for (j=0; j < smartlist_len(service->intro_nodes); ++j) {
|
|
|
|
nickname = smartlist_get(service->intro_nodes, j);
|
|
|
|
router = router_get_by_nickname(smartlist_get(service->intro_nodes,j));
|
|
|
|
if (!router) {
|
|
|
|
log(severity, " Intro point at %s: unrecognized router",nickname);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
circ = find_intro_circuit(router, service->pk_digest);
|
|
|
|
if (!circ) {
|
|
|
|
log(severity, " Intro point at %s: no circuit",nickname);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
log(severity, " Intro point at %s: circuit is %s",nickname,
|
|
|
|
circuit_state_to_string[circ->state]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** 'conn' is a rendezvous exit stream. Look up the hidden service for
|
2004-05-05 23:32:43 +02:00
|
|
|
* 'circ', and look up the port and address based on conn->port.
|
|
|
|
* Assign the actual conn->addr and conn->port. Return -1 if failure,
|
|
|
|
* or 0 for success.
|
2004-04-07 00:05:49 +02:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
rend_service_set_connection_addr_port(connection_t *conn, circuit_t *circ)
|
|
|
|
{
|
|
|
|
rend_service_t *service;
|
|
|
|
int i;
|
|
|
|
rend_service_port_config_t *p;
|
2004-04-15 04:02:46 +02:00
|
|
|
char serviceid[REND_SERVICE_ID_LEN+1];
|
2004-04-07 00:05:49 +02:00
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(circ->purpose == CIRCUIT_PURPOSE_S_REND_JOINED);
|
2004-04-15 01:52:29 +02:00
|
|
|
log_fn(LOG_DEBUG,"beginning to hunt for addr/port");
|
2004-04-12 07:12:50 +02:00
|
|
|
if (base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
|
|
|
|
circ->rend_pk_digest,10)) {
|
2004-04-16 16:26:23 +02:00
|
|
|
log_fn(LOG_WARN,"bug: base32 failed");
|
2004-04-12 07:12:50 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2004-04-07 00:05:49 +02:00
|
|
|
service = rend_service_get_by_pk_digest(circ->rend_pk_digest);
|
|
|
|
if (!service) {
|
|
|
|
log_fn(LOG_WARN, "Couldn't find any service associated with pk %s on rendezvous circuit %d; closing",
|
2004-04-12 07:12:50 +02:00
|
|
|
serviceid, circ->n_circ_id);
|
2004-04-15 03:23:43 +02:00
|
|
|
return -1;
|
2004-04-07 00:05:49 +02:00
|
|
|
}
|
|
|
|
for (i = 0; i < smartlist_len(service->ports); ++i) {
|
|
|
|
p = smartlist_get(service->ports, i);
|
|
|
|
if (conn->port == p->virtual_port) {
|
|
|
|
conn->addr = p->real_address;
|
|
|
|
conn->port = p->real_port;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2004-04-16 16:26:23 +02:00
|
|
|
log_fn(LOG_INFO, "No virtual port mapping exists for port %d on service %s",
|
2004-04-12 07:12:50 +02:00
|
|
|
conn->port,serviceid);
|
2004-04-07 00:05:49 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-04-01 05:34:05 +02:00
|
|
|
/*
|
|
|
|
Local Variables:
|
|
|
|
mode:c
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
c-basic-offset:2
|
|
|
|
End:
|
|
|
|
*/
|