Merge branch 'extract_ext_orport'

This commit is contained in:
Nick Mathewson 2020-02-19 13:55:18 -05:00
commit 65b75ef3d5
8 changed files with 130 additions and 83 deletions

3
changes/ticket33368 Normal file
View File

@ -0,0 +1,3 @@
o Minor features (client-only compilation):
- Disable more code related to the ext_orport protocol when compiling
without support for relay mode. Closes ticket 33368.

View File

@ -75,6 +75,7 @@ tor_cleanup(void)
/* Remove Extended ORPort cookie authentication file */
{
char *cookie_fname = get_ext_or_auth_cookie_file_name();
if (cookie_fname)
tor_remove_file(cookie_fname);
tor_free(cookie_fname);
}

View File

@ -109,10 +109,6 @@ TO_OR_CONN(connection_t *c)
return DOWNCAST(or_connection_t, c);
}
/** Global map between Extended ORPort identifiers and OR
* connections. */
static digestmap_t *orconn_ext_or_id_map = NULL;
/** Clear clear conn->identity_digest and update other data
* structures as appropriate.*/
void
@ -198,71 +194,6 @@ connection_or_set_identity_digest(or_connection_t *conn,
channel_set_identity_digest(chan, rsa_digest, ed_id);
}
/** Remove the Extended ORPort identifier of <b>conn</b> from the
* global identifier list. Also, clear the identifier from the
* connection itself. */
void
connection_or_remove_from_ext_or_id_map(or_connection_t *conn)
{
or_connection_t *tmp;
if (!orconn_ext_or_id_map)
return;
if (!conn->ext_or_conn_id)
return;
tmp = digestmap_remove(orconn_ext_or_id_map, conn->ext_or_conn_id);
if (!tor_digest_is_zero(conn->ext_or_conn_id))
tor_assert(tmp == conn);
memset(conn->ext_or_conn_id, 0, EXT_OR_CONN_ID_LEN);
}
/** Return the connection whose ext_or_id is <b>id</b>. Return NULL if no such
* connection is found. */
or_connection_t *
connection_or_get_by_ext_or_id(const char *id)
{
if (!orconn_ext_or_id_map)
return NULL;
return digestmap_get(orconn_ext_or_id_map, id);
}
/** Deallocate the global Extended ORPort identifier list */
void
connection_or_clear_ext_or_id_map(void)
{
digestmap_free(orconn_ext_or_id_map, NULL);
orconn_ext_or_id_map = NULL;
}
/** Creates an Extended ORPort identifier for <b>conn</b> and deposits
* it into the global list of identifiers. */
void
connection_or_set_ext_or_identifier(or_connection_t *conn)
{
char random_id[EXT_OR_CONN_ID_LEN];
or_connection_t *tmp;
if (!orconn_ext_or_id_map)
orconn_ext_or_id_map = digestmap_new();
/* Remove any previous identifiers: */
if (conn->ext_or_conn_id && !tor_digest_is_zero(conn->ext_or_conn_id))
connection_or_remove_from_ext_or_id_map(conn);
do {
crypto_rand(random_id, sizeof(random_id));
} while (digestmap_get(orconn_ext_or_id_map, random_id));
if (!conn->ext_or_conn_id)
conn->ext_or_conn_id = tor_malloc_zero(EXT_OR_CONN_ID_LEN);
memcpy(conn->ext_or_conn_id, random_id, EXT_OR_CONN_ID_LEN);
tmp = digestmap_set(orconn_ext_or_id_map, random_id, conn);
tor_assert(!tmp);
}
/**************************************************************/
/** Map from a string describing what a non-open OR connection was doing when

View File

@ -24,4 +24,11 @@ struct ext_or_cmd_t {
int fetch_ext_or_command_from_buf(struct buf_t *buf,
struct ext_or_cmd_t **out);
ext_or_cmd_t *ext_or_cmd_new(uint16_t len);
#define ext_or_cmd_free(cmd) \
FREE_AND_NULL(ext_or_cmd_t, ext_or_cmd_free_, (cmd))
void ext_or_cmd_free_(ext_or_cmd_t *cmd);
#endif /* !defined(TOR_PROTO_EXT_OR_H) */

View File

@ -1420,8 +1420,10 @@ create_managed_proxy_environment(const managed_proxy_t *mp)
smartlist_add_asprintf(envs, "TOR_PT_EXTENDED_SERVER_PORT=%s",
ext_or_addrport_tmp);
}
if (cookie_file_loc) {
smartlist_add_asprintf(envs, "TOR_PT_AUTH_COOKIE_FILE=%s",
cookie_file_loc);
}
tor_free(ext_or_addrport_tmp);
tor_free(cookie_file_loc);

View File

