control-spec.txt 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. $Id$
  2. TC: A Tor control protocol (Version 1)
  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 with a
  6. locally running Tor process. It is not part of the Tor onion routing
  7. protocol.
  8. This protocol replaces version 0 of TC, which is now deprecated. For
  9. reference, TC is described in "control-spec-v0.txt". Implementors are
  10. recommended to avoid using TC directly, but instead to use a library that
  11. can easily be updated to use the newer protocol.
  12. 1 Protocol outline
  13. TC is a bidirectional message-based protocol. It assumes an underlying
  14. stream for communication between a controlling process (the "client"
  15. or "controller") and a Tor process (or "server"). The stream may be
  16. implemented via TCP, TLS-over-TCP, a Unix-domain socket, or so on,
  17. but it must provide reliable in-order delivery. For security, the
  18. stream should not be accessible by untrusted parties.
  19. In TC, the client and server send typed messages to each other over the
  20. underlying stream. The client sends "commands" and the server sends
  21. "replies".
  22. By default, all messages from the server are in response to messages from
  23. the client. Some client requests, however, will cause the server to send
  24. messages to the client indefinitely far into the future. Such
  25. "asynchronous" replies are marked as such.
  26. Servers respond to messages in the order messages are received.
  27. 2 Message format
  28. 2.1 Description format
  29. The message formats listed below use ABNF as described in RFC2234.
  30. The protocol itself is loosely based on SMTP (see RFC 2821).
  31. We use the following nonterminals from RFC2822: atom, qcontent
  32. We define the following general-use nonterminals:
  33. String = DQUOTE *qcontent DQUOTE
  34. There are explicitly no limits on line length. All 8-bit characters are
  35. permitted unless explicitly disallowed.
  36. 2.2 Commands from controller to Tor
  37. Command = Keyword Arguments CRLF / "+" Keyword Arguments CRLF Data
  38. Keyword = 1*ALPHA
  39. Arguments = *(SP / VCHAR)
  40. Specific commands and their arguments are described below in section 3.
  41. 2.3 Replies from Tor to the controller
  42. Reply = *(MidReplyLine / DataReplyLine) EndReplyLine
  43. MidReplyLine = "-" ReplyLine
  44. DataReplyLine = "+" ReplyLine Data
  45. EndReplyLine = SP ReplyLine
  46. ReplyLine = StatusCode [ SP ReplyText ] CRLF
  47. ReplyText = XXXX
  48. StatusCode = XXXX
  49. Specific replies are mentioned below in section 3, and described more fully
  50. in section 4.
  51. 2.4 General-use tokens
  52. ; Identifiers for servers.
  53. ServerID = Nickname / Fingerprint
  54. Nickname = 1*NicknameChar
  55. [XXX perhaps this should be 1*19 NicknameChar ? -RD]
  56. NicknameChar = "a"-"z" / "A"-"Z" / "0" - "9"
  57. Fingerprint = "$" 40*HEXDIG
  58. ; Unique identifiers for streams or circuits. Currently, Tor only
  59. ; uses digits, but this may change
  60. StreamID = 1*16 IDChar
  61. CircuitID = 1*16 IDChar
  62. IDChar = ALPHA / DIGIT
  63. Address = ip4-address / ip6-address / hostname (XXXX Define these)
  64. ; A "Data" section is a sequence of octets concluded by the terminating
  65. ; sequence CRLF "." CRLF. The terminating sequence may not appear in the
  66. ; body of the data. Leading periods on lines in the data are escaped with
  67. ; an additional leading period as in RFC2821 section 4.5.2
  68. Data = *DataLine "." CRLF
  69. DataLine = CRLF / "." 1*LineItem CRLF/ NonDotItem *LineItem CRLF
  70. LineItem = NonCR / 1*CR NonCRLF
  71. NonDotItem = NonDotCR / 1*CR NonCRLF
  72. 3 Commands
  73. All commands and other keywords are case-insensitive.
  74. 3.1 SETCONF
  75. Change the value of one or more configuration variables. The syntax is:
  76. "SETCONF" 1*(SP keyword ["=" String]) CRLF
  77. Tor behaves as though it had just read each of the key-value pairs
  78. from its configuration file. Keywords with no corresponding values have
  79. their configuration values reset to their defaults. SETCONF is
  80. all-or-nothing: if there is an error in any of the configuration settings,
  81. Tor sets none of them.
  82. Tor responds with a "250 configuration values set" reply on success.
  83. Tor responds with a "513 syntax error in configuration values" reply on
  84. syntax error, or a "553 impossible configuration setting" reply on a
  85. semantic error.
  86. When a configuration option takes multiple values, or when multiple
  87. configuration keys form a context-sensitive group (see GETCONF below), then
  88. setting _any_ of the options in a SETCONF command is taken to reset all of
  89. the others. For example, if two ORBindAddress values are configured, and a
  90. SETCONF command arrives containing a single ORBindAddress value, the new
  91. command's value replaces the two old values.
  92. To _remove_ all settings for a given option entirely (and go back to its
  93. default value), send a single line containing the key and no value.
  94. 3.2 GETCONF
  95. Request the value of a configuration variable. The syntax is:
  96. "GETCONF" 1*(SP keyword) CRLF
  97. If all of the listed keywords exist in the Tor configuration, Tor replies
  98. with a series of reply lines of the form:
  99. 250 keyword=value
  100. If some of the listed keywords can't be found, Tor replies with a
  101. "552 unknown configuration keyword" message.
  102. If an option appears multiple times in the configuration, all of its
  103. key-value pairs are returned in order.
  104. Some options are context-sensitive, and depend on other options with
  105. different keywords. These cannot be fetched directly. Currently there
  106. is only one such option: clients should use the "HiddenServiceOptions"
  107. virtual keyword to get all HiddenServiceDir, HiddenServicePort,
  108. HiddenServiceNodes, and HiddenServiceExcludeNodes option settings.
  109. 3.3 SETEVENTS
  110. Request the server to inform the client about interesting events. The
  111. syntax is:
  112. "SETEVENTS" *(SP EventCode) CRLF
  113. EventCode = "CIRC" / "STREAM" / "ORCONN" / "BW" / "DEBUG" /
  114. "INFO" / "NOTICE" / "WARN" / "ERR" / "NEWDESC"
  115. [XXX We should have an "ADDRESSMAP" event to hear when we learn
  116. about dns resolves, etc, so the controller can keep synced. -RD]
  117. Any events *not* listed in the SETEVENTS line are turned off; thus, sending
  118. SETEVENTS with an empty body turns off all event reporting.
  119. The server responds with a "250 OK" reply on success, and a "552
  120. Unrecognized event" reply if one of the event codes isn't recognized. (On
  121. error, the list of active event codes isn't changed.)
  122. 3.4 AUTHENTICATE
  123. Sent from the client to the server. The syntax is:
  124. "AUTHENTICATE" SP 1*HEXDIG / QuotedString CRLF
  125. The server responds with "250 OK" on success or "515 Bad authentication" if
  126. the authentication cookie is incorrect.
  127. The format of the 'cookie' is implementation-dependent; see 5.1 below for
  128. information on how the standard Tor implementation handles it.
  129. If Tor requires authentication and the controller has not yet sent an
  130. AUTHENTICATE message, Tor sends a "514 authentication required" reply to
  131. any other kind of message.
  132. 3.5 SAVECONF
  133. Sent from the client to the server. The syntax is:
  134. "SAVECONF" CRLF
  135. Instructs the server to write out its config options into its torrc. Server
  136. returns "250 OK" if successful, or "551 Unable to write configuration
  137. to disk" if it can't write the file or some other error occurs.
  138. 3.6 SIGNAL
  139. Sent from the client to the server. The syntax is:
  140. "SIGNAL" SP Signal CRLF
  141. Signal = "RELOAD" / "SHUTDOWN" / "DUMP" / "DEBUG" / "TERM"
  142. The meaning of the signals are:
  143. RELOAD -- Reload: reload config items, refetch directory. (like HUP)
  144. SHUTDOWN -- Controlled shutdown: if server is an OP, exit immediately.
  145. If it's an OR, close listeners and exit after 30 seconds.
  146. (like INT)
  147. DUMP -- Dump stats: log information about open connections and
  148. circuits. (like USR1)
  149. DEBUG -- Debug: switch all open logs to loglevel debug. (like USR2)
  150. TERM -- Immediate shutdown: clean up and exit now. (like TERM)
  151. The server responds with "250 OK" if the signal is recognized (or simply
  152. closes the socket if it was asked to close immediately), or "552
  153. Unrecognized signal" if the signal is unrecognized.
  154. 3.7 MAPADDRESS
  155. Sent from the client to the server. The syntax is:
  156. "MAPADDRESS" 1*(Address "=" Address SP) CRLF
  157. The first address in each pair is an "original" address; the second is a
  158. "replacement" address. The client sends this message to the server in
  159. order to tell it that future SOCKS requests for connections to the original
  160. address should be replaced with connections to the specified replacement
  161. address. If the addresses are well-formed, and the server is able to
  162. fulfill the request, the server replies with a 250 message:
  163. 250-OldAddress1=NewAddress1
  164. 250 OldAddress2=NewAddress2
  165. containing the source and destination addresses. If request is malformed,
  166. the server replies with "512 syntax error in command argument". If the server
  167. can't fulfill the request, it replies with "451 resource exhausted."
  168. The client may decline to provide a body for the original address, and
  169. instead send a special null address ("0.0.0.0" for IPv4, "::0" for IPv6, or
  170. "." for hostname), signifying that the server should choose the original
  171. address itself, and return that address in the reply. The server
  172. should ensure that it returns an element of address space that is unlikely
  173. to be in actual use. If there is already an address mapped to the
  174. destination address, the server may reuse that mapping.
  175. If the original address is already mapped to a different address, the old
  176. mapping is removed. If the original address and the destination address
  177. are the same, the server removes any mapping in place for the original
  178. address.
  179. Example:
  180. C: MAPADDRESS 0.0.0.0=tor.eff.org 1.2.3.4=tor.freehaven.net
  181. S: 250-127.192.10.10=tor.eff.org
  182. S: 250 1.2.3.4=tor.freehaven.net
  183. {Note: This feature is designed to be used to help Tor-ify applications
  184. that need to use SOCKS4 or hostname-less SOCKS5. There are three
  185. approaches to doing this:
  186. 1. Somehow make them use SOCKS4a or SOCKS5-with-hostnames instead.
  187. 2. Use tor-resolve (or another interface to Tor's resolve-over-SOCKS
  188. feature) to resolve the hostname remotely. This doesn't work
  189. with special addresses like x.onion or x.y.exit.
  190. 3. Use MAPADDRESS to map an IP address to the desired hostname, and then
  191. arrange to fool the application into thinking that the hostname
  192. has resolved to that IP.
  193. This functionality is designed to help implement the 3rd approach.}
  194. Mappings set by the controller last until the Tor process exits:
  195. they never expire. If the controller wants the mapping to last only
  196. a certain time, then it must explicitly un-map the address when that
  197. time has elapsed.
  198. 3.8 GETINFO
  199. Sent from the client to the server. The syntax is as for GETCONF:
  200. "GETINFO" 1*(SP keyword) CRLF
  201. one or more NL-terminated strings. The server replies with an INFOVALUE
  202. message.
  203. Unlike GETCONF, this message is used for data that are not stored in the Tor
  204. configuration file, and that may be longer than a single line. On success,
  205. one ReplyLine is sent for each requested value, followed by a final 250 OK
  206. ReplyLine. If a value fits on a single line, the format is:
  207. 250-keyword=value
  208. If avalue must be split over multiple lines, the format is:
  209. 250+keyword=
  210. value
  211. .
  212. Recognized key and their values include:
  213. "version" -- The version of the server's software, including the name
  214. of the software. (example: "Tor 0.0.9.4")
  215. "desc/id/<OR identity>" or "desc/name/<OR nickname>" -- the latest server
  216. descriptor for a given OR, NUL-terminated. If no such OR is known, the
  217. corresponding value is an empty string.
  218. "network-status" -- a space-separated list of all known OR identities.
  219. This is in the same format as the router-status line in directories;
  220. see tor-spec.txt for details.
  221. "addr-mappings/all"
  222. "addr-mappings/config"
  223. "addr-mappings/cache"
  224. "addr-mappings/control" -- a space-separated list of address mappings, each
  225. in the form of "from-address=to-address". The 'config' key
  226. returns those address mappings set in the configuration; the 'cache'
  227. key returns the mappings in the client-side DNS cache; the 'control'
  228. key returns the mappings set via the control interface; the 'all'
  229. target returns the mappings set through any mechanism.
  230. "circuit-status"
  231. A series of lines as for a circuit status event. Each line is of the form:
  232. CircuitID SP CircStatus SP Path CRLF
  233. "stream-status"
  234. A series of lines as for a stream status event. Each is of the form:
  235. StreamID SP StreamStatus SP Target CRLF
  236. "orconn-status"
  237. A series of lines as for an OR connection status event. Each is of the
  238. form:
  239. ServerID SP ORStatus CRLF
  240. Examples:
  241. C: GETINFO version desc/name/moria1
  242. S: 250+desc/name/moria=
  243. S: [Descriptor for moria]
  244. S: .
  245. S: 250-version=Tor 0.1.1.0-alpha-cvs
  246. S: 250 OK
  247. 3.9 EXTENDCIRCUIT
  248. Sent from the client to the server. The format is:
  249. "EXTENDCIRCUIT" SP CircuitID SP ServerID *("," ServerID) CRLF
  250. This request takes one of two forms: either the Circuit ID is zero, in
  251. which case it is a request for the server to build a new circuit according
  252. to the specified path, or the Circuit ID is nonzero, in which case it is a
  253. request for the server to extend an existing circuit with that ID according
  254. to the specified path.
  255. If the request is successful, the server sends a "250 OK" message containing
  256. a message body consisting of the four-octet Circuit ID of the (maybe
  257. newly created) circuit.
  258. [XXX four-octet? that sounds binary to me, yes? -RD]
  259. 3.10 ATTACHSTREAM
  260. Sent from the client to the server. The syntax is:
  261. "ATTACHSTREAM" SP StreamID SP CircuitID CRLF
  262. This message informs the server that the specified stream should be
  263. associated with the specified circuit. Each stream may be associated with
  264. at most one circuit, and multiple streams may share the same circuit.
  265. Streams can only be attached to completed circuits (that is, circuits that
  266. have sent a circuit status 'built' event or are listed as built in a
  267. GETINFO circuit-status request).
  268. If the circuit ID is 0, responsibility for attaching the given stream is
  269. returned to Tor.
  270. Tor responds with "250 OK" if it can attach the stream, 552 if the circuit
  271. or stream didn't exist, or 551 if the stream couldn't be attached for
  272. another reason.
  273. {Implementation note: By default, Tor automatically attaches streams to
  274. circuits itself, unless the configuration variable
  275. "__LeaveStreamsUnattached" is set to "1". Attempting to attach streams
  276. via TC when "__LeaveStreamsUnattached" is false may cause a race between
  277. Tor and the controller, as both attempt to attach streams to circuits.}
  278. 3.11 POSTDESCRIPTOR
  279. Sent from the client to the server. The syntax is:
  280. "+POSTDESCRIPTOR" CRLF Descriptor CRLF "." CRLF
  281. This message informs the server about a new descriptor.
  282. The descriptor, when parsed, must contain a number of well-specified
  283. fields, including fields for its nickname and identity.
  284. If there is an error in parsing the descriptor, the server must send an
  285. appropriate error message.
  286. [XXX let's specify the status code here too -RD]
  287. If the descriptor is well-formed but the server
  288. chooses not to add it, it must reply with a 251 message whose body
  289. explains why the server was not added.
  290. 3.12 REDIRECTSTREAM
  291. Sent from the client to the server. The syntax is:
  292. "REDIRECTSTREAM" SP StreamID SP Address CRLF
  293. Tells the server to change the exit address on the specified stream. No
  294. remapping is performed on the new provided address.
  295. To be sure that the modified address will be used, this event must be sent
  296. after a new stream event is received, and before attaching this stream to
  297. a circuit.
  298. Tor replies with "250 OK" on success.
  299. 3.13 CLOSESTREAM
  300. Sent from the client to the server. The syntax is:
  301. "CLOSESTREAM" SP StreamID SP Reason *(SP Flag) CRLF
  302. Tells the server to close the specified stream. The reason should be one
  303. of the Tor RELAY_END reasons given in tor-spec.txt, as a decimal. Flags is
  304. not used currently; Tor servers SHOULD ignore unrecognized flags. Tor may
  305. hold the stream open for a while to flush any data that is pending.
  306. 3.14 CLOSECIRCUIT
  307. The syntax is:
  308. CLOSECIRCUIT SP CircuitID *(SP Flag) CRLF
  309. Flag = "IfUnused"
  310. Tells the server to close the specified circuit. If "IfUnused" is
  311. provided, do not close the circuit unless it is unused.
  312. Other flags may be defined in the future; Tor SHOULD ignore unrecognized
  313. flags.
  314. 4 Replies
  315. Reply codes follow the same 3-character format as used by SMTP, with the
  316. first character defining a status, the second character defining a
  317. subsystem, and the third designating fine-grained information.
  318. The TC protocol currently uses the following first characters:
  319. 2yz Positive Completion Reply
  320. The command was successful; a new request can be started.
  321. 4yz Temporary Negative Completion reply
  322. The command was unsuccessful but might be reattempted later.
  323. 5yz Permanent Negative Completion Reply
  324. The command was unsuccessful; the client should not try exactly
  325. that sequence of commands again.
  326. 6yz Asynchronous Reply
  327. Sent out-of-order in response to an earlier SETEVENTS command.
  328. The following second characters are used:
  329. x0z Syntax
  330. Sent in response to ill-formed or nonsensical commands.
  331. x1z Protocol
  332. Refers to operations of the Tor Control protocol.
  333. x5z Tor
  334. Refers to actual operations of Tor system.
  335. The following codes are defined:
  336. 250 OK
  337. 251 Operation was unnecessary
  338. [Tor has declined to perform the operation, but no harm was done.]
  339. 451 Resource exhausted
  340. 500 Syntax error: protocol
  341. 510 Unrecognized command
  342. 511 Unimplemented command
  343. 512 Syntax error in command argument
  344. 513 Unrecognized command argument
  345. 514 Authentication required
  346. 515 Bad authentication
  347. 550 Unspecified Tor error
  348. 551 Internal error
  349. [Something went wrong inside Tor, so that the client's
  350. request couldn't be fulfilled.]
  351. 552 Unrecognized entity
  352. [A configuration key, a stream ID, circuit ID, event,
  353. mentioned in the command did not actually exist.]
  354. 553 Invalid configuration value
  355. [The client tried to set a configuration option to an
  356. incorrect, ill-formed, or impossible value.]
  357. 650 Asynchronous event notification
  358. 4.1 Asynchronous events
  359. These replies can be sent after a corresponding SETEVENTS command has been
  360. received. They will not be interleaved with other Reply elements, but they
  361. can appear between a command and its corresponding reply. For example,
  362. this sequence is possible:
  363. C: SETEVENTS CIRC
  364. S: 250 OK
  365. C: GETCONFIG SOCKSPORT ORPORT
  366. S: 650 CIRC 1000 EXTENDED moria1,moria2
  367. S: 250-SOCKSPORT=9050
  368. S: 250 ORPORT=0
  369. But this sequence is disallowed:
  370. C: SETEVENTS CIRC
  371. S: 250 OK
  372. C: GETCONFIG SOCKSPORT ORPORT
  373. S: 250-SOCKSPORT=9050
  374. S: 650 CIRC 1000 EXTENDED moria1,moria2
  375. S: 250 ORPORT=0
  376. 4.1.1 Circuit status changed
  377. The syntax is:
  378. "650" SP "CIRC" SP CircuitID SP CircStatus SP Path
  379. CircStatus =
  380. "LAUNCHED" / ; circuit ID assigned to new circuit
  381. "BUILT" / ; all hops finished, can now accept streams
  382. "EXTENDED" / ; one more hop has been completed
  383. "FAILED" / ; circuit closed (was not built)
  384. "CLOSED" ; circuit closed (was built)
  385. Path = ServerID *("," ServerID)
  386. 4.1.2. Stream status changed
  387. The syntax is:
  388. "650" SP "STREAM" SP StreamID SP StreamStatus SP Target
  389. StreamStatus =
  390. "NEW" / ; New request to connect
  391. "NEWRESOLVE" / ; New request to resolve an address
  392. "SENTCONNECT" / ; Sent a connect cell along a circuit
  393. "SENTRESOLVE" / ; Sent a resolve cell along a circuit
  394. "SUCCEEDED" / ; Received a successful reply; stream established
  395. "FAILED" / ; Stream failed and not retriable.
  396. "CLOSED" / ; Stream closed
  397. "DETACHED" ; Stream detached from circuit; still retriable
  398. Target = Address ":" Port
  399. 4.1.3 OR Connection status changed
  400. The syntax is:
  401. "650" SP "ORCONN" SP ServerID SP ORStatus
  402. ORStatus = "LAUNCHED" / "CONNECTED" / "FAILED" / "CLOSED"
  403. 4.1.3 Bandwidth used in the last second
  404. The syntax is:
  405. "650" SP "BW" SP BytesRead SP BytesWritten
  406. BytesRead = 1*DIGIT
  407. BytesWritten = 1*DIGIT
  408. 4.1.4 Log message
  409. The syntax is:
  410. "650" SP Severity SP ReplyText
  411. or
  412. "650+" Severity CRLF Data
  413. Severity = "DEBUG" / "INFO" / "NOTICE" / "WARN"/ "ERR"
  414. 4.1.5 New descriptors available
  415. Syntax:
  416. "650" SP "NEWDESC" 1*(SP ServerID)
  417. 5. Implementation notes
  418. 5.1. Authentication
  419. By default, the current Tor implementation trusts all local users.
  420. If the 'CookieAuthentication' option is true, Tor writes a "magic cookie"
  421. file named "control_auth_cookie" into its data directory. To authenticate,
  422. the controller must send the contents of this file.
  423. If the 'HashedControlPassword' option is set, it must contain the salted
  424. hash of a secret password. The salted hash is computed according to the
  425. S2K algorithm in RFC 2440 (OpenPGP), and prefixed with the s2k specifier.
  426. This is then encoded in hexadecimal, prefixed by the indicator sequence
  427. "16:". Thus, for example, the password 'foo' could encode to:
  428. 16:660537E3E1CD49996044A3BF558097A981F539FEA2F9DA662B4626C1C2
  429. ++++++++++++++++**^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  430. salt hashed value
  431. indicator
  432. You can generate the salt of a password by calling
  433. 'tor --hash-password <password>'
  434. or by using the example code in the Python and Java controller libraries.
  435. To authenticate under this scheme, the controller sends Tor the original
  436. secret that was used to generate the password.
  437. 5.2. Don't let the buffer get too big.
  438. If you ask for lots of events, and 16MB of them queue up on the buffer,
  439. the Tor process will close the socket.
  440. 5.3. Backward compatibility
  441. For backward compatibility with the "version 0" control protocol, Tor checks
  442. whether the third byte the first command is zero. If it is, Tor
  443. assumes that version 0 is in use. This feature is deprecated, and will be
  444. removed in the 0.1.2.x Tor development series.