control.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. /* Copyright 2004 Roger Dingledine, Nick Mathewson. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. const char control_c_id[] = "$Id$";
  5. /**
  6. * \file control.c
  7. *
  8. * \brief Implementation for Tor's control-socket interface.
  9. */
  10. #include "or.h"
  11. /* Protocol outline: a bidirectional stream, over which each side
  12. * sends a series of messages. Each message has a two-byte length field,
  13. * a two-byte typecode, and a variable-length body whose length is
  14. * given in the length field.
  15. *
  16. * By default, the server only sends messages in response to client messages.
  17. * Every client message gets a message in response. The client may, however,
  18. * _request_ that other messages be delivered asynchronously.
  19. *
  20. *
  21. * Every message type is either client-only or server-only, and every
  22. * server message type is either synchronous-only (only occurs in
  23. * response to a client request) or asynchronous-only (never is an
  24. * answer to a client request.
  25. *
  26. * See control-spec.txt for full details.
  27. */
  28. /* Recognized message type codes. */
  29. #define CONTROL_CMD_ERROR 0x0000
  30. #define CONTROL_CMD_DONE 0x0001
  31. #define CONTROL_CMD_SETCONF 0x0002
  32. #define CONTROL_CMD_GETCONF 0x0003
  33. #define CONTROL_CMD_CONFVALUE 0x0004
  34. #define CONTROL_CMD_SETEVENTS 0x0005
  35. #define CONTROL_CMD_EVENT 0x0006
  36. #define CONTROL_CMD_AUTHENTICATE 0x0007
  37. #define CONTROL_CMD_SAVECONF 0x0008
  38. #define CONTROL_CMD_SIGNAL 0x0009
  39. #define CONTROL_CMD_MAPADDRESS 0x000A
  40. #define CONTROL_CMD_GETINFO 0x000B
  41. #define CONTROL_CMD_INFOVALUE 0x000C
  42. #define CONTROL_CMD_EXTENDCIRCUIT 0x000D
  43. #define CONTROL_CMD_ATTACHSTREAM 0x000E
  44. #define CONTROL_CMD_POSTDESCRIPTOR 0x000F
  45. #define CONTROL_CMD_FRAGMENTHEADER 0x0010
  46. #define CONTROL_CMD_FRAGMENT 0x0011
  47. #define _CONTROL_CMD_MAX_RECOGNIZED 0x0011
  48. /* Recognized error codes. */
  49. #define ERR_UNSPECIFIED 0x0000
  50. #define ERR_INTERNAL 0x0001
  51. #define ERR_UNRECOGNIZED_TYPE 0x0002
  52. #define ERR_SYNTAX 0x0003
  53. #define ERR_UNRECOGNIZED_CONFIG_KEY 0x0004
  54. #define ERR_INVALID_CONFIG_VALUE 0x0005
  55. #define ERR_UNRECOGNIZED_EVENT_CODE 0x0006
  56. #define ERR_UNAUTHORIZED 0x0007
  57. #define ERR_REJECTED_AUTHENTICATION 0x0008
  58. #define ERR_RESOURCE_EXHAUSETED 0x0009
  59. #define ERR_TOO_LONG 0x000A
  60. /* Recognized asynchronous event types. */
  61. #define _EVENT_MIN 0x0001
  62. #define EVENT_CIRCUIT_STATUS 0x0001
  63. #define EVENT_STREAM_STATUS 0x0002
  64. #define EVENT_OR_CONN_STATUS 0x0003
  65. #define EVENT_BANDWIDTH_USED 0x0004
  66. #define EVENT_WARNING 0x0005
  67. #define EVENT_NEW_DESC 0x0006
  68. #define _EVENT_MAX 0x0006
  69. /** Array mapping from message type codes to human-readable message
  70. * type names. */
  71. static const char * CONTROL_COMMANDS[_CONTROL_CMD_MAX_RECOGNIZED+1] = {
  72. "error",
  73. "done",
  74. "setconf",
  75. "getconf",
  76. "confvalue",
  77. "setevents",
  78. "events",
  79. "authenticate",
  80. "saveconf",
  81. "signal",
  82. "mapaddress",
  83. "getinfo",
  84. "infovalue",
  85. "extendcircuit",
  86. "attachstream",
  87. "postdescriptor",
  88. "fragmentheader",
  89. "fragment",
  90. };
  91. /** Bitfield: The bit 1&lt;&lt;e is set if <b>any</b> open control
  92. * connection is interested in events of type <b>e</b>. We use this
  93. * so that we can decide to skip generating event messages that nobody
  94. * has interest in without having to walk over the global connection
  95. * list to find out.
  96. **/
  97. static uint32_t global_event_mask = 0;
  98. /** Macro: true if any control connection is interested in events of type
  99. * <b>e</b>. */
  100. #define EVENT_IS_INTERESTING(e) (global_event_mask & (1<<(e)))
  101. /** If we're using cookie-type authentication, how long should our cookies be?
  102. */
  103. #define AUTHENTICATION_COOKIE_LEN 32
  104. /** If true, we've set authentication_cookie to a secret code and
  105. * stored it to disk. */
  106. static int authentication_cookie_is_set = 0;
  107. static char authentication_cookie[AUTHENTICATION_COOKIE_LEN];
  108. static void update_global_event_mask(void);
  109. static void send_control_message(connection_t *conn, uint16_t type,
  110. uint32_t len, const char *body);
  111. static void send_control_done(connection_t *conn);
  112. static void send_control_done2(connection_t *conn, const char *msg, size_t len);
  113. static void send_control_error(connection_t *conn, uint16_t error,
  114. const char *message);
  115. static void send_control_event(uint16_t event, uint32_t len, const char *body);
  116. static int handle_control_setconf(connection_t *conn, uint32_t len,
  117. char *body);
  118. static int handle_control_getconf(connection_t *conn, uint32_t len,
  119. const char *body);
  120. static int handle_control_setevents(connection_t *conn, uint32_t len,
  121. const char *body);
  122. static int handle_control_authenticate(connection_t *conn, uint32_t len,
  123. const char *body);
  124. static int handle_control_saveconf(connection_t *conn, uint32_t len,
  125. const char *body);
  126. static int handle_control_signal(connection_t *conn, uint32_t len,
  127. const char *body);
  128. static int handle_control_mapaddress(connection_t *conn, uint32_t len,
  129. const char *body);
  130. static int handle_control_getinfo(connection_t *conn, uint32_t len,
  131. const char *body);
  132. static int handle_control_extendcircuit(connection_t *conn, uint32_t len,
  133. const char *body);
  134. static int handle_control_attachstream(connection_t *conn, uint32_t len,
  135. const char *body);
  136. static int handle_control_postdescriptor(connection_t *conn, uint32_t len,
  137. const char *body);
  138. /** Given a possibly invalid message type code <b>cmd</b>, return a
  139. * human-readable string equivalent. */
  140. static INLINE const char *
  141. control_cmd_to_string(uint16_t cmd)
  142. {
  143. return (cmd<=_CONTROL_CMD_MAX_RECOGNIZED) ? CONTROL_COMMANDS[cmd] : "Unknown";
  144. }
  145. /** Set <b>global_event_mask</b> to the bitwise OR of each live control
  146. * connection's event_mask field. */
  147. static void update_global_event_mask(void)
  148. {
  149. connection_t **conns;
  150. int n_conns, i;
  151. global_event_mask = 0;
  152. get_connection_array(&conns, &n_conns);
  153. for (i = 0; i < n_conns; ++i) {
  154. if (conns[i]->type == CONN_TYPE_CONTROL &&
  155. conns[i]->state == CONTROL_CONN_STATE_OPEN) {
  156. global_event_mask |= conns[i]->event_mask;
  157. }
  158. }
  159. }
  160. /** Send a message of type <b>type</b> containing <b>len</b> bytes
  161. * from <b>body</b> along the control connection <b>conn</b> */
  162. static void
  163. send_control_message(connection_t *conn, uint16_t type, uint32_t len,
  164. const char *body)
  165. {
  166. char buf[10];
  167. tor_assert(conn);
  168. tor_assert(len || !body);
  169. tor_assert(type <= _CONTROL_CMD_MAX_RECOGNIZED);
  170. if (len < 65536) {
  171. set_uint16(buf, htons(len));
  172. set_uint16(buf+2, htons(type));
  173. connection_write_to_buf(buf, 4, conn);
  174. if (len)
  175. connection_write_to_buf(body, len, conn);
  176. } else {
  177. set_uint16(buf, htons(65535));
  178. set_uint16(buf+2, htons(CONTROL_CMD_FRAGMENTHEADER));
  179. set_uint16(buf+4, htons(type));
  180. set_uint32(buf+6, htonl(len));
  181. connection_write_to_buf(buf, 10, conn);
  182. connection_write_to_buf(body, 65535-6, conn);
  183. len -= (65535-6);
  184. body += (65535-6);
  185. while (len) {
  186. size_t chunklen = (len<65535)?len:65535;
  187. set_uint16(buf, htons((uint16_t)chunklen));
  188. set_uint16(buf+2, htons(CONTROL_CMD_FRAGMENT));
  189. connection_write_to_buf(buf, 4, conn);
  190. connection_write_to_buf(body, chunklen, conn);
  191. len -= chunklen;
  192. body += chunklen;
  193. }
  194. }
  195. }
  196. /** Send a "DONE" message down the control connection <b>conn</b> */
  197. static void
  198. send_control_done(connection_t *conn)
  199. {
  200. send_control_message(conn, CONTROL_CMD_DONE, 0, NULL);
  201. }
  202. static void send_control_done2(connection_t *conn, const char *msg, size_t len)
  203. {
  204. send_control_message(conn, CONTROL_CMD_DONE, len, msg);
  205. }
  206. /** Send an error message with error code <b>error</b> and body
  207. * <b>message</b> down the connection <b>conn</b> */
  208. static void
  209. send_control_error(connection_t *conn, uint16_t error, const char *message)
  210. {
  211. char buf[256];
  212. size_t len;
  213. set_uint16(buf, htons(error));
  214. len = strlen(message);
  215. tor_assert(len < (256-2));
  216. memcpy(buf+2, message, len);
  217. send_control_message(conn, CONTROL_CMD_ERROR, (uint16_t)(len+2), buf);
  218. }
  219. /** Send an 'event' message of event type <b>event</b>, containing
  220. * <b>len</b> bytes in <b>body</b> to every control connection that
  221. * is interested in it. */
  222. static void
  223. send_control_event(uint16_t event, uint32_t len, const char *body)
  224. {
  225. connection_t **conns;
  226. int n_conns, i;
  227. size_t buflen;
  228. char *buf;
  229. buflen = len + 2;
  230. buf = tor_malloc_zero(buflen);
  231. set_uint16(buf, htons(event));
  232. memcpy(buf+2, body, len);
  233. get_connection_array(&conns, &n_conns);
  234. for (i = 0; i < n_conns; ++i) {
  235. if (conns[i]->type == CONN_TYPE_CONTROL &&
  236. conns[i]->state == CONTROL_CONN_STATE_OPEN &&
  237. conns[i]->event_mask & (1<<event)) {
  238. send_control_message(conns[i], CONTROL_CMD_EVENT, buflen, buf);
  239. }
  240. }
  241. tor_free(buf);
  242. }
  243. /** Called when we receive a SETCONF message: parse the body and try
  244. * to update our configuration. Reply with a DONE or ERROR message. */
  245. static int
  246. handle_control_setconf(connection_t *conn, uint32_t len, char *body)
  247. {
  248. int r;
  249. struct config_line_t *lines=NULL;
  250. if (config_get_lines(body, &lines) < 0) {
  251. log_fn(LOG_WARN,"Controller gave us config lines we can't parse.");
  252. send_control_error(conn, ERR_SYNTAX, "Couldn't parse configuration");
  253. return 0;
  254. }
  255. if ((r=config_trial_assign(lines, 1)) < 0) {
  256. log_fn(LOG_WARN,"Controller gave us config lines that didn't validate.");
  257. if (r==-1) {
  258. send_control_error(conn, ERR_UNRECOGNIZED_CONFIG_KEY,
  259. "Unrecognized option");
  260. } else {
  261. send_control_error(conn, ERR_INVALID_CONFIG_VALUE,"Invalid option value");
  262. }
  263. config_free_lines(lines);
  264. return 0;
  265. }
  266. config_free_lines(lines);
  267. if (options_act() < 0) { /* acting on them failed. die. */
  268. log_fn(LOG_ERR,"Acting on config options left us in a broken state. Dying.");
  269. exit(1);
  270. }
  271. send_control_done(conn);
  272. return 0;
  273. }
  274. /** Called when we receive a GETCONF message. Parse the request, and
  275. * reply with a CONFVALUE or an ERROR message */
  276. static int
  277. handle_control_getconf(connection_t *conn, uint32_t body_len, const char *body)
  278. {
  279. smartlist_t *questions = NULL;
  280. smartlist_t *answers = NULL;
  281. char *msg = NULL;
  282. size_t msg_len;
  283. or_options_t *options = get_options();
  284. questions = smartlist_create();
  285. smartlist_split_string(questions, body, "\n",
  286. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  287. answers = smartlist_create();
  288. SMARTLIST_FOREACH(questions, const char *, q,
  289. {
  290. int recognized = config_option_is_recognized(q);
  291. if (!recognized) {
  292. send_control_error(conn, ERR_UNRECOGNIZED_CONFIG_KEY, body);
  293. goto done;
  294. } else {
  295. struct config_line_t *answer = config_get_assigned_option(options,q);
  296. while (answer) {
  297. struct config_line_t *next;
  298. size_t alen = strlen(answer->key)+strlen(answer->value)+3;
  299. char *astr = tor_malloc(alen);
  300. tor_snprintf(astr, alen, "%s %s\n", answer->key, answer->value);
  301. smartlist_add(answers, astr);
  302. next = answer->next;
  303. tor_free(answer->key);
  304. tor_free(answer->value);
  305. tor_free(answer);
  306. answer = next;
  307. }
  308. }
  309. });
  310. msg = smartlist_join_strings(answers, "", 0, &msg_len);
  311. send_control_message(conn, CONTROL_CMD_CONFVALUE,
  312. (uint16_t)msg_len, msg_len?msg:NULL);
  313. done:
  314. if (answers) SMARTLIST_FOREACH(answers, char *, cp, tor_free(cp));
  315. if (questions) SMARTLIST_FOREACH(questions, char *, cp, tor_free(cp));
  316. smartlist_free(answers);
  317. smartlist_free(questions);
  318. tor_free(msg);
  319. return 0;
  320. }
  321. /** Called when we get a SETEVENTS message: update conn->event_mask,
  322. * and reply with DONE or ERROR. */
  323. static int
  324. handle_control_setevents(connection_t *conn, uint32_t len, const char *body)
  325. {
  326. uint16_t event_code;
  327. uint32_t event_mask = 0;
  328. if (len % 2) {
  329. send_control_error(conn, ERR_SYNTAX,
  330. "Odd number of bytes in setevents message");
  331. return 0;
  332. }
  333. for (; len; len -= 2, body += 2) {
  334. event_code = ntohs(get_uint16(body));
  335. if (event_code < _EVENT_MIN || event_code > _EVENT_MAX) {
  336. send_control_error(conn, ERR_UNRECOGNIZED_EVENT_CODE,
  337. "Unrecognized event code");
  338. return 0;
  339. }
  340. event_mask |= (1 << event_code);
  341. }
  342. conn->event_mask = event_mask;
  343. update_global_event_mask();
  344. send_control_done(conn);
  345. return 0;
  346. }
  347. /** Decode the hashed, base64'd password stored in <b>hashed</b>. If
  348. * <b>buf</b> is provided, store the hashed password in the first
  349. * S2K_SPECIFIER_LEN+DIGEST_LEN bytes of <b>buf</b>. Return 0 on
  350. * success, -1 on failure.
  351. */
  352. int
  353. decode_hashed_password(char *buf, const char *hashed)
  354. {
  355. char decoded[64];
  356. if (base64_decode(decoded, sizeof(decoded), hashed, strlen(hashed))
  357. != S2K_SPECIFIER_LEN+DIGEST_LEN) {
  358. return -1;
  359. }
  360. if (buf)
  361. memcpy(buf, decoded, sizeof(decoded));
  362. return 0;
  363. }
  364. /** Called when we get an AUTHENTICATE message. Check whether the
  365. * authentication is valid, and if so, update the connection's state to
  366. * OPEN. Reply with DONE or ERROR.
  367. */
  368. static int
  369. handle_control_authenticate(connection_t *conn, uint32_t len, const char *body)
  370. {
  371. or_options_t *options = get_options();
  372. if (options->CookieAuthentication) {
  373. if (len == AUTHENTICATION_COOKIE_LEN &&
  374. !memcmp(authentication_cookie, body, len)) {
  375. goto ok;
  376. }
  377. } else if (options->HashedControlPassword) {
  378. char expected[S2K_SPECIFIER_LEN+DIGEST_LEN];
  379. char received[DIGEST_LEN];
  380. if (decode_hashed_password(expected, options->HashedControlPassword)<0) {
  381. log_fn(LOG_WARN,"Couldn't decode HashedControlPassword: invalid base64");
  382. goto err;
  383. }
  384. secret_to_key(received,DIGEST_LEN,body,len,expected);
  385. if (!memcmp(expected+S2K_SPECIFIER_LEN, received, DIGEST_LEN))
  386. goto ok;
  387. goto err;
  388. } else {
  389. if (len == 0) {
  390. /* if Tor doesn't demand any stronger authentication, then
  391. * the controller can get in with a blank auth line. */
  392. goto ok;
  393. }
  394. goto err;
  395. }
  396. err:
  397. send_control_error(conn, ERR_REJECTED_AUTHENTICATION,"Authentication failed");
  398. return 0;
  399. ok:
  400. log_fn(LOG_INFO, "Authenticated control connection (%d)", conn->s);
  401. send_control_done(conn);
  402. conn->state = CONTROL_CONN_STATE_OPEN;
  403. return 0;
  404. }
  405. static int
  406. handle_control_saveconf(connection_t *conn, uint32_t len,
  407. const char *body)
  408. {
  409. if (save_current_config()<0) {
  410. send_control_error(conn, ERR_INTERNAL,
  411. "Unable to write configuration to disk.");
  412. } else {
  413. send_control_done(conn);
  414. }
  415. return 0;
  416. }
  417. static int
  418. handle_control_signal(connection_t *conn, uint32_t len,
  419. const char *body)
  420. {
  421. if (len != 1) {
  422. send_control_error(conn, ERR_SYNTAX,
  423. "Body of SIGNAL command too long or too short.");
  424. } else if (control_signal_act((uint8_t)body[0]) < 0) {
  425. send_control_error(conn, ERR_SYNTAX, "Unrecognized signal number.");
  426. } else {
  427. send_control_done(conn);
  428. }
  429. return 0;
  430. }
  431. static int
  432. handle_control_mapaddress(connection_t *conn, uint32_t len, const char *body)
  433. {
  434. smartlist_t *elts;
  435. smartlist_t *lines;
  436. smartlist_t *reply;
  437. char *r;
  438. size_t sz;
  439. lines = smartlist_create();
  440. elts = smartlist_create();
  441. reply = smartlist_create();
  442. smartlist_split_string(lines, body, "\n",
  443. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  444. SMARTLIST_FOREACH(lines, const char *, line,
  445. {
  446. tor_strlower(line);
  447. smartlist_split_string(elts, line, " ", 0, 2);
  448. if (smartlist_len(elts) == 2) {
  449. const char *from = smartlist_get(elts,0);
  450. const char *to = smartlist_get(elts,1);
  451. if (!is_plausible_address(from)) {
  452. log_fn(LOG_WARN,"Skipping invalid argument '%s' in MapAddress msg",from);
  453. } else if (!is_plausible_address(to)) {
  454. log_fn(LOG_WARN,"Skipping invalid argument '%s' in MapAddress msg",to);
  455. } else if (!strcmp(from, ".") || !strcmp(from, "0.0.0.0")) {
  456. char *addr = addressmap_register_virtual_address(
  457. strcmp(from,".") ? RESOLVED_TYPE_HOSTNAME : RESOLVED_TYPE_IPV4,
  458. tor_strdup(to));
  459. if (!addr) {
  460. log_fn(LOG_WARN,
  461. "Unable to allocate address for '%s' in MapAddress msg",line);
  462. } else {
  463. size_t anslen = strlen(addr)+strlen(to)+2;
  464. char *ans = tor_malloc(anslen);
  465. tor_snprintf(ans, anslen, "%s %s", addr, to);
  466. tor_free(addr);
  467. smartlist_add(reply, ans);
  468. }
  469. } else {
  470. addressmap_register(from, tor_strdup(to), 1);
  471. smartlist_add(reply, tor_strdup(line));
  472. }
  473. } else {
  474. log_fn(LOG_WARN, "Skipping MapAddress line with wrong number of items.");
  475. }
  476. SMARTLIST_FOREACH(elts, char *, cp, tor_free(cp));
  477. smartlist_clear(elts);
  478. });
  479. SMARTLIST_FOREACH(lines, char *, cp, tor_free(cp));
  480. smartlist_free(lines);
  481. smartlist_free(elts);
  482. r = smartlist_join_strings(reply, "\n", 1, &sz);
  483. send_control_done2(conn,r,sz);
  484. SMARTLIST_FOREACH(reply, char *, cp, tor_free(cp));
  485. smartlist_free(reply);
  486. tor_free(r);
  487. return 0;
  488. }
  489. static char *
  490. handle_getinfo_helper(const char *question)
  491. {
  492. if (!strcmp(question, "version")) {
  493. return tor_strdup(VERSION);
  494. } else if (!strcmpstart(question, "desc/id/")) {
  495. routerinfo_t *ri = router_get_by_hexdigest(question+strlen("desc/id/"));
  496. if (!ri)
  497. return NULL;
  498. if (!ri->signed_descriptor)
  499. return NULL;
  500. return tor_strdup(ri->signed_descriptor);
  501. } else if (!strcmp(question, "desc/all-ids")) {
  502. routerlist_t *rl;
  503. char *answer, *cp;
  504. router_get_routerlist(&rl);
  505. if (!rl)
  506. return tor_strdup("");
  507. answer = tor_malloc(smartlist_len(rl->routers)*(HEX_DIGEST_LEN+1)+1);
  508. cp = answer;
  509. SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
  510. {
  511. base16_encode(cp, HEX_DIGEST_LEN+1, r->identity_digest, DIGEST_LEN);
  512. cp += HEX_DIGEST_LEN;
  513. *cp++ = ',';
  514. });
  515. return answer;
  516. } else if (!strcmpstart(question, "addr-mappings/")) {
  517. time_t min_e, max_e;
  518. smartlist_t *mappings;
  519. char *answer;
  520. if (!strcmp(question, "addr-mappings/all")) {
  521. min_e = 0; max_e = TIME_MAX;
  522. } else if (!strcmp(question, "addr-mappings/cache")) {
  523. min_e = 2; max_e = TIME_MAX;
  524. } else if (!strcmp(question, "addr-mappings/config")) {
  525. min_e = 0; max_e = 0;
  526. } else if (!strcmp(question, "addr-mappings/control")) {
  527. min_e = 1; max_e = 1;
  528. } else {
  529. return NULL;
  530. }
  531. mappings = smartlist_create();
  532. addressmap_get_mappings(mappings, min_e, max_e);
  533. answer = smartlist_join_strings(mappings, "\n", 1, NULL);
  534. SMARTLIST_FOREACH(mappings, char *, cp, tor_free(cp));
  535. smartlist_free(mappings);
  536. return answer;
  537. } else {
  538. /* unrecognized key */
  539. return NULL;
  540. }
  541. }
  542. static int
  543. handle_control_getinfo(connection_t *conn, uint32_t len, const char *body)
  544. {
  545. smartlist_t *questions = NULL;
  546. smartlist_t *answers = NULL;
  547. char *msg = NULL, *ans;
  548. size_t msg_len;
  549. questions = smartlist_create();
  550. smartlist_split_string(questions, body, "\n",
  551. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  552. answers = smartlist_create();
  553. SMARTLIST_FOREACH(questions, const char *, q,
  554. {
  555. ans = handle_getinfo_helper(q);
  556. if (!ans) {
  557. send_control_error(conn, ERR_UNRECOGNIZED_CONFIG_KEY, body);
  558. goto done;
  559. } else {
  560. smartlist_add(answers, tor_strdup(q));
  561. smartlist_add(answers, ans);
  562. msg_len += 2 + strlen(ans) + strlen(q);
  563. }
  564. });
  565. if (msg_len > 65535) {
  566. /* XXXX What if it *is* this long? */
  567. send_control_error(conn, ERR_TOO_LONG, body);
  568. goto done;
  569. }
  570. msg = smartlist_join_strings2(answers, "\0", 1, 1, &msg_len);
  571. send_control_message(conn, CONTROL_CMD_INFOVALUE,
  572. msg_len, msg_len?msg:NULL);
  573. done:
  574. if (answers) SMARTLIST_FOREACH(answers, char *, cp, tor_free(cp));
  575. if (questions) SMARTLIST_FOREACH(questions, char *, cp, tor_free(cp));
  576. smartlist_free(answers);
  577. smartlist_free(questions);
  578. tor_free(msg);
  579. return 0;
  580. }
  581. static int
  582. handle_control_extendcircuit(connection_t *conn, uint32_t len,
  583. const char *body)
  584. {
  585. send_control_error(conn,ERR_UNRECOGNIZED_TYPE,"not yet implemented");
  586. return 0;
  587. }
  588. static int handle_control_attachstream(connection_t *conn, uint32_t len,
  589. const char *body)
  590. {
  591. send_control_error(conn,ERR_UNRECOGNIZED_TYPE,"not yet implemented");
  592. return 0;
  593. }
  594. static int
  595. handle_control_postdescriptor(connection_t *conn, uint32_t len,
  596. const char *body)
  597. {
  598. if (router_load_single_router(body)<0) {
  599. /* XXXX a more specific error would be nice. */
  600. send_control_error(conn,ERR_UNSPECIFIED,
  601. "Could not parse descriptor or add it");
  602. return 0;
  603. }
  604. send_control_done(conn);
  605. return 0;
  606. }
  607. /** Called when <b>conn</b> has no more bytes left on its outbuf. */
  608. int
  609. connection_control_finished_flushing(connection_t *conn) {
  610. tor_assert(conn);
  611. tor_assert(conn->type == CONN_TYPE_CONTROL);
  612. connection_stop_writing(conn);
  613. return 0;
  614. }
  615. /** Called when <b>conn</b> has gotten its socket closed. */
  616. int connection_control_reached_eof(connection_t *conn) {
  617. log_fn(LOG_INFO,"Control connection reached EOF. Closing.");
  618. connection_mark_for_close(conn);
  619. return 0;
  620. }
  621. /** Called when <b>conn</b> has received more bytes on its inbuf.
  622. */
  623. int
  624. connection_control_process_inbuf(connection_t *conn) {
  625. uint32_t body_len;
  626. uint16_t command_type;
  627. char *body;
  628. tor_assert(conn);
  629. tor_assert(conn->type == CONN_TYPE_CONTROL);
  630. again:
  631. /* Try to suck a control message from the buffer. */
  632. switch (fetch_from_buf_control(conn->inbuf, &body_len, &command_type, &body))
  633. {
  634. case -1:
  635. tor_free(body);
  636. log_fn(LOG_WARN, "Error in control command. Failing.");
  637. return -1;
  638. case 0:
  639. /* Control command not all here yet. Wait. */
  640. return 0;
  641. case 1:
  642. /* We got a command. Process it. */
  643. break;
  644. default:
  645. tor_assert(0);
  646. }
  647. /* We got a command. If we need authentication, only authentication
  648. * commands will be considered. */
  649. if (conn->state == CONTROL_CONN_STATE_NEEDAUTH &&
  650. command_type != CONTROL_CMD_AUTHENTICATE) {
  651. log_fn(LOG_WARN, "Rejecting '%s' command; authentication needed.",
  652. control_cmd_to_string(command_type));
  653. send_control_error(conn, ERR_UNAUTHORIZED, "Authentication required");
  654. tor_free(body);
  655. goto again;
  656. }
  657. /* Okay, we're willing to process the command. */
  658. switch (command_type)
  659. {
  660. case CONTROL_CMD_SETCONF:
  661. if (handle_control_setconf(conn, body_len, body))
  662. return -1;
  663. break;
  664. case CONTROL_CMD_GETCONF:
  665. if (handle_control_getconf(conn, body_len, body))
  666. return -1;
  667. break;
  668. case CONTROL_CMD_SETEVENTS:
  669. if (handle_control_setevents(conn, body_len, body))
  670. return -1;
  671. break;
  672. case CONTROL_CMD_AUTHENTICATE:
  673. if (handle_control_authenticate(conn, body_len, body))
  674. return -1;
  675. break;
  676. case CONTROL_CMD_SAVECONF:
  677. if (handle_control_saveconf(conn, body_len, body))
  678. return -1;
  679. break;
  680. case CONTROL_CMD_SIGNAL:
  681. if (handle_control_signal(conn, body_len, body))
  682. return -1;
  683. break;
  684. case CONTROL_CMD_MAPADDRESS:
  685. if (handle_control_mapaddress(conn, body_len, body))
  686. return -1;
  687. break;
  688. case CONTROL_CMD_GETINFO:
  689. if (handle_control_getinfo(conn, body_len, body))
  690. return -1;
  691. break;
  692. case CONTROL_CMD_EXTENDCIRCUIT:
  693. if (handle_control_extendcircuit(conn, body_len, body))
  694. return -1;
  695. break;
  696. case CONTROL_CMD_ATTACHSTREAM:
  697. if (handle_control_attachstream(conn, body_len, body))
  698. return -1;
  699. break;
  700. case CONTROL_CMD_POSTDESCRIPTOR:
  701. if (handle_control_postdescriptor(conn, body_len, body))
  702. return -1;
  703. break;
  704. case CONTROL_CMD_ERROR:
  705. case CONTROL_CMD_DONE:
  706. case CONTROL_CMD_CONFVALUE:
  707. case CONTROL_CMD_EVENT:
  708. case CONTROL_CMD_INFOVALUE:
  709. log_fn(LOG_WARN, "Received client-only '%s' command; ignoring.",
  710. control_cmd_to_string(command_type));
  711. send_control_error(conn, ERR_UNRECOGNIZED_TYPE,
  712. "Command type only valid from server to tor client");
  713. break;
  714. case CONTROL_CMD_FRAGMENTHEADER:
  715. case CONTROL_CMD_FRAGMENT:
  716. log_fn(LOG_WARN, "Recieved command fragment out of order; ignoring.");
  717. send_control_error(conn, ERR_SYNTAX, "Bad fragmentation on command.");
  718. default:
  719. log_fn(LOG_WARN, "Received unrecognized command type %d; ignoring.",
  720. (int)command_type);
  721. send_control_error(conn, ERR_UNRECOGNIZED_TYPE,
  722. "Unrecognized command type");
  723. break;
  724. }
  725. tor_free(body);
  726. goto again; /* There might be more data. */
  727. }
  728. /** Something has happened to circuit <b>circ</b>: tell any interested
  729. * control connections. */
  730. int
  731. control_event_circuit_status(circuit_t *circ, circuit_status_event_t tp)
  732. {
  733. char *path, *msg;
  734. size_t path_len;
  735. if (!EVENT_IS_INTERESTING(EVENT_CIRCUIT_STATUS))
  736. return 0;
  737. tor_assert(circ);
  738. tor_assert(CIRCUIT_IS_ORIGIN(circ));
  739. path = circuit_list_path(circ,0);
  740. path_len = strlen(path);
  741. msg = tor_malloc(1+4+path_len+1); /* event, circid, path, NUL. */
  742. msg[0] = (uint8_t) tp;
  743. set_uint32(msg+1, htonl(circ->global_identifier));
  744. strlcpy(msg+5,path,path_len+1);
  745. send_control_event(EVENT_CIRCUIT_STATUS, (uint32_t)(path_len+6), msg);
  746. tor_free(path);
  747. tor_free(msg);
  748. return 0;
  749. }
  750. /** Something has happened to the stream associated with AP connection
  751. * <b>conn</b>: tell any interested control connections. */
  752. int
  753. control_event_stream_status(connection_t *conn, stream_status_event_t tp)
  754. {
  755. char *msg;
  756. size_t len;
  757. char buf[256];
  758. tor_assert(conn->type == CONN_TYPE_AP);
  759. tor_assert(conn->socks_request);
  760. if (!EVENT_IS_INTERESTING(EVENT_STREAM_STATUS))
  761. return 0;
  762. tor_snprintf(buf, sizeof(buf), "%s:%d",
  763. conn->socks_request->address, conn->socks_request->port),
  764. len = strlen(buf);
  765. msg = tor_malloc(5+len+1);
  766. msg[0] = (uint8_t) tp;
  767. set_uint32(msg+1, htonl(conn->global_identifier));
  768. strlcpy(msg+5, buf, len+1);
  769. send_control_event(EVENT_STREAM_STATUS, (uint32_t)(5+len+1), msg);
  770. tor_free(msg);
  771. return 0;
  772. }
  773. /** Something has happened to the OR connection <b>conn</b>: tell any
  774. * interested control connections. */
  775. int
  776. control_event_or_conn_status(connection_t *conn,or_conn_status_event_t tp)
  777. {
  778. char buf[HEX_DIGEST_LEN+3]; /* status, dollar, identity, NUL */
  779. size_t len;
  780. tor_assert(conn->type == CONN_TYPE_OR);
  781. if (!EVENT_IS_INTERESTING(EVENT_OR_CONN_STATUS))
  782. return 0;
  783. buf[0] = (uint8_t)tp;
  784. strlcpy(buf+1,conn->nickname,sizeof(buf)-1);
  785. len = strlen(buf+1);
  786. send_control_event(EVENT_OR_CONN_STATUS, (uint32_t)(len+1), buf);
  787. return 0;
  788. }
  789. /** A second or more has elapsed: tell any interested control
  790. * connections how much bandwidth we used. */
  791. int
  792. control_event_bandwidth_used(uint32_t n_read, uint32_t n_written)
  793. {
  794. char buf[8];
  795. if (!EVENT_IS_INTERESTING(EVENT_BANDWIDTH_USED))
  796. return 0;
  797. set_uint32(buf, htonl(n_read));
  798. set_uint32(buf+4, htonl(n_written));
  799. send_control_event(EVENT_BANDWIDTH_USED, 8, buf);
  800. return 0;
  801. }
  802. /** We got a log message: tell any interested control connections. */
  803. void
  804. control_event_logmsg(int severity, const char *msg)
  805. {
  806. size_t len;
  807. if (severity > LOG_NOTICE) /* Less important than notice? ignore for now. */
  808. return;
  809. if (!EVENT_IS_INTERESTING(EVENT_WARNING))
  810. return;
  811. len = strlen(msg);
  812. send_control_event(EVENT_WARNING, (uint32_t)(len+1), msg);
  813. }
  814. /** DOCDOC */
  815. int control_event_descriptors_changed(smartlist_t *routers)
  816. {
  817. size_t len;
  818. char *msg;
  819. smartlist_t *identities;
  820. char buf[HEX_DIGEST_LEN+1];
  821. if (!EVENT_IS_INTERESTING(EVENT_NEW_DESC))
  822. return 0;
  823. identities = smartlist_create();
  824. SMARTLIST_FOREACH(routers, routerinfo_t *, r,
  825. {
  826. base16_encode(buf,sizeof(buf),r->identity_digest,DIGEST_LEN);
  827. smartlist_add(identities, tor_strdup(buf));
  828. });
  829. msg = smartlist_join_strings(identities, ",", 1, &len);
  830. send_control_event(EVENT_NEW_DESC, len+1, msg);
  831. SMARTLIST_FOREACH(identities, char *, cp, tor_free(cp));
  832. smartlist_free(identities);
  833. tor_free(msg);
  834. return 0;
  835. }
  836. /** Choose a random authentication cookie and write it to disk.
  837. * Anybody who can read the cookie from disk will be considered
  838. * authorized to use the control connection. */
  839. int
  840. init_cookie_authentication(int enabled)
  841. {
  842. char fname[512];
  843. if (!enabled) {
  844. authentication_cookie_is_set = 0;
  845. return 0;
  846. }
  847. tor_snprintf(fname, sizeof(fname), "%s/control_auth_cookie",
  848. get_options()->DataDirectory);
  849. crypto_rand(authentication_cookie, AUTHENTICATION_COOKIE_LEN);
  850. authentication_cookie_is_set = 1;
  851. if (write_bytes_to_file(fname, authentication_cookie,
  852. AUTHENTICATION_COOKIE_LEN, 1)) {
  853. log_fn(LOG_WARN,"Error writing authentication cookie.");
  854. return -1;
  855. }
  856. return 0;
  857. }