@ -652,6 +652,77 @@ connection_ext_or_start_auth(or_connection_t *or_conn)
return 0;
}
/** Global map between Extended ORPort identifiers and OR
* connections. */
static digestmap_t *orconn_ext_or_id_map = NULL;
/** Remove the Extended ORPort identifier of <b>conn</b> from the
* global identifier list. Also, clear the identifier from the
* connection itself. */
void
connection_or_remove_from_ext_or_id_map(or_connection_t *conn)
{
or_connection_t *tmp;
if (!orconn_ext_or_id_map)
return;
if (!conn->ext_or_conn_id)
return;
tmp = digestmap_remove(orconn_ext_or_id_map, conn->ext_or_conn_id);
if (!tor_digest_is_zero(conn->ext_or_conn_id))
tor_assert(tmp == conn);
memset(conn->ext_or_conn_id, 0, EXT_OR_CONN_ID_LEN);
}
#ifdef TOR_UNIT_TESTS
/** Return the connection whose ext_or_id is <b>id</b>. Return NULL if no such
* connection is found. */
or_connection_t *
connection_or_get_by_ext_or_id(const char *id)
{
if (!orconn_ext_or_id_map)
return NULL;
return digestmap_get(orconn_ext_or_id_map, id);
}
#endif
/** Deallocate the global Extended ORPort identifier list */
void
connection_or_clear_ext_or_id_map(void)
{
digestmap_free(orconn_ext_or_id_map, NULL);
orconn_ext_or_id_map = NULL;
}
/** Creates an Extended ORPort identifier for <b>conn</b> and deposits
* it into the global list of identifiers. */
void
connection_or_set_ext_or_identifier(or_connection_t *conn)
{
char random_id[EXT_OR_CONN_ID_LEN];
or_connection_t *tmp;
if (!orconn_ext_or_id_map)
orconn_ext_or_id_map = digestmap_new();
/* Remove any previous identifiers: */
if (conn->ext_or_conn_id && !tor_digest_is_zero(conn->ext_or_conn_id))
connection_or_remove_from_ext_or_id_map(conn);
do {
crypto_rand(random_id, sizeof(random_id));
} while (digestmap_get(orconn_ext_or_id_map, random_id));
if (!conn->ext_or_conn_id)
conn->ext_or_conn_id = tor_malloc_zero(EXT_OR_CONN_ID_LEN);
memcpy(conn->ext_or_conn_id, random_id, EXT_OR_CONN_ID_LEN);
tmp = digestmap_set(orconn_ext_or_id_map, random_id, conn);
tor_assert(!tmp);
}
/** Free any leftover allocated memory of the ext_orport.c subsystem. */
void
ext_orport_free_all(void)

View File

@ -31,26 +31,56 @@
#define EXT_OR_CONN_STATE_FLUSHING 5
#define EXT_OR_CONN_STATE_MAX_ 5
#ifdef HAVE_MODULE_RELAY
int connection_ext_or_start_auth(or_connection_t *or_conn);
ext_or_cmd_t *ext_or_cmd_new(uint16_t len);
#define ext_or_cmd_free(cmd) \
FREE_AND_NULL(ext_or_cmd_t, ext_or_cmd_free_, (cmd))
void ext_or_cmd_free_(ext_or_cmd_t *cmd);
void connection_or_set_ext_or_identifier(or_connection_t *conn);
void connection_or_remove_from_ext_or_id_map(or_connection_t *conn);
void connection_or_clear_ext_or_id_map(void);
or_connection_t *connection_or_get_by_ext_or_id(const char *id);
int connection_ext_or_finished_flushing(or_connection_t *conn);
int connection_ext_or_process_inbuf(or_connection_t *or_conn);
int init_ext_or_cookie_authentication(int is_enabled);
char *get_ext_or_auth_cookie_file_name(void);
/* (No stub needed for these: they are only called within feature/relay.) */
int init_ext_or_cookie_authentication(int is_enabled);
void ext_orport_free_all(void);
#else /* !defined(HAVE_MODULE_RELAY) */
static inline int
connection_ext_or_start_auth(or_connection_t *conn)
{
(void)conn;
tor_assert_nonfatal_unreached();
return -1;
}
static inline int
connection_ext_or_finished_flushing(or_connection_t *conn)
{
(void)conn;
tor_assert_nonfatal_unreached();
return -1;
}
static inline int
connection_ext_or_process_inbuf(or_connection_t *conn)
{
(void)conn;
tor_assert_nonfatal_unreached();
return -1;
}
#define connection_or_set_ext_or_identifier(conn) \
((void)(conn))
#define connection_or_remove_from_ext_or_id_map(conn) \
((void)(conn))
#define connection_or_clear_ext_or_id_map() \
STMT_NIL
#define get_ext_or_auth_cookie_file_name() \
(NULL)
#endif /* defined(HAVE_MODULE_RELAY) */
#ifdef EXT_ORPORT_PRIVATE
STATIC int connection_write_ext_or_command(connection_t *conn,
uint16_t command,
@ -60,9 +90,11 @@ STATIC int handle_client_auth_nonce(const char *client_nonce,
size_t client_nonce_len,
char **client_hash_out,
char **reply_out, size_t *reply_len_out);
#ifdef TOR_UNIT_TESTS
extern uint8_t *ext_or_auth_cookie;
extern int ext_or_auth_cookie_is_set;
or_connection_t *connection_or_get_by_ext_or_id(const char *id);
#endif
#endif /* defined(EXT_ORPORT_PRIVATE) */

View File

@ -2,7 +2,6 @@
# Legacy shared relay code: migrate to the relay module over time
LIBTOR_APP_A_SOURCES += \
src/feature/relay/dns.c \
src/feature/relay/ext_orport.c \
src/feature/relay/onion_queue.c \
src/feature/relay/router.c \
src/feature/relay/routerkeys.c
@ -11,6 +10,7 @@ LIBTOR_APP_A_SOURCES += \
# ADD_C_FILE: INSERT SOURCES HERE.
MODULE_RELAY_SOURCES = \
src/feature/relay/ext_orport.c \
src/feature/relay/routermode.c \
src/feature/relay/relay_config.c \
src/feature/relay/relay_periodic.c \