2003-09-27 23:30:10 +02:00
|
|
|
/* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include "or.h"
|
|
|
|
|
2004-03-29 21:28:16 +02:00
|
|
|
/* How old do we allow a router to get before removing it? (seconds) */
|
|
|
|
#define ROUTER_MAX_AGE (60*60*24)
|
|
|
|
|
2003-10-22 18:41:35 +02:00
|
|
|
/* How far in the future do we allow a router to get? (seconds) */
|
|
|
|
#define ROUTER_ALLOW_SKEW (30*60)
|
|
|
|
|
2003-09-30 01:14:49 +02:00
|
|
|
extern or_options_t options; /* command-line and config-file options */
|
|
|
|
|
2003-09-27 23:30:10 +02:00
|
|
|
static int the_directory_is_dirty = 1;
|
|
|
|
|
2003-10-01 02:43:34 +02:00
|
|
|
static int list_running_servers(char **nicknames_out);
|
2004-04-13 22:06:08 +02:00
|
|
|
static void directory_remove_unrecognized(void);
|
2003-10-01 02:43:34 +02:00
|
|
|
|
2003-09-29 09:50:08 +02:00
|
|
|
/************** Fingerprint handling code ************/
|
|
|
|
|
2003-09-27 23:30:10 +02:00
|
|
|
typedef struct fingerprint_entry_t {
|
|
|
|
char *nickname;
|
|
|
|
char *fingerprint;
|
|
|
|
} fingerprint_entry_t;
|
|
|
|
|
|
|
|
static fingerprint_entry_t fingerprint_list[MAX_ROUTERS_IN_DIR];
|
|
|
|
static int n_fingerprints = 0;
|
|
|
|
|
2004-03-29 21:50:59 +02:00
|
|
|
void /* Should be static; exposed for testing */
|
2003-09-30 21:27:54 +02:00
|
|
|
add_fingerprint_to_dir(const char *nickname, const char *fp)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < n_fingerprints; ++i) {
|
|
|
|
if (!strcasecmp(fingerprint_list[i].nickname,nickname)) {
|
|
|
|
free(fingerprint_list[i].fingerprint);
|
2003-10-04 05:29:09 +02:00
|
|
|
fingerprint_list[i].fingerprint = tor_strdup(fp);
|
2003-09-30 21:27:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2003-10-04 05:29:09 +02:00
|
|
|
fingerprint_list[n_fingerprints].nickname = tor_strdup(nickname);
|
|
|
|
fingerprint_list[n_fingerprints].fingerprint = tor_strdup(fp);
|
2003-09-30 21:27:54 +02:00
|
|
|
++n_fingerprints;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk)
|
|
|
|
{
|
|
|
|
char fp[FINGERPRINT_LEN+1];
|
|
|
|
if (crypto_pk_get_fingerprint(pk, fp)<0) {
|
|
|
|
log_fn(LOG_ERR, "Error computing fingerprint");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
add_fingerprint_to_dir(nickname, fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-09-27 23:30:10 +02:00
|
|
|
/* return 0 on success, -1 on failure */
|
2003-12-14 07:03:46 +01:00
|
|
|
int
|
2003-09-27 23:30:10 +02:00
|
|
|
dirserv_parse_fingerprint_file(const char *fname)
|
|
|
|
{
|
|
|
|
FILE *file;
|
2003-09-29 09:50:08 +02:00
|
|
|
char line[FINGERPRINT_LEN+MAX_NICKNAME_LEN+20+1];
|
|
|
|
char *nickname, *fingerprint;
|
2003-09-27 23:30:10 +02:00
|
|
|
fingerprint_entry_t fingerprint_list_tmp[MAX_ROUTERS_IN_DIR];
|
|
|
|
int n_fingerprints_tmp = 0;
|
2003-09-29 09:50:08 +02:00
|
|
|
int i, result;
|
|
|
|
|
|
|
|
if(!(file = fopen(fname, "r"))) {
|
2003-10-10 03:48:32 +02:00
|
|
|
log_fn(LOG_WARN, "Cannot open fingerprint file %s", fname);
|
2003-09-29 09:50:08 +02:00
|
|
|
return -1;
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
2003-09-29 09:50:08 +02:00
|
|
|
while( (result=parse_line_from_file(line, sizeof(line),file,&nickname,&fingerprint)) > 0) {
|
2003-09-27 23:30:10 +02:00
|
|
|
if (strlen(nickname) > MAX_NICKNAME_LEN) {
|
2003-10-10 03:48:32 +02:00
|
|
|
log(LOG_WARN, "Nickname %s too long in fingerprint file. Skipping.", nickname);
|
2003-09-29 09:50:08 +02:00
|
|
|
continue;
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
2003-09-29 09:50:08 +02:00
|
|
|
if(strlen(fingerprint) != FINGERPRINT_LEN ||
|
|
|
|
!crypto_pk_check_fingerprint_syntax(fingerprint)) {
|
2003-10-10 03:48:32 +02:00
|
|
|
log_fn(LOG_WARN, "Invalid fingerprint (nickname %s, fingerprint %s). Skipping.",
|
2003-09-29 09:50:08 +02:00
|
|
|
nickname, fingerprint);
|
|
|
|
continue;
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
|
|
|
for (i = 0; i < n_fingerprints_tmp; ++i) {
|
|
|
|
if (0==strcasecmp(fingerprint_list_tmp[i].nickname, nickname)) {
|
2003-10-10 03:48:32 +02:00
|
|
|
log(LOG_WARN, "Duplicate nickname %s. Skipping.",nickname);
|
2003-09-29 09:50:08 +02:00
|
|
|
break; /* out of the for. the 'if' below means skip to the next line. */
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
|
|
|
}
|
2003-09-29 09:50:08 +02:00
|
|
|
if(i == n_fingerprints_tmp) { /* not a duplicate */
|
2003-10-04 05:29:09 +02:00
|
|
|
fingerprint_list_tmp[n_fingerprints_tmp].nickname = tor_strdup(nickname);
|
|
|
|
fingerprint_list_tmp[n_fingerprints_tmp].fingerprint = tor_strdup(fingerprint);
|
2003-09-29 09:50:08 +02:00
|
|
|
++n_fingerprints_tmp;
|
|
|
|
}
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
2003-09-29 09:50:08 +02:00
|
|
|
fclose(file);
|
|
|
|
if(result == 0) { /* eof; replace the global fingerprints list. */
|
|
|
|
dirserv_free_fingerprint_list();
|
|
|
|
memcpy(fingerprint_list, fingerprint_list_tmp,
|
|
|
|
sizeof(fingerprint_entry_t)*n_fingerprints_tmp);
|
|
|
|
n_fingerprints = n_fingerprints_tmp;
|
2004-04-13 22:06:08 +02:00
|
|
|
/* Delete any routers whose fingerprints we no longer recognize */
|
|
|
|
directory_remove_unrecognized();
|
2003-09-29 09:50:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* error */
|
2003-10-10 03:48:32 +02:00
|
|
|
log_fn(LOG_WARN, "Error reading from fingerprint file");
|
2003-09-27 23:30:10 +02:00
|
|
|
for (i = 0; i < n_fingerprints_tmp; ++i) {
|
|
|
|
free(fingerprint_list_tmp[i].nickname);
|
|
|
|
free(fingerprint_list_tmp[i].fingerprint);
|
|
|
|
}
|
|
|
|
return -1;
|
2003-12-14 06:25:23 +01:00
|
|
|
}
|
2003-09-27 23:30:10 +02:00
|
|
|
|
2003-11-12 06:12:51 +01:00
|
|
|
/* return 1 if router's identity and nickname match,
|
|
|
|
* -1 if they don't match, 0 if the nickname is not known. */
|
2003-09-27 23:30:10 +02:00
|
|
|
int
|
|
|
|
dirserv_router_fingerprint_is_known(const routerinfo_t *router)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
fingerprint_entry_t *ent =NULL;
|
|
|
|
char fp[FINGERPRINT_LEN+1];
|
|
|
|
|
2003-09-30 21:27:54 +02:00
|
|
|
log_fn(LOG_DEBUG, "%d fingerprints known.", n_fingerprints);
|
2003-09-27 23:30:10 +02:00
|
|
|
for (i=0;i<n_fingerprints;++i) {
|
2003-09-30 21:27:54 +02:00
|
|
|
log_fn(LOG_DEBUG,"%s vs %s", router->nickname, fingerprint_list[i].nickname);
|
2003-09-27 23:30:10 +02:00
|
|
|
if (!strcasecmp(router->nickname,fingerprint_list[i].nickname)) {
|
|
|
|
ent = &fingerprint_list[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-12-13 23:53:17 +01:00
|
|
|
|
2003-09-29 09:50:08 +02:00
|
|
|
if (!ent) { /* No such server known */
|
2003-11-12 06:12:51 +01:00
|
|
|
log_fn(LOG_INFO,"no fingerprint found for %s",router->nickname);
|
2003-09-27 23:30:10 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (crypto_pk_get_fingerprint(router->identity_pkey, fp)) {
|
2003-10-10 03:48:32 +02:00
|
|
|
log_fn(LOG_WARN,"error computing fingerprint");
|
2003-11-12 06:12:51 +01:00
|
|
|
return -1;
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
|
|
|
if (0==strcasecmp(ent->fingerprint, fp)) {
|
2003-09-30 21:27:54 +02:00
|
|
|
log_fn(LOG_DEBUG,"good fingerprint for %s",router->nickname);
|
2003-09-29 09:50:08 +02:00
|
|
|
return 1; /* Right fingerprint. */
|
2003-09-27 23:30:10 +02:00
|
|
|
} else {
|
2003-10-10 03:48:32 +02:00
|
|
|
log_fn(LOG_WARN,"mismatched fingerprint for %s",router->nickname);
|
2003-11-12 06:12:51 +01:00
|
|
|
return -1; /* Wrong fingerprint. */
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-17 22:09:31 +01:00
|
|
|
void
|
2003-09-27 23:30:10 +02:00
|
|
|
dirserv_free_fingerprint_list()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < n_fingerprints; ++i) {
|
|
|
|
free(fingerprint_list[i].nickname);
|
|
|
|
free(fingerprint_list[i].fingerprint);
|
|
|
|
}
|
|
|
|
n_fingerprints = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Descriptor list
|
|
|
|
*/
|
|
|
|
typedef struct descriptor_entry_t {
|
|
|
|
char *nickname;
|
|
|
|
time_t published;
|
|
|
|
size_t desc_len;
|
|
|
|
char *descriptor;
|
2004-04-13 22:06:08 +02:00
|
|
|
routerinfo_t *router;
|
2003-09-27 23:30:10 +02:00
|
|
|
} descriptor_entry_t;
|
|
|
|
|
|
|
|
static descriptor_entry_t *descriptor_list[MAX_ROUTERS_IN_DIR];
|
|
|
|
static int n_descriptors = 0;
|
|
|
|
|
|
|
|
static void free_descriptor_entry(descriptor_entry_t *desc)
|
|
|
|
{
|
2003-10-21 11:48:17 +02:00
|
|
|
tor_free(desc->descriptor);
|
|
|
|
tor_free(desc->nickname);
|
2004-04-13 22:06:08 +02:00
|
|
|
routerinfo_free(desc->router);
|
2003-09-27 23:30:10 +02:00
|
|
|
free(desc);
|
|
|
|
}
|
|
|
|
|
2003-12-14 07:03:46 +01:00
|
|
|
void
|
2003-09-27 23:30:10 +02:00
|
|
|
dirserv_free_descriptors()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < n_descriptors; ++i) {
|
|
|
|
free_descriptor_entry(descriptor_list[i]);
|
|
|
|
}
|
|
|
|
n_descriptors = 0;
|
|
|
|
}
|
|
|
|
|
2004-03-12 13:43:13 +01:00
|
|
|
/* Return 1 if descriptor is well-formed and accepted;
|
2004-03-30 01:23:01 +02:00
|
|
|
* 0 if well-formed and server or descriptor is unapproved;
|
|
|
|
* -1 if not well-formed or other error.
|
2004-03-12 13:43:13 +01:00
|
|
|
*
|
|
|
|
* Update *desc to point after the descriptor if the
|
2003-11-12 06:12:51 +01:00
|
|
|
* descriptor is well-formed.
|
|
|
|
*/
|
2003-09-27 23:30:10 +02:00
|
|
|
int
|
|
|
|
dirserv_add_descriptor(const char **desc)
|
|
|
|
{
|
|
|
|
descriptor_entry_t **desc_ent_ptr;
|
|
|
|
routerinfo_t *ri = NULL;
|
2003-11-12 06:12:51 +01:00
|
|
|
int i, r;
|
2003-09-27 23:30:10 +02:00
|
|
|
char *start, *end;
|
2003-12-09 00:45:37 +01:00
|
|
|
char *desc_tmp = NULL;
|
|
|
|
const char *cp;
|
2003-09-27 23:30:10 +02:00
|
|
|
size_t desc_len;
|
2004-03-29 21:28:16 +02:00
|
|
|
time_t now;
|
2003-09-27 23:30:10 +02:00
|
|
|
|
|
|
|
start = strstr(*desc, "router ");
|
|
|
|
if (!start) {
|
2004-04-05 02:47:48 +02:00
|
|
|
log_fn(LOG_WARN, "no 'router' line found. This is not a descriptor.");
|
2004-03-12 13:43:13 +01:00
|
|
|
return -1;
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
2003-09-30 22:04:40 +02:00
|
|
|
if ((end = strstr(start+6, "\nrouter "))) {
|
2003-09-27 23:30:10 +02:00
|
|
|
++end; /* Include NL. */
|
2003-09-30 22:04:40 +02:00
|
|
|
} else if ((end = strstr(start+6, "\ndirectory-signature"))) {
|
|
|
|
++end;
|
2003-09-27 23:30:10 +02:00
|
|
|
} else {
|
|
|
|
end = start+strlen(start);
|
|
|
|
}
|
|
|
|
desc_len = end-start;
|
2003-12-09 00:45:37 +01:00
|
|
|
cp = desc_tmp = tor_strndup(start, desc_len);
|
2003-09-27 23:30:10 +02:00
|
|
|
|
|
|
|
/* Check: is the descriptor syntactically valid? */
|
2004-03-05 06:48:28 +01:00
|
|
|
ri = router_get_entry_from_string(cp, NULL);
|
2004-03-12 13:43:13 +01:00
|
|
|
tor_free(desc_tmp);
|
2003-09-27 23:30:10 +02:00
|
|
|
if (!ri) {
|
2003-10-10 03:48:32 +02:00
|
|
|
log(LOG_WARN, "Couldn't parse descriptor");
|
2004-03-12 13:43:13 +01:00
|
|
|
return -1;
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
|
|
|
/* Okay. Now check whether the fingerprint is recognized. */
|
2003-11-12 06:12:51 +01:00
|
|
|
r = dirserv_router_fingerprint_is_known(ri);
|
|
|
|
if(r<1) {
|
|
|
|
if(r==0) {
|
2004-03-08 01:15:40 +01:00
|
|
|
char fp[FINGERPRINT_LEN+1];
|
2004-03-09 15:53:00 +01:00
|
|
|
log_fn(LOG_WARN, "Unknown nickname %s (%s:%d). Not adding.",
|
|
|
|
ri->nickname, ri->address, ri->or_port);
|
2004-03-08 01:15:40 +01:00
|
|
|
if (crypto_pk_get_fingerprint(ri->identity_pkey, fp) < 0) {
|
|
|
|
log_fn(LOG_WARN, "Error computing fingerprint for %s", ri->nickname);
|
|
|
|
} else {
|
|
|
|
log_fn(LOG_WARN, "Fingerprint line: %s %s", ri->nickname, fp);
|
|
|
|
}
|
2003-11-12 06:12:51 +01:00
|
|
|
} else {
|
|
|
|
log_fn(LOG_WARN, "Known nickname %s, wrong fingerprint. Not adding.", ri->nickname);
|
|
|
|
}
|
|
|
|
routerinfo_free(ri);
|
|
|
|
*desc = end;
|
|
|
|
return 0;
|
2003-10-22 18:41:35 +02:00
|
|
|
}
|
|
|
|
/* Is there too much clock skew? */
|
2004-03-29 21:28:16 +02:00
|
|
|
now = time(NULL);
|
|
|
|
if (ri->published_on > now+ROUTER_ALLOW_SKEW) {
|
2004-03-30 01:23:01 +02:00
|
|
|
log_fn(LOG_WARN, "Publication time for nickname %s is too far in the future; possible clock skew. Not adding.", ri->nickname);
|
2003-11-12 06:12:51 +01:00
|
|
|
routerinfo_free(ri);
|
|
|
|
*desc = end;
|
2004-03-30 01:23:01 +02:00
|
|
|
return 0;
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
2004-03-29 21:28:16 +02:00
|
|
|
if (ri->published_on < now-ROUTER_MAX_AGE) {
|
2004-03-30 01:23:01 +02:00
|
|
|
log_fn(LOG_WARN, "Publication time for router with nickname %s is too far in the past. Not adding.", ri->nickname);
|
2004-03-29 21:28:16 +02:00
|
|
|
routerinfo_free(ri);
|
|
|
|
*desc = end;
|
2004-03-30 01:23:01 +02:00
|
|
|
return 0;
|
2004-03-29 21:28:16 +02:00
|
|
|
}
|
|
|
|
|
2003-09-27 23:30:10 +02:00
|
|
|
/* Do we already have an entry for this router? */
|
|
|
|
desc_ent_ptr = NULL;
|
|
|
|
for (i = 0; i < n_descriptors; ++i) {
|
|
|
|
if (!strcasecmp(ri->nickname, descriptor_list[i]->nickname)) {
|
|
|
|
desc_ent_ptr = &descriptor_list[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (desc_ent_ptr) {
|
|
|
|
/* if so, decide whether to update it. */
|
|
|
|
if ((*desc_ent_ptr)->published > ri->published_on) {
|
|
|
|
/* We already have a newer descriptor */
|
2003-09-30 10:18:10 +02:00
|
|
|
log_fn(LOG_INFO,"We already have a newer desc for nickname %s. Not adding.",ri->nickname);
|
2004-03-12 13:43:13 +01:00
|
|
|
/* This isn't really an error; return success. */
|
2003-11-12 06:12:51 +01:00
|
|
|
routerinfo_free(ri);
|
2003-09-30 21:27:54 +02:00
|
|
|
*desc = end;
|
2004-03-12 13:43:13 +01:00
|
|
|
return 1;
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
|
|
|
/* We don't have a newer one; we'll update this one. */
|
|
|
|
free_descriptor_entry(*desc_ent_ptr);
|
|
|
|
} else {
|
|
|
|
/* Add this at the end. */
|
|
|
|
desc_ent_ptr = &descriptor_list[n_descriptors++];
|
2003-11-12 06:12:51 +01:00
|
|
|
/* XXX check if n_descriptors is too big */
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
2003-12-14 06:25:23 +01:00
|
|
|
|
2003-09-27 23:30:10 +02:00
|
|
|
(*desc_ent_ptr) = tor_malloc(sizeof(descriptor_entry_t));
|
2003-11-13 07:49:25 +01:00
|
|
|
(*desc_ent_ptr)->nickname = tor_strdup(ri->nickname);
|
2003-09-27 23:30:10 +02:00
|
|
|
(*desc_ent_ptr)->published = ri->published_on;
|
|
|
|
(*desc_ent_ptr)->desc_len = desc_len;
|
|
|
|
(*desc_ent_ptr)->descriptor = tor_malloc(desc_len+1);
|
|
|
|
strncpy((*desc_ent_ptr)->descriptor, start, desc_len);
|
|
|
|
(*desc_ent_ptr)->descriptor[desc_len] = '\0';
|
2004-04-13 22:06:08 +02:00
|
|
|
(*desc_ent_ptr)->router = ri;
|
2003-09-27 23:30:10 +02:00
|
|
|
*desc = end;
|
2003-12-14 06:25:23 +01:00
|
|
|
directory_set_dirty();
|
|
|
|
|
2004-03-12 13:43:13 +01:00
|
|
|
return 1;
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
|
|
|
|
2004-04-13 22:06:08 +02:00
|
|
|
static void
|
|
|
|
directory_remove_unrecognized(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < n_descriptors; ++i) {
|
|
|
|
if (dirserv_router_fingerprint_is_known(descriptor_list[i]->router)<=0) {
|
|
|
|
log(LOG_INFO, "Router %s is no longer recognized",
|
|
|
|
descriptor_list[i]->nickname);
|
|
|
|
free_descriptor_entry(descriptor_list[i]);
|
|
|
|
descriptor_list[i--] = descriptor_list[--n_descriptors];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-17 22:09:31 +01:00
|
|
|
void
|
2003-09-27 23:30:10 +02:00
|
|
|
directory_set_dirty()
|
|
|
|
{
|
|
|
|
the_directory_is_dirty = 1;
|
|
|
|
}
|
|
|
|
|
2003-12-17 22:09:31 +01:00
|
|
|
int
|
2003-09-27 23:30:10 +02:00
|
|
|
dirserv_init_from_directory_string(const char *dir)
|
|
|
|
{
|
|
|
|
const char *cp = dir;
|
|
|
|
while(1) {
|
|
|
|
cp = strstr(cp, "\nrouter ");
|
|
|
|
if (!cp) break;
|
|
|
|
++cp;
|
2004-03-12 13:43:13 +01:00
|
|
|
if (dirserv_add_descriptor(&cp) < 0) {
|
2003-09-27 23:30:10 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
--cp; /*Back up to newline.*/
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-12-14 07:03:46 +01:00
|
|
|
static int
|
2003-10-01 02:43:34 +02:00
|
|
|
list_running_servers(char **nicknames_out)
|
|
|
|
{
|
|
|
|
char *nickname_lst[MAX_ROUTERS_IN_DIR];
|
|
|
|
connection_t **connection_array;
|
|
|
|
int n_conns;
|
|
|
|
connection_t *conn;
|
|
|
|
char *cp;
|
|
|
|
int n = 0, i;
|
|
|
|
int length;
|
|
|
|
*nicknames_out = NULL;
|
|
|
|
nickname_lst[n++] = options.Nickname;
|
2003-12-14 07:03:46 +01:00
|
|
|
|
2003-10-01 02:43:34 +02:00
|
|
|
get_connection_array(&connection_array, &n_conns);
|
|
|
|
for (i = 0; i<n_conns; ++i) {
|
|
|
|
conn = connection_array[i];
|
|
|
|
if (conn->type != CONN_TYPE_OR || conn->state != OR_CONN_STATE_OPEN)
|
|
|
|
continue; /* only list successfully handshaked OR's. */
|
|
|
|
if(!conn->nickname) /* it's an OP, don't list it */
|
|
|
|
continue;
|
|
|
|
nickname_lst[n++] = conn->nickname;
|
|
|
|
}
|
|
|
|
length = n + 1; /* spaces + EOS + 1. */
|
|
|
|
for (i = 0; i<n; ++i) {
|
|
|
|
length += strlen(nickname_lst[i]);
|
|
|
|
}
|
2003-11-18 09:20:19 +01:00
|
|
|
*nicknames_out = tor_malloc_zero(length);
|
2003-10-01 02:43:34 +02:00
|
|
|
cp = *nicknames_out;
|
|
|
|
for (i = 0; i<n; ++i) {
|
|
|
|
if (i)
|
|
|
|
strcat(cp, " ");
|
2004-03-14 23:47:11 +01:00
|
|
|
strcat(cp, nickname_lst[i]); /* can't overflow */
|
2003-12-17 22:09:31 +01:00
|
|
|
while (*cp)
|
2003-10-01 02:43:34 +02:00
|
|
|
++cp;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-03-29 21:28:16 +02:00
|
|
|
/* Remove any descriptors from the directory that are more than ROUTER_MAX_AGE
|
|
|
|
* seconds old.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
dirserv_remove_old_servers(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
time_t cutoff;
|
|
|
|
cutoff = time(NULL) - ROUTER_MAX_AGE;
|
|
|
|
for (i = 0; i < n_descriptors; ++i) {
|
|
|
|
if (descriptor_list[i]->published < cutoff) {
|
|
|
|
/* descriptor_list[i] is too old. Remove it. */
|
|
|
|
free_descriptor_entry(descriptor_list[i]);
|
|
|
|
descriptor_list[i] = descriptor_list[n_descriptors-1];
|
|
|
|
--n_descriptors;
|
|
|
|
directory_set_dirty();
|
|
|
|
--i; /* Don't advance the index; consider the new value now at i. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dump all routers currently in the directory into the string <s>, using
|
|
|
|
* at most <maxlen> characters, and signing the directory with <private_key>.
|
|
|
|
* Return 0 on success, -1 on failure.
|
|
|
|
*/
|
2003-09-27 23:30:10 +02:00
|
|
|
int
|
|
|
|
dirserv_dump_directory_to_string(char *s, int maxlen,
|
|
|
|
crypto_pk_env_t *private_key)
|
|
|
|
{
|
|
|
|
char *cp, *eos;
|
|
|
|
char digest[20];
|
|
|
|
char signature[128];
|
|
|
|
char published[33];
|
|
|
|
time_t published_on;
|
|
|
|
int i;
|
|
|
|
eos = s+maxlen;
|
|
|
|
|
|
|
|
if (list_running_servers(&cp))
|
|
|
|
return -1;
|
2004-03-29 21:28:16 +02:00
|
|
|
dirserv_remove_old_servers();
|
2003-09-27 23:30:10 +02:00
|
|
|
published_on = time(NULL);
|
|
|
|
strftime(published, 32, "%Y-%m-%d %H:%M:%S", gmtime(&published_on));
|
|
|
|
snprintf(s, maxlen,
|
|
|
|
"signed-directory\n"
|
|
|
|
"published %s\n"
|
2003-11-13 07:49:25 +01:00
|
|
|
"recommended-software %s\n"
|
|
|
|
"running-routers %s\n\n", published, options.RecommendedVersions, cp);
|
2003-09-27 23:30:10 +02:00
|
|
|
free(cp);
|
|
|
|
i = strlen(s);
|
|
|
|
cp = s+i;
|
2003-12-14 06:25:23 +01:00
|
|
|
|
2003-09-27 23:30:10 +02:00
|
|
|
for (i = 0; i < n_descriptors; ++i) {
|
2004-03-29 21:28:16 +02:00
|
|
|
if (strlcat(s, descriptor_list[i]->descriptor, maxlen) >= maxlen)
|
|
|
|
goto truncated;
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
2004-03-29 21:28:16 +02:00
|
|
|
/* These multiple strlcat calls are inefficient, but dwarfed by the RSA
|
2003-09-27 23:30:10 +02:00
|
|
|
signature.
|
|
|
|
*/
|
2004-03-29 21:28:16 +02:00
|
|
|
if (strlcat(s, "directory-signature\n", maxlen) >= maxlen)
|
|
|
|
goto truncated;
|
2003-12-14 06:25:23 +01:00
|
|
|
|
2003-09-30 21:27:54 +02:00
|
|
|
if (router_get_dir_hash(s,digest)) {
|
2003-10-10 03:48:32 +02:00
|
|
|
log_fn(LOG_WARN,"couldn't compute digest");
|
2003-09-27 23:30:10 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (crypto_pk_private_sign(private_key, digest, 20, signature) < 0) {
|
2003-10-10 03:48:32 +02:00
|
|
|
log_fn(LOG_WARN,"couldn't sign digest");
|
2003-09-27 23:30:10 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2004-04-06 22:16:12 +02:00
|
|
|
log(LOG_DEBUG,"generated directory digest begins with %s",hex_str(digest,4));
|
2003-12-14 06:25:23 +01:00
|
|
|
|
2004-03-29 21:28:16 +02:00
|
|
|
if (strlcat(cp, "-----BEGIN SIGNATURE-----\n", maxlen) >= maxlen)
|
|
|
|
goto truncated;
|
2003-12-14 06:25:23 +01:00
|
|
|
|
2003-09-27 23:30:10 +02:00
|
|
|
i = strlen(s);
|
|
|
|
cp = s+i;
|
|
|
|
if (base64_encode(cp, maxlen-i, signature, 128) < 0) {
|
2003-10-10 03:48:32 +02:00
|
|
|
log_fn(LOG_WARN,"couldn't base64-encode signature");
|
2003-09-27 23:30:10 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-03-29 21:28:16 +02:00
|
|
|
if (strlcat(s, "-----END SIGNATURE-----\n", maxlen) >= maxlen)
|
|
|
|
goto truncated;
|
2003-09-27 23:30:10 +02:00
|
|
|
|
|
|
|
return 0;
|
2004-03-29 21:28:16 +02:00
|
|
|
truncated:
|
|
|
|
log_fn(LOG_WARN,"tried to exceed string length.");
|
|
|
|
return -1;
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
|
|
|
|
2003-12-14 06:25:23 +01:00
|
|
|
static char *the_directory = NULL;
|
|
|
|
static int the_directory_len = -1;
|
|
|
|
|
2003-09-27 23:30:10 +02:00
|
|
|
size_t dirserv_get_directory(const char **directory)
|
|
|
|
{
|
|
|
|
char *new_directory;
|
2003-09-30 01:14:49 +02:00
|
|
|
char filename[512];
|
2003-09-27 23:30:10 +02:00
|
|
|
if (the_directory_is_dirty) {
|
|
|
|
new_directory = tor_malloc(MAX_DIR_SIZE);
|
|
|
|
if (dirserv_dump_directory_to_string(new_directory, MAX_DIR_SIZE,
|
|
|
|
get_identity_key())) {
|
2003-10-10 03:48:32 +02:00
|
|
|
log(LOG_WARN, "Error creating directory.");
|
2003-09-27 23:30:10 +02:00
|
|
|
free(new_directory);
|
|
|
|
return 0;
|
|
|
|
}
|
2003-10-21 11:48:17 +02:00
|
|
|
tor_free(the_directory);
|
2003-09-27 23:30:10 +02:00
|
|
|
the_directory = new_directory;
|
|
|
|
the_directory_len = strlen(the_directory);
|
|
|
|
log_fn(LOG_INFO,"New directory (size %d):\n%s",the_directory_len,
|
|
|
|
the_directory);
|
|
|
|
the_directory_is_dirty = 0;
|
|
|
|
/* Now read the directory we just made in order to update our own
|
|
|
|
* router lists. This does more signature checking than is strictly
|
|
|
|
* necessary, but safe is better than sorry. */
|
2003-10-04 05:29:09 +02:00
|
|
|
new_directory = tor_strdup(the_directory);
|
2003-09-30 01:14:49 +02:00
|
|
|
/* use a new copy of the dir, since get_dir_from_string scribbles on it */
|
2003-12-05 10:51:49 +01:00
|
|
|
if (router_set_routerlist_from_directory(new_directory, get_identity_key())) {
|
2003-09-27 23:30:10 +02:00
|
|
|
log_fn(LOG_ERR, "We just generated a directory we can't parse. Dying.");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
free(new_directory);
|
2003-09-30 01:14:49 +02:00
|
|
|
sprintf(filename,"%s/cached-directory", options.DataDirectory);
|
|
|
|
if(write_str_to_file(filename,the_directory) < 0) {
|
2003-10-10 03:48:32 +02:00
|
|
|
log_fn(LOG_WARN, "Couldn't write cached directory to disk. Ignoring.");
|
2003-09-30 01:14:49 +02:00
|
|
|
}
|
2003-09-27 23:30:10 +02:00
|
|
|
} else {
|
|
|
|
log(LOG_INFO,"Directory still clean, reusing.");
|
|
|
|
}
|
|
|
|
*directory = the_directory;
|
|
|
|
return the_directory_len;
|
|
|
|
}
|
2003-10-18 05:23:26 +02:00
|
|
|
|
2004-04-03 06:05:12 +02:00
|
|
|
/*
|
|
|
|
Local Variables:
|
|
|
|
mode:c
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
c-basic-offset:2
|
|
|
|
End:
|
|
|
|
*/
|