control.c
上传用户:awang829
上传日期:2019-07-14
资源大小:2356k
文件大小:136k
- /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2009, The Tor Project, Inc. */
- /* See LICENSE for licensing information */
- /**
- * file control.c
- * brief Implementation for Tor's control-socket interface.
- * See doc/spec/control-spec.txt for full details on protocol.
- **/
- #define CONTROL_PRIVATE
- #include "or.h"
- /** Yield true iff <b>s</b> is the state of a control_connection_t that has
- * finished authentication and is accepting commands. */
- #define STATE_IS_OPEN(s) ((s) == CONTROL_CONN_STATE_OPEN)
- /* Recognized asynchronous event types. It's okay to expand this list
- * because it is used both as a list of v0 event types, and as indices
- * into the bitfield to determine which controllers want which events.
- */
- #define _EVENT_MIN 0x0001
- #define EVENT_CIRCUIT_STATUS 0x0001
- #define EVENT_STREAM_STATUS 0x0002
- #define EVENT_OR_CONN_STATUS 0x0003
- #define EVENT_BANDWIDTH_USED 0x0004
- #define EVENT_LOG_OBSOLETE 0x0005 /* Can reclaim this. */
- #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
- // #define EVENT_AUTHDIR_NEWDESCS 0x000D
- #define EVENT_DESCCHANGED 0x000E
- // #define EVENT_NS 0x000F
- #define EVENT_STATUS_CLIENT 0x0010
- #define EVENT_STATUS_SERVER 0x0011
- #define EVENT_STATUS_GENERAL 0x0012
- #define EVENT_GUARD 0x0013
- #define EVENT_STREAM_BANDWIDTH_USED 0x0014
- #define EVENT_CLIENTS_SEEN 0x0015
- #define EVENT_NEWCONSENSUS 0x0016
- #define _EVENT_MAX 0x0016
- /* If _EVENT_MAX ever hits 0x0020, we need to make the mask wider. */
- /** Bitfield: The bit 1<<e is set if <b>any</b> open control
- * 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
- * has interest in without having to walk over the global connection
- * list to find out.
- **/
- typedef uint32_t event_mask_t;
- /** An event mask of all the events that controller with the LONG_NAMES option
- * set is interested in receiving. */
- static event_mask_t global_event_mask1long = 0;
- /** An event mask of all the events that controller with the SHORT_NAMES option
- * set is interested in receiving. */
- static event_mask_t global_event_mask1short = 0;
- /** True iff we have disabled log messages from being sent to the controller */
- static int disable_log_messages = 0;
- /** Macro: true if any control connection is interested in events of type
- * <b>e</b>. */
- #define EVENT_IS_INTERESTING(e)
- ((global_event_mask1long|global_event_mask1short) & (1<<(e)))
- /** Macro: true if any control connection with the LONG_NAMES option is
- * interested in events of type <b>e</b>. */
- #define EVENT_IS_INTERESTING1L(e) (global_event_mask1long & (1<<(e)))
- /** Macro: true if any control connection with the SHORT_NAMES option is
- * interested in events of type <b>e</b>. */
- #define EVENT_IS_INTERESTING1S(e) (global_event_mask1short & (1<<(e)))
- /** If we're using cookie-type authentication, how long should our cookies be?
- */
- #define AUTHENTICATION_COOKIE_LEN 32
- /** If true, we've set authentication_cookie to a secret code and
- * stored it to disk. */
- static int authentication_cookie_is_set = 0;
- /** 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. */
- static char authentication_cookie[AUTHENTICATION_COOKIE_LEN];
- /** 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];
- /** Flag for event_format_t. Indicates that we should use the old
- * name format of nickname|hexdigest
- */
- #define SHORT_NAMES 1
- /** Flag for event_format_t. Indicates that we should use the new
- * name format of $hexdigest[=~]nickname
- */
- #define LONG_NAMES 2
- #define ALL_NAMES (SHORT_NAMES|LONG_NAMES)
- /** Flag for event_format_t. Indicates that we should use the new event
- * format where extra event fields are allowed using a NAME=VAL format. */
- #define EXTENDED_FORMAT 4
- /** Flag for event_format_t. Indicates that we are using the old event format
- * where extra fields aren't allowed. */
- #define NONEXTENDED_FORMAT 8
- #define ALL_FORMATS (EXTENDED_FORMAT|NONEXTENDED_FORMAT)
- /** Bit field of flags to select how to format a controller event. Recognized
- * flags are SHORT_NAMES, LONG_NAMES, EXTENDED_FORMAT, NONEXTENDED_FORMAT. */
- typedef int event_format_t;
- static void connection_printf_to_buf(control_connection_t *conn,
- const char *format, ...)
- CHECK_PRINTF(2,3);
- static void send_control_done(control_connection_t *conn);
- static void send_control_event(uint16_t event, event_format_t which,
- const char *format, ...)
- CHECK_PRINTF(3,4);
- static void send_control_event_extended(uint16_t event, event_format_t which,
- const char *format, ...)
- CHECK_PRINTF(3,4);
- static int handle_control_setconf(control_connection_t *conn, uint32_t len,
- char *body);
- static int handle_control_resetconf(control_connection_t *conn, uint32_t len,
- char *body);
- static int handle_control_getconf(control_connection_t *conn, uint32_t len,
- const char *body);
- static int handle_control_loadconf(control_connection_t *conn, uint32_t len,
- const char *body);
- static int handle_control_setevents(control_connection_t *conn, uint32_t len,
- const char *body);
- static int handle_control_authenticate(control_connection_t *conn,
- uint32_t len,
- const char *body);
- static int handle_control_saveconf(control_connection_t *conn, uint32_t len,
- const char *body);
- static int handle_control_signal(control_connection_t *conn, uint32_t len,
- const char *body);
- static int handle_control_mapaddress(control_connection_t *conn, uint32_t len,
- const char *body);
- static char *list_getinfo_options(void);
- static int handle_control_getinfo(control_connection_t *conn, uint32_t len,
- const char *body);
- static int handle_control_extendcircuit(control_connection_t *conn,
- uint32_t len,
- const char *body);
- static int handle_control_setcircuitpurpose(control_connection_t *conn,
- uint32_t len, const char *body);
- static int handle_control_attachstream(control_connection_t *conn,
- uint32_t len,
- const char *body);
- static int handle_control_postdescriptor(control_connection_t *conn,
- uint32_t len,
- const char *body);
- static int handle_control_redirectstream(control_connection_t *conn,
- uint32_t len,
- const char *body);
- static int handle_control_closestream(control_connection_t *conn, uint32_t len,
- const char *body);
- static int handle_control_closecircuit(control_connection_t *conn,
- uint32_t len,
- const char *body);
- static int handle_control_resolve(control_connection_t *conn, uint32_t len,
- const char *body);
- static int handle_control_usefeature(control_connection_t *conn,
- uint32_t len,
- const char *body);
- static int write_stream_target_to_buf(edge_connection_t *conn, char *buf,
- size_t len);
- static void orconn_target_get_name(int long_names, char *buf, size_t len,
- or_connection_t *conn);
- static char *get_cookie_file(void);
- /** Given a control event code for a message event, return the corresponding
- * log severity. */
- 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;
- }
- }
- /** Given a log severity, return the corresponding control event code. */
- 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;
- }
- }
- /** Set <b>global_event_mask*</b> to the bitwise OR of each live control
- * connection's event_mask field. */
- void
- control_update_global_event_mask(void)
- {
- smartlist_t *conns = get_connection_array();
- event_mask_t old_mask, new_mask;
- old_mask = global_event_mask1short;
- old_mask |= global_event_mask1long;
- global_event_mask1short = 0;
- global_event_mask1long = 0;
- 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);
- if (conn->use_long_names)
- global_event_mask1long |= conn->event_mask;
- else
- global_event_mask1short |= conn->event_mask;
- }
- });
- new_mask = global_event_mask1short;
- new_mask |= global_event_mask1long;
- /* Handle the aftermath. Set up the log callback to tell us only what
- * we want to hear...*/
- control_adjust_event_log_severity();
- /* ...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)) {
- 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;
- }
- });
- }
- }
- /** 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. */
- void
- control_adjust_event_log_severity(void)
- {
- int i;
- int min_log_event=EVENT_ERR_MSG, max_log_event=EVENT_DEBUG_MSG;
- for (i = EVENT_DEBUG_MSG; i <= EVENT_ERR_MSG; ++i) {
- if (EVENT_IS_INTERESTING(i)) {
- min_log_event = i;
- break;
- }
- }
- for (i = EVENT_ERR_MSG; i >= EVENT_DEBUG_MSG; --i) {
- if (EVENT_IS_INTERESTING(i)) {
- max_log_event = i;
- break;
- }
- }
- if (EVENT_IS_INTERESTING(EVENT_LOG_OBSOLETE) ||
- EVENT_IS_INTERESTING(EVENT_STATUS_GENERAL)) {
- 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;
- }
- 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);
- }
- /** 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);
- }
- /** Append a NUL-terminated string <b>s</b> to the end of
- * <b>conn</b>->outbuf.
- */
- static INLINE void
- connection_write_str_to_buf(const char *s, control_connection_t *conn)
- {
- size_t len = strlen(s);
- connection_write_to_buf(s, len, TO_CONN(conn));
- }
- /** Given a <b>len</b>-character string in <b>data</b>, made of lines
- * terminated by CRLF, allocate a new string in *<b>out</b>, and copy the
- * contents of <b>data</b> into *<b>out</b>, adding a period before any period
- * that that appears at the start of a line, and adding a period-CRLF line at
- * the end. Replace all LF characters sequences with CRLF. Return the number
- * of bytes in *<b>out</b>.
- */
- /* static */ size_t
- write_escaped_data(const char *data, size_t len, char **out)
- {
- size_t sz_out = len+8;
- char *outp;
- const char *start = data, *end;
- int i;
- int start_of_line;
- for (i=0; i<(int)len; ++i) {
- if (data[i]== 'n')
- sz_out += 2; /* Maybe add a CR; maybe add a dot. */
- }
- *out = outp = tor_malloc(sz_out+1);
- end = data+len;
- start_of_line = 1;
- while (data < end) {
- if (*data == 'n') {
- if (data > start && data[-1] != 'r')
- *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++;
- }
- if (outp < *out+2 || memcmp(outp-2, "rn", 2)) {
- *outp++ = 'r';
- *outp++ = 'n';
- }
- *outp++ = '.';
- *outp++ = 'r';
- *outp++ = 'n';
- *outp = ' '; /* NUL-terminate just in case. */
- tor_assert((outp - *out) <= (int)sz_out);
- return outp - *out;
- }
- /** Given a <b>len</b>-character string in <b>data</b>, made of lines
- * terminated by CRLF, allocate a new string in *<b>out</b>, and copy
- * the contents of <b>data</b> into *<b>out</b>, removing any period
- * that appears at the start of a line, and replacing all CRLF sequences
- * with LF. Return the number of
- * bytes in *<b>out</b>. */
- /* static */ size_t
- read_escaped_data(const char *data, size_t len, char **out)
- {
- char *outp;
- const char *next;
- const char *end;
- *out = outp = tor_malloc(len+1);
- end = data+len;
- while (data < end) {
- /* we're at the start of a line. */
- if (*data == '.')
- ++data;
- next = memchr(data, 'n', end-data);
- if (next) {
- 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. */
- } else {
- memcpy(outp, data, end-data);
- outp += (end-data);
- *outp = ' ';
- return outp - *out;
- }
- *outp++ = 'n';
- }
- *outp = ' ';
- return outp - *out;
- }
- /** If the first <b>in_len_max</b> characters in <b>start</b> contain a
- * 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)
- {
- const char *cp, *end;
- int chars = 0;
- if (*start != '"')
- return -1;
- cp = start+1;
- end = start+in_len_max;
- /* Calculate length. */
- while (1) {
- if (cp >= end) {
- return -1; /* Too long. */
- } else if (*cp == '\') {
- if (++cp == end)
- return -1; /* Can't escape EOS. */
- ++cp;
- ++chars;
- } else if (*cp == '"') {
- break;
- } else {
- ++cp;
- ++chars;
- }
- }
- if (chars_out)
- *chars_out = chars;
- return (int)(cp - start+1);
- }
- /** 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;
- *out = tor_strndup(start, *out_len);
- return start+length;
- }
- /** Given a pointer to a string starting at <b>start</b> containing
- * <b>in_len_max</b> characters, decode a string beginning with one double
- * quote, containing any number of non-quote characters or characters escaped
- * with a backslash, and ending with a final double quote. Place the resulting
- * string (unquoted, unescaped) into a newly allocated string in *<b>out</b>;
- * store its length in <b>out_len</b>. On success, return a pointer to the
- * character immediately following the escaped string. On failure, return
- * NULL. */
- static const char *
- decode_escaped_string(const char *start, size_t in_len_max,
- char **out, size_t *out_len)
- {
- const char *cp, *end;
- char *outp;
- int len, n_chars = 0;
- len = get_escaped_string_length(start, in_len_max, &n_chars);
- if (len<0)
- return NULL;
- end = start+len-1; /* Index of last quote. */
- tor_assert(*end == '"');
- outp = *out = tor_malloc(len+1);
- *out_len = n_chars;
- cp = start+1;
- while (cp < end) {
- if (*cp == '\')
- ++cp;
- *outp++ = *cp++;
- }
- *outp = ' ';
- tor_assert((outp - *out) == (int)*out_len);
- return end+1;
- }
- /** 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.
- *
- * Currently the length of the message is limited to 1024 (including the
- * ending CR LF NUL ("\r\n\0"). */
- static void
- connection_printf_to_buf(control_connection_t *conn, const char *format, ...)
- {
- #define CONNECTION_PRINTF_TO_BUF_BUFFERSIZE 1024
- va_list ap;
- char buf[CONNECTION_PRINTF_TO_BUF_BUFFERSIZE];
- int r;
- size_t len;
- va_start(ap,format);
- r = tor_vsnprintf(buf, sizeof(buf), format, ap);
- va_end(ap);
- if (r<0) {
- log_warn(LD_BUG, "Unable to format string for controller.");
- return;
- }
- len = strlen(buf);
- if (memcmp("rn ", buf+len-2, 3)) {
- buf[CONNECTION_PRINTF_TO_BUF_BUFFERSIZE-1] = ' ';
- buf[CONNECTION_PRINTF_TO_BUF_BUFFERSIZE-2] = 'n';
- buf[CONNECTION_PRINTF_TO_BUF_BUFFERSIZE-3] = 'r';
- }
- connection_write_to_buf(buf, len, TO_CONN(conn));
- }
- /** Send a "DONE" message down the control connection <b>conn</b>. */
- static void
- send_control_done(control_connection_t *conn)
- {
- connection_write_str_to_buf("250 OKrn", conn);
- }
- /** Send an event to all v1 controllers that are listening for code
- * <b>event</b>. The event's body is given by <b>msg</b>.
- *
- * 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
- * <b>which</b> & LONG_NAMES, the event contains long-format names: send it
- * to controllers that <em>have</em> enabled VERBOSE_NAMES.
- *
- * The EXTENDED_FORMAT and NONEXTENDED_FORMAT flags behave similarly with
- * respect to the EXTENDED_EVENTS feature. */
- static void
- send_control_event_string(uint16_t event, event_format_t which,
- const char *msg)
- {
- smartlist_t *conns = get_connection_array();
- tor_assert(event >= _EVENT_MIN && event <= _EVENT_MAX);
- SMARTLIST_FOREACH(conns, connection_t *, conn,
- {
- 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);
- if (control_conn->use_long_names) {
- if (!(which & LONG_NAMES))
- continue;
- } else {
- if (!(which & SHORT_NAMES))
- continue;
- }
- if (control_conn->use_extended_events) {
- if (!(which & EXTENDED_FORMAT))
- continue;
- } else {
- if (!(which & NONEXTENDED_FORMAT))
- continue;
- }
- if (control_conn->event_mask & (1<<event)) {
- int is_err = 0;
- connection_write_to_buf(msg, strlen(msg), TO_CONN(control_conn));
- if (event == EVENT_ERR_MSG)
- 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)
- connection_handle_write(TO_CONN(control_conn), 1);
- }
- }
- });
- }
- /** 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.
- *
- * If <b>extended</b> is true, and the format contains a single '@' character,
- * it will be replaced with a space and all text after that character will be
- * sent only to controllers that have enabled extended events.
- *
- * Currently the length of the message is limited to 1024 (including the
- * ending \r\n\0). */
- static void
- send_control_event_impl(uint16_t event, event_format_t which, int extended,
- const char *format, va_list ap)
- {
- /* This is just a little longer than the longest allowed log message */
- #define SEND_CONTROL1_EVENT_BUFFERSIZE 10064
- int r;
- char buf[SEND_CONTROL1_EVENT_BUFFERSIZE];
- size_t len;
- char *cp;
- r = tor_vsnprintf(buf, sizeof(buf), format, ap);
- if (r<0) {
- log_warn(LD_BUG, "Unable to format event for controller.");
- return;
- }
- len = strlen(buf);
- if (memcmp("rn ", buf+len-2, 3)) {
- /* if it is not properly terminated, do it now */
- buf[SEND_CONTROL1_EVENT_BUFFERSIZE-1] = ' ';
- buf[SEND_CONTROL1_EVENT_BUFFERSIZE-2] = 'n';
- buf[SEND_CONTROL1_EVENT_BUFFERSIZE-3] = 'r';
- }
- if (extended && (cp = strchr(buf, '@'))) {
- which &= ~ALL_FORMATS;
- *cp = ' ';
- send_control_event_string(event, which|EXTENDED_FORMAT, buf);
- memcpy(cp, "rn ", 3);
- send_control_event_string(event, which|NONEXTENDED_FORMAT, buf);
- } else {
- send_control_event_string(event, which|ALL_FORMATS, buf);
- }
- }
- /** Send an event to all v1 controllers that are listening for code
- * <b>event</b>. The event's body is created by the printf-style format in
- * <b>format</b>, and other arguments as provided.
- *
- * Currently the length of the message is limited to 1024 (including the
- * ending \n\r\0. */
- static void
- send_control_event(uint16_t event, event_format_t which,
- const char *format, ...)
- {
- va_list ap;
- va_start(ap, format);
- send_control_event_impl(event, which, 0, format, ap);
- va_end(ap);
- }
- /** Send an event to all v1 controllers that are listening for code
- * <b>event</b>. The event's body is created by the printf-style format in
- * <b>format</b>, and other arguments as provided.
- *
- * If the format contains a single '@' character, it will be replaced with a
- * space and all text after that character will be sent only to controllers
- * that have enabled extended events.
- *
- * Currently the length of the message is limited to 1024 (including the
- * ending \n\r\0. */
- static void
- send_control_event_extended(uint16_t event, event_format_t which,
- const char *format, ...)
- {
- va_list ap;
- va_start(ap, format);
- send_control_event_impl(event, which, 1, format, ap);
- va_end(ap);
- }
- /** Given a text circuit <b>id</b>, return the corresponding circuit. */
- static origin_circuit_t *
- get_circ(const char *id)
- {
- uint32_t n_id;
- int ok;
- n_id = (uint32_t) tor_parse_ulong(id, 10, 0, UINT32_MAX, &ok, NULL);
- if (!ok)
- return NULL;
- return circuit_get_by_global_id(n_id);
- }
- /** Given a text stream <b>id</b>, return the corresponding AP connection. */
- static edge_connection_t *
- get_stream(const char *id)
- {
- uint64_t n_id;
- int ok;
- connection_t *conn;
- n_id = tor_parse_uint64(id, 10, 0, UINT64_MAX, &ok, NULL);
- if (!ok)
- return NULL;
- conn = connection_get_by_global_id(n_id);
- if (!conn || conn->type != CONN_TYPE_AP || conn->marked_for_close)
- return NULL;
- return TO_EDGE_CONN(conn);
- }
- /** Helper for setconf and resetconf. Acts like setconf, except
- * it passes <b>use_defaults</b> on to options_trial_assign(). Modifies the
- * contents of body.
- */
- static int
- control_setconf_helper(control_connection_t *conn, uint32_t len, char *body,
- int use_defaults)
- {
- setopt_err_t opt_err;
- config_line_t *lines=NULL;
- char *start = body;
- char *errstring = NULL;
- const int clear_first = 1;
- 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 == '=') {
- char *val=NULL;
- size_t val_len=0;
- 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);
- } else {
- body = (char*)extract_escaped_string(body, (len - (body-start)),
- &val, &val_len);
- if (!body) {
- connection_write_str_to_buf("551 Couldn't parse stringrn", conn);
- SMARTLIST_FOREACH(entries, char *, cp, tor_free(cp));
- smartlist_free(entries);
- tor_free(key);
- return 0;
- }
- }
- 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;
- }
- smartlist_add(entries, entry);
- while (TOR_ISSPACE(*body))
- ++body;
- }
- smartlist_add(entries, tor_strdup(""));
- config = smartlist_join_strings(entries, "n", 0, NULL);
- SMARTLIST_FOREACH(entries, char *, cp, tor_free(cp));
- smartlist_free(entries);
- 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 configurationrn",
- conn);
- tor_free(config);
- return 0;
- }
- tor_free(config);
- opt_err = options_trial_assign(lines, use_defaults, clear_first, &errstring);
- {
- const char *msg;
- switch (opt_err) {
- case SETOPT_ERR_MISC:
- msg = "552 Unrecognized option";
- break;
- case SETOPT_ERR_PARSE:
- msg = "513 Unacceptable option value";
- break;
- case SETOPT_ERR_TRANSITION:
- msg = "553 Transition not allowed";
- break;
- case SETOPT_ERR_SETTING:
- default:
- msg = "553 Unable to set option";
- break;
- case SETOPT_OK:
- config_free_lines(lines);
- send_control_done(conn);
- return 0;
- }
- log_warn(LD_CONTROL,
- "Controller gave us config lines that didn't validate: %s",
- errstring);
- connection_printf_to_buf(conn, "%s: %srn", msg, errstring);
- config_free_lines(lines);
- tor_free(errstring);
- return 0;
- }
- }
- /** Called when we receive a SETCONF message: parse the body and try
- * to update our configuration. Reply with a DONE or ERROR message.
- * Modifies the contents of body.*/
- static int
- handle_control_setconf(control_connection_t *conn, uint32_t len, char *body)
- {
- return control_setconf_helper(conn, len, body, 0);
- }
- /** Called when we receive a RESETCONF message: parse the body and try
- * to update our configuration. Reply with a DONE or ERROR message.
- * Modifies the contents of body. */
- static int
- handle_control_resetconf(control_connection_t *conn, uint32_t len, char *body)
- {
- return control_setconf_helper(conn, len, body, 1);
- }
- /** Called when we receive a GETCONF message. Parse the request, and
- * reply with a CONFVALUE or an ERROR message */
- static int
- handle_control_getconf(control_connection_t *conn, uint32_t body_len,
- const char *body)
- {
- smartlist_t *questions = smartlist_create();
- smartlist_t *answers = smartlist_create();
- smartlist_t *unrecognized = smartlist_create();
- char *msg = NULL;
- size_t msg_len;
- or_options_t *options = get_options();
- int i, len;
- (void) body_len; /* body is NUL-terminated; so we can ignore len. */
- smartlist_split_string(questions, body, " ",
- SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
- SMARTLIST_FOREACH(questions, const char *, q,
- {
- if (!option_is_recognized(q)) {
- smartlist_add(unrecognized, (char*) q);
- } else {
- config_line_t *answer = option_get_assignment(options,q);
- if (!answer) {
- const char *name = option_get_canonical_name(q);
- size_t alen = strlen(name)+8;
- char *astr = tor_malloc(alen);
- tor_snprintf(astr, alen, "250-%srn", name);
- smartlist_add(answers, astr);
- }
- while (answer) {
- config_line_t *next;
- size_t alen = strlen(answer->key)+strlen(answer->value)+8;
- char *astr = tor_malloc(alen);
- tor_snprintf(astr, alen, "250-%s=%srn",
- answer->key, answer->value);
- smartlist_add(answers, astr);
- next = answer->next;
- tor_free(answer->key);
- tor_free(answer->value);
- tor_free(answer);
- answer = next;
- }
- }
- });
- if ((len = smartlist_len(unrecognized))) {
- for (i=0; i < len-1; ++i)
- connection_printf_to_buf(conn,
- "552-Unrecognized configuration key "%s"rn",
- (char*)smartlist_get(unrecognized, i));
- connection_printf_to_buf(conn,
- "552 Unrecognized configuration key "%s"rn",
- (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 OKrn", conn);
- }
- SMARTLIST_FOREACH(answers, char *, cp, tor_free(cp));
- smartlist_free(answers);
- SMARTLIST_FOREACH(questions, char *, cp, tor_free(cp));
- smartlist_free(questions);
- smartlist_free(unrecognized);
- tor_free(msg);
- return 0;
- }
- /** Called when we get a +LOADCONF message. */
- static int
- handle_control_loadconf(control_connection_t *conn, uint32_t len,
- const char *body)
- {
- setopt_err_t retval;
- char *errstring = NULL;
- const char *msg = NULL;
- (void) len;
- retval = options_init_from_string(body, CMD_RUN_TOR, NULL, &errstring);
- if (retval != SETOPT_OK) {
- log_warn(LD_CONTROL,
- "Controller gave us config file that didn't validate: %s",
- errstring);
- 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:
- tor_fragile_assert();
- break;
- }
- if (*errstring)
- connection_printf_to_buf(conn, "%s: %srn", msg, errstring);
- else
- connection_printf_to_buf(conn, "%srn", msg);
- tor_free(errstring);
- return 0;
- }
- send_control_done(conn);
- return 0;
- }
- /** Called when we get a SETEVENTS message: update conn->event_mask,
- * and reply with DONE or ERROR. */
- static int
- handle_control_setevents(control_connection_t *conn, uint32_t len,
- const char *body)
- {
- uint16_t event_code;
- uint32_t event_mask = 0;
- unsigned int extended = 0;
- smartlist_t *events = smartlist_create();
- (void) len;
- smartlist_split_string(events, body, " ",
- SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
- SMARTLIST_FOREACH_BEGIN(events, const char *, ev)
- {
- if (!strcasecmp(ev, "EXTENDED")) {
- extended = 1;
- 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;
- else if (!strcasecmp(ev, "STREAM_BW"))
- event_code = EVENT_STREAM_BANDWIDTH_USED;
- else if (!strcasecmp(ev, "CLIENTS_SEEN"))
- event_code = EVENT_CLIENTS_SEEN;
- else if (!strcasecmp(ev, "NEWCONSENSUS"))
- event_code = EVENT_NEWCONSENSUS;
- else {
- connection_printf_to_buf(conn, "552 Unrecognized event "%s"rn",
- ev);
- SMARTLIST_FOREACH(events, char *, e, tor_free(e));
- smartlist_free(events);
- return 0;
- }
- event_mask |= (1 << event_code);
- }
- SMARTLIST_FOREACH_END(ev);
- SMARTLIST_FOREACH(events, char *, e, tor_free(e));
- smartlist_free(events);
- conn->event_mask = event_mask;
- if (extended)
- conn->use_extended_events = 1;
- control_update_global_event_mask();
- send_control_done(conn);
- return 0;
- }
- /** 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.
- */
- smartlist_t *
- decode_hashed_passwords(config_line_t *passwords)
- {
- char decoded[64];
- 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;
- }
- } 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));
- }
- return sl;
- err:
- SMARTLIST_FOREACH(sl, char*, cp, tor_free(cp));
- smartlist_free(sl);
- return NULL;
- }
- /** 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
- handle_control_authenticate(control_connection_t *conn, uint32_t len,
- const char *body)
- {
- int used_quoted_string = 0;
- or_options_t *options = get_options();
- const char *errstr = NULL;
- char *password;
- size_t password_len;
- const char *cp;
- int i;
- int bad_cookie=0, bad_password=0;
- smartlist_t *sl = NULL;
- if (TOR_ISXDIGIT(body[0])) {
- cp = body;
- while (TOR_ISXDIGIT(*cp))
- ++cp;
- i = (int)(cp - body);
- tor_assert(i>0);
- password_len = i/2;
- password = tor_malloc(password_len + 1);
- if (base16_decode(password, password_len+1, body, i)<0) {
- connection_write_str_to_buf(
- "551 Invalid hexadecimal encoding. Maybe you tried a plain text "
- "password? If so, the standard requires that you put it in "
- "double quotes.rn", conn);
- connection_mark_for_close(TO_CONN(conn));
- tor_free(password);
- return 0;
- }
- } else if (TOR_ISSPACE(body[0])) {
- password = tor_strdup("");
- password_len = 0;
- } else {
- if (!decode_escaped_string(body, len, &password, &password_len)) {
- connection_write_str_to_buf("551 Invalid quoted string. You need "
- "to put the password in double quotes.rn", conn);
- connection_mark_for_close(TO_CONN(conn));
- return 0;
- }
- used_quoted_string = 1;
- }
- if (!options->CookieAuthentication && !options->HashedControlPassword &&
- !options->HashedControlSessionPassword) {
- /* if Tor doesn't demand any stronger authentication, then
- * the controller can get in with anything. */
- goto ok;
- }
- if (options->CookieAuthentication) {
- int also_password = options->HashedControlPassword != NULL ||
- options->HashedControlSessionPassword != NULL;
- if (password_len != AUTHENTICATION_COOKIE_LEN) {
- if (!also_password) {
- log_warn(LD_CONTROL, "Got authentication cookie with wrong length "
- "(%d)", (int)password_len);
- errstr = "Wrong length on authentication cookie.";
- goto err;
- }
- bad_cookie = 1;
- } else if (memcmp(authentication_cookie, password, password_len)) {
- if (!also_password) {
- log_warn(LD_CONTROL, "Got mismatched authentication cookie");
- errstr = "Authentication cookie did not match expected value.";
- goto err;
- }
- bad_cookie = 1;
- } else {
- goto ok;
- }
- }
- if (options->HashedControlPassword ||
- options->HashedControlSessionPassword) {
- int bad = 0;
- smartlist_t *sl_tmp;
- char received[DIGEST_LEN];
- int also_cookie = options->CookieAuthentication;
- 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) {
- if (!also_cookie) {
- log_warn(LD_CONTROL,
- "Couldn't decode HashedControlPassword: invalid base16");
- errstr="Couldn't decode HashedControlPassword value in configuration.";
- }
- bad_password = 1;
- SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
- smartlist_free(sl);
- } else {
- 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);
- 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;
- }
- }
- /** 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.";
- err:
- tor_free(password);
- connection_printf_to_buf(conn, "515 Authentication failed: %srn",
- errstr ? errstr : "Unknown reason.");
- connection_mark_for_close(TO_CONN(conn));
- return 0;
- ok:
- log_info(LD_CONTROL, "Authenticated control connection (%d)", conn->_base.s);
- send_control_done(conn);
- conn->_base.state = CONTROL_CONN_STATE_OPEN;
- tor_free(password);
- if (sl) { /* clean up */
- SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
- smartlist_free(sl);
- }
- return 0;
- }
- /** Called when we get a SAVECONF command. Try to flush the current options to
- * disk, and report success or failure. */
- static int
- handle_control_saveconf(control_connection_t *conn, uint32_t len,
- const char *body)
- {
- (void) len;
- (void) body;
- if (options_save_current()<0) {
- connection_write_str_to_buf(
- "551 Unable to write configuration to disk.rn", conn);
- } else {
- send_control_done(conn);
- }
- return 0;
- }
- /** 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.) */
- static int
- handle_control_signal(control_connection_t *conn, uint32_t len,
- const char *body)
- {
- int sig;
- int n = 0;
- char *s;
- (void) len;
- 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"rn",
- s);
- sig = -1;
- }
- tor_free(s);
- if (sig<0)
- return 0;
- send_control_done(conn);
- /* Flush the "done" first if the signal might make us shut down. */
- if (sig == SIGTERM || sig == SIGINT)
- connection_handle_write(TO_CONN(conn), 1);
- control_signal_act(sig);
- return 0;
- }
- /** Called when we get a MAPADDRESS command; try to bind all listed addresses,
- * and report success or failure. */
- static int
- handle_control_mapaddress(control_connection_t *conn, uint32_t len,
- const char *body)
- {
- smartlist_t *elts;
- smartlist_t *lines;
- smartlist_t *reply;
- char *r;
- size_t sz;
- (void) len; /* body is NUL-terminated, so it's safe to ignore the length. */
- lines = smartlist_create();
- elts = smartlist_create();
- reply = smartlist_create();
- smartlist_split_string(lines, body, " ",
- SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
- SMARTLIST_FOREACH(lines, char *, line,
- {
- tor_strlower(line);
- smartlist_split_string(elts, line, "=", 0, 2);
- if (smartlist_len(elts) == 2) {
- const char *from = smartlist_get(elts,0);
- const char *to = smartlist_get(elts,1);
- size_t anslen = strlen(line)+512;
- char *ans = tor_malloc(anslen);
- if (address_is_invalid_destination(to, 1)) {
- tor_snprintf(ans, anslen,
- "512-syntax error: invalid address '%s'", to);
- smartlist_add(reply, ans);
- log_warn(LD_CONTROL,
- "Skipping invalid argument '%s' in MapAddress msg", to);
- } else if (!strcmp(from, ".") || !strcmp(from, "0.0.0.0")) {
- const char *address = addressmap_register_virtual_address(
- !strcmp(from,".") ? RESOLVED_TYPE_HOSTNAME : RESOLVED_TYPE_IPV4,
- tor_strdup(to));
- if (!address) {
- tor_snprintf(ans, anslen,
- "451-resource exhausted: skipping '%s'", line);
- smartlist_add(reply, ans);
- log_warn(LD_CONTROL,
- "Unable to allocate address for '%s' in MapAddress msg",
- safe_str(line));
- } else {
- tor_snprintf(ans, anslen, "250-%s=%s", address, to);
- smartlist_add(reply, ans);
- }
- } else {
- addressmap_register(from, tor_strdup(to), 1, ADDRMAPSRC_CONTROLLER);
- tor_snprintf(ans, anslen, "250-%s", line);
- smartlist_add(reply, ans);
- }
- } else {
- 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);
- log_info(LD_CONTROL, "Skipping MapAddress '%s': wrong "
- "number of items.", safe_str(line));
- }
- 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);
- if (smartlist_len(reply)) {
- ((char*)smartlist_get(reply,smartlist_len(reply)-1))[3] = ' ';
- r = smartlist_join_strings(reply, "rn", 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.rn";
- connection_write_to_buf(response, strlen(response), TO_CONN(conn));
- }
- SMARTLIST_FOREACH(reply, char *, cp, tor_free(cp));
- smartlist_free(reply);
- return 0;
- }
- /** Implementation helper for GETINFO: knows the answers for various
- * trivial-to-implement questions. */
- static int
- getinfo_helper_misc(control_connection_t *conn, const char *question,
- char **answer)
- {
- (void) conn;
- if (!strcmp(question, "version")) {
- *answer = tor_strdup(get_version());
- } else if (!strcmp(question, "config-file")) {
- *answer = tor_strdup(get_torrc_fname());
- } else if (!strcmp(question, "info/names")) {
- *answer = list_getinfo_options();
- } else if (!strcmp(question, "events/names")) {
- *answer = tor_strdup("CIRC STREAM ORCONN BW DEBUG INFO NOTICE WARN ERR "
- "NEWDESC ADDRMAP AUTHDIR_NEWDESCS DESCCHANGED "
- "NS STATUS_GENERAL STATUS_CLIENT STATUS_SERVER "
- "GUARD STREAM_BW CLIENTS_SEEN NEWCONSENSUS");
- } else if (!strcmp(question, "features/names")) {
- *answer = tor_strdup("VERBOSE_NAMES EXTENDED_EVENTS");
- } else if (!strcmp(question, "address")) {
- uint32_t addr;
- if (router_pick_published_address(get_options(), &addr) < 0)
- return -1;
- *answer = tor_dup_ip(addr);
- } else if (!strcmp(question, "dir-usage")) {
- *answer = directory_dump_request_log();
- } else if (!strcmp(question, "fingerprint")) {
- routerinfo_t *me = router_get_my_routerinfo();
- 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);
- }
- return 0;
- }
- /** 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
- * necessarily have a valid signature.
- *
- * New code should never use this; it's for backward compatibility.
- *
- * NOTE: <b>ri_body</b> is as returned by signed_descriptor_get_body: it might
- * not be NUL-terminated. */
- 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++ = ' ';
- 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);
- }
- /** Implementation helper for GETINFO: knows the answers for questions about
- * directory information. */
- static int
- getinfo_helper_dir(control_connection_t *control_conn,
- const char *question, char **answer)
- {
- if (!strcmpstart(question, "desc/id/")) {
- routerinfo_t *ri = router_get_by_hexdigest(question+strlen("desc/id/"));
- if (ri) {
- const char *body = signed_descriptor_get_body(&ri->cache_info);
- if (body)
- *answer = tor_strndup(body, ri->cache_info.signed_descriptor_len);
- }
- } else if (!strcmpstart(question, "desc/name/")) {
- routerinfo_t *ri = router_get_by_nickname(question+strlen("desc/name/"),1);
- if (ri) {
- const char *body = signed_descriptor_get_body(&ri->cache_info);
- if (body)
- *answer = tor_strndup(body, ri->cache_info.signed_descriptor_len);
- }
- } 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,
- {
- const char *body = signed_descriptor_get_body(&ri->cache_info);
- 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);
- } 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(
- ri->cache_info.extra_info_digest);
- 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);
- } 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,
- ri->cache_info.annotations_len);
- }
- } 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);
- smartlist_free(descs);
- 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 = ' ';
- tor_free(url);
- smartlist_free(descs);
- } else if (!strcmpstart(question, "dir/status/")) {
- if (directory_permits_controller_requests(get_options())) {
- size_t len=0;
- char *cp;
- 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 = ' ';
- 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;
- char *fname = networkstatus_get_cache_filename(fp);
- s = read_file_to_str(fname, 0, NULL);
- if (s)
- smartlist_add(status_list, s);
- tor_free(fname);
- });
- 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);
- }
- } else if (!strcmp(question, "dir/status-vote/current/consensus")) { /* v3 */
- if (directory_caches_dir_info(get_options())) {
- const cached_dir_t *consensus = dirserv_get_consensus();
- 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 */
- routerlist_t *routerlist = router_get_routerlist();
- int verbose = control_conn->use_long_names;
- if (!routerlist || !routerlist->routers ||
- list_server_status_v1(routerlist->routers, answer,
- verbose ? 2 : 1) < 0) {
- return -1;
- }
- } else if (!strcmpstart(question, "extra-info/digest/")) {
- question += strlen("extra-info/digest/");
- if (strlen(question) == HEX_DIGEST_LEN) {
- char d[DIGEST_LEN];
- signed_descriptor_t *sd = NULL;
- 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);
- }
- if (sd) {
- const char *body = signed_descriptor_get_body(sd);
- if (body)
- *answer = tor_strndup(body, sd->signed_descriptor_len);
- }
- }
- }
- return 0;
- }
- /** Implementation helper for GETINFO: knows how to generate summaries of the
- * current states of things we send events about. */
- static int
- getinfo_helper_events(control_connection_t *control_conn,
- const char *question, char **answer)
- {
- if (!strcmp(question, "circuit-status")) {
- 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;
- const char *purpose;
- if (! CIRCUIT_IS_ORIGIN(circ) || circ->marked_for_close)
- continue;
- if (control_conn->use_long_names)
- path = circuit_list_path_for_controller(TO_ORIGIN_CIRCUIT(circ));
- else
- path = circuit_list_path(TO_ORIGIN_CIRCUIT(circ),0);
- if (circ->state == CIRCUIT_STATE_OPEN)
- state = "BUILT";
- else if (strlen(path))
- state = "EXTENDED";
- else
- state = "LAUNCHED";
- purpose = circuit_purpose_to_controller_string(circ->purpose);
- slen = strlen(path)+strlen(state)+strlen(purpose)+30;
- s = tor_malloc(slen+1);
- tor_snprintf(s, slen, "%lu %s%s%s PURPOSE=%s",
- (unsigned long)TO_ORIGIN_CIRCUIT(circ)->global_identifier,
- state, *path ? " " : "", path, purpose);
- smartlist_add(status, s);
- tor_free(path);
- }
- *answer = smartlist_join_strings(status, "rn", 0, NULL);
- SMARTLIST_FOREACH(status, char *, cp, tor_free(cp));
- smartlist_free(status);
- } else if (!strcmp(question, "stream-status")) {
- smartlist_t *conns = get_connection_array();
- smartlist_t *status = smartlist_create();
- char buf[256];
- SMARTLIST_FOREACH_BEGIN(conns, connection_t *, base_conn) {
- const char *state;
- edge_connection_t *conn;
- char *s;
- size_t slen;
- circuit_t *circ;
- origin_circuit_t *origin_circ = NULL;
- 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)
- continue;
- conn = TO_EDGE_CONN(base_conn);
- switch (conn->_base.state)
- {
- case AP_CONN_STATE_CONTROLLER_WAIT:
- case AP_CONN_STATE_CIRCUIT_WAIT:
- if (conn->socks_request &&
- SOCKS_COMMAND_IS_RESOLVE(conn->socks_request->command))
- 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:
- log_warn(LD_BUG, "Asked for stream in unknown state %d",
- conn->_base.state);
- continue;
- }
- circ = circuit_get_by_edge_conn(conn);
- if (circ && CIRCUIT_IS_ORIGIN(circ))
- origin_circ = TO_ORIGIN_CIRCUIT(circ);
- write_stream_target_to_buf(conn, buf, sizeof(buf));
- slen = strlen(buf)+strlen(state)+32;
- s = tor_malloc(slen+1);
- tor_snprintf(s, slen, "%lu %s %lu %s",
- (unsigned long) conn->_base.global_identifier,state,
- origin_circ?
- (unsigned long)origin_circ->global_identifier : 0ul,
- buf);
- smartlist_add(status, s);
- } SMARTLIST_FOREACH_END(base_conn);
- *answer = smartlist_join_strings(status, "rn", 0, NULL);
- SMARTLIST_FOREACH(status, char *, cp, tor_free(cp));
- smartlist_free(status);
- } else if (!strcmp(question, "orconn-status")) {
- smartlist_t *conns = get_connection_array();
- smartlist_t *status = smartlist_create();
- SMARTLIST_FOREACH(conns, connection_t *, base_conn,
- {
- const char *state;
- char *s;
- char name[128];
- size_t slen;
- or_connection_t *conn;
- if (base_conn->type != CONN_TYPE_OR || base_conn->marked_for_close)
- continue;
- conn = TO_OR_CONN(base_conn);
- if (conn->_base.state == OR_CONN_STATE_OPEN)
- state = "CONNECTED";
- else if (conn->nickname)
- state = "LAUNCHED";
- else
- state = "NEW";
- orconn_target_get_name(control_conn->use_long_names, name, sizeof(name),
- conn);
- slen = strlen(name)+strlen(state)+2;
- s = tor_malloc(slen+1);
- tor_snprintf(s, slen, "%s %s", name, state);
- smartlist_add(status, s);
- });
- *answer = smartlist_join_strings(status, "rn", 0, NULL);
- SMARTLIST_FOREACH(status, char *, cp, tor_free(cp));
- smartlist_free(status);
- } else if (!strcmpstart(question, "addr-mappings/") ||
- !strcmpstart(question, "address-mappings/")) {
- time_t min_e, max_e;
- smartlist_t *mappings;
- int want_expiry = !strcmpstart(question, "address-mappings/");
- if (!strcmpstart(question, "addr-mappings/")) {
- /* XXXX022 This has been deprecated since 0.2.0.3-alpha, and has
- generated a warning since 0.2.1.10-alpha; remove late in 0.2.2.x. */
- log_warn(LD_CONTROL, "Controller used obsolete addr-mappings/ GETINFO "
- "key; use address-mappings/ instead.");
- }
- question += strlen(want_expiry ? "address-mappings/"
- : "addr-mappings/");
- if (!strcmp(question, "all")) {
- min_e = 0; max_e = TIME_MAX;
- } else if (!strcmp(question, "cache")) {
- min_e = 2; max_e = TIME_MAX;
- } else if (!strcmp(question, "config")) {
- min_e = 0; max_e = 0;
- } else if (!strcmp(question, "control")) {
- min_e = 1; max_e = 1;
- } else {
- return 0;
- }
- mappings = smartlist_create();
- addressmap_get_mappings(mappings, min_e, max_e, want_expiry);
- *answer = smartlist_join_strings(mappings, "rn", 0, NULL);
- SMARTLIST_FOREACH(mappings, char *, cp, tor_free(cp));
- smartlist_free(mappings);
- } else if (!strcmpstart(question, "status/")) {
- /* 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. */
- if (!strcmp(question, "status/circuit-established")) {
- *answer = tor_strdup(has_completed_circuit ? "1" : "0");
- } else if (!strcmp(question, "status/enough-dir-info")) {
- *answer = tor_strdup(router_have_minimum_dir_info() ? "1" : "0");
- } 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. */
- *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);
- } else if (!strcmp(question, "status/bootstrap-phase")) {
- *answer = tor_strdup(last_sent_bootstrap_message);
- } else if (!strcmpstart(question, "status/version/")) {
- int is_server = server_mode(get_options());
- networkstatus_t *c = networkstatus_get_latest_consensus();
- 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;
- }
- if (!strcmp(question, "status/version/recommended")) {
- *answer = tor_strdup(recommended);
- return 0;
- }
- if (!strcmp(question, "status/version/current")) {
- switch (status)
- {
- 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;
- case VS_EMPTY: *answer = tor_strdup("none recommended"); break;
- case VS_UNKNOWN: *answer = tor_strdup("unknown"); break;
- default: tor_fragile_assert();
- }
- } else if (!strcmp(question, "status/version/num-versioning") ||
- !strcmp(question, "status/version/num-concurring")) {
- char s[33];
- tor_snprintf(s, sizeof(s), "%d", get_n_authorities(V3_AUTHORITY));
- *answer = tor_strdup(s);
- log_warn(LD_GENERAL, "%s is deprecated; it no longer gives useful "
- "information", question);
- }
- } else if (!strcmp(question, "status/clients-seen")) {
- char geoip_start[ISO_TIME_LEN+1];
- size_t answer_len;
- char *geoip_summary = extrainfo_get_client_geoip_summary(time(NULL));
- if (!geoip_summary)
- return -1;
- answer_len = strlen("TimeStarted="" CountrySummary=") +
- ISO_TIME_LEN + strlen(geoip_summary) + 1;
- *answer = tor_malloc(answer_len);
- format_iso_time(geoip_start, geoip_get_history_start());
- tor_snprintf(*answer, answer_len,
- "TimeStarted="%s" CountrySummary=%s",
- geoip_start, geoip_summary);
- tor_free(geoip_summary);
- } else {
- return 0;
- }
- }
- return 0;
- }
- /** 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.
- */
- typedef int (*getinfo_helper_t)(control_connection_t *,
- const char *q, char **a);
- /** A single item for the GETINFO question-to-answer-function table. */
- typedef struct getinfo_item_t {
- const char *varname; /**< The value (or prefix) of the question. */
- 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? */
- } 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 }
- /** Table mapping questions accepted by GETINFO to the functions that know how
- * to answer them. */
- static const getinfo_item_t getinfo_items[] = {
- ITEM("version", misc, "The current version of Tor."),
- ITEM("config-file", misc, "Current location of the "torrc" file."),
- ITEM("accounting/bytes", accounting,
- "Number of bytes read/written so far in the accounting interval."),
- ITEM("accounting/bytes-left", accounting,
- "Number of bytes left to write/read so far in the accounting interval."),
- ITEM("accounting/enabled", accounting, "Is accounting currently enabled?"),
- ITEM("accounting/hibernating", accounting, "Are we hibernating or awake?"),
- ITEM("accounting/interval-start", accounting,
- "Time when the accounting period starts."),
- ITEM("accounting/interval-end", accounting,
- "Time when the accounting period ends."),
- ITEM("accounting/interval-wake", accounting,
- "Time to wake up in this accounting period."),
- ITEM("helper-nodes", entry_guards, NULL), /* deprecated */
- ITEM("entry-guards", entry_guards,
- "Which nodes are we using as entry guards?"),
- ITEM("fingerprint", misc, NULL),
- 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."),
- ITEM("desc/all-recent-extrainfo-hack", dir, NULL), /* Hack. */
- PREFIX("extra-info/digest/", dir, "Extra-info documents by digest."),
- ITEM("ns/all", networkstatus,
- "Brief summary of router status (v2 directory format)"),
- PREFIX("ns/id/", networkstatus,
- "Brief summary of router status by ID (v2 directory format)."),
- PREFIX("ns/name/", networkstatus,
- "Brief summary of router status by nickname (v2 directory format)."),
- PREFIX("ns/purpose/", networkstatus,
- "Brief summary of router status by purpose (v2 directory format)."),
- PREFIX("unregistered-servers-", dirserv_unregistered, NULL),
- ITEM("network-status", dir,
- "Brief summary of router status (v1 directory format)"),
- ITEM("circuit-status", events, "List of current circuits originating here."),
- ITEM("stream-status", events,"List of current streams."),
- ITEM("orconn-status", events, "A list of current OR connections."),
- PREFIX("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."),
- PREFIX("addr-mappings/", events, NULL),
- DOC("addr-mappings/all", "Current address mappings without expiry times."),
- DOC("addr-mappings/cache",
- "Current cached DNS replies without expiry times."),
- DOC("addr-mappings/config",
- "Current address mappings from configuration without expiry times."),
- DOC("addr-mappings/control",
- "Current address mappings from controller without expiry times."),
- PREFIX("status/", events, NULL),
- DOC("status/circuit-established",
- "Whether we think client functionality is working."),
- DOC("status/enough-dir-info",
- "Whether we have enough up-to-date directory information to build "
- "circuits."),
- DOC("status/bootstrap-phase",
- "The last bootstrap phase status event that Tor sent."),
- DOC("status/clients-seen",
- "Breakdown of client countries seen by a bridge."),
- 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"),
- ITEM("address", misc, "IP address of this Tor host, if we can guess it."),
- ITEM("dir-usage", misc, "Breakdown of bytes transferred over DirPort."),
- PREFIX("desc-annotations/id/", dir, "Router annotations by hexdigest."),
- PREFIX("dir/server/", dir,"Router descriptors as retrieved from a DirPort."),
- 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."),
- PREFIX("exit-policy/default", policies,
- "The default value appended to the configured exit policy."),
- PREFIX("ip-to-country/", geoip, "Perform a GEOIP lookup"),
- { NULL, NULL, NULL, 0 }
- };
- /** Allocate and return a list of recognized GETINFO options. */
- 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 -- %sn",
- 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;