control.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. /* Copyright 2004-2005 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_REDIRECTSTREAM 0x0012
  48. #define CONTROL_CMD_CLOSESTREAM 0x0013
  49. #define CONTROL_CMD_CLOSECIRCUIT 0x0014
  50. #define _CONTROL_CMD_MAX_RECOGNIZED 0x0014
  51. /* Recognized error codes. */
  52. #define ERR_UNSPECIFIED 0x0000
  53. #define ERR_INTERNAL 0x0001
  54. #define ERR_UNRECOGNIZED_TYPE 0x0002
  55. #define ERR_SYNTAX 0x0003
  56. #define ERR_UNRECOGNIZED_CONFIG_KEY 0x0004
  57. #define ERR_INVALID_CONFIG_VALUE 0x0005
  58. #define ERR_UNRECOGNIZED_EVENT_CODE 0x0006
  59. #define ERR_UNAUTHORIZED 0x0007
  60. #define ERR_REJECTED_AUTHENTICATION 0x0008
  61. #define ERR_RESOURCE_EXHAUSETED 0x0009
  62. #define ERR_NO_STREAM 0x000A
  63. #define ERR_NO_CIRC 0x000B
  64. #define ERR_NO_ROUTER 0x000C
  65. /* Recognized asynchronous event types. */
  66. #define _EVENT_MIN 0x0001
  67. #define EVENT_CIRCUIT_STATUS 0x0001
  68. #define EVENT_STREAM_STATUS 0x0002
  69. #define EVENT_OR_CONN_STATUS 0x0003
  70. #define EVENT_BANDWIDTH_USED 0x0004
  71. #define EVENT_LOG_OBSOLETE 0x0005
  72. #define EVENT_NEW_DESC 0x0006
  73. #define EVENT_DEBUG_MSG 0x0007
  74. #define EVENT_INFO_MSG 0x0008
  75. #define EVENT_NOTICE_MSG 0x0009
  76. #define EVENT_WARN_MSG 0x000A
  77. #define EVENT_ERR_MSG 0x000B
  78. #define _EVENT_MAX 0x000B
  79. /** Array mapping from message type codes to human-readable message
  80. * type names. */
  81. static const char * CONTROL_COMMANDS[_CONTROL_CMD_MAX_RECOGNIZED+1] = {
  82. "error",
  83. "done",
  84. "setconf",
  85. "getconf",
  86. "confvalue",
  87. "setevents",
  88. "events",
  89. "authenticate",
  90. "saveconf",
  91. "signal",
  92. "mapaddress",
  93. "getinfo",
  94. "infovalue",
  95. "extendcircuit",
  96. "attachstream",
  97. "postdescriptor",
  98. "fragmentheader",
  99. "fragment",
  100. };
  101. /** Bitfield: The bit 1&lt;&lt;e is set if <b>any</b> open control
  102. * connection is interested in events of type <b>e</b>. We use this
  103. * so that we can decide to skip generating event messages that nobody
  104. * has interest in without having to walk over the global connection
  105. * list to find out.
  106. **/
  107. static uint32_t global_event_mask = 0;
  108. /** Macro: true if any control connection is interested in events of type
  109. * <b>e</b>. */
  110. #define EVENT_IS_INTERESTING(e) (global_event_mask & (1<<(e)))
  111. /** If we're using cookie-type authentication, how long should our cookies be?
  112. */
  113. #define AUTHENTICATION_COOKIE_LEN 32
  114. /** If true, we've set authentication_cookie to a secret code and
  115. * stored it to disk. */
  116. static int authentication_cookie_is_set = 0;
  117. static char authentication_cookie[AUTHENTICATION_COOKIE_LEN];
  118. static void update_global_event_mask(void);
  119. static void send_control_message(connection_t *conn, uint16_t type,
  120. uint32_t len, const char *body);
  121. static void send_control_done(connection_t *conn);
  122. static void send_control_done2(connection_t *conn, const char *msg, size_t len);
  123. static void send_control_error(connection_t *conn, uint16_t error,
  124. const char *message);
  125. static void send_control_event(uint16_t event, uint32_t len, const char *body);
  126. static int handle_control_setconf(connection_t *conn, uint32_t len,
  127. char *body);
  128. static int handle_control_getconf(connection_t *conn, uint32_t len,
  129. const char *body);
  130. static int handle_control_setevents(connection_t *conn, uint32_t len,
  131. const char *body);
  132. static int handle_control_authenticate(connection_t *conn, uint32_t len,
  133. const char *body);
  134. static int handle_control_saveconf(connection_t *conn, uint32_t len,
  135. const char *body);
  136. static int handle_control_signal(connection_t *conn, uint32_t len,
  137. const char *body);
  138. static int handle_control_mapaddress(connection_t *conn, uint32_t len,
  139. const char *body);
  140. static int handle_control_getinfo(connection_t *conn, uint32_t len,
  141. const char *body);
  142. static int handle_control_extendcircuit(connection_t *conn, uint32_t len,
  143. const char *body);
  144. static int handle_control_attachstream(connection_t *conn, uint32_t len,
  145. const char *body);
  146. static int handle_control_postdescriptor(connection_t *conn, uint32_t len,
  147. const char *body);
  148. static int handle_control_redirectstream(connection_t *conn, uint32_t len,
  149. const char *body);
  150. static int handle_control_closestream(connection_t *conn, uint32_t len,
  151. const char *body);
  152. static int handle_control_closecircuit(connection_t *conn, uint32_t len,
  153. const char *body);
  154. /** Given a possibly invalid message type code <b>cmd</b>, return a
  155. * human-readable string equivalent. */
  156. static INLINE const char *
  157. control_cmd_to_string(uint16_t cmd)
  158. {
  159. return (cmd<=_CONTROL_CMD_MAX_RECOGNIZED) ? CONTROL_COMMANDS[cmd] : "Unknown";
  160. }
  161. static INLINE int
  162. event_to_log_severity(int event)
  163. {
  164. switch (event) {
  165. case EVENT_DEBUG_MSG: return LOG_DEBUG;
  166. case EVENT_INFO_MSG: return LOG_INFO;
  167. case EVENT_NOTICE_MSG: return LOG_NOTICE;
  168. case EVENT_WARN_MSG: return LOG_WARN;
  169. case EVENT_ERR_MSG: return LOG_ERR;
  170. default: return -1;
  171. }
  172. }
  173. static INLINE int
  174. log_severity_to_event(int severity)
  175. {
  176. switch (severity) {
  177. case LOG_DEBUG: return EVENT_DEBUG_MSG;
  178. case LOG_INFO: return EVENT_INFO_MSG;
  179. case LOG_NOTICE: return EVENT_NOTICE_MSG;
  180. case LOG_WARN: return EVENT_WARN_MSG;
  181. case LOG_ERR: return EVENT_ERR_MSG;
  182. default: return -1;
  183. }
  184. }
  185. /** Set <b>global_event_mask</b> to the bitwise OR of each live control
  186. * connection's event_mask field. */
  187. static void update_global_event_mask(void)
  188. {
  189. connection_t **conns;
  190. int n_conns, i;
  191. global_event_mask = 0;
  192. get_connection_array(&conns, &n_conns);
  193. for (i = 0; i < n_conns; ++i) {
  194. if (conns[i]->type == CONN_TYPE_CONTROL &&
  195. conns[i]->state == CONTROL_CONN_STATE_OPEN) {
  196. global_event_mask |= conns[i]->event_mask;
  197. }
  198. }
  199. adjust_event_log_severity();
  200. }
  201. void adjust_event_log_severity(void) {
  202. int i;
  203. int min_log_event=EVENT_ERR_MSG, max_log_event=EVENT_DEBUG_MSG;
  204. for (i = EVENT_DEBUG_MSG; i <= EVENT_ERR_MSG; ++i) {
  205. if (EVENT_IS_INTERESTING(i)) {
  206. min_log_event = i;
  207. break;
  208. }
  209. }
  210. for (i = EVENT_ERR_MSG; i >= EVENT_DEBUG_MSG; --i) {
  211. if (EVENT_IS_INTERESTING(i)) {
  212. max_log_event = i;
  213. break;
  214. }
  215. }
  216. if (EVENT_IS_INTERESTING(EVENT_LOG_OBSOLETE)) {
  217. if (min_log_event > EVENT_NOTICE_MSG)
  218. min_log_event = EVENT_NOTICE_MSG;
  219. if (max_log_event < EVENT_ERR_MSG)
  220. max_log_event = EVENT_ERR_MSG;
  221. }
  222. change_callback_log_severity(event_to_log_severity(min_log_event),
  223. event_to_log_severity(max_log_event),
  224. control_event_logmsg);
  225. }
  226. /** Send a message of type <b>type</b> containing <b>len</b> bytes
  227. * from <b>body</b> along the control connection <b>conn</b> */
  228. static void
  229. send_control_message(connection_t *conn, uint16_t type, uint32_t len,
  230. const char *body)
  231. {
  232. char buf[10];
  233. tor_assert(conn);
  234. tor_assert(len || !body);
  235. tor_assert(type <= _CONTROL_CMD_MAX_RECOGNIZED);
  236. if (len < 65536) {
  237. set_uint16(buf, htons(len));
  238. set_uint16(buf+2, htons(type));
  239. connection_write_to_buf(buf, 4, conn);
  240. if (len)
  241. connection_write_to_buf(body, len, conn);
  242. } else {
  243. set_uint16(buf, htons(65535));
  244. set_uint16(buf+2, htons(CONTROL_CMD_FRAGMENTHEADER));
  245. set_uint16(buf+4, htons(type));
  246. set_uint32(buf+6, htonl(len));
  247. connection_write_to_buf(buf, 10, conn);
  248. connection_write_to_buf(body, 65535-6, conn);
  249. len -= (65535-6);
  250. body += (65535-6);
  251. while (len) {
  252. size_t chunklen = (len<65535)?len:65535;
  253. set_uint16(buf, htons((uint16_t)chunklen));
  254. set_uint16(buf+2, htons(CONTROL_CMD_FRAGMENT));
  255. connection_write_to_buf(buf, 4, conn);
  256. connection_write_to_buf(body, chunklen, conn);
  257. len -= chunklen;
  258. body += chunklen;
  259. }
  260. }
  261. }
  262. /** Send a "DONE" message down the control connection <b>conn</b> */
  263. static void
  264. send_control_done(connection_t *conn)
  265. {
  266. send_control_message(conn, CONTROL_CMD_DONE, 0, NULL);
  267. }
  268. static void send_control_done2(connection_t *conn, const char *msg, size_t len)
  269. {
  270. if (len==0)
  271. len = strlen(msg);
  272. send_control_message(conn, CONTROL_CMD_DONE, len, msg);
  273. }
  274. /** Send an error message with error code <b>error</b> and body
  275. * <b>message</b> down the connection <b>conn</b> */
  276. static void
  277. send_control_error(connection_t *conn, uint16_t error, const char *message)
  278. {
  279. char buf[256];
  280. size_t len;
  281. set_uint16(buf, htons(error));
  282. len = strlen(message);
  283. tor_assert(len < (256-2));
  284. memcpy(buf+2, message, len);
  285. send_control_message(conn, CONTROL_CMD_ERROR, (uint16_t)(len+2), buf);
  286. }
  287. /** Send an 'event' message of event type <b>event</b>, containing
  288. * <b>len</b> bytes in <b>body</b> to every control connection that
  289. * is interested in it. */
  290. static void
  291. send_control_event(uint16_t event, uint32_t len, const char *body)
  292. {
  293. connection_t **conns;
  294. int n_conns, i;
  295. size_t buflen;
  296. char *buf;
  297. tor_assert(event >= _EVENT_MIN && event <= _EVENT_MAX);
  298. buflen = len + 2;
  299. buf = tor_malloc_zero(buflen);
  300. set_uint16(buf, htons(event));
  301. memcpy(buf+2, body, len);
  302. get_connection_array(&conns, &n_conns);
  303. for (i = 0; i < n_conns; ++i) {
  304. if (conns[i]->type == CONN_TYPE_CONTROL &&
  305. conns[i]->state == CONTROL_CONN_STATE_OPEN &&
  306. conns[i]->event_mask & (1<<event)) {
  307. send_control_message(conns[i], CONTROL_CMD_EVENT, buflen, buf);
  308. if (event == EVENT_ERR_MSG) {
  309. _connection_controller_force_write(conns[i]);
  310. }
  311. }
  312. }
  313. tor_free(buf);
  314. }
  315. /** Called when we receive a SETCONF message: parse the body and try
  316. * to update our configuration. Reply with a DONE or ERROR message. */
  317. static int
  318. handle_control_setconf(connection_t *conn, uint32_t len, char *body)
  319. {
  320. int r;
  321. struct config_line_t *lines=NULL;
  322. if (config_get_lines(body, &lines) < 0) {
  323. log_fn(LOG_WARN,"Controller gave us config lines we can't parse.");
  324. send_control_error(conn, ERR_SYNTAX, "Couldn't parse configuration");
  325. return 0;
  326. }
  327. if ((r=config_trial_assign(lines, 1)) < 0) {
  328. log_fn(LOG_WARN,"Controller gave us config lines that didn't validate.");
  329. if (r==-1) {
  330. send_control_error(conn, ERR_UNRECOGNIZED_CONFIG_KEY,
  331. "Unrecognized option");
  332. } else {
  333. send_control_error(conn, ERR_INVALID_CONFIG_VALUE,"Invalid option value");
  334. }
  335. config_free_lines(lines);
  336. return 0;
  337. }
  338. config_free_lines(lines);
  339. if (options_act() < 0) { /* acting on them failed. die. */
  340. log_fn(LOG_ERR,"Acting on config options left us in a broken state. Dying.");
  341. exit(1);
  342. }
  343. send_control_done(conn);
  344. return 0;
  345. }
  346. /** Called when we receive a GETCONF message. Parse the request, and
  347. * reply with a CONFVALUE or an ERROR message */
  348. static int
  349. handle_control_getconf(connection_t *conn, uint32_t body_len, const char *body)
  350. {
  351. smartlist_t *questions = NULL;
  352. smartlist_t *answers = NULL;
  353. char *msg = NULL;
  354. size_t msg_len;
  355. or_options_t *options = get_options();
  356. questions = smartlist_create();
  357. smartlist_split_string(questions, body, "\n",
  358. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  359. answers = smartlist_create();
  360. SMARTLIST_FOREACH(questions, const char *, q,
  361. {
  362. int recognized = config_option_is_recognized(q);
  363. if (!recognized) {
  364. send_control_error(conn, ERR_UNRECOGNIZED_CONFIG_KEY, q);
  365. goto done;
  366. } else {
  367. struct config_line_t *answer = config_get_assigned_option(options,q);
  368. while (answer) {
  369. struct config_line_t *next;
  370. size_t alen = strlen(answer->key)+strlen(answer->value)+3;
  371. char *astr = tor_malloc(alen);
  372. tor_snprintf(astr, alen, "%s %s\n", answer->key, answer->value);
  373. smartlist_add(answers, astr);
  374. next = answer->next;
  375. tor_free(answer->key);
  376. tor_free(answer->value);
  377. tor_free(answer);
  378. answer = next;
  379. }
  380. }
  381. });
  382. msg = smartlist_join_strings(answers, "", 0, &msg_len);
  383. send_control_message(conn, CONTROL_CMD_CONFVALUE,
  384. (uint16_t)msg_len, msg_len?msg:NULL);
  385. done:
  386. if (answers) SMARTLIST_FOREACH(answers, char *, cp, tor_free(cp));
  387. if (questions) SMARTLIST_FOREACH(questions, char *, cp, tor_free(cp));
  388. smartlist_free(answers);
  389. smartlist_free(questions);
  390. tor_free(msg);
  391. return 0;
  392. }
  393. /** Called when we get a SETEVENTS message: update conn->event_mask,
  394. * and reply with DONE or ERROR. */
  395. static int
  396. handle_control_setevents(connection_t *conn, uint32_t len, const char *body)
  397. {
  398. uint16_t event_code;
  399. uint32_t event_mask = 0;
  400. if (len % 2) {
  401. send_control_error(conn, ERR_SYNTAX,
  402. "Odd number of bytes in setevents message");
  403. return 0;
  404. }
  405. for (; len; len -= 2, body += 2) {
  406. event_code = ntohs(get_uint16(body));
  407. if (event_code < _EVENT_MIN || event_code > _EVENT_MAX) {
  408. send_control_error(conn, ERR_UNRECOGNIZED_EVENT_CODE,
  409. "Unrecognized event code");
  410. return 0;
  411. }
  412. event_mask |= (1 << event_code);
  413. }
  414. conn->event_mask = event_mask;
  415. update_global_event_mask();
  416. send_control_done(conn);
  417. return 0;
  418. }
  419. /** Decode the hashed, base64'd password stored in <b>hashed</b>. If
  420. * <b>buf</b> is provided, store the hashed password in the first
  421. * S2K_SPECIFIER_LEN+DIGEST_LEN bytes of <b>buf</b>. Return 0 on
  422. * success, -1 on failure.
  423. */
  424. int
  425. decode_hashed_password(char *buf, const char *hashed)
  426. {
  427. char decoded[64];
  428. if (base64_decode(decoded, sizeof(decoded), hashed, strlen(hashed))
  429. != S2K_SPECIFIER_LEN+DIGEST_LEN) {
  430. return -1;
  431. }
  432. if (buf)
  433. memcpy(buf, decoded, sizeof(decoded));
  434. return 0;
  435. }
  436. /** Called when we get an AUTHENTICATE message. Check whether the
  437. * authentication is valid, and if so, update the connection's state to
  438. * OPEN. Reply with DONE or ERROR.
  439. */
  440. static int
  441. handle_control_authenticate(connection_t *conn, uint32_t len, const char *body)
  442. {
  443. or_options_t *options = get_options();
  444. if (options->CookieAuthentication) {
  445. if (len == AUTHENTICATION_COOKIE_LEN &&
  446. !memcmp(authentication_cookie, body, len)) {
  447. goto ok;
  448. }
  449. } else if (options->HashedControlPassword) {
  450. char expected[S2K_SPECIFIER_LEN+DIGEST_LEN];
  451. char received[DIGEST_LEN];
  452. if (decode_hashed_password(expected, options->HashedControlPassword)<0) {
  453. log_fn(LOG_WARN,"Couldn't decode HashedControlPassword: invalid base64");
  454. goto err;
  455. }
  456. secret_to_key(received,DIGEST_LEN,body,len,expected);
  457. if (!memcmp(expected+S2K_SPECIFIER_LEN, received, DIGEST_LEN))
  458. goto ok;
  459. goto err;
  460. } else {
  461. if (len == 0) {
  462. /* if Tor doesn't demand any stronger authentication, then
  463. * the controller can get in with a blank auth line. */
  464. goto ok;
  465. }
  466. goto err;
  467. }
  468. err:
  469. send_control_error(conn, ERR_REJECTED_AUTHENTICATION,"Authentication failed");
  470. return 0;
  471. ok:
  472. log_fn(LOG_INFO, "Authenticated control connection (%d)", conn->s);
  473. send_control_done(conn);
  474. conn->state = CONTROL_CONN_STATE_OPEN;
  475. return 0;
  476. }
  477. static int
  478. handle_control_saveconf(connection_t *conn, uint32_t len,
  479. const char *body)
  480. {
  481. if (save_current_config()<0) {
  482. send_control_error(conn, ERR_INTERNAL,
  483. "Unable to write configuration to disk.");
  484. } else {
  485. send_control_done(conn);
  486. }
  487. return 0;
  488. }
  489. static int
  490. handle_control_signal(connection_t *conn, uint32_t len,
  491. const char *body)
  492. {
  493. if (len != 1) {
  494. send_control_error(conn, ERR_SYNTAX,
  495. "Body of SIGNAL command too long or too short.");
  496. } else if (control_signal_act((uint8_t)body[0]) < 0) {
  497. send_control_error(conn, ERR_SYNTAX, "Unrecognized signal number.");
  498. } else {
  499. send_control_done(conn);
  500. }
  501. return 0;
  502. }
  503. static int
  504. handle_control_mapaddress(connection_t *conn, uint32_t len, const char *body)
  505. {
  506. smartlist_t *elts;
  507. smartlist_t *lines;
  508. smartlist_t *reply;
  509. char *r;
  510. size_t sz;
  511. lines = smartlist_create();
  512. elts = smartlist_create();
  513. reply = smartlist_create();
  514. smartlist_split_string(lines, body, "\n",
  515. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  516. SMARTLIST_FOREACH(lines, char *, line,
  517. {
  518. tor_strlower(line);
  519. smartlist_split_string(elts, line, " ", 0, 2);
  520. if (smartlist_len(elts) == 2) {
  521. const char *from = smartlist_get(elts,0);
  522. const char *to = smartlist_get(elts,1);
  523. if (!is_plausible_address(from)) {
  524. log_fn(LOG_WARN,"Skipping invalid argument '%s' in MapAddress msg",from);
  525. } else if (!is_plausible_address(to)) {
  526. log_fn(LOG_WARN,"Skipping invalid argument '%s' in MapAddress msg",to);
  527. } else if (!strcmp(from, ".") || !strcmp(from, "0.0.0.0")) {
  528. const char *addr = addressmap_register_virtual_address(
  529. strcmp(from,".") ? RESOLVED_TYPE_HOSTNAME : RESOLVED_TYPE_IPV4,
  530. tor_strdup(to));
  531. if (!addr) {
  532. log_fn(LOG_WARN,
  533. "Unable to allocate address for '%s' in MapAddress msg",line);
  534. } else {
  535. size_t anslen = strlen(addr)+strlen(to)+2;
  536. char *ans = tor_malloc(anslen);
  537. tor_snprintf(ans, anslen, "%s %s", addr, to);
  538. smartlist_add(reply, ans);
  539. }
  540. } else {
  541. addressmap_register(from, tor_strdup(to), 1);
  542. smartlist_add(reply, tor_strdup(line));
  543. }
  544. } else {
  545. log_fn(LOG_WARN, "Skipping MapAddress line with wrong number of items.");
  546. }
  547. SMARTLIST_FOREACH(elts, char *, cp, tor_free(cp));
  548. smartlist_clear(elts);
  549. });
  550. SMARTLIST_FOREACH(lines, char *, cp, tor_free(cp));
  551. smartlist_free(lines);
  552. smartlist_free(elts);
  553. r = smartlist_join_strings(reply, "\n", 1, &sz);
  554. send_control_done2(conn,r,sz);
  555. SMARTLIST_FOREACH(reply, char *, cp, tor_free(cp));
  556. smartlist_free(reply);
  557. tor_free(r);
  558. return 0;
  559. }
  560. /** Lookup the 'getinfo' entry <b>question</b>, and return
  561. * the answer in <b>*answer</b> (or NULL if key not recognized).
  562. * Return 0 if success, or -1 if internal error. */
  563. static int
  564. handle_getinfo_helper(const char *question, char **answer)
  565. {
  566. *answer = NULL; /* unrecognized key by default */
  567. if (!strcmp(question, "version")) {
  568. *answer = tor_strdup(VERSION);
  569. } else if (!strcmpstart(question, "desc/id/")) {
  570. routerinfo_t *ri = router_get_by_hexdigest(question+strlen("desc/id/"));
  571. if (ri && ri->signed_descriptor)
  572. *answer = tor_strdup(ri->signed_descriptor);
  573. } else if (!strcmpstart(question, "desc/name/")) {
  574. routerinfo_t *ri = router_get_by_nickname(question+strlen("desc/name/"));
  575. if (ri && ri->signed_descriptor)
  576. *answer = tor_strdup(ri->signed_descriptor);
  577. } else if (!strcmp(question, "network-status")) {
  578. routerlist_t *routerlist;
  579. router_get_routerlist(&routerlist);
  580. if (!routerlist || !routerlist->routers ||
  581. list_server_status(routerlist->routers, NULL, answer) < 0) {
  582. return -1;
  583. }
  584. } else if (!strcmpstart(question, "addr-mappings/")) {
  585. time_t min_e, max_e;
  586. smartlist_t *mappings;
  587. if (!strcmp(question, "addr-mappings/all")) {
  588. min_e = 0; max_e = TIME_MAX;
  589. } else if (!strcmp(question, "addr-mappings/cache")) {
  590. min_e = 2; max_e = TIME_MAX;
  591. } else if (!strcmp(question, "addr-mappings/config")) {
  592. min_e = 0; max_e = 0;
  593. } else if (!strcmp(question, "addr-mappings/control")) {
  594. min_e = 1; max_e = 1;
  595. } else {
  596. return 0;
  597. }
  598. mappings = smartlist_create();
  599. addressmap_get_mappings(mappings, min_e, max_e);
  600. *answer = smartlist_join_strings(mappings, "\n", 1, NULL);
  601. SMARTLIST_FOREACH(mappings, char *, cp, tor_free(cp));
  602. smartlist_free(mappings);
  603. }
  604. return 0;
  605. }
  606. static int
  607. handle_control_getinfo(connection_t *conn, uint32_t len, const char *body)
  608. {
  609. smartlist_t *questions = NULL;
  610. smartlist_t *answers = NULL;
  611. char *msg = NULL, *ans;
  612. size_t msg_len;
  613. questions = smartlist_create();
  614. smartlist_split_string(questions, body, "\n",
  615. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  616. answers = smartlist_create();
  617. SMARTLIST_FOREACH(questions, const char *, q,
  618. {
  619. if (handle_getinfo_helper(q, &ans) < 0) {
  620. send_control_error(conn, ERR_INTERNAL, body);
  621. goto done;
  622. } if (!ans) {
  623. send_control_error(conn, ERR_UNRECOGNIZED_CONFIG_KEY, body);
  624. goto done;
  625. }
  626. smartlist_add(answers, tor_strdup(q));
  627. smartlist_add(answers, ans);
  628. });
  629. msg = smartlist_join_strings2(answers, "\0", 1, 1, &msg_len);
  630. tor_assert(msg_len > 0); /* it will at least be terminated */
  631. send_control_message(conn, CONTROL_CMD_INFOVALUE,
  632. msg_len, msg);
  633. done:
  634. if (answers) SMARTLIST_FOREACH(answers, char *, cp, tor_free(cp));
  635. if (questions) SMARTLIST_FOREACH(questions, char *, cp, tor_free(cp));
  636. smartlist_free(answers);
  637. smartlist_free(questions);
  638. tor_free(msg);
  639. return 0;
  640. }
  641. static int
  642. handle_control_extendcircuit(connection_t *conn, uint32_t len,
  643. const char *body)
  644. {
  645. smartlist_t *router_nicknames, *routers;
  646. uint32_t circ_id;
  647. circuit_t *circ;
  648. char reply[4];
  649. if (len<5) {
  650. send_control_error(conn, ERR_SYNTAX, "extendcircuit message too short");
  651. return 0;
  652. }
  653. router_nicknames = smartlist_create();
  654. routers = smartlist_create();
  655. smartlist_split_string(router_nicknames, body+4, ",", 0, 0);
  656. SMARTLIST_FOREACH(router_nicknames, const char *, n,
  657. {
  658. routerinfo_t *r = router_get_by_nickname(n);
  659. if (!r) {
  660. send_control_error(conn, ERR_NO_ROUTER, n);
  661. goto done;
  662. }
  663. smartlist_add(routers, r);
  664. });
  665. if (!smartlist_len(routers)) {
  666. send_control_error(conn, ERR_SYNTAX, "No router names provided");
  667. goto done;
  668. }
  669. circ_id = ntohl(get_uint32(body));
  670. if (!circ_id) {
  671. /* start a new circuit */
  672. circ = circuit_init(CIRCUIT_PURPOSE_C_GENERAL, 0, 0, 0);
  673. } else {
  674. circ = circuit_get_by_global_id(circ_id);
  675. if (!circ) {
  676. send_control_error(conn, ERR_NO_CIRC,
  677. "No circuit found with given ID");
  678. goto done;
  679. }
  680. }
  681. /* now circ refers to something that is ready to be extended */
  682. SMARTLIST_FOREACH(routers, routerinfo_t *, r,
  683. {
  684. circuit_append_new_exit(circ, r);
  685. });
  686. /* now that we've populated the cpath, start extending */
  687. if (!circ_id) {
  688. if (circuit_handle_first_hop(circ) < 0) {
  689. circuit_mark_for_close(circ);
  690. send_control_error(conn, ERR_INTERNAL, "couldn't start circuit");
  691. goto done;
  692. }
  693. } else {
  694. if (circ->state == CIRCUIT_STATE_OPEN) {
  695. circ->state = CIRCUIT_STATE_BUILDING;
  696. if (circuit_send_next_onion_skin(circ) < 0) {
  697. log_fn(LOG_INFO,"send_next_onion_skin failed; circuit marked for closing.");
  698. circuit_mark_for_close(circ);
  699. send_control_error(conn, ERR_INTERNAL, "couldn't send onion skin");
  700. goto done;
  701. }
  702. }
  703. }
  704. set_uint32(reply, htonl(circ->global_identifier));
  705. send_control_done2(conn, reply, sizeof(reply));
  706. done:
  707. SMARTLIST_FOREACH(router_nicknames, char *, n, tor_free(n));
  708. smartlist_free(router_nicknames);
  709. smartlist_free(routers);
  710. return 0;
  711. }
  712. static int handle_control_attachstream(connection_t *conn, uint32_t len,
  713. const char *body)
  714. {
  715. uint32_t conn_id;
  716. uint32_t circ_id;
  717. connection_t *ap_conn;
  718. circuit_t *circ;
  719. if (len < 8) {
  720. send_control_error(conn, ERR_SYNTAX, "attachstream message too short");
  721. return 0;
  722. }
  723. conn_id = ntohl(get_uint32(body));
  724. circ_id = ntohl(get_uint32(body+4));
  725. if (!(ap_conn = connection_get_by_global_id(conn_id))) {
  726. send_control_error(conn, ERR_NO_STREAM,
  727. "No connection found with given ID");
  728. return 0;
  729. }
  730. if (ap_conn->state != AP_CONN_STATE_CONTROLLER_WAIT) {
  731. send_control_error(conn, ERR_NO_STREAM,
  732. "Connection was not managed by controller.");
  733. return 0;
  734. }
  735. if (!circ_id) {
  736. ap_conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  737. if (connection_ap_handshake_attach_circuit(ap_conn)<0)
  738. connection_mark_unattached_ap(ap_conn, END_STREAM_REASON_CANT_ATTACH);
  739. send_control_done(conn);
  740. return 0;
  741. }
  742. if (!(circ = circuit_get_by_global_id(circ_id))) {
  743. send_control_error(conn, ERR_NO_CIRC, "No circuit found with given ID");
  744. return 0;
  745. }
  746. if (circ->state != CIRCUIT_STATE_OPEN) {
  747. send_control_error(conn, ERR_INTERNAL, "Refuse to attach stream to non-open circ.");
  748. return 0;
  749. }
  750. if (connection_ap_handshake_attach_chosen_circuit(ap_conn, circ) != 1) {
  751. send_control_error(conn, ERR_INTERNAL, "Unable to attach stream.");
  752. return 0;
  753. }
  754. send_control_done(conn);
  755. return 0;
  756. }
  757. static int
  758. handle_control_postdescriptor(connection_t *conn, uint32_t len,
  759. const char *body)
  760. {
  761. const char *msg;
  762. switch (router_load_single_router(body, &msg)) {
  763. case -1:
  764. send_control_error(conn,ERR_SYNTAX,msg?msg: "Could not parse descriptor");
  765. break;
  766. case 0:
  767. send_control_done2(conn,msg?msg: "Descriptor not added",0);
  768. break;
  769. case 1:
  770. send_control_done(conn);
  771. break;
  772. }
  773. return 0;
  774. }
  775. static int
  776. handle_control_redirectstream(connection_t *conn, uint32_t len,
  777. const char *body)
  778. {
  779. connection_t *ap_conn;
  780. uint32_t conn_id;
  781. if (len < 6) {
  782. send_control_error(conn, ERR_SYNTAX, "redirectstream message too short");
  783. return 0;
  784. }
  785. conn_id = ntohl(get_uint32(body));
  786. if (!(ap_conn = connection_get_by_global_id(conn_id))
  787. || ap_conn->state != CONN_TYPE_AP
  788. || !ap_conn->socks_request) {
  789. send_control_error(conn, ERR_NO_STREAM,
  790. "No AP connection found with given ID");
  791. return 0;
  792. }
  793. strlcpy(ap_conn->socks_request->address, body+4,
  794. sizeof(ap_conn->socks_request->address));
  795. send_control_done(conn);
  796. return 0;
  797. }
  798. static int
  799. handle_control_closestream(connection_t *conn, uint32_t len,
  800. const char *body)
  801. {
  802. uint32_t conn_id;
  803. connection_t *ap_conn;
  804. uint8_t reason;
  805. if (len < 6) {
  806. send_control_error(conn, ERR_SYNTAX, "closestream message too short");
  807. return 0;
  808. }
  809. conn_id = ntohl(get_uint32(body));
  810. reason = *(uint8_t*)(body+4);
  811. if (!(ap_conn = connection_get_by_global_id(conn_id))
  812. || ap_conn->state != CONN_TYPE_AP
  813. || !ap_conn->socks_request) {
  814. send_control_error(conn, ERR_NO_STREAM,
  815. "No AP connection found with given ID");
  816. return 0;
  817. }
  818. connection_mark_unattached_ap(ap_conn, reason);
  819. send_control_done(conn);
  820. return 0;
  821. }
  822. static int
  823. handle_control_closecircuit(connection_t *conn, uint32_t len,
  824. const char *body)
  825. {
  826. uint32_t circ_id;
  827. circuit_t *circ;
  828. int safe;
  829. if (len < 5) {
  830. send_control_error(conn, ERR_SYNTAX, "closecircuit message too short");
  831. return 0;
  832. }
  833. circ_id = ntohl(get_uint32(body));
  834. safe = (*(uint8_t*)(body+4)) & 1;
  835. if (!(circ = circuit_get_by_global_id(circ_id))) {
  836. send_control_error(conn, ERR_NO_CIRC,
  837. "No circuit found with given ID");
  838. return 0;
  839. }
  840. if (!safe || !circ->p_streams) {
  841. circuit_mark_for_close(circ);
  842. }
  843. send_control_done(conn);
  844. return 0;
  845. }
  846. /** Called when <b>conn</b> has no more bytes left on its outbuf. */
  847. int
  848. connection_control_finished_flushing(connection_t *conn) {
  849. tor_assert(conn);
  850. tor_assert(conn->type == CONN_TYPE_CONTROL);
  851. connection_stop_writing(conn);
  852. return 0;
  853. }
  854. /** Called when <b>conn</b> has gotten its socket closed. */
  855. int connection_control_reached_eof(connection_t *conn) {
  856. log_fn(LOG_INFO,"Control connection reached EOF. Closing.");
  857. connection_mark_for_close(conn);
  858. return 0;
  859. }
  860. /** Called when <b>conn</b> has received more bytes on its inbuf.
  861. */
  862. int
  863. connection_control_process_inbuf(connection_t *conn) {
  864. uint32_t body_len;
  865. uint16_t command_type;
  866. char *body;
  867. tor_assert(conn);
  868. tor_assert(conn->type == CONN_TYPE_CONTROL);
  869. again:
  870. /* Try to suck a control message from the buffer. */
  871. switch (fetch_from_buf_control(conn->inbuf, &body_len, &command_type, &body))
  872. {
  873. case -1:
  874. tor_free(body);
  875. log_fn(LOG_WARN, "Error in control command. Failing.");
  876. return -1;
  877. case 0:
  878. /* Control command not all here yet. Wait. */
  879. return 0;
  880. case 1:
  881. /* We got a command. Process it. */
  882. break;
  883. default:
  884. tor_assert(0);
  885. }
  886. /* We got a command. If we need authentication, only authentication
  887. * commands will be considered. */
  888. if (conn->state == CONTROL_CONN_STATE_NEEDAUTH &&
  889. command_type != CONTROL_CMD_AUTHENTICATE) {
  890. log_fn(LOG_WARN, "Rejecting '%s' command; authentication needed.",
  891. control_cmd_to_string(command_type));
  892. send_control_error(conn, ERR_UNAUTHORIZED, "Authentication required");
  893. tor_free(body);
  894. goto again;
  895. }
  896. /* Okay, we're willing to process the command. */
  897. switch (command_type)
  898. {
  899. case CONTROL_CMD_SETCONF:
  900. if (handle_control_setconf(conn, body_len, body))
  901. return -1;
  902. break;
  903. case CONTROL_CMD_GETCONF:
  904. if (handle_control_getconf(conn, body_len, body))
  905. return -1;
  906. break;
  907. case CONTROL_CMD_SETEVENTS:
  908. if (handle_control_setevents(conn, body_len, body))
  909. return -1;
  910. break;
  911. case CONTROL_CMD_AUTHENTICATE:
  912. if (handle_control_authenticate(conn, body_len, body))
  913. return -1;
  914. break;
  915. case CONTROL_CMD_SAVECONF:
  916. if (handle_control_saveconf(conn, body_len, body))
  917. return -1;
  918. break;
  919. case CONTROL_CMD_SIGNAL:
  920. if (handle_control_signal(conn, body_len, body))
  921. return -1;
  922. break;
  923. case CONTROL_CMD_MAPADDRESS:
  924. if (handle_control_mapaddress(conn, body_len, body))
  925. return -1;
  926. break;
  927. case CONTROL_CMD_GETINFO:
  928. if (handle_control_getinfo(conn, body_len, body))
  929. return -1;
  930. break;
  931. case CONTROL_CMD_EXTENDCIRCUIT:
  932. if (handle_control_extendcircuit(conn, body_len, body))
  933. return -1;
  934. break;
  935. case CONTROL_CMD_ATTACHSTREAM:
  936. if (handle_control_attachstream(conn, body_len, body))
  937. return -1;
  938. break;
  939. case CONTROL_CMD_POSTDESCRIPTOR:
  940. if (handle_control_postdescriptor(conn, body_len, body))
  941. return -1;
  942. break;
  943. case CONTROL_CMD_REDIRECTSTREAM:
  944. if (handle_control_redirectstream(conn, body_len, body))
  945. return -1;
  946. break;
  947. case CONTROL_CMD_CLOSESTREAM:
  948. if (handle_control_closestream(conn, body_len, body))
  949. return -1;
  950. break;
  951. case CONTROL_CMD_CLOSECIRCUIT:
  952. if (handle_control_closecircuit(conn, body_len, body))
  953. return -1;
  954. break;
  955. case CONTROL_CMD_ERROR:
  956. case CONTROL_CMD_DONE:
  957. case CONTROL_CMD_CONFVALUE:
  958. case CONTROL_CMD_EVENT:
  959. case CONTROL_CMD_INFOVALUE:
  960. log_fn(LOG_WARN, "Received client-only '%s' command; ignoring.",
  961. control_cmd_to_string(command_type));
  962. send_control_error(conn, ERR_UNRECOGNIZED_TYPE,
  963. "Command type only valid from server to tor client");
  964. break;
  965. case CONTROL_CMD_FRAGMENTHEADER:
  966. case CONTROL_CMD_FRAGMENT:
  967. log_fn(LOG_WARN, "Recieved command fragment out of order; ignoring.");
  968. send_control_error(conn, ERR_SYNTAX, "Bad fragmentation on command.");
  969. default:
  970. log_fn(LOG_WARN, "Received unrecognized command type %d; ignoring.",
  971. (int)command_type);
  972. send_control_error(conn, ERR_UNRECOGNIZED_TYPE,
  973. "Unrecognized command type");
  974. break;
  975. }
  976. tor_free(body);
  977. goto again; /* There might be more data. */
  978. }
  979. /** Something has happened to circuit <b>circ</b>: tell any interested
  980. * control connections. */
  981. int
  982. control_event_circuit_status(circuit_t *circ, circuit_status_event_t tp)
  983. {
  984. char *path, *msg;
  985. size_t path_len;
  986. if (!EVENT_IS_INTERESTING(EVENT_CIRCUIT_STATUS))
  987. return 0;
  988. tor_assert(circ);
  989. tor_assert(CIRCUIT_IS_ORIGIN(circ));
  990. path = circuit_list_path(circ,0);
  991. path_len = strlen(path);
  992. msg = tor_malloc(1+4+path_len+1); /* event, circid, path, NUL. */
  993. msg[0] = (uint8_t) tp;
  994. set_uint32(msg+1, htonl(circ->global_identifier));
  995. strlcpy(msg+5,path,path_len+1);
  996. send_control_event(EVENT_CIRCUIT_STATUS, (uint32_t)(path_len+6), msg);
  997. tor_free(path);
  998. tor_free(msg);
  999. return 0;
  1000. }
  1001. /** Something has happened to the stream associated with AP connection
  1002. * <b>conn</b>: tell any interested control connections. */
  1003. int
  1004. control_event_stream_status(connection_t *conn, stream_status_event_t tp)
  1005. {
  1006. char *msg;
  1007. size_t len;
  1008. char buf[256], buf2[256];
  1009. tor_assert(conn->type == CONN_TYPE_AP);
  1010. tor_assert(conn->socks_request);
  1011. if (!EVENT_IS_INTERESTING(EVENT_STREAM_STATUS))
  1012. return 0;
  1013. if (conn->chosen_exit_name)
  1014. tor_snprintf(buf2, sizeof(buf2), ".%s.exit", conn->chosen_exit_name);
  1015. tor_snprintf(buf, sizeof(buf), "%s%s:%d",
  1016. conn->socks_request->address,
  1017. conn->chosen_exit_name ? buf2 : "",
  1018. conn->socks_request->port),
  1019. len = strlen(buf);
  1020. msg = tor_malloc(5+len+1);
  1021. msg[0] = (uint8_t) tp;
  1022. set_uint32(msg+1, htonl(conn->global_identifier));
  1023. strlcpy(msg+5, buf, len+1);
  1024. send_control_event(EVENT_STREAM_STATUS, (uint32_t)(5+len+1), msg);
  1025. tor_free(msg);
  1026. return 0;
  1027. }
  1028. /** Something has happened to the OR connection <b>conn</b>: tell any
  1029. * interested control connections. */
  1030. int
  1031. control_event_or_conn_status(connection_t *conn,or_conn_status_event_t tp)
  1032. {
  1033. char buf[HEX_DIGEST_LEN+3]; /* status, dollar, identity, NUL */
  1034. size_t len;
  1035. tor_assert(conn->type == CONN_TYPE_OR);
  1036. if (!EVENT_IS_INTERESTING(EVENT_OR_CONN_STATUS))
  1037. return 0;
  1038. buf[0] = (uint8_t)tp;
  1039. strlcpy(buf+1,conn->nickname,sizeof(buf)-1);
  1040. len = strlen(buf+1);
  1041. send_control_event(EVENT_OR_CONN_STATUS, (uint32_t)(len+1), buf);
  1042. return 0;
  1043. }
  1044. /** A second or more has elapsed: tell any interested control
  1045. * connections how much bandwidth we used. */
  1046. int
  1047. control_event_bandwidth_used(uint32_t n_read, uint32_t n_written)
  1048. {
  1049. char buf[8];
  1050. if (!EVENT_IS_INTERESTING(EVENT_BANDWIDTH_USED))
  1051. return 0;
  1052. set_uint32(buf, htonl(n_read));
  1053. set_uint32(buf+4, htonl(n_written));
  1054. send_control_event(EVENT_BANDWIDTH_USED, 8, buf);
  1055. return 0;
  1056. }
  1057. /** We got a log message: tell any interested control connections. */
  1058. void
  1059. control_event_logmsg(int severity, const char *msg)
  1060. {
  1061. static int sending_logmsg=0;
  1062. int oldlog, event;
  1063. if (sending_logmsg)
  1064. return;
  1065. oldlog = EVENT_IS_INTERESTING(EVENT_LOG_OBSOLETE) &&
  1066. (severity == LOG_NOTICE || severity == LOG_WARN || severity == LOG_ERR);
  1067. event = log_severity_to_event(severity);
  1068. if (event<0 || !EVENT_IS_INTERESTING(event))
  1069. event = 0;
  1070. if (oldlog || event) {
  1071. size_t len = strlen(msg);
  1072. sending_logmsg = 1;
  1073. if (event)
  1074. send_control_event(event, (uint32_t)(len+1), msg);
  1075. if (oldlog)
  1076. send_control_event(EVENT_LOG_OBSOLETE, (uint32_t)(len+1), msg);
  1077. sending_logmsg = 0;
  1078. }
  1079. }
  1080. /** Called whenever we receive new router descriptors: tell any
  1081. * interested control connections. <b>routers</b> is a list of
  1082. * DIGEST_LEN-byte identity digests.
  1083. */
  1084. int control_event_descriptors_changed(smartlist_t *routers)
  1085. {
  1086. size_t len;
  1087. char *msg;
  1088. smartlist_t *identities;
  1089. char buf[HEX_DIGEST_LEN+1];
  1090. if (!EVENT_IS_INTERESTING(EVENT_NEW_DESC))
  1091. return 0;
  1092. identities = smartlist_create();
  1093. SMARTLIST_FOREACH(routers, routerinfo_t *, r,
  1094. {
  1095. base16_encode(buf,sizeof(buf),r->identity_digest,DIGEST_LEN);
  1096. smartlist_add(identities, tor_strdup(buf));
  1097. });
  1098. msg = smartlist_join_strings(identities, ",", 1, &len);
  1099. send_control_event(EVENT_NEW_DESC, len+1, msg);
  1100. SMARTLIST_FOREACH(identities, char *, cp, tor_free(cp));
  1101. smartlist_free(identities);
  1102. tor_free(msg);
  1103. return 0;
  1104. }
  1105. /** Choose a random authentication cookie and write it to disk.
  1106. * Anybody who can read the cookie from disk will be considered
  1107. * authorized to use the control connection. */
  1108. int
  1109. init_cookie_authentication(int enabled)
  1110. {
  1111. char fname[512];
  1112. if (!enabled) {
  1113. authentication_cookie_is_set = 0;
  1114. return 0;
  1115. }
  1116. tor_snprintf(fname, sizeof(fname), "%s/control_auth_cookie",
  1117. get_options()->DataDirectory);
  1118. crypto_rand(authentication_cookie, AUTHENTICATION_COOKIE_LEN);
  1119. authentication_cookie_is_set = 1;
  1120. if (write_bytes_to_file(fname, authentication_cookie,
  1121. AUTHENTICATION_COOKIE_LEN, 1)) {
  1122. log_fn(LOG_WARN,"Error writing authentication cookie.");
  1123. return -1;
  1124. }
  1125. return 0;
  1126. }