2007-12-12 22:09:01 +01:00
|
|
|
/* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2009-05-02 22:00:54 +02:00
|
|
|
* Copyright (c) 2007-2009, The Tor Project, Inc. */
|
2004-11-03 02:32:26 +01:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
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.
|
2007-05-13 11:25:06 +02:00
|
|
|
* See doc/spec/control-spec.txt for full details on protocol.
|
2005-06-11 07:31:17 +02:00
|
|
|
**/
|
2004-11-07 22:37:50 +01:00
|
|
|
|
2007-04-11 15:18:25 +02:00
|
|
|
#define CONTROL_PRIVATE
|
|
|
|
|
2004-11-03 02:32:26 +01:00
|
|
|
#include "or.h"
|
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Yield true iff <b>s</b> is the state of a control_connection_t that has
|
|
|
|
* finished authentication and is accepting commands. */
|
2007-03-04 22:08:28 +01:00
|
|
|
#define STATE_IS_OPEN(s) ((s) == CONTROL_CONN_STATE_OPEN)
|
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
|
2007-03-04 22:08:28 +01:00
|
|
|
#define EVENT_LOG_OBSOLETE 0x0005 /* Can reclaim this. */
|
2006-10-07 02:50:39 +02:00
|
|
|
#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 EVENT_ADDRMAP 0x000C
|
2007-05-31 20:48:28 +02:00
|
|
|
// #define EVENT_AUTHDIR_NEWDESCS 0x000D
|
2006-10-07 02:50:39 +02:00
|
|
|
#define EVENT_DESCCHANGED 0x000E
|
2007-12-31 22:12:16 +01: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
|
2007-02-14 17:46:49 +01:00
|
|
|
#define EVENT_STREAM_BANDWIDTH_USED 0x0014
|
2008-12-27 07:50:07 +01:00
|
|
|
#define EVENT_CLIENTS_SEEN 0x0015
|
2009-02-16 07:18:03 +01:00
|
|
|
#define EVENT_NEWCONSENSUS 0x0016
|
|
|
|
#define _EVENT_MAX 0x0016
|
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 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.
|
|
|
|
**/
|
2007-02-14 17:46:55 +01:00
|
|
|
typedef uint32_t event_mask_t;
|
2008-12-23 18:56:31 +01:00
|
|
|
|
2009-05-08 18:12:20 +02:00
|
|
|
/** An event mask of all the events that any controller is interested in
|
|
|
|
* receiving. */
|
|
|
|
static event_mask_t global_event_mask = 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>. */
|
2007-03-04 22:08:28 +01:00
|
|
|
#define EVENT_IS_INTERESTING(e) \
|
2009-05-08 18:12:20 +02:00
|
|
|
(global_event_mask & (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;
|
2008-12-23 18:56:31 +01:00
|
|
|
/** If authentication_cookie_is_set, a secret cookie that we've stored to disk
|
|
|
|
* and which we're using to authenticate controllers. (If the controller can
|
|
|
|
* read it off disk, it has permission to connect. */
|
2004-11-03 20:49:03 +01:00
|
|
|
static char authentication_cookie[AUTHENTICATION_COOKIE_LEN];
|
|
|
|
|
2008-06-17 10:01:43 +02:00
|
|
|
/** A sufficiently large size to record the last bootstrap phase string. */
|
|
|
|
#define BOOTSTRAP_MSG_LEN 1024
|
|
|
|
|
|
|
|
/** What was the last bootstrap phase message we sent? We keep track
|
|
|
|
* of this so we can respond to getinfo status/bootstrap-phase queries. */
|
|
|
|
static char last_sent_bootstrap_message[BOOTSTRAP_MSG_LEN];
|
|
|
|
|
2009-05-08 18:12:20 +02:00
|
|
|
/** Flag for event_format_t. Indicates that we should use the one standard
|
|
|
|
format.
|
2008-12-22 18:53:04 +01:00
|
|
|
*/
|
2009-05-08 18:12:20 +02:00
|
|
|
#define ALL_FORMATS 1
|
2008-12-22 18:53:04 +01:00
|
|
|
|
|
|
|
/** Bit field of flags to select how to format a controller event. Recognized
|
2009-05-08 18:12:20 +02:00
|
|
|
* flag is ALL_FORMATS. */
|
2006-10-09 17:47:50 +02:00
|
|
|
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);
|
2006-07-26 21:07:26 +02:00
|
|
|
static void send_control_done(control_connection_t *conn);
|
2007-03-04 22:08:28 +01:00
|
|
|
static void send_control_event(uint16_t event, event_format_t which,
|
|
|
|
const char *format, ...)
|
2006-10-03 20:58:40 +02:00
|
|
|
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);
|
2008-03-10 13:41:52 +01:00
|
|
|
static int handle_control_loadconf(control_connection_t *conn, uint32_t len,
|
|
|
|
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_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);
|
2007-10-12 00:19:47 +02:00
|
|
|
static int handle_control_setcircuitpurpose(control_connection_t *conn,
|
|
|
|
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);
|
2007-07-10 19:13:24 +02:00
|
|
|
static int handle_control_resolve(control_connection_t *conn, uint32_t len,
|
|
|
|
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);
|
2009-05-08 18:12:20 +02:00
|
|
|
static void orconn_target_get_name(char *buf, size_t len,
|
2006-10-03 21:00:18 +02:00
|
|
|
or_connection_t *conn);
|
2007-08-16 19:31:23 +02:00
|
|
|
static char *get_cookie_file(void);
|
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
|
|
|
{
|
2007-05-22 17:49:14 +02:00
|
|
|
smartlist_t *conns = get_connection_array();
|
2007-02-14 17:46:55 +01:00
|
|
|
event_mask_t old_mask, new_mask;
|
2009-05-08 18:12:20 +02:00
|
|
|
old_mask = global_event_mask;
|
2007-02-14 17:46:55 +01:00
|
|
|
|
2009-05-08 18:12:20 +02:00
|
|
|
global_event_mask = 0;
|
2007-05-22 17:49:14 +02:00
|
|
|
SMARTLIST_FOREACH(conns, connection_t *, _conn,
|
|
|
|
{
|
|
|
|
if (_conn->type == CONN_TYPE_CONTROL &&
|
|
|
|
STATE_IS_OPEN(_conn->state)) {
|
|
|
|
control_connection_t *conn = TO_CONTROL_CONN(_conn);
|
2009-05-08 18:12:20 +02:00
|
|
|
global_event_mask |= conn->event_mask;
|
2004-11-03 02:32:26 +01:00
|
|
|
}
|
2007-05-22 17:49:14 +02:00
|
|
|
});
|
2005-04-06 00:56:17 +02:00
|
|
|
|
2009-05-08 18:12:20 +02:00
|
|
|
new_mask = global_event_mask;
|
2007-02-14 17:46:55 +01:00
|
|
|
|
|
|
|
/* Handle the aftermath. Set up the log callback to tell us only what
|
|
|
|
* we want to hear...*/
|
2005-07-13 07:14:42 +02:00
|
|
|
control_adjust_event_log_severity();
|
2007-02-14 17:46:55 +01:00
|
|
|
|
|
|
|
/* ...then, if we've started logging stream bw, clear the appropriate
|
|
|
|
* fields. */
|
|
|
|
if (! (old_mask & EVENT_STREAM_BANDWIDTH_USED) &&
|
|
|
|
(new_mask & EVENT_STREAM_BANDWIDTH_USED)) {
|
2007-05-22 17:49:14 +02:00
|
|
|
SMARTLIST_FOREACH(conns, connection_t *, conn,
|
|
|
|
{
|
|
|
|
if (conn->type == CONN_TYPE_AP) {
|
|
|
|
edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
|
|
|
|
edge_conn->n_written = edge_conn->n_read = 0;
|
2007-02-14 17:46:55 +01:00
|
|
|
}
|
2007-05-22 17:49:14 +02:00
|
|
|
});
|
2007-02-14 17:46:55 +01:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|
2007-01-06 06:42:31 +01:00
|
|
|
if (EVENT_IS_INTERESTING(EVENT_LOG_OBSOLETE) ||
|
|
|
|
EVENT_IS_INTERESTING(EVENT_STATUS_GENERAL)) {
|
2005-04-06 00:56:17 +02:00
|
|
|
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;
|
|
|
|
}
|
2008-03-17 04:37:52 +01:00
|
|
|
if (min_log_event <= max_log_event)
|
|
|
|
change_callback_log_severity(event_to_log_severity(min_log_event),
|
|
|
|
event_to_log_severity(max_log_event),
|
|
|
|
control_event_logmsg);
|
|
|
|
else
|
|
|
|
change_callback_log_severity(LOG_ERR, LOG_ERR,
|
|
|
|
control_event_logmsg);
|
2004-11-03 02:32:26 +01:00
|
|
|
}
|
|
|
|
|
2007-05-31 20:48:28 +02:00
|
|
|
/** Return true iff the event with code <b>c</b> is being sent to any current
|
|
|
|
* control connection. This is useful if the amount of work needed to prepare
|
|
|
|
* to call the appropriate control_event_...() function is high.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
control_event_is_interesting(int event)
|
|
|
|
{
|
|
|
|
return EVENT_IS_INTERESTING(event);
|
|
|
|
}
|
|
|
|
|
2005-07-15 21:31:11 +02:00
|
|
|
/** Append a NUL-terminated string <b>s</b> to the end of
|
2007-10-12 00:19:47 +02:00
|
|
|
* <b>conn</b>-\>outbuf.
|
2005-07-15 21:31:11 +02:00
|
|
|
*/
|
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
|
2007-08-29 21:02:33 +02:00
|
|
|
* 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
|
2009-12-18 12:55:05 +01:00
|
|
|
* that appears at the start of a line, and adding a period-CRLF line at
|
2007-08-29 21:02:33 +02:00
|
|
|
* the end. Replace all LF characters sequences with CRLF. Return the number
|
|
|
|
* of bytes in *<b>out</b>.
|
2005-07-15 21:31:11 +02:00
|
|
|
*/
|
|
|
|
/* static */ size_t
|
2007-08-29 21:02:33 +02:00
|
|
|
write_escaped_data(const char *data, size_t len, char **out)
|
2005-06-18 04:39:25 +02:00
|
|
|
{
|
|
|
|
size_t sz_out = len+8;
|
|
|
|
char *outp;
|
2007-07-16 19:27:35 +02:00
|
|
|
const char *start = data, *end;
|
2005-06-18 04:39:25 +02:00
|
|
|
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') {
|
2007-08-29 21:02:33 +02:00
|
|
|
if (data > start && data[-1] != '\r')
|
2005-06-18 04:39:25 +02:00
|
|
|
*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
|
2007-08-29 21:02:33 +02:00
|
|
|
* that appears at the start of a line, and replacing all CRLF sequences
|
|
|
|
* with LF. Return the number of
|
2005-07-15 21:31:11 +02:00
|
|
|
* bytes in *<b>out</b>. */
|
2007-04-11 15:18:25 +02:00
|
|
|
/* static */ size_t
|
2007-08-29 21:02:33 +02:00
|
|
|
read_escaped_data(const char *data, size_t len, char **out)
|
2005-06-18 04:39:25 +02:00
|
|
|
{
|
|
|
|
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) {
|
2007-08-29 21:02:33 +02:00
|
|
|
/* we're at the start of a line. */
|
2005-06-18 04:39:25 +02:00
|
|
|
if (*data == '.')
|
|
|
|
++data;
|
2007-08-29 21:02:33 +02:00
|
|
|
next = memchr(data, '\n', end-data);
|
2005-06-18 04:39:25 +02:00
|
|
|
if (next) {
|
2007-08-29 21:02:33 +02:00
|
|
|
size_t n_to_copy = next-data;
|
|
|
|
/* Don't copy a CR that precedes this LF. */
|
|
|
|
if (n_to_copy && *(next-1) == '\r')
|
|
|
|
--n_to_copy;
|
|
|
|
memcpy(outp, data, n_to_copy);
|
|
|
|
outp += n_to_copy;
|
|
|
|
data = next+1; /* This will point at the start of the next line,
|
|
|
|
* or the end of the string, or a period. */
|
2005-06-18 04:39:25 +02:00
|
|
|
} 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;
|
|
|
|
}
|
2007-08-29 21:02:33 +02:00
|
|
|
*outp++ = '\n';
|
2005-06-18 04:39:25 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2008-12-22 15:56:16 +01:00
|
|
|
/** If the first <b>in_len_max</b> characters in <b>start</b> contain a
|
2008-02-06 17:58:05 +01:00
|
|
|
* double-quoted string with escaped characters, return the length of that
|
|
|
|
* string (as encoded, including quotes). Otherwise return -1. */
|
|
|
|
static INLINE int
|
|
|
|
get_escaped_string_length(const char *start, size_t in_len_max,
|
|
|
|
int *chars_out)
|
2008-01-07 19:54:55 +01:00
|
|
|
{
|
|
|
|
const char *cp, *end;
|
2008-02-06 17:58:05 +01:00
|
|
|
int chars = 0;
|
2008-01-07 19:54:55 +01:00
|
|
|
|
|
|
|
if (*start != '\"')
|
2008-02-06 17:58:05 +01:00
|
|
|
return -1;
|
2008-01-07 19:54:55 +01:00
|
|
|
|
|
|
|
cp = start+1;
|
|
|
|
end = start+in_len_max;
|
|
|
|
|
|
|
|
/* Calculate length. */
|
|
|
|
while (1) {
|
2008-02-06 17:58:05 +01:00
|
|
|
if (cp >= end) {
|
|
|
|
return -1; /* Too long. */
|
|
|
|
} else if (*cp == '\\') {
|
2008-01-07 19:54:55 +01:00
|
|
|
if (++cp == end)
|
2008-02-06 17:58:05 +01:00
|
|
|
return -1; /* Can't escape EOS. */
|
2008-01-07 19:54:55 +01:00
|
|
|
++cp;
|
2008-02-06 17:58:05 +01:00
|
|
|
++chars;
|
2008-01-07 19:54:55 +01:00
|
|
|
} else if (*cp == '\"') {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
++cp;
|
2008-02-06 17:58:05 +01:00
|
|
|
++chars;
|
2008-01-07 19:54:55 +01:00
|
|
|
}
|
|
|
|
}
|
2008-02-06 17:58:05 +01:00
|
|
|
if (chars_out)
|
|
|
|
*chars_out = chars;
|
2008-02-22 20:09:45 +01:00
|
|
|
return (int)(cp - start+1);
|
2008-02-06 17:58:05 +01:00
|
|
|
}
|
2008-01-07 19:54:55 +01:00
|
|
|
|
2008-02-06 17:58:05 +01:00
|
|
|
/** As decode_escaped_string, but does not decode the string: copies the
|
|
|
|
* entire thing, including quotation marks. */
|
|
|
|
static const char *
|
|
|
|
extract_escaped_string(const char *start, size_t in_len_max,
|
|
|
|
char **out, size_t *out_len)
|
|
|
|
{
|
|
|
|
int length = get_escaped_string_length(start, in_len_max, NULL);
|
|
|
|
if (length<0)
|
|
|
|
return NULL;
|
|
|
|
*out_len = length;
|
2008-01-07 19:54:55 +01:00
|
|
|
*out = tor_strndup(start, *out_len);
|
2008-02-06 17:58:05 +01:00
|
|
|
return start+length;
|
2008-01-07 19:54:55 +01:00
|
|
|
}
|
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Given a pointer to a string starting at <b>start</b> containing
|
2007-08-24 01:03:32 +02:00
|
|
|
* <b>in_len_max</b> characters, decode a string beginning with one double
|
2005-09-30 22:04:55 +02:00
|
|
|
* quote, containing any number of non-quote characters or characters escaped
|
2007-08-24 01:03:32 +02:00
|
|
|
* with a backslash, and ending with a final double quote. Place the resulting
|
2005-09-30 22:04:55 +02:00
|
|
|
* 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 *
|
2008-02-06 17:58:05 +01:00
|
|
|
decode_escaped_string(const char *start, size_t in_len_max,
|
2005-06-19 22:40:41 +02:00
|
|
|
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;
|
2008-02-06 17:58:05 +01:00
|
|
|
int len, n_chars = 0;
|
2005-06-19 22:40:41 +02:00
|
|
|
|
2008-02-06 17:58:05 +01:00
|
|
|
len = get_escaped_string_length(start, in_len_max, &n_chars);
|
|
|
|
if (len<0)
|
2005-06-19 22:40:41 +02:00
|
|
|
return NULL;
|
|
|
|
|
2008-02-06 17:58:05 +01:00
|
|
|
end = start+len-1; /* Index of last quote. */
|
|
|
|
tor_assert(*end == '\"');
|
2005-06-19 22:40:41 +02:00
|
|
|
outp = *out = tor_malloc(len+1);
|
2008-02-06 17:58:05 +01:00
|
|
|
*out_len = n_chars;
|
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
|
2008-12-25 16:37:47 +01:00
|
|
|
* ending CR LF NUL ("\\r\\n\\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
|
|
|
}
|
|
|
|
|
2007-10-12 00:19:47 +02: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
|
|
|
{
|
2007-03-04 22:08:28 +01:00
|
|
|
connection_write_str_to_buf("250 OK\r\n", conn);
|
2004-11-03 02:32:26 +01:00
|
|
|
}
|
|
|
|
|
2008-12-22 15:56:28 +01: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>.
|
|
|
|
*
|
2007-02-02 19:58:11 +01:00
|
|
|
* If <b>which</b> & SHORT_NAMES, the event contains short-format names: send
|
|
|
|
* it to controllers that haven't enabled the VERBOSE_NAMES feature. If
|
2007-02-06 03:49:07 +01:00
|
|
|
* <b>which</b> & LONG_NAMES, the event contains long-format names: send it
|
2009-05-27 23:55:51 +02:00
|
|
|
* to controllers that <em>have</em> enabled VERBOSE_NAMES.
|
2007-02-02 19:58:11 +01:00
|
|
|
*
|
2007-02-06 03:49:07 +01:00
|
|
|
* The EXTENDED_FORMAT and NONEXTENDED_FORMAT flags behave similarly with
|
2007-02-02 19:58:11 +01:00
|
|
|
* respect to the EXTENDED_EVENTS feature. */
|
2005-06-17 22:37:21 +02:00
|
|
|
static void
|
2007-03-04 22:08:28 +01:00
|
|
|
send_control_event_string(uint16_t event, event_format_t which,
|
|
|
|
const char *msg)
|
2005-06-17 22:37:21 +02:00
|
|
|
{
|
2007-05-22 17:49:14 +02:00
|
|
|
smartlist_t *conns = get_connection_array();
|
2009-05-08 18:12:20 +02:00
|
|
|
(void)which;
|
2005-06-17 22:37:21 +02:00
|
|
|
tor_assert(event >= _EVENT_MIN && event <= _EVENT_MAX);
|
|
|
|
|
2009-05-08 18:12:20 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
|
2007-05-22 17:49:14 +02:00
|
|
|
if (conn->type == CONN_TYPE_CONTROL &&
|
|
|
|
!conn->marked_for_close &&
|
|
|
|
conn->state == CONTROL_CONN_STATE_OPEN) {
|
|
|
|
control_connection_t *control_conn = TO_CONTROL_CONN(conn);
|
2009-05-08 18:12:20 +02:00
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
if (control_conn->event_mask & (1<<event)) {
|
2007-01-06 06:42:31 +01:00
|
|
|
int is_err = 0;
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_write_to_buf(msg, strlen(msg), TO_CONN(control_conn));
|
|
|
|
if (event == EVENT_ERR_MSG)
|
2007-01-06 06:42:31 +01:00
|
|
|
is_err = 1;
|
|
|
|
else if (event == EVENT_STATUS_GENERAL)
|
|
|
|
is_err = !strcmpstart(msg, "STATUS_GENERAL ERR ");
|
|
|
|
else if (event == EVENT_STATUS_CLIENT)
|
|
|
|
is_err = !strcmpstart(msg, "STATUS_CLIENT ERR ");
|
|
|
|
else if (event == EVENT_STATUS_SERVER)
|
|
|
|
is_err = !strcmpstart(msg, "STATUS_SERVER ERR ");
|
|
|
|
if (is_err)
|
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
|
|
|
}
|
2009-05-08 18:12:20 +02:00
|
|
|
} SMARTLIST_FOREACH_END(conn);
|
2005-06-17 22:37:21 +02:00
|
|
|
}
|
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Helper for send_control1_event and send_control1_event_extended:
|
|
|
|
* 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
|
2008-12-22 15:56:16 +01:00
|
|
|
* ending \\r\\n\\0). */
|
2005-10-18 18:45:43 +02:00
|
|
|
static void
|
2009-05-08 18:12:20 +02:00
|
|
|
send_control_event_impl(uint16_t event, event_format_t which,
|
|
|
|
const char *format, va_list ap)
|
2005-10-18 18:45:43 +02:00
|
|
|
{
|
2007-02-24 21:28:41 +01:00
|
|
|
/* This is just a little longer than the longest allowed log message */
|
|
|
|
#define SEND_CONTROL1_EVENT_BUFFERSIZE 10064
|
2005-10-18 18:45:43 +02:00
|
|
|
int r;
|
2007-02-24 21:28:41 +01:00
|
|
|
char buf[SEND_CONTROL1_EVENT_BUFFERSIZE];
|
2005-10-18 18:45:43 +02:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
|
2009-05-08 18:12:20 +02:00
|
|
|
send_control_event_string(event, which|ALL_FORMATS, buf);
|
2006-10-09 17:47:50 +02:00
|
|
|
}
|
|
|
|
|
2008-12-22 15:56:28 +01: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 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
|
2008-12-22 18:53:30 +01:00
|
|
|
* ending \\n\\r\\0. */
|
2006-10-09 17:47:50 +02:00
|
|
|
static void
|
2007-03-04 22:08:28 +01:00
|
|
|
send_control_event(uint16_t event, event_format_t which,
|
2008-12-27 07:50:07 +01:00
|
|
|
const char *format, ...)
|
2006-10-09 17:47:50 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
2009-05-08 18:12:20 +02:00
|
|
|
send_control_event_impl(event, which, format, ap);
|
2006-10-09 17:47:50 +02:00
|
|
|
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)
|
|
|
|
{
|
2008-02-22 20:09:45 +01:00
|
|
|
uint32_t n_id;
|
2005-06-18 04:39:25 +02:00
|
|
|
int ok;
|
2008-02-22 20:09:45 +01:00
|
|
|
n_id = (uint32_t) tor_parse_ulong(id, 10, 0, UINT32_MAX, &ok, NULL);
|
2005-06-18 04:39:25 +02:00
|
|
|
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)
|
|
|
|
{
|
2008-08-15 15:55:01 +02:00
|
|
|
uint64_t n_id;
|
2005-06-18 04:39:25 +02:00
|
|
|
int ok;
|
2008-08-15 15:55:01 +02:00
|
|
|
connection_t *conn;
|
|
|
|
n_id = tor_parse_uint64(id, 10, 0, UINT64_MAX, &ok, NULL);
|
2005-06-18 04:39:25 +02:00
|
|
|
if (!ok)
|
|
|
|
return NULL;
|
|
|
|
conn = connection_get_by_global_id(n_id);
|
2008-08-15 15:55:01 +02:00
|
|
|
if (!conn || conn->type != CONN_TYPE_AP || conn->marked_for_close)
|
2005-06-18 04:39:25 +02:00
|
|
|
return NULL;
|
2008-08-15 15:55:01 +02:00
|
|
|
return TO_EDGE_CONN(conn);
|
2005-06-18 04:39:25 +02:00
|
|
|
}
|
|
|
|
|
2005-09-08 05:18:51 +02:00
|
|
|
/** Helper for setconf and resetconf. Acts like setconf, except
|
2007-01-30 23:19:41 +01:00
|
|
|
* it passes <b>use_defaults</b> on to options_trial_assign(). Modifies the
|
|
|
|
* contents of body.
|
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,
|
2007-03-04 22:08:28 +01:00
|
|
|
int use_defaults)
|
2004-11-03 02:32:26 +01:00
|
|
|
{
|
2008-03-13 16:11:56 +01:00
|
|
|
setopt_err_t opt_err;
|
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;
|
2007-03-04 22:08:28 +01:00
|
|
|
const int clear_first = 1;
|
2004-11-04 23:31:50 +01:00
|
|
|
|
2007-03-17 22:34:37 +01:00
|
|
|
char *config;
|
|
|
|
smartlist_t *entries = smartlist_create();
|
|
|
|
|
|
|
|
/* We have a string, "body", of the format '(key(=val|="val")?)' entries
|
|
|
|
* separated by space. break it into a list of configuration entries. */
|
|
|
|
while (*body) {
|
|
|
|
char *eq = body;
|
|
|
|
char *key;
|
|
|
|
char *entry;
|
|
|
|
while (!TOR_ISSPACE(*eq) && *eq != '=')
|
|
|
|
++eq;
|
|
|
|
key = tor_strndup(body, eq-body);
|
|
|
|
body = eq+1;
|
|
|
|
if (*eq == '=') {
|
2008-01-10 18:54:24 +01:00
|
|
|
char *val=NULL;
|
|
|
|
size_t val_len=0;
|
2007-03-17 22:34:37 +01:00
|
|
|
size_t ent_len;
|
|
|
|
if (*body != '\"') {
|
|
|
|
char *val_start = body;
|
|
|
|
while (!TOR_ISSPACE(*body))
|
|
|
|
body++;
|
|
|
|
val = tor_strndup(val_start, body-val_start);
|
|
|
|
val_len = strlen(val);
|
2007-02-10 22:26:29 +01:00
|
|
|
} else {
|
2008-01-07 19:54:55 +01:00
|
|
|
body = (char*)extract_escaped_string(body, (len - (body-start)),
|
|
|
|
&val, &val_len);
|
2007-03-17 22:34:37 +01:00
|
|
|
if (!body) {
|
|
|
|
connection_write_str_to_buf("551 Couldn't parse string\r\n", conn);
|
|
|
|
SMARTLIST_FOREACH(entries, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(entries);
|
2008-01-16 06:27:19 +01:00
|
|
|
tor_free(key);
|
2007-03-17 22:34:37 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2005-06-18 05:50:08 +02:00
|
|
|
}
|
2007-03-17 22:34:37 +01:00
|
|
|
ent_len = strlen(key)+val_len+3;
|
|
|
|
entry = tor_malloc(ent_len+1);
|
|
|
|
tor_snprintf(entry, ent_len, "%s %s", key, val);
|
|
|
|
tor_free(key);
|
|
|
|
tor_free(val);
|
|
|
|
} else {
|
|
|
|
entry = key;
|
2005-06-18 05:50:08 +02:00
|
|
|
}
|
2007-03-17 22:34:37 +01:00
|
|
|
smartlist_add(entries, entry);
|
|
|
|
while (TOR_ISSPACE(*body))
|
|
|
|
++body;
|
|
|
|
}
|
2007-02-10 22:26:29 +01:00
|
|
|
|
2007-03-17 22:34:37 +01:00
|
|
|
smartlist_add(entries, tor_strdup(""));
|
|
|
|
config = smartlist_join_strings(entries, "\n", 0, NULL);
|
|
|
|
SMARTLIST_FOREACH(entries, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(entries);
|
2005-06-18 05:50:08 +02:00
|
|
|
|
2007-03-17 22:34:37 +01:00
|
|
|
if (config_get_lines(config, &lines) < 0) {
|
|
|
|
log_warn(LD_CONTROL,"Controller gave us config lines we can't parse.");
|
|
|
|
connection_write_str_to_buf("551 Couldn't parse configuration\r\n",
|
|
|
|
conn);
|
2005-06-18 05:50:08 +02:00
|
|
|
tor_free(config);
|
2007-03-17 22:34:37 +01:00
|
|
|
return 0;
|
2004-11-06 06:18:11 +01:00
|
|
|
}
|
2007-03-17 22:34:37 +01:00
|
|
|
tor_free(config);
|
2004-11-04 23:31:50 +01:00
|
|
|
|
2008-12-29 15:21:25 +01:00
|
|
|
opt_err = options_trial_assign(lines, use_defaults, clear_first, &errstring);
|
|
|
|
{
|
2005-09-14 04:36:29 +02:00
|
|
|
const char *msg;
|
2008-03-13 16:11:56 +01:00
|
|
|
switch (opt_err) {
|
|
|
|
case SETOPT_ERR_MISC:
|
2005-11-17 23:01:46 +01:00
|
|
|
msg = "552 Unrecognized option";
|
2005-09-14 04:36:29 +02:00
|
|
|
break;
|
2008-03-13 16:11:56 +01:00
|
|
|
case SETOPT_ERR_PARSE:
|
2005-12-11 10:33:38 +01:00
|
|
|
msg = "513 Unacceptable option value";
|
2005-09-14 04:36:29 +02:00
|
|
|
break;
|
2008-03-13 16:11:56 +01:00
|
|
|
case SETOPT_ERR_TRANSITION:
|
2005-11-17 23:01:46 +01:00
|
|
|
msg = "553 Transition not allowed";
|
2005-09-14 04:36:29 +02:00
|
|
|
break;
|
2008-03-13 16:11:56 +01:00
|
|
|
case SETOPT_ERR_SETTING:
|
2005-09-14 04:36:29 +02:00
|
|
|
default:
|
2005-11-17 23:01:46 +01:00
|
|
|
msg = "553 Unable to set option";
|
2005-09-14 04:36:29 +02:00
|
|
|
break;
|
2008-03-13 16:11:56 +01:00
|
|
|
case SETOPT_OK:
|
2008-12-29 15:21:25 +01:00
|
|
|
config_free_lines(lines);
|
|
|
|
send_control_done(conn);
|
|
|
|
return 0;
|
2005-09-14 04:36:29 +02:00
|
|
|
}
|
2008-12-29 15:21:25 +01:00
|
|
|
log_warn(LD_CONTROL,
|
|
|
|
"Controller gave us config lines that didn't validate: %s",
|
|
|
|
errstring);
|
2007-03-04 22:08:28 +01:00
|
|
|
connection_printf_to_buf(conn, "%s: %s\r\n", msg, errstring);
|
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;
|
|
|
|
}
|
2004-11-03 02:32:26 +01:00
|
|
|
}
|
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
|
2007-01-30 23:19:41 +01:00
|
|
|
* to update our configuration. Reply with a DONE or ERROR message.
|
|
|
|
* Modifies the contents of body.*/
|
2005-09-08 05:18:51 +02:00
|
|
|
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
|
|
|
{
|
2007-03-04 22:08:28 +01:00
|
|
|
return control_setconf_helper(conn, len, body, 0);
|
2005-09-08 05:18:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Called when we receive a RESETCONF message: parse the body and try
|
2007-01-30 23:19:41 +01:00
|
|
|
* to update our configuration. Reply with a DONE or ERROR message.
|
|
|
|
* Modifies the contents of body. */
|
2005-09-08 05:18:51 +02:00
|
|
|
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
|
|
|
{
|
2007-03-04 22:08:28 +01:00
|
|
|
return control_setconf_helper(conn, len, body, 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
|
|
|
{
|
2009-01-13 15:43:51 +01:00
|
|
|
smartlist_t *questions = smartlist_create();
|
|
|
|
smartlist_t *answers = smartlist_create();
|
|
|
|
smartlist_t *unrecognized = smartlist_create();
|
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();
|
2007-03-17 22:34:37 +01:00
|
|
|
int i, len;
|
2004-11-03 19:33:07 +01:00
|
|
|
|
2009-05-27 23:55:51 +02:00
|
|
|
(void) body_len; /* body is NUL-terminated; so we can ignore len. */
|
2007-03-04 22:08:28 +01:00
|
|
|
smartlist_split_string(questions, body, " ",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
2009-01-13 15:43:51 +01:00
|
|
|
SMARTLIST_FOREACH(questions, const char *, q,
|
2004-11-04 23:31:50 +01:00
|
|
|
{
|
2005-07-23 03:58:05 +02:00
|
|
|
if (!option_is_recognized(q)) {
|
2009-01-13 15:43:51 +01:00
|
|
|
smartlist_add(unrecognized, (char*) 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);
|
2007-03-04 22:08:28 +01:00
|
|
|
if (!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);
|
2007-03-04 22:08:28 +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
|
|
|
|
2007-03-17 22:34:37 +01:00
|
|
|
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));
|
2007-03-17 22:34:37 +01: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);
|
|
|
|
connection_write_to_buf(msg, msg_len, TO_CONN(conn));
|
|
|
|
} else {
|
|
|
|
connection_write_str_to_buf("250 OK\r\n", conn);
|
2005-06-17 22:37:21 +02:00
|
|
|
}
|
2004-11-03 19:33:07 +01:00
|
|
|
|
2009-01-13 15:43:51 +01:00
|
|
|
SMARTLIST_FOREACH(answers, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(answers);
|
|
|
|
SMARTLIST_FOREACH(questions, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(questions);
|
2005-06-17 22:37:21 +02:00
|
|
|
smartlist_free(unrecognized);
|
2009-01-13 15:43:51 +01:00
|
|
|
|
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
|
|
|
|
2008-03-10 13:41:52 +01:00
|
|
|
/** Called when we get a +LOADCONF message. */
|
|
|
|
static int
|
|
|
|
handle_control_loadconf(control_connection_t *conn, uint32_t len,
|
|
|
|
const char *body)
|
|
|
|
{
|
2008-03-13 16:11:56 +01:00
|
|
|
setopt_err_t retval;
|
2008-03-10 13:41:52 +01:00
|
|
|
char *errstring = NULL;
|
2008-03-11 05:30:14 +01:00
|
|
|
const char *msg = NULL;
|
|
|
|
(void) len;
|
2008-03-10 13:41:52 +01:00
|
|
|
|
|
|
|
retval = options_init_from_string(body, CMD_RUN_TOR, NULL, &errstring);
|
|
|
|
|
2009-10-27 03:35:29 +01:00
|
|
|
if (retval != SETOPT_OK)
|
2008-03-10 13:41:52 +01:00
|
|
|
log_warn(LD_CONTROL,
|
|
|
|
"Controller gave us config file that didn't validate: %s",
|
|
|
|
errstring);
|
2009-10-27 03:35:29 +01:00
|
|
|
|
|
|
|
switch (retval) {
|
|
|
|
case SETOPT_ERR_PARSE:
|
|
|
|
msg = "552 Invalid config file";
|
|
|
|
break;
|
|
|
|
case SETOPT_ERR_TRANSITION:
|
|
|
|
msg = "553 Transition not allowed";
|
|
|
|
break;
|
|
|
|
case SETOPT_ERR_SETTING:
|
|
|
|
msg = "553 Unable to set option";
|
|
|
|
break;
|
|
|
|
case SETOPT_ERR_MISC:
|
|
|
|
default:
|
|
|
|
msg = "550 Unable to load config";
|
|
|
|
break;
|
|
|
|
case SETOPT_OK:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (msg) {
|
2009-10-27 04:13:29 +01:00
|
|
|
if (errstring)
|
2008-03-10 13:41:52 +01:00
|
|
|
connection_printf_to_buf(conn, "%s: %s\r\n", msg, errstring);
|
|
|
|
else
|
|
|
|
connection_printf_to_buf(conn, "%s\r\n", msg);
|
2009-10-27 03:35:29 +01:00
|
|
|
} else {
|
|
|
|
send_control_done(conn);
|
2008-03-10 13:41:52 +01:00
|
|
|
}
|
2009-10-27 04:13:29 +01:00
|
|
|
tor_free(errstring);
|
2008-03-10 13:41:52 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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;
|
2007-03-17 22:34:37 +01:00
|
|
|
smartlist_t *events = smartlist_create();
|
|
|
|
|
2007-03-04 22:08:28 +01:00
|
|
|
(void) len;
|
2004-11-03 02:32:26 +01:00
|
|
|
|
2007-03-17 22:34:37 +01:00
|
|
|
smartlist_split_string(events, body, " ",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
2008-12-27 01:20:08 +01:00
|
|
|
SMARTLIST_FOREACH_BEGIN(events, const char *, ev)
|
2007-03-17 22:34:37 +01:00
|
|
|
{
|
|
|
|
if (!strcasecmp(ev, "EXTENDED")) {
|
|
|
|
continue;
|
|
|
|
} else if (!strcasecmp(ev, "CIRC"))
|
|
|
|
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"))
|
|
|
|
event_code = EVENT_DEBUG_MSG;
|
|
|
|
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;
|
|
|
|
else if (!strcasecmp(ev, "ADDRMAP"))
|
|
|
|
event_code = EVENT_ADDRMAP;
|
|
|
|
else if (!strcasecmp(ev, "AUTHDIR_NEWDESCS"))
|
|
|
|
event_code = EVENT_AUTHDIR_NEWDESCS;
|
|
|
|
else if (!strcasecmp(ev, "DESCCHANGED"))
|
|
|
|
event_code = EVENT_DESCCHANGED;
|
|
|
|
else if (!strcasecmp(ev, "NS"))
|
|
|
|
event_code = EVENT_NS;
|
|
|
|
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;
|
|
|
|
else if (!strcasecmp(ev, "GUARD"))
|
|
|
|
event_code = EVENT_GUARD;
|
2008-12-27 01:20:08 +01:00
|
|
|
else if (!strcasecmp(ev, "STREAM_BW"))
|
2007-03-17 22:34:37 +01:00
|
|
|
event_code = EVENT_STREAM_BANDWIDTH_USED;
|
2009-01-21 04:24:27 +01:00
|
|
|
else if (!strcasecmp(ev, "CLIENTS_SEEN"))
|
|
|
|
event_code = EVENT_CLIENTS_SEEN;
|
2009-02-16 07:18:03 +01:00
|
|
|
else if (!strcasecmp(ev, "NEWCONSENSUS"))
|
|
|
|
event_code = EVENT_NEWCONSENSUS;
|
2007-03-17 22:34:37 +01: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);
|
2008-12-27 01:20:08 +01:00
|
|
|
}
|
|
|
|
SMARTLIST_FOREACH_END(ev);
|
2007-03-17 22:34:37 +01:00
|
|
|
SMARTLIST_FOREACH(events, char *, e, tor_free(e));
|
|
|
|
smartlist_free(events);
|
|
|
|
|
2004-11-03 02:32:26 +01:00
|
|
|
conn->event_mask = event_mask;
|
|
|
|
|
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
|
|
|
|
2007-12-09 05:59:27 +01:00
|
|
|
/** Decode the hashed, base64'd passwords stored in <b>passwords</b>.
|
|
|
|
* Return a smartlist of acceptable passwords (unterminated strings of
|
|
|
|
* length S2K_SPECIFIER_LEN+DIGEST_LEN) on success, or NULL on failure.
|
2004-12-13 19:32:29 +01:00
|
|
|
*/
|
2007-12-09 05:59:27 +01:00
|
|
|
smartlist_t *
|
|
|
|
decode_hashed_passwords(config_line_t *passwords)
|
2004-12-13 19:32:29 +01:00
|
|
|
{
|
|
|
|
char decoded[64];
|
2007-12-09 05:59:27 +01:00
|
|
|
config_line_t *cl;
|
|
|
|
smartlist_t *sl = smartlist_create();
|
|
|
|
|
|
|
|
tor_assert(passwords);
|
|
|
|
|
|
|
|
for (cl = passwords; cl; cl = cl->next) {
|
|
|
|
const char *hashed = cl->value;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
goto err;
|
2005-05-23 04:31:53 +02:00
|
|
|
}
|
2007-12-09 05:59:27 +01:00
|
|
|
} else {
|
|
|
|
if (base64_decode(decoded, sizeof(decoded), hashed, strlen(hashed))
|
|
|
|
!= S2K_SPECIFIER_LEN+DIGEST_LEN) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
smartlist_add(sl, tor_memdup(decoded, S2K_SPECIFIER_LEN+DIGEST_LEN));
|
2004-12-13 19:32:29 +01:00
|
|
|
}
|
2007-12-09 05:59:27 +01:00
|
|
|
|
|
|
|
return sl;
|
|
|
|
|
|
|
|
err:
|
|
|
|
SMARTLIST_FOREACH(sl, char*, cp, tor_free(cp));
|
|
|
|
smartlist_free(sl);
|
|
|
|
return NULL;
|
2004-12-13 19:32:29 +01:00
|
|
|
}
|
|
|
|
|
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;
|
2007-05-15 23:17:48 +02:00
|
|
|
const char *cp;
|
|
|
|
int i;
|
2007-09-06 20:04:28 +02:00
|
|
|
int bad_cookie=0, bad_password=0;
|
2007-12-09 05:59:27 +01:00
|
|
|
smartlist_t *sl = NULL;
|
2007-03-17 22:34:37 +01:00
|
|
|
|
|
|
|
if (TOR_ISXDIGIT(body[0])) {
|
2007-05-15 23:17:48 +02:00
|
|
|
cp = body;
|
|
|
|
while (TOR_ISXDIGIT(*cp))
|
|
|
|
++cp;
|
2008-02-22 20:09:45 +01:00
|
|
|
i = (int)(cp - body);
|
2007-05-15 23:17:48 +02:00
|
|
|
tor_assert(i>0);
|
|
|
|
password_len = i/2;
|
|
|
|
password = tor_malloc(password_len + 1);
|
|
|
|
if (base16_decode(password, password_len+1, body, i)<0) {
|
2007-03-17 22:34:37 +01:00
|
|
|
connection_write_str_to_buf(
|
2005-12-09 06:37:26 +01:00
|
|
|
"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);
|
2007-08-02 03:28:40 +02:00
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
2007-03-17 22:34:37 +01:00
|
|
|
tor_free(password);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (TOR_ISSPACE(body[0])) {
|
|
|
|
password = tor_strdup("");
|
|
|
|
password_len = 0;
|
|
|
|
} else {
|
2008-02-06 17:58:05 +01:00
|
|
|
if (!decode_escaped_string(body, len, &password, &password_len)) {
|
2007-03-17 22:34:37 +01:00
|
|
|
connection_write_str_to_buf("551 Invalid quoted string. You need "
|
2005-12-14 21:40:40 +01:00
|
|
|
"to put the password in double quotes.\r\n", conn);
|
2007-08-02 03:28:40 +02:00
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
2007-03-17 22:34:37 +01:00
|
|
|
return 0;
|
2005-06-18 05:50:08 +02:00
|
|
|
}
|
2007-03-17 22:34:37 +01:00
|
|
|
used_quoted_string = 1;
|
2005-06-18 05:50:08 +02:00
|
|
|
}
|
2007-03-17 22:34:37 +01:00
|
|
|
|
2008-02-17 19:45:07 +01:00
|
|
|
if (!options->CookieAuthentication && !options->HashedControlPassword &&
|
|
|
|
!options->HashedControlSessionPassword) {
|
2007-09-05 02:31:07 +02:00
|
|
|
/* if Tor doesn't demand any stronger authentication, then
|
|
|
|
* the controller can get in with anything. */
|
|
|
|
goto ok;
|
|
|
|
}
|
|
|
|
|
2007-08-15 17:36:34 +02:00
|
|
|
if (options->CookieAuthentication) {
|
2008-02-17 19:45:07 +01:00
|
|
|
int also_password = options->HashedControlPassword != NULL ||
|
|
|
|
options->HashedControlSessionPassword != NULL;
|
2006-10-02 20:08:46 +02:00
|
|
|
if (password_len != AUTHENTICATION_COOKIE_LEN) {
|
2007-09-05 02:31:07 +02:00
|
|
|
if (!also_password) {
|
2007-09-08 22:25:57 +02:00
|
|
|
log_warn(LD_CONTROL, "Got authentication cookie with wrong length "
|
|
|
|
"(%d)", (int)password_len);
|
2007-09-05 02:31:07 +02:00
|
|
|
errstr = "Wrong length on authentication cookie.";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
bad_cookie = 1;
|
2006-10-02 20:08:46 +02:00
|
|
|
} else if (memcmp(authentication_cookie, password, password_len)) {
|
2007-09-05 02:31:07 +02:00
|
|
|
if (!also_password) {
|
|
|
|
log_warn(LD_CONTROL, "Got mismatched authentication cookie");
|
|
|
|
errstr = "Authentication cookie did not match expected value.";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
bad_cookie = 1;
|
2006-10-02 20:08:46 +02:00
|
|
|
} else {
|
2004-11-12 17:39:03 +01:00
|
|
|
goto ok;
|
|
|
|
}
|
2007-09-05 02:31:07 +02:00
|
|
|
}
|
|
|
|
|
2008-02-19 06:08:27 +01:00
|
|
|
if (options->HashedControlPassword ||
|
|
|
|
options->HashedControlSessionPassword) {
|
2008-02-17 19:45:07 +01:00
|
|
|
int bad = 0;
|
|
|
|
smartlist_t *sl_tmp;
|
2004-11-03 20:49:03 +01:00
|
|
|
char received[DIGEST_LEN];
|
2007-09-05 02:31:07 +02:00
|
|
|
int also_cookie = options->CookieAuthentication;
|
2008-02-17 19:45:07 +01:00
|
|
|
sl = smartlist_create();
|
|
|
|
if (options->HashedControlPassword) {
|
|
|
|
sl_tmp = decode_hashed_passwords(options->HashedControlPassword);
|
|
|
|
if (!sl_tmp)
|
|
|
|
bad = 1;
|
|
|
|
else {
|
|
|
|
smartlist_add_all(sl, sl_tmp);
|
|
|
|
smartlist_free(sl_tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (options->HashedControlSessionPassword) {
|
|
|
|
sl_tmp = decode_hashed_passwords(options->HashedControlSessionPassword);
|
|
|
|
if (!sl_tmp)
|
|
|
|
bad = 1;
|
|
|
|
else {
|
|
|
|
smartlist_add_all(sl, sl_tmp);
|
|
|
|
smartlist_free(sl_tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bad) {
|
2007-09-05 02:31:07 +02:00
|
|
|
if (!also_cookie) {
|
|
|
|
log_warn(LD_CONTROL,
|
|
|
|
"Couldn't decode HashedControlPassword: invalid base16");
|
2007-09-08 22:25:57 +02:00
|
|
|
errstr="Couldn't decode HashedControlPassword value in configuration.";
|
2007-09-05 02:31:07 +02:00
|
|
|
}
|
|
|
|
bad_password = 1;
|
2008-02-17 19:45:07 +01:00
|
|
|
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(sl);
|
2007-09-05 02:31:07 +02:00
|
|
|
} else {
|
2007-12-09 05:59:27 +01:00
|
|
|
SMARTLIST_FOREACH(sl, char *, expected,
|
|
|
|
{
|
|
|
|
secret_to_key(received,DIGEST_LEN,password,password_len,expected);
|
|
|
|
if (!memcmp(expected+S2K_SPECIFIER_LEN, received, DIGEST_LEN))
|
|
|
|
goto ok;
|
|
|
|
});
|
|
|
|
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(sl);
|
2006-10-02 20:08:46 +02:00
|
|
|
|
2007-09-05 02:31:07 +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? "
|
|
|
|
"If so, the standard requires that you put it in double quotes.";
|
|
|
|
bad_password = 1;
|
|
|
|
if (!also_cookie)
|
|
|
|
goto err;
|
|
|
|
}
|
2004-11-07 12:33:04 +01:00
|
|
|
}
|
2004-11-03 20:49:03 +01:00
|
|
|
|
2007-09-05 02:31:07 +02:00
|
|
|
/** We only get here if both kinds of authentication failed. */
|
|
|
|
tor_assert(bad_password && bad_cookie);
|
|
|
|
log_warn(LD_CONTROL, "Bad password or authentication cookie on controller.");
|
|
|
|
errstr = "Password did not match HashedControlPassword *or* authentication "
|
|
|
|
"cookie.";
|
|
|
|
|
2004-11-03 20:49:03 +01:00
|
|
|
err:
|
2007-03-04 22:08:28 +01:00
|
|
|
tor_free(password);
|
|
|
|
connection_printf_to_buf(conn, "515 Authentication failed: %s\r\n",
|
2008-12-29 15:21:25 +01:00
|
|
|
errstr ? errstr : "Unknown reason.");
|
2007-08-02 03:28:40 +02:00
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
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);
|
2007-03-04 22:08:28 +01:00
|
|
|
conn->_base.state = CONTROL_CONN_STATE_OPEN;
|
|
|
|
tor_free(password);
|
2007-12-09 05:59:27 +01:00
|
|
|
if (sl) { /* clean up */
|
|
|
|
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(sl);
|
|
|
|
}
|
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) {
|
2007-03-04 22:08:28 +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;
|
2007-03-17 22:34:37 +01:00
|
|
|
int n = 0;
|
|
|
|
char *s;
|
|
|
|
|
2007-03-04 22:08:28 +01:00
|
|
|
(void) len;
|
|
|
|
|
2007-03-17 22:34:37 +01:00
|
|
|
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;
|
|
|
|
else if (!strcasecmp(s, "NEWNYM"))
|
|
|
|
sig = SIGNEWNYM;
|
|
|
|
else if (!strcasecmp(s, "CLEARDNSCACHE"))
|
|
|
|
sig = SIGCLEARDNSCACHE;
|
|
|
|
else {
|
|
|
|
connection_printf_to_buf(conn, "552 Unrecognized signal code \"%s\"\r\n",
|
|
|
|
s);
|
|
|
|
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_control_done(conn);
|
2007-10-21 06:15:28 +02:00
|
|
|
/* Flush the "done" first if the signal might make us shut down. */
|
|
|
|
if (sig == SIGTERM || sig == SIGINT)
|
|
|
|
connection_handle_write(TO_CONN(conn), 1);
|
2006-10-25 23:39:42 +02:00
|
|
|
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,
|
2009-05-27 23:55:51 +02:00
|
|
|
* and report success or failure. */
|
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;
|
2009-05-27 23:55:51 +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();
|
2007-03-04 22:08:28 +01:00
|
|
|
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);
|
2007-03-04 22:08:28 +01:00
|
|
|
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);
|
2007-01-11 17:02:39 +01:00
|
|
|
if (address_is_invalid_destination(to, 1)) {
|
2007-03-04 22:08:28 +01:00
|
|
|
tor_snprintf(ans, anslen,
|
|
|
|
"512-syntax error: invalid address '%s'", to);
|
|
|
|
smartlist_add(reply, ans);
|
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) {
|
2007-03-04 22:08:28 +01:00
|
|
|
tor_snprintf(ans, anslen,
|
|
|
|
"451-resource exhausted: skipping '%s'", line);
|
|
|
|
smartlist_add(reply, ans);
|
2006-02-13 10:02:35 +01:00
|
|
|
log_warn(LD_CONTROL,
|
|
|
|
"Unable to allocate address for '%s' in MapAddress msg",
|
2009-12-15 23:23:36 +01:00
|
|
|
safe_str_client(line));
|
2005-03-02 20:26:46 +01:00
|
|
|
} else {
|
2007-03-04 22:08:28 +01: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 {
|
2008-02-21 19:45:11 +01:00
|
|
|
addressmap_register(from, tor_strdup(to), 1, ADDRMAPSRC_CONTROLLER);
|
2007-03-04 22:08:28 +01: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 {
|
2007-03-04 22:08:28 +01:00
|
|
|
size_t anslen = strlen(line)+256;
|
|
|
|
char *ans = tor_malloc(anslen);
|
|
|
|
tor_snprintf(ans, anslen, "512-syntax error: mapping '%s' is "
|
|
|
|
"not of expected form 'foo=bar'.", line);
|
|
|
|
smartlist_add(reply, ans);
|
2006-04-01 11:50:43 +02:00
|
|
|
log_info(LD_CONTROL, "Skipping MapAddress '%s': wrong "
|
2009-09-28 15:08:32 +02:00
|
|
|
"number of items.",
|
2009-12-15 23:23:36 +01:00
|
|
|
safe_str_client(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);
|
|
|
|
|
2007-03-17 22:34:37 +01:00
|
|
|
if (smartlist_len(reply)) {
|
|
|
|
((char*)smartlist_get(reply,smartlist_len(reply)-1))[3] = ' ';
|
|
|
|
r = smartlist_join_strings(reply, "\r\n", 1, &sz);
|
|
|
|
connection_write_to_buf(r, sz, TO_CONN(conn));
|
|
|
|
tor_free(r);
|
|
|
|
} else {
|
|
|
|
const char *response =
|
|
|
|
"512 syntax error: not enough arguments to mapaddress.\r\n";
|
|
|
|
connection_write_to_buf(response, strlen(response), TO_CONN(conn));
|
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
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Implementation helper for GETINFO: knows the answers for various
|
|
|
|
* trivial-to-implement questions. */
|
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")) {
|
2007-05-22 17:48:46 +02:00
|
|
|
*answer = tor_strdup(get_version());
|
2005-08-10 20:05:20 +02:00
|
|
|
} else if (!strcmp(question, "config-file")) {
|
|
|
|
*answer = tor_strdup(get_torrc_fname());
|
2009-12-14 01:21:06 +01:00
|
|
|
} else if (!strcmp(question, "config-text")) {
|
|
|
|
*answer = options_dump(get_options(), 1);
|
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 "
|
2007-02-16 21:01:02 +01:00
|
|
|
"NS STATUS_GENERAL STATUS_CLIENT STATUS_SERVER "
|
2009-02-16 07:18:44 +01:00
|
|
|
"GUARD STREAM_BW CLIENTS_SEEN NEWCONSENSUS");
|
2006-10-24 04:58:03 +02:00
|
|
|
} 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;
|
2008-07-24 15:44:04 +02:00
|
|
|
*answer = tor_dup_ip(addr);
|
2006-12-08 05:39:13 +01:00
|
|
|
} 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();
|
2007-03-15 04:11:53 +01:00
|
|
|
if (!me)
|
|
|
|
return -1;
|
|
|
|
*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;
|
|
|
|
}
|
|
|
|
|
2007-10-04 18:21:58 +02:00
|
|
|
/** Awful hack: return a newly allocated string based on a routerinfo and
|
|
|
|
* (possibly) an extrainfo, sticking the read-history and write-history from
|
|
|
|
* <b>ei</b> into the resulting string. The thing you get back won't
|
2007-10-19 01:18:28 +02:00
|
|
|
* necessarily have a valid signature.
|
2007-10-04 18:21:58 +02:00
|
|
|
*
|
2008-09-14 06:07:29 +02:00
|
|
|
* New code should never use this; it's for backward compatibility.
|
2007-10-04 18:21:58 +02:00
|
|
|
*
|
|
|
|
* NOTE: <b>ri_body</b> is as returned by signed_descriptor_get_body: it might
|
|
|
|
* not be NUL-terminated. */
|
2007-08-24 16:41:10 +02:00
|
|
|
static char *
|
|
|
|
munge_extrainfo_into_routerinfo(const char *ri_body, signed_descriptor_t *ri,
|
|
|
|
signed_descriptor_t *ei)
|
|
|
|
{
|
|
|
|
char *out = NULL, *outp;
|
|
|
|
int i;
|
|
|
|
const char *router_sig;
|
|
|
|
const char *ei_body = signed_descriptor_get_body(ei);
|
|
|
|
size_t ri_len = ri->signed_descriptor_len;
|
|
|
|
size_t ei_len = ei->signed_descriptor_len;
|
|
|
|
if (!ei_body)
|
|
|
|
goto bail;
|
|
|
|
|
|
|
|
outp = out = tor_malloc(ri_len+ei_len+1);
|
|
|
|
if (!(router_sig = tor_memstr(ri_body, ri_len, "\nrouter-signature")))
|
|
|
|
goto bail;
|
|
|
|
++router_sig;
|
|
|
|
memcpy(out, ri_body, router_sig-ri_body);
|
|
|
|
outp += router_sig-ri_body;
|
|
|
|
|
|
|
|
for (i=0; i < 2; ++i) {
|
|
|
|
const char *kwd = i?"\nwrite-history ":"\nread-history ";
|
|
|
|
const char *cp, *eol;
|
|
|
|
if (!(cp = tor_memstr(ei_body, ei_len, kwd)))
|
|
|
|
continue;
|
|
|
|
++cp;
|
|
|
|
eol = memchr(cp, '\n', ei_len - (cp-ei_body));
|
|
|
|
memcpy(outp, cp, eol-cp+1);
|
|
|
|
outp += eol-cp+1;
|
|
|
|
}
|
|
|
|
memcpy(outp, router_sig, ri_len - (router_sig-ri_body));
|
|
|
|
*outp++ = '\0';
|
|
|
|
tor_assert(outp-out < (int)(ri_len+ei_len+1));
|
|
|
|
|
|
|
|
return out;
|
|
|
|
bail:
|
|
|
|
tor_free(out);
|
|
|
|
return tor_strndup(ri_body, ri->signed_descriptor_len);
|
|
|
|
}
|
|
|
|
|
2007-02-24 08:50:38 +01:00
|
|
|
/** Implementation helper for GETINFO: knows the answers for questions about
|
2007-02-16 21:01:02 +01:00
|
|
|
* directory information. */
|
2006-12-08 05:39:13 +01:00
|
|
|
static int
|
|
|
|
getinfo_helper_dir(control_connection_t *control_conn,
|
|
|
|
const char *question, char **answer)
|
|
|
|
{
|
2009-05-08 18:12:20 +02:00
|
|
|
(void) control_conn;
|
2006-12-08 05:39:13 +01:00
|
|
|
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);
|
2007-08-24 16:41:10 +02:00
|
|
|
} else if (!strcmp(question, "desc/all-recent-extrainfo-hack")) {
|
|
|
|
/* XXXX Remove this once Torstat asks for extrainfos. */
|
|
|
|
routerlist_t *routerlist = router_get_routerlist();
|
|
|
|
smartlist_t *sl = smartlist_create();
|
|
|
|
if (routerlist && routerlist->routers) {
|
|
|
|
SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, ri,
|
|
|
|
{
|
|
|
|
const char *body = signed_descriptor_get_body(&ri->cache_info);
|
|
|
|
signed_descriptor_t *ei = extrainfo_get_by_descriptor_digest(
|
2007-08-24 16:41:15 +02:00
|
|
|
ri->cache_info.extra_info_digest);
|
2007-08-24 16:41:10 +02:00
|
|
|
if (ei && body) {
|
|
|
|
smartlist_add(sl, munge_extrainfo_into_routerinfo(body,
|
|
|
|
&ri->cache_info, ei));
|
|
|
|
} else if (body) {
|
|
|
|
smartlist_add(sl,
|
|
|
|
tor_strndup(body, ri->cache_info.signed_descriptor_len));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
*answer = smartlist_join_strings(sl, "", 0, NULL);
|
|
|
|
SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
|
|
|
|
smartlist_free(sl);
|
2007-12-06 08:15:06 +01:00
|
|
|
} else if (!strcmpstart(question, "desc-annotations/id/")) {
|
|
|
|
routerinfo_t *ri = router_get_by_hexdigest(question+
|
|
|
|
strlen("desc-annotations/id/"));
|
|
|
|
if (ri) {
|
|
|
|
const char *annotations =
|
|
|
|
signed_descriptor_get_annotations(&ri->cache_info);
|
|
|
|
if (annotations)
|
|
|
|
*answer = tor_strndup(annotations,
|
2007-12-06 08:17:05 +01:00
|
|
|
ri->cache_info.annotations_len);
|
2007-12-06 08:15:06 +01:00
|
|
|
}
|
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);
|
2008-01-16 06:27:19 +01:00
|
|
|
smartlist_free(descs);
|
2009-10-27 03:12:40 +01:00
|
|
|
tor_free(url);
|
2006-12-08 05:39:13 +01:00
|
|
|
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/")) {
|
2007-12-01 05:58:53 +01:00
|
|
|
if (directory_permits_controller_requests(get_options())) {
|
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();
|
|
|
|
dirserv_get_networkstatus_v2_fingerprints(
|
|
|
|
fp_list, question+strlen("dir/status/"));
|
|
|
|
SMARTLIST_FOREACH(fp_list, const char *, fp, {
|
|
|
|
char *s;
|
2007-10-17 18:55:44 +02:00
|
|
|
char *fname = networkstatus_get_cache_filename(fp);
|
|
|
|
s = read_file_to_str(fname, 0, NULL);
|
2006-12-08 05:39:13 +01:00
|
|
|
if (s)
|
|
|
|
smartlist_add(status_list, s);
|
2007-10-17 18:55:44 +02:00
|
|
|
tor_free(fname);
|
2006-12-08 05:39:13 +01:00
|
|
|
});
|
|
|
|
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);
|
|
|
|
}
|
2008-09-14 06:07:29 +02:00
|
|
|
} else if (!strcmp(question, "dir/status-vote/current/consensus")) { /* v3 */
|
|
|
|
if (directory_caches_dir_info(get_options())) {
|
2009-10-13 23:06:01 +02:00
|
|
|
const cached_dir_t *consensus = dirserv_get_consensus("ns");
|
2008-09-14 06:07:29 +02:00
|
|
|
if (consensus)
|
|
|
|
*answer = tor_strdup(consensus->dir);
|
|
|
|
}
|
|
|
|
if (!*answer) { /* try loading it from disk */
|
|
|
|
char *filename = get_datadir_fname("cached-consensus");
|
|
|
|
*answer = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
|
|
|
|
tor_free(filename);
|
|
|
|
}
|
|
|
|
} else if (!strcmp(question, "network-status")) { /* v1 */
|
2005-10-18 19:43:54 +02:00
|
|
|
routerlist_t *routerlist = router_get_routerlist();
|
2005-03-23 20:15:10 +01:00
|
|
|
if (!routerlist || !routerlist->routers ||
|
2009-05-08 18:12:20 +02:00
|
|
|
list_server_status_v1(routerlist->routers, answer, 1) < 0) {
|
2005-03-22 04:27:51 +01:00
|
|
|
return -1;
|
2005-03-23 20:15:10 +01:00
|
|
|
}
|
2007-08-24 16:41:15 +02:00
|
|
|
} else if (!strcmpstart(question, "extra-info/digest/")) {
|
|
|
|
question += strlen("extra-info/digest/");
|
|
|
|
if (strlen(question) == HEX_DIGEST_LEN) {
|
|
|
|
char d[DIGEST_LEN];
|
2008-01-14 20:00:23 +01:00
|
|
|
signed_descriptor_t *sd = NULL;
|
2008-02-24 23:11:12 +01:00
|
|
|
if (base16_decode(d, sizeof(d), question, strlen(question))==0) {
|
|
|
|
/* XXXX this test should move into extrainfo_get_by_descriptor_digest,
|
|
|
|
* but I don't want to risk affecting other parts of the code,
|
|
|
|
* especially since the rules for using our own extrainfo (including
|
|
|
|
* when it might be freed) are different from those for using one
|
|
|
|
* we have downloaded. */
|
|
|
|
if (router_extrainfo_digest_is_me(d))
|
|
|
|
sd = &(router_get_my_extrainfo()->cache_info);
|
|
|
|
else
|
|
|
|
sd = extrainfo_get_by_descriptor_digest(d);
|
|
|
|
}
|
2007-08-24 16:41:15 +02:00
|
|
|
if (sd) {
|
|
|
|
const char *body = signed_descriptor_get_body(sd);
|
|
|
|
if (body)
|
|
|
|
*answer = tor_strndup(body, sd->signed_descriptor_len);
|
|
|
|
}
|
|
|
|
}
|
2006-12-08 05:39:13 +01:00
|
|
|
}
|
2007-08-24 16:41:15 +02:00
|
|
|
|
2006-12-08 05:39:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Implementation helper for GETINFO: knows how to generate summaries of the
|
|
|
|
* current states of things we send events about. */
|
2006-12-08 05:39:13 +01:00
|
|
|
static int
|
|
|
|
getinfo_helper_events(control_connection_t *control_conn,
|
|
|
|
const char *question, char **answer)
|
|
|
|
{
|
2009-05-08 18:12:20 +02:00
|
|
|
(void) control_conn;
|
2006-12-08 05:39:13 +01:00
|
|
|
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;
|
2008-11-11 16:59:24 +01:00
|
|
|
const char *purpose;
|
2005-06-19 22:40:41 +02:00
|
|
|
if (! CIRCUIT_IS_ORIGIN(circ) || circ->marked_for_close)
|
|
|
|
continue;
|
2009-05-08 18:12:20 +02:00
|
|
|
|
|
|
|
path = circuit_list_path_for_controller(TO_ORIGIN_CIRCUIT(circ));
|
|
|
|
|
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";
|
|
|
|
|
2008-11-11 16:59:24 +01:00
|
|
|
purpose = circuit_purpose_to_controller_string(circ->purpose);
|
|
|
|
slen = strlen(path)+strlen(state)+strlen(purpose)+30;
|
2005-06-19 22:40:41 +02:00
|
|
|
s = tor_malloc(slen+1);
|
2008-11-11 16:59:24 +01:00
|
|
|
tor_snprintf(s, slen, "%lu %s%s%s PURPOSE=%s",
|
2006-07-28 17:11:11 +02:00
|
|
|
(unsigned long)TO_ORIGIN_CIRCUIT(circ)->global_identifier,
|
2008-11-11 16:59:24 +01:00
|
|
|
state, *path ? " " : "", path, purpose);
|
2005-06-19 22:40:41 +02:00
|
|
|
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")) {
|
2007-05-22 17:49:14 +02:00
|
|
|
smartlist_t *conns = get_connection_array();
|
2005-06-19 22:40:41 +02:00
|
|
|
smartlist_t *status = smartlist_create();
|
2007-05-22 17:49:14 +02:00
|
|
|
char buf[256];
|
2008-08-15 15:55:01 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(conns, connection_t *, base_conn) {
|
2005-06-19 22:40:41 +02:00
|
|
|
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;
|
2007-05-22 17:49:14 +02:00
|
|
|
if (base_conn->type != CONN_TYPE_AP ||
|
|
|
|
base_conn->marked_for_close ||
|
|
|
|
base_conn->state == AP_CONN_STATE_SOCKS_WAIT ||
|
|
|
|
base_conn->state == AP_CONN_STATE_NATD_WAIT)
|
2005-06-19 22:40:41 +02:00
|
|
|
continue;
|
2007-05-22 17:49:14 +02:00
|
|
|
conn = TO_EDGE_CONN(base_conn);
|
2006-07-26 21:07:26 +02:00
|
|
|
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",
|
2008-08-15 15:55:01 +02:00
|
|
|
(unsigned long) conn->_base.global_identifier,state,
|
2006-07-28 17:11:11 +02:00
|
|
|
origin_circ?
|
|
|
|
(unsigned long)origin_circ->global_identifier : 0ul,
|
2005-06-19 22:40:41 +02:00
|
|
|
buf);
|
|
|
|
smartlist_add(status, s);
|
2008-08-15 15:55:01 +02:00
|
|
|
} SMARTLIST_FOREACH_END(base_conn);
|
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")) {
|
2007-05-22 17:49:14 +02:00
|
|
|
smartlist_t *conns = get_connection_array();
|
2005-06-19 22:40:41 +02:00
|
|
|
smartlist_t *status = smartlist_create();
|
2009-05-08 18:12:20 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(conns, connection_t *, base_conn) {
|
2005-06-19 22:40:41 +02:00
|
|
|
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;
|
2007-05-22 17:49:14 +02:00
|
|
|
if (base_conn->type != CONN_TYPE_OR || base_conn->marked_for_close)
|
2005-06-19 22:40:41 +02:00
|
|
|
continue;
|
2007-05-22 17:49:14 +02:00
|
|
|
conn = TO_OR_CONN(base_conn);
|
2006-07-26 21:07:26 +02:00
|
|
|
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";
|
2009-05-08 18:12:20 +02:00
|
|
|
orconn_target_get_name(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);
|
2009-05-08 18:12:20 +02:00
|
|
|
} SMARTLIST_FOREACH_END(base_conn);
|
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);
|
2009-05-01 12:25:18 +02:00
|
|
|
} else if (!strcmpstart(question, "address-mappings/")) {
|
2005-03-03 07:37:54 +01:00
|
|
|
time_t min_e, max_e;
|
|
|
|
smartlist_t *mappings;
|
2009-05-01 12:25:18 +02:00
|
|
|
question += strlen("address-mappings/");
|
2007-07-16 18:58:11 +02:00
|
|
|
if (!strcmp(question, "all")) {
|
2005-03-03 07:37:54 +01:00
|
|
|
min_e = 0; max_e = TIME_MAX;
|
2007-07-16 18:58:11 +02:00
|
|
|
} else if (!strcmp(question, "cache")) {
|
2005-03-03 07:37:54 +01:00
|
|
|
min_e = 2; max_e = TIME_MAX;
|
2007-07-16 18:58:11 +02:00
|
|
|
} else if (!strcmp(question, "config")) {
|
2005-03-03 07:37:54 +01:00
|
|
|
min_e = 0; max_e = 0;
|
2007-07-16 18:58:11 +02:00
|
|
|
} else if (!strcmp(question, "control")) {
|
2005-03-03 07:37:54 +01:00
|
|
|
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();
|
2009-05-01 12:25:18 +02:00
|
|
|
addressmap_get_mappings(mappings, min_e, max_e, 1);
|
2007-03-15 07:25:00 +01: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);
|
2007-05-06 00:51:02 +02:00
|
|
|
} else if (!strcmpstart(question, "status/")) {
|
2007-05-10 21:30:02 +02:00
|
|
|
/* Note that status/ is not a catch-all for events; there's only supposed
|
|
|
|
* to be a status GETINFO if there's a corresponding STATUS event. */
|
2007-05-06 00:51:02 +02:00
|
|
|
if (!strcmp(question, "status/circuit-established")) {
|
|
|
|
*answer = tor_strdup(has_completed_circuit ? "1" : "0");
|
2007-07-10 19:14:55 +02:00
|
|
|
} else if (!strcmp(question, "status/enough-dir-info")) {
|
|
|
|
*answer = tor_strdup(router_have_minimum_dir_info() ? "1" : "0");
|
2009-09-01 00:37:25 +02:00
|
|
|
} else if (!strcmp(question, "status/good-server-descriptor") ||
|
|
|
|
!strcmp(question, "status/accepted-server-descriptor")) {
|
|
|
|
/* They're equivalent for now, until we can figure out how to make
|
|
|
|
* good-server-descriptor be what we want. See comment in
|
|
|
|
* control-spec.txt. */
|
2007-09-18 17:53:55 +02:00
|
|
|
*answer = tor_strdup(directories_have_accepted_server_descriptor()
|
|
|
|
? "1" : "0");
|
|
|
|
} else if (!strcmp(question, "status/reachability-succeeded/or")) {
|
|
|
|
*answer = tor_strdup(check_whether_orport_reachable() ? "1" : "0");
|
|
|
|
} else if (!strcmp(question, "status/reachability-succeeded/dir")) {
|
|
|
|
*answer = tor_strdup(check_whether_dirport_reachable() ? "1" : "0");
|
|
|
|
} else if (!strcmp(question, "status/reachability-succeeded")) {
|
|
|
|
*answer = tor_malloc(16);
|
|
|
|
tor_snprintf(*answer, 16, "OR=%d DIR=%d",
|
|
|
|
check_whether_orport_reachable() ? 1 : 0,
|
|
|
|
check_whether_dirport_reachable() ? 1 : 0);
|
2008-06-17 10:01:43 +02:00
|
|
|
} else if (!strcmp(question, "status/bootstrap-phase")) {
|
|
|
|
*answer = tor_strdup(last_sent_bootstrap_message);
|
2007-05-10 21:30:02 +02:00
|
|
|
} else if (!strcmpstart(question, "status/version/")) {
|
|
|
|
int is_server = server_mode(get_options());
|
2008-02-05 22:39:29 +01:00
|
|
|
networkstatus_t *c = networkstatus_get_latest_consensus();
|
2007-10-16 01:15:24 +02:00
|
|
|
version_status_t status;
|
|
|
|
const char *recommended;
|
|
|
|
if (c) {
|
|
|
|
recommended = is_server ? c->server_versions : c->client_versions;
|
|
|
|
status = tor_version_is_obsolete(VERSION, recommended);
|
|
|
|
} else {
|
|
|
|
recommended = "?";
|
|
|
|
status = VS_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2007-05-10 21:30:02 +02:00
|
|
|
if (!strcmp(question, "status/version/recommended")) {
|
2007-10-16 01:15:24 +02:00
|
|
|
*answer = tor_strdup(recommended);
|
2007-05-10 21:30:02 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!strcmp(question, "status/version/current")) {
|
2007-10-16 01:15:24 +02:00
|
|
|
switch (status)
|
2007-05-10 21:30:02 +02:00
|
|
|
{
|
|
|
|
case VS_RECOMMENDED: *answer = tor_strdup("recommended"); break;
|
|
|
|
case VS_OLD: *answer = tor_strdup("obsolete"); break;
|
|
|
|
case VS_NEW: *answer = tor_strdup("new"); break;
|
|
|
|
case VS_NEW_IN_SERIES: *answer = tor_strdup("new in series"); break;
|
|
|
|
case VS_UNRECOMMENDED: *answer = tor_strdup("unrecommended"); break;
|
2008-02-06 13:45:04 +01:00
|
|
|
case VS_EMPTY: *answer = tor_strdup("none recommended"); break;
|
2007-10-16 01:15:24 +02:00
|
|
|
case VS_UNKNOWN: *answer = tor_strdup("unknown"); break;
|
2007-05-10 21:30:02 +02:00
|
|
|
default: tor_fragile_assert();
|
|
|
|
}
|
2007-10-16 01:15:24 +02:00
|
|
|
} else if (!strcmp(question, "status/version/num-versioning") ||
|
|
|
|
!strcmp(question, "status/version/num-concurring")) {
|
2007-05-10 21:30:02 +02:00
|
|
|
char s[33];
|
2007-10-16 01:15:24 +02:00
|
|
|
tor_snprintf(s, sizeof(s), "%d", get_n_authorities(V3_AUTHORITY));
|
2007-05-10 21:30:02 +02:00
|
|
|
*answer = tor_strdup(s);
|
2008-02-05 22:39:49 +01:00
|
|
|
log_warn(LD_GENERAL, "%s is deprecated; it no longer gives useful "
|
2008-02-05 22:39:51 +01:00
|
|
|
"information", question);
|
2007-05-10 21:30:02 +02:00
|
|
|
}
|
2009-01-28 07:50:36 +01:00
|
|
|
} else if (!strcmp(question, "status/clients-seen")) {
|
2009-12-12 08:32:46 +01:00
|
|
|
char *bridge_stats = geoip_get_bridge_stats_controller(time(NULL));
|
|
|
|
if (!bridge_stats)
|
2009-01-28 07:50:36 +01:00
|
|
|
return -1;
|
2009-12-12 08:32:46 +01:00
|
|
|
*answer = bridge_stats;
|
2007-05-06 00:51:02 +02:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2006-12-08 05:39:13 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Callback function for GETINFO: on a given control connection, try to
|
|
|
|
* answer the question <b>q</b> and store the newly-allocated answer in
|
|
|
|
* *<b>a</b>. If there's no answer, or an error occurs, just don't set
|
|
|
|
* <b>a</b>. Return 0.
|
|
|
|
*/
|
2006-12-08 05:39:13 +01:00
|
|
|
typedef int (*getinfo_helper_t)(control_connection_t *,
|
|
|
|
const char *q, char **a);
|
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** A single item for the GETINFO question-to-answer-function table. */
|
2006-12-08 05:39:13 +01:00
|
|
|
typedef struct getinfo_item_t {
|
2007-02-24 08:50:38 +01:00
|
|
|
const char *varname; /**< The value (or prefix) of the question. */
|
2007-02-16 21:01:02 +01:00
|
|
|
getinfo_helper_t fn; /**< The function that knows the answer: NULL if
|
|
|
|
* this entry is documentation-only. */
|
|
|
|
const char *desc; /**< Description of the variable. */
|
|
|
|
int is_prefix; /** Must varname match exactly, or must it be a prefix? */
|
2006-12-08 05:39:13 +01:00
|
|
|
} 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 }
|
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Table mapping questions accepted by GETINFO to the functions that know how
|
|
|
|
* to answer them. */
|
2006-12-08 05:39:13 +01:00
|
|
|
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."),
|
2009-12-14 01:21:06 +01:00
|
|
|
ITEM("config-text", misc,
|
|
|
|
"Return the string that would be written by a saveconf command."),
|
2006-12-08 05:39:13 +01:00
|
|
|
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-08-24 16:41:10 +02:00
|
|
|
ITEM("desc/all-recent-extrainfo-hack", dir, NULL), /* Hack. */
|
2007-09-14 21:27:04 +02:00
|
|
|
PREFIX("extra-info/digest/", dir, "Extra-info documents by digest."),
|
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)."),
|
2007-12-19 05:58:58 +01:00
|
|
|
PREFIX("ns/purpose/", networkstatus,
|
|
|
|
"Brief summary of router status by purpose (v2 directory format)."),
|
2006-12-08 05:39:13 +01:00
|
|
|
|
|
|
|
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."),
|
2007-07-16 18:58:11 +02:00
|
|
|
PREFIX("address-mappings/", events, NULL),
|
|
|
|
DOC("address-mappings/all", "Current address mappings."),
|
|
|
|
DOC("address-mappings/cache", "Current cached DNS replies."),
|
|
|
|
DOC("address-mappings/config",
|
|
|
|
"Current address mappings from configuration."),
|
|
|
|
DOC("address-mappings/control", "Current address mappings from controller."),
|
2007-05-06 00:51:02 +02:00
|
|
|
PREFIX("status/", events, NULL),
|
|
|
|
DOC("status/circuit-established",
|
|
|
|
"Whether we think client functionality is working."),
|
2007-07-10 19:14:55 +02:00
|
|
|
DOC("status/enough-dir-info",
|
|
|
|
"Whether we have enough up-to-date directory information to build "
|
|
|
|
"circuits."),
|
2008-06-17 10:01:43 +02:00
|
|
|
DOC("status/bootstrap-phase",
|
|
|
|
"The last bootstrap phase status event that Tor sent."),
|
2009-01-28 07:50:36 +01:00
|
|
|
DOC("status/clients-seen",
|
|
|
|
"Breakdown of client countries seen by a bridge."),
|
2007-05-10 21:30:02 +02:00
|
|
|
DOC("status/version/recommended", "List of currently recommended versions."),
|
|
|
|
DOC("status/version/current", "Status of the current version."),
|
|
|
|
DOC("status/version/num-versioning", "Number of versioning authorities."),
|
|
|
|
DOC("status/version/num-concurring",
|
|
|
|
"Number of versioning authorities agreeing on the status of the "
|
|
|
|
"current version"),
|
2006-12-08 05:39:13 +01:00
|
|
|
ITEM("address", misc, "IP address of this Tor host, if we can guess it."),
|
|
|
|
ITEM("dir-usage", misc, "Breakdown of bytes transferred over DirPort."),
|
2007-12-22 06:27:30 +01:00
|
|
|
PREFIX("desc-annotations/id/", dir, "Router annotations by hexdigest."),
|
2006-12-08 05:39:13 +01:00
|
|
|
PREFIX("dir/server/", dir,"Router descriptors as retrieved from a DirPort."),
|
2008-09-14 06:07:29 +02:00
|
|
|
PREFIX("dir/status/", dir,
|
|
|
|
"v2 networkstatus docs as retrieved from a DirPort."),
|
|
|
|
ITEM("dir/status-vote/current/consensus", dir,
|
|
|
|
"v3 Networkstatus consensus as retrieved from a DirPort."),
|
2006-12-08 05:39:13 +01:00
|
|
|
PREFIX("exit-policy/default", policies,
|
|
|
|
"The default value appended to the configured exit policy."),
|
2007-12-17 23:44:18 +01:00
|
|
|
PREFIX("ip-to-country/", geoip, "Perform a GEOIP lookup"),
|
2006-12-08 05:39:13 +01:00
|
|
|
{ NULL, NULL, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Allocate and return a list of recognized GETINFO options. */
|
2006-12-08 05:39:13 +01:00
|
|
|
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
|
|
|
{
|
2009-01-13 15:43:51 +01:00
|
|
|
smartlist_t *questions = smartlist_create();
|
|
|
|
smartlist_t *answers = smartlist_create();
|
|
|
|
smartlist_t *unrecognized = smartlist_create();
|
2005-08-13 03:55:23 +02:00
|
|
|
char *msg = NULL, *ans = NULL;
|
2007-03-17 22:34:37 +01:00
|
|
|
int i;
|
2009-05-27 23:55:51 +02:00
|
|
|
(void) len; /* body is NUL-terminated, so it's safe to ignore the length. */
|
2005-02-25 07:37:07 +01:00
|
|
|
|
2007-03-04 22:08:28 +01:00
|
|
|
smartlist_split_string(questions, body, " ",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
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) {
|
2007-03-04 22:08:28 +01:00
|
|
|
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) {
|
2007-03-04 22:08:28 +01:00
|
|
|
smartlist_add(unrecognized, (char*)q);
|
2005-06-18 04:39:25 +02:00
|
|
|
} 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)) {
|
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
|
|
|
|
2007-03-17 22:34:37 +01:00
|
|
|
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')) {
|
|
|
|
connection_printf_to_buf(conn, "250-%s=", k);
|
|
|
|
connection_write_str_to_buf(v, conn);
|
|
|
|
connection_write_str_to_buf("\r\n", conn);
|
|
|
|
} else {
|
|
|
|
char *esc = NULL;
|
2007-05-17 00:15:14 +02:00
|
|
|
size_t esc_len;
|
2007-08-29 21:02:33 +02:00
|
|
|
esc_len = write_escaped_data(v, strlen(v), &esc);
|
2007-03-17 22:34:37 +01:00
|
|
|
connection_printf_to_buf(conn, "250+%s=\r\n", k);
|
2007-05-17 00:15:14 +02:00
|
|
|
connection_write_to_buf(esc, esc_len, TO_CONN(conn));
|
2007-03-17 22:34:37 +01:00
|
|
|
tor_free(esc);
|
2005-06-18 04:39:25 +02:00
|
|
|
}
|
|
|
|
}
|
2007-03-17 22:34:37 +01:00
|
|
|
connection_write_str_to_buf("250 OK\r\n", conn);
|
2005-02-25 07:37:07 +01:00
|
|
|
|
|
|
|
done:
|
2009-01-13 15:43:51 +01:00
|
|
|
SMARTLIST_FOREACH(answers, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(answers);
|
|
|
|
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
|
|
|
|
2007-10-12 09:57:29 +02:00
|
|
|
/** Given a string, convert it to a circuit purpose. */
|
|
|
|
static uint8_t
|
|
|
|
circuit_purpose_from_string(const char *string)
|
2006-02-23 07:51:09 +01:00
|
|
|
{
|
2007-10-12 09:57:29 +02:00
|
|
|
if (!strcmpstart(string, "purpose="))
|
|
|
|
string += strlen("purpose=");
|
|
|
|
|
|
|
|
if (!strcmp(string, "general"))
|
|
|
|
return CIRCUIT_PURPOSE_C_GENERAL;
|
|
|
|
else if (!strcmp(string, "controller"))
|
|
|
|
return CIRCUIT_PURPOSE_CONTROLLER;
|
|
|
|
else
|
|
|
|
return CIRCUIT_PURPOSE_UNKNOWN;
|
2006-02-23 07:51:09 +01:00
|
|
|
}
|
|
|
|
|
2007-03-17 22:34:41 +01:00
|
|
|
/** Return a newly allocated smartlist containing the arguments to the command
|
|
|
|
* waiting in <b>body</b>. If there are fewer than <b>min_args</b> arguments,
|
|
|
|
* or if <b>max_args</b> is nonnegative and there are more than
|
|
|
|
* <b>max_args</b> arguments, send a 512 error to the controller, using
|
|
|
|
* <b>command</b> as the command name in the error message. */
|
|
|
|
static smartlist_t *
|
|
|
|
getargs_helper(const char *command, control_connection_t *conn,
|
|
|
|
const char *body, int min_args, int max_args)
|
|
|
|
{
|
|
|
|
smartlist_t *args = smartlist_create();
|
|
|
|
smartlist_split_string(args, body, " ",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
|
|
if (smartlist_len(args) < min_args) {
|
|
|
|
connection_printf_to_buf(conn, "512 Missing argument to %s\r\n",command);
|
|
|
|
goto err;
|
|
|
|
} else if (max_args >= 0 && smartlist_len(args) > max_args) {
|
|
|
|
connection_printf_to_buf(conn, "512 Too many arguments to %s\r\n",command);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
err:
|
|
|
|
SMARTLIST_FOREACH(args, char *, s, tor_free(s));
|
|
|
|
smartlist_free(args);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
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;
|
2006-07-28 17:11:11 +02:00
|
|
|
origin_circuit_t *circ = NULL;
|
2007-03-04 22:08:28 +01:00
|
|
|
int zero_circ;
|
2006-02-23 07:51:09 +01:00
|
|
|
uint8_t intended_purpose = CIRCUIT_PURPOSE_C_GENERAL;
|
2007-03-17 22:34:37 +01:00
|
|
|
smartlist_t *args;
|
2007-03-04 22:08:28 +01:00
|
|
|
(void) len;
|
2005-03-19 07:05:55 +01:00
|
|
|
|
|
|
|
router_nicknames = smartlist_create();
|
2005-06-18 05:09:43 +02:00
|
|
|
|
2007-03-17 22:34:41 +01:00
|
|
|
args = getargs_helper("EXTENDCIRCUIT", conn, body, 2, -1);
|
|
|
|
if (!args)
|
2007-03-17 22:34:37 +01:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
zero_circ = !strcmp("0", (char*)smartlist_get(args,0));
|
|
|
|
if (!zero_circ && !(circ = get_circ(smartlist_get(args,0)))) {
|
|
|
|
connection_printf_to_buf(conn, "552 Unknown circuit \"%s\"\r\n",
|
|
|
|
(char*)smartlist_get(args, 0));
|
|
|
|
}
|
|
|
|
smartlist_split_string(router_nicknames, smartlist_get(args,1), ",", 0, 0);
|
|
|
|
|
|
|
|
if (zero_circ && smartlist_len(args)>2) {
|
|
|
|
char *purp = smartlist_get(args,2);
|
2007-10-12 09:57:29 +02:00
|
|
|
intended_purpose = circuit_purpose_from_string(purp);
|
|
|
|
if (intended_purpose == CIRCUIT_PURPOSE_UNKNOWN) {
|
2007-03-17 22:34:37 +01:00
|
|
|
connection_printf_to_buf(conn, "552 Unknown purpose \"%s\"\r\n", purp);
|
|
|
|
SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(args);
|
2005-07-17 23:06:00 +02:00
|
|
|
goto done;
|
|
|
|
}
|
2005-03-22 01:42:38 +01:00
|
|
|
}
|
2007-03-17 22:34:37 +01:00
|
|
|
SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(args);
|
|
|
|
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) {
|
2007-03-04 22:08:28 +01:00
|
|
|
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)) {
|
2007-03-04 22:08:28 +01:00
|
|
|
connection_write_str_to_buf("512 No router names provided\r\n", conn);
|
2005-06-18 05:09:43 +02:00
|
|
|
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 */
|
2007-12-23 20:15:22 +01:00
|
|
|
circ = origin_circuit_init(intended_purpose, 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);
|
2007-03-04 22:08:28 +01:00
|
|
|
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);
|
2007-03-15 04:15:34 +01:00
|
|
|
connection_write_str_to_buf("551 Couldn't send onion skin\r\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
|
|
|
|
2007-03-04 22:08:28 +01:00
|
|
|
connection_printf_to_buf(conn, "250 EXTENDED %lu\r\n",
|
2005-06-18 05:09:43 +02:00
|
|
|
(unsigned long)circ->global_identifier);
|
2007-03-17 05:32:04 +01:00
|
|
|
if (zero_circ) /* send a 'launched' event, for completeness */
|
|
|
|
control_event_circuit_status(circ, CIRC_EVENT_LAUNCHED, 0);
|
2005-03-19 07:05:55 +01:00
|
|
|
done:
|
|
|
|
SMARTLIST_FOREACH(router_nicknames, char *, n, tor_free(n));
|
|
|
|
smartlist_free(router_nicknames);
|
2009-12-12 08:07:59 +01:00
|
|
|
smartlist_free(routers);
|
2005-02-25 07:16:28 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2005-06-11 20:52:12 +02:00
|
|
|
|
2007-10-12 00:19:47 +02:00
|
|
|
/** Called when we get a SETCIRCUITPURPOSE message. If we can find the
|
|
|
|
* circuit and it's a valid purpose, change it. */
|
2006-02-23 07:51:09 +01:00
|
|
|
static int
|
2007-10-12 00:19:47 +02:00
|
|
|
handle_control_setcircuitpurpose(control_connection_t *conn,
|
|
|
|
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-02-23 07:51:09 +01:00
|
|
|
uint8_t new_purpose;
|
2007-03-17 22:34:41 +01:00
|
|
|
smartlist_t *args;
|
2009-05-27 23:55:51 +02:00
|
|
|
(void) len; /* body is NUL-terminated, so it's safe to ignore the length. */
|
2007-03-17 22:34:41 +01:00
|
|
|
|
2007-10-12 00:19:47 +02:00
|
|
|
args = getargs_helper("SETCIRCUITPURPOSE", conn, body, 2, -1);
|
2007-03-17 22:34:41 +01:00
|
|
|
if (!args)
|
2006-02-23 07:51:09 +01:00
|
|
|
goto done;
|
|
|
|
|
2007-10-12 00:19:47 +02:00
|
|
|
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;
|
2006-02-23 07:51:09 +01:00
|
|
|
}
|
|
|
|
|
2006-09-18 06:24:41 +02:00
|
|
|
{
|
|
|
|
char *purp = smartlist_get(args,1);
|
2007-10-12 09:57:29 +02:00
|
|
|
new_purpose = circuit_purpose_from_string(purp);
|
|
|
|
if (new_purpose == CIRCUIT_PURPOSE_UNKNOWN) {
|
2006-09-18 06:24:41 +02:00
|
|
|
connection_printf_to_buf(conn, "552 Unknown purpose \"%s\"\r\n", purp);
|
|
|
|
goto done;
|
|
|
|
}
|
2006-02-23 07:51:09 +01:00
|
|
|
}
|
|
|
|
|
2007-10-12 00:19:47 +02:00
|
|
|
circ->_base.purpose = new_purpose;
|
2006-02-23 07:51:09 +01:00
|
|
|
connection_write_str_to_buf("250 OK\r\n", conn);
|
|
|
|
|
|
|
|
done:
|
2007-03-17 22:34:41 +01:00
|
|
|
if (args) {
|
|
|
|
SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(args);
|
|
|
|
}
|
2006-02-23 07:51:09 +01:00
|
|
|
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;
|
2007-03-17 22:34:37 +01:00
|
|
|
smartlist_t *args;
|
2007-04-30 13:10:45 +02:00
|
|
|
crypt_path_t *cpath=NULL;
|
|
|
|
int hop=0, hop_line_ok=1;
|
2007-03-04 22:08:28 +01:00
|
|
|
(void) len;
|
2005-03-12 05:22:01 +01:00
|
|
|
|
2007-03-17 22:34:41 +01:00
|
|
|
args = getargs_helper("ATTACHSTREAM", conn, body, 2, -1);
|
|
|
|
if (!args)
|
2007-03-17 22:34:37 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
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));
|
2007-04-30 13:10:45 +02:00
|
|
|
} else if (circ && smartlist_len(args) > 2) {
|
|
|
|
char *hopstring = smartlist_get(args, 2);
|
|
|
|
if (!strcasecmpstart(hopstring, "HOP=")) {
|
|
|
|
hopstring += strlen("HOP=");
|
2008-02-22 20:09:45 +01:00
|
|
|
hop = (int) tor_parse_ulong(hopstring, 10, 0, INT_MAX,
|
|
|
|
&hop_line_ok, NULL);
|
2007-04-30 13:10:45 +02:00
|
|
|
if (!hop_line_ok) { /* broken hop line */
|
|
|
|
connection_printf_to_buf(conn, "552 Bad value hop=%s\r\n", hopstring);
|
|
|
|
}
|
|
|
|
}
|
2005-03-12 05:22:01 +01:00
|
|
|
}
|
2007-03-17 22:34:37 +01:00
|
|
|
SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(args);
|
2007-04-30 13:10:45 +02:00
|
|
|
if (!ap_conn || (!zero_circ && !circ) || !hop_line_ok)
|
2007-03-17 22:34:37 +01:00
|
|
|
return 0;
|
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) {
|
2007-03-04 22:08:28 +01:00
|
|
|
connection_write_str_to_buf(
|
|
|
|
"555 Connection is not managed by controller.\r\n",
|
|
|
|
conn);
|
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);
|
2007-03-24 16:57:51 +01:00
|
|
|
connection_edge_end(ap_conn, END_STREAM_REASON_TIMEOUT);
|
2006-03-09 07:29:53 +01:00
|
|
|
/* Un-mark it as ending, since we're going to reuse it. */
|
2008-12-17 15:59:28 +01:00
|
|
|
ap_conn->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)) {
|
2007-03-04 22:08:28 +01:00
|
|
|
connection_write_str_to_buf(
|
2007-04-30 13:10:45 +02:00
|
|
|
"551 Can't attach stream to non-open origin circuit\r\n",
|
2007-03-04 22:08:28 +01:00
|
|
|
conn);
|
2005-03-24 07:18:59 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2008-09-26 20:58:45 +02:00
|
|
|
/* Is this a single hop circuit? */
|
2007-04-30 13:10:45 +02:00
|
|
|
if (circ && (circuit_get_cpath_len(circ)<2 || hop==1)) {
|
2008-09-26 20:58:45 +02:00
|
|
|
routerinfo_t *r = NULL;
|
|
|
|
char* exit_digest;
|
|
|
|
if (circ->build_state &&
|
|
|
|
circ->build_state->chosen_exit &&
|
2009-10-27 02:59:34 +01:00
|
|
|
!tor_digest_is_zero(circ->build_state->chosen_exit->identity_digest)) {
|
2008-09-26 20:58:45 +02:00
|
|
|
exit_digest = circ->build_state->chosen_exit->identity_digest;
|
|
|
|
r = router_get_by_digest(exit_digest);
|
|
|
|
}
|
|
|
|
/* Do both the client and relay allow one-hop exit circuits? */
|
|
|
|
if (!r || !r->allow_single_hop_exits ||
|
|
|
|
!get_options()->AllowSingleHopCircuits) {
|
|
|
|
connection_write_str_to_buf(
|
|
|
|
"551 Can't attach stream to this one-hop circuit.\r\n", conn);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ap_conn->chosen_exit_name = tor_strdup(hex_str(exit_digest, DIGEST_LEN));
|
2006-10-01 22:50:11 +02:00
|
|
|
}
|
2008-09-26 20:58:45 +02:00
|
|
|
|
2007-04-30 13:10:45 +02:00
|
|
|
if (circ && hop>0) {
|
|
|
|
/* find this hop in the circuit, and set cpath */
|
|
|
|
cpath = circuit_get_cpath_hop(circ, hop);
|
|
|
|
if (!cpath) {
|
|
|
|
connection_printf_to_buf(conn,
|
|
|
|
"551 Circuit doesn't have %d hops.\r\n", hop);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (connection_ap_handshake_rewrite_and_attach(ap_conn, circ, cpath) < 0) {
|
2007-03-04 22:08:28 +01:00
|
|
|
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;
|
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;
|
2007-10-12 09:57:29 +02:00
|
|
|
int cache = 0; /* eventually, we may switch this to 1 */
|
2005-06-20 11:38:29 +02:00
|
|
|
|
2007-03-17 22:34:37 +01:00
|
|
|
char *cp = memchr(body, '\n', len);
|
|
|
|
smartlist_t *args = smartlist_create();
|
|
|
|
tor_assert(cp);
|
|
|
|
*cp++ = '\0';
|
2007-03-17 22:34:41 +01:00
|
|
|
|
2007-03-17 22:34:37 +01:00
|
|
|
smartlist_split_string(args, body, " ",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
2007-10-12 09:57:29 +02:00
|
|
|
SMARTLIST_FOREACH(args, char *, option,
|
|
|
|
{
|
|
|
|
if (!strcasecmpstart(option, "purpose=")) {
|
|
|
|
option += strlen("purpose=");
|
|
|
|
purpose = router_purpose_from_string(option);
|
|
|
|
if (purpose == ROUTER_PURPOSE_UNKNOWN) {
|
|
|
|
connection_printf_to_buf(conn, "552 Unknown purpose \"%s\"\r\n",
|
|
|
|
option);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
} else if (!strcasecmpstart(option, "cache=")) {
|
|
|
|
option += strlen("cache=");
|
|
|
|
if (!strcmp(option, "no"))
|
|
|
|
cache = 0;
|
|
|
|
else if (!strcmp(option, "yes"))
|
|
|
|
cache = 1;
|
|
|
|
else {
|
|
|
|
connection_printf_to_buf(conn, "552 Unknown cache request \"%s\"\r\n",
|
|
|
|
option);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
} else { /* unrecognized argument? */
|
|
|
|
connection_printf_to_buf(conn,
|
|
|
|
"512 Unexpected argument \"%s\" to postdescriptor\r\n", option);
|
|
|
|
goto done;
|
2006-03-17 23:08:59 +01:00
|
|
|
}
|
2007-10-12 09:57:29 +02:00
|
|
|
});
|
|
|
|
|
2007-08-29 21:02:33 +02:00
|
|
|
read_escaped_data(cp, len-(cp-body), &desc);
|
2005-06-18 04:39:25 +02:00
|
|
|
|
2007-10-12 09:57:29 +02:00
|
|
|
switch (router_load_single_router(desc, purpose, cache, &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";
|
2007-03-04 22:08:28 +01:00
|
|
|
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";
|
2007-03-04 22:08:28 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-03-04 22:08:28 +01:00
|
|
|
tor_free(desc);
|
2007-10-12 09:57:29 +02:00
|
|
|
done:
|
|
|
|
SMARTLIST_FOREACH(args, char *, arg, tor_free(arg));
|
|
|
|
smartlist_free(args);
|
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-06-18 04:39:25 +02:00
|
|
|
char *new_addr = NULL;
|
2005-11-18 12:32:59 +01:00
|
|
|
uint16_t new_port = 0;
|
2007-03-17 22:34:37 +01:00
|
|
|
smartlist_t *args;
|
2007-03-04 22:08:28 +01:00
|
|
|
(void) len;
|
2005-03-19 07:05:55 +01:00
|
|
|
|
2007-03-17 22:34:41 +01:00
|
|
|
args = getargs_helper("REDIRECTSTREAM", conn, body, 2, -1);
|
|
|
|
if (!args)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!(ap_conn = get_stream(smartlist_get(args, 0)))
|
2007-03-17 22:34:37 +01:00
|
|
|
|| !ap_conn->socks_request) {
|
|
|
|
connection_printf_to_buf(conn, "552 Unknown stream \"%s\"\r\n",
|
|
|
|
(char*)smartlist_get(args, 0));
|
|
|
|
} else {
|
2008-01-15 06:57:19 +01:00
|
|
|
int ok = 1;
|
2007-03-17 22:34:37 +01:00
|
|
|
if (smartlist_len(args) > 2) { /* they included a port too */
|
|
|
|
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));
|
2005-06-18 04:39:25 +02:00
|
|
|
} else {
|
2007-03-17 22:34:37 +01:00
|
|
|
new_addr = tor_strdup(smartlist_get(args, 1));
|
2005-06-18 04:39:25 +02:00
|
|
|
}
|
2005-03-19 07:05:55 +01:00
|
|
|
}
|
|
|
|
|
2007-03-17 22:34:37 +01:00
|
|
|
SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(args);
|
|
|
|
if (!new_addr)
|
|
|
|
return 0;
|
|
|
|
|
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;
|
2007-03-17 22:34:37 +01:00
|
|
|
smartlist_t *args;
|
|
|
|
int ok;
|
2007-03-04 22:08:28 +01:00
|
|
|
(void) len;
|
2005-03-22 20:36:38 +01:00
|
|
|
|
2007-03-17 22:34:41 +01:00
|
|
|
args = getargs_helper("CLOSESTREAM", conn, body, 2, -1);
|
|
|
|
if (!args)
|
|
|
|
return 0;
|
|
|
|
|
2007-03-17 22:34:37 +01: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;
|
2005-06-18 04:39:25 +02:00
|
|
|
}
|
2005-03-22 20:36:38 +01:00
|
|
|
}
|
2007-03-17 22:34:37 +01:00
|
|
|
SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(args);
|
|
|
|
if (!ap_conn)
|
|
|
|
return 0;
|
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;
|
2007-03-17 22:34:37 +01:00
|
|
|
smartlist_t *args;
|
2007-03-04 22:08:28 +01:00
|
|
|
(void) len;
|
2005-03-19 07:05:55 +01:00
|
|
|
|
2007-03-17 22:34:41 +01:00
|
|
|
args = getargs_helper("CLOSECIRCUIT", conn, body, 1, -1);
|
|
|
|
if (!args)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!(circ=get_circ(smartlist_get(args, 0))))
|
2007-03-17 22:34:37 +01:00
|
|
|
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
|
|
|
|
log_info(LD_CONTROL, "Skipping unknown option %s",
|
|
|
|
(char*)smartlist_get(args,i));
|
2005-06-18 04:39:25 +02:00
|
|
|
}
|
2005-03-22 20:36:38 +01:00
|
|
|
}
|
2007-03-17 22:34:37 +01: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
|
|
|
|
2007-10-04 18:21:58 +02:00
|
|
|
/** Called when we get a RESOLVE command: start trying to resolve
|
|
|
|
* the listed addresses. */
|
2007-07-10 19:13:24 +02:00
|
|
|
static int
|
|
|
|
handle_control_resolve(control_connection_t *conn, uint32_t len,
|
|
|
|
const char *body)
|
|
|
|
{
|
2008-02-05 22:39:40 +01:00
|
|
|
smartlist_t *args, *failed;
|
2007-07-10 19:14:51 +02:00
|
|
|
int is_reverse = 0;
|
2007-07-10 19:13:24 +02:00
|
|
|
(void) len; /* body is nul-terminated; it's safe to ignore the length */
|
|
|
|
|
2009-09-01 05:23:47 +02:00
|
|
|
if (!(conn->event_mask & ((uint32_t)1L<<EVENT_ADDRMAP))) {
|
2007-07-10 19:13:24 +02:00
|
|
|
log_warn(LD_CONTROL, "Controller asked us to resolve an address, but "
|
|
|
|
"isn't listening for ADDRMAP events. It probably won't see "
|
|
|
|
"the answer.");
|
|
|
|
}
|
|
|
|
args = smartlist_create();
|
|
|
|
smartlist_split_string(args, body, " ",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
2007-07-10 19:14:51 +02:00
|
|
|
if (smartlist_len(args) &&
|
|
|
|
!strcasecmp(smartlist_get(args, 0), "mode=reverse")) {
|
|
|
|
char *cp = smartlist_get(args, 0);
|
|
|
|
smartlist_del_keeporder(args, 0);
|
|
|
|
tor_free(cp);
|
|
|
|
is_reverse = 1;
|
|
|
|
}
|
2008-02-05 22:39:40 +01:00
|
|
|
failed = smartlist_create();
|
2007-07-10 19:13:24 +02:00
|
|
|
SMARTLIST_FOREACH(args, const char *, arg, {
|
2008-02-05 22:39:40 +01:00
|
|
|
if (dnsserv_launch_request(arg, is_reverse)<0)
|
|
|
|
smartlist_add(failed, (char*)arg);
|
|
|
|
});
|
|
|
|
|
|
|
|
send_control_done(conn);
|
|
|
|
SMARTLIST_FOREACH(failed, const char *, arg, {
|
|
|
|
control_event_address_mapped(arg, arg, time(NULL),
|
|
|
|
"Unable to launch resolve request");
|
2007-07-10 19:13:24 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(args);
|
2008-02-05 22:39:40 +01:00
|
|
|
smartlist_free(failed);
|
2007-07-10 19:13:24 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-04 18:21:58 +02:00
|
|
|
/** Called when we get a PROTOCOLINFO command: send back a reply. */
|
2007-08-16 19:31:23 +02:00
|
|
|
static int
|
|
|
|
handle_control_protocolinfo(control_connection_t *conn, uint32_t len,
|
|
|
|
const char *body)
|
|
|
|
{
|
|
|
|
const char *bad_arg = NULL;
|
|
|
|
smartlist_t *args;
|
|
|
|
(void)len;
|
|
|
|
|
|
|
|
conn->have_sent_protocolinfo = 1;
|
|
|
|
args = smartlist_create();
|
|
|
|
smartlist_split_string(args, body, " ",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
|
|
SMARTLIST_FOREACH(args, const char *, arg, {
|
|
|
|
int ok;
|
|
|
|
tor_parse_long(arg, 10, 0, LONG_MAX, &ok, NULL);
|
|
|
|
if (!ok) {
|
|
|
|
bad_arg = arg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (bad_arg) {
|
|
|
|
connection_printf_to_buf(conn, "513 No such version %s\r\n",
|
|
|
|
escaped(bad_arg));
|
|
|
|
/* Don't tolerate bad arguments when not authenticated. */
|
|
|
|
if (!STATE_IS_OPEN(TO_CONN(conn)->state))
|
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
|
|
|
goto done;
|
|
|
|
} else {
|
|
|
|
or_options_t *options = get_options();
|
|
|
|
int cookies = options->CookieAuthentication;
|
|
|
|
char *cfile = get_cookie_file();
|
|
|
|
char *esc_cfile = esc_for_log(cfile);
|
|
|
|
char *methods;
|
|
|
|
{
|
2008-02-29 02:45:06 +01:00
|
|
|
int passwd = (options->HashedControlPassword != NULL ||
|
|
|
|
options->HashedControlSessionPassword != NULL);
|
2007-08-16 19:31:23 +02:00
|
|
|
smartlist_t *mlist = smartlist_create();
|
|
|
|
if (cookies)
|
|
|
|
smartlist_add(mlist, (char*)"COOKIE");
|
|
|
|
if (passwd)
|
|
|
|
smartlist_add(mlist, (char*)"HASHEDPASSWORD");
|
|
|
|
if (!cookies && !passwd)
|
|
|
|
smartlist_add(mlist, (char*)"NULL");
|
|
|
|
methods = smartlist_join_strings(mlist, ",", 0, NULL);
|
|
|
|
smartlist_free(mlist);
|
|
|
|
}
|
|
|
|
|
|
|
|
connection_printf_to_buf(conn,
|
2007-08-22 09:57:10 +02:00
|
|
|
"250-PROTOCOLINFO 1\r\n"
|
2007-08-16 19:31:23 +02:00
|
|
|
"250-AUTH METHODS=%s%s%s\r\n"
|
|
|
|
"250-VERSION Tor=%s\r\n"
|
|
|
|
"250 OK\r\n",
|
|
|
|
methods,
|
|
|
|
cookies?" COOKIEFILE=":"",
|
|
|
|
cookies?esc_cfile:"",
|
|
|
|
escaped(VERSION));
|
2007-10-11 05:27:47 +02:00
|
|
|
tor_free(methods);
|
2007-08-16 19:31:23 +02:00
|
|
|
tor_free(cfile);
|
|
|
|
tor_free(esc_cfile);
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(args);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Called when we get a USEFEATURE command: parse the feature list, and
|
|
|
|
* set up the control_connection's options properly. */
|
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 bad = 0;
|
2006-10-03 21:00:18 +02:00
|
|
|
(void) len; /* body is nul-terminated; it's safe to ignore the length */
|
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"))
|
2009-05-08 18:12:20 +02:00
|
|
|
;
|
2008-12-10 00:10:38 +01:00
|
|
|
else if (!strcasecmp(arg, "EXTENDED_EVENTS"))
|
2009-05-08 18:12:20 +02:00
|
|
|
;
|
2008-12-09 20:55:19 +01:00
|
|
|
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) {
|
|
|
|
send_control_done(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(args);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-08-16 19:31:23 +02:00
|
|
|
/** Return true iff <b>cmd</b> is allowable (or at least forgivable) at this
|
|
|
|
* stage of the protocol. */
|
|
|
|
static int
|
|
|
|
is_valid_initial_command(control_connection_t *conn, const char *cmd)
|
|
|
|
{
|
|
|
|
if (conn->_base.state == CONTROL_CONN_STATE_OPEN)
|
|
|
|
return 1;
|
|
|
|
if (!strcasecmp(cmd, "PROTOCOLINFO"))
|
|
|
|
return !conn->have_sent_protocolinfo;
|
|
|
|
if (!strcasecmp(cmd, "AUTHENTICATE") ||
|
|
|
|
!strcasecmp(cmd, "QUIT"))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-05 22:39:49 +01:00
|
|
|
/** Do not accept any control command of more than 1MB in length. Anything
|
|
|
|
* that needs to be anywhere near this long probably means that one of our
|
|
|
|
* interfaces is broken. */
|
|
|
|
#define MAX_COMMAND_LINE_LENGTH (1024*1024)
|
|
|
|
|
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.
|
|
|
|
*/
|
2007-03-04 22:08:28 +01:00
|
|
|
int
|
|
|
|
connection_control_process_inbuf(control_connection_t *conn)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2005-08-12 19:24:53 +02:00
|
|
|
size_t data_len;
|
2008-02-22 20:09:45 +01:00
|
|
|
uint32_t cmd_data_len;
|
2005-08-12 19:24:53 +02:00
|
|
|
int cmd_len;
|
2005-06-17 22:37:21 +02:00
|
|
|
char *args;
|
2004-11-03 02:32:26 +01:00
|
|
|
|
|
|
|
tor_assert(conn);
|
2007-03-04 22:08:28 +01:00
|
|
|
tor_assert(conn->_base.state == CONTROL_CONN_STATE_OPEN ||
|
|
|
|
conn->_base.state == CONTROL_CONN_STATE_NEEDAUTH);
|
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;
|
|
|
|
}
|
|
|
|
|
2007-03-04 22:08:28 +01:00
|
|
|
if (conn->_base.state == CONTROL_CONN_STATE_NEEDAUTH &&
|
|
|
|
peek_buf_has_control0_command(conn->_base.inbuf)) {
|
|
|
|
/* Detect v0 commands and send a "no more v0" message. */
|
|
|
|
size_t body_len;
|
|
|
|
char buf[128];
|
|
|
|
set_uint16(buf+2, htons(0x0000)); /* type == error */
|
|
|
|
set_uint16(buf+4, htons(0x0001)); /* code == internal error */
|
2007-08-29 21:02:33 +02:00
|
|
|
strlcpy(buf+6, "The v0 control protocol is not supported by Tor 0.1.2.17 "
|
|
|
|
"and later; upgrade your controller.",
|
2007-05-22 17:48:46 +02:00
|
|
|
sizeof(buf)-6);
|
2007-03-04 22:08:28 +01:00
|
|
|
body_len = 2+strlen(buf+6)+2; /* code, msg, nul. */
|
|
|
|
set_uint16(buf+0, htons(body_len));
|
|
|
|
connection_write_to_buf(buf, 4+body_len, TO_CONN(conn));
|
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
|
|
|
conn->_base.hold_open_until_flushed = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-17 20:49:55 +02:00
|
|
|
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) {
|
2008-02-05 22:39:49 +01:00
|
|
|
if (data_len + conn->incoming_cmd_cur_len > MAX_COMMAND_LINE_LENGTH) {
|
2008-02-05 22:39:51 +01:00
|
|
|
connection_write_str_to_buf("500 Line too long.\r\n", conn);
|
2008-02-05 22:39:49 +01:00
|
|
|
connection_stop_reading(TO_CONN(conn));
|
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
|
|
|
conn->_base.hold_open_until_flushed = 1;
|
|
|
|
}
|
2007-12-29 03:33:42 +01:00
|
|
|
while (conn->incoming_cmd_len < data_len+conn->incoming_cmd_cur_len)
|
|
|
|
conn->incoming_cmd_len *= 2;
|
|
|
|
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;
|
2008-02-22 20:09:45 +01:00
|
|
|
conn->incoming_cmd_cur_len += (int)data_len;
|
2005-06-17 20:49:55 +02:00
|
|
|
|
|
|
|
/* 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;
|
2007-08-29 21:02:33 +02:00
|
|
|
/* XXXX this code duplication is kind of dumb. */
|
2005-06-17 20:49:55 +02:00
|
|
|
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. */
|
2008-03-10 13:13:43 +01:00
|
|
|
conn->incoming_cmd[last_idx] = '\0';
|
2005-07-15 21:31:11 +02:00
|
|
|
conn->incoming_cmd_cur_len -= 3;
|
2005-06-17 20:49:55 +02:00
|
|
|
break;
|
2007-08-29 21:02:33 +02:00
|
|
|
} else if (last_idx+2 == conn->incoming_cmd_cur_len &&
|
|
|
|
!memcmp(conn->incoming_cmd + last_idx, ".\n", 2)) {
|
|
|
|
/* Just appended ".\n"; we're done. Remove it. */
|
2008-03-10 13:13:43 +01:00
|
|
|
conn->incoming_cmd[last_idx] = '\0';
|
2007-08-29 21:02:33 +02:00
|
|
|
conn->incoming_cmd_cur_len -= 2;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-06-24 05:14:44 +02:00
|
|
|
/* If the connection is already closing, ignore further commands */
|
|
|
|
if (TO_CONN(conn)->marked_for_close) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, Quit is always valid. */
|
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));
|
2009-06-24 05:09:27 +02:00
|
|
|
conn->_base.hold_open_until_flushed = 1;
|
2005-08-13 03:55:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-04 22:08:28 +01:00
|
|
|
if (conn->_base.state == CONTROL_CONN_STATE_NEEDAUTH &&
|
2007-08-16 19:31:23 +02:00
|
|
|
!is_valid_initial_command(conn, conn->incoming_cmd)) {
|
2005-06-17 20:49:55 +02:00
|
|
|
connection_write_str_to_buf("514 Authentication required.\r\n", conn);
|
2007-08-02 03:28:40 +02:00
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
2007-08-02 06:28:29 +02:00
|
|
|
return 0;
|
2005-06-17 20:49:55 +02:00
|
|
|
}
|
|
|
|
|
2008-02-22 20:09:45 +01:00
|
|
|
if (data_len >= UINT32_MAX) {
|
|
|
|
connection_write_str_to_buf("500 A 4GB command? Nice try.\r\n", conn);
|
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd_data_len = (uint32_t)data_len;
|
2005-06-17 22:37:21 +02:00
|
|
|
if (!strcasecmp(conn->incoming_cmd, "SETCONF")) {
|
2008-02-22 20:09:45 +01:00
|
|
|
if (handle_control_setconf(conn, cmd_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")) {
|
2008-02-22 20:09:45 +01:00
|
|
|
if (handle_control_resetconf(conn, cmd_data_len, args))
|
2005-09-08 05:18:51 +02:00
|
|
|
return -1;
|
2005-06-17 22:37:21 +02:00
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "GETCONF")) {
|
2008-02-22 20:09:45 +01:00
|
|
|
if (handle_control_getconf(conn, cmd_data_len, args))
|
2005-06-17 20:49:55 +02:00
|
|
|
return -1;
|
2008-03-10 13:41:52 +01:00
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "+LOADCONF")) {
|
|
|
|
if (handle_control_loadconf(conn, cmd_data_len, args))
|
|
|
|
return -1;
|
2005-06-17 22:37:21 +02:00
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "SETEVENTS")) {
|
2008-02-22 20:09:45 +01:00
|
|
|
if (handle_control_setevents(conn, cmd_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")) {
|
2008-02-22 20:09:45 +01:00
|
|
|
if (handle_control_authenticate(conn, cmd_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")) {
|
2008-02-22 20:09:45 +01:00
|
|
|
if (handle_control_saveconf(conn, cmd_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")) {
|
2008-02-22 20:09:45 +01:00
|
|
|
if (handle_control_signal(conn, cmd_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")) {
|
2008-02-22 20:09:45 +01:00
|
|
|
if (handle_control_mapaddress(conn, cmd_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")) {
|
2008-02-22 20:09:45 +01:00
|
|
|
if (handle_control_getinfo(conn, cmd_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")) {
|
2008-02-22 20:09:45 +01:00
|
|
|
if (handle_control_extendcircuit(conn, cmd_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")) {
|
2008-02-22 20:09:45 +01:00
|
|
|
if (handle_control_setcircuitpurpose(conn, cmd_data_len, args))
|
2006-03-17 23:08:59 +01:00
|
|
|
return -1;
|
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "SETROUTERPURPOSE")) {
|
2007-10-12 00:19:47 +02:00
|
|
|
connection_write_str_to_buf("511 SETROUTERPURPOSE is obsolete.\r\n", conn);
|
2005-06-17 22:37:21 +02:00
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "ATTACHSTREAM")) {
|
2008-02-22 20:09:45 +01:00
|
|
|
if (handle_control_attachstream(conn, cmd_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")) {
|
2008-02-22 20:09:45 +01:00
|
|
|
if (handle_control_postdescriptor(conn, cmd_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")) {
|
2008-02-22 20:09:45 +01:00
|
|
|
if (handle_control_redirectstream(conn, cmd_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")) {
|
2008-02-22 20:09:45 +01:00
|
|
|
if (handle_control_closestream(conn, cmd_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")) {
|
2008-02-22 20:09:45 +01:00
|
|
|
if (handle_control_closecircuit(conn, cmd_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")) {
|
2008-02-22 20:09:45 +01:00
|
|
|
if (handle_control_usefeature(conn, cmd_data_len, args))
|
2006-10-03 20:58:52 +02:00
|
|
|
return -1;
|
2007-07-10 19:13:24 +02:00
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "RESOLVE")) {
|
2008-02-22 20:09:45 +01:00
|
|
|
if (handle_control_resolve(conn, cmd_data_len, args))
|
2007-07-10 19:13:24 +02:00
|
|
|
return -1;
|
2007-08-16 19:31:23 +02:00
|
|
|
} else if (!strcasecmp(conn->incoming_cmd, "PROTOCOLINFO")) {
|
2008-02-22 20:09:45 +01:00
|
|
|
if (handle_control_protocolinfo(conn, cmd_data_len, args))
|
2007-08-16 19:31:23 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2007-03-17 22:34:37 +01:00
|
|
|
const char *status;
|
2008-09-30 00:34:22 +02:00
|
|
|
char extended_buf[96];
|
2007-03-17 22:34:37 +01:00
|
|
|
int providing_reason=0;
|
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);
|
|
|
|
|
2007-03-17 22:34:37 +01: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:
|
|
|
|
log_warn(LD_BUG, "Unrecognized status code %d", (int)tp);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-10-09 17:47:27 +02:00
|
|
|
|
2008-09-30 00:34:22 +02:00
|
|
|
tor_snprintf(extended_buf, sizeof(extended_buf), "PURPOSE=%s",
|
|
|
|
circuit_purpose_to_controller_string(circ->_base.purpose));
|
|
|
|
|
2007-03-17 22:34:37 +01:00
|
|
|
if (tp == CIRC_EVENT_FAILED || tp == CIRC_EVENT_CLOSED) {
|
2008-06-11 02:17:02 +02:00
|
|
|
const char *reason_str = circuit_end_reason_to_control_string(reason_code);
|
2007-03-17 22:34:37 +01:00
|
|
|
char *reason = NULL;
|
2008-09-30 00:34:22 +02:00
|
|
|
size_t n=strlen(extended_buf);
|
2007-03-17 22:34:37 +01:00
|
|
|
providing_reason=1;
|
|
|
|
if (!reason_str) {
|
|
|
|
reason = tor_malloc(16);
|
|
|
|
tor_snprintf(reason, 16, "UNKNOWN_%d", reason_code);
|
|
|
|
reason_str = reason;
|
2006-10-09 17:47:27 +02:00
|
|
|
}
|
2007-03-17 22:34:37 +01:00
|
|
|
if (reason_code > 0 && reason_code & END_CIRC_REASON_FLAG_REMOTE) {
|
2008-09-30 00:34:22 +02:00
|
|
|
tor_snprintf(extended_buf+n, sizeof(extended_buf)-n,
|
|
|
|
" REASON=DESTROYED REMOTE_REASON=%s", reason_str);
|
2007-03-17 22:34:37 +01:00
|
|
|
} else {
|
2008-09-30 00:34:22 +02:00
|
|
|
tor_snprintf(extended_buf+n, sizeof(extended_buf)-n,
|
|
|
|
" REASON=%s", reason_str);
|
2007-03-17 22:34:37 +01:00
|
|
|
}
|
|
|
|
tor_free(reason);
|
|
|
|
}
|
2006-10-09 17:47:27 +02:00
|
|
|
|
2009-05-08 18:12:20 +02:00
|
|
|
{
|
2007-03-17 22:34:37 +01:00
|
|
|
char *vpath = circuit_list_path_for_controller(circ);
|
|
|
|
const char *sp = strlen(vpath) ? " " : "";
|
2009-05-08 18:12:20 +02:00
|
|
|
send_control_event(EVENT_CIRCUIT_STATUS, ALL_FORMATS,
|
|
|
|
"650 CIRC %lu %s%s%s %s\r\n",
|
2008-09-30 00:34:22 +02:00
|
|
|
(unsigned long)circ->global_identifier,
|
|
|
|
status, sp, vpath, extended_buf);
|
2007-03-17 22:34:37 +01:00
|
|
|
tor_free(vpath);
|
2005-06-17 22:37:21 +02:00
|
|
|
}
|
2007-03-17 22:34:37 +01: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;
|
2008-09-05 23:19:53 +02:00
|
|
|
if (!conn->socks_request)
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2007-03-17 22:34:37 +01:00
|
|
|
char reason_buf[64];
|
2007-06-17 17:10:51 +02:00
|
|
|
char addrport_buf[64];
|
2007-03-17 22:34:37 +01:00
|
|
|
const char *status;
|
|
|
|
circuit_t *circ;
|
|
|
|
origin_circuit_t *origin_circ = NULL;
|
2005-06-19 22:40:41 +02:00
|
|
|
char buf[256];
|
2008-05-27 23:02:36 +02:00
|
|
|
const char *purpose = "";
|
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;
|
|
|
|
|
2007-02-07 07:54:22 +01:00
|
|
|
if (tp == STREAM_EVENT_CLOSED &&
|
|
|
|
(reason_code & END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED))
|
|
|
|
return 0;
|
|
|
|
|
2005-06-19 22:40:41 +02:00
|
|
|
write_stream_target_to_buf(conn, buf, sizeof(buf));
|
2007-03-04 22:08:28 +01:00
|
|
|
|
2007-03-17 22:34:37 +01:00
|
|
|
reason_buf[0] = '\0';
|
|
|
|
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;
|
|
|
|
case STREAM_EVENT_REMAP: status = "REMAP"; break;
|
|
|
|
default:
|
|
|
|
log_warn(LD_BUG, "Unrecognized status code %d", (int)tp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (reason_code && (tp == STREAM_EVENT_FAILED ||
|
|
|
|
tp == STREAM_EVENT_CLOSED ||
|
|
|
|
tp == STREAM_EVENT_FAILED_RETRIABLE)) {
|
2008-12-10 02:34:24 +01:00
|
|
|
const char *reason_str = stream_end_reason_to_control_string(reason_code);
|
2007-03-17 22:34:37 +01:00
|
|
|
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);
|
|
|
|
} else if (reason_code && tp == STREAM_EVENT_REMAP) {
|
|
|
|
switch (reason_code) {
|
|
|
|
case REMAP_STREAM_SOURCE_CACHE:
|
|
|
|
strlcpy(reason_buf, "SOURCE=CACHE", sizeof(reason_buf));
|
|
|
|
break;
|
|
|
|
case REMAP_STREAM_SOURCE_EXIT:
|
|
|
|
strlcpy(reason_buf, "SOURCE=EXIT", sizeof(reason_buf));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
tor_snprintf(reason_buf, sizeof(reason_buf), "REASON=UNKNOWN_%d",
|
|
|
|
reason_code);
|
2008-12-10 02:35:21 +01:00
|
|
|
/* XXX do we want SOURCE=UNKNOWN_%d above instead? -RD */
|
2007-03-17 22:34:37 +01:00
|
|
|
break;
|
2006-10-20 19:54:36 +02:00
|
|
|
}
|
2007-03-17 22:34:37 +01:00
|
|
|
}
|
2007-06-17 17:10:51 +02:00
|
|
|
|
|
|
|
if (tp == STREAM_EVENT_NEW) {
|
|
|
|
tor_snprintf(addrport_buf,sizeof(addrport_buf), "%sSOURCE_ADDR=%s:%d",
|
|
|
|
strlen(reason_buf) ? " " : "",
|
|
|
|
TO_CONN(conn)->address, TO_CONN(conn)->port );
|
|
|
|
} else {
|
|
|
|
addrport_buf[0] = '\0';
|
|
|
|
}
|
|
|
|
|
2008-05-27 23:02:36 +02:00
|
|
|
if (tp == STREAM_EVENT_NEW_RESOLVE) {
|
|
|
|
purpose = " PURPOSE=DNS_REQUEST";
|
|
|
|
} else if (tp == STREAM_EVENT_NEW) {
|
|
|
|
if (conn->is_dns_request ||
|
|
|
|
(conn->socks_request &&
|
|
|
|
SOCKS_COMMAND_IS_RESOLVE(conn->socks_request->command)))
|
|
|
|
purpose = " PURPOSE=DNS_REQUEST";
|
|
|
|
else if (conn->use_begindir) {
|
|
|
|
connection_t *linked = TO_CONN(conn)->linked_conn;
|
|
|
|
int linked_dir_purpose = -1;
|
|
|
|
if (linked && linked->type == CONN_TYPE_DIR)
|
|
|
|
linked_dir_purpose = linked->purpose;
|
|
|
|
if (DIR_PURPOSE_IS_UPLOAD(linked_dir_purpose))
|
|
|
|
purpose = " PURPOSE=DIR_UPLOAD";
|
|
|
|
else
|
|
|
|
purpose = " PURPOSE=DIR_FETCH";
|
|
|
|
} else
|
|
|
|
purpose = " PURPOSE=USER";
|
|
|
|
}
|
|
|
|
|
2007-03-17 22:34:37 +01:00
|
|
|
circ = circuit_get_by_edge_conn(conn);
|
|
|
|
if (circ && CIRCUIT_IS_ORIGIN(circ))
|
|
|
|
origin_circ = TO_ORIGIN_CIRCUIT(circ);
|
2009-05-08 18:12:20 +02:00
|
|
|
send_control_event(EVENT_STREAM_STATUS, ALL_FORMATS,
|
|
|
|
"650 STREAM "U64_FORMAT" %s %lu %s %s%s%s\r\n",
|
2008-08-15 15:55:01 +02:00
|
|
|
U64_PRINTF_ARG(conn->_base.global_identifier), status,
|
2006-07-28 17:11:11 +02:00
|
|
|
origin_circ?
|
2006-08-10 11:02:12 +02:00
|
|
|
(unsigned long)origin_circ->global_identifier : 0ul,
|
2008-05-27 23:02:36 +02:00
|
|
|
buf, reason_buf, addrport_buf, purpose);
|
2007-06-17 17:10:51 +02:00
|
|
|
|
2007-03-17 22:34:37 +01:00
|
|
|
/* XXX need to specify its intended exit, etc? */
|
|
|
|
|
2004-11-03 02:32:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-24 08:50:38 +01:00
|
|
|
/** Figure out the best name for the target router of an OR connection
|
|
|
|
* <b>conn</b>, and write it into the <b>len</b>-character buffer
|
2009-05-08 18:12:20 +02:00
|
|
|
* <b>name</b>. */
|
2006-10-03 21:00:18 +02:00
|
|
|
static void
|
2009-05-08 18:12:20 +02:00
|
|
|
orconn_target_get_name(char *name, size_t len, or_connection_t *conn)
|
|
|
|
{
|
|
|
|
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-03 21:00:18 +02:00
|
|
|
} else {
|
2009-05-08 18:12:20 +02:00
|
|
|
tor_snprintf(name, len, "%s:%d",
|
|
|
|
conn->_base.address, conn->_base.port);
|
2006-10-03 21:00:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Called when the status of an OR connection <b>conn</b> changes: tell any
|
|
|
|
* interested control connections. <b>tp</b> is the new status for the
|
|
|
|
* connection. If <b>conn</b> has just closed or failed, then <b>reason</b>
|
|
|
|
* may be the reason why.
|
|
|
|
*/
|
2004-11-07 22:37:50 +01:00
|
|
|
int
|
2007-02-16 21:01:02 +01:00
|
|
|
control_event_or_conn_status(or_connection_t *conn, or_conn_status_event_t tp,
|
2007-02-24 08:50:38 +01:00
|
|
|
int reason)
|
2004-11-03 02:32:26 +01:00
|
|
|
{
|
2007-01-15 22:13:37 +01:00
|
|
|
int ncircs = 0;
|
2007-03-17 22:34:37 +01:00
|
|
|
const char *status;
|
|
|
|
char name[128];
|
|
|
|
char ncircs_buf[32] = {0}; /* > 8 + log10(2^32)=10 + 2 */
|
2004-11-03 19:33:07 +01:00
|
|
|
|
2004-11-03 02:32:26 +01:00
|
|
|
if (!EVENT_IS_INTERESTING(EVENT_OR_CONN_STATUS))
|
|
|
|
return 0;
|
|
|
|
|
2007-03-17 22:34:37 +01: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;
|
|
|
|
case OR_CONN_EVENT_NEW: status = "NEW"; break;
|
|
|
|
default:
|
|
|
|
log_warn(LD_BUG, "Unrecognized status code %d", (int)tp);
|
|
|
|
return 0;
|
2007-01-15 22:13:37 +01:00
|
|
|
}
|
2007-03-17 22:34:37 +01:00
|
|
|
ncircs = circuit_count_pending_on_or_conn(conn);
|
|
|
|
ncircs += conn->n_circuits;
|
|
|
|
if (ncircs && (tp == OR_CONN_EVENT_FAILED || tp == OR_CONN_EVENT_CLOSED)) {
|
|
|
|
tor_snprintf(ncircs_buf, sizeof(ncircs_buf), "%sNCIRCS=%d",
|
|
|
|
reason ? " " : "", ncircs);
|
|
|
|
}
|
2007-01-15 22:13:37 +01:00
|
|
|
|
2009-05-08 18:12:20 +02:00
|
|
|
orconn_target_get_name(name, sizeof(name), conn);
|
|
|
|
send_control_event(EVENT_OR_CONN_STATUS, ALL_FORMATS,
|
|
|
|
"650 ORCONN %s %s %s%s%s\r\n",
|
|
|
|
name, status,
|
|
|
|
reason ? "REASON=" : "",
|
|
|
|
orconn_end_reason_to_control_string(reason),
|
|
|
|
ncircs_buf);
|
2007-03-17 22:34:37 +01:00
|
|
|
|
2004-11-03 02:32:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-14 14:20:27 +02:00
|
|
|
/**
|
|
|
|
* Print out STREAM_BW event for a single conn
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
control_event_stream_bandwidth(edge_connection_t *edge_conn)
|
|
|
|
{
|
|
|
|
if (EVENT_IS_INTERESTING(EVENT_STREAM_BANDWIDTH_USED)) {
|
|
|
|
if (!edge_conn->n_read && !edge_conn->n_written)
|
|
|
|
return 0;
|
|
|
|
|
2009-05-25 18:55:59 +02:00
|
|
|
send_control_event(EVENT_STREAM_BANDWIDTH_USED, ALL_FORMATS,
|
2009-05-14 14:20:27 +02:00
|
|
|
"650 STREAM_BW "U64_FORMAT" %lu %lu\r\n",
|
|
|
|
U64_PRINTF_ARG(edge_conn->_base.global_identifier),
|
|
|
|
(unsigned long)edge_conn->n_read,
|
|
|
|
(unsigned long)edge_conn->n_written);
|
|
|
|
|
|
|
|
edge_conn->n_written = edge_conn->n_read = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-14 17:46:49 +01:00
|
|
|
/** A second or more has elapsed: tell any interested control
|
|
|
|
* connections how much bandwidth streams have used. */
|
|
|
|
int
|
2007-02-14 17:46:55 +01:00
|
|
|
control_event_stream_bandwidth_used(void)
|
2007-02-14 17:46:49 +01:00
|
|
|
{
|
2007-03-04 22:08:28 +01:00
|
|
|
if (EVENT_IS_INTERESTING(EVENT_STREAM_BANDWIDTH_USED)) {
|
2007-05-22 17:49:14 +02:00
|
|
|
smartlist_t *conns = get_connection_array();
|
|
|
|
edge_connection_t *edge_conn;
|
2007-02-14 17:46:49 +01:00
|
|
|
|
2008-08-15 15:55:01 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn)
|
2007-05-22 17:49:14 +02:00
|
|
|
{
|
|
|
|
if (conn->type != CONN_TYPE_AP)
|
2007-02-14 17:46:49 +01:00
|
|
|
continue;
|
2007-05-22 17:49:14 +02:00
|
|
|
edge_conn = TO_EDGE_CONN(conn);
|
|
|
|
if (!edge_conn->n_read && !edge_conn->n_written)
|
2007-02-14 17:46:49 +01:00
|
|
|
continue;
|
|
|
|
|
2009-05-08 18:12:20 +02:00
|
|
|
send_control_event(EVENT_STREAM_BANDWIDTH_USED, ALL_FORMATS,
|
2008-12-27 07:50:07 +01:00
|
|
|
"650 STREAM_BW "U64_FORMAT" %lu %lu\r\n",
|
|
|
|
U64_PRINTF_ARG(edge_conn->_base.global_identifier),
|
|
|
|
(unsigned long)edge_conn->n_read,
|
|
|
|
(unsigned long)edge_conn->n_written);
|
2007-02-14 17:46:49 +01:00
|
|
|
|
2007-05-22 17:49:14 +02:00
|
|
|
edge_conn->n_written = edge_conn->n_read = 0;
|
2008-08-15 15:55:01 +02:00
|
|
|
}
|
|
|
|
SMARTLIST_FOREACH_END(conn);
|
2007-02-14 17:46:49 +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
|
|
|
{
|
2007-03-04 22:08:28 +01:00
|
|
|
if (EVENT_IS_INTERESTING(EVENT_BANDWIDTH_USED)) {
|
2009-05-08 18:12:20 +02:00
|
|
|
send_control_event(EVENT_BANDWIDTH_USED, ALL_FORMATS,
|
2008-12-27 07:50:07 +01:00
|
|
|
"650 BW %lu %lu\r\n",
|
|
|
|
(unsigned long)n_read,
|
|
|
|
(unsigned long)n_written);
|
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
|
|
|
/** 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
|
2007-01-06 06:42:31 +01:00
|
|
|
control_event_logmsg(int severity, uint32_t domain, const char *msg)
|
2004-11-03 02:32:26 +01:00
|
|
|
{
|
2007-03-04 22:08:28 +01:00
|
|
|
int event;
|
2005-04-18 00:52:02 +02:00
|
|
|
|
2009-05-29 16:18:50 +02:00
|
|
|
/* Don't even think of trying to add stuff to a buffer from a cpuworker
|
|
|
|
* thread. */
|
|
|
|
if (! in_main_thread())
|
|
|
|
return;
|
|
|
|
|
2005-07-13 07:14:42 +02:00
|
|
|
if (disable_log_messages)
|
2005-04-18 00:52:02 +02:00
|
|
|
return;
|
|
|
|
|
2007-01-06 06:42:31 +01:00
|
|
|
if (domain == LD_BUG && EVENT_IS_INTERESTING(EVENT_STATUS_GENERAL) &&
|
|
|
|
severity <= LOG_NOTICE) {
|
|
|
|
char *esc = esc_for_log(msg);
|
|
|
|
++disable_log_messages;
|
|
|
|
control_event_general_status(severity, "BUG REASON=\"%s\"", esc);
|
|
|
|
--disable_log_messages;
|
|
|
|
tor_free(esc);
|
|
|
|
}
|
|
|
|
|
2005-04-18 00:52:02 +02:00
|
|
|
event = log_severity_to_event(severity);
|
2007-03-04 22:08:28 +01:00
|
|
|
if (event >= 0 && EVENT_IS_INTERESTING(event)) {
|
2005-06-18 04:39:25 +02:00
|
|
|
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;
|
2009-05-08 18:12:20 +02:00
|
|
|
send_control_event(event, ALL_FORMATS, "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
|
2007-07-16 02:11:03 +02:00
|
|
|
* routerinfo_t's.
|
2005-03-17 13:38:37 +01:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
control_event_descriptors_changed(smartlist_t *routers)
|
2005-03-02 23:29:58 +01:00
|
|
|
{
|
|
|
|
char *msg;
|
|
|
|
|
|
|
|
if (!EVENT_IS_INTERESTING(EVENT_NEW_DESC))
|
|
|
|
return 0;
|
2009-05-08 18:12:20 +02:00
|
|
|
|
|
|
|
{
|
2006-10-03 21:00:18 +02:00
|
|
|
smartlist_t *names = smartlist_create();
|
|
|
|
char *ids;
|
2007-05-17 00:15:14 +02:00
|
|
|
size_t names_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);
|
|
|
|
});
|
2007-05-17 00:15:14 +02:00
|
|
|
ids = smartlist_join_strings(names, " ", 0, &names_len);
|
|
|
|
names_len = strlen(ids)+32;
|
|
|
|
msg = tor_malloc(names_len);
|
|
|
|
tor_snprintf(msg, names_len, "650 NEWDESC %s\r\n", ids);
|
2009-05-08 18:12:20 +02:00
|
|
|
send_control_event_string(EVENT_NEW_DESC, 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);
|
|
|
|
}
|
2005-03-02 23:29:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-22 15:56:16 +01:00
|
|
|
/** Called when an address mapping on <b>from</b> from changes to <b>to</b>.
|
2007-08-15 21:56:01 +02:00
|
|
|
* <b>expires</b> values less than 3 are special; see connection_edge.c. If
|
2007-08-19 06:58:55 +02:00
|
|
|
* <b>error</b> is non-NULL, it is an error code describing the failure
|
2007-08-15 21:56:01 +02:00
|
|
|
* mode of the mapping.
|
|
|
|
*/
|
2005-06-19 22:40:41 +02:00
|
|
|
int
|
2007-07-10 19:14:51 +02:00
|
|
|
control_event_address_mapped(const char *from, const char *to, time_t expires,
|
|
|
|
const char *error)
|
2005-06-19 22:40:41 +02:00
|
|
|
{
|
2007-03-04 22:08:28 +01:00
|
|
|
if (!EVENT_IS_INTERESTING(EVENT_ADDRMAP))
|
2005-06-19 22:40:41 +02:00
|
|
|
return 0;
|
|
|
|
|
2007-07-10 19:14:51 +02:00
|
|
|
if (expires < 3 || expires == TIME_MAX)
|
2009-05-08 18:12:20 +02:00
|
|
|
send_control_event(EVENT_ADDRMAP, ALL_FORMATS,
|
|
|
|
"650 ADDRMAP %s %s NEVER %s\r\n", from, to,
|
2007-08-19 06:58:55 +02:00
|
|
|
error?error:"");
|
2005-06-19 22:40:41 +02:00
|
|
|
else {
|
|
|
|
char buf[ISO_TIME_LEN+1];
|
2007-07-26 00:56:54 +02:00
|
|
|
char buf2[ISO_TIME_LEN+1];
|
2005-06-19 22:40:41 +02:00
|
|
|
format_local_iso_time(buf,expires);
|
2007-07-26 00:56:54 +02:00
|
|
|
format_iso_time(buf2,expires);
|
2009-05-08 18:12:20 +02:00
|
|
|
send_control_event(EVENT_ADDRMAP, ALL_FORMATS,
|
2007-07-26 00:56:54 +02:00
|
|
|
"650 ADDRMAP %s %s \"%s\""
|
2009-05-08 18:12:20 +02:00
|
|
|
" %s%sEXPIRES=\"%s\"\r\n",
|
2007-07-26 00:56:54 +02:00
|
|
|
from, to, buf,
|
2007-08-19 06:58:55 +02:00
|
|
|
error?error:"", error?" ":"",
|
2007-07-26 00:56:54 +02:00
|
|
|
buf2);
|
2005-06-19 22:40:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2007-05-31 20:48:28 +02:00
|
|
|
const char *desc, size_t desclen,
|
2005-12-14 21:40:40 +01:00
|
|
|
const char *msg)
|
2005-11-19 19:35:43 +01:00
|
|
|
{
|
|
|
|
char firstline[1024];
|
|
|
|
char *buf;
|
2008-02-22 20:09:45 +01:00
|
|
|
size_t totallen;
|
2005-11-19 19:35:43 +01:00
|
|
|
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 */
|
2007-08-29 21:02:33 +02:00
|
|
|
esclen = write_escaped_data(desc, desclen, &esc);
|
2005-11-19 19:35:43 +01:00
|
|
|
|
|
|
|
totallen = strlen(firstline) + esclen + 1;
|
|
|
|
buf = tor_malloc(totallen);
|
|
|
|
strlcpy(buf, firstline, totallen);
|
|
|
|
strlcpy(buf+strlen(firstline), esc, totallen);
|
2009-05-08 18:12:20 +02:00
|
|
|
send_control_event_string(EVENT_AUTHDIR_NEWDESCS, ALL_FORMATS,
|
2007-07-29 06:03:25 +02:00
|
|
|
buf);
|
2009-05-08 18:12:20 +02:00
|
|
|
send_control_event_string(EVENT_AUTHDIR_NEWDESCS, ALL_FORMATS,
|
2007-07-29 06:03:25 +02:00
|
|
|
"650 OK\r\n");
|
2005-11-19 19:35:43 +01:00
|
|
|
tor_free(esc);
|
|
|
|
tor_free(buf);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-16 11:39:10 +01:00
|
|
|
/** Helper function for NS-style events. Constructs and sends an event
|
|
|
|
* of type <b>event</b> with string <b>event_string</b> out of the set of
|
|
|
|
* networkstatuses <b>statuses</b>. Currently it is used for NS events
|
|
|
|
* and NEWCONSENSUS events. */
|
2009-02-16 07:18:03 +01:00
|
|
|
static int
|
|
|
|
control_event_networkstatus_changed_helper(smartlist_t *statuses,
|
|
|
|
uint16_t event,
|
|
|
|
const char *event_string)
|
2006-10-20 23:04:39 +02:00
|
|
|
{
|
|
|
|
smartlist_t *strs;
|
2007-07-26 00:57:02 +02:00
|
|
|
char *s, *esc = NULL;
|
2009-02-16 07:18:03 +01:00
|
|
|
if (!EVENT_IS_INTERESTING(event) || !smartlist_len(statuses))
|
2006-10-20 23:04:39 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
strs = smartlist_create();
|
2009-02-16 07:18:03 +01:00
|
|
|
smartlist_add(strs, tor_strdup("650+"));
|
|
|
|
smartlist_add(strs, tor_strdup(event_string));
|
|
|
|
smartlist_add(strs, tor_strdup("\r\n"));
|
2007-10-09 17:27:15 +02:00
|
|
|
SMARTLIST_FOREACH(statuses, routerstatus_t *, rs,
|
2006-10-20 23:04:39 +02:00
|
|
|
{
|
2007-10-09 17:27:15 +02:00
|
|
|
s = networkstatus_getinfo_helper_single(rs);
|
2006-10-20 23:04:39 +02:00
|
|
|
if (!s) continue;
|
|
|
|
smartlist_add(strs, s);
|
|
|
|
});
|
|
|
|
|
|
|
|
s = smartlist_join_strings(strs, "", 0, NULL);
|
2007-08-29 21:02:33 +02:00
|
|
|
write_escaped_data(s, strlen(s), &esc);
|
2006-10-20 23:04:39 +02:00
|
|
|
SMARTLIST_FOREACH(strs, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(strs);
|
|
|
|
tor_free(s);
|
2009-05-08 18:12:20 +02:00
|
|
|
send_control_event_string(event, ALL_FORMATS, esc);
|
|
|
|
send_control_event_string(event, ALL_FORMATS,
|
2007-07-29 06:03:25 +02:00
|
|
|
"650 OK\r\n");
|
|
|
|
|
2007-07-26 00:57:02 +02:00
|
|
|
tor_free(esc);
|
2006-10-20 23:04:39 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-16 11:39:10 +01:00
|
|
|
/** Called when the routerstatus_ts <b>statuses</b> have changed: sends
|
|
|
|
* an NS event to any controller that cares. */
|
2009-02-16 07:18:03 +01:00
|
|
|
int
|
|
|
|
control_event_networkstatus_changed(smartlist_t *statuses)
|
|
|
|
{
|
|
|
|
return control_event_networkstatus_changed_helper(statuses, EVENT_NS, "NS");
|
|
|
|
}
|
|
|
|
|
2009-02-16 11:39:10 +01:00
|
|
|
/** Called when we get a new consensus networkstatus. Sends a NEWCONSENSUS
|
|
|
|
* event consisting of an NS-style line for each relay in the consensus. */
|
2009-02-16 07:18:03 +01:00
|
|
|
int
|
|
|
|
control_event_newconsensus(const networkstatus_t *consensus)
|
|
|
|
{
|
2009-02-20 11:25:08 +01:00
|
|
|
if (!control_event_is_interesting(EVENT_NEWCONSENSUS))
|
|
|
|
return 0;
|
2009-02-16 07:18:03 +01:00
|
|
|
return control_event_networkstatus_changed_helper(
|
|
|
|
consensus->routerstatus_list, EVENT_NEWCONSENSUS, "NEWCONSENSUS");
|
|
|
|
}
|
|
|
|
|
2007-02-02 19:58:11 +01:00
|
|
|
/** Called when a single local_routerstatus_t has changed: Sends an NS event
|
2009-05-27 23:55:51 +02:00
|
|
|
* to any controller that cares. */
|
2006-10-20 23:04:39 +02:00
|
|
|
int
|
2007-10-09 17:27:15 +02:00
|
|
|
control_event_networkstatus_changed_single(routerstatus_t *rs)
|
2006-10-20 23:04:39 +02:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2009-05-08 18:12:20 +02:00
|
|
|
send_control_event(EVENT_DESCCHANGED, ALL_FORMATS, "650 DESCCHANGED\r\n");
|
2006-09-29 20:13:25 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-06 03:49:07 +01:00
|
|
|
/** Helper: sends a status event where <b>type</b> is one of
|
2007-02-02 19:58:11 +01:00
|
|
|
* EVENT_STATUS_{GENERAL,CLIENT,SERVER}, where <b>severity</b> is one of
|
2007-02-06 03:49:07 +01:00
|
|
|
* LOG_{NOTICE,WARN,ERR}, and where <b>format</b> is a printf-style format
|
2007-02-02 19:58:11 +01:00
|
|
|
* string corresponding to <b>args</b>. */
|
2006-10-23 07:24:57 +02:00
|
|
|
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:
|
2008-03-11 05:30:14 +01:00
|
|
|
status = "STATUS_SERVER";
|
2006-10-23 07:24:57 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-05-08 18:12:20 +02:00
|
|
|
send_control_event_impl(type, ALL_FORMATS, format_buf, args);
|
2006-10-23 07:24:57 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-02 19:58:11 +01:00
|
|
|
/** Format and send an EVENT_STATUS_GENERAL event whose main text is obtained
|
2007-02-06 03:49:07 +01:00
|
|
|
* by formatting the arguments using the printf-style <b>format</b>. */
|
2006-10-23 07:24:57 +02:00
|
|
|
int
|
|
|
|
control_event_general_status(int severity, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int r;
|
2007-03-04 22:08:28 +01:00
|
|
|
if (!EVENT_IS_INTERESTING(EVENT_STATUS_GENERAL))
|
2006-10-23 07:24:57 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
r = control_event_status(EVENT_STATUS_GENERAL, severity, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2007-02-02 19:58:11 +01:00
|
|
|
/** Format and send an EVENT_STATUS_CLIENT event whose main text is obtained
|
2007-02-06 03:49:07 +01:00
|
|
|
* by formatting the arguments using the printf-style <b>format</b>. */
|
2006-10-23 07:24:57 +02:00
|
|
|
int
|
|
|
|
control_event_client_status(int severity, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int r;
|
2007-03-04 22:08:28 +01:00
|
|
|
if (!EVENT_IS_INTERESTING(EVENT_STATUS_CLIENT))
|
2006-10-23 07:24:57 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
r = control_event_status(EVENT_STATUS_CLIENT, severity, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2007-02-02 19:58:11 +01:00
|
|
|
/** Format and send an EVENT_STATUS_SERVER event whose main text is obtained
|
2007-02-06 03:49:07 +01:00
|
|
|
* by formatting the arguments using the printf-style <b>format</b>. */
|
2006-10-23 07:24:57 +02:00
|
|
|
int
|
|
|
|
control_event_server_status(int severity, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int r;
|
2007-03-04 22:08:28 +01:00
|
|
|
if (!EVENT_IS_INTERESTING(EVENT_STATUS_SERVER))
|
2006-10-23 07:24:57 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
r = control_event_status(EVENT_STATUS_SERVER, severity, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2007-02-02 19:58:11 +01:00
|
|
|
/** Called when the status of an entry guard with the given <b>nickname</b>
|
|
|
|
* and identity <b>digest</b> has changed to <b>status</b>: tells any
|
2007-02-06 03:49:07 +01:00
|
|
|
* controllers that care. */
|
2006-12-15 23:40:20 +01:00
|
|
|
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);
|
2007-03-04 22:08:28 +01:00
|
|
|
if (!EVENT_IS_INTERESTING(EVENT_GUARD))
|
2006-12-15 23:40:20 +01:00
|
|
|
return 0;
|
|
|
|
|
2009-05-08 18:12:20 +02:00
|
|
|
{
|
2006-12-15 23:40:20 +01:00
|
|
|
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);
|
|
|
|
}
|
2009-05-08 18:12:20 +02:00
|
|
|
send_control_event(EVENT_GUARD, ALL_FORMATS,
|
2008-12-27 07:50:07 +01:00
|
|
|
"650 GUARD ENTRY %s %s\r\n", buf, status);
|
2006-12-15 23:40:20 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-04 18:21:58 +02:00
|
|
|
/** Helper: Return a newly allocated string containing a path to the
|
|
|
|
* file where we store our authentication cookie. */
|
2007-08-16 19:31:23 +02:00
|
|
|
static char *
|
|
|
|
get_cookie_file(void)
|
|
|
|
{
|
2007-08-16 19:46:01 +02:00
|
|
|
or_options_t *options = get_options();
|
|
|
|
if (options->CookieAuthFile && strlen(options->CookieAuthFile)) {
|
|
|
|
return tor_strdup(options->CookieAuthFile);
|
|
|
|
} else {
|
2007-10-17 18:55:44 +02:00
|
|
|
return get_datadir_fname("control_auth_cookie");
|
2007-08-16 19:46:01 +02:00
|
|
|
}
|
2007-08-16 19:31:23 +02:00
|
|
|
}
|
|
|
|
|
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
|
2007-08-15 17:26:14 +02:00
|
|
|
* authorized to use the control connection. Return -1 if we can't
|
|
|
|
* write the file, or 0 on success. */
|
2004-11-07 22:37:50 +01:00
|
|
|
int
|
2004-11-12 17:39:03 +01:00
|
|
|
init_cookie_authentication(int enabled)
|
2004-11-03 20:49:03 +01:00
|
|
|
{
|
2007-08-16 19:31:23 +02:00
|
|
|
char *fname;
|
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
|
|
|
|
2007-08-15 17:26:14 +02:00
|
|
|
/* We don't want to generate a new cookie every time we call
|
|
|
|
* options_act(). One should be enough. */
|
|
|
|
if (authentication_cookie_is_set)
|
|
|
|
return 0; /* all set */
|
|
|
|
|
2007-08-16 19:31:23 +02:00
|
|
|
fname = get_cookie_file();
|
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)) {
|
2007-08-15 17:26:14 +02:00
|
|
|
log_warn(LD_FS,"Error writing authentication cookie to %s.",
|
|
|
|
escaped(fname));
|
2007-08-16 19:31:23 +02:00
|
|
|
tor_free(fname);
|
2004-11-03 20:49:03 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2007-08-16 19:46:01 +02:00
|
|
|
#ifndef MS_WINDOWS
|
|
|
|
if (get_options()->CookieAuthFileGroupReadable) {
|
|
|
|
if (chmod(fname, 0640)) {
|
|
|
|
log_warn(LD_FS,"Unable to make %s group-readable.", escaped(fname));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2004-11-03 20:49:03 +01:00
|
|
|
|
2007-08-16 19:31:23 +02:00
|
|
|
tor_free(fname);
|
2004-11-03 20:49:03 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2005-06-11 20:59:24 +02:00
|
|
|
|
2008-06-08 04:53:32 +02:00
|
|
|
/** Convert the name of a bootstrapping phase <b>s</b> into strings
|
|
|
|
* <b>tag</b> and <b>summary</b> suitable for display by the controller. */
|
2008-06-09 20:32:43 +02:00
|
|
|
static int
|
2008-06-08 04:53:32 +02:00
|
|
|
bootstrap_status_to_string(bootstrap_status_t s, const char **tag,
|
|
|
|
const char **summary)
|
2008-06-07 07:27:34 +02:00
|
|
|
{
|
|
|
|
switch (s) {
|
2008-06-18 07:35:19 +02:00
|
|
|
case BOOTSTRAP_STATUS_UNDEF:
|
|
|
|
*tag = "undef";
|
|
|
|
*summary = "Undefined";
|
|
|
|
break;
|
2008-06-07 07:27:34 +02:00
|
|
|
case BOOTSTRAP_STATUS_STARTING:
|
2008-06-08 04:53:32 +02:00
|
|
|
*tag = "starting";
|
|
|
|
*summary = "Starting";
|
|
|
|
break;
|
2008-06-07 07:27:34 +02:00
|
|
|
case BOOTSTRAP_STATUS_CONN_DIR:
|
2008-06-08 04:53:32 +02:00
|
|
|
*tag = "conn_dir";
|
2008-06-13 08:23:46 +02:00
|
|
|
*summary = "Connecting to directory server";
|
2008-06-08 04:53:32 +02:00
|
|
|
break;
|
2008-06-07 07:27:34 +02:00
|
|
|
case BOOTSTRAP_STATUS_HANDSHAKE:
|
2008-06-08 04:53:32 +02:00
|
|
|
*tag = "status_handshake";
|
|
|
|
*summary = "Finishing handshake";
|
|
|
|
break;
|
2008-06-07 07:27:34 +02:00
|
|
|
case BOOTSTRAP_STATUS_HANDSHAKE_DIR:
|
2008-06-08 04:53:32 +02:00
|
|
|
*tag = "handshake_dir";
|
2008-06-13 08:23:46 +02:00
|
|
|
*summary = "Finishing handshake with directory server";
|
2008-06-08 04:53:32 +02:00
|
|
|
break;
|
2008-06-07 07:27:34 +02:00
|
|
|
case BOOTSTRAP_STATUS_ONEHOP_CREATE:
|
2008-06-08 04:53:32 +02:00
|
|
|
*tag = "onehop_create";
|
2008-06-13 08:23:46 +02:00
|
|
|
*summary = "Establishing an encrypted directory connection";
|
2008-06-08 04:53:32 +02:00
|
|
|
break;
|
2008-06-07 07:27:34 +02:00
|
|
|
case BOOTSTRAP_STATUS_REQUESTING_STATUS:
|
2008-06-08 04:53:32 +02:00
|
|
|
*tag = "requesting_status";
|
|
|
|
*summary = "Asking for networkstatus consensus";
|
|
|
|
break;
|
2008-06-07 07:27:34 +02:00
|
|
|
case BOOTSTRAP_STATUS_LOADING_STATUS:
|
2008-06-08 04:53:32 +02:00
|
|
|
*tag = "loading_status";
|
|
|
|
*summary = "Loading networkstatus consensus";
|
|
|
|
break;
|
2008-06-07 07:27:34 +02:00
|
|
|
case BOOTSTRAP_STATUS_LOADING_KEYS:
|
2008-06-08 04:53:32 +02:00
|
|
|
*tag = "loading_keys";
|
|
|
|
*summary = "Loading authority key certs";
|
|
|
|
break;
|
2008-06-07 07:27:34 +02:00
|
|
|
case BOOTSTRAP_STATUS_REQUESTING_DESCRIPTORS:
|
2008-06-08 04:53:32 +02:00
|
|
|
*tag = "requesting_descriptors";
|
|
|
|
*summary = "Asking for relay descriptors";
|
|
|
|
break;
|
2008-06-07 07:27:34 +02:00
|
|
|
case BOOTSTRAP_STATUS_LOADING_DESCRIPTORS:
|
2008-06-08 04:53:32 +02:00
|
|
|
*tag = "loading_descriptors";
|
|
|
|
*summary = "Loading relay descriptors";
|
|
|
|
break;
|
2008-06-07 07:27:34 +02:00
|
|
|
case BOOTSTRAP_STATUS_CONN_OR:
|
2008-06-08 04:53:32 +02:00
|
|
|
*tag = "conn_or";
|
2008-06-13 08:23:46 +02:00
|
|
|
*summary = "Connecting to the Tor network";
|
2008-06-08 04:53:32 +02:00
|
|
|
break;
|
2008-06-07 07:27:34 +02:00
|
|
|
case BOOTSTRAP_STATUS_HANDSHAKE_OR:
|
2008-06-08 04:53:32 +02:00
|
|
|
*tag = "handshake_or";
|
2008-06-13 08:23:46 +02:00
|
|
|
*summary = "Finishing handshake with first hop";
|
2008-06-08 04:53:32 +02:00
|
|
|
break;
|
2008-06-07 07:27:34 +02:00
|
|
|
case BOOTSTRAP_STATUS_CIRCUIT_CREATE:
|
2008-06-08 04:53:32 +02:00
|
|
|
*tag = "circuit_create";
|
2008-06-13 08:23:46 +02:00
|
|
|
*summary = "Establishing a Tor circuit";
|
2008-06-08 04:53:32 +02:00
|
|
|
break;
|
2008-06-07 07:27:34 +02:00
|
|
|
case BOOTSTRAP_STATUS_DONE:
|
2008-06-08 04:53:32 +02:00
|
|
|
*tag = "done";
|
|
|
|
*summary = "Done";
|
|
|
|
break;
|
2008-06-07 07:27:34 +02:00
|
|
|
default:
|
2008-06-09 20:32:43 +02:00
|
|
|
// log_warn(LD_BUG, "Unrecognized bootstrap status code %d", s);
|
2008-06-08 04:53:32 +02:00
|
|
|
*tag = *summary = "unknown";
|
2008-06-09 20:32:43 +02:00
|
|
|
return -1;
|
2008-06-07 07:27:34 +02:00
|
|
|
}
|
2008-06-09 20:32:43 +02:00
|
|
|
return 0;
|
2008-06-07 07:27:34 +02:00
|
|
|
}
|
|
|
|
|
2008-06-09 20:32:43 +02:00
|
|
|
/** What percentage through the bootstrap process are we? We remember
|
|
|
|
* this so we can avoid sending redundant bootstrap status events, and
|
|
|
|
* so we can guess context for the bootstrap messages which are
|
2008-06-18 07:35:19 +02:00
|
|
|
* ambiguous. It starts at 'undef', but gets set to 'starting' while
|
|
|
|
* Tor initializes. */
|
|
|
|
static int bootstrap_percent = BOOTSTRAP_STATUS_UNDEF;
|
2008-06-09 20:32:43 +02:00
|
|
|
|
|
|
|
/** How many problems have we had getting to the next bootstrapping phase?
|
|
|
|
* These include failure to establish a connection to a Tor relay,
|
|
|
|
* failures to finish the TLS handshake, failures to validate the
|
|
|
|
* consensus document, etc. */
|
|
|
|
static int bootstrap_problems = 0;
|
|
|
|
|
|
|
|
/* We only tell the controller once we've hit a threshold of problems
|
|
|
|
* for the current phase. */
|
|
|
|
#define BOOTSTRAP_PROBLEM_THRESHOLD 10
|
|
|
|
|
2008-06-07 07:27:34 +02:00
|
|
|
/** Called when Tor has made progress at bootstrapping its directory
|
2008-06-09 20:32:43 +02:00
|
|
|
* information and initial circuits.
|
|
|
|
*
|
|
|
|
* <b>status</b> is the new status, that is, what task we will be doing
|
|
|
|
* next. <b>percent</b> is zero if we just started this task, else it
|
|
|
|
* represents progress on the task. */
|
|
|
|
void
|
|
|
|
control_event_bootstrap(bootstrap_status_t status, int progress)
|
2008-06-07 07:27:34 +02:00
|
|
|
{
|
2008-06-08 04:53:32 +02:00
|
|
|
const char *tag, *summary;
|
2008-06-17 10:01:43 +02:00
|
|
|
char buf[BOOTSTRAP_MSG_LEN];
|
2008-06-07 07:27:34 +02:00
|
|
|
|
2008-06-18 07:35:19 +02:00
|
|
|
if (bootstrap_percent == BOOTSTRAP_STATUS_DONE)
|
2008-06-09 20:32:43 +02:00
|
|
|
return; /* already bootstrapped; nothing to be done here. */
|
2008-06-07 07:27:34 +02:00
|
|
|
|
2008-06-09 09:01:52 +02:00
|
|
|
/* special case for handshaking status, since our TLS handshaking code
|
2008-06-07 07:27:34 +02:00
|
|
|
* can't distinguish what the connection is going to be for. */
|
|
|
|
if (status == BOOTSTRAP_STATUS_HANDSHAKE) {
|
2008-06-09 20:32:43 +02:00
|
|
|
if (bootstrap_percent < BOOTSTRAP_STATUS_CONN_OR) {
|
2008-06-07 07:27:34 +02:00
|
|
|
status = BOOTSTRAP_STATUS_HANDSHAKE_DIR;
|
|
|
|
} else {
|
|
|
|
status = BOOTSTRAP_STATUS_HANDSHAKE_OR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-09 20:32:43 +02:00
|
|
|
if (status > bootstrap_percent ||
|
|
|
|
(progress && progress > bootstrap_percent)) {
|
2008-06-08 04:53:32 +02:00
|
|
|
bootstrap_status_to_string(status, &tag, &summary);
|
2008-06-24 10:00:30 +02:00
|
|
|
log(status ? LOG_NOTICE : LOG_INFO, LD_CONTROL,
|
|
|
|
"Bootstrapped %d%%: %s.", progress ? progress : status, summary);
|
2008-06-17 10:01:43 +02:00
|
|
|
tor_snprintf(buf, sizeof(buf),
|
2008-06-08 04:53:32 +02:00
|
|
|
"BOOTSTRAP PROGRESS=%d TAG=%s SUMMARY=\"%s\"",
|
2008-06-09 20:32:43 +02:00
|
|
|
progress ? progress : status, tag, summary);
|
2008-06-17 10:01:43 +02:00
|
|
|
tor_snprintf(last_sent_bootstrap_message,
|
|
|
|
sizeof(last_sent_bootstrap_message),
|
|
|
|
"NOTICE %s", buf);
|
|
|
|
control_event_client_status(LOG_NOTICE, "%s", buf);
|
2008-06-09 20:32:43 +02:00
|
|
|
if (status > bootstrap_percent) {
|
|
|
|
bootstrap_percent = status; /* new milestone reached */
|
|
|
|
}
|
|
|
|
if (progress > bootstrap_percent) {
|
|
|
|
/* incremental progress within a milestone */
|
|
|
|
bootstrap_percent = progress;
|
2008-10-13 05:34:29 +02:00
|
|
|
bootstrap_problems = 0; /* Progress! Reset our problem counter. */
|
2008-06-09 20:32:43 +02:00
|
|
|
}
|
2008-06-07 11:26:41 +02:00
|
|
|
}
|
2008-06-09 20:32:43 +02:00
|
|
|
}
|
2008-06-07 07:27:34 +02:00
|
|
|
|
2008-12-22 15:56:28 +01:00
|
|
|
/** Called when Tor has failed to make bootstrapping progress in a way
|
2008-06-09 20:32:43 +02:00
|
|
|
* that indicates a problem. <b>warn</b> gives a hint as to why, and
|
2008-08-05 01:38:32 +02:00
|
|
|
* <b>reason</b> provides an "or_conn_end_reason" tag.
|
2008-06-09 20:32:43 +02:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
control_event_bootstrap_problem(const char *warn, int reason)
|
|
|
|
{
|
|
|
|
int status = bootstrap_percent;
|
|
|
|
const char *tag, *summary;
|
2008-06-17 10:01:43 +02:00
|
|
|
char buf[BOOTSTRAP_MSG_LEN];
|
2008-06-19 06:50:06 +02:00
|
|
|
const char *recommendation = "ignore";
|
2008-06-09 20:32:43 +02:00
|
|
|
|
2008-06-10 00:15:08 +02:00
|
|
|
if (bootstrap_percent == 100)
|
|
|
|
return; /* already bootstrapped; nothing to be done here. */
|
|
|
|
|
2008-06-19 06:50:06 +02:00
|
|
|
bootstrap_problems++;
|
|
|
|
|
|
|
|
if (bootstrap_problems >= BOOTSTRAP_PROBLEM_THRESHOLD)
|
|
|
|
recommendation = "warn";
|
2008-06-09 20:32:43 +02:00
|
|
|
|
2008-06-24 10:00:30 +02:00
|
|
|
if (reason == END_OR_CONN_REASON_NO_ROUTE)
|
|
|
|
recommendation = "warn";
|
|
|
|
|
2008-06-20 06:34:39 +02:00
|
|
|
if (get_options()->UseBridges &&
|
|
|
|
!any_bridge_descriptors_known() &&
|
|
|
|
!any_pending_bridge_descriptor_fetches())
|
|
|
|
recommendation = "warn";
|
|
|
|
|
2008-06-18 07:35:19 +02:00
|
|
|
while (status>=0 && bootstrap_status_to_string(status, &tag, &summary) < 0)
|
2008-06-09 20:32:43 +02:00
|
|
|
status--; /* find a recognized status string based on current progress */
|
2008-10-13 05:34:29 +02:00
|
|
|
status = bootstrap_percent; /* set status back to the actual number */
|
2008-06-09 20:32:43 +02:00
|
|
|
|
2008-08-05 01:38:32 +02:00
|
|
|
log_fn(!strcmp(recommendation, "warn") ? LOG_WARN : LOG_INFO,
|
|
|
|
LD_CONTROL, "Problem bootstrapping. Stuck at %d%%: %s. (%s; %s; "
|
|
|
|
"count %d; recommendation %s)",
|
|
|
|
status, summary, warn,
|
|
|
|
orconn_end_reason_to_control_string(reason),
|
|
|
|
bootstrap_problems, recommendation);
|
2008-06-17 10:01:43 +02:00
|
|
|
tor_snprintf(buf, sizeof(buf),
|
2008-06-19 06:50:06 +02:00
|
|
|
"BOOTSTRAP PROGRESS=%d TAG=%s SUMMARY=\"%s\" WARNING=\"%s\" REASON=%s "
|
|
|
|
"COUNT=%d RECOMMENDATION=%s",
|
2008-06-09 20:32:43 +02:00
|
|
|
bootstrap_percent, tag, summary, warn,
|
2008-06-19 06:50:06 +02:00
|
|
|
orconn_end_reason_to_control_string(reason), bootstrap_problems,
|
|
|
|
recommendation);
|
2008-06-17 10:01:43 +02:00
|
|
|
tor_snprintf(last_sent_bootstrap_message,
|
|
|
|
sizeof(last_sent_bootstrap_message),
|
|
|
|
"WARN %s", buf);
|
|
|
|
control_event_client_status(LOG_WARN, "%s", buf);
|
2008-06-07 07:27:34 +02:00
|
|
|
}
|
|
|
|
|
2008-12-27 07:50:07 +01:00
|
|
|
/** We just generated a new summary of which countries we've seen clients
|
|
|
|
* from recently. Send a copy to the controller in case it wants to
|
|
|
|
* display it for the user. */
|
|
|
|
void
|
2009-12-12 08:32:46 +01:00
|
|
|
control_event_clients_seen(const char *controller_str)
|
2008-12-27 07:50:07 +01:00
|
|
|
{
|
|
|
|
send_control_event(EVENT_CLIENTS_SEEN, 0,
|
2009-12-12 08:32:46 +01:00
|
|
|
"650 CLIENTS_SEEN %s\r\n", controller_str);
|
2008-12-27 07:50:07 +01:00
|
|
|
}
|
|
|
|
|