2007-05-15 00:51:05 +02:00
|
|
|
/* oCpyright (c) 2001 Matej Pfajfar.
|
2006-02-09 06:46:49 +01:00
|
|
|
* Copyright (c) 2001-2004, Roger Dingledine.
|
2007-02-12 22:39:53 +01:00
|
|
|
* Copyright (c) 2004-2007, Roger Dingledine, Nick Mathewson. */
|
2004-05-10 19:30:51 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
2005-12-14 21:40:40 +01:00
|
|
|
const char routerparse_c_id[] =
|
|
|
|
"$Id$";
|
2004-05-10 19:30:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \file routerparse.c
|
|
|
|
* \brief Code to parse and validate router descriptors and directories.
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include "or.h"
|
|
|
|
|
|
|
|
/****************************************************************************/
|
|
|
|
|
|
|
|
/** Enumeration of possible token types. The ones starting with K_
|
|
|
|
* correspond to directory 'keywords'. _UNRECOGNIZED is for an
|
|
|
|
* unrecognized keyword; _ERR is an error in the tokenizing process,
|
|
|
|
* _EOF is an end-of-file marker, and _NIL is used to encode
|
|
|
|
* not-a-token.
|
|
|
|
*/
|
|
|
|
typedef enum {
|
2007-04-16 06:18:21 +02:00
|
|
|
K_ACCEPT = 0,
|
2004-05-10 19:30:51 +02:00
|
|
|
K_DIRECTORY_SIGNATURE,
|
|
|
|
K_RECOMMENDED_SOFTWARE,
|
|
|
|
K_REJECT,
|
|
|
|
K_ROUTER,
|
|
|
|
K_SIGNED_DIRECTORY,
|
|
|
|
K_SIGNING_KEY,
|
|
|
|
K_ONION_KEY,
|
|
|
|
K_ROUTER_SIGNATURE,
|
|
|
|
K_PUBLISHED,
|
|
|
|
K_RUNNING_ROUTERS,
|
2004-10-27 02:48:51 +02:00
|
|
|
K_ROUTER_STATUS,
|
2004-05-10 19:30:51 +02:00
|
|
|
K_PLATFORM,
|
|
|
|
K_OPT,
|
|
|
|
K_BANDWIDTH,
|
|
|
|
K_PORTS,
|
2004-06-21 06:37:27 +02:00
|
|
|
K_CONTACT,
|
2004-06-25 02:29:31 +02:00
|
|
|
K_NETWORK_STATUS,
|
2004-08-17 07:29:41 +02:00
|
|
|
K_UPTIME,
|
2004-10-07 23:10:40 +02:00
|
|
|
K_DIR_SIGNING_KEY,
|
2004-10-15 03:58:11 +02:00
|
|
|
K_FAMILY,
|
2005-05-02 23:22:31 +02:00
|
|
|
K_FINGERPRINT,
|
|
|
|
K_HIBERNATING,
|
|
|
|
K_READ_HISTORY,
|
|
|
|
K_WRITE_HISTORY,
|
2005-09-02 22:37:31 +02:00
|
|
|
K_NETWORK_STATUS_VERSION,
|
|
|
|
K_DIR_SOURCE,
|
|
|
|
K_DIR_OPTIONS,
|
|
|
|
K_CLIENT_VERSIONS,
|
|
|
|
K_SERVER_VERSIONS,
|
|
|
|
K_R,
|
|
|
|
K_S,
|
2006-12-24 05:09:48 +01:00
|
|
|
K_V,
|
2006-09-21 23:48:06 +02:00
|
|
|
K_EVENTDNS,
|
2007-04-16 06:17:58 +02:00
|
|
|
K_EXTRA_INFO,
|
|
|
|
K_EXTRA_INFO_DIGEST,
|
|
|
|
K_CACHES_EXTRA_INFO,
|
2007-05-02 23:37:55 +02:00
|
|
|
|
|
|
|
K_DIR_KEY_CERTIFICATE_VERSION,
|
|
|
|
K_DIR_IDENTITY_KEY,
|
|
|
|
K_DIR_KEY_PUBLISHED,
|
|
|
|
K_DIR_KEY_EXPIRES,
|
|
|
|
K_DIR_KEY_CERTIFICATION,
|
|
|
|
|
|
|
|
K_VOTE_STATUS,
|
2007-06-03 05:05:10 +02:00
|
|
|
K_VALID_AFTER,
|
|
|
|
K_FRESH_UNTIL,
|
2007-05-02 23:37:55 +02:00
|
|
|
K_VALID_UNTIL,
|
2007-06-03 05:05:10 +02:00
|
|
|
K_VOTING_DELAY,
|
|
|
|
|
2007-05-02 23:37:55 +02:00
|
|
|
K_KNOWN_FLAGS,
|
|
|
|
K_VOTE_DIGEST,
|
|
|
|
K_CONSENSUS_DIGEST,
|
2007-07-17 19:49:16 +02:00
|
|
|
K_CONSENSUS_METHODS,
|
2007-05-02 23:37:55 +02:00
|
|
|
|
2004-05-10 19:30:51 +02:00
|
|
|
_UNRECOGNIZED,
|
|
|
|
_ERR,
|
|
|
|
_EOF,
|
|
|
|
_NIL
|
|
|
|
} directory_keyword;
|
|
|
|
|
|
|
|
/** Structure to hold a single directory token.
|
|
|
|
*
|
|
|
|
* We parse a directory by breaking it into "tokens", each consisting
|
|
|
|
* of a keyword, a line full of arguments, and a binary object. The
|
|
|
|
* arguments and object are both optional, depending on the keyword
|
|
|
|
* type.
|
|
|
|
*/
|
|
|
|
typedef struct directory_token_t {
|
|
|
|
directory_keyword tp; /**< Type of the token. */
|
|
|
|
int n_args; /**< Number of elements in args */
|
|
|
|
char **args; /**< Array of arguments from keyword line. */
|
|
|
|
char *object_type; /**< -----BEGIN [object_type]-----*/
|
2007-04-16 06:18:21 +02:00
|
|
|
size_t object_size; /**< Bytes in object_body */
|
2004-05-10 19:30:51 +02:00
|
|
|
char *object_body; /**< Contents of object, base64-decoded. */
|
|
|
|
crypto_pk_env_t *key; /**< For public keys only. */
|
2007-04-16 18:28:06 +02:00
|
|
|
char *error; /**< For _ERR tokens only. */
|
2004-05-10 19:30:51 +02:00
|
|
|
} directory_token_t;
|
|
|
|
|
|
|
|
/* ********************************************************************** */
|
|
|
|
|
|
|
|
/** We use a table of rules to decide how to parse each token type. */
|
|
|
|
|
|
|
|
/** Rules for whether the keyword needs an object. */
|
|
|
|
typedef enum {
|
2007-05-15 00:51:05 +02:00
|
|
|
NO_OBJ, /**< No object, ever. */
|
|
|
|
NEED_OBJ, /**< Object is required. */
|
|
|
|
NEED_KEY_1024, /**< Object is required, and must be a 1024 bit public key */
|
|
|
|
NEED_KEY, /**< Object is required, and must be a public key. */
|
|
|
|
OBJ_OK, /**< Object is optional. */
|
2004-05-10 19:30:51 +02:00
|
|
|
} obj_syntax;
|
|
|
|
|
2007-05-15 00:51:05 +02:00
|
|
|
#define AT_START 1
|
2007-06-03 05:05:10 +02:00
|
|
|
#define AT_END 2
|
2007-05-15 00:51:05 +02:00
|
|
|
|
|
|
|
/** Determines the parsing rules for a single token type. */
|
2007-04-16 06:18:21 +02:00
|
|
|
typedef struct token_rule_t {
|
2007-05-15 00:51:05 +02:00
|
|
|
/** The string value of the keyword identifying the type of item. */
|
|
|
|
const char *t;
|
|
|
|
/** The corresponding directory_keyword enum. */
|
|
|
|
directory_keyword v;
|
|
|
|
/** Minimum number of arguments for this item */
|
|
|
|
int min_args;
|
|
|
|
/** Maximum number of arguments for this item */
|
|
|
|
int max_args;
|
|
|
|
/** If true, we concatenate all arguments for this item into a single
|
|
|
|
* string. */
|
|
|
|
int concat_args;
|
|
|
|
/** Requirments on object syntax for this item. */
|
2007-04-16 18:28:06 +02:00
|
|
|
obj_syntax os;
|
2007-05-15 00:51:05 +02:00
|
|
|
/** Lowest number of times this item may appear in a document. */
|
|
|
|
int min_cnt;
|
|
|
|
/** Highest number of times this item may appear in a document. */
|
|
|
|
int max_cnt;
|
|
|
|
/** One or more of AT_START/AT_END to limit where the item may appear in a
|
|
|
|
* document. */
|
|
|
|
int pos;
|
2007-04-16 06:18:21 +02:00
|
|
|
} token_rule_t;
|
|
|
|
|
2007-05-15 00:51:05 +02:00
|
|
|
/*
|
|
|
|
* Helper macros to define token tables. 's' is a string, 't' is a
|
|
|
|
* directory_keyword, 'a' is a trio of argument multiplicities, and 'o' is an
|
|
|
|
* object syntax.
|
|
|
|
*
|
|
|
|
*/
|
2007-04-16 06:18:21 +02:00
|
|
|
|
2007-05-15 00:51:05 +02:00
|
|
|
/** Appears to indicate the end of a table. */
|
|
|
|
#define END_OF_TABLE { NULL, _NIL, 0,0,0, NO_OBJ, 0, INT_MAX, 0 }
|
|
|
|
/** An item with no restrictions: used for obsolete document types */
|
|
|
|
#define T(s,t,a,o) { s, t, a, o, 0, INT_MAX, 0 }
|
|
|
|
/** An item with no restrictions on multiplicity or location. */
|
|
|
|
#define T0N(s,t,a,o) { s, t, a, o, 0, INT_MAX, 0 }
|
|
|
|
/** An item that must appear exactly once */
|
|
|
|
#define T1(s,t,a,o) { s, t, a, o, 1, 1, 0 }
|
|
|
|
/** An item that must appear exactly once, at the start of the document */
|
2007-06-03 05:05:10 +02:00
|
|
|
#define T1_START(s,t,a,o) { s, t, a, o, 1, 1, AT_START }
|
2007-05-15 00:51:05 +02:00
|
|
|
/** An item that must appear exactly once, at the end of the document */
|
2007-06-03 05:05:10 +02:00
|
|
|
#define T1_END(s,t,a,o) { s, t, a, o, 1, 1, AT_END }
|
2007-05-15 00:51:05 +02:00
|
|
|
/** An item that must appear one or more times */
|
|
|
|
#define T1N(s,t,a,o) { s, t, a, o, 1, INT_MAX, 0 }
|
|
|
|
/** An item that must appear no more than once */
|
|
|
|
#define T01(s,t,a,o) { s, t, a, o, 0, 1, 0 }
|
|
|
|
|
|
|
|
/* Argument multiplicity: any number of arguments. */
|
2007-04-16 18:28:06 +02:00
|
|
|
#define ARGS 0,INT_MAX,0
|
2007-05-15 00:51:05 +02:00
|
|
|
/* Argument multiplicity: no arguments. */
|
2007-04-16 18:28:06 +02:00
|
|
|
#define NO_ARGS 0,0,0
|
2007-05-15 00:51:05 +02:00
|
|
|
/* Argument multiplicity: concatenate all arguments. */
|
2007-04-16 18:28:06 +02:00
|
|
|
#define CONCAT_ARGS 1,1,1
|
2007-05-15 00:51:05 +02:00
|
|
|
/* Argument multiplicity: at least <b>n</b> arguments. */
|
2007-04-16 18:28:06 +02:00
|
|
|
#define GE(n) n,INT_MAX,0
|
2007-05-15 00:51:05 +02:00
|
|
|
/* Argument multiplicity: exactly <b>n</b> arguments. */
|
2007-04-16 18:28:06 +02:00
|
|
|
#define EQ(n) n,n,0
|
|
|
|
|
2007-05-15 00:51:05 +02:00
|
|
|
/** List of tokens allowable in router derscriptors */
|
2007-04-16 06:18:21 +02:00
|
|
|
static token_rule_t routerdesc_token_table[] = {
|
|
|
|
T0N("reject", K_REJECT, ARGS, NO_OBJ ),
|
2007-05-15 00:51:05 +02:00
|
|
|
T0N("accept", K_ACCEPT, ARGS, NO_OBJ ),
|
2007-06-06 06:51:30 +02:00
|
|
|
T1_START( "router", K_ROUTER, GE(5), NO_OBJ ),
|
2007-05-02 23:37:55 +02:00
|
|
|
T1( "signing-key", K_SIGNING_KEY, NO_ARGS, NEED_KEY_1024 ),
|
|
|
|
T1( "onion-key", K_ONION_KEY, NO_ARGS, NEED_KEY_1024 ),
|
2007-06-06 06:51:30 +02:00
|
|
|
T1_END( "router-signature", K_ROUTER_SIGNATURE, NO_ARGS, NEED_OBJ ),
|
2007-04-16 06:18:21 +02:00
|
|
|
T1( "published", K_PUBLISHED, CONCAT_ARGS, NO_OBJ ),
|
2007-04-16 18:28:06 +02:00
|
|
|
T01("uptime", K_UPTIME, GE(1), NO_OBJ ),
|
2007-04-16 06:18:21 +02:00
|
|
|
T01("fingerprint", K_FINGERPRINT, CONCAT_ARGS, NO_OBJ ),
|
2007-04-16 18:28:06 +02:00
|
|
|
T01("hibernating", K_HIBERNATING, GE(1), NO_OBJ ),
|
2007-05-15 00:51:05 +02:00
|
|
|
T01("platform", K_PLATFORM, CONCAT_ARGS, NO_OBJ ),
|
|
|
|
T01("contact", K_CONTACT, CONCAT_ARGS, NO_OBJ ),
|
2007-04-16 06:18:21 +02:00
|
|
|
T01("read-history", K_READ_HISTORY, ARGS, NO_OBJ ),
|
|
|
|
T01("write-history", K_WRITE_HISTORY, ARGS, NO_OBJ ),
|
2007-04-16 18:28:06 +02:00
|
|
|
T01("extra-info-digest", K_EXTRA_INFO_DIGEST, GE(1), NO_OBJ ),
|
2007-05-15 00:51:05 +02:00
|
|
|
|
|
|
|
T01("family", K_FAMILY, ARGS, NO_OBJ ),
|
2007-04-16 06:18:21 +02:00
|
|
|
T01("caches-extra-info", K_CACHES_EXTRA_INFO, NO_ARGS, NO_OBJ ),
|
2007-05-15 00:51:05 +02:00
|
|
|
T01("eventdns", K_EVENTDNS, ARGS, NO_OBJ ),
|
|
|
|
|
|
|
|
T0N("opt", K_OPT, CONCAT_ARGS, OBJ_OK ),
|
2007-04-16 18:28:06 +02:00
|
|
|
T1( "bandwidth", K_BANDWIDTH, GE(3), NO_OBJ ),
|
2007-04-16 06:18:21 +02:00
|
|
|
|
|
|
|
END_OF_TABLE
|
|
|
|
};
|
|
|
|
|
2007-05-15 00:51:05 +02:00
|
|
|
/** List of tokens allowable in extra-info documents. */
|
2007-04-16 06:18:21 +02:00
|
|
|
static token_rule_t extrainfo_token_table[] = {
|
2007-06-06 06:51:30 +02:00
|
|
|
T1_END( "router-signature", K_ROUTER_SIGNATURE, NO_ARGS, NEED_OBJ ),
|
2007-04-16 06:18:21 +02:00
|
|
|
T1( "published", K_PUBLISHED, CONCAT_ARGS, NO_OBJ ),
|
|
|
|
T0N("opt", K_OPT, CONCAT_ARGS, OBJ_OK ),
|
|
|
|
T01("read-history", K_READ_HISTORY, ARGS, NO_OBJ ),
|
|
|
|
T01("write-history", K_WRITE_HISTORY, ARGS, NO_OBJ ),
|
2007-06-06 06:51:30 +02:00
|
|
|
T1_START( "extra-info", K_EXTRA_INFO, GE(2), NO_OBJ ),
|
2007-04-16 06:18:21 +02:00
|
|
|
|
|
|
|
END_OF_TABLE
|
|
|
|
};
|
|
|
|
|
2007-05-15 00:51:05 +02:00
|
|
|
/** List of tokens allowable in the body part of v2 and v3 networkstatus
|
|
|
|
* documents. */
|
2007-04-16 06:18:21 +02:00
|
|
|
static token_rule_t rtrstatus_token_table[] = {
|
2007-05-25 21:41:31 +02:00
|
|
|
T1( "r", K_R, GE(8), NO_OBJ ),
|
2007-04-16 06:18:21 +02:00
|
|
|
T1( "s", K_S, ARGS, NO_OBJ ),
|
|
|
|
T01("v", K_V, CONCAT_ARGS, NO_OBJ ),
|
|
|
|
T0N("opt", K_OPT, CONCAT_ARGS, OBJ_OK ),
|
|
|
|
END_OF_TABLE
|
|
|
|
};
|
|
|
|
|
2007-05-15 00:51:05 +02:00
|
|
|
/** List of tokens allowable in the header part of v2 networkstatus documents.
|
|
|
|
*/
|
2007-04-16 06:18:21 +02:00
|
|
|
static token_rule_t netstatus_token_table[] = {
|
|
|
|
T1( "published", K_PUBLISHED, CONCAT_ARGS, NO_OBJ ),
|
|
|
|
T0N("opt", K_OPT, CONCAT_ARGS, OBJ_OK ),
|
|
|
|
T1( "contact", K_CONTACT, CONCAT_ARGS, NO_OBJ ),
|
2007-05-24 20:12:44 +02:00
|
|
|
T1( "dir-signing-key", K_DIR_SIGNING_KEY, NO_ARGS, NEED_KEY_1024 ),
|
2007-04-16 06:18:21 +02:00
|
|
|
T1( "fingerprint", K_FINGERPRINT, CONCAT_ARGS, NO_OBJ ),
|
2007-06-03 05:05:10 +02:00
|
|
|
T1_START("network-status-version", K_NETWORK_STATUS_VERSION,
|
2007-04-16 18:28:06 +02:00
|
|
|
GE(1), NO_OBJ ),
|
|
|
|
T1( "dir-source", K_DIR_SOURCE, GE(3), NO_OBJ ),
|
2007-04-16 06:18:21 +02:00
|
|
|
T01("dir-options", K_DIR_OPTIONS, ARGS, NO_OBJ ),
|
2007-04-16 18:28:06 +02:00
|
|
|
T01("client-versions", K_CLIENT_VERSIONS, CONCAT_ARGS, NO_OBJ ),
|
2007-05-25 21:41:31 +02:00
|
|
|
T01("server-versions", K_SERVER_VERSIONS, CONCAT_ARGS, NO_OBJ ),
|
2007-04-16 06:18:21 +02:00
|
|
|
|
|
|
|
END_OF_TABLE
|
|
|
|
};
|
|
|
|
|
2007-05-15 00:51:05 +02:00
|
|
|
/** List of tokens allowable in the footer of v1/v2 directory/networkstatus
|
|
|
|
* footers. */
|
2007-04-16 06:18:21 +02:00
|
|
|
static token_rule_t dir_footer_token_table[] = {
|
2007-05-15 00:51:05 +02:00
|
|
|
T1("directory-signature", K_DIRECTORY_SIGNATURE, EQ(1), NEED_OBJ ),
|
2007-04-16 06:18:21 +02:00
|
|
|
END_OF_TABLE
|
|
|
|
};
|
|
|
|
|
2007-05-15 00:51:05 +02:00
|
|
|
/** List of tokens allowable in v1 diectory headers/footers. */
|
2007-04-16 06:18:21 +02:00
|
|
|
static token_rule_t dir_token_table[] = {
|
|
|
|
/* don't enforce counts; this is obsolete. */
|
|
|
|
T( "network-status", K_NETWORK_STATUS, NO_ARGS, NO_OBJ ),
|
|
|
|
T( "directory-signature", K_DIRECTORY_SIGNATURE, ARGS, NEED_OBJ ),
|
2007-04-16 18:28:06 +02:00
|
|
|
T( "recommended-software",K_RECOMMENDED_SOFTWARE,CONCAT_ARGS, NO_OBJ ),
|
2007-04-16 06:18:21 +02:00
|
|
|
T( "signed-directory", K_SIGNED_DIRECTORY, NO_ARGS, NO_OBJ ),
|
|
|
|
|
|
|
|
T( "running-routers", K_RUNNING_ROUTERS, ARGS, NO_OBJ ),
|
|
|
|
T( "router-status", K_ROUTER_STATUS, ARGS, NO_OBJ ),
|
2007-04-16 18:28:06 +02:00
|
|
|
T( "published", K_PUBLISHED, CONCAT_ARGS, NO_OBJ ),
|
2007-04-16 06:18:21 +02:00
|
|
|
T( "opt", K_OPT, CONCAT_ARGS, OBJ_OK ),
|
|
|
|
T( "contact", K_CONTACT, CONCAT_ARGS, NO_OBJ ),
|
|
|
|
T( "dir-signing-key", K_DIR_SIGNING_KEY, ARGS, OBJ_OK ),
|
|
|
|
T( "fingerprint", K_FINGERPRINT, CONCAT_ARGS, NO_OBJ ),
|
|
|
|
|
|
|
|
END_OF_TABLE
|
2004-05-10 19:30:51 +02:00
|
|
|
};
|
|
|
|
|
2007-05-15 00:51:05 +02:00
|
|
|
/** List of tokens allowable in the footer of v1/v2 directory/networkstatus
|
|
|
|
* footers. */
|
2007-05-02 23:37:55 +02:00
|
|
|
#define CERTIFICATE_MEMBERS \
|
|
|
|
T1("dir-key-certificate-version", K_DIR_KEY_CERTIFICATE_VERSION, \
|
2007-05-25 21:41:31 +02:00
|
|
|
GE(1), NO_OBJ ), \
|
2007-05-02 23:37:55 +02:00
|
|
|
T1("dir-identity-key", K_DIR_IDENTITY_KEY, NO_ARGS, NEED_KEY ),\
|
|
|
|
T1("dir-key-published",K_DIR_KEY_PUBLISHED, CONCAT_ARGS, NO_OBJ), \
|
|
|
|
T1("dir-key-expires", K_DIR_KEY_EXPIRES, CONCAT_ARGS, NO_OBJ), \
|
|
|
|
T1("dir-signing-key", K_DIR_SIGNING_KEY, NO_ARGS, NEED_KEY ),\
|
|
|
|
T1("dir-key-certification", K_DIR_KEY_CERTIFICATION, \
|
|
|
|
NO_ARGS, NEED_OBJ),
|
|
|
|
|
|
|
|
static token_rule_t dir_key_certificate_table[] = {
|
|
|
|
CERTIFICATE_MEMBERS
|
|
|
|
T1("fingerprint", K_FINGERPRINT, CONCAT_ARGS, NO_OBJ ),
|
|
|
|
END_OF_TABLE
|
|
|
|
};
|
|
|
|
|
2007-06-03 05:05:10 +02:00
|
|
|
static token_rule_t networkstatus_vote_token_table[] = {
|
2007-05-02 23:37:55 +02:00
|
|
|
T1("network-status-version", K_NETWORK_STATUS_VERSION,
|
|
|
|
GE(1), NO_OBJ ),
|
|
|
|
T1("vote-status", K_VOTE_STATUS, GE(1), NO_OBJ ),
|
|
|
|
T1("published", K_PUBLISHED, CONCAT_ARGS, NO_OBJ ),
|
2007-06-03 05:05:10 +02:00
|
|
|
T1("valid-after", K_VALID_AFTER, CONCAT_ARGS, NO_OBJ ),
|
|
|
|
T1("fresh-until", K_FRESH_UNTIL, CONCAT_ARGS, NO_OBJ ),
|
2007-05-02 23:37:55 +02:00
|
|
|
T1("valid-until", K_VALID_UNTIL, CONCAT_ARGS, NO_OBJ ),
|
2007-06-03 05:05:10 +02:00
|
|
|
T1("voting-delay", K_VOTING_DELAY, GE(2), NO_OBJ ),
|
2007-06-13 20:16:05 +02:00
|
|
|
T1("known-flags", K_KNOWN_FLAGS, ARGS, NO_OBJ ),
|
2007-05-02 23:37:55 +02:00
|
|
|
T( "fingerprint", K_FINGERPRINT, CONCAT_ARGS, NO_OBJ ),
|
|
|
|
|
|
|
|
CERTIFICATE_MEMBERS
|
|
|
|
|
|
|
|
T0N("opt", K_OPT, CONCAT_ARGS, OBJ_OK ),
|
|
|
|
T1( "contact", K_CONTACT, CONCAT_ARGS, NO_OBJ ),
|
2007-06-04 21:19:01 +02:00
|
|
|
T1( "dir-source", K_DIR_SOURCE, GE(6), NO_OBJ ),
|
2007-05-02 23:37:55 +02:00
|
|
|
T1( "known-flags", K_KNOWN_FLAGS, CONCAT_ARGS, NO_OBJ ),
|
|
|
|
T01("client-versions", K_CLIENT_VERSIONS, CONCAT_ARGS, NO_OBJ ),
|
|
|
|
T01("server-versions", K_SERVER_VERSIONS, CONCAT_ARGS, NO_OBJ ),
|
2007-07-17 19:49:16 +02:00
|
|
|
T1( "consensus-methods", K_CONSENSUS_METHODS, GE(1), NO_OBJ ),
|
2007-05-02 23:37:55 +02:00
|
|
|
|
|
|
|
END_OF_TABLE
|
|
|
|
};
|
2007-06-05 00:29:00 +02:00
|
|
|
static token_rule_t networkstatus_consensus_token_table[] = {
|
2007-05-02 23:37:55 +02:00
|
|
|
T1("network-status-version", K_NETWORK_STATUS_VERSION,
|
|
|
|
GE(1), NO_OBJ ),
|
|
|
|
T1("vote-status", K_VOTE_STATUS, GE(1), NO_OBJ ),
|
2007-06-03 05:05:10 +02:00
|
|
|
T1("valid-after", K_VALID_AFTER, CONCAT_ARGS, NO_OBJ ),
|
|
|
|
T1("fresh-until", K_FRESH_UNTIL, CONCAT_ARGS, NO_OBJ ),
|
2007-05-02 23:37:55 +02:00
|
|
|
T1("valid-until", K_VALID_UNTIL, CONCAT_ARGS, NO_OBJ ),
|
2007-06-03 05:05:10 +02:00
|
|
|
T1("voting-delay", K_VOTING_DELAY, GE(2), NO_OBJ ),
|
2007-05-02 23:37:55 +02:00
|
|
|
|
|
|
|
T0N("opt", K_OPT, CONCAT_ARGS, OBJ_OK ),
|
|
|
|
|
|
|
|
T1N("dir-source", K_DIR_SOURCE, GE(3), NO_OBJ ),
|
|
|
|
T1N("contact", K_CONTACT, CONCAT_ARGS, NO_OBJ ),
|
|
|
|
T1N("vote-digest", K_VOTE_DIGEST, GE(1), NO_OBJ ),
|
|
|
|
|
|
|
|
T1( "known-flags", K_KNOWN_FLAGS, CONCAT_ARGS, NO_OBJ ),
|
|
|
|
|
|
|
|
T01("client-versions", K_CLIENT_VERSIONS, CONCAT_ARGS, NO_OBJ ),
|
|
|
|
T01("server-versions", K_SERVER_VERSIONS, CONCAT_ARGS, NO_OBJ ),
|
|
|
|
|
|
|
|
END_OF_TABLE
|
|
|
|
};
|
|
|
|
|
2007-06-03 05:05:10 +02:00
|
|
|
static token_rule_t networkstatus_vote_footer_token_table[] = {
|
2007-05-02 23:37:55 +02:00
|
|
|
T( "directory-signature", K_DIRECTORY_SIGNATURE, GE(2), NEED_OBJ ),
|
|
|
|
END_OF_TABLE
|
|
|
|
};
|
2007-06-03 05:05:10 +02:00
|
|
|
|
2007-04-16 06:18:21 +02:00
|
|
|
#undef T
|
|
|
|
|
2004-05-10 19:30:51 +02:00
|
|
|
/* static function prototypes */
|
|
|
|
static int router_add_exit_policy(routerinfo_t *router,directory_token_t *tok);
|
2004-12-04 02:14:36 +01:00
|
|
|
static addr_policy_t *router_parse_addr_policy(directory_token_t *tok);
|
2006-12-29 21:50:24 +01:00
|
|
|
static addr_policy_t *router_parse_addr_policy_private(directory_token_t *tok);
|
|
|
|
|
2004-05-10 19:30:51 +02:00
|
|
|
static int router_get_hash_impl(const char *s, char *digest,
|
2007-05-22 20:52:32 +02:00
|
|
|
const char *start_str, const char *end_str,
|
|
|
|
char end_char);
|
2004-05-10 19:30:51 +02:00
|
|
|
static void token_free(directory_token_t *tok);
|
|
|
|
static smartlist_t *find_all_exitpolicy(smartlist_t *s);
|
|
|
|
static directory_token_t *find_first_by_keyword(smartlist_t *s,
|
|
|
|
directory_keyword keyword);
|
|
|
|
static int tokenize_string(const char *start, const char *end,
|
2007-04-16 06:18:21 +02:00
|
|
|
smartlist_t *out,
|
2007-05-15 00:51:05 +02:00
|
|
|
token_rule_t *table);
|
2007-04-16 06:18:21 +02:00
|
|
|
static directory_token_t *get_next_token(const char **s,
|
2007-07-10 22:08:18 +02:00
|
|
|
const char *eos,
|
2007-05-15 00:51:05 +02:00
|
|
|
token_rule_t *table);
|
|
|
|
static int check_signature_token(const char *digest,
|
|
|
|
directory_token_t *tok,
|
|
|
|
crypto_pk_env_t *pkey,
|
|
|
|
int check_authority,
|
|
|
|
const char *doctype);
|
2007-07-10 22:08:18 +02:00
|
|
|
static crypto_pk_env_t *find_dir_signing_key(const char *str, const char *eos);
|
2005-03-01 02:44:57 +01:00
|
|
|
static int tor_version_same_series(tor_version_t *a, tor_version_t *b);
|
2004-05-10 19:30:51 +02:00
|
|
|
|
|
|
|
/** Set <b>digest</b> to the SHA-1 digest of the hash of the directory in
|
2005-09-13 23:14:55 +02:00
|
|
|
* <b>s</b>. Return 0 on success, -1 on failure.
|
2004-05-10 19:30:51 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
router_get_dir_hash(const char *s, char *digest)
|
2004-05-10 19:30:51 +02:00
|
|
|
{
|
|
|
|
return router_get_hash_impl(s,digest,
|
2007-05-22 20:52:32 +02:00
|
|
|
"signed-directory","\ndirectory-signature",'\n');
|
2004-05-10 19:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Set <b>digest</b> to the SHA-1 digest of the hash of the first router in
|
2005-09-13 23:14:55 +02:00
|
|
|
* <b>s</b>. Return 0 on success, -1 on failure.
|
2004-05-10 19:30:51 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
router_get_router_hash(const char *s, char *digest)
|
2004-05-10 19:30:51 +02:00
|
|
|
{
|
|
|
|
return router_get_hash_impl(s,digest,
|
2007-05-22 20:52:32 +02:00
|
|
|
"router ","\nrouter-signature", '\n');
|
2004-05-10 19:30:51 +02:00
|
|
|
}
|
|
|
|
|
2004-07-17 00:23:18 +02:00
|
|
|
/** Set <b>digest</b> to the SHA-1 digest of the hash of the running-routers
|
2005-09-13 23:14:55 +02:00
|
|
|
* string in <b>s</b>. Return 0 on success, -1 on failure.
|
2004-07-17 00:23:18 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
router_get_runningrouters_hash(const char *s, char *digest)
|
2004-06-25 02:29:31 +02:00
|
|
|
{
|
|
|
|
return router_get_hash_impl(s,digest,
|
2007-05-22 20:52:32 +02:00
|
|
|
"network-status","\ndirectory-signature", '\n');
|
2004-06-25 02:29:31 +02:00
|
|
|
}
|
|
|
|
|
2005-09-13 23:14:55 +02:00
|
|
|
/** Set <b>digest</b> to the SHA-1 digest of the hash of the network-status
|
|
|
|
* string in <b>s</b>. Return 0 on success, -1 on failure. */
|
2005-08-25 22:33:17 +02:00
|
|
|
int
|
|
|
|
router_get_networkstatus_v2_hash(const char *s, char *digest)
|
|
|
|
{
|
|
|
|
return router_get_hash_impl(s,digest,
|
2007-05-22 20:52:32 +02:00
|
|
|
"network-status-version","\ndirectory-signature",
|
|
|
|
'\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Set <b>digest</b> to the SHA-1 digest of the hash of the network-status
|
|
|
|
* string in <b>s</b>. Return 0 on success, -1 on failure. */
|
|
|
|
int
|
|
|
|
router_get_networkstatus_v3_hash(const char *s, char *digest)
|
|
|
|
{
|
|
|
|
return router_get_hash_impl(s,digest,
|
|
|
|
"network-status-version","\ndirectory-signature",
|
|
|
|
' ');
|
2005-08-25 22:33:17 +02:00
|
|
|
}
|
|
|
|
|
2007-05-15 00:51:05 +02:00
|
|
|
/** Set <b>digest</b> to the SHA-1 digest of the hash of the extrainfo
|
|
|
|
* string in <b>s</b>. Return 0 on success, -1 on failure. */
|
2007-04-16 06:17:58 +02:00
|
|
|
int
|
|
|
|
router_get_extrainfo_hash(const char *s, char *digest)
|
|
|
|
{
|
2007-05-22 20:52:32 +02:00
|
|
|
return router_get_hash_impl(s,digest,"extra-info","\nrouter-signature",'\n');
|
2007-04-16 06:17:58 +02:00
|
|
|
}
|
|
|
|
|
2005-08-26 17:34:53 +02:00
|
|
|
/** Helper: used to generate signatures for routers, directories and
|
|
|
|
* network-status objects. Given a digest in <b>digest</b> and a secret
|
|
|
|
* <b>private_key</b>, generate an PKCS1-padded signature, BASE64-encode it,
|
|
|
|
* surround it with -----BEGIN/END----- pairs, and write it to the
|
|
|
|
* <b>buf_len</b>-byte buffer at <b>buf</b>. Return 0 on success, -1 on
|
|
|
|
* failure.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
router_append_dirobj_signature(char *buf, size_t buf_len, const char *digest,
|
|
|
|
crypto_pk_env_t *private_key)
|
|
|
|
{
|
2007-05-02 23:37:55 +02:00
|
|
|
char *signature;
|
2005-08-26 17:34:53 +02:00
|
|
|
int i;
|
|
|
|
|
2007-05-02 23:37:55 +02:00
|
|
|
signature = tor_malloc(crypto_pk_keysize(private_key));
|
2005-08-26 17:34:53 +02:00
|
|
|
if (crypto_pk_private_sign(private_key, signature, digest, DIGEST_LEN) < 0) {
|
|
|
|
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG,"Couldn't sign digest.");
|
2007-05-02 23:37:55 +02:00
|
|
|
goto err;
|
2005-08-26 17:34:53 +02:00
|
|
|
}
|
|
|
|
if (strlcat(buf, "-----BEGIN SIGNATURE-----\n", buf_len) >= buf_len)
|
|
|
|
goto truncated;
|
|
|
|
|
|
|
|
i = strlen(buf);
|
|
|
|
if (base64_encode(buf+i, buf_len-i, signature, 128) < 0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG,"couldn't base64-encode signature");
|
2005-08-26 17:34:53 +02:00
|
|
|
tor_free(buf);
|
2007-05-02 23:37:55 +02:00
|
|
|
goto err;
|
2005-08-26 17:34:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strlcat(buf, "-----END SIGNATURE-----\n", buf_len) >= buf_len)
|
|
|
|
goto truncated;
|
|
|
|
|
2007-05-02 23:37:55 +02:00
|
|
|
tor_free(signature);
|
2005-08-26 17:34:53 +02:00
|
|
|
return 0;
|
2007-05-02 23:37:55 +02:00
|
|
|
|
2005-08-26 17:34:53 +02:00
|
|
|
truncated:
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG,"tried to exceed string length.");
|
2007-05-02 23:37:55 +02:00
|
|
|
err:
|
|
|
|
tor_free(signature);
|
2005-08-26 17:34:53 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Return VS_RECOMMENDED if <b>myversion</b> is contained in
|
|
|
|
* <b>versionlist</b>. Else, return VS_OLD if every member of
|
|
|
|
* <b>versionlist</b> is newer than <b>myversion</b>. Else, return
|
|
|
|
* VS_NEW_IN_SERIES if there is at least one member of <b>versionlist</b> in
|
|
|
|
* the same series (major.minor.micro) as <b>myversion</b>, but no such member
|
|
|
|
* is newer than <b>myversion.</b>. Else, return VS_NEW if every memeber of
|
|
|
|
* <b>versionlist</b> is older than <b>myversion</b>. Else, return
|
|
|
|
* VS_UNRECOMMENDED.
|
2005-03-01 02:44:57 +01:00
|
|
|
*
|
2004-07-17 01:33:42 +02:00
|
|
|
* (versionlist is a comma-separated list of version strings,
|
|
|
|
* optionally prefixed with "Tor". Versions that can't be parsed are
|
2005-09-21 02:41:06 +02:00
|
|
|
* ignored.)
|
2005-09-30 22:04:55 +02:00
|
|
|
*/
|
2005-09-21 02:41:06 +02:00
|
|
|
version_status_t
|
|
|
|
tor_version_is_obsolete(const char *myversion, const char *versionlist)
|
|
|
|
{
|
2004-07-17 01:33:42 +02:00
|
|
|
tor_version_t mine, other;
|
2005-09-21 02:41:06 +02:00
|
|
|
int found_newer = 0, found_older = 0, found_newer_in_series = 0,
|
|
|
|
found_any_in_series = 0, r, same;
|
|
|
|
version_status_t ret = VS_UNRECOMMENDED;
|
2004-09-02 20:39:59 +02:00
|
|
|
smartlist_t *version_sl;
|
2004-06-01 20:19:01 +02:00
|
|
|
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(LD_CONFIG,"Checking whether version '%s' is in '%s'",
|
|
|
|
myversion, versionlist);
|
2004-06-01 20:19:01 +02:00
|
|
|
|
2004-07-17 01:33:42 +02:00
|
|
|
if (tor_version_parse(myversion, &mine)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_err(LD_BUG,"I couldn't parse my own version (%s)", myversion);
|
2004-07-17 01:33:42 +02:00
|
|
|
tor_assert(0);
|
|
|
|
}
|
2004-09-02 20:39:59 +02:00
|
|
|
version_sl = smartlist_create();
|
|
|
|
smartlist_split_string(version_sl, versionlist, ",", SPLIT_SKIP_SPACE, 0);
|
2004-07-17 01:33:42 +02:00
|
|
|
|
2004-09-02 20:39:59 +02:00
|
|
|
SMARTLIST_FOREACH(version_sl, const char *, cp, {
|
|
|
|
if (!strcmpstart(cp, "Tor "))
|
2004-07-17 01:33:42 +02:00
|
|
|
cp += 4;
|
|
|
|
|
|
|
|
if (tor_version_parse(cp, &other)) {
|
|
|
|
/* Couldn't parse other; it can't be a match. */
|
|
|
|
} else {
|
2005-03-01 02:44:57 +01:00
|
|
|
same = tor_version_same_series(&mine, &other);
|
|
|
|
if (same)
|
|
|
|
found_any_in_series = 1;
|
2004-07-17 01:33:42 +02:00
|
|
|
r = tor_version_compare(&mine, &other);
|
|
|
|
if (r==0) {
|
2005-09-21 02:41:06 +02:00
|
|
|
ret = VS_RECOMMENDED;
|
2004-09-02 20:39:59 +02:00
|
|
|
goto done;
|
2004-07-17 01:33:42 +02:00
|
|
|
} else if (r<0) {
|
|
|
|
found_newer = 1;
|
2005-03-01 02:44:57 +01:00
|
|
|
if (same)
|
|
|
|
found_newer_in_series = 1;
|
2005-09-21 02:41:06 +02:00
|
|
|
} else if (r>0) {
|
|
|
|
found_older = 1;
|
2004-07-17 01:33:42 +02:00
|
|
|
}
|
|
|
|
}
|
2004-09-02 20:39:59 +02:00
|
|
|
});
|
|
|
|
|
2005-03-01 02:44:57 +01:00
|
|
|
/* We didn't find the listed version. Is it new or old? */
|
2007-02-08 08:24:06 +01:00
|
|
|
if (found_any_in_series && !found_newer_in_series && found_newer) {
|
2005-09-21 02:41:06 +02:00
|
|
|
ret = VS_NEW_IN_SERIES;
|
|
|
|
} else if (found_newer && !found_older) {
|
|
|
|
ret = VS_OLD;
|
|
|
|
} else if (found_older && !found_newer) {
|
|
|
|
ret = VS_NEW;
|
2004-07-17 01:33:42 +02:00
|
|
|
} else {
|
2005-09-21 02:41:06 +02:00
|
|
|
ret = VS_UNRECOMMENDED;
|
2004-06-01 20:19:01 +02:00
|
|
|
}
|
2004-09-02 20:39:59 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
SMARTLIST_FOREACH(version_sl, char *, version, tor_free(version));
|
|
|
|
smartlist_free(version_sl);
|
|
|
|
return ret;
|
2004-06-01 20:19:01 +02:00
|
|
|
}
|
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Return the combined status of the current version, given that we know of
|
|
|
|
* one set of networkstatuses that give us status <b>a</b>, and another that
|
|
|
|
* gives us status <b>b</b>.
|
|
|
|
*
|
|
|
|
* For example, if one authority thinks that we're NEW, and another thinks
|
|
|
|
* we're OLD, we're simply UNRECOMMENDED.
|
|
|
|
*
|
|
|
|
* This function does not handle calculating whether we're RECOMMENDED; that
|
|
|
|
* follows a simple majority rule. This function simply calculates *why*
|
|
|
|
* we're not recommended (if we're not).
|
|
|
|
*/
|
2005-09-21 02:41:06 +02:00
|
|
|
version_status_t
|
|
|
|
version_status_join(version_status_t a, version_status_t b)
|
2004-06-01 20:19:01 +02:00
|
|
|
{
|
2005-09-21 02:41:06 +02:00
|
|
|
if (a == b)
|
|
|
|
return a;
|
|
|
|
else if (a == VS_UNRECOMMENDED || b == VS_UNRECOMMENDED)
|
|
|
|
return VS_UNRECOMMENDED;
|
|
|
|
else if (a == VS_RECOMMENDED)
|
|
|
|
return b;
|
|
|
|
else if (b == VS_RECOMMENDED)
|
|
|
|
return a;
|
|
|
|
/* Okay. Neither is 'recommended' or 'unrecommended', and they differ. */
|
|
|
|
else if (a == VS_OLD || b == VS_OLD)
|
|
|
|
return VS_UNRECOMMENDED;
|
|
|
|
/* One is VS_NEW, the other is VS_NEW_IN_SERIES */
|
|
|
|
else
|
|
|
|
return VS_NEW_IN_SERIES;
|
2004-06-01 20:19:01 +02:00
|
|
|
}
|
2004-05-10 19:30:51 +02:00
|
|
|
|
2005-09-15 07:19:38 +02:00
|
|
|
/** Read a signed directory from <b>str</b>. If it's well-formed, return 0.
|
|
|
|
* Otherwise, return -1. If we're a directory cache, cache it.
|
2004-05-10 19:30:51 +02:00
|
|
|
*/
|
2005-09-15 07:19:38 +02:00
|
|
|
int
|
|
|
|
router_parse_directory(const char *str)
|
2004-05-10 19:30:51 +02:00
|
|
|
{
|
|
|
|
directory_token_t *tok;
|
2004-06-25 02:29:31 +02:00
|
|
|
char digest[DIGEST_LEN];
|
|
|
|
time_t published_on;
|
2005-09-14 23:09:25 +02:00
|
|
|
int r;
|
2004-08-04 04:35:06 +02:00
|
|
|
const char *end, *cp;
|
2004-05-10 19:30:51 +02:00
|
|
|
smartlist_t *tokens = NULL;
|
2004-10-07 23:10:40 +02:00
|
|
|
crypto_pk_env_t *declared_key = NULL;
|
2004-05-10 19:30:51 +02:00
|
|
|
|
2005-09-16 18:41:45 +02:00
|
|
|
/* XXXX This could be simplified a lot, but it will all go away
|
|
|
|
* once pre-0.1.1.8 is obsolete, and for now it's better not to
|
2006-01-20 21:54:42 +01:00
|
|
|
* touch it. */
|
2005-09-15 07:19:38 +02:00
|
|
|
|
2004-05-10 19:30:51 +02:00
|
|
|
if (router_get_dir_hash(str, digest)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Unable to compute digest of directory");
|
2004-05-10 19:30:51 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(LD_DIR,"Received directory hashes to %s",hex_str(digest,4));
|
2004-05-10 19:30:51 +02:00
|
|
|
|
2004-08-04 04:35:06 +02:00
|
|
|
/* Check signature first, before we try to tokenize. */
|
|
|
|
cp = str;
|
|
|
|
while (cp && (end = strstr(cp+1, "\ndirectory-signature")))
|
|
|
|
cp = end;
|
|
|
|
if (cp == str || !cp) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "No signature found on directory."); goto err;
|
2004-08-04 04:35:06 +02:00
|
|
|
}
|
|
|
|
++cp;
|
|
|
|
tokens = smartlist_create();
|
2007-04-16 06:18:21 +02:00
|
|
|
if (tokenize_string(cp,strchr(cp,'\0'),tokens,dir_token_table)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Error tokenizing directory signature"); goto err;
|
2004-08-04 04:35:06 +02:00
|
|
|
}
|
|
|
|
if (smartlist_len(tokens) != 1) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Unexpected number of tokens in signature"); goto err;
|
2004-08-04 04:35:06 +02:00
|
|
|
}
|
2004-08-07 03:17:38 +02:00
|
|
|
tok=smartlist_get(tokens,0);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (tok->tp != K_DIRECTORY_SIGNATURE) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR,"Expected a single directory signature"); goto err;
|
2004-08-04 04:35:06 +02:00
|
|
|
}
|
2007-07-10 22:08:18 +02:00
|
|
|
declared_key = find_dir_signing_key(str, str+strlen(str));
|
2006-10-31 20:17:07 +01:00
|
|
|
note_crypto_pk_op(VERIFY_DIR);
|
2007-05-15 00:51:05 +02:00
|
|
|
if (check_signature_token(digest, tok, declared_key, 1, "directory")<0)
|
2004-08-04 04:35:06 +02:00
|
|
|
goto err;
|
2004-08-07 03:17:38 +02:00
|
|
|
|
2007-05-17 00:15:14 +02:00
|
|
|
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t));
|
2004-08-04 04:35:06 +02:00
|
|
|
smartlist_free(tokens);
|
|
|
|
tokens = NULL;
|
|
|
|
|
|
|
|
/* Now try to parse the first part of the directory. */
|
2004-05-10 19:30:51 +02:00
|
|
|
if ((end = strstr(str,"\nrouter "))) {
|
|
|
|
++end;
|
|
|
|
} else if ((end = strstr(str, "\ndirectory-signature"))) {
|
|
|
|
++end;
|
|
|
|
} else {
|
|
|
|
end = str + strlen(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
tokens = smartlist_create();
|
2007-04-16 06:18:21 +02:00
|
|
|
if (tokenize_string(str,end,tokens,dir_token_table)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Error tokenizing directory"); goto err;
|
2004-05-10 19:30:51 +02:00
|
|
|
}
|
|
|
|
|
2007-04-16 06:18:29 +02:00
|
|
|
tok = find_first_by_keyword(tokens, K_PUBLISHED);
|
|
|
|
tor_assert(tok);
|
2004-05-10 19:30:51 +02:00
|
|
|
tor_assert(tok->n_args == 1);
|
|
|
|
|
2004-08-07 04:46:16 +02:00
|
|
|
if (parse_iso_time(tok->args[0], &published_on) < 0) {
|
2004-05-10 19:30:51 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2004-11-15 10:38:17 +01:00
|
|
|
/* Now that we know the signature is okay, and we have a
|
|
|
|
* publication time, cache the directory. */
|
2007-05-02 11:12:04 +02:00
|
|
|
if (get_options()->DirPort && !authdir_mode_v1(get_options()))
|
2004-11-15 17:17:59 +01:00
|
|
|
dirserv_set_cached_directory(str, published_on, 0);
|
2004-11-09 19:22:17 +01:00
|
|
|
|
2004-06-25 02:29:31 +02:00
|
|
|
r = 0;
|
|
|
|
goto done;
|
|
|
|
err:
|
|
|
|
r = -1;
|
|
|
|
done:
|
2004-10-07 23:10:40 +02:00
|
|
|
if (declared_key) crypto_free_pk_env(declared_key);
|
2004-06-25 02:29:31 +02:00
|
|
|
if (tokens) {
|
2007-05-17 00:15:14 +02:00
|
|
|
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t));
|
2004-06-25 02:29:31 +02:00
|
|
|
smartlist_free(tokens);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2005-09-15 07:19:38 +02:00
|
|
|
/** Read a signed router status statement from <b>str</b>. If it's
|
|
|
|
* well-formed, return 0. Otherwise, return -1. If we're a directory cache,
|
|
|
|
* cache it.*/
|
2005-09-14 23:09:25 +02:00
|
|
|
int
|
|
|
|
router_parse_runningrouters(const char *str)
|
2004-06-25 02:29:31 +02:00
|
|
|
{
|
|
|
|
char digest[DIGEST_LEN];
|
|
|
|
directory_token_t *tok;
|
|
|
|
time_t published_on;
|
2005-09-14 23:09:25 +02:00
|
|
|
int r = -1;
|
2004-10-07 23:10:40 +02:00
|
|
|
crypto_pk_env_t *declared_key = NULL;
|
2004-06-25 02:29:31 +02:00
|
|
|
smartlist_t *tokens = NULL;
|
2007-07-10 22:08:18 +02:00
|
|
|
const char *eos = str + strlen(str);
|
2004-06-25 02:29:31 +02:00
|
|
|
|
|
|
|
if (router_get_runningrouters_hash(str, digest)) {
|
2006-09-30 00:33:40 +02:00
|
|
|
log_warn(LD_DIR, "Unable to compute digest of running-routers");
|
2004-06-25 02:29:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
tokens = smartlist_create();
|
2007-07-10 22:08:18 +02:00
|
|
|
if (tokenize_string(str,eos,tokens,dir_token_table)) {
|
2006-09-30 00:33:40 +02:00
|
|
|
log_warn(LD_DIR, "Error tokenizing running-routers"); goto err;
|
2004-06-25 02:29:31 +02:00
|
|
|
}
|
|
|
|
tok = smartlist_get(tokens,0);
|
|
|
|
if (tok->tp != K_NETWORK_STATUS) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Network-status starts with wrong token");
|
2004-06-25 02:29:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2007-04-16 06:18:29 +02:00
|
|
|
tok = find_first_by_keyword(tokens, K_PUBLISHED);
|
|
|
|
tor_assert(tok);
|
2004-06-25 02:29:31 +02:00
|
|
|
tor_assert(tok->n_args == 1);
|
2004-08-07 04:46:16 +02:00
|
|
|
if (parse_iso_time(tok->args[0], &published_on) < 0) {
|
2004-06-25 02:29:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!(tok = find_first_by_keyword(tokens, K_DIRECTORY_SIGNATURE))) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Missing signature on running-routers");
|
2004-06-25 02:29:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2007-07-10 22:08:18 +02:00
|
|
|
declared_key = find_dir_signing_key(str, eos);
|
2006-10-31 20:17:07 +01:00
|
|
|
note_crypto_pk_op(VERIFY_DIR);
|
2007-05-15 00:51:05 +02:00
|
|
|
if (check_signature_token(digest, tok, declared_key, 1, "running-routers")
|
|
|
|
< 0)
|
2004-06-25 02:29:31 +02:00
|
|
|
goto err;
|
|
|
|
|
2005-09-14 23:09:25 +02:00
|
|
|
/* Now that we know the signature is okay, and we have a
|
|
|
|
* publication time, cache the list. */
|
2007-05-02 11:12:04 +02:00
|
|
|
if (get_options()->DirPort && !authdir_mode_v1(get_options()))
|
2005-09-14 23:09:25 +02:00
|
|
|
dirserv_set_cached_directory(str, published_on, 1);
|
|
|
|
|
|
|
|
r = 0;
|
2004-06-25 02:29:31 +02:00
|
|
|
err:
|
2004-10-07 23:10:40 +02:00
|
|
|
if (declared_key) crypto_free_pk_env(declared_key);
|
2004-06-25 02:29:31 +02:00
|
|
|
if (tokens) {
|
2007-05-17 00:15:14 +02:00
|
|
|
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t));
|
2004-06-25 02:29:31 +02:00
|
|
|
smartlist_free(tokens);
|
|
|
|
}
|
2005-09-14 23:09:25 +02:00
|
|
|
return r;
|
2004-06-25 02:29:31 +02:00
|
|
|
}
|
|
|
|
|
2004-10-07 23:10:40 +02:00
|
|
|
/** Given a directory or running-routers string in <b>str</b>, try to
|
|
|
|
* find the its dir-signing-key token (if any). If this token is
|
|
|
|
* present, extract and return the key. Return NULL on failure. */
|
2005-06-11 20:52:12 +02:00
|
|
|
static crypto_pk_env_t *
|
2007-07-10 22:08:18 +02:00
|
|
|
find_dir_signing_key(const char *str, const char *eos)
|
2004-10-07 23:10:40 +02:00
|
|
|
{
|
|
|
|
const char *cp;
|
|
|
|
directory_token_t *tok;
|
|
|
|
crypto_pk_env_t *key = NULL;
|
2007-07-10 22:08:18 +02:00
|
|
|
tor_assert(str);
|
|
|
|
tor_assert(eos);
|
2004-10-07 23:10:40 +02:00
|
|
|
|
|
|
|
/* Is there a dir-signing-key in the directory? */
|
2007-07-10 22:08:18 +02:00
|
|
|
cp = tor_memstr(str, eos-str, "\nopt dir-signing-key");
|
2004-10-07 23:10:40 +02:00
|
|
|
if (!cp)
|
2007-07-10 22:08:18 +02:00
|
|
|
cp = tor_memstr(str, eos-str, "\ndir-signing-key");
|
2004-10-07 23:10:40 +02:00
|
|
|
if (!cp)
|
|
|
|
return NULL;
|
|
|
|
++cp; /* Now cp points to the start of the token. */
|
|
|
|
|
2007-07-10 22:08:18 +02:00
|
|
|
tok = get_next_token(&cp, eos, dir_token_table);
|
2004-10-07 23:10:40 +02:00
|
|
|
if (!tok) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Unparseable dir-signing-key token");
|
2004-10-07 23:10:40 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (tok->tp != K_DIR_SIGNING_KEY) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Dir-signing-key token did not parse as expected");
|
2004-10-07 23:10:40 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok->key) {
|
|
|
|
key = tok->key;
|
|
|
|
tok->key = NULL; /* steal reference. */
|
|
|
|
} else {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Dir-signing-key token contained no key");
|
2004-10-07 23:10:40 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
token_free(tok);
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return true iff <b>key</b> is allowed to sign directories.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static int
|
|
|
|
dir_signing_key_is_trusted(crypto_pk_env_t *key)
|
2004-10-07 23:10:40 +02:00
|
|
|
{
|
|
|
|
char digest[DIGEST_LEN];
|
|
|
|
if (!key) return 0;
|
|
|
|
if (crypto_pk_get_digest(key, digest) < 0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Error computing dir-signing-key digest");
|
2004-10-07 23:10:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!router_digest_is_trusted_dir(digest)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Listed dir-signing-key is not trusted");
|
2004-10-07 23:10:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-05-15 00:51:05 +02:00
|
|
|
/** Check whether the object body of the token in <b>tok</b> has a good
|
|
|
|
* signature for <b>digest</b> using key <b>pkey</b>. If
|
|
|
|
* <b>check_authority</b> is set, make sure that <b>pkey</b> is the key of a
|
|
|
|
* directory authority. Use <b>doctype</b> as the type of the document when
|
|
|
|
* generating log messages. Return 0 on success, negative on failure.
|
2004-10-07 23:10:40 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static int
|
2007-05-15 00:51:05 +02:00
|
|
|
check_signature_token(const char *digest,
|
|
|
|
directory_token_t *tok,
|
|
|
|
crypto_pk_env_t *pkey,
|
|
|
|
int check_authority,
|
|
|
|
const char *doctype)
|
2004-06-25 02:29:31 +02:00
|
|
|
{
|
2007-05-02 23:37:55 +02:00
|
|
|
char *signed_digest;
|
2004-08-07 03:48:50 +02:00
|
|
|
|
2007-05-15 00:51:05 +02:00
|
|
|
tor_assert(pkey);
|
|
|
|
tor_assert(tok);
|
|
|
|
tor_assert(digest);
|
|
|
|
tor_assert(doctype);
|
|
|
|
|
|
|
|
if (check_authority && !dir_signing_key_is_trusted(pkey)) {
|
|
|
|
log_warn(LD_DIR, "Key on %s did not come from an authority; rejecting",
|
|
|
|
doctype);
|
2004-10-19 20:19:59 +02:00
|
|
|
return -1;
|
2004-08-07 03:48:50 +02:00
|
|
|
}
|
2004-10-07 23:10:40 +02:00
|
|
|
|
2007-05-02 23:37:55 +02:00
|
|
|
if (strcmp(tok->object_type, "SIGNATURE")) {
|
2007-05-15 00:51:05 +02:00
|
|
|
log_warn(LD_DIR, "Bad object type on %s signature", doctype);
|
2004-06-25 02:29:31 +02:00
|
|
|
return -1;
|
2004-05-10 19:30:51 +02:00
|
|
|
}
|
2004-08-07 03:48:50 +02:00
|
|
|
|
2007-05-02 23:37:55 +02:00
|
|
|
signed_digest = tor_malloc(tok->object_size);
|
2007-05-15 00:51:05 +02:00
|
|
|
if (crypto_pk_public_checksig(pkey, signed_digest, tok->object_body,
|
2007-05-02 23:37:55 +02:00
|
|
|
tok->object_size)
|
2007-05-15 00:51:05 +02:00
|
|
|
!= DIGEST_LEN) {
|
|
|
|
log_warn(LD_DIR, "Error reading %s: invalid signature.", doctype);
|
2007-05-27 06:38:50 +02:00
|
|
|
tor_free(signed_digest);
|
2004-08-07 03:48:50 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2007-05-15 00:51:05 +02:00
|
|
|
log_debug(LD_DIR,"Signed %s hash starts %s", doctype,
|
2006-02-13 11:33:00 +01:00
|
|
|
hex_str(signed_digest,4));
|
2005-08-24 04:31:02 +02:00
|
|
|
if (memcmp(digest, signed_digest, DIGEST_LEN)) {
|
2007-05-15 00:51:05 +02:00
|
|
|
log_warn(LD_DIR, "Error reading %s: signature does not match.", doctype);
|
2007-05-27 06:38:50 +02:00
|
|
|
tor_free(signed_digest);
|
2004-08-07 03:48:50 +02:00
|
|
|
return -1;
|
2004-05-10 19:30:51 +02:00
|
|
|
}
|
2007-05-27 06:38:50 +02:00
|
|
|
tor_free(signed_digest);
|
2004-06-25 02:29:31 +02:00
|
|
|
return 0;
|
2004-05-10 19:30:51 +02:00
|
|
|
}
|
|
|
|
|
2004-07-17 01:05:40 +02:00
|
|
|
/** Given a string *<b>s</b> containing a concatenated sequence of router
|
2007-05-15 00:51:05 +02:00
|
|
|
* descriptors (or extra-info documents if <b>is_extrainfo</b> is set), parses
|
|
|
|
* them and stores the result in <b>dest</b>. All routers are marked running
|
|
|
|
* and valid. Advances *s to a point immediately following the last router
|
|
|
|
* entry. Ignore any trailing router entries that are not complete.
|
2007-01-22 08:51:06 +01:00
|
|
|
*
|
|
|
|
* If <b>saved_location</b> isn't SAVED_IN_CACHE, make a local copy of each
|
|
|
|
* descriptor in the signed_descriptor_body field of each routerinfo_t. If it
|
|
|
|
* isn't SAVED_NOWHERE, remember the offset of each descriptor.
|
|
|
|
*
|
|
|
|
* Returns 0 on success and -1 on failure.
|
2004-05-10 19:30:51 +02:00
|
|
|
*/
|
|
|
|
int
|
2007-05-22 04:20:52 +02:00
|
|
|
router_parse_list_from_string(const char **s, const char *eos,
|
|
|
|
smartlist_t *dest,
|
2007-04-30 21:48:39 +02:00
|
|
|
saved_location_t saved_location,
|
2007-05-15 09:13:56 +02:00
|
|
|
int want_extrainfo)
|
2004-05-10 19:30:51 +02:00
|
|
|
{
|
|
|
|
routerinfo_t *router;
|
2007-04-30 21:48:39 +02:00
|
|
|
extrainfo_t *extrainfo;
|
|
|
|
signed_descriptor_t *signed_desc;
|
|
|
|
void *elt;
|
2007-05-15 09:13:56 +02:00
|
|
|
const char *end, *start;
|
|
|
|
int have_extrainfo;
|
2004-05-10 19:30:51 +02:00
|
|
|
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(s);
|
|
|
|
tor_assert(*s);
|
2005-09-30 23:22:25 +02:00
|
|
|
tor_assert(dest);
|
2004-05-10 19:30:51 +02:00
|
|
|
|
2006-04-29 20:42:26 +02:00
|
|
|
start = *s;
|
2007-05-22 04:20:52 +02:00
|
|
|
if (!eos)
|
|
|
|
eos = *s + strlen(*s);
|
|
|
|
|
|
|
|
tor_assert(eos >= *s);
|
|
|
|
|
2004-05-10 19:30:51 +02:00
|
|
|
while (1) {
|
2007-05-22 04:20:52 +02:00
|
|
|
*s = eat_whitespace_eos(*s, eos);
|
|
|
|
if ((eos - *s) < 32) /* make sure it's long enough. */
|
|
|
|
break;
|
|
|
|
|
2004-05-10 19:30:51 +02:00
|
|
|
/* Don't start parsing the rest of *s unless it contains a router. */
|
2007-05-15 09:13:56 +02:00
|
|
|
if (strcmpstart(*s, "extra-info ")==0) {
|
|
|
|
have_extrainfo = 1;
|
|
|
|
} else if (strcmpstart(*s, "router ")==0) {
|
|
|
|
have_extrainfo = 0;
|
2007-04-30 21:48:39 +02:00
|
|
|
} else {
|
2007-05-15 09:13:56 +02:00
|
|
|
/* skip junk. */
|
2007-05-22 04:20:52 +02:00
|
|
|
const char *ei = tor_memstr(*s, eos-*s, "\nextra-info ");
|
|
|
|
const char *ri = tor_memstr(*s, eos-*s, "\nrouter ");
|
2007-05-15 09:13:56 +02:00
|
|
|
if (ri && (!ei || ri < ei)) {
|
|
|
|
have_extrainfo = 0;
|
|
|
|
*s = ri + 1;
|
|
|
|
} else if (ei) {
|
|
|
|
have_extrainfo = 1;
|
|
|
|
*s = ei + 1;
|
|
|
|
} else {
|
2007-04-30 21:48:39 +02:00
|
|
|
break;
|
2007-05-15 09:13:56 +02:00
|
|
|
}
|
2007-04-30 21:48:39 +02:00
|
|
|
}
|
2007-05-22 04:20:52 +02:00
|
|
|
end = tor_memstr(*s, eos-*s, "\nrouter-signature");
|
2007-05-15 09:13:56 +02:00
|
|
|
if (end)
|
2007-07-16 05:39:21 +02:00
|
|
|
end = tor_memstr(end, eos-end, "\n-----END SIGNATURE-----\n");
|
2007-05-15 09:13:56 +02:00
|
|
|
if (end)
|
|
|
|
end += strlen("\n-----END SIGNATURE-----\n");
|
2005-10-14 04:26:13 +02:00
|
|
|
|
2007-05-15 09:13:56 +02:00
|
|
|
if (!end)
|
|
|
|
break;
|
2004-05-10 19:30:51 +02:00
|
|
|
|
2007-05-29 17:08:55 +02:00
|
|
|
elt = NULL;
|
|
|
|
|
2007-05-15 09:13:56 +02:00
|
|
|
if (have_extrainfo && want_extrainfo) {
|
2007-04-30 21:48:39 +02:00
|
|
|
routerlist_t *rl = router_get_routerlist();
|
2007-06-12 00:19:40 +02:00
|
|
|
/* XXXX020 fix this cast to digestmap_t* */
|
2007-04-30 21:48:39 +02:00
|
|
|
extrainfo = extrainfo_parse_entry_from_string(*s, end,
|
|
|
|
saved_location != SAVED_IN_CACHE,
|
2007-06-12 00:19:40 +02:00
|
|
|
(digestmap_t*)rl->identity_map);
|
2007-05-29 17:08:55 +02:00
|
|
|
if (extrainfo) {
|
|
|
|
signed_desc = &extrainfo->cache_info;
|
|
|
|
elt = extrainfo;
|
|
|
|
}
|
2007-05-15 09:13:56 +02:00
|
|
|
} else if (!have_extrainfo && !want_extrainfo) {
|
2007-04-30 21:48:39 +02:00
|
|
|
router = router_parse_entry_from_string(*s, end,
|
|
|
|
saved_location != SAVED_IN_CACHE);
|
2007-05-29 17:08:55 +02:00
|
|
|
if (router) {
|
|
|
|
signed_desc = &router->cache_info;
|
|
|
|
elt = router;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!elt) {
|
2006-06-22 09:19:28 +02:00
|
|
|
*s = end;
|
2004-05-10 19:30:51 +02:00
|
|
|
continue;
|
|
|
|
}
|
2006-06-22 09:01:54 +02:00
|
|
|
if (saved_location != SAVED_NOWHERE) {
|
2007-04-30 21:48:39 +02:00
|
|
|
signed_desc->saved_location = saved_location;
|
|
|
|
signed_desc->saved_offset = *s - start;
|
2006-04-29 20:42:26 +02:00
|
|
|
}
|
2006-06-22 09:19:28 +02:00
|
|
|
*s = end;
|
2007-04-30 21:48:39 +02:00
|
|
|
smartlist_add(dest, elt);
|
2004-05-10 19:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-11-30 23:23:01 +01:00
|
|
|
/* For debugging: define to count every descriptor digest we've seen so we
|
|
|
|
* know if we need to try harder to avoid duplicate verifies. */
|
|
|
|
#undef COUNT_DISTINCT_DIGESTS
|
|
|
|
|
|
|
|
#ifdef COUNT_DISTINCT_DIGESTS
|
|
|
|
static digestmap_t *verified_digests = NULL;
|
|
|
|
#endif
|
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Log the total count of the number of distinct router digests we've ever
|
|
|
|
* verified. When compared to the number of times we've verified routerdesc
|
|
|
|
* signatures <i>in toto</i>, this will tell us if we're doing too much
|
|
|
|
* multiple-verification. */
|
2006-11-01 19:16:44 +01:00
|
|
|
void
|
2006-11-30 23:23:01 +01:00
|
|
|
dump_distinct_digest_count(int severity)
|
2006-11-14 01:06:45 +01:00
|
|
|
{
|
2006-11-30 23:23:01 +01:00
|
|
|
#ifdef COUNT_DISTINCT_DIGESTS
|
|
|
|
if (!verified_digests)
|
|
|
|
verified_digests = digestmap_new();
|
2006-11-01 19:16:44 +01:00
|
|
|
log(severity, LD_GENERAL, "%d *distinct* router digests verified",
|
2006-11-30 23:23:01 +01:00
|
|
|
digestmap_size(verified_digests));
|
|
|
|
#else
|
|
|
|
(void)severity; /* suppress "unused parameter" warning */
|
|
|
|
#endif
|
2006-11-01 19:16:44 +01:00
|
|
|
}
|
|
|
|
|
2004-05-10 19:30:51 +02:00
|
|
|
/** Helper function: reads a single router entry from *<b>s</b> ...
|
|
|
|
* *<b>end</b>. Mallocs a new router and returns it if all goes well, else
|
2007-01-22 08:51:06 +01:00
|
|
|
* returns NULL. If <b>cache_copy</b> is true, duplicate the contents of
|
|
|
|
* s through end into the signed_descriptor_body of the resulting
|
|
|
|
* routerinfo_t.
|
2004-05-10 19:30:51 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
routerinfo_t *
|
2006-06-22 09:01:54 +02:00
|
|
|
router_parse_entry_from_string(const char *s, const char *end,
|
|
|
|
int cache_copy)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-05-10 19:30:51 +02:00
|
|
|
routerinfo_t *router = NULL;
|
|
|
|
char digest[128];
|
|
|
|
smartlist_t *tokens = NULL, *exit_policy_tokens = NULL;
|
|
|
|
directory_token_t *tok;
|
2005-08-16 04:52:27 +02:00
|
|
|
struct in_addr in;
|
2004-05-10 19:30:51 +02:00
|
|
|
|
|
|
|
if (!end) {
|
|
|
|
end = s + strlen(s);
|
|
|
|
}
|
|
|
|
|
2005-10-07 20:33:30 +02:00
|
|
|
/* point 'end' to a point immediately after the final newline. */
|
|
|
|
while (end > s+2 && *(end-1) == '\n' && *(end-2) == '\n')
|
|
|
|
--end;
|
|
|
|
|
2004-05-10 19:30:51 +02:00
|
|
|
if (router_get_router_hash(s, digest) < 0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Couldn't compute router hash.");
|
2004-05-10 19:30:51 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
tokens = smartlist_create();
|
2007-04-16 06:18:21 +02:00
|
|
|
if (tokenize_string(s,end,tokens,routerdesc_token_table)) {
|
2007-05-25 21:41:31 +02:00
|
|
|
log_warn(LD_DIR, "Error tokenizing router descriptor.");
|
2005-05-02 23:48:25 +02:00
|
|
|
goto err;
|
2004-05-10 19:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (smartlist_len(tokens) < 2) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Impossibly short router descriptor.");
|
2004-05-10 19:30:51 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
tok = smartlist_get(tokens,0);
|
|
|
|
if (tok->tp != K_ROUTER) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR,"Entry does not start with \"router\"");
|
2004-05-10 19:30:51 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2007-04-16 18:28:06 +02:00
|
|
|
tor_assert(tok->n_args >= 5);
|
2004-05-10 19:30:51 +02:00
|
|
|
|
|
|
|
router = tor_malloc_zero(sizeof(routerinfo_t));
|
2006-09-20 01:18:30 +02:00
|
|
|
router->routerlist_index = -1;
|
2006-06-22 09:01:54 +02:00
|
|
|
if (cache_copy)
|
|
|
|
router->cache_info.signed_descriptor_body = tor_strndup(s, end-s);
|
2005-11-05 21:15:27 +01:00
|
|
|
router->cache_info.signed_descriptor_len = end-s;
|
|
|
|
memcpy(router->cache_info.signed_descriptor_digest, digest, DIGEST_LEN);
|
2004-05-10 19:30:51 +02:00
|
|
|
|
2007-04-16 18:28:06 +02:00
|
|
|
router->nickname = tor_strdup(tok->args[0]);
|
|
|
|
if (!is_legal_nickname(router->nickname)) {
|
|
|
|
log_warn(LD_DIR,"Router nickname is invalid");
|
2004-05-10 19:30:51 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2007-04-16 18:28:06 +02:00
|
|
|
router->address = tor_strdup(tok->args[1]);
|
|
|
|
if (!tor_inet_aton(router->address, &in)) {
|
|
|
|
log_warn(LD_DIR,"Router address is not an IP.");
|
2004-05-10 19:30:51 +02:00
|
|
|
goto err;
|
2007-04-16 18:28:06 +02:00
|
|
|
}
|
|
|
|
router->addr = ntohl(in.s_addr);
|
2007-02-24 20:29:42 +01:00
|
|
|
|
2007-04-16 18:28:06 +02:00
|
|
|
router->or_port =
|
|
|
|
(uint16_t) tor_parse_long(tok->args[2],10,0,65535,NULL,NULL);
|
|
|
|
router->dir_port =
|
|
|
|
(uint16_t) tor_parse_long(tok->args[4],10,0,65535,NULL,NULL);
|
2007-02-24 20:29:42 +01:00
|
|
|
|
2007-04-16 18:28:06 +02:00
|
|
|
tok = find_first_by_keyword(tokens, K_BANDWIDTH);
|
|
|
|
tor_assert(tok && tok->n_args >= 3);
|
|
|
|
router->bandwidthrate =
|
|
|
|
tor_parse_long(tok->args[0],10,0,INT_MAX,NULL,NULL);
|
2007-02-24 20:29:42 +01:00
|
|
|
|
2007-04-16 18:28:06 +02:00
|
|
|
if (!router->bandwidthrate) {
|
|
|
|
log_warn(LD_DIR, "bandwidthrate %s unreadable or 0. Failing.",
|
|
|
|
escaped(tok->args[0]));
|
|
|
|
goto err;
|
2004-05-10 19:30:51 +02:00
|
|
|
}
|
2007-04-16 18:28:06 +02:00
|
|
|
router->bandwidthburst =
|
|
|
|
tor_parse_long(tok->args[1],10,0,INT_MAX,NULL,NULL);
|
|
|
|
router->bandwidthcapacity =
|
|
|
|
tor_parse_long(tok->args[2],10,0,INT_MAX,NULL,NULL);
|
2007-05-24 20:12:44 +02:00
|
|
|
/* XXXX020 we don't error-check these values? -RD */
|
2004-05-10 19:30:51 +02:00
|
|
|
|
2004-08-17 07:29:41 +02:00
|
|
|
if ((tok = find_first_by_keyword(tokens, K_UPTIME))) {
|
2007-04-16 18:28:06 +02:00
|
|
|
tor_assert(tok->n_args >= 1);
|
|
|
|
router->uptime = tor_parse_long(tok->args[0],10,0,LONG_MAX,NULL,NULL);
|
2004-08-17 07:29:41 +02:00
|
|
|
}
|
|
|
|
|
2005-08-30 17:04:24 +02:00
|
|
|
if ((tok = find_first_by_keyword(tokens, K_HIBERNATING))) {
|
2007-04-16 18:28:06 +02:00
|
|
|
tor_assert(tok->n_args >= 1);
|
|
|
|
router->is_hibernating
|
|
|
|
= (tor_parse_long(tok->args[0],10,0,LONG_MAX,NULL,NULL) != 0);
|
2005-08-30 17:04:24 +02:00
|
|
|
}
|
|
|
|
|
2007-04-16 06:18:29 +02:00
|
|
|
tok = find_first_by_keyword(tokens, K_PUBLISHED);
|
|
|
|
tor_assert(tok);
|
2004-05-10 19:30:51 +02:00
|
|
|
tor_assert(tok->n_args == 1);
|
2005-11-05 21:15:27 +01:00
|
|
|
if (parse_iso_time(tok->args[0], &router->cache_info.published_on) < 0)
|
2004-10-17 00:14:52 +02:00
|
|
|
goto err;
|
2004-05-10 19:30:51 +02:00
|
|
|
|
2007-04-16 06:18:29 +02:00
|
|
|
tok = find_first_by_keyword(tokens, K_ONION_KEY);
|
|
|
|
tor_assert(tok);
|
2004-05-10 19:30:51 +02:00
|
|
|
router->onion_pkey = tok->key;
|
|
|
|
tok->key = NULL; /* Prevent free */
|
|
|
|
|
2007-04-16 06:18:29 +02:00
|
|
|
tok = find_first_by_keyword(tokens, K_SIGNING_KEY);
|
|
|
|
tor_assert(tok);
|
2004-05-10 19:30:51 +02:00
|
|
|
router->identity_pkey = tok->key;
|
|
|
|
tok->key = NULL; /* Prevent free */
|
2005-11-05 21:15:27 +01:00
|
|
|
if (crypto_pk_get_digest(router->identity_pkey,
|
|
|
|
router->cache_info.identity_digest)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Couldn't calculate key digest"); goto err;
|
2004-07-01 03:16:59 +02:00
|
|
|
}
|
2004-05-10 19:30:51 +02:00
|
|
|
|
2005-11-15 22:24:32 +01:00
|
|
|
if ((tok = find_first_by_keyword(tokens, K_FINGERPRINT))) {
|
|
|
|
/* If there's a fingerprint line, it must match the identity digest. */
|
|
|
|
char d[DIGEST_LEN];
|
2007-04-16 18:28:06 +02:00
|
|
|
tor_assert(tok->n_args == 1);
|
2005-11-15 22:24:32 +01:00
|
|
|
tor_strstrip(tok->args[0], " ");
|
|
|
|
if (base16_decode(d, DIGEST_LEN, tok->args[0], strlen(tok->args[0]))) {
|
2006-09-30 00:33:40 +02:00
|
|
|
log_warn(LD_DIR, "Couldn't decode router fingerprint %s",
|
2006-03-05 10:50:26 +01:00
|
|
|
escaped(tok->args[0]));
|
2005-11-15 22:24:32 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (memcmp(d,router->cache_info.identity_digest, DIGEST_LEN)!=0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Fingerprint '%s' does not match identity digest.",
|
|
|
|
tok->args[0]);
|
2005-11-15 22:24:32 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-10 19:30:51 +02:00
|
|
|
if ((tok = find_first_by_keyword(tokens, K_PLATFORM))) {
|
|
|
|
router->platform = tor_strdup(tok->args[0]);
|
|
|
|
}
|
|
|
|
|
2005-05-02 23:22:31 +02:00
|
|
|
if ((tok = find_first_by_keyword(tokens, K_CONTACT))) {
|
|
|
|
router->contact_info = tor_strdup(tok->args[0]);
|
|
|
|
}
|
|
|
|
|
2006-09-21 23:48:06 +02:00
|
|
|
if ((tok = find_first_by_keyword(tokens, K_EVENTDNS))) {
|
|
|
|
router->has_old_dnsworkers = tok->n_args && !strcmp(tok->args[0], "0");
|
|
|
|
} else if (router->platform) {
|
|
|
|
if (! tor_version_as_new_as(router->platform, "0.1.2.2-alpha"))
|
|
|
|
router->has_old_dnsworkers = 1;
|
|
|
|
}
|
|
|
|
|
2004-05-10 19:30:51 +02:00
|
|
|
exit_policy_tokens = find_all_exitpolicy(tokens);
|
|
|
|
SMARTLIST_FOREACH(exit_policy_tokens, directory_token_t *, t,
|
|
|
|
if (router_add_exit_policy(router,t)<0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR,"Error in exit policy");
|
2005-05-02 23:48:25 +02:00
|
|
|
goto err;
|
|
|
|
});
|
2004-05-10 19:30:51 +02:00
|
|
|
|
2004-10-15 03:58:11 +02:00
|
|
|
if ((tok = find_first_by_keyword(tokens, K_FAMILY)) && tok->n_args) {
|
|
|
|
int i;
|
|
|
|
router->declared_family = smartlist_create();
|
|
|
|
for (i=0;i<tok->n_args;++i) {
|
|
|
|
if (!is_legal_nickname_or_hexdigest(tok->args[i])) {
|
2006-03-05 10:50:26 +01:00
|
|
|
log_warn(LD_DIR, "Illegal nickname %s in family line",
|
|
|
|
escaped(tok->args[i]));
|
2004-10-15 03:58:11 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
smartlist_add(router->declared_family, tor_strdup(tok->args[i]));
|
|
|
|
}
|
|
|
|
}
|
2004-11-09 21:04:00 +01:00
|
|
|
|
2007-04-16 06:17:58 +02:00
|
|
|
if ((tok = find_first_by_keyword(tokens, K_CACHES_EXTRA_INFO)))
|
|
|
|
router->caches_extra_info = 1;
|
|
|
|
|
2007-04-16 18:28:06 +02:00
|
|
|
if ((tok = find_first_by_keyword(tokens, K_EXTRA_INFO_DIGEST))) {
|
|
|
|
tor_assert(tok->n_args >= 1);
|
2007-04-16 06:17:58 +02:00
|
|
|
if (strlen(tok->args[0]) == HEX_DIGEST_LEN) {
|
2007-04-16 23:37:21 +02:00
|
|
|
base16_decode(router->cache_info.extra_info_digest,
|
|
|
|
DIGEST_LEN, tok->args[0], HEX_DIGEST_LEN);
|
2007-04-16 06:17:58 +02:00
|
|
|
} else {
|
|
|
|
log_warn(LD_DIR, "Invalid extra info digest %s", escaped(tok->args[0]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-16 06:18:29 +02:00
|
|
|
tok = find_first_by_keyword(tokens, K_ROUTER_SIGNATURE);
|
|
|
|
tor_assert(tok);
|
2006-10-31 20:17:07 +01:00
|
|
|
note_crypto_pk_op(VERIFY_RTR);
|
2006-11-30 23:23:01 +01:00
|
|
|
#ifdef COUNT_DISTINCT_DIGESTS
|
|
|
|
if (!verified_digests)
|
|
|
|
verified_digests = digestmap_new();
|
|
|
|
digestmap_set(verified_digests, signed_digest, (void*)(uintptr_t)1);
|
|
|
|
#endif
|
2007-05-15 00:51:05 +02:00
|
|
|
if (check_signature_token(digest, tok, router->identity_pkey, 0,
|
|
|
|
"router descriptor") < 0)
|
2005-05-02 23:48:25 +02:00
|
|
|
goto err;
|
2004-05-10 19:30:51 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!router->or_port) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR,"or_port unreadable or 0. Failing.");
|
2004-05-10 19:30:51 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2007-02-24 20:29:42 +01:00
|
|
|
|
2004-05-10 19:30:51 +02:00
|
|
|
if (!router->platform) {
|
|
|
|
router->platform = tor_strdup("<unknown>");
|
|
|
|
}
|
|
|
|
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
err:
|
|
|
|
routerinfo_free(router);
|
|
|
|
router = NULL;
|
|
|
|
done:
|
|
|
|
if (tokens) {
|
2007-05-17 00:15:14 +02:00
|
|
|
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t));
|
2004-05-10 19:30:51 +02:00
|
|
|
smartlist_free(tokens);
|
|
|
|
}
|
|
|
|
if (exit_policy_tokens) {
|
|
|
|
smartlist_free(exit_policy_tokens);
|
|
|
|
}
|
|
|
|
return router;
|
|
|
|
}
|
|
|
|
|
2007-05-15 00:51:05 +02:00
|
|
|
/** Parse a single extrainfo entry from the string <b>s</b>, ending at
|
|
|
|
* <b>end</b>. (If <b>end</b> is NULL, parse up to the end of <b>s</b>.) If
|
|
|
|
* <b>cache_copy</b> is true, make a copy of the extra-info document in the
|
|
|
|
* cache_info fields of the result. If <b>routermap</b> is provided, use it
|
|
|
|
* as a map from router identity to routerinfo_t when looking up signing keys.
|
|
|
|
*/
|
2007-04-16 06:17:58 +02:00
|
|
|
extrainfo_t *
|
|
|
|
extrainfo_parse_entry_from_string(const char *s, const char *end,
|
|
|
|
int cache_copy, digestmap_t *routermap)
|
|
|
|
{
|
|
|
|
extrainfo_t *extrainfo = NULL;
|
|
|
|
char digest[128];
|
|
|
|
smartlist_t *tokens = NULL;
|
|
|
|
directory_token_t *tok;
|
|
|
|
crypto_pk_env_t *key = NULL;
|
|
|
|
routerinfo_t *router;
|
|
|
|
|
|
|
|
if (!end) {
|
|
|
|
end = s + strlen(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* point 'end' to a point immediately after the final newline. */
|
|
|
|
while (end > s+2 && *(end-1) == '\n' && *(end-2) == '\n')
|
|
|
|
--end;
|
|
|
|
|
|
|
|
if (router_get_extrainfo_hash(s, digest) < 0) {
|
|
|
|
log_warn(LD_DIR, "Couldn't compute router hash.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
tokens = smartlist_create();
|
2007-04-16 06:18:21 +02:00
|
|
|
if (tokenize_string(s,end,tokens,extrainfo_token_table)) {
|
2007-05-25 21:41:31 +02:00
|
|
|
log_warn(LD_DIR, "Error tokenizing router descriptor.");
|
2007-04-16 06:17:58 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (smartlist_len(tokens) < 2) {
|
|
|
|
log_warn(LD_DIR, "Impossibly short router descriptor.");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
tok = smartlist_get(tokens,0);
|
|
|
|
if (tok->tp != K_EXTRA_INFO) {
|
|
|
|
log_warn(LD_DIR,"Entry does not start with \"extra-info\"");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
extrainfo = tor_malloc_zero(sizeof(extrainfo_t));
|
2007-05-20 07:15:53 +02:00
|
|
|
extrainfo->cache_info.is_extrainfo = 1;
|
2007-04-16 06:17:58 +02:00
|
|
|
if (cache_copy)
|
|
|
|
extrainfo->cache_info.signed_descriptor_body = tor_strndup(s, end-s);
|
|
|
|
extrainfo->cache_info.signed_descriptor_len = end-s;
|
|
|
|
memcpy(extrainfo->cache_info.signed_descriptor_digest, digest, DIGEST_LEN);
|
2007-04-16 19:55:08 +02:00
|
|
|
|
2007-04-16 18:28:06 +02:00
|
|
|
tor_assert(tok->n_args >= 2);
|
2007-04-16 06:17:58 +02:00
|
|
|
if (!is_legal_nickname(tok->args[0])) {
|
|
|
|
log_warn(LD_DIR,"Bad nickname %s on \"extra-info\"",escaped(tok->args[0]));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
strlcpy(extrainfo->nickname, tok->args[0], sizeof(extrainfo->nickname));
|
|
|
|
if (strlen(tok->args[1]) != HEX_DIGEST_LEN ||
|
|
|
|
base16_decode(extrainfo->cache_info.identity_digest, DIGEST_LEN,
|
|
|
|
tok->args[1], HEX_DIGEST_LEN)) {
|
|
|
|
log_warn(LD_DIR,"Invalid fingerprint %s on \"extra-info\"",
|
|
|
|
escaped(tok->args[1]));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2007-04-16 06:18:29 +02:00
|
|
|
tok = find_first_by_keyword(tokens, K_PUBLISHED);
|
|
|
|
tor_assert(tok);
|
2007-04-16 06:17:58 +02:00
|
|
|
if (parse_iso_time(tok->args[0], &extrainfo->cache_info.published_on)) {
|
|
|
|
log_warn(LD_DIR,"Invalid published time %s on \"extra-info\"",
|
|
|
|
escaped(tok->args[0]));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (routermap &&
|
|
|
|
(router = digestmap_get(routermap,
|
|
|
|
extrainfo->cache_info.identity_digest))) {
|
|
|
|
key = router->identity_pkey;
|
|
|
|
}
|
|
|
|
|
2007-04-16 06:18:29 +02:00
|
|
|
tok = find_first_by_keyword(tokens, K_ROUTER_SIGNATURE);
|
|
|
|
tor_assert(tok);
|
2007-05-15 00:51:05 +02:00
|
|
|
if (strcmp(tok->object_type, "SIGNATURE") ||
|
|
|
|
tok->object_size < 128 || tok->object_size > 512) {
|
|
|
|
log_warn(LD_DIR, "Bad object type or length on extra-info signature");
|
2007-04-16 06:17:58 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key) {
|
|
|
|
note_crypto_pk_op(VERIFY_RTR);
|
2007-05-15 00:51:05 +02:00
|
|
|
if (check_signature_token(digest, tok, key, 0, "extra-info") < 0)
|
2007-04-16 06:17:58 +02:00
|
|
|
goto err;
|
2007-05-15 00:51:05 +02:00
|
|
|
|
2007-04-16 06:17:58 +02:00
|
|
|
} else {
|
2007-05-15 00:51:05 +02:00
|
|
|
extrainfo->pending_sig = tor_memdup(tok->object_body,
|
|
|
|
tok->object_size);
|
|
|
|
extrainfo->pending_sig_len = tok->object_size;
|
2007-04-16 06:17:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
goto done;
|
|
|
|
err:
|
2007-05-15 00:51:05 +02:00
|
|
|
if (extrainfo)
|
|
|
|
extrainfo_free(extrainfo);
|
2007-04-16 06:17:58 +02:00
|
|
|
extrainfo = NULL;
|
|
|
|
done:
|
|
|
|
if (tokens) {
|
2007-05-17 00:15:14 +02:00
|
|
|
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t));
|
2007-04-16 06:17:58 +02:00
|
|
|
smartlist_free(tokens);
|
|
|
|
}
|
|
|
|
return extrainfo;
|
|
|
|
}
|
|
|
|
|
2007-05-15 00:51:05 +02:00
|
|
|
/** Parse a key certificate from <b>s</b>; point <b>end-of-string</b> to
|
|
|
|
* the first character after the certificate. */
|
2007-05-02 23:37:55 +02:00
|
|
|
authority_cert_t *
|
2007-05-22 19:58:25 +02:00
|
|
|
authority_cert_parse_from_string(const char *s, const char **end_of_string)
|
2007-05-02 23:37:55 +02:00
|
|
|
{
|
|
|
|
authority_cert_t *cert = NULL;
|
|
|
|
smartlist_t *tokens = NULL;
|
|
|
|
char digest[DIGEST_LEN];
|
|
|
|
directory_token_t *tok;
|
|
|
|
char fp_declared[DIGEST_LEN];
|
2007-05-22 19:58:25 +02:00
|
|
|
char *eos;
|
2007-05-02 23:37:55 +02:00
|
|
|
size_t len;
|
2007-05-22 19:58:25 +02:00
|
|
|
trusted_dir_server_t *ds;
|
|
|
|
|
|
|
|
s = eat_whitespace(s);
|
|
|
|
eos = strstr(s, "\n-----END SIGNATURE-----\n");
|
2007-05-02 23:37:55 +02:00
|
|
|
if (! eos) {
|
|
|
|
log_warn(LD_DIR, "No end-of-signature found on key certificate");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
eos = strchr(eos+2, '\n');
|
|
|
|
tor_assert(eos);
|
|
|
|
++eos;
|
|
|
|
len = eos - s;
|
|
|
|
|
|
|
|
tokens = smartlist_create();
|
|
|
|
if (tokenize_string(s, eos, tokens, dir_key_certificate_table) < 0) {
|
|
|
|
log_warn(LD_DIR, "Error tokenizing key certificate");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (router_get_hash_impl(s, digest, "dir-key-certificate-version",
|
2007-05-22 20:52:32 +02:00
|
|
|
"\ndir-key-certification", '\n') < 0)
|
2007-05-02 23:37:55 +02:00
|
|
|
goto err;
|
|
|
|
tok = smartlist_get(tokens, 0);
|
|
|
|
if (tok->tp != K_DIR_KEY_CERTIFICATE_VERSION || strcmp(tok->args[0], "3")) {
|
|
|
|
log_warn(LD_DIR,
|
|
|
|
"Key certificate does not begin with a recognized version (3).");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
cert = tor_malloc_zero(sizeof(authority_cert_t));
|
2007-05-22 19:58:25 +02:00
|
|
|
memcpy(cert->cache_info.signed_descriptor_digest, digest, DIGEST_LEN);
|
2007-05-02 23:37:55 +02:00
|
|
|
|
|
|
|
tok = find_first_by_keyword(tokens, K_DIR_SIGNING_KEY);
|
|
|
|
tor_assert(tok && tok->key);
|
|
|
|
cert->signing_key = tok->key;
|
|
|
|
tok->key = NULL;
|
|
|
|
|
|
|
|
tok = find_first_by_keyword(tokens, K_DIR_IDENTITY_KEY);
|
|
|
|
tor_assert(tok && tok->key);
|
|
|
|
cert->identity_key = tok->key;
|
|
|
|
tok->key = NULL;
|
|
|
|
|
|
|
|
tok = find_first_by_keyword(tokens, K_FINGERPRINT);
|
|
|
|
tor_assert(tok && tok->n_args);
|
|
|
|
if (base16_decode(fp_declared, DIGEST_LEN, tok->args[0],
|
|
|
|
strlen(tok->args[0]))) {
|
|
|
|
log_warn(LD_DIR, "Couldn't decode key certificate fingerprint %s",
|
|
|
|
escaped(tok->args[0]));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (crypto_pk_get_digest(cert->identity_key,
|
|
|
|
cert->cache_info.identity_digest))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (memcmp(cert->cache_info.identity_digest, fp_declared, DIGEST_LEN)) {
|
|
|
|
log_warn(LD_DIR, "Digest of certificate key didn't match declared "
|
|
|
|
"fingerprint");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
tok = find_first_by_keyword(tokens, K_DIR_KEY_PUBLISHED);
|
|
|
|
tor_assert(tok);
|
|
|
|
if (parse_iso_time(tok->args[0], &cert->cache_info.published_on) < 0) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
tok = find_first_by_keyword(tokens, K_DIR_KEY_EXPIRES);
|
|
|
|
tor_assert(tok);
|
|
|
|
if (parse_iso_time(tok->args[0], &cert->expires) < 0) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
tok = smartlist_get(tokens, smartlist_len(tokens)-1);
|
|
|
|
if (tok->tp != K_DIR_KEY_CERTIFICATION) {
|
|
|
|
log_warn(LD_DIR, "Certificate didn't end with dir-key-certification.");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2007-05-22 19:58:25 +02:00
|
|
|
/* If we already have this cert, don't bother checking the signature. */
|
|
|
|
ds = trusteddirserver_get_by_v3_auth_digest(
|
|
|
|
cert->cache_info.identity_digest);
|
|
|
|
if (ds && ds->v3_cert &&
|
|
|
|
ds->v3_cert->cache_info.signed_descriptor_len == len &&
|
|
|
|
ds->v3_cert->cache_info.signed_descriptor_body &&
|
|
|
|
! memcmp(s, ds->v3_cert->cache_info.signed_descriptor_body, len)) {
|
|
|
|
log_debug(LD_DIR, "We already checked the signature on this certificate;"
|
|
|
|
" no need to do so again.");
|
|
|
|
} else {
|
|
|
|
if (check_signature_token(digest, tok, cert->identity_key, 0,
|
|
|
|
"key certificate")) {
|
|
|
|
goto err;
|
|
|
|
}
|
2007-05-02 23:37:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cert->cache_info.signed_descriptor_len = len;
|
|
|
|
cert->cache_info.signed_descriptor_body = tor_malloc(len+1);
|
|
|
|
memcpy(cert->cache_info.signed_descriptor_body, s, len);
|
|
|
|
cert->cache_info.signed_descriptor_body[len] = 0;
|
|
|
|
cert->cache_info.saved_location = SAVED_NOWHERE;
|
2007-05-22 19:58:25 +02:00
|
|
|
|
|
|
|
if (end_of_string) {
|
|
|
|
*end_of_string = eat_whitespace(eos);
|
|
|
|
}
|
2007-05-02 23:37:55 +02:00
|
|
|
return cert;
|
|
|
|
err:
|
|
|
|
authority_cert_free(cert);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-09-02 22:37:31 +02:00
|
|
|
/** Helper: given a string <b>s</b>, return the start of the next router-status
|
|
|
|
* object (starting with "r " at the start of a line). If none is found,
|
|
|
|
* return the start of the next directory signature. If none is found, return
|
|
|
|
* the end of the string. */
|
|
|
|
static INLINE const char *
|
|
|
|
find_start_of_next_routerstatus(const char *s)
|
|
|
|
{
|
|
|
|
const char *eos = strstr(s, "\nr ");
|
|
|
|
if (!eos)
|
|
|
|
eos = strstr(s, "\ndirectory-signature");
|
|
|
|
if (eos)
|
|
|
|
return eos+1;
|
|
|
|
else
|
|
|
|
return s + strlen(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Given a string at *<b>s</b>, containing a routerstatus object, and an
|
|
|
|
* empty smartlist at <b>tokens</b>, parse and return the first router status
|
|
|
|
* object in the string, and advance *<b>s</b> to just after the end of the
|
|
|
|
* router status. Return NULL and advance *<b>s</b> on error. */
|
|
|
|
static routerstatus_t *
|
2007-05-31 21:03:44 +02:00
|
|
|
routerstatus_parse_entry_from_string(const char **s, smartlist_t *tokens,
|
|
|
|
networkstatus_vote_t *vote,
|
2007-06-03 05:05:10 +02:00
|
|
|
vote_routerstatus_t *vote_rs)
|
2005-09-02 22:37:31 +02:00
|
|
|
{
|
|
|
|
const char *eos;
|
|
|
|
routerstatus_t *rs = NULL;
|
|
|
|
directory_token_t *tok;
|
|
|
|
char timebuf[ISO_TIME_LEN+1];
|
|
|
|
struct in_addr in;
|
|
|
|
tor_assert(tokens);
|
2007-06-03 05:05:10 +02:00
|
|
|
tor_assert(bool_eq(vote, vote_rs));
|
2005-09-02 22:37:31 +02:00
|
|
|
|
|
|
|
eos = find_start_of_next_routerstatus(*s);
|
|
|
|
|
2007-04-16 06:18:21 +02:00
|
|
|
if (tokenize_string(*s, eos, tokens, rtrstatus_token_table)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Error tokenizing router status");
|
2005-09-02 22:37:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (smartlist_len(tokens) < 1) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Impossibly short router status");
|
2005-09-02 22:37:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2007-04-16 06:18:29 +02:00
|
|
|
tok = find_first_by_keyword(tokens, K_R);
|
|
|
|
tor_assert(tok);
|
2007-04-16 18:28:06 +02:00
|
|
|
tor_assert(tok->n_args >= 8);
|
2007-06-03 05:05:10 +02:00
|
|
|
if (vote_rs) {
|
|
|
|
rs = &vote_rs->status;
|
|
|
|
} else {
|
|
|
|
rs = tor_malloc_zero(sizeof(routerstatus_t));
|
|
|
|
}
|
2005-09-02 22:37:31 +02:00
|
|
|
|
|
|
|
if (!is_legal_nickname(tok->args[0])) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR,
|
2006-03-05 10:50:26 +01:00
|
|
|
"Invalid nickname %s in router status; skipping.",
|
|
|
|
escaped(tok->args[0]));
|
2005-09-02 22:37:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
strlcpy(rs->nickname, tok->args[0], sizeof(rs->nickname));
|
|
|
|
|
2005-09-18 04:22:21 +02:00
|
|
|
if (digest_from_base64(rs->identity_digest, tok->args[1])) {
|
2006-09-30 00:51:47 +02:00
|
|
|
log_warn(LD_DIR, "Error decoding identity digest %s",
|
|
|
|
escaped(tok->args[1]));
|
2005-09-02 22:37:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2005-09-18 04:22:21 +02:00
|
|
|
if (digest_from_base64(rs->descriptor_digest, tok->args[2])) {
|
2006-09-30 00:51:47 +02:00
|
|
|
log_warn(LD_DIR, "Error decoding descriptor digest %s",
|
|
|
|
escaped(tok->args[2]));
|
2005-09-02 22:37:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tor_snprintf(timebuf, sizeof(timebuf), "%s %s",
|
|
|
|
tok->args[3], tok->args[4]) < 0 ||
|
|
|
|
parse_iso_time(timebuf, &rs->published_on)<0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Error parsing time '%s %s'",
|
|
|
|
tok->args[3], tok->args[4]);
|
2005-09-02 22:37:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2005-09-08 20:46:25 +02:00
|
|
|
if (tor_inet_aton(tok->args[5], &in) == 0) {
|
2006-09-30 00:33:40 +02:00
|
|
|
log_warn(LD_DIR, "Error parsing router address in network-status %s",
|
|
|
|
escaped(tok->args[5]));
|
2005-09-02 22:37:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
rs->addr = ntohl(in.s_addr);
|
|
|
|
|
|
|
|
rs->or_port =(uint16_t) tor_parse_long(tok->args[6],10,0,65535,NULL,NULL);
|
|
|
|
rs->dir_port = (uint16_t) tor_parse_long(tok->args[7],10,0,65535,NULL,NULL);
|
|
|
|
|
2007-05-31 21:03:44 +02:00
|
|
|
tok = find_first_by_keyword(tokens, K_S);
|
|
|
|
if (tok && vote) {
|
2007-06-08 20:41:09 +02:00
|
|
|
int i;
|
2007-06-03 05:05:10 +02:00
|
|
|
vote_rs->flags = 0;
|
2007-05-31 21:03:44 +02:00
|
|
|
for (i=0; i < tok->n_args; ++i) {
|
2007-06-08 20:41:09 +02:00
|
|
|
int p = smartlist_string_pos(vote->known_flags, tok->args[i]);
|
|
|
|
if (p >= 0) {
|
|
|
|
vote_rs->flags |= (1<<p);
|
2007-06-08 20:41:12 +02:00
|
|
|
} else {
|
|
|
|
log_warn(LD_DIR, "Flags line had a flag %s not listed in known_flags.",
|
|
|
|
escaped(tok->args[i]));
|
|
|
|
goto err;
|
2007-05-31 21:03:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (tok) {
|
2005-09-02 22:37:31 +02:00
|
|
|
int i;
|
|
|
|
for (i=0; i < tok->n_args; ++i) {
|
|
|
|
if (!strcmp(tok->args[i], "Exit"))
|
|
|
|
rs->is_exit = 1;
|
|
|
|
else if (!strcmp(tok->args[i], "Stable"))
|
|
|
|
rs->is_stable = 1;
|
|
|
|
else if (!strcmp(tok->args[i], "Fast"))
|
|
|
|
rs->is_fast = 1;
|
|
|
|
else if (!strcmp(tok->args[i], "Running"))
|
|
|
|
rs->is_running = 1;
|
|
|
|
else if (!strcmp(tok->args[i], "Named"))
|
|
|
|
rs->is_named = 1;
|
|
|
|
else if (!strcmp(tok->args[i], "Valid"))
|
|
|
|
rs->is_valid = 1;
|
2005-12-12 02:03:28 +01:00
|
|
|
else if (!strcmp(tok->args[i], "V2Dir"))
|
|
|
|
rs->is_v2_dir = 1;
|
2006-01-24 01:31:16 +01:00
|
|
|
else if (!strcmp(tok->args[i], "Guard"))
|
|
|
|
rs->is_possible_guard = 1;
|
2006-10-12 00:06:01 +02:00
|
|
|
else if (!strcmp(tok->args[i], "BadExit"))
|
|
|
|
rs->is_bad_exit = 1;
|
2007-01-03 20:58:00 +01:00
|
|
|
else if (!strcmp(tok->args[i], "BadDirectory"))
|
|
|
|
rs->is_bad_directory = 1;
|
2007-06-13 21:06:23 +02:00
|
|
|
else if (!strcmp(tok->args[i], "Authority"))
|
|
|
|
rs->is_authority = 1;
|
2006-12-24 05:09:48 +01:00
|
|
|
}
|
|
|
|
}
|
2007-04-16 18:28:06 +02:00
|
|
|
if ((tok = find_first_by_keyword(tokens, K_V))) {
|
|
|
|
tor_assert(tok->n_args == 1);
|
2006-12-24 05:09:48 +01:00
|
|
|
rs->version_known = 1;
|
|
|
|
if (strcmpstart(tok->args[0], "Tor ")) {
|
|
|
|
rs->version_supports_begindir = 1;
|
2007-05-01 22:13:49 +02:00
|
|
|
rs->version_supports_extrainfo_upload = 1;
|
2006-12-24 05:09:48 +01:00
|
|
|
} else {
|
|
|
|
rs->version_supports_begindir =
|
|
|
|
tor_version_as_new_as(tok->args[0], "0.1.2.2-alpha");
|
2007-07-17 11:37:00 +02:00
|
|
|
rs->version_supports_extrainfo_upload =
|
2007-05-01 22:13:49 +02:00
|
|
|
tor_version_as_new_as(tok->args[0], "0.2.0.0-alpha-dev (r10070)");
|
2005-09-02 22:37:31 +02:00
|
|
|
}
|
2007-06-03 05:05:10 +02:00
|
|
|
if (vote_rs) {
|
|
|
|
vote_rs->version = tok->args[0];
|
|
|
|
tok->args[0] = NULL; /* suppress free() */
|
|
|
|
}
|
2005-09-02 22:37:31 +02:00
|
|
|
}
|
|
|
|
|
2006-09-29 06:51:28 +02:00
|
|
|
if (!strcasecmp(rs->nickname, UNNAMED_ROUTER_NICKNAME))
|
|
|
|
rs->is_named = 0;
|
|
|
|
|
2005-09-02 22:37:31 +02:00
|
|
|
goto done;
|
|
|
|
err:
|
2007-06-03 05:05:10 +02:00
|
|
|
if (rs && !vote_rs)
|
2005-09-02 22:37:31 +02:00
|
|
|
routerstatus_free(rs);
|
|
|
|
rs = NULL;
|
|
|
|
done:
|
|
|
|
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t));
|
|
|
|
smartlist_clear(tokens);
|
|
|
|
*s = eos;
|
|
|
|
|
|
|
|
return rs;
|
|
|
|
}
|
|
|
|
|
2005-09-12 08:56:42 +02:00
|
|
|
/** Helper to sort a smartlist of pointers to routerstatus_t */
|
|
|
|
static int
|
|
|
|
_compare_routerstatus_entries(const void **_a, const void **_b)
|
|
|
|
{
|
|
|
|
const routerstatus_t *a = *_a, *b = *_b;
|
|
|
|
return memcmp(a->identity_digest, b->identity_digest, DIGEST_LEN);
|
|
|
|
}
|
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Helper: used in call to _smartlist_uniq to clear out duplicate entries. */
|
2006-09-15 06:27:58 +02:00
|
|
|
static void
|
|
|
|
_free_duplicate_routerstatus_entry(void *e)
|
|
|
|
{
|
|
|
|
log_warn(LD_DIR,
|
|
|
|
"Network-status has two entries for the same router. "
|
|
|
|
"Dropping one.");
|
|
|
|
routerstatus_free(e);
|
|
|
|
}
|
|
|
|
|
2007-06-03 05:05:10 +02:00
|
|
|
/** Given a v2 network-status object in <b>s</b>, try to
|
2005-09-02 22:37:31 +02:00
|
|
|
* parse it and return the result. Return NULL on failure. Check the
|
|
|
|
* signature of the network status, but do not (yet) check the signing key for
|
|
|
|
* authority.
|
|
|
|
*/
|
|
|
|
networkstatus_t *
|
|
|
|
networkstatus_parse_from_string(const char *s)
|
|
|
|
{
|
|
|
|
const char *eos;
|
|
|
|
smartlist_t *tokens = smartlist_create();
|
2007-04-16 06:18:21 +02:00
|
|
|
smartlist_t *footer_tokens = smartlist_create();
|
2005-09-02 22:37:31 +02:00
|
|
|
networkstatus_t *ns = NULL;
|
|
|
|
char ns_digest[DIGEST_LEN];
|
|
|
|
char tmp_digest[DIGEST_LEN];
|
|
|
|
struct in_addr in;
|
|
|
|
directory_token_t *tok;
|
2005-09-18 04:22:21 +02:00
|
|
|
int i;
|
2005-09-02 22:37:31 +02:00
|
|
|
|
|
|
|
if (router_get_networkstatus_v2_hash(s, ns_digest)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Unable to compute digest of network-status");
|
2005-09-02 22:37:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
eos = find_start_of_next_routerstatus(s);
|
2007-04-16 06:18:21 +02:00
|
|
|
if (tokenize_string(s, eos, tokens, netstatus_token_table)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Error tokenizing network-status header.");
|
2005-09-02 22:37:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
ns = tor_malloc_zero(sizeof(networkstatus_t));
|
2005-09-07 18:42:53 +02:00
|
|
|
memcpy(ns->networkstatus_digest, ns_digest, DIGEST_LEN);
|
2005-09-02 22:37:31 +02:00
|
|
|
|
2007-04-16 06:18:29 +02:00
|
|
|
tok = find_first_by_keyword(tokens, K_NETWORK_STATUS_VERSION);
|
2007-06-03 05:05:10 +02:00
|
|
|
tor_assert(tok && tok->n_args >= 1);
|
|
|
|
if (strcmp(tok->args[0], "2")) {
|
|
|
|
log_warn(LD_BUG, "Got a non-v2 networkstatus. Version was "
|
|
|
|
"%s", escaped(tok->args[0]));
|
|
|
|
goto err;
|
|
|
|
}
|
2005-09-02 22:37:31 +02:00
|
|
|
|
2007-04-16 06:18:29 +02:00
|
|
|
tok = find_first_by_keyword(tokens, K_DIR_SOURCE);
|
|
|
|
tor_assert(tok);
|
2007-04-16 18:28:06 +02:00
|
|
|
tor_assert(tok->n_args >= 3);
|
2005-09-02 22:37:31 +02:00
|
|
|
ns->source_address = tok->args[0]; tok->args[0] = NULL;
|
2005-09-08 20:53:57 +02:00
|
|
|
if (tor_inet_aton(tok->args[1], &in) == 0) {
|
2006-09-30 00:33:40 +02:00
|
|
|
log_warn(LD_DIR, "Error parsing network-status source address %s",
|
|
|
|
escaped(tok->args[1]));
|
2005-09-02 22:37:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
ns->source_addr = ntohl(in.s_addr);
|
|
|
|
ns->source_dirport =
|
|
|
|
(uint16_t) tor_parse_long(tok->args[2],10,0,65535,NULL,NULL);
|
|
|
|
if (ns->source_dirport == 0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Directory source without dirport; skipping.");
|
2005-09-02 22:37:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2007-04-16 06:18:29 +02:00
|
|
|
tok = find_first_by_keyword(tokens, K_FINGERPRINT);
|
|
|
|
tor_assert(tok);
|
2007-04-16 18:28:06 +02:00
|
|
|
tor_assert(tok->n_args);
|
2005-09-05 01:12:27 +02:00
|
|
|
if (base16_decode(ns->identity_digest, DIGEST_LEN, tok->args[0],
|
2005-09-02 22:37:31 +02:00
|
|
|
strlen(tok->args[0]))) {
|
2006-09-30 00:33:40 +02:00
|
|
|
log_warn(LD_DIR, "Couldn't decode networkstatus fingerprint %s",
|
|
|
|
escaped(tok->args[0]));
|
2005-09-02 22:37:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2007-04-16 18:28:06 +02:00
|
|
|
if ((tok = find_first_by_keyword(tokens, K_CONTACT))) {
|
|
|
|
tor_assert(tok->n_args);
|
2005-09-02 22:37:31 +02:00
|
|
|
ns->contact = tok->args[0];
|
|
|
|
tok->args[0] = NULL;
|
|
|
|
}
|
|
|
|
|
2007-04-16 06:18:29 +02:00
|
|
|
tok = find_first_by_keyword(tokens, K_DIR_SIGNING_KEY);
|
|
|
|
tor_assert(tok && tok->key);
|
2005-09-02 22:37:31 +02:00
|
|
|
ns->signing_key = tok->key;
|
|
|
|
tok->key = NULL;
|
|
|
|
|
|
|
|
if (crypto_pk_get_digest(ns->signing_key, tmp_digest)<0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Couldn't compute signing key digest");
|
2005-09-02 22:37:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2005-09-05 01:12:27 +02:00
|
|
|
if (memcmp(tmp_digest, ns->identity_digest, DIGEST_LEN)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR,
|
|
|
|
"network-status fingerprint did not match dir-signing-key");
|
2005-09-02 22:37:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2005-09-21 02:41:06 +02:00
|
|
|
if ((tok = find_first_by_keyword(tokens, K_DIR_OPTIONS))) {
|
|
|
|
for (i=0; i < tok->n_args; ++i) {
|
|
|
|
if (!strcmp(tok->args[i], "Names"))
|
|
|
|
ns->binds_names = 1;
|
|
|
|
if (!strcmp(tok->args[i], "Versions"))
|
|
|
|
ns->recommends_versions = 1;
|
2006-10-19 17:45:48 +02:00
|
|
|
if (!strcmp(tok->args[i], "BadExits"))
|
|
|
|
ns->lists_bad_exits = 1;
|
2007-01-03 20:58:00 +01:00
|
|
|
if (!strcmp(tok->args[i], "BadDirectories"))
|
|
|
|
ns->lists_bad_directories = 1;
|
2005-09-21 02:41:06 +02:00
|
|
|
}
|
2005-09-02 22:37:31 +02:00
|
|
|
}
|
|
|
|
|
2005-09-21 04:38:51 +02:00
|
|
|
if (ns->recommends_versions) {
|
2007-04-16 18:28:06 +02:00
|
|
|
if (!(tok = find_first_by_keyword(tokens, K_CLIENT_VERSIONS))) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Missing client-versions");
|
2005-09-21 02:41:06 +02:00
|
|
|
}
|
|
|
|
ns->client_versions = tok->args[0];
|
|
|
|
tok->args[0] = NULL;
|
2005-09-02 22:37:31 +02:00
|
|
|
|
2005-09-21 02:41:06 +02:00
|
|
|
if (!(tok = find_first_by_keyword(tokens, K_SERVER_VERSIONS)) ||
|
|
|
|
tok->n_args<1) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Missing server-versions on versioning directory");
|
2005-09-21 02:41:06 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
ns->server_versions = tok->args[0];
|
|
|
|
tok->args[0] = NULL;
|
2005-09-02 22:37:31 +02:00
|
|
|
}
|
|
|
|
|
2007-04-16 06:18:29 +02:00
|
|
|
tok = find_first_by_keyword(tokens, K_PUBLISHED);
|
|
|
|
tor_assert(tok);
|
2005-09-02 22:37:31 +02:00
|
|
|
tor_assert(tok->n_args == 1);
|
|
|
|
if (parse_iso_time(tok->args[0], &ns->published_on) < 0) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ns->entries = smartlist_create();
|
|
|
|
s = eos;
|
|
|
|
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t));
|
|
|
|
smartlist_clear(tokens);
|
|
|
|
while (!strcmpstart(s, "r ")) {
|
|
|
|
routerstatus_t *rs;
|
2007-05-31 21:03:44 +02:00
|
|
|
if ((rs = routerstatus_parse_entry_from_string(&s, tokens, NULL, NULL)))
|
2005-09-02 22:37:31 +02:00
|
|
|
smartlist_add(ns->entries, rs);
|
|
|
|
}
|
2005-09-12 08:56:42 +02:00
|
|
|
smartlist_sort(ns->entries, _compare_routerstatus_entries);
|
2006-09-15 06:27:58 +02:00
|
|
|
smartlist_uniq(ns->entries, _compare_routerstatus_entries,
|
|
|
|
_free_duplicate_routerstatus_entry);
|
2005-09-18 04:22:21 +02:00
|
|
|
|
2007-04-16 06:18:21 +02:00
|
|
|
if (tokenize_string(s, NULL, footer_tokens, dir_footer_token_table)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Error tokenizing network-status footer.");
|
2005-09-02 22:37:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2007-04-16 06:18:21 +02:00
|
|
|
if (smartlist_len(footer_tokens) < 1) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Too few items in network-status footer.");
|
2005-09-02 22:37:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2007-04-16 06:18:21 +02:00
|
|
|
tok = smartlist_get(footer_tokens, smartlist_len(footer_tokens)-1);
|
2005-09-02 22:37:31 +02:00
|
|
|
if (tok->tp != K_DIRECTORY_SIGNATURE) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR,
|
|
|
|
"Expected network-status footer to end with a signature.");
|
2005-09-02 22:37:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2006-10-31 20:17:07 +01:00
|
|
|
note_crypto_pk_op(VERIFY_DIR);
|
2007-05-15 00:51:05 +02:00
|
|
|
if (check_signature_token(ns_digest, tok, ns->signing_key, 0,
|
|
|
|
"network-status") < 0)
|
2005-09-02 22:37:31 +02:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
goto done;
|
|
|
|
err:
|
2005-09-08 20:24:26 +02:00
|
|
|
if (ns)
|
|
|
|
networkstatus_free(ns);
|
2005-09-02 22:37:31 +02:00
|
|
|
ns = NULL;
|
|
|
|
done:
|
|
|
|
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t));
|
|
|
|
smartlist_free(tokens);
|
2007-04-16 06:18:21 +02:00
|
|
|
SMARTLIST_FOREACH(footer_tokens, directory_token_t *, t, token_free(t));
|
|
|
|
smartlist_free(footer_tokens);
|
2005-09-02 22:37:31 +02:00
|
|
|
|
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
|
2007-06-03 05:05:10 +02:00
|
|
|
/** DOCDOC */
|
|
|
|
networkstatus_vote_t *
|
2007-06-05 00:29:00 +02:00
|
|
|
networkstatus_parse_vote_from_string(const char *s, int is_vote)
|
2007-06-03 05:05:10 +02:00
|
|
|
{
|
|
|
|
smartlist_t *tokens = smartlist_create();
|
|
|
|
smartlist_t *rs_tokens = NULL, *footer_tokens = NULL;
|
2007-06-05 00:29:00 +02:00
|
|
|
networkstatus_voter_info_t *voter = NULL;
|
2007-06-03 05:05:10 +02:00
|
|
|
networkstatus_vote_t *ns = NULL;
|
2007-06-05 00:29:00 +02:00
|
|
|
char ns_digest[DIGEST_LEN];
|
|
|
|
const char *cert, *end_of_header, *end_of_footer;
|
2007-06-03 05:05:10 +02:00
|
|
|
directory_token_t *tok;
|
|
|
|
int ok;
|
|
|
|
struct in_addr in;
|
2007-06-08 20:41:12 +02:00
|
|
|
int i, inorder;
|
2007-06-03 05:05:10 +02:00
|
|
|
|
|
|
|
if (router_get_networkstatus_v3_hash(s, ns_digest)) {
|
|
|
|
log_warn(LD_DIR, "Unable to compute digest of network-status");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
end_of_header = find_start_of_next_routerstatus(s);
|
|
|
|
if (tokenize_string(s, end_of_header, tokens,
|
2007-06-05 00:29:00 +02:00
|
|
|
is_vote ? networkstatus_vote_token_table :
|
|
|
|
networkstatus_consensus_token_table)) {
|
2007-06-03 05:05:10 +02:00
|
|
|
log_warn(LD_DIR, "Error tokenizing network-status vote header.");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ns = tor_malloc_zero(sizeof(networkstatus_vote_t));
|
2007-06-05 00:57:23 +02:00
|
|
|
memcpy(ns->networkstatus_digest, ns_digest, DIGEST_LEN);
|
2007-06-05 00:29:00 +02:00
|
|
|
|
|
|
|
if (is_vote) {
|
|
|
|
const char *end_of_cert = NULL;
|
|
|
|
if (!(cert = strstr(s, "\ndir-key-certificate-version")))
|
|
|
|
goto err;
|
|
|
|
++cert;
|
|
|
|
ns->cert = authority_cert_parse_from_string(cert, &end_of_cert);
|
|
|
|
if (!ns->cert || !end_of_cert || end_of_cert > end_of_header)
|
|
|
|
goto err;
|
|
|
|
}
|
2007-06-03 05:05:10 +02:00
|
|
|
|
|
|
|
tok = find_first_by_keyword(tokens, K_VOTE_STATUS);
|
|
|
|
tor_assert(tok);
|
|
|
|
tor_assert(tok->n_args);
|
2007-06-05 00:29:00 +02:00
|
|
|
if (!strcmp(tok->args[0], "vote")) {
|
|
|
|
ns->is_vote = 1;
|
|
|
|
} else if (!strcmp(tok->args[0], "consensus")) {
|
|
|
|
ns->is_vote = 0;
|
|
|
|
} else {
|
|
|
|
log_warn(LD_DIR, "Unrecognized vote status %s in network-status",
|
2007-06-03 05:05:10 +02:00
|
|
|
escaped(tok->args[0]));
|
|
|
|
goto err;
|
|
|
|
}
|
2007-06-05 00:29:00 +02:00
|
|
|
if (!bool_eq(ns->is_vote, is_vote)) {
|
|
|
|
log_warn(LD_DIR, "Got the wrong kind of v3 networkstatus.");
|
2007-06-03 05:05:10 +02:00
|
|
|
goto err;
|
2007-06-05 00:29:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ns->is_vote) {
|
|
|
|
tok = find_first_by_keyword(tokens, K_PUBLISHED);
|
|
|
|
if (parse_iso_time(tok->args[0], &ns->published))
|
|
|
|
goto err;
|
|
|
|
}
|
2007-06-03 05:05:10 +02:00
|
|
|
|
|
|
|
tok = find_first_by_keyword(tokens, K_VALID_AFTER);
|
|
|
|
if (parse_iso_time(tok->args[0], &ns->valid_after))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
tok = find_first_by_keyword(tokens, K_FRESH_UNTIL);
|
|
|
|
if (parse_iso_time(tok->args[0], &ns->fresh_until))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
tok = find_first_by_keyword(tokens, K_VALID_UNTIL);
|
|
|
|
if (parse_iso_time(tok->args[0], &ns->valid_until))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
tok = find_first_by_keyword(tokens, K_VOTING_DELAY);
|
|
|
|
tor_assert(tok->n_args >= 2);
|
|
|
|
ns->vote_seconds =
|
|
|
|
(int) tor_parse_long(tok->args[0], 10, 0, INT_MAX, &ok, NULL);
|
|
|
|
if (!ok)
|
|
|
|
goto err;
|
|
|
|
ns->dist_seconds =
|
2007-06-13 20:16:05 +02:00
|
|
|
(int) tor_parse_long(tok->args[1], 10, 0, INT_MAX, &ok, NULL);
|
2007-06-03 05:05:10 +02:00
|
|
|
if (!ok)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if ((tok = find_first_by_keyword(tokens, K_CLIENT_VERSIONS))) {
|
|
|
|
ns->client_versions = tok->args[0];
|
|
|
|
tok->args[0] = NULL;
|
|
|
|
}
|
|
|
|
if ((tok = find_first_by_keyword(tokens, K_SERVER_VERSIONS))) {
|
|
|
|
ns->server_versions = tok->args[0];
|
|
|
|
tok->args[0] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tok = find_first_by_keyword(tokens, K_KNOWN_FLAGS);
|
2007-06-08 20:41:09 +02:00
|
|
|
ns->known_flags = smartlist_create();
|
2007-06-08 20:41:12 +02:00
|
|
|
inorder = 1;
|
|
|
|
for (i = 0; i < tok->n_args; ++i) {
|
2007-06-08 20:41:09 +02:00
|
|
|
smartlist_add(ns->known_flags, tok->args[i]);
|
2007-06-13 20:16:05 +02:00
|
|
|
if (i>0 && strcmp(tok->args[i-1], tok->args[i])>= 0) {
|
|
|
|
log_warn(LD_DIR, "%s >= %s", tok->args[i-1], tok->args[i]);
|
2007-06-08 20:41:12 +02:00
|
|
|
inorder = 0;
|
2007-06-13 20:16:05 +02:00
|
|
|
}
|
2007-06-08 20:41:12 +02:00
|
|
|
}
|
2007-06-03 05:05:10 +02:00
|
|
|
tok->n_args = 0; /* suppress free of args members, but not of args itself. */
|
2007-06-08 20:41:12 +02:00
|
|
|
if (!inorder) {
|
|
|
|
log_warn(LD_DIR, "known-flags not in order");
|
|
|
|
goto err;
|
|
|
|
}
|
2007-06-03 05:05:10 +02:00
|
|
|
|
2007-06-05 00:29:00 +02:00
|
|
|
ns->voters = smartlist_create();
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(tokens, directory_token_t *, _tok,
|
|
|
|
{
|
|
|
|
tok = _tok;
|
|
|
|
if (tok->tp == K_DIR_SOURCE) {
|
|
|
|
tor_assert(tok->n_args >= 6);
|
|
|
|
|
|
|
|
if (voter)
|
|
|
|
smartlist_add(ns->voters, voter);
|
|
|
|
voter = tor_malloc_zero(sizeof(networkstatus_voter_info_t));
|
2007-06-13 21:06:26 +02:00
|
|
|
if (is_vote)
|
|
|
|
memcpy(voter->vote_digest, ns_digest, DIGEST_LEN);
|
2007-06-05 00:29:00 +02:00
|
|
|
|
|
|
|
voter->nickname = tor_strdup(tok->args[0]);
|
|
|
|
if (strlen(tok->args[1]) != HEX_DIGEST_LEN ||
|
|
|
|
base16_decode(voter->identity_digest, sizeof(voter->identity_digest),
|
|
|
|
tok->args[1], HEX_DIGEST_LEN) < 0) {
|
|
|
|
log_warn(LD_DIR, "Error decoding identity digest %s in "
|
|
|
|
"network-status vote.", escaped(tok->args[1]));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
voter->address = tor_strdup(tok->args[2]);
|
|
|
|
if (!tor_inet_aton(tok->args[3], &in)) {
|
|
|
|
log_warn(LD_DIR, "Error decoding IP address %s in network-status.",
|
|
|
|
escaped(tok->args[3]));
|
|
|
|
goto err;
|
|
|
|
}
|
2007-06-13 20:16:05 +02:00
|
|
|
voter->addr = ntohl(in.s_addr);
|
2007-06-05 00:29:00 +02:00
|
|
|
voter->dir_port = (uint64_t)
|
|
|
|
(int) tor_parse_long(tok->args[4], 10, 0, 65535, &ok, NULL);
|
|
|
|
if (!ok)
|
|
|
|
goto err;
|
|
|
|
voter->or_port = (uint64_t)
|
|
|
|
(int) tor_parse_long(tok->args[5], 10, 0, 65535, &ok, NULL);
|
|
|
|
if (!ok)
|
|
|
|
goto err;
|
|
|
|
} else if (tok->tp == K_CONTACT) {
|
|
|
|
if (!voter || voter->contact) {
|
|
|
|
log_warn(LD_DIR, "contact element is out of place.");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
voter->contact = tor_strdup(tok->args[0]);
|
|
|
|
} else if (tok->tp == K_VOTE_DIGEST) {
|
|
|
|
tor_assert(!is_vote);
|
|
|
|
tor_assert(tok->n_args >= 1);
|
|
|
|
if (!voter || ! tor_digest_is_zero(voter->vote_digest)) {
|
|
|
|
log_warn(LD_DIR, "vote-digest element is out of place.");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (strlen(tok->args[0]) != HEX_DIGEST_LEN ||
|
|
|
|
base16_decode(voter->vote_digest, sizeof(voter->vote_digest),
|
|
|
|
tok->args[0], HEX_DIGEST_LEN) < 0) {
|
|
|
|
log_warn(LD_DIR, "Error decoding vote digest %s in "
|
|
|
|
"network-status consensus.", escaped(tok->args[1]));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (voter) {
|
|
|
|
smartlist_add(ns->voters, voter);
|
|
|
|
voter = NULL;
|
2007-06-03 05:05:10 +02:00
|
|
|
}
|
2007-06-05 00:29:00 +02:00
|
|
|
if (smartlist_len(ns->voters) == 0) {
|
|
|
|
log_warn(LD_DIR, "Missing dir-source elements in a vote networkstatus.");
|
2007-06-03 05:05:10 +02:00
|
|
|
goto err;
|
2007-06-05 00:29:00 +02:00
|
|
|
} else if (is_vote && smartlist_len(ns->voters) != 1) {
|
|
|
|
log_warn(LD_DIR, "Too many dir-source elements in a vote networkstatus.");
|
2007-06-03 05:05:10 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse routerstatus lines. */
|
|
|
|
rs_tokens = smartlist_create();
|
|
|
|
s = end_of_header;
|
|
|
|
ns->routerstatus_list = smartlist_create();
|
2007-06-05 00:29:00 +02:00
|
|
|
|
2007-06-03 05:05:10 +02:00
|
|
|
while (!strcmpstart(s, "r ")) {
|
2007-06-05 00:29:00 +02:00
|
|
|
if (is_vote) {
|
|
|
|
vote_routerstatus_t *rs = tor_malloc_zero(sizeof(vote_routerstatus_t));
|
|
|
|
if (routerstatus_parse_entry_from_string(&s, tokens, ns, rs))
|
|
|
|
smartlist_add(ns->routerstatus_list, rs);
|
|
|
|
else {
|
|
|
|
tor_free(rs->version);
|
|
|
|
tor_free(rs);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
routerstatus_t *rs;
|
|
|
|
if ((rs =routerstatus_parse_entry_from_string(&s, tokens, NULL, NULL)))
|
|
|
|
smartlist_add(ns->routerstatus_list, rs);
|
2007-06-03 05:05:10 +02:00
|
|
|
}
|
|
|
|
}
|
2007-06-04 21:19:01 +02:00
|
|
|
for (i = 1; i < smartlist_len(ns->routerstatus_list); ++i) {
|
2007-06-05 00:29:00 +02:00
|
|
|
routerstatus_t *rs1, *rs2;
|
|
|
|
if (is_vote) {
|
|
|
|
vote_routerstatus_t *a = smartlist_get(ns->routerstatus_list, i-1);
|
|
|
|
vote_routerstatus_t *b = smartlist_get(ns->routerstatus_list, i);
|
|
|
|
rs1 = &a->status; rs2 = &b->status;
|
|
|
|
} else {
|
|
|
|
rs1 = smartlist_get(ns->routerstatus_list, i-1);
|
|
|
|
rs2 = smartlist_get(ns->routerstatus_list, i);
|
|
|
|
}
|
|
|
|
if (memcmp(rs1->identity_digest, rs2->identity_digest, DIGEST_LEN) >= 0) {
|
2007-06-04 21:19:01 +02:00
|
|
|
log_warn(LD_DIR, "Vote networkstatus entries not sorted by identity "
|
|
|
|
"digest");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2007-06-03 05:05:10 +02:00
|
|
|
|
|
|
|
/* Parse footer; check signature. */
|
|
|
|
footer_tokens = smartlist_create();
|
|
|
|
end_of_footer = s + strlen(s);
|
|
|
|
if (tokenize_string(s, end_of_footer, footer_tokens,
|
|
|
|
networkstatus_vote_footer_token_table)) {
|
|
|
|
log_warn(LD_DIR, "Error tokenizing network-status vote footer.");
|
|
|
|
goto err;
|
|
|
|
}
|
2007-06-05 00:29:00 +02:00
|
|
|
|
|
|
|
SMARTLIST_FOREACH(footer_tokens, directory_token_t *, _tok,
|
|
|
|
{
|
|
|
|
char declared_identity[DIGEST_LEN];
|
|
|
|
networkstatus_voter_info_t *v;
|
|
|
|
tok = _tok;
|
|
|
|
if (tok->tp != K_DIRECTORY_SIGNATURE)
|
|
|
|
continue;
|
|
|
|
tor_assert(tok->n_args >= 2);
|
|
|
|
|
|
|
|
if (!tok->object_type ||
|
|
|
|
strcmp(tok->object_type, "SIGNATURE") ||
|
|
|
|
tok->object_size < 128 || tok->object_size > 512) {
|
|
|
|
log_warn(LD_DIR, "Bad object type or length on directory-signature");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(tok->args[0]) != HEX_DIGEST_LEN ||
|
|
|
|
base16_decode(declared_identity, sizeof(declared_identity),
|
|
|
|
tok->args[0], HEX_DIGEST_LEN) < 0) {
|
|
|
|
log_warn(LD_DIR, "Error decoding declared identity %s in "
|
|
|
|
"network-status vote.", escaped(tok->args[0]));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!(v = networkstatus_get_voter_by_id(ns, declared_identity))) {
|
|
|
|
log_warn(LD_DIR, "ID on signature on network-status vote does not match"
|
|
|
|
"any declared directory source.");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (strlen(tok->args[1]) != HEX_DIGEST_LEN ||
|
|
|
|
base16_decode(v->signing_key_digest, sizeof(v->signing_key_digest),
|
|
|
|
tok->args[1], HEX_DIGEST_LEN) < 0) {
|
|
|
|
log_warn(LD_DIR, "Error decoding declared digest %s in "
|
|
|
|
"network-status vote.", escaped(tok->args[1]));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_vote &&
|
|
|
|
memcmp(declared_identity, ns->cert->cache_info.identity_digest,
|
|
|
|
DIGEST_LEN)) {
|
|
|
|
log_warn(LD_DIR, "Digest mismatch between declared and actual on "
|
|
|
|
"network-status vote.");
|
|
|
|
goto err;
|
|
|
|
/* XXXX020 also check cert against dir-source line. */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_vote) {
|
|
|
|
if (check_signature_token(ns_digest, tok, ns->cert->signing_key, 0,
|
|
|
|
"network-status vote"))
|
|
|
|
goto err;
|
2007-06-05 00:57:23 +02:00
|
|
|
v->good_signature = 1;
|
2007-06-05 00:29:00 +02:00
|
|
|
} else {
|
|
|
|
v->pending_signature = tor_memdup(tok->object_body,
|
|
|
|
tok->object_size);
|
|
|
|
v->pending_signature_len = tok->object_size;
|
|
|
|
}
|
|
|
|
});
|
2007-06-13 20:16:05 +02:00
|
|
|
/* XXXX020 enforce: vote must have at least one signature. */
|
2007-06-03 05:05:10 +02:00
|
|
|
|
|
|
|
/* XXXX020 check dates for plausibility. ??? */
|
|
|
|
|
|
|
|
goto done;
|
|
|
|
err:
|
|
|
|
if (ns)
|
|
|
|
networkstatus_vote_free(ns);
|
|
|
|
ns = NULL;
|
|
|
|
done:
|
|
|
|
if (tokens) {
|
|
|
|
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t));
|
|
|
|
smartlist_free(tokens);
|
|
|
|
}
|
2007-06-05 00:29:00 +02:00
|
|
|
if (voter) {
|
|
|
|
tor_free(voter->nickname);
|
|
|
|
tor_free(voter->address);
|
|
|
|
tor_free(voter->contact);
|
|
|
|
tor_free(voter);
|
|
|
|
}
|
2007-06-03 05:05:10 +02:00
|
|
|
if (rs_tokens) {
|
|
|
|
SMARTLIST_FOREACH(rs_tokens, directory_token_t *, t, token_free(t));
|
|
|
|
smartlist_free(rs_tokens);
|
|
|
|
}
|
|
|
|
if (footer_tokens) {
|
|
|
|
SMARTLIST_FOREACH(footer_tokens, directory_token_t *, t, token_free(t));
|
|
|
|
smartlist_free(footer_tokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
|
2006-03-27 04:25:34 +02:00
|
|
|
/** Parse the addr policy in the string <b>s</b> and return it. If
|
2005-08-08 23:58:48 +02:00
|
|
|
* assume_action is nonnegative, then insert its action (ADDR_POLICY_ACCEPT or
|
|
|
|
* ADDR_POLICY_REJECT) for items that specify no action.
|
2004-05-10 19:30:51 +02:00
|
|
|
*/
|
2004-12-04 02:14:36 +01:00
|
|
|
addr_policy_t *
|
2005-08-08 23:58:48 +02:00
|
|
|
router_parse_addr_policy_from_string(const char *s, int assume_action)
|
2004-05-10 19:30:51 +02:00
|
|
|
{
|
|
|
|
directory_token_t *tok = NULL;
|
|
|
|
const char *cp;
|
2007-07-19 21:40:45 +02:00
|
|
|
char *tmp = NULL;
|
2004-12-04 02:14:36 +01:00
|
|
|
addr_policy_t *r;
|
2004-10-14 04:49:48 +02:00
|
|
|
size_t len, idx;
|
2007-07-10 22:08:18 +02:00
|
|
|
const char *eos;
|
2004-05-10 19:30:51 +02:00
|
|
|
|
|
|
|
/* *s might not end with \n, so we need to extend it with one. */
|
|
|
|
len = strlen(s);
|
|
|
|
cp = tmp = tor_malloc(len+2);
|
|
|
|
for (idx = 0; idx < len; ++idx) {
|
2006-08-31 19:39:51 +02:00
|
|
|
tmp[idx] = TOR_TOLOWER(s[idx]);
|
2004-05-10 19:30:51 +02:00
|
|
|
}
|
|
|
|
tmp[len]='\n';
|
|
|
|
tmp[len+1]='\0';
|
2005-08-08 23:58:48 +02:00
|
|
|
while (TOR_ISSPACE(*cp))
|
|
|
|
++cp;
|
|
|
|
if ((*cp == '*' || TOR_ISDIGIT(*cp)) && assume_action >= 0) {
|
|
|
|
char *new_str = tor_malloc(len+10);
|
|
|
|
tor_snprintf(new_str, len+10, "%s %s\n",
|
|
|
|
assume_action == ADDR_POLICY_ACCEPT?"accept":"reject", cp);
|
|
|
|
tor_free(tmp);
|
|
|
|
cp = tmp = new_str;
|
|
|
|
}
|
2007-07-11 00:44:32 +02:00
|
|
|
eos = cp + strlen(cp);
|
2007-07-10 22:08:18 +02:00
|
|
|
tok = get_next_token(&cp, eos, routerdesc_token_table);
|
2004-05-10 19:30:51 +02:00
|
|
|
if (tok->tp == _ERR) {
|
2006-03-27 04:25:34 +02:00
|
|
|
log_warn(LD_DIR, "Error reading address policy: %s", tok->error);
|
2004-05-10 19:30:51 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (tok->tp != K_ACCEPT && tok->tp != K_REJECT) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Expected 'accept' or 'reject'.");
|
2004-05-10 19:30:51 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2006-03-27 04:25:34 +02:00
|
|
|
/* Now that we've gotten an addr policy, add it to the router. */
|
2004-11-12 20:39:13 +01:00
|
|
|
r = router_parse_addr_policy(tok);
|
2004-05-10 19:30:51 +02:00
|
|
|
goto done;
|
|
|
|
err:
|
2004-05-20 04:42:50 +02:00
|
|
|
r = NULL;
|
2004-05-10 19:30:51 +02:00
|
|
|
done:
|
2004-09-29 08:52:36 +02:00
|
|
|
tor_free(tmp);
|
2004-05-10 19:30:51 +02:00
|
|
|
token_free(tok);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2005-09-13 23:14:55 +02:00
|
|
|
/** Add an exit policy stored in the token <b>tok</b> to the router info in
|
|
|
|
* <b>router</b>. Return 0 on success, -1 on failure. */
|
2004-11-12 20:39:13 +01:00
|
|
|
static int
|
2005-09-13 23:14:55 +02:00
|
|
|
router_add_exit_policy(routerinfo_t *router, directory_token_t *tok)
|
2004-05-20 04:42:50 +02:00
|
|
|
{
|
2004-12-04 02:14:36 +01:00
|
|
|
addr_policy_t *newe, **tmpe;
|
2004-11-12 20:39:13 +01:00
|
|
|
newe = router_parse_addr_policy(tok);
|
2004-05-20 04:42:50 +02:00
|
|
|
if (!newe)
|
|
|
|
return -1;
|
|
|
|
for (tmpe = &router->exit_policy; *tmpe; tmpe=&((*tmpe)->next))
|
|
|
|
;
|
|
|
|
*tmpe = newe;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-08-18 23:56:45 +02:00
|
|
|
/** Given a K_ACCEPT or K_REJECT token and a router, create and return
|
|
|
|
* a new exit_policy_t corresponding to the token. */
|
2004-12-04 02:14:36 +01:00
|
|
|
static addr_policy_t *
|
2005-06-11 20:52:12 +02:00
|
|
|
router_parse_addr_policy(directory_token_t *tok)
|
|
|
|
{
|
2004-12-04 02:14:36 +01:00
|
|
|
addr_policy_t *newe;
|
2005-02-22 09:18:36 +01:00
|
|
|
char *arg;
|
2006-12-29 03:49:12 +01:00
|
|
|
char buf[POLICY_BUF_LEN];
|
2004-05-10 19:30:51 +02:00
|
|
|
|
|
|
|
tor_assert(tok->tp == K_REJECT || tok->tp == K_ACCEPT);
|
|
|
|
|
|
|
|
if (tok->n_args != 1)
|
2004-05-20 04:42:50 +02:00
|
|
|
return NULL;
|
2004-05-10 19:30:51 +02:00
|
|
|
arg = tok->args[0];
|
|
|
|
|
2006-03-05 06:27:59 +01:00
|
|
|
if (!strcmpstart(arg,"private"))
|
2006-12-29 21:50:24 +01:00
|
|
|
return router_parse_addr_policy_private(tok);
|
2006-03-05 06:27:59 +01:00
|
|
|
|
2004-12-04 02:14:36 +01:00
|
|
|
newe = tor_malloc_zero(sizeof(addr_policy_t));
|
2004-05-10 19:30:51 +02:00
|
|
|
|
2004-11-12 20:39:13 +01:00
|
|
|
newe->policy_type = (tok->tp == K_REJECT) ? ADDR_POLICY_REJECT
|
|
|
|
: ADDR_POLICY_ACCEPT;
|
2004-05-10 19:30:51 +02:00
|
|
|
|
2007-07-19 21:40:45 +02:00
|
|
|
if (parse_addr_and_port_range(arg, &newe->addr, &newe->maskbits,
|
2004-10-16 23:42:09 +02:00
|
|
|
&newe->prt_min, &newe->prt_max))
|
2004-05-10 19:30:51 +02:00
|
|
|
goto policy_read_failed;
|
|
|
|
|
2006-12-29 03:49:12 +01:00
|
|
|
if (policy_write_item(buf, sizeof(buf), newe) < 0)
|
|
|
|
goto policy_read_failed;
|
2004-05-10 19:30:51 +02:00
|
|
|
|
2006-12-29 03:49:12 +01:00
|
|
|
newe->string = tor_strdup(buf);
|
2004-05-20 04:42:50 +02:00
|
|
|
return newe;
|
2004-05-10 19:30:51 +02:00
|
|
|
|
|
|
|
policy_read_failed:
|
2006-12-29 03:49:12 +01:00
|
|
|
log_warn(LD_DIR,"Couldn't parse line %s. Dropping", escaped(arg));
|
2004-09-29 08:52:36 +02:00
|
|
|
tor_free(newe);
|
2004-05-20 04:42:50 +02:00
|
|
|
return NULL;
|
2004-05-10 19:30:51 +02:00
|
|
|
}
|
|
|
|
|
2006-03-05 06:27:59 +01:00
|
|
|
/** Parse an exit policy line of the format "accept/reject private:...".
|
|
|
|
* This didn't exist until Tor 0.1.1.15, so nobody should generate it in
|
|
|
|
* router descriptors until earlier versions are obsolete.
|
|
|
|
*/
|
|
|
|
static addr_policy_t *
|
2006-12-29 21:50:24 +01:00
|
|
|
router_parse_addr_policy_private(directory_token_t *tok)
|
2006-03-05 06:27:59 +01:00
|
|
|
{
|
|
|
|
static const char *private_nets[] = {
|
|
|
|
"0.0.0.0/8", "169.254.0.0/16",
|
|
|
|
"127.0.0.0/8", "192.168.0.0/16", "10.0.0.0/8", "172.16.0.0/12",NULL };
|
|
|
|
char *arg;
|
|
|
|
addr_policy_t *result, **nextp;
|
|
|
|
int net;
|
|
|
|
uint16_t port_min, port_max;
|
|
|
|
|
|
|
|
arg = tok->args[0];
|
|
|
|
if (strcmpstart(arg, "private"))
|
|
|
|
return NULL;
|
|
|
|
arg += strlen("private");
|
|
|
|
arg = (char*) eat_whitespace(arg);
|
|
|
|
if (!arg || *arg != ':')
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (parse_port_range(arg+1, &port_min, &port_max)<0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
nextp = &result;
|
|
|
|
for (net = 0; private_nets[net]; ++net) {
|
|
|
|
size_t len;
|
|
|
|
*nextp = tor_malloc_zero(sizeof(addr_policy_t));
|
|
|
|
(*nextp)->policy_type = (tok->tp == K_REJECT) ? ADDR_POLICY_REJECT
|
|
|
|
: ADDR_POLICY_ACCEPT;
|
|
|
|
len = strlen(arg)+strlen(private_nets[net])+16;
|
|
|
|
(*nextp)->string = tor_malloc(len+1);
|
|
|
|
tor_snprintf((*nextp)->string, len, "%s %s%s",
|
|
|
|
tok->tp == K_REJECT ? "reject" : "accept",
|
|
|
|
private_nets[net], arg);
|
|
|
|
if (parse_addr_and_port_range((*nextp)->string + 7,
|
2007-07-19 21:40:45 +02:00
|
|
|
&(*nextp)->addr, &(*nextp)->maskbits,
|
2006-03-05 06:27:59 +01:00
|
|
|
&(*nextp)->prt_min, &(*nextp)->prt_max)) {
|
|
|
|
log_warn(LD_BUG, "Couldn't parse an address range we generated!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
nextp = &(*nextp)->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-09-13 23:14:55 +02:00
|
|
|
/** Log and exit if <b>t</b> is malformed */
|
2004-12-04 02:14:36 +01:00
|
|
|
void
|
|
|
|
assert_addr_policy_ok(addr_policy_t *t)
|
|
|
|
{
|
|
|
|
addr_policy_t *t2;
|
|
|
|
while (t) {
|
|
|
|
tor_assert(t->policy_type == ADDR_POLICY_REJECT ||
|
|
|
|
t->policy_type == ADDR_POLICY_ACCEPT);
|
|
|
|
tor_assert(t->prt_min <= t->prt_max);
|
2005-08-08 23:58:48 +02:00
|
|
|
t2 = router_parse_addr_policy_from_string(t->string, -1);
|
2004-12-04 02:14:36 +01:00
|
|
|
tor_assert(t2);
|
|
|
|
tor_assert(t2->policy_type == t->policy_type);
|
|
|
|
tor_assert(t2->addr == t->addr);
|
2007-07-19 21:40:45 +02:00
|
|
|
tor_assert(t2->maskbits == t->maskbits);
|
2004-12-04 02:14:36 +01:00
|
|
|
tor_assert(t2->prt_min == t->prt_min);
|
|
|
|
tor_assert(t2->prt_max == t->prt_max);
|
|
|
|
tor_assert(!strcmp(t2->string, t->string));
|
|
|
|
tor_assert(t2->next == NULL);
|
|
|
|
addr_policy_free(t2);
|
|
|
|
|
|
|
|
t = t->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-05-10 19:30:51 +02:00
|
|
|
/*
|
|
|
|
* Low-level tokenizer for router descriptors and directories.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Free all resources allocated for <b>tok</b> */
|
|
|
|
static void
|
|
|
|
token_free(directory_token_t *tok)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
tor_assert(tok);
|
|
|
|
if (tok->args) {
|
|
|
|
for (i = 0; i < tok->n_args; ++i) {
|
|
|
|
tor_free(tok->args[i]);
|
|
|
|
}
|
|
|
|
tor_free(tok->args);
|
|
|
|
}
|
|
|
|
tor_free(tok->object_type);
|
|
|
|
tor_free(tok->object_body);
|
2007-04-16 18:28:06 +02:00
|
|
|
tor_free(tok->error);
|
2004-05-10 19:30:51 +02:00
|
|
|
if (tok->key)
|
|
|
|
crypto_free_pk_env(tok->key);
|
|
|
|
tor_free(tok);
|
|
|
|
}
|
|
|
|
|
2007-05-15 00:51:05 +02:00
|
|
|
#define RET_ERR(msg) \
|
2007-06-17 20:22:39 +02:00
|
|
|
STMT_BEGIN \
|
2007-05-15 00:51:05 +02:00
|
|
|
if (tok) token_free(tok); \
|
|
|
|
tok = tor_malloc_zero(sizeof(directory_token_t)); \
|
|
|
|
tok->tp = _ERR; \
|
|
|
|
tok->error = tor_strdup(msg); \
|
2007-06-17 20:22:39 +02:00
|
|
|
goto done_tokenizing; \
|
|
|
|
STMT_END
|
2007-05-15 00:51:05 +02:00
|
|
|
|
|
|
|
static INLINE directory_token_t *
|
|
|
|
token_check_object(const char *kwd,
|
|
|
|
directory_token_t *tok, obj_syntax o_syn)
|
|
|
|
{
|
|
|
|
char ebuf[128];
|
|
|
|
switch (o_syn) {
|
|
|
|
case NO_OBJ:
|
|
|
|
if (tok->object_body) {
|
|
|
|
tor_snprintf(ebuf, sizeof(ebuf), "Unexpected object for %s", kwd);
|
|
|
|
RET_ERR(ebuf);
|
|
|
|
}
|
|
|
|
if (tok->key) {
|
|
|
|
tor_snprintf(ebuf, sizeof(ebuf), "Unexpected public key for %s", kwd);
|
|
|
|
RET_ERR(ebuf);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NEED_OBJ:
|
|
|
|
if (!tok->object_body) {
|
|
|
|
tor_snprintf(ebuf, sizeof(ebuf), "Missing object for %s", kwd);
|
|
|
|
RET_ERR(ebuf);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NEED_KEY_1024:
|
|
|
|
if (tok->key && crypto_pk_keysize(tok->key) != PK_BYTES) {
|
|
|
|
tor_snprintf(ebuf, sizeof(ebuf), "Wrong size on key for %s: %d bits",
|
|
|
|
kwd, (int)crypto_pk_keysize(tok->key));
|
|
|
|
RET_ERR(ebuf);
|
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
case NEED_KEY:
|
|
|
|
if (!tok->key) {
|
|
|
|
tor_snprintf(ebuf, sizeof(ebuf), "Missing public key for %s", kwd);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OBJ_OK:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
done_tokenizing:
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Helper function: read the next token from *s, advance *s to the end of the
|
|
|
|
* token, and return the parsed token. Parse *<b>s</b> according to the list
|
|
|
|
* of tokens in <b>table</b>.
|
2004-05-10 19:30:51 +02:00
|
|
|
*/
|
|
|
|
static directory_token_t *
|
2007-07-10 22:08:18 +02:00
|
|
|
get_next_token(const char **s, const char *eos, token_rule_t *table)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2007-07-16 20:26:31 +02:00
|
|
|
const char *next, *eol, *obstart;
|
|
|
|
int i, j, allocated, obname_len;
|
2004-05-10 19:30:51 +02:00
|
|
|
directory_token_t *tok;
|
|
|
|
obj_syntax o_syn = NO_OBJ;
|
2007-04-16 18:28:06 +02:00
|
|
|
char ebuf[128];
|
|
|
|
const char *kwd = "";
|
2004-05-10 19:30:51 +02:00
|
|
|
|
2007-04-16 18:28:06 +02:00
|
|
|
#define RET_ERR(msg) \
|
2007-06-17 20:22:39 +02:00
|
|
|
STMT_BEGIN \
|
2007-04-16 18:28:06 +02:00
|
|
|
if (tok) token_free(tok); \
|
|
|
|
tok = tor_malloc_zero(sizeof(directory_token_t)); \
|
|
|
|
tok->tp = _ERR; \
|
|
|
|
tok->error = tor_strdup(msg); \
|
2007-06-17 20:22:39 +02:00
|
|
|
goto done_tokenizing; \
|
|
|
|
STMT_END
|
2004-05-10 19:30:51 +02:00
|
|
|
|
|
|
|
tok = tor_malloc_zero(sizeof(directory_token_t));
|
|
|
|
tok->tp = _ERR;
|
|
|
|
|
2007-07-16 20:26:31 +02:00
|
|
|
/* Set *s to first token, eol to end-of-line, next to after first token */
|
|
|
|
*s = eat_whitespace_eos(*s, eos); /* eat multi-line whitespace */
|
|
|
|
eol = memchr(*s, '\n', eos-*s);
|
|
|
|
if (!eol)
|
|
|
|
eol = eos;
|
|
|
|
next = find_whitespace_eos(*s, eol);
|
|
|
|
|
|
|
|
if (!strcmp_len(*s, "opt", next-*s)) {
|
2007-05-15 00:51:05 +02:00
|
|
|
/* Skip past an "opt" at the start of the line. */
|
2007-07-16 20:26:31 +02:00
|
|
|
*s = eat_whitespace_eos_no_nl(next, eol);
|
|
|
|
next = find_whitespace_eos(*s, eol);
|
|
|
|
} else if (*s == eos) { /* If no "opt", and end-of-line, line is invalid */
|
|
|
|
RET_ERR("Unexpected EOF");
|
2004-06-21 06:37:27 +02:00
|
|
|
}
|
2007-07-16 20:26:31 +02:00
|
|
|
|
2007-05-15 00:51:05 +02:00
|
|
|
/* Search the table for the appropriate entry. (I tried a binary search
|
|
|
|
* instead, but it wasn't any faster.) */
|
2007-04-16 06:18:21 +02:00
|
|
|
for (i = 0; table[i].t ; ++i) {
|
2007-07-16 20:26:31 +02:00
|
|
|
if (!strcmp_len(*s, table[i].t, next-*s)) {
|
2004-05-10 19:30:51 +02:00
|
|
|
/* We've found the keyword. */
|
2007-04-16 18:28:06 +02:00
|
|
|
kwd = table[i].t;
|
2007-04-16 06:18:21 +02:00
|
|
|
tok->tp = table[i].v;
|
|
|
|
o_syn = table[i].os;
|
2007-07-16 20:26:31 +02:00
|
|
|
*s = eat_whitespace_eos_no_nl(next, eol);
|
|
|
|
next = find_whitespace_eos(*s, eol);
|
|
|
|
if (1 || *s < eol) { /* make sure there are arguments to store */
|
|
|
|
/* XXXX020 actually, we go ahead whether there are arguments or not,
|
|
|
|
* so that tok->args is always set if we want arguments. */
|
|
|
|
if (table[i].concat_args) {
|
|
|
|
/* The keyword takes the line as a single argument */
|
|
|
|
tok->args = tor_malloc(sizeof(char*));
|
|
|
|
tok->args[0] = tor_strndup(*s,eol-*s); /* Grab everything on line */
|
|
|
|
tok->n_args = 1;
|
|
|
|
} else {
|
|
|
|
/* This keyword takes multiple arguments. */
|
|
|
|
j = 0;
|
|
|
|
allocated = 16;
|
|
|
|
tok->args = tor_malloc(sizeof(char*)*allocated);
|
|
|
|
while (*s < eol) { /* While not at eol, store the next token */
|
|
|
|
if (j == allocated) {
|
|
|
|
allocated *= 2;
|
|
|
|
tok->args = tor_realloc(tok->args,sizeof(char*)*allocated);
|
|
|
|
}
|
|
|
|
tok->args[j++] = tor_strndup(*s, next-*s);
|
|
|
|
*s = eat_whitespace_eos_no_nl(next, eol); /* eat intra-line ws */
|
|
|
|
next = find_whitespace_eos(*s, eol); /* find end of token at *s */
|
2004-05-10 19:30:51 +02:00
|
|
|
}
|
2007-07-16 20:26:31 +02:00
|
|
|
tok->n_args = j;
|
2004-05-10 19:30:51 +02:00
|
|
|
}
|
|
|
|
}
|
2007-04-16 18:28:06 +02:00
|
|
|
if (tok->n_args < table[i].min_args) {
|
|
|
|
tor_snprintf(ebuf, sizeof(ebuf), "Too few arguments to %s", kwd);
|
|
|
|
RET_ERR(ebuf);
|
|
|
|
} else if (tok->n_args > table[i].max_args) {
|
|
|
|
tor_snprintf(ebuf, sizeof(ebuf), "Too many arguments to %s", kwd);
|
|
|
|
RET_ERR(ebuf);
|
|
|
|
}
|
2004-05-10 19:30:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-07-16 20:26:31 +02:00
|
|
|
|
|
|
|
if (tok->tp == _ERR) { /* No keyword matched; call it an "opt". */
|
2006-12-24 05:09:42 +01:00
|
|
|
tok->tp = K_OPT;
|
|
|
|
tok->args = tor_malloc(sizeof(char*));
|
2007-07-16 20:26:31 +02:00
|
|
|
tok->args[0] = tor_strndup(*s, eol-*s);
|
2006-12-24 05:09:42 +01:00
|
|
|
tok->n_args = 1;
|
|
|
|
o_syn = OBJ_OK;
|
2004-05-10 19:30:51 +02:00
|
|
|
}
|
2007-07-16 20:26:31 +02:00
|
|
|
|
|
|
|
/* Check whether there's an object present */
|
|
|
|
*s = eat_whitespace_eos(eol, eos); /* Scan from end of first line */
|
|
|
|
eol = memchr(*s, '\n', eos-*s);
|
|
|
|
if (!eol || eol-*s<11 || strcmpstart(*s, "-----BEGIN ")) /* No object. */
|
2007-05-15 00:51:05 +02:00
|
|
|
goto check_object;
|
2007-07-16 20:26:31 +02:00
|
|
|
|
|
|
|
obstart = *s; /* Set obstart to start of object spec */
|
|
|
|
if (*s+11 >= eol-5 || memchr(*s+11,'\0',eol-*s-16) || /* no short lines, */
|
|
|
|
strcmp_len(eol-5, "-----", 5)) { /* nuls or invalid endings */
|
2004-05-10 19:30:51 +02:00
|
|
|
RET_ERR("Malformed object: bad begin line");
|
|
|
|
}
|
2007-07-16 20:26:31 +02:00
|
|
|
tok->object_type = tor_strndup(*s+11, eol-*s-16);
|
|
|
|
obname_len = eol-*s-16; /* store objname length here to avoid a strlen() */
|
|
|
|
*s = eol+1; /* Set *s to possible start of object data (could be eos) */
|
|
|
|
|
|
|
|
/* Go to the end of the object */
|
2007-07-10 22:08:18 +02:00
|
|
|
next = tor_memstr(*s, eos-*s, "-----END ");
|
2004-05-10 19:30:51 +02:00
|
|
|
if (!next) {
|
2007-07-16 20:26:31 +02:00
|
|
|
RET_ERR("Malformed object: missing object end line");
|
|
|
|
}
|
|
|
|
eol = memchr(next, '\n', eos-next);
|
|
|
|
if (!eol) /* end-of-line marker, or eos if there's no '\n' */
|
|
|
|
eol = eos;
|
|
|
|
/* Validate the ending tag, which should be 9 + NAME + 5 + eol */
|
|
|
|
if (eol-next != 9+obname_len+5 ||
|
|
|
|
strcmp_len(next+9, tok->object_type, obname_len) ||
|
|
|
|
strcmp_len(eol-5, "-----", 5)) {
|
|
|
|
snprintf(ebuf, sizeof(ebuf), "Malformed object: mismatched end tag %s",
|
|
|
|
tok->object_type);
|
|
|
|
ebuf[sizeof(ebuf)-1] = '\0';
|
|
|
|
RET_ERR(ebuf);
|
|
|
|
}
|
|
|
|
if (!strcmp(tok->object_type, "RSA PUBLIC KEY")) { /* If it's a key... */
|
2004-05-10 19:30:51 +02:00
|
|
|
tok->key = crypto_new_pk_env();
|
2007-07-16 20:26:31 +02:00
|
|
|
if (crypto_pk_read_public_key_from_string(tok->key, obstart, eol-obstart))
|
2004-05-10 19:30:51 +02:00
|
|
|
RET_ERR("Couldn't parse public key.");
|
2007-07-16 20:26:31 +02:00
|
|
|
} else { /* If it's something else, try to base64-decode it */
|
|
|
|
int r;
|
2004-05-10 19:30:51 +02:00
|
|
|
tok->object_body = tor_malloc(next-*s); /* really, this is too much RAM. */
|
2007-07-16 20:26:31 +02:00
|
|
|
r = base64_decode(tok->object_body, next-*s, *s, next-*s);
|
|
|
|
if (r<0)
|
2004-05-10 19:30:51 +02:00
|
|
|
RET_ERR("Malformed object: bad base64-encoded data");
|
2007-07-16 20:26:31 +02:00
|
|
|
tok->object_size = r;
|
2004-05-10 19:30:51 +02:00
|
|
|
}
|
2007-07-16 20:26:31 +02:00
|
|
|
*s = eol;
|
2004-05-10 19:30:51 +02:00
|
|
|
|
2007-05-15 00:51:05 +02:00
|
|
|
check_object:
|
|
|
|
tok = token_check_object(kwd, tok, o_syn);
|
2004-05-10 19:30:51 +02:00
|
|
|
|
2007-05-15 00:51:05 +02:00
|
|
|
done_tokenizing:
|
2004-05-10 19:30:51 +02:00
|
|
|
return tok;
|
2007-05-15 00:51:05 +02:00
|
|
|
|
2004-05-10 19:30:51 +02:00
|
|
|
#undef RET_ERR
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Read all tokens from a string between <b>start</b> and <b>end</b>, and add
|
2007-05-15 00:51:05 +02:00
|
|
|
* them to <b>out</b>. Parse according to the token rules in <b>table</b>.
|
2004-05-10 19:30:51 +02:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
tokenize_string(const char *start, const char *end, smartlist_t *out,
|
2007-04-16 06:18:21 +02:00
|
|
|
token_rule_t *table)
|
2004-05-10 19:30:51 +02:00
|
|
|
{
|
|
|
|
const char **s;
|
|
|
|
directory_token_t *tok = NULL;
|
2007-04-16 06:18:21 +02:00
|
|
|
int counts[_NIL];
|
|
|
|
int i;
|
|
|
|
|
2004-05-10 19:30:51 +02:00
|
|
|
s = &start;
|
2005-09-08 21:10:36 +02:00
|
|
|
if (!end)
|
|
|
|
end = start+strlen(start);
|
2007-04-16 06:18:21 +02:00
|
|
|
for (i = 0; i < _NIL; ++i)
|
|
|
|
counts[i] = 0;
|
2004-05-10 19:30:51 +02:00
|
|
|
while (*s < end && (!tok || tok->tp != _EOF)) {
|
2007-07-10 22:08:18 +02:00
|
|
|
tok = get_next_token(s, end, table);
|
2004-05-10 19:30:51 +02:00
|
|
|
if (tok->tp == _ERR) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "parse error: %s", tok->error);
|
2004-05-10 19:30:51 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2007-04-16 06:18:21 +02:00
|
|
|
++counts[tok->tp];
|
2004-05-10 19:30:51 +02:00
|
|
|
smartlist_add(out, tok);
|
2007-07-10 22:08:18 +02:00
|
|
|
*s = eat_whitespace_eos(*s, end);
|
2004-05-10 19:30:51 +02:00
|
|
|
}
|
|
|
|
|
2007-04-16 06:18:21 +02:00
|
|
|
for (i = 0; table[i].t; ++i) {
|
|
|
|
if (counts[table[i].v] < table[i].min_cnt) {
|
|
|
|
log_warn(LD_DIR, "Parse error: missing %s element.", table[i].t);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (counts[table[i].v] > table[i].max_cnt) {
|
|
|
|
log_warn(LD_DIR, "Parse error: too many %s elements.", table[i].t);
|
|
|
|
return -1;
|
|
|
|
}
|
2007-05-15 00:51:05 +02:00
|
|
|
if (table[i].pos & AT_START) {
|
|
|
|
if (smartlist_len(out) < 1 ||
|
|
|
|
(tok = smartlist_get(out, 0))->tp != table[i].v) {
|
|
|
|
log_warn(LD_DIR, "Parse error: first item is not %s.", table[i].t);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (table[i].pos & AT_END) {
|
|
|
|
if (smartlist_len(out) < 1 ||
|
|
|
|
(tok = smartlist_get(out, smartlist_len(out)-1))->tp != table[i].v) {
|
|
|
|
log_warn(LD_DIR, "Parse error: last item is not %s.", table[i].t);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2007-04-16 06:18:21 +02:00
|
|
|
}
|
2004-05-10 19:30:51 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Find the first token in <b>s</b> whose keyword is <b>keyword</b>; return
|
|
|
|
* NULL if no such keyword is found.
|
|
|
|
*/
|
|
|
|
static directory_token_t *
|
|
|
|
find_first_by_keyword(smartlist_t *s, directory_keyword keyword)
|
|
|
|
{
|
|
|
|
SMARTLIST_FOREACH(s, directory_token_t *, t, if (t->tp == keyword) return t);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return a newly allocated smartlist of all accept or reject tokens in
|
|
|
|
* <b>s</b>.
|
|
|
|
*/
|
|
|
|
static smartlist_t *
|
|
|
|
find_all_exitpolicy(smartlist_t *s)
|
|
|
|
{
|
|
|
|
smartlist_t *out = smartlist_create();
|
|
|
|
SMARTLIST_FOREACH(s, directory_token_t *, t,
|
|
|
|
if (t->tp == K_ACCEPT || t->tp == K_REJECT)
|
|
|
|
smartlist_add(out,t));
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2006-02-05 02:57:27 +01:00
|
|
|
/** Compute the SHA-1 digest of the substring of <b>s</b> taken from the first
|
2007-05-22 20:52:32 +02:00
|
|
|
* occurrence of <b>start_str</b> through the first instance of c after the
|
|
|
|
* first subsequent occurrence of <b>end_str</b>; store the 20-byte result in
|
2004-05-10 19:30:51 +02:00
|
|
|
* <b>digest</b>; return 0 on success.
|
|
|
|
*
|
|
|
|
* If no such substring exists, return -1.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static int
|
|
|
|
router_get_hash_impl(const char *s, char *digest,
|
|
|
|
const char *start_str,
|
2007-05-22 20:52:32 +02:00
|
|
|
const char *end_str, char end_c)
|
2004-05-10 19:30:51 +02:00
|
|
|
{
|
|
|
|
char *start, *end;
|
|
|
|
start = strstr(s, start_str);
|
|
|
|
if (!start) {
|
2006-09-30 00:51:47 +02:00
|
|
|
log_warn(LD_DIR,"couldn't find start of hashed material \"%s\"",start_str);
|
2004-05-10 19:30:51 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2004-08-06 21:54:20 +02:00
|
|
|
if (start != s && *(start-1) != '\n') {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR,
|
|
|
|
"first occurrence of \"%s\" is not at the start of a line",
|
|
|
|
start_str);
|
2004-08-06 21:54:20 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2004-05-10 19:30:51 +02:00
|
|
|
end = strstr(start+strlen(start_str), end_str);
|
|
|
|
if (!end) {
|
2006-09-30 00:51:47 +02:00
|
|
|
log_warn(LD_DIR,"couldn't find end of hashed material \"%s\"",end_str);
|
2004-05-10 19:30:51 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2007-05-22 20:52:32 +02:00
|
|
|
end = strchr(end+strlen(end_str), end_c);
|
2004-05-10 19:30:51 +02:00
|
|
|
if (!end) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR,"couldn't find EOL");
|
2004-05-10 19:30:51 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
++end;
|
|
|
|
|
2004-11-02 03:28:51 +01:00
|
|
|
if (crypto_digest(digest, start, end-start)) {
|
2006-09-30 00:33:40 +02:00
|
|
|
log_warn(LD_BUG,"couldn't compute digest");
|
2004-05-10 19:30:51 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-09-27 08:00:43 +02:00
|
|
|
/** Parse the Tor version of the platform string <b>platform</b>,
|
|
|
|
* and compare it to the version in <b>cutoff</b>. Return 1 if
|
|
|
|
* the router is at least as new as the cutoff, else return 0.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
tor_version_as_new_as(const char *platform, const char *cutoff)
|
|
|
|
{
|
2004-09-27 08:00:43 +02:00
|
|
|
tor_version_t cutoff_version, router_version;
|
2007-04-17 02:58:30 +02:00
|
|
|
char *s, *s2, *start;
|
2004-09-27 08:00:43 +02:00
|
|
|
char tmp[128];
|
|
|
|
|
2006-12-13 01:28:56 +01:00
|
|
|
tor_assert(platform);
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (tor_version_parse(cutoff, &cutoff_version)<0) {
|
2007-03-04 21:11:46 +01:00
|
|
|
log_warn(LD_BUG,"cutoff version '%s' unparseable.",cutoff);
|
2004-09-27 08:00:43 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (strcmpstart(platform,"Tor ")) /* nonstandard Tor; be safe and say yes */
|
2004-09-27 08:00:43 +02:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
start = (char *)eat_whitespace(platform+3);
|
|
|
|
if (!*start) return 0;
|
|
|
|
s = (char *)find_whitespace(start); /* also finds '\0', which is fine */
|
2007-04-17 02:58:30 +02:00
|
|
|
s2 = (char*)eat_whitespace(s);
|
|
|
|
if (!strcmpstart(s2, "(r"))
|
|
|
|
s = (char*)find_whitespace(s2);
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if ((size_t)(s-start+1) >= sizeof(tmp)) /* too big, no */
|
2004-09-27 08:00:43 +02:00
|
|
|
return 0;
|
|
|
|
strlcpy(tmp, start, s-start+1);
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (tor_version_parse(tmp, &router_version)<0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_DIR,"Router version '%s' unparseable.",tmp);
|
2004-09-27 08:00:43 +02:00
|
|
|
return 1; /* be safe and say yes */
|
|
|
|
}
|
|
|
|
|
2007-04-17 02:58:30 +02:00
|
|
|
/* Here's why we don't need to do any special handling for svn revisions:
|
|
|
|
* - If neither has an svn revision, we're fine.
|
|
|
|
* - If the router doesn't have an svn revision, we can't assume that it
|
|
|
|
* is "at least" any svn revision, so we need to return 0.
|
|
|
|
* - If the target version doesn't have an svn revision, any svn revision
|
|
|
|
* (or none at all) is good enough, so return 1.
|
|
|
|
* - If both target and router have an svn revision, we compare them.
|
|
|
|
*/
|
|
|
|
|
2004-09-27 08:00:43 +02:00
|
|
|
return tor_version_compare(&router_version, &cutoff_version) >= 0;
|
|
|
|
}
|
|
|
|
|
2004-11-09 19:22:17 +01:00
|
|
|
/** Parse a tor version from <b>s</b>, and store the result in <b>out</b>.
|
|
|
|
* Return 0 on success, -1 on failure. */
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
tor_version_parse(const char *s, tor_version_t *out)
|
2004-07-13 21:16:49 +02:00
|
|
|
{
|
2007-04-17 02:58:30 +02:00
|
|
|
char *eos=NULL;
|
|
|
|
const char *cp=NULL;
|
2004-07-13 21:16:49 +02:00
|
|
|
/* Format is:
|
2006-07-25 06:34:14 +02:00
|
|
|
* "Tor " ? NUM dot NUM dot NUM [ ( pre | rc | dot ) NUM [ - tag ] ]
|
2004-07-13 21:16:49 +02:00
|
|
|
*/
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(s);
|
|
|
|
tor_assert(out);
|
2004-11-30 00:04:26 +01:00
|
|
|
|
2004-07-13 21:16:49 +02:00
|
|
|
memset(out, 0, sizeof(tor_version_t));
|
|
|
|
|
2005-03-01 02:44:57 +01:00
|
|
|
if (!strcasecmpstart(s, "Tor "))
|
2006-01-02 06:11:31 +01:00
|
|
|
s += 4;
|
2005-03-01 02:44:57 +01:00
|
|
|
|
2004-07-13 21:16:49 +02:00
|
|
|
/* Get major. */
|
|
|
|
out->major = strtol(s,&eos,10);
|
|
|
|
if (!eos || eos==s || *eos != '.') return -1;
|
|
|
|
cp = eos+1;
|
|
|
|
|
|
|
|
/* Get minor */
|
|
|
|
out->minor = strtol(cp,&eos,10);
|
|
|
|
if (!eos || eos==cp || *eos != '.') return -1;
|
|
|
|
cp = eos+1;
|
|
|
|
|
|
|
|
/* Get micro */
|
|
|
|
out->micro = strtol(cp,&eos,10);
|
|
|
|
if (!eos || eos==cp) return -1;
|
|
|
|
if (!*eos) {
|
|
|
|
out->status = VER_RELEASE;
|
|
|
|
out->patchlevel = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
cp = eos;
|
|
|
|
|
|
|
|
/* Get status */
|
|
|
|
if (*cp == '.') {
|
|
|
|
out->status = VER_RELEASE;
|
|
|
|
++cp;
|
|
|
|
} else if (0==strncmp(cp, "pre", 3)) {
|
|
|
|
out->status = VER_PRE;
|
|
|
|
cp += 3;
|
|
|
|
} else if (0==strncmp(cp, "rc", 2)) {
|
|
|
|
out->status = VER_RC;
|
|
|
|
cp += 2;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get patchlevel */
|
|
|
|
out->patchlevel = strtol(cp,&eos,10);
|
|
|
|
if (!eos || eos==cp) return -1;
|
|
|
|
cp = eos;
|
|
|
|
|
2006-08-26 08:51:02 +02:00
|
|
|
/* Get status tag. */
|
2004-11-30 00:04:26 +01:00
|
|
|
if (*cp == '-' || *cp == '.')
|
|
|
|
++cp;
|
2007-04-17 02:58:30 +02:00
|
|
|
eos = (char*) find_whitespace(cp);
|
|
|
|
if (eos-cp >= (int)sizeof(out->status_tag))
|
|
|
|
strlcpy(out->status_tag, cp, sizeof(out->status_tag));
|
|
|
|
else {
|
|
|
|
memcpy(out->status_tag, cp, eos-cp);
|
|
|
|
out->status_tag[eos-cp] = 0;
|
|
|
|
}
|
|
|
|
cp = eat_whitespace(eos);
|
|
|
|
|
|
|
|
if (!strcmpstart(cp, "(r")) {
|
|
|
|
cp += 2;
|
|
|
|
out->svn_revision = strtol(cp,&eos,10);
|
|
|
|
}
|
2004-07-13 21:16:49 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Compare two tor versions; Return <0 if a < b; 0 if a ==b, >0 if a >
|
|
|
|
* b. */
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
tor_version_compare(tor_version_t *a, tor_version_t *b)
|
2004-07-13 21:16:49 +02:00
|
|
|
{
|
|
|
|
int i;
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(a);
|
|
|
|
tor_assert(b);
|
2004-07-13 21:16:49 +02:00
|
|
|
if ((i = a->major - b->major))
|
|
|
|
return i;
|
|
|
|
else if ((i = a->minor - b->minor))
|
|
|
|
return i;
|
|
|
|
else if ((i = a->micro - b->micro))
|
|
|
|
return i;
|
|
|
|
else if ((i = a->status - b->status))
|
|
|
|
return i;
|
|
|
|
else if ((i = a->patchlevel - b->patchlevel))
|
|
|
|
return i;
|
2007-04-17 02:58:30 +02:00
|
|
|
else if ((i = strcmp(a->status_tag, b->status_tag)))
|
|
|
|
return i;
|
|
|
|
else
|
|
|
|
return a->svn_revision - b->svn_revision;
|
2004-07-13 21:16:49 +02:00
|
|
|
}
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/** Return true iff versions <b>a</b> and <b>b</b> belong to the same series.
|
|
|
|
*/
|
2005-03-01 02:44:57 +01:00
|
|
|
static int
|
|
|
|
tor_version_same_series(tor_version_t *a, tor_version_t *b)
|
|
|
|
{
|
|
|
|
tor_assert(a);
|
|
|
|
tor_assert(b);
|
|
|
|
return ((a->major == b->major) &&
|
|
|
|
(a->minor == b->minor) &&
|
|
|
|
(a->micro == b->micro));
|
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|
2006-02-06 06:04:27 +01:00
|
|
|
/** Helper: Given pointers to two strings describing tor versions, return -1
|
|
|
|
* if _a precedes _b, 1 if _b preceeds _a, and 0 if they are equivalent.
|
|
|
|
* Used to sort a list of versions. */
|
|
|
|
static int
|
|
|
|
_compare_tor_version_str_ptr(const void **_a, const void **_b)
|
|
|
|
{
|
|
|
|
const char *a = *_a, *b = *_b;
|
|
|
|
int ca, cb;
|
|
|
|
tor_version_t va, vb;
|
|
|
|
ca = tor_version_parse(a, &va);
|
|
|
|
cb = tor_version_parse(b, &vb);
|
|
|
|
/* If they both parse, compare them. */
|
|
|
|
if (!ca && !cb)
|
|
|
|
return tor_version_compare(&va,&vb);
|
|
|
|
/* If one parses, it comes first. */
|
|
|
|
if (!ca && cb)
|
|
|
|
return -1;
|
|
|
|
if (ca && !cb)
|
|
|
|
return 1;
|
|
|
|
/* If neither parses, compare strings. Also, the directory server admin
|
|
|
|
** needs to be smacked upside the head. But Tor is tolerant and gentle. */
|
|
|
|
return strcmp(a,b);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Sort a list of string-representations of versions in ascending order. */
|
|
|
|
void
|
2006-04-03 08:23:24 +02:00
|
|
|
sort_version_list(smartlist_t *versions, int remove_duplicates)
|
2006-02-06 06:04:27 +01:00
|
|
|
{
|
|
|
|
smartlist_sort(versions, _compare_tor_version_str_ptr);
|
2006-09-15 06:27:58 +02:00
|
|
|
|
|
|
|
if (remove_duplicates)
|
|
|
|
smartlist_uniq(versions, _compare_tor_version_str_ptr, NULL);
|
2006-02-06 06:04:27 +01:00
|
|
|
}
|
|
|
|
|