control-spec.txt 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. $Id$
  2. TC: A Tor control protocol
  3. 0. Scope
  4. This document describes an implementation-specific protocol that is used
  5. for other programs (such as frontend user-interfaces) to communicate
  6. with a locally running Tor process. It is not part of the Tor onion
  7. routing protocol.
  8. We're trying to be pretty extensible here, but not infinitely
  9. forward-compatible.
  10. 1. Protocol outline
  11. TC is a bidirectional message-based protocol. It assumes an underlying
  12. stream for communication between a controlling process (the "client") and
  13. a Tor process (the "server"). The stream may be implemented via TCP,
  14. TLS-over-TCP, a Unix-domain socket, or so on, but it must provide
  15. reliable in-order delivery. For security, the stream should not be
  16. accessible by untrusted parties.
  17. In TC, the client and server send typed variable-length messages to each
  18. other over the underlying stream. By default, all messages from the server
  19. are in response to messages from the client. Some client requests, however,
  20. will cause the server to send messages to the client indefinitely far into
  21. the future.
  22. Servers respond to messages in the order they're received.
  23. 2. Message format
  24. The messages take the following format:
  25. Length [2 octets; big-endian]
  26. Type [2 octets; big-endian]
  27. Body [Length octets]
  28. Upon encountering a recognized Type, implementations behave as described in
  29. section 3 below. If the type is not recognized, servers respond with a
  30. "STAT" message (code UNRECOGNIZED; see 3.1 below), and clients simply ignore
  31. the message.
  32. 3. Message types
  33. 3.1. ERROR (Type 0x0000)
  34. Sent in response to a message that could not be processed as requested.
  35. The body of the message begins with a 2-byte error code. The following
  36. values are defined:
  37. 0x0000 Unspecified error
  38. []
  39. 0x0001 Internal error
  40. [Something went wrong inside Tor, so that the client's
  41. request couldn't be fulfilled.]
  42. 0x0002 Unrecognized message type
  43. [The client sent a message type we don't understand.]
  44. 0x0003 Syntax error
  45. [The client sent a message body in a format we can't parse.]
  46. 0x0004 Unrecognized configuration key
  47. [The client tried to get or set a configuration option we don't
  48. recognize.]
  49. 0x0005 Invalid configuration value
  50. [The client tried to set a configuration option to an
  51. incorrect, ill-formed, or impossible value.]
  52. 0x0006 Unrecognized event code
  53. [The client tried to set an event code that we don't recognize.]
  54. 0x0007 Unauthorized.
  55. [The client tried to send a command that requires
  56. authorization, but it hasn't sent a valid AUTHENTICATE message.]
  57. 0x0008 Failed authentication attempt
  58. [The client sent a well-formed authorization message.]
  59. The rest of the body should be a human-readable description of the error.
  60. In general, new error codes should only be added when they don't fall under
  61. one of the existing error codes.
  62. 3.2. DONE (Type 0x0001)
  63. Sent from server to client in response to a request that was successfully
  64. completed, with no more information needed. The body is empty.
  65. 3.3. SETCONF (Type 0x0002)
  66. Change the value of a configuration variable. The body contains a list of
  67. newline-terminated key-value configuration lines.
  68. The server behaves as though it had just read the key-value pair in its
  69. configuration file.
  70. The server responds with a DONE message on success, or an ERROR message on
  71. failure.
  72. When a configuration options takes multiple values, or when multiple
  73. configuration keys form a context-sensitive group (see below), then
  74. setting _any_ of the options in a SETCONF command is taken to reset all of
  75. the others. For example, if two ORBindAddress values are configured,
  76. and a SETCONF command arrives containing a single ORBindAddress value, the
  77. new command's value replaces the two old values.
  78. To _remove_ all settings for a given option entirely (and go back to its
  79. default value), send a single line containing the key and no value.
  80. 3.4. GETCONF (Type 0x0003)
  81. Request the value of a configuration variable. The body contains one or
  82. more NL-terminated strings for configuration keys. The server replies
  83. with a CONFVALUE message.
  84. If an option appears multiple times in the configuration, all of its
  85. key-value pairs are returned in order.
  86. Some options are context-sensitive, and depend on other options with
  87. different keywords. These cannot be fetched directly. Currently there
  88. is only one such option: clients should use the "HiddenServiceOptions"
  89. virtual keyword to get all HiddenServiceDir, HiddenServicePort,
  90. HiddenServiceNodes, and HiddenServiceExcludeNodes option settings.
  91. 3.5. CONFVALUE (Type 0x0004)
  92. Sent in response to a GETCONF message; contains a list of "Key Value\n"
  93. (A non-whitespace keyword, a single space, a non-NL value, a NL)
  94. strings.
  95. 3.6. SETEVENTS (Type 0x0005)
  96. Request the server to inform the client about interesting events.
  97. The body contains a list of 2-byte event codes (see "event" below).
  98. Sending SETEVENTS with an empty body turns off all event reporting.
  99. The server responds with a DONE message on success, and an ERROR message
  100. if one of the event codes isn't recognized. (On error, the list of active
  101. event codes isn't changed.)
  102. 3.7. EVENT (Type 0x0006)
  103. Sent from the server to the client when an event has occurred and the
  104. client has requested that kind of event. The body contains a 2-byte
  105. event code followed by additional event-dependent information. Event
  106. codes are:
  107. 0x0001 -- Circuit status changed
  108. Status [1 octet]
  109. (Launched=0,Built=1,Extended=2,Failed=3,Closed=4)
  110. Circuit ID [4 octets]
  111. (Must be unique to Tor process/time)
  112. Path [NUL-terminated comma-separated string]
  113. (For extended/failed, is the portion of the path that is
  114. built)
  115. 0x0002 -- Stream status changed
  116. Status [1 octet]
  117. (Sent connect=0,sent resolve=1,succeeded=2,failed=3,
  118. closed=4)
  119. Stream ID [4 octets]
  120. (Must be unique to Tor process/time)
  121. Target (NUL-terminated address-port string]
  122. 0x0003 -- OR Connection status changed
  123. Status [1 octet]
  124. (Launched=0,connected=1,failed=2,closed=3)
  125. OR nickname/identity [NUL-terminated]
  126. 0x0004 -- Bandwidth used in the last second
  127. Bytes read [4 octets]
  128. Bytes written [4 octets]
  129. 0x0005 -- Notice/warning/error occurred
  130. Message [NUL-terminated]
  131. 3.8. AUTHENTICATE (Type 0x0007)
  132. Sent from the client to the server. Contains a 'magic cookie' to prove
  133. that client is really the admin for this Tor process. The server responds
  134. with DONE or ERROR.
  135. 3.9. SAVECONF (Type 0x0008)
  136. Sent from the client to the server. Instructs the server to write out
  137. its config options into its torrc. Server returns DONE if successful, or
  138. ERROR if it can't write the file or some other error occurs.
  139. 4. Implementation notes
  140. 4.1. There are four ways we could authenticate, for now:
  141. 1) Listen on 127.0.0.1; trust all local users.
  142. 2) Write a named socket in tor's data-directory or in some other location;
  143. rely on the OS to ensure that only authorized users can open it. (NOTE:
  144. the Linux unix(7) man page suggests that some BSDs don't enforce
  145. authorization.) If the OS has named sockets, and implements
  146. authentication, trust all users who can read Tor's data directory.
  147. 3) Write a random magic cookie to the FS in Tor's data-directory; use that
  148. magic cookie for authentication. Trust all users who can read Tor's data
  149. directory.
  150. 4) Store a salted-and-hashed passphrase in Tor's configuration. Use the
  151. passphrase for authentication. Trust all users who know the passphrase.
  152. On Win32, our only options are 1, 3, and 4. Since the semantics for 2
  153. and 3 are so similar, we chose to not support 2, and just always bind
  154. on 127.0.0.1. We've implemented 1, 3, and 4.
  155. By default, the Tor client accepts authentication approach #1. If
  156. the controller wants Tor to demand more authentication, it should use
  157. setconf and saveconf to configure Tor to demand more next time.
  158. 4.2. Don't let the buffer get too big.
  159. If you ask for lots of events, and 16MB of them queue up on the buffer,
  160. the Tor process will close the socket.
  161. -----------
  162. (for emacs)
  163. Local Variables:
  164. mode:text
  165. indent-tabs-mode:nil
  166. fill-column:77
  167. End: