control.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /* Copyright 2004 Roger Dingledine, Nick Mathewson. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /**
  5. * /file control.c
  6. *
  7. * /brief Implementation for Tor's control-socket interface.
  8. */
  9. #include "or.h"
  10. /* Protocol outline: a bidirectional stream, over which each side
  11. * sends a series of messages. Each message has a two-byte length field,
  12. * a two-byte typecode, and a variable-length body whose length is
  13. * given in the length field.
  14. *
  15. * By default, the server only sends messages in response to client messages.
  16. * Every client message gets a message in response. The client may, however,
  17. * _request_ that other messages be delivered asynchronously.
  18. *
  19. *
  20. * Every message type is either client-only or server-only, and every
  21. * server message type is either synchronous-only (only occurs in
  22. * response to a client request) or asynchronous-only (never is an
  23. * answer to a client request.
  24. *
  25. * See control-spec.txt for full details.
  26. */
  27. /* Recognized message type codes. */
  28. #define CONTROL_CMD_ERROR 0x0000
  29. #define CONTROL_CMD_DONE 0x0001
  30. #define CONTROL_CMD_SETCONF 0x0002
  31. #define CONTROL_CMD_GETCONF 0x0003
  32. #define CONTROL_CMD_CONFVALUE 0x0004
  33. #define CONTROL_CMD_SETEVENTS 0x0005
  34. #define CONTROL_CMD_EVENT 0x0006
  35. #define CONTROL_CMD_AUTHENTICATE 0x0007
  36. #define CONTROL_CMD_SAFECONF 0x0008
  37. #define _CONTROL_CMD_MAX_RECOGNIZED 0x0008
  38. /* Recognized error codes. */
  39. #define ERR_UNSPECIFIED 0x0000
  40. #define ERR_INTERNAL 0x0001
  41. #define ERR_UNRECOGNIZED_TYPE 0x0002
  42. #define ERR_SYNTAX 0x0003
  43. #define ERR_UNRECOGNIZED_CONFIG_KEY 0x0004
  44. #define ERR_INVALID_CONFIG_VALUE 0x0005
  45. #define ERR_UNRECOGNIZED_EVENT_CODE 0x0006
  46. #define ERR_UNAUTHORIZED 0x0007
  47. #define ERR_REJECTED_AUTHENTICATION 0x0008
  48. /* Recognized asynchronous event types. */
  49. #define _EVENT_MIN 0x0001
  50. #define EVENT_CIRCUIT_STATUS 0x0001
  51. #define EVENT_STREAM_STATUS 0x0002
  52. #define EVENT_OR_CONN_STATUS 0x0003
  53. #define EVENT_BANDWIDTH_USED 0x0004
  54. #define EVENT_WARNING 0x0005
  55. #define _EVENT_MAX 0x0005
  56. /** Array mapping from message type codes to human-readable message
  57. * type names. */
  58. static const char * CONTROL_COMMANDS[] = {
  59. "error",
  60. "done",
  61. "setconf",
  62. "getconf",
  63. "confvalue",
  64. "setevents",
  65. "events",
  66. "authenticate",
  67. "saveconf",
  68. };
  69. /** Bitfield: The bit 1&lt;&lt;e is set if <b>any</b> open control
  70. * connection is interested in events of type <b>e</b>. We use this
  71. * so that we can decide to skip generating event messages that nobody
  72. * has interest in without having to walk over the global connection
  73. * list to find out.
  74. **/
  75. static uint32_t global_event_mask = 0;
  76. /** Macro: true if any control connection is interested in events of type
  77. * <b>e</b>. */
  78. #define EVENT_IS_INTERESTING(e) (global_event_mask & (1<<(e)))
  79. /** If we're using cookie-type authentication, how long should our cookies be?
  80. */
  81. #define AUTHENTICATION_COOKIE_LEN 32
  82. /** If true, we've set authentication_cookie to a secret code and
  83. * stored it to disk. */
  84. static int authentication_cookie_is_set = 0;
  85. static char authentication_cookie[AUTHENTICATION_COOKIE_LEN];
  86. static void update_global_event_mask(void);
  87. static void send_control_message(connection_t *conn, uint16_t type,
  88. uint16_t len, const char *body);
  89. static void send_control_done(connection_t *conn);
  90. static void send_control_error(connection_t *conn, uint16_t error,
  91. const char *message);
  92. static void send_control_event(uint16_t event, uint16_t len, const char *body);
  93. static int handle_control_setconf(connection_t *conn, uint16_t len,
  94. char *body);
  95. static int handle_control_getconf(connection_t *conn, uint16_t len,
  96. const char *body);
  97. static int handle_control_setevents(connection_t *conn, uint16_t len,
  98. const char *body);
  99. static int handle_control_authenticate(connection_t *conn, uint16_t len,
  100. const char *body);
  101. static int handle_control_saveconf(connection_t *conn, uint16_t len,
  102. const char *body);
  103. /** Given a possibly invalid message type code <b>cmd</b>, return a
  104. * human-readable string equivalent. */
  105. static INLINE const char *
  106. control_cmd_to_string(uint16_t cmd)
  107. {
  108. return (cmd<=_CONTROL_CMD_MAX_RECOGNIZED) ? CONTROL_COMMANDS[cmd] : "Unknown";
  109. }
  110. /** Set <b>global_event_mask</b> to the bitwise OR of each live control
  111. * connection's event_mask field. */
  112. static void update_global_event_mask(void)
  113. {
  114. connection_t **conns;
  115. int n_conns, i;
  116. global_event_mask = 0;
  117. get_connection_array(&conns, &n_conns);
  118. for (i = 0; i < n_conns; ++i) {
  119. if (conns[i]->type == CONN_TYPE_CONTROL &&
  120. conns[i]->state == CONTROL_CONN_STATE_OPEN) {
  121. global_event_mask |= conns[i]->event_mask;
  122. }
  123. }
  124. }
  125. /** Send a message of type <b>type</b> containing <b>len</b> bytes
  126. * from <b>body</b> along the control connection <b>conn</b> */
  127. static void
  128. send_control_message(connection_t *conn, uint16_t type, uint16_t len,
  129. const char *body)
  130. {
  131. char buf[4];
  132. tor_assert(conn);
  133. tor_assert(len || !body);
  134. tor_assert(type <= _CONTROL_CMD_MAX_RECOGNIZED);
  135. set_uint32(buf, htons(len));
  136. set_uint32(buf+2, htons(type));
  137. connection_write_to_buf(buf, 4, conn);
  138. if (len)
  139. connection_write_to_buf(body, len, conn);
  140. }
  141. /** Send a "DONE" message down the control connection <b>conn</b> */
  142. static void
  143. send_control_done(connection_t *conn)
  144. {
  145. send_control_message(conn, CONTROL_CMD_DONE, 0, NULL);
  146. }
  147. /** Send an error message with error code <b>error</b> and body
  148. * <b>message</b> down the connection <b>conn</b> */
  149. static void
  150. send_control_error(connection_t *conn, uint16_t error, const char *message)
  151. {
  152. char buf[256];
  153. size_t len;
  154. set_uint16(buf, htons(error));
  155. len = strlen(message);
  156. tor_assert(len < (256-2));
  157. memcpy(buf+2, message, len);
  158. send_control_message(conn, CONTROL_CMD_ERROR, (uint16_t)(len+2), buf);
  159. }
  160. /** Send an 'event' message of event type <b>event</b>, containing
  161. * <b>len</b> bytes in <b>body</b> to every control connection that
  162. * is interested in it. */
  163. static void
  164. send_control_event(uint16_t event, uint16_t len, const char *body)
  165. {
  166. connection_t **conns;
  167. int n_conns, i;
  168. get_connection_array(&conns, &n_conns);
  169. for (i = 0; i < n_conns; ++i) {
  170. if (conns[i]->type == CONN_TYPE_CONTROL &&
  171. conns[i]->state == CONTROL_CONN_STATE_OPEN &&
  172. conns[i]->event_mask & (1<<event)) {
  173. send_control_message(conns[i], CONTROL_CMD_EVENT, len, body);
  174. }
  175. }
  176. }
  177. /** Called when we receive a SETCONF message: parse the body and try
  178. * to update our configuration. Reply with a DONE or ERROR message. */
  179. static int
  180. handle_control_setconf(connection_t *conn, uint16_t len, char *body)
  181. {
  182. int r;
  183. struct config_line_t *lines=NULL;
  184. or_options_t *options = get_options();
  185. if (config_get_lines(body, &lines) < 0) {
  186. log_fn(LOG_WARN,"Controller gave us config lines we can't parse.");
  187. send_control_error(conn, ERR_SYNTAX, "Couldn't parse configuration");
  188. return 0;
  189. }
  190. if ((r=config_trial_assign(&options, lines, 1)) < 0) {
  191. log_fn(LOG_WARN,"Controller gave us config lines that didn't validate.");
  192. if (r==-1) {
  193. send_control_error(conn, ERR_UNRECOGNIZED_CONFIG_KEY,
  194. "Unrecognized option");
  195. } else {
  196. send_control_error(conn, ERR_INVALID_CONFIG_VALUE,"Invalid option value");
  197. }
  198. config_free_lines(lines);
  199. return 0;
  200. }
  201. set_options(options); /* put the new one into place */
  202. config_free_lines(lines);
  203. send_control_done(conn);
  204. return 0;
  205. }
  206. /** Called when we receive a GETCONF message. Parse the request, and
  207. * reply with a CONFVALUE or an ERROR message */
  208. static int
  209. handle_control_getconf(connection_t *conn, uint16_t body_len, const char *body)
  210. {
  211. smartlist_t *questions = NULL;
  212. smartlist_t *answers = NULL;
  213. char *msg = NULL;
  214. size_t msg_len;
  215. or_options_t *options = get_options();
  216. questions = smartlist_create();
  217. smartlist_split_string(questions, body, "\n",
  218. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  219. answers = smartlist_create();
  220. SMARTLIST_FOREACH(questions, const char *, q,
  221. {
  222. struct config_line_t *answer = config_get_assigned_option(options,q);
  223. if (!answer) {
  224. send_control_error(conn, ERR_UNRECOGNIZED_CONFIG_KEY, body);
  225. goto done;
  226. } else {
  227. while (answer) {
  228. struct config_line_t *next;
  229. size_t alen = strlen(answer->key)+strlen(answer->value)+2;
  230. char *astr = tor_malloc(alen);
  231. tor_snprintf(astr, alen, "%s %s\n", answer->key, answer->value);
  232. smartlist_add(answers, astr);
  233. next = answer->next;
  234. tor_free(answer->key);
  235. tor_free(answer->value);
  236. tor_free(answer);
  237. answer = next;
  238. }
  239. }
  240. });
  241. msg = smartlist_join_strings(answers, "", 0, &msg_len);
  242. send_control_message(conn, CONTROL_CMD_CONFVALUE,
  243. (uint16_t)msg_len, msg);
  244. done:
  245. if (answers) SMARTLIST_FOREACH(answers, char *, cp, tor_free(cp));
  246. if (questions) SMARTLIST_FOREACH(questions, char *, cp, tor_free(cp));
  247. smartlist_free(answers);
  248. smartlist_free(questions);
  249. tor_free(msg);
  250. return 0;
  251. }
  252. /** Called when we get a SETEVENTS message: update conn->event_mask,
  253. * and reply with DONE or ERROR. */
  254. static int
  255. handle_control_setevents(connection_t *conn, uint16_t len, const char *body)
  256. {
  257. uint16_t event_code;
  258. uint32_t event_mask = 0;
  259. if (len % 2) {
  260. send_control_error(conn, ERR_SYNTAX,
  261. "Odd number of bytes in setevents message");
  262. return 0;
  263. }
  264. for (; len; len -= 2, body += 2) {
  265. event_code = ntohs(get_uint16(body));
  266. if (event_code < _EVENT_MIN || event_code > _EVENT_MAX) {
  267. send_control_error(conn, ERR_UNRECOGNIZED_EVENT_CODE,
  268. "Unrecognized event code");
  269. return 0;
  270. }
  271. event_mask |= (1 << event_code);
  272. }
  273. conn->event_mask = event_mask;
  274. update_global_event_mask();
  275. send_control_done(conn);
  276. return 0;
  277. }
  278. /** Called when we get an AUTHENTICATE message. Check whether the
  279. * authentication is valid, and if so, update the connection's state to
  280. * OPEN. Reply with DONE or ERROR.
  281. */
  282. static int
  283. handle_control_authenticate(connection_t *conn, uint16_t len, const char *body)
  284. {
  285. or_options_t *options = get_options();
  286. if (len == AUTHENTICATION_COOKIE_LEN &&
  287. authentication_cookie_is_set &&
  288. !memcmp(authentication_cookie, body, len)) {
  289. goto ok;
  290. }
  291. if (options->HashedControlPassword) {
  292. char expected[S2K_SPECIFIER_LEN+DIGEST_LEN];
  293. char received[DIGEST_LEN];
  294. if (base64_decode(expected,sizeof(expected),
  295. options->HashedControlPassword,
  296. strlen(options->HashedControlPassword))<0) {
  297. /* XXXX009 NM we should warn sooner. */
  298. log_fn(LOG_WARN,"Couldn't decode HashedControlPassword: invalid base64");
  299. goto err;
  300. }
  301. secret_to_key(received,DIGEST_LEN,body,len,expected);
  302. if (!memcmp(expected+S2K_SPECIFIER_LEN, received, DIGEST_LEN))
  303. goto ok;
  304. goto err;
  305. }
  306. if (len == 0) {
  307. /* if Tor doesn't demand any stronger authentication, then
  308. * the controller can get in with a blank auth line. */
  309. goto ok;
  310. }
  311. err:
  312. send_control_error(conn, ERR_REJECTED_AUTHENTICATION,"Authentication failed");
  313. return 0;
  314. ok:
  315. log_fn(LOG_INFO, "Authenticated control connection (%d)", conn->s);
  316. send_control_done(conn);
  317. conn->state = CONTROL_CONN_STATE_OPEN;
  318. return 0;
  319. }
  320. static int
  321. handle_control_saveconf(connection_t *conn, uint16_t len,
  322. const char *body)
  323. {
  324. send_control_error(conn, ERR_INTERNAL, "Not implemented");
  325. return 0;
  326. }
  327. /** Called when <b>conn</b> has no more bytes left on its outbuf. */
  328. int
  329. connection_control_finished_flushing(connection_t *conn) {
  330. tor_assert(conn);
  331. tor_assert(conn->type == CONN_TYPE_CONTROL);
  332. connection_stop_writing(conn);
  333. return 0;
  334. }
  335. /** Called when <b>conn</b> has received more bytes on its inbuf, or has
  336. * gotten its socket closed. */
  337. int
  338. connection_control_process_inbuf(connection_t *conn) {
  339. uint16_t body_len, command_type;
  340. char *body;
  341. tor_assert(conn);
  342. tor_assert(conn->type == CONN_TYPE_CONTROL);
  343. if(conn->inbuf_reached_eof) {
  344. log_fn(LOG_INFO,"Control connection reached EOF. Closing.");
  345. connection_mark_for_close(conn);
  346. return 0;
  347. }
  348. again:
  349. /* Try to suck a control message from the buffer. */
  350. switch(fetch_from_buf_control(conn->inbuf, &body_len, &command_type, &body))
  351. {
  352. case -1:
  353. tor_free(body);
  354. log_fn(LOG_WARN, "Error in control command. Failing.");
  355. return -1;
  356. case 0:
  357. /* Control command not all here yet. Wait. */
  358. return 0;
  359. case 1:
  360. /* We got a command. Process it. */
  361. break;
  362. default:
  363. tor_assert(0);
  364. }
  365. /* We got a command. If we need authentication, only authentication
  366. * commands will be considered. */
  367. if (conn->state == CONTROL_CONN_STATE_NEEDAUTH &&
  368. command_type != CONTROL_CMD_AUTHENTICATE) {
  369. log_fn(LOG_WARN, "Rejecting '%s' command; authentication needed.",
  370. control_cmd_to_string(command_type));
  371. send_control_error(conn, ERR_UNAUTHORIZED, "Authentication required");
  372. tor_free(body);
  373. goto again;
  374. }
  375. /* Okay, we're willing to process the command. */
  376. switch(command_type)
  377. {
  378. case CONTROL_CMD_SETCONF:
  379. if (handle_control_setconf(conn, body_len, body))
  380. return -1;
  381. break;
  382. case CONTROL_CMD_GETCONF:
  383. if (handle_control_getconf(conn, body_len, body))
  384. return -1;
  385. break;
  386. case CONTROL_CMD_SETEVENTS:
  387. if (handle_control_setevents(conn, body_len, body))
  388. return -1;
  389. break;
  390. case CONTROL_CMD_AUTHENTICATE:
  391. if (handle_control_authenticate(conn, body_len, body))
  392. return -1;
  393. break;
  394. case CONTROL_CMD_SAFECONF:
  395. if (handle_control_saveconf(conn, body_len, body))
  396. return -1;
  397. break;
  398. case CONTROL_CMD_ERROR:
  399. case CONTROL_CMD_DONE:
  400. case CONTROL_CMD_CONFVALUE:
  401. case CONTROL_CMD_EVENT:
  402. log_fn(LOG_WARN, "Received client-only '%s' command; ignoring.",
  403. control_cmd_to_string(command_type));
  404. send_control_error(conn, ERR_UNRECOGNIZED_TYPE,
  405. "Command type only valid from server to tor client");
  406. break;
  407. default:
  408. log_fn(LOG_WARN, "Received unrecognized command type %d; ignoring.",
  409. (int)command_type);
  410. send_control_error(conn, ERR_UNRECOGNIZED_TYPE,
  411. "Unrecognized command type");
  412. break;
  413. }
  414. tor_free(body);
  415. goto again; /* There might be more data. */
  416. }
  417. /** Something has happened to circuit <b>circ</b>: tell any interested
  418. * control connections. */
  419. int
  420. control_event_circuit_status(circuit_t *circ, circuit_status_event_t tp)
  421. {
  422. char *path, *msg;
  423. size_t path_len;
  424. if (!EVENT_IS_INTERESTING(EVENT_CIRCUIT_STATUS))
  425. return 0;
  426. tor_assert(circ);
  427. tor_assert(CIRCUIT_IS_ORIGIN(circ));
  428. path = circuit_list_path(circ);
  429. path_len = strlen(path);
  430. msg = tor_malloc(1+4+path_len+1); /* event, circid, path, NUL. */
  431. msg[0] = (uint8_t) tp;
  432. set_uint32(msg+1, htonl(circ->global_identifier));
  433. strlcpy(msg+5,path,path_len+1);
  434. send_control_event(EVENT_STREAM_STATUS, (uint16_t)(path_len+6), msg);
  435. tor_free(path);
  436. tor_free(msg);
  437. return 0;
  438. }
  439. /** Something has happened to the stream associated with AP connection
  440. * <b>conn</b>: tell any interested control connections. */
  441. int
  442. control_event_stream_status(connection_t *conn, stream_status_event_t tp)
  443. {
  444. char *msg;
  445. size_t len;
  446. tor_assert(conn->type == CONN_TYPE_AP);
  447. tor_assert(conn->socks_request);
  448. if (!EVENT_IS_INTERESTING(EVENT_STREAM_STATUS))
  449. return 0;
  450. len = strlen(conn->socks_request->address);
  451. msg = tor_malloc(5+len+1);
  452. msg[0] = (uint8_t) tp;
  453. set_uint32(msg+1, htonl(conn->s)); /* ???? Is this a security problem? */
  454. strlcpy(msg+5, conn->socks_request->address, len+1);
  455. send_control_event(EVENT_STREAM_STATUS, (uint16_t)(5+len+1), msg);
  456. tor_free(msg);
  457. return 0;
  458. }
  459. /** Something has happened to the OR connection <b>conn</b>: tell any
  460. * interested control connections. */
  461. int
  462. control_event_or_conn_status(connection_t *conn,or_conn_status_event_t tp)
  463. {
  464. char buf[HEX_DIGEST_LEN+3]; /* status, dollar, identity, NUL */
  465. size_t len;
  466. tor_assert(conn->type == CONN_TYPE_OR);
  467. if (!EVENT_IS_INTERESTING(EVENT_OR_CONN_STATUS))
  468. return 0;
  469. buf[0] = (uint8_t)tp;
  470. strlcpy(buf+1,conn->nickname,sizeof(buf)-1);
  471. len = strlen(buf+1);
  472. send_control_event(EVENT_OR_CONN_STATUS, (uint16_t)(len+1), buf);
  473. return 0;
  474. }
  475. /** A second or more has elapsed: tell any interested control
  476. * connections how much bandwidth we used. */
  477. int
  478. control_event_bandwidth_used(uint32_t n_read, uint32_t n_written)
  479. {
  480. char buf[8];
  481. if (!EVENT_IS_INTERESTING(EVENT_BANDWIDTH_USED))
  482. return 0;
  483. set_uint32(buf, htonl(n_read));
  484. set_uint32(buf+4, htonl(n_read));
  485. send_control_event(EVENT_BANDWIDTH_USED, 8, buf);
  486. return 0;
  487. }
  488. /** We got a log message: tell any interested control connections. */
  489. void
  490. control_event_logmsg(int severity, const char *msg)
  491. {
  492. size_t len;
  493. if (severity > LOG_WARN) /* Less important than warning? ignore for now. */
  494. return;
  495. if (!EVENT_IS_INTERESTING(EVENT_WARNING))
  496. return;
  497. len = strlen(msg);
  498. send_control_event(EVENT_WARNING, (uint16_t)(len+1), msg);
  499. }
  500. /** Choose a random authentication cookie and write it to disk.
  501. * Anybody who can read the cookie from disk will be considered
  502. * authorized to use the control connection. */
  503. int
  504. init_cookie_authentication(void)
  505. {
  506. char fname[512];
  507. /* XXXX009 NM add config option to disable this. */
  508. tor_snprintf(fname, sizeof(fname), "%s/control_auth_cookie",
  509. get_data_directory());
  510. crypto_rand(authentication_cookie, AUTHENTICATION_COOKIE_LEN);
  511. authentication_cookie_is_set = 1;
  512. if (write_bytes_to_file(fname, authentication_cookie,
  513. AUTHENTICATION_COOKIE_LEN, 1)) {
  514. log_fn(LOG_WARN,"Error writing authentication cookie.");
  515. return -1;
  516. }
  517. return 0;
  518. }
  519. /*
  520. Local Variabls:
  521. mode:c
  522. indent-tabs-mode:nil
  523. c-basic-offset:2
  524. End:
  525. */