2006-02-09 06:46:49 +01:00
|
|
|
/* Copyright 2004-2006 Roger Dingledine, Nick Mathewson. */
|
2004-11-03 02:32:26 +01:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
2005-12-14 21:40:40 +01:00
|
|
|
const char control_c_id[] =
|
|
|
|
"$Id$";
|
2004-11-03 02:32:26 +01:00
|
|
|
|
2004-11-07 22:37:50 +01:00
|
|
|
/**
|
2004-11-30 07:17:35 +01:00
|
|
|
* \file control.c
|
|
|
|
* \brief Implementation for Tor's control-socket interface.
|
2005-06-11 07:31:17 +02:00
|
|
|
**/
|
2004-11-07 22:37:50 +01:00
|
|
|
|
2004-11-03 02:32:26 +01:00
|
|
|
#include "or.h"
|
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
#define STATE_IS_OPEN(s) ((s) == CONTROL_CONN_STATE_OPEN_V0 || \
|
2005-06-17 20:49:55 +02:00
|
|
|
(s) == CONTROL_CONN_STATE_OPEN_V1)
|
2005-12-14 21:40:40 +01:00
|
|
|
#define STATE_IS_V0(s) ((s) == CONTROL_CONN_STATE_NEEDAUTH_V0 || \
|
2005-06-17 20:49:55 +02:00
|
|
|
(s) == CONTROL_CONN_STATE_OPEN_V0)
|
|
|
|
|
|
|
|
/*
|
2005-12-14 21:40:40 +01:00
|
|
|
* See control-spec.txt and control-spec-v0.txt for full details on
|
|
|
|
* protocol(s).
|
|
|
|
*
|
2004-11-07 22:37:50 +01:00
|
|
|
*/
|
|
|
|
|
2006-10-03 20:58:52 +02:00
|
|
|
/* Recognized version 0 message type codes; do not add new codes to this list.
|
|
|
|
* Version 0 is dead; version 1 doesn't use codes. */
|
2005-06-17 20:49:55 +02:00
|
|
|
#define CONTROL0_CMD_ERROR 0x0000
|
|
|
|
#define CONTROL0_CMD_DONE 0x0001
|
|
|
|
#define CONTROL0_CMD_SETCONF 0x0002
|
|
|
|
#define CONTROL0_CMD_GETCONF 0x0003
|
|
|
|
#define CONTROL0_CMD_CONFVALUE 0x0004
|
|
|
|
#define CONTROL0_CMD_SETEVENTS 0x0005
|
|
|
|
#define CONTROL0_CMD_EVENT 0x0006
|
|
|
|
#define CONTROL0_CMD_AUTHENTICATE 0x0007
|
|
|
|
#define CONTROL0_CMD_SAVECONF 0x0008
|
|
|
|
#define CONTROL0_CMD_SIGNAL 0x0009
|
|
|
|
#define CONTROL0_CMD_MAPADDRESS 0x000A
|
|
|
|
#define CONTROL0_CMD_GETINFO 0x000B
|
|
|
|
#define CONTROL0_CMD_INFOVALUE 0x000C
|
|
|
|
#define CONTROL0_CMD_EXTENDCIRCUIT 0x000D
|
|
|
|
#define CONTROL0_CMD_ATTACHSTREAM 0x000E
|
|
|
|
#define CONTROL0_CMD_POSTDESCRIPTOR 0x000F
|
|
|
|
#define CONTROL0_CMD_FRAGMENTHEADER 0x0010
|
|
|
|
#define CONTROL0_CMD_FRAGMENT 0x0011
|
|
|
|
#define CONTROL0_CMD_REDIRECTSTREAM 0x0012
|
|
|
|
#define CONTROL0_CMD_CLOSESTREAM 0x0013
|
|
|
|
#define CONTROL0_CMD_CLOSECIRCUIT 0x0014
|
|
|
|
#define _CONTROL0_CMD_MAX_RECOGNIZED 0x0014
|
2004-11-03 02:32:26 +01:00
|
|
|
|
2006-10-03 20:58:52 +02:00
|
|
|
/* Recognized version 0 error codes. Do not expand. */
|
2004-11-03 02:32:26 +01:00
|
|
|
#define ERR_UNSPECIFIED 0x0000
|
2004-11-07 23:58:35 +01:00
|
|
|
#define ERR_INTERNAL 0x0001
|
|
|
|
#define ERR_UNRECOGNIZED_TYPE 0x0002
|
|
|
|
#define ERR_SYNTAX 0x0003
|
|
|
|
#define ERR_UNRECOGNIZED_CONFIG_KEY 0x0004
|
|
|
|
#define ERR_INVALID_CONFIG_VALUE 0x0005
|
|
|
|
#define ERR_UNRECOGNIZED_EVENT_CODE 0x0006
|
|
|
|
#define ERR_UNAUTHORIZED 0x0007
|
|
|
|
#define ERR_REJECTED_AUTHENTICATION 0x0008
|
2005-02-25 07:16:28 +01:00
|
|
|
#define ERR_RESOURCE_EXHAUSETED 0x0009
|
2005-03-12 05:22:01 +01:00
|
|
|
#define ERR_NO_STREAM 0x000A
|
|
|
|
#define ERR_NO_CIRC 0x000B
|
2005-03-19 07:05:55 +01:00
|
|
|
#define ERR_NO_ROUTER 0x000C
|
2004-11-03 02:32:26 +01:00
|
|
|
|
2006-10-03 20:58:52 +02:00
|
|
|
/* Recognized asynchronous event types. It's okay to expand this list
|
2006-10-07 11:13:30 +02:00
|
|
|
* because it is used both as a list of v0 event types, and as indices
|
2006-10-03 20:58:52 +02:00
|
|
|
* into the bitfield to determine which controllers want which events.
|
|
|
|
*/
|
2006-10-07 02:50:39 +02:00
|
|
|
#define _EVENT_MIN 0x0001
|
|
|
|
#define EVENT_CIRCUIT_STATUS 0x0001
|
|
|
|
#define EVENT_STREAM_STATUS 0x0002
|
|
|
|
#define EVENT_OR_CONN_STATUS 0x0003
|
|
|
|
#define EVENT_BANDWIDTH_USED 0x0004
|
|
|
|
#define EVENT_LOG_OBSOLETE 0x0005
|
|
|
|
#define EVENT_NEW_DESC 0x0006
|
|
|
|
#define EVENT_DEBUG_MSG 0x0007
|
|
|
|
#define EVENT_INFO_MSG 0x0008
|
|
|
|
#define EVENT_NOTICE_MSG 0x0009
|
|
|
|
#define EVENT_WARN_MSG 0x000A
|
|
|
|
#define EVENT_ERR_MSG 0x000B
|
|
|
|
#define LAST_V0_EVENT 0x000B
|
|
|
|
#define EVENT_ADDRMAP 0x000C
|
2005-11-19 19:35:43 +01:00
|
|
|
#define EVENT_AUTHDIR_NEWDESCS 0x000D
|
2006-10-07 02:50:39 +02:00
|
|
|
#define EVENT_DESCCHANGED 0x000E
|
2006-10-20 23:04:39 +02:00
|
|
|
#define EVENT_NS 0x000F
|
2006-10-23 07:24:57 +02:00
|
|
|
#define EVENT_STATUS_CLIENT 0x0010
|
|
|
|
#define EVENT_STATUS_SERVER 0x0011
|
|
|
|
#define EVENT_STATUS_GENERAL 0x0012
|
2006-12-15 23:40:20 +01:00
|
|
|
#define EVENT_GUARD 0x0013
|
|
|
|
#define _EVENT_MAX 0x0013
|
2006-10-20 23:04:39 +02:00
|
|
|
/* If _EVENT_MAX ever hits 0x0020, we need to make the mask wider. */
|
2004-11-03 02:32:26 +01:00
|
|
|
|
2004-11-07 22:37:50 +01:00
|
|
|
/** Array mapping from message type codes to human-readable message
|
2005-08-13 03:55:23 +02:00
|
|
|
* type names. Used for compatibility with version 0 of the control
|
2006-10-03 20:58:52 +02:00
|
|
|
* protocol. Do not add new items to this list. */
|
2005-06-17 20:49:55 +02:00
|
|
|
static const char * CONTROL0_COMMANDS[_CONTROL0_CMD_MAX_RECOGNIZED+1] = {
|
2004-11-03 02:32:26 +01:00
|
|
|
"error",
|
|
|
|
"done",
|
|
|
|
"setconf",
|
|
|
|
"getconf",
|
|
|
|
"confvalue",
|
|
|
|
"setevents",
|
|
|
|
"events",
|
|
|
|
"authenticate",
|
2004-11-07 23:37:59 +01:00
|
|
|
"saveconf",
|
2005-02-25 07:16:28 +01:00
|
|
|
"signal",
|
|
|
|
"mapaddress",
|
|
|
|
"getinfo",
|
|
|
|
"infovalue",
|
|
|
|
"extendcircuit",
|
|
|
|
"attachstream",
|
2005-03-02 21:22:10 +01:00
|
|
|
"postdescriptor",
|
|
|
|
"fragmentheader",
|
|
|
|
"fragment",
|
2006-10-03 20:58:52 +02:00
|
|
|
"redirectstream",
|
|
|
|
"closestream",
|
|
|
|
"closecircuit",
|
2004-11-03 02:32:26 +01:00
|
|
|
};
|
|
|
|
|
2004-11-07 23:37:59 +01:00
|
|
|
/** Bitfield: The bit 1<<e is set if <b>any</b> open control
|
2004-11-07 22:37:50 +01:00
|
|
|
* connection is interested in events of type <b>e</b>. We use this
|
|
|
|
* so that we can decide to skip generating event messages that nobody
|
2004-11-09 02:24:10 +01:00
|
|
|
* has interest in without having to walk over the global connection
|
2004-11-07 22:37:50 +01:00
|
|
|
* list to find out.
|
|
|
|
**/
|
2005-06-17 22:37:21 +02:00
|
|
|
static uint32_t global_event_mask0 = 0;
|
2006-10-03 20:58:40 +02:00
|
|
|
static uint32_t global_event_mask1long = 0;
|
|
|
|
static uint32_t global_event_mask1short = 0;
|
2004-11-03 02:32:26 +01:00
|
|
|
|
2005-07-13 07:14:42 +02:00
|
|
|
/** True iff we have disabled log messages from being sent to the controller */
|
|
|
|
static int disable_log_messages = 0;
|
|
|
|
|
2004-11-07 22:37:50 +01:00
|
|
|
/** Macro: true if any control connection is interested in events of type
|
|
|
|
* <b>e</b>. */
|
2005-06-17 22:37:21 +02:00
|
|
|
#define EVENT_IS_INTERESTING0(e) (global_event_mask0 & (1<<(e)))
|
2006-10-03 20:58:40 +02:00
|
|
|
#define EVENT_IS_INTERESTING1(e) \
|
|
|
|
((global_event_mask1long|global_event_mask1short) & (1<<(e)))
|
|
|
|
#define EVENT_IS_INTERESTING1L(e) (global_event_mask1long & (1<<(e)))
|
|
|
|
#define EVENT_IS_INTERESTING1S(e) (global_event_mask1short & (1<<(e)))
|
|
|
|
#define EVENT_IS_INTERESTING(e) \
|
|
|
|
((global_event_mask0|global_event_mask1short|global_event_mask1long) \
|
|
|
|
& (1<<(e)))
|
2004-11-07 22:37:50 +01:00
|
|
|
|
|
|
|
/** If we're using cookie-type authentication, how long should our cookies be?
|
|
|
|
*/
|
2004-11-03 20:49:03 +01:00
|
|
|
#define AUTHENTICATION_COOKIE_LEN 32
|
2004-11-07 22:37:50 +01:00
|
|
|
|
|
|
|
/** If true, we've set authentication_cookie to a secret code and
|
|
|
|
* stored it to disk. */
|
2004-11-03 20:49:03 +01:00
|
|
|
static int authentication_cookie_is_set = 0;
|
|
|
|
static char authentication_cookie[AUTHENTICATION_COOKIE_LEN];
|
|
|
|
|
2006-10-09 17:47:50 +02:00
|
|
|
#define SHORT_NAMES 1
|
|
|
|
#define LONG_NAMES 2
|
|
|
|
#define ALL_NAMES (SHORT_NAMES|LONG_NAMES)
|
|
|
|
#define EXTENDED_FORMAT 4
|
|
|
|
#define NONEXTENDED_FORMAT 8
|
|
|
|
#define ALL_FORMATS (EXTENDED_FORMAT|NONEXTENDED_FORMAT)
|
|
|
|
typedef int event_format_t;
|
2006-10-03 20:58:40 +02:00
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
static void connection_printf_to_buf(control_connection_t *conn,
|
2005-12-14 21:40:40 +01:00
|
|
|
const char *format, ...)
|
2005-06-17 22:37:21 +02:00
|
|
|
CHECK_PRINTF(2,3);
|
2005-08-12 19:24:53 +02:00
|
|
|
/*static*/ size_t write_escaped_data(const char *data, size_t len,
|
|
|
|
int translate_newlines, char **out);
|
|
|
|
/*static*/ size_t read_escaped_data(const char *data, size_t len,
|
|
|
|
int translate_newlines, char **out);
|
2006-07-26 21:07:26 +02:00
|
|
|
static void send_control0_message(control_connection_t *conn, uint16_t type,
|
2005-03-02 21:22:10 +01:00
|
|
|
uint32_t len, const char *body);
|
2006-07-26 21:07:26 +02:00
|
|
|
static void send_control_done(control_connection_t *conn);
|
|
|
|
static void send_control_done2(control_connection_t *conn, const char *msg,
|
2005-12-14 21:40:40 +01:00
|
|
|
size_t len);
|
2006-07-26 21:07:26 +02:00
|
|
|
static void send_control0_error(control_connection_t *conn, uint16_t error,
|
2004-11-03 02:32:26 +01:00
|
|
|
const char *message);
|
2005-12-14 21:40:40 +01:00
|
|
|
static void send_control0_event(uint16_t event, uint32_t len,
|
|
|
|
const char *body);
|
2006-10-09 17:47:50 +02:00
|
|
|
static void send_control1_event(uint16_t event, event_format_t which,
|
2006-10-03 20:58:40 +02:00
|
|
|
const char *format, ...)
|
|
|
|
CHECK_PRINTF(3,4);
|
2006-10-09 17:47:50 +02:00
|
|
|
static void send_control1_event_extended(uint16_t event, event_format_t which,
|
|
|
|
const char *format, ...)
|
|
|
|
CHECK_PRINTF(3,4);
|
2006-07-26 21:07:26 +02:00
|
|
|
static int handle_control_setconf(control_connection_t *conn, uint32_t len,
|
2004-11-04 23:31:50 +01:00
|
|
|
char *body);
|
2006-07-26 21:07:26 +02:00
|
|
|
static int handle_control_resetconf(control_connection_t *conn, uint32_t len,
|
2005-09-08 05:18:51 +02:00
|
|
|
char *body);
|
2006-07-26 21:07:26 +02:00
|
|
|
static int handle_control_getconf(control_connection_t *conn, uint32_t len,
|
2004-11-03 02:32:26 +01:00
|
|
|
const char *body);
|
2006-07-26 21:07:26 +02:00
|
|
|
static int handle_control_setevents(control_connection_t *conn, uint32_t len,
|
2004-11-03 02:32:26 +01:00
|
|
|
const char *body);
|
2006-07-26 21:07:37 +02:00
|
|
|
static int handle_control_authenticate(control_connection_t *conn,
|
|
|
|
uint32_t len,
|
2004-11-03 02:32:26 +01:00
|
|
|
const char *body);
|
2006-07-26 21:07:26 +02:00
|
|
|
static int handle_control_saveconf(control_connection_t *conn, uint32_t len,
|
2004-11-07 23:37:59 +01:00
|
|
|
const char *body);
|
2006-07-26 21:07:26 +02:00
|
|
|
static int handle_control_signal(control_connection_t *conn, uint32_t len,
|
2005-01-05 07:40:47 +01:00
|
|
|
const char *body);
|
2006-07-26 21:07:26 +02:00
|
|
|
static int handle_control_mapaddress(control_connection_t *conn, uint32_t len,
|
2005-02-25 07:16:28 +01:00
|
|
|
const char *body);
|
2006-12-08 05:39:13 +01:00
|
|
|
static char *list_getinfo_options(void);
|
2006-07-26 21:07:26 +02:00
|
|
|
static int handle_control_getinfo(control_connection_t *conn, uint32_t len,
|
2005-02-25 07:16:28 +01:00
|
|
|
const char *body);
|
2006-07-26 21:07:37 +02:00
|
|
|
static int handle_control_extendcircuit(control_connection_t *conn,
|
|
|
|
uint32_t len,
|
2005-02-25 07:16:28 +01:00
|
|
|
const char *body);
|
2006-07-26 21:07:37 +02:00
|
|
|
static int handle_control_setpurpose(control_connection_t *conn,
|
|
|
|
int for_circuits,
|
2006-03-17 23:08:59 +01:00
|
|
|
uint32_t len, const char *body);
|
2006-07-26 21:07:37 +02:00
|
|
|
static int handle_control_attachstream(control_connection_t *conn,
|
|
|
|
uint32_t len,
|
2005-02-25 07:16:28 +01:00
|
|
|
const char *body);
|
2006-07-26 21:07:37 +02:00
|
|
|
static int handle_control_postdescriptor(control_connection_t *conn,
|
|
|
|
uint32_t len,
|
2005-02-25 07:16:28 +01:00
|
|
|
const char *body);
|
2006-07-26 21:07:37 +02:00
|
|
|
static int handle_control_redirectstream(control_connection_t *conn,
|
|
|
|
uint32_t len,
|
2005-03-19 07:05:55 +01:00
|
|
|
const char *body);
|
2006-07-26 21:07:26 +02:00
|
|
|
static int handle_control_closestream(control_connection_t *conn, uint32_t len,
|
2005-03-22 20:36:38 +01:00
|
|
|
const char *body);
|
2006-07-26 21:07:37 +02:00
|
|
|
static int handle_control_closecircuit(control_connection_t *conn,
|
|
|
|
uint32_t len,
|
2005-03-22 20:36:38 +01:00
|
|
|
const char *body);
|
2006-10-03 20:58:52 +02:00
|
|
|
static int handle_control_usefeature(control_connection_t *conn,
|
|
|
|
uint32_t len,
|
|
|
|
const char *body);
|
2006-07-26 21:07:26 +02:00
|
|
|
static int write_stream_target_to_buf(edge_connection_t *conn, char *buf,
|
2005-12-14 21:40:40 +01:00
|
|
|
size_t len);
|
2006-10-03 21:00:18 +02:00
|
|
|
static void orconn_target_get_name(int long_names, char *buf, size_t len,
|
|
|
|
or_connection_t *conn);
|
2004-11-03 02:32:26 +01:00
|
|
|
|
2004-11-07 22:37:50 +01:00
|
|
|
/** Given a possibly invalid message type code <b>cmd</b>, return a
|
|
|
|
* human-readable string equivalent. */
|
2004-11-03 02:32:26 +01:00
|
|
|
static INLINE const char *
|
|
|
|
control_cmd_to_string(uint16_t cmd)
|
|
|
|
{
|
2005-12-14 21:40:40 +01:00
|
|
|
return (cmd<=_CONTROL0_CMD_MAX_RECOGNIZED) ?
|
|
|
|
CONTROL0_COMMANDS[cmd] : "Unknown";
|
2004-11-03 02:32:26 +01:00
|
|
|
}
|
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Given a control event code for a message event, return the corresponding
|
|
|
|
* log severity. */
|
2005-04-06 00:56:17 +02:00
|
|
|
static INLINE int
|
|
|
|
event_to_log_severity(int event)
|
|
|
|
{
|
|
|
|
switch (event) {
|
|
|
|
case EVENT_DEBUG_MSG: return LOG_DEBUG;
|
|
|
|
case EVENT_INFO_MSG: return LOG_INFO;
|
|
|
|
case EVENT_NOTICE_MSG: return LOG_NOTICE;
|
|
|
|
case EVENT_WARN_MSG: return LOG_WARN;
|
|
|
|
case EVENT_ERR_MSG: return LOG_ERR;
|
|
|
|
default: return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Given a log severity, return the corresponding control event code. */
|
2005-04-06 00:56:17 +02:00
|
|
|
static INLINE int
|
|
|
|
log_severity_to_event(int severity)
|
|
|
|
{
|
|
|
|
switch (severity) {
|
|
|
|
case LOG_DEBUG: return EVENT_DEBUG_MSG;
|
|
|
|
case LOG_INFO: return EVENT_INFO_MSG;
|
|
|
|
case LOG_NOTICE: return EVENT_NOTICE_MSG;
|
|
|
|
case LOG_WARN: return EVENT_WARN_MSG;
|
|
|
|
case LOG_ERR: return EVENT_ERR_MSG;
|
|
|
|
default: return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-03 20:58:52 +02:00
|
|
|
/** Set <b>global_event_mask*</b> to the bitwise OR of each live control
|
|
|
|
* connection's event_mask field. */
|
2005-08-07 21:20:55 +02:00
|
|
|
void
|
|
|
|
control_update_global_event_mask(void)
|
2004-11-03 02:32:26 +01:00
|
|
|
{
|
|
|
|
connection_t **conns;
|
|
|
|
int n_conns, i;
|
2005-06-17 22:37:21 +02:00
|
|
|
global_event_mask0 = 0;
|
2006-10-03 20:58:40 +02:00
|
|
|
global_event_mask1short = 0;
|
|
|
|
global_event_mask1long = 0;
|
2004-11-03 02:32:26 +01:00
|
|
|
get_connection_array(&conns, &n_conns);
|
|
|
|
for (i = 0; i < n_conns; ++i) {
|
|
|
|
if (conns[i]->type == CONN_TYPE_CONTROL &&
|
2005-06-17 20:49:55 +02:00
|
|
|
STATE_IS_OPEN(conns[i]->state)) {
|
2006-07-26 21:07:26 +02:00
|
|
|
control_connection_t *conn = TO_CONTROL_CONN(conns[i]);
|
|
|
|
if (STATE_IS_V0(conn->_base.state))
|
|
|
|
global_event_mask0 |= conn->event_mask;
|
2006-10-03 20:58:40 +02:00
|
|
|
else if (conn->use_long_names)
|
|
|
|
global_event_mask1long |= conn->event_mask;
|
2005-06-17 22:37:21 +02:00
|
|
|
else
|
2006-10-03 20:58:40 +02:00
|
|
|
global_event_mask1short |= conn->event_mask;
|
2004-11-03 02:32:26 +01:00
|
|
|
}
|
|
|
|
}
|
2005-04-06 00:56:17 +02:00
|
|
|
|
2005-07-13 07:14:42 +02:00
|
|
|
control_adjust_event_log_severity();
|
2005-04-06 00:56:17 +02:00
|
|
|
}
|
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Adjust the log severities that result in control_event_logmsg being called
|
|
|
|
* to match the severity of log messages that any controllers are interested
|
|
|
|
* in. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2005-07-13 07:14:42 +02:00
|
|
|
control_adjust_event_log_severity(void)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2005-04-06 00:56:17 +02:00
|
|
|
int i;
|
|
|
|
int min_log_event=EVENT_ERR_MSG, max_log_event=EVENT_DEBUG_MSG;
|
|
|
|
|
|
|
|
for (i = EVENT_DEBUG_MSG; i <= EVENT_ERR_MSG; ++i) {
|
|
|
|
if (EVENT_IS_INTERESTING(i)) {
|
|
|
|
min_log_event = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = EVENT_ERR_MSG; i >= EVENT_DEBUG_MSG; --i) {
|
|
|
|
if (EVENT_IS_INTERESTING(i)) {
|
|
|
|
max_log_event = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (EVENT_IS_INTERESTING(EVENT_LOG_OBSOLETE)) {
|
|
|
|
if (min_log_event > EVENT_NOTICE_MSG)
|
|
|
|
min_log_event = EVENT_NOTICE_MSG;
|
|
|
|
if (max_log_event < EVENT_ERR_MSG)
|
|
|
|
max_log_event = EVENT_ERR_MSG;
|
|
|
|
}
|
|
|
|
change_callback_log_severity(event_to_log_severity(min_log_event),
|
|
|
|
event_to_log_severity(max_log_event),
|
|
|
|
control_event_logmsg);
|
2004-11-03 02:32:26 +01:00
|
|
|
}
|
|
|
|
|
2005-07-15 21:31:11 +02:00
|
|
|
/** Append a NUL-terminated string <b>s</b> to the end of
|
|
|
|
* <b>conn</b>-\>outbuf
|
|
|
|
*/
|
2005-06-17 20:49:55 +02:00
|
|
|
static INLINE void
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_write_str_to_buf(const char *s, control_connection_t *conn)
|
2005-06-17 20:49:55 +02:00
|
|
|
{
|
|
|
|
size_t len = strlen(s);
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_write_to_buf(s, len, TO_CONN(conn));
|
2005-06-17 20:49:55 +02:00
|
|
|
}
|
|
|
|
|
2005-07-15 21:31:11 +02:00
|
|
|
/** Given a <b>len</b>-character string in <b>data</b>, made of lines
|
|
|
|
* terminated by CRLF, allocate a new string in *<b>out</b>, and copy
|
|
|
|
* the contents of <b>data</b> into *<b>out</b>, adding a period
|
|
|
|
* before any period that that appears at the start of a line, and
|
|
|
|
* adding a period-CRLF line at the end. If <b>translate_newlines</b>
|
|
|
|
* is true, replace all LF characters sequences with CRLF. Return the
|
|
|
|
* number of bytes in *<b>out</b>.
|
|
|
|
*/
|
|
|
|
/* static */ size_t
|
2005-06-18 04:39:25 +02:00
|
|
|
write_escaped_data(const char *data, size_t len, int translate_newlines,
|
|
|
|
char **out)
|
|
|
|
{
|
|
|
|
size_t sz_out = len+8;
|
|
|
|
char *outp;
|
|
|
|
const char *end;
|
|
|
|
int i;
|
|
|
|
int start_of_line;
|
2005-06-28 01:35:04 +02:00
|
|
|
for (i=0; i<(int)len; ++i) {
|
2005-06-18 04:39:25 +02:00
|
|
|
if (data[i]== '\n')
|
2005-09-22 01:13:29 +02:00
|
|
|
sz_out += 2; /* Maybe add a CR; maybe add a dot. */
|
2005-06-18 04:39:25 +02:00
|
|
|
}
|
|
|
|
*out = outp = tor_malloc(sz_out+1);
|
|
|
|
end = data+len;
|
|
|
|
start_of_line = 1;
|
|
|
|
while (data < end) {
|
|
|
|
if (*data == '\n') {
|
|
|
|
if (translate_newlines)
|
|
|
|
*outp++ = '\r';
|
|
|
|
start_of_line = 1;
|
|
|
|
} else if (*data == '.') {
|
|
|
|
if (start_of_line) {
|
|
|
|
start_of_line = 0;
|
|
|
|
*outp++ = '.';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
start_of_line = 0;
|
|
|
|
}
|
|
|
|
*outp++ = *data++;
|
|
|
|
}
|
2005-06-19 22:40:41 +02:00
|
|
|
if (outp < *out+2 || memcmp(outp-2, "\r\n", 2)) {
|
|
|
|
*outp++ = '\r';
|
|
|
|
*outp++ = '\n';
|
|
|
|
}
|
2005-06-18 04:39:25 +02:00
|
|
|
*outp++ = '.';
|
|
|
|
*outp++ = '\r';
|
|
|
|
*outp++ = '\n';
|
2005-09-22 01:13:29 +02:00
|
|
|
*outp = '\0'; /* NUL-terminate just in case. */
|
2005-09-29 22:49:41 +02:00
|
|
|
tor_assert((outp - *out) <= (int)sz_out);
|
2005-06-18 04:39:25 +02:00
|
|
|
return outp - *out;
|
|
|
|
}
|
|
|
|
|
2005-07-15 21:31:11 +02:00
|
|
|
/** Given a <b>len</b>-character string in <b>data</b>, made of lines
|
|
|
|
* terminated by CRLF, allocate a new string in *<b>out</b>, and copy
|
|
|
|
* the contents of <b>data</b> into *<b>out</b>, removing any period
|
|
|
|
* that appears at the start of a line. If <b>translate_newlines</b>
|
|
|
|
* is true, replace all CRLF sequences with LF. Return the number of
|
|
|
|
* bytes in *<b>out</b>. */
|
|
|
|
/*static*/ size_t
|
2005-06-18 04:39:25 +02:00
|
|
|
read_escaped_data(const char *data, size_t len, int translate_newlines,
|
|
|
|
char **out)
|
|
|
|
{
|
|
|
|
char *outp;
|
|
|
|
const char *next;
|
2005-07-15 21:31:11 +02:00
|
|
|
const char *end;
|
|
|
|
|
|
|
|
*out = outp = tor_malloc(len+1);
|
2005-06-18 04:39:25 +02:00
|
|
|
|
2005-07-15 21:31:11 +02:00
|
|
|
end = data+len;
|
2005-06-18 04:39:25 +02:00
|
|
|
|
2005-07-15 21:31:11 +02:00
|
|
|
while (data < end) {
|
2005-06-18 04:39:25 +02:00
|
|
|
if (*data == '.')
|
|
|
|
++data;
|
|
|
|
if (translate_newlines)
|
2005-07-15 21:31:11 +02:00
|
|
|
next = tor_memmem(data, end-data, "\r\n", 2);
|
2005-06-18 04:39:25 +02:00
|
|
|
else
|
2005-07-15 21:31:11 +02:00
|
|
|
next = tor_memmem(data, end-data, "\r\n.", 3);
|
2005-06-18 04:39:25 +02:00
|
|
|
if (next) {
|
|
|
|
memcpy(outp, data, next-data);
|
|
|
|
outp += (next-data);
|
|
|
|
data = next+2;
|
|
|
|
} else {
|
2005-07-15 21:31:11 +02:00
|
|
|
memcpy(outp, data, end-data);
|
|
|
|
outp += (end-data);
|
|
|
|
*outp = '\0';
|
2005-06-18 04:39:25 +02:00
|
|
|
return outp - *out;
|
|
|
|
}
|
|
|
|
if (translate_newlines) {
|
|
|
|
*outp++ = '\n';
|
|
|
|
} else {
|
|
|
|
*outp++ = '\r';
|
|
|
|
*outp++ = '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-15 21:31:11 +02:00
|
|
|
*outp = '\0';
|
2005-06-18 04:39:25 +02:00
|
|
|
return outp - *out;
|
|
|
|
}
|
2005-06-17 20:49:55 +02:00
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Given a pointer to a string starting at <b>start</b> containing
|
|
|
|
* <b>in_len_max</b> characters, decode a string beginning with a single
|
|
|
|
* quote, containing any number of non-quote characters or characters escaped
|
|
|
|
* with a backslash, and ending with a final quote. Place the resulting
|
|
|
|
* string (unquoted, unescaped) into a newly allocated string in *<b>out</b>;
|
|
|
|
* store its length in <b>out_len</b>. On success, return a pointer to the
|
|
|
|
* character immediately following the escaped string. On failure, return
|
2005-10-04 08:53:59 +02:00
|
|
|
* NULL. */
|
2005-06-18 05:50:08 +02:00
|
|
|
static const char *
|
2005-06-19 22:40:41 +02:00
|
|
|
get_escaped_string(const char *start, size_t in_len_max,
|
|
|
|
char **out, size_t *out_len)
|
2005-06-18 05:50:08 +02:00
|
|
|
{
|
2005-06-19 22:40:41 +02:00
|
|
|
const char *cp, *end;
|
|
|
|
char *outp;
|
|
|
|
size_t len=0;
|
|
|
|
|
|
|
|
if (*start != '\"')
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
cp = start+1;
|
|
|
|
end = start+in_len_max;
|
2005-06-18 05:50:08 +02:00
|
|
|
|
2005-06-19 22:40:41 +02:00
|
|
|
/* Calculate length. */
|
|
|
|
while (1) {
|
|
|
|
if (cp >= end)
|
|
|
|
return NULL;
|
|
|
|
else if (*cp == '\\') {
|
|
|
|
if (++cp == end)
|
|
|
|
return NULL; /* Can't escape EOS. */
|
|
|
|
++cp;
|
|
|
|
++len;
|
|
|
|
} else if (*cp == '\"') {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
++cp;
|
|
|
|
++len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end = cp;
|
|
|
|
outp = *out = tor_malloc(len+1);
|
|
|
|
*out_len = len;
|
2005-06-17 22:37:21 +02:00
|
|
|
|
2005-06-19 22:40:41 +02:00
|
|
|
cp = start+1;
|
|
|
|
while (cp < end) {
|
|
|
|
if (*cp == '\\')
|
|
|
|
++cp;
|
|
|
|
*outp++ = *cp++;
|
|
|
|
}
|
|
|
|
*outp = '\0';
|
2005-09-08 08:37:50 +02:00
|
|
|
tor_assert((outp - *out) == (int)*out_len);
|
2005-06-19 22:40:41 +02:00
|
|
|
|
|
|
|
return end+1;
|
|
|
|
}
|
|
|
|
|
2005-07-15 21:31:11 +02:00
|
|
|
/** Acts like sprintf, but writes its formatted string to the end of
|
|
|
|
* <b>conn</b>-\>outbuf. The message may be truncated if it is too long,
|
|
|
|
* but it will always end with a CRLF sequence.
|
2005-10-18 18:45:43 +02:00
|
|
|
*
|
|
|
|
* Currently the length of the message is limited to 1024 (including the
|
|
|
|
* ending \n\r\0. */
|
2005-06-17 20:49:55 +02:00
|
|
|
static void
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_printf_to_buf(control_connection_t *conn, const char *format, ...)
|
2005-06-17 20:49:55 +02:00
|
|
|
{
|
2005-10-18 18:45:43 +02:00
|
|
|
#define CONNECTION_PRINTF_TO_BUF_BUFFERSIZE 1024
|
2005-06-17 20:49:55 +02:00
|
|
|
va_list ap;
|
2005-10-18 18:45:43 +02:00
|
|
|
char buf[CONNECTION_PRINTF_TO_BUF_BUFFERSIZE];
|
2005-06-17 20:49:55 +02:00
|
|
|
int r;
|
|
|
|
size_t len;
|
|
|
|
va_start(ap,format);
|
2005-06-17 22:37:21 +02:00
|
|
|
r = tor_vsnprintf(buf, sizeof(buf), format, ap);
|
2005-06-17 20:49:55 +02:00
|
|
|
va_end(ap);
|
2006-10-09 04:35:43 +02:00
|
|
|
if (r<0) {
|
|
|
|
log_warn(LD_BUG, "Unable to format string for controller.");
|
|
|
|
return;
|
|
|
|
}
|
2005-06-17 20:49:55 +02:00
|
|
|
len = strlen(buf);
|
|
|
|
if (memcmp("\r\n\0", buf+len-2, 3)) {
|
2005-10-18 18:45:43 +02:00
|
|
|
buf[CONNECTION_PRINTF_TO_BUF_BUFFERSIZE-1] = '\0';
|
|
|
|
buf[CONNECTION_PRINTF_TO_BUF_BUFFERSIZE-2] = '\n';
|
|
|
|
buf[CONNECTION_PRINTF_TO_BUF_BUFFERSIZE-3] = '\r';
|
2005-06-17 20:49:55 +02:00
|
|
|
}
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_write_to_buf(buf, len, TO_CONN(conn));
|
2005-06-17 20:49:55 +02:00
|
|
|
}
|
|
|
|
|
2004-11-07 22:37:50 +01:00
|
|
|
/** Send a message of type <b>type</b> containing <b>len</b> bytes
|
|
|
|
* from <b>body</b> along the control connection <b>conn</b> */
|
2004-11-03 02:32:26 +01:00
|
|
|
static void
|
2006-07-26 21:07:26 +02:00
|
|
|
send_control0_message(control_connection_t *conn, uint16_t type, uint32_t len,
|
|
|
|
const char *body)
|
2004-11-03 02:32:26 +01:00
|
|
|
{
|
2005-03-02 21:22:10 +01:00
|
|
|
char buf[10];
|
2004-11-03 02:32:26 +01:00
|
|
|
tor_assert(conn);
|
2006-07-26 21:07:26 +02:00
|
|
|
tor_assert(STATE_IS_V0(conn->_base.state));
|
2004-11-09 11:08:42 +01:00
|
|
|
tor_assert(len || !body);
|
2005-06-17 20:49:55 +02:00
|
|
|
tor_assert(type <= _CONTROL0_CMD_MAX_RECOGNIZED);
|
2005-03-02 21:22:10 +01:00
|
|
|
if (len < 65536) {
|
|
|
|
set_uint16(buf, htons(len));
|
|
|
|
set_uint16(buf+2, htons(type));
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_write_to_buf(buf, 4, TO_CONN(conn));
|
2005-03-02 21:22:10 +01:00
|
|
|
if (len)
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_write_to_buf(body, len, TO_CONN(conn));
|
2005-03-02 21:22:10 +01:00
|
|
|
} else {
|
|
|
|
set_uint16(buf, htons(65535));
|
2005-06-17 20:49:55 +02:00
|
|
|
set_uint16(buf+2, htons(CONTROL0_CMD_FRAGMENTHEADER));
|
2005-03-02 21:22:10 +01:00
|
|
|
set_uint16(buf+4, htons(type));
|
|
|
|
set_uint32(buf+6, htonl(len));
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_write_to_buf(buf, 10, TO_CONN(conn));
|
|
|
|
connection_write_to_buf(body, 65535-6, TO_CONN(conn));
|
2005-03-02 21:22:10 +01:00
|
|
|
len -= (65535-6);
|
|
|
|
body += (65535-6);
|
|
|
|
while (len) {
|
|
|
|
size_t chunklen = (len<65535)?len:65535;
|
|
|
|
set_uint16(buf, htons((uint16_t)chunklen));
|
2005-06-17 20:49:55 +02:00
|
|
|
set_uint16(buf+2, htons(CONTROL0_CMD_FRAGMENT));
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_write_to_buf(buf, 4, TO_CONN(conn));
|
|
|
|
connection_write_to_buf(body, chunklen, TO_CONN(conn));
|
2005-03-02 21:22:10 +01:00
|
|
|
len -= chunklen;
|
|
|
|
body += chunklen;
|
|
|
|
}
|
|
|
|
}
|
2004-11-03 02:32:26 +01:00
|
|
|
}
|
|
|
|
|
2004-11-07 22:37:50 +01:00
|
|
|
/** Send a "DONE" message down the control connection <b>conn</b> */
|
2004-11-03 02:32:26 +01:00
|
|
|
static void
|
2006-07-26 21:07:26 +02:00
|
|
|
send_control_done(control_connection_t *conn)
|
2004-11-03 02:32:26 +01:00
|
|
|
{
|
2006-07-26 21:07:26 +02:00
|
|
|
if (STATE_IS_V0(conn->_base.state)) {
|
2005-06-17 20:49:55 +02:00
|
|
|
send_control0_message(conn, CONTROL0_CMD_DONE, 0, NULL);
|
|
|
|
} else {
|
|
|
|
connection_write_str_to_buf("250 OK\r\n", conn);
|
|
|
|
}
|
2004-11-03 02:32:26 +01:00
|
|
|
}
|
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Send a "DONE" message down the v0 control message <b>conn</b>, with body
|
|
|
|
* as provided in the <b>len</b> bytes at <b>msg</b>.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
2006-07-26 21:07:26 +02:00
|
|
|
send_control_done2(control_connection_t *conn, const char *msg, size_t len)
|
2005-02-25 21:46:13 +01:00
|
|
|
{
|
2005-04-03 00:02:13 +02:00
|
|
|
if (len==0)
|
|
|
|
len = strlen(msg);
|
2005-06-17 20:49:55 +02:00
|
|
|
send_control0_message(conn, CONTROL0_CMD_DONE, len, msg);
|
2005-02-25 21:46:13 +01:00
|
|
|
}
|
|
|
|
|
2004-11-07 22:37:50 +01:00
|
|
|
/** Send an error message with error code <b>error</b> and body
|
|
|
|
* <b>message</b> down the connection <b>conn</b> */
|
2004-11-03 02:32:26 +01:00
|
|
|
static void
|
2006-07-26 21:07:37 +02:00
|
|
|
send_control0_error(control_connection_t *conn, uint16_t error,
|
|
|
|
const char *message)
|
2004-11-03 02:32:26 +01:00
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
size_t len;
|
|
|
|
set_uint16(buf, htons(error));
|
|
|
|
len = strlen(message);
|
|
|
|
tor_assert(len < (256-2));
|
|
|
|
memcpy(buf+2, message, len);
|
2005-06-17 20:49:55 +02:00
|
|
|
send_control0_message(conn, CONTROL0_CMD_ERROR, (uint16_t)(len+2), buf);
|
2004-11-03 02:32:26 +01:00
|
|
|
}
|
|
|
|
|
2004-11-07 22:37:50 +01:00
|
|
|
/** Send an 'event' message of event type <b>event</b>, containing
|
|
|
|
* <b>len</b> bytes in <b>body</b> to every control connection that
|
|
|
|
* is interested in it. */
|
2004-11-03 02:32:26 +01:00
|
|
|
static void
|
2005-06-17 20:49:55 +02:00
|
|
|
send_control0_event(uint16_t event, uint32_t len, const char *body)
|
2004-11-03 02:32:26 +01:00
|
|
|
{
|
|
|
|
connection_t **conns;
|
|
|
|
int n_conns, i;
|
2004-11-11 01:54:53 +01:00
|
|
|
size_t buflen;
|
|
|
|
char *buf;
|
|
|
|
|
2005-09-22 01:30:15 +02:00
|
|
|
tor_assert(event >= _EVENT_MIN && event <= LAST_V0_EVENT);
|
2005-04-18 00:52:02 +02:00
|
|
|
|
2004-11-11 01:54:53 +01:00
|
|
|
buflen = len + 2;
|
|
|
|
buf = tor_malloc_zero(buflen);
|
|
|
|
set_uint16(buf, htons(event));
|
|
|
|
memcpy(buf+2, body, len);
|
2004-11-03 02:32:26 +01:00
|
|
|
|
|
|
|
get_connection_array(&conns, &n_conns);
|
|
|
|
for (i = 0; i < n_conns; ++i) {
|
|
|
|
if (conns[i]->type == CONN_TYPE_CONTROL &&
|
2005-08-07 21:27:38 +02:00
|
|
|
!conns[i]->marked_for_close &&
|
2006-07-26 21:07:26 +02:00
|
|
|
conns[i]->state == CONTROL_CONN_STATE_OPEN_V0) {
|
|
|
|
control_connection_t *control_conn = TO_CONTROL_CONN(conns[i]);
|
|
|
|
if (control_conn->event_mask & (1<<event)) {
|
|
|
|
send_control0_message(control_conn, CONTROL0_CMD_EVENT, buflen, buf);
|
|
|
|
if (event == EVENT_ERR_MSG)
|
2006-12-29 06:06:47 +01:00
|
|
|
connection_handle_write(TO_CONN(control_conn), 1);
|
2006-07-26 21:07:26 +02:00
|
|
|
}
|
2004-11-03 02:32:26 +01:00
|
|
|
}
|
|
|
|
}
|
2004-11-11 01:54:53 +01:00
|
|
|
|
2004-11-12 17:39:03 +01:00
|
|
|
tor_free(buf);
|
2004-11-03 02:32:26 +01:00
|
|
|
}
|
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/* Send an event to all v1 controllers that are listening for code
|
2006-10-09 17:47:50 +02:00
|
|
|
* <b>event</b>. The event's body is given by <b>msg</b>.
|
|
|
|
*
|
|
|
|
* docdoc which, is_extended */
|
2005-06-17 22:37:21 +02:00
|
|
|
static void
|
2006-10-09 17:47:50 +02:00
|
|
|
send_control1_event_string(uint16_t event, event_format_t which,
|
|
|
|
const char *msg)
|
2005-06-17 22:37:21 +02:00
|
|
|
{
|
|
|
|
connection_t **conns;
|
2005-10-18 18:45:43 +02:00
|
|
|
int n_conns, i;
|
2005-06-17 22:37:21 +02:00
|
|
|
|
|
|
|
tor_assert(event >= _EVENT_MIN && event <= _EVENT_MAX);
|
|
|
|
|
|
|
|
get_connection_array(&conns, &n_conns);
|
|
|
|
for (i = 0; i < n_conns; ++i) {
|
|
|
|
if (conns[i]->type == CONN_TYPE_CONTROL &&
|
2005-08-07 21:27:38 +02:00
|
|
|
!conns[i]->marked_for_close &&
|
2006-07-26 21:07:26 +02:00
|
|
|
conns[i]->state == CONTROL_CONN_STATE_OPEN_V1) {
|
|
|
|
control_connection_t *control_conn = TO_CONTROL_CONN(conns[i]);
|
2006-10-09 17:47:50 +02:00
|
|
|
if (control_conn->use_long_names) {
|
|
|
|
if (!(which & LONG_NAMES))
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
if (!(which & SHORT_NAMES))
|
2006-10-03 20:58:40 +02:00
|
|
|
continue;
|
2006-10-09 17:47:50 +02:00
|
|
|
}
|
|
|
|
if (control_conn->use_extended_events) {
|
|
|
|
if (!(which & EXTENDED_FORMAT))
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
if (!(which & NONEXTENDED_FORMAT))
|
2006-10-03 20:58:40 +02:00
|
|
|
continue;
|
|
|
|
}
|
2006-07-26 21:07:26 +02:00
|
|
|
if (control_conn->event_mask & (1<<event)) {
|
|
|
|
connection_write_to_buf(msg, strlen(msg), TO_CONN(control_conn));
|
|
|
|
if (event == EVENT_ERR_MSG)
|
2006-12-29 06:06:47 +01:00
|
|
|
connection_handle_write(TO_CONN(control_conn), 1);
|
2006-07-26 21:07:26 +02:00
|
|
|
}
|
2005-06-17 22:37:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-18 18:45:43 +02:00
|
|
|
static void
|
2006-10-09 17:47:50 +02:00
|
|
|
send_control1_event_impl(uint16_t event, event_format_t which, int extended,
|
|
|
|
const char *format, va_list ap)
|
2005-10-18 18:45:43 +02:00
|
|
|
{
|
|
|
|
#define SEND_CONTROL1_EVENT_BUFFERSIZE 1024
|
|
|
|
int r;
|
|
|
|
char buf[SEND_CONTROL1_EVENT_BUFFERSIZE]; /* XXXX Length */
|
|
|
|
size_t len;
|
2006-10-09 17:47:50 +02:00
|
|
|
char *cp;
|
2005-10-18 18:45:43 +02:00
|
|
|
|
|
|
|
r = tor_vsnprintf(buf, sizeof(buf), format, ap);
|
2006-10-09 04:35:43 +02:00
|
|
|
if (r<0) {
|
|
|
|
log_warn(LD_BUG, "Unable to format event for controller.");
|
|
|
|
return;
|
|
|
|
}
|
2005-10-18 18:45:43 +02:00
|
|
|
|
|
|
|
len = strlen(buf);
|
|
|
|
if (memcmp("\r\n\0", buf+len-2, 3)) {
|
|
|
|
/* if it is not properly terminated, do it now */
|
|
|
|
buf[SEND_CONTROL1_EVENT_BUFFERSIZE-1] = '\0';
|
|
|
|
buf[SEND_CONTROL1_EVENT_BUFFERSIZE-2] = '\n';
|
|
|
|
buf[SEND_CONTROL1_EVENT_BUFFERSIZE-3] = '\r';
|
|
|
|
}
|
|
|
|
|
2006-10-09 17:47:50 +02:00
|
|
|
if (extended && (cp = strchr(buf, '@'))) {
|
|
|
|
which &= ~ALL_FORMATS;
|
|
|
|
*cp = ' ';
|
|
|
|
send_control1_event_string(event, which|EXTENDED_FORMAT, buf);
|
|
|
|
memcpy(cp, "\r\n\0", 3);
|
|
|
|
send_control1_event_string(event, which|NONEXTENDED_FORMAT, buf);
|
|
|
|
} else {
|
|
|
|
send_control1_event_string(event, which|ALL_FORMATS, buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send an event to all v1 controllers that are listening for code
|
|
|
|
* <b>event</b>. The event's body is created by the printf-style format in
|
|
|
|
* <b>format</b>, and other arguments as provided.
|
|
|
|
*
|
|
|
|
* Currently the length of the message is limited to 1024 (including the
|
|
|
|
* ending \n\r\0. */
|
|
|
|
static void
|
|
|
|
send_control1_event(uint16_t event, event_format_t which,
|
|
|
|
const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
send_control1_event_impl(event, which, 0, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send an event to all v1 controllers that are listening for code
|
|
|
|
* <b>event</b>. The event's body is created by the printf-style format in
|
|
|
|
* <b>format</b>, and other arguments as provided.
|
|
|
|
*
|
|
|
|
* If the format contains a single '@' character, it will be replaced with a
|
|
|
|
* space and all text after that character will be sent only to controllers
|
|
|
|
* that have enabled extended events.
|
|
|
|
*
|
|
|
|
* Currently the length of the message is limited to 1024 (including the
|
|
|
|
* ending \n\r\0. */
|
|
|
|
static void
|
|
|
|
send_control1_event_extended(uint16_t event, event_format_t which,
|
|
|
|
const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
send_control1_event_impl(event, which, 1, format, ap);
|
|
|
|
va_end(ap);
|
2005-10-18 18:45:43 +02:00
|
|
|
}
|
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Given a text circuit <b>id</b>, return the corresponding circuit. */
|
2006-07-28 17:11:11 +02:00
|
|
|
static origin_circuit_t *
|
2005-06-18 04:39:25 +02:00
|
|
|
get_circ(const char *id)
|
|
|
|
{
|
|
|
|
unsigned long n_id;
|
|
|
|
int ok;
|
|
|
|
n_id = tor_parse_ulong(id, 10, 0, ULONG_MAX, &ok, NULL);
|
|
|
|
if (!ok)
|
|
|
|
return NULL;
|
|
|
|
return circuit_get_by_global_id(n_id);
|
|
|
|
}
|
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Given a text stream <b>id</b>, return the corresponding AP connection. */
|
2006-07-28 17:11:11 +02:00
|
|
|
static edge_connection_t *
|
2005-06-18 04:39:25 +02:00
|
|
|
get_stream(const char *id)
|
|
|
|
{
|
|
|
|
unsigned long n_id;
|
|
|
|
int ok;
|
2006-07-28 17:11:11 +02:00
|
|
|
edge_connection_t *conn;
|
2005-06-18 04:39:25 +02:00
|
|
|
n_id = tor_parse_ulong(id, 10, 0, ULONG_MAX, &ok, NULL);
|
|
|
|
if (!ok)
|
|
|
|
return NULL;
|
|
|
|
conn = connection_get_by_global_id(n_id);
|
2006-07-28 17:11:11 +02:00
|
|
|
if (!conn || conn->_base.type != CONN_TYPE_AP)
|
2005-06-18 04:39:25 +02:00
|
|
|
return NULL;
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
2005-09-08 05:18:51 +02:00
|
|
|
/** Helper for setconf and resetconf. Acts like setconf, except
|
2005-09-14 04:07:35 +02:00
|
|
|
* it passes <b>use_defaults</b> on to options_trial_assign().
|
2005-09-08 05:18:51 +02:00
|
|
|
*/
|
2004-11-03 19:33:07 +01:00
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
control_setconf_helper(control_connection_t *conn, uint32_t len, char *body,
|
2005-09-14 04:07:35 +02:00
|
|
|
int use_defaults, int clear_first)
|
2004-11-03 02:32:26 +01:00
|
|
|
{
|
2004-11-07 23:58:35 +01:00
|
|
|
int r;
|
2005-07-22 23:12:10 +02:00
|
|
|
config_line_t *lines=NULL;
|
2005-06-19 22:40:41 +02:00
|
|
|
char *start = body;
|
2006-03-26 08:51:26 +02:00
|
|
|
char *errstring = NULL;
|
2006-07-26 21:07:26 +02:00
|
|
|
int v0 = STATE_IS_V0(conn->_base.state);
|
2004-11-04 23:31:50 +01:00
|
|
|
|
2005-06-18 05:50:08 +02:00
|
|
|
if (!v0) {
|
|
|
|
char *config = tor_malloc(len+1);
|
|
|
|
char *outp = config;
|
|
|
|
while (*body) {
|
|
|
|
char *eq = body;
|
|
|
|
while (!TOR_ISSPACE(*eq) && *eq != '=')
|
|
|
|
++eq;
|
|
|
|
memcpy(outp, body, eq-body);
|
|
|
|
outp += (eq-body);
|
2005-06-19 22:40:41 +02:00
|
|
|
*outp++ = ' ';
|
2005-06-18 05:50:08 +02:00
|
|
|
body = eq+1;
|
|
|
|
if (*eq == '=') {
|
|
|
|
if (*body != '\"') {
|
|
|
|
while (!TOR_ISSPACE(*body))
|
|
|
|
*outp++ = *body++;
|
|
|
|
} else {
|
|
|
|
char *val;
|
|
|
|
size_t val_len;
|
2005-06-19 22:40:41 +02:00
|
|
|
body = (char*)get_escaped_string(body, (len - (body-start)),
|
|
|
|
&val, &val_len);
|
2005-06-18 05:50:08 +02:00
|
|
|
if (!body) {
|
|
|
|
connection_write_str_to_buf("551 Couldn't parse string\r\n", conn);
|
|
|
|
tor_free(config);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy(outp, val, val_len);
|
|
|
|
outp += val_len;
|
2005-06-19 22:40:41 +02:00
|
|
|
tor_free(val);
|
2005-06-18 05:50:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (TOR_ISSPACE(*body))
|
|
|
|
++body;
|
|
|
|
*outp++ = '\n';
|
|
|
|
}
|
|
|
|
*outp = '\0';
|
|
|
|
|
|
|
|
if (config_get_lines(config, &lines) < 0) {
|
2006-02-13 10:02:35 +01:00
|
|
|
log_warn(LD_CONTROL,"Controller gave us config lines we can't parse.");
|
2005-12-14 21:40:40 +01:00
|
|
|
connection_write_str_to_buf("551 Couldn't parse configuration\r\n",
|
|
|
|
conn);
|
2005-06-18 05:50:08 +02:00
|
|
|
tor_free(config);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
tor_free(config);
|
|
|
|
} else {
|
|
|
|
if (config_get_lines(body, &lines) < 0) {
|
2006-10-03 00:13:42 +02:00
|
|
|
log_warn(LD_CONTROL,
|
|
|
|
"V0 controller gave us config lines we can't parse.");
|
2005-06-18 05:50:08 +02:00
|
|
|
send_control0_error(conn, ERR_SYNTAX, "Couldn't parse configuration");
|
|
|
|
return 0;
|
|
|
|
}
|
2004-11-06 06:18:11 +01:00
|
|
|
}
|
2004-11-04 23:31:50 +01:00
|
|
|
|
2006-03-26 08:51:26 +02:00
|
|
|
if ((r=options_trial_assign(lines, use_defaults,
|
|
|
|
clear_first, &errstring)) < 0) {
|
2005-09-14 04:36:29 +02:00
|
|
|
int v0_err;
|
|
|
|
const char *msg;
|
2006-02-13 10:02:35 +01:00
|
|
|
log_warn(LD_CONTROL,
|
2006-03-26 08:51:26 +02:00
|
|
|
"Controller gave us config lines that didn't validate: %s.",
|
|
|
|
errstring);
|
2005-09-14 04:36:29 +02:00
|
|
|
switch (r) {
|
|
|
|
case -1:
|
|
|
|
v0_err = ERR_UNRECOGNIZED_CONFIG_KEY;
|
2005-11-17 23:01:46 +01:00
|
|
|
msg = "552 Unrecognized option";
|
2005-09-14 04:36:29 +02:00
|
|
|
break;
|
|
|
|
case -2:
|
|
|
|
v0_err = ERR_INVALID_CONFIG_VALUE;
|
2005-12-11 10:33:38 +01:00
|
|
|
msg = "513 Unacceptable option value";
|
2005-09-14 04:36:29 +02:00
|
|
|
break;
|
|
|
|
case -3:
|
|
|
|
v0_err = ERR_INVALID_CONFIG_VALUE;
|
2005-11-17 23:01:46 +01:00
|
|
|
msg = "553 Transition not allowed";
|
2005-09-14 04:36:29 +02:00
|
|
|
break;
|
|
|
|
case -4:
|
|
|
|
default:
|
|
|
|
v0_err = ERR_INVALID_CONFIG_VALUE;
|
2005-11-17 23:01:46 +01:00
|
|
|
msg = "553 Unable to set option";
|
2005-09-14 04:36:29 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (v0) {
|
|
|
|
send_control0_error(conn, v0_err, msg);
|
2004-11-07 23:58:35 +01:00
|
|
|
} else {
|
2006-03-26 08:51:26 +02:00
|
|
|
connection_printf_to_buf(conn, "%s: %s\r\n", msg, errstring);
|
2004-11-07 23:58:35 +01:00
|
|
|
}
|
2004-11-06 06:18:11 +01:00
|
|
|
config_free_lines(lines);
|
2006-03-26 08:51:26 +02:00
|
|
|
tor_free(errstring);
|
2004-11-06 06:18:11 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2005-08-16 01:46:18 +02:00
|
|
|
config_free_lines(lines);
|
2004-11-06 06:18:11 +01:00
|
|
|
send_control_done(conn);
|
2004-11-03 02:32:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2004-11-03 19:33:07 +01:00
|
|
|
|
2005-09-08 05:18:51 +02:00
|
|
|
/** Called when we receive a SETCONF message: parse the body and try
|
|
|
|
* to update our configuration. Reply with a DONE or ERROR message. */
|
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
handle_control_setconf(control_connection_t *conn, uint32_t len, char *body)
|
2005-09-08 05:18:51 +02:00
|
|
|
{
|
2005-09-14 04:07:35 +02:00
|
|
|
return control_setconf_helper(conn, len, body, 0, 1);
|
2005-09-08 05:18:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Called when we receive a RESETCONF message: parse the body and try
|
|
|
|
* to update our configuration. Reply with a DONE or ERROR message. */
|
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
handle_control_resetconf(control_connection_t *conn, uint32_t len, char *body)
|
2005-09-08 05:18:51 +02:00
|
|
|
{
|
2006-07-26 21:07:26 +02:00
|
|
|
int v0 = STATE_IS_V0(conn->_base.state);
|
2005-09-08 05:18:51 +02:00
|
|
|
tor_assert(!v0);
|
2005-09-14 04:07:35 +02:00
|
|
|
return control_setconf_helper(conn, len, body, 1, 1);
|
2005-09-08 05:18:51 +02:00
|
|
|
}
|
|
|
|
|
2004-11-07 22:37:50 +01:00
|
|
|
/** Called when we receive a GETCONF message. Parse the request, and
|
|
|
|
* reply with a CONFVALUE or an ERROR message */
|
2004-11-04 23:31:50 +01:00
|
|
|
static int
|
2006-07-26 21:07:37 +02:00
|
|
|
handle_control_getconf(control_connection_t *conn, uint32_t body_len,
|
|
|
|
const char *body)
|
2004-11-03 02:32:26 +01:00
|
|
|
{
|
2004-11-04 23:31:50 +01:00
|
|
|
smartlist_t *questions = NULL;
|
|
|
|
smartlist_t *answers = NULL;
|
2005-06-17 22:37:21 +02:00
|
|
|
smartlist_t *unrecognized = NULL;
|
2004-11-03 19:33:07 +01:00
|
|
|
char *msg = NULL;
|
|
|
|
size_t msg_len;
|
2004-11-06 06:18:11 +01:00
|
|
|
or_options_t *options = get_options();
|
2006-07-26 21:07:26 +02:00
|
|
|
int v0 = STATE_IS_V0(conn->_base.state);
|
2004-11-03 19:33:07 +01:00
|
|
|
|
2004-11-04 23:31:50 +01:00
|
|
|
questions = smartlist_create();
|
2006-07-15 21:21:30 +02:00
|
|
|
(void) body_len; /* body is nul-terminated; so we can ignore len. */
|
2005-06-17 22:37:21 +02:00
|
|
|
if (v0) {
|
|
|
|
smartlist_split_string(questions, body, "\n",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
|
|
} else {
|
|
|
|
smartlist_split_string(questions, body, " ",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
|
|
}
|
2004-11-04 23:31:50 +01:00
|
|
|
answers = smartlist_create();
|
2005-06-17 22:37:21 +02:00
|
|
|
unrecognized = smartlist_create();
|
|
|
|
SMARTLIST_FOREACH(questions, char *, q,
|
2004-11-04 23:31:50 +01:00
|
|
|
{
|
2005-07-23 03:58:05 +02:00
|
|
|
if (!option_is_recognized(q)) {
|
2005-06-17 22:37:21 +02:00
|
|
|
if (v0) {
|
|
|
|
send_control0_error(conn, ERR_UNRECOGNIZED_CONFIG_KEY, q);
|
|
|
|
goto done;
|
|
|
|
} else {
|
|
|
|
smartlist_add(unrecognized, q);
|
|
|
|
}
|
2004-11-03 19:33:07 +01:00
|
|
|
} else {
|
2005-07-23 03:58:05 +02:00
|
|
|
config_line_t *answer = option_get_assignment(options,q);
|
2005-06-20 00:38:36 +02:00
|
|
|
if (!v0 && !answer) {
|
2005-07-23 03:58:05 +02:00
|
|
|
const char *name = option_get_canonical_name(q);
|
2005-07-11 20:11:54 +02:00
|
|
|
size_t alen = strlen(name)+8;
|
2005-06-20 00:38:36 +02:00
|
|
|
char *astr = tor_malloc(alen);
|
2005-07-11 20:11:54 +02:00
|
|
|
tor_snprintf(astr, alen, "250-%s\r\n", name);
|
2005-06-20 00:38:36 +02:00
|
|
|
smartlist_add(answers, astr);
|
|
|
|
}
|
2004-11-09 07:40:32 +01:00
|
|
|
|
2004-11-03 19:33:07 +01:00
|
|
|
while (answer) {
|
2005-07-22 23:12:10 +02:00
|
|
|
config_line_t *next;
|
2005-06-17 22:37:21 +02:00
|
|
|
size_t alen = strlen(answer->key)+strlen(answer->value)+8;
|
2004-11-04 23:31:50 +01:00
|
|
|
char *astr = tor_malloc(alen);
|
2005-06-17 22:37:21 +02:00
|
|
|
if (v0)
|
|
|
|
tor_snprintf(astr, alen, "%s %s\n", answer->key, answer->value);
|
|
|
|
else
|
2005-12-14 21:40:40 +01:00
|
|
|
tor_snprintf(astr, alen, "250-%s=%s\r\n",
|
|
|
|
answer->key, answer->value);
|
2004-11-04 23:31:50 +01:00
|
|
|
smartlist_add(answers, astr);
|
|
|
|
|
2004-11-03 19:33:07 +01:00
|
|
|
next = answer->next;
|
2004-11-04 23:31:50 +01:00
|
|
|
tor_free(answer->key);
|
|
|
|
tor_free(answer->value);
|
2004-11-03 19:33:07 +01:00
|
|
|
tor_free(answer);
|
|
|
|
answer = next;
|
|
|
|
}
|
|
|
|
}
|
2004-11-04 23:31:50 +01:00
|
|
|
});
|
2004-11-03 19:33:07 +01:00
|
|
|
|
2005-06-17 22:37:21 +02:00
|
|
|
if (v0) {
|
|
|
|
msg = smartlist_join_strings(answers, "", 0, &msg_len);
|
|
|
|
send_control0_message(conn, CONTROL0_CMD_CONFVALUE,
|
|
|
|
(uint16_t)msg_len, msg_len?msg:NULL);
|
|
|
|
} else {
|
|
|
|
int i,len;
|
|
|
|
if ((len = smartlist_len(unrecognized))) {
|
|
|
|
for (i=0; i < len-1; ++i)
|
|
|
|
connection_printf_to_buf(conn,
|
2005-12-14 21:40:40 +01:00
|
|
|
"552-Unrecognized configuration key \"%s\"\r\n",
|
|
|
|
(char*)smartlist_get(unrecognized, i));
|
2005-06-17 22:37:21 +02:00
|
|
|
connection_printf_to_buf(conn,
|
|
|
|
"552 Unrecognized configuration key \"%s\"\r\n",
|
|
|
|
(char*)smartlist_get(unrecognized, len-1));
|
|
|
|
} else if ((len = smartlist_len(answers))) {
|
|
|
|
char *tmp = smartlist_get(answers, len-1);
|
|
|
|
tor_assert(strlen(tmp)>4);
|
|
|
|
tmp[3] = ' ';
|
|
|
|
msg = smartlist_join_strings(answers, "", 0, &msg_len);
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_write_to_buf(msg, msg_len, TO_CONN(conn));
|
2005-06-17 22:37:21 +02:00
|
|
|
} else {
|
|
|
|
connection_write_str_to_buf("250 OK\r\n", conn);
|
|
|
|
}
|
|
|
|
}
|
2004-11-03 19:33:07 +01:00
|
|
|
|
|
|
|
done:
|
2005-10-25 09:02:13 +02:00
|
|
|
if (answers) {
|
|
|
|
SMARTLIST_FOREACH(answers, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(answers);
|
|
|
|
}
|
|
|
|
if (questions) {
|
|
|
|
SMARTLIST_FOREACH(questions, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(questions);
|
|
|
|
}
|
2005-06-17 22:37:21 +02:00
|
|
|
smartlist_free(unrecognized);
|
2004-11-03 19:33:07 +01:00
|
|
|
tor_free(msg);
|
|
|
|
|
2004-11-03 02:32:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2004-11-04 23:31:50 +01:00
|
|
|
|
2004-11-07 22:37:50 +01:00
|
|
|
/** Called when we get a SETEVENTS message: update conn->event_mask,
|
|
|
|
* and reply with DONE or ERROR. */
|
|
|
|
static int
|
2006-07-26 21:07:37 +02:00
|
|
|
handle_control_setevents(control_connection_t *conn, uint32_t len,
|
|
|
|
const char *body)
|
2004-11-03 02:32:26 +01:00
|
|
|
{
|
|
|
|
uint16_t event_code;
|
|
|
|
uint32_t event_mask = 0;
|
2005-10-12 06:31:44 +02:00
|
|
|
unsigned int extended = 0;
|
2004-11-03 02:32:26 +01:00
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
if (STATE_IS_V0(conn->_base.state)) {
|
2005-06-17 22:37:21 +02:00
|
|
|
if (len % 2) {
|
|
|
|
send_control0_error(conn, ERR_SYNTAX,
|
|
|
|
"Odd number of bytes in setevents message");
|
2004-11-03 02:32:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-17 22:37:21 +02:00
|
|
|
for (; len; len -= 2, body += 2) {
|
|
|
|
event_code = ntohs(get_uint16(body));
|
2005-09-22 01:30:15 +02:00
|
|
|
if (event_code < _EVENT_MIN || event_code > LAST_V0_EVENT) {
|
2005-06-17 22:37:21 +02:00
|
|
|
send_control0_error(conn, ERR_UNRECOGNIZED_EVENT_CODE,
|
|
|
|
"Unrecognized event code");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
event_mask |= (1 << event_code);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
smartlist_t *events = smartlist_create();
|
|
|
|
smartlist_split_string(events, body, " ",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
|
|
SMARTLIST_FOREACH(events, const char *, ev,
|
|
|
|
{
|
2005-10-12 06:31:44 +02:00
|
|
|
if (!strcasecmp(ev, "EXTENDED")) {
|
|
|
|
extended = 1;
|
|
|
|
continue;
|
|
|
|
} else if (!strcasecmp(ev, "CIRC"))
|
2005-06-17 22:37:21 +02:00
|
|
|
event_code = EVENT_CIRCUIT_STATUS;
|
|
|
|
else if (!strcasecmp(ev, "STREAM"))
|
|
|
|
event_code = EVENT_STREAM_STATUS;
|
|
|
|
else if (!strcasecmp(ev, "ORCONN"))
|
|
|
|
event_code = EVENT_OR_CONN_STATUS;
|
|
|
|
else if (!strcasecmp(ev, "BW"))
|
|
|
|
event_code = EVENT_BANDWIDTH_USED;
|
|
|
|
else if (!strcasecmp(ev, "DEBUG"))
|
2005-07-13 07:14:42 +02:00
|
|
|
event_code = EVENT_DEBUG_MSG;
|
2005-06-17 22:37:21 +02:00
|
|
|
else if (!strcasecmp(ev, "INFO"))
|
|
|
|
event_code = EVENT_INFO_MSG;
|
|
|
|
else if (!strcasecmp(ev, "NOTICE"))
|
|
|
|
event_code = EVENT_NOTICE_MSG;
|
|
|
|
else if (!strcasecmp(ev, "WARN"))
|
|
|
|
event_code = EVENT_WARN_MSG;
|
|
|
|
else if (!strcasecmp(ev, "ERR"))
|
|
|
|
event_code = EVENT_ERR_MSG;
|
|
|
|
else if (!strcasecmp(ev, "NEWDESC"))
|
|
|
|
event_code = EVENT_NEW_DESC;
|
2005-06-19 22:40:41 +02:00
|
|
|
else if (!strcasecmp(ev, "ADDRMAP"))
|
|
|
|
event_code = EVENT_ADDRMAP;
|
2005-11-19 19:35:43 +01:00
|
|
|
else if (!strcasecmp(ev, "AUTHDIR_NEWDESCS"))
|
|
|
|
event_code = EVENT_AUTHDIR_NEWDESCS;
|
2006-09-29 20:13:25 +02:00
|
|
|
else if (!strcasecmp(ev, "DESCCHANGED"))
|
|
|
|
event_code = EVENT_DESCCHANGED;
|
2006-10-20 23:04:39 +02:00
|
|
|
else if (!strcasecmp(ev, "NS"))
|
|
|
|
event_code = EVENT_NS;
|
2006-10-23 07:24:57 +02:00
|
|
|
else if (!strcasecmp(ev, "STATUS_GENERAL"))
|
|
|
|
event_code = EVENT_STATUS_GENERAL;
|
|
|
|
else if (!strcasecmp(ev, "STATUS_CLIENT"))
|
|
|
|
event_code = EVENT_STATUS_CLIENT;
|
|
|
|
else if (!strcasecmp(ev, "STATUS_SERVER"))
|
|
|
|
event_code = EVENT_STATUS_SERVER;
|
2006-12-15 23:40:20 +01:00
|
|
|
else if (!strcasecmp(ev, "GUARD"))
|
|
|
|
event_code = EVENT_GUARD;
|
2005-06-17 22:37:21 +02:00
|
|
|
else {
|
|
|
|
connection_printf_to_buf(conn, "552 Unrecognized event \"%s\"\r\n",
|
|
|
|
ev);
|
|
|
|
SMARTLIST_FOREACH(events, char *, e, tor_free(e));
|
|
|
|
smartlist_free(events);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
event_mask |= (1 << event_code);
|
|
|
|
});
|
|
|
|
SMARTLIST_FOREACH(events, char *, e, tor_free(e));
|
|
|
|
smartlist_free(events);
|
|
|
|
}
|
2004-11-03 02:32:26 +01:00
|
|
|
conn->event_mask = event_mask;
|
2006-10-09 17:47:50 +02:00
|
|
|
if (extended)
|
|
|
|
conn->use_extended_events = 1;
|
2004-11-03 02:32:26 +01:00
|
|
|
|
2005-08-07 21:20:55 +02:00
|
|
|
control_update_global_event_mask();
|
2004-11-03 02:32:26 +01:00
|
|
|
send_control_done(conn);
|
|
|
|
return 0;
|
|
|
|
}
|
2004-11-04 23:31:50 +01:00
|
|
|
|
2004-12-13 19:32:29 +01:00
|
|
|
/** Decode the hashed, base64'd password stored in <b>hashed</b>. If
|
|
|
|
* <b>buf</b> is provided, store the hashed password in the first
|
|
|
|
* S2K_SPECIFIER_LEN+DIGEST_LEN bytes of <b>buf</b>. Return 0 on
|
|
|
|
* success, -1 on failure.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
decode_hashed_password(char *buf, const char *hashed)
|
|
|
|
{
|
|
|
|
char decoded[64];
|
2005-05-23 04:31:53 +02:00
|
|
|
if (!strcmpstart(hashed, "16:")) {
|
|
|
|
if (base16_decode(decoded, sizeof(decoded), hashed+3, strlen(hashed+3))<0
|
|
|
|
|| strlen(hashed+3) != (S2K_SPECIFIER_LEN+DIGEST_LEN)*2) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (base64_decode(decoded, sizeof(decoded), hashed, strlen(hashed))
|
|
|
|
!= S2K_SPECIFIER_LEN+DIGEST_LEN) {
|
|
|
|
return -1;
|
|
|
|
}
|
2004-12-13 19:32:29 +01:00
|
|
|
}
|
|
|
|
if (buf)
|
2005-06-05 16:28:47 +02:00
|
|
|
memcpy(buf, decoded, S2K_SPECIFIER_LEN+DIGEST_LEN);
|
2004-12-13 19:32:29 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-07 22:37:50 +01:00
|
|
|
/** Called when we get an AUTHENTICATE message. Check whether the
|
|
|
|
* authentication is valid, and if so, update the connection's state to
|
|
|
|
* OPEN. Reply with DONE or ERROR.
|
|
|
|
*/
|
|
|
|
static int
|
2006-07-26 21:07:37 +02:00
|
|
|
handle_control_authenticate(control_connection_t *conn, uint32_t len,
|
|
|
|
const char *body)
|
2004-11-03 02:32:26 +01:00
|
|
|
{
|
2005-10-12 21:45:35 +02:00
|
|
|
int used_quoted_string = 0;
|
2004-11-06 06:18:11 +01:00
|
|
|
or_options_t *options = get_options();
|
2006-10-02 20:08:46 +02:00
|
|
|
const char *errstr = NULL;
|
2005-06-18 05:50:08 +02:00
|
|
|
char *password;
|
|
|
|
size_t password_len;
|
2006-07-26 21:07:26 +02:00
|
|
|
if (STATE_IS_V0(conn->_base.state)) {
|
2005-06-18 05:50:08 +02:00
|
|
|
password = (char*)body;
|
|
|
|
password_len = len;
|
|
|
|
} else {
|
|
|
|
if (TOR_ISXDIGIT(body[0])) {
|
|
|
|
int i = 0;
|
|
|
|
while (TOR_ISXDIGIT(body[i]))
|
|
|
|
++i;
|
|
|
|
password = tor_malloc(i/2 + 1);
|
|
|
|
if (base16_decode(password, i/2+1, body, i)<0) {
|
2005-12-09 06:37:26 +01:00
|
|
|
connection_write_str_to_buf(
|
|
|
|
"551 Invalid hexadecimal encoding. Maybe you tried a plain text "
|
2006-10-07 11:13:30 +02:00
|
|
|
"password? If so, the standard requires that you put it in "
|
|
|
|
"double quotes.\r\n", conn);
|
2005-06-18 05:50:08 +02:00
|
|
|
tor_free(password);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
password_len = i/2;
|
|
|
|
} else if (TOR_ISSPACE(body[0])) {
|
|
|
|
password = tor_strdup("");
|
|
|
|
password_len = 0;
|
|
|
|
} else {
|
2005-06-19 22:40:41 +02:00
|
|
|
if (!get_escaped_string(body, len, &password, &password_len)) {
|
2005-12-14 21:40:40 +01:00
|
|
|
connection_write_str_to_buf("551 Invalid quoted string. You need "
|
|
|
|
"to put the password in double quotes.\r\n", conn);
|
2005-06-18 05:50:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2005-10-12 21:45:35 +02:00
|
|
|
used_quoted_string = 1;
|
2005-06-18 05:50:08 +02:00
|
|
|
}
|
|
|
|
}
|
2006-11-14 02:07:52 +01:00
|
|
|
if (options->CookieAuthentication && authentication_cookie_is_set) {
|
2006-10-02 20:08:46 +02:00
|
|
|
if (password_len != AUTHENTICATION_COOKIE_LEN) {
|
|
|
|
log_warn(LD_CONTROL, "Got authentication cookie with wrong length (%d)",
|
2006-10-07 00:37:10 +02:00
|
|
|
(int)password_len);
|
2006-10-02 20:08:46 +02:00
|
|
|
errstr = "Wrong length on authentication cookie.";
|
|
|
|
goto err;
|
|
|
|
} else if (memcmp(authentication_cookie, password, password_len)) {
|
|
|
|
log_warn(LD_CONTROL, "Got mismatched authentication cookie");
|
|
|
|
errstr = "Authentication cookie did not match expected value.";
|
|
|
|
goto err;
|
|
|
|
} else {
|
2004-11-12 17:39:03 +01:00
|
|
|
goto ok;
|
|
|
|
}
|
|
|
|
} else if (options->HashedControlPassword) {
|
2004-11-03 20:49:03 +01:00
|
|
|
char expected[S2K_SPECIFIER_LEN+DIGEST_LEN];
|
|
|
|
char received[DIGEST_LEN];
|
2004-12-13 19:32:29 +01:00
|
|
|
if (decode_hashed_password(expected, options->HashedControlPassword)<0) {
|
2006-02-13 10:02:35 +01:00
|
|
|
log_warn(LD_CONTROL,
|
|
|
|
"Couldn't decode HashedControlPassword: invalid base16");
|
2006-10-02 20:08:46 +02:00
|
|
|
errstr = "Couldn't decode HashedControlPassword value in configuration.";
|
2004-11-03 20:49:03 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2005-06-18 05:50:08 +02:00
|
|
|
secret_to_key(received,DIGEST_LEN,password,password_len,expected);
|
2004-11-03 20:49:03 +01:00
|
|
|
if (!memcmp(expected+S2K_SPECIFIER_LEN, received, DIGEST_LEN))
|
|
|
|
goto ok;
|
2006-10-02 20:08:46 +02:00
|
|
|
|
|
|
|
if (used_quoted_string)
|
|
|
|
errstr = "Password did not match HashedControlPassword value from "
|
|
|
|
"configuration";
|
|
|
|
else
|
|
|
|
errstr = "Password did not match HashedControlPassword value from "
|
|
|
|
"configuration. Maybe you tried a plain text password? "
|
2006-10-07 11:13:30 +02:00
|
|
|
"If so, the standard requires that you put it in double quotes.";
|
2004-11-07 23:37:59 +01:00
|
|
|
goto err;
|
2004-11-12 17:39:03 +01:00
|
|
|
} else {
|
2005-06-17 22:37:21 +02:00
|
|
|
/* if Tor doesn't demand any stronger authentication, then
|
|
|
|
* the controller can get in with anything. */
|
|
|
|
goto ok;
|
2004-11-07 12:33:04 +01:00
|
|
|
}
|
2004-11-03 20:49:03 +01:00
|
|
|
|
|
|
|
err:
|
2006-07-26 21:07:26 +02:00
|
|
|
if (STATE_IS_V0(conn->_base.state))
|
2005-12-14 21:40:40 +01:00
|
|
|
send_control0_error(conn,ERR_REJECTED_AUTHENTICATION,
|
|
|
|
"Authentication failed");
|
2005-06-18 05:50:08 +02:00
|
|
|
else {
|
|
|
|
tor_free(password);
|
2006-10-02 20:08:46 +02:00
|
|
|
if (!errstr)
|
|
|
|
errstr = "Unknown reason.";
|
|
|
|
connection_printf_to_buf(conn, "515 Authentication failed: %s\r\n",
|
|
|
|
errstr);
|
2005-06-18 05:50:08 +02:00
|
|
|
}
|
2004-11-03 02:32:26 +01:00
|
|
|
return 0;
|
2004-11-03 20:49:03 +01:00
|
|
|
ok:
|
2006-07-26 21:07:26 +02:00
|
|
|
log_info(LD_CONTROL, "Authenticated control connection (%d)", conn->_base.s);
|
2004-11-03 20:49:03 +01:00
|
|
|
send_control_done(conn);
|
2006-07-26 21:07:26 +02:00
|
|
|
if (STATE_IS_V0(conn->_base.state))
|
|
|
|
conn->_base.state = CONTROL_CONN_STATE_OPEN_V0;
|
2005-06-18 05:50:08 +02:00
|
|
|
else {
|
2006-07-26 21:07:26 +02:00
|
|
|
conn->_base.state = CONTROL_CONN_STATE_OPEN_V1;
|
2005-06-18 05:50:08 +02:00
|
|
|
tor_free(password);
|
|
|
|
}
|
2004-11-03 20:49:03 +01:00
|
|
|
return 0;
|
2004-11-03 02:32:26 +01:00
|
|
|
}
|
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Called when we get a SAVECONF command. Try to flush the current options to
|
|
|
|
* disk, and report success or failure. */
|
2004-11-07 23:37:59 +01:00
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
handle_control_saveconf(control_connection_t *conn, uint32_t len,
|
2004-11-07 23:37:59 +01:00
|
|
|
const char *body)
|
|
|
|
{
|
2006-06-05 00:42:13 +02:00
|
|
|
(void) len;
|
|
|
|
(void) body;
|
2005-07-23 03:58:05 +02:00
|
|
|
if (options_save_current()<0) {
|
2006-07-26 21:07:26 +02:00
|
|
|
if (STATE_IS_V0(conn->_base.state))
|
2005-06-17 22:37:21 +02:00
|
|
|
send_control0_error(conn, ERR_INTERNAL,
|
|
|
|
"Unable to write configuration to disk.");
|
|
|
|
else
|
2006-02-20 02:24:26 +01:00
|
|
|
connection_write_str_to_buf(
|
|
|
|
"551 Unable to write configuration to disk.\r\n", conn);
|
2004-11-15 05:02:59 +01:00
|
|
|
} else {
|
|
|
|
send_control_done(conn);
|
2004-11-14 21:51:28 +01:00
|
|
|
}
|
2004-11-07 23:37:59 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Called when we get a SIGNAL command. React to the provided signal, and
|
|
|
|
* report success or failure. (If the signal results in a shutdown, success
|
|
|
|
* may not be reported.) */
|
2005-01-05 07:40:47 +01:00
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
handle_control_signal(control_connection_t *conn, uint32_t len,
|
2005-01-05 07:40:47 +01:00
|
|
|
const char *body)
|
|
|
|
{
|
2005-06-17 22:37:21 +02:00
|
|
|
int sig;
|
2006-07-26 21:07:26 +02:00
|
|
|
if (STATE_IS_V0(conn->_base.state)) {
|
2005-06-17 22:37:21 +02:00
|
|
|
if (len != 1) {
|
|
|
|
send_control0_error(conn, ERR_SYNTAX,
|
|
|
|
"Body of SIGNAL command too long or too short.");
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
sig = (uint8_t)body[0];
|
2006-10-25 23:39:42 +02:00
|
|
|
switch (sig)
|
|
|
|
{
|
|
|
|
case 1: sig = SIGHUP; break;
|
|
|
|
case 2: sig = SIGINT; break;
|
|
|
|
case 10: sig = SIGUSR1; break;
|
|
|
|
case 12: sig = SIGUSR2; break;
|
|
|
|
case 15: sig = SIGTERM; break;
|
|
|
|
case SIGNEWNYM: break;
|
|
|
|
default:
|
|
|
|
send_control0_error(conn, ERR_SYNTAX, "Unrecognized signal number.");
|
|
|
|
return 0;
|
|
|
|
}
|
2005-06-17 22:37:21 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int n = 0;
|
|
|
|
char *s;
|
|
|
|
while (body[n] && ! TOR_ISSPACE(body[n]))
|
|
|
|
++n;
|
|
|
|
s = tor_strndup(body, n);
|
|
|
|
if (!strcasecmp(s, "RELOAD") || !strcasecmp(s, "HUP"))
|
|
|
|
sig = SIGHUP;
|
|
|
|
else if (!strcasecmp(s, "SHUTDOWN") || !strcasecmp(s, "INT"))
|
|
|
|
sig = SIGINT;
|
|
|
|
else if (!strcasecmp(s, "DUMP") || !strcasecmp(s, "USR1"))
|
|
|
|
sig = SIGUSR1;
|
|
|
|
else if (!strcasecmp(s, "DEBUG") || !strcasecmp(s, "USR2"))
|
|
|
|
sig = SIGUSR2;
|
|
|
|
else if (!strcasecmp(s, "HALT") || !strcasecmp(s, "TERM"))
|
|
|
|
sig = SIGTERM;
|
2006-03-09 01:18:16 +01:00
|
|
|
else if (!strcasecmp(s, "NEWNYM"))
|
|
|
|
sig = SIGNEWNYM;
|
2006-10-25 23:46:21 +02:00
|
|
|
else if (!strcasecmp(s, "CLEARDNSCACHE"))
|
|
|
|
sig = SIGCLEARDNSCACHE;
|
2005-06-17 22:37:21 +02:00
|
|
|
else {
|
|
|
|
connection_printf_to_buf(conn, "552 Unrecognized signal code \"%s\"\r\n",
|
2006-01-19 03:21:24 +01:00
|
|
|
s);
|
2005-06-17 22:37:21 +02:00
|
|
|
sig = -1;
|
|
|
|
}
|
|
|
|
tor_free(s);
|
|
|
|
if (sig<0)
|
|
|
|
return 0;
|
|
|
|
}
|
2006-08-31 01:34:49 +02:00
|
|
|
|
2006-10-25 23:39:42 +02:00
|
|
|
/* Send DONE first, in case the signal makes us shut down. */
|
|
|
|
send_control_done(conn);
|
|
|
|
control_signal_act(sig);
|
2005-01-05 07:40:47 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Called when we get a MAPADDRESS command; try to bind all listed addresses,
|
|
|
|
* and report success or failrue. */
|
2005-02-25 07:16:28 +01:00
|
|
|
static int
|
2006-07-26 21:07:37 +02:00
|
|
|
handle_control_mapaddress(control_connection_t *conn, uint32_t len,
|
|
|
|
const char *body)
|
2005-02-25 07:16:28 +01:00
|
|
|
{
|
2005-02-25 21:46:13 +01:00
|
|
|
smartlist_t *elts;
|
|
|
|
smartlist_t *lines;
|
|
|
|
smartlist_t *reply;
|
|
|
|
char *r;
|
|
|
|
size_t sz;
|
2006-07-26 21:07:26 +02:00
|
|
|
int v0 = STATE_IS_V0(conn->_base.state);
|
2006-07-15 21:21:30 +02:00
|
|
|
(void) len; /* body is nul-terminated, so it's safe to ignore the length. */
|
2006-06-05 00:42:13 +02:00
|
|
|
|
2005-02-25 21:46:13 +01:00
|
|
|
lines = smartlist_create();
|
|
|
|
elts = smartlist_create();
|
|
|
|
reply = smartlist_create();
|
2005-06-19 22:40:41 +02:00
|
|
|
if (v0)
|
|
|
|
smartlist_split_string(lines, body, "\n",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
|
|
else
|
|
|
|
smartlist_split_string(lines, body, " ",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
2005-03-10 07:15:46 +01:00
|
|
|
SMARTLIST_FOREACH(lines, char *, line,
|
2005-02-25 21:46:13 +01:00
|
|
|
{
|
2005-03-03 07:37:54 +01:00
|
|
|
tor_strlower(line);
|
2005-06-19 22:40:41 +02:00
|
|
|
if (v0)
|
|
|
|
smartlist_split_string(elts, line, " ", 0, 2);
|
|
|
|
else
|
|
|
|
smartlist_split_string(elts, line, "=", 0, 2);
|
2005-02-25 21:46:13 +01:00
|
|
|
if (smartlist_len(elts) == 2) {
|
|
|
|
const char *from = smartlist_get(elts,0);
|
|
|
|
const char *to = smartlist_get(elts,1);
|
2006-04-01 11:50:43 +02:00
|
|
|
size_t anslen = strlen(line)+512;
|
|
|
|
char *ans = tor_malloc(anslen);
|
2006-12-29 06:51:50 +01:00
|
|
|
if (address_is_invalid_destination(to)) {
|
2006-04-01 11:50:43 +02:00
|
|
|
if (!v0) {
|
|
|
|
tor_snprintf(ans, anslen,
|
|
|
|
"512-syntax error: invalid address '%s'", to);
|
|
|
|
smartlist_add(reply, ans);
|
|
|
|
} else
|
|
|
|
tor_free(ans); /* don't respond if v0 */
|
2006-02-13 10:02:35 +01:00
|
|
|
log_warn(LD_CONTROL,
|
|
|
|
"Skipping invalid argument '%s' in MapAddress msg", to);
|
2005-03-02 20:26:46 +01:00
|
|
|
} else if (!strcmp(from, ".") || !strcmp(from, "0.0.0.0")) {
|
2005-10-05 04:06:36 +02:00
|
|
|
const char *address = addressmap_register_virtual_address(
|
2005-06-19 22:40:41 +02:00
|
|
|
!strcmp(from,".") ? RESOLVED_TYPE_HOSTNAME : RESOLVED_TYPE_IPV4,
|
2005-03-02 22:02:11 +01:00
|
|
|
tor_strdup(to));
|
2005-10-05 04:06:36 +02:00
|
|
|
if (!address) {
|
2006-04-01 11:50:43 +02:00
|
|
|
if (!v0) {
|
|
|
|
tor_snprintf(ans, anslen,
|
|
|
|
"451-resource exhausted: skipping '%s'", line);
|
|
|
|
smartlist_add(reply, ans);
|
|
|
|
} else
|
|
|
|
tor_free(ans); /* don't respond if v0 */
|
2006-02-13 10:02:35 +01:00
|
|
|
log_warn(LD_CONTROL,
|
|
|
|
"Unable to allocate address for '%s' in MapAddress msg",
|
|
|
|
safe_str(line));
|
2005-03-02 20:26:46 +01:00
|
|
|
} else {
|
2005-06-19 22:40:41 +02:00
|
|
|
if (v0)
|
2005-10-05 04:06:36 +02:00
|
|
|
tor_snprintf(ans, anslen, "%s %s", address, to);
|
2005-06-19 22:40:41 +02:00
|
|
|
else
|
2005-10-05 04:06:36 +02:00
|
|
|
tor_snprintf(ans, anslen, "250-%s=%s", address, to);
|
2005-03-02 20:26:46 +01:00
|
|
|
smartlist_add(reply, ans);
|
|
|
|
}
|
2005-02-25 21:46:13 +01:00
|
|
|
} else {
|
2005-03-03 07:37:54 +01:00
|
|
|
addressmap_register(from, tor_strdup(to), 1);
|
2005-06-19 22:40:41 +02:00
|
|
|
if (v0)
|
2006-04-01 11:50:43 +02:00
|
|
|
tor_snprintf(ans, anslen, "%s", line);
|
|
|
|
else
|
2005-06-19 22:40:41 +02:00
|
|
|
tor_snprintf(ans, anslen, "250-%s", line);
|
2006-04-01 11:50:43 +02:00
|
|
|
smartlist_add(reply, ans);
|
2005-02-25 21:46:13 +01:00
|
|
|
}
|
|
|
|
} else {
|
2006-04-01 11:50:43 +02:00
|
|
|
if (!v0) {
|
|
|
|
size_t anslen = strlen(line)+256;
|
|
|
|
char *ans = tor_malloc(anslen);
|
2006-04-01 11:54:54 +02:00
|
|
|
tor_snprintf(ans, anslen, "512-syntax error: mapping '%s' is "
|
|
|
|
"not of expected form 'foo=bar'.", line);
|
2006-04-01 11:50:43 +02:00
|
|
|
smartlist_add(reply, ans);
|
|
|
|
}
|
|
|
|
log_info(LD_CONTROL, "Skipping MapAddress '%s': wrong "
|
|
|
|
"number of items.", safe_str(line));
|
2005-02-25 21:46:13 +01:00
|
|
|
}
|
|
|
|
SMARTLIST_FOREACH(elts, char *, cp, tor_free(cp));
|
|
|
|
smartlist_clear(elts);
|
|
|
|
});
|
|
|
|
SMARTLIST_FOREACH(lines, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(lines);
|
|
|
|
smartlist_free(elts);
|
|
|
|
|
2005-06-19 22:40:41 +02:00
|
|
|
if (v0) {
|
|
|
|
r = smartlist_join_strings(reply, "\n", 1, &sz);
|
|
|
|
send_control_done2(conn,r,sz);
|
2006-04-03 00:13:34 +02:00
|
|
|
tor_free(r);
|
2005-06-19 22:40:41 +02:00
|
|
|
} else {
|
2006-04-03 00:13:34 +02:00
|
|
|
if (smartlist_len(reply)) {
|
2005-06-19 22:40:41 +02:00
|
|
|
((char*)smartlist_get(reply,smartlist_len(reply)-1))[3] = ' ';
|
2006-04-03 00:13:34 +02:00
|
|
|
r = smartlist_join_strings(reply, "\r\n", 1, &sz);
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_write_to_buf(r, sz, TO_CONN(conn));
|
2006-04-03 00:13:34 +02:00
|
|
|
tor_free(r);
|
|
|
|
} else {
|
|
|
|
const char *response =
|
2006-04-03 00:19:01 +02:00
|
|
|
"512 syntax error: not enough arguments to mapaddress.\r\n";
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_write_to_buf(response, strlen(response), TO_CONN(conn));
|
2006-04-03 00:13:34 +02:00
|
|
|
}
|
2005-06-19 22:40:41 +02:00
|
|
|
}
|
2005-02-25 21:46:13 +01:00
|
|
|
|
|
|
|
SMARTLIST_FOREACH(reply, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(reply);
|
2005-02-25 07:16:28 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2005-02-25 07:37:07 +01:00
|
|
|
|
2005-03-22 04:27:51 +01:00
|
|
|
static int
|
2006-12-08 05:39:13 +01:00
|
|
|
getinfo_helper_misc(control_connection_t *conn, const char *question,
|
|
|
|
char **answer)
|
2005-02-25 07:37:07 +01:00
|
|
|
{
|
2006-12-08 05:39:13 +01:00
|
|
|
(void) conn;
|
2005-02-25 07:37:07 +01:00
|
|
|
if (!strcmp(question, "version")) {
|
2005-03-22 04:27:51 +01:00
|
|
|
*answer = tor_strdup(VERSION);
|
2005-08-10 20:05:20 +02:00
|
|
|
} else if (!strcmp(question, "config-file")) {
|
|
|
|
*answer = tor_strdup(get_torrc_fname());
|
2005-08-04 21:56:41 +02:00
|
|
|
} else if (!strcmp(question, "info/names")) {
|
|
|
|
*answer = list_getinfo_options();
|
2006-10-24 04:58:03 +02:00
|
|
|
} else if (!strcmp(question, "events/names")) {
|
|
|
|
*answer = tor_strdup("CIRC STREAM ORCONN BW DEBUG INFO NOTICE WARN ERR "
|
|
|
|
"NEWDESC ADDRMAP AUTHDIR_NEWDESCS DESCCHANGED "
|
|
|
|
"NS STATUS_GENERAL STATUS_CLIENT STATUS_SERVER");
|
|
|
|
} else if (!strcmp(question, "features/names")) {
|
|
|
|
*answer = tor_strdup("VERBOSE_NAMES EXTENDED_EVENTS");
|
2006-12-08 05:39:13 +01:00
|
|
|
} else if (!strcmp(question, "address")) {
|
|
|
|
uint32_t addr;
|
|
|
|
if (router_pick_published_address(get_options(), &addr) < 0)
|
|
|
|
return -1;
|
|
|
|
*answer = tor_dup_addr(addr);
|
|
|
|
} else if (!strcmp(question, "dir-usage")) {
|
|
|
|
*answer = directory_dump_request_log();
|
2006-12-15 22:51:24 +01:00
|
|
|
} else if (!strcmp(question, "fingerprint")) {
|
|
|
|
routerinfo_t *me = router_get_my_routerinfo();
|
|
|
|
if (!me) {
|
|
|
|
*answer = tor_strdup("");
|
|
|
|
} else {
|
|
|
|
*answer = tor_malloc(HEX_DIGEST_LEN+1);
|
|
|
|
base16_encode(*answer, HEX_DIGEST_LEN+1, me->cache_info.identity_digest,
|
|
|
|
DIGEST_LEN);
|
|
|
|
}
|
2006-12-08 05:39:13 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
getinfo_helper_dir(control_connection_t *control_conn,
|
|
|
|
const char *question, char **answer)
|
|
|
|
{
|
|
|
|
if (!strcmpstart(question, "desc/id/")) {
|
2005-02-25 21:46:13 +01:00
|
|
|
routerinfo_t *ri = router_get_by_hexdigest(question+strlen("desc/id/"));
|
2006-01-12 19:04:17 +01:00
|
|
|
if (ri) {
|
|
|
|
const char *body = signed_descriptor_get_body(&ri->cache_info);
|
|
|
|
if (body)
|
2006-06-22 09:01:54 +02:00
|
|
|
*answer = tor_strndup(body, ri->cache_info.signed_descriptor_len);
|
2006-01-12 19:04:17 +01:00
|
|
|
}
|
2005-03-23 09:40:11 +01:00
|
|
|
} else if (!strcmpstart(question, "desc/name/")) {
|
2005-10-05 00:23:31 +02:00
|
|
|
routerinfo_t *ri = router_get_by_nickname(question+strlen("desc/name/"),1);
|
2006-01-12 19:04:17 +01:00
|
|
|
if (ri) {
|
|
|
|
const char *body = signed_descriptor_get_body(&ri->cache_info);
|
|
|
|
if (body)
|
2006-06-22 09:01:54 +02:00
|
|
|
*answer = tor_strndup(body, ri->cache_info.signed_descriptor_len);
|
2006-01-12 19:04:17 +01:00
|
|
|
}
|
2006-01-10 21:09:31 +01:00
|
|
|
} else if (!strcmp(question, "desc/all-recent")) {
|
|
|
|
routerlist_t *routerlist = router_get_routerlist();
|
|
|
|
smartlist_t *sl = smartlist_create();
|
|
|
|
if (routerlist && routerlist->routers) {
|
|
|
|
SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, ri,
|
|
|
|
{
|
2006-01-12 19:04:17 +01:00
|
|
|
const char *body = signed_descriptor_get_body(&ri->cache_info);
|
|
|
|
if (body)
|
2006-06-22 09:01:54 +02:00
|
|
|
smartlist_add(sl,
|
|
|
|
tor_strndup(body, ri->cache_info.signed_descriptor_len));
|
2006-01-10 21:09:31 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
*answer = smartlist_join_strings(sl, "", 0, NULL);
|
|
|
|
SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
|
|
|
|
smartlist_free(sl);
|
2006-12-08 05:39:13 +01:00
|
|
|
} else if (!strcmpstart(question, "dir/server/")) {
|
|
|
|
size_t answer_len = 0, url_len = strlen(question)+2;
|
|
|
|
char *url = tor_malloc(url_len);
|
|
|
|
smartlist_t *descs = smartlist_create();
|
|
|
|
const char *msg;
|
|
|
|
int res;
|
|
|
|
char *cp;
|
|
|
|
tor_snprintf(url, url_len, "/tor/%s", question+4);
|
|
|
|
res = dirserv_get_routerdescs(descs, url, &msg);
|
|
|
|
if (res) {
|
|
|
|
log_warn(LD_CONTROL, "getinfo '%s': %s", question, msg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
SMARTLIST_FOREACH(descs, signed_descriptor_t *, sd,
|
|
|
|
answer_len += sd->signed_descriptor_len);
|
|
|
|
cp = *answer = tor_malloc(answer_len+1);
|
|
|
|
SMARTLIST_FOREACH(descs, signed_descriptor_t *, sd,
|
|
|
|
{
|
|
|
|
memcpy(cp, signed_descriptor_get_body(sd),
|
|
|
|
sd->signed_descriptor_len);
|
|
|
|
cp += sd->signed_descriptor_len;
|
|
|
|
});
|
|
|
|
*cp = '\0';
|
|
|
|
tor_free(url);
|
|
|
|
smartlist_free(descs);
|
|
|
|
} else if (!strcmpstart(question, "dir/status/")) {
|
|
|
|
if (get_options()->DirPort) {
|
2006-12-10 05:50:33 +01:00
|
|
|
size_t len=0;
|
|
|
|
char *cp;
|
2006-12-08 05:39:13 +01:00
|
|
|
smartlist_t *status_list = smartlist_create();
|
|
|
|
dirserv_get_networkstatus_v2(status_list,
|
|
|
|
question+strlen("dir/status/"));
|
|
|
|
SMARTLIST_FOREACH(status_list, cached_dir_t *, d, len += d->dir_len);
|
|
|
|
cp = *answer = tor_malloc(len+1);
|
|
|
|
SMARTLIST_FOREACH(status_list, cached_dir_t *, d, {
|
|
|
|
memcpy(cp, d->dir, d->dir_len);
|
|
|
|
cp += d->dir_len;
|
|
|
|
});
|
|
|
|
*cp = '\0';
|
|
|
|
smartlist_free(status_list);
|
|
|
|
} else {
|
|
|
|
smartlist_t *fp_list = smartlist_create();
|
|
|
|
smartlist_t *status_list = smartlist_create();
|
|
|
|
size_t fn_len = strlen(get_options()->DataDirectory)+HEX_DIGEST_LEN+32;
|
|
|
|
char *fn = tor_malloc(fn_len+1);
|
|
|
|
char hex_id[HEX_DIGEST_LEN+1];
|
|
|
|
dirserv_get_networkstatus_v2_fingerprints(
|
|
|
|
fp_list, question+strlen("dir/status/"));
|
|
|
|
SMARTLIST_FOREACH(fp_list, const char *, fp, {
|
|
|
|
char *s;
|
|
|
|
base16_encode(hex_id, sizeof(hex_id), fp, DIGEST_LEN);
|
|
|
|
tor_snprintf(fn, fn_len, "%s/cached-status/%s",
|
|
|
|
get_options()->DataDirectory, hex_id);
|
|
|
|
s = read_file_to_str(fn, 0, NULL);
|
|
|
|
if (s)
|
|
|
|
smartlist_add(status_list, s);
|
|
|
|
});
|
|
|
|
SMARTLIST_FOREACH(fp_list, char *, fp, tor_free(fp));
|
|
|
|
smartlist_free(fp_list);
|
|
|
|
*answer = smartlist_join_strings(status_list, "", 0, NULL);
|
|
|
|
SMARTLIST_FOREACH(status_list, char *, s, tor_free(s));
|
|
|
|
smartlist_free(status_list);
|
|
|
|
}
|
2005-03-22 04:27:51 +01:00
|
|
|
} else if (!strcmp(question, "network-status")) {
|
2005-10-18 19:43:54 +02:00
|
|
|
routerlist_t *routerlist = router_get_routerlist();
|
2006-10-27 04:07:04 +02:00
|
|
|
int verbose = control_conn->use_long_names;
|
2005-03-23 20:15:10 +01:00
|
|
|
if (!routerlist || !routerlist->routers ||
|
2006-10-27 04:07:04 +02:00
|
|
|
list_server_status(routerlist->routers, answer, verbose ? 2 : 1) < 0) {
|
2005-03-22 04:27:51 +01:00
|
|
|
return -1;
|
2005-03-23 20:15:10 +01:00
|
|
|
}
|
2006-12-08 05:39:13 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
getinfo_helper_events(control_connection_t *control_conn,
|
|
|
|
const char *question, char **answer)
|
|
|
|
{
|
|
|
|
if (!strcmp(question, "circuit-status")) {
|
2005-06-19 22:40:41 +02:00
|
|
|
circuit_t *circ;
|
|
|
|
smartlist_t *status = smartlist_create();
|
|
|
|
for (circ = _circuit_get_global_list(); circ; circ = circ->next) {
|
|
|
|
char *s, *path;
|
|
|
|
size_t slen;
|
|
|
|
const char *state;
|
|
|
|
if (! CIRCUIT_IS_ORIGIN(circ) || circ->marked_for_close)
|
|
|
|
continue;
|
2006-10-03 21:00:18 +02:00
|
|
|
if (control_conn->use_long_names)
|
|
|
|
path = circuit_list_path_for_controller(TO_ORIGIN_CIRCUIT(circ));
|
|
|
|
else
|
|
|
|
path = circuit_list_path(TO_ORIGIN_CIRCUIT(circ),0);
|
2005-06-19 22:40:41 +02:00
|
|
|
if (circ->state == CIRCUIT_STATE_OPEN)
|
|
|
|
state = "BUILT";
|
|
|
|
else if (strlen(path))
|
|
|
|
state = "EXTENDED";
|
|
|
|
else
|
|
|
|
state = "LAUNCHED";
|
|
|
|
|
|
|
|
slen = strlen(path)+strlen(state)+20;
|
|
|
|
s = tor_malloc(slen+1);
|
2005-12-14 21:40:40 +01:00
|
|
|
tor_snprintf(s, slen, "%lu %s %s",
|
2006-07-28 17:11:11 +02:00
|
|
|
(unsigned long)TO_ORIGIN_CIRCUIT(circ)->global_identifier,
|
2005-06-19 22:40:41 +02:00
|
|
|
state, path);
|
|
|
|
smartlist_add(status, s);
|
|
|
|
tor_free(path);
|
|
|
|
}
|
2005-08-09 07:16:29 +02:00
|
|
|
*answer = smartlist_join_strings(status, "\r\n", 0, NULL);
|
2005-06-19 22:40:41 +02:00
|
|
|
SMARTLIST_FOREACH(status, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(status);
|
|
|
|
} else if (!strcmp(question, "stream-status")) {
|
|
|
|
connection_t **conns;
|
|
|
|
int n_conns, i;
|
|
|
|
char buf[256];
|
|
|
|
smartlist_t *status = smartlist_create();
|
|
|
|
get_connection_array(&conns, &n_conns);
|
|
|
|
for (i=0; i < n_conns; ++i) {
|
|
|
|
const char *state;
|
2006-07-26 21:07:26 +02:00
|
|
|
edge_connection_t *conn;
|
2005-06-19 22:40:41 +02:00
|
|
|
char *s;
|
|
|
|
size_t slen;
|
|
|
|
circuit_t *circ;
|
2006-07-28 17:11:11 +02:00
|
|
|
origin_circuit_t *origin_circ = NULL;
|
2005-06-19 22:40:41 +02:00
|
|
|
if (conns[i]->type != CONN_TYPE_AP ||
|
|
|
|
conns[i]->marked_for_close ||
|
2006-11-14 01:06:31 +01:00
|
|
|
conns[i]->state == AP_CONN_STATE_SOCKS_WAIT ||
|
|
|
|
conns[i]->state == AP_CONN_STATE_NATD_WAIT)
|
2005-06-19 22:40:41 +02:00
|
|
|
continue;
|
2006-07-26 21:07:26 +02:00
|
|
|
conn = TO_EDGE_CONN(conns[i]);
|
|
|
|
switch (conn->_base.state)
|
2005-06-19 22:40:41 +02:00
|
|
|
{
|
|
|
|
case AP_CONN_STATE_CONTROLLER_WAIT:
|
|
|
|
case AP_CONN_STATE_CIRCUIT_WAIT:
|
2006-07-26 21:07:26 +02:00
|
|
|
if (conn->socks_request &&
|
2006-12-13 01:28:56 +01:00
|
|
|
SOCKS_COMMAND_IS_RESOLVE(conn->socks_request->command))
|
2005-06-19 22:40:41 +02:00
|
|
|
state = "NEWRESOLVE";
|
|
|
|
else
|
|
|
|
state = "NEW";
|
|
|
|
break;
|
|
|
|
case AP_CONN_STATE_RENDDESC_WAIT:
|
|
|
|
case AP_CONN_STATE_CONNECT_WAIT:
|
|
|
|
state = "SENTCONNECT"; break;
|
|
|
|
case AP_CONN_STATE_RESOLVE_WAIT:
|
|
|
|
state = "SENTRESOLVE"; break;
|
|
|
|
case AP_CONN_STATE_OPEN:
|
|
|
|
state = "SUCCEEDED"; break;
|
|
|
|
default:
|
2006-02-13 10:02:35 +01:00
|
|
|
log_warn(LD_BUG, "Asked for stream in unknown state %d",
|
2006-07-26 21:07:26 +02:00
|
|
|
conn->_base.state);
|
2005-06-19 22:40:41 +02:00
|
|
|
continue;
|
|
|
|
}
|
2006-07-26 21:07:26 +02:00
|
|
|
circ = circuit_get_by_edge_conn(conn);
|
2006-09-12 04:50:14 +02:00
|
|
|
if (circ && CIRCUIT_IS_ORIGIN(circ))
|
2006-07-28 17:11:11 +02:00
|
|
|
origin_circ = TO_ORIGIN_CIRCUIT(circ);
|
2006-07-26 21:07:26 +02:00
|
|
|
write_stream_target_to_buf(conn, buf, sizeof(buf));
|
2005-06-19 22:40:41 +02:00
|
|
|
slen = strlen(buf)+strlen(state)+32;
|
|
|
|
s = tor_malloc(slen+1);
|
|
|
|
tor_snprintf(s, slen, "%lu %s %lu %s",
|
2006-07-28 17:11:11 +02:00
|
|
|
(unsigned long) conn->global_identifier,state,
|
|
|
|
origin_circ?
|
|
|
|
(unsigned long)origin_circ->global_identifier : 0ul,
|
2005-06-19 22:40:41 +02:00
|
|
|
buf);
|
|
|
|
smartlist_add(status, s);
|
|
|
|
}
|
2005-08-09 07:16:29 +02:00
|
|
|
*answer = smartlist_join_strings(status, "\r\n", 0, NULL);
|
2005-06-19 22:40:41 +02:00
|
|
|
SMARTLIST_FOREACH(status, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(status);
|
|
|
|
} else if (!strcmp(question, "orconn-status")) {
|
|
|
|
connection_t **conns;
|
|
|
|
int n_conns, i;
|
|
|
|
smartlist_t *status = smartlist_create();
|
|
|
|
get_connection_array(&conns, &n_conns);
|
|
|
|
for (i=0; i < n_conns; ++i) {
|
|
|
|
const char *state;
|
|
|
|
char *s;
|
2006-03-08 23:29:08 +01:00
|
|
|
char name[128];
|
2005-06-19 22:40:41 +02:00
|
|
|
size_t slen;
|
2006-07-26 21:07:26 +02:00
|
|
|
or_connection_t *conn;
|
|
|
|
if (conns[i]->type != CONN_TYPE_OR || conns[i]->marked_for_close)
|
2005-06-19 22:40:41 +02:00
|
|
|
continue;
|
2006-07-26 21:07:26 +02:00
|
|
|
conn = TO_OR_CONN(conns[i]);
|
|
|
|
if (conn->_base.state == OR_CONN_STATE_OPEN)
|
2005-06-19 22:40:41 +02:00
|
|
|
state = "CONNECTED";
|
2006-03-08 23:29:08 +01:00
|
|
|
else if (conn->nickname)
|
2005-06-19 22:40:41 +02:00
|
|
|
state = "LAUNCHED";
|
2006-03-08 23:29:08 +01:00
|
|
|
else
|
|
|
|
state = "NEW";
|
2006-10-03 21:00:18 +02:00
|
|
|
orconn_target_get_name(control_conn->use_long_names, name, sizeof(name),
|
|
|
|
conn);
|
2006-03-08 23:29:08 +01:00
|
|
|
slen = strlen(name)+strlen(state)+2;
|
2005-06-19 22:40:41 +02:00
|
|
|
s = tor_malloc(slen+1);
|
2006-03-08 23:29:08 +01:00
|
|
|
tor_snprintf(s, slen, "%s %s", name, state);
|
2005-06-19 22:40:41 +02:00
|
|
|
smartlist_add(status, s);
|
|
|
|
}
|
2005-08-09 07:16:29 +02:00
|
|
|
*answer = smartlist_join_strings(status, "\r\n", 0, NULL);
|
2005-06-19 22:40:41 +02:00
|
|
|
SMARTLIST_FOREACH(status, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(status);
|
2005-03-03 07:37:54 +01:00
|
|
|
} else if (!strcmpstart(question, "addr-mappings/")) {
|
|
|
|
time_t min_e, max_e;
|
|
|
|
smartlist_t *mappings;
|
|
|
|
if (!strcmp(question, "addr-mappings/all")) {
|
|
|
|
min_e = 0; max_e = TIME_MAX;
|
|
|
|
} else if (!strcmp(question, "addr-mappings/cache")) {
|
|
|
|
min_e = 2; max_e = TIME_MAX;
|
|
|
|
} else if (!strcmp(question, "addr-mappings/config")) {
|
|
|
|
min_e = 0; max_e = 0;
|
|
|
|
} else if (!strcmp(question, "addr-mappings/control")) {
|
|
|
|
min_e = 1; max_e = 1;
|
|
|
|
} else {
|
2005-03-22 04:27:51 +01:00
|
|
|
return 0;
|
2005-03-03 07:37:54 +01:00
|
|
|
}
|
|
|
|
mappings = smartlist_create();
|
|
|
|
addressmap_get_mappings(mappings, min_e, max_e);
|
2006-09-09 05:38:03 +02:00
|
|
|
*answer = smartlist_join_strings(mappings, "\r\n", 0, NULL);
|
2005-03-03 07:37:54 +01:00
|
|
|
SMARTLIST_FOREACH(mappings, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(mappings);
|
2006-12-08 05:39:13 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef int (*getinfo_helper_t)(control_connection_t *,
|
|
|
|
const char *q, char **a);
|
|
|
|
|
|
|
|
typedef struct getinfo_item_t {
|
|
|
|
const char *varname;
|
|
|
|
getinfo_helper_t fn;
|
|
|
|
const char *desc;
|
|
|
|
int is_prefix;
|
|
|
|
} getinfo_item_t;
|
|
|
|
|
|
|
|
#define ITEM(name, fn, desc) { name, getinfo_helper_##fn, desc, 0 }
|
|
|
|
#define PREFIX(name, fn, desc) { name, getinfo_helper_##fn, desc, 1 }
|
|
|
|
#define DOC(name, desc) { name, NULL, desc, 0 }
|
|
|
|
|
|
|
|
static const getinfo_item_t getinfo_items[] = {
|
|
|
|
ITEM("version", misc, "The current version of Tor."),
|
|
|
|
ITEM("config-file", misc, "Current location of the \"torrc\" file."),
|
|
|
|
ITEM("accounting/bytes", accounting,
|
|
|
|
"Number of bytes read/written so far in the accounting interval."),
|
|
|
|
ITEM("accounting/bytes-left", accounting,
|
|
|
|
"Number of bytes left to write/read so far in the accounting interval."),
|
|
|
|
ITEM("accounting/enabled", accounting, "Is accounting currently enabled?"),
|
|
|
|
ITEM("accounting/hibernating", accounting, "Are we hibernating or awake?"),
|
|
|
|
ITEM("accounting/interval-start", accounting,
|
|
|
|
"Time when the accounting period starts."),
|
|
|
|
ITEM("accounting/interval-end", accounting,
|
|
|
|
"Time when the accounting period ends."),
|
2006-12-10 09:04:50 +01:00
|
|
|
ITEM("accounting/interval-wake", accounting,
|
2006-12-08 05:39:13 +01:00
|
|
|
"Time to wake up in this accounting period."),
|
2006-12-15 22:51:24 +01:00
|
|
|
ITEM("helper-nodes", entry_guards, NULL), /* deprecated */
|
2006-12-10 18:30:33 +01:00
|
|
|
ITEM("entry-guards", entry_guards,
|
2006-12-08 05:39:13 +01:00
|
|
|
"Which nodes are we using as entry guards?"),
|
2006-12-15 22:51:24 +01:00
|
|
|
ITEM("fingerprint", misc, NULL),
|
2006-12-08 05:39:13 +01:00
|
|
|
PREFIX("config/", config, "Current configuration values."),
|
|
|
|
DOC("config/names",
|
|
|
|
"List of configuration options, types, and documentation."),
|
|
|
|
ITEM("info/names", misc,
|
|
|
|
"List of GETINFO options, types, and documentation."),
|
|
|
|
ITEM("events/names", misc,
|
|
|
|
"Events that the controller can ask for with SETEVENTS."),
|
|
|
|
ITEM("features/names", misc, "What arguments can USEFEATURE take?"),
|
|
|
|
PREFIX("desc/id/", dir, "Router descriptors by ID."),
|
|
|
|
PREFIX("desc/name/", dir, "Router descriptors by nickname."),
|
|
|
|
ITEM("desc/all-recent", dir,
|
|
|
|
"All non-expired, non-superseded router descriptors."),
|
2007-01-04 06:41:24 +01:00
|
|
|
ITEM("ns/all", networkstatus,
|
|
|
|
"Brief summary of router status (v2 directory format)"),
|
2006-12-08 05:39:13 +01:00
|
|
|
PREFIX("ns/id/", networkstatus,
|
|
|
|
"Brief summary of router status by ID (v2 directory format)."),
|
|
|
|
PREFIX("ns/name/", networkstatus,
|
|
|
|
"Brief summary of router status by nickname (v2 directory format)."),
|
|
|
|
|
|
|
|
PREFIX("unregisterd-servers-", dirserv_unregistered, NULL),
|
|
|
|
ITEM("network-status", dir,
|
|
|
|
"Brief summary of router status (v1 directory format)"),
|
|
|
|
ITEM("circuit-status", events, "List of current circuits originating here."),
|
|
|
|
ITEM("stream-status", events,"List of current streams."),
|
|
|
|
ITEM("orconn-status", events, "A list of current OR connections."),
|
|
|
|
PREFIX("addr-mappings/", events, NULL),
|
|
|
|
DOC("addr-mappings/all", "Current address mappings."),
|
|
|
|
DOC("addr-mappings/cache", "Current cached DNS replies."),
|
|
|
|
DOC("addr-mappings/config", "Current address mappings from configuration."),
|
|
|
|
DOC("addr-mappings/control", "Current address mappings from controller."),
|
|
|
|
|
|
|
|
ITEM("address", misc, "IP address of this Tor host, if we can guess it."),
|
|
|
|
ITEM("dir-usage", misc, "Breakdown of bytes transferred over DirPort."),
|
|
|
|
PREFIX("dir/server/", dir,"Router descriptors as retrieved from a DirPort."),
|
|
|
|
PREFIX("dir/status/", dir,"Networkstatus docs as retrieved from a DirPort."),
|
|
|
|
PREFIX("exit-policy/default", policies,
|
|
|
|
"The default value appended to the configured exit policy."),
|
|
|
|
|
|
|
|
{ NULL, NULL, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static char *
|
|
|
|
list_getinfo_options(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char buf[300];
|
|
|
|
smartlist_t *lines = smartlist_create();
|
|
|
|
char *ans;
|
|
|
|
for (i = 0; getinfo_items[i].varname; ++i) {
|
|
|
|
if (!getinfo_items[i].desc)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
tor_snprintf(buf, sizeof(buf), "%s%s -- %s\n",
|
|
|
|
getinfo_items[i].varname,
|
|
|
|
getinfo_items[i].is_prefix ? "*" : "",
|
|
|
|
getinfo_items[i].desc);
|
|
|
|
smartlist_add(lines, tor_strdup(buf));
|
|
|
|
}
|
|
|
|
smartlist_sort_strings(lines);
|
|
|
|
|
|
|
|
ans = smartlist_join_strings(lines, "", 0, NULL);
|
|
|
|
SMARTLIST_FOREACH(lines, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(lines);
|
|
|
|
|
|
|
|
return ans;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Lookup the 'getinfo' entry <b>question</b>, and return
|
|
|
|
* the answer in <b>*answer</b> (or NULL if key not recognized).
|
|
|
|
* Return 0 if success or unrecognized, or -1 if recognized but
|
|
|
|
* internal error. */
|
|
|
|
static int
|
|
|
|
handle_getinfo_helper(control_connection_t *control_conn,
|
|
|
|
const char *question, char **answer)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
*answer = NULL; /* unrecognized key by default */
|
|
|
|
|
|
|
|
for (i = 0; getinfo_items[i].varname; ++i) {
|
|
|
|
int match;
|
|
|
|
if (getinfo_items[i].is_prefix)
|
|
|
|
match = !strcmpstart(question, getinfo_items[i].varname);
|
|
|
|
else
|
|
|
|
match = !strcmp(question, getinfo_items[i].varname);
|
|
|
|
if (match) {
|
|
|
|
tor_assert(getinfo_items[i].fn);
|
|
|
|
return getinfo_items[i].fn(control_conn, question, answer);
|
2006-04-02 00:17:37 +02:00
|
|
|
}
|
2005-02-25 07:37:07 +01:00
|
|
|
}
|
2006-12-08 05:39:13 +01:00
|
|
|
|
2006-09-25 07:59:13 +02:00
|
|
|
return 0; /* unrecognized */
|
2005-02-25 07:37:07 +01:00
|
|
|
}
|
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Called when we receive a GETINFO command. Try to fetch all requested
|
|
|
|
* information, and reply with information or error message. */
|
2005-02-25 07:16:28 +01:00
|
|
|
static int
|
2006-07-26 21:07:37 +02:00
|
|
|
handle_control_getinfo(control_connection_t *conn, uint32_t len,
|
|
|
|
const char *body)
|
2005-02-25 07:16:28 +01:00
|
|
|
{
|
2005-02-25 07:37:07 +01:00
|
|
|
smartlist_t *questions = NULL;
|
|
|
|
smartlist_t *answers = NULL;
|
2005-06-18 04:39:25 +02:00
|
|
|
smartlist_t *unrecognized = NULL;
|
2005-08-13 03:55:23 +02:00
|
|
|
char *msg = NULL, *ans = NULL;
|
2005-02-25 07:37:07 +01:00
|
|
|
size_t msg_len;
|
2006-07-26 21:07:26 +02:00
|
|
|
int v0 = STATE_IS_V0(conn->_base.state);
|
2006-07-15 21:21:30 +02:00
|
|
|
(void) len; /* body is nul-terminated, so it's safe to ignore the length. */
|
2005-02-25 07:37:07 +01:00
|
|
|
|
|
|
|
questions = smartlist_create();
|
2005-06-18 04:39:25 +02:00
|
|
|
if (v0)
|
|
|
|
smartlist_split_string(questions, body, "\n",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
|
|
else
|
|
|
|
smartlist_split_string(questions, body, " ",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
2005-02-25 07:37:07 +01:00
|
|
|
answers = smartlist_create();
|
2005-06-18 04:39:25 +02:00
|
|
|
unrecognized = smartlist_create();
|
2005-02-25 07:37:07 +01:00
|
|
|
SMARTLIST_FOREACH(questions, const char *, q,
|
|
|
|
{
|
2006-10-03 21:00:18 +02:00
|
|
|
if (handle_getinfo_helper(conn, q, &ans) < 0) {
|
2005-06-18 04:39:25 +02:00
|
|
|
if (v0)
|
|
|
|
send_control0_error(conn, ERR_INTERNAL, body);
|
|
|
|
else
|
|
|
|
connection_write_str_to_buf("551 Internal error\r\n", conn);
|
2005-02-25 07:37:07 +01:00
|
|
|
goto done;
|
|
|
|
}
|
2005-06-18 04:39:25 +02:00
|
|
|
if (!ans) {
|
|
|
|
if (v0) {
|
|
|
|
send_control0_error(conn, ERR_UNRECOGNIZED_CONFIG_KEY, body);
|
|
|
|
goto done;
|
|
|
|
} else
|
|
|
|
smartlist_add(unrecognized, (char*)q);
|
|
|
|
} else {
|
|
|
|
smartlist_add(answers, tor_strdup(q));
|
|
|
|
smartlist_add(answers, ans);
|
|
|
|
}
|
2005-02-25 07:37:07 +01:00
|
|
|
});
|
2005-06-18 04:39:25 +02:00
|
|
|
if (smartlist_len(unrecognized)) {
|
|
|
|
int i;
|
|
|
|
tor_assert(!v0);
|
2005-06-19 22:40:41 +02:00
|
|
|
for (i=0; i < smartlist_len(unrecognized)-1; ++i)
|
2005-06-18 04:39:25 +02:00
|
|
|
connection_printf_to_buf(conn,
|
2005-06-19 22:40:41 +02:00
|
|
|
"552-Unrecognized key \"%s\"\r\n",
|
2005-06-18 04:39:25 +02:00
|
|
|
(char*)smartlist_get(unrecognized, i));
|
|
|
|
connection_printf_to_buf(conn,
|
2005-06-19 22:40:41 +02:00
|
|
|
"552 Unrecognized key \"%s\"\r\n",
|
|
|
|
(char*)smartlist_get(unrecognized, i));
|
2005-06-18 04:39:25 +02:00
|
|
|
goto done;
|
|
|
|
}
|
2005-02-25 07:37:07 +01:00
|
|
|
|
2005-06-18 04:39:25 +02:00
|
|
|
if (v0) {
|
|
|
|
msg = smartlist_join_strings2(answers, "\0", 1, 1, &msg_len);
|
|
|
|
tor_assert(msg_len > 0); /* it will at least be terminated */
|
|
|
|
send_control0_message(conn, CONTROL0_CMD_INFOVALUE,
|
|
|
|
msg_len, msg);
|
2005-11-17 22:45:38 +01:00
|
|
|
} else {
|
2005-06-18 04:39:25 +02:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < smartlist_len(answers); i += 2) {
|
|
|
|
char *k = smartlist_get(answers, i);
|
|
|
|
char *v = smartlist_get(answers, i+1);
|
|
|
|
if (!strchr(v, '\n') && !strchr(v, '\r')) {
|
2005-06-19 22:40:41 +02:00
|
|
|
connection_printf_to_buf(conn, "250-%s=", k);
|
|
|
|
connection_write_str_to_buf(v, conn);
|
|
|
|
connection_write_str_to_buf("\r\n", conn);
|
2005-06-18 04:39:25 +02:00
|
|
|
} else {
|
|
|
|
char *esc = NULL;
|
|
|
|
size_t len;
|
|
|
|
len = write_escaped_data(v, strlen(v), 1, &esc);
|
2005-06-19 22:40:41 +02:00
|
|
|
connection_printf_to_buf(conn, "250+%s=\r\n", k);
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_write_to_buf(esc, len, TO_CONN(conn));
|
2005-06-18 04:39:25 +02:00
|
|
|
tor_free(esc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
connection_write_str_to_buf("250 OK\r\n", conn);
|
|
|
|
}
|
2005-02-25 07:37:07 +01:00
|
|
|
|
|
|
|
done:
|
2005-10-25 09:02:13 +02:00
|
|
|
if (answers) {
|
|
|
|
SMARTLIST_FOREACH(answers, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(answers);
|
|
|
|
}
|
|
|
|
if (questions) {
|
|
|
|
SMARTLIST_FOREACH(questions, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(questions);
|
|
|
|
}
|
2005-06-18 04:39:25 +02:00
|
|
|
smartlist_free(unrecognized);
|
2005-02-25 07:37:07 +01:00
|
|
|
tor_free(msg);
|
|
|
|
|
2005-02-25 07:16:28 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2005-06-11 20:52:12 +02:00
|
|
|
|
2006-09-18 06:24:41 +02:00
|
|
|
/** If *<b>string</b> contains a recognized purpose (for
|
2006-03-17 23:08:59 +01:00
|
|
|
* circuits if <b>for_circuits</b> is 1, else for routers),
|
2006-02-23 07:51:09 +01:00
|
|
|
* possibly prefaced with the string "purpose=", then assign it
|
2006-09-18 06:24:41 +02:00
|
|
|
* and return 0. Otherwise return -1.
|
|
|
|
*
|
|
|
|
* If it's prefaced with "purpose=", then set *<b>string</b> to
|
|
|
|
* the remainder of the string. */
|
2006-02-23 07:51:09 +01:00
|
|
|
static int
|
2006-09-18 06:24:41 +02:00
|
|
|
get_purpose(char **string, int for_circuits, uint8_t *purpose)
|
2006-02-23 07:51:09 +01:00
|
|
|
{
|
2006-09-18 06:24:41 +02:00
|
|
|
if (!strcmpstart(*string, "purpose="))
|
|
|
|
*string += strlen("purpose=");
|
2006-02-23 07:51:09 +01:00
|
|
|
|
2006-09-18 06:24:41 +02:00
|
|
|
if (!strcmp(*string, "general"))
|
2006-03-17 23:08:59 +01:00
|
|
|
*purpose = for_circuits ? CIRCUIT_PURPOSE_C_GENERAL :
|
|
|
|
ROUTER_PURPOSE_GENERAL;
|
2006-09-18 06:24:41 +02:00
|
|
|
else if (!strcmp(*string, "controller"))
|
2006-03-17 23:08:59 +01:00
|
|
|
*purpose = for_circuits ? CIRCUIT_PURPOSE_CONTROLLER :
|
|
|
|
ROUTER_PURPOSE_GENERAL;
|
2006-02-23 07:51:09 +01:00
|
|
|
else { /* not a recognized purpose */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-10-05 04:09:27 +02:00
|
|
|
/** Called when we get an EXTENDCIRCUIT message. Try to extend the listed
|
|
|
|
* circuit, and report success or failure. */
|
2005-02-25 07:16:28 +01:00
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
handle_control_extendcircuit(control_connection_t *conn, uint32_t len,
|
2005-02-25 07:16:28 +01:00
|
|
|
const char *body)
|
|
|
|
{
|
2005-06-18 05:09:43 +02:00
|
|
|
smartlist_t *router_nicknames=NULL, *routers=NULL;
|
2005-03-19 07:05:55 +01:00
|
|
|
uint32_t circ_id;
|
2006-07-28 17:11:11 +02:00
|
|
|
origin_circuit_t *circ = NULL;
|
2005-06-18 05:09:43 +02:00
|
|
|
int zero_circ, v0;
|
2005-03-24 02:08:25 +01:00
|
|
|
char reply[4];
|
2006-02-23 07:51:09 +01:00
|
|
|
uint8_t intended_purpose = CIRCUIT_PURPOSE_C_GENERAL;
|
2005-03-19 07:05:55 +01:00
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
v0 = STATE_IS_V0(conn->_base.state);
|
2005-03-19 07:05:55 +01:00
|
|
|
router_nicknames = smartlist_create();
|
2005-06-18 05:09:43 +02:00
|
|
|
|
|
|
|
if (v0) {
|
|
|
|
if (len<5) {
|
|
|
|
send_control0_error(conn, ERR_SYNTAX, "extendcircuit message too short");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
smartlist_split_string(router_nicknames, body+4, ",", 0, 0);
|
|
|
|
circ_id = ntohl(get_uint32(body));
|
|
|
|
if (!circ_id) {
|
|
|
|
/* start a new circuit */
|
|
|
|
zero_circ = 1;
|
|
|
|
} else {
|
|
|
|
circ = circuit_get_by_global_id(circ_id);
|
|
|
|
zero_circ = 0;
|
|
|
|
if (!circ) {
|
|
|
|
send_control0_error(conn, ERR_NO_CIRC,
|
|
|
|
"No circuit found with given ID");
|
2005-03-19 07:05:55 +01:00
|
|
|
goto done;
|
|
|
|
}
|
2005-06-18 05:09:43 +02:00
|
|
|
}
|
2005-07-17 23:06:00 +02:00
|
|
|
} else { /* v1 */
|
2005-06-18 05:09:43 +02:00
|
|
|
smartlist_t *args;
|
|
|
|
args = smartlist_create();
|
|
|
|
smartlist_split_string(args, body, " ",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
2005-12-12 17:50:55 +01:00
|
|
|
if (smartlist_len(args)<2) {
|
2005-12-14 21:40:40 +01:00
|
|
|
connection_printf_to_buf(conn,
|
|
|
|
"512 Missing argument to EXTENDCIRCUIT\r\n");
|
2005-12-12 17:50:55 +01:00
|
|
|
SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(args);
|
|
|
|
goto done;
|
|
|
|
}
|
2005-06-18 05:09:43 +02:00
|
|
|
|
2005-07-17 23:06:00 +02:00
|
|
|
zero_circ = !strcmp("0", (char*)smartlist_get(args,0));
|
|
|
|
if (!zero_circ && !(circ = get_circ(smartlist_get(args,0)))) {
|
2005-06-18 05:09:43 +02:00
|
|
|
connection_printf_to_buf(conn, "552 Unknown circuit \"%s\"\r\n",
|
2005-07-17 23:06:00 +02:00
|
|
|
(char*)smartlist_get(args, 0));
|
2005-06-18 05:09:43 +02:00
|
|
|
}
|
2005-07-17 23:06:00 +02:00
|
|
|
smartlist_split_string(router_nicknames, smartlist_get(args,1), ",", 0, 0);
|
2005-06-18 05:09:43 +02:00
|
|
|
|
2006-09-18 06:24:41 +02:00
|
|
|
if (zero_circ && smartlist_len(args)>2) {
|
|
|
|
char *purp = smartlist_get(args,2);
|
|
|
|
if (get_purpose(&purp, 1, &intended_purpose) < 0) {
|
|
|
|
connection_printf_to_buf(conn, "552 Unknown purpose \"%s\"\r\n", purp);
|
|
|
|
SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(args);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
2005-06-18 05:09:43 +02:00
|
|
|
SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(args);
|
2005-07-17 23:06:00 +02:00
|
|
|
if (!zero_circ && !circ) {
|
|
|
|
goto done;
|
|
|
|
}
|
2005-03-22 01:42:38 +01:00
|
|
|
}
|
|
|
|
|
2005-06-18 05:09:43 +02:00
|
|
|
routers = smartlist_create();
|
|
|
|
SMARTLIST_FOREACH(router_nicknames, const char *, n,
|
|
|
|
{
|
2005-10-05 00:23:31 +02:00
|
|
|
routerinfo_t *r = router_get_by_nickname(n, 1);
|
2005-06-18 05:09:43 +02:00
|
|
|
if (!r) {
|
|
|
|
if (v0)
|
|
|
|
send_control0_error(conn, ERR_NO_ROUTER, n);
|
|
|
|
else
|
|
|
|
connection_printf_to_buf(conn, "552 No such router \"%s\"\r\n", n);
|
2005-03-22 01:42:38 +01:00
|
|
|
goto done;
|
|
|
|
}
|
2005-06-18 05:09:43 +02:00
|
|
|
smartlist_add(routers, r);
|
|
|
|
});
|
|
|
|
if (!smartlist_len(routers)) {
|
|
|
|
if (v0)
|
|
|
|
send_control0_error(conn, ERR_SYNTAX, "No router names provided");
|
|
|
|
else
|
|
|
|
connection_write_str_to_buf("512 No router names provided\r\n", conn);
|
|
|
|
goto done;
|
2005-03-22 01:42:38 +01:00
|
|
|
}
|
|
|
|
|
2005-07-17 23:22:18 +02:00
|
|
|
if (zero_circ) {
|
2005-07-17 23:13:36 +02:00
|
|
|
/* start a new circuit */
|
2006-12-13 03:49:45 +01:00
|
|
|
circ = origin_circuit_init(intended_purpose, 0, 0, 0, 0);
|
2005-07-17 23:13:36 +02:00
|
|
|
}
|
|
|
|
|
2005-03-22 01:42:38 +01:00
|
|
|
/* now circ refers to something that is ready to be extended */
|
2005-03-19 07:05:55 +01:00
|
|
|
SMARTLIST_FOREACH(routers, routerinfo_t *, r,
|
2005-06-18 05:09:43 +02:00
|
|
|
{
|
2005-06-29 23:46:55 +02:00
|
|
|
extend_info_t *info = extend_info_from_router(r);
|
2006-07-28 17:11:11 +02:00
|
|
|
circuit_append_new_exit(circ, info);
|
2005-06-29 23:46:55 +02:00
|
|
|
extend_info_free(info);
|
2005-06-18 05:09:43 +02:00
|
|
|
});
|
2005-03-22 01:42:38 +01:00
|
|
|
|
|
|
|
/* now that we've populated the cpath, start extending */
|
2005-06-18 05:09:43 +02:00
|
|
|
if (zero_circ) {
|
2006-10-09 17:47:27 +02:00
|
|
|
int err_reason = 0;
|
|
|
|
if ((err_reason = circuit_handle_first_hop(circ)) < 0) {
|
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ), -err_reason);
|
2005-06-18 05:09:43 +02:00
|
|
|
if (v0)
|
|
|
|
send_control0_error(conn, ERR_INTERNAL, "couldn't start circuit");
|
|
|
|
else
|
|
|
|
connection_write_str_to_buf("551 Couldn't start circuit\r\n", conn);
|
2005-03-24 02:08:25 +01:00
|
|
|
goto done;
|
2005-03-22 01:42:38 +01:00
|
|
|
}
|
|
|
|
} else {
|
2006-07-28 17:11:11 +02:00
|
|
|
if (circ->_base.state == CIRCUIT_STATE_OPEN) {
|
2006-10-09 17:47:27 +02:00
|
|
|
int err_reason = 0;
|
2006-07-28 17:11:11 +02:00
|
|
|
circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_BUILDING);
|
2006-10-09 17:47:27 +02:00
|
|
|
if ((err_reason = circuit_send_next_onion_skin(circ)) < 0) {
|
2006-02-13 10:02:35 +01:00
|
|
|
log_info(LD_CONTROL,
|
|
|
|
"send_next_onion_skin failed; circuit marked for closing.");
|
2006-10-09 17:47:27 +02:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ), -err_reason);
|
2005-06-18 05:09:43 +02:00
|
|
|
if (v0)
|
|
|
|
send_control0_error(conn, ERR_INTERNAL, "couldn't send onion skin");
|
|
|
|
else
|
|
|
|
connection_write_str_to_buf("551 Couldn't send onion skinr\n", conn);
|
2005-03-24 02:08:25 +01:00
|
|
|
goto done;
|
2005-03-22 01:42:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-03-19 07:05:55 +01:00
|
|
|
|
2005-06-18 05:09:43 +02:00
|
|
|
if (v0) {
|
|
|
|
set_uint32(reply, htonl(circ->global_identifier));
|
|
|
|
send_control_done2(conn, reply, sizeof(reply));
|
|
|
|
} else {
|
|
|
|
connection_printf_to_buf(conn, "250 EXTENDED %lu\r\n",
|
|
|
|
(unsigned long)circ->global_identifier);
|
|
|
|
}
|
2005-03-19 07:05:55 +01:00
|
|
|
done:
|
|
|
|
SMARTLIST_FOREACH(router_nicknames, char *, n, tor_free(n));
|
|
|
|
smartlist_free(router_nicknames);
|
2005-07-17 23:06:00 +02:00
|
|
|
if (routers)
|
|
|
|
smartlist_free(routers);
|
2005-02-25 07:16:28 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2005-06-11 20:52:12 +02:00
|
|
|
|
2006-03-17 23:08:59 +01:00
|
|
|
/** Called when we get a SETCIRCUITPURPOSE (if <b>for_circuits</b>
|
|
|
|
* is 1) or SETROUTERPURPOSE message. If we can find
|
|
|
|
* the circuit/router and it's a valid purpose, change it. */
|
2006-02-23 07:51:09 +01:00
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
handle_control_setpurpose(control_connection_t *conn, int for_circuits,
|
2006-03-17 23:08:59 +01:00
|
|
|
uint32_t len, const char *body)
|
2006-02-23 07:51:09 +01:00
|
|
|
{
|
2006-07-28 17:11:11 +02:00
|
|
|
origin_circuit_t *circ = NULL;
|
2006-03-17 23:08:59 +01:00
|
|
|
routerinfo_t *ri = NULL;
|
2006-02-23 07:51:09 +01:00
|
|
|
uint8_t new_purpose;
|
|
|
|
smartlist_t *args = smartlist_create();
|
2006-07-15 21:21:30 +02:00
|
|
|
(void) len; /* body is nul-terminated, so it's safe to ignore the length. */
|
2006-02-23 07:51:09 +01:00
|
|
|
smartlist_split_string(args, body, " ",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
|
|
if (smartlist_len(args)<2) {
|
|
|
|
connection_printf_to_buf(conn,
|
2006-03-17 23:08:59 +01:00
|
|
|
"512 Missing argument to SET%sPURPOSE\r\n",
|
|
|
|
for_circuits ? "CIRCUIT" : "ROUTER");
|
2006-02-23 07:51:09 +01:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2006-03-17 23:08:59 +01:00
|
|
|
if (for_circuits) {
|
|
|
|
if (!(circ = get_circ(smartlist_get(args,0)))) {
|
|
|
|
connection_printf_to_buf(conn, "552 Unknown circuit \"%s\"\r\n",
|
|
|
|
(char*)smartlist_get(args, 0));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!(ri = router_get_by_nickname(smartlist_get(args,0), 0))) {
|
|
|
|
connection_printf_to_buf(conn, "552 Unknown router \"%s\"\r\n",
|
|
|
|
(char*)smartlist_get(args, 0));
|
|
|
|
goto done;
|
|
|
|
}
|
2006-02-23 07:51:09 +01:00
|
|
|
}
|
|
|
|
|
2006-09-18 06:24:41 +02:00
|
|
|
{
|
|
|
|
char *purp = smartlist_get(args,1);
|
|
|
|
if (get_purpose(&purp, for_circuits, &new_purpose) < 0) {
|
|
|
|
connection_printf_to_buf(conn, "552 Unknown purpose \"%s\"\r\n", purp);
|
|
|
|
goto done;
|
|
|
|
}
|
2006-02-23 07:51:09 +01:00
|
|
|
}
|
|
|
|
|
2006-03-17 23:08:59 +01:00
|
|
|
if (for_circuits)
|
2006-07-28 17:11:11 +02:00
|
|
|
circ->_base.purpose = new_purpose;
|
2006-03-17 23:08:59 +01:00
|
|
|
else
|
|
|
|
ri->purpose = new_purpose;
|
2006-02-23 07:51:09 +01:00
|
|
|
connection_write_str_to_buf("250 OK\r\n", conn);
|
|
|
|
|
|
|
|
done:
|
|
|
|
SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(args);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-10-05 04:09:27 +02:00
|
|
|
/** Called when we get an ATTACHSTREAM message. Try to attach the requested
|
|
|
|
* stream, and report success or failure. */
|
2005-06-11 20:52:12 +02:00
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
handle_control_attachstream(control_connection_t *conn, uint32_t len,
|
2005-06-18 05:09:43 +02:00
|
|
|
const char *body)
|
2005-02-25 07:16:28 +01:00
|
|
|
{
|
2006-07-28 17:11:11 +02:00
|
|
|
edge_connection_t *ap_conn = NULL;
|
|
|
|
origin_circuit_t *circ = NULL;
|
2005-06-18 05:09:43 +02:00
|
|
|
int zero_circ;
|
2005-03-12 05:22:01 +01:00
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
if (STATE_IS_V0(conn->_base.state)) {
|
2005-06-18 05:09:43 +02:00
|
|
|
uint32_t conn_id;
|
|
|
|
uint32_t circ_id;
|
|
|
|
if (len < 8) {
|
|
|
|
send_control0_error(conn, ERR_SYNTAX, "attachstream message too short");
|
|
|
|
return 0;
|
|
|
|
}
|
2005-03-12 05:22:01 +01:00
|
|
|
|
2005-06-18 05:09:43 +02:00
|
|
|
conn_id = ntohl(get_uint32(body));
|
|
|
|
circ_id = ntohl(get_uint32(body+4));
|
|
|
|
zero_circ = circ_id == 0;
|
2005-03-12 05:22:01 +01:00
|
|
|
|
2005-06-18 05:09:43 +02:00
|
|
|
if (!(ap_conn = connection_get_by_global_id(conn_id))) {
|
|
|
|
send_control0_error(conn, ERR_NO_STREAM,
|
|
|
|
"No connection found with given ID");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (circ_id && !(circ = circuit_get_by_global_id(circ_id))) {
|
|
|
|
send_control0_error(conn, ERR_NO_CIRC, "No circuit found with given ID");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
smartlist_t *args;
|
|
|
|
args = smartlist_create();
|
|
|
|
smartlist_split_string(args, body, " ",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
2005-12-12 17:50:55 +01:00
|
|
|
if (smartlist_len(args)<2) {
|
2005-12-14 21:40:40 +01:00
|
|
|
connection_printf_to_buf(conn,
|
|
|
|
"512 Missing argument to ATTACHSTREAM\r\n");
|
2005-12-12 17:50:55 +01:00
|
|
|
SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(args);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-06-18 05:09:43 +02:00
|
|
|
|
|
|
|
zero_circ = !strcmp("0", (char*)smartlist_get(args,1));
|
|
|
|
|
|
|
|
if (!(ap_conn = get_stream(smartlist_get(args, 0)))) {
|
|
|
|
connection_printf_to_buf(conn, "552 Unknown stream \"%s\"\r\n",
|
|
|
|
(char*)smartlist_get(args, 0));
|
|
|
|
} else if (!zero_circ && !(circ = get_circ(smartlist_get(args, 1)))) {
|
|
|
|
connection_printf_to_buf(conn, "552 Unknown circuit \"%s\"\r\n",
|
|
|
|
(char*)smartlist_get(args, 1));
|
|
|
|
}
|
|
|
|
SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(args);
|
|
|
|
if (!ap_conn || (!zero_circ && !circ))
|
|
|
|
return 0;
|
2005-03-12 05:22:01 +01:00
|
|
|
}
|
2005-06-18 05:09:43 +02:00
|
|
|
|
2006-07-28 17:11:11 +02:00
|
|
|
if (ap_conn->_base.state != AP_CONN_STATE_CONTROLLER_WAIT &&
|
|
|
|
ap_conn->_base.state != AP_CONN_STATE_CONNECT_WAIT &&
|
|
|
|
ap_conn->_base.state != AP_CONN_STATE_RESOLVE_WAIT) {
|
2006-07-26 21:07:26 +02:00
|
|
|
if (STATE_IS_V0(conn->_base.state)) {
|
2005-06-18 05:09:43 +02:00
|
|
|
send_control0_error(conn, ERR_NO_STREAM,
|
|
|
|
"Connection is not managed by controller.");
|
|
|
|
} else {
|
|
|
|
connection_write_str_to_buf(
|
2005-12-14 21:40:40 +01:00
|
|
|
"555 Connection is not managed by controller.\r\n",
|
|
|
|
conn);
|
2005-06-18 05:09:43 +02:00
|
|
|
}
|
2005-03-12 05:22:01 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-03-09 07:29:53 +01:00
|
|
|
/* Do we need to detach it first? */
|
2006-07-28 17:11:11 +02:00
|
|
|
if (ap_conn->_base.state != AP_CONN_STATE_CONTROLLER_WAIT) {
|
|
|
|
circuit_t *tmpcirc = circuit_get_by_edge_conn(ap_conn);
|
|
|
|
connection_edge_end(ap_conn, END_STREAM_REASON_TIMEOUT,
|
|
|
|
ap_conn->cpath_layer);
|
2006-03-09 07:29:53 +01:00
|
|
|
/* Un-mark it as ending, since we're going to reuse it. */
|
2006-07-28 17:11:11 +02:00
|
|
|
ap_conn->_base.edge_has_sent_end = 0;
|
2006-10-20 19:54:48 +02:00
|
|
|
ap_conn->end_reason = 0;
|
2006-03-12 05:07:21 +01:00
|
|
|
if (tmpcirc)
|
2006-07-28 17:11:11 +02:00
|
|
|
circuit_detach_stream(tmpcirc,ap_conn);
|
|
|
|
ap_conn->_base.state = AP_CONN_STATE_CONTROLLER_WAIT;
|
2006-03-09 07:29:53 +01:00
|
|
|
}
|
|
|
|
|
2006-07-28 17:11:11 +02:00
|
|
|
if (circ && (circ->_base.state != CIRCUIT_STATE_OPEN)) {
|
2006-07-26 21:07:26 +02:00
|
|
|
if (STATE_IS_V0(conn->_base.state))
|
2005-12-14 21:40:40 +01:00
|
|
|
send_control0_error(conn, ERR_INTERNAL,
|
2006-07-23 09:37:35 +02:00
|
|
|
"Refuse to attach stream to non-open, origin circ.");
|
2005-06-18 05:09:43 +02:00
|
|
|
else
|
|
|
|
connection_write_str_to_buf(
|
2006-07-23 09:37:35 +02:00
|
|
|
"551 Can't attach stream to non-open, origin circuit\r\n",
|
|
|
|
conn);
|
2005-03-24 07:18:59 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2006-10-01 22:50:11 +02:00
|
|
|
if (circ && circuit_get_cpath_len(circ) < 2) {
|
|
|
|
if (STATE_IS_V0(conn->_base.state))
|
|
|
|
send_control0_error(conn, ERR_INTERNAL,
|
|
|
|
"Refuse to attach stream to one-hop circuit.");
|
|
|
|
else
|
|
|
|
connection_write_str_to_buf(
|
|
|
|
"551 Can't attach stream to one-hop circuit.\r\n",
|
|
|
|
conn);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-07-28 17:11:11 +02:00
|
|
|
if (connection_ap_handshake_rewrite_and_attach(ap_conn, circ) < 0) {
|
2006-07-26 21:07:26 +02:00
|
|
|
if (STATE_IS_V0(conn->_base.state))
|
2005-06-18 05:09:43 +02:00
|
|
|
send_control0_error(conn, ERR_INTERNAL, "Unable to attach stream.");
|
|
|
|
else
|
|
|
|
connection_write_str_to_buf("551 Unable to attach stream\r\n", conn);
|
2005-03-12 05:22:01 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2005-03-19 07:05:55 +01:00
|
|
|
send_control_done(conn);
|
2005-02-25 07:16:28 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2005-06-11 20:52:12 +02:00
|
|
|
|
2005-10-05 04:09:27 +02:00
|
|
|
/** Called when we get a POSTDESCRIPTOR message. Try to learn the provided
|
|
|
|
* descriptor, and report success or failure. */
|
2005-02-25 07:16:28 +01:00
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
handle_control_postdescriptor(control_connection_t *conn, uint32_t len,
|
2005-02-25 07:16:28 +01:00
|
|
|
const char *body)
|
|
|
|
{
|
2005-06-18 04:39:25 +02:00
|
|
|
char *desc;
|
2006-07-26 21:07:26 +02:00
|
|
|
int v0 = STATE_IS_V0(conn->_base.state);
|
2005-06-20 11:38:29 +02:00
|
|
|
const char *msg=NULL;
|
2006-03-17 23:08:59 +01:00
|
|
|
uint8_t purpose = ROUTER_PURPOSE_GENERAL;
|
2005-06-20 11:38:29 +02:00
|
|
|
|
2005-06-18 04:39:25 +02:00
|
|
|
if (v0)
|
|
|
|
desc = (char*)body;
|
|
|
|
else {
|
2006-03-17 23:08:59 +01:00
|
|
|
char *cp = memchr(body, '\n', len);
|
|
|
|
smartlist_t *args = smartlist_create();
|
2005-06-18 04:39:25 +02:00
|
|
|
tor_assert(cp);
|
2006-03-17 23:08:59 +01:00
|
|
|
*cp++ = '\0';
|
|
|
|
smartlist_split_string(args, body, " ",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
|
|
if (smartlist_len(args)) {
|
2006-09-18 06:24:41 +02:00
|
|
|
char *purp = smartlist_get(args,0);
|
|
|
|
if (get_purpose(&purp, 0, &purpose) < 0) {
|
2006-03-17 23:08:59 +01:00
|
|
|
connection_printf_to_buf(conn, "552 Unknown purpose \"%s\"\r\n",
|
2006-09-18 06:24:41 +02:00
|
|
|
purp);
|
2006-03-17 23:08:59 +01:00
|
|
|
SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(args);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(args);
|
2005-06-18 04:39:25 +02:00
|
|
|
read_escaped_data(cp, len-(cp-body), 1, &desc);
|
|
|
|
}
|
|
|
|
|
2006-03-17 23:08:59 +01:00
|
|
|
switch (router_load_single_router(desc, purpose, &msg)) {
|
2005-04-03 00:02:13 +02:00
|
|
|
case -1:
|
2005-06-18 04:39:25 +02:00
|
|
|
if (!msg) msg = "Could not parse descriptor";
|
|
|
|
if (v0)
|
|
|
|
send_control0_error(conn,ERR_SYNTAX,msg);
|
|
|
|
else
|
|
|
|
connection_printf_to_buf(conn, "554 %s\r\n", msg);
|
2005-04-03 00:02:13 +02:00
|
|
|
break;
|
|
|
|
case 0:
|
2005-06-18 04:39:25 +02:00
|
|
|
if (!msg) msg = "Descriptor not added";
|
|
|
|
if (v0)
|
|
|
|
send_control_done2(conn,msg,0);
|
|
|
|
else
|
|
|
|
connection_printf_to_buf(conn, "251 %s\r\n",msg);
|
2005-04-03 00:02:13 +02:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
send_control_done(conn);
|
2005-04-11 04:52:09 +02:00
|
|
|
break;
|
2005-02-25 21:46:13 +01:00
|
|
|
}
|
|
|
|
|
2005-06-18 05:09:43 +02:00
|
|
|
if (!v0)
|
|
|
|
tor_free(desc);
|
2005-02-25 07:16:28 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2005-06-11 20:52:12 +02:00
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Called when we receive a REDIRECTSTERAM command. Try to change the target
|
2005-11-18 12:32:59 +01:00
|
|
|
* address of the named AP stream, and report success or failure. */
|
2005-03-19 07:05:55 +01:00
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
handle_control_redirectstream(control_connection_t *conn, uint32_t len,
|
2005-03-19 07:05:55 +01:00
|
|
|
const char *body)
|
|
|
|
{
|
2006-07-28 17:11:11 +02:00
|
|
|
edge_connection_t *ap_conn = NULL;
|
2005-03-19 07:05:55 +01:00
|
|
|
uint32_t conn_id;
|
2005-06-18 04:39:25 +02:00
|
|
|
char *new_addr = NULL;
|
2005-11-18 12:32:59 +01:00
|
|
|
uint16_t new_port = 0;
|
2006-07-26 21:07:26 +02:00
|
|
|
if (STATE_IS_V0(conn->_base.state)) {
|
2005-06-18 04:39:25 +02:00
|
|
|
if (len < 6) {
|
2005-12-14 21:40:40 +01:00
|
|
|
send_control0_error(conn, ERR_SYNTAX,
|
|
|
|
"redirectstream message too short");
|
2005-06-18 04:39:25 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
conn_id = ntohl(get_uint32(body));
|
2005-03-19 07:05:55 +01:00
|
|
|
|
2005-06-18 04:39:25 +02:00
|
|
|
if (!(ap_conn = connection_get_by_global_id(conn_id))
|
2006-07-28 17:11:11 +02:00
|
|
|
|| ap_conn->_base.state != CONN_TYPE_AP
|
|
|
|
|| ap_conn->socks_request) {
|
2005-06-18 04:39:25 +02:00
|
|
|
send_control0_error(conn, ERR_NO_STREAM,
|
|
|
|
"No AP connection found with given ID");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
new_addr = tor_strdup(body+4);
|
|
|
|
} else {
|
|
|
|
smartlist_t *args;
|
|
|
|
args = smartlist_create();
|
|
|
|
smartlist_split_string(args, body, " ",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
2005-11-18 12:32:59 +01:00
|
|
|
if (smartlist_len(args) < 2)
|
2005-12-14 21:40:40 +01:00
|
|
|
connection_printf_to_buf(conn,
|
|
|
|
"512 Missing argument to REDIRECTSTREAM\r\n");
|
2005-06-18 04:39:25 +02:00
|
|
|
else if (!(ap_conn = get_stream(smartlist_get(args, 0)))
|
2006-07-28 17:11:11 +02:00
|
|
|
|| !ap_conn->socks_request) {
|
2005-06-18 04:39:25 +02:00
|
|
|
connection_printf_to_buf(conn, "552 Unknown stream \"%s\"\r\n",
|
|
|
|
(char*)smartlist_get(args, 0));
|
|
|
|
} else {
|
2005-11-18 12:32:59 +01:00
|
|
|
int ok;
|
2005-11-18 12:35:23 +01:00
|
|
|
if (smartlist_len(args) > 2) { /* they included a port too */
|
2005-11-18 12:32:59 +01:00
|
|
|
new_port = (uint16_t) tor_parse_ulong(smartlist_get(args, 2),
|
|
|
|
10, 1, 65535, &ok, NULL);
|
|
|
|
}
|
|
|
|
if (!ok) {
|
|
|
|
connection_printf_to_buf(conn, "512 Cannot parse port \"%s\"\r\n",
|
|
|
|
(char*)smartlist_get(args, 2));
|
|
|
|
} else {
|
|
|
|
new_addr = tor_strdup(smartlist_get(args, 1));
|
|
|
|
}
|
2005-06-18 04:39:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(args);
|
|
|
|
if (!new_addr)
|
|
|
|
return 0;
|
2005-03-19 07:05:55 +01:00
|
|
|
}
|
|
|
|
|
2006-07-28 17:11:11 +02:00
|
|
|
strlcpy(ap_conn->socks_request->address, new_addr,
|
|
|
|
sizeof(ap_conn->socks_request->address));
|
|
|
|
if (new_port)
|
|
|
|
ap_conn->socks_request->port = new_port;
|
|
|
|
tor_free(new_addr);
|
|
|
|
send_control_done(conn);
|
|
|
|
return 0;
|
2005-03-19 07:05:55 +01:00
|
|
|
}
|
2005-06-11 20:52:12 +02:00
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Called when we get a CLOSESTREAM command; try to close the named stream
|
|
|
|
* and report success or failure. */
|
2005-03-22 20:36:38 +01:00
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
handle_control_closestream(control_connection_t *conn, uint32_t len,
|
2005-03-22 20:36:38 +01:00
|
|
|
const char *body)
|
|
|
|
{
|
2006-07-28 17:11:11 +02:00
|
|
|
edge_connection_t *ap_conn=NULL;
|
2005-06-18 04:39:25 +02:00
|
|
|
uint8_t reason=0;
|
2005-03-22 20:36:38 +01:00
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
if (STATE_IS_V0(conn->_base.state)) {
|
2005-06-18 04:39:25 +02:00
|
|
|
uint32_t conn_id;
|
|
|
|
if (len < 6) {
|
|
|
|
send_control0_error(conn, ERR_SYNTAX, "closestream message too short");
|
|
|
|
return 0;
|
|
|
|
}
|
2005-03-22 20:36:38 +01:00
|
|
|
|
2005-06-18 04:39:25 +02:00
|
|
|
conn_id = ntohl(get_uint32(body));
|
|
|
|
reason = *(uint8_t*)(body+4);
|
2005-03-22 20:36:38 +01:00
|
|
|
|
2005-06-18 04:39:25 +02:00
|
|
|
if (!(ap_conn = connection_get_by_global_id(conn_id))
|
2006-07-28 17:11:11 +02:00
|
|
|
|| ap_conn->_base.state != CONN_TYPE_AP
|
|
|
|
|| ap_conn->socks_request) {
|
2005-06-18 04:39:25 +02:00
|
|
|
send_control0_error(conn, ERR_NO_STREAM,
|
|
|
|
"No AP connection found with given ID");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
smartlist_t *args;
|
|
|
|
int ok;
|
|
|
|
args = smartlist_create();
|
|
|
|
smartlist_split_string(args, body, " ",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
|
|
if (smartlist_len(args)<2)
|
2005-12-14 21:40:40 +01:00
|
|
|
connection_printf_to_buf(conn,
|
|
|
|
"512 Missing argument to CLOSESTREAM\r\n");
|
2005-06-18 04:39:25 +02:00
|
|
|
else if (!(ap_conn = get_stream(smartlist_get(args, 0))))
|
|
|
|
connection_printf_to_buf(conn, "552 Unknown stream \"%s\"\r\n",
|
|
|
|
(char*)smartlist_get(args, 0));
|
|
|
|
else {
|
|
|
|
reason = (uint8_t) tor_parse_ulong(smartlist_get(args,1), 10, 0, 255,
|
|
|
|
&ok, NULL);
|
|
|
|
if (!ok) {
|
|
|
|
connection_printf_to_buf(conn, "552 Unrecognized reason \"%s\"\r\n",
|
|
|
|
(char*)smartlist_get(args, 1));
|
|
|
|
ap_conn = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(args);
|
|
|
|
if (!ap_conn)
|
|
|
|
return 0;
|
2005-03-22 20:36:38 +01:00
|
|
|
}
|
2005-06-18 04:39:25 +02:00
|
|
|
|
2006-07-28 17:11:11 +02:00
|
|
|
connection_mark_unattached_ap(ap_conn, reason);
|
2005-03-22 20:36:38 +01:00
|
|
|
send_control_done(conn);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-03-27 06:55:13 +02:00
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Called when we get a CLOSECIRCUIT command; try to close the named circuit
|
|
|
|
* and report success or failure. */
|
2005-03-22 20:36:38 +01:00
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
handle_control_closecircuit(control_connection_t *conn, uint32_t len,
|
2005-03-22 20:36:38 +01:00
|
|
|
const char *body)
|
|
|
|
{
|
2006-07-28 17:11:11 +02:00
|
|
|
origin_circuit_t *circ = NULL;
|
2005-06-18 04:39:25 +02:00
|
|
|
int safe = 0;
|
2005-03-19 07:05:55 +01:00
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
if (STATE_IS_V0(conn->_base.state)) {
|
2005-06-18 04:39:25 +02:00
|
|
|
uint32_t circ_id;
|
|
|
|
if (len < 5) {
|
|
|
|
send_control0_error(conn, ERR_SYNTAX, "closecircuit message too short");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
circ_id = ntohl(get_uint32(body));
|
|
|
|
safe = (*(uint8_t*)(body+4)) & 1;
|
2005-03-22 20:36:38 +01:00
|
|
|
|
2005-06-18 04:39:25 +02:00
|
|
|
if (!(circ = circuit_get_by_global_id(circ_id))) {
|
|
|
|
send_control0_error(conn, ERR_NO_CIRC,
|
|
|
|
"No circuit found with given ID");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
smartlist_t *args;
|
|
|
|
args = smartlist_create();
|
|
|
|
smartlist_split_string(args, body, " ",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
|
|
if (smartlist_len(args)<1)
|
2005-12-14 21:40:40 +01:00
|
|
|
connection_printf_to_buf(conn,
|
|
|
|
"512 Missing argument to CLOSECIRCUIT\r\n");
|
2005-06-18 04:39:25 +02:00
|
|
|
else if (!(circ=get_circ(smartlist_get(args, 0))))
|
|
|
|
connection_printf_to_buf(conn, "552 Unknown circuit \"%s\"\r\n",
|
|
|
|
(char*)smartlist_get(args, 0));
|
|
|
|
else {
|
|
|
|
int i;
|
|
|
|
for (i=1; i < smartlist_len(args); ++i) {
|
|
|
|
if (!strcasecmp(smartlist_get(args, i), "IfUnused"))
|
|
|
|
safe = 1;
|
|
|
|
else
|
2006-02-13 10:02:35 +01:00
|
|
|
log_info(LD_CONTROL, "Skipping unknown option %s",
|
|
|
|
(char*)smartlist_get(args,i));
|
2005-06-18 04:39:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(args);
|
|
|
|
if (!circ)
|
|
|
|
return 0;
|
2005-03-22 20:36:38 +01:00
|
|
|
}
|
|
|
|
|
2006-07-28 17:11:11 +02:00
|
|
|
if (!safe || !circ->p_streams) {
|
2006-10-13 07:27:59 +02:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_REQUESTED);
|
2005-03-22 20:36:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
send_control_done(conn);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-02-25 07:16:28 +01:00
|
|
|
|
2006-10-03 20:58:52 +02:00
|
|
|
static int
|
|
|
|
handle_control_usefeature(control_connection_t *conn,
|
|
|
|
uint32_t len,
|
|
|
|
const char *body)
|
|
|
|
{
|
|
|
|
smartlist_t *args;
|
2006-10-03 20:58:56 +02:00
|
|
|
int verbose_names = 0, extended_events = 0;
|
|
|
|
int bad = 0;
|
2006-10-03 21:00:18 +02:00
|
|
|
(void) len; /* body is nul-terminated; it's safe to ignore the length */
|
|
|
|
tor_assert(! STATE_IS_V0(conn->_base.state));
|
2006-10-03 20:58:52 +02:00
|
|
|
args = smartlist_create();
|
|
|
|
smartlist_split_string(args, body, " ",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
|
|
SMARTLIST_FOREACH(args, const char *, arg, {
|
|
|
|
if (!strcasecmp(arg, "VERBOSE_NAMES"))
|
|
|
|
verbose_names = 1;
|
2006-10-20 23:04:44 +02:00
|
|
|
else if (!strcasecmp(arg, "EXTENDED_EVENTS")) /* <- documented */
|
2006-10-03 20:58:56 +02:00
|
|
|
extended_events = 1;
|
2006-10-20 23:04:44 +02:00
|
|
|
else if (!strcasecmp(arg, "EXTENDED_FORMAT")) {
|
2006-10-23 07:24:57 +02:00
|
|
|
/* remove this in 0.1.2.4; EXTENDED_FORMAT only ever worked for a
|
|
|
|
* little while during 0.1.2.2-alpha-dev. */
|
2006-10-20 23:04:44 +02:00
|
|
|
log_warn(LD_GENERAL,
|
2006-10-24 04:58:03 +02:00
|
|
|
"EXTENDED_FORMAT is deprecated; use EXTENDED_EVENTS "
|
|
|
|
"instead.");
|
2006-10-20 23:04:44 +02:00
|
|
|
extended_events = 1;
|
|
|
|
} else {
|
2006-10-03 20:58:52 +02:00
|
|
|
connection_printf_to_buf(conn, "552 Unrecognized feature \"%s\"\r\n",
|
|
|
|
arg);
|
|
|
|
bad = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!bad) {
|
|
|
|
if (verbose_names) {
|
|
|
|
conn->use_long_names = 1;
|
|
|
|
control_update_global_event_mask();
|
|
|
|
}
|
2006-10-03 20:58:56 +02:00
|
|
|
if (extended_events)
|
|
|
|
conn->use_extended_events = 1;
|
2006-10-03 20:58:52 +02:00
|
|
|
send_control_done(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(args);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Called when we get a v0 FRAGMENTHEADER or FRAGMENT command; try to append
|
|
|
|
* the data to conn->incoming_cmd, setting conn->incoming_(type|len|cur_len)
|
|
|
|
* as appropriate. If the command is malformed, drop it and all pending
|
|
|
|
* fragments and report failure.
|
|
|
|
*/
|
2005-04-28 00:01:34 +02:00
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
handle_control_fragments(control_connection_t *conn, uint16_t command_type,
|
2005-04-28 00:01:34 +02:00
|
|
|
uint32_t body_len, char *body)
|
|
|
|
{
|
2005-06-17 20:49:55 +02:00
|
|
|
if (command_type == CONTROL0_CMD_FRAGMENTHEADER) {
|
2005-04-28 00:01:34 +02:00
|
|
|
if (conn->incoming_cmd) {
|
2006-09-30 00:33:40 +02:00
|
|
|
log_warn(LD_CONTROL, "Dropping incomplete fragmented command; new "
|
|
|
|
"fragmented command was begun.");
|
2005-04-28 00:01:34 +02:00
|
|
|
tor_free(conn->incoming_cmd);
|
|
|
|
}
|
|
|
|
if (body_len < 6) {
|
2005-06-17 20:49:55 +02:00
|
|
|
send_control0_error(conn, ERR_SYNTAX, "FRAGMENTHEADER too short.");
|
2005-04-28 00:01:34 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
conn->incoming_cmd_type = ntohs(get_uint16(body));
|
|
|
|
conn->incoming_cmd_len = ntohl(get_uint32(body+2));
|
|
|
|
conn->incoming_cmd_cur_len = 0;
|
|
|
|
conn->incoming_cmd = tor_malloc(conn->incoming_cmd_len);
|
|
|
|
body += 6;
|
|
|
|
body_len -= 6;
|
2005-06-17 20:49:55 +02:00
|
|
|
} else if (command_type == CONTROL0_CMD_FRAGMENT) {
|
2005-04-28 00:01:34 +02:00
|
|
|
if (!conn->incoming_cmd) {
|
2005-06-17 20:49:55 +02:00
|
|
|
send_control0_error(conn, ERR_SYNTAX, "Out-of-place FRAGMENT");
|
2005-04-28 00:01:34 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tor_assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conn->incoming_cmd_cur_len + body_len > conn->incoming_cmd_len) {
|
|
|
|
tor_free(conn->incoming_cmd);
|
2005-06-17 20:49:55 +02:00
|
|
|
send_control0_error(conn, ERR_SYNTAX,
|
2005-04-28 00:01:34 +02:00
|
|
|
"Fragmented data exceeds declared length");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy(conn->incoming_cmd + conn->incoming_cmd_cur_len,
|
|
|
|
body, body_len);
|
|
|
|
conn->incoming_cmd_cur_len += body_len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-07 22:37:50 +01:00
|
|
|
/** Called when <b>conn</b> has no more bytes left on its outbuf. */
|
|
|
|
int
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_control_finished_flushing(control_connection_t *conn)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-11-03 02:32:26 +01:00
|
|
|
tor_assert(conn);
|
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_stop_writing(TO_CONN(conn));
|
2004-11-03 02:32:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-21 11:14:57 +01:00
|
|
|
/** Called when <b>conn</b> has gotten its socket closed. */
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_control_reached_eof(control_connection_t *conn)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2005-06-17 20:49:55 +02:00
|
|
|
tor_assert(conn);
|
|
|
|
|
2006-02-13 10:02:35 +01:00
|
|
|
log_info(LD_CONTROL,"Control connection reached EOF. Closing.");
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
2004-11-21 11:14:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Called when data has arrived on a v1 control connection: Try to fetch
|
|
|
|
* commands from conn->inbuf, and execute them.
|
|
|
|
*/
|
2005-06-17 20:49:55 +02:00
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_control_process_inbuf_v1(control_connection_t *conn)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2005-08-12 19:24:53 +02:00
|
|
|
size_t data_len;
|
|
|
|
int cmd_len;
|
2005-06-17 22:37:21 +02:00
|
|
|
char *args;
|
2004-11-03 02:32:26 +01:00
|
|
|
|
|
|
|
tor_assert(conn);
|
2006-07-26 21:07:26 +02:00
|
|
|
tor_assert(conn->_base.state == CONTROL_CONN_STATE_OPEN_V1 ||
|
|
|
|
conn->_base.state == CONTROL_CONN_STATE_NEEDAUTH_V1);
|
2005-06-17 20:49:55 +02:00
|
|
|
|
|
|
|
if (!conn->incoming_cmd) {
|
|
|
|
conn->incoming_cmd = tor_malloc(1024);
|
|
|
|
conn->incoming_cmd_len = 1024;
|
|
|
|
conn->incoming_cmd_cur_len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
again:
|
|
|
|
while (1) {
|
|
|
|
size_t last_idx;
|
|
|
|
int r;
|
2005-07-15 21:31:11 +02:00
|
|
|
/* First, fetch a line. */
|
2005-06-17 22:37:21 +02:00
|
|
|
do {
|
2005-06-17 20:49:55 +02:00
|
|
|
data_len = conn->incoming_cmd_len - conn->incoming_cmd_cur_len;
|
2006-07-26 21:07:26 +02:00
|
|
|
r = fetch_from_buf_line(conn->_base.inbuf,
|
2005-07-15 21:31:11 +02:00
|
|
|
conn->incoming_cmd+conn->incoming_cmd_cur_len,
|
|
|
|
&data_len);
|
2005-06-17 20:49:55 +02:00
|
|
|
if (r == 0)
|
|
|
|
/* Line not all here yet. Wait. */
|
|
|
|
return 0;
|
|
|
|
else if (r == -1) {
|
2005-06-17 22:37:21 +02:00
|
|
|
while (conn->incoming_cmd_len < data_len+conn->incoming_cmd_cur_len)
|
2005-06-17 20:49:55 +02:00
|
|
|
conn->incoming_cmd_len *= 2;
|
2005-07-15 21:31:11 +02:00
|
|
|
conn->incoming_cmd = tor_realloc(conn->incoming_cmd,
|
|
|
|
conn->incoming_cmd_len);
|
2005-06-17 20:49:55 +02:00
|
|
|
}
|
|
|
|
} while (r != 1);
|
|
|
|
|
|
|
|
tor_assert(data_len);
|
|
|
|
|
|
|
|
last_idx = conn->incoming_cmd_cur_len;
|
|
|
|
conn->incoming_cmd_cur_len += data_len;
|
|
|
|
|
|
|
|
/* We have appended a line to incoming_cmd. Is the command done? */
|
|
|
|
if (last_idx == 0 && *conn->incoming_cmd != '+')
|
|
|
|
/* One line command, didn't start with '+'. */
|
|
|
|
break;
|
|
|
|
if (last_idx+3 == conn->incoming_cmd_cur_len &&
|
2005-07-15 21:31:11 +02:00
|
|
|
!memcmp(conn->incoming_cmd + last_idx, ".\r\n", 3)) {
|
|
|
|
/* Just appended ".\r\n"; we're done. Remove it. */
|
|
|
|
conn->incoming_cmd_cur_len -= 3;
|
2005-06-17 20:49:55 +02:00
|
|
|
break;
|
2005-07-15 21:31:11 +02:00
|
|
|
}
|
2005-06-17 20:49:55 +02:00
|
|
|
/* Otherwise, read another line. */
|
|
|
|
}
|
|
|
|
data_len = conn->incoming_cmd_cur_len;
|
|
|
|
/* Okay, we now have a command sitting on conn->incoming_cmd. See if we
|
|
|
|
* recognize it.
|
|
|
|
*/
|
2005-06-17 22:37:21 +02:00
|
|
|
cmd_len = 0;
|
2005-09-08 08:37:50 +02:00
|
|
|
while ((size_t)cmd_len < data_len
|
|
|
|
&& !TOR_ISSPACE(conn->incoming_cmd[cmd_len]))
|
2005-06-17 20:49:55 +02:00
|
|
|
++cmd_len;
|
|
|
|
|
2005-06-17 22:37:21 +02:00
|
|
|
data_len -= cmd_len;
|
|
|
|
conn->incoming_cmd[cmd_len]='\0';
|
|
|
|
args = conn->incoming_cmd+cmd_len+1;
|
|
|
|
while (*args == ' ' || *args == '\t') {
|
|
|
|
++args;
|
|
|
|
--data_len;
|
|
|
|
}
|
|
|
|
|
2005-08-13 03:55:23 +02:00
|
|
|
if (!strcasecmp(conn->incoming_cmd, "QUIT")) {
|
|
|
|
connection_write_str_to_buf("250 closing connection\r\n", conn);
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
2005-08-13 03:55:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
if (conn->_base.state == CONTROL_CONN_STATE_NEEDAUTH_V1 &&
|
2005-06-17 22:37:21 +02:00
|
|
|
strcasecmp(conn->incoming_cmd, "AUTHENTICATE")) {
|
2005-06-17 20:49:55 +02:00
|
|
|
connection_write_str_to_buf("514 Authentication required.\r\n", conn);
|
|
|
|
conn->incoming_cmd_cur_len = 0;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
2005-06-17 22:37:21 +02:00
|
|
|
if (!strcasecmp(conn->incoming_cmd, "SETCONF")) {
|
|
|
|
if (handle_control_setconf(conn, data_len, args))
|
2005-06-17 20:49:55 +02:00
|
|
|
return -1;
|
2005-09-08 05:18:51 +02:00
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "RESETCONF")) {
|
|
|
|
if (handle_control_resetconf(conn, data_len, args))
|
|
|
|
return -1;
|
2005-06-17 22:37:21 +02:00
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "GETCONF")) {
|
|
|
|
if (handle_control_getconf(conn, data_len, args))
|
2005-06-17 20:49:55 +02:00
|
|
|
return -1;
|
2005-06-17 22:37:21 +02:00
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "SETEVENTS")) {
|
|
|
|
if (handle_control_setevents(conn, data_len, args))
|
2005-06-17 20:49:55 +02:00
|
|
|
return -1;
|
2005-06-17 22:37:21 +02:00
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "AUTHENTICATE")) {
|
|
|
|
if (handle_control_authenticate(conn, data_len, args))
|
2005-06-17 20:49:55 +02:00
|
|
|
return -1;
|
2005-06-17 22:37:21 +02:00
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "SAVECONF")) {
|
|
|
|
if (handle_control_saveconf(conn, data_len, args))
|
2005-06-17 20:49:55 +02:00
|
|
|
return -1;
|
2005-06-17 22:37:21 +02:00
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "SIGNAL")) {
|
|
|
|
if (handle_control_signal(conn, data_len, args))
|
2005-06-17 20:49:55 +02:00
|
|
|
return -1;
|
2005-06-17 22:37:21 +02:00
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "MAPADDRESS")) {
|
|
|
|
if (handle_control_mapaddress(conn, data_len, args))
|
2005-06-17 20:49:55 +02:00
|
|
|
return -1;
|
2005-06-17 22:37:21 +02:00
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "GETINFO")) {
|
|
|
|
if (handle_control_getinfo(conn, data_len, args))
|
2005-06-17 20:49:55 +02:00
|
|
|
return -1;
|
2005-06-17 22:37:21 +02:00
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "EXTENDCIRCUIT")) {
|
|
|
|
if (handle_control_extendcircuit(conn, data_len, args))
|
2005-06-17 20:49:55 +02:00
|
|
|
return -1;
|
2006-02-23 07:51:09 +01:00
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "SETCIRCUITPURPOSE")) {
|
2006-03-17 23:08:59 +01:00
|
|
|
if (handle_control_setpurpose(conn, 1, data_len, args))
|
|
|
|
return -1;
|
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "SETROUTERPURPOSE")) {
|
|
|
|
if (handle_control_setpurpose(conn, 0, data_len, args))
|
2006-02-23 07:51:09 +01:00
|
|
|
return -1;
|
2005-06-17 22:37:21 +02:00
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "ATTACHSTREAM")) {
|
|
|
|
if (handle_control_attachstream(conn, data_len, args))
|
2005-06-17 20:49:55 +02:00
|
|
|
return -1;
|
2005-06-17 22:37:21 +02:00
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "+POSTDESCRIPTOR")) {
|
|
|
|
if (handle_control_postdescriptor(conn, data_len, args))
|
2005-06-17 20:49:55 +02:00
|
|
|
return -1;
|
2005-06-17 22:37:21 +02:00
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "REDIRECTSTREAM")) {
|
|
|
|
if (handle_control_redirectstream(conn, data_len, args))
|
2005-06-17 20:49:55 +02:00
|
|
|
return -1;
|
2005-06-17 22:37:21 +02:00
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "CLOSESTREAM")) {
|
|
|
|
if (handle_control_closestream(conn, data_len, args))
|
2005-06-17 20:49:55 +02:00
|
|
|
return -1;
|
2005-06-17 22:37:21 +02:00
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "CLOSECIRCUIT")) {
|
|
|
|
if (handle_control_closecircuit(conn, data_len, args))
|
2005-06-17 20:49:55 +02:00
|
|
|
return -1;
|
2006-10-03 20:58:52 +02:00
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "USEFEATURE")) {
|
|
|
|
if (handle_control_usefeature(conn, data_len, args))
|
|
|
|
return -1;
|
2005-06-17 20:49:55 +02:00
|
|
|
} else {
|
2005-06-17 22:37:21 +02:00
|
|
|
connection_printf_to_buf(conn, "510 Unrecognized command \"%s\"\r\n",
|
2005-06-17 20:49:55 +02:00
|
|
|
conn->incoming_cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->incoming_cmd_cur_len = 0;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Called when data has arrived on a v0 control connection: Try to fetch
|
|
|
|
* commands from conn->inbuf, and execute them.
|
|
|
|
*/
|
2005-06-17 20:49:55 +02:00
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_control_process_inbuf_v0(control_connection_t *conn)
|
2005-06-17 20:49:55 +02:00
|
|
|
{
|
|
|
|
uint32_t body_len;
|
|
|
|
uint16_t command_type;
|
2005-07-12 07:48:15 +02:00
|
|
|
char *body=NULL;
|
2004-11-03 02:32:26 +01:00
|
|
|
|
|
|
|
again:
|
2004-11-07 22:37:50 +01:00
|
|
|
/* Try to suck a control message from the buffer. */
|
2006-07-26 21:07:37 +02:00
|
|
|
switch (fetch_from_buf_control0(conn->_base.inbuf, &body_len, &command_type,
|
|
|
|
&body,
|
|
|
|
conn->_base.state == CONTROL_CONN_STATE_NEEDAUTH_V0))
|
2004-11-03 02:32:26 +01:00
|
|
|
{
|
2005-06-17 20:49:55 +02:00
|
|
|
case -2:
|
|
|
|
tor_free(body);
|
2006-02-13 10:02:35 +01:00
|
|
|
log_info(LD_CONTROL,
|
|
|
|
"Detected v1 control protocol on connection (fd %d)",
|
2006-07-26 21:07:26 +02:00
|
|
|
conn->_base.s);
|
|
|
|
conn->_base.state = CONTROL_CONN_STATE_NEEDAUTH_V1;
|
2005-06-17 20:49:55 +02:00
|
|
|
return connection_control_process_inbuf_v1(conn);
|
2004-11-03 02:32:26 +01:00
|
|
|
case -1:
|
2004-11-07 23:58:35 +01:00
|
|
|
tor_free(body);
|
2006-02-13 10:02:35 +01:00
|
|
|
log_warn(LD_CONTROL, "Error in control command. Failing.");
|
2004-11-03 02:32:26 +01:00
|
|
|
return -1;
|
|
|
|
case 0:
|
|
|
|
/* Control command not all here yet. Wait. */
|
|
|
|
return 0;
|
|
|
|
case 1:
|
|
|
|
/* We got a command. Process it. */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
tor_assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We got a command. If we need authentication, only authentication
|
|
|
|
* commands will be considered. */
|
2006-07-26 21:07:26 +02:00
|
|
|
if (conn->_base.state == CONTROL_CONN_STATE_NEEDAUTH_V0 &&
|
2005-06-17 20:49:55 +02:00
|
|
|
command_type != CONTROL0_CMD_AUTHENTICATE) {
|
2006-02-13 10:02:35 +01:00
|
|
|
log_info(LD_CONTROL, "Rejecting '%s' command; authentication needed.",
|
|
|
|
control_cmd_to_string(command_type));
|
2005-06-17 20:49:55 +02:00
|
|
|
send_control0_error(conn, ERR_UNAUTHORIZED, "Authentication required");
|
2004-11-03 02:32:26 +01:00
|
|
|
tor_free(body);
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
2005-06-17 20:49:55 +02:00
|
|
|
if (command_type == CONTROL0_CMD_FRAGMENTHEADER ||
|
|
|
|
command_type == CONTROL0_CMD_FRAGMENT) {
|
2005-04-28 00:01:34 +02:00
|
|
|
if (handle_control_fragments(conn, command_type, body_len, body))
|
|
|
|
return -1;
|
|
|
|
tor_free(body);
|
|
|
|
if (conn->incoming_cmd_cur_len != conn->incoming_cmd_len)
|
|
|
|
goto again;
|
|
|
|
|
|
|
|
command_type = conn->incoming_cmd_type;
|
|
|
|
body_len = conn->incoming_cmd_len;
|
|
|
|
body = conn->incoming_cmd;
|
|
|
|
conn->incoming_cmd = NULL;
|
|
|
|
} else if (conn->incoming_cmd) {
|
2006-02-13 10:02:35 +01:00
|
|
|
log_warn(LD_CONTROL, "Dropping incomplete fragmented command");
|
2005-04-28 00:01:34 +02:00
|
|
|
tor_free(conn->incoming_cmd);
|
|
|
|
}
|
|
|
|
|
2004-11-07 22:37:50 +01:00
|
|
|
/* Okay, we're willing to process the command. */
|
2004-11-28 10:05:49 +01:00
|
|
|
switch (command_type)
|
2004-11-03 02:32:26 +01:00
|
|
|
{
|
2005-06-17 20:49:55 +02:00
|
|
|
case CONTROL0_CMD_SETCONF:
|
2004-11-03 02:32:26 +01:00
|
|
|
if (handle_control_setconf(conn, body_len, body))
|
|
|
|
return -1;
|
|
|
|
break;
|
2005-06-17 20:49:55 +02:00
|
|
|
case CONTROL0_CMD_GETCONF:
|
2004-11-03 02:32:26 +01:00
|
|
|
if (handle_control_getconf(conn, body_len, body))
|
|
|
|
return -1;
|
|
|
|
break;
|
2005-06-17 20:49:55 +02:00
|
|
|
case CONTROL0_CMD_SETEVENTS:
|
2004-11-03 02:32:26 +01:00
|
|
|
if (handle_control_setevents(conn, body_len, body))
|
|
|
|
return -1;
|
|
|
|
break;
|
2005-06-17 20:49:55 +02:00
|
|
|
case CONTROL0_CMD_AUTHENTICATE:
|
2004-11-03 02:32:26 +01:00
|
|
|
if (handle_control_authenticate(conn, body_len, body))
|
|
|
|
return -1;
|
|
|
|
break;
|
2005-06-17 20:49:55 +02:00
|
|
|
case CONTROL0_CMD_SAVECONF:
|
2004-11-07 23:37:59 +01:00
|
|
|
if (handle_control_saveconf(conn, body_len, body))
|
|
|
|
return -1;
|
|
|
|
break;
|
2005-06-17 20:49:55 +02:00
|
|
|
case CONTROL0_CMD_SIGNAL:
|
2005-01-05 07:40:47 +01:00
|
|
|
if (handle_control_signal(conn, body_len, body))
|
|
|
|
return -1;
|
|
|
|
break;
|
2005-06-17 20:49:55 +02:00
|
|
|
case CONTROL0_CMD_MAPADDRESS:
|
2005-02-25 07:16:28 +01:00
|
|
|
if (handle_control_mapaddress(conn, body_len, body))
|
|
|
|
return -1;
|
|
|
|
break;
|
2005-06-17 20:49:55 +02:00
|
|
|
case CONTROL0_CMD_GETINFO:
|
2005-02-25 07:16:28 +01:00
|
|
|
if (handle_control_getinfo(conn, body_len, body))
|
|
|
|
return -1;
|
|
|
|
break;
|
2005-06-17 20:49:55 +02:00
|
|
|
case CONTROL0_CMD_EXTENDCIRCUIT:
|
2005-02-25 07:16:28 +01:00
|
|
|
if (handle_control_extendcircuit(conn, body_len, body))
|
|
|
|
return -1;
|
|
|
|
break;
|
2005-06-17 20:49:55 +02:00
|
|
|
case CONTROL0_CMD_ATTACHSTREAM:
|
2005-02-25 07:16:28 +01:00
|
|
|
if (handle_control_attachstream(conn, body_len, body))
|
|
|
|
return -1;
|
|
|
|
break;
|
2005-06-17 20:49:55 +02:00
|
|
|
case CONTROL0_CMD_POSTDESCRIPTOR:
|
2005-02-25 07:16:28 +01:00
|
|
|
if (handle_control_postdescriptor(conn, body_len, body))
|
|
|
|
return -1;
|
|
|
|
break;
|
2005-06-17 20:49:55 +02:00
|
|
|
case CONTROL0_CMD_REDIRECTSTREAM:
|
2005-03-19 07:05:55 +01:00
|
|
|
if (handle_control_redirectstream(conn, body_len, body))
|
|
|
|
return -1;
|
|
|
|
break;
|
2005-06-17 20:49:55 +02:00
|
|
|
case CONTROL0_CMD_CLOSESTREAM:
|
2005-03-22 20:36:38 +01:00
|
|
|
if (handle_control_closestream(conn, body_len, body))
|
|
|
|
return -1;
|
|
|
|
break;
|
2005-06-17 20:49:55 +02:00
|
|
|
case CONTROL0_CMD_CLOSECIRCUIT:
|
2005-03-22 20:36:38 +01:00
|
|
|
if (handle_control_closecircuit(conn, body_len, body))
|
|
|
|
return -1;
|
|
|
|
break;
|
2005-06-17 20:49:55 +02:00
|
|
|
case CONTROL0_CMD_ERROR:
|
|
|
|
case CONTROL0_CMD_DONE:
|
|
|
|
case CONTROL0_CMD_CONFVALUE:
|
|
|
|
case CONTROL0_CMD_EVENT:
|
|
|
|
case CONTROL0_CMD_INFOVALUE:
|
2006-02-13 10:02:35 +01:00
|
|
|
log_warn(LD_CONTROL, "Received client-only '%s' command; ignoring.",
|
|
|
|
control_cmd_to_string(command_type));
|
2005-06-17 20:49:55 +02:00
|
|
|
send_control0_error(conn, ERR_UNRECOGNIZED_TYPE,
|
2004-11-03 11:08:44 +01:00
|
|
|
"Command type only valid from server to tor client");
|
2004-11-03 02:32:26 +01:00
|
|
|
break;
|
2005-06-17 20:49:55 +02:00
|
|
|
case CONTROL0_CMD_FRAGMENTHEADER:
|
|
|
|
case CONTROL0_CMD_FRAGMENT:
|
2006-02-13 10:02:35 +01:00
|
|
|
log_warn(LD_CONTROL,
|
|
|
|
"Recieved command fragment out of order; ignoring.");
|
2005-06-17 20:49:55 +02:00
|
|
|
send_control0_error(conn, ERR_SYNTAX, "Bad fragmentation on command.");
|
2004-11-03 02:32:26 +01:00
|
|
|
default:
|
2006-02-13 10:02:35 +01:00
|
|
|
log_warn(LD_CONTROL, "Received unrecognized command type %d; ignoring.",
|
|
|
|
(int)command_type);
|
2005-06-17 20:49:55 +02:00
|
|
|
send_control0_error(conn, ERR_UNRECOGNIZED_TYPE,
|
2004-11-03 02:32:26 +01:00
|
|
|
"Unrecognized command type");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tor_free(body);
|
|
|
|
goto again; /* There might be more data. */
|
|
|
|
}
|
|
|
|
|
2005-06-17 20:49:55 +02:00
|
|
|
/** Called when <b>conn</b> has received more bytes on its inbuf.
|
|
|
|
*/
|
|
|
|
int
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_control_process_inbuf(control_connection_t *conn)
|
2005-06-17 20:49:55 +02:00
|
|
|
{
|
|
|
|
tor_assert(conn);
|
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
if (STATE_IS_V0(conn->_base.state))
|
2005-06-17 20:49:55 +02:00
|
|
|
return connection_control_process_inbuf_v0(conn);
|
|
|
|
else
|
|
|
|
return connection_control_process_inbuf_v1(conn);
|
|
|
|
}
|
|
|
|
|
2006-10-09 17:47:50 +02:00
|
|
|
static const char *
|
|
|
|
circuit_end_reason_to_string(int reason)
|
|
|
|
{
|
2006-10-20 01:04:49 +02:00
|
|
|
if (reason >= 0 && reason & END_CIRC_REASON_FLAG_REMOTE)
|
|
|
|
reason &= ~END_CIRC_REASON_FLAG_REMOTE;
|
2006-10-09 17:47:50 +02:00
|
|
|
switch (reason) {
|
|
|
|
case END_CIRC_AT_ORIGIN:
|
|
|
|
/* This shouldn't get passed here; it's a catch-all reason. */
|
2006-10-20 01:04:49 +02:00
|
|
|
return "ORIGIN";
|
2006-10-09 17:47:50 +02:00
|
|
|
case END_CIRC_REASON_NONE:
|
|
|
|
/* This shouldn't get passed here; it's a catch-all reason. */
|
2006-10-20 01:04:49 +02:00
|
|
|
return "NONE";
|
2006-10-09 17:47:50 +02:00
|
|
|
case END_CIRC_REASON_TORPROTOCOL:
|
2006-10-20 01:04:49 +02:00
|
|
|
return "TORPROTOCOL";
|
2006-10-09 17:47:50 +02:00
|
|
|
case END_CIRC_REASON_INTERNAL:
|
2006-10-20 01:04:49 +02:00
|
|
|
return "INTERNAL";
|
2006-10-09 17:47:50 +02:00
|
|
|
case END_CIRC_REASON_REQUESTED:
|
2006-10-20 01:04:49 +02:00
|
|
|
return "REQUESTED";
|
2006-10-09 17:47:50 +02:00
|
|
|
case END_CIRC_REASON_HIBERNATING:
|
2006-10-20 01:04:49 +02:00
|
|
|
return "HIBERNATING";
|
2006-10-09 17:47:50 +02:00
|
|
|
case END_CIRC_REASON_RESOURCELIMIT:
|
2006-10-20 01:04:49 +02:00
|
|
|
return "RESOURCELIMIT";
|
2006-10-09 17:47:50 +02:00
|
|
|
case END_CIRC_REASON_CONNECTFAILED:
|
2006-10-20 01:04:49 +02:00
|
|
|
return "CONNECTFAILED";
|
2006-10-09 17:47:50 +02:00
|
|
|
case END_CIRC_REASON_OR_IDENTITY:
|
2006-10-20 01:04:49 +02:00
|
|
|
return "OR_IDENTITY";
|
2006-10-09 17:47:50 +02:00
|
|
|
case END_CIRC_REASON_OR_CONN_CLOSED:
|
2006-10-20 01:04:49 +02:00
|
|
|
return "OR_CONN_CLOSED";
|
2006-10-17 17:20:00 +02:00
|
|
|
case END_CIRC_REASON_FINISHED:
|
2006-10-20 01:04:49 +02:00
|
|
|
return "FINISHED";
|
2006-10-17 17:20:00 +02:00
|
|
|
case END_CIRC_REASON_TIMEOUT:
|
2006-10-20 01:04:49 +02:00
|
|
|
return "TIMEOUT";
|
2006-10-17 17:20:00 +02:00
|
|
|
case END_CIRC_REASON_DESTROYED:
|
2006-10-20 01:04:49 +02:00
|
|
|
return "DESTROYED";
|
2006-10-17 17:20:00 +02:00
|
|
|
case END_CIRC_REASON_NOPATH:
|
2006-10-20 01:04:49 +02:00
|
|
|
return "NOPATH";
|
2006-10-17 17:20:00 +02:00
|
|
|
case END_CIRC_REASON_NOSUCHSERVICE:
|
2006-10-20 01:04:49 +02:00
|
|
|
return "NOSUCHSERVICE";
|
2006-10-09 17:47:50 +02:00
|
|
|
default:
|
|
|
|
log_warn(LD_BUG, "Unrecognized reason code %d", (int)reason);
|
2006-10-20 01:04:49 +02:00
|
|
|
return NULL;
|
2006-10-09 17:47:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-07 22:37:50 +01:00
|
|
|
/** Something has happened to circuit <b>circ</b>: tell any interested
|
|
|
|
* control connections. */
|
|
|
|
int
|
2006-10-09 17:47:27 +02:00
|
|
|
control_event_circuit_status(origin_circuit_t *circ, circuit_status_event_t tp,
|
2006-10-09 17:47:50 +02:00
|
|
|
int reason_code)
|
2004-11-03 02:32:26 +01:00
|
|
|
{
|
2006-10-03 20:58:40 +02:00
|
|
|
char *path=NULL, *msg;
|
2004-11-03 02:32:26 +01:00
|
|
|
if (!EVENT_IS_INTERESTING(EVENT_CIRCUIT_STATUS))
|
|
|
|
return 0;
|
2004-11-03 19:33:07 +01:00
|
|
|
tor_assert(circ);
|
|
|
|
|
2006-10-03 20:58:40 +02:00
|
|
|
if (EVENT_IS_INTERESTING0(EVENT_CIRCUIT_STATUS) ||
|
|
|
|
EVENT_IS_INTERESTING1S(EVENT_CIRCUIT_STATUS))
|
|
|
|
path = circuit_list_path(circ,0);
|
2005-06-17 22:37:21 +02:00
|
|
|
if (EVENT_IS_INTERESTING0(EVENT_CIRCUIT_STATUS)) {
|
|
|
|
size_t path_len = strlen(path);
|
|
|
|
msg = tor_malloc(1+4+path_len+1); /* event, circid, path, NUL. */
|
|
|
|
msg[0] = (uint8_t) tp;
|
2006-07-28 17:11:11 +02:00
|
|
|
set_uint32(msg+1, htonl(circ->global_identifier));
|
2005-06-17 22:37:21 +02:00
|
|
|
strlcpy(msg+5,path,path_len+1);
|
|
|
|
|
|
|
|
send_control0_event(EVENT_CIRCUIT_STATUS, (uint32_t)(path_len+6), msg);
|
|
|
|
tor_free(msg);
|
|
|
|
}
|
|
|
|
if (EVENT_IS_INTERESTING1(EVENT_CIRCUIT_STATUS)) {
|
|
|
|
const char *status;
|
2006-10-20 01:04:49 +02:00
|
|
|
char reason_buf[64];
|
2006-10-21 01:23:19 +02:00
|
|
|
int providing_reason=0;
|
2005-06-17 22:37:21 +02:00
|
|
|
switch (tp)
|
|
|
|
{
|
|
|
|
case CIRC_EVENT_LAUNCHED: status = "LAUNCHED"; break;
|
|
|
|
case CIRC_EVENT_BUILT: status = "BUILT"; break;
|
|
|
|
case CIRC_EVENT_EXTENDED: status = "EXTENDED"; break;
|
|
|
|
case CIRC_EVENT_FAILED: status = "FAILED"; break;
|
|
|
|
case CIRC_EVENT_CLOSED: status = "CLOSED"; break;
|
|
|
|
default:
|
2006-02-13 10:02:35 +01:00
|
|
|
log_warn(LD_BUG, "Unrecognized status code %d", (int)tp);
|
2005-06-17 22:37:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2006-10-09 17:47:27 +02:00
|
|
|
|
2006-10-09 17:47:50 +02:00
|
|
|
if (tp == CIRC_EVENT_FAILED || tp == CIRC_EVENT_CLOSED) {
|
2006-10-20 01:04:49 +02:00
|
|
|
const char *reason_str = circuit_end_reason_to_string(reason_code);
|
|
|
|
char *reason = NULL;
|
2006-10-21 01:23:19 +02:00
|
|
|
providing_reason=1;
|
2006-10-20 01:04:49 +02:00
|
|
|
if (!reason_str) {
|
|
|
|
reason = tor_malloc(16);
|
|
|
|
tor_snprintf(reason, 16, "UNKNOWN_%d", reason_code);
|
|
|
|
reason_str = reason;
|
|
|
|
}
|
|
|
|
if (reason_code > 0 && reason_code & END_CIRC_REASON_FLAG_REMOTE) {
|
|
|
|
tor_snprintf(reason_buf, sizeof(reason_buf),
|
|
|
|
"REASON=DESTROYED REMOTE_REASON=%s", reason_str);
|
|
|
|
} else {
|
|
|
|
tor_snprintf(reason_buf, sizeof(reason_buf),
|
|
|
|
"REASON=%s", reason_str);
|
|
|
|
}
|
|
|
|
tor_free(reason);
|
2006-10-09 17:47:27 +02:00
|
|
|
}
|
|
|
|
|
2006-10-03 20:58:40 +02:00
|
|
|
if (EVENT_IS_INTERESTING1S(EVENT_CIRCUIT_STATUS)) {
|
2006-10-13 07:42:57 +02:00
|
|
|
const char *sp = strlen(path) ? " " : "";
|
2006-10-21 01:23:19 +02:00
|
|
|
if (providing_reason)
|
|
|
|
send_control1_event_extended(EVENT_CIRCUIT_STATUS, SHORT_NAMES,
|
|
|
|
"650 CIRC %lu %s%s%s@%s\r\n",
|
|
|
|
(unsigned long)circ->global_identifier,
|
|
|
|
status, sp, path, reason_buf);
|
|
|
|
else
|
|
|
|
send_control1_event_extended(EVENT_CIRCUIT_STATUS, SHORT_NAMES,
|
|
|
|
"650 CIRC %lu %s%s%s\r\n",
|
|
|
|
(unsigned long)circ->global_identifier,
|
|
|
|
status, sp, path);
|
2006-10-03 20:58:40 +02:00
|
|
|
}
|
|
|
|
if (EVENT_IS_INTERESTING1L(EVENT_CIRCUIT_STATUS)) {
|
|
|
|
char *vpath = circuit_list_path_for_controller(circ);
|
2006-10-13 07:42:57 +02:00
|
|
|
const char *sp = strlen(vpath) ? " " : "";
|
2006-10-21 01:23:19 +02:00
|
|
|
if (providing_reason)
|
|
|
|
send_control1_event_extended(EVENT_CIRCUIT_STATUS, LONG_NAMES,
|
|
|
|
"650 CIRC %lu %s%s%s@%s\r\n",
|
|
|
|
(unsigned long)circ->global_identifier,
|
|
|
|
status, sp, vpath, reason_buf);
|
|
|
|
else
|
|
|
|
send_control1_event_extended(EVENT_CIRCUIT_STATUS, LONG_NAMES,
|
|
|
|
"650 CIRC %lu %s%s%s\r\n",
|
|
|
|
(unsigned long)circ->global_identifier,
|
|
|
|
status, sp, vpath);
|
2006-10-03 20:58:40 +02:00
|
|
|
tor_free(vpath);
|
|
|
|
}
|
2005-06-17 22:37:21 +02:00
|
|
|
}
|
2004-11-03 19:33:07 +01:00
|
|
|
tor_free(path);
|
2005-06-17 22:37:21 +02:00
|
|
|
|
2004-11-03 02:32:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Given an AP connection <b>conn</b> and a <b>len</b>-character buffer
|
|
|
|
* <b>buf</b>, determine the address:port combination requested on
|
|
|
|
* <b>conn</b>, and write it to <b>buf</b>. Return 0 on success, -1 on
|
|
|
|
* failure. */
|
2005-06-19 22:40:41 +02:00
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
write_stream_target_to_buf(edge_connection_t *conn, char *buf, size_t len)
|
2005-06-19 22:40:41 +02:00
|
|
|
{
|
|
|
|
char buf2[256];
|
|
|
|
if (conn->chosen_exit_name)
|
|
|
|
if (tor_snprintf(buf2, sizeof(buf2), ".%s.exit", conn->chosen_exit_name)<0)
|
|
|
|
return -1;
|
2005-11-17 00:54:24 +01:00
|
|
|
if (tor_snprintf(buf, len, "%s%s%s:%d",
|
2005-12-14 21:40:40 +01:00
|
|
|
conn->socks_request->address,
|
|
|
|
conn->chosen_exit_name ? buf2 : "",
|
|
|
|
!conn->chosen_exit_name &&
|
|
|
|
connection_edge_is_rendezvous_stream(conn) ? ".onion" : "",
|
|
|
|
conn->socks_request->port)<0)
|
2005-06-19 22:40:41 +02:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-20 19:54:36 +02:00
|
|
|
/* DOCDOC */
|
|
|
|
static const char *
|
|
|
|
stream_end_reason_to_string(int reason)
|
|
|
|
{
|
|
|
|
reason &= ~END_CIRC_REASON_FLAG_REMOTE;
|
|
|
|
switch (reason) {
|
|
|
|
case END_STREAM_REASON_MISC: return "MISC";
|
|
|
|
case END_STREAM_REASON_RESOLVEFAILED: return "RESOLVEFAILED";
|
|
|
|
case END_STREAM_REASON_CONNECTREFUSED: return "CONNECTREFUSED";
|
|
|
|
case END_STREAM_REASON_EXITPOLICY: return "EXITPOLICY";
|
|
|
|
case END_STREAM_REASON_DESTROY: return "DESTROY";
|
|
|
|
case END_STREAM_REASON_DONE: return "DONE";
|
|
|
|
case END_STREAM_REASON_TIMEOUT: return "TIMEOUT";
|
|
|
|
case END_STREAM_REASON_HIBERNATING: return "HIBERNATING";
|
|
|
|
case END_STREAM_REASON_INTERNAL: return "INTERNAL";
|
|
|
|
case END_STREAM_REASON_RESOURCELIMIT: return "RESOURCELIMIT";
|
|
|
|
case END_STREAM_REASON_CONNRESET: return "CONNRESET";
|
|
|
|
case END_STREAM_REASON_TORPROTOCOL: return "TORPROTOCOL";
|
|
|
|
case END_STREAM_REASON_NOTDIRECTORY: return "NOTDIRECTORY";
|
2006-10-20 19:54:43 +02:00
|
|
|
|
|
|
|
case END_STREAM_REASON_ALREADY_SOCKS_REPLIED: return "INTERNAL";
|
|
|
|
case END_STREAM_REASON_CANT_ATTACH: return "CANT_ATTACH";
|
|
|
|
case END_STREAM_REASON_NET_UNREACHABLE: return "NET_UNREACHABLE";
|
|
|
|
case END_STREAM_REASON_SOCKSPROTOCOL: return "SOCKS_PROTOCOL";
|
|
|
|
|
2006-10-20 19:54:36 +02:00
|
|
|
default: return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-07 22:37:50 +01:00
|
|
|
/** Something has happened to the stream associated with AP connection
|
|
|
|
* <b>conn</b>: tell any interested control connections. */
|
|
|
|
int
|
2006-10-20 19:54:36 +02:00
|
|
|
control_event_stream_status(edge_connection_t *conn, stream_status_event_t tp,
|
|
|
|
int reason_code)
|
2004-11-03 02:32:26 +01:00
|
|
|
{
|
2004-11-03 19:33:07 +01:00
|
|
|
char *msg;
|
|
|
|
size_t len;
|
2005-06-19 22:40:41 +02:00
|
|
|
char buf[256];
|
2004-11-03 19:33:07 +01:00
|
|
|
tor_assert(conn->socks_request);
|
2004-11-03 02:32:26 +01:00
|
|
|
|
|
|
|
if (!EVENT_IS_INTERESTING(EVENT_STREAM_STATUS))
|
|
|
|
return 0;
|
|
|
|
|
2005-06-19 22:40:41 +02:00
|
|
|
write_stream_target_to_buf(conn, buf, sizeof(buf));
|
2005-06-17 22:37:21 +02:00
|
|
|
if (EVENT_IS_INTERESTING0(EVENT_STREAM_STATUS)) {
|
|
|
|
len = strlen(buf);
|
|
|
|
msg = tor_malloc(5+len+1);
|
|
|
|
msg[0] = (uint8_t) tp;
|
2006-07-28 17:11:11 +02:00
|
|
|
set_uint32(msg+1, htonl(conn->global_identifier));
|
2005-06-17 22:37:21 +02:00
|
|
|
strlcpy(msg+5, buf, len+1);
|
|
|
|
|
|
|
|
send_control0_event(EVENT_STREAM_STATUS, (uint32_t)(5+len+1), msg);
|
|
|
|
tor_free(msg);
|
|
|
|
}
|
2005-07-18 00:28:01 +02:00
|
|
|
if (EVENT_IS_INTERESTING1(EVENT_STREAM_STATUS)) {
|
2006-10-20 19:54:36 +02:00
|
|
|
char reason_buf[64];
|
2005-06-17 22:37:21 +02:00
|
|
|
const char *status;
|
2005-06-19 22:40:41 +02:00
|
|
|
circuit_t *circ;
|
2006-07-28 17:11:11 +02:00
|
|
|
origin_circuit_t *origin_circ = NULL;
|
2006-10-20 19:54:36 +02:00
|
|
|
reason_buf[0] = '\0';
|
2005-06-17 22:37:21 +02:00
|
|
|
switch (tp)
|
|
|
|
{
|
|
|
|
case STREAM_EVENT_SENT_CONNECT: status = "SENTCONNECT"; break;
|
|
|
|
case STREAM_EVENT_SENT_RESOLVE: status = "SENTRESOLVE"; break;
|
|
|
|
case STREAM_EVENT_SUCCEEDED: status = "SUCCEEDED"; break;
|
|
|
|
case STREAM_EVENT_FAILED: status = "FAILED"; break;
|
|
|
|
case STREAM_EVENT_CLOSED: status = "CLOSED"; break;
|
|
|
|
case STREAM_EVENT_NEW: status = "NEW"; break;
|
|
|
|
case STREAM_EVENT_NEW_RESOLVE: status = "NEWRESOLVE"; break;
|
|
|
|
case STREAM_EVENT_FAILED_RETRIABLE: status = "DETACHED"; break;
|
|
|
|
default:
|
2006-02-13 10:02:35 +01:00
|
|
|
log_warn(LD_BUG, "Unrecognized status code %d", (int)tp);
|
2005-06-17 22:37:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2006-10-20 19:54:36 +02:00
|
|
|
if (reason_code && (tp == STREAM_EVENT_FAILED ||
|
|
|
|
tp == STREAM_EVENT_CLOSED ||
|
|
|
|
tp == STREAM_EVENT_FAILED_RETRIABLE)) {
|
|
|
|
const char *reason_str = stream_end_reason_to_string(reason_code);
|
|
|
|
char *r = NULL;
|
|
|
|
if (!reason_str) {
|
|
|
|
r = tor_malloc(16);
|
|
|
|
tor_snprintf(r, 16, "UNKNOWN_%d", reason_code);
|
|
|
|
reason_str = r;
|
|
|
|
}
|
|
|
|
if (reason_code & END_STREAM_REASON_FLAG_REMOTE)
|
|
|
|
tor_snprintf(reason_buf, sizeof(reason_buf),
|
|
|
|
"REASON=END REMOTE_REASON=%s", reason_str);
|
|
|
|
else
|
|
|
|
tor_snprintf(reason_buf, sizeof(reason_buf),
|
|
|
|
"REASON=%s", reason_str);
|
|
|
|
tor_free(r);
|
|
|
|
}
|
2005-06-19 22:40:41 +02:00
|
|
|
circ = circuit_get_by_edge_conn(conn);
|
2006-07-28 17:11:11 +02:00
|
|
|
if (circ && CIRCUIT_IS_ORIGIN(circ))
|
|
|
|
origin_circ = TO_ORIGIN_CIRCUIT(circ);
|
2006-10-20 19:54:36 +02:00
|
|
|
send_control1_event_extended(EVENT_STREAM_STATUS, ALL_NAMES,
|
|
|
|
"650 STREAM %lu %s %lu %s@%s\r\n",
|
2006-07-28 17:11:11 +02:00
|
|
|
(unsigned long)conn->global_identifier, status,
|
|
|
|
origin_circ?
|
2006-08-10 11:02:12 +02:00
|
|
|
(unsigned long)origin_circ->global_identifier : 0ul,
|
2006-10-20 19:54:36 +02:00
|
|
|
buf, reason_buf);
|
2005-08-15 05:25:40 +02:00
|
|
|
/* XXX need to specify its intended exit, etc? */
|
2005-06-17 22:37:21 +02:00
|
|
|
}
|
2004-11-03 02:32:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-03 21:00:18 +02:00
|
|
|
static void
|
|
|
|
orconn_target_get_name(int long_names,
|
|
|
|
char *name, size_t len, or_connection_t *conn)
|
|
|
|
{
|
2006-10-09 20:19:32 +02:00
|
|
|
if (! long_names) {
|
2006-10-03 21:00:18 +02:00
|
|
|
if (conn->nickname)
|
|
|
|
strlcpy(name, conn->nickname, len);
|
|
|
|
else
|
|
|
|
tor_snprintf(name, len, "%s:%d",
|
|
|
|
conn->_base.address, conn->_base.port);
|
|
|
|
} else {
|
|
|
|
routerinfo_t *ri = router_get_by_digest(conn->identity_digest);
|
|
|
|
if (ri) {
|
|
|
|
tor_assert(len > MAX_VERBOSE_NICKNAME_LEN);
|
|
|
|
router_get_verbose_nickname(name, ri);
|
|
|
|
} else if (! tor_digest_is_zero(conn->identity_digest)) {
|
|
|
|
name[0] = '$';
|
|
|
|
base16_encode(name+1, len-1, conn->identity_digest,
|
|
|
|
DIGEST_LEN);
|
2006-10-13 07:27:59 +02:00
|
|
|
} else {
|
2006-10-09 20:19:32 +02:00
|
|
|
tor_snprintf(name, len, "%s:%d",
|
2006-10-03 21:00:18 +02:00
|
|
|
conn->_base.address, conn->_base.port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-07 22:37:50 +01:00
|
|
|
/** Something has happened to the OR connection <b>conn</b>: tell any
|
|
|
|
* interested control connections. */
|
|
|
|
int
|
2006-07-26 21:07:26 +02:00
|
|
|
control_event_or_conn_status(or_connection_t *conn,or_conn_status_event_t tp)
|
2004-11-03 02:32:26 +01:00
|
|
|
{
|
2004-11-03 19:33:07 +01:00
|
|
|
char buf[HEX_DIGEST_LEN+3]; /* status, dollar, identity, NUL */
|
|
|
|
size_t len;
|
|
|
|
|
2004-11-03 02:32:26 +01:00
|
|
|
if (!EVENT_IS_INTERESTING(EVENT_OR_CONN_STATUS))
|
|
|
|
return 0;
|
|
|
|
|
2005-06-17 22:37:21 +02:00
|
|
|
if (EVENT_IS_INTERESTING0(EVENT_OR_CONN_STATUS)) {
|
|
|
|
buf[0] = (uint8_t)tp;
|
2006-03-08 23:29:08 +01:00
|
|
|
strlcpy(buf+1,conn->nickname ? conn->nickname : "",sizeof(buf)-1);
|
2005-06-17 22:37:21 +02:00
|
|
|
len = strlen(buf+1);
|
|
|
|
send_control0_event(EVENT_OR_CONN_STATUS, (uint32_t)(len+1), buf);
|
|
|
|
}
|
|
|
|
if (EVENT_IS_INTERESTING1(EVENT_OR_CONN_STATUS)) {
|
|
|
|
const char *status;
|
2006-03-08 23:29:08 +01:00
|
|
|
char name[128];
|
2005-06-17 22:37:21 +02:00
|
|
|
switch (tp)
|
|
|
|
{
|
|
|
|
case OR_CONN_EVENT_LAUNCHED: status = "LAUNCHED"; break;
|
|
|
|
case OR_CONN_EVENT_CONNECTED: status = "CONNECTED"; break;
|
|
|
|
case OR_CONN_EVENT_FAILED: status = "FAILED"; break;
|
|
|
|
case OR_CONN_EVENT_CLOSED: status = "CLOSED"; break;
|
2006-07-11 05:33:16 +02:00
|
|
|
case OR_CONN_EVENT_NEW: status = "NEW"; break;
|
2005-06-17 22:37:21 +02:00
|
|
|
default:
|
2006-02-13 10:02:35 +01:00
|
|
|
log_warn(LD_BUG, "Unrecognized status code %d", (int)tp);
|
2005-06-17 22:37:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2006-10-03 20:59:52 +02:00
|
|
|
if (EVENT_IS_INTERESTING1S(EVENT_OR_CONN_STATUS)) {
|
2006-10-03 21:00:18 +02:00
|
|
|
orconn_target_get_name(0, name, sizeof(name), conn);
|
2006-10-03 20:59:52 +02:00
|
|
|
send_control1_event(EVENT_OR_CONN_STATUS, SHORT_NAMES,
|
|
|
|
"650 ORCONN %s %s\r\n",
|
|
|
|
name, status);
|
|
|
|
}
|
|
|
|
if (EVENT_IS_INTERESTING1L(EVENT_OR_CONN_STATUS)) {
|
2006-10-03 21:00:18 +02:00
|
|
|
orconn_target_get_name(1, name, sizeof(name), conn);
|
2006-10-03 20:59:52 +02:00
|
|
|
send_control1_event(EVENT_OR_CONN_STATUS, LONG_NAMES,
|
|
|
|
"650 ORCONN %s %s\r\n",
|
|
|
|
name, status);
|
|
|
|
}
|
2005-06-17 22:37:21 +02:00
|
|
|
}
|
2004-11-03 02:32:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-07 22:37:50 +01:00
|
|
|
/** A second or more has elapsed: tell any interested control
|
|
|
|
* connections how much bandwidth we used. */
|
|
|
|
int
|
|
|
|
control_event_bandwidth_used(uint32_t n_read, uint32_t n_written)
|
2004-11-03 02:32:26 +01:00
|
|
|
{
|
|
|
|
char buf[8];
|
|
|
|
|
2005-06-17 22:37:21 +02:00
|
|
|
if (EVENT_IS_INTERESTING0(EVENT_BANDWIDTH_USED)) {
|
|
|
|
set_uint32(buf, htonl(n_read));
|
|
|
|
set_uint32(buf+4, htonl(n_written));
|
|
|
|
send_control0_event(EVENT_BANDWIDTH_USED, 8, buf);
|
|
|
|
}
|
|
|
|
if (EVENT_IS_INTERESTING1(EVENT_BANDWIDTH_USED)) {
|
2006-10-03 20:58:40 +02:00
|
|
|
send_control1_event(EVENT_BANDWIDTH_USED, ALL_NAMES,
|
2005-06-17 22:37:21 +02:00
|
|
|
"650 BW %lu %lu\r\n",
|
|
|
|
(unsigned long)n_read,
|
|
|
|
(unsigned long)n_written);
|
|
|
|
}
|
2004-11-03 02:32:26 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Called when we are sending a log message to the controllers: suspend
|
|
|
|
* sending further log messages to the controllers until we're done. Used by
|
|
|
|
* CONN_LOG_PROTECT. */
|
2005-07-13 07:14:42 +02:00
|
|
|
void
|
|
|
|
disable_control_logging(void)
|
|
|
|
{
|
|
|
|
++disable_log_messages;
|
|
|
|
}
|
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** We're done sending a log message to the controllers: re-enable controller
|
|
|
|
* logging. Used by CONN_LOG_PROTECT. */
|
2005-07-13 07:14:42 +02:00
|
|
|
void
|
|
|
|
enable_control_logging(void)
|
|
|
|
{
|
|
|
|
if (--disable_log_messages < 0)
|
|
|
|
tor_assert(0);
|
|
|
|
}
|
|
|
|
|
2004-11-07 22:37:50 +01:00
|
|
|
/** We got a log message: tell any interested control connections. */
|
|
|
|
void
|
2005-10-25 09:02:13 +02:00
|
|
|
control_event_logmsg(int severity, unsigned int domain, const char *msg)
|
2004-11-03 02:32:26 +01:00
|
|
|
{
|
2005-04-18 00:52:02 +02:00
|
|
|
int oldlog, event;
|
2006-06-05 00:42:13 +02:00
|
|
|
(void) domain;
|
2005-04-18 00:52:02 +02:00
|
|
|
|
2005-07-13 07:14:42 +02:00
|
|
|
if (disable_log_messages)
|
2005-04-18 00:52:02 +02:00
|
|
|
return;
|
|
|
|
|
2005-06-18 04:39:25 +02:00
|
|
|
oldlog = EVENT_IS_INTERESTING0(EVENT_LOG_OBSOLETE) &&
|
2005-04-06 00:56:17 +02:00
|
|
|
(severity == LOG_NOTICE || severity == LOG_WARN || severity == LOG_ERR);
|
2005-04-18 00:52:02 +02:00
|
|
|
event = log_severity_to_event(severity);
|
2005-04-06 00:56:17 +02:00
|
|
|
|
2005-06-18 04:39:25 +02:00
|
|
|
if (event<0 || !EVENT_IS_INTERESTING0(event))
|
2005-04-06 00:56:17 +02:00
|
|
|
event = 0;
|
|
|
|
|
|
|
|
if (oldlog || event) {
|
|
|
|
size_t len = strlen(msg);
|
2005-09-30 22:04:55 +02:00
|
|
|
++disable_log_messages;
|
2005-04-06 00:56:17 +02:00
|
|
|
if (event)
|
2005-06-17 20:49:55 +02:00
|
|
|
send_control0_event(event, (uint32_t)(len+1), msg);
|
2005-04-06 00:56:17 +02:00
|
|
|
if (oldlog)
|
2005-06-17 20:49:55 +02:00
|
|
|
send_control0_event(EVENT_LOG_OBSOLETE, (uint32_t)(len+1), msg);
|
2005-09-30 22:04:55 +02:00
|
|
|
--disable_log_messages;
|
2005-04-06 00:56:17 +02:00
|
|
|
}
|
2005-06-18 04:39:25 +02:00
|
|
|
|
|
|
|
event = log_severity_to_event(severity);
|
|
|
|
if (event >= 0 && EVENT_IS_INTERESTING1(event)) {
|
|
|
|
char *b = NULL;
|
|
|
|
const char *s;
|
|
|
|
if (strchr(msg, '\n')) {
|
|
|
|
char *cp;
|
|
|
|
b = tor_strdup(msg);
|
|
|
|
for (cp = b; *cp; ++cp)
|
|
|
|
if (*cp == '\r' || *cp == '\n')
|
|
|
|
*cp = ' ';
|
|
|
|
}
|
2005-06-19 22:40:41 +02:00
|
|
|
switch (severity) {
|
2005-07-13 07:14:42 +02:00
|
|
|
case LOG_DEBUG: s = "DEBUG"; break;
|
2005-06-18 04:39:25 +02:00
|
|
|
case LOG_INFO: s = "INFO"; break;
|
|
|
|
case LOG_NOTICE: s = "NOTICE"; break;
|
|
|
|
case LOG_WARN: s = "WARN"; break;
|
|
|
|
case LOG_ERR: s = "ERR"; break;
|
|
|
|
default: s = "UnknownLogSeverity"; break;
|
|
|
|
}
|
2005-09-30 22:04:55 +02:00
|
|
|
++disable_log_messages;
|
2006-10-03 20:58:40 +02:00
|
|
|
send_control1_event(event, ALL_NAMES, "650 %s %s\r\n", s, b?b:msg);
|
2005-09-30 22:04:55 +02:00
|
|
|
--disable_log_messages;
|
2005-06-18 04:39:25 +02:00
|
|
|
tor_free(b);
|
|
|
|
}
|
2004-11-03 02:32:26 +01:00
|
|
|
}
|
|
|
|
|
2005-03-17 13:38:37 +01:00
|
|
|
/** Called whenever we receive new router descriptors: tell any
|
|
|
|
* interested control connections. <b>routers</b> is a list of
|
|
|
|
* DIGEST_LEN-byte identity digests.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
control_event_descriptors_changed(smartlist_t *routers)
|
2005-03-02 23:29:58 +01:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char *msg;
|
2006-10-03 20:58:40 +02:00
|
|
|
smartlist_t *identities = NULL;
|
2005-03-02 23:29:58 +01:00
|
|
|
char buf[HEX_DIGEST_LEN+1];
|
|
|
|
|
|
|
|
if (!EVENT_IS_INTERESTING(EVENT_NEW_DESC))
|
|
|
|
return 0;
|
2006-10-03 20:58:40 +02:00
|
|
|
if (EVENT_IS_INTERESTING0(EVENT_NEW_DESC) ||
|
|
|
|
EVENT_IS_INTERESTING1S(EVENT_NEW_DESC)) {
|
|
|
|
identities = smartlist_create();
|
|
|
|
SMARTLIST_FOREACH(routers, routerinfo_t *, r,
|
|
|
|
{
|
|
|
|
base16_encode(buf,sizeof(buf),r->cache_info.identity_digest,DIGEST_LEN);
|
|
|
|
smartlist_add(identities, tor_strdup(buf));
|
|
|
|
});
|
|
|
|
}
|
2005-06-17 22:37:21 +02:00
|
|
|
if (EVENT_IS_INTERESTING0(EVENT_NEW_DESC)) {
|
|
|
|
msg = smartlist_join_strings(identities, ",", 0, &len);
|
|
|
|
send_control0_event(EVENT_NEW_DESC, len+1, msg);
|
|
|
|
tor_free(msg);
|
|
|
|
}
|
2006-10-03 20:58:40 +02:00
|
|
|
if (EVENT_IS_INTERESTING1S(EVENT_NEW_DESC)) {
|
2006-05-23 01:24:06 +02:00
|
|
|
char *ids = smartlist_join_strings(identities, " ", 0, &len);
|
|
|
|
size_t len = strlen(ids)+32;
|
|
|
|
msg = tor_malloc(len);
|
|
|
|
tor_snprintf(msg, len, "650 NEWDESC %s\r\n", ids);
|
2006-10-09 17:47:50 +02:00
|
|
|
send_control1_event_string(EVENT_NEW_DESC, SHORT_NAMES|ALL_FORMATS, msg);
|
2006-05-23 01:24:06 +02:00
|
|
|
tor_free(ids);
|
2005-06-17 22:37:21 +02:00
|
|
|
tor_free(msg);
|
|
|
|
}
|
2006-10-03 20:58:40 +02:00
|
|
|
if (EVENT_IS_INTERESTING1L(EVENT_NEW_DESC)) {
|
2006-10-03 21:00:18 +02:00
|
|
|
smartlist_t *names = smartlist_create();
|
|
|
|
char *ids;
|
|
|
|
size_t len;
|
2006-10-03 20:58:40 +02:00
|
|
|
SMARTLIST_FOREACH(routers, routerinfo_t *, ri, {
|
|
|
|
char *b = tor_malloc(MAX_VERBOSE_NICKNAME_LEN+1);
|
2006-10-09 04:35:43 +02:00
|
|
|
router_get_verbose_nickname(b, ri);
|
2006-10-03 20:58:40 +02:00
|
|
|
smartlist_add(names, b);
|
|
|
|
});
|
2006-10-03 21:00:18 +02:00
|
|
|
ids = smartlist_join_strings(names, " ", 0, &len);
|
|
|
|
len = strlen(ids)+32;
|
2006-10-03 20:58:40 +02:00
|
|
|
msg = tor_malloc(len);
|
|
|
|
tor_snprintf(msg, len, "650 NEWDESC %s\r\n", ids);
|
2006-10-09 17:47:50 +02:00
|
|
|
send_control1_event_string(EVENT_NEW_DESC, LONG_NAMES|ALL_FORMATS, msg);
|
2006-10-03 20:58:40 +02:00
|
|
|
tor_free(ids);
|
|
|
|
tor_free(msg);
|
|
|
|
SMARTLIST_FOREACH(names, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(names);
|
|
|
|
}
|
|
|
|
if (identities) {
|
|
|
|
SMARTLIST_FOREACH(identities, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(identities);
|
|
|
|
}
|
2005-03-02 23:29:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Called whenever an address mapping on <b>from<b> from changes to <b>to</b>.
|
|
|
|
* <b>expires</b> values less than 3 are special; see connection_edge.c. */
|
2005-06-19 22:40:41 +02:00
|
|
|
int
|
|
|
|
control_event_address_mapped(const char *from, const char *to, time_t expires)
|
|
|
|
{
|
|
|
|
if (!EVENT_IS_INTERESTING1(EVENT_ADDRMAP))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (expires < 3)
|
2006-10-03 20:58:40 +02:00
|
|
|
send_control1_event(EVENT_ADDRMAP, ALL_NAMES,
|
2005-12-14 21:40:40 +01:00
|
|
|
"650 ADDRMAP %s %s NEVER\r\n", from, to);
|
2005-06-19 22:40:41 +02:00
|
|
|
else {
|
|
|
|
char buf[ISO_TIME_LEN+1];
|
|
|
|
format_local_iso_time(buf,expires);
|
2006-10-03 20:58:40 +02:00
|
|
|
send_control1_event(EVENT_ADDRMAP, ALL_NAMES,
|
|
|
|
"650 ADDRMAP %s %s \"%s\"\r\n",
|
2005-06-19 22:40:41 +02:00
|
|
|
from, to, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-19 19:35:43 +01:00
|
|
|
/** The authoritative dirserver has received a new descriptor that
|
|
|
|
* has passed basic syntax checks and is properly self-signed.
|
|
|
|
*
|
|
|
|
* Notify any interested party of the new descriptor and what has
|
|
|
|
* been done with it, and also optionally give an explanation/reason. */
|
|
|
|
int
|
2005-12-14 21:40:40 +01:00
|
|
|
control_event_or_authdir_new_descriptor(const char *action,
|
|
|
|
const char *descriptor,
|
|
|
|
const char *msg)
|
2005-11-19 19:35:43 +01:00
|
|
|
{
|
|
|
|
char firstline[1024];
|
|
|
|
char *buf;
|
|
|
|
int totallen;
|
|
|
|
char *esc = NULL;
|
|
|
|
size_t esclen;
|
|
|
|
|
|
|
|
if (!EVENT_IS_INTERESTING(EVENT_AUTHDIR_NEWDESCS))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
tor_snprintf(firstline, sizeof(firstline),
|
|
|
|
"650+AUTHDIR_NEWDESC=\r\n%s\r\n%s\r\n",
|
|
|
|
action,
|
|
|
|
msg ? msg : "");
|
|
|
|
|
|
|
|
/* Escape the server descriptor properly */
|
|
|
|
esclen = write_escaped_data(descriptor, strlen(descriptor), 1, &esc);
|
|
|
|
|
|
|
|
totallen = strlen(firstline) + esclen + 1;
|
|
|
|
buf = tor_malloc(totallen);
|
|
|
|
strlcpy(buf, firstline, totallen);
|
|
|
|
strlcpy(buf+strlen(firstline), esc, totallen);
|
2006-10-09 17:47:50 +02:00
|
|
|
send_control1_event_string(EVENT_AUTHDIR_NEWDESCS, ALL_NAMES|ALL_FORMATS,
|
|
|
|
buf);
|
2005-11-19 19:35:43 +01:00
|
|
|
|
|
|
|
tor_free(esc);
|
|
|
|
tor_free(buf);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-20 23:04:39 +02:00
|
|
|
/* DOCDOC takes a list of local_routerstatus_t */
|
|
|
|
int
|
|
|
|
control_event_networkstatus_changed(smartlist_t *statuses)
|
|
|
|
{
|
|
|
|
smartlist_t *strs;
|
|
|
|
char *s;
|
|
|
|
if (!EVENT_IS_INTERESTING(EVENT_NS) || !smartlist_len(statuses))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
strs = smartlist_create();
|
|
|
|
smartlist_add(strs, tor_strdup("650+NS\r\n"));
|
|
|
|
SMARTLIST_FOREACH(statuses, local_routerstatus_t *, rs,
|
|
|
|
{
|
|
|
|
s = networkstatus_getinfo_helper_single(&rs->status);
|
|
|
|
if (!s) continue;
|
|
|
|
smartlist_add(strs, s);
|
|
|
|
});
|
|
|
|
smartlist_add(strs, tor_strdup("\r\n.\r\n"));
|
|
|
|
|
|
|
|
s = smartlist_join_strings(strs, "", 0, NULL);
|
|
|
|
SMARTLIST_FOREACH(strs, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(strs);
|
|
|
|
send_control1_event_string(EVENT_NS, ALL_NAMES|ALL_FORMATS, s);
|
|
|
|
tor_free(s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-23 07:24:57 +02:00
|
|
|
/* DOCDOC */
|
2006-10-20 23:04:39 +02:00
|
|
|
int
|
|
|
|
control_event_networkstatus_changed_single(local_routerstatus_t *rs)
|
|
|
|
{
|
|
|
|
smartlist_t *statuses;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (!EVENT_IS_INTERESTING(EVENT_NS))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
statuses = smartlist_create();
|
|
|
|
smartlist_add(statuses, rs);
|
|
|
|
r = control_event_networkstatus_changed(statuses);
|
|
|
|
smartlist_free(statuses);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2006-09-29 20:13:25 +02:00
|
|
|
/** Our own router descriptor has changed; tell any controllers that care.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
control_event_my_descriptor_changed(void)
|
|
|
|
{
|
2006-10-03 20:58:40 +02:00
|
|
|
send_control1_event(EVENT_DESCCHANGED, ALL_NAMES, "650 DESCCHANGED\r\n");
|
2006-09-29 20:13:25 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-23 07:24:57 +02:00
|
|
|
/* DOCDOC */
|
|
|
|
static int
|
|
|
|
control_event_status(int type, int severity, const char *format, va_list args)
|
|
|
|
{
|
|
|
|
char format_buf[160];
|
|
|
|
const char *status, *sev;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case EVENT_STATUS_GENERAL:
|
|
|
|
status = "STATUS_GENERAL";
|
|
|
|
break;
|
|
|
|
case EVENT_STATUS_CLIENT:
|
|
|
|
status = "STATUS_CLIENT";
|
|
|
|
break;
|
|
|
|
case EVENT_STATUS_SERVER:
|
|
|
|
status = "STATUS_SEVER";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
log_warn(LD_BUG, "Unrecognized status type %d", type);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
switch (severity) {
|
|
|
|
case LOG_NOTICE:
|
|
|
|
sev = "NOTICE";
|
|
|
|
break;
|
|
|
|
case LOG_WARN:
|
|
|
|
sev = "WARN";
|
|
|
|
break;
|
|
|
|
case LOG_ERR:
|
|
|
|
sev = "ERR";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
log_warn(LD_BUG, "Unrecognized status severity %d", severity);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (tor_snprintf(format_buf, sizeof(format_buf), "650 %s %s %s\r\n",
|
|
|
|
status, sev, format)<0) {
|
|
|
|
log_warn(LD_BUG, "Format string too long.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
send_control1_event_impl(type, ALL_NAMES|ALL_FORMATS, 0, format_buf, args);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* DOCDOC */
|
|
|
|
int
|
|
|
|
control_event_general_status(int severity, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int r;
|
|
|
|
if (!EVENT_IS_INTERESTING1(EVENT_STATUS_GENERAL))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
r = control_event_status(EVENT_STATUS_GENERAL, severity, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* DOCDOC */
|
|
|
|
int
|
|
|
|
control_event_client_status(int severity, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int r;
|
|
|
|
if (!EVENT_IS_INTERESTING1(EVENT_STATUS_CLIENT))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
r = control_event_status(EVENT_STATUS_CLIENT, severity, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* DOCDOC */
|
|
|
|
int
|
|
|
|
control_event_server_status(int severity, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int r;
|
|
|
|
if (!EVENT_IS_INTERESTING1(EVENT_STATUS_SERVER))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
r = control_event_status(EVENT_STATUS_SERVER, severity, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2006-12-15 23:40:20 +01:00
|
|
|
/** DOCDOC */
|
|
|
|
int
|
|
|
|
control_event_guard(const char *nickname, const char *digest,
|
|
|
|
const char *status)
|
|
|
|
{
|
|
|
|
char hbuf[HEX_DIGEST_LEN+1];
|
|
|
|
base16_encode(hbuf, sizeof(hbuf), digest, DIGEST_LEN);
|
|
|
|
if (!EVENT_IS_INTERESTING1(EVENT_GUARD))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (EVENT_IS_INTERESTING1L(EVENT_GUARD)) {
|
|
|
|
char buf[MAX_VERBOSE_NICKNAME_LEN+1];
|
|
|
|
routerinfo_t *ri = router_get_by_digest(digest);
|
|
|
|
if (ri) {
|
|
|
|
router_get_verbose_nickname(buf, ri);
|
|
|
|
} else {
|
|
|
|
tor_snprintf(buf, sizeof(buf), "$%s~%s", hbuf, nickname);
|
|
|
|
}
|
|
|
|
send_control1_event(EVENT_GUARD, LONG_NAMES,
|
|
|
|
"650 GUARD ENTRY %s %s\r\n", buf, status);
|
|
|
|
}
|
|
|
|
if (EVENT_IS_INTERESTING1S(EVENT_GUARD)) {
|
|
|
|
send_control1_event(EVENT_GUARD, SHORT_NAMES,
|
|
|
|
"650 GUARD ENTRY $%s %s\r\n", hbuf, status);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-07 22:37:50 +01:00
|
|
|
/** Choose a random authentication cookie and write it to disk.
|
|
|
|
* Anybody who can read the cookie from disk will be considered
|
|
|
|
* authorized to use the control connection. */
|
|
|
|
int
|
2004-11-12 17:39:03 +01:00
|
|
|
init_cookie_authentication(int enabled)
|
2004-11-03 20:49:03 +01:00
|
|
|
{
|
|
|
|
char fname[512];
|
|
|
|
|
2004-12-07 20:36:43 +01:00
|
|
|
if (!enabled) {
|
2004-11-12 17:39:03 +01:00
|
|
|
authentication_cookie_is_set = 0;
|
2004-12-07 20:36:43 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2004-11-03 20:49:03 +01:00
|
|
|
|
|
|
|
tor_snprintf(fname, sizeof(fname), "%s/control_auth_cookie",
|
2004-11-09 08:05:53 +01:00
|
|
|
get_options()->DataDirectory);
|
2004-11-03 20:49:03 +01:00
|
|
|
crypto_rand(authentication_cookie, AUTHENTICATION_COOKIE_LEN);
|
|
|
|
authentication_cookie_is_set = 1;
|
|
|
|
if (write_bytes_to_file(fname, authentication_cookie,
|
|
|
|
AUTHENTICATION_COOKIE_LEN, 1)) {
|
2006-02-13 10:02:35 +01:00
|
|
|
log_warn(LD_FS,"Error writing authentication cookie.");
|
2004-11-03 20:49:03 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2005-06-11 20:59:24 +02:00
|
|
|
|