control.c 20 KB

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