control-spec.txt 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. 0x0001 Unrecognized message type
  39. 0x0002 Unrecognized configuration key
  40. 0x0003 Invalid configuration value
  41. 0x0004 Unrecognized event code
  42. 0x0005 Unauthorized user
  43. 0x0006 Failed authentication attempt
  44. The rest of the body should be a human-readable description of the error.
  45. 3.2. DONE (Type 0x0001)
  46. Sent from server to client in response to a request that was successfully
  47. completed, with no more information needed. The body is empty.
  48. 3.3. SETCONF (Type 0x0002)
  49. Change the value of a configuration variable. The body contains a list of
  50. newline-terminated key-value configuration lines.
  51. The server behaves as though it had just read the key-value pair in its
  52. configuration file.
  53. The server responds with a DONE message on success, or an ERROR message on
  54. failure.
  55. When a configuration options takes multiple values, or when multiple
  56. configuration keys form a context-sensitive group (see below), then
  57. setting _any_ of the options in a SETCONF command is taken to reset all of
  58. the others. For example, if two ORBindAddress values are configured,
  59. and a SETCONF command arrives containing a single ORBindAddress value, the
  60. new command's value replaces the two old values.
  61. To _remove_ all settings for a given option entirely (and go back to its
  62. default value), send a single line containing the key and no value.
  63. 3.4. GETCONF (Type 0x0003)
  64. Request the value of a configuration variable. The body contains one or
  65. more NL-terminated strings for configuration keys. The server replies
  66. with a CONFVALUE message.
  67. If an option appears multiple times in the configuration, all of its
  68. key-value pairs are returned in order.
  69. Some options are context-sensitive, and depend on other options with
  70. different keywords. These cannot be fetched directly. Currently there
  71. is only one such option: clients should use the "HiddenServiceOptions"
  72. virtual keyword to get all HiddenServiceDir, HiddenServicePort,
  73. HiddenServiceNodes, and HiddenServiceExcludeNodes option settings.
  74. 3.5. CONFVALUE (Type 0x0004)
  75. Sent in response to a GETCONF message; contains a list of "Key Value\n"
  76. (A non-whitespace keyword, a single space, a non-NL value, a NL)
  77. strings.
  78. 3.6. SETEVENTS (Type 0x0005)
  79. Request the server to inform the client about interesting events.
  80. The body contains a list of 2-byte event codes (see "event" below).
  81. Sending SETEVENTS with an empty body turns off all event reporting.
  82. The server responds with a DONE message on success, and an ERROR message
  83. if one of the event codes isn't recognized. (On error, the list of active
  84. event codes isn't changed.)
  85. 3.7. EVENT (Type 0x0006)
  86. Sent from the server to the client when an event has occurred and the
  87. client has requested that kind of event. The body contains a 2-byte
  88. event code followed by additional event-dependent information. Event
  89. codes are:
  90. 0x0001 -- Circuit status changed
  91. Status [1 octet]
  92. (Launched=0,Built=1,Extended=2,Failed=3,Closed=4)
  93. Circuit ID [4 octets]
  94. (Must be unique to Tor process/time)
  95. Path [NUL-terminated comma-separated string]
  96. (For extended/failed, is the portion of the path that is
  97. built)
  98. 0x0002 -- Stream status changed
  99. Status [1 octet]
  100. (Sent connect=0,sent resolve=1,succeeded=2,failed=3,
  101. closed=4)
  102. Stream ID [4 octets]
  103. (Must be unique to Tor process/time)
  104. Target (NUL-terminated address-port string]
  105. 0x0003 -- OR Connection status changed
  106. Status [1 octet]
  107. (Launched=0,connected=1,failed=2,closed=3)
  108. OR nickname/identity [NUL-terminated]
  109. 0x0004 -- Bandwidth used in last N seconds.
  110. Number of seconds covered in interval [2 octets]
  111. Bytes read [4 octets]
  112. Bytes written [4 octets]
  113. 0x0005 -- Warning/error occurred
  114. Message [NUL-terminated]
  115. 3.8. AUTHENTICATE (Type 0x0007)
  116. Sent from the client to the server. Contains a 'magic cookie' to prove
  117. that client is really the admin for this Tor process. The server responds
  118. with DONE or ERROR.
  119. 3.9. SAVECONF (Type 0x0008)
  120. Sent from the client to the server. Instructs the server to write out
  121. its config options into its torrc. Server returns DONE if successful, or
  122. ERROR if it can't write the file or some other error occurs.
  123. 4. Implementation notes
  124. 4.1. There are four ways we could authenticate, for now:
  125. 1) Listen on 127.0.0.1; trust all local users.
  126. 2) Write a named socket in tor's data-directory or in some other location;
  127. rely on the OS to ensure that only authorized users can open it. (NOTE:
  128. the Linux unix(7) man page suggests that some BSDs don't enforce
  129. authorization.) If the OS has named sockets, and implements
  130. authentication, trust all users who can read Tor's data directory.
  131. 3) Write a random magic cookie to the FS in Tor's data-directory; use that
  132. magic cookie for authentication. Trust all users who can read Tor's data
  133. directory.
  134. 4) Store a salted-and-hashed passphrase in Tor's configuration. Use the
  135. passphrase for authentication. Trust all users who know the passphrase.
  136. On Win32, our only options are 1, 3, and 4. Since the semantics for 2
  137. and 3 are so similar, we chose to not support 2, and just always bind
  138. on 127.0.0.1. We've implemented 1, 3, and 4.
  139. By default, the Tor client accepts authentication approach #1. If
  140. the controller wants Tor to demand more authentication, it should use
  141. setconf and saveconf to configure Tor to demand more next time.
  142. 4.2. Don't let the buffer get too big.
  143. If you ask for lots of events, and 16MB of them queue up on the buffer,
  144. the Tor process will close the socket.
  145. -----------
  146. (for emacs)
  147. Local Variables:
  148. mode:text
  149. indent-tabs-mode:nil
  150. fill-column:77
  151. End: