control.c 19 KB

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